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