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 __GLXDRIcontext dri_vtable
;
40 __DRIcontext
*driContext
;
41 __GLXscreenConfigs
*psc
;
46 __GLXscreenConfigs base
;
48 __DRIscreen
*driScreen
;
49 __GLXDRIscreen vtable
;
50 const __DRIcoreExtension
*core
;
51 const __DRIswrastExtension
*swrast
;
52 const __DRIconfig
**driver_configs
;
59 __GLXDRIdrawable base
;
64 __DRIdrawable
*driDrawable
;
70 XCreateDrawable(struct drisw_drawable
* pdp
,
71 Display
* dpy
, XID drawable
, int visualid
)
79 pdp
->gc
= XCreateGC(dpy
, drawable
, 0, NULL
);
80 pdp
->swapgc
= XCreateGC(dpy
, drawable
, 0, NULL
);
82 gcvalues
.function
= GXcopy
;
83 gcvalues
.graphics_exposures
= False
;
84 XChangeGC(dpy
, pdp
->gc
, GCFunction
, &gcvalues
);
85 XChangeGC(dpy
, pdp
->swapgc
, GCFunction
, &gcvalues
);
86 XChangeGC(dpy
, pdp
->swapgc
, GCGraphicsExposures
, &gcvalues
);
89 visTemp
.screen
= DefaultScreen(dpy
);
90 visTemp
.visualid
= visualid
;
91 visMask
= (VisualScreenMask
| VisualIDMask
);
92 pdp
->visinfo
= XGetVisualInfo(dpy
, visMask
, &visTemp
, &num_visuals
);
95 pdp
->ximage
= XCreateImage(dpy
,
98 ZPixmap
, 0, /* format, offset */
100 0, 0, /* width, height */
102 0); /* bytes_per_line */
108 XDestroyDrawable(struct drisw_drawable
* pdp
, Display
* dpy
, XID drawable
)
110 XDestroyImage(pdp
->ximage
);
113 XFreeGC(dpy
, pdp
->gc
);
114 XFreeGC(dpy
, pdp
->swapgc
);
118 * swrast loader functions
122 swrastGetDrawableInfo(__DRIdrawable
* draw
,
123 int *x
, int *y
, int *w
, int *h
,
126 struct drisw_drawable
*pdp
= loaderPrivate
;
127 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
128 Display
*dpy
= pdraw
->psc
->dpy
;
133 unsigned uw
, uh
, bw
, depth
;
135 drawable
= pdraw
->xDrawable
;
137 stat
= XGetGeometry(dpy
, drawable
, &root
,
138 x
, y
, &uw
, &uh
, &bw
, &depth
);
144 * Align renderbuffer pitch.
146 * This should be chosen by the driver and the loader (libGL, xserver/glx)
147 * should use the driver provided pitch.
149 * It seems that the xorg loader (that is the xserver loading swrast_dri for
150 * indirect rendering, not client-side libGL) requires that the pitch is
151 * exactly the image width padded to 32 bits. XXX
153 * The above restriction can probably be overcome by using ScratchPixmap and
154 * CopyArea in the xserver, similar to ShmPutImage, and setting the width of
155 * the scratch pixmap to 'pitch / cpp'.
158 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
160 unsigned mask
= mul
- 1;
162 return ((pitch_bits
+ mask
) & ~mask
) / 8;
166 swrastPutImage(__DRIdrawable
* draw
, int op
,
167 int x
, int y
, int w
, int h
,
168 char *data
, void *loaderPrivate
)
170 struct drisw_drawable
*pdp
= loaderPrivate
;
171 __GLXDRIdrawable
*pdraw
= &(pdp
->base
);
172 Display
*dpy
= pdraw
->psc
->dpy
;
178 case __DRI_SWRAST_IMAGE_OP_DRAW
:
181 case __DRI_SWRAST_IMAGE_OP_SWAP
:
188 drawable
= pdraw
->xDrawable
;
190 ximage
= pdp
->ximage
;
194 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
196 XPutImage(dpy
, drawable
, gc
, ximage
, 0, 0, x
, y
, w
, h
);
202 swrastGetImage(__DRIdrawable
* read
,
203 int x
, int y
, int w
, int h
,
204 char *data
, void *loaderPrivate
)
206 struct drisw_drawable
*prp
= loaderPrivate
;
207 __GLXDRIdrawable
*pread
= &(prp
->base
);
208 Display
*dpy
= pread
->psc
->dpy
;
212 readable
= pread
->xDrawable
;
214 ximage
= prp
->ximage
;
218 ximage
->bytes_per_line
= bytes_per_line(w
* ximage
->bits_per_pixel
, 32);
220 XGetSubImage(dpy
, readable
, x
, y
, w
, h
, ~0L, ZPixmap
, ximage
, 0, 0);
225 static const __DRIswrastLoaderExtension swrastLoaderExtension
= {
226 {__DRI_SWRAST_LOADER
, __DRI_SWRAST_LOADER_VERSION
},
227 swrastGetDrawableInfo
,
232 static const __DRIextension
*loader_extensions
[] = {
233 &systemTimeExtension
.base
,
234 &swrastLoaderExtension
.base
,
243 driDestroyContext(__GLXcontext
*context
)
245 struct drisw_context
*pcp
= (struct drisw_context
*) context
;
246 struct drisw_screen
*psc
= (struct drisw_screen
*) context
->psc
;
248 (*psc
->core
->destroyContext
) (pcp
->driContext
);
254 driBindContext(__GLXcontext
* 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(__GLXcontext
* 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 const struct glx_context_vtable drisw_context_vtable
= {
283 static __GLXcontext
*
284 driCreateContext(__GLXscreenConfigs
*base
,
285 const __GLcontextModes
*mode
,
286 GLXContext shareList
, int renderType
)
288 struct drisw_context
*pcp
, *pcp_shared
;
289 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) mode
;
290 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
291 __DRIcontext
*shared
= NULL
;
293 if (!psc
->base
.driScreen
)
297 pcp_shared
= (struct drisw_context
*) shareList
->driContext
;
298 shared
= pcp_shared
->driContext
;
301 pcp
= Xmalloc(sizeof *pcp
);
305 memset(pcp
, 0, sizeof *pcp
);
306 if (!glx_context_init(&pcp
->base
, &psc
->base
, mode
)) {
312 (*psc
->core
->createNewContext
) (psc
->driScreen
,
313 config
->driConfig
, shared
, pcp
);
314 if (pcp
->driContext
== NULL
) {
319 pcp
->base
.vtable
= &drisw_context_vtable
;
320 pcp
->base
.driContext
= &pcp
->dri_vtable
;
321 pcp
->dri_vtable
.destroyContext
= driDestroyContext
;
322 pcp
->dri_vtable
.bindContext
= driBindContext
;
323 pcp
->dri_vtable
.unbindContext
= driUnbindContext
;
329 driDestroyDrawable(__GLXDRIdrawable
* pdraw
)
331 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
332 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
334 (*psc
->core
->destroyDrawable
) (pdp
->driDrawable
);
336 XDestroyDrawable(pdp
, pdraw
->psc
->dpy
, pdraw
->drawable
);
340 static __GLXDRIdrawable
*
341 driCreateDrawable(__GLXscreenConfigs
*base
, XID xDrawable
,
342 GLXDrawable drawable
, const __GLcontextModes
* modes
)
344 struct drisw_drawable
*pdp
;
345 __GLXDRIconfigPrivate
*config
= (__GLXDRIconfigPrivate
*) modes
;
346 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
348 const __DRIswrastExtension
*swrast
= psc
->swrast
;
350 /* Old dri can't handle GLX 1.3+ drawable constructors. */
351 if (xDrawable
!= drawable
)
354 pdp
= Xmalloc(sizeof(*pdp
));
358 pdp
->base
.xDrawable
= xDrawable
;
359 pdp
->base
.drawable
= drawable
;
360 pdp
->base
.psc
= &psc
->base
;
362 XCreateDrawable(pdp
, psc
->base
.dpy
, xDrawable
, modes
->visualID
);
364 /* Create a new drawable */
366 (*swrast
->createNewDrawable
) (psc
->driScreen
, config
->driConfig
, pdp
);
368 if (!pdp
->driDrawable
) {
369 XDestroyDrawable(pdp
, psc
->base
.dpy
, xDrawable
);
374 pdp
->base
.destroyDrawable
= driDestroyDrawable
;
380 driSwapBuffers(__GLXDRIdrawable
* pdraw
,
381 int64_t target_msc
, int64_t divisor
, int64_t remainder
)
383 struct drisw_drawable
*pdp
= (struct drisw_drawable
*) pdraw
;
384 struct drisw_screen
*psc
= (struct drisw_screen
*) pdp
->base
.psc
;
390 (*psc
->core
->swapBuffers
) (pdp
->driDrawable
);
396 driDestroyScreen(__GLXscreenConfigs
*base
)
398 struct drisw_screen
*psc
= (struct drisw_screen
*) base
;
400 /* Free the direct rendering per screen data */
401 (*psc
->core
->destroyScreen
) (psc
->driScreen
);
402 driDestroyConfigs(psc
->driver_configs
);
403 psc
->driScreen
= NULL
;
405 dlclose(psc
->driver
);
414 driver
= driOpenDriver("swrast");
417 driver
= driOpenDriver("swrastg");
422 static __GLXscreenConfigs
*
423 driCreateScreen(int screen
, __GLXdisplayPrivate
*priv
)
426 const __DRIconfig
**driver_configs
;
427 const __DRIextension
**extensions
;
428 struct drisw_screen
*psc
;
431 psc
= Xcalloc(1, sizeof *psc
);
435 memset(psc
, 0, sizeof *psc
);
436 if (!glx_screen_init(&psc
->base
, screen
, priv
))
439 psc
->driver
= driOpenSwrast();
440 if (psc
->driver
== NULL
)
443 extensions
= dlsym(psc
->driver
, __DRI_DRIVER_EXTENSIONS
);
444 if (extensions
== NULL
) {
445 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
449 for (i
= 0; extensions
[i
]; i
++) {
450 if (strcmp(extensions
[i
]->name
, __DRI_CORE
) == 0)
451 psc
->core
= (__DRIcoreExtension
*) extensions
[i
];
452 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST
) == 0)
453 psc
->swrast
= (__DRIswrastExtension
*) extensions
[i
];
456 if (psc
->core
== NULL
|| psc
->swrast
== NULL
) {
457 ErrorMessageF("core dri extension not found\n");
462 psc
->swrast
->createNewScreen(screen
, loader_extensions
,
463 &driver_configs
, psc
);
464 if (psc
->driScreen
== NULL
) {
465 ErrorMessageF("failed to create dri screen\n");
469 extensions
= psc
->core
->getExtensions(psc
->driScreen
);
472 driConvertConfigs(psc
->core
, psc
->base
.configs
, driver_configs
);
474 driConvertConfigs(psc
->core
, psc
->base
.visuals
, driver_configs
);
476 psc
->driver_configs
= driver_configs
;
479 psc
->base
.driScreen
= psp
;
480 psp
->destroyScreen
= driDestroyScreen
;
481 psp
->createContext
= driCreateContext
;
482 psp
->createDrawable
= driCreateDrawable
;
483 psp
->swapBuffers
= driSwapBuffers
;
491 dlclose(psc
->driver
);
493 ErrorMessageF("reverting to indirect rendering\n");
498 /* Called from __glXFreeDisplayPrivate.
501 driDestroyDisplay(__GLXDRIdisplay
* dpy
)
507 * Allocate, initialize and return a __DRIdisplayPrivate object.
508 * This is called from __glXInitialize() when we are given a new
511 _X_HIDDEN __GLXDRIdisplay
*
512 driswCreateDisplay(Display
* dpy
)
514 struct drisw_display
*pdpyp
;
516 pdpyp
= Xmalloc(sizeof *pdpyp
);
520 pdpyp
->base
.destroyDisplay
= driDestroyDisplay
;
521 pdpyp
->base
.createScreen
= driCreateScreen
;
526 #endif /* GLX_DIRECT_RENDERING */