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