5 #include "eglcontext.h"
6 #include "egldisplay.h"
8 #include "eglglobals.h"
9 #include "eglsurface.h"
13 * Initialize the given _EGLContext object to defaults and/or the values
17 _eglInitContext(_EGLDriver
*drv
, EGLDisplay dpy
, _EGLContext
*ctx
,
18 EGLConfig config
, const EGLint
*attrib_list
)
21 _EGLDisplay
*display
= _eglLookupDisplay(dpy
);
23 const EGLenum api
= eglQueryAPI();
25 if (api
== EGL_NONE
) {
26 _eglError(EGL_BAD_MATCH
, "eglCreateContext(no client API)");
30 conf
= _eglLookupConfig(drv
, dpy
, config
);
32 _eglError(EGL_BAD_CONFIG
, "_eglInitContext");
36 ctx
->ClientVersion
= 1; /* the default, per EGL spec */
38 for (i
= 0; attrib_list
&& attrib_list
[i
] != EGL_NONE
; i
++) {
39 switch (attrib_list
[i
]) {
40 case EGL_CONTEXT_CLIENT_VERSION
:
42 ctx
->ClientVersion
= attrib_list
[i
];
45 _eglError(EGL_BAD_ATTRIBUTE
, "_eglInitContext");
50 memset(ctx
, 0, sizeof(_EGLContext
));
51 ctx
->Display
= display
;
53 ctx
->DrawSurface
= EGL_NO_SURFACE
;
54 ctx
->ReadSurface
= EGL_NO_SURFACE
;
62 * Save a new _EGLContext into the hash table.
65 _eglSaveContext(_EGLContext
*ctx
)
68 * Public EGLContext handle and private _EGLContext are the same.
74 * Remove the given _EGLContext object from the hash table.
77 _eglRemoveContext(_EGLContext
*ctx
)
80 * Public EGLContext handle and private _EGLContext are the same.
86 * Return the public handle for the given private context ptr.
87 * This is the inverse of _eglLookupContext().
90 _eglGetContextHandle(_EGLContext
*ctx
)
93 return (EGLContext
) ctx
;
98 * Return the _EGLContext object that corresponds to the given
100 * This is the inverse of _eglGetContextHandle().
103 _eglLookupContext(EGLContext ctx
)
105 /* just a cast since EGLContext is just a void ptr */
106 return (_EGLContext
*) ctx
;
111 * Return the currently bound _EGLContext object, or NULL.
114 _eglGetCurrentContext(void)
116 _EGLThreadInfo
*t
= _eglGetCurrentThread();
117 return t
->CurrentContext
;
122 * Just a placeholder/demo function. Real driver will never use this!
125 _eglCreateContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
,
126 EGLContext share_list
, const EGLint
*attrib_list
)
128 #if 0 /* example code */
129 _EGLContext
*context
;
131 context
= (_EGLContext
*) calloc(1, sizeof(_EGLContext
));
133 return EGL_NO_CONTEXT
;
135 if (!_eglInitContext(drv
, dpy
, context
, config
, attrib_list
)) {
137 return EGL_NO_CONTEXT
;
140 _eglSaveContext(context
);
141 return (EGLContext
) context
;
143 return EGL_NO_CONTEXT
;
148 * Default fallback routine - drivers should usually override this.
151 _eglDestroyContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext ctx
)
153 _EGLContext
*context
= _eglLookupContext(ctx
);
155 if (context
->IsBound
) {
156 context
->DeletePending
= EGL_TRUE
;
164 _eglError(EGL_BAD_CONTEXT
, "eglDestroyContext");
171 _eglQueryContext(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext ctx
,
172 EGLint attribute
, EGLint
*value
)
174 _EGLContext
*c
= _eglLookupContext(ctx
);
180 _eglError(EGL_BAD_CONTEXT
, "eglQueryContext");
186 *value
= GET_CONFIG_ATTRIB(c
->Config
, EGL_CONFIG_ID
);
188 #ifdef EGL_VERSION_1_2
189 case EGL_CONTEXT_CLIENT_TYPE
:
190 *value
= c
->ClientAPI
;
192 #endif /* EGL_VERSION_1_2 */
194 _eglError(EGL_BAD_ATTRIBUTE
, "eglQueryContext");
201 * Drivers will typically call this to do the error checking and
202 * update the various IsBound and DeletePending flags.
203 * Then, the driver will do its device-dependent Make-Current stuff.
206 _eglMakeCurrent(_EGLDriver
*drv
, EGLDisplay dpy
, EGLSurface d
,
207 EGLSurface r
, EGLContext context
)
209 _EGLThreadInfo
*t
= _eglGetCurrentThread();
210 _EGLContext
*ctx
= _eglLookupContext(context
);
211 _EGLSurface
*draw
= _eglLookupSurface(d
);
212 _EGLSurface
*read
= _eglLookupSurface(r
);
214 _EGLContext
*oldContext
= _eglGetCurrentContext();
215 _EGLSurface
*oldDrawSurface
= _eglGetCurrentSurface(EGL_DRAW
);
216 _EGLSurface
*oldReadSurface
= _eglGetCurrentSurface(EGL_READ
);
220 if (draw
== NULL
|| read
== NULL
) {
221 _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
224 if (draw
->Config
!= ctx
->Config
) {
225 _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
228 if (read
->Config
!= ctx
->Config
) {
229 _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
235 * check if the old context or surfaces need to be deleted
237 if (oldDrawSurface
!= NULL
) {
238 oldDrawSurface
->IsBound
= EGL_FALSE
;
239 if (oldDrawSurface
->DeletePending
) {
240 /* make sure we don't try to rebind a deleted surface */
241 if (draw
== oldDrawSurface
|| draw
== oldReadSurface
) {
244 /* really delete surface now */
245 drv
->API
.DestroySurface(drv
, dpy
, oldDrawSurface
->Handle
);
248 if (oldReadSurface
!= NULL
&& oldReadSurface
!= oldDrawSurface
) {
249 oldReadSurface
->IsBound
= EGL_FALSE
;
250 if (oldReadSurface
->DeletePending
) {
251 /* make sure we don't try to rebind a deleted surface */
252 if (read
== oldDrawSurface
|| read
== oldReadSurface
) {
255 /* really delete surface now */
256 drv
->API
.DestroySurface(drv
, dpy
, oldReadSurface
->Handle
);
259 if (oldContext
!= NULL
) {
260 oldContext
->IsBound
= EGL_FALSE
;
261 if (oldContext
->DeletePending
) {
262 /* make sure we don't try to rebind a deleted context */
263 if (ctx
== oldContext
) {
266 /* really delete context now */
267 drv
->API
.DestroyContext(drv
, dpy
, _eglGetContextHandle(oldContext
));
272 /* check read/draw again, in case we deleted them above */
273 if (draw
== NULL
|| read
== NULL
) {
274 _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
277 ctx
->DrawSurface
= draw
;
278 ctx
->ReadSurface
= read
;
279 ctx
->IsBound
= EGL_TRUE
;
280 draw
->IsBound
= EGL_TRUE
;
281 read
->IsBound
= EGL_TRUE
;
284 t
->CurrentContext
= ctx
;
291 * This is defined by the EGL_MESA_copy_context extension.
294 _eglCopyContextMESA(_EGLDriver
*drv
, EGLDisplay dpy
, EGLContext source
,
295 EGLContext dest
, EGLint mask
)
297 /* This function will always have to be overridden/implemented in the
298 * device driver. If the driver is based on Mesa, use _mesa_copy_context().