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