blob: 4fe9352d0a5e055a978605d50e0701938d2bb78e [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 */
msarett71df2d72016-09-30 12:41:42 -070014static bool valid_divs(const int* divs, int count, int start, int end) {
15 int prev = start - 1;
msarettc573a402016-08-02 08:05:56 -070016 for (int i = 0; i < count; i++) {
msarett71df2d72016-09-30 12:41:42 -070017 if (prev >= divs[i] || divs[i] >= end) {
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) {
msarett71df2d72016-09-30 12:41:42 -070026 SkIRect totalBounds = SkIRect::MakeWH(width, height);
27 SkASSERT(lattice.fBounds);
28 const SkIRect latticeBounds = *lattice.fBounds;
29 if (!totalBounds.contains(latticeBounds)) {
30 return false;
31 }
32
33 bool zeroXDivs = lattice.fXCount <= 0 || (1 == lattice.fXCount &&
34 latticeBounds.fLeft == lattice.fXDivs[0]);
35 bool zeroYDivs = lattice.fYCount <= 0 || (1 == lattice.fYCount &&
36 latticeBounds.fTop == lattice.fYDivs[0]);
msarett0764efe2016-09-02 11:24:30 -070037 if (zeroXDivs && zeroYDivs) {
38 return false;
39 }
40
msarett71df2d72016-09-30 12:41:42 -070041 return valid_divs(lattice.fXDivs, lattice.fXCount, latticeBounds.fLeft, latticeBounds.fRight)
42 && valid_divs(lattice.fYDivs, lattice.fYCount, latticeBounds.fTop, latticeBounds.fBottom);
msarettc573a402016-08-02 08:05:56 -070043}
44
45/**
46 * Count the number of pixels that are in "scalable" patches.
47 */
48static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsScalable,
msarett71df2d72016-09-30 12:41:42 -070049 int start, int end) {
msarettc573a402016-08-02 08:05:56 -070050 if (0 == numDivs) {
msarett71df2d72016-09-30 12:41:42 -070051 return firstIsScalable ? end - start : 0;
msarettc573a402016-08-02 08:05:56 -070052 }
53
54 int i;
55 int count;
56 if (firstIsScalable) {
msarett71df2d72016-09-30 12:41:42 -070057 count = divs[0] - start;
msarettc573a402016-08-02 08:05:56 -070058 i = 1;
59 } else {
60 count = 0;
61 i = 0;
62 }
63
64 for (; i < numDivs; i += 2) {
65 // Alternatively, we could use |top| and |bottom| as variable names, instead of
66 // |left| and |right|.
67 int left = divs[i];
msarett71df2d72016-09-30 12:41:42 -070068 int right = (i + 1 < numDivs) ? divs[i + 1] : end;
msarettc573a402016-08-02 08:05:56 -070069 count += right - left;
70 }
71
72 return count;
73}
74
75/**
76 * Set points for the src and dst rects on subsequent draw calls.
77 */
78static void set_points(float* dst, float* src, const int* divs, int divCount, int srcFixed,
msarett71df2d72016-09-30 12:41:42 -070079 int srcScalable, float srcStart, float srcEnd, float dstStart, float dstEnd,
80 bool isScalable) {
msarettc573a402016-08-02 08:05:56 -070081
msarett71df2d72016-09-30 12:41:42 -070082 float dstLen = dstEnd - dstStart;
msarettc573a402016-08-02 08:05:56 -070083 float scale;
84 if (srcFixed <= dstLen) {
85 // This is the "normal" case, where we scale the "scalable" patches and leave
86 // the other patches fixed.
87 scale = (dstLen - ((float) srcFixed)) / ((float) srcScalable);
88 } else {
89 // In this case, we eliminate the "scalable" patches and scale the "fixed" patches.
90 scale = dstLen / ((float) srcFixed);
91 }
92
msarett71df2d72016-09-30 12:41:42 -070093 src[0] = srcStart;
msarettc573a402016-08-02 08:05:56 -070094 dst[0] = dstStart;
95 for (int i = 0; i < divCount; i++) {
96 src[i + 1] = (float) (divs[i]);
97 float srcDelta = src[i + 1] - src[i];
98 float dstDelta;
99 if (srcFixed <= dstLen) {
100 dstDelta = isScalable ? scale * srcDelta : srcDelta;
101 } else {
102 dstDelta = isScalable ? 0.0f : scale * srcDelta;
103 }
104 dst[i + 1] = dst[i] + dstDelta;
105
106 // Alternate between "scalable" and "fixed" patches.
107 isScalable = !isScalable;
108 }
109
msarett71df2d72016-09-30 12:41:42 -0700110 src[divCount + 1] = srcEnd;
111 dst[divCount + 1] = dstEnd;
msarettc573a402016-08-02 08:05:56 -0700112}
113
msarett71df2d72016-09-30 12:41:42 -0700114SkLatticeIter::SkLatticeIter(const SkCanvas::Lattice& lattice, const SkRect& dst) {
msarettc573a402016-08-02 08:05:56 -0700115 const int* xDivs = lattice.fXDivs;
msarett0764efe2016-09-02 11:24:30 -0700116 const int origXCount = lattice.fXCount;
msarettc573a402016-08-02 08:05:56 -0700117 const int* yDivs = lattice.fYDivs;
msarett0764efe2016-09-02 11:24:30 -0700118 const int origYCount = lattice.fYCount;
msarett71df2d72016-09-30 12:41:42 -0700119 SkASSERT(lattice.fBounds);
120 const SkIRect src = *lattice.fBounds;
msarettc573a402016-08-02 08:05:56 -0700121
122 // In the x-dimension, the first rectangle always starts at x = 0 and is "scalable".
123 // If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so the
124 // first real rectangle "scalable" in the x-direction.
125 //
126 // The same interpretation applies to the y-dimension.
127 //
128 // As we move left to right across the image, alternating patches will be "fixed" or
129 // "scalable" in the x-direction. Similarly, as move top to bottom, alternating
130 // patches will be "fixed" or "scalable" in the y-direction.
msarett0764efe2016-09-02 11:24:30 -0700131 int xCount = origXCount;
132 int yCount = origYCount;
msarett71df2d72016-09-30 12:41:42 -0700133 bool xIsScalable = (xCount > 0 && src.fLeft == xDivs[0]);
msarettc573a402016-08-02 08:05:56 -0700134 if (xIsScalable) {
135 // Once we've decided that the first patch is "scalable", we don't need the
msarett71df2d72016-09-30 12:41:42 -0700136 // xDiv. It is always implied that we start at the edge of the bounds.
msarettc573a402016-08-02 08:05:56 -0700137 xDivs++;
138 xCount--;
139 }
msarett71df2d72016-09-30 12:41:42 -0700140 bool yIsScalable = (yCount > 0 && src.fTop == yDivs[0]);
msarettc573a402016-08-02 08:05:56 -0700141 if (yIsScalable) {
142 // Once we've decided that the first patch is "scalable", we don't need the
msarett71df2d72016-09-30 12:41:42 -0700143 // yDiv. It is always implied that we start at the edge of the bounds.
msarettc573a402016-08-02 08:05:56 -0700144 yDivs++;
145 yCount--;
146 }
147
msarettc573a402016-08-02 08:05:56 -0700148 // Count "scalable" and "fixed" pixels in each dimension.
msarett71df2d72016-09-30 12:41:42 -0700149 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, src.fLeft, src.fRight);
150 int xCountFixed = src.width() - xCountScalable;
151 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, src.fTop, src.fBottom);
152 int yCountFixed = src.height() - yCountScalable;
msarettc573a402016-08-02 08:05:56 -0700153
154 fSrcX.reset(xCount + 2);
155 fDstX.reset(xCount + 2);
156 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountScalable,
msarett71df2d72016-09-30 12:41:42 -0700157 src.fLeft, src.fRight, dst.fLeft, dst.fRight, xIsScalable);
msarettc573a402016-08-02 08:05:56 -0700158
159 fSrcY.reset(yCount + 2);
160 fDstY.reset(yCount + 2);
161 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountScalable,
msarett71df2d72016-09-30 12:41:42 -0700162 src.fTop, src.fBottom, dst.fTop, dst.fBottom, yIsScalable);
msarettc573a402016-08-02 08:05:56 -0700163
164 fCurrX = fCurrY = 0;
msarett0764efe2016-09-02 11:24:30 -0700165 fNumRectsInLattice = (xCount + 1) * (yCount + 1);
166 fNumRectsToDraw = fNumRectsInLattice;
167
168 if (lattice.fFlags) {
169 fFlags.push_back_n(fNumRectsInLattice);
170
171 const SkCanvas::Lattice::Flags* flags = lattice.fFlags;
172
173 bool hasPadRow = (yCount != origYCount);
174 bool hasPadCol = (xCount != origXCount);
175 if (hasPadRow) {
176 // The first row of rects are all empty, skip the first row of flags.
177 flags += origXCount + 1;
178 }
179
180 int i = 0;
181 for (int y = 0; y < yCount + 1; y++) {
182 for (int x = 0; x < origXCount + 1; x++) {
183 if (0 == x && hasPadCol) {
184 // The first column of rects are all empty. Skip a rect.
185 flags++;
186 continue;
187 }
188
189 fFlags[i] = *flags;
190 flags++;
191 i++;
192 }
193 }
194
195 for (int j = 0; j < fFlags.count(); j++) {
196 if (SkCanvas::Lattice::kTransparent_Flags == fFlags[j]) {
197 fNumRectsToDraw--;
198 }
199 }
200 }
msarettc573a402016-08-02 08:05:56 -0700201}
202
203bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) {
204 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center);
205}
206
207SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst) {
208 SkASSERT(SkIRect::MakeWH(w, h).contains(c));
209
210 fSrcX.reset(4);
211 fSrcY.reset(4);
212 fDstX.reset(4);
213 fDstY.reset(4);
214
215 fSrcX[0] = 0;
216 fSrcX[1] = SkIntToScalar(c.fLeft);
217 fSrcX[2] = SkIntToScalar(c.fRight);
218 fSrcX[3] = SkIntToScalar(w);
219
220 fSrcY[0] = 0;
221 fSrcY[1] = SkIntToScalar(c.fTop);
222 fSrcY[2] = SkIntToScalar(c.fBottom);
223 fSrcY[3] = SkIntToScalar(h);
224
225 fDstX[0] = dst.fLeft;
226 fDstX[1] = dst.fLeft + SkIntToScalar(c.fLeft);
227 fDstX[2] = dst.fRight - SkIntToScalar(w - c.fRight);
228 fDstX[3] = dst.fRight;
229
230 fDstY[0] = dst.fTop;
231 fDstY[1] = dst.fTop + SkIntToScalar(c.fTop);
232 fDstY[2] = dst.fBottom - SkIntToScalar(h - c.fBottom);
233 fDstY[3] = dst.fBottom;
234
235 if (fDstX[1] > fDstX[2]) {
236 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width());
237 fDstX[2] = fDstX[1];
238 }
239
240 if (fDstY[1] > fDstY[2]) {
241 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height());
242 fDstY[2] = fDstY[1];
243 }
244
245 fCurrX = fCurrY = 0;
msarett0764efe2016-09-02 11:24:30 -0700246 fNumRectsInLattice = 9;
247 fNumRectsToDraw = 9;
msarettc573a402016-08-02 08:05:56 -0700248}
249
250bool SkLatticeIter::next(SkRect* src, SkRect* dst) {
msarett0764efe2016-09-02 11:24:30 -0700251 int currRect = fCurrX + fCurrY * (fSrcX.count() - 1);
252 if (currRect == fNumRectsInLattice) {
msarettc573a402016-08-02 08:05:56 -0700253 return false;
254 }
255
256 const int x = fCurrX;
257 const int y = fCurrY;
258 SkASSERT(x >= 0 && x < fSrcX.count() - 1);
259 SkASSERT(y >= 0 && y < fSrcY.count() - 1);
260
msarettc573a402016-08-02 08:05:56 -0700261 if (fSrcX.count() - 1 == ++fCurrX) {
262 fCurrX = 0;
263 fCurrY += 1;
msarettc573a402016-08-02 08:05:56 -0700264 }
msarett0764efe2016-09-02 11:24:30 -0700265
266 if (fFlags.count() > 0 && SkToBool(SkCanvas::Lattice::kTransparent_Flags & fFlags[currRect])) {
267 return this->next(src, dst);
268 }
269
270 src->set(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]);
271 dst->set(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]);
msarettc573a402016-08-02 08:05:56 -0700272 return true;
273}
msarett10e3d9b2016-08-18 15:46:03 -0700274
275void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) {
276 SkASSERT(matrix.isScaleTranslate());
277 SkScalar tx = matrix.getTranslateX();
278 SkScalar sx = matrix.getScaleX();
279 for (int i = 0; i < fDstX.count(); i++) {
280 fDstX[i] = fDstX[i] * sx + tx;
281 }
282
283 SkScalar ty = matrix.getTranslateY();
284 SkScalar sy = matrix.getScaleY();
285 for (int i = 0; i < fDstY.count(); i++) {
286 fDstY[i] = fDstY[i] * sy + ty;
287 }
288}