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<Composite>& compList, 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* compTarget = nullptr;
51 CompositeMethod compMethod = CompositeMethod::None;
53 uint8_t opacity = 255;
56 if (compTarget) delete(compTarget);
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 (compTarget) compTarget->pImpl->dispose(renderer);
137 return smethod->dispose(renderer);
140 void* update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t opacity, Array<Composite>& compList, uint32_t pFlag)
142 if (flag & RenderUpdateFlag::Transform) {
143 if (!rTransform) return nullptr;
144 if (!rTransform->update()) {
146 rTransform = nullptr;
150 void *compdata = nullptr;
152 if (compTarget && compMethod == CompositeMethod::ClipPath) {
153 compdata = compTarget->pImpl->update(renderer, pTransform, opacity, compList, pFlag);
154 if (compdata) compList.push({compdata, compMethod});
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, compList, newFlag);
166 auto outTransform = pTransform ? pTransform : rTransform;
167 edata = smethod->update(renderer, outTransform, opacity, compList, newFlag);
170 if (compdata) compList.pop();
175 bool render(RenderMethod& renderer)
177 return smethod->render(renderer);
182 auto ret = smethod->duplicate();
184 //duplicate Transform
185 if (ret && rTransform) {
186 ret->pImpl->rTransform = new RenderTransform();
187 if (ret->pImpl->rTransform) {
188 *ret->pImpl->rTransform = *rTransform;
189 ret->pImpl->flag |= RenderUpdateFlag::Transform;
193 ret->pImpl->opacity = opacity;
198 bool composite(Paint* target, CompositeMethod method)
200 if (!target && method != CompositeMethod::None) return false;
209 struct PaintMethod : StrategyMethod
213 PaintMethod(T* _inst) : inst(_inst) {}
216 bool bounds(float* x, float* y, float* w, float* h) const override
218 return inst->bounds(x, y, w, h);
221 bool bounds(RenderMethod& renderer, uint32_t* x, uint32_t* y, uint32_t* w, uint32_t* h) const override
223 return inst->bounds(renderer, x, y, w, h);
226 bool dispose(RenderMethod& renderer) override
228 return inst->dispose(renderer);
231 void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<Composite>& compList, RenderUpdateFlag flag) override
233 return inst->update(renderer, transform, opacity, compList, flag);
236 bool render(RenderMethod& renderer) override
238 return inst->render(renderer);
241 Paint* duplicate() override
243 return inst->duplicate();
248 #endif //_TVG_PAINT_H_