1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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;
78 * Parse the list of context attributes and return the proper error code.
81 _eglParseContextAttribList(_EGLContext *ctx, _EGLDisplay *dpy,
82 const EGLint *attrib_list)
84 EGLenum api = ctx->ClientAPI;
85 EGLint i, err = EGL_SUCCESS;
90 if (api == EGL_OPENVG_API && attrib_list[0] != EGL_NONE) {
91 _eglLog(_EGL_DEBUG, "bad context attribute 0x%04x", attrib_list[0]);
92 return EGL_BAD_ATTRIBUTE;
95 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
96 EGLint attr = attrib_list[i++];
97 EGLint val = attrib_list[i];
100 case EGL_CONTEXT_CLIENT_VERSION:
101 ctx->ClientMajorVersion = val;
104 case EGL_CONTEXT_MINOR_VERSION_KHR:
105 if (!dpy->Extensions.KHR_create_context) {
106 err = EGL_BAD_ATTRIBUTE;
110 ctx->ClientMinorVersion = val;
113 case EGL_CONTEXT_FLAGS_KHR:
114 if (!dpy->Extensions.KHR_create_context) {
115 err = EGL_BAD_ATTRIBUTE;
119 /* The EGL_KHR_create_context spec says:
121 * "Flags are only defined for OpenGL context creation, and
122 * specifying a flags value other than zero for other types of
123 * contexts, including OpenGL ES contexts, will generate an
126 if (api != EGL_OPENGL_API && val != 0) {
127 err = EGL_BAD_ATTRIBUTE;
134 case EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR:
135 if (!dpy->Extensions.KHR_create_context) {
136 err = EGL_BAD_ATTRIBUTE;
140 /* The EGL_KHR_create_context spec says:
142 * "[EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR] is only meaningful for
143 * OpenGL contexts, and specifying it for other types of
144 * contexts, including OpenGL ES contexts, will generate an
147 if (api != EGL_OPENGL_API) {
148 err = EGL_BAD_ATTRIBUTE;
155 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR:
156 /* The EGL_KHR_create_context spec says:
158 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR] is only
159 * meaningful for OpenGL contexts, and specifying it for other
160 * types of contexts, including OpenGL ES contexts, will generate
163 if (!dpy->Extensions.KHR_create_context
164 || api != EGL_OPENGL_API) {
165 err = EGL_BAD_ATTRIBUTE;
169 ctx->ResetNotificationStrategy = val;
173 err = EGL_BAD_ATTRIBUTE;
177 if (err != EGL_SUCCESS) {
178 _eglLog(_EGL_DEBUG, "bad context attribute 0x%04x", attr);
183 if (api == EGL_OPENGL_API) {
184 /* The EGL_KHR_create_context spec says:
186 * "If the requested OpenGL version is less than 3.2,
187 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR is ignored and the
188 * functionality of the context is determined solely by the
189 * requested version."
191 * Since the value is ignored, only validate the setting if the version
194 if (ctx->ClientMajorVersion >= 4
195 || (ctx->ClientMajorVersion == 3 && ctx->ClientMinorVersion >= 2)) {
196 switch (ctx->Profile) {
197 case EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR:
198 case EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR:
202 /* The EGL_KHR_create_context spec says:
204 * "* If an OpenGL context is requested, the requested version
205 * is greater than 3.2, and the value for attribute
206 * EGL_CONTEXT_PROFILE_MASK_KHR has no bits set; has any
207 * bits set other than EGL_CONTEXT_CORE_PROFILE_BIT_KHR and
208 * EGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_KHR; has more than
209 * one of these bits set; or if the implementation does not
210 * support the requested profile, then an
211 * EGL_BAD_PROFILE_KHR error is generated."
213 * However, it does not define EGL_BAD_PROFILE_KHR. For now use
216 err = EGL_BAD_ATTRIBUTE;
221 /* The EGL_KHR_create_context spec says:
223 * "* If an OpenGL context is requested and the values for
224 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
225 * EGL_CONTEXT_MINOR_VERSION_KHR, when considered together with
226 * the value for attribute
227 * EGL_CONTEXT_FORWARD_COMPATIBLE_BIT_KHR, specify an OpenGL
228 * version and feature set that are not defined, than an
229 * EGL_BAD_MATCH error is generated.
231 * ... Thus, examples of invalid combinations of attributes
234 * - Major version < 1 or > 4
235 * - Major version == 1 and minor version < 0 or > 5
236 * - Major version == 2 and minor version < 0 or > 1
237 * - Major version == 3 and minor version < 0 or > 2
238 * - Major version == 4 and minor version < 0 or > 2
239 * - Forward-compatible flag set and major version < 3"
241 if (ctx->ClientMajorVersion < 1 || ctx->ClientMinorVersion < 0)
244 switch (ctx->ClientMajorVersion) {
246 if (ctx->ClientMinorVersion > 5
247 || (ctx->Flags & EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR) != 0)
252 if (ctx->ClientMinorVersion > 1
253 || (ctx->Flags & EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR) != 0)
258 /* Note: The text above is incorrect. There *is* an OpenGL 3.3!
260 if (ctx->ClientMinorVersion > 3)
266 /* Don't put additional version checks here. We don't know that
267 * there won't be versions > 4.2.
271 } else if (api == EGL_OPENGL_ES_API) {
272 /* The EGL_KHR_create_context spec says:
274 * "* If an OpenGL ES context is requested and the values for
275 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
276 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
277 * is not defined, than an EGL_BAD_MATCH error is generated.
279 * ... Examples of invalid combinations of attributes include:
281 * - Major version < 1 or > 2
282 * - Major version == 1 and minor version < 0 or > 1
283 * - Major version == 2 and minor version != 0
285 if (ctx->ClientMajorVersion < 1 || ctx->ClientMinorVersion < 0)
288 switch (ctx->ClientMajorVersion) {
290 if (ctx->ClientMinorVersion > 1)
296 /* Don't put additional version checks here. We don't know that
297 * there won't be versions > 2.0.
303 switch (ctx->ResetNotificationStrategy) {
304 case EGL_NO_RESET_NOTIFICATION_KHR:
305 case EGL_LOSE_CONTEXT_ON_RESET_KHR:
309 err = EGL_BAD_ATTRIBUTE;
313 if ((ctx->Flags & (EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
314 | EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
315 | EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR)) != 0) {
316 err = EGL_BAD_ATTRIBUTE;
324 * Initialize the given _EGLContext object to defaults and/or the values
325 * in the attrib_list.
328 _eglInitContext(_EGLContext *ctx, _EGLDisplay *dpy, _EGLConfig *conf,
329 const EGLint *attrib_list)
331 const EGLenum api = eglQueryAPI();
334 if (api == EGL_NONE) {
335 _eglError(EGL_BAD_MATCH, "eglCreateContext(no client API)");
339 _eglInitResource(&ctx->Resource, sizeof(*ctx), dpy);
340 ctx->ClientAPI = api;
342 ctx->WindowRenderBuffer = EGL_NONE;
343 ctx->Profile = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
345 ctx->ClientMajorVersion = 1; /* the default, per EGL spec */
346 ctx->ClientMinorVersion = 0;
348 ctx->Profile = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
349 ctx->ResetNotificationStrategy = EGL_NO_RESET_NOTIFICATION_KHR;
351 err = _eglParseContextAttribList(ctx, dpy, attrib_list);
352 if (err == EGL_SUCCESS && ctx->Config) {
355 api_bit = _eglGetContextAPIBit(ctx);
356 if (!(ctx->Config->RenderableType & api_bit)) {
357 _eglLog(_EGL_DEBUG, "context api is 0x%x while config supports 0x%x",
358 api_bit, ctx->Config->RenderableType);
359 err = EGL_BAD_CONFIG;
362 if (err != EGL_SUCCESS)
363 return _eglError(err, "eglCreateContext");
370 _eglQueryContextRenderBuffer(_EGLContext *ctx)
372 _EGLSurface *surf = ctx->DrawSurface;
377 if (surf->Type == EGL_WINDOW_BIT && ctx->WindowRenderBuffer != EGL_NONE)
378 rb = ctx->WindowRenderBuffer;
380 rb = surf->RenderBuffer;
386 _eglQueryContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *c,
387 EGLint attribute, EGLint *value)
393 return _eglError(EGL_BAD_PARAMETER, "eglQueryContext");
398 return _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
399 *value = c->Config->ConfigID;
401 case EGL_CONTEXT_CLIENT_VERSION:
402 *value = c->ClientMajorVersion;
404 case EGL_CONTEXT_CLIENT_TYPE:
405 *value = c->ClientAPI;
407 case EGL_RENDER_BUFFER:
408 *value = _eglQueryContextRenderBuffer(c);
411 return _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
419 * Bind the context to the thread and return the previous context.
421 * Note that the context may be NULL.
424 _eglBindContextToThread(_EGLContext *ctx, _EGLThreadInfo *t)
430 _eglConvertApiToIndex(ctx->ClientAPI) : t->CurrentAPIIndex;
432 oldCtx = t->CurrentContexts[apiIndex];
435 oldCtx->Binding = NULL;
439 t->CurrentContexts[apiIndex] = ctx;
447 * Return true if the given context and surfaces can be made current.
450 _eglCheckMakeCurrent(_EGLContext *ctx, _EGLSurface *draw, _EGLSurface *read)
452 _EGLThreadInfo *t = _eglGetCurrentThread();
456 if (_eglIsCurrentThreadDummy())
457 return _eglError(EGL_BAD_ALLOC, "eglMakeCurrent");
462 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
466 dpy = ctx->Resource.Display;
467 if (!dpy->Extensions.KHR_surfaceless_context
468 && (draw == NULL || read == NULL))
469 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
474 * "If ctx is current to some other thread, or if either draw or read are
475 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
480 * "at most one context may be bound to a particular surface at a given
483 if (ctx->Binding && ctx->Binding != t)
484 return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
485 if (draw && draw->CurrentContext && draw->CurrentContext != ctx) {
486 if (draw->CurrentContext->Binding != t ||
487 draw->CurrentContext->ClientAPI != ctx->ClientAPI)
488 return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
490 if (read && read->CurrentContext && read->CurrentContext != ctx) {
491 if (read->CurrentContext->Binding != t ||
492 read->CurrentContext->ClientAPI != ctx->ClientAPI)
493 return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
496 /* simply require the configs to be equal */
497 if ((draw && draw->Config != ctx->Config) ||
498 (read && read->Config != ctx->Config))
499 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
501 switch (ctx->ClientAPI) {
502 /* OpenGL and OpenGL ES are conflicting */
503 case EGL_OPENGL_ES_API:
504 conflict_api = EGL_OPENGL_API;
507 conflict_api = EGL_OPENGL_ES_API;
514 if (conflict_api >= 0 && _eglGetAPIContext(conflict_api))
515 return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
522 * Bind the context to the current thread and given surfaces. Return the
523 * previous bound context and surfaces. The caller should unreference the
524 * returned context and surfaces.
526 * Making a second call with the resources returned by the first call
527 * unsurprisingly undoes the first call, except for the resouce reference
531 _eglBindContext(_EGLContext *ctx, _EGLSurface *draw, _EGLSurface *read,
532 _EGLContext **old_ctx,
533 _EGLSurface **old_draw, _EGLSurface **old_read)
535 _EGLThreadInfo *t = _eglGetCurrentThread();
536 _EGLContext *prev_ctx;
537 _EGLSurface *prev_draw, *prev_read;
539 if (!_eglCheckMakeCurrent(ctx, draw, read))
542 /* increment refcounts before binding */
544 _eglGetSurface(draw);
545 _eglGetSurface(read);
547 /* bind the new context */
548 prev_ctx = _eglBindContextToThread(ctx, t);
550 /* break previous bindings */
552 prev_draw = prev_ctx->DrawSurface;
553 prev_read = prev_ctx->ReadSurface;
556 prev_draw->CurrentContext = NULL;
558 prev_read->CurrentContext = NULL;
560 prev_ctx->DrawSurface = NULL;
561 prev_ctx->ReadSurface = NULL;
564 prev_draw = prev_read = NULL;
567 /* establish new bindings */
570 draw->CurrentContext = ctx;
572 read->CurrentContext = ctx;
574 ctx->DrawSurface = draw;
575 ctx->ReadSurface = read;
578 assert(old_ctx && old_draw && old_read);
580 *old_draw = prev_draw;
581 *old_read = prev_read;