Line data Source code
1 : /*
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 : */
14 0 : static bool valid_divs(const int* divs, int count, int start, int end) {
15 0 : int prev = start - 1;
16 0 : for (int i = 0; i < count; i++) {
17 0 : if (prev >= divs[i] || divs[i] >= end) {
18 0 : return false;
19 : }
20 : }
21 :
22 0 : return true;
23 : }
24 :
25 0 : bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattice) {
26 0 : SkIRect totalBounds = SkIRect::MakeWH(width, height);
27 0 : SkASSERT(lattice.fBounds);
28 0 : const SkIRect latticeBounds = *lattice.fBounds;
29 0 : if (!totalBounds.contains(latticeBounds)) {
30 0 : return false;
31 : }
32 :
33 0 : bool zeroXDivs = lattice.fXCount <= 0 || (1 == lattice.fXCount &&
34 0 : latticeBounds.fLeft == lattice.fXDivs[0]);
35 0 : bool zeroYDivs = lattice.fYCount <= 0 || (1 == lattice.fYCount &&
36 0 : latticeBounds.fTop == lattice.fYDivs[0]);
37 0 : if (zeroXDivs && zeroYDivs) {
38 0 : return false;
39 : }
40 :
41 0 : return valid_divs(lattice.fXDivs, lattice.fXCount, latticeBounds.fLeft, latticeBounds.fRight)
42 0 : && valid_divs(lattice.fYDivs, lattice.fYCount, latticeBounds.fTop, latticeBounds.fBottom);
43 : }
44 :
45 : /**
46 : * Count the number of pixels that are in "scalable" patches.
47 : */
48 0 : static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsScalable,
49 : int start, int end) {
50 0 : if (0 == numDivs) {
51 0 : return firstIsScalable ? end - start : 0;
52 : }
53 :
54 : int i;
55 : int count;
56 0 : if (firstIsScalable) {
57 0 : count = divs[0] - start;
58 0 : i = 1;
59 : } else {
60 0 : count = 0;
61 0 : i = 0;
62 : }
63 :
64 0 : for (; i < numDivs; i += 2) {
65 : // Alternatively, we could use |top| and |bottom| as variable names, instead of
66 : // |left| and |right|.
67 0 : int left = divs[i];
68 0 : int right = (i + 1 < numDivs) ? divs[i + 1] : end;
69 0 : count += right - left;
70 : }
71 :
72 0 : return count;
73 : }
74 :
75 : /**
76 : * Set points for the src and dst rects on subsequent draw calls.
77 : */
78 0 : static void set_points(float* dst, float* src, const int* divs, int divCount, int srcFixed,
79 : int srcScalable, float srcStart, float srcEnd, float dstStart, float dstEnd,
80 : bool isScalable) {
81 :
82 0 : float dstLen = dstEnd - dstStart;
83 : float scale;
84 0 : if (srcFixed <= dstLen) {
85 : // This is the "normal" case, where we scale the "scalable" patches and leave
86 : // the other patches fixed.
87 0 : scale = (dstLen - ((float) srcFixed)) / ((float) srcScalable);
88 : } else {
89 : // In this case, we eliminate the "scalable" patches and scale the "fixed" patches.
90 0 : scale = dstLen / ((float) srcFixed);
91 : }
92 :
93 0 : src[0] = srcStart;
94 0 : dst[0] = dstStart;
95 0 : for (int i = 0; i < divCount; i++) {
96 0 : src[i + 1] = (float) (divs[i]);
97 0 : float srcDelta = src[i + 1] - src[i];
98 : float dstDelta;
99 0 : if (srcFixed <= dstLen) {
100 0 : dstDelta = isScalable ? scale * srcDelta : srcDelta;
101 : } else {
102 0 : dstDelta = isScalable ? 0.0f : scale * srcDelta;
103 : }
104 0 : dst[i + 1] = dst[i] + dstDelta;
105 :
106 : // Alternate between "scalable" and "fixed" patches.
107 0 : isScalable = !isScalable;
108 : }
109 :
110 0 : src[divCount + 1] = srcEnd;
111 0 : dst[divCount + 1] = dstEnd;
112 0 : }
113 :
114 0 : SkLatticeIter::SkLatticeIter(const SkCanvas::Lattice& lattice, const SkRect& dst) {
115 0 : const int* xDivs = lattice.fXDivs;
116 0 : const int origXCount = lattice.fXCount;
117 0 : const int* yDivs = lattice.fYDivs;
118 0 : const int origYCount = lattice.fYCount;
119 0 : SkASSERT(lattice.fBounds);
120 0 : const SkIRect src = *lattice.fBounds;
121 :
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.
131 0 : int xCount = origXCount;
132 0 : int yCount = origYCount;
133 0 : bool xIsScalable = (xCount > 0 && src.fLeft == xDivs[0]);
134 0 : if (xIsScalable) {
135 : // Once we've decided that the first patch is "scalable", we don't need the
136 : // xDiv. It is always implied that we start at the edge of the bounds.
137 0 : xDivs++;
138 0 : xCount--;
139 : }
140 0 : bool yIsScalable = (yCount > 0 && src.fTop == yDivs[0]);
141 0 : if (yIsScalable) {
142 : // Once we've decided that the first patch is "scalable", we don't need the
143 : // yDiv. It is always implied that we start at the edge of the bounds.
144 0 : yDivs++;
145 0 : yCount--;
146 : }
147 :
148 : // Count "scalable" and "fixed" pixels in each dimension.
149 0 : int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, src.fLeft, src.fRight);
150 0 : int xCountFixed = src.width() - xCountScalable;
151 0 : int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, src.fTop, src.fBottom);
152 0 : int yCountFixed = src.height() - yCountScalable;
153 :
154 0 : fSrcX.reset(xCount + 2);
155 0 : fDstX.reset(xCount + 2);
156 0 : set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountScalable,
157 0 : src.fLeft, src.fRight, dst.fLeft, dst.fRight, xIsScalable);
158 :
159 0 : fSrcY.reset(yCount + 2);
160 0 : fDstY.reset(yCount + 2);
161 0 : set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountScalable,
162 0 : src.fTop, src.fBottom, dst.fTop, dst.fBottom, yIsScalable);
163 :
164 0 : fCurrX = fCurrY = 0;
165 0 : fNumRectsInLattice = (xCount + 1) * (yCount + 1);
166 0 : fNumRectsToDraw = fNumRectsInLattice;
167 :
168 0 : if (lattice.fFlags) {
169 0 : fFlags.push_back_n(fNumRectsInLattice);
170 :
171 0 : const SkCanvas::Lattice::Flags* flags = lattice.fFlags;
172 :
173 0 : bool hasPadRow = (yCount != origYCount);
174 0 : bool hasPadCol = (xCount != origXCount);
175 0 : if (hasPadRow) {
176 : // The first row of rects are all empty, skip the first row of flags.
177 0 : flags += origXCount + 1;
178 : }
179 :
180 0 : int i = 0;
181 0 : for (int y = 0; y < yCount + 1; y++) {
182 0 : for (int x = 0; x < origXCount + 1; x++) {
183 0 : if (0 == x && hasPadCol) {
184 : // The first column of rects are all empty. Skip a rect.
185 0 : flags++;
186 0 : continue;
187 : }
188 :
189 0 : fFlags[i] = *flags;
190 0 : flags++;
191 0 : i++;
192 : }
193 : }
194 :
195 0 : for (int j = 0; j < fFlags.count(); j++) {
196 0 : if (SkCanvas::Lattice::kTransparent_Flags == fFlags[j]) {
197 0 : fNumRectsToDraw--;
198 : }
199 : }
200 : }
201 0 : }
202 :
203 0 : bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) {
204 0 : return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center);
205 : }
206 :
207 0 : SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst) {
208 0 : SkASSERT(SkIRect::MakeWH(w, h).contains(c));
209 :
210 0 : fSrcX.reset(4);
211 0 : fSrcY.reset(4);
212 0 : fDstX.reset(4);
213 0 : fDstY.reset(4);
214 :
215 0 : fSrcX[0] = 0;
216 0 : fSrcX[1] = SkIntToScalar(c.fLeft);
217 0 : fSrcX[2] = SkIntToScalar(c.fRight);
218 0 : fSrcX[3] = SkIntToScalar(w);
219 :
220 0 : fSrcY[0] = 0;
221 0 : fSrcY[1] = SkIntToScalar(c.fTop);
222 0 : fSrcY[2] = SkIntToScalar(c.fBottom);
223 0 : fSrcY[3] = SkIntToScalar(h);
224 :
225 0 : fDstX[0] = dst.fLeft;
226 0 : fDstX[1] = dst.fLeft + SkIntToScalar(c.fLeft);
227 0 : fDstX[2] = dst.fRight - SkIntToScalar(w - c.fRight);
228 0 : fDstX[3] = dst.fRight;
229 :
230 0 : fDstY[0] = dst.fTop;
231 0 : fDstY[1] = dst.fTop + SkIntToScalar(c.fTop);
232 0 : fDstY[2] = dst.fBottom - SkIntToScalar(h - c.fBottom);
233 0 : fDstY[3] = dst.fBottom;
234 :
235 0 : if (fDstX[1] > fDstX[2]) {
236 0 : fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width());
237 0 : fDstX[2] = fDstX[1];
238 : }
239 :
240 0 : if (fDstY[1] > fDstY[2]) {
241 0 : fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height());
242 0 : fDstY[2] = fDstY[1];
243 : }
244 :
245 0 : fCurrX = fCurrY = 0;
246 0 : fNumRectsInLattice = 9;
247 0 : fNumRectsToDraw = 9;
248 0 : }
249 :
250 0 : bool SkLatticeIter::next(SkRect* src, SkRect* dst) {
251 0 : int currRect = fCurrX + fCurrY * (fSrcX.count() - 1);
252 0 : if (currRect == fNumRectsInLattice) {
253 0 : return false;
254 : }
255 :
256 0 : const int x = fCurrX;
257 0 : const int y = fCurrY;
258 0 : SkASSERT(x >= 0 && x < fSrcX.count() - 1);
259 0 : SkASSERT(y >= 0 && y < fSrcY.count() - 1);
260 :
261 0 : if (fSrcX.count() - 1 == ++fCurrX) {
262 0 : fCurrX = 0;
263 0 : fCurrY += 1;
264 : }
265 :
266 0 : if (fFlags.count() > 0 && SkToBool(SkCanvas::Lattice::kTransparent_Flags & fFlags[currRect])) {
267 0 : return this->next(src, dst);
268 : }
269 :
270 0 : src->set(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]);
271 0 : dst->set(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]);
272 0 : return true;
273 : }
274 :
275 0 : void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) {
276 0 : SkASSERT(matrix.isScaleTranslate());
277 0 : SkScalar tx = matrix.getTranslateX();
278 0 : SkScalar sx = matrix.getScaleX();
279 0 : for (int i = 0; i < fDstX.count(); i++) {
280 0 : fDstX[i] = fDstX[i] * sx + tx;
281 : }
282 :
283 0 : SkScalar ty = matrix.getTranslateY();
284 0 : SkScalar sy = matrix.getScaleY();
285 0 : for (int i = 0; i < fDstY.count(); i++) {
286 0 : fDstY[i] = fDstY[i] * sy + ty;
287 : }
288 0 : }
|