upload tizen2.0 source
[framework/uifw/xorg/server/xorg-server.git] / glx / glxdriswrast.c
1 /*
2  * Copyright © 2008 George Sapountzis <gsap7@yahoo.gr>
3  * Copyright © 2008 Red Hat, Inc
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of the
10  * copyright holders not be used in advertising or publicity
11  * pertaining to distribution of the software without specific,
12  * written prior permission.  The copyright holders make no
13  * representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied
15  * warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24  * SOFTWARE.
25  */
26
27 #ifdef HAVE_DIX_CONFIG_H
28 #include <dix-config.h>
29 #endif
30
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <sys/time.h>
36 #include <dlfcn.h>
37
38 #include <GL/gl.h>
39 #include <GL/internal/dri_interface.h>
40 #include <GL/glxtokens.h>
41
42 #include "scrnintstr.h"
43 #include "pixmapstr.h"
44 #include "gcstruct.h"
45 #include "os.h"
46
47 #include "glxserver.h"
48 #include "glxutil.h"
49 #include "glxdricommon.h"
50
51 #include "glapitable.h"
52 #include "glapi.h"
53 #include "glthread.h"
54 #include "dispatch.h"
55 #include "extension_string.h"
56
57 /* RTLD_LOCAL is not defined on Cygwin */
58 #ifdef __CYGWIN__
59 #ifndef RTLD_LOCAL
60 #define RTLD_LOCAL 0
61 #endif
62 #endif
63
64 typedef struct __GLXDRIscreen __GLXDRIscreen;
65 typedef struct __GLXDRIcontext __GLXDRIcontext;
66 typedef struct __GLXDRIdrawable __GLXDRIdrawable;
67
68 struct __GLXDRIscreen {
69     __GLXscreen base;
70     __DRIscreen *driScreen;
71     void *driver;
72
73     const __DRIcoreExtension *core;
74     const __DRIswrastExtension *swrast;
75     const __DRIcopySubBufferExtension *copySubBuffer;
76     const __DRItexBufferExtension *texBuffer;
77     const __DRIconfig **driConfigs;
78 };
79
80 struct __GLXDRIcontext {
81     __GLXcontext base;
82     __DRIcontext *driContext;
83 };
84
85 struct __GLXDRIdrawable {
86     __GLXdrawable base;
87     __DRIdrawable *driDrawable;
88     __GLXDRIscreen *screen;
89
90     GCPtr gc;                   /* scratch GC for span drawing */
91     GCPtr swapgc;               /* GC for swapping the color buffers */
92 };
93
94 static void
95 __glXDRIdrawableDestroy(__GLXdrawable * drawable)
96 {
97     __GLXDRIdrawable *private = (__GLXDRIdrawable *) drawable;
98     const __DRIcoreExtension *core = private->screen->core;
99
100     (*core->destroyDrawable) (private->driDrawable);
101
102     FreeGC(private->gc, (GContext) 0);
103     FreeGC(private->swapgc, (GContext) 0);
104
105     __glXDrawableRelease(drawable);
106
107     free(private);
108 }
109
110 static GLboolean
111 __glXDRIdrawableSwapBuffers(ClientPtr client, __GLXdrawable * drawable)
112 {
113     __GLXDRIdrawable *private = (__GLXDRIdrawable *) drawable;
114     const __DRIcoreExtension *core = private->screen->core;
115
116     (*core->swapBuffers) (private->driDrawable);
117
118     return TRUE;
119 }
120
121 static void
122 __glXDRIdrawableCopySubBuffer(__GLXdrawable * basePrivate,
123                               int x, int y, int w, int h)
124 {
125     __GLXDRIdrawable *private = (__GLXDRIdrawable *) basePrivate;
126     const __DRIcopySubBufferExtension *copySubBuffer =
127         private->screen->copySubBuffer;
128
129     if (copySubBuffer)
130         (*copySubBuffer->copySubBuffer) (private->driDrawable, x, y, w, h);
131 }
132
133 static void
134 __glXDRIcontextDestroy(__GLXcontext * baseContext)
135 {
136     __GLXDRIcontext *context = (__GLXDRIcontext *) baseContext;
137     __GLXDRIscreen *screen = (__GLXDRIscreen *) context->base.pGlxScreen;
138
139     (*screen->core->destroyContext) (context->driContext);
140     __glXContextDestroy(&context->base);
141     free(context);
142 }
143
144 static int
145 __glXDRIcontextMakeCurrent(__GLXcontext * baseContext)
146 {
147     __GLXDRIcontext *context = (__GLXDRIcontext *) baseContext;
148     __GLXDRIdrawable *draw = (__GLXDRIdrawable *) baseContext->drawPriv;
149     __GLXDRIdrawable *read = (__GLXDRIdrawable *) baseContext->readPriv;
150     __GLXDRIscreen *screen = (__GLXDRIscreen *) context->base.pGlxScreen;
151
152     return (*screen->core->bindContext) (context->driContext,
153                                          draw->driDrawable, read->driDrawable);
154 }
155
156 static int
157 __glXDRIcontextLoseCurrent(__GLXcontext * baseContext)
158 {
159     __GLXDRIcontext *context = (__GLXDRIcontext *) baseContext;
160     __GLXDRIscreen *screen = (__GLXDRIscreen *) context->base.pGlxScreen;
161
162     return (*screen->core->unbindContext) (context->driContext);
163 }
164
165 static int
166 __glXDRIcontextCopy(__GLXcontext * baseDst, __GLXcontext * baseSrc,
167                     unsigned long mask)
168 {
169     __GLXDRIcontext *dst = (__GLXDRIcontext *) baseDst;
170     __GLXDRIcontext *src = (__GLXDRIcontext *) baseSrc;
171     __GLXDRIscreen *screen = (__GLXDRIscreen *) dst->base.pGlxScreen;
172
173     return (*screen->core->copyContext) (dst->driContext,
174                                          src->driContext, mask);
175 }
176
177 #ifdef __DRI_TEX_BUFFER
178
179 static int
180 __glXDRIbindTexImage(__GLXcontext * baseContext,
181                      int buffer, __GLXdrawable * glxPixmap)
182 {
183     __GLXDRIdrawable *drawable = (__GLXDRIdrawable *) glxPixmap;
184     const __DRItexBufferExtension *texBuffer = drawable->screen->texBuffer;
185     __GLXDRIcontext *context = (__GLXDRIcontext *) baseContext;
186
187     if (texBuffer == NULL)
188         return Success;
189
190 #if __DRI_TEX_BUFFER_VERSION >= 2
191     if (texBuffer->base.version >= 2 && texBuffer->setTexBuffer2 != NULL) {
192         (*texBuffer->setTexBuffer2) (context->driContext,
193                                      glxPixmap->target,
194                                      glxPixmap->format, drawable->driDrawable);
195     }
196     else
197 #endif
198         texBuffer->setTexBuffer(context->driContext,
199                                 glxPixmap->target, drawable->driDrawable);
200
201     return Success;
202 }
203
204 static int
205 __glXDRIreleaseTexImage(__GLXcontext * baseContext,
206                         int buffer, __GLXdrawable * pixmap)
207 {
208     /* FIXME: Just unbind the texture? */
209     return Success;
210 }
211
212 #else
213
214 static int
215 __glXDRIbindTexImage(__GLXcontext * baseContext,
216                      int buffer, __GLXdrawable * glxPixmap)
217 {
218     return Success;
219 }
220
221 static int
222 __glXDRIreleaseTexImage(__GLXcontext * baseContext,
223                         int buffer, __GLXdrawable * pixmap)
224 {
225     return Success;
226 }
227
228 #endif
229
230 static __GLXtextureFromPixmap __glXDRItextureFromPixmap = {
231     __glXDRIbindTexImage,
232     __glXDRIreleaseTexImage
233 };
234
235 static void
236 __glXDRIscreenDestroy(__GLXscreen * baseScreen)
237 {
238     int i;
239
240     __GLXDRIscreen *screen = (__GLXDRIscreen *) baseScreen;
241
242     (*screen->core->destroyScreen) (screen->driScreen);
243
244     dlclose(screen->driver);
245
246     __glXScreenDestroy(baseScreen);
247
248     if (screen->driConfigs) {
249         for (i = 0; screen->driConfigs[i] != NULL; i++)
250             free((__DRIconfig **) screen->driConfigs[i]);
251         free(screen->driConfigs);
252     }
253
254     free(screen);
255 }
256
257 static __GLXcontext *
258 __glXDRIscreenCreateContext(__GLXscreen * baseScreen,
259                             __GLXconfig * glxConfig,
260                             __GLXcontext * baseShareContext)
261 {
262     __GLXDRIscreen *screen = (__GLXDRIscreen *) baseScreen;
263     __GLXDRIcontext *context, *shareContext;
264     __GLXDRIconfig *config = (__GLXDRIconfig *) glxConfig;
265     const __DRIcoreExtension *core = screen->core;
266     __DRIcontext *driShare;
267
268     shareContext = (__GLXDRIcontext *) baseShareContext;
269     if (shareContext)
270         driShare = shareContext->driContext;
271     else
272         driShare = NULL;
273
274     context = calloc(1, sizeof *context);
275     if (context == NULL)
276         return NULL;
277
278     context->base.destroy = __glXDRIcontextDestroy;
279     context->base.makeCurrent = __glXDRIcontextMakeCurrent;
280     context->base.loseCurrent = __glXDRIcontextLoseCurrent;
281     context->base.copy = __glXDRIcontextCopy;
282     context->base.textureFromPixmap = &__glXDRItextureFromPixmap;
283
284     context->driContext =
285         (*core->createNewContext) (screen->driScreen,
286                                    config->driConfig, driShare, context);
287
288     return &context->base;
289 }
290
291 static __GLXdrawable *
292 __glXDRIscreenCreateDrawable(ClientPtr client,
293                              __GLXscreen * screen,
294                              DrawablePtr pDraw,
295                              XID drawId,
296                              int type, XID glxDrawId, __GLXconfig * glxConfig)
297 {
298     XID gcvals[2];
299     int status;
300     __GLXDRIscreen *driScreen = (__GLXDRIscreen *) screen;
301     __GLXDRIconfig *config = (__GLXDRIconfig *) glxConfig;
302     __GLXDRIdrawable *private;
303
304     private = calloc(1, sizeof *private);
305     if (private == NULL)
306         return NULL;
307
308     private->screen = driScreen;
309     if (!__glXDrawableInit(&private->base, screen,
310                            pDraw, type, glxDrawId, glxConfig)) {
311         free(private);
312         return NULL;
313     }
314
315     private->base.destroy = __glXDRIdrawableDestroy;
316     private->base.swapBuffers = __glXDRIdrawableSwapBuffers;
317     private->base.copySubBuffer = __glXDRIdrawableCopySubBuffer;
318
319     gcvals[0] = GXcopy;
320     private->gc =
321         CreateGC(pDraw, GCFunction, gcvals, &status, (XID) 0, serverClient);
322     gcvals[1] = FALSE;
323     private->swapgc =
324         CreateGC(pDraw, GCFunction | GCGraphicsExposures, gcvals, &status,
325                  (XID) 0, serverClient);
326
327     private->driDrawable =
328         (*driScreen->swrast->createNewDrawable) (driScreen->driScreen,
329                                                  config->driConfig, private);
330
331     return &private->base;
332 }
333
334 static void
335 swrastGetDrawableInfo(__DRIdrawable * draw,
336                       int *x, int *y, int *w, int *h, void *loaderPrivate)
337 {
338     __GLXDRIdrawable *drawable = loaderPrivate;
339     DrawablePtr pDraw = drawable->base.pDraw;
340
341     *x = pDraw->x;
342     *y = pDraw->x;
343     *w = pDraw->width;
344     *h = pDraw->height;
345 }
346
347 static void
348 swrastPutImage(__DRIdrawable * draw, int op,
349                int x, int y, int w, int h, char *data, void *loaderPrivate)
350 {
351     __GLXDRIdrawable *drawable = loaderPrivate;
352     DrawablePtr pDraw = drawable->base.pDraw;
353     GCPtr gc;
354
355     switch (op) {
356     case __DRI_SWRAST_IMAGE_OP_DRAW:
357         gc = drawable->gc;
358         break;
359     case __DRI_SWRAST_IMAGE_OP_SWAP:
360         gc = drawable->swapgc;
361         break;
362     default:
363         return;
364     }
365
366     ValidateGC(pDraw, gc);
367
368     gc->ops->PutImage(pDraw, gc, pDraw->depth, x, y, w, h, 0, ZPixmap, data);
369 }
370
371 static void
372 swrastGetImage(__DRIdrawable * draw,
373                int x, int y, int w, int h, char *data, void *loaderPrivate)
374 {
375     __GLXDRIdrawable *drawable = loaderPrivate;
376     DrawablePtr pDraw = drawable->base.pDraw;
377     ScreenPtr pScreen = pDraw->pScreen;
378
379     pScreen->GetImage(pDraw, x, y, w, h, ZPixmap, ~0L, data);
380 }
381
382 static const __DRIswrastLoaderExtension swrastLoaderExtension = {
383     {__DRI_SWRAST_LOADER, __DRI_SWRAST_LOADER_VERSION},
384     swrastGetDrawableInfo,
385     swrastPutImage,
386     swrastGetImage
387 };
388
389 static const __DRIextension *loader_extensions[] = {
390     &systemTimeExtension.base,
391     &swrastLoaderExtension.base,
392     NULL
393 };
394
395 static void
396 initializeExtensions(__GLXDRIscreen * screen)
397 {
398     const __DRIextension **extensions;
399     int i;
400
401     extensions = screen->core->getExtensions(screen->driScreen);
402
403     for (i = 0; extensions[i]; i++) {
404 #ifdef __DRI_COPY_SUB_BUFFER
405         if (strcmp(extensions[i]->name, __DRI_COPY_SUB_BUFFER) == 0) {
406             screen->copySubBuffer =
407                 (const __DRIcopySubBufferExtension *) extensions[i];
408             /* GLX_MESA_copy_sub_buffer is always enabled. */
409         }
410 #endif
411
412 #ifdef __DRI_TEX_BUFFER
413         if (strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0) {
414             screen->texBuffer = (const __DRItexBufferExtension *) extensions[i];
415             /* GLX_EXT_texture_from_pixmap is always enabled. */
416         }
417 #endif
418         /* Ignore unknown extensions */
419     }
420 }
421
422 static __GLXscreen *
423 __glXDRIscreenProbe(ScreenPtr pScreen)
424 {
425     const char *driverName = "swrast";
426     __GLXDRIscreen *screen;
427
428     screen = calloc(1, sizeof *screen);
429     if (screen == NULL)
430         return NULL;
431
432     screen->base.destroy = __glXDRIscreenDestroy;
433     screen->base.createContext = __glXDRIscreenCreateContext;
434     screen->base.createDrawable = __glXDRIscreenCreateDrawable;
435     screen->base.swapInterval = NULL;
436     screen->base.pScreen = pScreen;
437
438     screen->driver = glxProbeDriver(driverName,
439                                     (void **) &screen->core,
440                                     __DRI_CORE, __DRI_CORE_VERSION,
441                                     (void **) &screen->swrast,
442                                     __DRI_SWRAST, __DRI_SWRAST_VERSION);
443     if (screen->driver == NULL) {
444         goto handle_error;
445     }
446
447     screen->driScreen =
448         (*screen->swrast->createNewScreen) (pScreen->myNum,
449                                             loader_extensions,
450                                             &screen->driConfigs, screen);
451
452     if (screen->driScreen == NULL) {
453         LogMessage(X_ERROR, "AIGLX error: Calling driver entry point failed\n");
454         goto handle_error;
455     }
456
457     initializeExtensions(screen);
458
459     screen->base.fbconfigs = glxConvertConfigs(screen->core, screen->driConfigs,
460                                                GLX_WINDOW_BIT |
461                                                GLX_PIXMAP_BIT |
462                                                GLX_PBUFFER_BIT);
463
464     __glXScreenInit(&screen->base, pScreen);
465
466     screen->base.GLXmajor = 1;
467     screen->base.GLXminor = 4;
468
469     LogMessage(X_INFO, "AIGLX: Loaded and initialized %s\n", driverName);
470
471     return &screen->base;
472
473  handle_error:
474     if (screen->driver)
475         dlclose(screen->driver);
476
477     free(screen);
478
479     LogMessage(X_ERROR, "GLX: could not load software renderer\n");
480
481     return NULL;
482 }
483
484 _X_EXPORT __GLXprovider __glXDRISWRastProvider = {
485     __glXDRIscreenProbe,
486     "DRISWRAST",
487     NULL
488 };