From fa3107778963b165106b6604cf832e90b73b2cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 12 Dec 2020 13:52:47 +0100 Subject: [PATCH] Platform: sync EmscriptenApp canvas size setup for the contextless case. It was just completely wrong there, probably just forgot to update that. --- doc/changelog.dox | 3 +++ src/Magnum/Platform/EmscriptenApplication.cpp | 24 ++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 13c7e5c19..1c9d4560b 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -270,6 +270,9 @@ See also: - Fixed @ref Platform::GlfwApplication, @ref Platform::Sdl2Application and @ref Platform::EmscriptenApplication to correctly print app-specified DPI scaling in its verbose output +- Fixed canvas size setup in @ref Platform::EmscriptenApplication to be the + same in the @ref Platform::EmscriptenApplication::Configuration::WindowFlag::Contextless "Contextless" + case as for a WebGL-enabled context @subsection changelog-latest-deprecated Deprecated APIs diff --git a/src/Magnum/Platform/EmscriptenApplication.cpp b/src/Magnum/Platform/EmscriptenApplication.cpp index 526762f7c..978b147d4 100644 --- a/src/Magnum/Platform/EmscriptenApplication.cpp +++ b/src/Magnum/Platform/EmscriptenApplication.cpp @@ -319,11 +319,29 @@ bool EmscriptenApplication::tryCreate(const Configuration& configuration) { _canvasTarget = (_deprecatedTargetBehavior ? "" : "#") + canvasId(); - _dpiScaling = dpiScaling(configuration); + /* Get CSS canvas size and cache it. This is used later to detect canvas + resizes in emscripten_set_resize_callback() and fire viewport events, + because browsers are only required to fire resize events on the window + and not on particular DOM elements. */ + _lastKnownCanvasSize = windowSize(); + + /* By default Emscripten creates a 300x150 canvas. That's so freaking + random I'm getting mad. Use the real (CSS pixels) canvas size instead, + if the size is not hardcoded from the configuration. This is then + multiplied by the DPI scaling. */ + Vector2i canvasSize; if(!configuration.size().isZero()) { - const Vector2i scaledCanvasSize = configuration.size()*_dpiScaling; - emscripten_set_canvas_element_size(_canvasTarget.data(), scaledCanvasSize.x(), scaledCanvasSize.y()); + canvasSize = configuration.size(); + /* Because hardcoding canvas size for WebGL is usually a wrong thing to + do, notify about that in the verbose output */ + Debug{verbose} << "Platform::EmscriptenApplication::tryCreate(): hardcoded canvas size" << canvasSize; + } else { + canvasSize = _lastKnownCanvasSize; + Debug{verbose} << "Platform::EmscriptenApplication::tryCreate(): autodetected canvas size" << canvasSize; } + _dpiScaling = dpiScaling(configuration); + const Vector2i scaledCanvasSize = canvasSize*_dpiScaling*_devicePixelRatio; + emscripten_set_canvas_element_size(_canvasTarget.data(), scaledCanvasSize.x(), scaledCanvasSize.y()); setupCallbacks(!!(configuration.windowFlags() & Configuration::WindowFlag::Resizable)); setupAnimationFrame(!!(configuration.windowFlags() & Configuration::WindowFlag::AlwaysRequestAnimationFrame));