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"
29 #include "dri_common.h"
31 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate
;
32 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate
;
33 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate
;
35 struct __GLXDRIdisplayPrivateRec
40 struct __GLXDRIcontextPrivateRec
43 __DRIcontext
*driContext
;
44 __GLXscreenConfigs
*psc
;
47 struct __GLXDRIdrawablePrivateRec
49 __GLXDRIdrawable base
;
60 * swrast loader functions
64 XCreateDrawable(__GLXDRIdrawablePrivate
* pdp
,
65 Display
* dpy
, XID drawable
, int visualid
)
73 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
74 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
76 gcvalues
.function
= GXcopy
;
77 gcvalues
.graphics_exposures
= False
;
78 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
79 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
80 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
83 visTemp
.screen
= DefaultScreen(dpy
);
84 visTemp
.visualid
= visualid
;
85 visMask
= (VisualScreenMask
| VisualIDMask
);
86 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
88 pdp
->ximage
= XCreateImage(dpy
,
91 ZPixmap
, 0, /* format, offset */
95 0); /* bytes_per_line */
97 /* get the true number of bits per pixel */
98 pdp
->bpp
= pdp
->ximage
->bits_per_pixel
;
104 XDestroyDrawable(__GLXDRIdrawablePrivate
* pdp
, Display
* dpy
, XID drawable
)
106 XDestroyImage(pdp
->ximage
);
109 XFreeGC(dpy
, pdp
->gc
);
110 XFreeGC(dpy
, pdp
->swapgc
);
114 swrastGetDrawableInfo(__DRIdrawable
* draw
,
115 int *x
, int *y
, int *w
, int *h
, void *loaderPrivate
)
117 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
118 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
119 Display
*dpy
= pdraw
->psc
->dpy
;
124 unsigned int bw
, depth
;
126 drawable
= pdraw
->xDrawable
;
128 stat
= XGetGeometry(dpy
, drawable
, &root
,
129 x
, y
, (unsigned int *) w
, (unsigned int *) h
,
134 bytes_per_line(int w
, int bpp
, unsigned mul
)
136 unsigned mask
= mul
- 1;
138 return ((w
* bpp
+ mask
) & ~mask
) / 8;
142 swrastPutImage(__DRIdrawable
* draw
, int op
,
143 int x
, int y
, int w
, int h
, char *data
, void *loaderPrivate
)
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
, void *loaderPrivate
)
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
,
217 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
);
229 driBindContext(__GLXDRIcontext
* context
,
230 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
232 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
233 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
235 return (*core
->bindContext
) (pcp
->driContext
,
236 draw
->driDrawable
, read
->driDrawable
);
240 driUnbindContext(__GLXDRIcontext
* context
)
242 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
243 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
245 (*core
->unbindContext
) (pcp
->driContext
);
248 static __GLXDRIcontext
*
249 driCreateContext(__GLXscreenConfigs
* psc
,
250 const __GLcontextModes
* mode
,
251 GLXContext gc
, GLXContext shareList
, int renderType
)
253 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
254 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
255 const __DRIcoreExtension
*core
;
256 __DRIcontext
*shared
= NULL
;
258 if (!psc
|| !psc
->driScreen
)
264 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
265 shared
= pcp_shared
->driContext
;
268 pcp
= Xmalloc(sizeof *pcp
);
274 (*core
->createNewContext
) (psc
->__driScreen
,
275 config
->driConfig
, shared
, pcp
);
276 if (pcp
->driContext
== NULL
) {
281 pcp
->base
.destroyContext
= driDestroyContext
;
282 pcp
->base
.bindContext
= driBindContext
;
283 pcp
->base
.unbindContext
= driUnbindContext
;
289 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
291 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
292 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
294 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
296 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
300 static __GLXDRIdrawable
*
301 driCreateDrawable(__GLXscreenConfigs
* psc
,
303 GLXDrawable drawable
, const __GLcontextModes
* modes
)
305 __GLXDRIdrawable
*pdraw
;
306 __GLXDRIdrawablePrivate
*pdp
;
307 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
308 const __DRIswrastExtension
*swrast
= psc
->swrast
;
310 /* Old dri can't handle GLX 1.3+ drawable constructors. */
311 if (xDrawable
!= drawable
)
314 pdp
= Xmalloc(sizeof(*pdp
));
318 pdraw
= &(pdp
->base
);
319 pdraw
->xDrawable
= xDrawable
;
320 pdraw
->drawable
= drawable
;
323 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
325 /* Create a new drawable */
327 (*swrast
->createNewDrawable
) (psc
->__driScreen
, config
->driConfig
, pdp
);
329 if (!pdraw
->driDrawable
) {
330 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
335 pdraw
->destroyDrawable
= driDestroyDrawable
;
341 driSwapBuffers(__GLXDRIdrawable
* pdraw
,
342 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
348 (*pdraw
->psc
->core
->swapBuffers
) (pdraw
->driDrawable
);
354 driDestroyScreen(__GLXscreenConfigs
* psc
)
356 /* Free the direct rendering per screen data */
357 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
358 psc
->__driScreen
= NULL
;
360 dlclose(psc
->driver
);
363 static __GLXDRIscreen
*
364 driCreateScreen(__GLXscreenConfigs
* psc
, int screen
,
365 __GLXdisplayPrivate
* priv
)
368 const __DRIconfig
**driver_configs
;
369 const __DRIextension
**extensions
;
370 const char *driverName
= "swrast";
373 psp
= Xcalloc(1, sizeof *psp
);
377 psc
->driver
= driOpenDriver(driverName
);
378 if (psc
->driver
== NULL
)
381 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
382 if (extensions
== NULL
) {
383 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
387 for (i
= 0; extensions
[i
]; i
++) {
388 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
389 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
390 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
391 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
394 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
395 ErrorMessageF("core dri extension not found\n");
400 psc
->swrast
->createNewScreen(screen
,
401 loader_extensions
, &driver_configs
, psc
);
402 if (psc
->__driScreen
== NULL
) {
403 ErrorMessageF("failed to create dri screen\n");
407 driBindExtensions(psc
);
408 driBindCommonExtensions(psc
);
410 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
411 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
413 psc
->driver_configs
= driver_configs
;
415 psp
->destroyScreen
= driDestroyScreen
;
416 psp
->createContext
= driCreateContext
;
417 psp
->createDrawable
= driCreateDrawable
;
418 psp
->swapBuffers
= driSwapBuffers
;
428 dlclose(psc
->driver
);
430 ErrorMessageF("reverting to indirect rendering\n");
435 /* Called from __glXFreeDisplayPrivate.
438 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
444 * Allocate, initialize and return a __DRIdisplayPrivate object.
445 * This is called from __glXInitialize() when we are given a new
448 _X_HIDDEN __GLXDRIdisplay
*
449 driswCreateDisplay(Display
* dpy
)
451 __GLXDRIdisplayPrivate
*pdpyp
;
453 pdpyp
= Xmalloc(sizeof *pdpyp
);
457 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
458 pdpyp
->base
.createScreen
= driCreateScreen
;
463 #endif /* GLX_DIRECT_RENDERING */