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_st.h"
40 #include "egl_g3d_image.h"
43 * Return the state tracker for the given context.
45 static struct st_api
*
46 egl_g3d_choose_st(_EGLDriver
*drv
, _EGLContext
*ctx
)
48 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
52 switch (ctx
->ClientAPI
) {
53 case EGL_OPENGL_ES_API
:
54 switch (ctx
->ClientVersion
) {
56 idx
= ST_API_OPENGL_ES1
;
59 idx
= ST_API_OPENGL_ES2
;
62 _eglLog(_EGL_WARNING
, "unknown client version %d",
74 _eglLog(_EGL_WARNING
, "unknown client API 0x%04x", ctx
->ClientAPI
);
78 stapi
= (idx
>= 0) ? gdrv
->stapis
[idx
] : NULL
;
83 * Initialize the state trackers.
86 egl_g3d_init_st(_EGLDriver
*drv
)
88 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
91 /* already initialized */
95 for (i
= 0; i
< ST_API_COUNT
; i
++) {
96 gdrv
->stapis
[i
] = egl_g3d_create_st_api(i
);
98 gdrv
->api_mask
|= egl_g3d_st_api_bit(i
);
102 _eglLog(_EGL_DEBUG
, "Driver API mask: 0x%x", gdrv
->api_mask
);
104 _eglLog(_EGL_WARNING
, "No supported client API");
108 * Get the probe object of the display.
110 * Note that this function may be called before the display is initialized.
112 static struct native_probe
*
113 egl_g3d_get_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
115 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
116 struct native_probe
*nprobe
;
118 nprobe
= (struct native_probe
*) _eglGetProbeCache(gdrv
->probe_key
);
119 if (!nprobe
|| nprobe
->display
!= dpy
->NativeDisplay
) {
121 nprobe
->destroy(nprobe
);
122 nprobe
= native_create_probe(dpy
->NativeDisplay
);
123 _eglSetProbeCache(gdrv
->probe_key
, (void *) nprobe
);
130 * Destroy the probe object of the display. The display may be NULL.
132 * Note that this function may be called before the display is initialized.
135 egl_g3d_destroy_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
137 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
138 struct native_probe
*nprobe
;
140 nprobe
= (struct native_probe
*) _eglGetProbeCache(gdrv
->probe_key
);
141 if (nprobe
&& (!dpy
|| nprobe
->display
== dpy
->NativeDisplay
)) {
142 nprobe
->destroy(nprobe
);
143 _eglSetProbeCache(gdrv
->probe_key
, NULL
);
147 #ifdef EGL_MESA_screen_surface
150 egl_g3d_add_screens(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
152 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
153 const struct native_connector
**native_connectors
;
154 EGLint num_connectors
, i
;
157 gdpy
->native
->modeset
->get_connectors(gdpy
->native
, &num_connectors
, NULL
);
158 if (!num_connectors
) {
159 if (native_connectors
)
160 free(native_connectors
);
164 for (i
= 0; i
< num_connectors
; i
++) {
165 const struct native_connector
*nconn
= native_connectors
[i
];
166 struct egl_g3d_screen
*gscr
;
167 const struct native_mode
**native_modes
;
170 /* TODO support for hotplug */
172 gdpy
->native
->modeset
->get_modes(gdpy
->native
, nconn
, &num_modes
);
179 gscr
= CALLOC_STRUCT(egl_g3d_screen
);
185 _eglInitScreen(&gscr
->base
);
187 for (j
= 0; j
< num_modes
; j
++) {
188 const struct native_mode
*nmode
= native_modes
[j
];
191 mode
= _eglAddNewMode(&gscr
->base
, nmode
->width
, nmode
->height
,
192 nmode
->refresh_rate
, nmode
->desc
);
195 /* gscr->native_modes and gscr->base.Modes should be consistent */
196 assert(mode
== &gscr
->base
.Modes
[j
]);
199 gscr
->native
= nconn
;
200 gscr
->native_modes
= native_modes
;
202 _eglAddScreen(dpy
, &gscr
->base
);
205 free(native_connectors
);
208 #endif /* EGL_MESA_screen_surface */
211 * Initialize an EGL config from the native config.
214 egl_g3d_init_config(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
215 _EGLConfig
*conf
, const struct native_config
*nconf
)
217 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
218 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
219 const __GLcontextModes
*mode
= &nconf
->mode
;
220 EGLint buffer_mask
, api_mask
;
224 buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
225 if (mode
->doubleBufferMode
)
226 buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
227 if (mode
->stereoMode
) {
228 buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
229 if (mode
->doubleBufferMode
)
230 buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
233 gconf
->stvis
.buffer_mask
= buffer_mask
;
234 gconf
->stvis
.color_format
= nconf
->color_format
;
235 gconf
->stvis
.depth_stencil_format
= nconf
->depth_format
;
236 gconf
->stvis
.accum_format
= PIPE_FORMAT_NONE
;
237 gconf
->stvis
.samples
= 0;
239 gconf
->stvis
.render_buffer
= (buffer_mask
& ST_ATTACHMENT_BACK_LEFT
) ?
240 ST_ATTACHMENT_BACK_LEFT
: ST_ATTACHMENT_FRONT_LEFT
;
243 for (i
= 0; i
< ST_API_COUNT
; i
++) {
244 struct st_api
*stapi
= gdrv
->stapis
[i
];
246 if (stapi
->is_visual_supported(stapi
, &gconf
->stvis
))
247 api_mask
|= egl_g3d_st_api_bit(i
);
250 /* this is required by EGL, not by OpenGL ES */
251 if ((mode
->drawableType
& GLX_WINDOW_BIT
) && !mode
->doubleBufferMode
)
252 api_mask
&= ~(EGL_OPENGL_ES_BIT
| EGL_OPENGL_ES2_BIT
);
255 _eglLog(_EGL_DEBUG
, "no state tracker supports config 0x%x",
259 valid
= _eglConfigFromContextModesRec(&gconf
->base
,
260 mode
, api_mask
, api_mask
);
262 #ifdef EGL_MESA_screen_surface
263 /* check if scanout surface bit is set */
264 if (nconf
->scanout_bit
) {
265 EGLint val
= GET_CONFIG_ATTRIB(&gconf
->base
, EGL_SURFACE_TYPE
);
266 val
|= EGL_SCREEN_BIT_MESA
;
267 SET_CONFIG_ATTRIB(&gconf
->base
, EGL_SURFACE_TYPE
, val
);
270 valid
= _eglValidateConfig(&gconf
->base
, EGL_FALSE
);
273 _eglLog(_EGL_DEBUG
, "skip invalid config 0x%x", mode
->visualID
);
277 gconf
->native
= nconf
;
283 * Add configs to display and return the next config ID.
286 egl_g3d_add_configs(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint id
)
288 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
289 const struct native_config
**native_configs
;
292 native_configs
= gdpy
->native
->get_configs(gdpy
->native
, &num_configs
);
295 free(native_configs
);
299 for (i
= 0; i
< num_configs
; i
++) {
300 struct egl_g3d_config
*gconf
;
302 gconf
= CALLOC_STRUCT(egl_g3d_config
);
304 _eglInitConfig(&gconf
->base
, dpy
, id
);
305 if (!egl_g3d_init_config(drv
, dpy
, &gconf
->base
, native_configs
[i
])) {
310 _eglAddConfig(dpy
, &gconf
->base
);
315 free(native_configs
);
320 egl_g3d_invalid_surface(struct native_display
*ndpy
,
321 struct native_surface
*nsurf
,
322 unsigned int seq_num
)
324 /* XXX not thread safe? */
325 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(nsurf
->user_data
);
326 struct egl_g3d_context
*gctx
;
329 * Some functions such as egl_g3d_copy_buffers create a temporary native
330 * surface. There is no gsurf associated with it.
332 gctx
= (gsurf
) ? egl_g3d_context(gsurf
->base
.CurrentContext
) : NULL
;
334 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
, gsurf
->stfbi
);
337 static struct native_event_handler egl_g3d_native_event_handler
= {
338 .invalid_surface
= egl_g3d_invalid_surface
342 egl_g3d_terminate(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
344 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
347 _eglReleaseDisplayResources(drv
, dpy
);
348 _eglCleanupDisplay(dpy
);
351 gdpy
->pipe
->destroy(gdpy
->pipe
);
354 for (i
= 0; i
< dpy
->NumScreens
; i
++) {
355 struct egl_g3d_screen
*gscr
= egl_g3d_screen(dpy
->Screens
[i
]);
356 free(gscr
->native_modes
);
363 egl_g3d_destroy_st_manager(gdpy
->smapi
);
366 gdpy
->native
->destroy(gdpy
->native
);
369 dpy
->DriverData
= NULL
;
375 egl_g3d_initialize(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
376 EGLint
*major
, EGLint
*minor
)
378 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
379 struct egl_g3d_display
*gdpy
;
381 /* the probe object is unlikely to be needed again */
382 egl_g3d_destroy_probe(drv
, dpy
);
384 gdpy
= CALLOC_STRUCT(egl_g3d_display
);
386 _eglError(EGL_BAD_ALLOC
, "eglInitialize");
389 dpy
->DriverData
= gdpy
;
391 gdpy
->native
= native_create_display(dpy
->NativeDisplay
,
392 &egl_g3d_native_event_handler
);
394 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(no usable display)");
398 gdpy
->native
->user_data
= (void *) dpy
;
400 egl_g3d_init_st(&gdrv
->base
);
401 dpy
->ClientAPIsMask
= gdrv
->api_mask
;
403 gdpy
->smapi
= egl_g3d_create_st_manager(dpy
);
405 _eglError(EGL_NOT_INITIALIZED
,
406 "eglInitialize(failed to create st manager)");
410 #ifdef EGL_MESA_screen_surface
411 /* enable MESA_screen_surface before adding (and validating) configs */
412 if (gdpy
->native
->modeset
) {
413 dpy
->Extensions
.MESA_screen_surface
= EGL_TRUE
;
414 egl_g3d_add_screens(drv
, dpy
);
418 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
419 if (gdpy
->native
->get_param(gdpy
->native
, NATIVE_PARAM_USE_NATIVE_BUFFER
))
420 dpy
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
422 if (egl_g3d_add_configs(drv
, dpy
, 1) == 1) {
423 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(unable to add configs)");
434 egl_g3d_terminate(drv
, dpy
);
439 egl_g3d_create_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
440 _EGLContext
*share
, const EGLint
*attribs
)
442 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
443 struct egl_g3d_context
*gshare
= egl_g3d_context(share
);
444 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
445 struct egl_g3d_context
*gctx
;
447 gctx
= CALLOC_STRUCT(egl_g3d_context
);
449 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
453 if (!_eglInitContext(&gctx
->base
, dpy
, conf
, attribs
)) {
458 gctx
->stapi
= egl_g3d_choose_st(drv
, &gctx
->base
);
464 gctx
->stctxi
= gctx
->stapi
->create_context(gctx
->stapi
, gdpy
->smapi
,
465 &gconf
->stvis
, (gshare
) ? gshare
->stctxi
: NULL
);
471 gctx
->stctxi
->st_manager_private
= (void *) &gctx
->base
;
480 destroy_context(_EGLDisplay
*dpy
, _EGLContext
*ctx
)
482 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
484 /* FIXME a context might live longer than its display */
485 if (!dpy
->Initialized
)
486 _eglLog(_EGL_FATAL
, "destroy a context with an unitialized display");
488 gctx
->stctxi
->destroy(gctx
->stctxi
);
494 egl_g3d_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
496 if (!_eglIsContextBound(ctx
))
497 destroy_context(dpy
, ctx
);
501 struct egl_g3d_create_surface_arg
{
504 EGLNativeWindowType win
;
505 EGLNativePixmapType pix
;
510 egl_g3d_create_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
511 struct egl_g3d_create_surface_arg
*arg
,
512 const EGLint
*attribs
)
514 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
515 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
516 struct egl_g3d_surface
*gsurf
;
517 struct native_surface
*nsurf
;
522 err
= "eglCreateWindowSurface";
525 err
= "eglCreatePixmapSurface";
527 case EGL_PBUFFER_BIT
:
528 err
= "eglCreatePBufferSurface";
530 #ifdef EGL_MESA_screen_surface
531 case EGL_SCREEN_BIT_MESA
:
532 err
= "eglCreateScreenSurface";
536 err
= "eglCreateUnknownSurface";
540 gsurf
= CALLOC_STRUCT(egl_g3d_surface
);
542 _eglError(EGL_BAD_ALLOC
, err
);
546 if (!_eglInitSurface(&gsurf
->base
, dpy
, arg
->type
, conf
, attribs
)) {
551 /* create the native surface */
554 nsurf
= gdpy
->native
->create_window_surface(gdpy
->native
,
555 arg
->u
.win
, gconf
->native
);
558 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
559 arg
->u
.pix
, gconf
->native
);
561 case EGL_PBUFFER_BIT
:
562 nsurf
= gdpy
->native
->create_pbuffer_surface(gdpy
->native
,
563 gconf
->native
, gsurf
->base
.Width
, gsurf
->base
.Height
);
565 #ifdef EGL_MESA_screen_surface
566 case EGL_SCREEN_BIT_MESA
:
567 /* prefer back buffer (move to _eglInitSurface?) */
568 gsurf
->base
.RenderBuffer
= EGL_BACK_BUFFER
;
569 nsurf
= gdpy
->native
->modeset
->create_scanout_surface(gdpy
->native
,
570 gconf
->native
, gsurf
->base
.Width
, gsurf
->base
.Height
);
582 /* initialize the geometry */
583 if (!nsurf
->validate(nsurf
, 0x0, &gsurf
->sequence_number
, NULL
,
584 &gsurf
->base
.Width
, &gsurf
->base
.Height
)) {
585 nsurf
->destroy(nsurf
);
590 gsurf
->stvis
= gconf
->stvis
;
591 if (gsurf
->base
.RenderBuffer
== EGL_SINGLE_BUFFER
)
592 gsurf
->stvis
.render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
594 gsurf
->stfbi
= egl_g3d_create_st_framebuffer(&gsurf
->base
);
596 gsurf
->native
->destroy(gsurf
->native
);
601 nsurf
->user_data
= &gsurf
->base
;
602 gsurf
->native
= nsurf
;
608 egl_g3d_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
609 _EGLConfig
*conf
, EGLNativeWindowType win
,
610 const EGLint
*attribs
)
612 struct egl_g3d_create_surface_arg arg
;
614 memset(&arg
, 0, sizeof(arg
));
615 arg
.type
= EGL_WINDOW_BIT
;
618 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
622 egl_g3d_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
623 _EGLConfig
*conf
, EGLNativePixmapType pix
,
624 const EGLint
*attribs
)
626 struct egl_g3d_create_surface_arg arg
;
628 memset(&arg
, 0, sizeof(arg
));
629 arg
.type
= EGL_PIXMAP_BIT
;
632 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
636 egl_g3d_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
637 _EGLConfig
*conf
, const EGLint
*attribs
)
639 struct egl_g3d_create_surface_arg arg
;
641 memset(&arg
, 0, sizeof(arg
));
642 arg
.type
= EGL_PBUFFER_BIT
;
644 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
651 destroy_surface(_EGLDisplay
*dpy
, _EGLSurface
*surf
)
653 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
655 /* FIXME a surface might live longer than its display */
656 if (!dpy
->Initialized
)
657 _eglLog(_EGL_FATAL
, "destroy a surface with an unitialized display");
659 pipe_texture_reference(&gsurf
->render_texture
, NULL
);
660 egl_g3d_destroy_st_framebuffer(gsurf
->stfbi
);
661 gsurf
->native
->destroy(gsurf
->native
);
666 egl_g3d_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
668 if (!_eglIsSurfaceBound(surf
))
669 destroy_surface(dpy
, surf
);
674 egl_g3d_make_current(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
675 _EGLSurface
*draw
, _EGLSurface
*read
, _EGLContext
*ctx
)
677 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
678 struct egl_g3d_surface
*gdraw
= egl_g3d_surface(draw
);
679 struct egl_g3d_surface
*gread
= egl_g3d_surface(read
);
680 struct egl_g3d_context
*old_gctx
;
681 EGLBoolean ok
= EGL_TRUE
;
683 /* bind the new context and return the "orphaned" one */
684 if (!_eglBindContext(&ctx
, &draw
, &read
))
686 old_gctx
= egl_g3d_context(ctx
);
689 /* flush old context */
690 old_gctx
->stctxi
->flush(old_gctx
->stctxi
,
691 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
695 ok
= gctx
->stapi
->make_current(gctx
->stapi
, gctx
->stctxi
,
696 (gdraw
) ? gdraw
->stfbi
: NULL
, (gread
) ? gread
->stfbi
: NULL
);
698 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
, gdraw
->stfbi
);
699 if (gread
!= gdraw
) {
700 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
,
704 if (gdraw
->base
.Type
== EGL_WINDOW_BIT
) {
705 gctx
->base
.WindowRenderBuffer
=
706 (gdraw
->stvis
.render_buffer
== ST_ATTACHMENT_FRONT_LEFT
) ?
707 EGL_SINGLE_BUFFER
: EGL_BACK_BUFFER
;
712 ok
= old_gctx
->stapi
->make_current(old_gctx
->stapi
, NULL
, NULL
, NULL
);
713 old_gctx
->base
.WindowRenderBuffer
= EGL_NONE
;
716 if (ctx
&& !_eglIsContextLinked(ctx
))
717 destroy_context(dpy
, ctx
);
718 if (draw
&& !_eglIsSurfaceLinked(draw
))
719 destroy_surface(dpy
, draw
);
720 if (read
&& read
!= draw
&& !_eglIsSurfaceLinked(read
))
721 destroy_surface(dpy
, read
);
727 egl_g3d_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
729 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
730 _EGLContext
*ctx
= _eglGetCurrentContext();
731 struct egl_g3d_context
*gctx
= NULL
;
733 /* no-op for pixmap or pbuffer surface */
734 if (gsurf
->base
.Type
== EGL_PIXMAP_BIT
||
735 gsurf
->base
.Type
== EGL_PBUFFER_BIT
)
738 /* or when the surface is single-buffered */
739 if (gsurf
->stvis
.render_buffer
== ST_ATTACHMENT_FRONT_LEFT
)
742 if (ctx
&& ctx
->DrawSurface
== surf
)
743 gctx
= egl_g3d_context(ctx
);
745 /* flush if the surface is current */
747 gctx
->stctxi
->flush(gctx
->stctxi
,
748 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
751 return gsurf
->native
->swap_buffers(gsurf
->native
);
755 * Find a config that supports the pixmap.
758 egl_g3d_find_pixmap_config(_EGLDisplay
*dpy
, EGLNativePixmapType pix
)
760 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
761 struct egl_g3d_config
*gconf
;
764 for (i
= 0; i
< dpy
->NumConfigs
; i
++) {
765 gconf
= egl_g3d_config(dpy
->Configs
[i
]);
766 if (gdpy
->native
->is_pixmap_supported(gdpy
->native
, pix
, gconf
->native
))
770 return (i
< dpy
->NumConfigs
) ? &gconf
->base
: NULL
;
774 * Get the pipe surface of the given attachment of the native surface.
776 static struct pipe_surface
*
777 get_pipe_surface(struct native_display
*ndpy
, struct native_surface
*nsurf
,
778 enum native_attachment natt
)
780 struct pipe_texture
*textures
[NUM_NATIVE_ATTACHMENTS
];
781 struct pipe_surface
*psurf
;
783 textures
[natt
] = NULL
;
784 nsurf
->validate(nsurf
, 1 << natt
, NULL
, textures
, NULL
, NULL
);
788 psurf
= ndpy
->screen
->get_tex_surface(ndpy
->screen
, textures
[natt
],
789 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE
);
790 pipe_texture_reference(&textures
[natt
], NULL
);
796 egl_g3d_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
797 EGLNativePixmapType target
)
799 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
800 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
801 _EGLContext
*ctx
= _eglGetCurrentContext();
802 struct egl_g3d_config
*gconf
;
803 struct native_surface
*nsurf
;
804 struct pipe_screen
*screen
= gdpy
->native
->screen
;
805 struct pipe_surface
*psurf
;
807 if (!gsurf
->render_texture
)
810 gconf
= egl_g3d_config(egl_g3d_find_pixmap_config(dpy
, target
));
812 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
814 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
815 target
, gconf
->native
);
817 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
819 /* flush if the surface is current */
820 if (ctx
&& ctx
->DrawSurface
== &gsurf
->base
) {
821 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
822 gctx
->stctxi
->flush(gctx
->stctxi
,
823 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
826 /* create a pipe context to copy surfaces */
829 gdpy
->native
->screen
->context_create(gdpy
->native
->screen
, NULL
);
834 psurf
= get_pipe_surface(gdpy
->native
, nsurf
, NATIVE_ATTACHMENT_FRONT_LEFT
);
836 struct pipe_surface
*psrc
;
838 psrc
= screen
->get_tex_surface(screen
, gsurf
->render_texture
,
839 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ
);
841 gdpy
->pipe
->surface_copy(gdpy
->pipe
, psurf
, 0, 0,
842 psrc
, 0, 0, psurf
->width
, psurf
->height
);
843 pipe_surface_reference(&psrc
, NULL
);
845 nsurf
->flush_frontbuffer(nsurf
);
848 pipe_surface_reference(&psurf
, NULL
);
851 nsurf
->destroy(nsurf
);
857 egl_g3d_wait_client(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
859 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
860 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
861 struct pipe_screen
*screen
= gdpy
->native
->screen
;
862 struct pipe_fence_handle
*fence
= NULL
;
864 gctx
->stctxi
->flush(gctx
->stctxi
,
865 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
866 screen
->fence_finish(screen
, fence
, 0);
867 screen
->fence_reference(screen
, &fence
, NULL
);
873 egl_g3d_wait_native(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint engine
)
875 _EGLContext
*ctx
= _eglGetCurrentContext();
877 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
878 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
880 if (ctx
&& ctx
->DrawSurface
) {
881 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(ctx
->DrawSurface
);
882 gsurf
->native
->wait(gsurf
->native
);
889 egl_g3d_get_proc_address(_EGLDriver
*drv
, const char *procname
)
891 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
895 /* in case this is called before a display is initialized */
896 egl_g3d_init_st(&gdrv
->base
);
898 for (i
= 0; i
< ST_API_COUNT
; i
++) {
899 struct st_api
*stapi
= gdrv
->stapis
[i
];
901 proc
= (_EGLProc
) stapi
->get_proc_address(stapi
, procname
);
907 return (_EGLProc
) NULL
;
911 egl_g3d_bind_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
912 _EGLSurface
*surf
, EGLint buffer
)
914 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
915 _EGLContext
*es1
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
916 struct egl_g3d_context
*gctx
;
917 enum pipe_format internal_format
;
918 enum st_texture_type target
;
920 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
)
921 return _eglError(EGL_BAD_SURFACE
, "eglBindTexImage");
922 if (buffer
!= EGL_BACK_BUFFER
)
923 return _eglError(EGL_BAD_PARAMETER
, "eglBindTexImage");
924 if (gsurf
->base
.BoundToTexture
)
925 return _eglError(EGL_BAD_ACCESS
, "eglBindTexImage");
927 switch (gsurf
->base
.TextureFormat
) {
928 case EGL_TEXTURE_RGB
:
929 internal_format
= PIPE_FORMAT_R8G8B8_UNORM
;
931 case EGL_TEXTURE_RGBA
:
932 internal_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
935 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
938 switch (gsurf
->base
.TextureTarget
) {
940 target
= ST_TEXTURE_2D
;
943 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
948 if (!gsurf
->render_texture
)
951 /* flush properly if the surface is bound */
952 if (gsurf
->base
.CurrentContext
) {
953 gctx
= egl_g3d_context(gsurf
->base
.CurrentContext
);
954 gctx
->stctxi
->flush(gctx
->stctxi
,
955 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
958 gctx
= egl_g3d_context(es1
);
959 if (gctx
->stctxi
->teximage
) {
960 if (!gctx
->stctxi
->teximage(gctx
->stctxi
, target
,
961 gsurf
->base
.MipmapLevel
, internal_format
,
962 gsurf
->render_texture
, gsurf
->base
.MipmapTexture
))
964 gsurf
->base
.BoundToTexture
= EGL_TRUE
;
971 egl_g3d_release_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
972 _EGLSurface
*surf
, EGLint buffer
)
974 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
976 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
||
977 !gsurf
->base
.BoundToTexture
)
978 return _eglError(EGL_BAD_SURFACE
, "eglReleaseTexImage");
979 if (buffer
!= EGL_BACK_BUFFER
)
980 return _eglError(EGL_BAD_PARAMETER
, "eglReleaseTexImage");
982 if (gsurf
->render_texture
) {
983 _EGLContext
*ctx
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
984 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
986 /* what if the context the surface binds to is no longer current? */
988 gctx
->stctxi
->teximage(gctx
->stctxi
, ST_TEXTURE_2D
,
989 gsurf
->base
.MipmapLevel
, PIPE_FORMAT_NONE
, NULL
, FALSE
);
993 gsurf
->base
.BoundToTexture
= EGL_FALSE
;
998 #ifdef EGL_MESA_screen_surface
1000 static _EGLSurface
*
1001 egl_g3d_create_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1002 _EGLConfig
*conf
, const EGLint
*attribs
)
1004 struct egl_g3d_create_surface_arg arg
;
1006 memset(&arg
, 0, sizeof(arg
));
1007 arg
.type
= EGL_SCREEN_BIT_MESA
;
1009 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
1013 egl_g3d_show_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1014 _EGLScreen
*scr
, _EGLSurface
*surf
,
1017 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
1018 struct egl_g3d_screen
*gscr
= egl_g3d_screen(scr
);
1019 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
1020 struct native_surface
*nsurf
;
1021 const struct native_mode
*nmode
;
1028 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
1029 if (gsurf
->base
.Type
!= EGL_SCREEN_BIT_MESA
)
1030 return _eglError(EGL_BAD_SURFACE
, "eglShowScreenSurfaceMESA");
1031 if (gsurf
->base
.Width
< mode
->Width
|| gsurf
->base
.Height
< mode
->Height
)
1032 return _eglError(EGL_BAD_MATCH
,
1033 "eglShowSurfaceMESA(surface smaller than mode size)");
1035 /* find the index of the mode */
1036 for (idx
= 0; idx
< gscr
->base
.NumModes
; idx
++)
1037 if (mode
== &gscr
->base
.Modes
[idx
])
1039 if (idx
>= gscr
->base
.NumModes
) {
1040 return _eglError(EGL_BAD_MODE_MESA
,
1041 "eglShowSurfaceMESA(unknown mode)");
1044 nsurf
= gsurf
->native
;
1045 nmode
= gscr
->native_modes
[idx
];
1049 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
1051 /* disable the screen */
1056 /* TODO surface panning by CRTC choosing */
1057 changed
= gdpy
->native
->modeset
->program(gdpy
->native
, 0, nsurf
,
1058 gscr
->base
.OriginX
, gscr
->base
.OriginY
, &gscr
->native
, 1, nmode
);
1060 gscr
->base
.CurrentSurface
= &gsurf
->base
;
1061 gscr
->base
.CurrentMode
= mode
;
1067 #endif /* EGL_MESA_screen_surface */
1070 egl_g3d_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1072 struct native_probe
*nprobe
;
1073 enum native_probe_result res
;
1076 nprobe
= egl_g3d_get_probe(drv
, dpy
);
1077 res
= native_get_probe_result(nprobe
);
1080 case NATIVE_PROBE_UNKNOWN
:
1084 case NATIVE_PROBE_FALLBACK
:
1087 case NATIVE_PROBE_SUPPORTED
:
1090 case NATIVE_PROBE_EXACT
:
1099 egl_g3d_unload(_EGLDriver
*drv
)
1101 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
1104 for (i
= 0; i
< ST_API_COUNT
; i
++) {
1105 if (gdrv
->stapis
[i
])
1106 gdrv
->stapis
[i
]->destroy(gdrv
->stapis
[i
]);
1109 egl_g3d_destroy_probe(drv
, NULL
);
1114 _eglMain(const char *args
)
1116 static char driver_name
[64];
1117 struct egl_g3d_driver
*gdrv
;
1119 snprintf(driver_name
, sizeof(driver_name
),
1120 "Gallium/%s", native_get_name());
1122 gdrv
= CALLOC_STRUCT(egl_g3d_driver
);
1126 _eglInitDriverFallbacks(&gdrv
->base
);
1128 gdrv
->base
.API
.Initialize
= egl_g3d_initialize
;
1129 gdrv
->base
.API
.Terminate
= egl_g3d_terminate
;
1130 gdrv
->base
.API
.CreateContext
= egl_g3d_create_context
;
1131 gdrv
->base
.API
.DestroyContext
= egl_g3d_destroy_context
;
1132 gdrv
->base
.API
.CreateWindowSurface
= egl_g3d_create_window_surface
;
1133 gdrv
->base
.API
.CreatePixmapSurface
= egl_g3d_create_pixmap_surface
;
1134 gdrv
->base
.API
.CreatePbufferSurface
= egl_g3d_create_pbuffer_surface
;
1135 gdrv
->base
.API
.DestroySurface
= egl_g3d_destroy_surface
;
1136 gdrv
->base
.API
.MakeCurrent
= egl_g3d_make_current
;
1137 gdrv
->base
.API
.SwapBuffers
= egl_g3d_swap_buffers
;
1138 gdrv
->base
.API
.CopyBuffers
= egl_g3d_copy_buffers
;
1139 gdrv
->base
.API
.WaitClient
= egl_g3d_wait_client
;
1140 gdrv
->base
.API
.WaitNative
= egl_g3d_wait_native
;
1141 gdrv
->base
.API
.GetProcAddress
= egl_g3d_get_proc_address
;
1143 gdrv
->base
.API
.BindTexImage
= egl_g3d_bind_tex_image
;
1144 gdrv
->base
.API
.ReleaseTexImage
= egl_g3d_release_tex_image
;
1146 gdrv
->base
.API
.CreateImageKHR
= egl_g3d_create_image
;
1147 gdrv
->base
.API
.DestroyImageKHR
= egl_g3d_destroy_image
;
1149 #ifdef EGL_MESA_screen_surface
1150 gdrv
->base
.API
.CreateScreenSurfaceMESA
= egl_g3d_create_screen_surface
;
1151 gdrv
->base
.API
.ShowScreenSurfaceMESA
= egl_g3d_show_screen_surface
;
1154 gdrv
->base
.Name
= driver_name
;
1155 gdrv
->base
.Probe
= egl_g3d_probe
;
1156 gdrv
->base
.Unload
= egl_g3d_unload
;
1158 /* the key is " EGL G3D" */
1159 gdrv
->probe_key
= 0x0E61063D;