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
25 /************************************************************************/
26 /* Internal Class Implementation */
27 /************************************************************************/
30 static bool _compFastTrack(Paint* cmpTarget, const RenderTransform* pTransform, RenderTransform* rTransform, RenderRegion& viewport)
32 /* Access Shape class by Paint is bad... but it's ok still it's an internal usage. */
33 auto shape = static_cast<Shape*>(cmpTarget);
35 //Rectangle Candidates?
37 if (shape->pathCoords(&pts) != 4) return false;
39 if (rTransform) rTransform->update();
42 if (pTransform && !mathRightAngle(&pTransform->m)) return false;
43 if (rTransform && !mathRightAngle(&rTransform->m)) return false;
45 //Perpendicular Rectangle?
51 if ((mathEqual(pt1->x, pt2->x) && mathEqual(pt2->y, pt3->y) && mathEqual(pt3->x, pt4->x) && mathEqual(pt1->y, pt4->y)) ||
52 (mathEqual(pt2->x, pt3->x) && mathEqual(pt1->y, pt2->y) && mathEqual(pt1->x, pt4->x) && mathEqual(pt3->y, pt4->y))) {
58 mathMultiply(&v1, &rTransform->m);
59 mathMultiply(&v2, &rTransform->m);
63 mathMultiply(&v1, &pTransform->m);
64 mathMultiply(&v2, &pTransform->m);
80 viewport.x = static_cast<int32_t>(v1.x);
81 viewport.y = static_cast<int32_t>(v1.y);
82 viewport.w = static_cast<int32_t>(v2.x - v1.x + 0.5f);
83 viewport.h = static_cast<int32_t>(v2.y - v1.y + 0.5f);
85 if (viewport.w < 0) viewport.w = 0;
86 if (viewport.h < 0) viewport.h = 0;
95 Paint* Paint::Impl::duplicate()
97 auto ret = smethod->duplicate();
98 if (!ret) return nullptr;
100 //duplicate Transform
102 ret->pImpl->rTransform = new RenderTransform();
103 *ret->pImpl->rTransform = *rTransform;
104 ret->pImpl->renderFlag |= RenderUpdateFlag::Transform;
107 ret->pImpl->opacity = opacity;
109 if (cmpTarget) ret->pImpl->cmpTarget = cmpTarget->duplicate();
111 ret->pImpl->cmpMethod = cmpMethod;
117 bool Paint::Impl::rotate(float degree)
120 if (mathEqual(degree, rTransform->degree)) return true;
122 if (mathZero(degree)) return true;
123 rTransform = new RenderTransform();
125 rTransform->degree = degree;
126 if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform;
132 bool Paint::Impl::scale(float factor)
135 if (mathEqual(factor, rTransform->scale)) return true;
137 if (mathZero(factor)) return true;
138 rTransform = new RenderTransform();
140 rTransform->scale = factor;
141 if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform;
147 bool Paint::Impl::translate(float x, float y)
150 if (mathEqual(x, rTransform->x) && mathEqual(y, rTransform->y)) return true;
152 if (mathZero(x) && mathZero(y)) return true;
153 rTransform = new RenderTransform();
157 if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform;
163 bool Paint::Impl::render(RenderMethod& renderer)
165 Compositor* cmp = nullptr;
167 //OPTIMIZE_ME: Can we replace the simple AlphaMasking with ClipPath?
169 /* Note: only ClipPath is processed in update() step.
170 Create a composition image. */
171 if (cmpTarget && cmpMethod != CompositeMethod::ClipPath && !(cmpTarget->pImpl->ctxFlag & ContextFlag::FastTrack)) {
172 auto region = smethod->bounds(renderer);
173 if (region.w == 0 || region.h == 0) return true;
174 cmp = renderer.target(region);
175 renderer.beginComposite(cmp, CompositeMethod::None, 255);
176 cmpTarget->pImpl->render(renderer);
179 if (cmp) renderer.beginComposite(cmp, cmpMethod, cmpTarget->pImpl->opacity);
181 auto ret = smethod->render(renderer);
183 if (cmp) renderer.endComposite(cmp);
189 void* Paint::Impl::update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, uint32_t pFlag)
191 if (renderFlag & RenderUpdateFlag::Transform) {
192 if (!rTransform) return nullptr;
193 if (!rTransform->update()) {
195 rTransform = nullptr;
199 /* 1. Composition Pre Processing */
200 void *cmpData = nullptr;
201 RenderRegion viewport;
202 bool cmpFastTrack = false;
205 cmpTarget->pImpl->ctxFlag = ContextFlag::Invalid; //reset
207 /* If transform has no rotation factors && ClipPath / AlphaMasking is a simple rectangle,
208 we can avoid regular ClipPath / AlphaMasking sequence but use viewport for performance */
209 auto tryFastTrack = false;
210 if (cmpMethod == CompositeMethod::ClipPath) tryFastTrack = true;
211 else if (cmpMethod == CompositeMethod::AlphaMask && cmpTarget->identifier() == TVG_CLASS_ID_SHAPE) {
212 auto shape = static_cast<Shape*>(cmpTarget);
214 shape->fillColor(nullptr, nullptr, nullptr, &a);
215 if (a == 255 && shape->opacity() == 255 && !shape->fill()) tryFastTrack = true;
218 RenderRegion viewport2;
219 if ((cmpFastTrack = _compFastTrack(cmpTarget, pTransform, cmpTarget->pImpl->rTransform, viewport2))) {
220 viewport = renderer.viewport();
221 viewport2.intersect(viewport);
222 renderer.viewport(viewport2);
223 cmpTarget->pImpl->ctxFlag |= ContextFlag::FastTrack;
227 cmpData = cmpTarget->pImpl->update(renderer, pTransform, 255, clips, pFlag);
228 if (cmpMethod == CompositeMethod::ClipPath) clips.push(cmpData);
233 void *edata = nullptr;
234 auto newFlag = static_cast<RenderUpdateFlag>(pFlag | renderFlag);
235 renderFlag = RenderUpdateFlag::None;
236 opacity = (opacity * this->opacity) / 255;
238 if (rTransform && pTransform) {
239 RenderTransform outTransform(pTransform, rTransform);
240 edata = smethod->update(renderer, &outTransform, opacity, clips, newFlag);
242 auto outTransform = pTransform ? pTransform : rTransform;
243 edata = smethod->update(renderer, outTransform, opacity, clips, newFlag);
246 /* 3. Composition Post Processing */
247 if (cmpFastTrack) renderer.viewport(viewport);
248 else if (cmpData && cmpMethod == CompositeMethod::ClipPath) clips.pop();
253 bool Paint::Impl::bounds(float* x, float* y, float* w, float* h, bool transformed)
257 //Case: No transformed, quick return!
258 if (!transformed || !(m = this->transform())) return smethod->bounds(x, y, w, h);
266 auto ret = smethod->bounds(&tx, &ty, &tw, &th);
269 Point pt[4] = {{tx, ty}, {tx + tw, ty}, {tx + tw, ty + th}, {tx, ty + th}};
277 //Compute the AABB after transformation
278 for (int i = 0; i < 4; i++) {
279 mathMultiply(&pt[i], m);
281 if (pt[i].x < x1) x1 = pt[i].x;
282 if (pt[i].x > x2) x2 = pt[i].x;
283 if (pt[i].y < y1) y1 = pt[i].y;
284 if (pt[i].y > y2) y2 = pt[i].y;
296 /************************************************************************/
297 /* External Class Implementation */
298 /************************************************************************/
300 Paint :: Paint() : pImpl(new Impl())
311 Result Paint::rotate(float degree) noexcept
313 if (pImpl->rotate(degree)) return Result::Success;
314 return Result::FailedAllocation;
318 Result Paint::scale(float factor) noexcept
320 if (pImpl->scale(factor)) return Result::Success;
321 return Result::FailedAllocation;
325 Result Paint::translate(float x, float y) noexcept
327 if (pImpl->translate(x, y)) return Result::Success;
328 return Result::FailedAllocation;
332 Result Paint::transform(const Matrix& m) noexcept
334 if (pImpl->transform(m)) return Result::Success;
335 return Result::FailedAllocation;
339 Matrix Paint::transform() noexcept
341 auto pTransform = pImpl->transform();
342 if (pTransform) return *pTransform;
343 return {1, 0, 0, 0, 1, 0, 0, 0, 1};
347 TVG_DEPRECATED Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
349 return this->bounds(x, y, w, h, false);
353 Result Paint::bounds(float* x, float* y, float* w, float* h, bool transform) const noexcept
355 if (pImpl->bounds(x, y, w, h, transform)) return Result::Success;
356 return Result::InsufficientCondition;
360 Paint* Paint::duplicate() const noexcept
362 return pImpl->duplicate();
366 Result Paint::composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept
368 auto p = target.release();
369 if (pImpl->composite(p, method)) return Result::Success;
371 return Result::InvalidArguments;
375 CompositeMethod Paint::composite(const Paint** target) const noexcept
377 if (target) *target = pImpl->cmpTarget;
379 return pImpl->cmpMethod;
383 Result Paint::opacity(uint8_t o) noexcept
385 if (pImpl->opacity == o) return Result::Success;
388 pImpl->renderFlag |= RenderUpdateFlag::Color;
390 return Result::Success;
394 uint8_t Paint::opacity() const noexcept
396 return pImpl->opacity;