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