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