Mercurial > hg > orthanc-stone
comparison Framework/Scene2D/OpenGLCompositor.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 | 32eaf4929b08 |
comparison
equal
deleted
inserted
replaced
946:dbe3e1e47019 | 947:1091b2adeb5a |
---|---|
35 private: | 35 private: |
36 std::auto_ptr<GlyphTextureAlphabet> alphabet_; | 36 std::auto_ptr<GlyphTextureAlphabet> alphabet_; |
37 std::auto_ptr<OpenGL::OpenGLTexture> texture_; | 37 std::auto_ptr<OpenGL::OpenGLTexture> texture_; |
38 | 38 |
39 public: | 39 public: |
40 Font(const GlyphBitmapAlphabet& dict) | 40 Font(OpenGL::IOpenGLContext& context, const GlyphBitmapAlphabet& dict) |
41 { | 41 { |
42 alphabet_.reset(new GlyphTextureAlphabet(dict)); | 42 alphabet_.reset(new GlyphTextureAlphabet(dict)); |
43 texture_.reset(new OpenGL::OpenGLTexture); | 43 texture_.reset(new OpenGL::OpenGLTexture(context)); |
44 | 44 |
45 std::auto_ptr<Orthanc::ImageAccessor> bitmap(alphabet_->ReleaseTexture()); | 45 std::auto_ptr<Orthanc::ImageAccessor> bitmap(alphabet_->ReleaseTexture()); |
46 texture_->Load(*bitmap, true /* enable linear interpolation */); | 46 texture_->Load(*bitmap, true /* enable linear interpolation */); |
47 } | 47 } |
48 | 48 |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 Internals::CompositorHelper::ILayerRenderer* OpenGLCompositor::Create(const ISceneLayer& layer) | 77 Internals::CompositorHelper::ILayerRenderer* OpenGLCompositor::Create(const ISceneLayer& layer) |
78 { | 78 { |
79 switch (layer.GetType()) | 79 if (!context_.IsContextLost()) |
80 { | 80 { |
81 switch (layer.GetType()) | |
82 { | |
81 case ISceneLayer::Type_InfoPanel: | 83 case ISceneLayer::Type_InfoPanel: |
82 return new Internals::OpenGLInfoPanelRenderer | 84 return new Internals::OpenGLInfoPanelRenderer |
83 (context_, colorTextureProgram_, dynamic_cast<const InfoPanelSceneLayer&>(layer)); | 85 (context_, colorTextureProgram_, dynamic_cast<const InfoPanelSceneLayer&>(layer)); |
84 | 86 |
85 case ISceneLayer::Type_ColorTexture: | 87 case ISceneLayer::Type_ColorTexture: |
86 return new Internals::OpenGLColorTextureRenderer | 88 return new Internals::OpenGLColorTextureRenderer |
87 (context_, colorTextureProgram_, dynamic_cast<const ColorTextureSceneLayer&>(layer)); | 89 (context_, colorTextureProgram_, dynamic_cast<const ColorTextureSceneLayer&>(layer)); |
88 | 90 |
89 case ISceneLayer::Type_FloatTexture: | 91 case ISceneLayer::Type_FloatTexture: |
90 return new Internals::OpenGLFloatTextureRenderer | 92 return new Internals::OpenGLFloatTextureRenderer |
91 (context_, floatTextureProgram_, dynamic_cast<const FloatTextureSceneLayer&>(layer)); | 93 (context_, floatTextureProgram_, dynamic_cast<const FloatTextureSceneLayer&>(layer)); |
92 | 94 |
93 case ISceneLayer::Type_LookupTableTexture: | 95 case ISceneLayer::Type_LookupTableTexture: |
94 return new Internals::OpenGLLookupTableTextureRenderer | 96 return new Internals::OpenGLLookupTableTextureRenderer |
95 (context_, colorTextureProgram_, dynamic_cast<const LookupTableTextureSceneLayer&>(layer)); | 97 (context_, colorTextureProgram_, dynamic_cast<const LookupTableTextureSceneLayer&>(layer)); |
96 | 98 |
97 case ISceneLayer::Type_Polyline: | 99 case ISceneLayer::Type_Polyline: |
98 return new Internals::OpenGLAdvancedPolylineRenderer | 100 return new Internals::OpenGLAdvancedPolylineRenderer |
99 (context_, linesProgram_, dynamic_cast<const PolylineSceneLayer&>(layer)); | 101 (context_, linesProgram_, dynamic_cast<const PolylineSceneLayer&>(layer)); |
100 //return new Internals::OpenGLBasicPolylineRenderer(context_, dynamic_cast<const PolylineSceneLayer&>(layer)); | 102 //return new Internals::OpenGLBasicPolylineRenderer(context_, dynamic_cast<const PolylineSceneLayer&>(layer)); |
101 | 103 |
102 case ISceneLayer::Type_Text: | 104 case ISceneLayer::Type_Text: |
103 { | 105 { |
104 const TextSceneLayer& l = dynamic_cast<const TextSceneLayer&>(layer); | 106 const TextSceneLayer& l = dynamic_cast<const TextSceneLayer&>(layer); |
108 return NULL; | 110 return NULL; |
109 } | 111 } |
110 else | 112 else |
111 { | 113 { |
112 return new Internals::OpenGLTextRenderer | 114 return new Internals::OpenGLTextRenderer |
113 (context_, textProgram_, font->GetAlphabet(), font->GetTexture(), l); | 115 (context_, textProgram_, font->GetAlphabet(), font->GetTexture(), l); |
114 } | 116 } |
115 } | 117 } |
116 | 118 |
117 default: | 119 default: |
118 return NULL; | 120 return NULL; |
121 } | |
122 } | |
123 else | |
124 { | |
125 // context is lost. returning null. | |
126 return NULL; | |
119 } | 127 } |
120 } | 128 } |
121 | 129 |
122 OpenGLCompositor::OpenGLCompositor(OpenGL::IOpenGLContext& context, | 130 OpenGLCompositor::OpenGLCompositor(OpenGL::IOpenGLContext& context, |
123 const Scene2D& scene) : | 131 const Scene2D& scene) : |
132 { | 140 { |
133 } | 141 } |
134 | 142 |
135 OpenGLCompositor::~OpenGLCompositor() | 143 OpenGLCompositor::~OpenGLCompositor() |
136 { | 144 { |
137 for (Fonts::iterator it = fonts_.begin(); it != fonts_.end(); ++it) | 145 if (!context_.IsContextLost()) |
138 { | 146 { |
139 assert(it->second != NULL); | 147 context_.MakeCurrent(); // this can throw if context lost! |
140 delete it->second; | 148 for (Fonts::iterator it = fonts_.begin(); it != fonts_.end(); ++it) |
149 { | |
150 assert(it->second != NULL); | |
151 delete it->second; | |
152 } | |
141 } | 153 } |
142 } | 154 } |
143 | 155 |
144 void OpenGLCompositor::Refresh() | 156 void OpenGLCompositor::Refresh() |
145 { | 157 { |
146 context_.MakeCurrent(); | 158 if (!context_.IsContextLost()) |
147 | 159 { |
148 canvasWidth_ = context_.GetCanvasWidth(); | 160 context_.MakeCurrent(); // this can throw if context lost! |
149 canvasHeight_ = context_.GetCanvasHeight(); | 161 |
150 | 162 canvasWidth_ = context_.GetCanvasWidth(); |
151 glViewport(0, 0, canvasWidth_, canvasHeight_); | 163 canvasHeight_ = context_.GetCanvasHeight(); |
152 glClearColor(0, 0, 0, 1); | 164 |
153 glClear(GL_COLOR_BUFFER_BIT); | 165 glViewport(0, 0, canvasWidth_, canvasHeight_); |
154 | 166 glClearColor(0, 0, 0, 1); |
155 helper_.Refresh(canvasWidth_, canvasHeight_); | 167 glClear(GL_COLOR_BUFFER_BIT); |
156 | 168 |
157 context_.SwapBuffer(); | 169 helper_.Refresh(canvasWidth_, canvasHeight_); |
170 | |
171 context_.SwapBuffer(); | |
172 } | |
173 | |
158 } | 174 } |
159 | 175 |
160 void OpenGLCompositor::SetFont(size_t index, | 176 void OpenGLCompositor::SetFont(size_t index, |
161 const GlyphBitmapAlphabet& dict) | 177 const GlyphBitmapAlphabet& dict) |
162 { | 178 { |
163 context_.MakeCurrent(); | 179 if (!context_.IsContextLost()) |
164 | 180 { |
165 std::auto_ptr<Font> font(new Font(dict)); | 181 context_.MakeCurrent(); // this can throw if context lost |
166 | 182 |
167 Fonts::iterator found = fonts_.find(index); | 183 std::auto_ptr<Font> font(new Font(context_, dict)); |
168 | 184 |
169 if (found == fonts_.end()) | 185 Fonts::iterator found = fonts_.find(index); |
170 { | 186 |
171 fonts_[index] = font.release(); | 187 if (found == fonts_.end()) |
172 } | 188 { |
173 else | 189 fonts_[index] = font.release(); |
174 { | 190 } |
175 assert(found->second != NULL); | 191 else |
176 delete found->second; | 192 { |
177 | 193 assert(found->second != NULL); |
178 found->second = font.release(); | 194 delete found->second; |
195 | |
196 found->second = font.release(); | |
197 } | |
179 } | 198 } |
180 } | 199 } |
181 | 200 |
182 #if ORTHANC_ENABLE_LOCALE == 1 | 201 #if ORTHANC_ENABLE_LOCALE == 1 |
183 void OpenGLCompositor::SetFont(size_t index, | 202 void OpenGLCompositor::SetFont(size_t index, |
184 Orthanc::EmbeddedResources::FileResourceId resource, | 203 Orthanc::EmbeddedResources::FileResourceId resource, |
185 unsigned int fontSize, | 204 unsigned int fontSize, |
186 Orthanc::Encoding codepage) | 205 Orthanc::Encoding codepage) |
187 { | 206 { |
188 FontRenderer renderer; | 207 if (!context_.IsContextLost()) |
189 renderer.LoadFont(resource, fontSize); | 208 { |
190 | 209 FontRenderer renderer; |
191 GlyphBitmapAlphabet dict; | 210 renderer.LoadFont(resource, fontSize); |
192 dict.LoadCodepage(renderer, codepage); | 211 |
193 | 212 GlyphBitmapAlphabet dict; |
194 SetFont(index, dict); | 213 dict.LoadCodepage(renderer, codepage); |
214 | |
215 SetFont(index, dict); | |
216 } | |
195 } | 217 } |
196 #endif | 218 #endif |
197 } | 219 } |