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 #ifdef GLX_DIRECT_RENDERING
27 #include "glxclient.h"
28 #include "glcontextmodes.h"
30 #include "dri_common.h"
32 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate
;
33 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate
;
34 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate
;
36 struct __GLXDRIdisplayPrivateRec
{
40 struct __GLXDRIcontextPrivateRec
{
42 __DRIcontext
*driContext
;
43 __GLXscreenConfigs
*psc
;
46 struct __GLXDRIdrawablePrivateRec
{
47 __GLXDRIdrawable base
;
58 * swrast loader functions
61 static Bool
XCreateDrawable(__GLXDRIdrawablePrivate
*pdp
,
62 Display
*dpy
, XID drawable
, int visualid
)
70 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
71 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
73 gcvalues
.function
= GXcopy
;
74 gcvalues
.graphics_exposures
= False
;
75 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
76 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
77 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
80 visTemp
.screen
= DefaultScreen(dpy
);
81 visTemp
.visualid
= visualid
;
82 visMask
= (VisualScreenMask
| VisualIDMask
);
83 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
85 pdp
->ximage
= XCreateImage(dpy
,
88 ZPixmap
, 0, /* format, offset */
92 0); /* bytes_per_line */
94 /* get the true number of bits per pixel */
95 pdp
->bpp
= pdp
->ximage
->bits_per_pixel
;
100 static void XDestroyDrawable(__GLXDRIdrawablePrivate
*pdp
,
101 Display
*dpy
, XID drawable
)
103 XDestroyImage(pdp
->ximage
);
106 XFreeGC(dpy
, pdp
->gc
);
107 XFreeGC(dpy
, pdp
->swapgc
);
111 swrastGetDrawableInfo(__DRIdrawable
*draw
,
112 int *x
, int *y
, int *w
, int *h
,
115 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
116 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);;
117 Display
*dpy
= pdraw
->psc
->dpy
;
122 unsigned int bw
, depth
;
124 drawable
= pdraw
->xDrawable
;
126 stat
= XGetGeometry(dpy
, drawable
, &root
,
127 x
, y
, (unsigned int *)w
, (unsigned int *)h
,
132 bytes_per_line(int w
, int bpp
, unsigned mul
)
134 unsigned mask
= mul
- 1;
136 return ((w
* bpp
+ mask
) & ~mask
) / 8;
140 swrastPutImage(__DRIdrawable
*draw
, int op
,
141 int x
, int y
, int w
, int h
, char *data
,
144 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
145 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);;
146 Display
*dpy
= pdraw
->psc
->dpy
;
152 case __DRI_SWRAST_IMAGE_OP_DRAW
:
155 case __DRI_SWRAST_IMAGE_OP_SWAP
:
162 drawable
= pdraw
->xDrawable
;
164 ximage
= pdp
->ximage
;
168 ximage
->bytes_per_line
= bytes_per_line(w
, pdp
->bpp
, 32);
170 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
176 swrastGetImage(__DRIdrawable
*draw
,
177 int x
, int y
, int w
, int h
, char *data
,
180 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
181 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);;
182 Display
*dpy
= pdraw
->psc
->dpy
;
186 drawable
= pdraw
->xDrawable
;
188 ximage
= pdp
->ximage
;
192 ximage
->bytes_per_line
= bytes_per_line(w
, pdp
->bpp
, 32);
194 XGetSubImage(dpy
, drawable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
199 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
200 { __DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
201 swrastGetDrawableInfo
,
206 static const __DRIextension
*loader_extensions
[] = {
207 &systemTimeExtension
.base
,
208 &swrastLoaderExtension
.base
,
216 static void driDestroyContext(__GLXDRIcontext
*context
,
217 __GLXscreenConfigs
*psc
, Display
*dpy
)
219 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
220 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
222 (*core
->destroyContext
)(pcp
->driContext
);
227 static Bool
driBindContext(__GLXDRIcontext
*context
,
228 __GLXDRIdrawable
*draw
, __GLXDRIdrawable
*read
)
230 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
231 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
233 return (*core
->bindContext
)(pcp
->driContext
,
238 static void driUnbindContext(__GLXDRIcontext
*context
)
240 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
241 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
243 (*core
->unbindContext
)(pcp
->driContext
);
246 static __GLXDRIcontext
*driCreateContext(__GLXscreenConfigs
*psc
,
247 const __GLcontextModes
*mode
,
249 GLXContext shareList
, int renderType
)
251 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
252 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
253 const __DRIcoreExtension
*core
= psc
->core
;
254 __DRIcontext
*shared
= NULL
;
256 if (!psc
|| !psc
->driScreen
)
260 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
261 shared
= pcp_shared
->driContext
;
264 pcp
= Xmalloc(sizeof *pcp
);
270 (*core
->createNewContext
)(psc
->__driScreen
,
271 config
->driConfig
, shared
, pcp
);
272 if (pcp
->driContext
== NULL
) {
277 pcp
->base
.destroyContext
= driDestroyContext
;
278 pcp
->base
.bindContext
= driBindContext
;
279 pcp
->base
.unbindContext
= driUnbindContext
;
284 static void driDestroyDrawable(__GLXDRIdrawable
*pdraw
)
286 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
287 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
289 (*core
->destroyDrawable
)(pdraw
->driDrawable
);
291 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
295 static __GLXDRIdrawable
*driCreateDrawable(__GLXscreenConfigs
*psc
,
297 GLXDrawable drawable
,
298 const __GLcontextModes
*modes
)
300 __GLXDRIdrawable
*pdraw
;
301 __GLXDRIdrawablePrivate
*pdp
;
302 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
303 const __DRIswrastExtension
*swrast
= psc
->swrast
;
305 /* Old dri can't handle GLX 1.3+ drawable constructors. */
306 if (xDrawable
!= drawable
)
309 pdp
= Xmalloc(sizeof(*pdp
));
313 pdraw
= &(pdp
->base
);
314 pdraw
->xDrawable
= xDrawable
;
315 pdraw
->drawable
= drawable
;
318 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
320 /* Create a new drawable */
322 (*swrast
->createNewDrawable
)(psc
->__driScreen
,
326 if (!pdraw
->driDrawable
) {
327 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
332 pdraw
->destroyDrawable
= driDestroyDrawable
;
337 static void driSwapBuffers(__GLXDRIdrawable
*pdraw
)
339 (*pdraw
->psc
->core
->swapBuffers
)(pdraw
->driDrawable
);
342 static void driDestroyScreen(__GLXscreenConfigs
*psc
)
344 /* Free the direct rendering per screen data */
345 (*psc
->core
->destroyScreen
)(psc
->__driScreen
);
346 psc
->__driScreen
= NULL
;
348 dlclose(psc
->driver
);
351 static __GLXDRIscreen
*driCreateScreen(__GLXscreenConfigs
*psc
, int screen
,
352 __GLXdisplayPrivate
*priv
)
355 const __DRIconfig
**driver_configs
;
356 const __DRIextension
**extensions
;
357 const char *driverName
= "swrast";
360 psp
= Xmalloc(sizeof *psp
);
364 /* Initialize per screen dynamic client GLX extensions */
365 psc
->ext_list_first_time
= GL_TRUE
;
367 psc
->driver
= driOpenDriver(driverName
);
368 if (psc
->driver
== NULL
)
371 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
372 if (extensions
== NULL
) {
373 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
377 for (i
= 0; extensions
[i
]; i
++) {
378 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
379 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
380 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
381 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
384 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
385 ErrorMessageF("core dri extension not found\n");
390 psc
->swrast
->createNewScreen(screen
,
391 loader_extensions
, &driver_configs
, psc
);
392 if (psc
->__driScreen
== NULL
) {
393 ErrorMessageF("failed to create dri screen\n");
397 driBindExtensions(psc
, 0);
399 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
400 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
402 psp
->destroyScreen
= driDestroyScreen
;
403 psp
->createContext
= driCreateContext
;
404 psp
->createDrawable
= driCreateDrawable
;
405 psp
->swapBuffers
= driSwapBuffers
;
413 dlclose(psc
->driver
);
415 ErrorMessageF("reverting to indirect rendering\n");
420 /* Called from __glXFreeDisplayPrivate.
422 static void driDestroyDisplay(__GLXDRIdisplay
*dpy
)
428 * Allocate, initialize and return a __DRIdisplayPrivate object.
429 * This is called from __glXInitialize() when we are given a new
432 _X_HIDDEN __GLXDRIdisplay
*driswCreateDisplay(Display
*dpy
)
434 __GLXDRIdisplayPrivate
*pdpyp
;
436 pdpyp
= Xmalloc(sizeof *pdpyp
);
440 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
441 pdpyp
->base
.createScreen
= driCreateScreen
;
446 #endif /* GLX_DIRECT_RENDERING */