Skip to content

Commit 937533b

Browse files
authored
Workaround Safari WebGL issue: After successfully acquiring WebGL con… (#13659)
* Workaround Safari WebGL issue: After successfully acquiring WebGL context on a canvas, calling .getContext() will always return that context independent of which 'webgl' or 'webgl2' context version was passed. See https://bugs.webkit.org/show_bug.cgi?id=222758. Fixes #13295 * return null
1 parent 5aabf10 commit 937533b

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/library_webgl.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,21 @@ var LibraryGL = {
592592
webGLContextAttributes['preserveDrawingBuffer'] = true;
593593
#endif
594594

595+
#if MIN_SAFARI_VERSION != TARGET_NOT_SUPPORTED && GL_WORKAROUND_SAFARI_GETCONTEXT_BUG
596+
// BUG: Workaround Safari WebGL issue: After successfully acquiring WebGL context on a canvas,
597+
// calling .getContext() will always return that context independent of which 'webgl' or 'webgl2'
598+
// context version was passed. See https://bugs.webkit.org/show_bug.cgi?id=222758 and
599+
// https://github.com/emscripten-core/emscripten/issues/13295.
600+
// TODO: Once the bug is fixed and shipped in Safari, adjust the Safari version field in above check.
601+
if (!canvas.getContextSafariWebGL2Fixed) {
602+
canvas.getContextSafariWebGL2Fixed = canvas.getContext;
603+
canvas.getContext = function(ver, attrs) {
604+
var gl = canvas.getContextSafariWebGL2Fixed(ver, attrs);
605+
return ((ver == 'webgl') == (gl instanceof WebGLRenderingContext)) ? gl : null;
606+
}
607+
}
608+
#endif
609+
595610
#if MAX_WEBGL_VERSION >= 2 && MIN_CHROME_VERSION <= 57
596611
// BUG: Workaround Chrome WebGL 2 issue: the first shipped versions of WebGL 2 in Chrome 57 did not actually implement
597612
// the new garbage free WebGL 2 entry points that take an offset and a length to an existing heap (instead of having to

src/settings.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,13 @@ var STB_IMAGE = 0;
568568
// [link]
569569
var GL_DISABLE_HALF_FLOAT_EXTENSION_IF_BROKEN = 0;
570570

571+
// Workaround Safari WebGL issue: After successfully acquiring WebGL context on a canvas,
572+
// calling .getContext() will always return that context independent of which 'webgl' or 'webgl2'
573+
// context version was passed. See https://bugs.webkit.org/show_bug.cgi?id=222758 and
574+
// https://github.com/emscripten-core/emscripten/issues/13295.
575+
// Set this to 0 to force-disable the workaround if you know the issue will not affect you.
576+
var GL_WORKAROUND_SAFARI_GETCONTEXT_BUG = 1;
577+
571578
// Use JavaScript math functions like Math.tan. This saves code size as we can avoid shipping
572579
// compiled musl code. However, it can be significantly slower as it calls out to JS. It
573580
// also may give different results as JS math is specced somewhat differently than libc, and

tests/test_other.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8447,6 +8447,7 @@ def test_minimal_runtime_code_size(self, test_name, js, compare_js_output=False)
84478447
'-s', 'GL_SUPPORT_EXPLICIT_SWAP_CONTROL=0',
84488448
'-s', 'GL_POOL_TEMP_BUFFERS=0',
84498449
'-s', 'MIN_CHROME_VERSION=58',
8450+
'-s', 'GL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0',
84508451
'-s', 'NO_FILESYSTEM',
84518452
'--output_eol', 'linux',
84528453
'-Oz',

0 commit comments

Comments
 (0)