2 * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
22 #ifndef _TVG_SCENE_IMPL_H_
23 #define _TVG_SCENE_IMPL_H_
28 /************************************************************************/
29 /* Internal Class Implementation */
30 /************************************************************************/
32 struct SceneIterator : Iterator
34 Array<Paint*>* paints;
37 SceneIterator(Array<Paint*>* p) : paints(p)
41 const Paint* next() override
43 if (idx >= paints->count) return nullptr;
44 return paints->data[idx++];
51 uint8_t opacity; //for composition
52 RenderMethod* renderer = nullptr; //keep it for explicit clear
56 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
61 bool dispose(RenderMethod& renderer)
63 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
64 (*paint)->pImpl->dispose(renderer);
67 this->renderer = nullptr;
72 bool needComposition(uint32_t opacity)
74 //Half translucent requires intermediate composition.
75 if (opacity == 255 || opacity == 0) return false;
77 //If scene has several children or only scene, it may require composition.
78 if (paints.count > 1) return true;
79 if (paints.count == 1 && (*paints.data)->id() == TVG_CLASS_ID_SCENE) return true;
83 void* update(RenderMethod &renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flag)
85 /* Overriding opacity value. If this scene is half-translucent,
86 It must do intermeidate composition with that opacity value. */
87 this->opacity = static_cast<uint8_t>(opacity);
88 if (needComposition(opacity)) opacity = 255;
90 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
91 (*paint)->pImpl->update(renderer, transform, opacity, clips, static_cast<uint32_t>(flag));
94 /* FXIME: it requires to return list of children engine data
95 This is necessary for scene composition */
97 this->renderer = &renderer;
102 bool render(RenderMethod& renderer)
104 Compositor* cmp = nullptr;
106 if (needComposition(opacity)) {
107 cmp = renderer.target(bounds(renderer));
108 renderer.beginComposite(cmp, CompositeMethod::None, opacity);
111 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
112 if (!(*paint)->pImpl->render(renderer)) return false;
115 if (cmp) renderer.endComposite(cmp);
120 RenderRegion bounds(RenderMethod& renderer) const
122 if (paints.count == 0) return {0, 0, 0, 0};
124 uint32_t x1 = UINT32_MAX;
125 uint32_t y1 = UINT32_MAX;
129 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
130 auto region = (*paint)->pImpl->bounds(renderer);
133 if (region.x < x1) x1 = region.x;
134 if (x2 < region.x + region.w) x2 = (region.x + region.w);
135 if (region.y < y1) y1 = region.y;
136 if (y2 < region.y + region.h) y2 = (region.y + region.h);
139 return {x1, y1, (x2 - x1), (y2 - y1)};
142 bool bounds(float* px, float* py, float* pw, float* ph) const
144 if (paints.count == 0) return false;
151 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
157 if (!(*paint)->pImpl->bounds(&x, &y, &w, &h)) continue;
161 if (x2 < x + w) x2 = (x + w);
163 if (y2 < y + h) y2 = (y + h);
168 if (pw) *pw = (x2 - x1);
169 if (ph) *ph = (y2 - y1);
176 auto ret = Scene::gen();
177 if (!ret) return nullptr;
178 auto dup = ret.get()->pImpl;
180 dup->paints.reserve(paints.count);
182 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
183 dup->paints.push((*paint)->duplicate());
186 return ret.release();
189 void clear(bool free)
191 auto dispose = renderer ? true : false;
193 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
194 if (dispose) (*paint)->pImpl->dispose(*renderer);
195 if (free) delete(*paint);
203 return new SceneIterator(&paints);
207 #endif //_TVG_SCENE_IMPL_H_