Debian Codegraph @

src/egl/main/eglcontext.c

raw
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <[email protected]>
5 * Copyright 2010-2011 LunarG, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 #include "eglcontext.h"
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "util/macros.h"
35 #include "eglconfig.h"
36 #include "eglcurrent.h"
37 #include "egldisplay.h"
38 #include "egllog.h"
39 #include "eglsurface.h"
40
41 /**
42 * Return the API bit (one of EGL_xxx_BIT) of the context.
43 */
44 static EGLint
45 _eglGetContextAPIBit(_EGLContext *ctx)
46 {
47 EGLint bit = 0;
48
49 switch (ctx->ClientAPI) {
50 case EGL_OPENGL_ES_API:
51 switch (ctx->ClientMajorVersion) {
52 case 1:
53 bit = EGL_OPENGL_ES_BIT;
54 break;
55 case 2:
56 bit = EGL_OPENGL_ES2_BIT;
57 break;
58 case 3:
59 bit = EGL_OPENGL_ES3_BIT_KHR;
60 break;
61 default:
62 break;
63 }
64 break;
65 case EGL_OPENVG_API:
66 bit = EGL_OPENVG_BIT;
67 break;
68 case EGL_OPENGL_API:
69 bit = EGL_OPENGL_BIT;
70 break;
71 default:
72 break;
73 }
74
75 return bit;
76 }
77
78 /**
79 * Parse the list of context attributes and return the proper error code.
80 */
81 static EGLint
82 _eglParseContextAttribList(_EGLContext *ctx, _EGLDisplay *disp,
83 const EGLint *attrib_list)
84 {
85 EGLenum api = ctx->ClientAPI;
86 EGLint i, err = EGL_SUCCESS;
87
88 if (!attrib_list)
89 return EGL_SUCCESS;
90
91 if (api == EGL_OPENVG_API && attrib_list[0] != EGL_NONE) {
92 _eglLog(_EGL_DEBUG, "bad context attribute 0x%04x", attrib_list[0]);
93 return EGL_BAD_ATTRIBUTE;
94 }
95
96 for (i = 0; attrib_list[i] != EGL_NONE; i++) {
97 EGLint attr = attrib_list[i++];
98 EGLint val = attrib_list[i];
99
100 switch (attr) {
101 case EGL_CONTEXT_CLIENT_VERSION:
102 /* The EGL 1.4 spec says:
103 *
104 * "attribute EGL_CONTEXT_CLIENT_VERSION is only valid when the
105 * current rendering API is EGL_OPENGL_ES_API"
106 *
107 * The EGL_KHR_create_context spec says:
108 *
109 * "EGL_CONTEXT_MAJOR_VERSION_KHR 0x3098
110 * (this token is an alias for EGL_CONTEXT_CLIENT_VERSION)"
111 *
112 * "The values for attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
113 * EGL_CONTEXT_MINOR_VERSION_KHR specify the requested client API
114 * version. They are only meaningful for OpenGL and OpenGL ES
115 * contexts, and specifying them for other types of contexts will
116 * generate an error."
117 */
118 if ((api != EGL_OPENGL_ES_API &&
119 (!disp->Extensions.KHR_create_context ||
120 api != EGL_OPENGL_API))) {
121 err = EGL_BAD_ATTRIBUTE;
122 break;
123 }
124
125 ctx->ClientMajorVersion = val;
126 break;
127
128 case EGL_CONTEXT_MINOR_VERSION_KHR:
129 /* The EGL_KHR_create_context spec says:
130 *
131 * "The values for attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
132 * EGL_CONTEXT_MINOR_VERSION_KHR specify the requested client API
133 * version. They are only meaningful for OpenGL and OpenGL ES
134 * contexts, and specifying them for other types of contexts will
135 * generate an error."
136 */
137 if (!disp->Extensions.KHR_create_context ||
138 (api != EGL_OPENGL_ES_API && api != EGL_OPENGL_API)) {
139 err = EGL_BAD_ATTRIBUTE;
140 break;
141 }
142
143 ctx->ClientMinorVersion = val;
144 break;
145
146 case EGL_CONTEXT_FLAGS_KHR:
147 if (!disp->Extensions.KHR_create_context) {
148 err = EGL_BAD_ATTRIBUTE;
149 break;
150 }
151
152 /* The EGL_KHR_create_context spec says:
153 *
154 * "If the EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR flag bit is set in
155 * EGL_CONTEXT_FLAGS_KHR, then a <debug context> will be created.
156 * [...]
157 * In some cases a debug context may be identical to a non-debug
158 * context. This bit is supported for OpenGL and OpenGL ES
159 * contexts."
160 */
161 if ((val & EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR) &&
162 (api != EGL_OPENGL_API && api != EGL_OPENGL_ES_API)) {
163 err = EGL_BAD_ATTRIBUTE;
164 break;
165 }
166
167 /* The EGL_KHR_create_context spec says:
168 *
169 * "If the EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR flag bit
170 * is set in EGL_CONTEXT_FLAGS_KHR, then a <forward-compatible>
171 * context will be created. Forward-compatible contexts are
172 * defined only for OpenGL versions 3.0 and later. They must not
173 * support functionality marked as <deprecated> by that version of
174 * the API, while a non-forward-compatible context must support
175 * all functionality in that version, deprecated or not. This bit
176 * is supported for OpenGL contexts, and requesting a
177 * forward-compatible context for OpenGL versions less than 3.0
178 * will generate an error."
179 *
180 * Note: since the forward-compatible flag can be set more than one
181 * way, the OpenGL version check is performed once, below.
182 */
183 if ((val & EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR) &&
184 api != EGL_OPENGL_API) {
185 err = EGL_BAD_ATTRIBUTE;
186 break;
187 }
188
189 if ((val & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) &&
190 api != EGL_OPENGL_API) {
191 /* The EGL_KHR_create_context spec says:
192 *
193 * 10) Which error should be generated if robust buffer access
194 * or reset notifications are requested under OpenGL ES?
195 *
196 * As per Issue 6, this extension does not support creating
197 * robust contexts for OpenGL ES. This is only supported via
198 * the EGL_EXT_create_context_robustness extension.
199 *
200 * Attempting to use this extension to create robust OpenGL
201 * ES context will generate an EGL_BAD_ATTRIBUTE error. This
202 * specific error is generated because this extension does
203 * not define the EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
204 * and EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR
205 * bits for OpenGL ES contexts. Thus, use of these bits fall
206 * under condition described by: "If an attribute is
207 * specified that is not meaningful for the client API
208 * type.." in the above specification.
209 *
210 * The spec requires that we emit the error even if the display
211 * supports EGL_EXT_create_context_robustness. To create a robust
212 * GLES context, the *attribute*
213 * EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT must be used, not the
214 * *flag* EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR.
215 */
216 err = EGL_BAD_ATTRIBUTE;
217 break;
218 }
219
220 /* The EGL_KHR_create_context spec says:
221 * "If <config> does not support a client API context compatible
222 * with the requested API major and minor version, context flags,
223 * and context reset notification behavior (for client API types
224 * where these attributes are supported), then an EGL_BAD_MATCH
225 * error is generated."
226 */
227 if ((val & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) &&
228 !disp->RobustBufferAccess) {
229 err = EGL_BAD_MATCH;
230 break;
231 }
232
233 ctx->Flags |= val;
234 break;
235
236 case EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR:
237 if (!disp->Extensions.KHR_create_context) {
238 err = EGL_BAD_ATTRIBUTE;
239 break;
240 }
241
242 /* The EGL_KHR_create_context spec says:
243 *
244 * "[EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR] is only meaningful for
245 * OpenGL contexts, and specifying it for other types of
246 * contexts, including OpenGL ES contexts, will generate an
247 * error."
248 */
249 if (api != EGL_OPENGL_API) {
250 err = EGL_BAD_ATTRIBUTE;
251 break;
252 }
253
254 ctx->Profile = val;
255 break;
256
257 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR:
258 /* The EGL_KHR_create_context spec says:
259 *
260 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR] is only
261 * meaningful for OpenGL contexts, and specifying it for other
262 * types of contexts, including OpenGL ES contexts, will generate
263 * an error."
264 *
265 * EGL 1.5 defines EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY
266 * (without a suffix) which has the same value as the KHR token,
267 * and specifies that it now works with both GL and ES contexts:
268 *
269 * "This attribute is supported only for OpenGL and OpenGL ES
270 * contexts."
271 */
272 if (!(disp->Extensions.KHR_create_context && api == EGL_OPENGL_API) &&
273 disp->Version < 15) {
274 err = EGL_BAD_ATTRIBUTE;
275 break;
276 }
277
278 /* The EGL 1.5 spec says:
279 * "An EGL_BAD_MATCH error is generated if an OpenGL or OpenGL ES
280 * context is requested with robust buffer access and with a
281 * specified reset notification behavior, and the implementation
282 * does not support that behavior."
283 *
284 * and the EGL_KHR_create_context spec says:
285 * "If <config> does not support a client API context compatible
286 * with the requested API major and minor version, context flags,
287 * and context reset notification behavior (for client API types
288 * where these attributes are supported), then an EGL_BAD_MATCH
289 * error is generated."
290 */
291 if (val != EGL_NO_RESET_NOTIFICATION_KHR &&
292 !disp->Extensions.EXT_create_context_robustness) {
293 err = EGL_BAD_MATCH;
294 break;
295 }
296
297 ctx->ResetNotificationStrategy = val;
298 break;
299
300 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT:
301 /* The EGL_EXT_create_context_robustness spec says:
302 *
303 * "[EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT] is only
304 * meaningful for OpenGL ES contexts, and specifying it for other
305 * types of contexts will generate an EGL_BAD_ATTRIBUTE error."
306 */
307 if (!disp->Extensions.EXT_create_context_robustness ||
308 api != EGL_OPENGL_ES_API) {
309 err = EGL_BAD_ATTRIBUTE;
310 break;
311 }
312
313 ctx->ResetNotificationStrategy = val;
314 break;
315
316 case EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT:
317 if (!disp->Extensions.EXT_create_context_robustness) {
318 err = EGL_BAD_ATTRIBUTE;
319 break;
320 }
321
322 /* The EGL_EXT_create_context_robustness spec says:
323 *
324 * "EGL_BAD_CONFIG is generated if
325 * [EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT] is set to EGL_TRUE and
326 * no GL context supporting the GL_EXT_robustness extension and
327 * robust access as described therein can be created."
328 */
329 if (val == EGL_TRUE && !disp->RobustBufferAccess) {
330 err = EGL_BAD_CONFIG;
331 break;
332 }
333
334 if (val == EGL_TRUE)
335 ctx->Flags |= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR;
336 break;
337
338 case EGL_CONTEXT_OPENGL_ROBUST_ACCESS:
339 if (disp->Version < 15) {
340 err = EGL_BAD_ATTRIBUTE;
341 break;
342 }
343
344 /* The EGL 1.5 spec says:
345 * "An EGL_BAD_MATCH error is generated if an OpenGL or OpenGL ES
346 * context is requested with robust buffer access, and the
347 * implementation does not support the corresponding OpenGL or
348 * OpenGL ES extension".
349 */
350 if (val == EGL_TRUE && !disp->RobustBufferAccess) {
351 err = EGL_BAD_MATCH;
352 break;
353 }
354
355 if (val == EGL_TRUE)
356 ctx->Flags |= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR;
357 break;
358
359 case EGL_CONTEXT_OPENGL_DEBUG:
360 if (disp->Version < 15) {
361 err = EGL_BAD_ATTRIBUTE;
362 break;
363 }
364
365 if (val == EGL_TRUE)
366 ctx->Flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
367 break;
368
369 case EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE:
370 if (disp->Version < 15) {
371 err = EGL_BAD_ATTRIBUTE;
372 break;
373 }
374
375 if (val == EGL_TRUE)
376 ctx->Flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;
377 break;
378
379 case EGL_CONTEXT_OPENGL_NO_ERROR_KHR:
380 if (disp->Version < 14 ||
381 !disp->Extensions.KHR_create_context_no_error) {
382 err = EGL_BAD_ATTRIBUTE;
383 break;
384 }
385
386 /* The KHR_no_error spec only applies against OpenGL 2.0+ and
387 * OpenGL ES 2.0+
388 */
389 if (((api != EGL_OPENGL_API && api != EGL_OPENGL_ES_API) ||
390 ctx->ClientMajorVersion < 2) &&
391 val == EGL_TRUE) {
392 err = EGL_BAD_ATTRIBUTE;
393 break;
394 }
395
396 /* Canonicalize value to EGL_TRUE/EGL_FALSE definitions */
397 ctx->NoError = !!val;
398 break;
399
400 case EGL_CONTEXT_PRIORITY_LEVEL_IMG:
401 /* The EGL_IMG_context_priority spec says:
402 *
403 * "EGL_CONTEXT_PRIORITY_LEVEL_IMG determines the priority level of
404 * the context to be created. This attribute is a hint, as an
405 * implementation may not support multiple contexts at some
406 * priority levels and system policy may limit access to high
407 * priority contexts to appropriate system privilege level. The
408 * default value for EGL_CONTEXT_PRIORITY_LEVEL_IMG is
409 * EGL_CONTEXT_PRIORITY_MEDIUM_IMG."
410 */
411 {
412 int bit;
413
414 switch (val) {
415 case EGL_CONTEXT_PRIORITY_REALTIME_NV:
416 if (disp->Extensions.NV_context_priority_realtime)
417 bit = __EGL_CONTEXT_PRIORITY_REALTIME_BIT;
418 else
419 bit = -1;
420 break;
421 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
422 bit = __EGL_CONTEXT_PRIORITY_HIGH_BIT;
423 break;
424 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
425 bit = __EGL_CONTEXT_PRIORITY_MEDIUM_BIT;
426 break;
427 case EGL_CONTEXT_PRIORITY_LOW_IMG:
428 bit = __EGL_CONTEXT_PRIORITY_LOW_BIT;
429 break;
430 default:
431 bit = -1;
432 break;
433 }
434
435 if (bit < 0) {
436 err = EGL_BAD_ATTRIBUTE;
437 break;
438 }
439
440 /* "This extension allows an EGLContext to be created with a
441 * priority hint. It is possible that an implementation will not
442 * honour the hint, especially if there are constraints on the
443 * number of high priority contexts available in the system, or
444 * system policy limits access to high priority contexts to
445 * appropriate system privilege level. A query is provided to find
446 * the real priority level assigned to the context after creation."
447 *
448 * We currently assume that the driver applies the priority hint
449 * and filters out any it cannot handle during the screen setup,
450 * e.g. dri2_setup_screen(). As such we can mask any change that
451 * the driver would fail, and ctx->ContextPriority matches the
452 * hint applied to the driver/hardware backend.
453 */
454 if (disp->Extensions.IMG_context_priority & (1 << bit))
455 ctx->ContextPriority = val;
456
457 break;
458 }
459
460 case EGL_CONTEXT_RELEASE_BEHAVIOR_KHR:
461 if (val == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR ||
462 val == EGL_CONTEXT_RELEASE_BEHAVIOR_FLUSH_KHR) {
463 ctx->ReleaseBehavior = val;
464 } else {
465 err = EGL_BAD_ATTRIBUTE;
466 }
467 break;
468
469 case EGL_PROTECTED_CONTENT_EXT:
470 if (!disp->Extensions.EXT_protected_content) {
471 err = EGL_BAD_ATTRIBUTE;
472 break;
473 }
474 ctx->Protected = val == EGL_TRUE;
475 break;
476
477 default:
478 err = EGL_BAD_ATTRIBUTE;
479 break;
480 }
481
482 if (err != EGL_SUCCESS) {
483 _eglLog(_EGL_DEBUG, "bad context attribute 0x%04x", attr);
484 break;
485 }
486 }
487
488 if (api == EGL_OPENGL_API) {
489 /* The EGL_KHR_create_context spec says:
490 *
491 * "If the requested OpenGL version is less than 3.2,
492 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR is ignored and the
493 * functionality of the context is determined solely by the
494 * requested version."
495 *
496 * Since the value is ignored, only validate the setting if the version
497 * is >= 3.2.
498 */
499 if (ctx->ClientMajorVersion >= 4 ||
500 (ctx->ClientMajorVersion == 3 && ctx->ClientMinorVersion >= 2)) {
501 switch (ctx->Profile) {
502 case EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR:
503 case EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR:
504 break;
505
506 default:
507 /* The EGL_KHR_create_context spec says:
508 *
509 * "* If an OpenGL context is requested, the requested version
510 * is greater than 3.2, and the value for attribute
511 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has
512 * any bits set other than
513 * EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
514 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
515 * one of these bits set; or if the implementation does not support
516 * the requested profile, then an EGL_BAD_MATCH error is generated."
517 */
518 err = EGL_BAD_MATCH;
519 break;
520 }
521 }
522
523 /* The EGL_KHR_create_context spec says:
524 *
525 * "* If an OpenGL context is requested and the values for
526 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
527 * EGL_CONTEXT_MINOR_VERSION_KHR, when considered together with
528 * the value for attribute
529 * EGL_CONTEXT_FORWARD_COMPATIBLE_BIT_KHR, specify an OpenGL
530 * version and feature set that are not defined, than an
531 * EGL_BAD_MATCH error is generated.
532 *
533 * ... Thus, examples of invalid combinations of attributes
534 * include:
535 *
536 * - Major version < 1 or > 4
537 * - Major version == 1 and minor version < 0 or > 5
538 * - Major version == 2 and minor version < 0 or > 1
539 * - Major version == 3 and minor version < 0 or > 2
540 * - Major version == 4 and minor version < 0 or > 2
541 * - Forward-compatible flag set and major version < 3"
542 */
543 if (ctx->ClientMajorVersion < 1 || ctx->ClientMinorVersion < 0)
544 err = EGL_BAD_MATCH;
545
546 switch (ctx->ClientMajorVersion) {
547 case 1:
548 if (ctx->ClientMinorVersion > 5 ||
549 (ctx->Flags & EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR) != 0)
550 err = EGL_BAD_MATCH;
551 break;
552
553 case 2:
554 if (ctx->ClientMinorVersion > 1 ||
555 (ctx->Flags & EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR) != 0)
556 err = EGL_BAD_MATCH;
557 break;
558
559 case 3:
560 /* Note: The text above is incorrect. There *is* an OpenGL 3.3!
561 */
562 if (ctx->ClientMinorVersion > 3)
563 err = EGL_BAD_MATCH;
564 break;
565
566 case 4:
567 default:
568 /* Don't put additional version checks here. We don't know that
569 * there won't be versions > 4.2.
570 */
571 break;
572 }
573 } else if (api == EGL_OPENGL_ES_API) {
574 /* The EGL_KHR_create_context spec says:
575 *
576 * "* If an OpenGL ES context is requested and the values for
577 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
578 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
579 * is not defined, than an EGL_BAD_MATCH error is generated.
580 *
581 * ... Examples of invalid combinations of attributes include:
582 *
583 * - Major version < 1 or > 2
584 * - Major version == 1 and minor version < 0 or > 1
585 * - Major version == 2 and minor version != 0
586 */
587 if (ctx->ClientMajorVersion < 1 || ctx->ClientMinorVersion < 0)
588 err = EGL_BAD_MATCH;
589
590 switch (ctx->ClientMajorVersion) {
591 case 1:
592 if (ctx->ClientMinorVersion > 1)
593 err = EGL_BAD_MATCH;
594 break;
595
596 case 2:
597 if (ctx->ClientMinorVersion > 0)
598 err = EGL_BAD_MATCH;
599 break;
600
601 case 3:
602 /* Don't put additional version checks here. We don't know that
603 * there won't be versions > 3.0.
604 */
605 break;
606
607 default:
608 err = EGL_BAD_MATCH;
609 break;
610 }
611 }
612
613 switch (ctx->ResetNotificationStrategy) {
614 case EGL_NO_RESET_NOTIFICATION_KHR:
615 case EGL_LOSE_CONTEXT_ON_RESET_KHR:
616 break;
617
618 default:
619 err = EGL_BAD_ATTRIBUTE;
620 break;
621 }
622
623 /* The EGL_KHR_create_context_no_error spec says:
624 *
625 * "BAD_MATCH is generated if the EGL_CONTEXT_OPENGL_NO_ERROR_KHR is TRUE
626 * at the same time as a debug or robustness context is specified."
627 */
628 if (ctx->NoError &&
629 (ctx->Flags & EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR ||
630 ctx->Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR)) {
631 err = EGL_BAD_MATCH;
632 }
633
634 if ((ctx->Flags & ~(EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR |
635 EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR |
636 EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR)) != 0) {
637 err = EGL_BAD_ATTRIBUTE;
638 }
639
640 return err;
641 }
642
643 /**
644 * Initialize the given _EGLContext object to defaults and/or the values
645 * in the attrib_list.
646 *
647 * According to EGL 1.5 Section 3.7:
648 *
649 * "EGL_OPENGL_API and EGL_OPENGL_ES_API are interchangeable for all
650 * purposes except eglCreateContext."
651 *
652 * And since we only support GL and GLES, this is the only place where the
653 * bound API matters at all. We look up the current API from the current
654 * thread, and stash that in the context we're initializing. Our caller is
655 * responsible for determining whether that's an API it supports.
656 */
657 EGLBoolean
658 _eglInitContext(_EGLContext *ctx, _EGLDisplay *disp, _EGLConfig *conf,
659 _EGLContext *share_list, const EGLint *attrib_list)
660 {
661 const EGLenum api = eglQueryAPI();
662 EGLint err;
663
664 if (api == EGL_NONE)
665 return _eglError(EGL_BAD_MATCH, "eglCreateContext(no client API)");
666
667 _eglInitResource(&ctx->Resource, sizeof(*ctx), disp);
668 ctx->ClientAPI = api;
669 ctx->Config = conf;
670 ctx->Profile = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
671
672 ctx->ClientMajorVersion = 1; /* the default, per EGL spec */
673 ctx->ClientMinorVersion = 0;
674 ctx->Flags = 0;
675 ctx->ResetNotificationStrategy = EGL_NO_RESET_NOTIFICATION_KHR;
676 ctx->ContextPriority = EGL_CONTEXT_PRIORITY_MEDIUM_IMG;
677 ctx->ReleaseBehavior = EGL_CONTEXT_RELEASE_BEHAVIOR_FLUSH_KHR;
678
679 err = _eglParseContextAttribList(ctx, disp, attrib_list);
680 if (err == EGL_SUCCESS && ctx->Config) {
681 EGLint api_bit;
682
683 api_bit = _eglGetContextAPIBit(ctx);
684 if (!(ctx->Config->RenderableType & api_bit)) {
685 _eglLog(_EGL_DEBUG, "context api is 0x%x while config supports 0x%x",
686 api_bit, ctx->Config->RenderableType);
687 err = EGL_BAD_CONFIG;
688 }
689 }
690 if (err != EGL_SUCCESS)
691 return _eglError(err, "eglCreateContext");
692
693 /* The EGL_EXT_create_context_robustness spec says:
694 *
695 * "Add to the eglCreateContext context creation errors: [...]
696 *
697 * * If the reset notification behavior of <share_context> and the
698 * newly created context are different then an EGL_BAD_MATCH error is
699 * generated."
700 */
701 if (share_list && share_list->ResetNotificationStrategy !=
702 ctx->ResetNotificationStrategy) {
703 return _eglError(
704 EGL_BAD_MATCH,
705 "eglCreateContext() share list notification strategy mismatch");
706 }
707
708 /* The EGL_KHR_create_context_no_error spec says:
709 *
710 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
711 * used to create <share_context> does not match the value of
712 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
713 */
714 if (share_list && share_list->NoError != ctx->NoError) {
715 return _eglError(EGL_BAD_MATCH,
716 "eglCreateContext() share list no-error mismatch");
717 }
718
719 return EGL_TRUE;
720 }
721
722 static EGLint
723 _eglQueryContextRenderBuffer(_EGLContext *ctx)
724 {
725 _EGLSurface *surf = ctx->DrawSurface;
726
727 /* From the EGL 1.5 spec:
728 *
729 * - If the context is not bound to a surface, then EGL_NONE will be
730 * returned.
731 */
732 if (!surf)
733 return EGL_NONE;
734
735 switch (surf->Type) {
736 default:
737 UNREACHABLE("bad EGLSurface type");
738 case EGL_PIXMAP_BIT:
739 /* - If the context is bound to a pixmap surface, then EGL_SINGLE_BUFFER
740 * will be returned.
741 */
742 return EGL_SINGLE_BUFFER;
743 case EGL_PBUFFER_BIT:
744 /* - If the context is bound to a pbuffer surface, then EGL_BACK_BUFFER
745 * will be returned.
746 */
747 return EGL_BACK_BUFFER;
748 case EGL_WINDOW_BIT:
749 /* - If the context is bound to a window surface, then either
750 * EGL_BACK_BUFFER or EGL_SINGLE_BUFFER may be returned. The value
751 * returned depends on both the buffer requested by the setting of the
752 * EGL_RENDER_BUFFER property of the surface [...], and on the client
753 * API (not all client APIs support single-buffer Rendering to window
754 * surfaces). Some client APIs allow control of whether rendering goes
755 * to the front or back buffer. This client API-specific choice is not
756 * reflected in the returned value, which only describes the buffer
757 * that will be rendered to by default if not overridden by the client
758 * API.
759 */
760 return surf->ActiveRenderBuffer;
761 }
762 }
763
764 EGLBoolean
765 _eglQueryContext(_EGLContext *c, EGLint attribute, EGLint *value)
766 {
767 _EGLDisplay *disp = c->Resource.Display;
768 EGLenum api = c->ClientAPI;
769
770 if (!value)
771 return _eglError(EGL_BAD_PARAMETER, "eglQueryContext");
772
773 switch (attribute) {
774 case EGL_CONFIG_ID:
775 /*
776 * From EGL_KHR_no_config_context:
777 *
778 * "Querying EGL_CONFIG_ID returns the ID of the EGLConfig with
779 * respect to which the context was created, or zero if created
780 * without respect to an EGLConfig."
781 */
782 *value = c->Config ? c->Config->ConfigID : 0;
783 break;
784 case EGL_CONTEXT_CLIENT_VERSION:
785 *value = c->ClientMajorVersion;
786 break;
787 case EGL_CONTEXT_CLIENT_TYPE:
788 *value = c->ClientAPI;
789 break;
790 case EGL_RENDER_BUFFER:
791 *value = _eglQueryContextRenderBuffer(c);
792 break;
793 case EGL_CONTEXT_PRIORITY_LEVEL_IMG:
794 *value = c->ContextPriority;
795 break;
796 case EGL_PROTECTED_CONTENT_EXT:
797 if (!disp->Extensions.EXT_protected_content)
798 return _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
799 *value = c->Protected;
800 break;
801 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT:
802 if (!disp->Extensions.EXT_create_context_robustness ||
803 api != EGL_OPENGL_ES_API)
804 return _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
805 *value = c->ResetNotificationStrategy;
806 break;
807 case EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR:
808 if (!(disp->Extensions.KHR_create_context && api == EGL_OPENGL_API) &&
809 disp->Version < 15)
810 return _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
811 *value = c->ResetNotificationStrategy;
812 break;
813 default:
814 return _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
815 }
816
817 return EGL_TRUE;
818 }
819
820 /**
821 * Bind the context to the thread and return the previous context.
822 *
823 * Note that the context may be NULL.
824 */
825 _EGLContext *
826 _eglBindContextToThread(_EGLContext *ctx, _EGLThreadInfo *t)
827 {
828 _EGLContext *oldCtx;
829
830 oldCtx = t->CurrentContext;
831 if (ctx != oldCtx) {
832 if (oldCtx)
833 oldCtx->Binding = NULL;
834 if (ctx)
835 ctx->Binding = t;
836
837 t->CurrentContext = ctx;
838 }
839
840 return oldCtx;
841 }
842
843 /**
844 * Return true if the given context and surfaces can be made current.
845 */
846 static EGLBoolean
847 _eglCheckMakeCurrent(_EGLContext *ctx, _EGLSurface *draw, _EGLSurface *read)
848 {
849 _EGLThreadInfo *t = _eglGetCurrentThread();
850 _EGLDisplay *disp;
851
852 /* this is easy */
853 if (!ctx) {
854 if (draw || read)
855 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
856 return EGL_TRUE;
857 }
858
859 disp = ctx->Resource.Display;
860 if (!disp->Extensions.KHR_surfaceless_context &&
861 (draw == NULL || read == NULL))
862 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
863
864 /*
865 * The spec says
866 *
867 * "If ctx is current to some other thread, or if either draw or read are
868 * bound to contexts in another thread, an EGL_BAD_ACCESS error is
869 * generated."
870 *
871 * and
872 *
873 * "at most one context may be bound to a particular surface at a given
874 * time"
875 */
876 if (ctx->Binding && ctx->Binding != t)
877 return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
878 if (draw && draw->CurrentContext && draw->CurrentContext != ctx) {
879 if (draw->CurrentContext->Binding != t)
880 return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
881 }
882 if (read && read->CurrentContext && read->CurrentContext != ctx) {
883 if (read->CurrentContext->Binding != t)
884 return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
885 }
886
887 /* If the context has a config then it must match that of the two
888 * surfaces */
889 if (ctx->Config) {
890 if ((draw && draw->Config != ctx->Config) ||
891 (read && read->Config != ctx->Config))
892 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
893 } else {
894 /* Otherwise we must be using the EGL_KHR_no_config_context
895 * extension */
896 assert(disp->Extensions.KHR_no_config_context);
897 }
898
899 return EGL_TRUE;
900 }
901
902 /**
903 * Bind the context to the current thread and given surfaces. Return the
904 * previous bound context and surfaces. The caller should unreference the
905 * returned context and surfaces.
906 *
907 * Making a second call with the resources returned by the first call
908 * unsurprisingly undoes the first call, except for the resource reference
909 * counts.
910 */
911 EGLBoolean
912 _eglBindContext(_EGLContext *ctx, _EGLSurface *draw, _EGLSurface *read,
913 _EGLContext **old_ctx, _EGLSurface **old_draw,
914 _EGLSurface **old_read)
915 {
916 _EGLThreadInfo *t = _eglGetCurrentThread();
917 _EGLContext *prev_ctx;
918 _EGLSurface *prev_draw, *prev_read;
919
920 if (!_eglCheckMakeCurrent(ctx, draw, read))
921 return EGL_FALSE;
922
923 /* increment refcounts before binding */
924 _eglGetContext(ctx);
925 _eglGetSurface(draw);
926 _eglGetSurface(read);
927
928 /* bind the new context */
929 prev_ctx = _eglBindContextToThread(ctx, t);
930
931 /* break previous bindings */
932 if (prev_ctx) {
933 prev_draw = prev_ctx->DrawSurface;
934 prev_read = prev_ctx->ReadSurface;
935
936 if (prev_draw)
937 prev_draw->CurrentContext = NULL;
938 if (prev_read)
939 prev_read->CurrentContext = NULL;
940
941 prev_ctx->DrawSurface = NULL;
942 prev_ctx->ReadSurface = NULL;
943 } else {
944 prev_draw = prev_read = NULL;
945 }
946
947 /* establish new bindings */
948 if (ctx) {
949 if (draw)
950 draw->CurrentContext = ctx;
951 if (read)
952 read->CurrentContext = ctx;
953
954 ctx->DrawSurface = draw;
955 ctx->ReadSurface = read;
956 }
957
958 assert(old_ctx && old_draw && old_read);
959 *old_ctx = prev_ctx;
960 *old_draw = prev_draw;
961 *old_read = prev_read;
962
963 return EGL_TRUE;
964 }