blob: 38ef2828bdfc34a4c96ebd04e50c73fd4690d968 [file] [log] [blame]
msarettc573a402016-08-02 08:05:56 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkLatticeIter.h"
9#include "SkRect.h"
10
11/**
12 * Divs must be in increasing order with no duplicates.
13 */
14static bool valid_divs(const int* divs, int count, int len) {
msarettc573a402016-08-02 08:05:56 -070015 int prev = -1;
16 for (int i = 0; i < count; i++) {
msarett0764efe2016-09-02 11:24:30 -070017 if (prev >= divs[i] || divs[i] >= len) {
msarettc573a402016-08-02 08:05:56 -070018 return false;
19 }
20 }
21
22 return true;
23}
24
25bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattice) {
msarett0764efe2016-09-02 11:24:30 -070026 bool zeroXDivs = lattice.fXCount <= 0 || (1 == lattice.fXCount && 0 == lattice.fXDivs[0]);
27 bool zeroYDivs = lattice.fYCount <= 0 || (1 == lattice.fYCount && 0 == lattice.fYDivs[0]);
28 if (zeroXDivs && zeroYDivs) {
29 return false;
30 }
31
msarettc573a402016-08-02 08:05:56 -070032 return valid_divs(lattice.fXDivs, lattice.fXCount, width) &&
33 valid_divs(lattice.fYDivs, lattice.fYCount, height);
34}
35
36/**
37 * Count the number of pixels that are in "scalable" patches.
38 */
39static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsScalable,
40 int length) {
41 if (0 == numDivs) {
42 return firstIsScalable ? length : 0;
43 }
44
45 int i;
46 int count;
47 if (firstIsScalable) {
48 count = divs[0];
49 i = 1;
50 } else {
51 count = 0;
52 i = 0;
53 }
54
55 for (; i < numDivs; i += 2) {
56 // Alternatively, we could use |top| and |bottom| as variable names, instead of
57 // |left| and |right|.
58 int left = divs[i];
59 int right = (i + 1 < numDivs) ? divs[i + 1] : length;
60 count += right - left;
61 }
62
63 return count;
64}
65
66/**
67 * Set points for the src and dst rects on subsequent draw calls.
68 */
69static void set_points(float* dst, float* src, const int* divs, int divCount, int srcFixed,
70 int srcScalable, float dstStart, float dstStop, bool isScalable) {
71
72 float dstLen = dstStop - dstStart;
73 int srcLen = srcFixed + srcScalable;
74 float scale;
75 if (srcFixed <= dstLen) {
76 // This is the "normal" case, where we scale the "scalable" patches and leave
77 // the other patches fixed.
78 scale = (dstLen - ((float) srcFixed)) / ((float) srcScalable);
79 } else {
80 // In this case, we eliminate the "scalable" patches and scale the "fixed" patches.
81 scale = dstLen / ((float) srcFixed);
82 }
83
84 src[0] = 0.0f;
85 dst[0] = dstStart;
86 for (int i = 0; i < divCount; i++) {
87 src[i + 1] = (float) (divs[i]);
88 float srcDelta = src[i + 1] - src[i];
89 float dstDelta;
90 if (srcFixed <= dstLen) {
91 dstDelta = isScalable ? scale * srcDelta : srcDelta;
92 } else {
93 dstDelta = isScalable ? 0.0f : scale * srcDelta;
94 }
95 dst[i + 1] = dst[i] + dstDelta;
96
97 // Alternate between "scalable" and "fixed" patches.
98 isScalable = !isScalable;
99 }
100
101 src[divCount + 1] = (float) srcLen;
102 dst[divCount + 1] = dstStop;
103}
104
105SkLatticeIter::SkLatticeIter(int srcWidth, int srcHeight, const SkCanvas::Lattice& lattice,
106 const SkRect& dst)
107{
108 const int* xDivs = lattice.fXDivs;
msarett0764efe2016-09-02 11:24:30 -0700109 const int origXCount = lattice.fXCount;
msarettc573a402016-08-02 08:05:56 -0700110 const int* yDivs = lattice.fYDivs;
msarett0764efe2016-09-02 11:24:30 -0700111 const int origYCount = lattice.fYCount;
msarettc573a402016-08-02 08:05:56 -0700112
113 // In the x-dimension, the first rectangle always starts at x = 0 and is "scalable".
114 // If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so the
115 // first real rectangle "scalable" in the x-direction.
116 //
117 // The same interpretation applies to the y-dimension.
118 //
119 // As we move left to right across the image, alternating patches will be "fixed" or
120 // "scalable" in the x-direction. Similarly, as move top to bottom, alternating
121 // patches will be "fixed" or "scalable" in the y-direction.
msarett0764efe2016-09-02 11:24:30 -0700122 int xCount = origXCount;
123 int yCount = origYCount;
124 bool xIsScalable = (xCount > 0 && 0 == xDivs[0]);
msarettc573a402016-08-02 08:05:56 -0700125 if (xIsScalable) {
126 // Once we've decided that the first patch is "scalable", we don't need the
127 // xDiv. It is always implied that we start at zero.
128 xDivs++;
129 xCount--;
130 }
msarett0764efe2016-09-02 11:24:30 -0700131 bool yIsScalable = (yCount > 0 && 0 == yDivs[0]);
msarettc573a402016-08-02 08:05:56 -0700132 if (yIsScalable) {
133 // Once we've decided that the first patch is "scalable", we don't need the
134 // yDiv. It is always implied that we start at zero.
135 yDivs++;
136 yCount--;
137 }
138
msarettc573a402016-08-02 08:05:56 -0700139 // Count "scalable" and "fixed" pixels in each dimension.
140 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, srcWidth);
141 int xCountFixed = srcWidth - xCountScalable;
142 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, srcHeight);
143 int yCountFixed = srcHeight - yCountScalable;
144
145 fSrcX.reset(xCount + 2);
146 fDstX.reset(xCount + 2);
147 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountScalable,
148 dst.fLeft, dst.fRight, xIsScalable);
149
150 fSrcY.reset(yCount + 2);
151 fDstY.reset(yCount + 2);
152 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountScalable,
153 dst.fTop, dst.fBottom, yIsScalable);
154
155 fCurrX = fCurrY = 0;
msarett0764efe2016-09-02 11:24:30 -0700156 fNumRectsInLattice = (xCount + 1) * (yCount + 1);
157 fNumRectsToDraw = fNumRectsInLattice;
158
159 if (lattice.fFlags) {
160 fFlags.push_back_n(fNumRectsInLattice);
161
162 const SkCanvas::Lattice::Flags* flags = lattice.fFlags;
163
164 bool hasPadRow = (yCount != origYCount);
165 bool hasPadCol = (xCount != origXCount);
166 if (hasPadRow) {
167 // The first row of rects are all empty, skip the first row of flags.
168 flags += origXCount + 1;
169 }
170
171 int i = 0;
172 for (int y = 0; y < yCount + 1; y++) {
173 for (int x = 0; x < origXCount + 1; x++) {
174 if (0 == x && hasPadCol) {
175 // The first column of rects are all empty. Skip a rect.
176 flags++;
177 continue;
178 }
179
180 fFlags[i] = *flags;
181 flags++;
182 i++;
183 }
184 }
185
186 for (int j = 0; j < fFlags.count(); j++) {
187 if (SkCanvas::Lattice::kTransparent_Flags == fFlags[j]) {
188 fNumRectsToDraw--;
189 }
190 }
191 }
msarettc573a402016-08-02 08:05:56 -0700192}
193
194bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) {
195 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center);
196}
197
198SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst) {
199 SkASSERT(SkIRect::MakeWH(w, h).contains(c));
200
201 fSrcX.reset(4);
202 fSrcY.reset(4);
203 fDstX.reset(4);
204 fDstY.reset(4);
205
206 fSrcX[0] = 0;
207 fSrcX[1] = SkIntToScalar(c.fLeft);
208 fSrcX[2] = SkIntToScalar(c.fRight);
209 fSrcX[3] = SkIntToScalar(w);
210
211 fSrcY[0] = 0;
212 fSrcY[1] = SkIntToScalar(c.fTop);
213 fSrcY[2] = SkIntToScalar(c.fBottom);
214 fSrcY[3] = SkIntToScalar(h);
215
216 fDstX[0] = dst.fLeft;
217 fDstX[1] = dst.fLeft + SkIntToScalar(c.fLeft);
218 fDstX[2] = dst.fRight - SkIntToScalar(w - c.fRight);
219 fDstX[3] = dst.fRight;
220
221 fDstY[0] = dst.fTop;
222 fDstY[1] = dst.fTop + SkIntToScalar(c.fTop);
223 fDstY[2] = dst.fBottom - SkIntToScalar(h - c.fBottom);
224 fDstY[3] = dst.fBottom;
225
226 if (fDstX[1] > fDstX[2]) {
227 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width());
228 fDstX[2] = fDstX[1];
229 }
230
231 if (fDstY[1] > fDstY[2]) {
232 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height());
233 fDstY[2] = fDstY[1];
234 }
235
236 fCurrX = fCurrY = 0;
msarett0764efe2016-09-02 11:24:30 -0700237 fNumRectsInLattice = 9;
238 fNumRectsToDraw = 9;
msarettc573a402016-08-02 08:05:56 -0700239}
240
241bool SkLatticeIter::next(SkRect* src, SkRect* dst) {
msarett0764efe2016-09-02 11:24:30 -0700242 int currRect = fCurrX + fCurrY * (fSrcX.count() - 1);
243 if (currRect == fNumRectsInLattice) {
msarettc573a402016-08-02 08:05:56 -0700244 return false;
245 }
246
247 const int x = fCurrX;
248 const int y = fCurrY;
249 SkASSERT(x >= 0 && x < fSrcX.count() - 1);
250 SkASSERT(y >= 0 && y < fSrcY.count() - 1);
251
msarettc573a402016-08-02 08:05:56 -0700252 if (fSrcX.count() - 1 == ++fCurrX) {
253 fCurrX = 0;
254 fCurrY += 1;
msarettc573a402016-08-02 08:05:56 -0700255 }
msarett0764efe2016-09-02 11:24:30 -0700256
257 if (fFlags.count() > 0 && SkToBool(SkCanvas::Lattice::kTransparent_Flags & fFlags[currRect])) {
258 return this->next(src, dst);
259 }
260
261 src->set(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]);
262 dst->set(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]);
msarettc573a402016-08-02 08:05:56 -0700263 return true;
264}
msarett10e3d9b2016-08-18 15:46:03 -0700265
266void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) {
267 SkASSERT(matrix.isScaleTranslate());
268 SkScalar tx = matrix.getTranslateX();
269 SkScalar sx = matrix.getScaleX();
270 for (int i = 0; i < fDstX.count(); i++) {
271 fDstX[i] = fDstX[i] * sx + tx;
272 }
273
274 SkScalar ty = matrix.getTranslateY();
275 SkScalar sy = matrix.getScaleY();
276 for (int i = 0; i < fDstY.count(); i++) {
277 fDstY[i] = fDstY[i] * sy + ty;
278 }
279}