common: nothing was rendered after an empty masked node came across
[platform/core/graphics/tizenvg.git] / src / lib / tvgPaint.cpp
1 /*
2  * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved.
3
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:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
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
20  * SOFTWARE.
21  */
22 #include "tvgMath.h"
23 #include "tvgPaint.h"
24
25 /************************************************************************/
26 /* Internal Class Implementation                                        */
27 /************************************************************************/
28
29
30 static bool _compFastTrack(Paint* cmpTarget, const RenderTransform* pTransform, RenderTransform* rTransform, RenderRegion& viewport)
31 {
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);
34
35     //Rectangle Candidates?
36     const Point* pts;
37      if (shape->pathCoords(&pts) != 4) return false;
38
39     if (rTransform) rTransform->update();
40
41     //No rotational.
42     if (pTransform && !mathRightAngle(&pTransform->m)) return false;
43     if (rTransform && !mathRightAngle(&rTransform->m)) return false;
44
45     //Perpendicular Rectangle?
46     auto pt1 = pts + 0;
47     auto pt2 = pts + 1;
48     auto pt3 = pts + 2;
49     auto pt4 = pts + 3;
50
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))) {
53
54         auto v1 = *pt1;
55         auto v2 = *pt3;
56
57         if (rTransform) {
58             mathMultiply(&v1, &rTransform->m);
59             mathMultiply(&v2, &rTransform->m);
60         }
61
62         if (pTransform) {
63             mathMultiply(&v1, &pTransform->m);
64             mathMultiply(&v2, &pTransform->m);
65         }
66
67         //sorting
68         if (v1.x > v2.x) {
69             auto tmp = v2.x;
70             v2.x = v1.x;
71             v1.x = tmp;
72         }
73
74         if (v1.y > v2.y) {
75             auto tmp = v2.y;
76             v2.y = v1.y;
77             v1.y = tmp;
78         }
79
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);
84
85         if (viewport.w < 0) viewport.w = 0;
86         if (viewport.h < 0) viewport.h = 0;
87
88         return true;
89     }
90
91     return false;
92 }
93
94
95 Paint* Paint::Impl::duplicate()
96 {
97     auto ret = smethod->duplicate();
98     if (!ret) return nullptr;
99
100     //duplicate Transform
101     if (rTransform) {
102         ret->pImpl->rTransform = new RenderTransform();
103         *ret->pImpl->rTransform = *rTransform;
104         ret->pImpl->renderFlag |= RenderUpdateFlag::Transform;
105     }
106
107     ret->pImpl->opacity = opacity;
108
109     if (cmpTarget) ret->pImpl->cmpTarget = cmpTarget->duplicate();
110
111     ret->pImpl->cmpMethod = cmpMethod;
112
113     return ret;
114 }
115
116
117 bool Paint::Impl::rotate(float degree)
118 {
119     if (rTransform) {
120         if (mathEqual(degree, rTransform->degree)) return true;
121     } else {
122         if (mathZero(degree)) return true;
123         rTransform = new RenderTransform();
124     }
125     rTransform->degree = degree;
126     if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform;
127
128     return true;
129 }
130
131
132 bool Paint::Impl::scale(float factor)
133 {
134     if (rTransform) {
135         if (mathEqual(factor, rTransform->scale)) return true;
136     } else {
137         if (mathZero(factor)) return true;
138         rTransform = new RenderTransform();
139     }
140     rTransform->scale = factor;
141     if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform;
142
143     return true;
144 }
145
146
147 bool Paint::Impl::translate(float x, float y)
148 {
149     if (rTransform) {
150         if (mathEqual(x, rTransform->x) && mathEqual(y, rTransform->y)) return true;
151     } else {
152         if (mathZero(x) && mathZero(y)) return true;
153         rTransform = new RenderTransform();
154     }
155     rTransform->x = x;
156     rTransform->y = y;
157     if (!rTransform->overriding) renderFlag |= RenderUpdateFlag::Transform;
158
159     return true;
160 }
161
162
163 bool Paint::Impl::render(RenderMethod& renderer)
164 {
165     Compositor* cmp = nullptr;
166
167     //OPTIMIZE_ME: Can we replace the simple AlphaMasking with ClipPath?
168
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);
177     }
178
179     if (cmp) renderer.beginComposite(cmp, cmpMethod, cmpTarget->pImpl->opacity);
180
181     auto ret = smethod->render(renderer);
182
183     if (cmp) renderer.endComposite(cmp);
184
185     return ret;
186 }
187
188
189 void* Paint::Impl::update(RenderMethod& renderer, const RenderTransform* pTransform, uint32_t opacity, Array<RenderData>& clips, uint32_t pFlag)
190 {
191     if (renderFlag & RenderUpdateFlag::Transform) {
192         if (!rTransform) return nullptr;
193         if (!rTransform->update()) {
194             delete(rTransform);
195             rTransform = nullptr;
196         }
197     }
198
199     /* 1. Composition Pre Processing */
200     void *cmpData = nullptr;
201     RenderRegion viewport;
202     bool cmpFastTrack = false;
203
204     if (cmpTarget) {
205         cmpTarget->pImpl->ctxFlag = ContextFlag::Invalid;   //reset
206
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);
213             uint8_t a;
214             shape->fillColor(nullptr, nullptr, nullptr, &a);
215             if (a == 255 && shape->opacity() == 255 && !shape->fill()) tryFastTrack = true;
216         }
217         if (tryFastTrack) {
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;
224             }
225         }
226         if (!cmpFastTrack) {
227             cmpData = cmpTarget->pImpl->update(renderer, pTransform, 255, clips, pFlag);
228             if (cmpMethod == CompositeMethod::ClipPath) clips.push(cmpData);
229         }
230     }
231
232     /* 2. Main Update */
233     void *edata = nullptr;
234     auto newFlag = static_cast<RenderUpdateFlag>(pFlag | renderFlag);
235     renderFlag = RenderUpdateFlag::None;
236     opacity = (opacity * this->opacity) / 255;
237
238     if (rTransform && pTransform) {
239         RenderTransform outTransform(pTransform, rTransform);
240         edata = smethod->update(renderer, &outTransform, opacity, clips, newFlag);
241     } else {
242         auto outTransform = pTransform ? pTransform : rTransform;
243         edata = smethod->update(renderer, outTransform, opacity, clips, newFlag);
244     }
245
246     /* 3. Composition Post Processing */
247     if (cmpFastTrack) renderer.viewport(viewport);
248     else if (cmpData && cmpMethod == CompositeMethod::ClipPath) clips.pop();
249
250     return edata;
251 }
252
253 bool Paint::Impl::bounds(float* x, float* y, float* w, float* h, bool transformed)
254 {
255     Matrix* m = nullptr;
256
257     //Case: No transformed, quick return!
258     if (!transformed || !(m = this->transform())) return smethod->bounds(x, y, w, h);
259
260     //Case: Transformed
261     auto tx = 0.0f;
262     auto ty = 0.0f;
263     auto tw = 0.0f;
264     auto th = 0.0f;
265
266     auto ret = smethod->bounds(&tx, &ty, &tw, &th);
267
268     //Get vertices
269     Point pt[4] = {{tx, ty}, {tx + tw, ty}, {tx + tw, ty + th}, {tx, ty + th}};
270
271     //New bounding box
272     auto x1 = FLT_MAX;
273     auto y1 = FLT_MAX;
274     auto x2 = -FLT_MAX;
275     auto y2 = -FLT_MAX;
276
277     //Compute the AABB after transformation
278     for (int i = 0; i < 4; i++) {
279         mathMultiply(&pt[i], m);
280
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;
285     }
286
287     if (x) *x = x1;
288     if (y) *y = y1;
289     if (w) *w = x2 - x1;
290     if (h) *h = y2 - y1;
291
292     return ret;
293 }
294
295
296 /************************************************************************/
297 /* External Class Implementation                                        */
298 /************************************************************************/
299
300 Paint :: Paint() : pImpl(new Impl())
301 {
302 }
303
304
305 Paint :: ~Paint()
306 {
307     delete(pImpl);
308 }
309
310
311 Result Paint::rotate(float degree) noexcept
312 {
313     if (pImpl->rotate(degree)) return Result::Success;
314     return Result::FailedAllocation;
315 }
316
317
318 Result Paint::scale(float factor) noexcept
319 {
320     if (pImpl->scale(factor)) return Result::Success;
321     return Result::FailedAllocation;
322 }
323
324
325 Result Paint::translate(float x, float y) noexcept
326 {
327     if (pImpl->translate(x, y)) return Result::Success;
328     return Result::FailedAllocation;
329 }
330
331
332 Result Paint::transform(const Matrix& m) noexcept
333 {
334     if (pImpl->transform(m)) return Result::Success;
335     return Result::FailedAllocation;
336 }
337
338
339 Matrix Paint::transform() noexcept
340 {
341     auto pTransform = pImpl->transform();
342     if (pTransform) return *pTransform;
343     return {1, 0, 0, 0, 1, 0, 0, 0, 1};
344 }
345
346
347 TVG_DEPRECATED Result Paint::bounds(float* x, float* y, float* w, float* h) const noexcept
348 {
349     return this->bounds(x, y, w, h, false);
350 }
351
352
353 Result Paint::bounds(float* x, float* y, float* w, float* h, bool transform) const noexcept
354 {
355     if (pImpl->bounds(x, y, w, h, transform)) return Result::Success;
356     return Result::InsufficientCondition;
357 }
358
359
360 Paint* Paint::duplicate() const noexcept
361 {
362     return pImpl->duplicate();
363 }
364
365
366 Result Paint::composite(std::unique_ptr<Paint> target, CompositeMethod method) noexcept
367 {
368     auto p = target.release();
369     if (pImpl->composite(p, method)) return Result::Success;
370     if (p) delete(p);
371     return Result::InvalidArguments;
372 }
373
374
375 CompositeMethod Paint::composite(const Paint** target) const noexcept
376 {
377     if (target) *target = pImpl->cmpTarget;
378
379     return pImpl->cmpMethod;
380 }
381
382
383 Result Paint::opacity(uint8_t o) noexcept
384 {
385     if (pImpl->opacity == o) return Result::Success;
386
387     pImpl->opacity = o;
388     pImpl->renderFlag |= RenderUpdateFlag::Color;
389
390     return Result::Success;
391 }
392
393
394 uint8_t Paint::opacity() const noexcept
395 {
396     return pImpl->opacity;
397 }