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
;
66 XCreateDrawable(struct drisw_drawable
* 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
);
91 pdp
->ximage
= XCreateImage(dpy
,
94 ZPixmap
, 0, /* format, offset */
96 0, 0, /* width, height */
98 0); /* bytes_per_line */
104 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
106 XDestroyImage(pdp
->ximage
);
109 XFreeGC(dpy
, pdp
->gc
);
110 XFreeGC(dpy
, pdp
->swapgc
);
114 * swrast loader functions
118 swrastGetDrawableInfo(__DRIdrawable
* draw
,
119 int *x
, int *y
, int *w
, int *h
,
122 struct drisw_drawable
*pdp
= loaderPrivate
;
123 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
124 Display
*dpy
= pdraw
->psc
->dpy
;
129 unsigned uw
, uh
, bw
, depth
;
131 drawable
= pdraw
->xDrawable
;
133 stat
= XGetGeometry(dpy
, drawable
, &root
,
134 x
, y
, &uw
, &uh
, &bw
, &depth
);
140 * Align renderbuffer pitch.
142 * This should be chosen by the driver and the loader (libGL, xserver/glx)
143 * should use the driver provided pitch.
145 * It seems that the xorg loader (that is the xserver loading swrast_dri for
146 * indirect rendering, not client-side libGL) requires that the pitch is
147 * exactly the image width padded to 32 bits. XXX
149 * The above restriction can probably be overcome by using ScratchPixmap and
150 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
151 * the scratch pixmap to 'pitch / cpp'.
154 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
156 unsigned mask
= mul
- 1;
158 return ((pitch_bits
+ mask
) & ~mask
) / 8;
162 swrastPutImage(__DRIdrawable
* draw
, int op
,
163 int x
, int y
, int w
, int h
,
164 char *data
, void *loaderPrivate
)
166 struct drisw_drawable
*pdp
= loaderPrivate
;
167 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
168 Display
*dpy
= pdraw
->psc
->dpy
;
174 case __DRI_SWRAST_IMAGE_OP_DRAW
:
177 case __DRI_SWRAST_IMAGE_OP_SWAP
:
184 drawable
= pdraw
->xDrawable
;
186 ximage
= pdp
->ximage
;
190 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
192 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
198 swrastGetImage(__DRIdrawable
* read
,
199 int x
, int y
, int w
, int h
,
200 char *data
, void *loaderPrivate
)
202 struct drisw_drawable
*prp
= loaderPrivate
;
203 __GLXDRIdrawable
*pread
= &(prp
->base
);
204 Display
*dpy
= pread
->psc
->dpy
;
208 readable
= pread
->xDrawable
;
210 ximage
= prp
->ximage
;
214 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
216 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
221 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
222 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
223 swrastGetDrawableInfo
,
228 static const __DRIextension
*loader_extensions
[] = {
229 &systemTimeExtension
.base
,
230 &swrastLoaderExtension
.base
,
239 driDestroyContext(__GLXDRIcontext
*context
,
240 __GLXscreenConfigs
*base
, Display
*dpy
)
242 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
243 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
245 (*psc
->core
->destroyContext
) (pcp
->driContext
);
251 driBindContext(__GLXDRIcontext
* context
,
252 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
254 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
255 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->psc
;
257 return (*psc
->core
->bindContext
) (pcp
->driContext
,
258 draw
->driDrawable
, read
->driDrawable
);
262 driUnbindContext(__GLXDRIcontext
* context
)
264 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
265 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->psc
;
267 (*psc
->core
->unbindContext
) (pcp
->driContext
);
270 static __GLXDRIcontext
*
271 driCreateContext(__GLXscreenConfigs
*base
,
272 const __GLcontextModes
*mode
,
273 GLXContext gc
, GLXContext shareList
, int renderType
)
275 struct drisw_context
*pcp
, *pcp_shared
;
276 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
277 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
278 __DRIcontext
*shared
= NULL
;
280 if (!psc
->base
.driScreen
)
284 pcp_shared
= (struct drisw_context
*) shareList
->driContext
;
285 shared
= pcp_shared
->driContext
;
288 pcp
= Xmalloc(sizeof *pcp
);
292 pcp
->psc
= &psc
->base
;
294 (*psc
->core
->createNewContext
) (psc
->driScreen
,
295 config
->driConfig
, shared
, pcp
);
296 if (pcp
->driContext
== NULL
) {
301 pcp
->base
.destroyContext
= driDestroyContext
;
302 pcp
->base
.bindContext
= driBindContext
;
303 pcp
->base
.unbindContext
= driUnbindContext
;
309 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
311 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
312 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
314 (*psc
->core
->destroyDrawable
) (pdraw
->driDrawable
);
316 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
320 static __GLXDRIdrawable
*
321 driCreateDrawable(__GLXscreenConfigs
*base
, XID xDrawable
,
322 GLXDrawable drawable
, const __GLcontextModes
* modes
)
324 __GLXDRIdrawable
*pdraw
;
325 struct drisw_drawable
*pdp
;
326 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
327 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
329 const __DRIswrastExtension
*swrast
= psc
->swrast
;
331 /* Old dri can't handle GLX 1.3+ drawable constructors. */
332 if (xDrawable
!= drawable
)
335 pdp
= Xmalloc(sizeof(*pdp
));
339 pdraw
= &(pdp
->base
);
340 pdraw
->xDrawable
= xDrawable
;
341 pdraw
->drawable
= drawable
;
342 pdraw
->psc
= &psc
->base
;
344 XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
346 /* Create a new drawable */
348 (*swrast
->createNewDrawable
) (psc
->driScreen
,
349 config
->driConfig
, pdp
);
351 if (!pdraw
->driDrawable
) {
352 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
357 pdraw
->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
) (pdraw
->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 */