1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
3 * Copyright 2008 George Sapountzis
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #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
42 struct __GLXDRIcontextPrivateRec
45 __DRIcontext
*driContext
;
46 __GLXscreenConfigs
*psc
;
49 struct __GLXDRIdrawablePrivateRec
51 __GLXDRIdrawable base
;
62 * swrast loader functions
66 XCreateDrawable(__GLXDRIdrawablePrivate
* pdp
,
67 Display
* dpy
, XID drawable
, int visualid
)
75 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
76 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
78 gcvalues
.function
= GXcopy
;
79 gcvalues
.graphics_exposures
= False
;
80 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
81 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
82 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
85 visTemp
.screen
= DefaultScreen(dpy
);
86 visTemp
.visualid
= visualid
;
87 visMask
= (VisualScreenMask
| VisualIDMask
);
88 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
90 pdp
->ximage
= XCreateImage(dpy
, pdp
->visinfo
->visual
, pdp
->visinfo
->depth
, ZPixmap
, 0, /* format, offset */
94 0); /* bytes_per_line */
96 /* get the true number of bits per pixel */
97 pdp
->bpp
= pdp
->ximage
->bits_per_pixel
;
103 XDestroyDrawable(__GLXDRIdrawablePrivate
* pdp
, Display
* dpy
, XID drawable
)
105 XDestroyImage(pdp
->ximage
);
108 XFreeGC(dpy
, pdp
->gc
);
109 XFreeGC(dpy
, pdp
->swapgc
);
113 swrastGetDrawableInfo(__DRIdrawable
* draw
,
114 int *x
, int *y
, int *w
, int *h
, void *loaderPrivate
)
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
, void *loaderPrivate
)
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
, void *loaderPrivate
)
179 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
180 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);;
181 Display
*dpy
= pdraw
->psc
->dpy
;
185 drawable
= pdraw
->xDrawable
;
187 ximage
= pdp
->ximage
;
191 ximage
->bytes_per_line
= bytes_per_line(w
, pdp
->bpp
, 32);
193 XGetSubImage(dpy
, drawable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
198 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
199 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
200 swrastGetDrawableInfo
,
205 static const __DRIextension
*loader_extensions
[] = {
206 &systemTimeExtension
.base
,
207 &swrastLoaderExtension
.base
,
216 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
);
228 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
,
235 draw
->driDrawable
, read
->driDrawable
);
239 driUnbindContext(__GLXDRIcontext
* context
)
241 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
242 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
244 (*core
->unbindContext
) (pcp
->driContext
);
247 static __GLXDRIcontext
*
248 driCreateContext(__GLXscreenConfigs
* psc
,
249 const __GLcontextModes
* mode
,
250 GLXContext gc
, 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
;
286 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
288 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
289 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
291 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
293 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
297 static __GLXDRIdrawable
*
298 driCreateDrawable(__GLXscreenConfigs
* psc
,
300 GLXDrawable drawable
, const __GLcontextModes
* modes
)
302 __GLXDRIdrawable
*pdraw
;
303 __GLXDRIdrawablePrivate
*pdp
;
304 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
305 const __DRIswrastExtension
*swrast
= psc
->swrast
;
307 /* Old dri can't handle GLX 1.3+ drawable constructors. */
308 if (xDrawable
!= drawable
)
311 pdp
= Xmalloc(sizeof(*pdp
));
315 pdraw
= &(pdp
->base
);
316 pdraw
->xDrawable
= xDrawable
;
317 pdraw
->drawable
= drawable
;
320 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
322 /* Create a new drawable */
324 (*swrast
->createNewDrawable
) (psc
->__driScreen
, config
->driConfig
, pdp
);
326 if (!pdraw
->driDrawable
) {
327 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
332 pdraw
->destroyDrawable
= driDestroyDrawable
;
338 driSwapBuffers(__GLXDRIdrawable
* pdraw
)
340 (*pdraw
->psc
->core
->swapBuffers
) (pdraw
->driDrawable
);
344 driDestroyScreen(__GLXscreenConfigs
* psc
)
346 /* Free the direct rendering per screen data */
347 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
348 psc
->__driScreen
= NULL
;
350 dlclose(psc
->driver
);
353 static __GLXDRIscreen
*
354 driCreateScreen(__GLXscreenConfigs
* psc
, int screen
,
355 __GLXdisplayPrivate
* priv
)
358 const __DRIconfig
**driver_configs
;
359 const __DRIextension
**extensions
;
360 const char *driverName
= "swrast";
363 psp
= Xmalloc(sizeof *psp
);
367 /* Initialize per screen dynamic client GLX extensions */
368 psc
->ext_list_first_time
= GL_TRUE
;
370 psc
->driver
= driOpenDriver(driverName
);
371 if (psc
->driver
== NULL
)
374 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
375 if (extensions
== NULL
) {
376 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
380 for (i
= 0; extensions
[i
]; i
++) {
381 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
382 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
383 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
384 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
387 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
388 ErrorMessageF("core dri extension not found\n");
393 psc
->swrast
->createNewScreen(screen
,
394 loader_extensions
, &driver_configs
, psc
);
395 if (psc
->__driScreen
== NULL
) {
396 ErrorMessageF("failed to create dri screen\n");
400 driBindExtensions(psc
, 0);
402 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
403 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
405 psp
->destroyScreen
= driDestroyScreen
;
406 psp
->createContext
= driCreateContext
;
407 psp
->createDrawable
= driCreateDrawable
;
408 psp
->swapBuffers
= driSwapBuffers
;
416 dlclose(psc
->driver
);
418 ErrorMessageF("reverting to indirect rendering\n");
423 /* Called from __glXFreeDisplayPrivate.
426 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
432 * Allocate, initialize and return a __DRIdisplayPrivate object.
433 * This is called from __glXInitialize() when we are given a new
436 _X_HIDDEN __GLXDRIdisplay
*
437 driswCreateDisplay(Display
* dpy
)
439 __GLXDRIdisplayPrivate
*pdpyp
;
441 pdpyp
= Xmalloc(sizeof *pdpyp
);
445 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
446 pdpyp
->base
.createScreen
= driCreateScreen
;
451 #endif /* GLX_DIRECT_RENDERING */