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
;
78 if (dpy
->Extensions
.NOK_texture_from_pixmap
)
79 texture_type
|= EGL_PIXMAP_BIT
;
81 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
++) {
82 EGLint attr
= attrib_list
[i
++];
83 EGLint val
= attrib_list
[i
];
86 /* common attributes */
87 case EGL_VG_COLORSPACE
:
89 case EGL_VG_COLORSPACE_sRGB
:
90 case EGL_VG_COLORSPACE_LINEAR
:
93 err
= EGL_BAD_ATTRIBUTE
;
96 if (err
!= EGL_SUCCESS
)
98 surf
->VGColorspace
= val
;
100 case EGL_VG_ALPHA_FORMAT
:
102 case EGL_VG_ALPHA_FORMAT_NONPRE
:
103 case EGL_VG_ALPHA_FORMAT_PRE
:
106 err
= EGL_BAD_ATTRIBUTE
;
109 if (err
!= EGL_SUCCESS
)
111 surf
->VGAlphaFormat
= val
;
113 /* window surface attributes */
114 case EGL_RENDER_BUFFER
:
115 if (type
!= EGL_WINDOW_BIT
) {
116 err
= EGL_BAD_ATTRIBUTE
;
119 if (val
!= EGL_BACK_BUFFER
&& val
!= EGL_SINGLE_BUFFER
) {
120 err
= EGL_BAD_ATTRIBUTE
;
123 surf
->RenderBuffer
= val
;
125 case EGL_POST_SUB_BUFFER_SUPPORTED_NV
:
126 if (!dpy
->Extensions
.NV_post_sub_buffer
||
127 type
!= EGL_WINDOW_BIT
) {
128 err
= EGL_BAD_ATTRIBUTE
;
131 if (val
!= EGL_TRUE
&& val
!= EGL_FALSE
) {
132 err
= EGL_BAD_PARAMETER
;
135 surf
->PostSubBufferSupportedNV
= val
;
137 /* pbuffer surface attributes */
139 if (type
!= EGL_PBUFFER_BIT
) {
140 err
= EGL_BAD_ATTRIBUTE
;
144 err
= EGL_BAD_PARAMETER
;
150 if (type
!= EGL_PBUFFER_BIT
) {
151 err
= EGL_BAD_ATTRIBUTE
;
155 err
= EGL_BAD_PARAMETER
;
160 case EGL_LARGEST_PBUFFER
:
161 if (type
!= EGL_PBUFFER_BIT
) {
162 err
= EGL_BAD_ATTRIBUTE
;
165 surf
->LargestPbuffer
= !!val
;
167 /* for eglBindTexImage */
168 case EGL_TEXTURE_FORMAT
:
169 if (!(type
& texture_type
)) {
170 err
= EGL_BAD_ATTRIBUTE
;
174 case EGL_TEXTURE_RGB
:
175 case EGL_TEXTURE_RGBA
:
179 err
= EGL_BAD_ATTRIBUTE
;
182 if (err
!= EGL_SUCCESS
)
184 surf
->TextureFormat
= val
;
186 case EGL_TEXTURE_TARGET
:
187 if (!(type
& texture_type
)) {
188 err
= EGL_BAD_ATTRIBUTE
;
196 err
= EGL_BAD_ATTRIBUTE
;
199 if (err
!= EGL_SUCCESS
)
201 surf
->TextureTarget
= val
;
203 case EGL_MIPMAP_TEXTURE
:
204 if (!(type
& texture_type
)) {
205 err
= EGL_BAD_ATTRIBUTE
;
208 surf
->MipmapTexture
= !!val
;
210 /* no pixmap surface specific attributes */
212 err
= EGL_BAD_ATTRIBUTE
;
216 if (err
!= EGL_SUCCESS
) {
217 _eglLog(_EGL_WARNING
, "bad surface attribute 0x%04x", attr
);
227 * Do error check on parameters and initialize the given _EGLSurface object.
228 * \return EGL_TRUE if no errors, EGL_FALSE otherwise.
231 _eglInitSurface(_EGLSurface
*surf
, _EGLDisplay
*dpy
, EGLint type
,
232 _EGLConfig
*conf
, const EGLint
*attrib_list
)
235 EGLint renderBuffer
= EGL_BACK_BUFFER
;
236 EGLint swapBehavior
= EGL_BUFFER_PRESERVED
;
241 func
= "eglCreateWindowSurface";
242 swapBehavior
= EGL_BUFFER_DESTROYED
;
245 func
= "eglCreatePixmapSurface";
246 renderBuffer
= EGL_SINGLE_BUFFER
;
248 case EGL_PBUFFER_BIT
:
249 func
= "eglCreatePBufferSurface";
252 _eglLog(_EGL_WARNING
, "Bad type in _eglInitSurface");
256 if ((conf
->SurfaceType
& type
) == 0) {
257 /* The config can't be used to create a surface of this type */
258 _eglError(EGL_BAD_CONFIG
, func
);
262 _eglInitResource(&surf
->Resource
, sizeof(*surf
), dpy
);
268 surf
->TextureFormat
= EGL_NO_TEXTURE
;
269 surf
->TextureTarget
= EGL_NO_TEXTURE
;
270 surf
->MipmapTexture
= EGL_FALSE
;
271 surf
->LargestPbuffer
= EGL_FALSE
;
272 surf
->RenderBuffer
= renderBuffer
;
273 surf
->VGAlphaFormat
= EGL_VG_ALPHA_FORMAT_NONPRE
;
274 surf
->VGColorspace
= EGL_VG_COLORSPACE_sRGB
;
276 surf
->MipmapLevel
= 0;
277 surf
->MultisampleResolve
= EGL_MULTISAMPLE_RESOLVE_DEFAULT
;
278 surf
->SwapBehavior
= swapBehavior
;
280 surf
->HorizontalResolution
= EGL_UNKNOWN
;
281 surf
->VerticalResolution
= EGL_UNKNOWN
;
282 surf
->AspectRatio
= EGL_UNKNOWN
;
284 surf
->PostSubBufferSupportedNV
= EGL_FALSE
;
286 /* the default swap interval is 1 */
287 _eglClampSwapInterval(surf
, 1);
289 err
= _eglParseSurfaceAttribList(surf
, attrib_list
);
290 if (err
!= EGL_SUCCESS
)
291 return _eglError(err
, func
);
298 _eglQuerySurface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
,
299 EGLint attribute
, EGLint
*value
)
303 *value
= surface
->Width
;
306 *value
= surface
->Height
;
309 *value
= surface
->Config
->ConfigID
;
311 case EGL_LARGEST_PBUFFER
:
312 *value
= surface
->LargestPbuffer
;
314 case EGL_TEXTURE_FORMAT
:
315 /* texture attributes: only for pbuffers, no error otherwise */
316 if (surface
->Type
== EGL_PBUFFER_BIT
)
317 *value
= surface
->TextureFormat
;
319 case EGL_TEXTURE_TARGET
:
320 if (surface
->Type
== EGL_PBUFFER_BIT
)
321 *value
= surface
->TextureTarget
;
323 case EGL_MIPMAP_TEXTURE
:
324 if (surface
->Type
== EGL_PBUFFER_BIT
)
325 *value
= surface
->MipmapTexture
;
327 case EGL_MIPMAP_LEVEL
:
328 if (surface
->Type
== EGL_PBUFFER_BIT
)
329 *value
= surface
->MipmapLevel
;
331 case EGL_SWAP_BEHAVIOR
:
332 *value
= surface
->SwapBehavior
;
334 case EGL_RENDER_BUFFER
:
335 *value
= surface
->RenderBuffer
;
337 case EGL_PIXEL_ASPECT_RATIO
:
338 *value
= surface
->AspectRatio
;
340 case EGL_HORIZONTAL_RESOLUTION
:
341 *value
= surface
->HorizontalResolution
;
343 case EGL_VERTICAL_RESOLUTION
:
344 *value
= surface
->VerticalResolution
;
346 case EGL_MULTISAMPLE_RESOLVE
:
347 *value
= surface
->MultisampleResolve
;
349 case EGL_VG_ALPHA_FORMAT
:
350 *value
= surface
->VGAlphaFormat
;
352 case EGL_VG_COLORSPACE
:
353 *value
= surface
->VGColorspace
;
355 case EGL_POST_SUB_BUFFER_SUPPORTED_NV
:
356 *value
= surface
->PostSubBufferSupportedNV
;
358 case EGL_BUFFER_AGE_EXT
:
359 if (!dpy
->Extensions
.EXT_buffer_age
) {
360 _eglError(EGL_BAD_ATTRIBUTE
, "eglQuerySurface");
363 *value
= drv
->API
.QueryBufferAge(drv
, dpy
, surface
);
366 _eglError(EGL_BAD_ATTRIBUTE
, "eglQuerySurface");
375 * Default fallback routine - drivers might override this.
378 _eglSurfaceAttrib(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
,
379 EGLint attribute
, EGLint value
)
382 EGLint err
= EGL_SUCCESS
;
383 EGLint all_es_bits
= EGL_OPENGL_ES_BIT
|
385 EGL_OPENGL_ES3_BIT_KHR
;
388 case EGL_MIPMAP_LEVEL
:
389 confval
= surface
->Config
->RenderableType
;
390 if (!(confval
& all_es_bits
)) {
391 err
= EGL_BAD_PARAMETER
;
394 surface
->MipmapLevel
= value
;
396 case EGL_MULTISAMPLE_RESOLVE
:
398 case EGL_MULTISAMPLE_RESOLVE_DEFAULT
:
400 case EGL_MULTISAMPLE_RESOLVE_BOX
:
401 confval
= surface
->Config
->SurfaceType
;
402 if (!(confval
& EGL_MULTISAMPLE_RESOLVE_BOX_BIT
))
406 err
= EGL_BAD_ATTRIBUTE
;
409 if (err
!= EGL_SUCCESS
)
411 surface
->MultisampleResolve
= value
;
413 case EGL_SWAP_BEHAVIOR
:
415 case EGL_BUFFER_DESTROYED
:
417 case EGL_BUFFER_PRESERVED
:
418 confval
= surface
->Config
->SurfaceType
;
419 if (!(confval
& EGL_SWAP_BEHAVIOR_PRESERVED_BIT
))
423 err
= EGL_BAD_ATTRIBUTE
;
426 if (err
!= EGL_SUCCESS
)
428 surface
->SwapBehavior
= value
;
431 err
= EGL_BAD_ATTRIBUTE
;
435 if (err
!= EGL_SUCCESS
)
436 return _eglError(err
, "eglSurfaceAttrib");
442 _eglBindTexImage(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surface
,
445 EGLint texture_type
= EGL_PBUFFER_BIT
;
447 /* Just do basic error checking and return success/fail.
448 * Drivers must implement the real stuff.
451 if (dpy
->Extensions
.NOK_texture_from_pixmap
)
452 texture_type
|= EGL_PIXMAP_BIT
;
454 if (!(surface
->Type
& texture_type
)) {
455 _eglError(EGL_BAD_SURFACE
, "eglBindTexImage");
459 if (surface
->TextureFormat
== EGL_NO_TEXTURE
) {
460 _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
464 if (surface
->TextureTarget
== EGL_NO_TEXTURE
) {
465 _eglError(EGL_BAD_MATCH
, "eglBindTexImage");
469 if (buffer
!= EGL_BACK_BUFFER
) {
470 _eglError(EGL_BAD_PARAMETER
, "eglBindTexImage");
474 surface
->BoundToTexture
= EGL_TRUE
;
480 _eglReleaseTexImage(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
483 /* TODO: do basic error checking and return success/fail.
484 * Drivers must implement the real stuff.
492 _eglSwapInterval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
495 _eglClampSwapInterval(surf
, interval
);