diff --git a/src/Magnum/GL/Test/CMakeLists.txt b/src/Magnum/GL/Test/CMakeLists.txt index b856f9728..08cdff37c 100644 --- a/src/Magnum/GL/Test/CMakeLists.txt +++ b/src/Magnum/GL/Test/CMakeLists.txt @@ -106,7 +106,6 @@ endif() if(BUILD_GL_TESTS) corrade_add_test(GLAbstractTextureGLTest AbstractTextureGLTest.cpp LIBRARIES MagnumOpenGLTester) corrade_add_test(GLBufferGLTest BufferGLTest.cpp LIBRARIES MagnumOpenGLTester) - corrade_add_test(GLContextGLTest ContextGLTest.cpp LIBRARIES MagnumOpenGLTester) corrade_add_test(GLCubeMapTextureGLTest CubeMapTextureGLTest.cpp LIBRARIES MagnumOpenGLTester) corrade_add_test(GLFramebufferGLTest FramebufferGLTest.cpp LIBRARIES MagnumOpenGLTester) corrade_add_test(GLMeshGLTest MeshGLTest.cpp LIBRARIES MagnumOpenGLTesterTestLib) @@ -120,6 +119,12 @@ if(BUILD_GL_TESTS) ${GLAbstractShaderProgramGLTest_RES} LIBRARIES MagnumOpenGLTester) + corrade_add_test(GLContextGLTest ContextGLTest.cpp LIBRARIES MagnumOpenGLTester) + if(NOT CORRADE_TARGET_EMSCRIPTEN) + find_package(Threads REQUIRED) + target_link_libraries(GLContextGLTest PRIVATE Threads::Threads) + endif() + if(CORRADE_TARGET_EMSCRIPTEN OR CORRADE_TARGET_ANDROID) set(SHADERGLTEST_FILES_DIR "ShaderGLTestFiles") else() diff --git a/src/Magnum/GL/Test/ContextGLTest.cpp b/src/Magnum/GL/Test/ContextGLTest.cpp index 63c234f0b..00f91b190 100644 --- a/src/Magnum/GL/Test/ContextGLTest.cpp +++ b/src/Magnum/GL/Test/ContextGLTest.cpp @@ -29,11 +29,19 @@ #include "Magnum/GL/Extensions.h" #include "Magnum/GL/OpenGLTester.h" +#ifndef CORRADE_TARGET_EMSCRIPTEN +#include +#endif + namespace Magnum { namespace GL { namespace Test { namespace { struct ContextGLTest: OpenGLTester { explicit ContextGLTest(); + #ifndef CORRADE_TARGET_EMSCRIPTEN + void multithreaded(); + #endif + void isVersionSupported(); #ifndef MAGNUM_TARGET_GLES void isVersionSupportedES(); @@ -44,15 +52,51 @@ struct ContextGLTest: OpenGLTester { }; ContextGLTest::ContextGLTest() { - addTests({&ContextGLTest::isVersionSupported, - #ifndef MAGNUM_TARGET_GLES - &ContextGLTest::isVersionSupportedES, - #endif - &ContextGLTest::supportedVersion, - &ContextGLTest::isExtensionSupported, - &ContextGLTest::isExtensionDisabled}); + addTests({ + #ifndef CORRADE_TARGET_EMSCRIPTEN + &ContextGLTest::multithreaded, + #endif + + &ContextGLTest::isVersionSupported, + #ifndef MAGNUM_TARGET_GLES + &ContextGLTest::isVersionSupportedES, + #endif + &ContextGLTest::supportedVersion, + &ContextGLTest::isExtensionSupported, + &ContextGLTest::isExtensionDisabled}); } +#ifndef CORRADE_TARGET_EMSCRIPTEN +void ContextGLTest::multithreaded() { + CORRADE_VERIFY(Context::hasCurrent()); + Containers::Optional otherThreadHasCurrent; + + std::thread t{[](Containers::Optional& hasCurrent) { + hasCurrent = Context::hasCurrent(); + }, std::ref(otherThreadHasCurrent)}; + + t.join(); + + CORRADE_VERIFY(otherThreadHasCurrent); + + Debug{} << "MAGNUM_BUILD_MULTITHREADED defined:" << + #ifdef MAGNUM_BUILD_MULTITHREADED + true + #else + false + #endif + ; + + Debug{} << "Current context visible in another thread:" << *otherThreadHasCurrent; + + #ifdef MAGNUM_BUILD_MULTITHREADED + CORRADE_VERIFY(!*otherThreadHasCurrent); + #else + CORRADE_VERIFY(*otherThreadHasCurrent); + #endif +} +#endif + void ContextGLTest::isVersionSupported() { const Version v = Context::current().version(); CORRADE_VERIFY(Context::current().isVersionSupported(v));