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 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
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 * Align renderbuffer pitch.
135 * This should be chosen by the driver and the loader (libGL, xserver/glx)
136 * should use the driver provided pitch.
138 * It seems that the xorg loader (that is the xserver loading swrast_dri for
139 * indirect rendering, not client-side libGL) requires that the pitch is
140 * exactly the image width padded to 32 bits. XXX
142 * The above restriction can probably be overcome by using ScratchPixmap and
143 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
144 * the scratch pixmap to 'pitch / cpp'.
147 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
149 unsigned mask
= mul
- 1;
151 return ((pitch_bits
+ mask
) & ~mask
) / 8;
155 swrastPutImage(__DRIdrawable
* draw
, int op
,
156 int x
, int y
, int w
, int h
,
157 char *data
, void *loaderPrivate
)
159 __GLXDRIdrawablePrivate
*pdp
= loaderPrivate
;
160 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
161 Display
*dpy
= pdraw
->psc
->dpy
;
167 case __DRI_SWRAST_IMAGE_OP_DRAW
:
170 case __DRI_SWRAST_IMAGE_OP_SWAP
:
177 drawable
= pdraw
->xDrawable
;
179 ximage
= pdp
->ximage
;
183 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
185 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
191 swrastGetImage(__DRIdrawable
* read
,
192 int x
, int y
, int w
, int h
,
193 char *data
, void *loaderPrivate
)
195 __GLXDRIdrawablePrivate
*prp
= loaderPrivate
;
196 __GLXDRIdrawable
*pread
= &(prp
->base
);
197 Display
*dpy
= pread
->psc
->dpy
;
201 readable
= pread
->xDrawable
;
203 ximage
= prp
->ximage
;
207 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
209 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
214 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
215 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
216 swrastGetDrawableInfo
,
221 static const __DRIextension
*loader_extensions
[] = {
222 &systemTimeExtension
.base
,
223 &swrastLoaderExtension
.base
,
232 driDestroyContext(__GLXDRIcontext
* context
,
233 __GLXscreenConfigs
* psc
, Display
* dpy
)
235 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
236 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
238 (*core
->destroyContext
) (pcp
->driContext
);
244 driBindContext(__GLXDRIcontext
* context
,
245 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
247 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
248 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
250 return (*core
->bindContext
) (pcp
->driContext
,
251 draw
->driDrawable
, read
->driDrawable
);
255 driUnbindContext(__GLXDRIcontext
* context
)
257 __GLXDRIcontextPrivate
*pcp
= (__GLXDRIcontextPrivate
*) context
;
258 const __DRIcoreExtension
*core
= pcp
->psc
->core
;
260 (*core
->unbindContext
) (pcp
->driContext
);
263 static __GLXDRIcontext
*
264 driCreateContext(__GLXscreenConfigs
* psc
,
265 const __GLcontextModes
* mode
,
266 GLXContext gc
, GLXContext shareList
, int renderType
)
268 __GLXDRIcontextPrivate
*pcp
, *pcp_shared
;
269 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
270 const __DRIcoreExtension
*core
;
271 __DRIcontext
*shared
= NULL
;
273 if (!psc
|| !psc
->driScreen
)
279 pcp_shared
= (__GLXDRIcontextPrivate
*) shareList
->driContext
;
280 shared
= pcp_shared
->driContext
;
283 pcp
= Xmalloc(sizeof *pcp
);
289 (*core
->createNewContext
) (psc
->__driScreen
,
290 config
->driConfig
, shared
, pcp
);
291 if (pcp
->driContext
== NULL
) {
296 pcp
->base
.destroyContext
= driDestroyContext
;
297 pcp
->base
.bindContext
= driBindContext
;
298 pcp
->base
.unbindContext
= driUnbindContext
;
304 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
306 __GLXDRIdrawablePrivate
*pdp
= (__GLXDRIdrawablePrivate
*) pdraw
;
307 const __DRIcoreExtension
*core
= pdraw
->psc
->core
;
309 (*core
->destroyDrawable
) (pdraw
->driDrawable
);
311 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
315 static __GLXDRIdrawable
*
316 driCreateDrawable(__GLXscreenConfigs
* psc
,
318 GLXDrawable drawable
, const __GLcontextModes
* modes
)
320 __GLXDRIdrawable
*pdraw
;
321 __GLXDRIdrawablePrivate
*pdp
;
322 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
323 const __DRIswrastExtension
*swrast
= psc
->swrast
;
325 /* Old dri can't handle GLX 1.3+ drawable constructors. */
326 if (xDrawable
!= drawable
)
329 pdp
= Xmalloc(sizeof(*pdp
));
333 pdraw
= &(pdp
->base
);
334 pdraw
->xDrawable
= xDrawable
;
335 pdraw
->drawable
= drawable
;
338 XCreateDrawable(pdp
, psc
->dpy
, xDrawable
, modes
->visualID
);
340 /* Create a new drawable */
342 (*swrast
->createNewDrawable
) (psc
->__driScreen
, config
->driConfig
, pdp
);
344 if (!pdraw
->driDrawable
) {
345 XDestroyDrawable(pdp
, psc
->dpy
, xDrawable
);
350 pdraw
->destroyDrawable
= driDestroyDrawable
;
356 driSwapBuffers(__GLXDRIdrawable
* pdraw
,
357 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
363 (*pdraw
->psc
->core
->swapBuffers
) (pdraw
->driDrawable
);
369 driDestroyScreen(__GLXscreenConfigs
* psc
)
371 /* Free the direct rendering per screen data */
372 (*psc
->core
->destroyScreen
) (psc
->__driScreen
);
373 psc
->__driScreen
= NULL
;
375 dlclose(psc
->driver
);
384 driver
= driOpenDriver("swrast");
387 driver
= driOpenDriver("swrastg");
392 static __GLXDRIscreen
*
393 driCreateScreen(__GLXscreenConfigs
* psc
, int screen
,
394 __GLXdisplayPrivate
* priv
)
397 const __DRIconfig
**driver_configs
;
398 const __DRIextension
**extensions
;
401 psp
= Xcalloc(1, sizeof *psp
);
405 psc
->driver
= driOpenSwrast();
406 if (psc
->driver
== NULL
)
409 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
410 if (extensions
== NULL
) {
411 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
415 for (i
= 0; extensions
[i
]; i
++) {
416 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
417 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
418 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
419 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
422 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
423 ErrorMessageF("core dri extension not found\n");
428 psc
->swrast
->createNewScreen(screen
,
429 loader_extensions
, &driver_configs
, psc
);
430 if (psc
->__driScreen
== NULL
) {
431 ErrorMessageF("failed to create dri screen\n");
435 driBindExtensions(psc
);
436 driBindCommonExtensions(psc
);
438 psc
->configs
= driConvertConfigs(psc
->core
, psc
->configs
, driver_configs
);
439 psc
->visuals
= driConvertConfigs(psc
->core
, psc
->visuals
, driver_configs
);
441 psc
->driver_configs
= driver_configs
;
443 psp
->destroyScreen
= driDestroyScreen
;
444 psp
->createContext
= driCreateContext
;
445 psp
->createDrawable
= driCreateDrawable
;
446 psp
->swapBuffers
= driSwapBuffers
;
456 dlclose(psc
->driver
);
458 ErrorMessageF("reverting to indirect rendering\n");
463 /* Called from __glXFreeDisplayPrivate.
466 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
472 * Allocate, initialize and return a __DRIdisplayPrivate object.
473 * This is called from __glXInitialize() when we are given a new
476 _X_HIDDEN __GLXDRIdisplay
*
477 driswCreateDisplay(Display
* dpy
)
479 __GLXDRIdisplayPrivate
*pdpyp
;
481 pdpyp
= Xmalloc(sizeof *pdpyp
);
485 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
486 pdpyp
->base
.createScreen
= driCreateScreen
;
491 #endif /* GLX_DIRECT_RENDERING */