From 75384ccfe12b979bdd410f672913836c4f87651b Mon Sep 17 00:00:00 2001 From: juj Date: Fri, 5 Jun 2020 17:34:11 +0300 Subject: [PATCH] Add checks for Emscripten API return values to make sure API calls are not failing (#2160) --- examples/common/entry/entry_html5.cpp | 22 +++++++++++----------- src/glcontext_egl.cpp | 6 +++++- src/glcontext_html5.cpp | 8 ++++---- src/renderer_gl.cpp | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/examples/common/entry/entry_html5.cpp b/examples/common/entry/entry_html5.cpp index 02242350e..d687af8ce 100644 --- a/examples/common/entry/entry_html5.cpp +++ b/examples/common/entry/entry_html5.cpp @@ -94,17 +94,17 @@ namespace entry // will take the first canvas element found on the web page. #define HTML5_TARGET_CANVAS_SELECTOR "#canvas" - emscripten_set_mousedown_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, mouseCb); - emscripten_set_mouseup_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, mouseCb); - emscripten_set_mousemove_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, mouseCb); + BX_CHECK(emscripten_set_mousedown_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, mouseCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_mousedown_callback() failed!"); + BX_CHECK(emscripten_set_mouseup_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, mouseCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_mouseup_callback() failed!"); + BX_CHECK(emscripten_set_mousemove_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, mouseCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_mousemove_callback() failed!"); - emscripten_set_wheel_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, wheelCb); + BX_CHECK(emscripten_set_wheel_callback(HTML5_TARGET_CANVAS_SELECTOR, this, true, wheelCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_wheel_callback() failed!"); - emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCb); - emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCb); - emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCb); + BX_CHECK(emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_keypress_callback() failed!"); + BX_CHECK(emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_keydown_callback() failed!"); + BX_CHECK(emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, keyCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_keyup_callback() failed!"); - emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, resizeCb); + BX_CHECK(emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, resizeCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_resize_callback() failed!"); EmscriptenFullscreenStrategy fullscreenStrategy = {}; fullscreenStrategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_DEFAULT; @@ -115,9 +115,9 @@ namespace entry emscripten_request_fullscreen_strategy(HTML5_TARGET_CANVAS_SELECTOR, false, &fullscreenStrategy); - emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, focusCb); - emscripten_set_focusin_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, focusCb); - emscripten_set_focusout_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, focusCb); + BX_CHECK(emscripten_set_focus_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, focusCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_focus_callback() failed!"); + BX_CHECK(emscripten_set_focusin_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, focusCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_focusin_callback() failed!"); + BX_CHECK(emscripten_set_focusout_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, this, true, focusCb) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_focusout_callback() failed!"); bgfx::PlatformData pd; bx::memSet(&pd, 0, sizeof(pd) ); diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index c4e726810..bb3fa9b6b 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -370,7 +370,11 @@ EGL_IMPORT ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format); } # elif BX_PLATFORM_EMSCRIPTEN - emscripten_set_canvas_size(_width, _height); +// TODO: Make this somehow configurable to the developer building a HTML5 page. Currently #canvas +// will take the first canvas element found on the web page. +#define HTML5_TARGET_CANVAS_SELECTOR "#canvas" + + BX_CHECK(emscripten_set_canvas_element_size(HTML5_TARGET_CANVAS_SELECTOR, _width, _height) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_canvas_element_size() failed in GlContext::resize()!"); # else BX_UNUSED(_width, _height); # endif // BX_PLATFORM_* diff --git a/src/glcontext_html5.cpp b/src/glcontext_html5.cpp index aa72d0a7b..c56d2e5ef 100644 --- a/src/glcontext_html5.cpp +++ b/src/glcontext_html5.cpp @@ -51,7 +51,7 @@ namespace bgfx { namespace gl void makeCurrent() { - emscripten_webgl_make_context_current(m_context); + BX_CHECK(emscripten_webgl_make_context_current(m_context) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_webgl_make_context_current() failed!"); } void swapBuffers() @@ -75,7 +75,7 @@ namespace bgfx { namespace gl m_primary = createSwapChain((void*)canvas); if (_width && _height) - emscripten_set_canvas_element_size(canvas, (int)_width, (int)_height); + BX_CHECK(emscripten_set_canvas_element_size(canvas, (int)_width, (int)_height) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_canvas_element_size() failed in GlContext::create()!"); makeCurrent(m_primary); } @@ -101,7 +101,7 @@ namespace bgfx { namespace gl return; } - emscripten_set_canvas_element_size(m_primary->m_canvas, (int) _width, (int) _height); + BX_CHECK(emscripten_set_canvas_element_size(m_primary->m_canvas, (int) _width, (int) _height) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_canvas_element_size() failed in GlContext::resize()!"); } SwapChainGL* GlContext::createSwapChain(void* _nwh) @@ -127,7 +127,7 @@ namespace bgfx { namespace gl EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(canvas, &s_attrs); if (context > 0) { - emscripten_webgl_make_context_current(context); + BX_CHECK(emscripten_webgl_make_context_current(context) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_webgl_make_context_current() failed in GlContext::createSwapChain()!"); SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(context, canvas); diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 1f8e315e1..8e13ae5b7 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -1599,7 +1599,7 @@ namespace bgfx { namespace gl // Avoid creating test textures for WebGL, that causes error noise in the browser console; instead examine the supported texture formats from the spec. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context(); EmscriptenWebGLContextAttributes attrs; - emscripten_webgl_get_context_attributes(ctx, &attrs); + BX_CHECK(emscripten_webgl_get_context_attributes(ctx, &attrs) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_webgl_get_context_attributes() failed in isTextureFormatValidPerSpec()!"); int glesVersion = attrs.majorVersion + 1; switch(_format) {