2 * Copyright (c) 2020 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
27 #include "tvgRender.h"
34 virtual ~StrategyMethod() {}
36 virtual bool dispose(RenderMethod& renderer) = 0;
37 virtual void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag pFlag) = 0; //Return engine data if it has.
38 virtual bool render(RenderMethod& renderer) = 0;
39 virtual bool bounds(float* x, float* y, float* w, float* h) const = 0;
40 virtual bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const = 0;
41 virtual Paint* duplicate() = 0;
46 StrategyMethod* smethod = nullptr;
47 RenderTransform *rTransform = nullptr;
48 uint32_t flag = RenderUpdateFlag::None;
50 Paint* cmpTarget = nullptr;
51 CompositeMethod cmpMethod = CompositeMethod::None;
53 uint8_t opacity = 255;
56 if (cmpTarget) delete(cmpTarget);
57 if (smethod) delete(smethod);
58 if (rTransform) delete(rTransform);
61 void method(StrategyMethod* method)
66 bool rotate(float degree)
69 if (fabsf(degree - rTransform->degree) <= FLT_EPSILON) return true;
71 if (fabsf(degree) <= FLT_EPSILON) return true;
72 rTransform = new RenderTransform();
73 if (!rTransform) return false;
75 rTransform->degree = degree;
76 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
81 bool scale(float factor)
84 if (fabsf(factor - rTransform->scale) <= FLT_EPSILON) return true;
86 if (fabsf(factor) <= FLT_EPSILON) return true;
87 rTransform = new RenderTransform();
88 if (!rTransform) return false;
90 rTransform->scale = factor;
91 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
96 bool translate(float x, float y)
99 if (fabsf(x - rTransform->x) <= FLT_EPSILON && fabsf(y - rTransform->y) <= FLT_EPSILON) return true;
101 if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON) return true;
102 rTransform = new RenderTransform();
103 if (!rTransform) return false;
107 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
112 bool transform(const Matrix& m)
115 rTransform = new RenderTransform();
116 if (!rTransform) return false;
118 rTransform->override(m);
119 flag |= RenderUpdateFlag::Transform;
124 bool bounds(float* x, float* y, float* w, float* h) const
126 return smethod->bounds(x, y, w, h);
129 bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const
131 return smethod->bounds(renderer, x, y, w, h);
134 bool dispose(RenderMethod& renderer)
136 if (cmpTarget) cmpTarget->pImpl->dispose(renderer);
137 return smethod->dispose(renderer);
140 void* update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, uint32_t pFlag)
142 if (flag & RenderUpdateFlag::Transform) {
143 if (!rTransform) return nullptr;
144 if (!rTransform->update()) {
146 rTransform = nullptr;
150 void *cmpData = nullptr;
153 cmpData = cmpTarget->pImpl->update(renderer, pTransform, opacity, clips, pFlag);
154 if (cmpMethod == CompositeMethod::ClipPath) clips.push(cmpData);
157 void *edata = nullptr;
158 auto newFlag = static_cast<RenderUpdateFlag>(pFlag | flag);
159 flag = RenderUpdateFlag::None;
160 opacity = (opacity * this->opacity) / 255;
162 if (rTransform && pTransform) {
163 RenderTransform outTransform(pTransform, rTransform);
164 edata = smethod->update(renderer, &outTransform, opacity, clips, newFlag);
166 auto outTransform = pTransform ? pTransform : rTransform;
167 edata = smethod->update(renderer, outTransform, opacity, clips, newFlag);
170 if (cmpData) clips.pop();
175 bool render(RenderMethod& renderer)
177 Compositor* cmp = nullptr;
179 /* Note: only ClipPath is processed in update() step */
180 if (cmpTarget && cmpMethod != CompositeMethod::ClipPath) {
182 if (!cmpTarget->pImpl->bounds(renderer, &x, &y, &w, &h)) return false;
183 cmp = renderer.addCompositor(x, y, w, h);
184 cmp->method = CompositeMethod::None;
186 cmpTarget->pImpl->render(renderer);
189 auto ret = smethod->render(renderer);
191 renderer.delCompositor(cmp);
198 auto ret = smethod->duplicate();
200 //duplicate Transform
201 if (ret && rTransform) {
202 ret->pImpl->rTransform = new RenderTransform();
203 if (ret->pImpl->rTransform) {
204 *ret->pImpl->rTransform = *rTransform;
205 ret->pImpl->flag |= RenderUpdateFlag::Transform;
209 ret->pImpl->opacity = opacity;
214 bool composite(Paint* target, CompositeMethod method)
216 if (!target && method != CompositeMethod::None) return false;
217 if (target && method == CompositeMethod::None) return false;
226 struct PaintMethod : StrategyMethod
230 PaintMethod(T* _inst) : inst(_inst) {}
233 bool bounds(float* x, float* y, float* w, float* h) const override
235 return inst->bounds(x, y, w, h);
238 bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const override
240 return inst->bounds(renderer, x, y, w, h);
243 bool dispose(RenderMethod& renderer) override
245 return inst->dispose(renderer);
248 void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<RenderData>& clips, RenderUpdateFlag flag) override
250 return inst->update(renderer, transform, opacity, clips, flag);
253 bool render(RenderMethod& renderer) override
255 return inst->render(renderer);
258 Paint* duplicate() override
260 return inst->duplicate();
265 #endif //_TVG_PAINT_H_