2 * Copyright (c) 2020 - 2022 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++];
47 uint32_t count() override
61 uint8_t opacity; //for composition
62 RenderMethod* renderer = nullptr; //keep it for explicit clear
66 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
71 bool dispose(RenderMethod& renderer)
73 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
74 (*paint)->pImpl->dispose(renderer);
77 this->renderer = nullptr;
82 bool needComposition(uint32_t opacity)
84 //Half translucent requires intermediate composition.
85 if (opacity == 255 || opacity == 0) return false;
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;
93 void* update(RenderMethod &renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flag)
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;
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));
104 /* FXIME: it requires to return list of children engine data
105 This is necessary for scene composition */
107 this->renderer = &renderer;
112 bool render(RenderMethod& renderer)
114 Compositor* cmp = nullptr;
116 if (needComposition(opacity)) {
117 cmp = renderer.target(bounds(renderer));
118 renderer.beginComposite(cmp, CompositeMethod::None, opacity);
121 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
122 if (!(*paint)->pImpl->render(renderer)) return false;
125 if (cmp) renderer.endComposite(cmp);
130 RenderRegion bounds(RenderMethod& renderer) const
132 if (paints.count == 0) return {0, 0, 0, 0};
134 int32_t x1 = INT32_MAX;
135 int32_t y1 = INT32_MAX;
139 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
140 auto region = (*paint)->pImpl->bounds(renderer);
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);
149 return {x1, y1, (x2 - x1), (y2 - y1)};
152 bool bounds(float* px, float* py, float* pw, float* ph)
154 if (paints.count == 0) return false;
161 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
167 if ((*paint)->bounds(&x, &y, &w, &h, true) != tvg::Result::Success) continue;
171 if (x2 < x + w) x2 = (x + w);
173 if (y2 < y + h) y2 = (y + h);
178 if (pw) *pw = (x2 - x1);
179 if (ph) *ph = (y2 - y1);
186 auto ret = Scene::gen();
188 auto dup = ret.get()->pImpl;
190 dup->paints.reserve(paints.count);
192 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
193 dup->paints.push((*paint)->duplicate());
196 return ret.release();
199 void clear(bool free)
201 auto dispose = renderer ? true : false;
203 for (auto paint = paints.data; paint < (paints.data + paints.count); ++paint) {
204 if (dispose) (*paint)->pImpl->dispose(*renderer);
205 if (free) delete(*paint);
213 return new SceneIterator(&paints);
217 #endif //_TVG_SCENE_IMPL_H_