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
28 #include "glxclient.h"
29 #include "glcontextmodes.h"
31 #include "dri_common.h"
33 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate
;
34 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate
;
35 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate
;
37 struct __GLXDRIdisplayPrivateRec
{
41 struct __GLXDRIcontextPrivateRec
{
43 __DRIcontext
*driContext
;
44 __GLXscreenConfigs
*psc
;
47 struct __GLXDRIdrawablePrivateRec
{
48 __GLXDRIdrawable base
;
59 * swrast loader functions
62 static Bool
XCreateDrawable(__GLXDRIdrawablePrivate
*pdp
,
63 Display
*dpy
, XID drawable
, int visualid
)
71 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
72 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
74 gcvalues
.function
= GXcopy
;
75 gcvalues
.graphics_exposures
= False
;
76 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
77 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
78 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
81 visTemp
.screen
= DefaultScreen(dpy
);
82 visTemp
.visualid
= visualid
;
83 visMask
= (VisualScreenMask
| VisualIDMask
);
84 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
86 pdp
->ximage
= XCreateImage(dpy
,
89 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
;
101 static void XDestroyDrawable(__GLXDRIdrawablePrivate
*pdp
,
102 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
,
116 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
117 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);;
118 Display
*dpy
= pdraw
->psc
->dpy
;
123 unsigned int bw
, depth
;
125 drawable
= pdraw
->xDrawable
;
127 stat
= XGetGeometry(dpy
, drawable
, &root
,
128 x
, y
, (unsigned int *)w
, (unsigned int *)h
,
133 bytes_per_line(int w
, int bpp
, unsigned mul
)
135 unsigned mask
= mul
- 1;
137 return ((w
* bpp
+ mask
) & ~mask
) / 8;
141 swrastPutImage(__DRIdrawable
*draw
, int op
,
142 int x
, int y
, int w
, int h
, char *data
,
145 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
146 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);;
147 Display
*dpy
= pdraw
->psc
->dpy
;
153 case __DRI_SWRAST_IMAGE_OP_DRAW
:
156 case __DRI_SWRAST_IMAGE_OP_SWAP
:
163 drawable
= pdraw
->xDrawable
;
165 ximage
= pdp
->ximage
;
169 ximage
->bytes_per_line
= bytes_per_line(w
, pdp
->bpp
, 32);
171 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
177 swrastGetImage(__DRIdrawable
*draw
,
178 int x
, int y
, int w
, int h
, char *data
,
181 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
182 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);;
183 Display
*dpy
= pdraw
->psc
->dpy
;
187 drawable
= pdraw
->xDrawable
;
189 ximage
= pdp
->ximage
;
193 ximage
->bytes_per_line
= bytes_per_line(w
, pdp
->bpp
, 32);
195 XGetSubImage(dpy
, drawable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
200 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
201 { __DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
202 swrastGetDrawableInfo
,
207 static const __DRIextension
*loader_extensions
[] = {
208 &systemTimeExtension
.base
,
209 &swrastLoaderExtension
.base
,
217 static void driDestroyContext(__GLXDRIcontext
*context
,
218 __GLXscreenConfigs
*psc
, Display
*dpy
)
220 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
221 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
223 (*core
->destroyContext
)(pcp
->driContext
);
228 static Bool
driBindContext(__GLXDRIcontext
*context
,
229 __GLXDRIdrawable
*draw
, __GLXDRIdrawable
*read
)
231 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
232 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
234 return (*core
->bindContext
)(pcp
->driContext
,
239 static void driUnbindContext(__GLXDRIcontext
*context
)
241 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
242 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
244 (*core
->unbindContext
)(pcp
->driContext
);
247 static __GLXDRIcontext
*driCreateContext(__GLXscreenConfigs
*psc
,
248 const __GLcontextModes
*mode
,
250 GLXContext shareList
, int renderType
)
252 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
253 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
254 const __DRIcoreExtension
*core
= psc
->core
;
255 __DRIcontext
*shared
= NULL
;
257 if (!psc
|| !psc
->driScreen
)
261 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
262 shared
= pcp_shared
->driContext
;
265 pcp
= Xmalloc(sizeof *pcp
);
271 (*core
->createNewContext
)(psc
->__driScreen
,
272 config
->driConfig
, shared
, pcp
);
273 if (pcp
->driContext
== NULL
) {
278 pcp
->base
.destroyContext
= driDestroyContext
;
279 pcp
->base
.bindContext
= driBindContext
;
280 pcp
->base
.unbindContext
= driUnbindContext
;
285 static void 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
*driCreateDrawable(__GLXscreenConfigs
*psc
,
298 GLXDrawable drawable
,
299 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
,
327 if (!pdraw
->driDrawable
) {
328 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
333 pdraw
->destroyDrawable
= driDestroyDrawable
;
338 static void driDestroyScreen(__GLXscreenConfigs
*psc
)
340 /* Free the direct rendering per screen data */
341 (*psc
->core
->destroyScreen
)(psc
->__driScreen
);
342 psc
->__driScreen
= NULL
;
344 dlclose(psc
->driver
);
347 static __GLXDRIscreen
*driCreateScreen(__GLXscreenConfigs
*psc
, int screen
,
348 __GLXdisplayPrivate
*priv
)
351 const __DRIconfig
**driver_configs
;
352 const __DRIextension
**extensions
;
353 const char *driverName
= "swrast";
356 psp
= Xmalloc(sizeof *psp
);
360 /* Initialize per screen dynamic client GLX extensions */
361 psc
->ext_list_first_time
= GL_TRUE
;
363 psc
->driver
= driOpenDriver(driverName
);
364 if (psc
->driver
== NULL
)
367 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
368 if (extensions
== NULL
) {
369 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
373 for (i
= 0; extensions
[i
]; i
++) {
374 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
375 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
376 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
377 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
380 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
381 ErrorMessageF("core dri extension not found\n");
386 psc
->swrast
->createNewScreen(screen
,
387 loader_extensions
, &driver_configs
, psc
);
388 if (psc
->__driScreen
== NULL
) {
389 ErrorMessageF("failed to create dri screen\n");
393 driBindExtensions(psc
, 0);
395 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
396 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
398 psp
->destroyScreen
= driDestroyScreen
;
399 psp
->createContext
= driCreateContext
;
400 psp
->createDrawable
= driCreateDrawable
;
408 dlclose(psc
->driver
);
410 ErrorMessageF("reverting to indirect rendering\n");
415 /* Called from __glXFreeDisplayPrivate.
417 static void driDestroyDisplay(__GLXDRIdisplay
*dpy
)
423 * Allocate, initialize and return a __DRIdisplayPrivate object.
424 * This is called from __glXInitialize() when we are given a new
427 _X_HIDDEN __GLXDRIdisplay
*driswCreateDisplay(Display
*dpy
)
429 __GLXDRIdisplayPrivate
*pdpyp
;
431 pdpyp
= Xmalloc(sizeof *pdpyp
);
435 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
436 pdpyp
->base
.createScreen
= driCreateScreen
;
441 #endif /* GLX_DIRECT_RENDERING */