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