annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
16 *
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "WebAssemblyViewport.h"
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
23 #include <emscripten/html5.h>
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 namespace OrthancStone
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 {
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
27 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas) :
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
28 WebAssemblyViewport(canvas),
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
29 context_(canvas)
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
31 compositor_.reset(new OpenGLCompositor(context_, GetScene()));
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
32 RegisterContextCallbacks();
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 }
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
35 WebAssemblyOpenGLViewport::WebAssemblyOpenGLViewport(const std::string& canvas,
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
36 boost::shared_ptr<Scene2D>& scene) :
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
37 WebAssemblyViewport(canvas, scene),
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
38 context_(canvas)
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 {
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
40 compositor_.reset(new OpenGLCompositor(context_, GetScene()));
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
41 RegisterContextCallbacks();
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 }
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
44 void WebAssemblyOpenGLViewport::UpdateSize()
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
46 if(compositor_.get() != NULL)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
47 compositor_->Refresh(); // Then refresh the content of the canvas
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 context_.UpdateSize(); // First read the size of the canvas
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
49 }
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
50
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
51 /*
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
52 typedef EM_BOOL (*em_webgl_context_callback)(int eventType, const void *reserved, void *userData);
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
53
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
54 EMSCRIPTEN_EVENT_WEBGLCONTEXTLOST EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
55
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
56 EMSCRIPTEN_RESULT emscripten_set_webglcontextlost_callback(
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
57 const char *target, void *userData, EM_BOOL useCapture, em_webgl_context_callback callback)
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
58
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
59 EMSCRIPTEN_RESULT emscripten_set_webglcontextrestored_callback(
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
60 const char *target, void *userData, EM_BOOL useCapture, em_webgl_context_callback callback)
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
61
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
62 */
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
63
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
64 EM_BOOL WebAssemblyOpenGLViewport_OpenGLContextLost_callback(
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
65 int eventType, const void* reserved, void* userData)
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
66 {
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
67 ORTHANC_ASSERT(eventType == EMSCRIPTEN_EVENT_WEBGLCONTEXTLOST);
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
68 WebAssemblyOpenGLViewport* viewport = reinterpret_cast<WebAssemblyOpenGLViewport*>(userData);
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
69 return viewport->OpenGLContextLost();
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
70 }
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
71
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
72 EM_BOOL WebAssemblyOpenGLViewport_OpenGLContextRestored_callback(
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
73 int eventType, const void* reserved, void* userData)
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
74 {
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
75 ORTHANC_ASSERT(eventType == EMSCRIPTEN_EVENT_WEBGLCONTEXTRESTORED);
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
76 WebAssemblyOpenGLViewport* viewport = reinterpret_cast<WebAssemblyOpenGLViewport*>(userData);
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
77 return viewport->OpenGLContextRestored();
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 }
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
79
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
80 void WebAssemblyOpenGLViewport::DisableCompositor()
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
81 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
82 compositor_.reset(NULL);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
83 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
84
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
85 void WebAssemblyOpenGLViewport::Refresh()
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
86 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
87 try
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
88 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
89 // the compositor COULD be dead!
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
90 if (GetCompositor())
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
91 GetCompositor()->Refresh();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
92 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
93 catch (const OpenGLContextLostException& e)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
94 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
95 LOG(WARNING) << "Context " << std::hex << e.context_ << " is lost! Compositor will be disabled.";
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
96 DisableCompositor();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
97 // we now need to wait for the "context restored" callback
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
98 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
99 catch (...)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
100 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
101 // something else nasty happened
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
102 throw;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
103 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
104 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
105
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
106 void WebAssemblyOpenGLViewport::RestoreCompositor()
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
107 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
108 // the context must have been restored!
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
109 ORTHANC_ASSERT(!context_.IsContextLost());
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
110 if (compositor_.get() == NULL)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
111 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
112 compositor_.reset(new OpenGLCompositor(context_, GetScene()));
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
113 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
114 else
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
115 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
116 LOG(WARNING) << "RestoreCompositor() called for \"" << GetCanvasIdentifier() << "\" while it was NOT lost! Nothing done.";
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
117 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
118 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
119
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
120 bool WebAssemblyOpenGLViewport::OpenGLContextLost()
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
121 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
122 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextLost() for canvas: " << GetCanvasIdentifier();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
123 DisableCompositor();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
124 return true;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
125 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
126
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
127 bool WebAssemblyOpenGLViewport::OpenGLContextRestored()
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
128 {
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
129 LOG(ERROR) << "WebAssemblyOpenGLViewport::OpenGLContextRestored() for canvas: " << GetCanvasIdentifier();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
130 RestoreCompositor();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
131 UpdateSize();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
132 return false;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
133 }
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
134
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
135 void WebAssemblyOpenGLViewport::RegisterContextCallbacks()
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
136 {
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
137 // TODO: what's the impact of userCapture=true ?
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
138 const char* canvasId = GetCanvasIdentifier().c_str();
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
139 void* that = reinterpret_cast<void*>(this);
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
140 EMSCRIPTEN_RESULT status = EMSCRIPTEN_RESULT_SUCCESS;
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
141
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
142 //status = emscripten_set_webglcontextlost_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextLost_callback);
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
143 //if (status != EMSCRIPTEN_RESULT_SUCCESS)
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
144 //{
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
145 // std::stringstream ss;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
146 // ss << "Error while calling emscripten_set_webglcontextlost_callback for: \"" << GetCanvasIdentifier() << "\"";
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
147 // std::string msg = ss.str();
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
148 // LOG(ERROR) << msg;
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
149 // ORTHANC_ASSERT(false, msg.c_str());
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
150 //}
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
151 status = emscripten_set_webglcontextrestored_callback(canvasId, that, true, WebAssemblyOpenGLViewport_OpenGLContextRestored_callback);
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
152 if (status != EMSCRIPTEN_RESULT_SUCCESS)
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
153 {
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
154 std::stringstream ss;
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
155 ss << "Error while calling emscripten_set_webglcontextrestored_callback for: \"" << GetCanvasIdentifier() << "\"";
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
156 std::string msg = ss.str();
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
157 LOG(ERROR) << msg;
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
158 ORTHANC_ASSERT(false, msg.c_str());
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
159 }
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
160 LOG(TRACE) << "WebAssemblyOpenGLViewport::RegisterContextCallbacks() SUCCESS!!!";
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
161 }
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
162
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
163 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas) :
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
164 WebAssemblyViewport(canvas),
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
165 canvas_(canvas),
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
166 compositor_(GetScene(), 1024, 768)
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
167 {
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
168 }
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
169
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
170 WebAssemblyCairoViewport::WebAssemblyCairoViewport(const std::string& canvas,
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
171 boost::shared_ptr<Scene2D>& scene) :
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
172 WebAssemblyViewport(canvas, scene),
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
173 canvas_(canvas),
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
174 compositor_(GetScene(), 1024, 768)
907
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
175 {
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
176 }
722ee73e6ba2 cleanup + started to implement WebAssemblyCairoViewport (wip)
Alain Mazy <alain@mazy.be>
parents: 891
diff changeset
177
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
178 void WebAssemblyCairoViewport::UpdateSize()
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
179 {
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
180 LOG(INFO) << "updating cairo viewport size";
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
181 double w, h;
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
182 emscripten_get_element_css_size(canvas_.c_str(), &w, &h);
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
183
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
184 /**
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
185 * Emscripten has the function emscripten_get_element_css_size()
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
186 * to query the width and height of a named HTML element. I'm
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
187 * calling this first to get the initial size of the canvas DOM
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
188 * element, and then call emscripten_set_canvas_size() to
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
189 * initialize the framebuffer size of the canvas to the same
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
190 * size as its DOM element.
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
191 * https://floooh.github.io/2017/02/22/emsc-html.html
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
192 **/
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
193 unsigned int canvasWidth = 0;
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
194 unsigned int canvasHeight = 0;
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
195
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
196 if (w > 0 ||
942
685c9a2d115f Added missing ORTHANC_OVERRIDE + preparation for lost GL context handling + stubs for GL context event handlers
Benjamin Golinvaux <bgo@osimis.io>
parents: 910
diff changeset
197 h > 0)
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
198 {
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
199 canvasWidth = static_cast<unsigned int>(boost::math::iround(w));
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
200 canvasHeight = static_cast<unsigned int>(boost::math::iround(h));
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
201 }
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
202
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
203 emscripten_set_canvas_element_size(canvas_.c_str(), canvasWidth, canvasHeight);
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
204 compositor_.UpdateSize(canvasWidth, canvasHeight);
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
205 }
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
206
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
207 void WebAssemblyCairoViewport::Refresh()
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
208 {
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
209 LOG(INFO) << "refreshing cairo viewport, TODO: blit to the canvans.getContext('2d')";
947
1091b2adeb5a Fixed animation frame stopping when returning false + big work on the OpenGL
Benjamin Golinvaux <bgo@osimis.io>
parents: 942
diff changeset
210 GetCompositor()->Refresh();
910
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
211 }
a6c12fe88bcb wip: WebAssemblyCairoViewport: still need to implement blit to canvas
Alain Mazy <alain@mazy.be>
parents: 907
diff changeset
212
891
0aff28f15ea2 new abstraction: IViewport
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 }