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
);
86 if (err
== EGL_SUCCESS
) {
87 EGLint renderable_type
, api_bit
;
89 renderable_type
= GET_CONFIG_ATTRIB(ctx
->Config
, EGL_RENDERABLE_TYPE
);
90 api_bit
= _eglGetContextAPIBit(ctx
);
91 if (!(renderable_type
& api_bit
))
100 * Initialize the given _EGLContext object to defaults and/or the values
101 * in the attrib_list.
104 _eglInitContext(_EGLContext
*ctx
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
105 const EGLint
*attrib_list
)
107 const EGLenum api
= eglQueryAPI();
110 if (api
== EGL_NONE
) {
111 _eglError(EGL_BAD_MATCH
, "eglCreateContext(no client API)");
115 memset(ctx
, 0, sizeof(_EGLContext
));
116 ctx
->Resource
.Display
= dpy
;
117 ctx
->ClientAPI
= api
;
119 ctx
->WindowRenderBuffer
= EGL_NONE
;
121 ctx
->ClientVersion
= 1; /* the default, per EGL spec */
123 err
= _eglParseContextAttribList(ctx
, attrib_list
);
124 if (err
!= EGL_SUCCESS
)
125 return _eglError(err
, "eglCreateContext");
132 * Just a placeholder/demo function. Real driver will never use this!
135 _eglCreateContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLConfig
*conf
,
136 _EGLContext
*share_list
, const EGLint
*attrib_list
)
143 * Default fallback routine - drivers should usually override this.
146 _eglDestroyContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
148 if (!_eglIsContextBound(ctx
))
154 #ifdef EGL_VERSION_1_2
156 _eglQueryContextRenderBuffer(_EGLContext
*ctx
)
158 _EGLSurface
*surf
= ctx
->DrawSurface
;
163 if (surf
->Type
== EGL_WINDOW_BIT
&& ctx
->WindowRenderBuffer
!= EGL_NONE
)
164 rb
= ctx
->WindowRenderBuffer
;
166 rb
= surf
->RenderBuffer
;
169 #endif /* EGL_VERSION_1_2 */
173 _eglQueryContext(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*c
,
174 EGLint attribute
, EGLint
*value
)
180 return _eglError(EGL_BAD_PARAMETER
, "eglQueryContext");
184 *value
= GET_CONFIG_ATTRIB(c
->Config
, EGL_CONFIG_ID
);
186 case EGL_CONTEXT_CLIENT_VERSION
:
187 *value
= c
->ClientVersion
;
189 #ifdef EGL_VERSION_1_2
190 case EGL_CONTEXT_CLIENT_TYPE
:
191 *value
= c
->ClientAPI
;
193 case EGL_RENDER_BUFFER
:
194 *value
= _eglQueryContextRenderBuffer(c
);
196 #endif /* EGL_VERSION_1_2 */
198 return _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
206 * Bind the context to the surfaces. Return the surfaces that are "orphaned".
207 * That is, when the context is not NULL, return the surfaces it previously
208 * bound to; when the context is NULL, the same surfaces are returned.
211 _eglBindContextToSurfaces(_EGLContext
*ctx
,
212 _EGLSurface
**draw
, _EGLSurface
**read
)
214 _EGLSurface
*newDraw
= *draw
, *newRead
= *read
;
216 if (newDraw
->CurrentContext
)
217 newDraw
->CurrentContext
->DrawSurface
= NULL
;
218 newDraw
->CurrentContext
= ctx
;
220 if (newRead
->CurrentContext
)
221 newRead
->CurrentContext
->ReadSurface
= NULL
;
222 newRead
->CurrentContext
= ctx
;
225 *draw
= ctx
->DrawSurface
;
226 ctx
->DrawSurface
= newDraw
;
228 *read
= ctx
->ReadSurface
;
229 ctx
->ReadSurface
= newRead
;
235 * Bind the context to the thread and return the previous context.
237 * Note that the context may be NULL.
240 _eglBindContextToThread(_EGLContext
*ctx
, _EGLThreadInfo
*t
)
246 _eglConvertApiToIndex(ctx
->ClientAPI
) : t
->CurrentAPIIndex
;
248 oldCtx
= t
->CurrentContexts
[apiIndex
];
253 oldCtx
->Binding
= NULL
;
257 t
->CurrentContexts
[apiIndex
] = ctx
;
264 * Return true if the given context and surfaces can be made current.
267 _eglCheckMakeCurrent(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
)
269 _EGLThreadInfo
*t
= _eglGetCurrentThread();
272 if (_eglIsCurrentThreadDummy())
273 return _eglError(EGL_BAD_ALLOC
, "eglMakeCurrent");
278 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
282 /* ctx/draw/read must be all given */
283 if (draw
== NULL
|| read
== NULL
)
284 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
286 /* context stealing from another thread is not allowed */
287 if (ctx
->Binding
&& ctx
->Binding
!= t
)
288 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
293 * "If ctx is current to some other thread, or if either draw or read are
294 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
299 * "at most one context may be bound to a particular surface at a given
302 * The latter is more restrictive so we can check only the latter case.
304 if ((draw
->CurrentContext
&& draw
->CurrentContext
!= ctx
) ||
305 (read
->CurrentContext
&& read
->CurrentContext
!= ctx
))
306 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
308 /* simply require the configs to be equal */
309 if (draw
->Config
!= ctx
->Config
|| read
->Config
!= ctx
->Config
)
310 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
312 switch (ctx
->ClientAPI
) {
313 #ifdef EGL_VERSION_1_4
314 /* OpenGL and OpenGL ES are conflicting */
315 case EGL_OPENGL_ES_API
:
316 conflict_api
= EGL_OPENGL_API
;
319 conflict_api
= EGL_OPENGL_ES_API
;
327 if (conflict_api
>= 0 && _eglGetAPIContext(conflict_api
))
328 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
335 * Bind the context to the current thread and given surfaces. Return the
336 * previously bound context and the surfaces it bound to. Each argument is
337 * both input and output.
340 _eglBindContext(_EGLContext
**ctx
, _EGLSurface
**draw
, _EGLSurface
**read
)
342 _EGLThreadInfo
*t
= _eglGetCurrentThread();
343 _EGLContext
*newCtx
= *ctx
, *oldCtx
;
345 if (!_eglCheckMakeCurrent(newCtx
, *draw
, *read
))
348 /* bind the new context */
349 oldCtx
= _eglBindContextToThread(newCtx
, t
);
352 _eglBindContextToSurfaces(newCtx
, draw
, read
);
354 /* unbind the old context from its binding surfaces */
357 * If the new context replaces some old context, the new one should not
358 * be current before the replacement and it should not be bound to any
362 assert(!*draw
&& !*read
);
364 *draw
= oldCtx
->DrawSurface
;
365 *read
= oldCtx
->ReadSurface
;
366 assert(*draw
&& *read
);
368 _eglBindContextToSurfaces(NULL
, draw
, read
);
376 * Just a placeholder/demo function. Drivers should override this.
379 _eglMakeCurrent(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*draw
,
380 _EGLSurface
*read
, _EGLContext
*ctx
)
387 * This is defined by the EGL_MESA_copy_context extension.
390 _eglCopyContextMESA(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext source
,
391 EGLContext dest
, EGLint mask
)
393 /* This function will always have to be overridden/implemented in the
394 * device driver. If the driver is based on Mesa, use _mesa_copy_context().