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 /* The EGL 1.4 spec says:
106 * "attribute EGL_CONTEXT_CLIENT_VERSION is only valid when the
107 * current rendering API is EGL_OPENGL_ES_API"
109 * The EGL_KHR_create_context spec says:
111 * "EGL_CONTEXT_MAJOR_VERSION_KHR 0x3098
112 * (this token is an alias for EGL_CONTEXT_CLIENT_VERSION)"
114 * "The values for attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
115 * EGL_CONTEXT_MINOR_VERSION_KHR specify the requested client API
116 * version. They are only meaningful for OpenGL and OpenGL ES
117 * contexts, and specifying them for other types of contexts will
118 * generate an error."
120 if ((api
!= EGL_OPENGL_ES_API
&&
121 (!dpy
->Extensions
.KHR_create_context
|| api
!= EGL_OPENGL_API
))) {
122 err
= EGL_BAD_ATTRIBUTE
;
126 ctx
->ClientMajorVersion
= val
;
129 case EGL_CONTEXT_MINOR_VERSION_KHR
:
130 /* The EGL_KHR_create_context spec says:
132 * "The values for attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
133 * EGL_CONTEXT_MINOR_VERSION_KHR specify the requested client API
134 * version. They are only meaningful for OpenGL and OpenGL ES
135 * contexts, and specifying them for other types of contexts will
136 * generate an error."
138 if (!dpy
->Extensions
.KHR_create_context
||
139 (api
!= EGL_OPENGL_ES_API
&& api
!= EGL_OPENGL_API
)) {
140 err
= EGL_BAD_ATTRIBUTE
;
144 ctx
->ClientMinorVersion
= val
;
147 case EGL_CONTEXT_FLAGS_KHR
:
148 if (!dpy
->Extensions
.KHR_create_context
) {
149 err
= EGL_BAD_ATTRIBUTE
;
153 /* The EGL_KHR_create_context spec says:
155 * "If the EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR flag bit is set in
156 * EGL_CONTEXT_FLAGS_KHR, then a <debug context> will be created.
158 * In some cases a debug context may be identical to a non-debug
159 * context. This bit is supported for OpenGL and OpenGL ES
162 if ((val
& EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
) &&
163 (api
!= EGL_OPENGL_API
&& api
!= EGL_OPENGL_ES_API
)) {
164 err
= EGL_BAD_ATTRIBUTE
;
168 /* The EGL_KHR_create_context spec says:
170 * "If the EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR flag bit
171 * is set in EGL_CONTEXT_FLAGS_KHR, then a <forward-compatible>
172 * context will be created. Forward-compatible contexts are
173 * defined only for OpenGL versions 3.0 and later. They must not
174 * support functionality marked as <deprecated> by that version of
175 * the API, while a non-forward-compatible context must support
176 * all functionality in that version, deprecated or not. This bit
177 * is supported for OpenGL contexts, and requesting a
178 * forward-compatible context for OpenGL versions less than 3.0
179 * will generate an error."
181 if ((val
& EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
) &&
182 (api
!= EGL_OPENGL_API
|| ctx
->ClientMajorVersion
< 3)) {
183 err
= EGL_BAD_ATTRIBUTE
;
187 if ((val
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) &&
188 api
!= EGL_OPENGL_API
) {
189 /* The EGL_KHR_create_context spec says:
191 * 10) Which error should be generated if robust buffer access
192 * or reset notifications are requested under OpenGL ES?
194 * As per Issue 6, this extension does not support creating
195 * robust contexts for OpenGL ES. This is only supported via
196 * the EGL_EXT_create_context_robustness extension.
198 * Attempting to use this extension to create robust OpenGL
199 * ES context will generate an EGL_BAD_ATTRIBUTE error. This
200 * specific error is generated because this extension does
201 * not define the EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
202 * and EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR
203 * bits for OpenGL ES contexts. Thus, use of these bits fall
204 * under condition described by: "If an attribute is
205 * specified that is not meaningful for the client API
206 * type.." in the above specification.
208 * The spec requires that we emit the error even if the display
209 * supports EGL_EXT_create_context_robustness. To create a robust
210 * GLES context, the *attribute*
211 * EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT must be used, not the
212 * *flag* EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR.
214 err
= EGL_BAD_ATTRIBUTE
;
221 case EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
:
222 if (!dpy
->Extensions
.KHR_create_context
) {
223 err
= EGL_BAD_ATTRIBUTE
;
227 /* The EGL_KHR_create_context spec says:
229 * "[EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR] is only meaningful for
230 * OpenGL contexts, and specifying it for other types of
231 * contexts, including OpenGL ES contexts, will generate an
234 if (api
!= EGL_OPENGL_API
) {
235 err
= EGL_BAD_ATTRIBUTE
;
242 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR
:
243 /* The EGL_KHR_create_context spec says:
245 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR] is only
246 * meaningful for OpenGL contexts, and specifying it for other
247 * types of contexts, including OpenGL ES contexts, will generate
250 if (!dpy
->Extensions
.KHR_create_context
251 || api
!= EGL_OPENGL_API
) {
252 err
= EGL_BAD_ATTRIBUTE
;
256 ctx
->ResetNotificationStrategy
= val
;
259 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT
:
260 /* The EGL_EXT_create_context_robustness spec says:
262 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT] is only
263 * meaningful for OpenGL ES contexts, and specifying it for other
264 * types of contexts will generate an EGL_BAD_ATTRIBUTE error."
266 if (!dpy
->Extensions
.EXT_create_context_robustness
267 || api
!= EGL_OPENGL_ES_API
) {
268 err
= EGL_BAD_ATTRIBUTE
;
272 ctx
->ResetNotificationStrategy
= val
;
275 case EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT
:
276 if (!dpy
->Extensions
.EXT_create_context_robustness
) {
277 err
= EGL_BAD_ATTRIBUTE
;
282 ctx
->Flags
|= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
;
285 case EGL_CONTEXT_OPENGL_ROBUST_ACCESS
:
286 if (dpy
->Version
< 15) {
287 err
= EGL_BAD_ATTRIBUTE
;
292 ctx
->Flags
|= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
;
295 case EGL_CONTEXT_OPENGL_DEBUG
:
296 if (dpy
->Version
< 15) {
297 err
= EGL_BAD_ATTRIBUTE
;
302 ctx
->Flags
|= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
;
305 case EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE
:
306 if (dpy
->Version
< 15) {
307 err
= EGL_BAD_ATTRIBUTE
;
312 ctx
->Flags
|= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
;
316 err
= EGL_BAD_ATTRIBUTE
;
320 if (err
!= EGL_SUCCESS
) {
321 _eglLog(_EGL_DEBUG
, "bad context attribute 0x%04x", attr
);
326 if (api
== EGL_OPENGL_API
) {
327 /* The EGL_KHR_create_context spec says:
329 * "If the requested OpenGL version is less than 3.2,
330 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR is ignored and the
331 * functionality of the context is determined solely by the
332 * requested version."
334 * Since the value is ignored, only validate the setting if the version
337 if (ctx
->ClientMajorVersion
>= 4
338 || (ctx
->ClientMajorVersion
== 3 && ctx
->ClientMinorVersion
>= 2)) {
339 switch (ctx
->Profile
) {
340 case EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
:
341 case EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR
:
345 /* The EGL_KHR_create_context spec says:
347 * "* If an OpenGL context is requested, the requested version
348 * is greater than 3.2, and the value for attribute
349 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has
350 * any bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
351 * and EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has
352 * more than one of these bits set; or if the implementation does
353 * not support the requested profile, then an EGL_BAD_MATCH error
361 /* The EGL_KHR_create_context spec says:
363 * "* If an OpenGL context is requested and the values for
364 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
365 * EGL_CONTEXT_MINOR_VERSION_KHR, when considered together with
366 * the value for attribute
367 * EGL_CONTEXT_FORWARD_COMPATIBLE_BIT_KHR, specify an OpenGL
368 * version and feature set that are not defined, than an
369 * EGL_BAD_MATCH error is generated.
371 * ... Thus, examples of invalid combinations of attributes
374 * - Major version < 1 or > 4
375 * - Major version == 1 and minor version < 0 or > 5
376 * - Major version == 2 and minor version < 0 or > 1
377 * - Major version == 3 and minor version < 0 or > 2
378 * - Major version == 4 and minor version < 0 or > 2
379 * - Forward-compatible flag set and major version < 3"
381 if (ctx
->ClientMajorVersion
< 1 || ctx
->ClientMinorVersion
< 0)
384 switch (ctx
->ClientMajorVersion
) {
386 if (ctx
->ClientMinorVersion
> 5
387 || (ctx
->Flags
& EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
) != 0)
392 if (ctx
->ClientMinorVersion
> 1
393 || (ctx
->Flags
& EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
) != 0)
398 /* Note: The text above is incorrect. There *is* an OpenGL 3.3!
400 if (ctx
->ClientMinorVersion
> 3)
406 /* Don't put additional version checks here. We don't know that
407 * there won't be versions > 4.2.
411 } else if (api
== EGL_OPENGL_ES_API
) {
412 /* The EGL_KHR_create_context spec says:
414 * "* If an OpenGL ES context is requested and the values for
415 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
416 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
417 * is not defined, than an EGL_BAD_MATCH error is generated.
419 * ... Examples of invalid combinations of attributes include:
421 * - Major version < 1 or > 2
422 * - Major version == 1 and minor version < 0 or > 1
423 * - Major version == 2 and minor version != 0
425 if (ctx
->ClientMajorVersion
< 1 || ctx
->ClientMinorVersion
< 0)
428 switch (ctx
->ClientMajorVersion
) {
430 if (ctx
->ClientMinorVersion
> 1)
435 if (ctx
->ClientMinorVersion
> 0)
440 /* Don't put additional version checks here. We don't know that
441 * there won't be versions > 3.0.
451 switch (ctx
->ResetNotificationStrategy
) {
452 case EGL_NO_RESET_NOTIFICATION_KHR
:
453 case EGL_LOSE_CONTEXT_ON_RESET_KHR
:
457 err
= EGL_BAD_ATTRIBUTE
;
461 if ((ctx
->Flags
& ~(EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
462 | EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
463 | EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
)) != 0) {
464 err
= EGL_BAD_ATTRIBUTE
;
472 * Initialize the given _EGLContext object to defaults and/or the values
473 * in the attrib_list.
475 * According to EGL 1.5 Section 3.7:
477 * "EGL_OPENGL_API and EGL_OPENGL_ES_API are interchangeable for all
478 * purposes except eglCreateContext."
480 * And since we only support GL and GLES, this is the only place where the
481 * bound API matters at all. We look up the current API from the current
482 * thread, and stash that in the context we're initializing. Our caller is
483 * responsible for determining whether that's an API it supports.
486 _eglInitContext(_EGLContext
*ctx
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
487 const EGLint
*attrib_list
)
489 const EGLenum api
= eglQueryAPI();
492 if (api
== EGL_NONE
) {
493 _eglError(EGL_BAD_MATCH
, "eglCreateContext(no client API)");
497 _eglInitResource(&ctx
->Resource
, sizeof(*ctx
), dpy
);
498 ctx
->ClientAPI
= api
;
500 ctx
->WindowRenderBuffer
= EGL_NONE
;
501 ctx
->Profile
= EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
;
503 ctx
->ClientMajorVersion
= 1; /* the default, per EGL spec */
504 ctx
->ClientMinorVersion
= 0;
506 ctx
->Profile
= EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
;
507 ctx
->ResetNotificationStrategy
= EGL_NO_RESET_NOTIFICATION_KHR
;
509 err
= _eglParseContextAttribList(ctx
, dpy
, attrib_list
);
510 if (err
== EGL_SUCCESS
&& ctx
->Config
) {
513 api_bit
= _eglGetContextAPIBit(ctx
);
514 if (!(ctx
->Config
->RenderableType
& api_bit
)) {
515 _eglLog(_EGL_DEBUG
, "context api is 0x%x while config supports 0x%x",
516 api_bit
, ctx
->Config
->RenderableType
);
517 err
= EGL_BAD_CONFIG
;
520 if (err
!= EGL_SUCCESS
)
521 return _eglError(err
, "eglCreateContext");
528 _eglQueryContextRenderBuffer(_EGLContext
*ctx
)
530 _EGLSurface
*surf
= ctx
->DrawSurface
;
535 if (surf
->Type
== EGL_WINDOW_BIT
&& ctx
->WindowRenderBuffer
!= EGL_NONE
)
536 rb
= ctx
->WindowRenderBuffer
;
538 rb
= surf
->RenderBuffer
;
544 _eglQueryContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*c
,
545 EGLint attribute
, EGLint
*value
)
551 return _eglError(EGL_BAD_PARAMETER
, "eglQueryContext");
556 * From EGL_KHR_no_config_context:
558 * "Querying EGL_CONFIG_ID returns the ID of the EGLConfig with
559 * respect to which the context was created, or zero if created
560 * without respect to an EGLConfig."
562 *value
= c
->Config
? c
->Config
->ConfigID
: 0;
564 case EGL_CONTEXT_CLIENT_VERSION
:
565 *value
= c
->ClientMajorVersion
;
567 case EGL_CONTEXT_CLIENT_TYPE
:
568 *value
= c
->ClientAPI
;
570 case EGL_RENDER_BUFFER
:
571 *value
= _eglQueryContextRenderBuffer(c
);
574 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
582 * Bind the context to the thread and return the previous context.
584 * Note that the context may be NULL.
587 _eglBindContextToThread(_EGLContext
*ctx
, _EGLThreadInfo
*t
)
591 oldCtx
= t
->CurrentContext
;
594 oldCtx
->Binding
= NULL
;
598 t
->CurrentContext
= ctx
;
606 * Return true if the given context and surfaces can be made current.
609 _eglCheckMakeCurrent(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
)
611 _EGLThreadInfo
*t
= _eglGetCurrentThread();
614 if (_eglIsCurrentThreadDummy())
615 return _eglError(EGL_BAD_ALLOC
, "eglMakeCurrent");
620 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
624 dpy
= ctx
->Resource
.Display
;
625 if (!dpy
->Extensions
.KHR_surfaceless_context
626 && (draw
== NULL
|| read
== NULL
))
627 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
632 * "If ctx is current to some other thread, or if either draw or read are
633 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
638 * "at most one context may be bound to a particular surface at a given
641 if (ctx
->Binding
&& ctx
->Binding
!= t
)
642 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
643 if (draw
&& draw
->CurrentContext
&& draw
->CurrentContext
!= ctx
) {
644 if (draw
->CurrentContext
->Binding
!= t
)
645 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
647 if (read
&& read
->CurrentContext
&& read
->CurrentContext
!= ctx
) {
648 if (read
->CurrentContext
->Binding
!= t
)
649 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
652 /* If the context has a config then it must match that of the two
655 if ((draw
&& draw
->Config
!= ctx
->Config
) ||
656 (read
&& read
->Config
!= ctx
->Config
))
657 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
659 /* Otherwise we must be using the EGL_KHR_no_config_context
661 assert(dpy
->Extensions
.KHR_no_config_context
);
663 /* The extension doesn't permit binding draw and read buffers with
664 * differing contexts */
665 if (draw
&& read
&& draw
->Config
!= read
->Config
)
666 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
674 * Bind the context to the current thread and given surfaces. Return the
675 * previous bound context and surfaces. The caller should unreference the
676 * returned context and surfaces.
678 * Making a second call with the resources returned by the first call
679 * unsurprisingly undoes the first call, except for the resouce reference
683 _eglBindContext(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
,
684 _EGLContext
**old_ctx
,
685 _EGLSurface
**old_draw
, _EGLSurface
**old_read
)
687 _EGLThreadInfo
*t
= _eglGetCurrentThread();
688 _EGLContext
*prev_ctx
;
689 _EGLSurface
*prev_draw
, *prev_read
;
691 if (!_eglCheckMakeCurrent(ctx
, draw
, read
))
694 /* increment refcounts before binding */
696 _eglGetSurface(draw
);
697 _eglGetSurface(read
);
699 /* bind the new context */
700 prev_ctx
= _eglBindContextToThread(ctx
, t
);
702 /* break previous bindings */
704 prev_draw
= prev_ctx
->DrawSurface
;
705 prev_read
= prev_ctx
->ReadSurface
;
708 prev_draw
->CurrentContext
= NULL
;
710 prev_read
->CurrentContext
= NULL
;
712 prev_ctx
->DrawSurface
= NULL
;
713 prev_ctx
->ReadSurface
= NULL
;
716 prev_draw
= prev_read
= NULL
;
719 /* establish new bindings */
722 draw
->CurrentContext
= ctx
;
724 read
->CurrentContext
= ctx
;
726 ctx
->DrawSurface
= draw
;
727 ctx
->ReadSurface
= read
;
730 assert(old_ctx
&& old_draw
&& old_read
);
732 *old_draw
= prev_draw
;
733 *old_read
= prev_read
;