glx: zero out drawable structs after allocation
[profile/ivi/mesa.git] / src / glx / drisw_glx.c
1 /*
2  * Copyright 2008 George Sapountzis
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * SOFTWARE.
22  */
23
24 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
25
26 #include <X11/Xlib.h>
27 #include "glxclient.h"
28 #include <dlfcn.h>
29 #include "dri_common.h"
30
31 struct drisw_display
32 {
33    __GLXDRIdisplay base;
34 };
35
36 struct drisw_context
37 {
38    __GLXcontext base;
39    __GLXDRIcontext dri_vtable;
40    __DRIcontext *driContext;
41    __GLXscreenConfigs *psc;
42 };
43
44 struct drisw_screen
45 {
46    __GLXscreenConfigs base;
47
48    __DRIscreen *driScreen;
49    __GLXDRIscreen vtable;
50    const __DRIcoreExtension *core;
51    const __DRIswrastExtension *swrast;
52    const __DRIconfig **driver_configs;
53
54    void *driver;
55 };
56
57 struct drisw_drawable
58 {
59    __GLXDRIdrawable base;
60
61    GC gc;
62    GC swapgc;
63
64    __DRIdrawable *driDrawable;
65    XVisualInfo *visinfo;
66    XImage *ximage;
67 };
68
69 static Bool
70 XCreateDrawable(struct drisw_drawable * pdp,
71                 Display * dpy, XID drawable, int visualid)
72 {
73    XGCValues gcvalues;
74    long visMask;
75    XVisualInfo visTemp;
76    int num_visuals;
77
78    /* create GC's */
79    pdp->gc = XCreateGC(dpy, drawable, 0, NULL);
80    pdp->swapgc = XCreateGC(dpy, drawable, 0, NULL);
81
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);
87
88    /* visual */
89    visTemp.screen = DefaultScreen(dpy);
90    visTemp.visualid = visualid;
91    visMask = (VisualScreenMask | VisualIDMask);
92    pdp->visinfo = XGetVisualInfo(dpy, visMask, &visTemp, &num_visuals);
93
94    /* create XImage */
95    pdp->ximage = XCreateImage(dpy,
96                               pdp->visinfo->visual,
97                               pdp->visinfo->depth,
98                               ZPixmap, 0,             /* format, offset */
99                               NULL,                   /* data */
100                               0, 0,                   /* width, height */
101                               32,                     /* bitmap_pad */
102                               0);                     /* bytes_per_line */
103
104    return True;
105 }
106
107 static void
108 XDestroyDrawable(struct drisw_drawable * pdp, Display * dpy, XID drawable)
109 {
110    XDestroyImage(pdp->ximage);
111    XFree(pdp->visinfo);
112
113    XFreeGC(dpy, pdp->gc);
114    XFreeGC(dpy, pdp->swapgc);
115 }
116
117 /**
118  * swrast loader functions
119  */
120
121 static void
122 swrastGetDrawableInfo(__DRIdrawable * draw,
123                       int *x, int *y, int *w, int *h,
124                       void *loaderPrivate)
125 {
126    struct drisw_drawable *pdp = loaderPrivate;
127    __GLXDRIdrawable *pdraw = &(pdp->base);
128    Display *dpy = pdraw->psc->dpy;
129    Drawable drawable;
130
131    Window root;
132    Status stat;
133    unsigned uw, uh, bw, depth;
134
135    drawable = pdraw->xDrawable;
136
137    stat = XGetGeometry(dpy, drawable, &root,
138                        x, y, &uw, &uh, &bw, &depth);
139    *w = uw;
140    *h = uh;
141 }
142
143 /**
144  * Align renderbuffer pitch.
145  *
146  * This should be chosen by the driver and the loader (libGL, xserver/glx)
147  * should use the driver provided pitch.
148  *
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
152  *
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'.
156  */
157 static inline int
158 bytes_per_line(unsigned pitch_bits, unsigned mul)
159 {
160    unsigned mask = mul - 1;
161
162    return ((pitch_bits + mask) & ~mask) / 8;
163 }
164
165 static void
166 swrastPutImage(__DRIdrawable * draw, int op,
167                int x, int y, int w, int h,
168                char *data, void *loaderPrivate)
169 {
170    struct drisw_drawable *pdp = loaderPrivate;
171    __GLXDRIdrawable *pdraw = &(pdp->base);
172    Display *dpy = pdraw->psc->dpy;
173    Drawable drawable;
174    XImage *ximage;
175    GC gc;
176
177    switch (op) {
178    case __DRI_SWRAST_IMAGE_OP_DRAW:
179       gc = pdp->gc;
180       break;
181    case __DRI_SWRAST_IMAGE_OP_SWAP:
182       gc = pdp->swapgc;
183       break;
184    default:
185       return;
186    }
187
188    drawable = pdraw->xDrawable;
189
190    ximage = pdp->ximage;
191    ximage->data = data;
192    ximage->width = w;
193    ximage->height = h;
194    ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
195
196    XPutImage(dpy, drawable, gc, ximage, 0, 0, x, y, w, h);
197
198    ximage->data = NULL;
199 }
200
201 static void
202 swrastGetImage(__DRIdrawable * read,
203                int x, int y, int w, int h,
204                char *data, void *loaderPrivate)
205 {
206    struct drisw_drawable *prp = loaderPrivate;
207    __GLXDRIdrawable *pread = &(prp->base);
208    Display *dpy = pread->psc->dpy;
209    Drawable readable;
210    XImage *ximage;
211
212    readable = pread->xDrawable;
213
214    ximage = prp->ximage;
215    ximage->data = data;
216    ximage->width = w;
217    ximage->height = h;
218    ximage->bytes_per_line = bytes_per_line(w * ximage->bits_per_pixel, 32);
219
220    XGetSubImage(dpy, readable, x, y, w, h, ~0L, ZPixmap, ximage, 0, 0);
221
222    ximage->data = NULL;
223 }
224
225 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
226    {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
227    swrastGetDrawableInfo,
228    swrastPutImage,
229    swrastGetImage
230 };
231
232 static const __DRIextension *loader_extensions[] = {
233    &systemTimeExtension.base,
234    &swrastLoaderExtension.base,
235    NULL
236 };
237
238 /**
239  * GLXDRI functions
240  */
241
242 static void
243 drisw_destroy_context(__GLXcontext *context)
244 {
245    struct drisw_context *pcp = (struct drisw_context *) context;
246    struct drisw_screen *psc = (struct drisw_screen *) context->psc;
247
248    glx_send_destroy_context(psc->base.dpy, context->xid);
249
250    if (context->extensions)
251       XFree((char *) context->extensions);
252
253    GarbageCollectDRIDrawables(context->psc);
254
255    (*psc->core->destroyContext) (pcp->driContext);
256
257    Xfree(pcp);
258 }
259
260 static Bool
261 driBindContext(__GLXcontext * context,
262                __GLXDRIdrawable * draw, __GLXDRIdrawable * read)
263 {
264    struct drisw_context *pcp = (struct drisw_context *) context;
265    struct drisw_screen *psc = (struct drisw_screen *) pcp->psc;
266    struct drisw_drawable *pdr = (struct drisw_drawable *) draw;
267    struct drisw_drawable *prd = (struct drisw_drawable *) read;
268
269    return (*psc->core->bindContext) (pcp->driContext,
270                                      pdr->driDrawable, prd->driDrawable);
271 }
272
273 static void
274 driUnbindContext(__GLXcontext * context)
275 {
276    struct drisw_context *pcp = (struct drisw_context *) context;
277    struct drisw_screen *psc = (struct drisw_screen *) pcp->psc;
278
279    (*psc->core->unbindContext) (pcp->driContext);
280 }
281
282 static const struct glx_context_vtable drisw_context_vtable = {
283    drisw_destroy_context,
284    NULL,
285    NULL,
286    DRI_glXUseXFont,
287    NULL,
288    NULL,
289 };
290
291 static __GLXcontext *
292 drisw_create_context(__GLXscreenConfigs *base,
293                      const __GLcontextModes *mode,
294                      GLXContext shareList, int renderType)
295 {
296    struct drisw_context *pcp, *pcp_shared;
297    __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) mode;
298    struct drisw_screen *psc = (struct drisw_screen *) base;
299    __DRIcontext *shared = NULL;
300
301    if (!psc->base.driScreen)
302       return NULL;
303
304    if (shareList) {
305       pcp_shared = (struct drisw_context *) shareList->driContext;
306       shared = pcp_shared->driContext;
307    }
308
309    pcp = Xmalloc(sizeof *pcp);
310    if (pcp == NULL)
311       return NULL;
312
313    memset(pcp, 0, sizeof *pcp);
314    if (!glx_context_init(&pcp->base, &psc->base, mode)) {
315       Xfree(pcp);
316       return NULL;
317    }
318
319    pcp->driContext =
320       (*psc->core->createNewContext) (psc->driScreen,
321                                       config->driConfig, shared, pcp);
322    if (pcp->driContext == NULL) {
323       Xfree(pcp);
324       return NULL;
325    }
326
327    pcp->base.vtable = &drisw_context_vtable;
328    pcp->base.driContext = &pcp->dri_vtable;
329    pcp->dri_vtable.bindContext = driBindContext;
330    pcp->dri_vtable.unbindContext = driUnbindContext;
331
332    return &pcp->base;
333 }
334
335 static void
336 driDestroyDrawable(__GLXDRIdrawable * pdraw)
337 {
338    struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
339    struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
340
341    (*psc->core->destroyDrawable) (pdp->driDrawable);
342
343    XDestroyDrawable(pdp, pdraw->psc->dpy, pdraw->drawable);
344    Xfree(pdp);
345 }
346
347 static __GLXDRIdrawable *
348 driCreateDrawable(__GLXscreenConfigs *base, XID xDrawable,
349                   GLXDrawable drawable, const __GLcontextModes * modes)
350 {
351    struct drisw_drawable *pdp;
352    __GLXDRIconfigPrivate *config = (__GLXDRIconfigPrivate *) modes;
353    struct drisw_screen *psc = (struct drisw_screen *) base;
354
355    const __DRIswrastExtension *swrast = psc->swrast;
356
357    /* Old dri can't handle GLX 1.3+ drawable constructors. */
358    if (xDrawable != drawable)
359       return NULL;
360
361    pdp = Xmalloc(sizeof(*pdp));
362    if (!pdp)
363       return NULL;
364
365    memset(pdp, 0, sizeof *pdp);
366    pdp->base.xDrawable = xDrawable;
367    pdp->base.drawable = drawable;
368    pdp->base.psc = &psc->base;
369
370    XCreateDrawable(pdp, psc->base.dpy, xDrawable, modes->visualID);
371
372    /* Create a new drawable */
373    pdp->driDrawable =
374       (*swrast->createNewDrawable) (psc->driScreen, config->driConfig, pdp);
375
376    if (!pdp->driDrawable) {
377       XDestroyDrawable(pdp, psc->base.dpy, xDrawable);
378       Xfree(pdp);
379       return NULL;
380    }
381
382    pdp->base.destroyDrawable = driDestroyDrawable;
383
384    return &pdp->base;
385 }
386
387 static int64_t
388 driSwapBuffers(__GLXDRIdrawable * pdraw,
389                int64_t target_msc, int64_t divisor, int64_t remainder)
390 {
391    struct drisw_drawable *pdp = (struct drisw_drawable *) pdraw;
392    struct drisw_screen *psc = (struct drisw_screen *) pdp->base.psc;
393
394    (void) target_msc;
395    (void) divisor;
396    (void) remainder;
397
398    (*psc->core->swapBuffers) (pdp->driDrawable);
399
400    return 0;
401 }
402
403 static void
404 driDestroyScreen(__GLXscreenConfigs *base)
405 {
406    struct drisw_screen *psc = (struct drisw_screen *) base;
407
408    /* Free the direct rendering per screen data */
409    (*psc->core->destroyScreen) (psc->driScreen);
410    driDestroyConfigs(psc->driver_configs);
411    psc->driScreen = NULL;
412    if (psc->driver)
413       dlclose(psc->driver);
414 }
415
416 static void *
417 driOpenSwrast(void)
418 {
419    void *driver = NULL;
420
421    if (driver == NULL)
422       driver = driOpenDriver("swrast");
423
424    if (driver == NULL)
425       driver = driOpenDriver("swrastg");
426
427    return driver;
428 }
429
430 static const struct glx_screen_vtable drisw_screen_vtable = {
431    drisw_create_context
432 };
433
434 static __GLXscreenConfigs *
435 driCreateScreen(int screen, __GLXdisplayPrivate *priv)
436 {
437    __GLXDRIscreen *psp;
438    const __DRIconfig **driver_configs;
439    const __DRIextension **extensions;
440    struct drisw_screen *psc;
441    int i;
442
443    psc = Xcalloc(1, sizeof *psc);
444    if (psc == NULL)
445       return NULL;
446
447    memset(psc, 0, sizeof *psc);
448    if (!glx_screen_init(&psc->base, screen, priv))
449        return NULL;
450
451    psc->driver = driOpenSwrast();
452    if (psc->driver == NULL)
453       goto handle_error;
454
455    extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
456    if (extensions == NULL) {
457       ErrorMessageF("driver exports no extensions (%s)\n", dlerror());
458       goto handle_error;
459    }
460
461    for (i = 0; extensions[i]; i++) {
462       if (strcmp(extensions[i]->name, __DRI_CORE) == 0)
463          psc->core = (__DRIcoreExtension *) extensions[i];
464       if (strcmp(extensions[i]->name, __DRI_SWRAST) == 0)
465          psc->swrast = (__DRIswrastExtension *) extensions[i];
466    }
467
468    if (psc->core == NULL || psc->swrast == NULL) {
469       ErrorMessageF("core dri extension not found\n");
470       goto handle_error;
471    }
472
473    psc->driScreen =
474       psc->swrast->createNewScreen(screen, loader_extensions,
475                                    &driver_configs, psc);
476    if (psc->driScreen == NULL) {
477       ErrorMessageF("failed to create dri screen\n");
478       goto handle_error;
479    }
480
481    extensions = psc->core->getExtensions(psc->driScreen);
482
483    psc->base.configs =
484       driConvertConfigs(psc->core, psc->base.configs, driver_configs);
485    psc->base.visuals =
486       driConvertConfigs(psc->core, psc->base.visuals, driver_configs);
487
488    psc->driver_configs = driver_configs;
489
490    psc->base.vtable = &drisw_screen_vtable;
491    psp = &psc->vtable;
492    psc->base.driScreen = psp;
493    psp->destroyScreen = driDestroyScreen;
494    psp->createDrawable = driCreateDrawable;
495    psp->swapBuffers = driSwapBuffers;
496
497    return &psc->base;
498
499  handle_error:
500    Xfree(psc);
501
502    if (psc->driver)
503       dlclose(psc->driver);
504
505    ErrorMessageF("reverting to indirect rendering\n");
506
507    return NULL;
508 }
509
510 /* Called from __glXFreeDisplayPrivate.
511  */
512 static void
513 driDestroyDisplay(__GLXDRIdisplay * dpy)
514 {
515    Xfree(dpy);
516 }
517
518 /*
519  * Allocate, initialize and return a __DRIdisplayPrivate object.
520  * This is called from __glXInitialize() when we are given a new
521  * display pointer.
522  */
523 _X_HIDDEN __GLXDRIdisplay *
524 driswCreateDisplay(Display * dpy)
525 {
526    struct drisw_display *pdpyp;
527
528    pdpyp = Xmalloc(sizeof *pdpyp);
529    if (pdpyp == NULL)
530       return NULL;
531
532    pdpyp->base.destroyDisplay = driDestroyDisplay;
533    pdpyp->base.createScreen = driCreateScreen;
534
535    return &pdpyp->base;
536 }
537
538 #endif /* GLX_DIRECT_RENDERING */