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
)
384 /* If the shareList context is not a DRISW context, we cannot possibly
385 * create a DRISW context that shares it.
387 if (shareList
->vtable
->destroy
!= drisw_destroy_context
) {
391 pcp_shared
= (struct drisw_context
*) shareList
;
392 shared
= pcp_shared
->driContext
;
395 pcp
= calloc(1, sizeof *pcp
);
399 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
405 (*psc
->core
->createNewContext
) (psc
->driScreen
,
406 config
->driConfig
, shared
, pcp
);
407 if (pcp
->driContext
== NULL
) {
412 pcp
->base
.vtable
= &drisw_context_vtable
;
417 static struct glx_context
*
418 drisw_create_context_attribs(struct glx_screen
*base
,
419 struct glx_config
*config_base
,
420 struct glx_context
*shareList
,
421 unsigned num_attribs
,
422 const uint32_t *attribs
,
425 struct drisw_context
*pcp
, *pcp_shared
;
426 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) config_base
;
427 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
428 __DRIcontext
*shared
= NULL
;
430 uint32_t minor_ver
= 1;
431 uint32_t major_ver
= 0;
434 int reset
= __DRI_CTX_RESET_NO_NOTIFICATION
;
435 uint32_t ctx_attribs
[2 * 4];
436 unsigned num_ctx_attribs
= 0;
438 if (!psc
->base
.driScreen
)
441 if (psc
->swrast
->base
.version
< 3)
444 /* Remap the GLX tokens to DRI2 tokens.
446 if (!dri2_convert_glx_attribs(num_attribs
, attribs
,
447 &major_ver
, &minor_ver
, &flags
, &api
, &reset
,
451 if (reset
!= __DRI_CTX_RESET_NO_NOTIFICATION
)
455 pcp_shared
= (struct drisw_context
*) shareList
;
456 shared
= pcp_shared
->driContext
;
459 pcp
= calloc(1, sizeof *pcp
);
463 if (!glx_context_init(&pcp
->base
, &psc
->base
, &config
->base
)) {
468 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
469 ctx_attribs
[num_ctx_attribs
++] = major_ver
;
470 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
471 ctx_attribs
[num_ctx_attribs
++] = minor_ver
;
474 ctx_attribs
[num_ctx_attribs
++] = __DRI_CTX_ATTRIB_FLAGS
;
476 /* The current __DRI_CTX_FLAG_* values are identical to the
477 * GLX_CONTEXT_*_BIT values.
479 ctx_attribs
[num_ctx_attribs
++] = flags
;
483 (*psc
->swrast
->createContextAttribs
) (psc
->driScreen
,
491 if (pcp
->driContext
== NULL
) {
496 pcp
->base
.vtable
= &drisw_context_vtable
;
502 driswDestroyDrawable(__GLXDRIdrawable
* pdraw
)
504 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
505 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
507 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
509 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
513 static __GLXDRIdrawable
*
514 driswCreateDrawable(struct glx_screen
*base
, XID xDrawable
,
515 GLXDrawable drawable
, struct glx_config
*modes
)
517 struct drisw_drawable
*pdp
;
518 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
519 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
521 const __DRIswrastExtension
*swrast
= psc
->swrast
;
523 pdp
= calloc(1, sizeof(*pdp
));
527 pdp
->base
.xDrawable
= xDrawable
;
528 pdp
->base
.drawable
= drawable
;
529 pdp
->base
.psc
= &psc
->base
;
531 ret
= XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
537 /* Create a new drawable */
539 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
541 if (!pdp
->driDrawable
) {
542 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
547 pdp
->base
.destroyDrawable
= driswDestroyDrawable
;
553 driswSwapBuffers(__GLXDRIdrawable
* pdraw
,
554 int64_t target_msc
, int64_t divisor
, int64_t remainder
,
557 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
558 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
568 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
574 driswDestroyScreen(struct glx_screen
*base
)
576 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
578 /* Free the direct rendering per screen data */
579 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
580 driDestroyConfigs(psc
->driver_configs
);
581 psc
->driScreen
= NULL
;
583 dlclose(psc
->driver
);
586 #define SWRAST_DRIVER_NAME "swrast"
594 driver
= driOpenDriver(SWRAST_DRIVER_NAME
);
599 static const struct glx_screen_vtable drisw_screen_vtable
= {
600 drisw_create_context
,
601 drisw_create_context_attribs
605 driswBindExtensions(struct drisw_screen
*psc
, const __DRIextension
**extensions
)
609 __glXEnableDirectExtension(&psc
->base
, "GLX_SGI_make_current_read");
611 if (psc
->swrast
->base
.version
>= 3) {
612 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context");
613 __glXEnableDirectExtension(&psc
->base
, "GLX_ARB_create_context_profile");
615 /* DRISW version >= 2 implies support for OpenGL ES 2.0.
617 __glXEnableDirectExtension(&psc
->base
,
618 "GLX_EXT_create_context_es2_profile");
621 /* FIXME: Figure out what other extensions can be ported here from dri2. */
622 for (i
= 0; extensions
[i
]; i
++) {
623 if ((strcmp(extensions
[i
]->name
, __DRI_TEX_BUFFER
) == 0)) {
624 psc
->texBuffer
= (__DRItexBufferExtension
*) extensions
[i
];
625 __glXEnableDirectExtension(&psc
->base
, "GLX_EXT_texture_from_pixmap");
630 static struct glx_screen
*
631 driswCreateScreen(int screen
, struct glx_display
*priv
)
634 const __DRIconfig
**driver_configs
;
635 const __DRIextension
**extensions
;
636 struct drisw_screen
*psc
;
637 struct glx_config
*configs
= NULL
, *visuals
= NULL
;
640 psc
= calloc(1, sizeof *psc
);
644 if (!glx_screen_init(&psc
->base
, screen
, priv
)) {
649 psc
->driver
= driOpenSwrast();
650 if (psc
->driver
== NULL
)
653 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
654 if (extensions
== NULL
) {
655 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
659 for (i
= 0; extensions
[i
]; i
++) {
660 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
661 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
662 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
663 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
666 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
667 ErrorMessageF("core dri extension not found\n");
672 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
673 &driver_configs
, psc
);
674 if (psc
->driScreen
== NULL
) {
675 ErrorMessageF("failed to create dri screen\n");
679 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
680 driswBindExtensions(psc
, extensions
);
682 configs
= driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
683 visuals
= driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
685 if (!configs
|| !visuals
)
688 glx_config_destroy_list(psc
->base
.configs
);
689 psc
->base
.configs
= configs
;
690 glx_config_destroy_list(psc
->base
.visuals
);
691 psc
->base
.visuals
= visuals
;
693 psc
->driver_configs
= driver_configs
;
695 psc
->base
.vtable
= &drisw_screen_vtable
;
697 psc
->base
.driScreen
= psp
;
698 psp
->destroyScreen
= driswDestroyScreen
;
699 psp
->createDrawable
= driswCreateDrawable
;
700 psp
->swapBuffers
= driswSwapBuffers
;
706 glx_config_destroy_list(configs
);
708 glx_config_destroy_list(visuals
);
710 psc
->core
->destroyScreen(psc
->driScreen
);
711 psc
->driScreen
= NULL
;
714 dlclose(psc
->driver
);
715 glx_screen_cleanup(&psc
->base
);
718 CriticalErrorMessageF("failed to load driver: %s\n", SWRAST_DRIVER_NAME
);
723 /* Called from __glXFreeDisplayPrivate.
726 driswDestroyDisplay(__GLXDRIdisplay
* dpy
)
732 * Allocate, initialize and return a __DRIdisplayPrivate object.
733 * This is called from __glXInitialize() when we are given a new
736 _X_HIDDEN __GLXDRIdisplay
*
737 driswCreateDisplay(Display
* dpy
)
739 struct drisw_display
*pdpyp
;
741 pdpyp
= malloc(sizeof *pdpyp
);
745 pdpyp
->base
.destroyDisplay
= driswDestroyDisplay
;
746 pdpyp
->base
.createScreen
= driswCreateScreen
;
751 #endif /* GLX_DIRECT_RENDERING */