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