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