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
;
52 const __DRIcopySubBufferExtension
*copySubBuffer
;
54 const __DRIconfig
**driver_configs
;
61 __GLXDRIdrawable base
;
66 __DRIdrawable
*driDrawable
;
72 XCreateDrawable(struct drisw_drawable
* pdp
,
73 Display
* dpy
, XID drawable
, int visualid
)
81 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
82 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
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
);
91 visTemp
.visualid
= visualid
;
92 visMask
= VisualIDMask
;
93 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
95 if (!pdp
->visinfo
|| num_visuals
== 0)
99 pdp
->ximage
= XCreateImage(dpy
,
100 pdp
->visinfo
->visual
,
102 ZPixmap
, 0, /* format, offset */
104 0, 0, /* width, height */
106 0); /* bytes_per_line */
109 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
110 * the conversion for us.
112 if (pdp
->ximage
->bits_per_pixel
== 24)
113 pdp
->ximage
->bits_per_pixel
= 32;
119 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
121 XDestroyImage(pdp
->ximage
);
124 XFreeGC(dpy
, pdp
->gc
);
125 XFreeGC(dpy
, pdp
->swapgc
);
129 * swrast loader functions
133 swrastGetDrawableInfo(__DRIdrawable
* draw
,
134 int *x
, int *y
, int *w
, int *h
,
137 struct drisw_drawable
*pdp
= loaderPrivate
;
138 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
139 Display
*dpy
= pdraw
->psc
->dpy
;
143 unsigned uw
, uh
, bw
, depth
;
145 drawable
= pdraw
->xDrawable
;
147 XGetGeometry(dpy
, drawable
, &root
, x
, y
, &uw
, &uh
, &bw
, &depth
);
153 * Align renderbuffer pitch.
155 * This should be chosen by the driver and the loader (libGL, xserver/glx)
156 * should use the driver provided pitch.
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
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'.
167 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
169 unsigned mask
= mul
- 1;
171 return ((pitch_bits
+ mask
) & ~mask
) / 8;
175 swrastPutImage2(__DRIdrawable
* draw
, int op
,
176 int x
, int y
, int w
, int h
, int stride
,
177 char *data
, void *loaderPrivate
)
179 struct drisw_drawable
*pdp
= loaderPrivate
;
180 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
181 Display
*dpy
= pdraw
->psc
->dpy
;
187 case __DRI_SWRAST_IMAGE_OP_DRAW
:
190 case __DRI_SWRAST_IMAGE_OP_SWAP
:
197 drawable
= pdraw
->xDrawable
;
199 ximage
= pdp
->ximage
;
203 ximage
->bytes_per_line
= stride
? stride
: bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
205 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
211 swrastPutImage(__DRIdrawable
* draw
, int op
,
212 int x
, int y
, int w
, int h
,
213 char *data
, void *loaderPrivate
)
215 swrastPutImage2(draw
, op
, x
, y
, w
, h
, 0, data
, loaderPrivate
);
219 swrastGetImage(__DRIdrawable
* read
,
220 int x
, int y
, int w
, int h
,
221 char *data
, void *loaderPrivate
)
223 struct drisw_drawable
*prp
= loaderPrivate
;
224 __GLXDRIdrawable
*pread
= &(prp
->base
);
225 Display
*dpy
= pread
->psc
->dpy
;
229 readable
= pread
->xDrawable
;
231 ximage
= prp
->ximage
;
235 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
237 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
242 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
243 .base
= {__DRI_SWRAST_LOADER
, 2 },
245 .getDrawableInfo
= swrastGetDrawableInfo
,
246 .putImage
= swrastPutImage
,
247 .getImage
= swrastGetImage
,
248 .putImage2
= swrastPutImage2
,
251 static const __DRIextension
*loader_extensions
[] = {
252 &systemTimeExtension
.base
,
253 &swrastLoaderExtension
.base
,
262 drisw_destroy_context(struct glx_context
*context
)
264 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
265 struct drisw_screen
*psc
= (struct drisw_screen
*) context
->psc
;
267 driReleaseDrawables(&pcp
->base
);
269 free((char *) context
->extensions
);
271 (*psc
->core
->destroyContext
) (pcp
->driContext
);
277 drisw_bind_context(struct glx_context
*context
, struct glx_context
*old
,
278 GLXDrawable draw
, GLXDrawable read
)
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
;
284 pdraw
= (struct drisw_drawable
*) driFetchDrawable(context
, draw
);
285 pread
= (struct drisw_drawable
*) driFetchDrawable(context
, read
);
287 driReleaseDrawables(&pcp
->base
);
289 if (pdraw
== NULL
|| pread
== NULL
)
290 return GLXBadDrawable
;
292 if ((*psc
->core
->bindContext
) (pcp
->driContext
,
293 pdraw
->driDrawable
, pread
->driDrawable
))
296 return GLXBadContext
;
300 drisw_unbind_context(struct glx_context
*context
, struct glx_context
*new)
302 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
303 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->base
.psc
;
305 (*psc
->core
->unbindContext
) (pcp
->driContext
);
309 drisw_bind_tex_image(Display
* dpy
,
310 GLXDrawable drawable
,
311 int buffer
, const int *attrib_list
)
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
;
319 __glXInitialize(dpy
);
322 psc
= (struct drisw_screen
*) base
->psc
;
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
,
335 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
336 pdraw
->base
.textureTarget
,
343 drisw_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
345 struct glx_context
*gc
= __glXGetCurrentContext();
346 struct drisw_context
*pcp
= (struct drisw_context
*) gc
;
347 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
348 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
349 struct drisw_drawable
*pdraw
= (struct drisw_drawable
*) base
;
350 struct drisw_screen
*psc
;
352 if (dpyPriv
!= NULL
&& pdraw
!= NULL
) {
353 psc
= (struct drisw_screen
*) base
->psc
;
358 if (psc
->texBuffer
->base
.version
>= 3 &&
359 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
360 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
361 pdraw
->base
.textureTarget
,
367 static const struct glx_context_vtable drisw_context_vtable
= {
368 .destroy
= drisw_destroy_context
,
369 .bind
= drisw_bind_context
,
370 .unbind
= drisw_unbind_context
,
373 .use_x_font
= DRI_glXUseXFont
,
374 .bind_tex_image
= drisw_bind_tex_image
,
375 .release_tex_image
= drisw_release_tex_image
,
376 .get_proc_address
= NULL
,
379 static struct glx_context
*
380 drisw_create_context(struct glx_screen
*base
,
381 struct glx_config
*config_base
,
382 struct glx_context
*shareList
, int renderType
)
384 struct drisw_context
*pcp
, *pcp_shared
;
385 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
386 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
387 __DRIcontext
*shared
= NULL
;
389 if (!psc
->base
.driScreen
)
392 /* Check the renderType value */
393 if (!validate_renderType_against_config(config_base
, renderType
))
397 /* If the shareList context is not a DRISW context, we cannot possibly
398 * create a DRISW context that shares it.
400 if (shareList
->vtable
->destroy
!= drisw_destroy_context
) {
404 pcp_shared
= (struct drisw_context
*) shareList
;
405 shared
= pcp_shared
->driContext
;
408 pcp
= calloc(1, sizeof *pcp
);
412 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
417 pcp
->base
.renderType
= renderType
;
420 (*psc
->core
->createNewContext
) (psc
->driScreen
,
421 config
->driConfig
, shared
, pcp
);
422 if (pcp
->driContext
== NULL
) {
427 pcp
->base
.vtable
= &drisw_context_vtable
;
432 static struct glx_context
*
433 drisw_create_context_attribs(struct glx_screen
*base
,
434 struct glx_config
*config_base
,
435 struct glx_context
*shareList
,
436 unsigned num_attribs
,
437 const uint32_t *attribs
,
440 struct drisw_context
*pcp
, *pcp_shared
;
441 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
442 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
443 __DRIcontext
*shared
= NULL
;
451 uint32_t ctx_attribs
[2 * 4];
452 unsigned num_ctx_attribs
= 0;
454 if (!psc
->base
.driScreen
)
457 if (psc
->swrast
->base
.version
< 3)
460 /* Remap the GLX tokens to DRI2 tokens.
462 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
463 &major_ver
, &minor_ver
, &renderType
, &flags
,
464 &api
, &reset
, error
))
467 /* Check the renderType value */
468 if (!validate_renderType_against_config(config_base
, renderType
)) {
472 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
)
476 pcp_shared
= (struct drisw_context
*) shareList
;
477 shared
= pcp_shared
->driContext
;
480 pcp
= calloc(1, sizeof *pcp
);
484 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
489 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
490 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
491 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
492 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
495 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
497 /* The current __DRI_CTX_FLAG_* values are identical to the
498 * GLX_CONTEXT_*_BIT values.
500 ctx_attribs
[num_ctx_attribs
++] = flags
;
503 pcp
->base
.renderType
= renderType
;
506 (*psc
->swrast
->createContextAttribs
) (psc
->driScreen
,
514 if (pcp
->driContext
== NULL
) {
519 pcp
->base
.vtable
= &drisw_context_vtable
;
525 driswDestroyDrawable(__GLXDRIdrawable
* pdraw
)
527 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
528 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
530 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
532 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
536 static __GLXDRIdrawable
*
537 driswCreateDrawable(struct glx_screen
*base
, XID xDrawable
,
538 GLXDrawable drawable
, struct glx_config
*modes
)
540 struct drisw_drawable
*pdp
;
541 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
542 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
544 const __DRIswrastExtension
*swrast
= psc
->swrast
;
546 pdp
= calloc(1, sizeof(*pdp
));
550 pdp
->base
.xDrawable
= xDrawable
;
551 pdp
->base
.drawable
= drawable
;
552 pdp
->base
.psc
= &psc
->base
;
554 ret
= XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
560 /* Create a new drawable */
562 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
564 if (!pdp
->driDrawable
) {
565 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
570 pdp
->base
.destroyDrawable
= driswDestroyDrawable
;
576 driswSwapBuffers(__GLXDRIdrawable
* pdraw
,
577 int64_t target_msc
, int64_t divisor
, int64_t remainder
,
580 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
581 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
591 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
597 driswCopySubBuffer(__GLXDRIdrawable
* pdraw
,
598 int x
, int y
, int width
, int height
, Bool flush
)
600 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
601 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
607 (*psc
->copySubBuffer
->copySubBuffer
) (pdp
->driDrawable
,
608 x
, y
, width
, height
);
612 driswDestroyScreen(struct glx_screen
*base
)
614 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
616 /* Free the direct rendering per screen data */
617 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
618 driDestroyConfigs(psc
->driver_configs
);
619 psc
->driScreen
= NULL
;
621 dlclose(psc
->driver
);
625 #define SWRAST_DRIVER_NAME "swrast"
633 driver
= driOpenDriver(SWRAST_DRIVER_NAME
);
638 static const struct glx_screen_vtable drisw_screen_vtable
= {
639 .create_context
= drisw_create_context
,
640 .create_context_attribs
= drisw_create_context_attribs
,
641 .query_renderer_integer
= NULL
,
642 .query_renderer_string
= NULL
,
646 driswBindExtensions(struct drisw_screen
*psc
, const __DRIextension
**extensions
)
650 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
652 if (psc
->swrast
->base
.version
>= 3) {
653 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
654 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
656 /* DRISW version >= 2 implies support for OpenGL ES 2.0.
658 __glXEnableDirectExtension(&psc
->base
,
659 "GLX_EXT_create_context_es2_profile");
662 if (psc
->copySubBuffer
)
663 __glXEnableDirectExtension(&psc
->base
, "GLX_MESA_copy_sub_buffer");
665 /* FIXME: Figure out what other extensions can be ported here from dri2. */
666 for (i
= 0; extensions
[i
]; i
++) {
667 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
668 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
669 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
674 static struct glx_screen
*
675 driswCreateScreen(int screen
, struct glx_display
*priv
)
678 const __DRIconfig
**driver_configs
;
679 const __DRIextension
**extensions
;
680 struct drisw_screen
*psc
;
681 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
684 psc
= calloc(1, sizeof *psc
);
688 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
693 psc
->driver
= driOpenSwrast();
694 if (psc
->driver
== NULL
)
697 extensions
= driGetDriverExtensions(psc
->driver
, SWRAST_DRIVER_NAME
);
698 if (extensions
== NULL
)
701 for (i
= 0; extensions
[i
]; i
++) {
702 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
703 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
704 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
705 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
706 if (strcmp(extensions
[i
]->name
, __DRI_COPY_SUB_BUFFER
) == 0)
707 psc
->copySubBuffer
= (__DRIcopySubBufferExtension
*) extensions
[i
];
710 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
711 ErrorMessageF("core dri extension not found\n");
715 if (psc
->swrast
->base
.version
>= 4) {
717 psc
->swrast
->createNewScreen2(screen
, loader_extensions
,
719 &driver_configs
, psc
);
722 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
723 &driver_configs
, psc
);
725 if (psc
->driScreen
== NULL
) {
726 ErrorMessageF("failed to create dri screen\n");
730 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
731 driswBindExtensions(psc
, extensions
);
733 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
734 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
736 if (!configs
|| !visuals
)
739 glx_config_destroy_list(psc
->base
.configs
);
740 psc
->base
.configs
= configs
;
741 glx_config_destroy_list(psc
->base
.visuals
);
742 psc
->base
.visuals
= visuals
;
744 psc
->driver_configs
= driver_configs
;
746 psc
->base
.vtable
= &drisw_screen_vtable
;
748 psc
->base
.driScreen
= psp
;
749 psp
->destroyScreen
= driswDestroyScreen
;
750 psp
->createDrawable
= driswCreateDrawable
;
751 psp
->swapBuffers
= driswSwapBuffers
;
753 if (psc
->copySubBuffer
)
754 psp
->copySubBuffer
= driswCopySubBuffer
;
760 glx_config_destroy_list(configs
);
762 glx_config_destroy_list(visuals
);
764 psc
->core
->destroyScreen(psc
->driScreen
);
765 psc
->driScreen
= NULL
;
768 dlclose(psc
->driver
);
769 glx_screen_cleanup(&psc
->base
);
772 CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME
);
777 /* Called from __glXFreeDisplayPrivate.
780 driswDestroyDisplay(__GLXDRIdisplay
* dpy
)
786 * Allocate, initialize and return a __DRIdisplayPrivate object.
787 * This is called from __glXInitialize() when we are given a new
790 _X_HIDDEN __GLXDRIdisplay
*
791 driswCreateDisplay(Display
* dpy
)
793 struct drisw_display
*pdpyp
;
795 pdpyp
= malloc(sizeof *pdpyp
);
799 pdpyp
->base
.destroyDisplay
= driswDestroyDisplay
;
800 pdpyp
->base
.createScreen
= driswCreateScreen
;
805 #endif /* GLX_DIRECT_RENDERING */