1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010 LunarG, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
32 * Surface-related functions.
39 #include "egldisplay.h"
40 #include "egldriver.h"
41 #include "eglcontext.h"
42 #include "eglconfig.h"
43 #include "eglcurrent.h"
45 #include "eglsurface.h"
49 _eglClampSwapInterval(_EGLSurface
*surf
, EGLint interval
)
51 EGLint bound
= surf
->Config
->MaxSwapInterval
;
52 if (interval
>= bound
) {
56 bound
= surf
->Config
->MinSwapInterval
;
60 surf
->SwapInterval
= interval
;
65 * Parse the list of surface attributes and return the proper error code.
68 _eglParseSurfaceAttribList(_EGLSurface
*surf
, const EGLint
*attrib_list
)
70 _EGLDisplay
*dpy
= surf
->Resource
.Display
;
71 EGLint type
= surf
->Type
;
72 EGLint texture_type
= EGL_PBUFFER_BIT
;
73 EGLint i
, err
= EGL_SUCCESS
;
74 EGLint tex_target
= -1;
75 EGLint tex_format
= -1;
80 if (dpy
->Extensions
.NOK_texture_from_pixmap
)
81 texture_type
|= EGL_PIXMAP_BIT
;
83 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
++) {
84 EGLint attr
= attrib_list
[i
++];
85 EGLint val
= attrib_list
[i
];
88 /* common attributes */
89 case EGL_GL_COLORSPACE_KHR
:
90 if (!dpy
->Extensions
.KHR_gl_colorspace
) {
91 err
= EGL_BAD_ATTRIBUTE
;
95 case EGL_GL_COLORSPACE_SRGB_KHR
:
96 case EGL_GL_COLORSPACE_LINEAR_KHR
:
99 err
= EGL_BAD_ATTRIBUTE
;
101 if (err
!= EGL_SUCCESS
)
103 surf
->GLColorspace
= val
;
105 case EGL_VG_COLORSPACE
:
107 case EGL_VG_COLORSPACE_sRGB
:
108 case EGL_VG_COLORSPACE_LINEAR
:
111 err
= EGL_BAD_ATTRIBUTE
;
114 if (err
!= EGL_SUCCESS
)
116 surf
->VGColorspace
= val
;
118 case EGL_VG_ALPHA_FORMAT
:
120 case EGL_VG_ALPHA_FORMAT_NONPRE
:
121 case EGL_VG_ALPHA_FORMAT_PRE
:
124 err
= EGL_BAD_ATTRIBUTE
;
127 if (err
!= EGL_SUCCESS
)
129 surf
->VGAlphaFormat
= val
;
131 /* window surface attributes */
132 case EGL_RENDER_BUFFER
:
133 if (type
!= EGL_WINDOW_BIT
) {
134 err
= EGL_BAD_ATTRIBUTE
;
137 if (val
!= EGL_BACK_BUFFER
&& val
!= EGL_SINGLE_BUFFER
) {
138 err
= EGL_BAD_ATTRIBUTE
;
141 surf
->RenderBuffer
= val
;
143 case EGL_POST_SUB_BUFFER_SUPPORTED_NV
:
144 if (!dpy
->Extensions
.NV_post_sub_buffer
||
145 type
!= EGL_WINDOW_BIT
) {
146 err
= EGL_BAD_ATTRIBUTE
;
149 if (val
!= EGL_TRUE
&& val
!= EGL_FALSE
) {
150 err
= EGL_BAD_PARAMETER
;
153 surf
->PostSubBufferSupportedNV
= val
;
155 /* pbuffer surface attributes */
157 if (type
!= EGL_PBUFFER_BIT
) {
158 err
= EGL_BAD_ATTRIBUTE
;
162 err
= EGL_BAD_PARAMETER
;
168 if (type
!= EGL_PBUFFER_BIT
) {
169 err
= EGL_BAD_ATTRIBUTE
;
173 err
= EGL_BAD_PARAMETER
;
178 case EGL_LARGEST_PBUFFER
:
179 if (type
!= EGL_PBUFFER_BIT
) {
180 err
= EGL_BAD_ATTRIBUTE
;
183 surf
->LargestPbuffer
= !!val
;
185 /* for eglBindTexImage */
186 case EGL_TEXTURE_FORMAT
:
187 if (!(type
& texture_type
)) {
188 err
= EGL_BAD_ATTRIBUTE
;
194 case EGL_TEXTURE_RGB
:
195 case EGL_TEXTURE_RGBA
:
199 err
= EGL_BAD_ATTRIBUTE
;
202 if (err
!= EGL_SUCCESS
)
204 surf
->TextureFormat
= val
;
206 case EGL_TEXTURE_TARGET
:
207 if (!(type
& texture_type
)) {
208 err
= EGL_BAD_ATTRIBUTE
;
218 err
= EGL_BAD_ATTRIBUTE
;
221 if (err
!= EGL_SUCCESS
)
223 surf
->TextureTarget
= val
;
225 case EGL_MIPMAP_TEXTURE
:
226 if (!(type
& texture_type
)) {
227 err
= EGL_BAD_ATTRIBUTE
;
230 surf
->MipmapTexture
= !!val
;
232 /* no pixmap surface specific attributes */
234 err
= EGL_BAD_ATTRIBUTE
;
238 if (type
== EGL_PBUFFER_BIT
) {
239 if (tex_target
== -1)
240 tex_target
= surf
->TextureTarget
;
242 if (tex_format
== -1)
243 tex_format
= surf
->TextureFormat
;
245 if ((tex_target
== EGL_NO_TEXTURE
&& tex_format
!= EGL_NO_TEXTURE
) ||
246 (tex_format
== EGL_NO_TEXTURE
&& tex_target
!= EGL_NO_TEXTURE
)) {
251 if (err
!= EGL_SUCCESS
) {
252 _eglLog(_EGL_WARNING
, "bad surface attribute 0x%04x", attr
);
262 * Do error check on parameters and initialize the given _EGLSurface object.
263 * \return EGL_TRUE if no errors, EGL_FALSE otherwise.
266 _eglInitSurface(_EGLSurface
*surf
, _EGLDisplay
*dpy
, EGLint type
,
267 _EGLConfig
*conf
, const EGLint
*attrib_list
)
270 EGLint renderBuffer
= EGL_BACK_BUFFER
;
271 EGLint swapBehavior
= EGL_BUFFER_PRESERVED
;
276 func
= "eglCreateWindowSurface";
277 swapBehavior
= EGL_BUFFER_DESTROYED
;
280 func
= "eglCreatePixmapSurface";
281 renderBuffer
= EGL_SINGLE_BUFFER
;
283 case EGL_PBUFFER_BIT
:
284 func
= "eglCreatePBufferSurface";
287 _eglLog(_EGL_WARNING
, "Bad type in _eglInitSurface");
291 if ((conf
->SurfaceType
& type
) == 0) {
292 /* The config can't be used to create a surface of this type */
293 _eglError(EGL_BAD_CONFIG
, func
);
297 _eglInitResource(&surf
->Resource
, sizeof(*surf
), dpy
);
303 surf
->TextureFormat
= EGL_NO_TEXTURE
;
304 surf
->TextureTarget
= EGL_NO_TEXTURE
;
305 surf
->MipmapTexture
= EGL_FALSE
;
306 surf
->LargestPbuffer
= EGL_FALSE
;
307 surf
->RenderBuffer
= renderBuffer
;
308 surf
->VGAlphaFormat
= EGL_VG_ALPHA_FORMAT_NONPRE
;
309 surf
->VGColorspace
= EGL_VG_COLORSPACE_sRGB
;
310 surf
->GLColorspace
= EGL_GL_COLORSPACE_LINEAR_KHR
;
312 surf
->MipmapLevel
= 0;
313 surf
->MultisampleResolve
= EGL_MULTISAMPLE_RESOLVE_DEFAULT
;
314 surf
->SwapBehavior
= swapBehavior
;
316 surf
->HorizontalResolution
= EGL_UNKNOWN
;
317 surf
->VerticalResolution
= EGL_UNKNOWN
;
318 surf
->AspectRatio
= EGL_UNKNOWN
;
320 surf
->PostSubBufferSupportedNV
= EGL_FALSE
;
322 /* the default swap interval is 1 */
323 _eglClampSwapInterval(surf
, 1);
325 err
= _eglParseSurfaceAttribList(surf
, attrib_list
);
326 if (err
!= EGL_SUCCESS
)
327 return _eglError(err
, func
);
329 /* if EGL_LARGEST_PBUFFER in use, clamp width and height */
330 if (surf
->LargestPbuffer
) {
331 surf
->Width
= MIN2(surf
->Width
, _EGL_MAX_PBUFFER_WIDTH
);
332 surf
->Height
= MIN2(surf
->Height
, _EGL_MAX_PBUFFER_HEIGHT
);
340 _eglQuerySurface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
,
341 EGLint attribute
, EGLint
*value
)
345 *value
= surface
->Width
;
348 *value
= surface
->Height
;
351 *value
= surface
->Config
->ConfigID
;
353 case EGL_LARGEST_PBUFFER
:
354 if (surface
->Type
== EGL_PBUFFER_BIT
)
355 *value
= surface
->LargestPbuffer
;
357 case EGL_TEXTURE_FORMAT
:
358 /* texture attributes: only for pbuffers, no error otherwise */
359 if (surface
->Type
== EGL_PBUFFER_BIT
)
360 *value
= surface
->TextureFormat
;
362 case EGL_TEXTURE_TARGET
:
363 if (surface
->Type
== EGL_PBUFFER_BIT
)
364 *value
= surface
->TextureTarget
;
366 case EGL_MIPMAP_TEXTURE
:
367 if (surface
->Type
== EGL_PBUFFER_BIT
)
368 *value
= surface
->MipmapTexture
;
370 case EGL_MIPMAP_LEVEL
:
371 if (surface
->Type
== EGL_PBUFFER_BIT
)
372 *value
= surface
->MipmapLevel
;
374 case EGL_SWAP_BEHAVIOR
:
375 *value
= surface
->SwapBehavior
;
377 case EGL_RENDER_BUFFER
:
378 *value
= surface
->RenderBuffer
;
380 case EGL_PIXEL_ASPECT_RATIO
:
381 *value
= surface
->AspectRatio
;
383 case EGL_HORIZONTAL_RESOLUTION
:
384 *value
= surface
->HorizontalResolution
;
386 case EGL_VERTICAL_RESOLUTION
:
387 *value
= surface
->VerticalResolution
;
389 case EGL_MULTISAMPLE_RESOLVE
:
390 *value
= surface
->MultisampleResolve
;
392 case EGL_VG_ALPHA_FORMAT
:
393 *value
= surface
->VGAlphaFormat
;
395 case EGL_VG_COLORSPACE
:
396 *value
= surface
->VGColorspace
;
398 case EGL_GL_COLORSPACE_KHR
:
399 if (!dpy
->Extensions
.KHR_gl_colorspace
) {
400 _eglError(EGL_BAD_ATTRIBUTE
, "eglQuerySurface");
403 *value
= surface
->GLColorspace
;
405 case EGL_POST_SUB_BUFFER_SUPPORTED_NV
:
406 *value
= surface
->PostSubBufferSupportedNV
;
408 case EGL_BUFFER_AGE_EXT
:
409 if (!dpy
->Extensions
.EXT_buffer_age
) {
410 _eglError(EGL_BAD_ATTRIBUTE
, "eglQuerySurface");
413 *value
= drv
->API
.QueryBufferAge(drv
, dpy
, surface
);
416 _eglError(EGL_BAD_ATTRIBUTE
, "eglQuerySurface");
425 * Default fallback routine - drivers might override this.
428 _eglSurfaceAttrib(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
,
429 EGLint attribute
, EGLint value
)
432 EGLint err
= EGL_SUCCESS
;
433 EGLint all_es_bits
= EGL_OPENGL_ES_BIT
|
435 EGL_OPENGL_ES3_BIT_KHR
;
438 case EGL_MIPMAP_LEVEL
:
439 confval
= surface
->Config
->RenderableType
;
440 if (!(confval
& all_es_bits
)) {
441 err
= EGL_BAD_PARAMETER
;
444 surface
->MipmapLevel
= value
;
446 case EGL_MULTISAMPLE_RESOLVE
:
448 case EGL_MULTISAMPLE_RESOLVE_DEFAULT
:
450 case EGL_MULTISAMPLE_RESOLVE_BOX
:
451 confval
= surface
->Config
->SurfaceType
;
452 if (!(confval
& EGL_MULTISAMPLE_RESOLVE_BOX_BIT
))
456 err
= EGL_BAD_ATTRIBUTE
;
459 if (err
!= EGL_SUCCESS
)
461 surface
->MultisampleResolve
= value
;
463 case EGL_SWAP_BEHAVIOR
:
465 case EGL_BUFFER_DESTROYED
:
467 case EGL_BUFFER_PRESERVED
:
468 confval
= surface
->Config
->SurfaceType
;
469 if (!(confval
& EGL_SWAP_BEHAVIOR_PRESERVED_BIT
))
473 err
= EGL_BAD_ATTRIBUTE
;
476 if (err
!= EGL_SUCCESS
)
478 surface
->SwapBehavior
= value
;
481 err
= EGL_BAD_ATTRIBUTE
;
485 if (err
!= EGL_SUCCESS
)
486 return _eglError(err
, "eglSurfaceAttrib");
492 _eglBindTexImage(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
,
495 EGLint texture_type
= EGL_PBUFFER_BIT
;
497 /* Just do basic error checking and return success/fail.
498 * Drivers must implement the real stuff.
501 if (dpy
->Extensions
.NOK_texture_from_pixmap
)
502 texture_type
|= EGL_PIXMAP_BIT
;
504 if (!(surface
->Type
& texture_type
)) {
505 _eglError(EGL_BAD_SURFACE
, "eglBindTexImage");
509 if (surface
->TextureFormat
== EGL_NO_TEXTURE
) {
510 _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
514 if (surface
->TextureTarget
== EGL_NO_TEXTURE
) {
515 _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
519 if (buffer
!= EGL_BACK_BUFFER
) {
520 _eglError(EGL_BAD_PARAMETER
, "eglBindTexImage");
524 surface
->BoundToTexture
= EGL_TRUE
;
530 _eglReleaseTexImage(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
533 /* TODO: do basic error checking and return success/fail.
534 * Drivers must implement the real stuff.
542 _eglSwapInterval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
545 _eglClampSwapInterval(surf
, interval
);