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 Paint* duplicate() = 0;
45 StrategyMethod* smethod = nullptr;
46 RenderTransform *rTransform = nullptr;
47 uint32_t flag = RenderUpdateFlag::None;
49 Paint* compTarget = nullptr;
50 CompositeMethod compMethod = CompositeMethod::None;
52 uint8_t opacity = 255;
55 if (compTarget) delete(compTarget);
56 if (smethod) delete(smethod);
57 if (rTransform) delete(rTransform);
60 void method(StrategyMethod* method)
65 bool rotate(float degree)
68 if (fabsf(degree - rTransform->degree) <= FLT_EPSILON) return true;
70 if (fabsf(degree) <= FLT_EPSILON) return true;
71 rTransform = new RenderTransform();
72 if (!rTransform) return false;
74 rTransform->degree = degree;
75 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
80 bool scale(float factor)
83 if (fabsf(factor - rTransform->scale) <= FLT_EPSILON) return true;
85 if (fabsf(factor) <= FLT_EPSILON) return true;
86 rTransform = new RenderTransform();
87 if (!rTransform) return false;
89 rTransform->scale = factor;
90 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
95 bool translate(float x, float y)
98 if (fabsf(x - rTransform->x) <= FLT_EPSILON && fabsf(y - rTransform->y) <= FLT_EPSILON) return true;
100 if (fabsf(x) <= FLT_EPSILON && fabsf(y) <= FLT_EPSILON) return true;
101 rTransform = new RenderTransform();
102 if (!rTransform) return false;
106 if (!rTransform->overriding) flag |= RenderUpdateFlag::Transform;
111 bool transform(const Matrix& m)
114 rTransform = new RenderTransform();
115 if (!rTransform) return false;
117 rTransform->override(m);
118 flag |= RenderUpdateFlag::Transform;
123 bool bounds(float* x, float* y, float* w, float* h) const
125 return smethod->bounds(x, y, w, h);
128 bool dispose(RenderMethod& renderer)
130 if (compTarget) compTarget->pImpl->dispose(renderer);
131 return smethod->dispose(renderer);
134 void* update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t opacity, Array<Composite>& compList, uint32_t pFlag)
136 if (flag & RenderUpdateFlag::Transform) {
137 if (!rTransform) return nullptr;
138 if (!rTransform->update()) {
140 rTransform = nullptr;
144 void *compdata = nullptr;
146 if (compTarget && compMethod == CompositeMethod::ClipPath) {
147 compdata = compTarget->pImpl->update(renderer, pTransform, opacity, compList, pFlag);
148 if (compdata) compList.push({compdata, compMethod});
151 void *edata = nullptr;
152 auto newFlag = static_cast<RenderUpdateFlag>(pFlag | flag);
153 flag = RenderUpdateFlag::None;
154 opacity = (opacity * this->opacity) / 255;
156 if (rTransform && pTransform) {
157 RenderTransform outTransform(pTransform, rTransform);
158 edata = smethod->update(renderer, &outTransform, opacity, compList, newFlag);
160 auto outTransform = pTransform ? pTransform : rTransform;
161 edata = smethod->update(renderer, outTransform, opacity, compList, newFlag);
164 if (compdata) compList.pop();
169 bool render(RenderMethod& renderer)
171 return smethod->render(renderer);
176 auto ret = smethod->duplicate();
178 //duplicate Transform
179 if (ret && rTransform) {
180 ret->pImpl->rTransform = new RenderTransform();
181 if (ret->pImpl->rTransform) {
182 *ret->pImpl->rTransform = *rTransform;
183 ret->pImpl->flag |= RenderUpdateFlag::Transform;
187 ret->pImpl->opacity = opacity;
192 bool composite(Paint* target, CompositeMethod method)
194 if (!target && method != CompositeMethod::None) return false;
203 struct PaintMethod : StrategyMethod
207 PaintMethod(T* _inst) : inst(_inst) {}
210 bool bounds(float* x, float* y, float* w, float* h) const override
212 return inst->bounds(x, y, w, h);
215 bool dispose(RenderMethod& renderer) override
217 return inst->dispose(renderer);
220 void* update(RenderMethod& renderer, const RenderTransform* transform, uint32_t opacity, Array<Composite>& compList, RenderUpdateFlag flag) override
222 return inst->update(renderer, transform, opacity, compList, flag);
225 bool render(RenderMethod& renderer) override
227 return inst->render(renderer);
230 Paint* duplicate() override
232 return inst->duplicate();
237 #endif //_TVG_PAINT_H_