comparison Framework/Scene2D/Internals/CompositorHelper.cpp @ 593:6bf8f881fcb5

OpenGLBasicPolylineRenderer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Apr 2019 13:04:56 +0200
parents Framework/Scene2D/CompositorHelper.cpp@b9ce24c606ae
children 03c4b998fcd0
comparison
equal deleted inserted replaced
592:bbe29efd3d1c 593:6bf8f881fcb5
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "CompositorHelper.h"
23
24 #include <Core/OrthancException.h>
25
26 namespace OrthancStone
27 {
28 namespace Internals
29 {
30 class CompositorHelper::Item : public boost::noncopyable
31 {
32 private:
33 std::auto_ptr<ILayerRenderer> renderer_;
34 const ISceneLayer& layer_;
35 uint64_t lastRevision_;
36
37 public:
38 Item(ILayerRenderer* renderer, // Takes ownership
39 const ISceneLayer& layer) :
40 renderer_(renderer),
41 layer_(layer),
42 lastRevision_(layer.GetRevision())
43 {
44 if (renderer == NULL)
45 {
46 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
47 }
48 }
49
50 ILayerRenderer& GetRenderer() const
51 {
52 assert(renderer_.get() != NULL);
53 return *renderer_;
54 }
55
56 const ISceneLayer& GetLayer() const
57 {
58 return layer_;
59 }
60
61 uint64_t GetLastRevision() const
62 {
63 return lastRevision_;
64 }
65
66 void UpdateRenderer()
67 {
68 assert(renderer_.get() != NULL);
69 renderer_->Update(layer_);
70 lastRevision_ = layer_.GetRevision();
71 }
72 };
73
74
75 void CompositorHelper::Visit(const ISceneLayer& layer,
76 int depth)
77 {
78 Content::iterator found = content_.find(depth);
79
80 assert(found == content_.end() ||
81 found->second != NULL);
82
83 if (found == content_.end() ||
84 &found->second->GetLayer() != &layer)
85 {
86 // This is the first time this layer is rendered, or the layer
87 // is not the same as before
88 if (found != content_.end())
89 {
90 delete found->second;
91 content_.erase(found);
92 }
93
94 std::auto_ptr<ILayerRenderer> renderer(factory_.Create(layer));
95
96 if (renderer.get() != NULL)
97 {
98 renderer->Render(sceneTransform_);
99 content_[depth] = new Item(renderer.release(), layer);
100 }
101 }
102 else
103 {
104 // This layer has already been rendered
105 if (found->second->GetLastRevision() < layer.GetRevision())
106 {
107 found->second->UpdateRenderer();
108 }
109
110 found->second->GetRenderer().Render(sceneTransform_);
111 }
112
113 // Check invariants
114 assert(content_.find(depth) == content_.end() ||
115 (&content_[depth]->GetLayer() == &layer &&
116 content_[depth]->GetLastRevision() == layer.GetRevision()));
117 }
118
119
120 CompositorHelper::~CompositorHelper()
121 {
122 for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
123 {
124 assert(it->second != NULL);
125 delete it->second;
126 }
127 }
128
129
130 void CompositorHelper::Refresh(unsigned int canvasWidth,
131 unsigned int canvasHeight)
132 {
133 // Bring coordinate (0,0) to the center of the canvas
134 AffineTransform2D offset = AffineTransform2D::CreateOffset(
135 static_cast<double>(canvasWidth) / 2.0,
136 static_cast<double>(canvasHeight) / 2.0);
137
138 sceneTransform_ = AffineTransform2D::Combine(offset, scene_.GetSceneToCanvasTransform());
139 scene_.Apply(*this);
140 }
141 }
142 }