glx: fix shared memory leak in X11
[mesa.git] / src / glx / drisw_glx.c
1 /*
2 * Copyright 2008 George Sapountzis
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
25
26 #include <X11/Xlib.h>
27 #include "glxclient.h"
28 #include <dlfcn.h>
29 #include "dri_common.h"
30 #include "drisw_priv.h"
31 #include <X11/extensions/shmproto.h>
32 #include <assert.h>
33
34 static Bool
35 XCreateGCs(struct drisw_drawable * pdp,
36 Display * dpy, XID drawable, int visualid)
37 {
38 XGCValues gcvalues;
39 long visMask;
40 XVisualInfo visTemp;
41 int num_visuals;
42
43 /* create GC's */
44 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
45 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
46
47 gcvalues.function = GXcopy;
48 gcvalues.graphics_exposures = False;
49 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
50 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
51 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
52
53 /* visual */
54 visTemp.visualid = visualid;
55 visMask = VisualIDMask;
56 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
57
58 if (!pdp->visinfo || num_visuals == 0)
59 return False;
60
61 return True;
62 }
63
64 static int xshm_error = 0;
65 static int xshm_opcode = -1;
66
67 /**
68 * Catches potential Xlib errors.
69 */
70 static int
71 handle_xerror(Display *dpy, XErrorEvent *event)
72 {
73 (void) dpy;
74
75 assert(xshm_opcode != -1);
76 if (event->request_code != xshm_opcode ||
77 event->minor_code != X_ShmAttach)
78 return 0;
79
80 xshm_error = 1;
81 return 0;
82 }
83
84 static Bool
85 XCreateDrawable(struct drisw_drawable * pdp, int shmid, Display * dpy)
86 {
87 if (pdp->ximage) {
88 XDestroyImage(pdp->ximage);
89 pdp->ximage = NULL;
90 }
91
92 if (!xshm_error && shmid >= 0) {
93 pdp->shminfo.shmid = shmid;
94 pdp->ximage = XShmCreateImage(dpy,
95 pdp->visinfo->visual,
96 pdp->visinfo->depth,
97 ZPixmap, /* format */
98 NULL, /* data */
99 &pdp->shminfo, /* shminfo */
100 0, 0); /* width, height */
101 if (pdp->ximage != NULL) {
102 int (*old_handler)(Display *, XErrorEvent *);
103
104 /* dispatch pending errors */
105 XSync(dpy, False);
106
107 old_handler = XSetErrorHandler(handle_xerror);
108 /* This may trigger the X protocol error we're ready to catch: */
109 XShmAttach(dpy, &pdp->shminfo);
110 XSync(dpy, False);
111
112 if (xshm_error) {
113 /* we are on a remote display, this error is normal, don't print it */
114 XDestroyImage(pdp->ximage);
115 pdp->ximage = NULL;
116 }
117
118 (void) XSetErrorHandler(old_handler);
119 }
120 }
121
122 if (pdp->ximage == NULL) {
123 pdp->shminfo.shmid = -1;
124 pdp->ximage = XCreateImage(dpy,
125 pdp->visinfo->visual,
126 pdp->visinfo->depth,
127 ZPixmap, 0, /* format, offset */
128 NULL, /* data */
129 0, 0, /* width, height */
130 32, /* bitmap_pad */
131 0); /* bytes_per_line */
132 }
133
134 /**
135 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
136 * the conversion for us.
137 */
138 if (pdp->ximage->bits_per_pixel == 24)
139 pdp->ximage->bits_per_pixel = 32;
140
141 return True;
142 }
143
144 static void
145 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
146 {
147 if (pdp->ximage)
148 XDestroyImage(pdp->ximage);
149
150 if (pdp->shminfo.shmid > 0)
151 XShmDetach(dpy, &pdp->shminfo);
152
153 free(pdp->visinfo);
154
155 XFreeGC(dpy, pdp->gc);
156 XFreeGC(dpy, pdp->swapgc);
157 }
158
159 /**
160 * swrast loader functions
161 */
162
163 static void
164 swrastGetDrawableInfo(__DRIdrawable * draw,
165 int *x, int *y, int *w, int *h,
166 void *loaderPrivate)
167 {
168 struct drisw_drawable *pdp = loaderPrivate;
169 __GLXDRIdrawable *pdraw = &(pdp->base);
170 Display *dpy = pdraw->psc->dpy;
171 Drawable drawable;
172
173 Window root;
174 unsigned uw, uh, bw, depth;
175
176 drawable = pdraw->xDrawable;
177
178 XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
179 *w = uw;
180 *h = uh;
181 }
182
183 /**
184 * Align renderbuffer pitch.
185 *
186 * This should be chosen by the driver and the loader (libGL, xserver/glx)
187 * should use the driver provided pitch.
188 *
189 * It seems that the xorg loader (that is the xserver loading swrast_dri for
190 * indirect rendering, not client-side libGL) requires that the pitch is
191 * exactly the image width padded to 32 bits. XXX
192 *
193 * The above restriction can probably be overcome by using ScratchPixmap and
194 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
195 * the scratch pixmap to 'pitch / cpp'.
196 */
197 static inline int
198 bytes_per_line(unsigned pitch_bits, unsigned mul)
199 {
200 unsigned mask = mul - 1;
201
202 return ((pitch_bits + mask) & ~mask) / 8;
203 }
204
205 static void
206 swrastXPutImage(__DRIdrawable * draw, int op,
207 int srcx, int srcy, int x, int y,
208 int w, int h, int stride,
209 int shmid, char *data, void *loaderPrivate)
210 {
211 struct drisw_drawable *pdp = loaderPrivate;
212 __GLXDRIdrawable *pdraw = &(pdp->base);
213 Display *dpy = pdraw->psc->dpy;
214 Drawable drawable;
215 XImage *ximage;
216 GC gc;
217
218 if (!pdp->ximage || shmid != pdp->shminfo.shmid) {
219 if (!XCreateDrawable(pdp, shmid, dpy))
220 return;
221 }
222
223 switch (op) {
224 case __DRI_SWRAST_IMAGE_OP_DRAW:
225 gc = pdp->gc;
226 break;
227 case __DRI_SWRAST_IMAGE_OP_SWAP:
228 gc = pdp->swapgc;
229 break;
230 default:
231 return;
232 }
233
234 drawable = pdraw->xDrawable;
235 ximage = pdp->ximage;
236 ximage->bytes_per_line = stride ? stride : bytes_per_line(w * ximage->bits_per_pixel, 32);
237 ximage->data = data;
238
239 if (pdp->shminfo.shmid >= 0) {
240 ximage->width = ximage->bytes_per_line / ((ximage->bits_per_pixel + 7)/ 8);
241 ximage->height = h;
242 XShmPutImage(dpy, drawable, gc, ximage, srcx, srcy, x, y, w, h, False);
243 XSync(dpy, False);
244 } else {
245 ximage->width = w;
246 ximage->height = h;
247 XPutImage(dpy, drawable, gc, ximage, srcx, srcy, x, y, w, h);
248 }
249 ximage->data = NULL;
250 }
251
252 static void
253 swrastPutImageShm(__DRIdrawable * draw, int op,
254 int x, int y, int w, int h, int stride,
255 int shmid, char *shmaddr, unsigned offset,
256 void *loaderPrivate)
257 {
258 struct drisw_drawable *pdp = loaderPrivate;
259
260 pdp->shminfo.shmaddr = shmaddr;
261 swrastXPutImage(draw, op, 0, 0, x, y, w, h, stride, shmid,
262 shmaddr + offset, loaderPrivate);
263 }
264
265 static void
266 swrastPutImageShm2(__DRIdrawable * draw, int op,
267 int x, int y,
268 int w, int h, int stride,
269 int shmid, char *shmaddr, unsigned offset,
270 void *loaderPrivate)
271 {
272 struct drisw_drawable *pdp = loaderPrivate;
273
274 pdp->shminfo.shmaddr = shmaddr;
275 swrastXPutImage(draw, op, x, 0, x, y, w, h, stride, shmid,
276 shmaddr + offset, loaderPrivate);
277 }
278
279 static void
280 swrastPutImage2(__DRIdrawable * draw, int op,
281 int x, int y, int w, int h, int stride,
282 char *data, void *loaderPrivate)
283 {
284 swrastXPutImage(draw, op, 0, 0, x, y, w, h, stride, -1,
285 data, loaderPrivate);
286 }
287
288 static void
289 swrastPutImage(__DRIdrawable * draw, int op,
290 int x, int y, int w, int h,
291 char *data, void *loaderPrivate)
292 {
293 swrastXPutImage(draw, op, 0, 0, x, y, w, h, 0, -1,
294 data, loaderPrivate);
295 }
296
297 static void
298 swrastGetImage2(__DRIdrawable * read,
299 int x, int y, int w, int h, int stride,
300 char *data, void *loaderPrivate)
301 {
302 struct drisw_drawable *prp = loaderPrivate;
303 __GLXDRIdrawable *pread = &(prp->base);
304 Display *dpy = pread->psc->dpy;
305 Drawable readable;
306 XImage *ximage;
307
308 if (!prp->ximage || prp->shminfo.shmid >= 0) {
309 if (!XCreateDrawable(prp, -1, dpy))
310 return;
311 }
312
313 readable = pread->xDrawable;
314
315 ximage = prp->ximage;
316 ximage->data = data;
317 ximage->width = w;
318 ximage->height = h;
319 ximage->bytes_per_line = stride ? stride : bytes_per_line(w * ximage->bits_per_pixel, 32);
320
321 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
322
323 ximage->data = NULL;
324 }
325
326 static void
327 swrastGetImage(__DRIdrawable * read,
328 int x, int y, int w, int h,
329 char *data, void *loaderPrivate)
330 {
331 swrastGetImage2(read, x, y, w, h, 0, data, loaderPrivate);
332 }
333
334 static void
335 swrastGetImageShm(__DRIdrawable * read,
336 int x, int y, int w, int h,
337 int shmid, void *loaderPrivate)
338 {
339 struct drisw_drawable *prp = loaderPrivate;
340 __GLXDRIdrawable *pread = &(prp->base);
341 Display *dpy = pread->psc->dpy;
342 Drawable readable;
343 XImage *ximage;
344
345 if (!prp->ximage || shmid != prp->shminfo.shmid) {
346 if (!XCreateDrawable(prp, shmid, dpy))
347 return;
348 }
349 readable = pread->xDrawable;
350
351 ximage = prp->ximage;
352 ximage->data = prp->shminfo.shmaddr; /* no offset */
353 ximage->width = w;
354 ximage->height = h;
355 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
356
357 XShmGetImage(dpy, readable, ximage, x, y, ~0L);
358 }
359
360 static const __DRIswrastLoaderExtension swrastLoaderExtension_shm = {
361 .base = {__DRI_SWRAST_LOADER, 5 },
362
363 .getDrawableInfo = swrastGetDrawableInfo,
364 .putImage = swrastPutImage,
365 .getImage = swrastGetImage,
366 .putImage2 = swrastPutImage2,
367 .getImage2 = swrastGetImage2,
368 .putImageShm = swrastPutImageShm,
369 .getImageShm = swrastGetImageShm,
370 .putImageShm2 = swrastPutImageShm2,
371 };
372
373 static const __DRIextension *loader_extensions_shm[] = {
374 &swrastLoaderExtension_shm.base,
375 NULL
376 };
377
378 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
379 .base = {__DRI_SWRAST_LOADER, 3 },
380
381 .getDrawableInfo = swrastGetDrawableInfo,
382 .putImage = swrastPutImage,
383 .getImage = swrastGetImage,
384 .putImage2 = swrastPutImage2,
385 .getImage2 = swrastGetImage2,
386 };
387
388 static const __DRIextension *loader_extensions_noshm[] = {
389 &swrastLoaderExtension.base,
390 NULL
391 };
392
393 /**
394 * GLXDRI functions
395 */
396
397 static void
398 drisw_destroy_context(struct glx_context *context)
399 {
400 struct drisw_context *pcp = (struct drisw_context *) context;
401 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
402
403 driReleaseDrawables(&pcp->base);
404
405 free((char *) context->extensions);
406
407 (*psc->core->destroyContext) (pcp->driContext);
408
409 free(pcp);
410 }
411
412 static int
413 drisw_bind_context(struct glx_context *context, struct glx_context *old,
414 GLXDrawable draw, GLXDrawable read)
415 {
416 struct drisw_context *pcp = (struct drisw_context *) context;
417 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
418 struct drisw_drawable *pdraw, *pread;
419
420 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
421 pread = (struct drisw_drawable *) driFetchDrawable(context, read);
422
423 driReleaseDrawables(&pcp->base);
424
425 if ((*psc->core->bindContext) (pcp->driContext,
426 pdraw ? pdraw->driDrawable : NULL,
427 pread ? pread->driDrawable : NULL))
428 return Success;
429
430 return GLXBadContext;
431 }
432
433 static void
434 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
435 {
436 struct drisw_context *pcp = (struct drisw_context *) context;
437 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
438
439 (*psc->core->unbindContext) (pcp->driContext);
440 }
441
442 static void
443 drisw_bind_tex_image(Display * dpy,
444 GLXDrawable drawable,
445 int buffer, const int *attrib_list)
446 {
447 struct glx_context *gc = __glXGetCurrentContext();
448 struct drisw_context *pcp = (struct drisw_context *) gc;
449 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
450 struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
451 struct drisw_screen *psc;
452
453 __glXInitialize(dpy);
454
455 if (pdraw != NULL) {
456 psc = (struct drisw_screen *) base->psc;
457
458 if (!psc->texBuffer)
459 return;
460
461 if (psc->texBuffer->base.version >= 2 &&
462 psc->texBuffer->setTexBuffer2 != NULL) {
463 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
464 pdraw->base.textureTarget,
465 pdraw->base.textureFormat,
466 pdraw->driDrawable);
467 }
468 else {
469 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
470 pdraw->base.textureTarget,
471 pdraw->driDrawable);
472 }
473 }
474 }
475
476 static void
477 drisw_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
478 {
479 struct glx_context *gc = __glXGetCurrentContext();
480 struct drisw_context *pcp = (struct drisw_context *) gc;
481 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
482 struct glx_display *dpyPriv = __glXInitialize(dpy);
483 struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
484 struct drisw_screen *psc;
485
486 if (dpyPriv != NULL && pdraw != NULL) {
487 psc = (struct drisw_screen *) base->psc;
488
489 if (!psc->texBuffer)
490 return;
491
492 if (psc->texBuffer->base.version >= 3 &&
493 psc->texBuffer->releaseTexBuffer != NULL) {
494 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
495 pdraw->base.textureTarget,
496 pdraw->driDrawable);
497 }
498 }
499 }
500
501 static const struct glx_context_vtable drisw_context_vtable = {
502 .destroy = drisw_destroy_context,
503 .bind = drisw_bind_context,
504 .unbind = drisw_unbind_context,
505 .wait_gl = NULL,
506 .wait_x = NULL,
507 .use_x_font = DRI_glXUseXFont,
508 .bind_tex_image = drisw_bind_tex_image,
509 .release_tex_image = drisw_release_tex_image,
510 .get_proc_address = NULL,
511 };
512
513 static struct glx_context *
514 drisw_create_context(struct glx_screen *base,
515 struct glx_config *config_base,
516 struct glx_context *shareList, int renderType)
517 {
518 struct drisw_context *pcp, *pcp_shared;
519 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
520 struct drisw_screen *psc = (struct drisw_screen *) base;
521 __DRIcontext *shared = NULL;
522
523 if (!psc->base.driScreen)
524 return NULL;
525
526 /* Check the renderType value */
527 if (!validate_renderType_against_config(config_base, renderType))
528 return NULL;
529
530 if (shareList) {
531 /* If the shareList context is not a DRISW context, we cannot possibly
532 * create a DRISW context that shares it.
533 */
534 if (shareList->vtable->destroy != drisw_destroy_context) {
535 return NULL;
536 }
537
538 pcp_shared = (struct drisw_context *) shareList;
539 shared = pcp_shared->driContext;
540 }
541
542 pcp = calloc(1, sizeof *pcp);
543 if (pcp == NULL)
544 return NULL;
545
546 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
547 free(pcp);
548 return NULL;
549 }
550
551 pcp->base.renderType = renderType;
552
553 pcp->driContext =
554 (*psc->core->createNewContext) (psc->driScreen,
555 config->driConfig, shared, pcp);
556 if (pcp->driContext == NULL) {
557 free(pcp);
558 return NULL;
559 }
560
561 pcp->base.vtable = &drisw_context_vtable;
562
563 return &pcp->base;
564 }
565
566 static struct glx_context *
567 drisw_create_context_attribs(struct glx_screen *base,
568 struct glx_config *config_base,
569 struct glx_context *shareList,
570 unsigned num_attribs,
571 const uint32_t *attribs,
572 unsigned *error)
573 {
574 struct drisw_context *pcp, *pcp_shared;
575 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
576 struct drisw_screen *psc = (struct drisw_screen *) base;
577 __DRIcontext *shared = NULL;
578
579 uint32_t minor_ver;
580 uint32_t major_ver;
581 uint32_t renderType;
582 uint32_t flags;
583 unsigned api;
584 int reset;
585 int release;
586 uint32_t ctx_attribs[2 * 5];
587 unsigned num_ctx_attribs = 0;
588
589 if (!psc->base.driScreen)
590 return NULL;
591
592 if (psc->swrast->base.version < 3)
593 return NULL;
594
595 /* Remap the GLX tokens to DRI2 tokens.
596 */
597 if (!dri2_convert_glx_attribs(num_attribs, attribs,
598 &major_ver, &minor_ver, &renderType, &flags,
599 &api, &reset, &release, error))
600 return NULL;
601
602 /* Check the renderType value */
603 if (!validate_renderType_against_config(config_base, renderType)) {
604 return NULL;
605 }
606
607 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION)
608 return NULL;
609
610 if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH &&
611 release != __DRI_CTX_RELEASE_BEHAVIOR_NONE)
612 return NULL;
613
614 if (shareList) {
615 pcp_shared = (struct drisw_context *) shareList;
616 shared = pcp_shared->driContext;
617 }
618
619 pcp = calloc(1, sizeof *pcp);
620 if (pcp == NULL)
621 return NULL;
622
623 if (!glx_context_init(&pcp->base, &psc->base, config_base)) {
624 free(pcp);
625 return NULL;
626 }
627
628 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
629 ctx_attribs[num_ctx_attribs++] = major_ver;
630 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
631 ctx_attribs[num_ctx_attribs++] = minor_ver;
632 if (release != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
633 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
634 ctx_attribs[num_ctx_attribs++] = release;
635 }
636
637 if (flags != 0) {
638 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
639
640 /* The current __DRI_CTX_FLAG_* values are identical to the
641 * GLX_CONTEXT_*_BIT values.
642 */
643 ctx_attribs[num_ctx_attribs++] = flags;
644 }
645
646 pcp->base.renderType = renderType;
647
648 pcp->driContext =
649 (*psc->swrast->createContextAttribs) (psc->driScreen,
650 api,
651 config ? config->driConfig : 0,
652 shared,
653 num_ctx_attribs / 2,
654 ctx_attribs,
655 error,
656 pcp);
657 if (pcp->driContext == NULL) {
658 free(pcp);
659 return NULL;
660 }
661
662 pcp->base.vtable = &drisw_context_vtable;
663
664 return &pcp->base;
665 }
666
667 static void
668 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
669 {
670 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
671 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
672
673 (*psc->core->destroyDrawable) (pdp->driDrawable);
674
675 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
676 free(pdp);
677 }
678
679 static __GLXDRIdrawable *
680 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
681 GLXDrawable drawable, struct glx_config *modes)
682 {
683 struct drisw_drawable *pdp;
684 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
685 struct drisw_screen *psc = (struct drisw_screen *) base;
686 Bool ret;
687 const __DRIswrastExtension *swrast = psc->swrast;
688
689 pdp = calloc(1, sizeof(*pdp));
690 if (!pdp)
691 return NULL;
692
693 pdp->base.xDrawable = xDrawable;
694 pdp->base.drawable = drawable;
695 pdp->base.psc = &psc->base;
696
697 ret = XCreateGCs(pdp, psc->base.dpy, xDrawable, modes->visualID);
698 if (!ret) {
699 free(pdp);
700 return NULL;
701 }
702
703 /* Create a new drawable */
704 pdp->driDrawable =
705 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
706
707 if (!pdp->driDrawable) {
708 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
709 free(pdp);
710 return NULL;
711 }
712
713 pdp->base.destroyDrawable = driswDestroyDrawable;
714
715 return &pdp->base;
716 }
717
718 static int64_t
719 driswSwapBuffers(__GLXDRIdrawable * pdraw,
720 int64_t target_msc, int64_t divisor, int64_t remainder,
721 Bool flush)
722 {
723 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
724 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
725
726 (void) target_msc;
727 (void) divisor;
728 (void) remainder;
729
730 if (flush) {
731 glFlush();
732 }
733
734 (*psc->core->swapBuffers) (pdp->driDrawable);
735
736 return 0;
737 }
738
739 static void
740 driswCopySubBuffer(__GLXDRIdrawable * pdraw,
741 int x, int y, int width, int height, Bool flush)
742 {
743 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
744 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
745
746 if (flush) {
747 glFlush();
748 }
749
750 (*psc->copySubBuffer->copySubBuffer) (pdp->driDrawable,
751 x, y, width, height);
752 }
753
754 static void
755 driswDestroyScreen(struct glx_screen *base)
756 {
757 struct drisw_screen *psc = (struct drisw_screen *) base;
758
759 /* Free the direct rendering per screen data */
760 (*psc->core->destroyScreen) (psc->driScreen);
761 driDestroyConfigs(psc->driver_configs);
762 psc->driScreen = NULL;
763 if (psc->driver)
764 dlclose(psc->driver);
765 free(psc);
766 }
767
768 #define SWRAST_DRIVER_NAME "swrast"
769
770 static const struct glx_screen_vtable drisw_screen_vtable = {
771 .create_context = drisw_create_context,
772 .create_context_attribs = drisw_create_context_attribs,
773 .query_renderer_integer = drisw_query_renderer_integer,
774 .query_renderer_string = drisw_query_renderer_string,
775 };
776
777 static void
778 driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
779 {
780 int i;
781
782 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
783
784 if (psc->swrast->base.version >= 3) {
785 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
786 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
787
788 /* DRISW version >= 2 implies support for OpenGL ES.
789 */
790 __glXEnableDirectExtension(&psc->base,
791 "GLX_EXT_create_context_es_profile");
792 __glXEnableDirectExtension(&psc->base,
793 "GLX_EXT_create_context_es2_profile");
794 }
795
796 if (psc->copySubBuffer)
797 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
798
799 /* FIXME: Figure out what other extensions can be ported here from dri2. */
800 for (i = 0; extensions[i]; i++) {
801 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
802 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
803 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
804 }
805 /* DRISW version 3 is also required because GLX_MESA_query_renderer
806 * requires GLX_ARB_create_context_profile.
807 */
808 if (psc->swrast->base.version >= 3
809 && strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
810 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
811 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
812 }
813 if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0) {
814 __glXEnableDirectExtension(&psc->base,
815 "GLX_ARB_context_flush_control");
816 }
817 }
818 }
819
820 static int
821 check_xshm(Display *dpy)
822 {
823 int ignore;
824
825 return XQueryExtension(dpy, "MIT-SHM", &xshm_opcode, &ignore, &ignore);
826 }
827
828 static struct glx_screen *
829 driswCreateScreen(int screen, struct glx_display *priv)
830 {
831 __GLXDRIscreen *psp;
832 const __DRIconfig **driver_configs;
833 const __DRIextension **extensions;
834 struct drisw_screen *psc;
835 struct glx_config *configs = NULL, *visuals = NULL;
836 int i;
837 const __DRIextension **loader_extensions_local;
838
839 psc = calloc(1, sizeof *psc);
840 if (psc == NULL)
841 return NULL;
842
843 if (!glx_screen_init(&psc->base, screen, priv)) {
844 free(psc);
845 return NULL;
846 }
847
848 extensions = driOpenDriver(SWRAST_DRIVER_NAME, &psc->driver);
849 if (extensions == NULL)
850 goto handle_error;
851
852 if (!check_xshm(psc->base.dpy))
853 loader_extensions_local = loader_extensions_noshm;
854 else
855 loader_extensions_local = loader_extensions_shm;
856
857 for (i = 0; extensions[i]; i++) {
858 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
859 psc->core = (__DRIcoreExtension *) extensions[i];
860 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
861 psc->swrast = (__DRIswrastExtension *) extensions[i];
862 if (strcmp(extensions[i]->name, __DRI_COPY_SUB_BUFFER) == 0)
863 psc->copySubBuffer = (__DRIcopySubBufferExtension *) extensions[i];
864 }
865
866 if (psc->core == NULL || psc->swrast == NULL) {
867 ErrorMessageF("core dri extension not found\n");
868 goto handle_error;
869 }
870
871 if (psc->swrast->base.version >= 4) {
872 psc->driScreen =
873 psc->swrast->createNewScreen2(screen, loader_extensions_local,
874 extensions,
875 &driver_configs, psc);
876 } else {
877 psc->driScreen =
878 psc->swrast->createNewScreen(screen, loader_extensions_local,
879 &driver_configs, psc);
880 }
881 if (psc->driScreen == NULL) {
882 ErrorMessageF("failed to create dri screen\n");
883 goto handle_error;
884 }
885
886 extensions = psc->core->getExtensions(psc->driScreen);
887 driswBindExtensions(psc, extensions);
888
889 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
890 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
891
892 if (!configs || !visuals) {
893 ErrorMessageF("No matching fbConfigs or visuals found\n");
894 goto handle_error;
895 }
896
897 glx_config_destroy_list(psc->base.configs);
898 psc->base.configs = configs;
899 glx_config_destroy_list(psc->base.visuals);
900 psc->base.visuals = visuals;
901
902 psc->driver_configs = driver_configs;
903
904 psc->base.vtable = &drisw_screen_vtable;
905 psp = &psc->vtable;
906 psc->base.driScreen = psp;
907 psp->destroyScreen = driswDestroyScreen;
908 psp->createDrawable = driswCreateDrawable;
909 psp->swapBuffers = driswSwapBuffers;
910
911 if (psc->copySubBuffer)
912 psp->copySubBuffer = driswCopySubBuffer;
913
914 return &psc->base;
915
916 handle_error:
917 if (configs)
918 glx_config_destroy_list(configs);
919 if (visuals)
920 glx_config_destroy_list(visuals);
921 if (psc->driScreen)
922 psc->core->destroyScreen(psc->driScreen);
923 psc->driScreen = NULL;
924
925 if (psc->driver)
926 dlclose(psc->driver);
927 glx_screen_cleanup(&psc->base);
928 free(psc);
929
930 CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME);
931
932 return NULL;
933 }
934
935 /* Called from __glXFreeDisplayPrivate.
936 */
937 static void
938 driswDestroyDisplay(__GLXDRIdisplay * dpy)
939 {
940 free(dpy);
941 }
942
943 /*
944 * Allocate, initialize and return a __DRIdisplayPrivate object.
945 * This is called from __glXInitialize() when we are given a new
946 * display pointer.
947 */
948 _X_HIDDEN __GLXDRIdisplay *
949 driswCreateDisplay(Display * dpy)
950 {
951 struct drisw_display *pdpyp;
952
953 pdpyp = malloc(sizeof *pdpyp);
954 if (pdpyp == NULL)
955 return NULL;
956
957 pdpyp->base.destroyDisplay = driswDestroyDisplay;
958 pdpyp->base.createScreen = driswCreateScreen;
959
960 return &pdpyp->base;
961 }
962
963 #endif /* GLX_DIRECT_RENDERING */