Skip to content

Commit

Permalink
Fix context creation of transferred offscreen canvas (emscripten-core…
Browse files Browse the repository at this point in the history
…#23518)

After a canvas has been transferred offscreen, it can't be used again
to create a webgl context because the result of findCanvasEventTarget
is a {canvas:...} map instead of an actual canvas object. This patch grabs
the underlying offscreencanvas if it exists.
  • Loading branch information
JoeOsborn authored Feb 18, 2025
1 parent 5858c64 commit cb88605
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/lib/libhtml5_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ var LibraryHtml5WebGL = {
#endif

var canvas = findCanvasEventTarget(target);

#if OFFSCREENCANVAS_SUPPORT
// If our canvas from findCanvasEventTarget is actually an offscreen canvas record, we should extract the inner canvas.
if (canvas?.canvas) { canvas = canvas.canvas; }
#endif
#if GL_DEBUG
var targetStr = UTF8ToString(target);
#endif
Expand Down
8 changes: 8 additions & 0 deletions test/browser/webgl_create_context_swapcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ int main() {
EMSCRIPTEN_RESULT res = emscripten_webgl_make_context_current(context);
assert(res == EMSCRIPTEN_RESULT_SUCCESS);
assert(emscripten_webgl_get_current_context() == context);
emscripten_webgl_destroy_context(context);
// Test that creating a context for a canvas that's already been used once succeeds
context = emscripten_webgl_create_context("#canvas", &attrs);
assert(context > 0); // Must have received a valid context.
res = emscripten_webgl_make_context_current(context);
assert(res == EMSCRIPTEN_RESULT_SUCCESS);
assert(emscripten_webgl_get_current_context() == context);
emscripten_webgl_destroy_context(context);
return 0;
}

0 comments on commit cb88605

Please sign in to comment.