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
);
95 pdp
->ximage
= XCreateImage(dpy
,
98 ZPixmap
, 0, /* format, offset */
100 0, 0, /* width, height */
102 0); /* bytes_per_line */
105 * swrast does not handle 24-bit depth with 24 bpp, so let X do the
106 * the conversion for us.
108 if (pdp
->ximage
->bits_per_pixel
== 24)
109 pdp
->ximage
->bits_per_pixel
= 32;
115 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
117 XDestroyImage(pdp
->ximage
);
120 XFreeGC(dpy
, pdp
->gc
);
121 XFreeGC(dpy
, pdp
->swapgc
);
125 * swrast loader functions
129 swrastGetDrawableInfo(__DRIdrawable
* draw
,
130 int *x
, int *y
, int *w
, int *h
,
133 struct drisw_drawable
*pdp
= loaderPrivate
;
134 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
135 Display
*dpy
= pdraw
->psc
->dpy
;
139 unsigned uw
, uh
, bw
, depth
;
141 drawable
= pdraw
->xDrawable
;
143 XGetGeometry(dpy
, drawable
, &root
, x
, y
, &uw
, &uh
, &bw
, &depth
);
149 * Align renderbuffer pitch.
151 * This should be chosen by the driver and the loader (libGL, xserver/glx)
152 * should use the driver provided pitch.
154 * It seems that the xorg loader (that is the xserver loading swrast_dri for
155 * indirect rendering, not client-side libGL) requires that the pitch is
156 * exactly the image width padded to 32 bits. XXX
158 * The above restriction can probably be overcome by using ScratchPixmap and
159 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
160 * the scratch pixmap to 'pitch / cpp'.
163 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
165 unsigned mask
= mul
- 1;
167 return ((pitch_bits
+ mask
) & ~mask
) / 8;
171 swrastPutImage(__DRIdrawable
* draw
, int op
,
172 int x
, int y
, int w
, int h
,
173 char *data
, void *loaderPrivate
)
175 struct drisw_drawable
*pdp
= loaderPrivate
;
176 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
177 Display
*dpy
= pdraw
->psc
->dpy
;
183 case __DRI_SWRAST_IMAGE_OP_DRAW
:
186 case __DRI_SWRAST_IMAGE_OP_SWAP
:
193 drawable
= pdraw
->xDrawable
;
195 ximage
= pdp
->ximage
;
199 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
201 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
207 swrastGetImage(__DRIdrawable
* read
,
208 int x
, int y
, int w
, int h
,
209 char *data
, void *loaderPrivate
)
211 struct drisw_drawable
*prp
= loaderPrivate
;
212 __GLXDRIdrawable
*pread
= &(prp
->base
);
213 Display
*dpy
= pread
->psc
->dpy
;
217 readable
= pread
->xDrawable
;
219 ximage
= prp
->ximage
;
223 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
225 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
230 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
231 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
232 swrastGetDrawableInfo
,
237 static const __DRIextension
*loader_extensions
[] = {
238 &systemTimeExtension
.base
,
239 &swrastLoaderExtension
.base
,
248 drisw_destroy_context(struct glx_context
*context
)
250 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
251 struct drisw_screen
*psc
= (struct drisw_screen
*) context
->psc
;
253 driReleaseDrawables(&pcp
->base
);
255 if (context
->extensions
)
256 XFree((char *) context
->extensions
);
258 (*psc
->core
->destroyContext
) (pcp
->driContext
);
264 drisw_bind_context(struct glx_context
*context
, struct glx_context
*old
,
265 GLXDrawable draw
, GLXDrawable read
)
267 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
268 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->base
.psc
;
269 struct drisw_drawable
*pdraw
, *pread
;
271 pdraw
= (struct drisw_drawable
*) driFetchDrawable(context
, draw
);
272 pread
= (struct drisw_drawable
*) driFetchDrawable(context
, read
);
274 driReleaseDrawables(&pcp
->base
);
276 if (pdraw
== NULL
|| pread
== NULL
)
277 return GLXBadDrawable
;
279 if ((*psc
->core
->bindContext
) (pcp
->driContext
,
280 pdraw
->driDrawable
, pread
->driDrawable
))
283 return GLXBadContext
;
287 drisw_unbind_context(struct glx_context
*context
, struct glx_context
*new)
289 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
290 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->base
.psc
;
292 (*psc
->core
->unbindContext
) (pcp
->driContext
);
296 drisw_bind_tex_image(Display
* dpy
,
297 GLXDrawable drawable
,
298 int buffer
, const int *attrib_list
)
300 struct glx_context
*gc
= __glXGetCurrentContext();
301 struct drisw_context
*pcp
= (struct drisw_context
*) gc
;
302 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
303 struct drisw_drawable
*pdraw
= (struct drisw_drawable
*) base
;
304 struct drisw_screen
*psc
;
306 __glXInitialize(dpy
);
309 psc
= (struct drisw_screen
*) base
->psc
;
314 if (psc
->texBuffer
->base
.version
>= 2 &&
315 psc
->texBuffer
->setTexBuffer2
!= NULL
) {
316 (*psc
->texBuffer
->setTexBuffer2
) (pcp
->driContext
,
317 pdraw
->base
.textureTarget
,
318 pdraw
->base
.textureFormat
,
322 (*psc
->texBuffer
->setTexBuffer
) (pcp
->driContext
,
323 pdraw
->base
.textureTarget
,
330 drisw_release_tex_image(Display
* dpy
, GLXDrawable drawable
, int buffer
)
332 #if __DRI_TEX_BUFFER_VERSION >= 3
333 struct glx_context
*gc
= __glXGetCurrentContext();
334 struct dri2_context
*pcp
= (struct dri2_context
*) gc
;
335 __GLXDRIdrawable
*base
= GetGLXDRIDrawable(dpy
, drawable
);
336 struct glx_display
*dpyPriv
= __glXInitialize(dpy
);
337 struct dri2_drawable
*pdraw
= (struct dri2_drawable
*) base
;
338 struct dri2_screen
*psc
;
341 psc
= (struct dri2_screen
*) base
->psc
;
346 if (psc
->texBuffer
->base
.version
>= 3 &&
347 psc
->texBuffer
->releaseTexBuffer
!= NULL
) {
348 (*psc
->texBuffer
->releaseTexBuffer
) (pcp
->driContext
,
349 pdraw
->base
.textureTarget
,
356 static const struct glx_context_vtable drisw_context_vtable
= {
357 drisw_destroy_context
,
359 drisw_unbind_context
,
363 drisw_bind_tex_image
,
364 drisw_release_tex_image
,
365 NULL
, /* get_proc_address */
368 static struct glx_context
*
369 drisw_create_context(struct glx_screen
*base
,
370 struct glx_config
*config_base
,
371 struct glx_context
*shareList
, int renderType
)
373 struct drisw_context
*pcp
, *pcp_shared
;
374 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
375 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
376 __DRIcontext
*shared
= NULL
;
378 if (!psc
->base
.driScreen
)
382 /* If the shareList context is not a DRISW context, we cannot possibly
383 * create a DRISW context that shares it.
385 if (shareList
->vtable
->destroy
!= drisw_destroy_context
) {
389 pcp_shared
= (struct drisw_context
*) shareList
;
390 shared
= pcp_shared
->driContext
;
393 pcp
= Xmalloc(sizeof *pcp
);
397 memset(pcp
, 0, sizeof *pcp
);
398 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
404 (*psc
->core
->createNewContext
) (psc
->driScreen
,
405 config
->driConfig
, shared
, pcp
);
406 if (pcp
->driContext
== NULL
) {
411 pcp
->base
.vtable
= &drisw_context_vtable
;
416 static struct glx_context
*
417 drisw_create_context_attribs(struct glx_screen
*base
,
418 struct glx_config
*config_base
,
419 struct glx_context
*shareList
,
420 unsigned num_attribs
,
421 const uint32_t *attribs
,
424 struct drisw_context
*pcp
, *pcp_shared
;
425 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
426 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
427 __DRIcontext
*shared
= NULL
;
429 uint32_t minor_ver
= 1;
430 uint32_t major_ver
= 0;
433 uint32_t ctx_attribs
[2 * 4];
434 unsigned num_ctx_attribs
= 0;
436 if (!psc
->base
.driScreen
)
439 if (psc
->swrast
->base
.version
< 3)
442 /* Remap the GLX tokens to DRI2 tokens.
444 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
445 &major_ver
, &minor_ver
, &flags
, &api
,
450 pcp_shared
= (struct drisw_context
*) shareList
;
451 shared
= pcp_shared
->driContext
;
454 pcp
= Xmalloc(sizeof *pcp
);
458 memset(pcp
, 0, sizeof *pcp
);
459 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
464 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
465 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
466 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
467 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
470 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
472 /* The current __DRI_CTX_FLAG_* values are identical to the
473 * GLX_CONTEXT_*_BIT values.
475 ctx_attribs
[num_ctx_attribs
++] = flags
;
479 (*psc
->swrast
->createContextAttribs
) (psc
->driScreen
,
487 if (pcp
->driContext
== NULL
) {
492 pcp
->base
.vtable
= &drisw_context_vtable
;
498 driswDestroyDrawable(__GLXDRIdrawable
* pdraw
)
500 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
501 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
503 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
505 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
509 static __GLXDRIdrawable
*
510 driswCreateDrawable(struct glx_screen
*base
, XID xDrawable
,
511 GLXDrawable drawable
, struct glx_config
*modes
)
513 struct drisw_drawable
*pdp
;
514 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
515 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
517 const __DRIswrastExtension
*swrast
= psc
->swrast
;
519 pdp
= Xmalloc(sizeof(*pdp
));
523 memset(pdp
, 0, sizeof *pdp
);
524 pdp
->base
.xDrawable
= xDrawable
;
525 pdp
->base
.drawable
= drawable
;
526 pdp
->base
.psc
= &psc
->base
;
528 XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
530 /* Create a new drawable */
532 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
534 if (!pdp
->driDrawable
) {
535 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
540 pdp
->base
.destroyDrawable
= driswDestroyDrawable
;
546 driswSwapBuffers(__GLXDRIdrawable
* pdraw
,
547 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
549 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
550 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
556 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
562 driswDestroyScreen(struct glx_screen
*base
)
564 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
566 /* Free the direct rendering per screen data */
567 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
568 driDestroyConfigs(psc
->driver_configs
);
569 psc
->driScreen
= NULL
;
571 dlclose(psc
->driver
);
580 driver
= driOpenDriver("swrast");
585 static const struct glx_screen_vtable drisw_screen_vtable
= {
586 drisw_create_context
,
587 drisw_create_context_attribs
591 driswBindExtensions(struct drisw_screen
*psc
, const __DRIextension
**extensions
)
595 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
597 if (psc
->swrast
->base
.version
>= 3) {
598 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
599 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
601 /* DRISW version >= 2 implies support for OpenGL ES 2.0.
603 __glXEnableDirectExtension(&psc
->base
,
604 "GLX_EXT_create_context_es2_profile");
607 /* FIXME: Figure out what other extensions can be ported here from dri2. */
608 for (i
= 0; extensions
[i
]; i
++) {
609 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
610 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
611 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
616 static struct glx_screen
*
617 driswCreateScreen(int screen
, struct glx_display
*priv
)
620 const __DRIconfig
**driver_configs
;
621 const __DRIextension
**extensions
;
622 struct drisw_screen
*psc
;
623 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
626 psc
= Xcalloc(1, sizeof *psc
);
630 memset(psc
, 0, sizeof *psc
);
631 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
636 psc
->driver
= driOpenSwrast();
637 if (psc
->driver
== NULL
)
640 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
641 if (extensions
== NULL
) {
642 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
646 for (i
= 0; extensions
[i
]; i
++) {
647 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
648 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
649 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
650 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
653 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
654 ErrorMessageF("core dri extension not found\n");
659 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
660 &driver_configs
, psc
);
661 if (psc
->driScreen
== NULL
) {
662 ErrorMessageF("failed to create dri screen\n");
666 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
667 driswBindExtensions(psc
, extensions
);
669 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
670 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
672 if (!configs
|| !visuals
)
675 glx_config_destroy_list(psc
->base
.configs
);
676 psc
->base
.configs
= configs
;
677 glx_config_destroy_list(psc
->base
.visuals
);
678 psc
->base
.visuals
= visuals
;
680 psc
->driver_configs
= driver_configs
;
682 psc
->base
.vtable
= &drisw_screen_vtable
;
684 psc
->base
.driScreen
= psp
;
685 psp
->destroyScreen
= driswDestroyScreen
;
686 psp
->createDrawable
= driswCreateDrawable
;
687 psp
->swapBuffers
= driswSwapBuffers
;
693 glx_config_destroy_list(configs
);
695 glx_config_destroy_list(visuals
);
697 psc
->core
->destroyScreen(psc
->driScreen
);
698 psc
->driScreen
= NULL
;
701 dlclose(psc
->driver
);
702 glx_screen_cleanup(&psc
->base
);
705 ErrorMessageF("reverting to indirect rendering\n");
710 /* Called from __glXFreeDisplayPrivate.
713 driswDestroyDisplay(__GLXDRIdisplay
* dpy
)
719 * Allocate, initialize and return a __DRIdisplayPrivate object.
720 * This is called from __glXInitialize() when we are given a new
723 _X_HIDDEN __GLXDRIdisplay
*
724 driswCreateDisplay(Display
* dpy
)
726 struct drisw_display
*pdpyp
;
728 pdpyp
= Xmalloc(sizeof *pdpyp
);
732 pdpyp
->base
.destroyDisplay
= driswDestroyDisplay
;
733 pdpyp
->base
.createScreen
= driswCreateScreen
;
738 #endif /* GLX_DIRECT_RENDERING */