From d537cd37045faf397614f4e51688d010e9d1d1fb Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 28 Mar 2024 14:11:46 -0700 Subject: [PATCH 1/3] cmake: fix vwebp_sdl compile w/libsdl-org release Prebuilt SDL2 releases from https://github.com/libsdl-org/SDL/releases provide include/SDL.h. Test for this and set WEBP_HAVE_JUST_SDL_H accordingly. This was inspired by: https://github.com/microsoft/vcpkg/blob/d0b3e8efe3a885dcd5b764bf578e73c0bb83cc9f/ports/libwebp/0008-sdl.patch Bug: b:301040580 Change-Id: I320ed1a23352fc4035d491399fdba184dc927554 Test: cmake .. -DCMAKE_PREFIX_PATH=/cmake --- CMakeLists.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec2850b0..c8310de6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -665,6 +665,21 @@ if(WEBP_BUILD_EXTRAS) ${CMAKE_CURRENT_BINARY_DIR}/src ${SDL2_INCLUDE_DIRS}) set(WEBP_HAVE_SDL 1) target_compile_definitions(vwebp_sdl PUBLIC WEBP_HAVE_SDL) + + set(CMAKE_REQUIRED_INCLUDES "${SDL2_INCLUDE_DIRS}") + check_c_source_compiles( + " + #define SDL_MAIN_HANDLED + #include \"SDL.h\" + int main(void) { + return 0; + } + " + HAVE_JUST_SDL_H) + set(CMAKE_REQUIRED_INCLUDES) + if(HAVE_JUST_SDL_H) + target_compile_definitions(vwebp_sdl PRIVATE WEBP_HAVE_JUST_SDL_H) + endif() endif() endif() From e2c8f233ef4c0fd4cfdb3959d12181e54199d74e Mon Sep 17 00:00:00 2001 From: James Zern Date: Thu, 28 Mar 2024 17:51:39 -0700 Subject: [PATCH 2/3] cmake,wasm: simplify SDL2 related flags Use the result of find_package(SDL2); the cmake module is available prior to downloading the port. Previously the result of this call was ignored. This also updates the flags to use the results of SDL2_LIBRARIES which may be more future proof than specifying them manually. SDL2_INCLUDE_DIRS do not appear to be necessary with -sUSE_SDL2. Based in part on: https://github.com/microsoft/vcpkg/blob/d0b3e8efe3a885dcd5b764bf578e73c0bb83cc9f/ports/libwebp/0008-sdl.patch Bug: b:301040580 Change-Id: I83386b6c3d9628e3f0d9d17f20d47338d0db044a --- CMakeLists.txt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8310de6..b785a8e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -694,13 +694,10 @@ if(WEBP_BUILD_WEBP_JS) else() set(emscripten_stack_size "-sTOTAL_STACK=5MB") endif() - # Set SDL2 flags so that ports are downloaded by emscripten. - set(EMSCRIPTEN_SDL2_FLAGS "-sUSE_SDL=2 -sUSE_SDL_IMAGE=2") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EMSCRIPTEN_SDL2_FLAGS}") + find_package(SDL2 REQUIRED) # wasm2js does not support SIMD. if(NOT WEBP_ENABLE_SIMD) # JavaScript version - find_package(SDL2 QUIET) add_executable(webp_js ${CMAKE_CURRENT_SOURCE_DIR}/extras/webp_to_sdl.c) target_link_libraries(webp_js webpdecoder SDL2) target_include_directories(webp_js PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) @@ -708,10 +705,13 @@ if(WEBP_BUILD_WEBP_JS) set_target_properties( webp_js PROPERTIES + # Emscripten puts -sUSE_SDL2=1 in this variable, though it's needed at + # compile time to ensure the headers are downloaded. + COMPILE_OPTIONS "${SDL2_LIBRARIES}" LINK_FLAGS "-sWASM=0 ${emscripten_stack_size} \ -sEXPORTED_FUNCTIONS=_WebPToSDL -sINVOKE_RUN=0 \ - -sEXPORTED_RUNTIME_METHODS=cwrap ${EMSCRIPTEN_SDL2_FLAGS} \ + -sEXPORTED_RUNTIME_METHODS=cwrap ${SDL2_LIBRARIES} \ -sALLOW_MEMORY_GROWTH") set_target_properties(webp_js PROPERTIES OUTPUT_NAME webp) target_compile_definitions(webp_js PUBLIC EMSCRIPTEN WEBP_HAVE_SDL) @@ -724,10 +724,13 @@ if(WEBP_BUILD_WEBP_JS) set_target_properties( webp_wasm PROPERTIES + # Emscripten puts -sUSE_SDL2=1 in this variable, though it's needed at + # compile time to ensure the headers are downloaded. + COMPILE_OPTIONS "${SDL2_LIBRARIES}" LINK_FLAGS "-sWASM=1 ${emscripten_stack_size} \ -sEXPORTED_FUNCTIONS=_WebPToSDL -sINVOKE_RUN=0 \ - -sEXPORTED_RUNTIME_METHODS=cwrap ${EMSCRIPTEN_SDL2_FLAGS} \ + -sEXPORTED_RUNTIME_METHODS=cwrap ${SDL2_LIBRARIES} \ -sALLOW_MEMORY_GROWTH") target_compile_definitions(webp_wasm PUBLIC EMSCRIPTEN WEBP_HAVE_SDL) From f88666eb4798123f6cde17a2eb49cd0b78384951 Mon Sep 17 00:00:00 2001 From: James Zern Date: Fri, 29 Mar 2024 11:53:13 -0700 Subject: [PATCH 3/3] webp_js/*.html: fix canvas mapping after: 24d7f9cb Switch code to SDL2. fixes: webp_wasm.js:1 Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at Object.registerOrRemoveHandler (webp_wasm.js:1:101330) at registerMouseEventCallback (webp_wasm.js:1:154227) at _emscripten_set_mousemove_callback_on_thread (webp_wasm.js:1:155015) ... The SDL2 port forces the canvas id to '#canvas': https://github.com/emscripten-ports/SDL2/blob/324df6865ae3c7d194ed233a86867c48ec96c6a3/src/video/emscripten/SDL_emscriptenvideo.c#L210 This change maps '#output_canvas' to this entry in specialHTMLTargets[]: https://emscripten.org/docs/api_reference/html5.h.html https://github.com/libsdl-org/SDL/issues/5260 & https://github.com/emscripten-ports/SDL2/issues/130 may also be related. Change-Id: I26f4aa22b9d68b0fc45b83edfe6fe074b59a82a7 Test: emscripten 3.1.16 --- webp_js/index.html | 2 ++ webp_js/index_wasm.html | 2 ++ 2 files changed, 4 insertions(+) diff --git a/webp_js/index.html b/webp_js/index.html index 33cacb4c..e7c3b652 100644 --- a/webp_js/index.html +++ b/webp_js/index.html @@ -29,6 +29,8 @@ function decode(webp_data, canvas_id) { // clear previous picture (if any) Module.canvas = canvas; canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height); + // Map this canvas to the default selector used by emscripten/SDL2. + specialHTMLTargets["#canvas"] = Module.canvas; // decode and measure timing var start = new Date(); var ret = WebpToCanvas(webp_data, webp_data.length); diff --git a/webp_js/index_wasm.html b/webp_js/index_wasm.html index 7a9b362a..29933aba 100644 --- a/webp_js/index_wasm.html +++ b/webp_js/index_wasm.html @@ -30,6 +30,8 @@ function decode(webp_data, canvas_id) { // clear previous picture (if any) Module.canvas = canvas; canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height); + // Map this canvas to the default selector used by emscripten/SDL2. + specialHTMLTargets["#canvas"] = Module.canvas; // decode and measure timing var start = new Date(); var ret = WebpToCanvas(webp_data, webp_data.length);