2 * Copyright 2008 George Sapountzis
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:
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
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
24 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
27 #include "glxclient.h"
29 #include "dri_common.h"
38 struct glx_context base
;
39 __DRIcontext
*driContext
;
45 struct glx_screen base
;
47 __DRIscreen
*driScreen
;
48 __GLXDRIscreen vtable
;
49 const __DRIcoreExtension
*core
;
50 const __DRIswrastExtension
*swrast
;
51 const __DRItexBufferExtension
*texBuffer
;
53 const __DRIconfig
**driver_configs
;
60 __GLXDRIdrawable base
;
65 __DRIdrawable
*driDrawable
;
71 XCreateDrawable(struct drisw_drawable
* pdp
,
72 Display
* dpy
, XID drawable
, int visualid
)
80 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
81 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
83 gcvalues
.function
= GXcopy
;
84 gcvalues
.graphics_exposures
= False
;
85 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
86 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
87 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
90 visTemp
.visualid
= visualid
;
91 visMask
= VisualIDMask
;
92 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
94 if (!pdp
->visinfo
|| num_visuals
== 0)
98 pdp
->ximage
= XCreateImage(dpy
,
101 ZPixmap
, 0, /* format, offset */
103 0, 0, /* width, height */
105 0); /* bytes_per_line */
108 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
109 * the conversion for us.
111 if (pdp
->ximage
->bits_per_pixel
== 24)
112 pdp
->ximage
->bits_per_pixel
= 32;
118 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
120 XDestroyImage(pdp
->ximage
);
123 XFreeGC(dpy
, pdp
->gc
);
124 XFreeGC(dpy
, pdp
->swapgc
);
128 * swrast loader functions
132 swrastGetDrawableInfo(__DRIdrawable
* draw
,
133 int *x
, int *y
, int *w
, int *h
,
136 struct drisw_drawable
*pdp
= loaderPrivate
;
137 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
138 Display
*dpy
= pdraw
->psc
->dpy
;
142 unsigned uw
, uh
, bw
, depth
;
144 drawable
= pdraw
->xDrawable
;
146 XGetGeometry(dpy
, drawable
, &root
, x
, y
, &uw
, &uh
, &bw
, &depth
);
152 * Align renderbuffer pitch.
154 * This should be chosen by the driver and the loader (libGL, xserver/glx)
155 * should use the driver provided pitch.
157 * It seems that the xorg loader (that is the xserver loading swrast_dri for
158 * indirect rendering, not client-side libGL) requires that the pitch is
159 * exactly the image width padded to 32 bits. XXX
161 * The above restriction can probably be overcome by using ScratchPixmap and
162 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
163 * the scratch pixmap to 'pitch / cpp'.
166 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
168 unsigned mask
= mul
- 1;
170 return ((pitch_bits
+ mask
) & ~mask
) / 8;
174 swrastPutImage(__DRIdrawable
* draw
, int op
,
175 int x
, int y
, int w
, int h
,
176 char *data
, void *loaderPrivate
)
178 struct drisw_drawable
*pdp
= loaderPrivate
;
179 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
180 Display
*dpy
= pdraw
->psc
->dpy
;
186 case __DRI_SWRAST_IMAGE_OP_DRAW
:
189 case __DRI_SWRAST_IMAGE_OP_SWAP
:
196 drawable
= pdraw
->xDrawable
;
198 ximage
= pdp
->ximage
;
202 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
204 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
210 swrastGetImage(__DRIdrawable
* read
,
211 int x
, int y
, int w
, int h
,
212 char *data
, void *loaderPrivate
)
214 struct drisw_drawable
*prp
= loaderPrivate
;
215 __GLXDRIdrawable
*pread
= &(prp
->base
);
216 Display
*dpy
= pread
->psc
->dpy
;
220 readable
= pread
->xDrawable
;
222 ximage
= prp
->ximage
;
226 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
228 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
233 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
234 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
235 swrastGetDrawableInfo
,
240 static const __DRIextension
*loader_extensions
[] = {
241 &systemTimeExtension
.base
,
242 &swrastLoaderExtension
.base
,
251 drisw_destroy_context(struct glx_context
*context
)
253 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
254 struct drisw_screen
*psc
= (struct drisw_screen
*) context
->psc
;
256 driReleaseDrawables(&pcp
->base
);
258 free((char *) context
->extensions
);
260 (*psc
->core
->destroyContext
) (pcp
->driContext
);
266 drisw_bind_context(struct glx_context
*context
, struct glx_context
*old
,
267 GLXDrawable draw
, GLXDrawable read
)
269 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
270 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->base
.psc
;
271 struct drisw_drawable
*pdraw
, *pread
;
273 pdraw
= (struct drisw_drawable
*) driFetchDrawable(context
, draw
);
274 pread
= (struct drisw_drawable
*) driFetchDrawable(context
, read
);
276 driReleaseDrawables(&pcp
->base
);
278 if (pdraw
== NULL
|| pread
== NULL
)
279 return GLXBadDrawable
;
281 if ((*psc
->core
->bindContext
) (pcp
->driContext
,
282 pdraw
->driDrawable
, pread
->driDrawable
))
285 return GLXBadContext
;
289 drisw_unbind_context(struct glx_context
*context
, struct glx_context
*new)
291 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
292 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->base
.psc
;
294 (*psc
->core
->unbindContext
) (pcp
->driContext
);
298 drisw_bind_tex_image(Display
* dpy
,
299 GLXDrawable drawable
,
300 int buffer
, const int *attrib_list
)
302 struct glx_context
*gc
= __glXGetCurrentContext();
303 struct drisw_context
*pcp
= (struct drisw_context
*) gc
;
304 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
305 struct drisw_drawable
*pdraw
= (struct drisw_drawable
*) base
;
306 struct drisw_screen
*psc
;
308 __glXInitialize(dpy
);
311 psc
= (struct drisw_screen
*) base
->psc
;
316 if (psc
->texBuffer
->base
.version
>= 2 &&
317 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
318 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
319 pdraw
->base
.textureTarget
,
320 pdraw
->base
.textureFormat
,
324 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
325 pdraw
->base
.textureTarget
,
332 drisw_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
334 #if __DRI_TEX_BUFFER_VERSION >= 3
335 struct glx_context
*gc
= __glXGetCurrentContext();
336 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
337 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
338 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
339 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
340 struct dri2_screen
*psc
;
343 psc
= (struct dri2_screen
*) base
->psc
;
348 if (psc
->texBuffer
->base
.version
>= 3 &&
349 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
350 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
351 pdraw
->base
.textureTarget
,
358 static const struct glx_context_vtable drisw_context_vtable
= {
359 drisw_destroy_context
,
361 drisw_unbind_context
,
365 drisw_bind_tex_image
,
366 drisw_release_tex_image
,
367 NULL
, /* get_proc_address */
370 static struct glx_context
*
371 drisw_create_context(struct glx_screen
*base
,
372 struct glx_config
*config_base
,
373 struct glx_context
*shareList
, int renderType
)
375 struct drisw_context
*pcp
, *pcp_shared
;
376 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
377 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
378 __DRIcontext
*shared
= NULL
;
380 if (!psc
->base
.driScreen
)
383 /* Check the renderType value */
384 if (!validate_renderType_against_config(config_base
, renderType
))
388 /* If the shareList context is not a DRISW context, we cannot possibly
389 * create a DRISW context that shares it.
391 if (shareList
->vtable
->destroy
!= drisw_destroy_context
) {
395 pcp_shared
= (struct drisw_context
*) shareList
;
396 shared
= pcp_shared
->driContext
;
399 pcp
= calloc(1, sizeof *pcp
);
403 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
408 pcp
->base
.renderType
= renderType
;
411 (*psc
->core
->createNewContext
) (psc
->driScreen
,
412 config
->driConfig
, shared
, pcp
);
413 if (pcp
->driContext
== NULL
) {
418 pcp
->base
.vtable
= &drisw_context_vtable
;
423 static struct glx_context
*
424 drisw_create_context_attribs(struct glx_screen
*base
,
425 struct glx_config
*config_base
,
426 struct glx_context
*shareList
,
427 unsigned num_attribs
,
428 const uint32_t *attribs
,
431 struct drisw_context
*pcp
, *pcp_shared
;
432 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
433 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
434 __DRIcontext
*shared
= NULL
;
442 uint32_t ctx_attribs
[2 * 4];
443 unsigned num_ctx_attribs
= 0;
445 if (!psc
->base
.driScreen
)
448 if (psc
->swrast
->base
.version
< 3)
451 /* Remap the GLX tokens to DRI2 tokens.
453 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
454 &major_ver
, &minor_ver
, &renderType
, &flags
,
455 &api
, &reset
, error
))
458 /* Check the renderType value */
459 if (!validate_renderType_against_config(config_base
, renderType
)) {
463 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
)
467 pcp_shared
= (struct drisw_context
*) shareList
;
468 shared
= pcp_shared
->driContext
;
471 pcp
= calloc(1, sizeof *pcp
);
475 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
480 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
481 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
482 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
483 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
486 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
488 /* The current __DRI_CTX_FLAG_* values are identical to the
489 * GLX_CONTEXT_*_BIT values.
491 ctx_attribs
[num_ctx_attribs
++] = flags
;
494 pcp
->base
.renderType
= renderType
;
497 (*psc
->swrast
->createContextAttribs
) (psc
->driScreen
,
505 if (pcp
->driContext
== NULL
) {
510 pcp
->base
.vtable
= &drisw_context_vtable
;
516 driswDestroyDrawable(__GLXDRIdrawable
* pdraw
)
518 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
519 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
521 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
523 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
527 static __GLXDRIdrawable
*
528 driswCreateDrawable(struct glx_screen
*base
, XID xDrawable
,
529 GLXDrawable drawable
, struct glx_config
*modes
)
531 struct drisw_drawable
*pdp
;
532 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
533 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
535 const __DRIswrastExtension
*swrast
= psc
->swrast
;
537 pdp
= calloc(1, sizeof(*pdp
));
541 pdp
->base
.xDrawable
= xDrawable
;
542 pdp
->base
.drawable
= drawable
;
543 pdp
->base
.psc
= &psc
->base
;
545 ret
= XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
551 /* Create a new drawable */
553 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
555 if (!pdp
->driDrawable
) {
556 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
561 pdp
->base
.destroyDrawable
= driswDestroyDrawable
;
567 driswSwapBuffers(__GLXDRIdrawable
* pdraw
,
568 int64_t target_msc
, int64_t divisor
, int64_t remainder
,
571 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
572 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
582 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
588 driswDestroyScreen(struct glx_screen
*base
)
590 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
592 /* Free the direct rendering per screen data */
593 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
594 driDestroyConfigs(psc
->driver_configs
);
595 psc
->driScreen
= NULL
;
597 dlclose(psc
->driver
);
600 #define SWRAST_DRIVER_NAME "swrast"
608 driver
= driOpenDriver(SWRAST_DRIVER_NAME
);
613 static const struct glx_screen_vtable drisw_screen_vtable
= {
614 drisw_create_context
,
615 drisw_create_context_attribs
619 driswBindExtensions(struct drisw_screen
*psc
, const __DRIextension
**extensions
)
623 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
625 if (psc
->swrast
->base
.version
>= 3) {
626 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
627 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
629 /* DRISW version >= 2 implies support for OpenGL ES 2.0.
631 __glXEnableDirectExtension(&psc
->base
,
632 "GLX_EXT_create_context_es2_profile");
635 /* FIXME: Figure out what other extensions can be ported here from dri2. */
636 for (i
= 0; extensions
[i
]; i
++) {
637 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
638 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
639 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
644 static struct glx_screen
*
645 driswCreateScreen(int screen
, struct glx_display
*priv
)
648 const __DRIconfig
**driver_configs
;
649 const __DRIextension
**extensions
;
650 struct drisw_screen
*psc
;
651 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
654 psc
= calloc(1, sizeof *psc
);
658 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
663 psc
->driver
= driOpenSwrast();
664 if (psc
->driver
== NULL
)
667 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
668 if (extensions
== NULL
) {
669 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
673 for (i
= 0; extensions
[i
]; i
++) {
674 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
675 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
676 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
677 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
680 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
681 ErrorMessageF("core dri extension not found\n");
686 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
687 &driver_configs
, psc
);
688 if (psc
->driScreen
== NULL
) {
689 ErrorMessageF("failed to create dri screen\n");
693 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
694 driswBindExtensions(psc
, extensions
);
696 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
697 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
699 if (!configs
|| !visuals
)
702 glx_config_destroy_list(psc
->base
.configs
);
703 psc
->base
.configs
= configs
;
704 glx_config_destroy_list(psc
->base
.visuals
);
705 psc
->base
.visuals
= visuals
;
707 psc
->driver_configs
= driver_configs
;
709 psc
->base
.vtable
= &drisw_screen_vtable
;
711 psc
->base
.driScreen
= psp
;
712 psp
->destroyScreen
= driswDestroyScreen
;
713 psp
->createDrawable
= driswCreateDrawable
;
714 psp
->swapBuffers
= driswSwapBuffers
;
720 glx_config_destroy_list(configs
);
722 glx_config_destroy_list(visuals
);
724 psc
->core
->destroyScreen(psc
->driScreen
);
725 psc
->driScreen
= NULL
;
728 dlclose(psc
->driver
);
729 glx_screen_cleanup(&psc
->base
);
732 CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME
);
737 /* Called from __glXFreeDisplayPrivate.
740 driswDestroyDisplay(__GLXDRIdisplay
* dpy
)
746 * Allocate, initialize and return a __DRIdisplayPrivate object.
747 * This is called from __glXInitialize() when we are given a new
750 _X_HIDDEN __GLXDRIdisplay
*
751 driswCreateDisplay(Display
* dpy
)
753 struct drisw_display
*pdpyp
;
755 pdpyp
= malloc(sizeof *pdpyp
);
759 pdpyp
->base
.destroyDisplay
= driswDestroyDisplay
;
760 pdpyp
->base
.createScreen
= driswCreateScreen
;
765 #endif /* GLX_DIRECT_RENDERING */