glx: indent -br -i3 -npcs --no-tabs drisw_glx.c
[mesa.git] / src / glx / x11 / drisw_glx.c
1 /* -*- mode: c; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3; coding: utf-8-unix -*- */
2 /*
3 * Copyright 2008 George Sapountzis
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #ifdef GLX_DIRECT_RENDERING
26
27 #include <X11/Xlib.h>
28 #include "glxclient.h"
29 #include "glcontextmodes.h"
30 #include <dlfcn.h>
31 #include "dri_common.h"
32
33 typedef struct __GLXDRIdisplayPrivateRec __GLXDRIdisplayPrivate;
34 typedef struct __GLXDRIcontextPrivateRec __GLXDRIcontextPrivate;
35 typedef struct __GLXDRIdrawablePrivateRec __GLXDRIdrawablePrivate;
36
37 struct __GLXDRIdisplayPrivateRec
38 {
39 __GLXDRIdisplay base;
40 };
41
42 struct __GLXDRIcontextPrivateRec
43 {
44 __GLXDRIcontext base;
45 __DRIcontext *driContext;
46 __GLXscreenConfigs *psc;
47 };
48
49 struct __GLXDRIdrawablePrivateRec
50 {
51 __GLXDRIdrawable base;
52
53 GC gc;
54 GC swapgc;
55
56 XVisualInfo *visinfo;
57 XImage *ximage;
58 int bpp;
59 };
60
61 /**
62 * swrast loader functions
63 */
64
65 static Bool
66 XCreateDrawable(__GLXDRIdrawablePrivate * pdp,
67 Display * dpy, XID drawable, int visualid)
68 {
69 XGCValues gcvalues;
70 long visMask;
71 XVisualInfo visTemp;
72 int num_visuals;
73
74 /* create GC's */
75 pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
76 pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
77
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);
83
84 /* create XImage */
85 visTemp.screen = DefaultScreen(dpy);
86 visTemp.visualid = visualid;
87 visMask = (VisualScreenMask | VisualIDMask);
88 pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
89
90 pdp->ximage = XCreateImage(dpy, pdp->visinfo->visual, pdp->visinfo->depth, ZPixmap, 0, /* format, offset */
91 NULL, /* data */
92 0, 0, /* size */
93 32, /* bitmap_pad */
94 0); /* bytes_per_line */
95
96 /* get the true number of bits per pixel */
97 pdp->bpp = pdp->ximage->bits_per_pixel;
98
99 return True;
100 }
101
102 static void
103 XDestroyDrawable(__GLXDRIdrawablePrivate * pdp, Display * dpy, XID drawable)
104 {
105 XDestroyImage(pdp->ximage);
106 XFree(pdp->visinfo);
107
108 XFreeGC(dpy, pdp->gc);
109 XFreeGC(dpy, pdp->swapgc);
110 }
111
112 static void
113 swrastGetDrawableInfo(__DRIdrawable * draw,
114 int *x, int *y, int *w, int *h, void *loaderPrivate)
115 {
116 __GLXDRIdrawablePrivate *pdp = loaderPrivate;
117 __GLXDRIdrawable *pdraw = &(pdp->base);;
118 Display *dpy = pdraw->psc->dpy;
119 Drawable drawable;
120
121 Window root;
122 Status stat;
123 unsigned int bw, depth;
124
125 drawable = pdraw->xDrawable;
126
127 stat = XGetGeometry(dpy, drawable, &root,
128 x, y, (unsigned int *) w, (unsigned int *) h,
129 &bw, &depth);
130 }
131
132 static inline int
133 bytes_per_line(int w, int bpp, unsigned mul)
134 {
135 unsigned mask = mul - 1;
136
137 return ((w * bpp + mask) & ~mask) / 8;
138 }
139
140 static void
141 swrastPutImage(__DRIdrawable * draw, int op,
142 int x, int y, int w, int h, char *data, void *loaderPrivate)
143 {
144 __GLXDRIdrawablePrivate *pdp = loaderPrivate;
145 __GLXDRIdrawable *pdraw = &(pdp->base);;
146 Display *dpy = pdraw->psc->dpy;
147 Drawable drawable;
148 XImage *ximage;
149 GC gc;
150
151 switch (op) {
152 case __DRI_SWRAST_IMAGE_OP_DRAW:
153 gc = pdp->gc;
154 break;
155 case __DRI_SWRAST_IMAGE_OP_SWAP:
156 gc = pdp->swapgc;
157 break;
158 default:
159 return;
160 }
161
162 drawable = pdraw->xDrawable;
163
164 ximage = pdp->ximage;
165 ximage->data = data;
166 ximage->width = w;
167 ximage->height = h;
168 ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32);
169
170 XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
171
172 ximage->data = NULL;
173 }
174
175 static void
176 swrastGetImage(__DRIdrawable * draw,
177 int x, int y, int w, int h, char *data, void *loaderPrivate)
178 {
179 __GLXDRIdrawablePrivate *pdp = loaderPrivate;
180 __GLXDRIdrawable *pdraw = &(pdp->base);;
181 Display *dpy = pdraw->psc->dpy;
182 Drawable drawable;
183 XImage *ximage;
184
185 drawable = pdraw->xDrawable;
186
187 ximage = pdp->ximage;
188 ximage->data = data;
189 ximage->width = w;
190 ximage->height = h;
191 ximage->bytes_per_line = bytes_per_line(w, pdp->bpp, 32);
192
193 XGetSubImage(dpy, drawable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
194
195 ximage->data = NULL;
196 }
197
198 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
199 {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
200 swrastGetDrawableInfo,
201 swrastPutImage,
202 swrastGetImage
203 };
204
205 static const __DRIextension *loader_extensions[] = {
206 &systemTimeExtension.base,
207 &swrastLoaderExtension.base,
208 NULL
209 };
210
211 /**
212 * GLXDRI functions
213 */
214
215 static void
216 driDestroyContext(__GLXDRIcontext * context,
217 __GLXscreenConfigs * psc, Display * dpy)
218 {
219 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
220 const __DRIcoreExtension *core = pcp->psc->core;
221
222 (*core->destroyContext) (pcp->driContext);
223
224 Xfree(pcp);
225 }
226
227 static Bool
228 driBindContext(__GLXDRIcontext * context,
229 __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
230 {
231 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
232 const __DRIcoreExtension *core = pcp->psc->core;
233
234 return (*core->bindContext) (pcp->driContext,
235 draw->driDrawable, read->driDrawable);
236 }
237
238 static void
239 driUnbindContext(__GLXDRIcontext * context)
240 {
241 __GLXDRIcontextPrivate *pcp = (__GLXDRIcontextPrivate *) context;
242 const __DRIcoreExtension *core = pcp->psc->core;
243
244 (*core->unbindContext) (pcp->driContext);
245 }
246
247 static __GLXDRIcontext *
248 driCreateContext(__GLXscreenConfigs * psc,
249 const __GLcontextModes * mode,
250 GLXContext gc, GLXContext shareList, int renderType)
251 {
252 __GLXDRIcontextPrivate *pcp, *pcp_shared;
253 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
254 const __DRIcoreExtension *core = psc->core;
255 __DRIcontext *shared = NULL;
256
257 if (!psc || !psc->driScreen)
258 return NULL;
259
260 if (shareList) {
261 pcp_shared = (__GLXDRIcontextPrivate *) shareList->driContext;
262 shared = pcp_shared->driContext;
263 }
264
265 pcp = Xmalloc(sizeof *pcp);
266 if (pcp == NULL)
267 return NULL;
268
269 pcp->psc = psc;
270 pcp->driContext =
271 (*core->createNewContext) (psc->__driScreen,
272 config->driConfig, shared, pcp);
273 if (pcp->driContext == NULL) {
274 Xfree(pcp);
275 return NULL;
276 }
277
278 pcp->base.destroyContext = driDestroyContext;
279 pcp->base.bindContext = driBindContext;
280 pcp->base.unbindContext = driUnbindContext;
281
282 return &pcp->base;
283 }
284
285 static void
286 driDestroyDrawable(__GLXDRIdrawable * pdraw)
287 {
288 __GLXDRIdrawablePrivate *pdp = (__GLXDRIdrawablePrivate *) pdraw;
289 const __DRIcoreExtension *core = pdraw->psc->core;
290
291 (*core->destroyDrawable) (pdraw->driDrawable);
292
293 XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
294 Xfree(pdp);
295 }
296
297 static __GLXDRIdrawable *
298 driCreateDrawable(__GLXscreenConfigs * psc,
299 XID xDrawable,
300 GLXDrawable drawable, const __GLcontextModes * modes)
301 {
302 __GLXDRIdrawable *pdraw;
303 __GLXDRIdrawablePrivate *pdp;
304 __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
305 const __DRIswrastExtension *swrast = psc->swrast;
306
307 /* Old dri can't handle GLX 1.3+ drawable constructors. */
308 if (xDrawable != drawable)
309 return NULL;
310
311 pdp = Xmalloc(sizeof(*pdp));
312 if (!pdp)
313 return NULL;
314
315 pdraw = &(pdp->base);
316 pdraw->xDrawable = xDrawable;
317 pdraw->drawable = drawable;
318 pdraw->psc = psc;
319
320 XCreateDrawable(pdp, psc->dpy, xDrawable, modes->visualID);
321
322 /* Create a new drawable */
323 pdraw->driDrawable =
324 (*swrast->createNewDrawable) (psc->__driScreen, config->driConfig, pdp);
325
326 if (!pdraw->driDrawable) {
327 XDestroyDrawable(pdp, psc->dpy, xDrawable);
328 Xfree(pdp);
329 return NULL;
330 }
331
332 pdraw->destroyDrawable = driDestroyDrawable;
333
334 return pdraw;
335 }
336
337 static void
338 driSwapBuffers(__GLXDRIdrawable * pdraw)
339 {
340 (*pdraw->psc->core->swapBuffers) (pdraw->driDrawable);
341 }
342
343 static void
344 driDestroyScreen(__GLXscreenConfigs * psc)
345 {
346 /* Free the direct rendering per screen data */
347 (*psc->core->destroyScreen) (psc->__driScreen);
348 psc->__driScreen = NULL;
349 if (psc->driver)
350 dlclose(psc->driver);
351 }
352
353 static __GLXDRIscreen *
354 driCreateScreen(__GLXscreenConfigs * psc, int screen,
355 __GLXdisplayPrivate * priv)
356 {
357 __GLXDRIscreen *psp;
358 const __DRIconfig **driver_configs;
359 const __DRIextension **extensions;
360 const char *driverName = "swrast";
361 int i;
362
363 psp = Xmalloc(sizeof *psp);
364 if (psp == NULL)
365 return NULL;
366
367 /* Initialize per screen dynamic client GLX extensions */
368 psc->ext_list_first_time = GL_TRUE;
369
370 psc->driver = driOpenDriver(driverName);
371 if (psc->driver == NULL)
372 goto handle_error;
373
374 extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
375 if (extensions == NULL) {
376 ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
377 goto handle_error;
378 }
379
380 for (i = 0; extensions[i]; i++) {
381 if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
382 psc->core = (__DRIcoreExtension *) extensions[i];
383 if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
384 psc->swrast = (__DRIswrastExtension *) extensions[i];
385 }
386
387 if (psc->core == NULL || psc->swrast == NULL) {
388 ErrorMessageF("core dri extension not found\n");
389 goto handle_error;
390 }
391
392 psc->__driScreen =
393 psc->swrast->createNewScreen(screen,
394 loader_extensions, &driver_configs, psc);
395 if (psc->__driScreen == NULL) {
396 ErrorMessageF("failed to create dri screen\n");
397 goto handle_error;
398 }
399
400 driBindExtensions(psc, 0);
401
402 psc->configs = driConvertConfigs(psc->core, psc->configs, driver_configs);
403 psc->visuals = driConvertConfigs(psc->core, psc->visuals, driver_configs);
404
405 psp->destroyScreen = driDestroyScreen;
406 psp->createContext = driCreateContext;
407 psp->createDrawable = driCreateDrawable;
408 psp->swapBuffers = driSwapBuffers;
409
410 return psp;
411
412 handle_error:
413 Xfree(psp);
414
415 if (psc->driver)
416 dlclose(psc->driver);
417
418 ErrorMessageF("reverting to indirect rendering\n");
419
420 return NULL;
421 }
422
423 /* Called from __glXFreeDisplayPrivate.
424 */
425 static void
426 driDestroyDisplay(__GLXDRIdisplay * dpy)
427 {
428 Xfree(dpy);
429 }
430
431 /*
432 * Allocate, initialize and return a __DRIdisplayPrivate object.
433 * This is called from __glXInitialize() when we are given a new
434 * display pointer.
435 */
436 _X_HIDDEN __GLXDRIdisplay *
437 driswCreateDisplay(Display * dpy)
438 {
439 __GLXDRIdisplayPrivate *pdpyp;
440
441 pdpyp = Xmalloc(sizeof *pdpyp);
442 if (pdpyp == NULL)
443 return NULL;
444
445 pdpyp->base.destroyDisplay = driDestroyDisplay;
446 pdpyp->base.createScreen = driCreateScreen;
447
448 return &pdpyp->base;
449 }
450
451 #endif /* GLX_DIRECT_RENDERING */