2 * Mesa 3-D graphics library
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
26 #include "egldriver.h"
27 #include "eglcurrent.h"
30 #include "pipe/p_screen.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_box.h"
36 #include "egl_g3d_api.h"
37 #include "egl_g3d_image.h"
38 #include "egl_g3d_sync.h"
39 #include "egl_g3d_st.h"
40 #include "egl_g3d_loader.h"
44 * Return the state tracker for the given context.
46 static struct st_api
*
47 egl_g3d_choose_st(_EGLDriver
*drv
, _EGLContext
*ctx
,
48 enum st_profile_type
*profile
)
50 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
54 *profile
= ST_PROFILE_DEFAULT
;
56 switch (ctx
->ClientAPI
) {
57 case EGL_OPENGL_ES_API
:
58 switch (ctx
->ClientVersion
) {
61 *profile
= ST_PROFILE_OPENGL_ES1
;
65 *profile
= ST_PROFILE_OPENGL_ES2
;
68 _eglLog(_EGL_WARNING
, "unknown client version %d",
80 _eglLog(_EGL_WARNING
, "unknown client API 0x%04x", ctx
->ClientAPI
);
86 stapi
= gdrv
->loader
->guess_gl_api(*profile
);
89 stapi
= gdrv
->loader
->get_st_api(api
);
95 if (stapi
&& !(stapi
->profile_mask
& (1 << *profile
)))
102 egl_g3d_compare_config(const _EGLConfig
*conf1
, const _EGLConfig
*conf2
,
105 const _EGLConfig
*criteria
= (const _EGLConfig
*) priv_data
;
107 /* EGL_NATIVE_VISUAL_TYPE ignored? */
108 return _eglCompareConfigs(conf1
, conf2
, criteria
, EGL_TRUE
);
112 egl_g3d_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
114 if (!_eglMatchConfig(conf
, criteria
))
117 if (criteria
->MatchNativePixmap
!= EGL_NONE
&&
118 criteria
->MatchNativePixmap
!= EGL_DONT_CARE
) {
119 struct egl_g3d_display
*gdpy
= egl_g3d_display(conf
->Display
);
120 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
121 EGLNativePixmapType pix
=
122 (EGLNativePixmapType
) criteria
->MatchNativePixmap
;
124 if (!gdpy
->native
->is_pixmap_supported(gdpy
->native
, pix
, gconf
->native
))
132 egl_g3d_choose_config(_EGLDriver
*drv
, _EGLDisplay
*dpy
, const EGLint
*attribs
,
133 EGLConfig
*configs
, EGLint size
, EGLint
*num_configs
)
135 _EGLConfig
**tmp_configs
, criteria
;
139 return _eglError(EGL_BAD_PARAMETER
, "eglChooseConfigs");
141 if (!_eglParseConfigAttribList(&criteria
, dpy
, attribs
))
142 return _eglError(EGL_BAD_ATTRIBUTE
, "eglChooseConfig");
144 /* get the number of matched configs */
145 tmp_size
= _eglFilterArray(dpy
->Configs
, NULL
, 0,
146 (_EGLArrayForEach
) egl_g3d_match_config
, (void *) &criteria
);
148 *num_configs
= tmp_size
;
152 tmp_configs
= MALLOC(sizeof(tmp_configs
[0]) * tmp_size
);
154 return _eglError(EGL_BAD_ALLOC
, "eglChooseConfig(out of memory)");
156 /* get the matched configs */
157 _eglFilterArray(dpy
->Configs
, (void **) tmp_configs
, tmp_size
,
158 (_EGLArrayForEach
) egl_g3d_match_config
, (void *) &criteria
);
160 /* perform sorting of configs */
161 if (tmp_configs
&& tmp_size
) {
162 _eglSortConfigs((const _EGLConfig
**) tmp_configs
, tmp_size
,
163 egl_g3d_compare_config
, (void *) &criteria
);
164 size
= MIN2(tmp_size
, size
);
165 for (i
= 0; i
< size
; i
++)
166 configs
[i
] = _eglGetConfigHandle(tmp_configs
[i
]);
177 egl_g3d_create_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
178 _EGLContext
*share
, const EGLint
*attribs
)
180 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
181 struct egl_g3d_context
*gshare
= egl_g3d_context(share
);
182 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
183 struct egl_g3d_context
*gctx
;
184 struct st_context_attribs stattribs
;
186 gctx
= CALLOC_STRUCT(egl_g3d_context
);
188 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
192 if (!_eglInitContext(&gctx
->base
, dpy
, conf
, attribs
)) {
197 memset(&stattribs
, 0, sizeof(stattribs
));
199 stattribs
.visual
= gconf
->stvis
;
201 gctx
->stapi
= egl_g3d_choose_st(drv
, &gctx
->base
, &stattribs
.profile
);
207 gctx
->stctxi
= gctx
->stapi
->create_context(gctx
->stapi
, gdpy
->smapi
,
208 &stattribs
, (gshare
) ? gshare
->stctxi
: NULL
);
214 gctx
->stctxi
->st_manager_private
= (void *) &gctx
->base
;
223 destroy_context(_EGLDisplay
*dpy
, _EGLContext
*ctx
)
225 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
227 /* FIXME a context might live longer than its display */
228 if (!dpy
->Initialized
)
229 _eglLog(_EGL_FATAL
, "destroy a context with an unitialized display");
231 gctx
->stctxi
->destroy(gctx
->stctxi
);
237 egl_g3d_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
239 if (_eglPutContext(ctx
))
240 destroy_context(dpy
, ctx
);
244 struct egl_g3d_create_surface_arg
{
247 EGLNativeWindowType win
;
248 EGLNativePixmapType pix
;
253 egl_g3d_create_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
254 struct egl_g3d_create_surface_arg
*arg
,
255 const EGLint
*attribs
)
257 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
258 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
259 struct egl_g3d_surface
*gsurf
;
260 struct native_surface
*nsurf
;
265 err
= "eglCreateWindowSurface";
268 err
= "eglCreatePixmapSurface";
270 #ifdef EGL_MESA_screen_surface
271 case EGL_SCREEN_BIT_MESA
:
272 err
= "eglCreateScreenSurface";
276 err
= "eglCreateUnknownSurface";
280 gsurf
= CALLOC_STRUCT(egl_g3d_surface
);
282 _eglError(EGL_BAD_ALLOC
, err
);
286 if (!_eglInitSurface(&gsurf
->base
, dpy
, arg
->type
, conf
, attribs
)) {
291 /* create the native surface */
294 nsurf
= gdpy
->native
->create_window_surface(gdpy
->native
,
295 arg
->u
.win
, gconf
->native
);
298 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
299 arg
->u
.pix
, gconf
->native
);
301 #ifdef EGL_MESA_screen_surface
302 case EGL_SCREEN_BIT_MESA
:
303 /* prefer back buffer (move to _eglInitSurface?) */
304 gsurf
->base
.RenderBuffer
= EGL_BACK_BUFFER
;
305 nsurf
= gdpy
->native
->modeset
->create_scanout_surface(gdpy
->native
,
306 gconf
->native
, gsurf
->base
.Width
, gsurf
->base
.Height
);
318 /* initialize the geometry */
319 if (!nsurf
->validate(nsurf
, 0x0, &gsurf
->sequence_number
, NULL
,
320 &gsurf
->base
.Width
, &gsurf
->base
.Height
)) {
321 nsurf
->destroy(nsurf
);
326 gsurf
->stvis
= gconf
->stvis
;
327 if (gsurf
->base
.RenderBuffer
== EGL_SINGLE_BUFFER
)
328 gsurf
->stvis
.render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
330 gsurf
->stfbi
= egl_g3d_create_st_framebuffer(&gsurf
->base
);
332 nsurf
->destroy(nsurf
);
337 nsurf
->user_data
= &gsurf
->base
;
338 gsurf
->native
= nsurf
;
344 egl_g3d_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
345 _EGLConfig
*conf
, EGLNativeWindowType win
,
346 const EGLint
*attribs
)
348 struct egl_g3d_create_surface_arg arg
;
350 memset(&arg
, 0, sizeof(arg
));
351 arg
.type
= EGL_WINDOW_BIT
;
354 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
358 egl_g3d_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
359 _EGLConfig
*conf
, EGLNativePixmapType pix
,
360 const EGLint
*attribs
)
362 struct egl_g3d_create_surface_arg arg
;
364 memset(&arg
, 0, sizeof(arg
));
365 arg
.type
= EGL_PIXMAP_BIT
;
368 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
371 static struct egl_g3d_surface
*
372 create_pbuffer_surface(_EGLDisplay
*dpy
, _EGLConfig
*conf
,
373 const EGLint
*attribs
, const char *func
)
375 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
376 struct egl_g3d_surface
*gsurf
;
378 gsurf
= CALLOC_STRUCT(egl_g3d_surface
);
380 _eglError(EGL_BAD_ALLOC
, func
);
384 if (!_eglInitSurface(&gsurf
->base
, dpy
, EGL_PBUFFER_BIT
, conf
, attribs
)) {
389 gsurf
->stvis
= gconf
->stvis
;
391 gsurf
->stfbi
= egl_g3d_create_st_framebuffer(&gsurf
->base
);
401 egl_g3d_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
402 _EGLConfig
*conf
, const EGLint
*attribs
)
404 struct egl_g3d_surface
*gsurf
;
405 struct pipe_resource
*ptex
= NULL
;
407 gsurf
= create_pbuffer_surface(dpy
, conf
, attribs
,
408 "eglCreatePbufferSurface");
412 gsurf
->client_buffer_type
= EGL_NONE
;
414 if (!gsurf
->stfbi
->validate(gsurf
->stfbi
,
415 &gsurf
->stvis
.render_buffer
, 1, &ptex
)) {
416 egl_g3d_destroy_st_framebuffer(gsurf
->stfbi
);
425 egl_g3d_create_pbuffer_from_client_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
427 EGLClientBuffer buffer
,
429 const EGLint
*attribs
)
431 struct egl_g3d_surface
*gsurf
;
432 struct pipe_resource
*ptex
= NULL
;
433 EGLint pbuffer_attribs
[32];
437 case EGL_OPENVG_IMAGE
:
440 _eglError(EGL_BAD_PARAMETER
, "eglCreatePbufferFromClientBuffer");
445 /* parse the attributes first */
447 for (i
= 0; attribs
&& attribs
[i
] != EGL_NONE
; i
++) {
448 EGLint attr
= attribs
[i
++];
449 EGLint val
= attribs
[i
];
450 EGLint err
= EGL_SUCCESS
;
453 case EGL_TEXTURE_FORMAT
:
454 case EGL_TEXTURE_TARGET
:
455 case EGL_MIPMAP_TEXTURE
:
456 pbuffer_attribs
[count
++] = attr
;
457 pbuffer_attribs
[count
++] = val
;
460 err
= EGL_BAD_ATTRIBUTE
;
464 if (err
!= EGL_SUCCESS
) {
465 _eglError(err
, "eglCreatePbufferFromClientBuffer");
470 pbuffer_attribs
[count
++] = EGL_NONE
;
472 gsurf
= create_pbuffer_surface(dpy
, conf
, pbuffer_attribs
,
473 "eglCreatePbufferFromClientBuffer");
477 gsurf
->client_buffer_type
= buftype
;
478 gsurf
->client_buffer
= buffer
;
480 if (!gsurf
->stfbi
->validate(gsurf
->stfbi
,
481 &gsurf
->stvis
.render_buffer
, 1, &ptex
)) {
482 egl_g3d_destroy_st_framebuffer(gsurf
->stfbi
);
494 destroy_surface(_EGLDisplay
*dpy
, _EGLSurface
*surf
)
496 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
498 /* FIXME a surface might live longer than its display */
499 if (!dpy
->Initialized
)
500 _eglLog(_EGL_FATAL
, "destroy a surface with an unitialized display");
502 pipe_resource_reference(&gsurf
->render_texture
, NULL
);
503 egl_g3d_destroy_st_framebuffer(gsurf
->stfbi
);
505 gsurf
->native
->destroy(gsurf
->native
);
510 egl_g3d_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
512 if (_eglPutSurface(surf
))
513 destroy_surface(dpy
, surf
);
518 egl_g3d_make_current(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
519 _EGLSurface
*draw
, _EGLSurface
*read
, _EGLContext
*ctx
)
521 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
522 struct egl_g3d_surface
*gdraw
= egl_g3d_surface(draw
);
523 struct egl_g3d_surface
*gread
= egl_g3d_surface(read
);
524 struct egl_g3d_context
*old_gctx
;
525 _EGLContext
*old_ctx
;
526 _EGLSurface
*old_draw
, *old_read
;
527 EGLBoolean ok
= EGL_TRUE
;
529 /* make new bindings */
530 if (!_eglBindContext(ctx
, draw
, read
, &old_ctx
, &old_draw
, &old_read
))
533 old_gctx
= egl_g3d_context(old_ctx
);
535 /* flush old context */
536 old_gctx
->stctxi
->flush(old_gctx
->stctxi
,
537 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
541 ok
= gctx
->stapi
->make_current(gctx
->stapi
, gctx
->stctxi
,
542 (gdraw
) ? gdraw
->stfbi
: NULL
, (gread
) ? gread
->stfbi
: NULL
);
545 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
,
548 if (gdraw
->base
.Type
== EGL_WINDOW_BIT
) {
549 gctx
->base
.WindowRenderBuffer
=
550 (gdraw
->stvis
.render_buffer
== ST_ATTACHMENT_FRONT_LEFT
) ?
551 EGL_SINGLE_BUFFER
: EGL_BACK_BUFFER
;
554 if (gread
&& gread
!= gdraw
) {
555 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
,
561 ok
= old_gctx
->stapi
->make_current(old_gctx
->stapi
, NULL
, NULL
, NULL
);
563 old_gctx
->base
.WindowRenderBuffer
= EGL_NONE
;
567 if (_eglPutContext(old_ctx
))
568 destroy_context(dpy
, old_ctx
);
569 if (_eglPutSurface(old_draw
))
570 destroy_surface(dpy
, old_draw
);
571 if (_eglPutSurface(old_read
))
572 destroy_surface(dpy
, old_read
);
575 /* undo the previous _eglBindContext */
576 _eglBindContext(old_ctx
, old_draw
, old_read
, &ctx
, &draw
, &read
);
577 assert(&gctx
->base
== ctx
&&
578 &gdraw
->base
== draw
&&
579 &gread
->base
== read
);
581 _eglPutSurface(draw
);
582 _eglPutSurface(read
);
585 _eglPutSurface(old_draw
);
586 _eglPutSurface(old_read
);
587 _eglPutContext(old_ctx
);
594 egl_g3d_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
596 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
597 _EGLContext
*ctx
= _eglGetCurrentContext();
598 struct egl_g3d_context
*gctx
= NULL
;
600 /* no-op for pixmap or pbuffer surface */
601 if (gsurf
->base
.Type
== EGL_PIXMAP_BIT
||
602 gsurf
->base
.Type
== EGL_PBUFFER_BIT
)
605 /* or when the surface is single-buffered */
606 if (gsurf
->stvis
.render_buffer
== ST_ATTACHMENT_FRONT_LEFT
)
609 if (ctx
&& ctx
->DrawSurface
== surf
)
610 gctx
= egl_g3d_context(ctx
);
612 /* flush if the surface is current */
614 gctx
->stctxi
->flush(gctx
->stctxi
,
615 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
618 return gsurf
->native
->present(gsurf
->native
,
619 NATIVE_ATTACHMENT_BACK_LEFT
,
620 gsurf
->base
.SwapBehavior
== EGL_BUFFER_PRESERVED
,
621 gsurf
->base
.SwapInterval
);
625 * Get the pipe surface of the given attachment of the native surface.
627 static struct pipe_resource
*
628 get_pipe_resource(struct native_display
*ndpy
, struct native_surface
*nsurf
,
629 enum native_attachment natt
)
631 struct pipe_resource
*textures
[NUM_NATIVE_ATTACHMENTS
];
633 textures
[natt
] = NULL
;
634 nsurf
->validate(nsurf
, 1 << natt
, NULL
, textures
, NULL
, NULL
);
636 return textures
[natt
];
640 egl_g3d_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
641 EGLNativePixmapType target
)
643 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
644 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
645 _EGLContext
*ctx
= _eglGetCurrentContext();
646 struct egl_g3d_config
*gconf
;
647 struct native_surface
*nsurf
;
648 struct pipe_resource
*ptex
;
650 if (!gsurf
->render_texture
)
653 gconf
= egl_g3d_config(egl_g3d_find_pixmap_config(dpy
, target
));
655 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
657 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
658 target
, gconf
->native
);
660 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
662 /* flush if the surface is current */
663 if (ctx
&& ctx
->DrawSurface
== &gsurf
->base
) {
664 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
665 gctx
->stctxi
->flush(gctx
->stctxi
,
666 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
669 /* create a pipe context to copy surfaces */
672 gdpy
->native
->screen
->context_create(gdpy
->native
->screen
, NULL
);
677 ptex
= get_pipe_resource(gdpy
->native
, nsurf
, NATIVE_ATTACHMENT_FRONT_LEFT
);
679 struct pipe_resource
*psrc
= gsurf
->render_texture
;
680 struct pipe_box src_box
;
681 u_box_origin_2d(ptex
->width0
, ptex
->height0
, &src_box
);
683 gdpy
->pipe
->resource_copy_region(gdpy
->pipe
, ptex
, 0, 0, 0, 0,
684 gsurf
->render_texture
, 0, &src_box
);
685 nsurf
->present(nsurf
, NATIVE_ATTACHMENT_FRONT_LEFT
, FALSE
, 0);
688 pipe_resource_reference(&ptex
, NULL
);
691 nsurf
->destroy(nsurf
);
697 egl_g3d_wait_client(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
699 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
700 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
701 struct pipe_screen
*screen
= gdpy
->native
->screen
;
702 struct pipe_fence_handle
*fence
= NULL
;
704 gctx
->stctxi
->flush(gctx
->stctxi
,
705 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
707 screen
->fence_finish(screen
, fence
, 0);
708 screen
->fence_reference(screen
, &fence
, NULL
);
715 egl_g3d_wait_native(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint engine
)
717 _EGLContext
*ctx
= _eglGetCurrentContext();
719 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
720 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
722 if (ctx
&& ctx
->DrawSurface
) {
723 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(ctx
->DrawSurface
);
726 gsurf
->native
->wait(gsurf
->native
);
733 egl_g3d_bind_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
734 _EGLSurface
*surf
, EGLint buffer
)
736 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
737 _EGLContext
*es1
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
738 struct egl_g3d_context
*gctx
;
739 enum pipe_format internal_format
;
740 enum st_texture_type target
;
742 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
)
743 return _eglError(EGL_BAD_SURFACE
, "eglBindTexImage");
744 if (buffer
!= EGL_BACK_BUFFER
)
745 return _eglError(EGL_BAD_PARAMETER
, "eglBindTexImage");
746 if (gsurf
->base
.BoundToTexture
)
747 return _eglError(EGL_BAD_ACCESS
, "eglBindTexImage");
749 switch (gsurf
->base
.TextureFormat
) {
750 case EGL_TEXTURE_RGB
:
751 internal_format
= PIPE_FORMAT_R8G8B8_UNORM
;
753 case EGL_TEXTURE_RGBA
:
754 internal_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
757 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
760 switch (gsurf
->base
.TextureTarget
) {
762 target
= ST_TEXTURE_2D
;
765 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
770 if (!gsurf
->render_texture
)
773 /* flush properly if the surface is bound */
774 if (gsurf
->base
.CurrentContext
) {
775 gctx
= egl_g3d_context(gsurf
->base
.CurrentContext
);
776 gctx
->stctxi
->flush(gctx
->stctxi
,
777 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
780 gctx
= egl_g3d_context(es1
);
781 if (gctx
->stctxi
->teximage
) {
782 if (!gctx
->stctxi
->teximage(gctx
->stctxi
, target
,
783 gsurf
->base
.MipmapLevel
, internal_format
,
784 gsurf
->render_texture
, gsurf
->base
.MipmapTexture
))
786 gsurf
->base
.BoundToTexture
= EGL_TRUE
;
793 egl_g3d_release_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
794 _EGLSurface
*surf
, EGLint buffer
)
796 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
798 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
||
799 !gsurf
->base
.BoundToTexture
)
800 return _eglError(EGL_BAD_SURFACE
, "eglReleaseTexImage");
801 if (buffer
!= EGL_BACK_BUFFER
)
802 return _eglError(EGL_BAD_PARAMETER
, "eglReleaseTexImage");
804 if (gsurf
->render_texture
) {
805 _EGLContext
*ctx
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
806 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
808 /* what if the context the surface binds to is no longer current? */
810 gctx
->stctxi
->teximage(gctx
->stctxi
, ST_TEXTURE_2D
,
811 gsurf
->base
.MipmapLevel
, PIPE_FORMAT_NONE
, NULL
, FALSE
);
815 gsurf
->base
.BoundToTexture
= EGL_FALSE
;
820 #ifdef EGL_MESA_screen_surface
823 egl_g3d_create_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
824 _EGLConfig
*conf
, const EGLint
*attribs
)
826 struct egl_g3d_create_surface_arg arg
;
828 memset(&arg
, 0, sizeof(arg
));
829 arg
.type
= EGL_SCREEN_BIT_MESA
;
831 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
835 egl_g3d_show_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
836 _EGLScreen
*scr
, _EGLSurface
*surf
,
839 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
840 struct egl_g3d_screen
*gscr
= egl_g3d_screen(scr
);
841 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
842 struct native_surface
*nsurf
;
843 const struct native_mode
*nmode
;
850 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
851 if (gsurf
->base
.Type
!= EGL_SCREEN_BIT_MESA
)
852 return _eglError(EGL_BAD_SURFACE
, "eglShowScreenSurfaceMESA");
853 if (gsurf
->base
.Width
< mode
->Width
|| gsurf
->base
.Height
< mode
->Height
)
854 return _eglError(EGL_BAD_MATCH
,
855 "eglShowSurfaceMESA(surface smaller than mode size)");
857 /* find the index of the mode */
858 for (idx
= 0; idx
< gscr
->base
.NumModes
; idx
++)
859 if (mode
== &gscr
->base
.Modes
[idx
])
861 if (idx
>= gscr
->base
.NumModes
) {
862 return _eglError(EGL_BAD_MODE_MESA
,
863 "eglShowSurfaceMESA(unknown mode)");
866 nsurf
= gsurf
->native
;
867 nmode
= gscr
->native_modes
[idx
];
871 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
873 /* disable the screen */
878 /* TODO surface panning by CRTC choosing */
879 changed
= gdpy
->native
->modeset
->program(gdpy
->native
, 0, nsurf
,
880 gscr
->base
.OriginX
, gscr
->base
.OriginY
, &gscr
->native
, 1, nmode
);
882 gscr
->base
.CurrentSurface
= &gsurf
->base
;
883 gscr
->base
.CurrentMode
= mode
;
889 #endif /* EGL_MESA_screen_surface */
892 * Find a config that supports the pixmap.
895 egl_g3d_find_pixmap_config(_EGLDisplay
*dpy
, EGLNativePixmapType pix
)
897 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
898 struct egl_g3d_config
*gconf
;
901 for (i
= 0; i
< dpy
->Configs
->Size
; i
++) {
902 gconf
= egl_g3d_config((_EGLConfig
*) dpy
->Configs
->Elements
[i
]);
903 if (gdpy
->native
->is_pixmap_supported(gdpy
->native
, pix
, gconf
->native
))
907 return (i
< dpy
->Configs
->Size
) ? &gconf
->base
: NULL
;
911 egl_g3d_init_driver_api(_EGLDriver
*drv
)
913 _eglInitDriverFallbacks(drv
);
915 drv
->API
.ChooseConfig
= egl_g3d_choose_config
;
917 drv
->API
.CreateContext
= egl_g3d_create_context
;
918 drv
->API
.DestroyContext
= egl_g3d_destroy_context
;
919 drv
->API
.CreateWindowSurface
= egl_g3d_create_window_surface
;
920 drv
->API
.CreatePixmapSurface
= egl_g3d_create_pixmap_surface
;
921 drv
->API
.CreatePbufferSurface
= egl_g3d_create_pbuffer_surface
;
922 drv
->API
.CreatePbufferFromClientBuffer
= egl_g3d_create_pbuffer_from_client_buffer
;
923 drv
->API
.DestroySurface
= egl_g3d_destroy_surface
;
924 drv
->API
.MakeCurrent
= egl_g3d_make_current
;
925 drv
->API
.SwapBuffers
= egl_g3d_swap_buffers
;
926 drv
->API
.CopyBuffers
= egl_g3d_copy_buffers
;
927 drv
->API
.WaitClient
= egl_g3d_wait_client
;
928 drv
->API
.WaitNative
= egl_g3d_wait_native
;
930 drv
->API
.BindTexImage
= egl_g3d_bind_tex_image
;
931 drv
->API
.ReleaseTexImage
= egl_g3d_release_tex_image
;
933 drv
->API
.CreateImageKHR
= egl_g3d_create_image
;
934 drv
->API
.DestroyImageKHR
= egl_g3d_destroy_image
;
935 #ifdef EGL_MESA_drm_image
936 drv
->API
.CreateDRMImageMESA
= egl_g3d_create_drm_image
;
937 drv
->API
.ExportDRMImageMESA
= egl_g3d_export_drm_image
;
940 #ifdef EGL_KHR_reusable_sync
941 drv
->API
.CreateSyncKHR
= egl_g3d_create_sync
;
942 drv
->API
.DestroySyncKHR
= egl_g3d_destroy_sync
;
943 drv
->API
.ClientWaitSyncKHR
= egl_g3d_client_wait_sync
;
944 drv
->API
.SignalSyncKHR
= egl_g3d_signal_sync
;
947 #ifdef EGL_MESA_screen_surface
948 drv
->API
.CreateScreenSurfaceMESA
= egl_g3d_create_screen_surface
;
949 drv
->API
.ShowScreenSurfaceMESA
= egl_g3d_show_screen_surface
;