1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 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 **************************************************************************/
34 #include "eglconfig.h"
35 #include "eglcontext.h"
36 #include "egldisplay.h"
37 #include "eglcurrent.h"
38 #include "eglsurface.h"
43 * Return the API bit (one of EGL_xxx_BIT) of the context.
46 _eglGetContextAPIBit(_EGLContext
*ctx
)
50 switch (ctx
->ClientAPI
) {
51 case EGL_OPENGL_ES_API
:
52 switch (ctx
->ClientMajorVersion
) {
54 bit
= EGL_OPENGL_ES_BIT
;
57 bit
= EGL_OPENGL_ES2_BIT
;
60 bit
= EGL_OPENGL_ES3_BIT_KHR
;
81 * Parse the list of context attributes and return the proper error code.
84 _eglParseContextAttribList(_EGLContext
*ctx
, _EGLDisplay
*dpy
,
85 const EGLint
*attrib_list
)
87 EGLenum api
= ctx
->ClientAPI
;
88 EGLint i
, err
= EGL_SUCCESS
;
93 if (api
== EGL_OPENVG_API
&& attrib_list
[0] != EGL_NONE
) {
94 _eglLog(_EGL_DEBUG
, "bad context attribute 0x%04x", attrib_list
[0]);
95 return EGL_BAD_ATTRIBUTE
;
98 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
++) {
99 EGLint attr
= attrib_list
[i
++];
100 EGLint val
= attrib_list
[i
];
103 case EGL_CONTEXT_CLIENT_VERSION
:
104 ctx
->ClientMajorVersion
= val
;
107 case EGL_CONTEXT_MINOR_VERSION_KHR
:
108 if (!dpy
->Extensions
.KHR_create_context
) {
109 err
= EGL_BAD_ATTRIBUTE
;
113 ctx
->ClientMinorVersion
= val
;
116 case EGL_CONTEXT_FLAGS_KHR
:
117 if (!dpy
->Extensions
.KHR_create_context
) {
118 err
= EGL_BAD_ATTRIBUTE
;
122 /* The EGL_KHR_create_context spec says:
124 * "Flags are only defined for OpenGL context creation, and
125 * specifying a flags value other than zero for other types of
126 * contexts, including OpenGL ES contexts, will generate an
129 if (api
!= EGL_OPENGL_API
&& val
!= 0) {
130 err
= EGL_BAD_ATTRIBUTE
;
137 case EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
:
138 if (!dpy
->Extensions
.KHR_create_context
) {
139 err
= EGL_BAD_ATTRIBUTE
;
143 /* The EGL_KHR_create_context spec says:
145 * "[EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR] is only meaningful for
146 * OpenGL contexts, and specifying it for other types of
147 * contexts, including OpenGL ES contexts, will generate an
150 if (api
!= EGL_OPENGL_API
) {
151 err
= EGL_BAD_ATTRIBUTE
;
158 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR
:
159 /* The EGL_KHR_create_context spec says:
161 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR] is only
162 * meaningful for OpenGL contexts, and specifying it for other
163 * types of contexts, including OpenGL ES contexts, will generate
166 if (!dpy
->Extensions
.KHR_create_context
167 || api
!= EGL_OPENGL_API
) {
168 err
= EGL_BAD_ATTRIBUTE
;
172 ctx
->ResetNotificationStrategy
= val
;
175 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT
:
176 /* The EGL_EXT_create_context_robustness spec says:
178 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT] is only
179 * meaningful for OpenGL ES contexts, and specifying it for other
180 * types of contexts will generate an EGL_BAD_ATTRIBUTE error."
182 if (!dpy
->Extensions
.EXT_create_context_robustness
183 || api
!= EGL_OPENGL_ES_API
) {
184 err
= EGL_BAD_ATTRIBUTE
;
188 ctx
->ResetNotificationStrategy
= val
;
191 case EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT
:
192 if (!dpy
->Extensions
.EXT_create_context_robustness
) {
193 err
= EGL_BAD_ATTRIBUTE
;
197 ctx
->Flags
= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
;
201 err
= EGL_BAD_ATTRIBUTE
;
205 if (err
!= EGL_SUCCESS
) {
206 _eglLog(_EGL_DEBUG
, "bad context attribute 0x%04x", attr
);
211 if (api
== EGL_OPENGL_API
) {
212 /* The EGL_KHR_create_context spec says:
214 * "If the requested OpenGL version is less than 3.2,
215 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR is ignored and the
216 * functionality of the context is determined solely by the
217 * requested version."
219 * Since the value is ignored, only validate the setting if the version
222 if (ctx
->ClientMajorVersion
>= 4
223 || (ctx
->ClientMajorVersion
== 3 && ctx
->ClientMinorVersion
>= 2)) {
224 switch (ctx
->Profile
) {
225 case EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
:
226 case EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR
:
230 /* The EGL_KHR_create_context spec says:
232 * "* If an OpenGL context is requested, the requested version
233 * is greater than 3.2, and the value for attribute
234 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has
235 * any bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
236 * and EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has
237 * more than one of these bits set; or if the implementation does
238 * not support the requested profile, then an EGL_BAD_MATCH error
246 /* The EGL_KHR_create_context spec says:
248 * "* If an OpenGL context is requested and the values for
249 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
250 * EGL_CONTEXT_MINOR_VERSION_KHR, when considered together with
251 * the value for attribute
252 * EGL_CONTEXT_FORWARD_COMPATIBLE_BIT_KHR, specify an OpenGL
253 * version and feature set that are not defined, than an
254 * EGL_BAD_MATCH error is generated.
256 * ... Thus, examples of invalid combinations of attributes
259 * - Major version < 1 or > 4
260 * - Major version == 1 and minor version < 0 or > 5
261 * - Major version == 2 and minor version < 0 or > 1
262 * - Major version == 3 and minor version < 0 or > 2
263 * - Major version == 4 and minor version < 0 or > 2
264 * - Forward-compatible flag set and major version < 3"
266 if (ctx
->ClientMajorVersion
< 1 || ctx
->ClientMinorVersion
< 0)
269 switch (ctx
->ClientMajorVersion
) {
271 if (ctx
->ClientMinorVersion
> 5
272 || (ctx
->Flags
& EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
) != 0)
277 if (ctx
->ClientMinorVersion
> 1
278 || (ctx
->Flags
& EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
) != 0)
283 /* Note: The text above is incorrect. There *is* an OpenGL 3.3!
285 if (ctx
->ClientMinorVersion
> 3)
291 /* Don't put additional version checks here. We don't know that
292 * there won't be versions > 4.2.
296 } else if (api
== EGL_OPENGL_ES_API
) {
297 /* The EGL_KHR_create_context spec says:
299 * "* If an OpenGL ES context is requested and the values for
300 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
301 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
302 * is not defined, than an EGL_BAD_MATCH error is generated.
304 * ... Examples of invalid combinations of attributes include:
306 * - Major version < 1 or > 2
307 * - Major version == 1 and minor version < 0 or > 1
308 * - Major version == 2 and minor version != 0
310 if (ctx
->ClientMajorVersion
< 1 || ctx
->ClientMinorVersion
< 0)
313 switch (ctx
->ClientMajorVersion
) {
315 if (ctx
->ClientMinorVersion
> 1)
320 if (ctx
->ClientMinorVersion
> 0)
326 /* Don't put additional version checks here. We don't know that
327 * there won't be versions > 3.0.
333 switch (ctx
->ResetNotificationStrategy
) {
334 case EGL_NO_RESET_NOTIFICATION_KHR
:
335 case EGL_LOSE_CONTEXT_ON_RESET_KHR
:
339 err
= EGL_BAD_ATTRIBUTE
;
343 if ((ctx
->Flags
& ~(EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
344 | EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
345 | EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
)) != 0) {
346 err
= EGL_BAD_ATTRIBUTE
;
354 * Initialize the given _EGLContext object to defaults and/or the values
355 * in the attrib_list.
358 _eglInitContext(_EGLContext
*ctx
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
359 const EGLint
*attrib_list
)
361 const EGLenum api
= eglQueryAPI();
364 if (api
== EGL_NONE
) {
365 _eglError(EGL_BAD_MATCH
, "eglCreateContext(no client API)");
369 memset(ctx
, 0, sizeof(_EGLContext
));
370 _eglInitResource(&ctx
->Resource
, sizeof(_EGLResource
), dpy
);
371 ctx
->ClientAPI
= api
;
373 ctx
->WindowRenderBuffer
= EGL_NONE
;
374 ctx
->Profile
= EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
;
376 ctx
->ClientMajorVersion
= 1; /* the default, per EGL spec */
377 ctx
->ClientMinorVersion
= 0;
379 ctx
->Profile
= EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
;
380 ctx
->ResetNotificationStrategy
= EGL_NO_RESET_NOTIFICATION_KHR
;
382 err
= _eglParseContextAttribList(ctx
, dpy
, attrib_list
);
383 if (err
== EGL_SUCCESS
&& ctx
->Config
) {
386 api_bit
= _eglGetContextAPIBit(ctx
);
387 if (!(ctx
->Config
->RenderableType
& api_bit
)) {
388 _eglLog(_EGL_DEBUG
, "context api is 0x%x while config supports 0x%x",
389 api_bit
, ctx
->Config
->RenderableType
);
390 err
= EGL_BAD_CONFIG
;
393 if (err
!= EGL_SUCCESS
)
394 return _eglError(err
, "eglCreateContext");
401 _eglQueryContextRenderBuffer(_EGLContext
*ctx
)
403 _EGLSurface
*surf
= ctx
->DrawSurface
;
408 if (surf
->Type
== EGL_WINDOW_BIT
&& ctx
->WindowRenderBuffer
!= EGL_NONE
)
409 rb
= ctx
->WindowRenderBuffer
;
411 rb
= surf
->RenderBuffer
;
417 _eglQueryContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*c
,
418 EGLint attribute
, EGLint
*value
)
424 return _eglError(EGL_BAD_PARAMETER
, "eglQueryContext");
429 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
430 *value
= c
->Config
->ConfigID
;
432 case EGL_CONTEXT_CLIENT_VERSION
:
433 *value
= c
->ClientMajorVersion
;
435 case EGL_CONTEXT_CLIENT_TYPE
:
436 *value
= c
->ClientAPI
;
438 case EGL_RENDER_BUFFER
:
439 *value
= _eglQueryContextRenderBuffer(c
);
442 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
450 * Bind the context to the thread and return the previous context.
452 * Note that the context may be NULL.
455 _eglBindContextToThread(_EGLContext
*ctx
, _EGLThreadInfo
*t
)
461 _eglConvertApiToIndex(ctx
->ClientAPI
) : t
->CurrentAPIIndex
;
463 oldCtx
= t
->CurrentContexts
[apiIndex
];
466 oldCtx
->Binding
= NULL
;
470 t
->CurrentContexts
[apiIndex
] = ctx
;
478 * Return true if the given context and surfaces can be made current.
481 _eglCheckMakeCurrent(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
)
483 _EGLThreadInfo
*t
= _eglGetCurrentThread();
487 if (_eglIsCurrentThreadDummy())
488 return _eglError(EGL_BAD_ALLOC
, "eglMakeCurrent");
493 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
497 dpy
= ctx
->Resource
.Display
;
498 if (!dpy
->Extensions
.KHR_surfaceless_context
499 && (draw
== NULL
|| read
== NULL
))
500 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
505 * "If ctx is current to some other thread, or if either draw or read are
506 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
511 * "at most one context may be bound to a particular surface at a given
514 if (ctx
->Binding
&& ctx
->Binding
!= t
)
515 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
516 if (draw
&& draw
->CurrentContext
&& draw
->CurrentContext
!= ctx
) {
517 if (draw
->CurrentContext
->Binding
!= t
||
518 draw
->CurrentContext
->ClientAPI
!= ctx
->ClientAPI
)
519 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
521 if (read
&& read
->CurrentContext
&& read
->CurrentContext
!= ctx
) {
522 if (read
->CurrentContext
->Binding
!= t
||
523 read
->CurrentContext
->ClientAPI
!= ctx
->ClientAPI
)
524 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
527 /* simply require the configs to be equal */
528 if ((draw
&& draw
->Config
!= ctx
->Config
) ||
529 (read
&& read
->Config
!= ctx
->Config
))
530 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
532 switch (ctx
->ClientAPI
) {
533 /* OpenGL and OpenGL ES are conflicting */
534 case EGL_OPENGL_ES_API
:
535 conflict_api
= EGL_OPENGL_API
;
538 conflict_api
= EGL_OPENGL_ES_API
;
545 if (conflict_api
>= 0 && _eglGetAPIContext(conflict_api
))
546 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
553 * Bind the context to the current thread and given surfaces. Return the
554 * previous bound context and surfaces. The caller should unreference the
555 * returned context and surfaces.
557 * Making a second call with the resources returned by the first call
558 * unsurprisingly undoes the first call, except for the resouce reference
562 _eglBindContext(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
,
563 _EGLContext
**old_ctx
,
564 _EGLSurface
**old_draw
, _EGLSurface
**old_read
)
566 _EGLThreadInfo
*t
= _eglGetCurrentThread();
567 _EGLContext
*prev_ctx
;
568 _EGLSurface
*prev_draw
, *prev_read
;
570 if (!_eglCheckMakeCurrent(ctx
, draw
, read
))
573 /* increment refcounts before binding */
575 _eglGetSurface(draw
);
576 _eglGetSurface(read
);
578 /* bind the new context */
579 prev_ctx
= _eglBindContextToThread(ctx
, t
);
581 /* break previous bindings */
583 prev_draw
= prev_ctx
->DrawSurface
;
584 prev_read
= prev_ctx
->ReadSurface
;
587 prev_draw
->CurrentContext
= NULL
;
589 prev_read
->CurrentContext
= NULL
;
591 prev_ctx
->DrawSurface
= NULL
;
592 prev_ctx
->ReadSurface
= NULL
;
595 prev_draw
= prev_read
= NULL
;
598 /* establish new bindings */
601 draw
->CurrentContext
= ctx
;
603 read
->CurrentContext
= ctx
;
605 ctx
->DrawSurface
= draw
;
606 ctx
->ReadSurface
= read
;
609 assert(old_ctx
&& old_draw
&& old_read
);
611 *old_draw
= prev_draw
;
612 *old_read
= prev_read
;