Merge branch 'llvm-cliptest-viewport'
[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 enum st_profile_type *profile)
48 {
49 struct egl_g3d_driver *gdrv = egl_g3d_driver(drv);
50 struct st_api *stapi;
51 EGLint api = -1;
52
53 *profile = ST_PROFILE_DEFAULT;
54
55 switch (ctx->ClientAPI) {
56 case EGL_OPENGL_ES_API:
57 switch (ctx->ClientVersion) {
58 case 1:
59 api = ST_API_OPENGL;
60 *profile = ST_PROFILE_OPENGL_ES1;
61 break;
62 case 2:
63 api = ST_API_OPENGL;
64 *profile = ST_PROFILE_OPENGL_ES2;
65 break;
66 default:
67 _eglLog(_EGL_WARNING, "unknown client version %d",
68 ctx->ClientVersion);
69 break;
70 }
71 break;
72 case EGL_OPENVG_API:
73 api = ST_API_OPENVG;
74 break;
75 case EGL_OPENGL_API:
76 api = ST_API_OPENGL;
77 break;
78 default:
79 _eglLog(_EGL_WARNING, "unknown client API 0x%04x", ctx->ClientAPI);
80 break;
81 }
82
83 switch (api) {
84 case ST_API_OPENGL:
85 stapi = gdrv->loader->guess_gl_api(*profile);
86 break;
87 case ST_API_OPENVG:
88 stapi = gdrv->loader->get_st_api(api);
89 break;
90 default:
91 stapi = NULL;
92 break;
93 }
94 if (stapi && !(stapi->profile_mask & (1 << *profile)))
95 stapi = NULL;
96
97 return stapi;
98 }
99
100 static _EGLContext *
101 egl_g3d_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
102 _EGLContext *share, const EGLint *attribs)
103 {
104 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
105 struct egl_g3d_context *gshare = egl_g3d_context(share);
106 struct egl_g3d_config *gconf = egl_g3d_config(conf);
107 struct egl_g3d_context *gctx;
108 struct st_context_attribs stattribs;
109
110 gctx = CALLOC_STRUCT(egl_g3d_context);
111 if (!gctx) {
112 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
113 return NULL;
114 }
115
116 if (!_eglInitContext(&gctx->base, dpy, conf, attribs)) {
117 FREE(gctx);
118 return NULL;
119 }
120
121 memset(&stattribs, 0, sizeof(stattribs));
122 if (gconf)
123 stattribs.visual = gconf->stvis;
124
125 gctx->stapi = egl_g3d_choose_st(drv, &gctx->base, &stattribs.profile);
126 if (!gctx->stapi) {
127 FREE(gctx);
128 return NULL;
129 }
130
131 gctx->stctxi = gctx->stapi->create_context(gctx->stapi, gdpy->smapi,
132 &stattribs, (gshare) ? gshare->stctxi : NULL);
133 if (!gctx->stctxi) {
134 FREE(gctx);
135 return NULL;
136 }
137
138 gctx->stctxi->st_manager_private = (void *) &gctx->base;
139
140 return &gctx->base;
141 }
142
143 /**
144 * Destroy a context.
145 */
146 static void
147 destroy_context(_EGLDisplay *dpy, _EGLContext *ctx)
148 {
149 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
150
151 /* FIXME a context might live longer than its display */
152 if (!dpy->Initialized)
153 _eglLog(_EGL_FATAL, "destroy a context with an unitialized display");
154
155 gctx->stctxi->destroy(gctx->stctxi);
156
157 FREE(gctx);
158 }
159
160 static EGLBoolean
161 egl_g3d_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
162 {
163 if (!_eglIsContextBound(ctx))
164 destroy_context(dpy, ctx);
165 return EGL_TRUE;
166 }
167
168 struct egl_g3d_create_surface_arg {
169 EGLint type;
170 union {
171 EGLNativeWindowType win;
172 EGLNativePixmapType pix;
173 } u;
174 };
175
176 static _EGLSurface *
177 egl_g3d_create_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
178 struct egl_g3d_create_surface_arg *arg,
179 const EGLint *attribs)
180 {
181 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
182 struct egl_g3d_config *gconf = egl_g3d_config(conf);
183 struct egl_g3d_surface *gsurf;
184 struct native_surface *nsurf;
185 const char *err;
186
187 switch (arg->type) {
188 case EGL_WINDOW_BIT:
189 err = "eglCreateWindowSurface";
190 break;
191 case EGL_PIXMAP_BIT:
192 err = "eglCreatePixmapSurface";
193 break;
194 #ifdef EGL_MESA_screen_surface
195 case EGL_SCREEN_BIT_MESA:
196 err = "eglCreateScreenSurface";
197 break;
198 #endif
199 default:
200 err = "eglCreateUnknownSurface";
201 break;
202 }
203
204 gsurf = CALLOC_STRUCT(egl_g3d_surface);
205 if (!gsurf) {
206 _eglError(EGL_BAD_ALLOC, err);
207 return NULL;
208 }
209
210 if (!_eglInitSurface(&gsurf->base, dpy, arg->type, conf, attribs)) {
211 FREE(gsurf);
212 return NULL;
213 }
214
215 /* create the native surface */
216 switch (arg->type) {
217 case EGL_WINDOW_BIT:
218 nsurf = gdpy->native->create_window_surface(gdpy->native,
219 arg->u.win, gconf->native);
220 break;
221 case EGL_PIXMAP_BIT:
222 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
223 arg->u.pix, gconf->native);
224 break;
225 #ifdef EGL_MESA_screen_surface
226 case EGL_SCREEN_BIT_MESA:
227 /* prefer back buffer (move to _eglInitSurface?) */
228 gsurf->base.RenderBuffer = EGL_BACK_BUFFER;
229 nsurf = gdpy->native->modeset->create_scanout_surface(gdpy->native,
230 gconf->native, gsurf->base.Width, gsurf->base.Height);
231 break;
232 #endif
233 default:
234 nsurf = NULL;
235 break;
236 }
237
238 if (!nsurf) {
239 FREE(gsurf);
240 return NULL;
241 }
242 /* initialize the geometry */
243 if (!nsurf->validate(nsurf, 0x0, &gsurf->sequence_number, NULL,
244 &gsurf->base.Width, &gsurf->base.Height)) {
245 nsurf->destroy(nsurf);
246 FREE(gsurf);
247 return NULL;
248 }
249
250 gsurf->stvis = gconf->stvis;
251 if (gsurf->base.RenderBuffer == EGL_SINGLE_BUFFER)
252 gsurf->stvis.render_buffer = ST_ATTACHMENT_FRONT_LEFT;
253
254 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
255 if (!gsurf->stfbi) {
256 nsurf->destroy(nsurf);
257 FREE(gsurf);
258 return NULL;
259 }
260
261 nsurf->user_data = &gsurf->base;
262 gsurf->native = nsurf;
263
264 return &gsurf->base;
265 }
266
267 static _EGLSurface *
268 egl_g3d_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy,
269 _EGLConfig *conf, EGLNativeWindowType win,
270 const EGLint *attribs)
271 {
272 struct egl_g3d_create_surface_arg arg;
273
274 memset(&arg, 0, sizeof(arg));
275 arg.type = EGL_WINDOW_BIT;
276 arg.u.win = win;
277
278 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
279 }
280
281 static _EGLSurface *
282 egl_g3d_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy,
283 _EGLConfig *conf, EGLNativePixmapType pix,
284 const EGLint *attribs)
285 {
286 struct egl_g3d_create_surface_arg arg;
287
288 memset(&arg, 0, sizeof(arg));
289 arg.type = EGL_PIXMAP_BIT;
290 arg.u.pix = pix;
291
292 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
293 }
294
295 static struct egl_g3d_surface *
296 create_pbuffer_surface(_EGLDisplay *dpy, _EGLConfig *conf,
297 const EGLint *attribs, const char *func)
298 {
299 struct egl_g3d_config *gconf = egl_g3d_config(conf);
300 struct egl_g3d_surface *gsurf;
301
302 gsurf = CALLOC_STRUCT(egl_g3d_surface);
303 if (!gsurf) {
304 _eglError(EGL_BAD_ALLOC, func);
305 return NULL;
306 }
307
308 if (!_eglInitSurface(&gsurf->base, dpy, EGL_PBUFFER_BIT, conf, attribs)) {
309 FREE(gsurf);
310 return NULL;
311 }
312
313 gsurf->stvis = gconf->stvis;
314
315 gsurf->stfbi = egl_g3d_create_st_framebuffer(&gsurf->base);
316 if (!gsurf->stfbi) {
317 FREE(gsurf);
318 return NULL;
319 }
320
321 return gsurf;
322 }
323
324 static _EGLSurface *
325 egl_g3d_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy,
326 _EGLConfig *conf, const EGLint *attribs)
327 {
328 struct egl_g3d_surface *gsurf;
329 struct pipe_resource *ptex = NULL;
330
331 gsurf = create_pbuffer_surface(dpy, conf, attribs,
332 "eglCreatePbufferSurface");
333 if (!gsurf)
334 return NULL;
335
336 gsurf->client_buffer_type = EGL_NONE;
337
338 if (!gsurf->stfbi->validate(gsurf->stfbi,
339 &gsurf->stvis.render_buffer, 1, &ptex)) {
340 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
341 FREE(gsurf);
342 return NULL;
343 }
344
345 return &gsurf->base;
346 }
347
348 static _EGLSurface *
349 egl_g3d_create_pbuffer_from_client_buffer(_EGLDriver *drv, _EGLDisplay *dpy,
350 EGLenum buftype,
351 EGLClientBuffer buffer,
352 _EGLConfig *conf,
353 const EGLint *attribs)
354 {
355 struct egl_g3d_surface *gsurf;
356 struct pipe_resource *ptex = NULL;
357 EGLint pbuffer_attribs[32];
358 EGLint count, i;
359
360 switch (buftype) {
361 case EGL_OPENVG_IMAGE:
362 break;
363 default:
364 _eglError(EGL_BAD_PARAMETER, "eglCreatePbufferFromClientBuffer");
365 return NULL;
366 break;
367 }
368
369 /* parse the attributes first */
370 count = 0;
371 for (i = 0; attribs && attribs[i] != EGL_NONE; i++) {
372 EGLint attr = attribs[i++];
373 EGLint val = attribs[i];
374 EGLint err = EGL_SUCCESS;
375
376 switch (attr) {
377 case EGL_TEXTURE_FORMAT:
378 case EGL_TEXTURE_TARGET:
379 case EGL_MIPMAP_TEXTURE:
380 pbuffer_attribs[count++] = attr;
381 pbuffer_attribs[count++] = val;
382 break;
383 default:
384 err = EGL_BAD_ATTRIBUTE;
385 break;
386 }
387 /* bail out */
388 if (err != EGL_SUCCESS) {
389 _eglError(err, "eglCreatePbufferFromClientBuffer");
390 return NULL;
391 }
392 }
393
394 pbuffer_attribs[count++] = EGL_NONE;
395
396 gsurf = create_pbuffer_surface(dpy, conf, pbuffer_attribs,
397 "eglCreatePbufferFromClientBuffer");
398 if (!gsurf)
399 return NULL;
400
401 gsurf->client_buffer_type = buftype;
402 gsurf->client_buffer = buffer;
403
404 if (!gsurf->stfbi->validate(gsurf->stfbi,
405 &gsurf->stvis.render_buffer, 1, &ptex)) {
406 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
407 FREE(gsurf);
408 return NULL;
409 }
410
411 return &gsurf->base;
412 }
413
414 /**
415 * Destroy a surface.
416 */
417 static void
418 destroy_surface(_EGLDisplay *dpy, _EGLSurface *surf)
419 {
420 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
421
422 /* FIXME a surface might live longer than its display */
423 if (!dpy->Initialized)
424 _eglLog(_EGL_FATAL, "destroy a surface with an unitialized display");
425
426 pipe_resource_reference(&gsurf->render_texture, NULL);
427 egl_g3d_destroy_st_framebuffer(gsurf->stfbi);
428 if (gsurf->native)
429 gsurf->native->destroy(gsurf->native);
430 FREE(gsurf);
431 }
432
433 static EGLBoolean
434 egl_g3d_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
435 {
436 if (!_eglIsSurfaceBound(surf))
437 destroy_surface(dpy, surf);
438 return EGL_TRUE;
439 }
440
441 static EGLBoolean
442 egl_g3d_make_current(_EGLDriver *drv, _EGLDisplay *dpy,
443 _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
444 {
445 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
446 struct egl_g3d_surface *gdraw = egl_g3d_surface(draw);
447 struct egl_g3d_surface *gread = egl_g3d_surface(read);
448 struct egl_g3d_context *old_gctx;
449 EGLBoolean ok = EGL_TRUE;
450
451 /* bind the new context and return the "orphaned" one */
452 if (!_eglBindContext(&ctx, &draw, &read))
453 return EGL_FALSE;
454 old_gctx = egl_g3d_context(ctx);
455
456 if (old_gctx) {
457 /* flush old context */
458 old_gctx->stctxi->flush(old_gctx->stctxi,
459 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
460 }
461
462 if (gctx) {
463 ok = gctx->stapi->make_current(gctx->stapi, gctx->stctxi,
464 (gdraw) ? gdraw->stfbi : NULL, (gread) ? gread->stfbi : NULL);
465 if (ok) {
466 if (gdraw) {
467 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
468 gdraw->stfbi);
469
470 if (gdraw->base.Type == EGL_WINDOW_BIT) {
471 gctx->base.WindowRenderBuffer =
472 (gdraw->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT) ?
473 EGL_SINGLE_BUFFER : EGL_BACK_BUFFER;
474 }
475 }
476 if (gread && gread != gdraw) {
477 gctx->stctxi->notify_invalid_framebuffer(gctx->stctxi,
478 gread->stfbi);
479 }
480 }
481 }
482 else if (old_gctx) {
483 ok = old_gctx->stapi->make_current(old_gctx->stapi, NULL, NULL, NULL);
484 old_gctx->base.WindowRenderBuffer = EGL_NONE;
485 }
486
487 if (ctx && !_eglIsContextLinked(ctx))
488 destroy_context(dpy, ctx);
489 if (draw && !_eglIsSurfaceLinked(draw))
490 destroy_surface(dpy, draw);
491 if (read && read != draw && !_eglIsSurfaceLinked(read))
492 destroy_surface(dpy, read);
493
494 return ok;
495 }
496
497 static EGLBoolean
498 egl_g3d_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
499 {
500 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
501 _EGLContext *ctx = _eglGetCurrentContext();
502 struct egl_g3d_context *gctx = NULL;
503
504 /* no-op for pixmap or pbuffer surface */
505 if (gsurf->base.Type == EGL_PIXMAP_BIT ||
506 gsurf->base.Type == EGL_PBUFFER_BIT)
507 return EGL_TRUE;
508
509 /* or when the surface is single-buffered */
510 if (gsurf->stvis.render_buffer == ST_ATTACHMENT_FRONT_LEFT)
511 return EGL_TRUE;
512
513 if (ctx && ctx->DrawSurface == surf)
514 gctx = egl_g3d_context(ctx);
515
516 /* flush if the surface is current */
517 if (gctx) {
518 gctx->stctxi->flush(gctx->stctxi,
519 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
520 }
521
522 return gsurf->native->swap_buffers(gsurf->native);
523 }
524
525 /**
526 * Get the pipe surface of the given attachment of the native surface.
527 */
528 static struct pipe_resource *
529 get_pipe_resource(struct native_display *ndpy, struct native_surface *nsurf,
530 enum native_attachment natt)
531 {
532 struct pipe_resource *textures[NUM_NATIVE_ATTACHMENTS];
533
534 textures[natt] = NULL;
535 nsurf->validate(nsurf, 1 << natt, NULL, textures, NULL, NULL);
536
537 return textures[natt];
538 }
539
540 static EGLBoolean
541 egl_g3d_copy_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
542 EGLNativePixmapType target)
543 {
544 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
545 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
546 _EGLContext *ctx = _eglGetCurrentContext();
547 struct egl_g3d_config *gconf;
548 struct native_surface *nsurf;
549 struct pipe_resource *ptex;
550
551 if (!gsurf->render_texture)
552 return EGL_TRUE;
553
554 gconf = egl_g3d_config(egl_g3d_find_pixmap_config(dpy, target));
555 if (!gconf)
556 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
557
558 nsurf = gdpy->native->create_pixmap_surface(gdpy->native,
559 target, gconf->native);
560 if (!nsurf)
561 return _eglError(EGL_BAD_NATIVE_PIXMAP, "eglCopyBuffers");
562
563 /* flush if the surface is current */
564 if (ctx && ctx->DrawSurface == &gsurf->base) {
565 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
566 gctx->stctxi->flush(gctx->stctxi,
567 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
568 }
569
570 /* create a pipe context to copy surfaces */
571 if (!gdpy->pipe) {
572 gdpy->pipe =
573 gdpy->native->screen->context_create(gdpy->native->screen, NULL);
574 if (!gdpy->pipe)
575 return EGL_FALSE;
576 }
577
578 ptex = get_pipe_resource(gdpy->native, nsurf, NATIVE_ATTACHMENT_FRONT_LEFT);
579 if (ptex) {
580 struct pipe_resource *psrc = gsurf->render_texture;
581 struct pipe_subresource subsrc, subdst;
582 subsrc.face = 0;
583 subsrc.level = 0;
584 subdst.face = 0;
585 subdst.level = 0;
586
587 if (psrc) {
588 gdpy->pipe->resource_copy_region(gdpy->pipe, ptex, subdst, 0, 0, 0,
589 gsurf->render_texture, subsrc, 0, 0, 0, ptex->width0, ptex->height0);
590
591 nsurf->flush_frontbuffer(nsurf);
592 }
593
594 pipe_resource_reference(&ptex, NULL);
595 }
596
597 nsurf->destroy(nsurf);
598
599 return EGL_TRUE;
600 }
601
602 static EGLBoolean
603 egl_g3d_wait_client(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
604 {
605 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
606 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
607 struct pipe_screen *screen = gdpy->native->screen;
608 struct pipe_fence_handle *fence = NULL;
609
610 gctx->stctxi->flush(gctx->stctxi,
611 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
612 if (fence) {
613 screen->fence_finish(screen, fence, 0);
614 screen->fence_reference(screen, &fence, NULL);
615 }
616
617 return EGL_TRUE;
618 }
619
620 static EGLBoolean
621 egl_g3d_wait_native(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
622 {
623 _EGLContext *ctx = _eglGetCurrentContext();
624
625 if (engine != EGL_CORE_NATIVE_ENGINE)
626 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
627
628 if (ctx && ctx->DrawSurface) {
629 struct egl_g3d_surface *gsurf = egl_g3d_surface(ctx->DrawSurface);
630
631 if (gsurf->native)
632 gsurf->native->wait(gsurf->native);
633 }
634
635 return EGL_TRUE;
636 }
637
638 static EGLBoolean
639 egl_g3d_bind_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
640 _EGLSurface *surf, EGLint buffer)
641 {
642 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
643 _EGLContext *es1 = _eglGetAPIContext(EGL_OPENGL_ES_API);
644 struct egl_g3d_context *gctx;
645 enum pipe_format internal_format;
646 enum st_texture_type target;
647
648 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT)
649 return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
650 if (buffer != EGL_BACK_BUFFER)
651 return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
652 if (gsurf->base.BoundToTexture)
653 return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
654
655 switch (gsurf->base.TextureFormat) {
656 case EGL_TEXTURE_RGB:
657 internal_format = PIPE_FORMAT_R8G8B8_UNORM;
658 break;
659 case EGL_TEXTURE_RGBA:
660 internal_format = PIPE_FORMAT_B8G8R8A8_UNORM;
661 break;
662 default:
663 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
664 }
665
666 switch (gsurf->base.TextureTarget) {
667 case EGL_TEXTURE_2D:
668 target = ST_TEXTURE_2D;
669 break;
670 default:
671 return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
672 }
673
674 if (!es1)
675 return EGL_TRUE;
676 if (!gsurf->render_texture)
677 return EGL_FALSE;
678
679 /* flush properly if the surface is bound */
680 if (gsurf->base.CurrentContext) {
681 gctx = egl_g3d_context(gsurf->base.CurrentContext);
682 gctx->stctxi->flush(gctx->stctxi,
683 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
684 }
685
686 gctx = egl_g3d_context(es1);
687 if (gctx->stctxi->teximage) {
688 if (!gctx->stctxi->teximage(gctx->stctxi, target,
689 gsurf->base.MipmapLevel, internal_format,
690 gsurf->render_texture, gsurf->base.MipmapTexture))
691 return EGL_FALSE;
692 gsurf->base.BoundToTexture = EGL_TRUE;
693 }
694
695 return EGL_TRUE;
696 }
697
698 static EGLBoolean
699 egl_g3d_release_tex_image(_EGLDriver *drv, _EGLDisplay *dpy,
700 _EGLSurface *surf, EGLint buffer)
701 {
702 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
703
704 if (!gsurf || gsurf->base.Type != EGL_PBUFFER_BIT ||
705 !gsurf->base.BoundToTexture)
706 return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
707 if (buffer != EGL_BACK_BUFFER)
708 return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
709
710 if (gsurf->render_texture) {
711 _EGLContext *ctx = _eglGetAPIContext(EGL_OPENGL_ES_API);
712 struct egl_g3d_context *gctx = egl_g3d_context(ctx);
713
714 /* what if the context the surface binds to is no longer current? */
715 if (gctx) {
716 gctx->stctxi->teximage(gctx->stctxi, ST_TEXTURE_2D,
717 gsurf->base.MipmapLevel, PIPE_FORMAT_NONE, NULL, FALSE);
718 }
719 }
720
721 gsurf->base.BoundToTexture = EGL_FALSE;
722
723 return EGL_TRUE;
724 }
725
726 #ifdef EGL_MESA_screen_surface
727
728 static _EGLSurface *
729 egl_g3d_create_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
730 _EGLConfig *conf, const EGLint *attribs)
731 {
732 struct egl_g3d_create_surface_arg arg;
733
734 memset(&arg, 0, sizeof(arg));
735 arg.type = EGL_SCREEN_BIT_MESA;
736
737 return egl_g3d_create_surface(drv, dpy, conf, &arg, attribs);
738 }
739
740 static EGLBoolean
741 egl_g3d_show_screen_surface(_EGLDriver *drv, _EGLDisplay *dpy,
742 _EGLScreen *scr, _EGLSurface *surf,
743 _EGLMode *mode)
744 {
745 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
746 struct egl_g3d_screen *gscr = egl_g3d_screen(scr);
747 struct egl_g3d_surface *gsurf = egl_g3d_surface(surf);
748 struct native_surface *nsurf;
749 const struct native_mode *nmode;
750 EGLBoolean changed;
751
752 if (gsurf) {
753 EGLint idx;
754
755 if (!mode)
756 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
757 if (gsurf->base.Type != EGL_SCREEN_BIT_MESA)
758 return _eglError(EGL_BAD_SURFACE, "eglShowScreenSurfaceMESA");
759 if (gsurf->base.Width < mode->Width || gsurf->base.Height < mode->Height)
760 return _eglError(EGL_BAD_MATCH,
761 "eglShowSurfaceMESA(surface smaller than mode size)");
762
763 /* find the index of the mode */
764 for (idx = 0; idx < gscr->base.NumModes; idx++)
765 if (mode == &gscr->base.Modes[idx])
766 break;
767 if (idx >= gscr->base.NumModes) {
768 return _eglError(EGL_BAD_MODE_MESA,
769 "eglShowSurfaceMESA(unknown mode)");
770 }
771
772 nsurf = gsurf->native;
773 nmode = gscr->native_modes[idx];
774 }
775 else {
776 if (mode)
777 return _eglError(EGL_BAD_MATCH, "eglShowSurfaceMESA");
778
779 /* disable the screen */
780 nsurf = NULL;
781 nmode = NULL;
782 }
783
784 /* TODO surface panning by CRTC choosing */
785 changed = gdpy->native->modeset->program(gdpy->native, 0, nsurf,
786 gscr->base.OriginX, gscr->base.OriginY, &gscr->native, 1, nmode);
787 if (changed) {
788 gscr->base.CurrentSurface = &gsurf->base;
789 gscr->base.CurrentMode = mode;
790 }
791
792 return changed;
793 }
794
795 #endif /* EGL_MESA_screen_surface */
796
797 /**
798 * Find a config that supports the pixmap.
799 */
800 _EGLConfig *
801 egl_g3d_find_pixmap_config(_EGLDisplay *dpy, EGLNativePixmapType pix)
802 {
803 struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
804 struct egl_g3d_config *gconf;
805 EGLint i;
806
807 for (i = 0; i < dpy->Configs->Size; i++) {
808 gconf = egl_g3d_config((_EGLConfig *) dpy->Configs->Elements[i]);
809 if (gdpy->native->is_pixmap_supported(gdpy->native, pix, gconf->native))
810 break;
811 }
812
813 return (i < dpy->Configs->Size) ? &gconf->base : NULL;
814 }
815
816 void
817 egl_g3d_init_driver_api(_EGLDriver *drv)
818 {
819 _eglInitDriverFallbacks(drv);
820
821 drv->API.CreateContext = egl_g3d_create_context;
822 drv->API.DestroyContext = egl_g3d_destroy_context;
823 drv->API.CreateWindowSurface = egl_g3d_create_window_surface;
824 drv->API.CreatePixmapSurface = egl_g3d_create_pixmap_surface;
825 drv->API.CreatePbufferSurface = egl_g3d_create_pbuffer_surface;
826 drv->API.CreatePbufferFromClientBuffer = egl_g3d_create_pbuffer_from_client_buffer;
827 drv->API.DestroySurface = egl_g3d_destroy_surface;
828 drv->API.MakeCurrent = egl_g3d_make_current;
829 drv->API.SwapBuffers = egl_g3d_swap_buffers;
830 drv->API.CopyBuffers = egl_g3d_copy_buffers;
831 drv->API.WaitClient = egl_g3d_wait_client;
832 drv->API.WaitNative = egl_g3d_wait_native;
833
834 drv->API.BindTexImage = egl_g3d_bind_tex_image;
835 drv->API.ReleaseTexImage = egl_g3d_release_tex_image;
836
837 drv->API.CreateImageKHR = egl_g3d_create_image;
838 drv->API.DestroyImageKHR = egl_g3d_destroy_image;
839 #ifdef EGL_MESA_drm_image
840 drv->API.CreateDRMImageMESA = egl_g3d_create_drm_image;
841 drv->API.ExportDRMImageMESA = egl_g3d_export_drm_image;
842 #endif
843
844 #ifdef EGL_KHR_reusable_sync
845 drv->API.CreateSyncKHR = egl_g3d_create_sync;
846 drv->API.DestroySyncKHR = egl_g3d_destroy_sync;
847 drv->API.ClientWaitSyncKHR = egl_g3d_client_wait_sync;
848 drv->API.SignalSyncKHR = egl_g3d_signal_sync;
849 #endif
850
851 #ifdef EGL_MESA_screen_surface
852 drv->API.CreateScreenSurfaceMESA = egl_g3d_create_screen_surface;
853 drv->API.ShowScreenSurfaceMESA = egl_g3d_show_screen_surface;
854 #endif
855 }