Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / gallium / state_trackers / egl / common / egl_g3d_api.c
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_sync.h"
38 #include "egl_g3d_st.h"
39 #include "egl_g3d_loader.h"
40 #include "native.h"
41
42 /**
43 * Return the state tracker for the given context.
44 */
45 static struct st_api *
46 egl_g3d_choose_st(_EGLDriver *drv, _EGLContext *ctx)
47 {
48 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
49 EGLint idx = -1;
50
51 switch (ctx->ClientAPI) {
52 case EGL_OPENGL_ES_API:
53 switch (ctx->ClientVersion) {
54 case 1:
55 idx = ST_API_OPENGL_ES1;
56 break;
57 case 2:
58 idx = ST_API_OPENGL_ES2;
59 break;
60 default:
61 _eglLog(_EGL_WARNING, "unknown client version %d",
62 ctx->ClientVersion);
63 break;
64 }
65 break;
66 case EGL_OPENVG_API:
67 idx = ST_API_OPENVG;
68 break;
69 case EGL_OPENGL_API:
70 idx = ST_API_OPENGL;
71 break;
72 default:
73 _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI);
74 break;
75 }
76
77 return (idx >= 0) ? gdrv->loader->get_st_api(idx) : NULL;
78 }
79
80 static _EGLContext *
81 egl_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) ? &gconf->stvis : NULL, (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 */
121 static void
122 destroy_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
135 static EGLBoolean
136 egl_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
143 struct egl_g3d_create_surface_arg {
144 EGLint type;
145 union {
146 EGLNativeWindowType win;
147 EGLNativePixmapType pix;
148 } u;
149 };
150
151 static _EGLSurface *
152 egl_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
242 static _EGLSurface *
243 egl_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
256 static _EGLSurface *
257 egl_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
270 static struct egl_g3d_surface *
271 create_pbuffer_surface(_EGLDisplay *dpy, _EGLConfig *conf,
272 const EGLint *attribs, const char *func)
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, func);
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;
297 }
298
299 static _EGLSurface *
300 egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
301 _EGLConfig *conf, const EGLint *attribs)
302 {
303 struct egl_g3d_surface *gsurf;
304 struct pipe_resource *ptex = NULL;
305
306 gsurf = create_pbuffer_surface(dpy, conf, attribs,
307 "eglCreatePbufferSurface");
308 if (!gsurf)
309 return NULL;
310
311 gsurf->client_buffer_type = EGL_NONE;
312
313 if (!gsurf->stfbi->validate(gsurf->stfbi,
314 &gsurf->stvis.render_buffer, 1, &ptex)) {
315 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
316 FREE(gsurf);
317 return NULL;
318 }
319
320 return &gsurf->base;
321 }
322
323 static _EGLSurface *
324 egl_g3d_create_pbuffer_from_client_buffer(_EGLDriver *drv, _EGLDisplay *dpy,
325 EGLenum buftype,
326 EGLClientBuffer buffer,
327 _EGLConfig *conf,
328 const EGLint *attribs)
329 {
330 struct egl_g3d_surface *gsurf;
331 struct pipe_resource *ptex = NULL;
332 EGLint pbuffer_attribs[32];
333 EGLint count, i;
334
335 switch (buftype) {
336 case EGL_OPENVG_IMAGE:
337 break;
338 default:
339 _eglError(EGL_BAD_PARAMETER, "eglCreatePbufferFromClientBuffer");
340 return NULL;
341 break;
342 }
343
344 /* parse the attributes first */
345 count = 0;
346 for (i = 0; attribs && attribs[i] != EGL_NONE; i++) {
347 EGLint attr = attribs[i++];
348 EGLint val = attribs[i];
349 EGLint err = EGL_SUCCESS;
350
351 switch (attr) {
352 case EGL_TEXTURE_FORMAT:
353 case EGL_TEXTURE_TARGET:
354 case EGL_MIPMAP_TEXTURE:
355 pbuffer_attribs[count++] = attr;
356 pbuffer_attribs[count++] = val;
357 break;
358 default:
359 err = EGL_BAD_ATTRIBUTE;
360 break;
361 }
362 /* bail out */
363 if (err != EGL_SUCCESS) {
364 _eglError(err, "eglCreatePbufferFromClientBuffer");
365 return NULL;
366 }
367 }
368
369 pbuffer_attribs[count++] = EGL_NONE;
370
371 gsurf = create_pbuffer_surface(dpy, conf, pbuffer_attribs,
372 "eglCreatePbufferFromClientBuffer");
373 if (!gsurf)
374 return NULL;
375
376 gsurf->client_buffer_type = buftype;
377 gsurf->client_buffer = buffer;
378
379 if (!gsurf->stfbi->validate(gsurf->stfbi,
380 &gsurf->stvis.render_buffer, 1, &ptex)) {
381 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
382 FREE(gsurf);
383 return NULL;
384 }
385
386 return &gsurf->base;
387 }
388
389 /**
390 * Destroy a surface.
391 */
392 static void
393 destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
394 {
395 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
396
397 /* FIXME a surface might live longer than its display */
398 if (!dpy->Initialized)
399 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display");
400
401 pipe_resource_reference(&gsurf->render_texture, NULL);
402 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
403 if (gsurf->native)
404 gsurf->native->destroy(gsurf->native);
405 FREE(gsurf);
406 }
407
408 static EGLBoolean
409 egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
410 {
411 if (!_eglIsSurfaceBound(surf))
412 destroy_surface(dpy, surf);
413 return EGL_TRUE;
414 }
415
416 static EGLBoolean
417 egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
418 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
419 {
420 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
421 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
422 struct egl_g3d_surface *gread = egl_g3d_surface(read);
423 struct egl_g3d_context *old_gctx;
424 EGLBoolean ok = EGL_TRUE;
425
426 /* bind the new context and return the "orphaned" one */
427 if (!_eglBindContext(&ctx, &draw, &read))
428 return EGL_FALSE;
429 old_gctx = egl_g3d_context(ctx);
430
431 if (old_gctx) {
432 /* flush old context */
433 old_gctx->stctxi->flush(old_gctx->stctxi,
434 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
435 }
436
437 if (gctx) {
438 ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi,
439 (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
440 if (ok) {
441 if (gdraw) {
442 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
443 gdraw->stfbi);
444
445 if (gdraw->base.Type == EGL_WINDOW_BIT) {
446 gctx->base.WindowRenderBuffer =
447 (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
448 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
449 }
450 }
451 if (gread && gread != gdraw) {
452 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
453 gread->stfbi);
454 }
455 }
456 }
457 else if (old_gctx) {
458 ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL);
459 old_gctx->base.WindowRenderBuffer = EGL_NONE;
460 }
461
462 if (ctx && !_eglIsContextLinked(ctx))
463 destroy_context(dpy, ctx);
464 if (draw && !_eglIsSurfaceLinked(draw))
465 destroy_surface(dpy, draw);
466 if (read && read != draw && !_eglIsSurfaceLinked(read))
467 destroy_surface(dpy, read);
468
469 return ok;
470 }
471
472 static EGLBoolean
473 egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
474 {
475 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
476 _EGLContext *ctx = _eglGetCurrentContext();
477 struct egl_g3d_context *gctx = NULL;
478
479 /* no-op for pixmap or pbuffer surface */
480 if (gsurf->base.Type == EGL_PIXMAP_BIT ||
481 gsurf->base.Type == EGL_PBUFFER_BIT)
482 return EGL_TRUE;
483
484 /* or when the surface is single-buffered */
485 if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT)
486 return EGL_TRUE;
487
488 if (ctx && ctx->DrawSurface == surf)
489 gctx = egl_g3d_context(ctx);
490
491 /* flush if the surface is current */
492 if (gctx) {
493 gctx->stctxi->flush(gctx->stctxi,
494 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
495 }
496
497 return gsurf->native->swap_buffers(gsurf->native);
498 }
499
500 /**
501 * Get the pipe surface of the given attachment of the native surface.
502 */
503 static struct pipe_resource *
504 get_pipe_resource(struct native_display *ndpy, struct native_surface *nsurf,
505 enum native_attachment natt)
506 {
507 struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
508
509 textures[natt] = NULL;
510 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
511
512 return textures[natt];
513 }
514
515 static EGLBoolean
516 egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
517 EGLNativePixmapType target)
518 {
519 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
520 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
521 _EGLContext *ctx = _eglGetCurrentContext();
522 struct egl_g3d_config *gconf;
523 struct native_surface *nsurf;
524 struct pipe_resource *ptex;
525
526 if (!gsurf->render_texture)
527 return EGL_TRUE;
528
529 gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
530 if (!gconf)
531 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
532
533 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
534 target, gconf->native);
535 if (!nsurf)
536 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
537
538 /* flush if the surface is current */
539 if (ctx && ctx->DrawSurface == &gsurf->base) {
540 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
541 gctx->stctxi->flush(gctx->stctxi,
542 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
543 }
544
545 /* create a pipe context to copy surfaces */
546 if (!gdpy->pipe) {
547 gdpy->pipe =
548 gdpy->native->screen->context_create(gdpy->native->screen, NULL);
549 if (!gdpy->pipe)
550 return EGL_FALSE;
551 }
552
553 ptex = get_pipe_resource(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
554 if (ptex) {
555 struct pipe_resource *psrc = gsurf->render_texture;
556 struct pipe_subresource subsrc, subdst;
557 subsrc.face = 0;
558 subsrc.level = 0;
559 subdst.face = 0;
560 subdst.level = 0;
561
562 if (psrc) {
563 gdpy->pipe->resource_copy_region(gdpy->pipe, ptex, subdst, 0, 0, 0,
564 gsurf->render_texture, subsrc, 0, 0, 0, ptex->width0, ptex->height0);
565
566 nsurf->flush_frontbuffer(nsurf);
567 }
568
569 pipe_resource_reference(&ptex, NULL);
570 }
571
572 nsurf->destroy(nsurf);
573
574 return EGL_TRUE;
575 }
576
577 static EGLBoolean
578 egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
579 {
580 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
581 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
582 struct pipe_screen *screen = gdpy->native->screen;
583 struct pipe_fence_handle *fence = NULL;
584
585 gctx->stctxi->flush(gctx->stctxi,
586 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
587 screen->fence_finish(screen, fence, 0);
588 screen->fence_reference(screen, &fence, NULL);
589
590 return EGL_TRUE;
591 }
592
593 static EGLBoolean
594 egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
595 {
596 _EGLContext *ctx = _eglGetCurrentContext();
597
598 if (engine != EGL_CORE_NATIVE_ENGINE)
599 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
600
601 if (ctx && ctx->DrawSurface) {
602 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface);
603
604 if (gsurf->native)
605 gsurf->native->wait(gsurf->native);
606 }
607
608 return EGL_TRUE;
609 }
610
611 static EGLBoolean
612 egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
613 _EGLSurface *surf, EGLint buffer)
614 {
615 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
616 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API);
617 struct egl_g3d_context *gctx;
618 enum pipe_format internal_format;
619 enum st_texture_type target;
620
621 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT)
622 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
623 if (buffer != EGL_BACK_BUFFER)
624 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
625 if (gsurf->base.BoundToTexture)
626 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
627
628 switch (gsurf->base.TextureFormat) {
629 case EGL_TEXTURE_RGB:
630 internal_format = PIPE_FORMAT_R8G8B8_UNORM;
631 break;
632 case EGL_TEXTURE_RGBA:
633 internal_format = PIPE_FORMAT_B8G8R8A8_UNORM;
634 break;
635 default:
636 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
637 }
638
639 switch (gsurf->base.TextureTarget) {
640 case EGL_TEXTURE_2D:
641 target = ST_TEXTURE_2D;
642 break;
643 default:
644 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
645 }
646
647 if (!es1)
648 return EGL_TRUE;
649 if (!gsurf->render_texture)
650 return EGL_FALSE;
651
652 /* flush properly if the surface is bound */
653 if (gsurf->base.CurrentContext) {
654 gctx = egl_g3d_context(gsurf->base.CurrentContext);
655 gctx->stctxi->flush(gctx->stctxi,
656 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
657 }
658
659 gctx = egl_g3d_context(es1);
660 if (gctx->stctxi->teximage) {
661 if (!gctx->stctxi->teximage(gctx->stctxi, target,
662 gsurf->base.MipmapLevel, internal_format,
663 gsurf->render_texture, gsurf->base.MipmapTexture))
664 return EGL_FALSE;
665 gsurf->base.BoundToTexture = EGL_TRUE;
666 }
667
668 return EGL_TRUE;
669 }
670
671 static EGLBoolean
672 egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
673 _EGLSurface *surf, EGLint buffer)
674 {
675 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
676
677 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT ||
678 !gsurf->base.BoundToTexture)
679 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
680 if (buffer != EGL_BACK_BUFFER)
681 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
682
683 if (gsurf->render_texture) {
684 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API);
685 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
686
687 /* what if the context the surface binds to is no longer current? */
688 if (gctx) {
689 gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D,
690 gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE);
691 }
692 }
693
694 gsurf->base.BoundToTexture = EGL_FALSE;
695
696 return EGL_TRUE;
697 }
698
699 #ifdef EGL_MESA_screen_surface
700
701 static _EGLSurface *
702 egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
703 _EGLConfig *conf, const EGLint *attribs)
704 {
705 struct egl_g3d_create_surface_arg arg;
706
707 memset(&arg, 0, sizeof(arg));
708 arg.type = EGL_SCREEN_BIT_MESA;
709
710 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
711 }
712
713 static EGLBoolean
714 egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
715 _EGLScreen *scr, _EGLSurface *surf,
716 _EGLMode *mode)
717 {
718 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
719 struct egl_g3d_screen *gscr = egl_g3d_screen(scr);
720 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
721 struct native_surface *nsurf;
722 const struct native_mode *nmode;
723 EGLBoolean changed;
724
725 if (gsurf) {
726 EGLint idx;
727
728 if (!mode)
729 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
730 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA)
731 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA");
732 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height)
733 return _eglError(EGL_BAD_MATCH,
734 "eglShowSurfaceMESA(surface smaller than mode size)");
735
736 /* find the index of the mode */
737 for (idx = 0; idx < gscr->base.NumModes; idx++)
738 if (mode == &gscr->base.Modes[idx])
739 break;
740 if (idx >= gscr->base.NumModes) {
741 return _eglError(EGL_BAD_MODE_MESA,
742 "eglShowSurfaceMESA(unknown mode)");
743 }
744
745 nsurf = gsurf->native;
746 nmode = gscr->native_modes[idx];
747 }
748 else {
749 if (mode)
750 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
751
752 /* disable the screen */
753 nsurf = NULL;
754 nmode = NULL;
755 }
756
757 /* TODO surface panning by CRTC choosing */
758 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf,
759 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode);
760 if (changed) {
761 gscr->base.CurrentSurface = &gsurf->base;
762 gscr->base.CurrentMode = mode;
763 }
764
765 return changed;
766 }
767
768 #endif /* EGL_MESA_screen_surface */
769
770 /**
771 * Find a config that supports the pixmap.
772 */
773 _EGLConfig *
774 egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
775 {
776 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
777 struct egl_g3d_config *gconf;
778 EGLint i;
779
780 for (i = 0; i < dpy->Configs->Size; i++) {
781 gconf = egl_g3d_config((_EGLConfig *) dpy->Configs->Elements[i]);
782 if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
783 break;
784 }
785
786 return (i < dpy->Configs->Size) ? &gconf->base : NULL;
787 }
788
789 void
790 egl_g3d_init_driver_api(_EGLDriver *drv)
791 {
792 _eglInitDriverFallbacks(drv);
793
794 drv->API.CreateContext = egl_g3d_create_context;
795 drv->API.DestroyContext = egl_g3d_destroy_context;
796 drv->API.CreateWindowSurface = egl_g3d_create_window_surface;
797 drv->API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
798 drv->API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
799 drv->API.CreatePbufferFromClientBuffer = egl_g3d_create_pbuffer_from_client_buffer;
800 drv->API.DestroySurface = egl_g3d_destroy_surface;
801 drv->API.MakeCurrent = egl_g3d_make_current;
802 drv->API.SwapBuffers = egl_g3d_swap_buffers;
803 drv->API.CopyBuffers = egl_g3d_copy_buffers;
804 drv->API.WaitClient = egl_g3d_wait_client;
805 drv->API.WaitNative = egl_g3d_wait_native;
806
807 drv->API.BindTexImage = egl_g3d_bind_tex_image;
808 drv->API.ReleaseTexImage = egl_g3d_release_tex_image;
809
810 drv->API.CreateImageKHR = egl_g3d_create_image;
811 drv->API.DestroyImageKHR = egl_g3d_destroy_image;
812 #ifdef EGL_MESA_drm_image
813 drv->API.CreateDRMImageMESA = egl_g3d_create_drm_image;
814 drv->API.ExportDRMImageMESA = egl_g3d_export_drm_image;
815 #endif
816
817 #ifdef EGL_KHR_reusable_sync
818 drv->API.CreateSyncKHR = egl_g3d_create_sync;
819 drv->API.DestroySyncKHR = egl_g3d_destroy_sync;
820 drv->API.ClientWaitSyncKHR = egl_g3d_client_wait_sync;
821 drv->API.SignalSyncKHR = egl_g3d_signal_sync;
822 #endif
823
824 #ifdef EGL_MESA_screen_surface
825 drv->API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
826 drv->API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
827 #endif
828 }