5 #include "eglcontext.h"
6 #include "egldisplay.h"
7 #include "eglcurrent.h"
8 #include "eglsurface.h"
13 * Return the API bit (one of EGL_xxx_BIT) of the context.
16 _eglGetContextAPIBit(_EGLContext
*ctx
)
20 switch (ctx
->ClientAPI
) {
21 case EGL_OPENGL_ES_API
:
22 switch (ctx
->ClientVersion
) {
24 bit
= EGL_OPENGL_ES_BIT
;
27 bit
= EGL_OPENGL_ES2_BIT
;
48 * Parse the list of context attributes and return the proper error code.
51 _eglParseContextAttribList(_EGLContext
*ctx
, const EGLint
*attrib_list
)
53 EGLenum api
= ctx
->ClientAPI
;
54 EGLint i
, err
= EGL_SUCCESS
;
59 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
++) {
60 EGLint attr
= attrib_list
[i
++];
61 EGLint val
= attrib_list
[i
];
64 case EGL_CONTEXT_CLIENT_VERSION
:
65 if (api
!= EGL_OPENGL_ES_API
) {
66 err
= EGL_BAD_ATTRIBUTE
;
69 if (val
!= 1 && val
!= 2) {
70 err
= EGL_BAD_ATTRIBUTE
;
73 ctx
->ClientVersion
= val
;
76 err
= EGL_BAD_ATTRIBUTE
;
80 if (err
!= EGL_SUCCESS
) {
81 _eglLog(_EGL_DEBUG
, "bad context attribute 0x%04x", attr
);
91 * Initialize the given _EGLContext object to defaults and/or the values
95 _eglInitContext(_EGLContext
*ctx
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
96 const EGLint
*attrib_list
)
98 const EGLenum api
= eglQueryAPI();
101 if (api
== EGL_NONE
) {
102 _eglError(EGL_BAD_MATCH
, "eglCreateContext(no client API)");
106 memset(ctx
, 0, sizeof(_EGLContext
));
107 ctx
->Resource
.Display
= dpy
;
108 ctx
->ClientAPI
= api
;
110 ctx
->WindowRenderBuffer
= EGL_NONE
;
112 ctx
->ClientVersion
= 1; /* the default, per EGL spec */
114 err
= _eglParseContextAttribList(ctx
, attrib_list
);
115 if (err
== EGL_SUCCESS
&& ctx
->Config
) {
118 api_bit
= _eglGetContextAPIBit(ctx
);
119 if (!(ctx
->Config
->RenderableType
& api_bit
)) {
120 _eglLog(_EGL_DEBUG
, "context api is 0x%x while config supports 0x%x",
121 api_bit
, ctx
->Config
->RenderableType
);
122 err
= EGL_BAD_CONFIG
;
125 if (err
!= EGL_SUCCESS
)
126 return _eglError(err
, "eglCreateContext");
132 #ifdef EGL_VERSION_1_2
134 _eglQueryContextRenderBuffer(_EGLContext
*ctx
)
136 _EGLSurface
*surf
= ctx
->DrawSurface
;
141 if (surf
->Type
== EGL_WINDOW_BIT
&& ctx
->WindowRenderBuffer
!= EGL_NONE
)
142 rb
= ctx
->WindowRenderBuffer
;
144 rb
= surf
->RenderBuffer
;
147 #endif /* EGL_VERSION_1_2 */
151 _eglQueryContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*c
,
152 EGLint attribute
, EGLint
*value
)
158 return _eglError(EGL_BAD_PARAMETER
, "eglQueryContext");
163 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
164 *value
= c
->Config
->ConfigID
;
166 case EGL_CONTEXT_CLIENT_VERSION
:
167 *value
= c
->ClientVersion
;
169 #ifdef EGL_VERSION_1_2
170 case EGL_CONTEXT_CLIENT_TYPE
:
171 *value
= c
->ClientAPI
;
173 case EGL_RENDER_BUFFER
:
174 *value
= _eglQueryContextRenderBuffer(c
);
176 #endif /* EGL_VERSION_1_2 */
178 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
186 * Bind the context to the thread and return the previous context.
188 * Note that the context may be NULL.
191 _eglBindContextToThread(_EGLContext
*ctx
, _EGLThreadInfo
*t
)
197 _eglConvertApiToIndex(ctx
->ClientAPI
) : t
->CurrentAPIIndex
;
199 oldCtx
= t
->CurrentContexts
[apiIndex
];
202 oldCtx
->Binding
= NULL
;
206 t
->CurrentContexts
[apiIndex
] = ctx
;
214 * Return true if the given context and surfaces can be made current.
217 _eglCheckMakeCurrent(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
)
219 _EGLThreadInfo
*t
= _eglGetCurrentThread();
222 EGLBoolean surfaceless
;
224 if (_eglIsCurrentThreadDummy())
225 return _eglError(EGL_BAD_ALLOC
, "eglMakeCurrent");
230 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
234 dpy
= ctx
->Resource
.Display
;
235 switch (_eglGetContextAPIBit(ctx
)) {
236 case EGL_OPENGL_ES_BIT
:
237 surfaceless
= dpy
->Extensions
.KHR_surfaceless_gles1
;
239 case EGL_OPENGL_ES2_BIT
:
240 surfaceless
= dpy
->Extensions
.KHR_surfaceless_gles2
;
243 surfaceless
= dpy
->Extensions
.KHR_surfaceless_opengl
;
246 surfaceless
= EGL_FALSE
;
250 if (!surfaceless
&& (draw
== NULL
|| read
== NULL
))
251 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
253 /* context stealing from another thread is not allowed */
254 if (ctx
->Binding
&& ctx
->Binding
!= t
)
255 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
260 * "If ctx is current to some other thread, or if either draw or read are
261 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
266 * "at most one context may be bound to a particular surface at a given
269 * The latter is more restrictive so we can check only the latter case.
271 if ((draw
&& draw
->CurrentContext
&& draw
->CurrentContext
!= ctx
) ||
272 (read
&& read
->CurrentContext
&& read
->CurrentContext
!= ctx
))
273 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
275 /* simply require the configs to be equal */
276 if ((draw
&& draw
->Config
!= ctx
->Config
) ||
277 (read
&& read
->Config
!= ctx
->Config
))
278 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
280 switch (ctx
->ClientAPI
) {
281 #ifdef EGL_VERSION_1_4
282 /* OpenGL and OpenGL ES are conflicting */
283 case EGL_OPENGL_ES_API
:
284 conflict_api
= EGL_OPENGL_API
;
287 conflict_api
= EGL_OPENGL_ES_API
;
295 if (conflict_api
>= 0 && _eglGetAPIContext(conflict_api
))
296 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
303 * Bind the context to the current thread and given surfaces. Return the
304 * "orphaned" context and surfaces. Each argument is both input and output.
307 _eglBindContext(_EGLContext
**ctx
, _EGLSurface
**draw
, _EGLSurface
**read
)
309 _EGLThreadInfo
*t
= _eglGetCurrentThread();
310 _EGLContext
*newCtx
= *ctx
, *oldCtx
;
311 _EGLSurface
*newDraw
= *draw
, *newRead
= *read
;
313 if (!_eglCheckMakeCurrent(newCtx
, newDraw
, newRead
))
316 /* bind the new context */
317 oldCtx
= _eglBindContextToThread(newCtx
, t
);
319 /* break old bindings */
322 *draw
= oldCtx
->DrawSurface
;
323 *read
= oldCtx
->ReadSurface
;
326 (*draw
)->CurrentContext
= NULL
;
328 (*read
)->CurrentContext
= NULL
;
330 oldCtx
->DrawSurface
= NULL
;
331 oldCtx
->ReadSurface
= NULL
;
334 /* establish new bindings */
337 newDraw
->CurrentContext
= newCtx
;
339 newRead
->CurrentContext
= newCtx
;
341 newCtx
->DrawSurface
= newDraw
;
342 newCtx
->ReadSurface
= newRead
;
345 /* an old context or surface is not orphaned if it is still bound */
348 if (*draw
== newDraw
|| *draw
== newRead
)
350 if (*read
== newDraw
|| *read
== newRead
)