egl_g3d_api.c revision 9d7219c560d468e1329d6867cff5033e979f535f
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.9
4 *
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26#include "egldriver.h"
27#include "eglcurrent.h"
28#include "egllog.h"
29
30#include "pipe/p_screen.h"
31#include "util/u_memory.h"
32#include "util/u_inlines.h"
33
34#include "egl_g3d.h"
35#include "egl_g3d_api.h"
36#include "egl_g3d_image.h"
37#include "egl_g3d_st.h"
38#include "native.h"
39
40/**
41 * Return the state tracker for the given context.
42 */
43static struct st_api *
44egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx)
45{
46   struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
47   struct st_api *stapi;
48   EGLint idx = -1;
49
50   switch (ctx->ClientAPI) {
51   case EGL_OPENGL_ES_API:
52      switch (ctx->ClientVersion) {
53      case 1:
54         idx = ST_API_OPENGL_ES1;
55         break;
56      case 2:
57         idx = ST_API_OPENGL_ES2;
58         break;
59      default:
60         _eglLog(_EGL_WARNING, "unknown client version %d",
61               ctx->ClientVersion);
62         break;
63      }
64      break;
65   case EGL_OPENVG_API:
66      idx = ST_API_OPENVG;
67      break;
68   case EGL_OPENGL_API:
69      idx = ST_API_OPENGL;
70      break;
71   default:
72      _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI);
73      break;
74   }
75
76   stapi = (idx >= 0) ? gdrv->stapis[idx] : NULL;
77   return stapi;
78}
79
80static _EGLContext *
81egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
82                       _EGLContext *share, const EGLint *attribs)
83{
84   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
85   struct egl_g3d_context *gshare = egl_g3d_context(share);
86   struct egl_g3d_config *gconf = egl_g3d_config(conf);
87   struct egl_g3d_context *gctx;
88
89   gctx = CALLOC_STRUCT(egl_g3d_context);
90   if (!gctx) {
91      _eglError(EGL_BAD_ALLOC, "eglCreateContext");
92      return NULL;
93   }
94
95   if (!_eglInitContext(&gctx->base, dpy, conf, attribs)) {
96      FREE(gctx);
97      return NULL;
98   }
99
100   gctx->stapi = egl_g3d_choose_st(drv, &gctx->base);
101   if (!gctx->stapi) {
102      FREE(gctx);
103      return NULL;
104   }
105
106   gctx->stctxi = gctx->stapi->create_context(gctx->stapi, gdpy->smapi,
107         &gconf->stvis, (gshare) ? gshare->stctxi : NULL);
108   if (!gctx->stctxi) {
109      FREE(gctx);
110      return NULL;
111   }
112
113   gctx->stctxi->st_manager_private = (void *) &gctx->base;
114
115   return &gctx->base;
116}
117
118/**
119 * Destroy a context.
120 */
121static void
122destroy_context(_EGLDisplay *dpy, _EGLContext *ctx)
123{
124   struct egl_g3d_context *gctx = egl_g3d_context(ctx);
125
126   /* FIXME a context might live longer than its display */
127   if (!dpy->Initialized)
128      _eglLog(_EGL_FATAL, "destroy a context with an unitialized display");
129
130   gctx->stctxi->destroy(gctx->stctxi);
131
132   FREE(gctx);
133}
134
135static EGLBoolean
136egl_g3d_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
137{
138   if (!_eglIsContextBound(ctx))
139      destroy_context(dpy, ctx);
140   return EGL_TRUE;
141}
142
143struct egl_g3d_create_surface_arg {
144   EGLint type;
145   union {
146      EGLNativeWindowType win;
147      EGLNativePixmapType pix;
148   } u;
149};
150
151static _EGLSurface *
152egl_g3d_create_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
153                       struct egl_g3d_create_surface_arg *arg,
154                       const EGLint *attribs)
155{
156   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
157   struct egl_g3d_config *gconf = egl_g3d_config(conf);
158   struct egl_g3d_surface *gsurf;
159   struct native_surface *nsurf;
160   const char *err;
161
162   switch (arg->type) {
163   case EGL_WINDOW_BIT:
164      err = "eglCreateWindowSurface";
165      break;
166   case EGL_PIXMAP_BIT:
167      err = "eglCreatePixmapSurface";
168      break;
169#ifdef EGL_MESA_screen_surface
170   case EGL_SCREEN_BIT_MESA:
171      err = "eglCreateScreenSurface";
172      break;
173#endif
174   default:
175      err = "eglCreateUnknownSurface";
176      break;
177   }
178
179   gsurf = CALLOC_STRUCT(egl_g3d_surface);
180   if (!gsurf) {
181      _eglError(EGL_BAD_ALLOC, err);
182      return NULL;
183   }
184
185   if (!_eglInitSurface(&gsurf->base, dpy, arg->type, conf, attribs)) {
186      FREE(gsurf);
187      return NULL;
188   }
189
190   /* create the native surface */
191   switch (arg->type) {
192   case EGL_WINDOW_BIT:
193      nsurf = gdpy->native->create_window_surface(gdpy->native,
194            arg->u.win, gconf->native);
195      break;
196   case EGL_PIXMAP_BIT:
197      nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
198            arg->u.pix, gconf->native);
199      break;
200#ifdef EGL_MESA_screen_surface
201   case EGL_SCREEN_BIT_MESA:
202      /* prefer back buffer (move to _eglInitSurface?) */
203      gsurf->base.RenderBuffer = EGL_BACK_BUFFER;
204      nsurf = gdpy->native->modeset->create_scanout_surface(gdpy->native,
205            gconf->native, gsurf->base.Width, gsurf->base.Height);
206      break;
207#endif
208   default:
209      nsurf = NULL;
210      break;
211   }
212
213   if (!nsurf) {
214      FREE(gsurf);
215      return NULL;
216   }
217   /* initialize the geometry */
218   if (!nsurf->validate(nsurf, 0x0, &gsurf->sequence_number, NULL,
219            &gsurf->base.Width, &gsurf->base.Height)) {
220      nsurf->destroy(nsurf);
221      FREE(gsurf);
222      return NULL;
223   }
224
225   gsurf->stvis = gconf->stvis;
226   if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER)
227      gsurf->stvis.render_buffer = ST_ATTACHMENT_FRONT_LEFT;
228
229   gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
230   if (!gsurf->stfbi) {
231      nsurf->destroy(nsurf);
232      FREE(gsurf);
233      return NULL;
234   }
235
236   nsurf->user_data = &gsurf->base;
237   gsurf->native = nsurf;
238
239   return &gsurf->base;
240}
241
242static _EGLSurface *
243egl_g3d_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
244                              _EGLConfig *conf, EGLNativeWindowType win,
245                              const EGLint *attribs)
246{
247   struct egl_g3d_create_surface_arg arg;
248
249   memset(&arg, 0, sizeof(arg));
250   arg.type = EGL_WINDOW_BIT;
251   arg.u.win = win;
252
253   return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
254}
255
256static _EGLSurface *
257egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
258                              _EGLConfig *conf, EGLNativePixmapType pix,
259                              const EGLint *attribs)
260{
261   struct egl_g3d_create_surface_arg arg;
262
263   memset(&arg, 0, sizeof(arg));
264   arg.type = EGL_PIXMAP_BIT;
265   arg.u.pix = pix;
266
267   return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
268}
269
270static _EGLSurface *
271egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
272                               _EGLConfig *conf, const EGLint *attribs)
273{
274   struct egl_g3d_config *gconf = egl_g3d_config(conf);
275   struct egl_g3d_surface *gsurf;
276
277   gsurf = CALLOC_STRUCT(egl_g3d_surface);
278   if (!gsurf) {
279      _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
280      return NULL;
281   }
282
283   if (!_eglInitSurface(&gsurf->base, dpy, EGL_PBUFFER_BIT, conf, attribs)) {
284      FREE(gsurf);
285      return NULL;
286   }
287
288   gsurf->stvis = gconf->stvis;
289
290   gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
291   if (!gsurf->stfbi) {
292      FREE(gsurf);
293      return NULL;
294   }
295
296   return &gsurf->base;
297}
298
299/**
300 * Destroy a surface.
301 */
302static void
303destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
304{
305   struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
306
307   /* FIXME a surface might live longer than its display */
308   if (!dpy->Initialized)
309      _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display");
310
311   pipe_resource_reference(&gsurf->render_texture, NULL);
312   egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
313   if (gsurf->native)
314      gsurf->native->destroy(gsurf->native);
315   FREE(gsurf);
316}
317
318static EGLBoolean
319egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
320{
321   if (!_eglIsSurfaceBound(surf))
322      destroy_surface(dpy, surf);
323   return EGL_TRUE;
324}
325
326static EGLBoolean
327egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
328                     _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
329{
330   struct egl_g3d_context *gctx = egl_g3d_context(ctx);
331   struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
332   struct egl_g3d_surface *gread = egl_g3d_surface(read);
333   struct egl_g3d_context *old_gctx;
334   EGLBoolean ok = EGL_TRUE;
335
336   /* bind the new context and return the "orphaned" one */
337   if (!_eglBindContext(&ctx, &draw, &read))
338      return EGL_FALSE;
339   old_gctx = egl_g3d_context(ctx);
340
341   if (old_gctx) {
342      /* flush old context */
343      old_gctx->stctxi->flush(old_gctx->stctxi,
344            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
345   }
346
347   if (gctx) {
348      ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi,
349            (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
350      if (ok) {
351         gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi, gdraw->stfbi);
352         if (gread != gdraw) {
353            gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
354                  gread->stfbi);
355         }
356
357         if (gdraw->base.Type == EGL_WINDOW_BIT) {
358            gctx->base.WindowRenderBuffer =
359               (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
360               EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
361         }
362      }
363   }
364   else if (old_gctx) {
365      ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL);
366      old_gctx->base.WindowRenderBuffer = EGL_NONE;
367   }
368
369   if (ctx && !_eglIsContextLinked(ctx))
370      destroy_context(dpy, ctx);
371   if (draw && !_eglIsSurfaceLinked(draw))
372      destroy_surface(dpy, draw);
373   if (read && read != draw && !_eglIsSurfaceLinked(read))
374      destroy_surface(dpy, read);
375
376   return ok;
377}
378
379static EGLBoolean
380egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
381{
382   struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
383   _EGLContext *ctx = _eglGetCurrentContext();
384   struct egl_g3d_context *gctx = NULL;
385
386   /* no-op for pixmap or pbuffer surface */
387   if (gsurf->base.Type == EGL_PIXMAP_BIT ||
388       gsurf->base.Type == EGL_PBUFFER_BIT)
389      return EGL_TRUE;
390
391   /* or when the surface is single-buffered */
392   if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT)
393      return EGL_TRUE;
394
395   if (ctx && ctx->DrawSurface == surf)
396      gctx = egl_g3d_context(ctx);
397
398   /* flush if the surface is current */
399   if (gctx) {
400      gctx->stctxi->flush(gctx->stctxi,
401            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
402   }
403
404   return gsurf->native->swap_buffers(gsurf->native);
405}
406
407/**
408 * Get the pipe surface of the given attachment of the native surface.
409 */
410static struct pipe_resource *
411get_pipe_resource(struct native_display *ndpy, struct native_surface *nsurf,
412                  enum native_attachment natt)
413{
414   struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
415
416   textures[natt] = NULL;
417   nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
418
419   return textures[natt];
420}
421
422static EGLBoolean
423egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
424                     EGLNativePixmapType target)
425{
426   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
427   struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
428   _EGLContext *ctx = _eglGetCurrentContext();
429   struct egl_g3d_config *gconf;
430   struct native_surface *nsurf;
431   struct pipe_resource *ptex;
432
433   if (!gsurf->render_texture)
434      return EGL_TRUE;
435
436   gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
437   if (!gconf)
438      return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
439
440   nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
441         target, gconf->native);
442   if (!nsurf)
443      return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
444
445   /* flush if the surface is current */
446   if (ctx && ctx->DrawSurface == &gsurf->base) {
447      struct egl_g3d_context *gctx = egl_g3d_context(ctx);
448      gctx->stctxi->flush(gctx->stctxi,
449            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
450   }
451
452   /* create a pipe context to copy surfaces */
453   if (!gdpy->pipe) {
454      gdpy->pipe =
455         gdpy->native->screen->context_create(gdpy->native->screen, NULL);
456      if (!gdpy->pipe)
457         return EGL_FALSE;
458   }
459
460   ptex = get_pipe_resource(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
461   if (ptex) {
462      struct pipe_resource *psrc = gsurf->render_texture;
463      struct pipe_subresource subsrc, subdst;
464      subsrc.face = 0;
465      subsrc.level = 0;
466      subdst.face = 0;
467      subdst.level = 0;
468
469      if (psrc) {
470         gdpy->pipe->resource_copy_region(gdpy->pipe, ptex, subdst, 0, 0, 0,
471               gsurf->render_texture, subsrc, 0, 0, 0, ptex->width0, ptex->height0);
472
473         nsurf->flush_frontbuffer(nsurf);
474      }
475
476      pipe_resource_reference(&ptex, NULL);
477   }
478
479   nsurf->destroy(nsurf);
480
481   return EGL_TRUE;
482}
483
484static EGLBoolean
485egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
486{
487   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
488   struct egl_g3d_context *gctx = egl_g3d_context(ctx);
489   struct pipe_screen *screen = gdpy->native->screen;
490   struct pipe_fence_handle *fence = NULL;
491
492   gctx->stctxi->flush(gctx->stctxi,
493         PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
494   screen->fence_finish(screen, fence, 0);
495   screen->fence_reference(screen, &fence, NULL);
496
497   return EGL_TRUE;
498}
499
500static EGLBoolean
501egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
502{
503   _EGLContext *ctx = _eglGetCurrentContext();
504
505   if (engine != EGL_CORE_NATIVE_ENGINE)
506      return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
507
508   if (ctx && ctx->DrawSurface) {
509      struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface);
510
511      if (gsurf->native)
512         gsurf->native->wait(gsurf->native);
513   }
514
515   return EGL_TRUE;
516}
517
518static EGLBoolean
519egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
520                       _EGLSurface *surf, EGLint buffer)
521{
522   struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
523   _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API);
524   struct egl_g3d_context *gctx;
525   enum pipe_format internal_format;
526   enum st_texture_type target;
527
528   if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT)
529      return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
530   if (buffer != EGL_BACK_BUFFER)
531      return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
532   if (gsurf->base.BoundToTexture)
533      return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
534
535   switch (gsurf->base.TextureFormat) {
536   case EGL_TEXTURE_RGB:
537      internal_format = PIPE_FORMAT_R8G8B8_UNORM;
538      break;
539   case EGL_TEXTURE_RGBA:
540      internal_format = PIPE_FORMAT_B8G8R8A8_UNORM;
541      break;
542   default:
543      return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
544   }
545
546   switch (gsurf->base.TextureTarget) {
547   case EGL_TEXTURE_2D:
548      target = ST_TEXTURE_2D;
549      break;
550   default:
551      return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
552   }
553
554   if (!es1)
555      return EGL_TRUE;
556   if (!gsurf->render_texture)
557      return EGL_FALSE;
558
559   /* flush properly if the surface is bound */
560   if (gsurf->base.CurrentContext) {
561      gctx = egl_g3d_context(gsurf->base.CurrentContext);
562      gctx->stctxi->flush(gctx->stctxi,
563            PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
564   }
565
566   gctx = egl_g3d_context(es1);
567   if (gctx->stctxi->teximage) {
568      if (!gctx->stctxi->teximage(gctx->stctxi, target,
569               gsurf->base.MipmapLevel, internal_format,
570               gsurf->render_texture, gsurf->base.MipmapTexture))
571         return EGL_FALSE;
572      gsurf->base.BoundToTexture = EGL_TRUE;
573   }
574
575   return EGL_TRUE;
576}
577
578static EGLBoolean
579egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
580                          _EGLSurface *surf, EGLint buffer)
581{
582   struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
583
584   if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT ||
585       !gsurf->base.BoundToTexture)
586      return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
587   if (buffer != EGL_BACK_BUFFER)
588      return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
589
590   if (gsurf->render_texture) {
591      _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API);
592      struct egl_g3d_context *gctx = egl_g3d_context(ctx);
593
594      /* what if the context the surface binds to is no longer current? */
595      if (gctx) {
596         gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D,
597               gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE);
598      }
599   }
600
601   gsurf->base.BoundToTexture = EGL_FALSE;
602
603   return EGL_TRUE;
604}
605
606#ifdef EGL_MESA_screen_surface
607
608static _EGLSurface *
609egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
610                              _EGLConfig *conf, const EGLint *attribs)
611{
612   struct egl_g3d_create_surface_arg arg;
613
614   memset(&arg, 0, sizeof(arg));
615   arg.type = EGL_SCREEN_BIT_MESA;
616
617   return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
618}
619
620static EGLBoolean
621egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
622                            _EGLScreen *scr, _EGLSurface *surf,
623                            _EGLMode *mode)
624{
625   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
626   struct egl_g3d_screen *gscr = egl_g3d_screen(scr);
627   struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
628   struct native_surface *nsurf;
629   const struct native_mode *nmode;
630   EGLBoolean changed;
631
632   if (gsurf) {
633      EGLint idx;
634
635      if (!mode)
636         return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
637      if (gsurf->base.Type != EGL_SCREEN_BIT_MESA)
638         return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA");
639      if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height)
640         return _eglError(EGL_BAD_MATCH,
641               "eglShowSurfaceMESA(surface smaller than mode size)");
642
643      /* find the index of the mode */
644      for (idx = 0; idx < gscr->base.NumModes; idx++)
645         if (mode == &gscr->base.Modes[idx])
646            break;
647      if (idx >= gscr->base.NumModes) {
648         return _eglError(EGL_BAD_MODE_MESA,
649               "eglShowSurfaceMESA(unknown mode)");
650      }
651
652      nsurf = gsurf->native;
653      nmode = gscr->native_modes[idx];
654   }
655   else {
656      if (mode)
657         return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
658
659      /* disable the screen */
660      nsurf = NULL;
661      nmode = NULL;
662   }
663
664   /* TODO surface panning by CRTC choosing */
665   changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf,
666         gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode);
667   if (changed) {
668      gscr->base.CurrentSurface = &gsurf->base;
669      gscr->base.CurrentMode = mode;
670   }
671
672   return changed;
673}
674
675#endif /* EGL_MESA_screen_surface */
676
677/**
678 * Find a config that supports the pixmap.
679 */
680_EGLConfig *
681egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
682{
683   struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
684   struct egl_g3d_config *gconf;
685   EGLint i;
686
687   for (i = 0; i < dpy->NumConfigs; i++) {
688      gconf = egl_g3d_config(dpy->Configs[i]);
689      if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
690         break;
691   }
692
693   return (i < dpy->NumConfigs) ? &gconf->base : NULL;
694}
695
696void
697egl_g3d_init_driver_api(_EGLDriver *drv)
698{
699   _eglInitDriverFallbacks(drv);
700
701   drv->API.CreateContext = egl_g3d_create_context;
702   drv->API.DestroyContext = egl_g3d_destroy_context;
703   drv->API.CreateWindowSurface = egl_g3d_create_window_surface;
704   drv->API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
705   drv->API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
706   drv->API.DestroySurface = egl_g3d_destroy_surface;
707   drv->API.MakeCurrent = egl_g3d_make_current;
708   drv->API.SwapBuffers = egl_g3d_swap_buffers;
709   drv->API.CopyBuffers = egl_g3d_copy_buffers;
710   drv->API.WaitClient = egl_g3d_wait_client;
711   drv->API.WaitNative = egl_g3d_wait_native;
712
713   drv->API.BindTexImage = egl_g3d_bind_tex_image;
714   drv->API.ReleaseTexImage = egl_g3d_release_tex_image;
715
716   drv->API.CreateImageKHR = egl_g3d_create_image;
717   drv->API.DestroyImageKHR = egl_g3d_destroy_image;
718
719#ifdef EGL_MESA_screen_surface
720   drv->API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
721   drv->API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
722#endif
723}
724