diff --git a/.github/workflows/deploy-github-page.yml b/.github/workflows/deploy-github-page.yml index 5b80622e..77721bfe 100644 --- a/.github/workflows/deploy-github-page.yml +++ b/.github/workflows/deploy-github-page.yml @@ -69,6 +69,10 @@ jobs: emmake make check-xeus-cpp -j ${{ env.ncpus }} emmake make -j ${{ env.ncpus }} install + - name: Setup headless display + if: runner.os == 'Linux' + uses: pyvista/setup-headless-display-action@v5 + - name: Test Emscripten xeus-cpp in browser shell: bash -l {0} run: | @@ -111,9 +115,9 @@ jobs: # Run tests in browsers echo "Running test_xeus_cpp in Firefox" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --browser-args="--headless" test_xeus_cpp.html + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="Google Chrome" --kill_exit --browser-args="--headless" test_xeus_cpp.html + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="Google Chrome" --kill_exit test_xeus_cpp.html else # Install Google Chrome wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb @@ -131,9 +135,13 @@ jobs: # Run tests in browsers echo "Running test_xeus_cpp in Firefox" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html + # Linux CI uses Mesa llvmpipe for software rendering. Chrome + # blocklists that driver and its Vulkan/compositing path fails on + # the virtual display, so allow the blocklisted driver and disable + # Vulkan and GPU compositing. macOS uses the native GPU instead. + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox --ignore-gpu-blocklist --disable-gpu-compositing --disable-features=Vulkan" test_xeus_cpp.html fi timeout-minutes: 4 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9398623b..4f717b16 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -267,6 +267,10 @@ jobs: emmake make check-xeus-cpp -j ${{ env.ncpus }} emmake make -j ${{ env.ncpus }} install + - name: Setup headless display + if: runner.os == 'Linux' + uses: pyvista/setup-headless-display-action@v5 + - name: Test Emscripten xeus-cpp in browser shell: bash -l {0} run: | @@ -309,14 +313,14 @@ jobs: # Run tests in browsers echo "Running test_xeus_cpp in Firefox" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox" test_xeus_cpp.html sudo safaridriver --enable python -m ensurepip python -m pip install selenium echo "Running test_xeus_cpp in Safari" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --no_browser --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html & + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --no_browser --kill_exit --timeout 60 test_xeus_cpp.html & python ../../scripts/browser_tests_safari.py test_xeus_cpp.html else # Install Google Chrome @@ -335,9 +339,14 @@ jobs: # Run tests in browsers echo "Running test_xeus_cpp in Firefox" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" - python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html + # Linux CI uses Mesa llvmpipe for software rendering: + # - --ignore-gpu-blocklist: Chrome otherwise rejects the software driver. + # - --disable-gpu-compositing: the llvmpipe compositing path fails. + # - --disable-features=Vulkan: Chrome's Vulkan path fails on this display. + # macOS uses its native GPU and needs none of these flags. + python ${{ env.BUILD_PREFIX }}/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox --ignore-gpu-blocklist --disable-gpu-compositing --disable-features=Vulkan" test_xeus_cpp.html fi timeout-minutes: 4 diff --git a/CMakeLists.txt b/CMakeLists.txt index aa6a6a64..57cad98f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -433,12 +433,24 @@ endif() if(EMSCRIPTEN) include(WasmBuildOptions) + include(XeusCppWasm) find_package(xeus-lite ${xeus_lite_REQUIRED_VERSION} REQUIRED) add_executable(xcpp src/main_emscripten_kernel.cpp ) target_link_libraries(xcpp PRIVATE xeus-lite) xeus_cpp_set_kernel_options(xcpp) xeus_wasm_compile_options(xcpp) xeus_wasm_link_options(xcpp "web,worker") + # Emscripten-only WebGL flags. Native builds must not receive these. + message(STATUS "Enabling WebGL2/OffscreenCanvas support for xcpp") + set(XEUS_CPP_WASM_WEBGL_LINK_OPTIONS + "SHELL: -s GL_ENABLE_GET_PROC_ADDRESS=1" + "SHELL: -s OFFSCREENCANVAS_SUPPORT=1" + "SHELL: -s MAX_WEBGL_VERSION=2" + "SHELL: -s FULL_ES3=1" + "SHELL: -Wl,--undefined=emscripten_webgl2_get_proc_address" + ) + target_link_options(xcpp PUBLIC ${XEUS_CPP_WASM_WEBGL_LINK_OPTIONS}) + xeus_cpp_wasm_append_runtime_method(xcpp GL) string(REPLACE "@" "@@" ESCAPED_SYSROOT_PATH "${SYSROOT_PATH}") string(REPLACE "@" "@@" ESCAPED_XEUS_CPP_RESOURCE_DIR "${XEUS_CPP_RESOURCE_DIR}") string(REPLACE "\\" "/" ESCAPED_XEUS_CPP_RESOURCE_DIR "${ESCAPED_XEUS_CPP_RESOURCE_DIR}") diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c64448f6..4f6a9ad5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -96,7 +96,7 @@ emmake make check-xeus-cpp emmake make install ``` -It is possible to run the Emscripten tests in a headless browser. We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this for Chrome and Firefox on MacOS execute the following from the build folder +The Emscripten tests can be run in a browser. We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this for Chrome and Firefox on MacOS execute the following from the build folder ```bash cd test @@ -115,9 +115,9 @@ export PATH="$PWD:$PATH" cd – echo "Running test_xeus_cpp in Firefox" -python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --browser-args="--headless" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" -python $BUILD_PREFIX/bin/emrun.py --browser="Google Chrome" --kill_exit --browser-args="--headless --no-sandbox" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox" test_xeus_cpp.html ``` To run tests in Safari you can make use of safaridriver. How to enable this will depend on @@ -126,7 +126,7 @@ python package. This only needs to be enable once, and then you can execute the ```bash echo "Running test_xeus_cpp in Safari" -python $BUILD_PREFIX/bin/emrun.py --no_browser --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html & +python $BUILD_PREFIX/bin/emrun.py --no_browser --kill_exit --timeout 60 test_xeus_cpp.html & python ../../scripts/browser_tests_safari.py test_xeus_cpp.html ``` @@ -147,11 +147,19 @@ export PATH="$PWD:$PATH" cd - echo "Running test_xeus_cpp in Firefox" -python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" -python $BUILD_PREFIX/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox --ignore-gpu-blocklist --disable-gpu-compositing --disable-features=Vulkan" test_xeus_cpp.html ``` +When Chrome runs on a Linux CI display backed by Mesa llvmpipe, these flags are required: + +- `--ignore-gpu-blocklist`: Chrome blocks llvmpipe as a software renderer. Without this flag, WebGL2 context creation fails. +- `--disable-gpu-compositing`: Chrome's llvmpipe compositing path fails. Disabling it keeps rendering on the working GL path. +- `--disable-features=Vulkan`: Chrome's Vulkan path fails on this display. Disabling it forces the GL path. + +macOS uses the native GPU and does not need these flags. + To build and test Jupyter Lite with this kernel locally you can execute the following ```bash micromamba create -n xeus-lite-host jupyter_server jupyterlite-xeus -c conda-forge diff --git a/README.md b/README.md index 6951df5e..9c08339a 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ emmake make check-xeus-cpp emmake make install ``` -It is possible to run the Emscripten tests in a headless browser. We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this for Chrome and Firefox on MacOS execute the following from the build folder +The Emscripten tests can be run in a browser. We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this for Chrome and Firefox on MacOS execute the following from the build folder ```bash cd test @@ -130,9 +130,9 @@ export PATH="$PWD:$PATH" cd - echo "Running test_xeus_cpp in Firefox" -python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" -python python $BUILD_PREFIX/bin/emrun.py --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox" test_xeus_cpp.html ``` To run tests in Safari you can make use of safaridriver. How to enable this will depend on @@ -141,7 +141,7 @@ python package. This only needs to be enable once, and then you can execute the ```bash echo "Running test_xeus_cpp in Safari" -python $BUILD_PREFIX/bin/emrun.py --no_browser --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html & +python $BUILD_PREFIX/bin/emrun.py --no_browser --kill_exit --timeout 60 test_xeus_cpp.html & python ../../scripts/browser_tests_safari.py test_xeus_cpp.html ``` @@ -162,11 +162,19 @@ export PATH="$PWD:$PATH" cd - echo "Running test_xeus_cpp in Firefox" -python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" -python $BUILD_PREFIX/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html +python $BUILD_PREFIX/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox --ignore-gpu-blocklist --disable-gpu-compositing --disable-features=Vulkan" test_xeus_cpp.html ``` +When Chrome runs on a Linux CI display backed by Mesa llvmpipe, these flags are required: + +- `--ignore-gpu-blocklist`: Chrome blocks llvmpipe as a software renderer. Without this flag, WebGL2 context creation fails. +- `--disable-gpu-compositing`: Chrome's llvmpipe compositing path fails. Disabling it keeps rendering on the working GL path. +- `--disable-features=Vulkan`: Chrome's Vulkan path fails on this display. Disabling it forces the GL path. + +macOS uses the native GPU and does not need these flags. + To build and test Jupyter Lite with this kernel locally you can execute the following ```bash micromamba create -n xeus-lite-host jupyter_server jupyterlite-xeus -c conda-forge diff --git a/cmake/XeusCppWasm.cmake b/cmake/XeusCppWasm.cmake new file mode 100644 index 00000000..13e45b1a --- /dev/null +++ b/cmake/XeusCppWasm.cmake @@ -0,0 +1,53 @@ +# Append one method to the EXPORTED_RUNTIME_METHODS link option already present +# on a target. This preserves methods exported by xeus_wasm_link_options and +# avoids maintaining a duplicate copy of the baseline list. +# +# Sets XEUS_CPP_WASM_EXPORTED_RUNTIME_METHODS in the caller scope so other +# targets (for example tests) can reuse the rewritten option. +function(xeus_cpp_wasm_append_runtime_method target method) + get_target_property(link_options ${target} LINK_OPTIONS) + if(NOT link_options) + message(FATAL_ERROR + "xeus-cpp: ${target} has no LINK_OPTIONS; " + "xeus_wasm_link_options() must run first" + ) + endif() + + set(updated_options) + set(runtime_methods_option "") + foreach(option IN LISTS link_options) + if(option MATCHES "EXPORTED_RUNTIME_METHODS='\\[(.*)\\]'") + if(runtime_methods_option) + message(FATAL_ERROR + "xeus-cpp: ${target} has multiple EXPORTED_RUNTIME_METHODS options" + ) + endif() + + # CMAKE_MATCH_1 is the JSON array contents without the brackets. + string(REPLACE "\"" "" methods "${CMAKE_MATCH_1}") + string(REPLACE "," ";" methods "${methods}") + list(TRANSFORM methods STRIP) + list(APPEND methods "${method}") + list(REMOVE_DUPLICATES methods) + string(REPLACE ";" "\",\"" methods "${methods}") + set(runtime_methods_option + "SHELL: -s EXPORTED_RUNTIME_METHODS='[\"${methods}\"]'" + ) + list(APPEND updated_options "${runtime_methods_option}") + else() + list(APPEND updated_options "${option}") + endif() + endforeach() + + if(NOT runtime_methods_option) + message(FATAL_ERROR + "xeus-cpp: ${target} does not export EXPORTED_RUNTIME_METHODS, the xeus wasm link options changed" + ) + endif() + + set_target_properties(${target} PROPERTIES LINK_OPTIONS "${updated_options}") + set(XEUS_CPP_WASM_EXPORTED_RUNTIME_METHODS + "${runtime_methods_option}" + PARENT_SCOPE + ) +endfunction() diff --git a/docs/source/InstallationAndUsage.rst b/docs/source/InstallationAndUsage.rst index 5fc1fda3..cd677bfb 100644 --- a/docs/source/InstallationAndUsage.rst +++ b/docs/source/InstallationAndUsage.rst @@ -91,7 +91,7 @@ You are now in a position to build the xeus-cpp kernel. You build and test it in emmake make check-xeus-cpp emmake make install -It is possible to run the Emscripten tests in a headless browser. We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this for Chrome and Firefox on MacOS execute the following from the build folder +The Emscripten tests can be run in a browser. We will run our tests in a fresh installed browser. Installing the browsers, and running the tests within the installed browsers will be platform dependent. To do this for Chrome and Firefox on MacOS execute the following from the build folder .. code-block:: bash @@ -111,9 +111,9 @@ It is possible to run the Emscripten tests in a headless browser. We will run ou cd - echo "Running test_xeus_cpp in Firefox" - python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html + python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" - python $BUILD_PREFIX/bin/emrun.py --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html + python $BUILD_PREFIX/bin/emrun.py --browser="Google Chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox" test_xeus_cpp.html To run tests in Safari you can make use of safaridriver. How to enable this will depend on your MacOS operating system, and is best to consult `safaridriver `_. You will also need to install the Selenium @@ -122,7 +122,7 @@ python package. This only needs to be enable once, and then you can execute the .. code-block:: bash echo "Running test_xeus_cpp in Safari" - python $BUILD_PREFIX/bin/emrun.py --no_browser --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html & + python $BUILD_PREFIX/bin/emrun.py --no_browser --kill_exit --timeout 60 test_xeus_cpp.html & python ../../scripts/browser_tests_safari.py test_xeus_cpp.html To run the browser tests on Ubuntu x86 execute the following from the build folder @@ -143,9 +143,17 @@ To run the browser tests on Ubuntu x86 execute the following from the build fold cd - echo "Running test_xeus_cpp in Firefox" - python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 --browser-args="--headless" test_xeus_cpp.html + python $BUILD_PREFIX/bin/emrun.py --browser="firefox" --kill_exit --timeout 60 test_xeus_cpp.html echo "Running test_xeus_cpp in Google Chrome" - python $BUILD_PREFIX/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--headless --no-sandbox" test_xeus_cpp.html + python $BUILD_PREFIX/bin/emrun.py --browser="google-chrome" --kill_exit --timeout 60 --browser-args="--no-sandbox --ignore-gpu-blocklist --disable-gpu-compositing --disable-features=Vulkan" test_xeus_cpp.html + +When Chrome runs on a Linux CI display backed by Mesa llvmpipe, these flags are required: + +- ``--ignore-gpu-blocklist``: Chrome blocks llvmpipe as a software renderer. Without this flag, WebGL2 context creation fails. +- ``--disable-gpu-compositing``: Chrome's llvmpipe compositing path fails. Disabling it keeps rendering on the working GL path. +- ``--disable-features=Vulkan``: Chrome's Vulkan path fails on this display. Disabling it forces the GL path. + +macOS uses the native GPU and does not need these flags. To build and test Jupyter Lite with this kernel locally you can execute the following diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7ccf116e..39bf85d9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -44,6 +44,10 @@ set(XEUS_CPP_TESTS test_interpreter.cpp ) +if(EMSCRIPTEN) + list(APPEND XEUS_CPP_TESTS test_webgl.cpp) +endif() + add_executable(test_xeus_cpp ${XEUS_CPP_TESTS}) if(EMSCRIPTEN) @@ -78,6 +82,11 @@ if(EMSCRIPTEN) target_include_directories(test_xeus_cpp PRIVATE ${XEUS_CPP_INCLUDE_DIR}) + target_link_options(test_xeus_cpp PUBLIC + ${XEUS_CPP_WASM_WEBGL_LINK_OPTIONS} + "${XEUS_CPP_WASM_EXPORTED_RUNTIME_METHODS}" + ) + add_custom_command(TARGET test_xeus_cpp POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_INSTALL_PREFIX}/lib/libclangCppInterOp.so diff --git a/test/test_webgl.cpp b/test/test_webgl.cpp new file mode 100644 index 00000000..d8d0a266 --- /dev/null +++ b/test/test_webgl.cpp @@ -0,0 +1,150 @@ +/*************************************************************************** + * Copyright (c) 2026, xeus-cpp contributors + * + * Distributed under the terms of the BSD 3-Clause License. + * + * The full license is in the file LICENSE, distributed with this software. + ****************************************************************************/ + +// Verifies that the Emscripten build provides a working WebGL2 / +// OffscreenCanvas runtime. +// +// Needs a browser: skipped under node (no DOM, no OffscreenCanvas). + +#include +#include + +#include +#include +#include + +#include "doctest/doctest.h" + +namespace +{ + constexpr int clear_red = 64; + constexpr int clear_green = 128; + constexpr int clear_blue = 191; + + bool in_browser() + { + return EM_ASM_INT({ + return (typeof document !== 'undefined' + && typeof OffscreenCanvas !== 'undefined') ? 1 : 0; + }) != 0; + } + + bool browser_supports_webgl2() + { + return EM_ASM_INT({ + try + { + return document.createElement('canvas').getContext('webgl2') ? 1 : 0; + } + catch (e) + { + return 0; + } + }) != 0; + } + + // Whether the browser itself can hand a canvas to a worker. Deliberately + // independent of the build options: when the browser can and the kernel + // still fails to create the context, the build is missing the WebGL link + // options and the test must fail. + bool browser_can_transfer_canvas() + { + return EM_ASM_INT({ + return (typeof OffscreenCanvas !== 'undefined' + && typeof document.createElement('canvas').transferControlToOffscreen === 'function') + ? 1 : 0; + }) != 0; + } +} + +TEST_SUITE("webgl_runtime") +{ + TEST_CASE("Module.GL.offscreenCanvases is exported") + { + if (!in_browser()) + { + // node has no DOM; the browser runs of this binary do the check. + return; + } + // What xeus-lite uses to hand a transferred OffscreenCanvas to the + // kernel worker. + CHECK(EM_ASM_INT({ + return (Module.GL && Module.GL.offscreenCanvases !== undefined) ? 1 : 0; + }) == 1); + } + + TEST_CASE("WebGL2 context on an OffscreenCanvas renders") + { + if (!in_browser()) + { + return; + } + // Browser test runs must provide WebGL2 and OffscreenCanvas transfer support. + if (!browser_supports_webgl2()) + { + FAIL("browser lacks WebGL2 support"); + } + if (!browser_can_transfer_canvas()) + { + FAIL("browser lacks OffscreenCanvas transfer support"); + } + + // Emscripten's HTML shells always ship this canvas, but create it when + // running from a shell that does not. + EM_ASM({ + if (!document.querySelector('#canvas')) + { + var canvas = document.createElement('canvas'); + canvas.id = 'canvas'; + document.body.appendChild(canvas); + } + }); + // clang-format on + + EmscriptenWebGLContextAttributes attributes; + emscripten_webgl_init_context_attributes(&attributes); + attributes.majorVersion = 2; + // Only honoured with OFFSCREENCANVAS_SUPPORT: transfers control of the + // canvas to an OffscreenCanvas and registers it in + // Module.GL.offscreenCanvases. + attributes.explicitSwapControl = true; + + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context("#canvas", &attributes); + REQUIRE(context > 0); + + // The transfer happened and emscripten tracks the OffscreenCanvas. + CHECK(EM_ASM_INT({ + var entry = Module.GL.offscreenCanvases['canvas']; + return (entry && entry.canvas instanceof OffscreenCanvas) ? 1 : 0; + }) == 1); + + REQUIRE(emscripten_webgl_make_context_current(context) == EMSCRIPTEN_RESULT_SUCCESS); + + const char* version = reinterpret_cast(glGetString(GL_VERSION)); + REQUIRE(version != nullptr); + CHECK(std::string(version).find("WebGL 2.0") != std::string::npos); + + // Needs GL_ENABLE_GET_PROC_ADDRESS, otherwise the linker rejects it. + CHECK(emscripten_webgl_get_proc_address("glClearColor") != nullptr); + + glClearColor(clear_red / 255.0f, clear_green / 255.0f, clear_blue / 255.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + CHECK(glGetError() == GL_NO_ERROR); + + std::uint8_t pixel[4] = {0, 0, 0, 0}; + glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); + CHECK(glGetError() == GL_NO_ERROR); + CHECK(pixel[0] == clear_red); + CHECK(pixel[1] == clear_green); + CHECK(pixel[2] == clear_blue); + CHECK(pixel[3] == 255); + + CHECK(emscripten_webgl_commit_frame() == EMSCRIPTEN_RESULT_SUCCESS); + CHECK(emscripten_webgl_destroy_context(context) == EMSCRIPTEN_RESULT_SUCCESS); + } +}