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
30 void VPainter::drawRle(const VPoint &, const VRle &rle)
32 if (rle.empty()) return;
33 // mSpanData.updateSpanFunc();
35 if (!mSpanData.mUnclippedBlendFunc) return;
37 // do draw after applying clip.
38 rle.intersect(mSpanData.clipRect(), mSpanData.mUnclippedBlendFunc,
42 void VPainter::drawRle(const VRle &rle, const VRle &clip)
44 if (rle.empty() || clip.empty()) return;
46 if (!mSpanData.mUnclippedBlendFunc) return;
48 rle.intersect(clip, mSpanData.mUnclippedBlendFunc, &mSpanData);
51 static void fillRect(const VRect &r, VSpanData *data)
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());
58 if (x2 <= x1 || y2 <= y1) return;
60 const int nspans = 256;
61 VRle::Span spans[nspans];
65 int n = std::min(nspans, y2 - y);
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;
75 data->mUnclippedBlendFunc(n, spans, data);
80 void VPainter::drawBitmapUntransform(const VRect & target,
81 const VBitmap &bitmap,
85 mSpanData.initTexture(&bitmap, const_alpha, source);
86 if (!mSpanData.mUnclippedBlendFunc) return;
88 // update translation matrix for source texture.
89 mSpanData.dx = float(target.x() - source.x());
90 mSpanData.dy = float(target.y() - source.y());
92 fillRect(target, &mSpanData);
95 VPainter::VPainter(VBitmap *buffer)
99 bool VPainter::begin(VBitmap *buffer)
101 mBuffer.prepare(buffer);
102 mSpanData.init(&mBuffer);
103 // TODO find a better api to clear the surface
107 void VPainter::end() {}
109 void VPainter::setDrawRegion(const VRect ®ion)
111 mSpanData.setDrawRegion(region);
114 void VPainter::setBrush(const VBrush &brush)
116 mSpanData.setup(brush);
119 void VPainter::setBlendMode(BlendMode mode)
121 mSpanData.mBlendMode = mode;
124 VRect VPainter::clipBoundingRect() const
126 return mSpanData.clipRect();
129 void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap,
130 const VRect &source, uint8_t const_alpha)
132 if (!bitmap.valid()) return;
134 drawBitmap(VRect(point, bitmap.size()),
135 bitmap, source, const_alpha);
138 void VPainter::drawBitmap(const VRect &target, const VBitmap &bitmap,
139 const VRect &source, uint8_t const_alpha)
141 if (!bitmap.valid()) return;
143 // clear any existing brush data.
146 if (target.size() == source.size()) {
147 drawBitmapUntransform(target, bitmap, source, const_alpha);
153 void VPainter::drawBitmap(const VPoint &point, const VBitmap &bitmap,
156 if (!bitmap.valid()) return;
158 drawBitmap(VRect(point, bitmap.size()),
159 bitmap, bitmap.rect(),
163 void VPainter::drawBitmap(const VRect &rect, const VBitmap &bitmap,
166 if (!bitmap.valid()) return;
168 drawBitmap(rect, bitmap, bitmap.rect(),