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