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
;
59 * swrast loader functions
63 XCreateDrawable(__GLXDRIdrawablePrivate
* pdp
,
64 Display
* dpy
, XID drawable
, int visualid
)
72 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
73 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
75 gcvalues
.function
= GXcopy
;
76 gcvalues
.graphics_exposures
= False
;
77 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
78 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
79 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
82 visTemp
.screen
= DefaultScreen(dpy
);
83 visTemp
.visualid
= visualid
;
84 visMask
= (VisualScreenMask
| VisualIDMask
);
85 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
87 pdp
->ximage
= XCreateImage(dpy
,
90 ZPixmap
, 0, /* format, offset */
92 0, 0, /* width, height */
94 0); /* bytes_per_line */
100 XDestroyDrawable(__GLXDRIdrawablePrivate
* pdp
, Display
* dpy
, XID drawable
)
102 XDestroyImage(pdp
->ximage
);
105 XFreeGC(dpy
, pdp
->gc
);
106 XFreeGC(dpy
, pdp
->swapgc
);
110 swrastGetDrawableInfo(__DRIdrawable
* draw
,
111 int *x
, int *y
, int *w
, int *h
,
114 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
115 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
116 Display
*dpy
= pdraw
->psc
->dpy
;
121 unsigned uw
, uh
, bw
, depth
;
123 drawable
= pdraw
->xDrawable
;
125 stat
= XGetGeometry(dpy
, drawable
, &root
,
126 x
, y
, &uw
, &uh
, &bw
, &depth
);
132 swrastPutImage2(__DRIdrawable
* draw
, int op
,
133 int x
, int y
, int w
, int h
,
134 char *data
, int pitch
, void *loaderPrivate
)
136 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
137 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
138 Display
*dpy
= pdraw
->psc
->dpy
;
144 case __DRI_SWRAST_IMAGE_OP_DRAW
:
147 case __DRI_SWRAST_IMAGE_OP_SWAP
:
154 drawable
= pdraw
->xDrawable
;
156 ximage
= pdp
->ximage
;
160 ximage
->bytes_per_line
= pitch
;
162 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
168 swrastGetImage2(__DRIdrawable
* read
,
169 int x
, int y
, int w
, int h
,
170 char *data
, int pitch
, void *loaderPrivate
)
172 __GLXDRIdrawablePrivate
*prp
= loaderPrivate
;
173 __GLXDRIdrawable
*pread
= &(prp
->base
);
174 Display
*dpy
= pread
->psc
->dpy
;
178 readable
= pread
->xDrawable
;
180 ximage
= prp
->ximage
;
184 ximage
->bytes_per_line
= pitch
;
186 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
192 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
194 unsigned mask
= mul
- 1;
196 return ((pitch_bits
+ mask
) & ~mask
) / 8;
200 swrastPutImage(__DRIdrawable
* draw
, int op
,
201 int x
, int y
, int w
, int h
,
202 char *data
, void *loaderPrivate
)
204 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
207 bpp
= pdp
->ximage
->bits_per_pixel
;
209 pitch
= bytes_per_line(w
* bpp
, 32);
211 swrastPutImage2(draw
, op
, x
, y
, w
, h
, data
, pitch
, loaderPrivate
);
215 swrastGetImage(__DRIdrawable
* read
,
216 int x
, int y
, int w
, int h
,
217 char *data
, void *loaderPrivate
)
219 __GLXDRIdrawablePrivate
*prp
= loaderPrivate
;
222 bpp
= prp
->ximage
->bits_per_pixel
;
224 pitch
= bytes_per_line(w
* bpp
, 32);
226 swrastGetImage2(read
, x
, y
, w
, h
, data
, pitch
, loaderPrivate
);
229 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
230 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
231 swrastGetDrawableInfo
,
236 static const __DRIextension
*loader_extensions
[] = {
237 &systemTimeExtension
.base
,
238 &swrastLoaderExtension
.base
,
247 driDestroyContext(__GLXDRIcontext
* context
,
248 __GLXscreenConfigs
* psc
, Display
* dpy
)
250 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
251 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
253 (*core
->destroyContext
) (pcp
->driContext
);
259 driBindContext(__GLXDRIcontext
* context
,
260 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
262 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
263 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
265 return (*core
->bindContext
) (pcp
->driContext
,
266 draw
->driDrawable
, read
->driDrawable
);
270 driUnbindContext(__GLXDRIcontext
* context
)
272 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
273 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
275 (*core
->unbindContext
) (pcp
->driContext
);
278 static __GLXDRIcontext
*
279 driCreateContext(__GLXscreenConfigs
* psc
,
280 const __GLcontextModes
* mode
,
281 GLXContext gc
, GLXContext shareList
, int renderType
)
283 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
284 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
285 const __DRIcoreExtension
*core
;
286 __DRIcontext
*shared
= NULL
;
288 if (!psc
|| !psc
->driScreen
)
294 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
295 shared
= pcp_shared
->driContext
;
298 pcp
= Xmalloc(sizeof *pcp
);
304 (*core
->createNewContext
) (psc
->__driScreen
,
305 config
->driConfig
, shared
, pcp
);
306 if (pcp
->driContext
== NULL
) {
311 pcp
->base
.destroyContext
= driDestroyContext
;
312 pcp
->base
.bindContext
= driBindContext
;
313 pcp
->base
.unbindContext
= driUnbindContext
;
319 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
321 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
322 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
324 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
326 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
330 static __GLXDRIdrawable
*
331 driCreateDrawable(__GLXscreenConfigs
* psc
,
333 GLXDrawable drawable
, const __GLcontextModes
* modes
)
335 __GLXDRIdrawable
*pdraw
;
336 __GLXDRIdrawablePrivate
*pdp
;
337 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
338 const __DRIswrastExtension
*swrast
= psc
->swrast
;
340 /* Old dri can't handle GLX 1.3+ drawable constructors. */
341 if (xDrawable
!= drawable
)
344 pdp
= Xmalloc(sizeof(*pdp
));
348 pdraw
= &(pdp
->base
);
349 pdraw
->xDrawable
= xDrawable
;
350 pdraw
->drawable
= drawable
;
353 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
355 /* Create a new drawable */
357 (*swrast
->createNewDrawable
) (psc
->__driScreen
, config
->driConfig
, pdp
);
359 if (!pdraw
->driDrawable
) {
360 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
365 pdraw
->destroyDrawable
= driDestroyDrawable
;
371 driSwapBuffers(__GLXDRIdrawable
* pdraw
,
372 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
378 (*pdraw
->psc
->core
->swapBuffers
) (pdraw
->driDrawable
);
384 driDestroyScreen(__GLXscreenConfigs
* psc
)
386 /* Free the direct rendering per screen data */
387 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
388 psc
->__driScreen
= NULL
;
390 dlclose(psc
->driver
);
399 driver
= driOpenDriver("swrast");
402 driver
= driOpenDriver("swrastg");
407 static __GLXDRIscreen
*
408 driCreateScreen(__GLXscreenConfigs
* psc
, int screen
,
409 __GLXdisplayPrivate
* priv
)
412 const __DRIconfig
**driver_configs
;
413 const __DRIextension
**extensions
;
416 psp
= Xcalloc(1, sizeof *psp
);
420 psc
->driver
= driOpenSwrast();
421 if (psc
->driver
== NULL
)
424 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
425 if (extensions
== NULL
) {
426 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
430 for (i
= 0; extensions
[i
]; i
++) {
431 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
432 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
433 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
434 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
437 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
438 ErrorMessageF("core dri extension not found\n");
443 psc
->swrast
->createNewScreen(screen
,
444 loader_extensions
, &driver_configs
, psc
);
445 if (psc
->__driScreen
== NULL
) {
446 ErrorMessageF("failed to create dri screen\n");
450 driBindExtensions(psc
);
451 driBindCommonExtensions(psc
);
453 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
454 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
456 psc
->driver_configs
= driver_configs
;
458 psp
->destroyScreen
= driDestroyScreen
;
459 psp
->createContext
= driCreateContext
;
460 psp
->createDrawable
= driCreateDrawable
;
461 psp
->swapBuffers
= driSwapBuffers
;
471 dlclose(psc
->driver
);
473 ErrorMessageF("reverting to indirect rendering\n");
478 /* Called from __glXFreeDisplayPrivate.
481 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
487 * Allocate, initialize and return a __DRIdisplayPrivate object.
488 * This is called from __glXInitialize() when we are given a new
491 _X_HIDDEN __GLXDRIdisplay
*
492 driswCreateDisplay(Display
* dpy
)
494 __GLXDRIdisplayPrivate
*pdpyp
;
496 pdpyp
= Xmalloc(sizeof *pdpyp
);
500 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
501 pdpyp
->base
.createScreen
= driCreateScreen
;
506 #endif /* GLX_DIRECT_RENDERING */