Improve matte rendering performance for simple layer
[platform/core/uifw/lottie-player.git] / src / vector / vpainter.cpp
1 /*
2  * Copyright (c) 2020 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
23 #include "vpainter.h"
24 #include <algorithm>
25
26
27 V_BEGIN_NAMESPACE
28
29
30 void VPainter::drawRle(const VPoint &, const VRle &rle)
31 {
32     if (rle.empty()) return;
33     // mSpanData.updateSpanFunc();
34
35     if (!mSpanData.mUnclippedBlendFunc) return;
36
37     // do draw after applying clip.
38     rle.intersect(mSpanData.clipRect(), mSpanData.mUnclippedBlendFunc,
39                   &mSpanData);
40 }
41
42 void VPainter::drawRle(const VRle &rle, const VRle &clip)
43 {
44     if (rle.empty() || clip.empty()) return;
45
46     if (!mSpanData.mUnclippedBlendFunc) return;
47
48     rle.intersect(clip, mSpanData.mUnclippedBlendFunc, &mSpanData);
49 }
50
51 static void fillRect(const VRect &r, VSpanData *data)
52 {
53     auto x1 = std::max(r.x(), 0);
54     auto x2 = std::min(r.x() + r.width(), data->mDrawableSize.width());
55     auto y1 = std::max(r.y(), 0);
56     auto y2 = std::min(r.y() + r.height(), data->mDrawableSize.height());
57
58     if (x2 <= x1 || y2 <= y1) return;
59
60     const int  nspans = 256;
61     VRle::Span spans[nspans];
62
63     int y = y1;
64     while (y < y2) {
65         int n = std::min(nspans, y2 - y);
66         int i = 0;
67         while (i < n) {
68             spans[i].x = short(x1);
69             spans[i].len = ushort(x2 - x1);
70             spans[i].y = short(y + i);
71             spans[i].coverage = 255;
72             ++i;
73         }
74
75         data->mUnclippedBlendFunc(n, spans, data);
76         y += n;
77     }
78 }
79
80 void VPainter::drawBitmapUntransform(const VRect &  target,
81                                          const VBitmap &bitmap,
82                                          const VRect &  source,
83                                          uint8_t        const_alpha)
84 {
85     mSpanData.initTexture(&bitmap, const_alpha, source);
86     if (!mSpanData.mUnclippedBlendFunc) return;
87
88     // update translation matrix for source texture.
89     mSpanData.dx = float(target.x() - source.x());
90     mSpanData.dy = float(target.y() - source.y());
91
92     fillRect(target, &mSpanData);
93 }
94
95 VPainter::VPainter(VBitmap *buffer)
96 {
97     begin(buffer);
98 }
99 bool VPainter::begin(VBitmap *buffer)
100 {
101     mBuffer.prepare(buffer);
102     mSpanData.init(&mBuffer);
103     // TODO find a better api to clear the surface
104     mBuffer.clear();
105     return true;
106 }
107 void VPainter::end() {}
108
109 void VPainter::setDrawRegion(const VRect &region)
110 {
111     mSpanData.setDrawRegion(region);
112 }
113
114 void VPainter::setBrush(const VBrush &brush)
115 {
116     mSpanData.setup(brush);
117 }
118
119 void VPainter::setBlendMode(BlendMode mode)
120 {
121     mSpanData.mBlendMode = mode;
122 }
123
124 VRect VPainter::clipBoundingRect() const
125 {
126     return mSpanData.clipRect();
127 }
128
129 void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap,
130                           const VRect &source, uint8_t const_alpha)
131 {
132     if (!bitmap.valid()) return;
133
134     drawBitmap(VRect(point, bitmap.size()),
135                bitmap, source, const_alpha);
136 }
137
138 void VPainter::drawBitmap(const VRect &target, const VBitmap &bitmap,
139                           const VRect &source, uint8_t const_alpha)
140 {
141     if (!bitmap.valid()) return;
142
143     // clear any existing brush data.
144     setBrush(VBrush());
145
146     if (target.size() == source.size()) {
147         drawBitmapUntransform(target, bitmap, source, const_alpha);
148     } else {
149         // @TODO scaling
150     }
151 }
152
153 void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap,
154                           uint8_t const_alpha)
155 {
156     if (!bitmap.valid()) return;
157
158     drawBitmap(VRect(point, bitmap.size()),
159                bitmap, bitmap.rect(),
160                const_alpha);
161 }
162
163 void VPainter::drawBitmap(const VRect &rect, const VBitmap &bitmap,
164                           uint8_t const_alpha)
165 {
166     if (!bitmap.valid()) return;
167
168     drawBitmap(rect, bitmap, bitmap.rect(),
169                const_alpha);
170 }
171
172 V_END_NAMESPACE