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 thread and return the previous context.
210 * Note that the context may be NULL.
213 _eglBindContextToThread(_EGLContext
*ctx
, _EGLThreadInfo
*t
)
219 _eglConvertApiToIndex(ctx
->ClientAPI
) : t
->CurrentAPIIndex
;
221 oldCtx
= t
->CurrentContexts
[apiIndex
];
224 oldCtx
->Binding
= NULL
;
228 t
->CurrentContexts
[apiIndex
] = ctx
;
236 * Return true if the given context and surfaces can be made current.
239 _eglCheckMakeCurrent(_EGLContext
*ctx
, _EGLSurface
*draw
, _EGLSurface
*read
)
241 _EGLThreadInfo
*t
= _eglGetCurrentThread();
244 EGLBoolean surfaceless
;
246 if (_eglIsCurrentThreadDummy())
247 return _eglError(EGL_BAD_ALLOC
, "eglMakeCurrent");
252 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
256 dpy
= ctx
->Resource
.Display
;
257 switch (_eglGetContextAPIBit(ctx
)) {
258 case EGL_OPENGL_ES_BIT
:
259 surfaceless
= dpy
->Extensions
.KHR_surfaceless_gles1
;
261 case EGL_OPENGL_ES2_BIT
:
262 surfaceless
= dpy
->Extensions
.KHR_surfaceless_gles2
;
265 surfaceless
= dpy
->Extensions
.KHR_surfaceless_opengl
;
268 surfaceless
= EGL_FALSE
;
272 if (!surfaceless
&& (draw
== NULL
|| read
== NULL
))
273 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
275 /* context stealing from another thread is not allowed */
276 if (ctx
->Binding
&& ctx
->Binding
!= t
)
277 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
282 * "If ctx is current to some other thread, or if either draw or read are
283 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
288 * "at most one context may be bound to a particular surface at a given
291 * The latter is more restrictive so we can check only the latter case.
293 if ((draw
&& draw
->CurrentContext
&& draw
->CurrentContext
!= ctx
) ||
294 (read
&& read
->CurrentContext
&& read
->CurrentContext
!= ctx
))
295 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
297 /* simply require the configs to be equal */
298 if ((draw
&& draw
->Config
!= ctx
->Config
) ||
299 (read
&& read
->Config
!= ctx
->Config
))
300 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
302 switch (ctx
->ClientAPI
) {
303 #ifdef EGL_VERSION_1_4
304 /* OpenGL and OpenGL ES are conflicting */
305 case EGL_OPENGL_ES_API
:
306 conflict_api
= EGL_OPENGL_API
;
309 conflict_api
= EGL_OPENGL_ES_API
;
317 if (conflict_api
>= 0 && _eglGetAPIContext(conflict_api
))
318 return _eglError(EGL_BAD_ACCESS
, "eglMakeCurrent");
325 * Bind the context to the current thread and given surfaces. Return the
326 * "orphaned" context and surfaces. Each argument is both input and output.
329 _eglBindContext(_EGLContext
**ctx
, _EGLSurface
**draw
, _EGLSurface
**read
)
331 _EGLThreadInfo
*t
= _eglGetCurrentThread();
332 _EGLContext
*newCtx
= *ctx
, *oldCtx
;
333 _EGLSurface
*newDraw
= *draw
, *newRead
= *read
;
335 if (!_eglCheckMakeCurrent(newCtx
, newDraw
, newRead
))
338 /* bind the new context */
339 oldCtx
= _eglBindContextToThread(newCtx
, t
);
341 /* break old bindings */
344 *draw
= oldCtx
->DrawSurface
;
345 *read
= oldCtx
->ReadSurface
;
348 (*draw
)->CurrentContext
= NULL
;
350 (*read
)->CurrentContext
= NULL
;
352 oldCtx
->DrawSurface
= NULL
;
353 oldCtx
->ReadSurface
= NULL
;
356 /* establish new bindings */
359 newDraw
->CurrentContext
= newCtx
;
361 newRead
->CurrentContext
= newCtx
;
363 newCtx
->DrawSurface
= newDraw
;
364 newCtx
->ReadSurface
= newRead
;
367 /* an old context or surface is not orphaned if it is still bound */
370 if (*draw
== newDraw
|| *draw
== newRead
)
372 if (*read
== newDraw
|| *read
== newRead
)
380 * Just a placeholder/demo function. Drivers should override this.
383 _eglMakeCurrent(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*draw
,
384 _EGLSurface
*read
, _EGLContext
*ctx
)
391 * This is defined by the EGL_MESA_copy_context extension.
394 _eglCopyContextMESA(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext source
,
395 EGLContext dest
, EGLint mask
)
397 /* This function will always have to be overridden/implemented in the
398 * device driver. If the driver is based on Mesa, use _mesa_copy_context().