example: just renamed the svg file.
[platform/core/graphics/tizenvg.git] / src / lib / tvgSceneImpl.h
1 /*
2  * Copyright (c) 2020 - 2022 Samsung Electronics Co., Ltd. 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 #ifndef _TVG_SCENE_IMPL_H_
23 #define _TVG_SCENE_IMPL_H_
24
25 #include <float.h>
26 #include "tvgPaint.h"
27
28 /************************************************************************/
29 /* Internal Class Implementation                                        */
30 /************************************************************************/
31
32 struct SceneIterator : Iterator
33 {
34     Array<Paint*>* paints;
35     uint32_t idx = 0;
36
37     SceneIterator(Array<Paint*>* p) : paints(p)
38     {
39     }
40
41     const Paint* next() override
42     {
43         if (idx >= paints->count) return nullptr;
44         return paints->data[idx++];
45     }
46
47     uint32_t count() override
48     {
49         return paints->count;
50     }
51
52     void begin() override
53     {
54         idx = 0;
55     }
56 };
57
58 struct Scene::Impl
59 {
60     Array<Paint*> paints;
61     uint8_t opacity;                     //for composition
62     RenderMethod* renderer = nullptr;    //keep it for explicit clear
63
64     ~Impl()
65     {
66         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
67             delete(*paint);
68         }
69     }
70
71     bool dispose(RenderMethod& renderer)
72     {
73         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
74             (*paint)->pImpl->dispose(renderer);
75         }
76
77         this->renderer = nullptr;
78
79         return true;
80     }
81
82     bool needComposition(uint32_t opacity)
83     {
84         //Half translucent requires intermediate composition.
85         if (opacity == 255 || opacity == 0) return false;
86
87         //If scene has several children or only scene, it may require composition.
88         if (paints.count > 1) return true;
89         if (paints.count == 1 && (*paints.data)->identifier() == TVG_CLASS_ID_SCENE) return true;
90         return false;
91     }
92
93     void* update(RenderMethod &renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flag)
94     {
95         /* Overriding opacity value. If this scene is half-translucent,
96            It must do intermeidate composition with that opacity value. */
97         this->opacity = static_cast<uint8_t>(opacity);
98         if (needComposition(opacity)) opacity = 255;
99
100         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
101             (*paint)->pImpl->update(renderer, transform, opacity, clips, static_cast<uint32_t>(flag));
102         }
103
104         /* FXIME: it requires to return list of children engine data
105            This is necessary for scene composition */
106
107         this->renderer = &renderer;
108
109         return nullptr;
110     }
111
112     bool render(RenderMethod& renderer)
113     {
114         Compositor* cmp = nullptr;
115
116         if (needComposition(opacity)) {
117             cmp = renderer.target(bounds(renderer));
118             renderer.beginComposite(cmp, CompositeMethod::None, opacity);
119         }
120
121         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
122             if (!(*paint)->pImpl->render(renderer)) return false;
123         }
124
125         if (cmp) renderer.endComposite(cmp);
126
127         return true;
128     }
129
130     RenderRegion bounds(RenderMethod& renderer) const
131     {
132         if (paints.count == 0) return {0, 0, 0, 0};
133
134         int32_t x1 = INT32_MAX;
135         int32_t y1 = INT32_MAX;
136         int32_t x2 = 0;
137         int32_t y2 = 0;
138
139         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
140             auto region = (*paint)->pImpl->bounds(renderer);
141
142             //Merge regions
143             if (region.x < x1) x1 = region.x;
144             if (x2 < region.x + region.w) x2 = (region.x + region.w);
145             if (region.y < y1) y1 = region.y;
146             if (y2 < region.y + region.h) y2 = (region.y + region.h);
147         }
148
149         return {x1, y1, (x2 - x1), (y2 - y1)};
150     }
151
152     bool bounds(float* px, float* py, float* pw, float* ph)
153     {
154         if (paints.count == 0) return false;
155
156         auto x1 = FLT_MAX;
157         auto y1 = FLT_MAX;
158         auto x2 = -FLT_MAX;
159         auto y2 = -FLT_MAX;
160
161         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
162             auto x = FLT_MAX;
163             auto y = FLT_MAX;
164             auto w = 0.0f;
165             auto h = 0.0f;
166
167             if ((*paint)->bounds(&x, &y, &w, &h, true) != tvg::Result::Success) continue;
168
169             //Merge regions
170             if (x < x1) x1 = x;
171             if (x2 < x + w) x2 = (x + w);
172             if (y < y1) y1 = y;
173             if (y2 < y + h) y2 = (y + h);
174         }
175
176         if (px) *px = x1;
177         if (py) *py = y1;
178         if (pw) *pw = (x2 - x1);
179         if (ph) *ph = (y2 - y1);
180
181         return true;
182     }
183
184     Paint* duplicate()
185     {
186         auto ret = Scene::gen();
187
188         auto dup = ret.get()->pImpl;
189
190         dup->paints.reserve(paints.count);
191
192         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
193             dup->paints.push((*paint)->duplicate());
194         }
195
196         return ret.release();
197     }
198
199     void clear(bool free)
200     {
201         auto dispose = renderer ? true : false;
202
203         for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
204             if (dispose) (*paint)->pImpl->dispose(*renderer);
205             if (free) delete(*paint);
206         }
207         paints.clear();
208         renderer = nullptr;
209     }
210
211     Iterator* iterator()
212     {
213         return new SceneIterator(&paints);
214     }
215 };
216
217 #endif //_TVG_SCENE_IMPL_H_