OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkLatticeIter.h" | 8 #include "SkLatticeIter.h" |
9 #include "SkRect.h" | 9 #include "SkRect.h" |
10 | 10 |
11 /** | 11 /** |
12 * Divs must be in increasing order with no duplicates. | 12 * Divs must be in increasing order with no duplicates. |
13 */ | 13 */ |
14 static bool valid_divs(const int* divs, int count, int len) { | 14 static bool valid_divs(const int* divs, int count, int len) { |
15 if (count <= 0) { | |
16 return false; | |
17 } | |
18 | |
19 int prev = -1; | 15 int prev = -1; |
20 for (int i = 0; i < count; i++) { | 16 for (int i = 0; i < count; i++) { |
21 if (prev >= divs[i] || divs[i] > len) { | 17 if (prev >= divs[i] || divs[i] >= len) { |
22 return false; | 18 return false; |
23 } | 19 } |
24 } | 20 } |
25 | 21 |
26 return true; | 22 return true; |
27 } | 23 } |
28 | 24 |
29 bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattic
e) { | 25 bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattic
e) { |
| 26 bool zeroXDivs = lattice.fXCount <= 0 || (1 == lattice.fXCount && 0 == latti
ce.fXDivs[0]); |
| 27 bool zeroYDivs = lattice.fYCount <= 0 || (1 == lattice.fYCount && 0 == latti
ce.fYDivs[0]); |
| 28 if (zeroXDivs && zeroYDivs) { |
| 29 return false; |
| 30 } |
| 31 |
30 return valid_divs(lattice.fXDivs, lattice.fXCount, width) && | 32 return valid_divs(lattice.fXDivs, lattice.fXCount, width) && |
31 valid_divs(lattice.fYDivs, lattice.fYCount, height); | 33 valid_divs(lattice.fYDivs, lattice.fYCount, height); |
32 } | 34 } |
33 | 35 |
34 /** | 36 /** |
35 * Count the number of pixels that are in "scalable" patches. | 37 * Count the number of pixels that are in "scalable" patches. |
36 */ | 38 */ |
37 static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsS
calable, | 39 static int count_scalable_pixels(const int32_t* divs, int numDivs, bool firstIsS
calable, |
38 int length) { | 40 int length) { |
39 if (0 == numDivs) { | 41 if (0 == numDivs) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 } | 99 } |
98 | 100 |
99 src[divCount + 1] = (float) srcLen; | 101 src[divCount + 1] = (float) srcLen; |
100 dst[divCount + 1] = dstStop; | 102 dst[divCount + 1] = dstStop; |
101 } | 103 } |
102 | 104 |
103 SkLatticeIter::SkLatticeIter(int srcWidth, int srcHeight, const SkCanvas::Lattic
e& lattice, | 105 SkLatticeIter::SkLatticeIter(int srcWidth, int srcHeight, const SkCanvas::Lattic
e& lattice, |
104 const SkRect& dst) | 106 const SkRect& dst) |
105 { | 107 { |
106 const int* xDivs = lattice.fXDivs; | 108 const int* xDivs = lattice.fXDivs; |
107 int xCount = lattice.fXCount; | 109 const int origXCount = lattice.fXCount; |
108 const int* yDivs = lattice.fYDivs; | 110 const int* yDivs = lattice.fYDivs; |
109 int yCount = lattice.fYCount; | 111 const int origYCount = lattice.fYCount; |
110 | 112 |
111 // In the x-dimension, the first rectangle always starts at x = 0 and is "sc
alable". | 113 // In the x-dimension, the first rectangle always starts at x = 0 and is "sc
alable". |
112 // If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so
the | 114 // If xDiv[0] is 0, it indicates that the first rectangle is degenerate, so
the |
113 // first real rectangle "scalable" in the x-direction. | 115 // first real rectangle "scalable" in the x-direction. |
114 // | 116 // |
115 // The same interpretation applies to the y-dimension. | 117 // The same interpretation applies to the y-dimension. |
116 // | 118 // |
117 // As we move left to right across the image, alternating patches will be "f
ixed" or | 119 // As we move left to right across the image, alternating patches will be "f
ixed" or |
118 // "scalable" in the x-direction. Similarly, as move top to bottom, alterna
ting | 120 // "scalable" in the x-direction. Similarly, as move top to bottom, alterna
ting |
119 // patches will be "fixed" or "scalable" in the y-direction. | 121 // patches will be "fixed" or "scalable" in the y-direction. |
120 SkASSERT(xCount > 0 && yCount > 0); | 122 int xCount = origXCount; |
121 bool xIsScalable = (0 == xDivs[0]); | 123 int yCount = origYCount; |
| 124 bool xIsScalable = (xCount > 0 && 0 == xDivs[0]); |
122 if (xIsScalable) { | 125 if (xIsScalable) { |
123 // Once we've decided that the first patch is "scalable", we don't need
the | 126 // 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. | 127 // xDiv. It is always implied that we start at zero. |
125 xDivs++; | 128 xDivs++; |
126 xCount--; | 129 xCount--; |
127 } | 130 } |
128 bool yIsScalable = (0 == yDivs[0]); | 131 bool yIsScalable = (yCount > 0 && 0 == yDivs[0]); |
129 if (yIsScalable) { | 132 if (yIsScalable) { |
130 // Once we've decided that the first patch is "scalable", we don't need
the | 133 // 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. | 134 // yDiv. It is always implied that we start at zero. |
132 yDivs++; | 135 yDivs++; |
133 yCount--; | 136 yCount--; |
134 } | 137 } |
135 | 138 |
136 // We never need the final xDiv/yDiv if it is equal to the width/height. Th
is 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. | 139 // Count "scalable" and "fixed" pixels in each dimension. |
145 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, srcWi
dth); | 140 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, srcWi
dth); |
146 int xCountFixed = srcWidth - xCountScalable; | 141 int xCountFixed = srcWidth - xCountScalable; |
147 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, srcHe
ight); | 142 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, srcHe
ight); |
148 int yCountFixed = srcHeight - yCountScalable; | 143 int yCountFixed = srcHeight - yCountScalable; |
149 | 144 |
150 fSrcX.reset(xCount + 2); | 145 fSrcX.reset(xCount + 2); |
151 fDstX.reset(xCount + 2); | 146 fDstX.reset(xCount + 2); |
152 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountS
calable, | 147 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountS
calable, |
153 dst.fLeft, dst.fRight, xIsScalable); | 148 dst.fLeft, dst.fRight, xIsScalable); |
154 | 149 |
155 fSrcY.reset(yCount + 2); | 150 fSrcY.reset(yCount + 2); |
156 fDstY.reset(yCount + 2); | 151 fDstY.reset(yCount + 2); |
157 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountS
calable, | 152 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountS
calable, |
158 dst.fTop, dst.fBottom, yIsScalable); | 153 dst.fTop, dst.fBottom, yIsScalable); |
159 | 154 |
160 fCurrX = fCurrY = 0; | 155 fCurrX = fCurrY = 0; |
161 fDone = false; | 156 fNumRectsInLattice = (xCount + 1) * (yCount + 1); |
162 fNumRects = (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 } |
163 } | 192 } |
164 | 193 |
165 bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) { | 194 bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) { |
166 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center); | 195 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center); |
167 } | 196 } |
168 | 197 |
169 SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst)
{ | 198 SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst)
{ |
170 SkASSERT(SkIRect::MakeWH(w, h).contains(c)); | 199 SkASSERT(SkIRect::MakeWH(w, h).contains(c)); |
171 | 200 |
172 fSrcX.reset(4); | 201 fSrcX.reset(4); |
(...skipping 25 matching lines...) Expand all Loading... |
198 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width()); | 227 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width()); |
199 fDstX[2] = fDstX[1]; | 228 fDstX[2] = fDstX[1]; |
200 } | 229 } |
201 | 230 |
202 if (fDstY[1] > fDstY[2]) { | 231 if (fDstY[1] > fDstY[2]) { |
203 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height()); | 232 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height()); |
204 fDstY[2] = fDstY[1]; | 233 fDstY[2] = fDstY[1]; |
205 } | 234 } |
206 | 235 |
207 fCurrX = fCurrY = 0; | 236 fCurrX = fCurrY = 0; |
208 fDone = false; | 237 fNumRectsInLattice = 9; |
209 fNumRects = 9; | 238 fNumRectsToDraw = 9; |
210 } | 239 } |
211 | 240 |
212 bool SkLatticeIter::next(SkRect* src, SkRect* dst) { | 241 bool SkLatticeIter::next(SkRect* src, SkRect* dst) { |
213 if (fDone) { | 242 int currRect = fCurrX + fCurrY * (fSrcX.count() - 1); |
| 243 if (currRect == fNumRectsInLattice) { |
214 return false; | 244 return false; |
215 } | 245 } |
216 | 246 |
217 const int x = fCurrX; | 247 const int x = fCurrX; |
218 const int y = fCurrY; | 248 const int y = fCurrY; |
219 SkASSERT(x >= 0 && x < fSrcX.count() - 1); | 249 SkASSERT(x >= 0 && x < fSrcX.count() - 1); |
220 SkASSERT(y >= 0 && y < fSrcY.count() - 1); | 250 SkASSERT(y >= 0 && y < fSrcY.count() - 1); |
221 | 251 |
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) { | 252 if (fSrcX.count() - 1 == ++fCurrX) { |
225 fCurrX = 0; | 253 fCurrX = 0; |
226 fCurrY += 1; | 254 fCurrY += 1; |
227 if (fCurrY >= fSrcY.count() - 1) { | |
228 fDone = true; | |
229 } | |
230 } | 255 } |
| 256 |
| 257 if (fFlags.count() > 0 && SkToBool(SkCanvas::Lattice::kTransparent_Flags & f
Flags[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]); |
231 return true; | 263 return true; |
232 } | 264 } |
233 | 265 |
234 void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) { | 266 void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) { |
235 SkASSERT(matrix.isScaleTranslate()); | 267 SkASSERT(matrix.isScaleTranslate()); |
236 SkScalar tx = matrix.getTranslateX(); | 268 SkScalar tx = matrix.getTranslateX(); |
237 SkScalar sx = matrix.getScaleX(); | 269 SkScalar sx = matrix.getScaleX(); |
238 for (int i = 0; i < fDstX.count(); i++) { | 270 for (int i = 0; i < fDstX.count(); i++) { |
239 fDstX[i] = fDstX[i] * sx + tx; | 271 fDstX[i] = fDstX[i] * sx + tx; |
240 } | 272 } |
241 | 273 |
242 SkScalar ty = matrix.getTranslateY(); | 274 SkScalar ty = matrix.getTranslateY(); |
243 SkScalar sy = matrix.getScaleY(); | 275 SkScalar sy = matrix.getScaleY(); |
244 for (int i = 0; i < fDstY.count(); i++) { | 276 for (int i = 0; i < fDstY.count(); i++) { |
245 fDstY[i] = fDstY[i] * sy + ty; | 277 fDstY[i] = fDstY[i] * sy + ty; |
246 } | 278 } |
247 } | 279 } |
OLD | NEW |