Debian Codegraph @

thirdparty/thorvg/src/renderer/tvgScene.h

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 2024 the ThorVG project. All rights reserved.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #ifndef _TVG_SCENE_H_
24 #define _TVG_SCENE_H_
25
26 #include "tvgMath.h"
27 #include "tvgPaint.h"
28
29 struct SceneIterator : Iterator
30 {
31 list<Paint*>* paints;
32 list<Paint*>::iterator itr;
33
34 SceneIterator(list<Paint*>* p) : paints(p)
35 {
36 begin();
37 }
38
39 const Paint* next() override
40 {
41 if (itr == paints->end()) return nullptr;
42 auto paint = *itr;
43 ++itr;
44 return paint;
45 }
46
47 uint32_t count() override
48 {
49 return paints->size();
50 }
51
52 void begin() override
53 {
54 itr = paints->begin();
55 }
56 };
57
58 struct Scene::Impl
59 {
60 list<Paint*> paints;
61 RenderData rd = nullptr;
62 Scene* scene = nullptr;
63 RenderRegion vport = {0, 0, INT32_MAX, INT32_MAX};
64 Array<RenderEffect*>* effects = nullptr;
65 uint8_t compFlag = CompositionFlag::Invalid;
66 uint8_t opacity; //for composition
67
68 Impl(Scene* s) : scene(s)
69 {
70 }
71
72 ~Impl()
73 {
74 resetEffects();
75
76 for (auto paint : paints) {
77 if (P(paint)->unref() == 0) delete(paint);
78 }
79
80 if (auto renderer = PP(scene)->renderer) {
81 renderer->dispose(rd);
82 }
83 }
84
85 uint8_t needComposition(uint8_t opacity)
86 {
87 compFlag = CompositionFlag::Invalid;
88
89 if (opacity == 0 || paints.empty()) return 0;
90
91 //post effects, masking, blending may require composition
92 if (effects) compFlag |= CompositionFlag::PostProcessing;
93 auto compMethod = scene->composite(nullptr);
94 if (compMethod != CompositeMethod::None && compMethod != CompositeMethod::ClipPath) compFlag |= CompositionFlag::Masking;
95 if (PP(scene)->blendMethod != BlendMethod::Normal) compFlag |= CompositionFlag::Blending;
96
97 //Half translucent requires intermediate composition.
98 if (opacity == 255) return compFlag;
99
100 //Only shape or picture may not require composition.
101 if (paints.size() == 1) {
102 auto type = paints.front()->type();
103 if (type == Type::Shape || type == Type::Picture) return compFlag;
104 }
105
106 compFlag |= CompositionFlag::Opacity;
107
108 return 1;
109 }
110
111 RenderData update(RenderMethod* renderer, const Matrix& transform, Array<RenderData>& clips, uint8_t opacity, RenderUpdateFlag flag, TVG_UNUSED bool clipper)
112 {
113 this->vport = renderer->viewport();
114
115 if (needComposition(opacity)) {
116 /* Overriding opacity value. If this scene is half-translucent,
117 It must do intermediate composition with that opacity value. */
118 this->opacity = opacity;
119 opacity = 255;
120 }
121 for (auto paint : paints) {
122 paint->pImpl->update(renderer, transform, clips, opacity, flag, false);
123 }
124
125 if (effects) {
126 for (auto e = effects->begin(); e < effects->end(); ++e) {
127 renderer->prepare(*e, transform);
128 }
129 }
130
131 return nullptr;
132 }
133
134 bool render(RenderMethod* renderer)
135 {
136 RenderCompositor* cmp = nullptr;
137 auto ret = true;
138
139 renderer->blend(PP(scene)->blendMethod);
140
141 if (compFlag) {
142 cmp = renderer->target(bounds(renderer), renderer->colorSpace(), static_cast<CompositionFlag>(compFlag));
143 renderer->beginComposite(cmp, CompositeMethod::None, opacity);
144 }
145
146 for (auto paint : paints) {
147 ret &= paint->pImpl->render(renderer);
148 }
149
150 if (cmp) {
151 //Apply post effects if any.
152 if (effects) {
153 //Notify the possiblity of the direct composition of the effect result to the origin surface.
154 auto direct = (effects->count == 1) & (compFlag == CompositionFlag::PostProcessing);
155 for (auto e = effects->begin(); e < effects->end(); ++e) {
156 if ((*e)->valid) renderer->render(cmp, *e, direct);
157 }
158 }
159 renderer->endComposite(cmp);
160 }
161
162 return ret;
163 }
164
165 RenderRegion bounds(RenderMethod* renderer) const
166 {
167 if (paints.empty()) return {0, 0, 0, 0};
168
169 int32_t x1 = INT32_MAX;
170 int32_t y1 = INT32_MAX;
171 int32_t x2 = 0;
172 int32_t y2 = 0;
173
174 for (auto paint : paints) {
175 auto region = paint->pImpl->bounds(renderer);
176
177 //Merge regions
178 if (region.x < x1) x1 = region.x;
179 if (x2 < region.x + region.w) x2 = (region.x + region.w);
180 if (region.y < y1) y1 = region.y;
181 if (y2 < region.y + region.h) y2 = (region.y + region.h);
182 }
183
184 //Extends the render region if post effects require
185 int32_t ex = 0, ey = 0, ew = 0, eh = 0;
186 if (effects) {
187 for (auto e = effects->begin(); e < effects->end(); ++e) {
188 auto effect = *e;
189 if (effect->valid && renderer->region(effect)) {
190 ex = std::min(ex, effect->extend.x);
191 ey = std::min(ey, effect->extend.y);
192 ew = std::max(ew, effect->extend.w);
193 eh = std::max(eh, effect->extend.h);
194 }
195 }
196 }
197
198 auto ret = RenderRegion{x1 + ex, y1 + ey, (x2 - x1) + ew, (y2 - y1) + eh};
199 ret.intersect(this->vport);
200 return ret;
201 }
202
203 bool bounds(float* px, float* py, float* pw, float* ph, bool stroking)
204 {
205 if (paints.empty()) return false;
206
207 auto x1 = FLT_MAX;
208 auto y1 = FLT_MAX;
209 auto x2 = -FLT_MAX;
210 auto y2 = -FLT_MAX;
211
212 for (auto paint : paints) {
213 auto x = FLT_MAX;
214 auto y = FLT_MAX;
215 auto w = 0.0f;
216 auto h = 0.0f;
217
218 if (!P(paint)->bounds(&x, &y, &w, &h, true, stroking)) continue;
219
220 //Merge regions
221 if (x < x1) x1 = x;
222 if (x2 < x + w) x2 = (x + w);
223 if (y < y1) y1 = y;
224 if (y2 < y + h) y2 = (y + h);
225 }
226
227 if (px) *px = x1;
228 if (py) *py = y1;
229 if (pw) *pw = (x2 - x1);
230 if (ph) *ph = (y2 - y1);
231
232 return true;
233 }
234
235 Paint* duplicate(Paint* ret)
236 {
237 if (ret) TVGERR("RENDERER", "TODO: duplicate()");
238
239 auto scene = Scene::gen().release();
240 auto dup = scene->pImpl;
241
242 for (auto paint : paints) {
243 auto cdup = paint->duplicate();
244 P(cdup)->ref();
245 dup->paints.push_back(cdup);
246 }
247
248 if (effects) {
249 dup->effects = new Array<RenderEffect*>;
250 for (auto p = effects->begin(); p < effects->end(); ++p) {
251 RenderEffect* ret = nullptr;
252 switch ((*p)->type) {
253 case SceneEffect::GaussianBlur: {
254 ret = new RenderEffectGaussianBlur(*(RenderEffectGaussianBlur*)(*p));
255 break;
256 }
257 case SceneEffect::DropShadow: {
258 ret = new RenderEffectDropShadow(*(RenderEffectDropShadow*)(*p));
259 break;
260 }
261 case SceneEffect::Fill: {
262 ret = new RenderEffectFill(*(RenderEffectFill*)(*p));
263 break;
264 }
265 case SceneEffect::Tint: {
266 ret = new RenderEffectTint(*(RenderEffectTint*)(*p));
267 break;
268 }
269 case SceneEffect::Tritone: {
270 ret = new RenderEffectTritone(*(RenderEffectTritone*)(*p));
271 break;
272 }
273 default: break;
274 }
275 if (ret) {
276 ret->rd = nullptr;
277 ret->valid = false;
278 dup->effects->push(ret);
279 }
280 }
281 }
282
283 return scene;
284 }
285
286 void clear(bool free)
287 {
288 for (auto paint : paints) {
289 if (P(paint)->unref() == 0 && free) delete(paint);
290 }
291 paints.clear();
292 }
293
294 Iterator* iterator()
295 {
296 return new SceneIterator(&paints);
297 }
298
299 Result resetEffects();
300 };
301
302 #endif //_TVG_SCENE_H_