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
) {
116 EGLint renderable_type
, api_bit
;
118 renderable_type
= GET_CONFIG_ATTRIB(ctx
->Config
, EGL_RENDERABLE_TYPE
);
119 api_bit
= _eglGetContextAPIBit(ctx
);
120 if (!(renderable_type
& api_bit
)) {
121 _eglLog(_EGL_DEBUG
, "context api is 0x%x while config supports 0x%x",
122 api_bit
, renderable_type
);
123 err
= EGL_BAD_CONFIG
;
126 if (err
!= EGL_SUCCESS
)
127 return _eglError(err
, "eglCreateContext");
134 * Just a placeholder/demo function. Real driver will never use this!
137 _eglCreateContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
138 _EGLContext
*share_list
, const EGLint
*attrib_list
)
145 * Default fallback routine - drivers should usually override this.
148 _eglDestroyContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
150 if (!_eglIsContextBound(ctx
))
156 #ifdef EGL_VERSION_1_2
158 _eglQueryContextRenderBuffer(_EGLContext
*ctx
)
160 _EGLSurface
*surf
= ctx
->DrawSurface
;
165 if (surf
->Type
== EGL_WINDOW_BIT
&& ctx
->WindowRenderBuffer
!= EGL_NONE
)
166 rb
= ctx
->WindowRenderBuffer
;
168 rb
= surf
->RenderBuffer
;
171 #endif /* EGL_VERSION_1_2 */
175 _eglQueryContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*c
,
176 EGLint attribute
, EGLint
*value
)
182 return _eglError(EGL_BAD_PARAMETER
, "eglQueryContext");
186 *value
= GET_CONFIG_ATTRIB(c
->Config
, EGL_CONFIG_ID
);
188 case EGL_CONTEXT_CLIENT_VERSION
:
189 *value
= c
->ClientVersion
;
191 #ifdef EGL_VERSION_1_2
192 case EGL_CONTEXT_CLIENT_TYPE
:
193 *value
= c
->ClientAPI
;
195 case EGL_RENDER_BUFFER
:
196 *value
= _eglQueryContextRenderBuffer(c
);
198 #endif /* EGL_VERSION_1_2 */
200 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
208 * Bind the context to the surfaces. Return the surfaces that are "orphaned".
209 * That is, when the context is not NULL, return the surfaces it previously
210 * bound to; when the context is NULL, the same surfaces are returned.
213 _eglBindContextToSurfaces(_EGLContext
*newCtx
,
214 _EGLSurface
**draw
, _EGLSurface
**read
)
216 _EGLSurface
*newDraw
= *draw
, *newRead
= *read
;
220 * The goal is to bind a newCtx to newDraw. Since newDraw may already have
221 * a binding context (oldCtx), and newCtx may already be bound to another
222 * surface (oldDraw), the old bindings are broken first and the new one is
226 oldCtx
= newDraw
->CurrentContext
;
227 if (newCtx
!= oldCtx
) {
229 assert(oldCtx
->DrawSurface
== newDraw
);
230 oldCtx
->DrawSurface
= NULL
;
233 newDraw
->CurrentContext
= newCtx
;
238 _EGLSurface
*oldDraw
= newCtx
->DrawSurface
;
240 oldDraw
->CurrentContext
= NULL
;
242 newCtx
->DrawSurface
= newDraw
;
247 if (newRead
&& newRead
!= newDraw
) {
248 oldCtx
= newRead
->CurrentContext
;
249 if (newCtx
!= oldCtx
) {
251 assert(oldCtx
->ReadSurface
== newRead
);
252 oldCtx
->ReadSurface
= NULL
;
255 newRead
->CurrentContext
= newCtx
;
260 _EGLSurface
*oldRead
= newCtx
->ReadSurface
;
262 oldRead
->CurrentContext
= NULL
;
264 newCtx
->ReadSurface
= newRead
;
272 * Bind the context to the thread and return the previous context.
274 * Note that the context may be NULL.
277 _eglBindContextToThread(_EGLContext
*ctx
, _EGLThreadInfo
*t
)
283 _eglConvertApiToIndex(ctx
->ClientAPI
) : t
->CurrentAPIIndex
;
285 oldCtx
= t
->CurrentContexts
[apiIndex
];
288 oldCtx
->Binding
= NULL
;
292 t
->CurrentContexts
[apiIndex
] = ctx
;
300 * Return true if the given context and surfaces can be made current.
303 _eglCheckMakeCurrent(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
)
305 _EGLThreadInfo
*t
= _eglGetCurrentThread();
308 EGLBoolean surfaceless
;
310 if (_eglIsCurrentThreadDummy())
311 return _eglError(EGL_BAD_ALLOC
, "eglMakeCurrent");
316 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
320 dpy
= ctx
->Resource
.Display
;
321 switch (_eglGetContextAPIBit(ctx
)) {
322 case EGL_OPENGL_ES_BIT
:
323 surfaceless
= dpy
->Extensions
.KHR_surfaceless_gles1
;
325 case EGL_OPENGL_ES2_BIT
:
326 surfaceless
= dpy
->Extensions
.KHR_surfaceless_gles2
;
329 surfaceless
= dpy
->Extensions
.KHR_surfaceless_opengl
;
332 surfaceless
= EGL_FALSE
;
336 if (!surfaceless
&& (draw
== NULL
|| read
== NULL
))
337 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
339 /* context stealing from another thread is not allowed */
340 if (ctx
->Binding
&& ctx
->Binding
!= t
)
341 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
346 * "If ctx is current to some other thread, or if either draw or read are
347 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
352 * "at most one context may be bound to a particular surface at a given
355 * The latter is more restrictive so we can check only the latter case.
357 if ((draw
&& draw
->CurrentContext
&& draw
->CurrentContext
!= ctx
) ||
358 (read
&& read
->CurrentContext
&& read
->CurrentContext
!= ctx
))
359 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
361 /* simply require the configs to be equal */
362 if ((draw
&& draw
->Config
!= ctx
->Config
) ||
363 (read
&& read
->Config
!= ctx
->Config
))
364 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
366 switch (ctx
->ClientAPI
) {
367 #ifdef EGL_VERSION_1_4
368 /* OpenGL and OpenGL ES are conflicting */
369 case EGL_OPENGL_ES_API
:
370 conflict_api
= EGL_OPENGL_API
;
373 conflict_api
= EGL_OPENGL_ES_API
;
381 if (conflict_api
>= 0 && _eglGetAPIContext(conflict_api
))
382 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
389 * Bind the context to the current thread and given surfaces. Return the
390 * previously bound context and the surfaces it bound to. Each argument is
391 * both input and output.
394 _eglBindContext(_EGLContext
**ctx
, _EGLSurface
**draw
, _EGLSurface
**read
)
396 _EGLThreadInfo
*t
= _eglGetCurrentThread();
397 _EGLContext
*newCtx
= *ctx
, *oldCtx
;
399 if (!_eglCheckMakeCurrent(newCtx
, *draw
, *read
))
402 /* bind the new context */
403 oldCtx
= _eglBindContextToThread(newCtx
, t
);
406 _eglBindContextToSurfaces(newCtx
, draw
, read
);
408 /* unbind the old context from its binding surfaces */
409 if (oldCtx
&& oldCtx
!= newCtx
) {
410 assert(!*draw
&& !*read
);
412 *draw
= oldCtx
->DrawSurface
;
413 *read
= oldCtx
->ReadSurface
;
415 _eglBindContextToSurfaces(NULL
, draw
, read
);
419 /* draw and read have been updated in _eglBindContextToSurfaces */
426 * Just a placeholder/demo function. Drivers should override this.
429 _eglMakeCurrent(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*draw
,
430 _EGLSurface
*read
, _EGLContext
*ctx
)
437 * This is defined by the EGL_MESA_copy_context extension.
440 _eglCopyContextMESA(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext source
,
441 EGLContext dest
, EGLint mask
)
443 /* This function will always have to be overridden/implemented in the
444 * device driver. If the driver is based on Mesa, use _mesa_copy_context().