From 128926a41b368e166af63515370d9c9367e3dda2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Mon, 11 Jun 2018 11:49:10 +0200 Subject: GHOST: Delay opengl context initialization This way they can be init in their owner thread. Contexts should not be shared accross threads. Once you make a context active on a thread it is owned by the thread. This commit only have the GLX backend updated but should not break orther platform. --- intern/gawain/src/gwn_vertex_array_id.cpp | 5 +++-- intern/ghost/intern/GHOST_ContextGLX.cpp | 30 ++++++++++++++++++++++++------ intern/ghost/intern/GHOST_ContextGLX.h | 2 ++ source/blender/draw/intern/draw_manager.c | 2 ++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/intern/gawain/src/gwn_vertex_array_id.cpp b/intern/gawain/src/gwn_vertex_array_id.cpp index ad60dea7542..2ab38a8fe18 100644 --- a/intern/gawain/src/gwn_vertex_array_id.cpp +++ b/intern/gawain/src/gwn_vertex_array_id.cpp @@ -18,7 +18,7 @@ #include #include -#if TRUST_NO_ONE +#if 0 extern "C" { extern int BLI_thread_is_main(void); // Blender-specific function } @@ -68,7 +68,8 @@ static void clear_orphans(Gwn_Context* ctx) Gwn_Context* GWN_context_create(void) { #if TRUST_NO_ONE - assert(thread_is_main()); + /* We cannot rely on this anymore. */ + // assert(thread_is_main()); #endif Gwn_Context* ctx = new Gwn_Context; glGenVertexArrays(1, &ctx->default_vao); diff --git a/intern/ghost/intern/GHOST_ContextGLX.cpp b/intern/ghost/intern/GHOST_ContextGLX.cpp index 815189b9098..1365a1a9c24 100644 --- a/intern/ghost/intern/GHOST_ContextGLX.cpp +++ b/intern/ghost/intern/GHOST_ContextGLX.cpp @@ -110,6 +110,9 @@ GHOST_TSuccess GHOST_ContextGLX::swapBuffers() GHOST_TSuccess GHOST_ContextGLX::activateDrawingContext() { + if (m_init == false) { + initContext(); + } if (m_display) { return ::glXMakeCurrent(m_display, m_window, m_context) ? GHOST_kSuccess : GHOST_kFailure; } @@ -184,7 +187,8 @@ const bool GLXEW_ARB_create_context_robustness = glxewInit(); #endif /* USE_GLXEW_INIT_WORKAROUND */ - + /* Only init the non-offscreen context directly */ + const bool do_init = (m_window != None); if (GLXEW_ARB_create_context) { int profileBitCore = m_contextProfileMask & GLX_CONTEXT_CORE_PROFILE_BIT_ARB; @@ -304,12 +308,27 @@ const bool GLXEW_ARB_create_context_robustness = GHOST_TSuccess success; if (m_context != NULL) { - const unsigned char *version; - if (!s_sharedContext) s_sharedContext = m_context; s_sharedCount++; + } + + if (do_init) { + success = initContext(); + } + + GHOST_X11_ERROR_HANDLERS_RESTORE(handler_store); + + return success; +} + +GHOST_TSuccess GHOST_ContextGLX::initContext() +{ + GHOST_TSuccess success; + + if (m_context != NULL) { + const unsigned char *version; glXMakeCurrent(m_display, m_window, m_context); @@ -341,13 +360,12 @@ const bool GLXEW_ARB_create_context_robustness = success = GHOST_kFailure; } - - GHOST_X11_ERROR_HANDLERS_RESTORE(handler_store); + /* We tag as init even if it fails. */ + m_init = true; return success; } - GHOST_TSuccess GHOST_ContextGLX::releaseNativeHandles() { m_window = 0; diff --git a/intern/ghost/intern/GHOST_ContextGLX.h b/intern/ghost/intern/GHOST_ContextGLX.h index ded1b293659..b4d6a841d72 100644 --- a/intern/ghost/intern/GHOST_ContextGLX.h +++ b/intern/ghost/intern/GHOST_ContextGLX.h @@ -116,6 +116,7 @@ public: private: void initContextGLXEW(); + GHOST_TSuccess initContext(); Display *m_display; GLXFBConfig m_fbconfig; @@ -128,6 +129,7 @@ private: const int m_contextResetNotificationStrategy; GLXContext m_context; + bool m_init; /** The first created OpenGL context (for sharing display lists) */ static GLXContext s_sharedContext; diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c index 65702a65541..5f059f92d5f 100644 --- a/source/blender/draw/intern/draw_manager.c +++ b/source/blender/draw/intern/draw_manager.c @@ -2160,6 +2160,8 @@ void DRW_opengl_context_create(void) } /* This changes the active context. */ DST.gl_context = WM_opengl_context_create(); + /* Make the context active for this thread (main thread) */ + WM_opengl_context_activate(DST.gl_context); /* Be sure to create gawain.context too. */ DST.gwn_context = GWN_context_create(); if (!G.background) { -- cgit v1.2.3