3bf15322ff85e1397aa3174a308a870c50ec4756
[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->config->rgbBits,
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->config->rgbBits,
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
651 pdp = calloc(1, sizeof(*pdp));
652 if (!pdp)
653 return NULL;
654
655 pdp->base.xDrawable = xDrawable;
656 pdp->base.drawable = drawable;
657 pdp->base.psc = &psc->base;
658 pdp->config = modes;
659 pdp->gc = XCreateGC(psc->base.dpy, xDrawable, 0, NULL);
660
661 /* Create a new drawable */
662 pdp->driDrawable =
663 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
664
665 if (!pdp->driDrawable) {
666 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
667 free(pdp);
668 return NULL;
669 }
670
671 pdp->base.destroyDrawable = driswDestroyDrawable;
672
673 return &pdp->base;
674 }
675
676 static int64_t
677 driswSwapBuffers(__GLXDRIdrawable * pdraw,
678 int64_t target_msc, int64_t divisor, int64_t remainder,
679 Bool flush)
680 {
681 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
682 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
683
684 (void) target_msc;
685 (void) divisor;
686 (void) remainder;
687
688 if (flush) {
689 glFlush();
690 }
691
692 (*psc->core->swapBuffers) (pdp->driDrawable);
693
694 return 0;
695 }
696
697 static void
698 driswCopySubBuffer(__GLXDRIdrawable * pdraw,
699 int x, int y, int width, int height, Bool flush)
700 {
701 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
702 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
703
704 if (flush) {
705 glFlush();
706 }
707
708 (*psc->copySubBuffer->copySubBuffer) (pdp->driDrawable,
709 x, y, width, height);
710 }
711
712 static void
713 driswDestroyScreen(struct glx_screen *base)
714 {
715 struct drisw_screen *psc = (struct drisw_screen *) base;
716
717 /* Free the direct rendering per screen data */
718 (*psc->core->destroyScreen) (psc->driScreen);
719 driDestroyConfigs(psc->driver_configs);
720 psc->driScreen = NULL;
721 if (psc->driver)
722 dlclose(psc->driver);
723 free(psc);
724 }
725
726 #define SWRAST_DRIVER_NAME "swrast"
727
728 static const struct glx_screen_vtable drisw_screen_vtable = {
729 .create_context = drisw_create_context,
730 .create_context_attribs = drisw_create_context_attribs,
731 .query_renderer_integer = drisw_query_renderer_integer,
732 .query_renderer_string = drisw_query_renderer_string,
733 };
734
735 static void
736 driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
737 {
738 int i;
739
740 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
741
742 if (psc->swrast->base.version >= 3) {
743 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
744 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
745
746 /* DRISW version >= 2 implies support for OpenGL ES.
747 */
748 __glXEnableDirectExtension(&psc->base,
749 "GLX_EXT_create_context_es_profile");
750 __glXEnableDirectExtension(&psc->base,
751 "GLX_EXT_create_context_es2_profile");
752 }
753
754 if (psc->copySubBuffer)
755 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
756
757 /* FIXME: Figure out what other extensions can be ported here from dri2. */
758 for (i = 0; extensions[i]; i++) {
759 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
760 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
761 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
762 }
763 /* DRISW version 3 is also required because GLX_MESA_query_renderer
764 * requires GLX_ARB_create_context_profile.
765 */
766 if (psc->swrast->base.version >= 3
767 && strcmp(extensions[i]->name, __DRI2_RENDERER_QUERY) == 0) {
768 psc->rendererQuery = (__DRI2rendererQueryExtension *) extensions[i];
769 __glXEnableDirectExtension(&psc->base, "GLX_MESA_query_renderer");
770 }
771 if (strcmp(extensions[i]->name, __DRI2_FLUSH_CONTROL) == 0) {
772 __glXEnableDirectExtension(&psc->base,
773 "GLX_ARB_context_flush_control");
774 }
775 }
776 }
777
778 static int
779 check_xshm(Display *dpy)
780 {
781 int (*old_handler)(Display *, XErrorEvent *);
782
783 int ignore;
784 XShmSegmentInfo info = { 0, };
785
786 if (!XQueryExtension(dpy, "MIT-SHM", &xshm_opcode, &ignore, &ignore))
787 return False;
788
789 old_handler = XSetErrorHandler(handle_xerror);
790 XShmDetach(dpy, &info);
791 XSync(dpy, False);
792 (void) XSetErrorHandler(old_handler);
793
794 /* BadRequest means we're a remote client. If we were local we'd
795 * expect BadValue since 'info' has an invalid segment name.
796 */
797 if (xshm_error == BadRequest)
798 return False;
799
800 xshm_error = 0;
801 return True;
802 }
803
804 static struct glx_screen *
805 driswCreateScreen(int screen, struct glx_display *priv)
806 {
807 __GLXDRIscreen *psp;
808 const __DRIconfig **driver_configs;
809 const __DRIextension **extensions;
810 struct drisw_screen *psc;
811 struct glx_config *configs = NULL, *visuals = NULL;
812 int i;
813 const __DRIextension **loader_extensions_local;
814
815 psc = calloc(1, sizeof *psc);
816 if (psc == NULL)
817 return NULL;
818
819 if (!glx_screen_init(&psc->base, screen, priv)) {
820 free(psc);
821 return NULL;
822 }
823
824 extensions = driOpenDriver(SWRAST_DRIVER_NAME, &psc->driver);
825 if (extensions == NULL)
826 goto handle_error;
827
828 if (!check_xshm(psc->base.dpy))
829 loader_extensions_local = loader_extensions_noshm;
830 else
831 loader_extensions_local = loader_extensions_shm;
832
833 for (i = 0; extensions[i]; i++) {
834 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
835 psc->core = (__DRIcoreExtension *) extensions[i];
836 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
837 psc->swrast = (__DRIswrastExtension *) extensions[i];
838 if (strcmp(extensions[i]->name, __DRI_COPY_SUB_BUFFER) == 0)
839 psc->copySubBuffer = (__DRIcopySubBufferExtension *) extensions[i];
840 }
841
842 if (psc->core == NULL || psc->swrast == NULL) {
843 ErrorMessageF("core dri extension not found\n");
844 goto handle_error;
845 }
846
847 if (psc->swrast->base.version >= 4) {
848 psc->driScreen =
849 psc->swrast->createNewScreen2(screen, loader_extensions_local,
850 extensions,
851 &driver_configs, psc);
852 } else {
853 psc->driScreen =
854 psc->swrast->createNewScreen(screen, loader_extensions_local,
855 &driver_configs, psc);
856 }
857 if (psc->driScreen == NULL) {
858 ErrorMessageF("failed to create dri screen\n");
859 goto handle_error;
860 }
861
862 extensions = psc->core->getExtensions(psc->driScreen);
863 driswBindExtensions(psc, extensions);
864
865 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
866 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
867
868 if (!configs || !visuals) {
869 ErrorMessageF("No matching fbConfigs or visuals found\n");
870 goto handle_error;
871 }
872
873 glx_config_destroy_list(psc->base.configs);
874 psc->base.configs = configs;
875 glx_config_destroy_list(psc->base.visuals);
876 psc->base.visuals = visuals;
877
878 psc->driver_configs = driver_configs;
879
880 psc->base.vtable = &drisw_screen_vtable;
881 psp = &psc->vtable;
882 psc->base.driScreen = psp;
883 psp->destroyScreen = driswDestroyScreen;
884 psp->createDrawable = driswCreateDrawable;
885 psp->swapBuffers = driswSwapBuffers;
886
887 if (psc->copySubBuffer)
888 psp->copySubBuffer = driswCopySubBuffer;
889
890 return &psc->base;
891
892 handle_error:
893 if (configs)
894 glx_config_destroy_list(configs);
895 if (visuals)
896 glx_config_destroy_list(visuals);
897 if (psc->driScreen)
898 psc->core->destroyScreen(psc->driScreen);
899 psc->driScreen = NULL;
900
901 if (psc->driver)
902 dlclose(psc->driver);
903 glx_screen_cleanup(&psc->base);
904 free(psc);
905
906 CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME);
907
908 return NULL;
909 }
910
911 /* Called from __glXFreeDisplayPrivate.
912 */
913 static void
914 driswDestroyDisplay(__GLXDRIdisplay * dpy)
915 {
916 free(dpy);
917 }
918
919 /*
920 * Allocate, initialize and return a __DRIdisplayPrivate object.
921 * This is called from __glXInitialize() when we are given a new
922 * display pointer.
923 */
924 _X_HIDDEN __GLXDRIdisplay *
925 driswCreateDisplay(Display * dpy)
926 {
927 struct drisw_display *pdpyp;
928
929 pdpyp = malloc(sizeof *pdpyp);
930 if (pdpyp == NULL)
931 return NULL;
932
933 pdpyp->base.destroyDisplay = driswDestroyDisplay;
934 pdpyp->base.createScreen = driswCreateScreen;
935
936 return &pdpyp->base;
937 }
938
939 #endif /* GLX_DIRECT_RENDERING */