33d347a6e169e1ac9e4a0e3b8e2981fc61a269f0
[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
31 struct drisw_display
32 {
33 __GLXDRIdisplay base;
34 };
35
36 struct drisw_context
37 {
38 struct glx_context base;
39 __DRIcontext *driContext;
40
41 };
42
43 struct drisw_screen
44 {
45 struct glx_screen base;
46
47 __DRIscreen *driScreen;
48 __GLXDRIscreen vtable;
49 const __DRIcoreExtension *core;
50 const __DRIswrastExtension *swrast;
51 const __DRItexBufferExtension *texBuffer;
52 const __DRIcopySubBufferExtension *copySubBuffer;
53
54 const __DRIconfig **driver_configs;
55
56 void *driver;
57 };
58
59 struct drisw_drawable
60 {
61 __GLXDRIdrawable base;
62
63 GC gc;
64 GC swapgc;
65
66 __DRIdrawable *driDrawable;
67 XVisualInfo *visinfo;
68 XImage *ximage;
69 };
70
71 static Bool
72 XCreateDrawable(struct drisw_drawable * pdp,
73 Display * dpy, XID drawable, int visualid)
74 {
75 XGCValues gcvalues;
76 long visMask;
77 XVisualInfo visTemp;
78 int num_visuals;
79
80 /* create GC's */
81 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
82 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
83
84 gcvalues.function = GXcopy;
85 gcvalues.graphics_exposures = False;
86 XChangeGC(dpy, pdp->gc, GCFunction, &gcvalues);
87 XChangeGC(dpy, pdp->swapgc, GCFunction, &gcvalues);
88 XChangeGC(dpy, pdp->swapgc, GCGraphicsExposures, &gcvalues);
89
90 /* visual */
91 visTemp.visualid = visualid;
92 visMask = VisualIDMask;
93 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
94
95 if (!pdp->visinfo || num_visuals == 0)
96 return False;
97
98 /* create XImage */
99 pdp->ximage = XCreateImage(dpy,
100 pdp->visinfo->visual,
101 pdp->visinfo->depth,
102 ZPixmap, 0, /* format, offset */
103 NULL, /* data */
104 0, 0, /* width, height */
105 32, /* bitmap_pad */
106 0); /* bytes_per_line */
107
108 /**
109 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
110 * the conversion for us.
111 */
112 if (pdp->ximage->bits_per_pixel == 24)
113 pdp->ximage->bits_per_pixel = 32;
114
115 return True;
116 }
117
118 static void
119 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
120 {
121 XDestroyImage(pdp->ximage);
122 free(pdp->visinfo);
123
124 XFreeGC(dpy, pdp->gc);
125 XFreeGC(dpy, pdp->swapgc);
126 }
127
128 /**
129 * swrast loader functions
130 */
131
132 static void
133 swrastGetDrawableInfo(__DRIdrawable * draw,
134 int *x, int *y, int *w, int *h,
135 void *loaderPrivate)
136 {
137 struct drisw_drawable *pdp = loaderPrivate;
138 __GLXDRIdrawable *pdraw = &(pdp->base);
139 Display *dpy = pdraw->psc->dpy;
140 Drawable drawable;
141
142 Window root;
143 unsigned uw, uh, bw, depth;
144
145 drawable = pdraw->xDrawable;
146
147 XGetGeometry(dpy, drawable, &root, x, y, &uw, &uh, &bw, &depth);
148 *w = uw;
149 *h = uh;
150 }
151
152 /**
153 * Align renderbuffer pitch.
154 *
155 * This should be chosen by the driver and the loader (libGL, xserver/glx)
156 * should use the driver provided pitch.
157 *
158 * It seems that the xorg loader (that is the xserver loading swrast_dri for
159 * indirect rendering, not client-side libGL) requires that the pitch is
160 * exactly the image width padded to 32 bits. XXX
161 *
162 * The above restriction can probably be overcome by using ScratchPixmap and
163 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
164 * the scratch pixmap to 'pitch / cpp'.
165 */
166 static inline int
167 bytes_per_line(unsigned pitch_bits, unsigned mul)
168 {
169 unsigned mask = mul - 1;
170
171 return ((pitch_bits + mask) & ~mask) / 8;
172 }
173
174 static void
175 swrastPutImage2(__DRIdrawable * draw, int op,
176 int x, int y, int w, int h, int stride,
177 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;
185
186 switch (op) {
187 case __DRI_SWRAST_IMAGE_OP_DRAW:
188 gc = pdp->gc;
189 break;
190 case __DRI_SWRAST_IMAGE_OP_SWAP:
191 gc = pdp->swapgc;
192 break;
193 default:
194 return;
195 }
196
197 drawable = pdraw->xDrawable;
198
199 ximage = pdp->ximage;
200 ximage->data = data;
201 ximage->width = w;
202 ximage->height = h;
203 ximage->bytes_per_line = stride ? stride : bytes_per_line(w * ximage->bits_per_pixel, 32);
204
205 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
206
207 ximage->data = NULL;
208 }
209
210 static void
211 swrastPutImage(__DRIdrawable * draw, int op,
212 int x, int y, int w, int h,
213 char *data, void *loaderPrivate)
214 {
215 swrastPutImage2(draw, op, x, y, w, h, 0, data, loaderPrivate);
216 }
217
218 static void
219 swrastGetImage(__DRIdrawable * read,
220 int x, int y, int w, int h,
221 char *data, void *loaderPrivate)
222 {
223 struct drisw_drawable *prp = loaderPrivate;
224 __GLXDRIdrawable *pread = &(prp->base);
225 Display *dpy = pread->psc->dpy;
226 Drawable readable;
227 XImage *ximage;
228
229 readable = pread->xDrawable;
230
231 ximage = prp->ximage;
232 ximage->data = data;
233 ximage->width = w;
234 ximage->height = h;
235 ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
236
237 XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
238
239 ximage->data = NULL;
240 }
241
242 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
243 .base = {__DRI_SWRAST_LOADER, 2 },
244
245 .getDrawableInfo = swrastGetDrawableInfo,
246 .putImage = swrastPutImage,
247 .getImage = swrastGetImage,
248 .putImage2 = swrastPutImage2,
249 };
250
251 static const __DRIextension *loader_extensions[] = {
252 &systemTimeExtension.base,
253 &swrastLoaderExtension.base,
254 NULL
255 };
256
257 /**
258 * GLXDRI functions
259 */
260
261 static void
262 drisw_destroy_context(struct glx_context *context)
263 {
264 struct drisw_context *pcp = (struct drisw_context *) context;
265 struct drisw_screen *psc = (struct drisw_screen *) context->psc;
266
267 driReleaseDrawables(&pcp->base);
268
269 free((char *) context->extensions);
270
271 (*psc->core->destroyContext) (pcp->driContext);
272
273 free(pcp);
274 }
275
276 static int
277 drisw_bind_context(struct glx_context *context, struct glx_context *old,
278 GLXDrawable draw, GLXDrawable read)
279 {
280 struct drisw_context *pcp = (struct drisw_context *) context;
281 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
282 struct drisw_drawable *pdraw, *pread;
283
284 pdraw = (struct drisw_drawable *) driFetchDrawable(context, draw);
285 pread = (struct drisw_drawable *) driFetchDrawable(context, read);
286
287 driReleaseDrawables(&pcp->base);
288
289 if (pdraw == NULL || pread == NULL)
290 return GLXBadDrawable;
291
292 if ((*psc->core->bindContext) (pcp->driContext,
293 pdraw->driDrawable, pread->driDrawable))
294 return Success;
295
296 return GLXBadContext;
297 }
298
299 static void
300 drisw_unbind_context(struct glx_context *context, struct glx_context *new)
301 {
302 struct drisw_context *pcp = (struct drisw_context *) context;
303 struct drisw_screen *psc = (struct drisw_screen *) pcp->base.psc;
304
305 (*psc->core->unbindContext) (pcp->driContext);
306 }
307
308 static void
309 drisw_bind_tex_image(Display * dpy,
310 GLXDrawable drawable,
311 int buffer, const int *attrib_list)
312 {
313 struct glx_context *gc = __glXGetCurrentContext();
314 struct drisw_context *pcp = (struct drisw_context *) gc;
315 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
316 struct drisw_drawable *pdraw = (struct drisw_drawable *) base;
317 struct drisw_screen *psc;
318
319 __glXInitialize(dpy);
320
321 if (pdraw != NULL) {
322 psc = (struct drisw_screen *) base->psc;
323
324 if (!psc->texBuffer)
325 return;
326
327 if (psc->texBuffer->base.version >= 2 &&
328 psc->texBuffer->setTexBuffer2 != NULL) {
329 (*psc->texBuffer->setTexBuffer2) (pcp->driContext,
330 pdraw->base.textureTarget,
331 pdraw->base.textureFormat,
332 pdraw->driDrawable);
333 }
334 else {
335 (*psc->texBuffer->setTexBuffer) (pcp->driContext,
336 pdraw->base.textureTarget,
337 pdraw->driDrawable);
338 }
339 }
340 }
341
342 static void
343 drisw_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
344 {
345 #if __DRI_TEX_BUFFER_VERSION >= 3
346 struct glx_context *gc = __glXGetCurrentContext();
347 struct dri2_context *pcp = (struct dri2_context *) gc;
348 __GLXDRIdrawable *base = GetGLXDRIDrawable(dpy, drawable);
349 struct glx_display *dpyPriv = __glXInitialize(dpy);
350 struct dri2_drawable *pdraw = (struct dri2_drawable *) base;
351 struct dri2_screen *psc;
352
353 if (pdraw != NULL) {
354 psc = (struct dri2_screen *) base->psc;
355
356 if (!psc->texBuffer)
357 return;
358
359 if (psc->texBuffer->base.version >= 3 &&
360 psc->texBuffer->releaseTexBuffer != NULL) {
361 (*psc->texBuffer->releaseTexBuffer) (pcp->driContext,
362 pdraw->base.textureTarget,
363 pdraw->driDrawable);
364 }
365 }
366 #endif
367 }
368
369 static const struct glx_context_vtable drisw_context_vtable = {
370 drisw_destroy_context,
371 drisw_bind_context,
372 drisw_unbind_context,
373 NULL,
374 NULL,
375 DRI_glXUseXFont,
376 drisw_bind_tex_image,
377 drisw_release_tex_image,
378 NULL, /* get_proc_address */
379 };
380
381 static struct glx_context *
382 drisw_create_context(struct glx_screen *base,
383 struct glx_config *config_base,
384 struct glx_context *shareList, int renderType)
385 {
386 struct drisw_context *pcp, *pcp_shared;
387 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
388 struct drisw_screen *psc = (struct drisw_screen *) base;
389 __DRIcontext *shared = NULL;
390
391 if (!psc->base.driScreen)
392 return NULL;
393
394 /* Check the renderType value */
395 if (!validate_renderType_against_config(config_base, renderType))
396 return NULL;
397
398 if (shareList) {
399 /* If the shareList context is not a DRISW context, we cannot possibly
400 * create a DRISW context that shares it.
401 */
402 if (shareList->vtable->destroy != drisw_destroy_context) {
403 return NULL;
404 }
405
406 pcp_shared = (struct drisw_context *) shareList;
407 shared = pcp_shared->driContext;
408 }
409
410 pcp = calloc(1, sizeof *pcp);
411 if (pcp == NULL)
412 return NULL;
413
414 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
415 free(pcp);
416 return NULL;
417 }
418
419 pcp->base.renderType = renderType;
420
421 pcp->driContext =
422 (*psc->core->createNewContext) (psc->driScreen,
423 config->driConfig, shared, pcp);
424 if (pcp->driContext == NULL) {
425 free(pcp);
426 return NULL;
427 }
428
429 pcp->base.vtable = &drisw_context_vtable;
430
431 return &pcp->base;
432 }
433
434 static struct glx_context *
435 drisw_create_context_attribs(struct glx_screen *base,
436 struct glx_config *config_base,
437 struct glx_context *shareList,
438 unsigned num_attribs,
439 const uint32_t *attribs,
440 unsigned *error)
441 {
442 struct drisw_context *pcp, *pcp_shared;
443 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) config_base;
444 struct drisw_screen *psc = (struct drisw_screen *) base;
445 __DRIcontext *shared = NULL;
446
447 uint32_t minor_ver;
448 uint32_t major_ver;
449 uint32_t renderType;
450 uint32_t flags;
451 unsigned api;
452 int reset;
453 uint32_t ctx_attribs[2 * 4];
454 unsigned num_ctx_attribs = 0;
455
456 if (!psc->base.driScreen)
457 return NULL;
458
459 if (psc->swrast->base.version < 3)
460 return NULL;
461
462 /* Remap the GLX tokens to DRI2 tokens.
463 */
464 if (!dri2_convert_glx_attribs(num_attribs, attribs,
465 &major_ver, &minor_ver, &renderType, &flags,
466 &api, &reset, error))
467 return NULL;
468
469 /* Check the renderType value */
470 if (!validate_renderType_against_config(config_base, renderType)) {
471 return NULL;
472 }
473
474 if (reset != __DRI_CTX_RESET_NO_NOTIFICATION)
475 return NULL;
476
477 if (shareList) {
478 pcp_shared = (struct drisw_context *) shareList;
479 shared = pcp_shared->driContext;
480 }
481
482 pcp = calloc(1, sizeof *pcp);
483 if (pcp == NULL)
484 return NULL;
485
486 if (!glx_context_init(&pcp->base, &psc->base, &config->base)) {
487 free(pcp);
488 return NULL;
489 }
490
491 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
492 ctx_attribs[num_ctx_attribs++] = major_ver;
493 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
494 ctx_attribs[num_ctx_attribs++] = minor_ver;
495
496 if (flags != 0) {
497 ctx_attribs[num_ctx_attribs++] = __DRI_CTX_ATTRIB_FLAGS;
498
499 /* The current __DRI_CTX_FLAG_* values are identical to the
500 * GLX_CONTEXT_*_BIT values.
501 */
502 ctx_attribs[num_ctx_attribs++] = flags;
503 }
504
505 pcp->base.renderType = renderType;
506
507 pcp->driContext =
508 (*psc->swrast->createContextAttribs) (psc->driScreen,
509 api,
510 config->driConfig,
511 shared,
512 num_ctx_attribs / 2,
513 ctx_attribs,
514 error,
515 pcp);
516 if (pcp->driContext == NULL) {
517 free(pcp);
518 return NULL;
519 }
520
521 pcp->base.vtable = &drisw_context_vtable;
522
523 return &pcp->base;
524 }
525
526 static void
527 driswDestroyDrawable(__GLXDRIdrawable * pdraw)
528 {
529 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
530 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
531
532 (*psc->core->destroyDrawable) (pdp->driDrawable);
533
534 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
535 free(pdp);
536 }
537
538 static __GLXDRIdrawable *
539 driswCreateDrawable(struct glx_screen *base, XID xDrawable,
540 GLXDrawable drawable, struct glx_config *modes)
541 {
542 struct drisw_drawable *pdp;
543 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
544 struct drisw_screen *psc = (struct drisw_screen *) base;
545 Bool ret;
546 const __DRIswrastExtension *swrast = psc->swrast;
547
548 pdp = calloc(1, sizeof(*pdp));
549 if (!pdp)
550 return NULL;
551
552 pdp->base.xDrawable = xDrawable;
553 pdp->base.drawable = drawable;
554 pdp->base.psc = &psc->base;
555
556 ret = XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
557 if (!ret) {
558 free(pdp);
559 return NULL;
560 }
561
562 /* Create a new drawable */
563 pdp->driDrawable =
564 (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
565
566 if (!pdp->driDrawable) {
567 XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
568 free(pdp);
569 return NULL;
570 }
571
572 pdp->base.destroyDrawable = driswDestroyDrawable;
573
574 return &pdp->base;
575 }
576
577 static int64_t
578 driswSwapBuffers(__GLXDRIdrawable * pdraw,
579 int64_t target_msc, int64_t divisor, int64_t remainder,
580 Bool flush)
581 {
582 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
583 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
584
585 (void) target_msc;
586 (void) divisor;
587 (void) remainder;
588
589 if (flush) {
590 glFlush();
591 }
592
593 (*psc->core->swapBuffers) (pdp->driDrawable);
594
595 return 0;
596 }
597
598 static void
599 driswCopySubBuffer(__GLXDRIdrawable * pdraw,
600 int x, int y, int width, int height, Bool flush)
601 {
602 struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
603 struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
604
605 if (flush) {
606 glFlush();
607 }
608
609 (*psc->copySubBuffer->copySubBuffer) (pdp->driDrawable,
610 x, y, width, height);
611 }
612
613 static void
614 driswDestroyScreen(struct glx_screen *base)
615 {
616 struct drisw_screen *psc = (struct drisw_screen *) base;
617
618 /* Free the direct rendering per screen data */
619 (*psc->core->destroyScreen) (psc->driScreen);
620 driDestroyConfigs(psc->driver_configs);
621 psc->driScreen = NULL;
622 if (psc->driver)
623 dlclose(psc->driver);
624 }
625
626 #define SWRAST_DRIVER_NAME "swrast"
627
628 static void *
629 driOpenSwrast(void)
630 {
631 void *driver = NULL;
632
633 if (driver == NULL)
634 driver = driOpenDriver(SWRAST_DRIVER_NAME);
635
636 return driver;
637 }
638
639 static const struct glx_screen_vtable drisw_screen_vtable = {
640 drisw_create_context,
641 drisw_create_context_attribs
642 };
643
644 static void
645 driswBindExtensions(struct drisw_screen *psc, const __DRIextension **extensions)
646 {
647 int i;
648
649 __glXEnableDirectExtension(&psc->base, "GLX_SGI_make_current_read");
650
651 if (psc->swrast->base.version >= 3) {
652 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context");
653 __glXEnableDirectExtension(&psc->base, "GLX_ARB_create_context_profile");
654
655 /* DRISW version >= 2 implies support for OpenGL ES 2.0.
656 */
657 __glXEnableDirectExtension(&psc->base,
658 "GLX_EXT_create_context_es2_profile");
659 }
660
661 if (psc->copySubBuffer)
662 __glXEnableDirectExtension(&psc->base, "GLX_MESA_copy_sub_buffer");
663
664 /* FIXME: Figure out what other extensions can be ported here from dri2. */
665 for (i = 0; extensions[i]; i++) {
666 if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
667 psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
668 __glXEnableDirectExtension(&psc->base, "GLX_EXT_texture_from_pixmap");
669 }
670 }
671 }
672
673 static struct glx_screen *
674 driswCreateScreen(int screen, struct glx_display *priv)
675 {
676 __GLXDRIscreen *psp;
677 const __DRIconfig **driver_configs;
678 const __DRIextension **extensions;
679 struct drisw_screen *psc;
680 struct glx_config *configs = NULL, *visuals = NULL;
681 int i;
682
683 psc = calloc(1, sizeof *psc);
684 if (psc == NULL)
685 return NULL;
686
687 if (!glx_screen_init(&psc->base, screen, priv)) {
688 free(psc);
689 return NULL;
690 }
691
692 psc->driver = driOpenSwrast();
693 if (psc->driver == NULL)
694 goto handle_error;
695
696 extensions = driGetDriverExtensions(psc->driver, SWRAST_DRIVER_NAME);
697 if (extensions == NULL)
698 goto handle_error;
699
700 for (i = 0; extensions[i]; i++) {
701 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
702 psc->core = (__DRIcoreExtension *) extensions[i];
703 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
704 psc->swrast = (__DRIswrastExtension *) extensions[i];
705 if (strcmp(extensions[i]->name, __DRI_COPY_SUB_BUFFER) == 0)
706 psc->copySubBuffer = (__DRIcopySubBufferExtension *) extensions[i];
707 }
708
709 if (psc->core == NULL || psc->swrast == NULL) {
710 ErrorMessageF("core dri extension not found\n");
711 goto handle_error;
712 }
713
714 if (psc->swrast->base.version >= 4) {
715 psc->driScreen =
716 psc->swrast->createNewScreen2(screen, loader_extensions,
717 extensions,
718 &driver_configs, psc);
719 } else {
720 psc->driScreen =
721 psc->swrast->createNewScreen(screen, loader_extensions,
722 &driver_configs, psc);
723 }
724 if (psc->driScreen == NULL) {
725 ErrorMessageF("failed to create dri screen\n");
726 goto handle_error;
727 }
728
729 extensions = psc->core->getExtensions(psc->driScreen);
730 driswBindExtensions(psc, extensions);
731
732 configs = driConvertConfigs(psc->core, psc->base.configs, driver_configs);
733 visuals = driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
734
735 if (!configs || !visuals)
736 goto handle_error;
737
738 glx_config_destroy_list(psc->base.configs);
739 psc->base.configs = configs;
740 glx_config_destroy_list(psc->base.visuals);
741 psc->base.visuals = visuals;
742
743 psc->driver_configs = driver_configs;
744
745 psc->base.vtable = &drisw_screen_vtable;
746 psp = &psc->vtable;
747 psc->base.driScreen = psp;
748 psp->destroyScreen = driswDestroyScreen;
749 psp->createDrawable = driswCreateDrawable;
750 psp->swapBuffers = driswSwapBuffers;
751
752 if (psc->copySubBuffer)
753 psp->copySubBuffer = driswCopySubBuffer;
754
755 return &psc->base;
756
757 handle_error:
758 if (configs)
759 glx_config_destroy_list(configs);
760 if (visuals)
761 glx_config_destroy_list(visuals);
762 if (psc->driScreen)
763 psc->core->destroyScreen(psc->driScreen);
764 psc->driScreen = NULL;
765
766 if (psc->driver)
767 dlclose(psc->driver);
768 glx_screen_cleanup(&psc->base);
769 free(psc);
770
771 CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME);
772
773 return NULL;
774 }
775
776 /* Called from __glXFreeDisplayPrivate.
777 */
778 static void
779 driswDestroyDisplay(__GLXDRIdisplay * dpy)
780 {
781 free(dpy);
782 }
783
784 /*
785 * Allocate, initialize and return a __DRIdisplayPrivate object.
786 * This is called from __glXInitialize() when we are given a new
787 * display pointer.
788 */
789 _X_HIDDEN __GLXDRIdisplay *
790 driswCreateDisplay(Display * dpy)
791 {
792 struct drisw_display *pdpyp;
793
794 pdpyp = malloc(sizeof *pdpyp);
795 if (pdpyp == NULL)
796 return NULL;
797
798 pdpyp->base.destroyDisplay = driswDestroyDisplay;
799 pdpyp->base.createScreen = driswCreateScreen;
800
801 return &pdpyp->base;
802 }
803
804 #endif /* GLX_DIRECT_RENDERING */