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 XCreateDrawable(__GLXDRIdrawablePrivate
* pdp
,
60 Display
* dpy
, XID drawable
, int visualid
)
68 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
69 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
71 gcvalues
.function
= GXcopy
;
72 gcvalues
.graphics_exposures
= False
;
73 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
74 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
75 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
78 visTemp
.screen
= DefaultScreen(dpy
);
79 visTemp
.visualid
= visualid
;
80 visMask
= (VisualScreenMask
| VisualIDMask
);
81 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
84 pdp
->ximage
= XCreateImage(dpy
,
87 ZPixmap
, 0, /* format, offset */
89 0, 0, /* width, height */
91 0); /* bytes_per_line */
97 XDestroyDrawable(__GLXDRIdrawablePrivate
* pdp
, Display
* dpy
, XID drawable
)
99 XDestroyImage(pdp
->ximage
);
102 XFreeGC(dpy
, pdp
->gc
);
103 XFreeGC(dpy
, pdp
->swapgc
);
107 * swrast loader functions
111 swrastGetDrawableInfo(__DRIdrawable
* draw
,
112 int *x
, int *y
, int *w
, int *h
,
115 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
116 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
117 Display
*dpy
= pdraw
->psc
->dpy
;
122 unsigned uw
, uh
, bw
, depth
;
124 drawable
= pdraw
->xDrawable
;
126 stat
= XGetGeometry(dpy
, drawable
, &root
,
127 x
, y
, &uw
, &uh
, &bw
, &depth
);
133 swrastPutImage2(__DRIdrawable
* draw
, int op
,
134 int x
, int y
, int w
, int h
,
135 char *data
, int pitch
, void *loaderPrivate
)
137 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
138 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
139 Display
*dpy
= pdraw
->psc
->dpy
;
145 case __DRI_SWRAST_IMAGE_OP_DRAW
:
148 case __DRI_SWRAST_IMAGE_OP_SWAP
:
155 drawable
= pdraw
->xDrawable
;
157 ximage
= pdp
->ximage
;
161 ximage
->bytes_per_line
= pitch
;
163 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
169 swrastGetImage2(__DRIdrawable
* read
,
170 int x
, int y
, int w
, int h
,
171 char *data
, int pitch
, void *loaderPrivate
)
173 __GLXDRIdrawablePrivate
*prp
= loaderPrivate
;
174 __GLXDRIdrawable
*pread
= &(prp
->base
);
175 Display
*dpy
= pread
->psc
->dpy
;
179 readable
= pread
->xDrawable
;
181 ximage
= prp
->ximage
;
185 ximage
->bytes_per_line
= pitch
;
187 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
193 * Align renderbuffer pitch.
195 * This should be chosen by the driver and the loader (libGL, xserver/glx)
196 * should use the driver provided pitch.
198 * It seems that the xorg loader (that is the xserver loading swrast_dri for
199 * indirect rendering, not client-side libGL) requires that the pitch is
200 * exactly the image width padded to 32 bits. XXX
202 * Is this a hard requirement that requires extra copies for different pitches
203 * or can the xorg loader use the driver pitch without extra copies ?
206 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
208 unsigned mask
= mul
- 1;
210 return ((pitch_bits
+ mask
) & ~mask
) / 8;
214 swrastPutImage(__DRIdrawable
* draw
, int op
,
215 int x
, int y
, int w
, int h
,
216 char *data
, void *loaderPrivate
)
218 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
221 bpp
= pdp
->ximage
->bits_per_pixel
;
223 pitch
= bytes_per_line(w
* bpp
, 32);
225 swrastPutImage2(draw
, op
, x
, y
, w
, h
, data
, pitch
, loaderPrivate
);
229 swrastGetImage(__DRIdrawable
* read
,
230 int x
, int y
, int w
, int h
,
231 char *data
, void *loaderPrivate
)
233 __GLXDRIdrawablePrivate
*prp
= loaderPrivate
;
236 bpp
= prp
->ximage
->bits_per_pixel
;
238 pitch
= bytes_per_line(w
* bpp
, 32);
240 swrastGetImage2(read
, x
, y
, w
, h
, data
, pitch
, loaderPrivate
);
243 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
244 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
245 swrastGetDrawableInfo
,
250 static const __DRIextension
*loader_extensions
[] = {
251 &systemTimeExtension
.base
,
252 &swrastLoaderExtension
.base
,
261 driDestroyContext(__GLXDRIcontext
* context
,
262 __GLXscreenConfigs
* psc
, Display
* dpy
)
264 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
265 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
267 (*core
->destroyContext
) (pcp
->driContext
);
273 driBindContext(__GLXDRIcontext
* context
,
274 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
276 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
277 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
279 return (*core
->bindContext
) (pcp
->driContext
,
280 draw
->driDrawable
, read
->driDrawable
);
284 driUnbindContext(__GLXDRIcontext
* context
)
286 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
287 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
289 (*core
->unbindContext
) (pcp
->driContext
);
292 static __GLXDRIcontext
*
293 driCreateContext(__GLXscreenConfigs
* psc
,
294 const __GLcontextModes
* mode
,
295 GLXContext gc
, GLXContext shareList
, int renderType
)
297 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
298 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
299 const __DRIcoreExtension
*core
;
300 __DRIcontext
*shared
= NULL
;
302 if (!psc
|| !psc
->driScreen
)
308 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
309 shared
= pcp_shared
->driContext
;
312 pcp
= Xmalloc(sizeof *pcp
);
318 (*core
->createNewContext
) (psc
->__driScreen
,
319 config
->driConfig
, shared
, pcp
);
320 if (pcp
->driContext
== NULL
) {
325 pcp
->base
.destroyContext
= driDestroyContext
;
326 pcp
->base
.bindContext
= driBindContext
;
327 pcp
->base
.unbindContext
= driUnbindContext
;
333 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
335 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
336 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
338 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
340 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
344 static __GLXDRIdrawable
*
345 driCreateDrawable(__GLXscreenConfigs
* psc
,
347 GLXDrawable drawable
, const __GLcontextModes
* modes
)
349 __GLXDRIdrawable
*pdraw
;
350 __GLXDRIdrawablePrivate
*pdp
;
351 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
352 const __DRIswrastExtension
*swrast
= psc
->swrast
;
354 /* Old dri can't handle GLX 1.3+ drawable constructors. */
355 if (xDrawable
!= drawable
)
358 pdp
= Xmalloc(sizeof(*pdp
));
362 pdraw
= &(pdp
->base
);
363 pdraw
->xDrawable
= xDrawable
;
364 pdraw
->drawable
= drawable
;
367 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
369 /* Create a new drawable */
371 (*swrast
->createNewDrawable
) (psc
->__driScreen
, config
->driConfig
, pdp
);
373 if (!pdraw
->driDrawable
) {
374 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
379 pdraw
->destroyDrawable
= driDestroyDrawable
;
385 driSwapBuffers(__GLXDRIdrawable
* pdraw
,
386 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
392 (*pdraw
->psc
->core
->swapBuffers
) (pdraw
->driDrawable
);
398 driDestroyScreen(__GLXscreenConfigs
* psc
)
400 /* Free the direct rendering per screen data */
401 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
402 psc
->__driScreen
= NULL
;
404 dlclose(psc
->driver
);
413 driver
= driOpenDriver("swrast");
416 driver
= driOpenDriver("swrastg");
421 static __GLXDRIscreen
*
422 driCreateScreen(__GLXscreenConfigs
* psc
, int screen
,
423 __GLXdisplayPrivate
* priv
)
426 const __DRIconfig
**driver_configs
;
427 const __DRIextension
**extensions
;
430 psp
= Xcalloc(1, sizeof *psp
);
434 psc
->driver
= driOpenSwrast();
435 if (psc
->driver
== NULL
)
438 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
439 if (extensions
== NULL
) {
440 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
444 for (i
= 0; extensions
[i
]; i
++) {
445 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
446 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
447 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
448 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
451 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
452 ErrorMessageF("core dri extension not found\n");
457 psc
->swrast
->createNewScreen(screen
,
458 loader_extensions
, &driver_configs
, psc
);
459 if (psc
->__driScreen
== NULL
) {
460 ErrorMessageF("failed to create dri screen\n");
464 driBindExtensions(psc
);
465 driBindCommonExtensions(psc
);
467 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
468 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
470 psc
->driver_configs
= driver_configs
;
472 psp
->destroyScreen
= driDestroyScreen
;
473 psp
->createContext
= driCreateContext
;
474 psp
->createDrawable
= driCreateDrawable
;
475 psp
->swapBuffers
= driSwapBuffers
;
485 dlclose(psc
->driver
);
487 ErrorMessageF("reverting to indirect rendering\n");
492 /* Called from __glXFreeDisplayPrivate.
495 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
501 * Allocate, initialize and return a __DRIdisplayPrivate object.
502 * This is called from __glXInitialize() when we are given a new
505 _X_HIDDEN __GLXDRIdisplay
*
506 driswCreateDisplay(Display
* dpy
)
508 __GLXDRIdisplayPrivate
*pdpyp
;
510 pdpyp
= Xmalloc(sizeof *pdpyp
);
514 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
515 pdpyp
->base
.createScreen
= driCreateScreen
;
520 #endif /* GLX_DIRECT_RENDERING */