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
41 struct __GLXDRIcontextPrivateRec
44 __DRIcontext
*driContext
;
45 __GLXscreenConfigs
*psc
;
48 struct __GLXDRIdrawablePrivateRec
50 __GLXDRIdrawable base
;
61 * swrast loader functions
65 XCreateDrawable(__GLXDRIdrawablePrivate
* pdp
,
66 Display
* dpy
, XID drawable
, int visualid
)
74 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
75 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
77 gcvalues
.function
= GXcopy
;
78 gcvalues
.graphics_exposures
= False
;
79 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
80 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
81 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
84 visTemp
.screen
= DefaultScreen(dpy
);
85 visTemp
.visualid
= visualid
;
86 visMask
= (VisualScreenMask
| VisualIDMask
);
87 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
89 pdp
->ximage
= XCreateImage(dpy
, pdp
->visinfo
->visual
, pdp
->visinfo
->depth
, ZPixmap
, 0, /* format, offset */
93 0); /* bytes_per_line */
95 /* get the true number of bits per pixel */
96 pdp
->bpp
= pdp
->ximage
->bits_per_pixel
;
102 XDestroyDrawable(__GLXDRIdrawablePrivate
* pdp
, Display
* dpy
, XID drawable
)
104 XDestroyImage(pdp
->ximage
);
107 XFreeGC(dpy
, pdp
->gc
);
108 XFreeGC(dpy
, pdp
->swapgc
);
112 swrastGetDrawableInfo(__DRIdrawable
* draw
,
113 int *x
, int *y
, int *w
, int *h
, void *loaderPrivate
)
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
, void *loaderPrivate
)
143 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
144 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
145 Display
*dpy
= pdraw
->psc
->dpy
;
151 case __DRI_SWRAST_IMAGE_OP_DRAW
:
154 case __DRI_SWRAST_IMAGE_OP_SWAP
:
161 drawable
= pdraw
->xDrawable
;
163 ximage
= pdp
->ximage
;
167 ximage
->bytes_per_line
= bytes_per_line(w
, pdp
->bpp
, 32);
169 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
175 swrastGetImage(__DRIdrawable
* draw
,
176 int x
, int y
, int w
, int h
, char *data
, void *loaderPrivate
)
178 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
179 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
180 Display
*dpy
= pdraw
->psc
->dpy
;
184 drawable
= pdraw
->xDrawable
;
186 ximage
= pdp
->ximage
;
190 ximage
->bytes_per_line
= bytes_per_line(w
, pdp
->bpp
, 32);
192 XGetSubImage(dpy
, drawable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
197 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
198 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
199 swrastGetDrawableInfo
,
204 static const __DRIextension
*loader_extensions
[] = {
205 &systemTimeExtension
.base
,
206 &swrastLoaderExtension
.base
,
215 driDestroyContext(__GLXDRIcontext
* context
,
216 __GLXscreenConfigs
* psc
, Display
* dpy
)
218 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
219 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
221 (*core
->destroyContext
) (pcp
->driContext
);
227 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
,
234 draw
->driDrawable
, read
->driDrawable
);
238 driUnbindContext(__GLXDRIcontext
* context
)
240 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
241 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
243 (*core
->unbindContext
) (pcp
->driContext
);
246 static __GLXDRIcontext
*
247 driCreateContext(__GLXscreenConfigs
* psc
,
248 const __GLcontextModes
* mode
,
249 GLXContext gc
, GLXContext shareList
, int renderType
)
251 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
252 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
253 const __DRIcoreExtension
*core
;
254 __DRIcontext
*shared
= NULL
;
256 if (!psc
|| !psc
->driScreen
)
262 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
263 shared
= pcp_shared
->driContext
;
266 pcp
= Xmalloc(sizeof *pcp
);
272 (*core
->createNewContext
) (psc
->__driScreen
,
273 config
->driConfig
, shared
, pcp
);
274 if (pcp
->driContext
== NULL
) {
279 pcp
->base
.destroyContext
= driDestroyContext
;
280 pcp
->base
.bindContext
= driBindContext
;
281 pcp
->base
.unbindContext
= driUnbindContext
;
287 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
289 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
290 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
292 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
294 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
298 static __GLXDRIdrawable
*
299 driCreateDrawable(__GLXscreenConfigs
* psc
,
301 GLXDrawable drawable
, const __GLcontextModes
* modes
)
303 __GLXDRIdrawable
*pdraw
;
304 __GLXDRIdrawablePrivate
*pdp
;
305 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
306 const __DRIswrastExtension
*swrast
= psc
->swrast
;
308 /* Old dri can't handle GLX 1.3+ drawable constructors. */
309 if (xDrawable
!= drawable
)
312 pdp
= Xmalloc(sizeof(*pdp
));
316 pdraw
= &(pdp
->base
);
317 pdraw
->xDrawable
= xDrawable
;
318 pdraw
->drawable
= drawable
;
321 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
323 /* Create a new drawable */
325 (*swrast
->createNewDrawable
) (psc
->__driScreen
, config
->driConfig
, pdp
);
327 if (!pdraw
->driDrawable
) {
328 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
333 pdraw
->destroyDrawable
= driDestroyDrawable
;
339 driSwapBuffers(__GLXDRIdrawable
* pdraw
)
341 (*pdraw
->psc
->core
->swapBuffers
) (pdraw
->driDrawable
);
345 driDestroyScreen(__GLXscreenConfigs
* psc
)
347 /* Free the direct rendering per screen data */
348 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
349 psc
->__driScreen
= NULL
;
351 dlclose(psc
->driver
);
354 static __GLXDRIscreen
*
355 driCreateScreen(__GLXscreenConfigs
* psc
, int screen
,
356 __GLXdisplayPrivate
* priv
)
359 const __DRIconfig
**driver_configs
;
360 const __DRIextension
**extensions
;
361 const char *driverName
= "swrast";
364 psp
= Xcalloc(1, sizeof *psp
);
368 /* Initialize per screen dynamic client GLX extensions */
369 psc
->ext_list_first_time
= GL_TRUE
;
371 psc
->driver
= driOpenDriver(driverName
);
372 if (psc
->driver
== NULL
)
375 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
376 if (extensions
== NULL
) {
377 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
381 for (i
= 0; extensions
[i
]; i
++) {
382 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
383 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
384 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
385 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
388 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
389 ErrorMessageF("core dri extension not found\n");
394 psc
->swrast
->createNewScreen(screen
,
395 loader_extensions
, &driver_configs
, psc
);
396 if (psc
->__driScreen
== NULL
) {
397 ErrorMessageF("failed to create dri screen\n");
401 driBindExtensions(psc
);
402 driBindCommonExtensions(psc
);
404 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
405 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
407 psc
->driver_configs
= driver_configs
;
409 psp
->destroyScreen
= driDestroyScreen
;
410 psp
->createContext
= driCreateContext
;
411 psp
->createDrawable
= driCreateDrawable
;
412 psp
->swapBuffers
= driSwapBuffers
;
422 dlclose(psc
->driver
);
424 ErrorMessageF("reverting to indirect rendering\n");
429 /* Called from __glXFreeDisplayPrivate.
432 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
438 * Allocate, initialize and return a __DRIdisplayPrivate object.
439 * This is called from __glXInitialize() when we are given a new
442 _X_HIDDEN __GLXDRIdisplay
*
443 driswCreateDisplay(Display
* dpy
)
445 __GLXDRIdisplayPrivate
*pdpyp
;
447 pdpyp
= Xmalloc(sizeof *pdpyp
);
451 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
452 pdpyp
->base
.createScreen
= driCreateScreen
;
457 #endif /* GLX_DIRECT_RENDERING */