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_format.h"
33 #include "util/u_string.h"
36 #include "egl_g3d_api.h"
37 #include "egl_g3d_st.h"
38 #include "egl_g3d_loader.h"
42 * Get the native platform.
44 static const struct native_platform
*
45 egl_g3d_get_platform(_EGLDriver
*drv
, _EGLPlatformType plat
)
47 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
49 if (!gdrv
->platforms
[plat
]) {
50 const char *plat_name
= NULL
;
51 const struct native_platform
*nplat
= NULL
;
54 case _EGL_PLATFORM_WINDOWS
:
55 plat_name
= "Windows";
56 #ifdef HAVE_GDI_BACKEND
57 nplat
= native_get_gdi_platform();
60 case _EGL_PLATFORM_X11
:
62 #ifdef HAVE_X11_BACKEND
63 nplat
= native_get_x11_platform();
66 case _EGL_PLATFORM_DRM
:
68 #ifdef HAVE_DRM_BACKEND
69 nplat
= native_get_drm_platform();
72 case _EGL_PLATFORM_FBDEV
:
74 #ifdef HAVE_FBDEV_BACKEND
75 nplat
= native_get_fbdev_platform();
83 _eglLog(_EGL_WARNING
, "unsupported platform %s", plat_name
);
85 gdrv
->platforms
[plat
] = nplat
;
88 return gdrv
->platforms
[plat
];
91 #ifdef EGL_MESA_screen_surface
94 egl_g3d_add_screens(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
96 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
97 const struct native_connector
**native_connectors
;
98 EGLint num_connectors
, i
;
101 gdpy
->native
->modeset
->get_connectors(gdpy
->native
, &num_connectors
, NULL
);
102 if (!num_connectors
) {
103 if (native_connectors
)
104 FREE(native_connectors
);
108 for (i
= 0; i
< num_connectors
; i
++) {
109 const struct native_connector
*nconn
= native_connectors
[i
];
110 struct egl_g3d_screen
*gscr
;
111 const struct native_mode
**native_modes
;
114 /* TODO support for hotplug */
116 gdpy
->native
->modeset
->get_modes(gdpy
->native
, nconn
, &num_modes
);
123 gscr
= CALLOC_STRUCT(egl_g3d_screen
);
129 _eglInitScreen(&gscr
->base
, dpy
, num_modes
);
130 for (j
= 0; j
< gscr
->base
.NumModes
; j
++) {
131 const struct native_mode
*nmode
= native_modes
[j
];
132 _EGLMode
*mode
= &gscr
->base
.Modes
[j
];
134 mode
->Width
= nmode
->width
;
135 mode
->Height
= nmode
->height
;
136 mode
->RefreshRate
= nmode
->refresh_rate
;
137 mode
->Optimal
= EGL_FALSE
;
138 mode
->Interlaced
= EGL_FALSE
;
139 /* no need to strdup() */
140 mode
->Name
= nmode
->desc
;
143 gscr
->native
= nconn
;
144 gscr
->native_modes
= native_modes
;
146 _eglLinkScreen(&gscr
->base
);
149 FREE(native_connectors
);
152 #endif /* EGL_MESA_screen_surface */
155 * Initialize and validate the EGL config attributes.
158 init_config_attributes(_EGLConfig
*conf
, const struct native_config
*nconf
,
159 EGLint api_mask
, enum pipe_format depth_stencil_format
,
160 EGLBoolean preserve_buffer
, EGLint max_swap_interval
)
162 uint rgba
[4], depth_stencil
[2], buffer_size
;
166 /* get the color and depth/stencil component sizes */
167 assert(nconf
->color_format
!= PIPE_FORMAT_NONE
);
169 for (i
= 0; i
< 4; i
++) {
170 rgba
[i
] = util_format_get_component_bits(nconf
->color_format
,
171 UTIL_FORMAT_COLORSPACE_RGB
, i
);
172 buffer_size
+= rgba
[i
];
174 for (i
= 0; i
< 2; i
++) {
175 if (depth_stencil_format
!= PIPE_FORMAT_NONE
) {
177 util_format_get_component_bits(depth_stencil_format
,
178 UTIL_FORMAT_COLORSPACE_ZS
, i
);
181 depth_stencil
[i
] = 0;
186 if (nconf
->window_bit
)
187 surface_type
|= EGL_WINDOW_BIT
;
188 if (nconf
->pixmap_bit
)
189 surface_type
|= EGL_PIXMAP_BIT
;
190 #ifdef EGL_MESA_screen_surface
191 if (nconf
->scanout_bit
)
192 surface_type
|= EGL_SCREEN_BIT_MESA
;
195 if (nconf
->buffer_mask
& (1 << NATIVE_ATTACHMENT_BACK_LEFT
))
196 surface_type
|= EGL_PBUFFER_BIT
;
198 conf
->Conformant
= api_mask
;
199 conf
->RenderableType
= api_mask
;
201 conf
->RedSize
= rgba
[0];
202 conf
->GreenSize
= rgba
[1];
203 conf
->BlueSize
= rgba
[2];
204 conf
->AlphaSize
= rgba
[3];
205 conf
->BufferSize
= buffer_size
;
207 conf
->DepthSize
= depth_stencil
[0];
208 conf
->StencilSize
= depth_stencil
[1];
210 conf
->SurfaceType
= surface_type
;
212 conf
->NativeRenderable
= EGL_TRUE
;
213 if (surface_type
& EGL_WINDOW_BIT
) {
214 conf
->NativeVisualID
= nconf
->native_visual_id
;
215 conf
->NativeVisualType
= nconf
->native_visual_type
;
218 if (surface_type
& EGL_PBUFFER_BIT
) {
219 conf
->BindToTextureRGB
= EGL_TRUE
;
221 conf
->BindToTextureRGBA
= EGL_TRUE
;
223 conf
->MaxPbufferWidth
= 4096;
224 conf
->MaxPbufferHeight
= 4096;
225 conf
->MaxPbufferPixels
= 4096 * 4096;
228 conf
->Level
= nconf
->level
;
229 conf
->Samples
= nconf
->samples
;
230 conf
->SampleBuffers
= 0;
232 if (nconf
->slow_config
)
233 conf
->ConfigCaveat
= EGL_SLOW_CONFIG
;
235 if (nconf
->transparent_rgb
) {
236 conf
->TransparentType
= EGL_TRANSPARENT_RGB
;
237 conf
->TransparentRedValue
= nconf
->transparent_rgb_values
[0];
238 conf
->TransparentGreenValue
= nconf
->transparent_rgb_values
[1];
239 conf
->TransparentBlueValue
= nconf
->transparent_rgb_values
[2];
242 conf
->MinSwapInterval
= 0;
243 conf
->MaxSwapInterval
= max_swap_interval
;
245 conf
->SurfaceType
|= EGL_SWAP_BEHAVIOR_PRESERVED_BIT
;
247 return _eglValidateConfig(conf
, EGL_FALSE
);
251 * Initialize an EGL config from the native config.
254 egl_g3d_init_config(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
255 _EGLConfig
*conf
, const struct native_config
*nconf
,
256 enum pipe_format depth_stencil_format
,
257 int preserve_buffer
, int max_swap_interval
)
259 struct egl_g3d_config
*gconf
= egl_g3d_config(conf
);
260 EGLint buffer_mask
, api_mask
;
263 /* skip single-buffered configs */
264 if (!(nconf
->buffer_mask
& (1 << NATIVE_ATTACHMENT_BACK_LEFT
)))
268 if (nconf
->buffer_mask
& (1 << NATIVE_ATTACHMENT_FRONT_LEFT
))
269 buffer_mask
|= ST_ATTACHMENT_FRONT_LEFT_MASK
;
270 if (nconf
->buffer_mask
& (1 << NATIVE_ATTACHMENT_BACK_LEFT
))
271 buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
272 if (nconf
->buffer_mask
& (1 << NATIVE_ATTACHMENT_FRONT_RIGHT
))
273 buffer_mask
|= ST_ATTACHMENT_FRONT_RIGHT_MASK
;
274 if (nconf
->buffer_mask
& (1 << NATIVE_ATTACHMENT_BACK_RIGHT
))
275 buffer_mask
|= ST_ATTACHMENT_BACK_RIGHT_MASK
;
277 gconf
->stvis
.buffer_mask
= buffer_mask
;
278 gconf
->stvis
.color_format
= nconf
->color_format
;
279 gconf
->stvis
.depth_stencil_format
= depth_stencil_format
;
280 gconf
->stvis
.accum_format
= PIPE_FORMAT_NONE
;
281 gconf
->stvis
.samples
= nconf
->samples
;
283 gconf
->stvis
.render_buffer
= (buffer_mask
& ST_ATTACHMENT_BACK_LEFT_MASK
) ?
284 ST_ATTACHMENT_BACK_LEFT
: ST_ATTACHMENT_FRONT_LEFT
;
286 api_mask
= dpy
->ClientAPIsMask
;
287 /* this is required by EGL, not by OpenGL ES */
288 if (nconf
->window_bit
&&
289 gconf
->stvis
.render_buffer
!= ST_ATTACHMENT_BACK_LEFT
)
290 api_mask
&= ~(EGL_OPENGL_ES_BIT
| EGL_OPENGL_ES2_BIT
);
293 _eglLog(_EGL_DEBUG
, "no state tracker supports config 0x%x",
294 nconf
->native_visual_id
);
297 valid
= init_config_attributes(&gconf
->base
,
298 nconf
, api_mask
, depth_stencil_format
,
299 preserve_buffer
, max_swap_interval
);
301 _eglLog(_EGL_DEBUG
, "skip invalid config 0x%x", nconf
->native_visual_id
);
305 gconf
->native
= nconf
;
311 * Get all interested depth/stencil formats of a display.
314 egl_g3d_fill_depth_stencil_formats(_EGLDisplay
*dpy
,
315 enum pipe_format formats
[8])
317 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
318 struct pipe_screen
*screen
= gdpy
->native
->screen
;
319 const EGLint candidates
[] = {
320 1, PIPE_FORMAT_Z16_UNORM
,
321 1, PIPE_FORMAT_Z32_UNORM
,
322 2, PIPE_FORMAT_Z24_UNORM_S8_USCALED
, PIPE_FORMAT_S8_USCALED_Z24_UNORM
,
323 2, PIPE_FORMAT_Z24X8_UNORM
, PIPE_FORMAT_X8Z24_UNORM
,
326 const EGLint
*fmt
= candidates
;
330 formats
[count
++] = PIPE_FORMAT_NONE
;
333 EGLint i
, n
= *fmt
++;
335 /* pick the first supported format */
336 for (i
= 0; i
< n
; i
++) {
337 if (screen
->is_format_supported(screen
, fmt
[i
],
338 PIPE_TEXTURE_2D
, 0, PIPE_BIND_DEPTH_STENCIL
, 0)) {
339 formats
[count
++] = fmt
[i
];
351 * Add configs to display and return the next config ID.
354 egl_g3d_add_configs(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint id
)
356 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
357 const struct native_config
**native_configs
;
358 enum pipe_format depth_stencil_formats
[8];
359 int num_formats
, num_configs
, i
, j
;
360 int preserve_buffer
, max_swap_interval
;
362 native_configs
= gdpy
->native
->get_configs(gdpy
->native
, &num_configs
);
365 FREE(native_configs
);
370 gdpy
->native
->get_param(gdpy
->native
, NATIVE_PARAM_PRESERVE_BUFFER
);
372 gdpy
->native
->get_param(gdpy
->native
, NATIVE_PARAM_MAX_SWAP_INTERVAL
);
374 num_formats
= egl_g3d_fill_depth_stencil_formats(dpy
,
375 depth_stencil_formats
);
377 for (i
= 0; i
< num_configs
; i
++) {
378 for (j
= 0; j
< num_formats
; j
++) {
379 struct egl_g3d_config
*gconf
;
381 gconf
= CALLOC_STRUCT(egl_g3d_config
);
383 _eglInitConfig(&gconf
->base
, dpy
, id
);
384 if (!egl_g3d_init_config(drv
, dpy
, &gconf
->base
,
385 native_configs
[i
], depth_stencil_formats
[j
],
386 preserve_buffer
, max_swap_interval
)) {
391 _eglLinkConfig(&gconf
->base
);
397 FREE(native_configs
);
402 egl_g3d_invalid_surface(struct native_display
*ndpy
,
403 struct native_surface
*nsurf
,
404 unsigned int seq_num
)
406 /* XXX not thread safe? */
407 struct egl_g3d_surface
*gsurf
= egl_g3d_surface(nsurf
->user_data
);
408 struct egl_g3d_context
*gctx
;
411 * Some functions such as egl_g3d_copy_buffers create a temporary native
412 * surface. There is no gsurf associated with it.
414 gctx
= (gsurf
) ? egl_g3d_context(gsurf
->base
.CurrentContext
) : NULL
;
416 gctx
->stctxi
->notify_invalid_framebuffer(gctx
->stctxi
, gsurf
->stfbi
);
419 static struct pipe_screen
*
420 egl_g3d_new_drm_screen(struct native_display
*ndpy
, const char *name
, int fd
)
422 _EGLDisplay
*dpy
= (_EGLDisplay
*) ndpy
->user_data
;
423 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
424 return gdpy
->loader
->create_drm_screen(name
, fd
);
427 static struct pipe_screen
*
428 egl_g3d_new_sw_screen(struct native_display
*ndpy
, struct sw_winsys
*ws
)
430 _EGLDisplay
*dpy
= (_EGLDisplay
*) ndpy
->user_data
;
431 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
432 return gdpy
->loader
->create_sw_screen(ws
);
435 static struct native_event_handler egl_g3d_native_event_handler
= {
436 egl_g3d_invalid_surface
,
437 egl_g3d_new_drm_screen
,
438 egl_g3d_new_sw_screen
442 egl_g3d_free_config(void *conf
)
444 struct egl_g3d_config
*gconf
= egl_g3d_config((_EGLConfig
*) conf
);
449 egl_g3d_free_screen(void *scr
)
451 #ifdef EGL_MESA_screen_surface
452 struct egl_g3d_screen
*gscr
= egl_g3d_screen((_EGLScreen
*) scr
);
453 FREE(gscr
->native_modes
);
459 egl_g3d_terminate(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
461 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
463 _eglReleaseDisplayResources(drv
, dpy
);
466 gdpy
->pipe
->destroy(gdpy
->pipe
);
469 _eglDestroyArray(dpy
->Configs
, egl_g3d_free_config
);
473 _eglDestroyArray(dpy
->Screens
, egl_g3d_free_screen
);
477 _eglCleanupDisplay(dpy
);
480 egl_g3d_destroy_st_manager(gdpy
->smapi
);
483 gdpy
->native
->destroy(gdpy
->native
);
486 dpy
->DriverData
= NULL
;
492 egl_g3d_initialize(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
493 EGLint
*major
, EGLint
*minor
)
495 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
496 struct egl_g3d_display
*gdpy
;
497 const struct native_platform
*nplat
;
499 nplat
= egl_g3d_get_platform(drv
, dpy
->Platform
);
503 gdpy
= CALLOC_STRUCT(egl_g3d_display
);
505 _eglError(EGL_BAD_ALLOC
, "eglInitialize");
508 gdpy
->loader
= gdrv
->loader
;
509 dpy
->DriverData
= gdpy
;
511 _eglLog(_EGL_INFO
, "use %s for display %p", nplat
->name
, dpy
->PlatformDisplay
);
512 gdpy
->native
= nplat
->create_display(dpy
->PlatformDisplay
,
513 &egl_g3d_native_event_handler
, (void *) dpy
);
515 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(no usable display)");
519 if (gdpy
->loader
->profile_masks
[ST_API_OPENGL
] & ST_PROFILE_DEFAULT_MASK
)
520 dpy
->ClientAPIsMask
|= EGL_OPENGL_BIT
;
521 if (gdpy
->loader
->profile_masks
[ST_API_OPENGL
] & ST_PROFILE_OPENGL_ES1_MASK
)
522 dpy
->ClientAPIsMask
|= EGL_OPENGL_ES_BIT
;
523 if (gdpy
->loader
->profile_masks
[ST_API_OPENGL
] & ST_PROFILE_OPENGL_ES2_MASK
)
524 dpy
->ClientAPIsMask
|= EGL_OPENGL_ES2_BIT
;
525 if (gdpy
->loader
->profile_masks
[ST_API_OPENVG
] & ST_PROFILE_DEFAULT_MASK
)
526 dpy
->ClientAPIsMask
|= EGL_OPENVG_BIT
;
528 gdpy
->smapi
= egl_g3d_create_st_manager(dpy
);
530 _eglError(EGL_NOT_INITIALIZED
,
531 "eglInitialize(failed to create st manager)");
535 #ifdef EGL_MESA_screen_surface
536 /* enable MESA_screen_surface before adding (and validating) configs */
537 if (gdpy
->native
->modeset
) {
538 dpy
->Extensions
.MESA_screen_surface
= EGL_TRUE
;
539 egl_g3d_add_screens(drv
, dpy
);
543 dpy
->Extensions
.KHR_image_base
= EGL_TRUE
;
544 if (gdpy
->native
->get_param(gdpy
->native
, NATIVE_PARAM_USE_NATIVE_BUFFER
))
545 dpy
->Extensions
.KHR_image_pixmap
= EGL_TRUE
;
547 dpy
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
548 dpy
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
550 dpy
->Extensions
.KHR_surfaceless_gles1
= EGL_TRUE
;
551 dpy
->Extensions
.KHR_surfaceless_gles2
= EGL_TRUE
;
552 dpy
->Extensions
.KHR_surfaceless_opengl
= EGL_TRUE
;
554 if (dpy
->Platform
== _EGL_PLATFORM_DRM
) {
555 dpy
->Extensions
.MESA_drm_display
= EGL_TRUE
;
556 if (gdpy
->native
->buffer
)
557 dpy
->Extensions
.MESA_drm_image
= EGL_TRUE
;
560 if (egl_g3d_add_configs(drv
, dpy
, 1) == 1) {
561 _eglError(EGL_NOT_INITIALIZED
, "eglInitialize(unable to add configs)");
572 egl_g3d_terminate(drv
, dpy
);
577 egl_g3d_get_proc_address(_EGLDriver
*drv
, const char *procname
)
579 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);
580 struct st_api
*stapi
= NULL
;
582 if (procname
&& procname
[0] == 'v' && procname
[1] == 'g')
583 stapi
= gdrv
->loader
->get_st_api(ST_API_OPENVG
);
584 else if (procname
&& procname
[0] == 'g' && procname
[1] == 'l')
585 stapi
= gdrv
->loader
->get_st_api(ST_API_OPENGL
);
587 return (_EGLProc
) ((stapi
) ?
588 stapi
->get_proc_address(stapi
, procname
) : NULL
);
592 egl_g3d_probe(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
594 return (egl_g3d_get_platform(drv
, dpy
->Platform
)) ? 90 : 0;
598 egl_g3d_create_driver(const struct egl_g3d_loader
*loader
)
600 struct egl_g3d_driver
*gdrv
;
602 gdrv
= CALLOC_STRUCT(egl_g3d_driver
);
606 gdrv
->loader
= loader
;
608 egl_g3d_init_driver_api(&gdrv
->base
);
609 gdrv
->base
.API
.Initialize
= egl_g3d_initialize
;
610 gdrv
->base
.API
.Terminate
= egl_g3d_terminate
;
611 gdrv
->base
.API
.GetProcAddress
= egl_g3d_get_proc_address
;
613 gdrv
->base
.Probe
= egl_g3d_probe
;
615 /* to be filled by the caller */
616 gdrv
->base
.Name
= NULL
;
617 gdrv
->base
.Unload
= NULL
;
623 egl_g3d_destroy_driver(_EGLDriver
*drv
)
625 struct egl_g3d_driver
*gdrv
= egl_g3d_driver(drv
);