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