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
= 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
;
285 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
287 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
288 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
290 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
292 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
296 static __GLXDRIdrawable
*
297 driCreateDrawable(__GLXscreenConfigs
* psc
,
299 GLXDrawable drawable
, const __GLcontextModes
* modes
)
301 __GLXDRIdrawable
*pdraw
;
302 __GLXDRIdrawablePrivate
*pdp
;
303 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
304 const __DRIswrastExtension
*swrast
= psc
->swrast
;
306 /* Old dri can't handle GLX 1.3+ drawable constructors. */
307 if (xDrawable
!= drawable
)
310 pdp
= Xmalloc(sizeof(*pdp
));
314 pdraw
= &(pdp
->base
);
315 pdraw
->xDrawable
= xDrawable
;
316 pdraw
->drawable
= drawable
;
319 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
321 /* Create a new drawable */
323 (*swrast
->createNewDrawable
) (psc
->__driScreen
, config
->driConfig
, pdp
);
325 if (!pdraw
->driDrawable
) {
326 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
331 pdraw
->destroyDrawable
= driDestroyDrawable
;
337 driSwapBuffers(__GLXDRIdrawable
* pdraw
)
339 (*pdraw
->psc
->core
->swapBuffers
) (pdraw
->driDrawable
);
343 driDestroyScreen(__GLXscreenConfigs
* psc
)
345 /* Free the direct rendering per screen data */
346 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
347 psc
->__driScreen
= NULL
;
349 dlclose(psc
->driver
);
352 static __GLXDRIscreen
*
353 driCreateScreen(__GLXscreenConfigs
* psc
, int screen
,
354 __GLXdisplayPrivate
* priv
)
357 const __DRIconfig
**driver_configs
;
358 const __DRIextension
**extensions
;
359 const char *driverName
= "swrast";
362 psp
= Xmalloc(sizeof *psp
);
366 /* Initialize per screen dynamic client GLX extensions */
367 psc
->ext_list_first_time
= GL_TRUE
;
369 psc
->driver
= driOpenDriver(driverName
);
370 if (psc
->driver
== NULL
)
373 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
374 if (extensions
== NULL
) {
375 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
379 for (i
= 0; extensions
[i
]; i
++) {
380 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
381 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
382 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
383 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
386 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
387 ErrorMessageF("core dri extension not found\n");
392 psc
->swrast
->createNewScreen(screen
,
393 loader_extensions
, &driver_configs
, psc
);
394 if (psc
->__driScreen
== NULL
) {
395 ErrorMessageF("failed to create dri screen\n");
399 driBindExtensions(psc
, 0);
401 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
402 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
404 psp
->destroyScreen
= driDestroyScreen
;
405 psp
->createContext
= driCreateContext
;
406 psp
->createDrawable
= driCreateDrawable
;
407 psp
->swapBuffers
= driSwapBuffers
;
415 dlclose(psc
->driver
);
417 ErrorMessageF("reverting to indirect rendering\n");
422 /* Called from __glXFreeDisplayPrivate.
425 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
431 * Allocate, initialize and return a __DRIdisplayPrivate object.
432 * This is called from __glXInitialize() when we are given a new
435 _X_HIDDEN __GLXDRIdisplay
*
436 driswCreateDisplay(Display
* dpy
)
438 __GLXDRIdisplayPrivate
*pdpyp
;
440 pdpyp
= Xmalloc(sizeof *pdpyp
);
444 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
445 pdpyp
->base
.createScreen
= driCreateScreen
;
450 #endif /* GLX_DIRECT_RENDERING */