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
;
51 const __DRIconfig
**driver_configs
;
58 __GLXDRIdrawable base
;
63 __DRIdrawable
*driDrawable
;
69 XCreateDrawable(struct drisw_drawable
* pdp
,
70 Display
* dpy
, XID drawable
, int visualid
)
78 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
79 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
81 gcvalues
.function
= GXcopy
;
82 gcvalues
.graphics_exposures
= False
;
83 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
84 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
85 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
88 visTemp
.screen
= DefaultScreen(dpy
);
89 visTemp
.visualid
= visualid
;
90 visMask
= (VisualScreenMask
| VisualIDMask
);
91 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
94 pdp
->ximage
= XCreateImage(dpy
,
97 ZPixmap
, 0, /* format, offset */
99 0, 0, /* width, height */
101 0); /* bytes_per_line */
107 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
109 XDestroyImage(pdp
->ximage
);
112 XFreeGC(dpy
, pdp
->gc
);
113 XFreeGC(dpy
, pdp
->swapgc
);
117 * swrast loader functions
121 swrastGetDrawableInfo(__DRIdrawable
* draw
,
122 int *x
, int *y
, int *w
, int *h
,
125 struct drisw_drawable
*pdp
= loaderPrivate
;
126 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
127 Display
*dpy
= pdraw
->psc
->dpy
;
132 unsigned uw
, uh
, bw
, depth
;
134 drawable
= pdraw
->xDrawable
;
136 stat
= XGetGeometry(dpy
, drawable
, &root
,
137 x
, y
, &uw
, &uh
, &bw
, &depth
);
143 * Align renderbuffer pitch.
145 * This should be chosen by the driver and the loader (libGL, xserver/glx)
146 * should use the driver provided pitch.
148 * It seems that the xorg loader (that is the xserver loading swrast_dri for
149 * indirect rendering, not client-side libGL) requires that the pitch is
150 * exactly the image width padded to 32 bits. XXX
152 * The above restriction can probably be overcome by using ScratchPixmap and
153 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
154 * the scratch pixmap to 'pitch / cpp'.
157 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
159 unsigned mask
= mul
- 1;
161 return ((pitch_bits
+ mask
) & ~mask
) / 8;
165 swrastPutImage(__DRIdrawable
* draw
, int op
,
166 int x
, int y
, int w
, int h
,
167 char *data
, void *loaderPrivate
)
169 struct drisw_drawable
*pdp
= loaderPrivate
;
170 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
171 Display
*dpy
= pdraw
->psc
->dpy
;
177 case __DRI_SWRAST_IMAGE_OP_DRAW
:
180 case __DRI_SWRAST_IMAGE_OP_SWAP
:
187 drawable
= pdraw
->xDrawable
;
189 ximage
= pdp
->ximage
;
193 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
195 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
201 swrastGetImage(__DRIdrawable
* read
,
202 int x
, int y
, int w
, int h
,
203 char *data
, void *loaderPrivate
)
205 struct drisw_drawable
*prp
= loaderPrivate
;
206 __GLXDRIdrawable
*pread
= &(prp
->base
);
207 Display
*dpy
= pread
->psc
->dpy
;
211 readable
= pread
->xDrawable
;
213 ximage
= prp
->ximage
;
217 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
219 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
224 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
225 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
226 swrastGetDrawableInfo
,
231 static const __DRIextension
*loader_extensions
[] = {
232 &systemTimeExtension
.base
,
233 &swrastLoaderExtension
.base
,
242 driDestroyContext(__GLXDRIcontext
*context
,
243 __GLXscreenConfigs
*base
, Display
*dpy
)
245 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
246 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
248 (*psc
->core
->destroyContext
) (pcp
->driContext
);
254 driBindContext(__GLXDRIcontext
* context
,
255 __GLXDRIdrawable
* draw
, __GLXDRIdrawable
* read
)
257 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
258 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->psc
;
259 struct drisw_drawable
*pdr
= (struct drisw_drawable
*) draw
;
260 struct drisw_drawable
*prd
= (struct drisw_drawable
*) read
;
262 return (*psc
->core
->bindContext
) (pcp
->driContext
,
263 pdr
->driDrawable
, prd
->driDrawable
);
267 driUnbindContext(__GLXDRIcontext
* context
)
269 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
270 struct drisw_screen
*psc
= (struct drisw_screen
*) pcp
->psc
;
272 (*psc
->core
->unbindContext
) (pcp
->driContext
);
275 static __GLXDRIcontext
*
276 driCreateContext(__GLXscreenConfigs
*base
,
277 const __GLcontextModes
*mode
,
278 GLXContext gc
, GLXContext shareList
, int renderType
)
280 struct drisw_context
*pcp
, *pcp_shared
;
281 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
282 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
283 __DRIcontext
*shared
= NULL
;
285 if (!psc
->base
.driScreen
)
289 pcp_shared
= (struct drisw_context
*) shareList
->driContext
;
290 shared
= pcp_shared
->driContext
;
293 pcp
= Xmalloc(sizeof *pcp
);
297 pcp
->psc
= &psc
->base
;
299 (*psc
->core
->createNewContext
) (psc
->driScreen
,
300 config
->driConfig
, shared
, pcp
);
301 if (pcp
->driContext
== NULL
) {
306 pcp
->base
.destroyContext
= driDestroyContext
;
307 pcp
->base
.bindContext
= driBindContext
;
308 pcp
->base
.unbindContext
= driUnbindContext
;
314 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
316 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
317 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
319 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
321 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
325 static __GLXDRIdrawable
*
326 driCreateDrawable(__GLXscreenConfigs
*base
, XID xDrawable
,
327 GLXDrawable drawable
, const __GLcontextModes
* modes
)
329 struct drisw_drawable
*pdp
;
330 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
331 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
333 const __DRIswrastExtension
*swrast
= psc
->swrast
;
335 /* Old dri can't handle GLX 1.3+ drawable constructors. */
336 if (xDrawable
!= drawable
)
339 pdp
= Xmalloc(sizeof(*pdp
));
343 pdp
->base
.xDrawable
= xDrawable
;
344 pdp
->base
.drawable
= drawable
;
345 pdp
->base
.psc
= &psc
->base
;
347 XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
349 /* Create a new drawable */
351 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
353 if (!pdp
->driDrawable
) {
354 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
359 pdp
->base
.destroyDrawable
= driDestroyDrawable
;
365 driSwapBuffers(__GLXDRIdrawable
* pdraw
,
366 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
368 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
369 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
375 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
381 driDestroyScreen(__GLXscreenConfigs
*base
)
383 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
385 /* Free the direct rendering per screen data */
386 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
387 driDestroyConfigs(psc
->driver_configs
);
388 psc
->driScreen
= NULL
;
390 dlclose(psc
->driver
);
399 driver
= driOpenDriver("swrast");
402 driver
= driOpenDriver("swrastg");
407 static __GLXscreenConfigs
*
408 driCreateScreen(int screen
, __GLXdisplayPrivate
*priv
)
411 const __DRIconfig
**driver_configs
;
412 const __DRIextension
**extensions
;
413 struct drisw_screen
*psc
;
416 psc
= Xcalloc(1, sizeof *psc
);
420 memset(psc
, 0, sizeof *psc
);
421 if (!glx_screen_init(&psc
->base
, screen
, priv
))
424 psc
->driver
= driOpenSwrast();
425 if (psc
->driver
== NULL
)
428 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
429 if (extensions
== NULL
) {
430 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
434 for (i
= 0; extensions
[i
]; i
++) {
435 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
436 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
437 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
438 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
441 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
442 ErrorMessageF("core dri extension not found\n");
447 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
448 &driver_configs
, psc
);
449 if (psc
->driScreen
== NULL
) {
450 ErrorMessageF("failed to create dri screen\n");
454 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
457 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
459 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
461 psc
->driver_configs
= driver_configs
;
464 psc
->base
.driScreen
= psp
;
465 psp
->destroyScreen
= driDestroyScreen
;
466 psp
->createContext
= driCreateContext
;
467 psp
->createDrawable
= driCreateDrawable
;
468 psp
->swapBuffers
= driSwapBuffers
;
478 dlclose(psc
->driver
);
480 ErrorMessageF("reverting to indirect rendering\n");
485 /* Called from __glXFreeDisplayPrivate.
488 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
494 * Allocate, initialize and return a __DRIdisplayPrivate object.
495 * This is called from __glXInitialize() when we are given a new
498 _X_HIDDEN __GLXDRIdisplay
*
499 driswCreateDisplay(Display
* dpy
)
501 struct drisw_display
*pdpyp
;
503 pdpyp
= Xmalloc(sizeof *pdpyp
);
507 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
508 pdpyp
->base
.createScreen
= driCreateScreen
;
513 #endif /* GLX_DIRECT_RENDERING */