comparison Framework/Viewport/WebAssemblyViewport.cpp @ 947:1091b2adeb5a toa2019081001

Fixed animation frame stopping when returning false + big work on the OpenGL objects to make them lost context-safe + debug code to forcefully tag a context as lost + debug macros
author Benjamin Golinvaux <bgo@osimis.io>
date Sat, 10 Aug 2019 13:07:31 +0200
parents 685c9a2d115f
children a7351ad54960
comparison
equal deleted inserted replaced
946:dbe3e1e47019 947:1091b2adeb5a
41 RegisterContextCallbacks(); 41 RegisterContextCallbacks();
42 } 42 }
43 43
44 void WebAssemblyOpenGLViewport::UpdateSize() 44 void WebAssemblyOpenGLViewport::UpdateSize()
45 { 45 {
46 if(compositor_.get() != NULL)
47 compositor_->Refresh(); // Then refresh the content of the canvas
46 context_.UpdateSize(); // First read the size of the canvas 48 context_.UpdateSize(); // First read the size of the canvas
47 compositor_->Refresh(); // Then refresh the content of the canvas
48 } 49 }
49 50
50 /* 51 /*
51 typedef EM_BOOL (*em_webgl_context_callback)(int eventType, const void *reserved, void *userData); 52 typedef EM_BOOL (*em_webgl_context_callback)(int eventType, const void *reserved, void *userData);
52 53
72 int eventType, const void* reserved, void* userData) 73 int eventType, const void* reserved, void* userData)
73 { 74 {
74 ORTHANC_ASSERT(eventType == EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED); 75 ORTHANC_ASSERT(eventType == EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED);
75 WebAssemblyOpenGLViewport* viewport = reinterpret_cast<WebAssemblyOpenGLViewport*>(userData); 76 WebAssemblyOpenGLViewport* viewport = reinterpret_cast<WebAssemblyOpenGLViewport*>(userData);
76 return viewport->OpenGLContextRestored(); 77 return viewport->OpenGLContextRestored();
78 }
79
80 void WebAssemblyOpenGLViewport::DisableCompositor()
81 {
82 compositor_.reset(NULL);
83 }
84
85 void WebAssemblyOpenGLViewport::Refresh()
86 {
87 try
88 {
89 // the compositor COULD be dead!
90 if (GetCompositor())
91 GetCompositor()->Refresh();
92 }
93 catch (const OpenGLContextLostException& e)
94 {
95 LOG(WARNING) << "Context " << std::hex << e.context_ << " is lost! Compositor will be disabled.";
96 DisableCompositor();
97 // we now need to wait for the "context restored" callback
98 }
99 catch (...)
100 {
101 // something else nasty happened
102 throw;
103 }
104 }
105
106 void WebAssemblyOpenGLViewport::RestoreCompositor()
107 {
108 // the context must have been restored!
109 ORTHANC_ASSERT(!context_.IsContextLost());
110 if (compositor_.get() == NULL)
111 {
112 compositor_.reset(new OpenGLCompositor(context_, GetScene()));
113 }
114 else
115 {
116 LOG(WARNING) << "RestoreCompositor() called for \"" << GetCanvasIdentifier() << "\" while it was NOT lost! Nothing done.";
117 }
118 }
119
120 bool WebAssemblyOpenGLViewport::OpenGLContextLost()
121 {
122 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextLost() for canvas: " << GetCanvasIdentifier();
123 DisableCompositor();
124 return true;
125 }
126
127 bool WebAssemblyOpenGLViewport::OpenGLContextRestored()
128 {
129 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextRestored() for canvas: " << GetCanvasIdentifier();
130 RestoreCompositor();
131 UpdateSize();
132 return false;
77 } 133 }
78 134
79 void WebAssemblyOpenGLViewport::RegisterContextCallbacks() 135 void WebAssemblyOpenGLViewport::RegisterContextCallbacks()
80 { 136 {
81 // TODO: what's the impact of userCapture=true ? 137 // TODO: what's the impact of userCapture=true ?
82 const char* canvasId = GetCanvasIdentifier().c_str(); 138 const char* canvasId = GetCanvasIdentifier().c_str();
83 void* that = reinterpret_cast<void*>(this); 139 void* that = reinterpret_cast<void*>(this);
84 EMSCRIPTEN_RESULT status = EMSCRIPTEN_RESULT_SUCCESS; 140 EMSCRIPTEN_RESULT status = EMSCRIPTEN_RESULT_SUCCESS;
85 141
86 status = emscripten_set_webglcontextlost_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextLost_callback); 142 //status = emscripten_set_webglcontextlost_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextLost_callback);
87 if (status != EMSCRIPTEN_RESULT_SUCCESS) 143 //if (status != EMSCRIPTEN_RESULT_SUCCESS)
88 { 144 //{
89 std::stringstream ss; 145 // std::stringstream ss;
90 ss << "Error while calling emscripten_set_webglcontextlost_callback for: \"" << GetCanvasIdentifier() << "\""; 146 // ss << "Error while calling emscripten_set_webglcontextlost_callback for: \"" << GetCanvasIdentifier() << "\"";
91 std::string msg = ss.str(); 147 // std::string msg = ss.str();
92 LOG(ERROR) << msg; 148 // LOG(ERROR) << msg;
93 ORTHANC_ASSERT(false, msg.c_str()); 149 // ORTHANC_ASSERT(false, msg.c_str());
94 } 150 //}
95 status = emscripten_set_webglcontextrestored_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextRestored_callback); 151 status = emscripten_set_webglcontextrestored_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextRestored_callback);
96 if (status != EMSCRIPTEN_RESULT_SUCCESS) 152 if (status != EMSCRIPTEN_RESULT_SUCCESS)
97 { 153 {
98 std::stringstream ss; 154 std::stringstream ss;
99 ss << "Error while calling emscripten_set_webglcontextrestored_callback for: \"" << GetCanvasIdentifier() << "\""; 155 ss << "Error while calling emscripten_set_webglcontextrestored_callback for: \"" << GetCanvasIdentifier() << "\"";
100 std::string msg = ss.str(); 156 std::string msg = ss.str();
101 LOG(ERROR) << msg; 157 LOG(ERROR) << msg;
102 ORTHANC_ASSERT(false, msg.c_str()); 158 ORTHANC_ASSERT(false, msg.c_str());
103 } 159 }
104 } 160 LOG(TRACE) << "WebAssemblyOpenGLViewport::RegisterContextCallbacks() SUCCESS!!!";
105
106 bool WebAssemblyOpenGLViewport::OpenGLContextLost()
107 {
108 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextLost() for canvas: " << GetCanvasIdentifier();
109 return false;
110 }
111
112 bool WebAssemblyOpenGLViewport::OpenGLContextRestored()
113 {
114 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextRestored() for canvas: " << GetCanvasIdentifier();
115 return false;
116 } 161 }
117 162
118 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas) : 163 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas) :
119 WebAssemblyViewport(canvas), 164 WebAssemblyViewport(canvas),
120 canvas_(canvas), 165 canvas_(canvas),
160 } 205 }
161 206
162 void WebAssemblyCairoViewport::Refresh() 207 void WebAssemblyCairoViewport::Refresh()
163 { 208 {
164 LOG(INFO) << "refreshing cairo viewport, TODO: blit to the canvans.getContext('2d')"; 209 LOG(INFO) << "refreshing cairo viewport, TODO: blit to the canvans.getContext('2d')";
165 GetCompositor().Refresh(); 210 GetCompositor()->Refresh();
166 } 211 }
167 212
168 } 213 }