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"
39 __DRIcontext
*driContext
;
40 __GLXscreenConfigs
*psc
;
45 __GLXscreenConfigs base
;
47 __DRIscreen
*driScreen
;
48 __GLXDRIscreen vtable
;
49 const __DRIcoreExtension
*core
;
50 const __DRIswrastExtension
*swrast
;
56 __GLXDRIdrawable base
;
61 __DRIdrawable
*driDrawable
;
67 XCreateDrawable(struct drisw_drawable
* pdp
,
68 Display
* dpy
, XID drawable
, int visualid
)
76 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
77 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
79 gcvalues
.function
= GXcopy
;
80 gcvalues
.graphics_exposures
= False
;
81 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
82 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
83 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
86 visTemp
.screen
= DefaultScreen(dpy
);
87 visTemp
.visualid
= visualid
;
88 visMask
= (VisualScreenMask
| VisualIDMask
);
89 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
92 pdp
->ximage
= XCreateImage(dpy
,
95 ZPixmap
, 0, /* format, offset */
97 0, 0, /* width, height */
99 0); /* bytes_per_line */
105 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
107 XDestroyImage(pdp
->ximage
);
110 XFreeGC(dpy
, pdp
->gc
);
111 XFreeGC(dpy
, pdp
->swapgc
);
115 * swrast loader functions
119 swrastGetDrawableInfo(__DRIdrawable
* draw
,
120 int *x
, int *y
, int *w
, int *h
,
123 struct drisw_drawable
*pdp
= loaderPrivate
;
124 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
125 Display
*dpy
= pdraw
->psc
->dpy
;
130 unsigned uw
, uh
, bw
, depth
;
132 drawable
= pdraw
->xDrawable
;
134 stat
= XGetGeometry(dpy
, drawable
, &root
,
135 x
, y
, &uw
, &uh
, &bw
, &depth
);
141 * Align renderbuffer pitch.
143 * This should be chosen by the driver and the loader (libGL, xserver/glx)
144 * should use the driver provided pitch.
146 * It seems that the xorg loader (that is the xserver loading swrast_dri for
147 * indirect rendering, not client-side libGL) requires that the pitch is
148 * exactly the image width padded to 32 bits. XXX
150 * The above restriction can probably be overcome by using ScratchPixmap and
151 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
152 * the scratch pixmap to 'pitch / cpp'.
155 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
157 unsigned mask
= mul
- 1;
159 return ((pitch_bits
+ mask
) & ~mask
) / 8;
163 swrastPutImage(__DRIdrawable
* draw
, int op
,
164 int x
, int y
, int w
, int h
,
165 char *data
, void *loaderPrivate
)
167 struct drisw_drawable
*pdp
= loaderPrivate
;
168 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
169 Display
*dpy
= pdraw
->psc
->dpy
;
175 case __DRI_SWRAST_IMAGE_OP_DRAW
:
178 case __DRI_SWRAST_IMAGE_OP_SWAP
:
185 drawable
= pdraw
->xDrawable
;
187 ximage
= pdp
->ximage
;
191 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
193 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
199 swrastGetImage(__DRIdrawable
* read
,
200 int x
, int y
, int w
, int h
,
201 char *data
, void *loaderPrivate
)
203 struct drisw_drawable
*prp
= loaderPrivate
;
204 __GLXDRIdrawable
*pread
= &(prp
->base
);
205 Display
*dpy
= pread
->psc
->dpy
;
209 readable
= pread
->xDrawable
;
211 ximage
= prp
->ximage
;
215 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
217 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
222 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
223 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
224 swrastGetDrawableInfo
,
229 static const __DRIextension
*loader_extensions
[] = {
230 &systemTimeExtension
.base
,
231 &swrastLoaderExtension
.base
,
240 driDestroyContext(__GLXDRIcontext
*context
,
241 __GLXscreenConfigs
*base
, Display
*dpy
)
243 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
244 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
246 (*psc
->core
->destroyContext
) (pcp
->driContext
);
252 driBindContext(__GLXDRIcontext
* context
,
253 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
255 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
256 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->psc
;
257 struct drisw_drawable
*pdr
= (struct drisw_drawable
*) draw
;
258 struct drisw_drawable
*prd
= (struct drisw_drawable
*) read
;
260 return (*psc
->core
->bindContext
) (pcp
->driContext
,
261 pdr
->driDrawable
, prd
->driDrawable
);
265 driUnbindContext(__GLXDRIcontext
* context
)
267 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
268 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->psc
;
270 (*psc
->core
->unbindContext
) (pcp
->driContext
);
273 static __GLXDRIcontext
*
274 driCreateContext(__GLXscreenConfigs
*base
,
275 const __GLcontextModes
*mode
,
276 GLXContext gc
, GLXContext shareList
, int renderType
)
278 struct drisw_context
*pcp
, *pcp_shared
;
279 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
280 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
281 __DRIcontext
*shared
= NULL
;
283 if (!psc
->base
.driScreen
)
287 pcp_shared
= (struct drisw_context
*) shareList
->driContext
;
288 shared
= pcp_shared
->driContext
;
291 pcp
= Xmalloc(sizeof *pcp
);
295 pcp
->psc
= &psc
->base
;
297 (*psc
->core
->createNewContext
) (psc
->driScreen
,
298 config
->driConfig
, shared
, pcp
);
299 if (pcp
->driContext
== NULL
) {
304 pcp
->base
.destroyContext
= driDestroyContext
;
305 pcp
->base
.bindContext
= driBindContext
;
306 pcp
->base
.unbindContext
= driUnbindContext
;
312 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
314 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
315 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
317 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
319 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
323 static __GLXDRIdrawable
*
324 driCreateDrawable(__GLXscreenConfigs
*base
, XID xDrawable
,
325 GLXDrawable drawable
, const __GLcontextModes
* modes
)
327 struct drisw_drawable
*pdp
;
328 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
329 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
331 const __DRIswrastExtension
*swrast
= psc
->swrast
;
333 /* Old dri can't handle GLX 1.3+ drawable constructors. */
334 if (xDrawable
!= drawable
)
337 pdp
= Xmalloc(sizeof(*pdp
));
341 pdp
->base
.xDrawable
= xDrawable
;
342 pdp
->base
.drawable
= drawable
;
343 pdp
->base
.psc
= &psc
->base
;
345 XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
347 /* Create a new drawable */
349 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
351 if (!pdp
->driDrawable
) {
352 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
357 pdp
->base
.destroyDrawable
= driDestroyDrawable
;
363 driSwapBuffers(__GLXDRIdrawable
* pdraw
,
364 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
366 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
367 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
373 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
379 driDestroyScreen(__GLXscreenConfigs
*base
)
381 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
383 /* Free the direct rendering per screen data */
384 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
385 psc
->driScreen
= NULL
;
387 dlclose(psc
->driver
);
396 driver
= driOpenDriver("swrast");
399 driver
= driOpenDriver("swrastg");
404 static __GLXscreenConfigs
*
405 driCreateScreen(int screen
, __GLXdisplayPrivate
*priv
)
408 const __DRIconfig
**driver_configs
;
409 const __DRIextension
**extensions
;
410 struct drisw_screen
*psc
;
413 psc
= Xcalloc(1, sizeof *psc
);
417 memset(psc
, 0, sizeof *psc
);
418 if (!glx_screen_init(&psc
->base
, screen
, priv
))
421 psc
->driver
= driOpenSwrast();
422 if (psc
->driver
== NULL
)
425 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
426 if (extensions
== NULL
) {
427 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
431 for (i
= 0; extensions
[i
]; i
++) {
432 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
433 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
434 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
435 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
438 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
439 ErrorMessageF("core dri extension not found\n");
444 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
445 &driver_configs
, psc
);
446 if (psc
->driScreen
== NULL
) {
447 ErrorMessageF("failed to create dri screen\n");
451 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
452 driBindCommonExtensions(&psc
->base
, extensions
);
455 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
457 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
459 psc
->base
.driver_configs
= driver_configs
;
462 psc
->base
.driScreen
= psp
;
463 psp
->destroyScreen
= driDestroyScreen
;
464 psp
->createContext
= driCreateContext
;
465 psp
->createDrawable
= driCreateDrawable
;
466 psp
->swapBuffers
= driSwapBuffers
;
476 dlclose(psc
->driver
);
478 ErrorMessageF("reverting to indirect rendering\n");
483 /* Called from __glXFreeDisplayPrivate.
486 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
492 * Allocate, initialize and return a __DRIdisplayPrivate object.
493 * This is called from __glXInitialize() when we are given a new
496 _X_HIDDEN __GLXDRIdisplay
*
497 driswCreateDisplay(Display
* dpy
)
499 struct drisw_display
*pdpyp
;
501 pdpyp
= Xmalloc(sizeof *pdpyp
);
505 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
506 pdpyp
->base
.createScreen
= driCreateScreen
;
511 #endif /* GLX_DIRECT_RENDERING */