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.
29 #include "pipe/p_screen.h"
30 #include "util/u_memory.h"
31 #include "util/u_rect.h"
32 #include "util/u_inlines.h"
33 #include "egldriver.h"
34 #include "eglcurrent.h"
35 #include "eglconfigutil.h"
40 #include "egl_g3d_st.h"
41 #include "egl_g3d_image.h"
44 * Return the state tracker for the given context.
46 static struct st_api
*
47 egl_g3d_choose_st(_EGLDriver
*drv
, _EGLContext
*ctx
)
49 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
53 switch (ctx
->ClientAPI
) {
54 case EGL_OPENGL_ES_API
:
55 switch (ctx
->ClientVersion
) {
57 idx
= ST_API_OPENGL_ES1
;
60 idx
= ST_API_OPENGL_ES2
;
63 _eglLog(_EGL_WARNING
, "unknown client version %d",
75 _eglLog(_EGL_WARNING
, "unknown client API 0x%04x", ctx
->ClientAPI
);
79 stapi
= (idx
>= 0) ? gdrv
->stapis
[idx
] : NULL
;
84 * Initialize the state trackers.
87 egl_g3d_init_st(_EGLDriver
*drv
)
89 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
92 /* already initialized */
96 for (i
= 0; i
< ST_API_COUNT
; i
++) {
97 gdrv
->stapis
[i
] = egl_g3d_create_st_api(i
);
99 gdrv
->api_mask
|= egl_g3d_st_api_bit(i
);
103 _eglLog(_EGL_DEBUG
, "Driver API mask: 0x%x", gdrv
->api_mask
);
105 _eglLog(_EGL_WARNING
, "No supported client API");
109 * Get the probe object of the display.
111 * Note that this function may be called before the display is initialized.
113 static struct native_probe
*
114 egl_g3d_get_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
116 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
117 struct native_probe
*nprobe
;
119 nprobe
= (struct native_probe
*) _eglGetProbeCache(gdrv
->probe_key
);
120 if (!nprobe
|| nprobe
->display
!= dpy
->NativeDisplay
) {
122 nprobe
->destroy(nprobe
);
123 nprobe
= native_create_probe(dpy
->NativeDisplay
);
124 _eglSetProbeCache(gdrv
->probe_key
, (void *) nprobe
);
131 * Destroy the probe object of the display. The display may be NULL.
133 * Note that this function may be called before the display is initialized.
136 egl_g3d_destroy_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
138 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
139 struct native_probe
*nprobe
;
141 nprobe
= (struct native_probe
*) _eglGetProbeCache(gdrv
->probe_key
);
142 if (nprobe
&& (!dpy
|| nprobe
->display
== dpy
->NativeDisplay
)) {
143 nprobe
->destroy(nprobe
);
144 _eglSetProbeCache(gdrv
->probe_key
, NULL
);
148 #ifdef EGL_MESA_screen_surface
151 egl_g3d_add_screens(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
153 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
154 const struct native_connector
**native_connectors
;
155 EGLint num_connectors
, i
;
158 gdpy
->native
->modeset
->get_connectors(gdpy
->native
, &num_connectors
, NULL
);
159 if (!num_connectors
) {
160 if (native_connectors
)
161 free(native_connectors
);
165 for (i
= 0; i
< num_connectors
; i
++) {
166 const struct native_connector
*nconn
= native_connectors
[i
];
167 struct egl_g3d_screen
*gscr
;
168 const struct native_mode
**native_modes
;
171 /* TODO support for hotplug */
173 gdpy
->native
->modeset
->get_modes(gdpy
->native
, nconn
, &num_modes
);
180 gscr
= CALLOC_STRUCT(egl_g3d_screen
);
186 _eglInitScreen(&gscr
->base
);
188 for (j
= 0; j
< num_modes
; j
++) {
189 const struct native_mode
*nmode
= native_modes
[j
];
192 mode
= _eglAddNewMode(&gscr
->base
, nmode
->width
, nmode
->height
,
193 nmode
->refresh_rate
, nmode
->desc
);
196 /* gscr->native_modes and gscr->base.Modes should be consistent */
197 assert(mode
== &gscr
->base
.Modes
[j
]);
200 gscr
->native
= nconn
;
201 gscr
->native_modes
= native_modes
;
203 _eglAddScreen(dpy
, &gscr
->base
);
206 free(native_connectors
);
209 #endif /* EGL_MESA_screen_surface */
212 * Initialize an EGL config from the native config.
215 egl_g3d_init_config(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
216 _EGLConfig
*conf
, const struct native_config
*nconf
)
218 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
219 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
220 const __GLcontextModes
*mode
= &nconf
->mode
;
221 EGLint buffer_mask
, api_mask
;
225 buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
226 if (mode
->doubleBufferMode
)
227 buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
228 if (mode
->stereoMode
) {
229 buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
230 if (mode
->doubleBufferMode
)
231 buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
234 gconf
->stvis
.buffer_mask
= buffer_mask
;
235 gconf
->stvis
.color_format
= nconf
->color_format
;
236 gconf
->stvis
.depth_stencil_format
= nconf
->depth_format
;
237 gconf
->stvis
.accum_format
= PIPE_FORMAT_NONE
;
238 gconf
->stvis
.samples
= 0;
240 gconf
->stvis
.render_buffer
= (buffer_mask
& ST_ATTACHMENT_BACK_LEFT_MASK
) ?
241 ST_ATTACHMENT_BACK_LEFT
: ST_ATTACHMENT_FRONT_LEFT
;
244 for (i
= 0; i
< ST_API_COUNT
; i
++) {
245 struct st_api
*stapi
= gdrv
->stapis
[i
];
247 if (stapi
->is_visual_supported(stapi
, &gconf
->stvis
))
248 api_mask
|= egl_g3d_st_api_bit(i
);
251 /* this is required by EGL, not by OpenGL ES */
252 if ((mode
->drawableType
& GLX_WINDOW_BIT
) && !mode
->doubleBufferMode
)
253 api_mask
&= ~(EGL_OPENGL_ES_BIT
| EGL_OPENGL_ES2_BIT
);
256 _eglLog(_EGL_DEBUG
, "no state tracker supports config 0x%x",
260 valid
= _eglConfigFromContextModesRec(&gconf
->base
,
261 mode
, api_mask
, api_mask
);
263 #ifdef EGL_MESA_screen_surface
264 /* check if scanout surface bit is set */
265 if (nconf
->scanout_bit
) {
266 EGLint val
= GET_CONFIG_ATTRIB(&gconf
->base
, EGL_SURFACE_TYPE
);
267 val
|= EGL_SCREEN_BIT_MESA
;
268 SET_CONFIG_ATTRIB(&gconf
->base
, EGL_SURFACE_TYPE
, val
);
271 valid
= _eglValidateConfig(&gconf
->base
, EGL_FALSE
);
274 _eglLog(_EGL_DEBUG
, "skip invalid config 0x%x", mode
->visualID
);
278 gconf
->native
= nconf
;
284 * Add configs to display and return the next config ID.
287 egl_g3d_add_configs(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint id
)
289 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
290 const struct native_config
**native_configs
;
293 native_configs
= gdpy
->native
->get_configs(gdpy
->native
, &num_configs
);
296 free(native_configs
);
300 for (i
= 0; i
< num_configs
; i
++) {
301 struct egl_g3d_config
*gconf
;
303 gconf
= CALLOC_STRUCT(egl_g3d_config
);
305 _eglInitConfig(&gconf
->base
, dpy
, id
);
306 if (!egl_g3d_init_config(drv
, dpy
, &gconf
->base
, native_configs
[i
])) {
311 _eglAddConfig(dpy
, &gconf
->base
);
316 free(native_configs
);
321 egl_g3d_invalid_surface(struct native_display
*ndpy
,
322 struct native_surface
*nsurf
,
323 unsigned int seq_num
)
325 /* XXX not thread safe? */
326 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(nsurf
->user_data
);
327 struct egl_g3d_context
*gctx
;
330 * Some functions such as egl_g3d_copy_buffers create a temporary native
331 * surface. There is no gsurf associated with it.
333 gctx
= (gsurf
) ? egl_g3d_context(gsurf
->base
.CurrentContext
) : NULL
;
335 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
, gsurf
->stfbi
);
338 static struct native_event_handler egl_g3d_native_event_handler
= {
339 .invalid_surface
= egl_g3d_invalid_surface
343 egl_g3d_terminate(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
345 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
348 _eglReleaseDisplayResources(drv
, dpy
);
349 _eglCleanupDisplay(dpy
);
352 gdpy
->pipe
->destroy(gdpy
->pipe
);
355 for (i
= 0; i
< dpy
->NumScreens
; i
++) {
356 struct egl_g3d_screen
*gscr
= egl_g3d_screen(dpy
->Screens
[i
]);
357 free(gscr
->native_modes
);
364 egl_g3d_destroy_st_manager(gdpy
->smapi
);
367 gdpy
->native
->destroy(gdpy
->native
);
370 dpy
->DriverData
= NULL
;
376 egl_g3d_initialize(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
377 EGLint
*major
, EGLint
*minor
)
379 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
380 struct egl_g3d_display
*gdpy
;
382 /* the probe object is unlikely to be needed again */
383 egl_g3d_destroy_probe(drv
, dpy
);
385 gdpy
= CALLOC_STRUCT(egl_g3d_display
);
387 _eglError(EGL_BAD_ALLOC
, "eglInitialize");
390 dpy
->DriverData
= gdpy
;
392 gdpy
->native
= native_create_display(dpy
->NativeDisplay
,
393 &egl_g3d_native_event_handler
);
395 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(no usable display)");
399 gdpy
->native
->user_data
= (void *) dpy
;
401 egl_g3d_init_st(&gdrv
->base
);
402 dpy
->ClientAPIsMask
= gdrv
->api_mask
;
404 gdpy
->smapi
= egl_g3d_create_st_manager(dpy
);
406 _eglError(EGL_NOT_INITIALIZED
,
407 "eglInitialize(failed to create st manager)");
411 #ifdef EGL_MESA_screen_surface
412 /* enable MESA_screen_surface before adding (and validating) configs */
413 if (gdpy
->native
->modeset
) {
414 dpy
->Extensions
.MESA_screen_surface
= EGL_TRUE
;
415 egl_g3d_add_screens(drv
, dpy
);
419 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
420 if (gdpy
->native
->get_param(gdpy
->native
, NATIVE_PARAM_USE_NATIVE_BUFFER
))
421 dpy
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
423 if (egl_g3d_add_configs(drv
, dpy
, 1) == 1) {
424 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(unable to add configs)");
435 egl_g3d_terminate(drv
, dpy
);
440 egl_g3d_create_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
441 _EGLContext
*share
, const EGLint
*attribs
)
443 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
444 struct egl_g3d_context
*gshare
= egl_g3d_context(share
);
445 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
446 struct egl_g3d_context
*gctx
;
448 gctx
= CALLOC_STRUCT(egl_g3d_context
);
450 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
454 if (!_eglInitContext(&gctx
->base
, dpy
, conf
, attribs
)) {
459 gctx
->stapi
= egl_g3d_choose_st(drv
, &gctx
->base
);
465 gctx
->stctxi
= gctx
->stapi
->create_context(gctx
->stapi
, gdpy
->smapi
,
466 &gconf
->stvis
, (gshare
) ? gshare
->stctxi
: NULL
);
472 gctx
->stctxi
->st_manager_private
= (void *) &gctx
->base
;
481 destroy_context(_EGLDisplay
*dpy
, _EGLContext
*ctx
)
483 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
485 /* FIXME a context might live longer than its display */
486 if (!dpy
->Initialized
)
487 _eglLog(_EGL_FATAL
, "destroy a context with an unitialized display");
489 gctx
->stctxi
->destroy(gctx
->stctxi
);
495 egl_g3d_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
497 if (!_eglIsContextBound(ctx
))
498 destroy_context(dpy
, ctx
);
502 struct egl_g3d_create_surface_arg
{
505 EGLNativeWindowType win
;
506 EGLNativePixmapType pix
;
511 egl_g3d_create_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
512 struct egl_g3d_create_surface_arg
*arg
,
513 const EGLint
*attribs
)
515 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
516 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
517 struct egl_g3d_surface
*gsurf
;
518 struct native_surface
*nsurf
;
523 err
= "eglCreateWindowSurface";
526 err
= "eglCreatePixmapSurface";
528 #ifdef EGL_MESA_screen_surface
529 case EGL_SCREEN_BIT_MESA
:
530 err
= "eglCreateScreenSurface";
534 err
= "eglCreateUnknownSurface";
538 gsurf
= CALLOC_STRUCT(egl_g3d_surface
);
540 _eglError(EGL_BAD_ALLOC
, err
);
544 if (!_eglInitSurface(&gsurf
->base
, dpy
, arg
->type
, conf
, attribs
)) {
549 /* create the native surface */
552 nsurf
= gdpy
->native
->create_window_surface(gdpy
->native
,
553 arg
->u
.win
, gconf
->native
);
556 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
557 arg
->u
.pix
, gconf
->native
);
559 #ifdef EGL_MESA_screen_surface
560 case EGL_SCREEN_BIT_MESA
:
561 /* prefer back buffer (move to _eglInitSurface?) */
562 gsurf
->base
.RenderBuffer
= EGL_BACK_BUFFER
;
563 nsurf
= gdpy
->native
->modeset
->create_scanout_surface(gdpy
->native
,
564 gconf
->native
, gsurf
->base
.Width
, gsurf
->base
.Height
);
576 /* initialize the geometry */
577 if (!nsurf
->validate(nsurf
, 0x0, &gsurf
->sequence_number
, NULL
,
578 &gsurf
->base
.Width
, &gsurf
->base
.Height
)) {
579 nsurf
->destroy(nsurf
);
584 gsurf
->stvis
= gconf
->stvis
;
585 if (gsurf
->base
.RenderBuffer
== EGL_SINGLE_BUFFER
)
586 gsurf
->stvis
.render_buffer
= ST_ATTACHMENT_FRONT_LEFT
;
588 gsurf
->stfbi
= egl_g3d_create_st_framebuffer(&gsurf
->base
);
590 nsurf
->destroy(nsurf
);
595 nsurf
->user_data
= &gsurf
->base
;
596 gsurf
->native
= nsurf
;
602 egl_g3d_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
603 _EGLConfig
*conf
, EGLNativeWindowType win
,
604 const EGLint
*attribs
)
606 struct egl_g3d_create_surface_arg arg
;
608 memset(&arg
, 0, sizeof(arg
));
609 arg
.type
= EGL_WINDOW_BIT
;
612 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
616 egl_g3d_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
617 _EGLConfig
*conf
, EGLNativePixmapType pix
,
618 const EGLint
*attribs
)
620 struct egl_g3d_create_surface_arg arg
;
622 memset(&arg
, 0, sizeof(arg
));
623 arg
.type
= EGL_PIXMAP_BIT
;
626 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
630 egl_g3d_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
631 _EGLConfig
*conf
, const EGLint
*attribs
)
633 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
634 struct egl_g3d_surface
*gsurf
;
636 gsurf
= CALLOC_STRUCT(egl_g3d_surface
);
638 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
642 if (!_eglInitSurface(&gsurf
->base
, dpy
, EGL_PBUFFER_BIT
, conf
, attribs
)) {
647 gsurf
->stvis
= gconf
->stvis
;
649 gsurf
->stfbi
= egl_g3d_create_st_framebuffer(&gsurf
->base
);
662 destroy_surface(_EGLDisplay
*dpy
, _EGLSurface
*surf
)
664 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
666 /* FIXME a surface might live longer than its display */
667 if (!dpy
->Initialized
)
668 _eglLog(_EGL_FATAL
, "destroy a surface with an unitialized display");
670 pipe_resource_reference(&gsurf
->render_texture
, NULL
);
671 egl_g3d_destroy_st_framebuffer(gsurf
->stfbi
);
673 gsurf
->native
->destroy(gsurf
->native
);
678 egl_g3d_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
680 if (!_eglIsSurfaceBound(surf
))
681 destroy_surface(dpy
, surf
);
686 egl_g3d_make_current(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
687 _EGLSurface
*draw
, _EGLSurface
*read
, _EGLContext
*ctx
)
689 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
690 struct egl_g3d_surface
*gdraw
= egl_g3d_surface(draw
);
691 struct egl_g3d_surface
*gread
= egl_g3d_surface(read
);
692 struct egl_g3d_context
*old_gctx
;
693 EGLBoolean ok
= EGL_TRUE
;
695 /* bind the new context and return the "orphaned" one */
696 if (!_eglBindContext(&ctx
, &draw
, &read
))
698 old_gctx
= egl_g3d_context(ctx
);
701 /* flush old context */
702 old_gctx
->stctxi
->flush(old_gctx
->stctxi
,
703 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
707 ok
= gctx
->stapi
->make_current(gctx
->stapi
, gctx
->stctxi
,
708 (gdraw
) ? gdraw
->stfbi
: NULL
, (gread
) ? gread
->stfbi
: NULL
);
710 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
, gdraw
->stfbi
);
711 if (gread
!= gdraw
) {
712 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
,
716 if (gdraw
->base
.Type
== EGL_WINDOW_BIT
) {
717 gctx
->base
.WindowRenderBuffer
=
718 (gdraw
->stvis
.render_buffer
== ST_ATTACHMENT_FRONT_LEFT
) ?
719 EGL_SINGLE_BUFFER
: EGL_BACK_BUFFER
;
724 ok
= old_gctx
->stapi
->make_current(old_gctx
->stapi
, NULL
, NULL
, NULL
);
725 old_gctx
->base
.WindowRenderBuffer
= EGL_NONE
;
728 if (ctx
&& !_eglIsContextLinked(ctx
))
729 destroy_context(dpy
, ctx
);
730 if (draw
&& !_eglIsSurfaceLinked(draw
))
731 destroy_surface(dpy
, draw
);
732 if (read
&& read
!= draw
&& !_eglIsSurfaceLinked(read
))
733 destroy_surface(dpy
, read
);
739 egl_g3d_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
741 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
742 _EGLContext
*ctx
= _eglGetCurrentContext();
743 struct egl_g3d_context
*gctx
= NULL
;
745 /* no-op for pixmap or pbuffer surface */
746 if (gsurf
->base
.Type
== EGL_PIXMAP_BIT
||
747 gsurf
->base
.Type
== EGL_PBUFFER_BIT
)
750 /* or when the surface is single-buffered */
751 if (gsurf
->stvis
.render_buffer
== ST_ATTACHMENT_FRONT_LEFT
)
754 if (ctx
&& ctx
->DrawSurface
== surf
)
755 gctx
= egl_g3d_context(ctx
);
757 /* flush if the surface is current */
759 gctx
->stctxi
->flush(gctx
->stctxi
,
760 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
763 return gsurf
->native
->swap_buffers(gsurf
->native
);
767 * Find a config that supports the pixmap.
770 egl_g3d_find_pixmap_config(_EGLDisplay
*dpy
, EGLNativePixmapType pix
)
772 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
773 struct egl_g3d_config
*gconf
;
776 for (i
= 0; i
< dpy
->NumConfigs
; i
++) {
777 gconf
= egl_g3d_config(dpy
->Configs
[i
]);
778 if (gdpy
->native
->is_pixmap_supported(gdpy
->native
, pix
, gconf
->native
))
782 return (i
< dpy
->NumConfigs
) ? &gconf
->base
: NULL
;
786 * Get the pipe surface of the given attachment of the native surface.
788 static struct pipe_surface
*
789 get_pipe_surface(struct native_display
*ndpy
, struct native_surface
*nsurf
,
790 enum native_attachment natt
,
793 struct pipe_resource
*textures
[NUM_NATIVE_ATTACHMENTS
];
794 struct pipe_surface
*psurf
;
796 textures
[natt
] = NULL
;
797 nsurf
->validate(nsurf
, 1 << natt
, NULL
, textures
, NULL
, NULL
);
801 psurf
= ndpy
->screen
->get_tex_surface(ndpy
->screen
, textures
[natt
],
803 pipe_resource_reference(&textures
[natt
], NULL
);
809 egl_g3d_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
810 EGLNativePixmapType target
)
812 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
813 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
814 _EGLContext
*ctx
= _eglGetCurrentContext();
815 struct egl_g3d_config
*gconf
;
816 struct native_surface
*nsurf
;
817 struct pipe_screen
*screen
= gdpy
->native
->screen
;
818 struct pipe_surface
*psurf
;
820 if (!gsurf
->render_texture
)
823 gconf
= egl_g3d_config(egl_g3d_find_pixmap_config(dpy
, target
));
825 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
827 nsurf
= gdpy
->native
->create_pixmap_surface(gdpy
->native
,
828 target
, gconf
->native
);
830 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "eglCopyBuffers");
832 /* flush if the surface is current */
833 if (ctx
&& ctx
->DrawSurface
== &gsurf
->base
) {
834 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
835 gctx
->stctxi
->flush(gctx
->stctxi
,
836 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
839 /* create a pipe context to copy surfaces */
842 gdpy
->native
->screen
->context_create(gdpy
->native
->screen
, NULL
);
847 psurf
= get_pipe_surface(gdpy
->native
, nsurf
, NATIVE_ATTACHMENT_FRONT_LEFT
,
848 PIPE_BIND_BLIT_DESTINATION
);
850 struct pipe_surface
*psrc
;
852 psrc
= screen
->get_tex_surface(screen
, gsurf
->render_texture
,
853 0, 0, 0, PIPE_BIND_BLIT_SOURCE
);
855 gdpy
->pipe
->surface_copy(gdpy
->pipe
, psurf
, 0, 0,
856 psrc
, 0, 0, psurf
->width
, psurf
->height
);
857 pipe_surface_reference(&psrc
, NULL
);
859 nsurf
->flush_frontbuffer(nsurf
);
862 pipe_surface_reference(&psurf
, NULL
);
865 nsurf
->destroy(nsurf
);
871 egl_g3d_wait_client(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
873 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
874 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
875 struct pipe_screen
*screen
= gdpy
->native
->screen
;
876 struct pipe_fence_handle
*fence
= NULL
;
878 gctx
->stctxi
->flush(gctx
->stctxi
,
879 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, &fence
);
880 screen
->fence_finish(screen
, fence
, 0);
881 screen
->fence_reference(screen
, &fence
, NULL
);
887 egl_g3d_wait_native(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint engine
)
889 _EGLContext
*ctx
= _eglGetCurrentContext();
891 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
892 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
894 if (ctx
&& ctx
->DrawSurface
) {
895 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(ctx
->DrawSurface
);
898 gsurf
->native
->wait(gsurf
->native
);
905 egl_g3d_get_proc_address(_EGLDriver
*drv
, const char *procname
)
907 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
911 /* in case this is called before a display is initialized */
912 egl_g3d_init_st(&gdrv
->base
);
914 for (i
= 0; i
< ST_API_COUNT
; i
++) {
915 struct st_api
*stapi
= gdrv
->stapis
[i
];
917 proc
= (_EGLProc
) stapi
->get_proc_address(stapi
, procname
);
923 return (_EGLProc
) NULL
;
927 egl_g3d_bind_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
928 _EGLSurface
*surf
, EGLint buffer
)
930 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
931 _EGLContext
*es1
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
932 struct egl_g3d_context
*gctx
;
933 enum pipe_format internal_format
;
934 enum st_texture_type target
;
936 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
)
937 return _eglError(EGL_BAD_SURFACE
, "eglBindTexImage");
938 if (buffer
!= EGL_BACK_BUFFER
)
939 return _eglError(EGL_BAD_PARAMETER
, "eglBindTexImage");
940 if (gsurf
->base
.BoundToTexture
)
941 return _eglError(EGL_BAD_ACCESS
, "eglBindTexImage");
943 switch (gsurf
->base
.TextureFormat
) {
944 case EGL_TEXTURE_RGB
:
945 internal_format
= PIPE_FORMAT_R8G8B8_UNORM
;
947 case EGL_TEXTURE_RGBA
:
948 internal_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
951 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
954 switch (gsurf
->base
.TextureTarget
) {
956 target
= ST_TEXTURE_2D
;
959 return _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
964 if (!gsurf
->render_texture
)
967 /* flush properly if the surface is bound */
968 if (gsurf
->base
.CurrentContext
) {
969 gctx
= egl_g3d_context(gsurf
->base
.CurrentContext
);
970 gctx
->stctxi
->flush(gctx
->stctxi
,
971 PIPE_FLUSH_RENDER_CACHE
| PIPE_FLUSH_FRAME
, NULL
);
974 gctx
= egl_g3d_context(es1
);
975 if (gctx
->stctxi
->teximage
) {
976 if (!gctx
->stctxi
->teximage(gctx
->stctxi
, target
,
977 gsurf
->base
.MipmapLevel
, internal_format
,
978 gsurf
->render_texture
, gsurf
->base
.MipmapTexture
))
980 gsurf
->base
.BoundToTexture
= EGL_TRUE
;
987 egl_g3d_release_tex_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
988 _EGLSurface
*surf
, EGLint buffer
)
990 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
992 if (!gsurf
|| gsurf
->base
.Type
!= EGL_PBUFFER_BIT
||
993 !gsurf
->base
.BoundToTexture
)
994 return _eglError(EGL_BAD_SURFACE
, "eglReleaseTexImage");
995 if (buffer
!= EGL_BACK_BUFFER
)
996 return _eglError(EGL_BAD_PARAMETER
, "eglReleaseTexImage");
998 if (gsurf
->render_texture
) {
999 _EGLContext
*ctx
= _eglGetAPIContext(EGL_OPENGL_ES_API
);
1000 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
1002 /* what if the context the surface binds to is no longer current? */
1004 gctx
->stctxi
->teximage(gctx
->stctxi
, ST_TEXTURE_2D
,
1005 gsurf
->base
.MipmapLevel
, PIPE_FORMAT_NONE
, NULL
, FALSE
);
1009 gsurf
->base
.BoundToTexture
= EGL_FALSE
;
1014 #ifdef EGL_MESA_screen_surface
1016 static _EGLSurface
*
1017 egl_g3d_create_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1018 _EGLConfig
*conf
, const EGLint
*attribs
)
1020 struct egl_g3d_create_surface_arg arg
;
1022 memset(&arg
, 0, sizeof(arg
));
1023 arg
.type
= EGL_SCREEN_BIT_MESA
;
1025 return egl_g3d_create_surface(drv
, dpy
, conf
, &arg
, attribs
);
1029 egl_g3d_show_screen_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1030 _EGLScreen
*scr
, _EGLSurface
*surf
,
1033 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
1034 struct egl_g3d_screen
*gscr
= egl_g3d_screen(scr
);
1035 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(surf
);
1036 struct native_surface
*nsurf
;
1037 const struct native_mode
*nmode
;
1044 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
1045 if (gsurf
->base
.Type
!= EGL_SCREEN_BIT_MESA
)
1046 return _eglError(EGL_BAD_SURFACE
, "eglShowScreenSurfaceMESA");
1047 if (gsurf
->base
.Width
< mode
->Width
|| gsurf
->base
.Height
< mode
->Height
)
1048 return _eglError(EGL_BAD_MATCH
,
1049 "eglShowSurfaceMESA(surface smaller than mode size)");
1051 /* find the index of the mode */
1052 for (idx
= 0; idx
< gscr
->base
.NumModes
; idx
++)
1053 if (mode
== &gscr
->base
.Modes
[idx
])
1055 if (idx
>= gscr
->base
.NumModes
) {
1056 return _eglError(EGL_BAD_MODE_MESA
,
1057 "eglShowSurfaceMESA(unknown mode)");
1060 nsurf
= gsurf
->native
;
1061 nmode
= gscr
->native_modes
[idx
];
1065 return _eglError(EGL_BAD_MATCH
, "eglShowSurfaceMESA");
1067 /* disable the screen */
1072 /* TODO surface panning by CRTC choosing */
1073 changed
= gdpy
->native
->modeset
->program(gdpy
->native
, 0, nsurf
,
1074 gscr
->base
.OriginX
, gscr
->base
.OriginY
, &gscr
->native
, 1, nmode
);
1076 gscr
->base
.CurrentSurface
= &gsurf
->base
;
1077 gscr
->base
.CurrentMode
= mode
;
1083 #endif /* EGL_MESA_screen_surface */
1086 egl_g3d_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
1088 struct native_probe
*nprobe
;
1089 enum native_probe_result res
;
1092 nprobe
= egl_g3d_get_probe(drv
, dpy
);
1093 res
= native_get_probe_result(nprobe
);
1096 case NATIVE_PROBE_UNKNOWN
:
1100 case NATIVE_PROBE_FALLBACK
:
1103 case NATIVE_PROBE_SUPPORTED
:
1106 case NATIVE_PROBE_EXACT
:
1115 egl_g3d_unload(_EGLDriver
*drv
)
1117 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
1120 for (i
= 0; i
< ST_API_COUNT
; i
++) {
1121 if (gdrv
->stapis
[i
])
1122 gdrv
->stapis
[i
]->destroy(gdrv
->stapis
[i
]);
1125 egl_g3d_destroy_probe(drv
, NULL
);
1130 _eglMain(const char *args
)
1132 static char driver_name
[64];
1133 struct egl_g3d_driver
*gdrv
;
1135 snprintf(driver_name
, sizeof(driver_name
),
1136 "Gallium/%s", native_get_name());
1138 gdrv
= CALLOC_STRUCT(egl_g3d_driver
);
1142 _eglInitDriverFallbacks(&gdrv
->base
);
1144 gdrv
->base
.API
.Initialize
= egl_g3d_initialize
;
1145 gdrv
->base
.API
.Terminate
= egl_g3d_terminate
;
1146 gdrv
->base
.API
.CreateContext
= egl_g3d_create_context
;
1147 gdrv
->base
.API
.DestroyContext
= egl_g3d_destroy_context
;
1148 gdrv
->base
.API
.CreateWindowSurface
= egl_g3d_create_window_surface
;
1149 gdrv
->base
.API
.CreatePixmapSurface
= egl_g3d_create_pixmap_surface
;
1150 gdrv
->base
.API
.CreatePbufferSurface
= egl_g3d_create_pbuffer_surface
;
1151 gdrv
->base
.API
.DestroySurface
= egl_g3d_destroy_surface
;
1152 gdrv
->base
.API
.MakeCurrent
= egl_g3d_make_current
;
1153 gdrv
->base
.API
.SwapBuffers
= egl_g3d_swap_buffers
;
1154 gdrv
->base
.API
.CopyBuffers
= egl_g3d_copy_buffers
;
1155 gdrv
->base
.API
.WaitClient
= egl_g3d_wait_client
;
1156 gdrv
->base
.API
.WaitNative
= egl_g3d_wait_native
;
1157 gdrv
->base
.API
.GetProcAddress
= egl_g3d_get_proc_address
;
1159 gdrv
->base
.API
.BindTexImage
= egl_g3d_bind_tex_image
;
1160 gdrv
->base
.API
.ReleaseTexImage
= egl_g3d_release_tex_image
;
1162 gdrv
->base
.API
.CreateImageKHR
= egl_g3d_create_image
;
1163 gdrv
->base
.API
.DestroyImageKHR
= egl_g3d_destroy_image
;
1165 #ifdef EGL_MESA_screen_surface
1166 gdrv
->base
.API
.CreateScreenSurfaceMESA
= egl_g3d_create_screen_surface
;
1167 gdrv
->base
.API
.ShowScreenSurfaceMESA
= egl_g3d_show_screen_surface
;
1170 gdrv
->base
.Name
= driver_name
;
1171 gdrv
->base
.Probe
= egl_g3d_probe
;
1172 gdrv
->base
.Unload
= egl_g3d_unload
;
1174 /* the key is " EGL G3D" */
1175 gdrv
->probe_key
= 0x0E61063D;