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