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 /* The EGL_KHR_create_context_spec says:
189 * "If the EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR bit is set in
190 * EGL_CONTEXT_FLAGS_KHR, then a context supporting <robust buffer
191 * access> will be created. Robust buffer access is defined in the
192 * GL_ARB_robustness extension specification, and the resulting
193 * context must also support either the GL_ARB_robustness
194 * extension, or a version of OpenGL incorporating equivalent
195 * functionality. This bit is supported for OpenGL contexts.
197 if ((val
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) &&
198 (api
!= EGL_OPENGL_API
||
199 !dpy
->Extensions
.EXT_create_context_robustness
)) {
200 err
= EGL_BAD_ATTRIBUTE
;
207 case EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
:
208 if (!dpy
->Extensions
.KHR_create_context
) {
209 err
= EGL_BAD_ATTRIBUTE
;
213 /* The EGL_KHR_create_context spec says:
215 * "[EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR] is only meaningful for
216 * OpenGL contexts, and specifying it for other types of
217 * contexts, including OpenGL ES contexts, will generate an
220 if (api
!= EGL_OPENGL_API
) {
221 err
= EGL_BAD_ATTRIBUTE
;
228 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR
:
229 /* The EGL_KHR_create_context spec says:
231 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR] is only
232 * meaningful for OpenGL contexts, and specifying it for other
233 * types of contexts, including OpenGL ES contexts, will generate
236 if (!dpy
->Extensions
.KHR_create_context
237 || api
!= EGL_OPENGL_API
) {
238 err
= EGL_BAD_ATTRIBUTE
;
242 ctx
->ResetNotificationStrategy
= val
;
245 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT
:
246 /* The EGL_EXT_create_context_robustness spec says:
248 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT] is only
249 * meaningful for OpenGL ES contexts, and specifying it for other
250 * types of contexts will generate an EGL_BAD_ATTRIBUTE error."
252 if (!dpy
->Extensions
.EXT_create_context_robustness
253 || api
!= EGL_OPENGL_ES_API
) {
254 err
= EGL_BAD_ATTRIBUTE
;
258 ctx
->ResetNotificationStrategy
= val
;
261 case EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT
:
262 if (!dpy
->Extensions
.EXT_create_context_robustness
) {
263 err
= EGL_BAD_ATTRIBUTE
;
268 ctx
->Flags
|= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
;
271 case EGL_CONTEXT_OPENGL_ROBUST_ACCESS
:
272 if (dpy
->Version
< 15) {
273 err
= EGL_BAD_ATTRIBUTE
;
278 ctx
->Flags
|= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
;
281 case EGL_CONTEXT_OPENGL_DEBUG
:
282 if (dpy
->Version
< 15) {
283 err
= EGL_BAD_ATTRIBUTE
;
288 ctx
->Flags
|= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
;
291 case EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE
:
292 if (dpy
->Version
< 15) {
293 err
= EGL_BAD_ATTRIBUTE
;
298 ctx
->Flags
|= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
;
302 err
= EGL_BAD_ATTRIBUTE
;
306 if (err
!= EGL_SUCCESS
) {
307 _eglLog(_EGL_DEBUG
, "bad context attribute 0x%04x", attr
);
312 if (api
== EGL_OPENGL_API
) {
313 /* The EGL_KHR_create_context spec says:
315 * "If the requested OpenGL version is less than 3.2,
316 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR is ignored and the
317 * functionality of the context is determined solely by the
318 * requested version."
320 * Since the value is ignored, only validate the setting if the version
323 if (ctx
->ClientMajorVersion
>= 4
324 || (ctx
->ClientMajorVersion
== 3 && ctx
->ClientMinorVersion
>= 2)) {
325 switch (ctx
->Profile
) {
326 case EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
:
327 case EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR
:
331 /* The EGL_KHR_create_context spec says:
333 * "* If an OpenGL context is requested, the requested version
334 * is greater than 3.2, and the value for attribute
335 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has
336 * any bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
337 * and EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has
338 * more than one of these bits set; or if the implementation does
339 * not support the requested profile, then an EGL_BAD_MATCH error
347 /* The EGL_KHR_create_context spec says:
349 * "* If an OpenGL context is requested and the values for
350 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
351 * EGL_CONTEXT_MINOR_VERSION_KHR, when considered together with
352 * the value for attribute
353 * EGL_CONTEXT_FORWARD_COMPATIBLE_BIT_KHR, specify an OpenGL
354 * version and feature set that are not defined, than an
355 * EGL_BAD_MATCH error is generated.
357 * ... Thus, examples of invalid combinations of attributes
360 * - Major version < 1 or > 4
361 * - Major version == 1 and minor version < 0 or > 5
362 * - Major version == 2 and minor version < 0 or > 1
363 * - Major version == 3 and minor version < 0 or > 2
364 * - Major version == 4 and minor version < 0 or > 2
365 * - Forward-compatible flag set and major version < 3"
367 if (ctx
->ClientMajorVersion
< 1 || ctx
->ClientMinorVersion
< 0)
370 switch (ctx
->ClientMajorVersion
) {
372 if (ctx
->ClientMinorVersion
> 5
373 || (ctx
->Flags
& EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
) != 0)
378 if (ctx
->ClientMinorVersion
> 1
379 || (ctx
->Flags
& EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
) != 0)
384 /* Note: The text above is incorrect. There *is* an OpenGL 3.3!
386 if (ctx
->ClientMinorVersion
> 3)
392 /* Don't put additional version checks here. We don't know that
393 * there won't be versions > 4.2.
397 } else if (api
== EGL_OPENGL_ES_API
) {
398 /* The EGL_KHR_create_context spec says:
400 * "* If an OpenGL ES context is requested and the values for
401 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
402 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
403 * is not defined, than an EGL_BAD_MATCH error is generated.
405 * ... Examples of invalid combinations of attributes include:
407 * - Major version < 1 or > 2
408 * - Major version == 1 and minor version < 0 or > 1
409 * - Major version == 2 and minor version != 0
411 if (ctx
->ClientMajorVersion
< 1 || ctx
->ClientMinorVersion
< 0)
414 switch (ctx
->ClientMajorVersion
) {
416 if (ctx
->ClientMinorVersion
> 1)
421 if (ctx
->ClientMinorVersion
> 0)
426 /* Don't put additional version checks here. We don't know that
427 * there won't be versions > 3.0.
437 switch (ctx
->ResetNotificationStrategy
) {
438 case EGL_NO_RESET_NOTIFICATION_KHR
:
439 case EGL_LOSE_CONTEXT_ON_RESET_KHR
:
443 err
= EGL_BAD_ATTRIBUTE
;
447 if ((ctx
->Flags
& ~(EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
448 | EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
449 | EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
)) != 0) {
450 err
= EGL_BAD_ATTRIBUTE
;
458 * Initialize the given _EGLContext object to defaults and/or the values
459 * in the attrib_list.
462 _eglInitContext(_EGLContext
*ctx
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
463 const EGLint
*attrib_list
)
465 const EGLenum api
= eglQueryAPI();
468 if (api
== EGL_NONE
) {
469 _eglError(EGL_BAD_MATCH
, "eglCreateContext(no client API)");
473 _eglInitResource(&ctx
->Resource
, sizeof(*ctx
), dpy
);
474 ctx
->ClientAPI
= api
;
476 ctx
->WindowRenderBuffer
= EGL_NONE
;
477 ctx
->Profile
= EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
;
479 ctx
->ClientMajorVersion
= 1; /* the default, per EGL spec */
480 ctx
->ClientMinorVersion
= 0;
482 ctx
->Profile
= EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
;
483 ctx
->ResetNotificationStrategy
= EGL_NO_RESET_NOTIFICATION_KHR
;
485 err
= _eglParseContextAttribList(ctx
, dpy
, attrib_list
);
486 if (err
== EGL_SUCCESS
&& ctx
->Config
) {
489 api_bit
= _eglGetContextAPIBit(ctx
);
490 if (!(ctx
->Config
->RenderableType
& api_bit
)) {
491 _eglLog(_EGL_DEBUG
, "context api is 0x%x while config supports 0x%x",
492 api_bit
, ctx
->Config
->RenderableType
);
493 err
= EGL_BAD_CONFIG
;
496 if (err
!= EGL_SUCCESS
)
497 return _eglError(err
, "eglCreateContext");
504 _eglQueryContextRenderBuffer(_EGLContext
*ctx
)
506 _EGLSurface
*surf
= ctx
->DrawSurface
;
511 if (surf
->Type
== EGL_WINDOW_BIT
&& ctx
->WindowRenderBuffer
!= EGL_NONE
)
512 rb
= ctx
->WindowRenderBuffer
;
514 rb
= surf
->RenderBuffer
;
520 _eglQueryContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*c
,
521 EGLint attribute
, EGLint
*value
)
527 return _eglError(EGL_BAD_PARAMETER
, "eglQueryContext");
532 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
533 *value
= c
->Config
->ConfigID
;
535 case EGL_CONTEXT_CLIENT_VERSION
:
536 *value
= c
->ClientMajorVersion
;
538 case EGL_CONTEXT_CLIENT_TYPE
:
539 *value
= c
->ClientAPI
;
541 case EGL_RENDER_BUFFER
:
542 *value
= _eglQueryContextRenderBuffer(c
);
545 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
553 * Bind the context to the thread and return the previous context.
555 * Note that the context may be NULL.
558 _eglBindContextToThread(_EGLContext
*ctx
, _EGLThreadInfo
*t
)
564 _eglConvertApiToIndex(ctx
->ClientAPI
) : t
->CurrentAPIIndex
;
566 oldCtx
= t
->CurrentContexts
[apiIndex
];
569 oldCtx
->Binding
= NULL
;
573 t
->CurrentContexts
[apiIndex
] = ctx
;
581 * Return true if the given context and surfaces can be made current.
584 _eglCheckMakeCurrent(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
)
586 _EGLThreadInfo
*t
= _eglGetCurrentThread();
590 if (_eglIsCurrentThreadDummy())
591 return _eglError(EGL_BAD_ALLOC
, "eglMakeCurrent");
596 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
600 dpy
= ctx
->Resource
.Display
;
601 if (!dpy
->Extensions
.KHR_surfaceless_context
602 && (draw
== NULL
|| read
== NULL
))
603 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
608 * "If ctx is current to some other thread, or if either draw or read are
609 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
614 * "at most one context may be bound to a particular surface at a given
617 if (ctx
->Binding
&& ctx
->Binding
!= t
)
618 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
619 if (draw
&& draw
->CurrentContext
&& draw
->CurrentContext
!= ctx
) {
620 if (draw
->CurrentContext
->Binding
!= t
||
621 draw
->CurrentContext
->ClientAPI
!= ctx
->ClientAPI
)
622 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
624 if (read
&& read
->CurrentContext
&& read
->CurrentContext
!= ctx
) {
625 if (read
->CurrentContext
->Binding
!= t
||
626 read
->CurrentContext
->ClientAPI
!= ctx
->ClientAPI
)
627 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
630 /* If the context has a config then it must match that of the two
633 if ((draw
&& draw
->Config
!= ctx
->Config
) ||
634 (read
&& read
->Config
!= ctx
->Config
))
635 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
637 /* Otherwise we must be using the EGL_MESA_configless_context
639 assert(dpy
->Extensions
.MESA_configless_context
);
641 /* The extension doesn't permit binding draw and read buffers with
642 * differing contexts */
643 if (draw
&& read
&& draw
->Config
!= read
->Config
)
644 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
647 switch (ctx
->ClientAPI
) {
648 /* OpenGL and OpenGL ES are conflicting */
649 case EGL_OPENGL_ES_API
:
650 conflict_api
= EGL_OPENGL_API
;
653 conflict_api
= EGL_OPENGL_ES_API
;
660 if (conflict_api
>= 0 && _eglGetAPIContext(conflict_api
))
661 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
668 * Bind the context to the current thread and given surfaces. Return the
669 * previous bound context and surfaces. The caller should unreference the
670 * returned context and surfaces.
672 * Making a second call with the resources returned by the first call
673 * unsurprisingly undoes the first call, except for the resouce reference
677 _eglBindContext(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
,
678 _EGLContext
**old_ctx
,
679 _EGLSurface
**old_draw
, _EGLSurface
**old_read
)
681 _EGLThreadInfo
*t
= _eglGetCurrentThread();
682 _EGLContext
*prev_ctx
;
683 _EGLSurface
*prev_draw
, *prev_read
;
685 if (!_eglCheckMakeCurrent(ctx
, draw
, read
))
688 /* increment refcounts before binding */
690 _eglGetSurface(draw
);
691 _eglGetSurface(read
);
693 /* bind the new context */
694 prev_ctx
= _eglBindContextToThread(ctx
, t
);
696 /* break previous bindings */
698 prev_draw
= prev_ctx
->DrawSurface
;
699 prev_read
= prev_ctx
->ReadSurface
;
702 prev_draw
->CurrentContext
= NULL
;
704 prev_read
->CurrentContext
= NULL
;
706 prev_ctx
->DrawSurface
= NULL
;
707 prev_ctx
->ReadSurface
= NULL
;
710 prev_draw
= prev_read
= NULL
;
713 /* establish new bindings */
716 draw
->CurrentContext
= ctx
;
718 read
->CurrentContext
= ctx
;
720 ctx
->DrawSurface
= draw
;
721 ctx
->ReadSurface
= read
;
724 assert(old_ctx
&& old_draw
&& old_read
);
726 *old_draw
= prev_draw
;
727 *old_read
= prev_read
;