| 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) { | 15 if (count <= 0) { |
| 16 return false; | 16 return false; |
| 17 } | 17 } |
| 18 | 18 |
| 19 int prev = -1; | 19 int prev = -1; |
| 20 for (int i = 0; i < count; i++) { | 20 for (int i = 0; i < count; i++) { |
| 21 if (prev >= divs[i] || divs[i] > len) { | 21 if (prev >= divs[i] || divs[i] >= len) { |
| 22 return false; | 22 return false; |
| 23 } | 23 } |
| 24 } | 24 } |
| 25 | 25 |
| 26 return true; | 26 return true; |
| 27 } | 27 } |
| 28 | 28 |
| 29 bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattic
e) { | 29 bool SkLatticeIter::Valid(int width, int height, const SkCanvas::Lattice& lattic
e) { |
| 30 return valid_divs(lattice.fXDivs, lattice.fXCount, width) && | 30 return valid_divs(lattice.fXDivs, lattice.fXCount, width) && |
| 31 valid_divs(lattice.fYDivs, lattice.fYCount, height); | 31 valid_divs(lattice.fYDivs, lattice.fYCount, height); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 97 } |
| 98 | 98 |
| 99 src[divCount + 1] = (float) srcLen; | 99 src[divCount + 1] = (float) srcLen; |
| 100 dst[divCount + 1] = dstStop; | 100 dst[divCount + 1] = dstStop; |
| 101 } | 101 } |
| 102 | 102 |
| 103 SkLatticeIter::SkLatticeIter(int srcWidth, int srcHeight, const SkCanvas::Lattic
e& lattice, | 103 SkLatticeIter::SkLatticeIter(int srcWidth, int srcHeight, const SkCanvas::Lattic
e& lattice, |
| 104 const SkRect& dst) | 104 const SkRect& dst) |
| 105 { | 105 { |
| 106 const int* xDivs = lattice.fXDivs; | 106 const int* xDivs = lattice.fXDivs; |
| 107 int xCount = lattice.fXCount; | 107 const int origXCount = lattice.fXCount; |
| 108 const int* yDivs = lattice.fYDivs; | 108 const int* yDivs = lattice.fYDivs; |
| 109 int yCount = lattice.fYCount; | 109 const int origYCount = lattice.fYCount; |
| 110 | 110 |
| 111 // In the x-dimension, the first rectangle always starts at x = 0 and is "sc
alable". | 111 // 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 | 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. | 113 // first real rectangle "scalable" in the x-direction. |
| 114 // | 114 // |
| 115 // The same interpretation applies to the y-dimension. | 115 // The same interpretation applies to the y-dimension. |
| 116 // | 116 // |
| 117 // As we move left to right across the image, alternating patches will be "f
ixed" or | 117 // 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 | 118 // "scalable" in the x-direction. Similarly, as move top to bottom, alterna
ting |
| 119 // patches will be "fixed" or "scalable" in the y-direction. | 119 // patches will be "fixed" or "scalable" in the y-direction. |
| 120 int xCount = origXCount; |
| 121 int yCount = origYCount; |
| 120 SkASSERT(xCount > 0 && yCount > 0); | 122 SkASSERT(xCount > 0 && yCount > 0); |
| 121 bool xIsScalable = (0 == xDivs[0]); | 123 bool xIsScalable = (0 == xDivs[0]); |
| 122 if (xIsScalable) { | 124 if (xIsScalable) { |
| 123 // Once we've decided that the first patch is "scalable", we don't need
the | 125 // 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. | 126 // xDiv. It is always implied that we start at zero. |
| 125 xDivs++; | 127 xDivs++; |
| 126 xCount--; | 128 xCount--; |
| 127 } | 129 } |
| 128 bool yIsScalable = (0 == yDivs[0]); | 130 bool yIsScalable = (0 == yDivs[0]); |
| 129 if (yIsScalable) { | 131 if (yIsScalable) { |
| 130 // Once we've decided that the first patch is "scalable", we don't need
the | 132 // 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. | 133 // yDiv. It is always implied that we start at zero. |
| 132 yDivs++; | 134 yDivs++; |
| 133 yCount--; | 135 yCount--; |
| 134 } | 136 } |
| 135 | 137 |
| 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. | 138 // Count "scalable" and "fixed" pixels in each dimension. |
| 145 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, srcWi
dth); | 139 int xCountScalable = count_scalable_pixels(xDivs, xCount, xIsScalable, srcWi
dth); |
| 146 int xCountFixed = srcWidth - xCountScalable; | 140 int xCountFixed = srcWidth - xCountScalable; |
| 147 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, srcHe
ight); | 141 int yCountScalable = count_scalable_pixels(yDivs, yCount, yIsScalable, srcHe
ight); |
| 148 int yCountFixed = srcHeight - yCountScalable; | 142 int yCountFixed = srcHeight - yCountScalable; |
| 149 | 143 |
| 150 fSrcX.reset(xCount + 2); | 144 fSrcX.reset(xCount + 2); |
| 151 fDstX.reset(xCount + 2); | 145 fDstX.reset(xCount + 2); |
| 152 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountS
calable, | 146 set_points(fDstX.begin(), fSrcX.begin(), xDivs, xCount, xCountFixed, xCountS
calable, |
| 153 dst.fLeft, dst.fRight, xIsScalable); | 147 dst.fLeft, dst.fRight, xIsScalable); |
| 154 | 148 |
| 155 fSrcY.reset(yCount + 2); | 149 fSrcY.reset(yCount + 2); |
| 156 fDstY.reset(yCount + 2); | 150 fDstY.reset(yCount + 2); |
| 157 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountS
calable, | 151 set_points(fDstY.begin(), fSrcY.begin(), yDivs, yCount, yCountFixed, yCountS
calable, |
| 158 dst.fTop, dst.fBottom, yIsScalable); | 152 dst.fTop, dst.fBottom, yIsScalable); |
| 159 | 153 |
| 160 fCurrX = fCurrY = 0; | 154 fCurrX = fCurrY = 0; |
| 161 fDone = false; | 155 fNumRectsInLattice = (xCount + 1) * (yCount + 1); |
| 162 fNumRects = (xCount + 1) * (yCount + 1); | 156 fNumRectsToDraw = fNumRectsInLattice; |
| 157 |
| 158 if (lattice.fFlags) { |
| 159 fFlags.push_back_n(fNumRectsInLattice); |
| 160 |
| 161 const SkCanvas::Lattice::Flags* flags = lattice.fFlags; |
| 162 if (yCount != origYCount) { |
| 163 // The first row of rects are all empty, skip the first row of flags
. |
| 164 flags += origXCount + 1; |
| 165 } |
| 166 |
| 167 int i = 0; |
| 168 for (int y = 0; y < yCount + 1; y++) { |
| 169 for (int x = 0; x < origXCount + 1; x++) { |
| 170 if (0 == x && xCount != origXCount) { |
| 171 // The first column of rects are all empty. Skip a rect. |
| 172 flags++; |
| 173 continue; |
| 174 } |
| 175 |
| 176 fFlags[i] = *flags; |
| 177 flags++; |
| 178 i++; |
| 179 } |
| 180 } |
| 181 |
| 182 for (int j = 0; j < fFlags.count(); j++) { |
| 183 if (SkCanvas::Lattice::kTransparent_Flags == fFlags[j]) { |
| 184 fNumRectsToDraw--; |
| 185 } |
| 186 } |
| 187 } |
| 163 } | 188 } |
| 164 | 189 |
| 165 bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) { | 190 bool SkLatticeIter::Valid(int width, int height, const SkIRect& center) { |
| 166 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center); | 191 return !center.isEmpty() && SkIRect::MakeWH(width, height).contains(center); |
| 167 } | 192 } |
| 168 | 193 |
| 169 SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst)
{ | 194 SkLatticeIter::SkLatticeIter(int w, int h, const SkIRect& c, const SkRect& dst)
{ |
| 170 SkASSERT(SkIRect::MakeWH(w, h).contains(c)); | 195 SkASSERT(SkIRect::MakeWH(w, h).contains(c)); |
| 171 | 196 |
| 172 fSrcX.reset(4); | 197 fSrcX.reset(4); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 198 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width()); | 223 fDstX[1] = fDstX[0] + (fDstX[3] - fDstX[0]) * c.fLeft / (w - c.width()); |
| 199 fDstX[2] = fDstX[1]; | 224 fDstX[2] = fDstX[1]; |
| 200 } | 225 } |
| 201 | 226 |
| 202 if (fDstY[1] > fDstY[2]) { | 227 if (fDstY[1] > fDstY[2]) { |
| 203 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height()); | 228 fDstY[1] = fDstY[0] + (fDstY[3] - fDstY[0]) * c.fTop / (h - c.height()); |
| 204 fDstY[2] = fDstY[1]; | 229 fDstY[2] = fDstY[1]; |
| 205 } | 230 } |
| 206 | 231 |
| 207 fCurrX = fCurrY = 0; | 232 fCurrX = fCurrY = 0; |
| 208 fDone = false; | 233 fNumRectsInLattice = 9; |
| 209 fNumRects = 9; | 234 fNumRectsToDraw = 9; |
| 210 } | 235 } |
| 211 | 236 |
| 212 bool SkLatticeIter::next(SkRect* src, SkRect* dst) { | 237 bool SkLatticeIter::next(SkRect* src, SkRect* dst) { |
| 213 if (fDone) { | 238 int currRect = fCurrX + fCurrY * (fSrcX.count() - 1); |
| 239 if (currRect == fNumRectsInLattice) { |
| 214 return false; | 240 return false; |
| 215 } | 241 } |
| 216 | 242 |
| 217 const int x = fCurrX; | 243 const int x = fCurrX; |
| 218 const int y = fCurrY; | 244 const int y = fCurrY; |
| 219 SkASSERT(x >= 0 && x < fSrcX.count() - 1); | 245 SkASSERT(x >= 0 && x < fSrcX.count() - 1); |
| 220 SkASSERT(y >= 0 && y < fSrcY.count() - 1); | 246 SkASSERT(y >= 0 && y < fSrcY.count() - 1); |
| 221 | 247 |
| 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) { | 248 if (fSrcX.count() - 1 == ++fCurrX) { |
| 225 fCurrX = 0; | 249 fCurrX = 0; |
| 226 fCurrY += 1; | 250 fCurrY += 1; |
| 227 if (fCurrY >= fSrcY.count() - 1) { | |
| 228 fDone = true; | |
| 229 } | |
| 230 } | 251 } |
| 252 |
| 253 if (fFlags.count() > 0 && SkToBool(SkCanvas::Lattice::kTransparent_Flags & f
Flags[currRect])) { |
| 254 return this->next(src, dst); |
| 255 } |
| 256 |
| 257 src->set(fSrcX[x], fSrcY[y], fSrcX[x + 1], fSrcY[y + 1]); |
| 258 dst->set(fDstX[x], fDstY[y], fDstX[x + 1], fDstY[y + 1]); |
| 231 return true; | 259 return true; |
| 232 } | 260 } |
| 233 | 261 |
| 234 void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) { | 262 void SkLatticeIter::mapDstScaleTranslate(const SkMatrix& matrix) { |
| 235 SkASSERT(matrix.isScaleTranslate()); | 263 SkASSERT(matrix.isScaleTranslate()); |
| 236 SkScalar tx = matrix.getTranslateX(); | 264 SkScalar tx = matrix.getTranslateX(); |
| 237 SkScalar sx = matrix.getScaleX(); | 265 SkScalar sx = matrix.getScaleX(); |
| 238 for (int i = 0; i < fDstX.count(); i++) { | 266 for (int i = 0; i < fDstX.count(); i++) { |
| 239 fDstX[i] = fDstX[i] * sx + tx; | 267 fDstX[i] = fDstX[i] * sx + tx; |
| 240 } | 268 } |
| 241 | 269 |
| 242 SkScalar ty = matrix.getTranslateY(); | 270 SkScalar ty = matrix.getTranslateY(); |
| 243 SkScalar sy = matrix.getScaleY(); | 271 SkScalar sy = matrix.getScaleY(); |
| 244 for (int i = 0; i < fDstY.count(); i++) { | 272 for (int i = 0; i < fDstY.count(); i++) { |
| 245 fDstY[i] = fDstY[i] * sy + ty; | 273 fDstY[i] = fDstY[i] * sy + ty; |
| 246 } | 274 } |
| 247 } | 275 } |
| OLD | NEW |