blob: ba6ac97a5b4395807a22c482b05e95ccc985eb0c [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) {
15 if (count <= 0) {
16 return false;
17 }
18
19 int prev = -1;
20 for (int i = 0; i < count; i++) {
21 if (prev >= divs[i] || divs[i] > len) {
22 return false;
23 }
24 }
25
26 return true;
27}
28
29bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattice) {
30 return valid_divs(lattice.fXDivs, lattice.fXCount, width) &&
31 valid_divs(lattice.fYDivs, lattice.fYCount, height);
32}
33
34/**
35 * Count the number of pixels that are in "scalable" patches.
36 */
37static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsScalable,
38 int length) {
39 if (0 == numDivs) {
40 return firstIsScalable ? length : 0;
41 }
42
43 int i;
44 int count;
45 if (firstIsScalable) {
46 count = divs[0];
47 i = 1;
48 } else {
49 count = 0;
50 i = 0;
51 }
52
53 for (; i < numDivs; i += 2) {
54 // Alternatively, we could use |top| and |bottom| as variable names, instead of
55 // |left| and |right|.
56 int left = divs[i];
57 int right = (i + 1 < numDivs) ? divs[i + 1] : length;
58 count += right - left;
59 }
60
61 return count;
62}
63
64/**
65 * Set points for the src and dst rects on subsequent draw calls.
66 */
67static void set_points(float* dst, float* src, const int* divs, int divCount, int srcFixed,
68 int srcScalable, float dstStart, float dstStop, bool isScalable) {
69
70 float dstLen = dstStop - dstStart;
71 int srcLen = srcFixed + srcScalable;
72 float scale;
73 if (srcFixed <= dstLen) {
74 // This is the "normal" case, where we scale the "scalable" patches and leave
75 // the other patches fixed.
76 scale = (dstLen - ((float) srcFixed)) / ((float) srcScalable);
77 } else {
78 // In this case, we eliminate the "scalable" patches and scale the "fixed" patches.
79 scale = dstLen / ((float) srcFixed);
80 }
81
82 src[0] = 0.0f;
83 dst[0] = dstStart;
84 for (int i = 0; i < divCount; i++) {
85 src[i + 1] = (float) (divs[i]);
86 float srcDelta = src[i + 1] - src[i];
87 float dstDelta;
88 if (srcFixed <= dstLen) {
89 dstDelta = isScalable ? scale * srcDelta : srcDelta;
90 } else {
91 dstDelta = isScalable ? 0.0f : scale * srcDelta;
92 }
93 dst[i + 1] = dst[i] + dstDelta;
94
95 // Alternate between "scalable" and "fixed" patches.
96 isScalable = !isScalable;
97 }
98
99 src[divCount + 1] = (float) srcLen;
100 dst[divCount + 1] = dstStop;
101}
102
103SkLatticeIter::SkLatticeIter(int srcWidth, int srcHeight, const SkCanvas::Lattice& lattice,
104 const SkRect& dst)
105{
106 const int* xDivs = lattice.fXDivs;
107 int xCount = lattice.fXCount;
108 const int* yDivs = lattice.fYDivs;
109 int yCount = lattice.fYCount;
110
111 // In the x-dimension, the first rectangle always starts at x = 0 and is "scalable".
112 // If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so the
113 // first real rectangle "scalable" in the x-direction.
114 //
115 // The same interpretation applies to the y-dimension.
116 //
117 // As we move left to right across the image, alternating patches will be "fixed" or
118 // "scalable" in the x-direction. Similarly, as move top to bottom, alternating
119 // patches will be "fixed" or "scalable" in the y-direction.
120 SkASSERT(xCount > 0 && yCount > 0);
121 bool xIsScalable = (0 == xDivs[0]);
122 if (xIsScalable) {
123 // Once we've decided that the first patch is "scalable", we don't need the
124 // xDiv. It is always implied that we start at zero.
125 xDivs++;
126 xCount--;
127 }
128 bool yIsScalable = (0 == yDivs[0]);
129 if (yIsScalable) {
130 // Once we've decided that the first patch is "scalable", we don't need the
131 // yDiv. It is always implied that we start at zero.
132 yDivs++;
133 yCount--;
134 }
135
136 // We never need the final xDiv/yDiv if it is equal to the width/height. This is implied.
137 if (xCount > 0 && srcWidth == xDivs[xCount - 1]) {
138 xCount--;
139 }
140 if (yCount > 0 && srcHeight == yDivs[yCount - 1]) {
141 yCount--;
142 }
143
144 // Count "scalable" and "fixed" pixels in each dimension.
145 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, srcWidth);
146 int xCountFixed = srcWidth - xCountScalable;
147 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, srcHeight);
148 int yCountFixed = srcHeight - yCountScalable;
149
150 fSrcX.reset(xCount + 2);
151 fDstX.reset(xCount + 2);
152 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountScalable,
153 dst.fLeft, dst.fRight, xIsScalable);
154
155 fSrcY.reset(yCount + 2);
156 fDstY.reset(yCount + 2);
157 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountScalable,
158 dst.fTop, dst.fBottom, yIsScalable);
159
160 fCurrX = fCurrY = 0;
161 fDone = false;
msarett10e3d9b2016-08-18 15:46:03 -0700162 fNumRects = (xCount + 1) * (yCount + 1);
msarettc573a402016-08-02 08:05:56 -0700163}
164
165bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) {
166 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center);
167}
168
169SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst) {
170 SkASSERT(SkIRect::MakeWH(w, h).contains(c));
171
172 fSrcX.reset(4);
173 fSrcY.reset(4);
174 fDstX.reset(4);
175 fDstY.reset(4);
176
177 fSrcX[0] = 0;
178 fSrcX[1] = SkIntToScalar(c.fLeft);
179 fSrcX[2] = SkIntToScalar(c.fRight);
180 fSrcX[3] = SkIntToScalar(w);
181
182 fSrcY[0] = 0;
183 fSrcY[1] = SkIntToScalar(c.fTop);
184 fSrcY[2] = SkIntToScalar(c.fBottom);
185 fSrcY[3] = SkIntToScalar(h);
186
187 fDstX[0] = dst.fLeft;
188 fDstX[1] = dst.fLeft + SkIntToScalar(c.fLeft);
189 fDstX[2] = dst.fRight - SkIntToScalar(w - c.fRight);
190 fDstX[3] = dst.fRight;
191
192 fDstY[0] = dst.fTop;
193 fDstY[1] = dst.fTop + SkIntToScalar(c.fTop);
194 fDstY[2] = dst.fBottom - SkIntToScalar(h - c.fBottom);
195 fDstY[3] = dst.fBottom;
196
197 if (fDstX[1] > fDstX[2]) {
198 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width());
199 fDstX[2] = fDstX[1];
200 }
201
202 if (fDstY[1] > fDstY[2]) {
203 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height());
204 fDstY[2] = fDstY[1];
205 }
206
207 fCurrX = fCurrY = 0;
208 fDone = false;
msarett10e3d9b2016-08-18 15:46:03 -0700209 fNumRects = 9;
msarettc573a402016-08-02 08:05:56 -0700210}
211
212bool SkLatticeIter::next(SkRect* src, SkRect* dst) {
213 if (fDone) {
214 return false;
215 }
216
217 const int x = fCurrX;
218 const int y = fCurrY;
219 SkASSERT(x >= 0 && x < fSrcX.count() - 1);
220 SkASSERT(y >= 0 && y < fSrcY.count() - 1);
221
222 src->set(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]);
223 dst->set(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]);
224 if (fSrcX.count() - 1 == ++fCurrX) {
225 fCurrX = 0;
226 fCurrY += 1;
227 if (fCurrY >= fSrcY.count() - 1) {
228 fDone = true;
229 }
230 }
231 return true;
232}
msarett10e3d9b2016-08-18 15:46:03 -0700233
234void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) {
235 SkASSERT(matrix.isScaleTranslate());
236 SkScalar tx = matrix.getTranslateX();
237 SkScalar sx = matrix.getScaleX();
238 for (int i = 0; i < fDstX.count(); i++) {
239 fDstX[i] = fDstX[i] * sx + tx;
240 }
241
242 SkScalar ty = matrix.getTranslateY();
243 SkScalar sy = matrix.getScaleY();
244 for (int i = 0; i < fDstY.count(); i++) {
245 fDstY[i] = fDstY[i] * sy + ty;
246 }
247}