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