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
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "pipe/p_screen.h"
29 #include "util/u_memory.h"
30 #include "util/u_rect.h"
31 #include "util/u_inlines.h"
32 #include "egldriver.h"
33 #include "eglcurrent.h"
34 #include "eglconfigutil.h"
39 #include "egl_g3d_image.h"
43 * Validate the draw/read surfaces of the context.
46 egl_g3d_validate_context(_EGLDisplay
*dpy
, _EGLContext
*ctx
)
48 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
49 struct pipe_screen
*screen
= gdpy
->native
->screen
;
50 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
51 const uint st_att_map
[NUM_NATIVE_ATTACHMENTS
] = {
52 ST_SURFACE_FRONT_LEFT
,
54 ST_SURFACE_FRONT_RIGHT
,
55 ST_SURFACE_BACK_RIGHT
,
57 EGLint num_surfaces
, s
;
59 /* validate draw and/or read buffers */
60 num_surfaces
= (gctx
->base
.ReadSurface
== gctx
->base
.DrawSurface
) ? 1 : 2;
61 for (s
= 0; s
< num_surfaces
; s
++) {
62 struct pipe_texture
*textures
[NUM_NATIVE_ATTACHMENTS
];
63 struct egl_g3d_surface
*gsurf
;
64 struct egl_g3d_buffer
*gbuf
;
68 gsurf
= egl_g3d_surface(gctx
->base
.DrawSurface
);
72 gsurf
= egl_g3d_surface(gctx
->base
.ReadSurface
);
76 if (!gctx
->force_validate
) {
79 gsurf
->native
->validate(gsurf
->native
, gbuf
->attachment_mask
,
80 &seq_num
, NULL
, NULL
, NULL
);
82 if (gsurf
->sequence_number
== seq_num
)
86 pipe_surface_reference(&gsurf
->render_surface
, NULL
);
87 memset(textures
, 0, sizeof(textures
));
89 gsurf
->native
->validate(gsurf
->native
, gbuf
->attachment_mask
,
90 &gsurf
->sequence_number
, textures
,
91 &gsurf
->base
.Width
, &gsurf
->base
.Height
);
92 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
93 struct pipe_texture
*pt
= textures
[att
];
94 struct pipe_surface
*ps
;
96 if (native_attachment_mask_test(gbuf
->attachment_mask
, att
) && pt
) {
97 ps
= screen
->get_tex_surface(screen
, pt
, 0, 0, 0,
98 PIPE_BUFFER_USAGE_GPU_READ
|
99 PIPE_BUFFER_USAGE_GPU_WRITE
);
100 gctx
->stapi
->st_set_framebuffer_surface(gbuf
->st_fb
,
101 st_att_map
[att
], ps
);
103 if (gsurf
->render_att
== att
)
104 pipe_surface_reference(&gsurf
->render_surface
, ps
);
106 pipe_surface_reference(&ps
, NULL
);
107 pipe_texture_reference(&pt
, NULL
);
111 gctx
->stapi
->st_resize_framebuffer(gbuf
->st_fb
,
112 gsurf
->base
.Width
, gsurf
->base
.Height
);
115 gctx
->force_validate
= EGL_FALSE
;
120 * Create a st_framebuffer.
122 static struct st_framebuffer
*
123 create_framebuffer(_EGLContext
*ctx
, _EGLSurface
*surf
)
125 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
126 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
127 struct egl_g3d_config
*gconf
= egl_g3d_config(gsurf
->base
.Config
);
129 return gctx
->stapi
->st_create_framebuffer(&gconf
->native
->mode
,
130 gconf
->native
->color_format
, gconf
->native
->depth_format
,
131 gconf
->native
->stencil_format
,
132 gsurf
->base
.Width
, gsurf
->base
.Height
, &gsurf
->base
);
136 * Update the attachments of draw/read surfaces.
139 egl_g3d_route_context(_EGLDisplay
*dpy
, _EGLContext
*ctx
)
141 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
144 /* route draw and read buffers' attachments */
145 for (s
= 0; s
< 2; s
++) {
146 struct egl_g3d_surface
*gsurf
;
147 struct egl_g3d_buffer
*gbuf
;
150 gsurf
= egl_g3d_surface(gctx
->base
.DrawSurface
);
154 gsurf
= egl_g3d_surface(gctx
->base
.ReadSurface
);
158 gbuf
->attachment_mask
= (1 << gsurf
->render_att
);
160 /* FIXME OpenGL defaults to draw the front or back buffer when the
161 * context is single-buffered or double-buffered respectively. In EGL,
162 * however, the buffer to be drawn is determined by the surface, instead
163 * of the context. As a result, rendering to a pixmap surface with a
164 * double-buffered context does not work as expected.
166 * gctx->stapi->st_draw_front_buffer(gctx->st_ctx, natt ==
167 * NATIVE_ATTACHMENT_FRONT_LEFT);
171 * FIXME If the back buffer is asked for here, and the front buffer is
172 * later needed by the client API (e.g. glDrawBuffer is called to draw
173 * the front buffer), it will create a new pipe texture and draw there.
174 * One fix is to ask for both buffers here, but it would be a waste if
175 * the front buffer is never used. A better fix is to add a callback to
176 * the pipe screen with context private (just like flush_frontbuffer).
182 * Reallocate the context's framebuffers after draw/read surfaces change.
185 egl_g3d_realloc_context(_EGLDisplay
*dpy
, _EGLContext
*ctx
)
187 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
188 struct egl_g3d_surface
*gdraw
= egl_g3d_surface(gctx
->base
.DrawSurface
);
189 struct egl_g3d_surface
*gread
= egl_g3d_surface(gctx
->base
.ReadSurface
);
191 /* unreference the old framebuffers */
192 if (gctx
->draw
.st_fb
) {
193 EGLBoolean is_equal
= (gctx
->draw
.st_fb
== gctx
->read
.st_fb
);
196 priv
= gctx
->stapi
->st_framebuffer_private(gctx
->draw
.st_fb
);
197 if (!gdraw
|| priv
!= (void *) &gdraw
->base
) {
198 gctx
->stapi
->st_unreference_framebuffer(gctx
->draw
.st_fb
);
199 gctx
->draw
.st_fb
= NULL
;
200 gctx
->draw
.attachment_mask
= 0x0;
204 gctx
->read
.st_fb
= NULL
;
205 gctx
->draw
.attachment_mask
= 0x0;
208 priv
= gctx
->stapi
->st_framebuffer_private(gctx
->read
.st_fb
);
209 if (!gread
|| priv
!= (void *) &gread
->base
) {
210 gctx
->stapi
->st_unreference_framebuffer(gctx
->read
.st_fb
);
211 gctx
->read
.st_fb
= NULL
;
212 gctx
->draw
.attachment_mask
= 0x0;
220 /* create the draw fb */
221 if (!gctx
->draw
.st_fb
) {
222 gctx
->draw
.st_fb
= create_framebuffer(&gctx
->base
, &gdraw
->base
);
223 if (!gctx
->draw
.st_fb
)
227 /* create the read fb */
228 if (!gctx
->read
.st_fb
) {
229 if (gread
!= gdraw
) {
230 gctx
->read
.st_fb
= create_framebuffer(&gctx
->base
, &gread
->base
);
231 if (!gctx
->read
.st_fb
) {
232 gctx
->stapi
->st_unreference_framebuffer(gctx
->draw
.st_fb
);
233 gctx
->draw
.st_fb
= NULL
;
238 /* there is no st_reference_framebuffer... */
239 gctx
->read
.st_fb
= gctx
->draw
.st_fb
;
243 egl_g3d_route_context(dpy
, &gctx
->base
);
244 gctx
->force_validate
= EGL_TRUE
;
250 * Return the state tracker for the given context.
252 static const struct egl_g3d_st
*
253 egl_g3d_choose_st(_EGLDriver
*drv
, _EGLContext
*ctx
)
255 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
256 const struct egl_g3d_st
*stapi
;
259 switch (ctx
->ClientAPI
) {
260 case EGL_OPENGL_ES_API
:
261 switch (ctx
->ClientVersion
) {
263 idx
= EGL_G3D_ST_OPENGL_ES
;
266 idx
= EGL_G3D_ST_OPENGL_ES2
;
269 _eglLog(_EGL_WARNING
, "unknown client version %d",
275 idx
= EGL_G3D_ST_OPENVG
;
278 idx
= EGL_G3D_ST_OPENGL
;
281 _eglLog(_EGL_WARNING
, "unknown client API 0x%04x", ctx
->ClientAPI
);
285 stapi
= (idx
>= 0) ? gdrv
->stapis
[idx
] : NULL
;
290 * Initialize the state trackers.
293 egl_g3d_init_st(_EGLDriver
*drv
)
295 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
298 /* already initialized */
302 for (i
= 0; i
< NUM_EGL_G3D_STS
; i
++) {
303 gdrv
->stapis
[i
] = egl_g3d_get_st(i
);
305 gdrv
->api_mask
|= gdrv
->stapis
[i
]->api_bit
;
309 _eglLog(_EGL_DEBUG
, "Driver API mask: 0x%x", gdrv
->api_mask
);
311 _eglLog(_EGL_WARNING
, "No supported client API");
315 * Get the probe object of the display.
317 * Note that this function may be called before the display is initialized.
319 static struct native_probe
*
320 egl_g3d_get_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
322 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
323 struct native_probe
*nprobe
;
325 nprobe
= (struct native_probe
*) _eglGetProbeCache(gdrv
->probe_key
);
326 if (!nprobe
|| nprobe
->display
!= dpy
->NativeDisplay
) {
328 nprobe
->destroy(nprobe
);
329 nprobe
= native_create_probe(dpy
->NativeDisplay
);
330 _eglSetProbeCache(gdrv
->probe_key
, (void *) nprobe
);
337 * Destroy the probe object of the display. The display may be NULL.
339 * Note that this function may be called before the display is initialized.
342 egl_g3d_destroy_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
344 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
345 struct native_probe
*nprobe
;
347 nprobe
= (struct native_probe
*) _eglGetProbeCache(gdrv
->probe_key
);
348 if (nprobe
&& (!dpy
|| nprobe
->display
== dpy
->NativeDisplay
)) {
349 nprobe
->destroy(nprobe
);
350 _eglSetProbeCache(gdrv
->probe_key
, NULL
);
355 * Return an API mask that consists of the state trackers that supports the
358 * FIXME add st_is_mode_supported()?
361 get_mode_api_mask(const __GLcontextModes
*mode
, EGLint api_mask
)
365 /* OpenGL ES 1.x and 2.x are checked together */
366 check
= EGL_OPENGL_ES_BIT
| EGL_OPENGL_ES2_BIT
;
367 if (api_mask
& check
) {
368 /* this is required by EGL, not by OpenGL ES */
369 if (mode
->drawableType
& GLX_WINDOW_BIT
&& !mode
->doubleBufferMode
)
373 check
= EGL_OPENVG_BIT
;
374 if (api_mask
& check
) {
375 /* vega st needs the depth/stencil rb */
376 if (!mode
->depthBits
&& !mode
->stencilBits
)
383 #ifdef EGL_MESA_screen_surface
386 egl_g3d_add_screens(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
388 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
389 const struct native_connector
**native_connectors
;
390 EGLint num_connectors
, i
;
393 gdpy
->native
->modeset
->get_connectors(gdpy
->native
, &num_connectors
, NULL
);
394 if (!num_connectors
) {
395 if (native_connectors
)
396 free(native_connectors
);
400 for (i
= 0; i
< num_connectors
; i
++) {
401 const struct native_connector
*nconn
= native_connectors
[i
];
402 struct egl_g3d_screen
*gscr
;
403 const struct native_mode
**native_modes
;
406 /* TODO support for hotplug */
408 gdpy
->native
->modeset
->get_modes(gdpy
->native
, nconn
, &num_modes
);
415 gscr
= CALLOC_STRUCT(egl_g3d_screen
);
421 _eglInitScreen(&gscr
->base
);
423 for (j
= 0; j
< num_modes
; j
++) {
424 const struct native_mode
*nmode
= native_modes
[j
];
427 mode
= _eglAddNewMode(&gscr
->base
, nmode
->width
, nmode
->height
,
428 nmode
->refresh_rate
, nmode
->desc
);
431 /* gscr->native_modes and gscr->base.Modes should be consistent */
432 assert(mode
== &gscr
->base
.Modes
[j
]);
435 gscr
->native
= nconn
;
436 gscr
->native_modes
= native_modes
;
438 _eglAddScreen(dpy
, &gscr
->base
);
441 free(native_connectors
);
444 #endif /* EGL_MESA_screen_surface */
447 * Add configs to display and return the next config ID.
450 egl_g3d_add_configs(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint id
)
452 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
453 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
454 const struct native_config
**native_configs
;
457 native_configs
= gdpy
->native
->get_configs(gdpy
->native
,
461 free(native_configs
);
465 for (i
= 0; i
< num_configs
; i
++) {
467 struct egl_g3d_config
*gconf
;
470 gconf
= CALLOC_STRUCT(egl_g3d_config
);
474 _eglInitConfig(&gconf
->base
, dpy
, id
);
476 api_mask
= get_mode_api_mask(&native_configs
[i
]->mode
, gdrv
->api_mask
);
478 _eglLog(_EGL_DEBUG
, "no state tracker supports config 0x%x",
479 native_configs
[i
]->mode
.visualID
);
482 valid
= _eglConfigFromContextModesRec(&gconf
->base
,
483 &native_configs
[i
]->mode
, api_mask
, api_mask
);
485 #ifdef EGL_MESA_screen_surface
486 /* check if scanout surface bit is set */
487 if (native_configs
[i
]->scanout_bit
) {
488 EGLint val
= GET_CONFIG_ATTRIB(&gconf
->base
, EGL_SURFACE_TYPE
);
489 val
|= EGL_SCREEN_BIT_MESA
;
490 SET_CONFIG_ATTRIB(&gconf
->base
, EGL_SURFACE_TYPE
, val
);
493 valid
= _eglValidateConfig(&gconf
->base
, EGL_FALSE
);
496 _eglLog(_EGL_DEBUG
, "skip invalid config 0x%x",
497 native_configs
[i
]->mode
.visualID
);
502 gconf
->native
= native_configs
[i
];
503 _eglAddConfig(dpy
, &gconf
->base
);
507 free(native_configs
);
512 * Re-validate the context.
515 egl_g3d_update_buffer(struct pipe_screen
*screen
, void *context_private
)
517 struct egl_g3d_context
*gctx
= egl_g3d_context(context_private
);
518 egl_g3d_validate_context(gctx
->base
.Resource
.Display
, &gctx
->base
);
522 egl_g3d_invalid_surface(struct native_display
*ndpy
,
523 struct native_surface
*nsurf
,
524 unsigned int seq_num
)
526 /* XXX not thread safe? */
527 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(nsurf
->user_data
);
528 struct egl_g3d_context
*gctx
= egl_g3d_context(gsurf
->base
.CurrentContext
);
530 /* set force_validate to skip an unnecessary check */
532 gctx
->force_validate
= TRUE
;
535 static struct native_event_handler egl_g3d_native_event_handler
= {
536 .invalid_surface
= egl_g3d_invalid_surface
540 egl_g3d_terminate(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
542 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
545 _eglReleaseDisplayResources(drv
, dpy
);
546 _eglCleanupDisplay(dpy
);
549 for (i
= 0; i
< dpy
->NumScreens
; i
++) {
550 struct egl_g3d_screen
*gscr
= egl_g3d_screen(dpy
->Screens
[i
]);
551 free(gscr
->native_modes
);
558 gdpy
->native
->destroy(gdpy
->native
);
561 dpy
->DriverData
= NULL
;
567 egl_g3d_initialize(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
568 EGLint
*major
, EGLint
*minor
)
570 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
571 struct egl_g3d_display
*gdpy
;
573 /* the probe object is unlikely to be needed again */
574 egl_g3d_destroy_probe(drv
, dpy
);
576 gdpy
= CALLOC_STRUCT(egl_g3d_display
);
578 _eglError(EGL_BAD_ALLOC
, "eglInitialize");
581 dpy
->DriverData
= gdpy
;
583 gdpy
->native
= native_create_display(dpy
->NativeDisplay
,
584 &egl_g3d_native_event_handler
);
586 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(no usable display)");
590 gdpy
->native
->user_data
= (void *) dpy
;
591 gdpy
->native
->screen
->update_buffer
= egl_g3d_update_buffer
;
593 egl_g3d_init_st(&gdrv
->base
);
594 dpy
->ClientAPIsMask
= gdrv
->api_mask
;
596 #ifdef EGL_MESA_screen_surface
597 /* enable MESA_screen_surface before adding (and validating) configs */
598 if (gdpy
->native
->modeset
) {
599 dpy
->Extensions
.MESA_screen_surface
= EGL_TRUE
;
600 egl_g3d_add_screens(drv
, dpy
);
604 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
605 if (gdpy
->native
->get_param(gdpy
->native
, NATIVE_PARAM_USE_NATIVE_BUFFER
))
606 dpy
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
608 if (egl_g3d_add_configs(drv
, dpy
, 1) == 1) {
609 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(unable to add configs)");
620 egl_g3d_terminate(drv
, dpy
);
625 egl_g3d_create_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
626 _EGLContext
*share
, const EGLint
*attribs
)
628 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
629 struct egl_g3d_context
*gshare
= egl_g3d_context(share
);
630 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
631 struct egl_g3d_context
*gctx
;
632 const __GLcontextModes
*mode
;
634 gctx
= CALLOC_STRUCT(egl_g3d_context
);
636 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
640 if (!_eglInitContext(&gctx
->base
, dpy
, conf
, attribs
)) {
645 gctx
->stapi
= egl_g3d_choose_st(drv
, &gctx
->base
);
651 mode
= &gconf
->native
->mode
;
653 gctx
->pipe
= gdpy
->native
->screen
->context_create(
654 gdpy
->native
->screen
,
655 (void *) &gctx
->base
);
662 gctx
->st_ctx
= gctx
->stapi
->st_create_context(gctx
->pipe
, mode
,
663 (gshare
) ? gshare
->st_ctx
: NULL
);
665 gctx
->pipe
->destroy(gctx
->pipe
);
677 destroy_context(_EGLDisplay
*dpy
, _EGLContext
*ctx
)
679 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
681 /* FIXME a context might live longer than its display */
682 if (!dpy
->Initialized
)
683 _eglLog(_EGL_FATAL
, "destroy a context with an unitialized display");
685 egl_g3d_realloc_context(dpy
, &gctx
->base
);
686 /* it will destroy the associated pipe context */
687 gctx
->stapi
->st_destroy_context(gctx
->st_ctx
);
693 egl_g3d_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
695 if (!_eglIsContextBound(ctx
))
696 destroy_context(dpy
, ctx
);
700 struct egl_g3d_create_surface_arg
{
703 EGLNativeWindowType win
;
704 EGLNativePixmapType pix
;
709 egl_g3d_create_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
710 struct egl_g3d_create_surface_arg
*arg
,
711 const EGLint
*attribs
)
713 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
714 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
715 struct egl_g3d_surface
*gsurf
;
716 struct native_surface
*nsurf
;
721 err
= "eglCreateWindowSurface";
724 err
= "eglCreatePixmapSurface";
726 case EGL_PBUFFER_BIT
:
727 err
= "eglCreatePBufferSurface";
729 #ifdef EGL_MESA_screen_surface
730 case EGL_SCREEN_BIT_MESA
:
731 err
= "eglCreateScreenSurface";
735 err
= "eglCreateUnknownSurface";
739 gsurf
= CALLOC_STRUCT(egl_g3d_surface
);
741 _eglError(EGL_BAD_ALLOC
, err
);
745 if (!_eglInitSurface(&gsurf
->base
, dpy
, arg
->type
, conf
, attribs
)) {
750 /* create the native surface */
753 nsurf
= gdpy
->native
->create_window_surface(gdpy
->native
,
754 arg
->u
.win
, gconf
->native
);
757 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
758 arg
->u
.pix
, gconf
->native
);
760 case EGL_PBUFFER_BIT
:
761 nsurf
= gdpy
->native
->create_pbuffer_surface(gdpy
->native
,
762 gconf
->native
, gsurf
->base
.Width
, gsurf
->base
.Height
);
764 #ifdef EGL_MESA_screen_surface
765 case EGL_SCREEN_BIT_MESA
:
766 /* prefer back buffer (move to _eglInitSurface?) */
767 gsurf
->base
.RenderBuffer
= EGL_BACK_BUFFER
;
768 nsurf
= gdpy
->native
->modeset
->create_scanout_surface(gdpy
->native
,
769 gconf
->native
, gsurf
->base
.Width
, gsurf
->base
.Height
);
781 /* initialize the geometry */
782 if (!nsurf
->validate(nsurf
, 0x0, &gsurf
->sequence_number
, NULL
,
783 &gsurf
->base
.Width
, &gsurf
->base
.Height
)) {
784 nsurf
->destroy(nsurf
);
789 nsurf
->user_data
= &gsurf
->base
;
790 gsurf
->native
= nsurf
;
792 gsurf
->render_att
= (gsurf
->base
.RenderBuffer
== EGL_SINGLE_BUFFER
) ?
793 NATIVE_ATTACHMENT_FRONT_LEFT
: NATIVE_ATTACHMENT_BACK_LEFT
;
794 if (!gconf
->native
->mode
.doubleBufferMode
)
795 gsurf
->render_att
= NATIVE_ATTACHMENT_FRONT_LEFT
;
801 egl_g3d_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
802 _EGLConfig
*conf
, EGLNativeWindowType win
,
803 const EGLint
*attribs
)
805 struct egl_g3d_create_surface_arg arg
;
807 memset(&arg
, 0, sizeof(arg
));
808 arg
.type
= EGL_WINDOW_BIT
;
811 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
815 egl_g3d_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
816 _EGLConfig
*conf
, EGLNativePixmapType pix
,
817 const EGLint
*attribs
)
819 struct egl_g3d_create_surface_arg arg
;
821 memset(&arg
, 0, sizeof(arg
));
822 arg
.type
= EGL_PIXMAP_BIT
;
825 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
829 egl_g3d_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
830 _EGLConfig
*conf
, const EGLint
*attribs
)
832 struct egl_g3d_create_surface_arg arg
;
834 memset(&arg
, 0, sizeof(arg
));
835 arg
.type
= EGL_PBUFFER_BIT
;
837 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
844 destroy_surface(_EGLDisplay
*dpy
, _EGLSurface
*surf
)
846 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
848 /* FIXME a surface might live longer than its display */
849 if (!dpy
->Initialized
)
850 _eglLog(_EGL_FATAL
, "destroy a surface with an unitialized display");
852 pipe_surface_reference(&gsurf
->render_surface
, NULL
);
853 gsurf
->native
->destroy(gsurf
->native
);
858 egl_g3d_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
860 if (!_eglIsSurfaceBound(surf
))
861 destroy_surface(dpy
, surf
);
866 egl_g3d_make_current(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
867 _EGLSurface
*draw
, _EGLSurface
*read
, _EGLContext
*ctx
)
869 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
870 struct egl_g3d_surface
*gdraw
= egl_g3d_surface(draw
);
871 struct egl_g3d_context
*old_gctx
;
872 EGLBoolean ok
= EGL_TRUE
;
874 /* bind the new context and return the "orphaned" one */
875 if (!_eglBindContext(&ctx
, &draw
, &read
))
877 old_gctx
= egl_g3d_context(ctx
);
880 /* flush old context */
881 old_gctx
->stapi
->st_flush(old_gctx
->st_ctx
,
882 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
885 * The old context is no longer current, and egl_g3d_realloc_context()
886 * should be called to destroy the framebuffers. However, it is possible
887 * that it will be made current again with the same draw/read surfaces.
888 * It might be better to keep it around.
893 ok
= egl_g3d_realloc_context(dpy
, &gctx
->base
);
895 /* XXX: need to pass the winsys argument for
896 * flush_frontbuffer in the fourth parameter here:
898 ok
= gctx
->stapi
->st_make_current(gctx
->st_ctx
,
903 egl_g3d_validate_context(dpy
, &gctx
->base
);
904 if (gdraw
->base
.Type
== EGL_WINDOW_BIT
) {
905 gctx
->base
.WindowRenderBuffer
=
906 (gdraw
->render_att
== NATIVE_ATTACHMENT_FRONT_LEFT
) ?
907 EGL_SINGLE_BUFFER
: EGL_BACK_BUFFER
;
913 ok
= old_gctx
->stapi
->st_make_current(NULL
, NULL
, NULL
, NULL
);
914 old_gctx
->base
.WindowRenderBuffer
= EGL_NONE
;
917 if (ctx
&& !_eglIsContextLinked(ctx
))
918 destroy_context(dpy
, ctx
);
919 if (draw
&& !_eglIsSurfaceLinked(draw
))
920 destroy_surface(dpy
, draw
);
921 if (read
&& read
!= draw
&& !_eglIsSurfaceLinked(read
))
922 destroy_surface(dpy
, read
);
928 egl_g3d_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
930 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
931 _EGLContext
*ctx
= _eglGetCurrentContext();
932 struct egl_g3d_context
*gctx
= NULL
;
934 /* no-op for pixmap or pbuffer surface */
935 if (gsurf
->base
.Type
== EGL_PIXMAP_BIT
||
936 gsurf
->base
.Type
== EGL_PBUFFER_BIT
)
939 /* or when the surface is single-buffered */
940 if (gsurf
->render_att
== NATIVE_ATTACHMENT_FRONT_LEFT
)
943 if (ctx
&& ctx
->DrawSurface
== surf
)
944 gctx
= egl_g3d_context(ctx
);
946 /* flush if the surface is current */
948 gctx
->stapi
->st_notify_swapbuffers(gctx
->draw
.st_fb
);
950 return gsurf
->native
->swap_buffers(gsurf
->native
);
954 * Find a config that supports the pixmap.
957 egl_g3d_find_pixmap_config(_EGLDisplay
*dpy
, EGLNativePixmapType pix
)
959 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
960 struct egl_g3d_config
*gconf
;
963 for (i
= 0; i
< dpy
->NumConfigs
; i
++) {
964 gconf
= egl_g3d_config(dpy
->Configs
[i
]);
965 if (gdpy
->native
->is_pixmap_supported(gdpy
->native
, pix
, gconf
->native
))
969 return (i
< dpy
->NumConfigs
) ? &gconf
->base
: NULL
;
973 * Get the pipe surface of the given attachment of the native surface.
975 static struct pipe_surface
*
976 get_pipe_surface(struct native_display
*ndpy
, struct native_surface
*nsurf
,
977 enum native_attachment natt
)
979 struct pipe_texture
*textures
[NUM_NATIVE_ATTACHMENTS
];
980 struct pipe_surface
*psurf
;
982 textures
[natt
] = NULL
;
983 nsurf
->validate(nsurf
, 1 << natt
, NULL
, textures
, NULL
, NULL
);
987 psurf
= ndpy
->screen
->get_tex_surface(ndpy
->screen
, textures
[natt
],
988 0, 0, 0, PIPE_BUFFER_USAGE_CPU_WRITE
);
989 pipe_texture_reference(&textures
[natt
], NULL
);
995 egl_g3d_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
996 EGLNativePixmapType target
)
998 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
999 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
1000 _EGLContext
*ctx
= _eglGetCurrentContext();
1001 struct egl_g3d_config
*gconf
;
1002 struct native_surface
*nsurf
;
1003 struct pipe_screen
*screen
= gdpy
->native
->screen
;
1004 struct pipe_surface
*psurf
;
1006 if (!gsurf
->render_surface
)
1009 gconf
= egl_g3d_config(egl_g3d_find_pixmap_config(dpy
, target
));
1011 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
1013 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
1014 target
, gconf
->native
);
1016 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
1018 /* flush if the surface is current */
1019 if (ctx
&& ctx
->DrawSurface
== &gsurf
->base
) {
1020 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
1021 gctx
->stapi
->st_flush(gctx
->st_ctx
,
1022 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
1025 psurf
= get_pipe_surface(gdpy
->native
, nsurf
, NATIVE_ATTACHMENT_FRONT_LEFT
);
1027 struct pipe_context pipe
;
1030 * XXX This is hacky. If we might allow the EGLDisplay to create a pipe
1031 * context of its own and use the blitter context for this.
1033 memset(&pipe
, 0, sizeof(pipe
));
1034 pipe
.screen
= screen
;
1036 util_surface_copy(&pipe
, FALSE
, psurf
, 0, 0,
1037 gsurf
->render_surface
, 0, 0, psurf
->width
, psurf
->height
);
1039 pipe_surface_reference(&psurf
, NULL
);
1040 nsurf
->flush_frontbuffer(nsurf
);
1043 nsurf
->destroy(nsurf
);
1049 egl_g3d_wait_client(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
1051 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
1052 gctx
->stapi
->st_finish(gctx
->st_ctx
);
1057 egl_g3d_wait_native(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint engine
)
1059 _EGLContext
*ctx
= _eglGetCurrentContext();
1061 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1062 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1064 if (ctx
&& ctx
->DrawSurface
) {
1065 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(ctx
->DrawSurface
);
1066 gsurf
->native
->wait(gsurf
->native
);
1073 egl_g3d_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1075 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
1079 /* in case this is called before a display is initialized */
1080 egl_g3d_init_st(&gdrv
->base
);
1082 for (i
= 0; i
< NUM_EGL_G3D_STS
; i
++) {
1083 const struct egl_g3d_st
*stapi
= gdrv
->stapis
[i
];
1085 proc
= (_EGLProc
) stapi
->st_get_proc_address(procname
);
1091 return (_EGLProc
) NULL
;
1095 egl_g3d_bind_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1096 _EGLSurface
*surf
, EGLint buffer
)
1098 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
1099 _EGLContext
*es1
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
1100 struct egl_g3d_context
*gctx
;
1101 enum pipe_format target_format
;
1104 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
)
1105 return _eglError(EGL_BAD_SURFACE
, "eglBindTexImage");
1106 if (buffer
!= EGL_BACK_BUFFER
)
1107 return _eglError(EGL_BAD_PARAMETER
, "eglBindTexImage");
1108 if (gsurf
->base
.BoundToTexture
)
1109 return _eglError(EGL_BAD_ACCESS
, "eglBindTexImage");
1111 switch (gsurf
->base
.TextureFormat
) {
1112 case EGL_TEXTURE_RGB
:
1113 target_format
= PIPE_FORMAT_R8G8B8_UNORM
;
1115 case EGL_TEXTURE_RGBA
:
1116 target_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
1119 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
1122 switch (gsurf
->base
.TextureTarget
) {
1123 case EGL_TEXTURE_2D
:
1124 target
= ST_TEXTURE_2D
;
1127 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
1132 if (!gsurf
->render_surface
)
1135 /* flush properly if the surface is bound */
1136 if (gsurf
->base
.CurrentContext
) {
1137 gctx
= egl_g3d_context(gsurf
->base
.CurrentContext
);
1138 gctx
->stapi
->st_flush(gctx
->st_ctx
,
1139 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
1142 gctx
= egl_g3d_context(es1
);
1143 gctx
->stapi
->st_bind_texture_surface(gsurf
->render_surface
,
1144 target
, gsurf
->base
.MipmapLevel
, target_format
);
1146 gsurf
->base
.BoundToTexture
= EGL_TRUE
;
1152 egl_g3d_release_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1153 _EGLSurface
*surf
, EGLint buffer
)
1155 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
1157 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
||
1158 !gsurf
->base
.BoundToTexture
)
1159 return _eglError(EGL_BAD_SURFACE
, "eglReleaseTexImage");
1160 if (buffer
!= EGL_BACK_BUFFER
)
1161 return _eglError(EGL_BAD_PARAMETER
, "eglReleaseTexImage");
1163 if (gsurf
->render_surface
) {
1164 _EGLContext
*ctx
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
1165 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
1167 /* what if the context the surface binds to is no longer current? */
1169 gctx
->stapi
->st_unbind_texture_surface(gsurf
->render_surface
,
1170 ST_TEXTURE_2D
, gsurf
->base
.MipmapLevel
);
1173 gsurf
->base
.BoundToTexture
= EGL_FALSE
;
1178 #ifdef EGL_MESA_screen_surface
1180 static _EGLSurface
*
1181 egl_g3d_create_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1182 _EGLConfig
*conf
, const EGLint
*attribs
)
1184 struct egl_g3d_create_surface_arg arg
;
1186 memset(&arg
, 0, sizeof(arg
));
1187 arg
.type
= EGL_SCREEN_BIT_MESA
;
1189 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
1193 egl_g3d_show_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1194 _EGLScreen
*scr
, _EGLSurface
*surf
,
1197 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
1198 struct egl_g3d_screen
*gscr
= egl_g3d_screen(scr
);
1199 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
1200 struct native_surface
*nsurf
;
1201 const struct native_mode
*nmode
;
1208 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
1209 if (gsurf
->base
.Type
!= EGL_SCREEN_BIT_MESA
)
1210 return _eglError(EGL_BAD_SURFACE
, "eglShowScreenSurfaceMESA");
1211 if (gsurf
->base
.Width
< mode
->Width
|| gsurf
->base
.Height
< mode
->Height
)
1212 return _eglError(EGL_BAD_MATCH
,
1213 "eglShowSurfaceMESA(surface smaller than mode size)");
1215 /* find the index of the mode */
1216 for (idx
= 0; idx
< gscr
->base
.NumModes
; idx
++)
1217 if (mode
== &gscr
->base
.Modes
[idx
])
1219 if (idx
>= gscr
->base
.NumModes
) {
1220 return _eglError(EGL_BAD_MODE_MESA
,
1221 "eglShowSurfaceMESA(unknown mode)");
1224 nsurf
= gsurf
->native
;
1225 nmode
= gscr
->native_modes
[idx
];
1229 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
1231 /* disable the screen */
1236 /* TODO surface panning by CRTC choosing */
1237 changed
= gdpy
->native
->modeset
->program(gdpy
->native
, 0, nsurf
,
1238 gscr
->base
.OriginX
, gscr
->base
.OriginY
, &gscr
->native
, 1, nmode
);
1240 gscr
->base
.CurrentSurface
= &gsurf
->base
;
1241 gscr
->base
.CurrentMode
= mode
;
1247 #endif /* EGL_MESA_screen_surface */
1250 egl_g3d_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1252 struct native_probe
*nprobe
;
1253 enum native_probe_result res
;
1256 nprobe
= egl_g3d_get_probe(drv
, dpy
);
1257 res
= native_get_probe_result(nprobe
);
1260 case NATIVE_PROBE_UNKNOWN
:
1264 case NATIVE_PROBE_FALLBACK
:
1267 case NATIVE_PROBE_SUPPORTED
:
1270 case NATIVE_PROBE_EXACT
:
1279 egl_g3d_unload(_EGLDriver
*drv
)
1281 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
1283 egl_g3d_destroy_probe(drv
, NULL
);
1288 _eglMain(const char *args
)
1290 static char driver_name
[64];
1291 struct egl_g3d_driver
*gdrv
;
1293 snprintf(driver_name
, sizeof(driver_name
),
1294 "Gallium/%s", native_get_name());
1296 gdrv
= CALLOC_STRUCT(egl_g3d_driver
);
1300 _eglInitDriverFallbacks(&gdrv
->base
);
1302 gdrv
->base
.API
.Initialize
= egl_g3d_initialize
;
1303 gdrv
->base
.API
.Terminate
= egl_g3d_terminate
;
1304 gdrv
->base
.API
.CreateContext
= egl_g3d_create_context
;
1305 gdrv
->base
.API
.DestroyContext
= egl_g3d_destroy_context
;
1306 gdrv
->base
.API
.CreateWindowSurface
= egl_g3d_create_window_surface
;
1307 gdrv
->base
.API
.CreatePixmapSurface
= egl_g3d_create_pixmap_surface
;
1308 gdrv
->base
.API
.CreatePbufferSurface
= egl_g3d_create_pbuffer_surface
;
1309 gdrv
->base
.API
.DestroySurface
= egl_g3d_destroy_surface
;
1310 gdrv
->base
.API
.MakeCurrent
= egl_g3d_make_current
;
1311 gdrv
->base
.API
.SwapBuffers
= egl_g3d_swap_buffers
;
1312 gdrv
->base
.API
.CopyBuffers
= egl_g3d_copy_buffers
;
1313 gdrv
->base
.API
.WaitClient
= egl_g3d_wait_client
;
1314 gdrv
->base
.API
.WaitNative
= egl_g3d_wait_native
;
1315 gdrv
->base
.API
.GetProcAddress
= egl_g3d_get_proc_address
;
1317 gdrv
->base
.API
.BindTexImage
= egl_g3d_bind_tex_image
;
1318 gdrv
->base
.API
.ReleaseTexImage
= egl_g3d_release_tex_image
;
1320 gdrv
->base
.API
.CreateImageKHR
= egl_g3d_create_image
;
1321 gdrv
->base
.API
.DestroyImageKHR
= egl_g3d_destroy_image
;
1323 #ifdef EGL_MESA_screen_surface
1324 gdrv
->base
.API
.CreateScreenSurfaceMESA
= egl_g3d_create_screen_surface
;
1325 gdrv
->base
.API
.ShowScreenSurfaceMESA
= egl_g3d_show_screen_surface
;
1328 gdrv
->base
.Name
= driver_name
;
1329 gdrv
->base
.Probe
= egl_g3d_probe
;
1330 gdrv
->base
.Unload
= egl_g3d_unload
;
1332 /* the key is " EGL G3D" */
1333 gdrv
->probe_key
= 0x0E61063D;