Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include "SkNinePatch.h" |
michael@0 | 11 | #include "SkCanvas.h" |
michael@0 | 12 | #include "SkShader.h" |
michael@0 | 13 | |
michael@0 | 14 | static const uint16_t g3x3Indices[] = { |
michael@0 | 15 | 0, 5, 1, 0, 4, 5, |
michael@0 | 16 | 1, 6, 2, 1, 5, 6, |
michael@0 | 17 | 2, 7, 3, 2, 6, 7, |
michael@0 | 18 | |
michael@0 | 19 | 4, 9, 5, 4, 8, 9, |
michael@0 | 20 | 5, 10, 6, 5, 9, 10, |
michael@0 | 21 | 6, 11, 7, 6, 10, 11, |
michael@0 | 22 | |
michael@0 | 23 | 8, 13, 9, 8, 12, 13, |
michael@0 | 24 | 9, 14, 10, 9, 13, 14, |
michael@0 | 25 | 10, 15, 11, 10, 14, 15 |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | static int fillIndices(uint16_t indices[], int xCount, int yCount) { |
michael@0 | 29 | uint16_t* startIndices = indices; |
michael@0 | 30 | |
michael@0 | 31 | int n = 0; |
michael@0 | 32 | for (int y = 0; y < yCount; y++) { |
michael@0 | 33 | for (int x = 0; x < xCount; x++) { |
michael@0 | 34 | *indices++ = n; |
michael@0 | 35 | *indices++ = n + xCount + 2; |
michael@0 | 36 | *indices++ = n + 1; |
michael@0 | 37 | |
michael@0 | 38 | *indices++ = n; |
michael@0 | 39 | *indices++ = n + xCount + 1; |
michael@0 | 40 | *indices++ = n + xCount + 2; |
michael@0 | 41 | |
michael@0 | 42 | n += 1; |
michael@0 | 43 | } |
michael@0 | 44 | n += 1; |
michael@0 | 45 | } |
michael@0 | 46 | return static_cast<int>(indices - startIndices); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | // Computes the delta between vertices along a single axis |
michael@0 | 50 | static SkScalar computeVertexDelta(bool isStretchyVertex, |
michael@0 | 51 | SkScalar currentVertex, |
michael@0 | 52 | SkScalar prevVertex, |
michael@0 | 53 | SkScalar stretchFactor) { |
michael@0 | 54 | // the standard delta between vertices if no stretching is required |
michael@0 | 55 | SkScalar delta = currentVertex - prevVertex; |
michael@0 | 56 | |
michael@0 | 57 | // if the stretch factor is negative or zero we need to shrink the 9-patch |
michael@0 | 58 | // to fit within the target bounds. This means that we will eliminate all |
michael@0 | 59 | // stretchy areas and scale the fixed areas to fit within the target bounds. |
michael@0 | 60 | if (stretchFactor <= 0) { |
michael@0 | 61 | if (isStretchyVertex) |
michael@0 | 62 | delta = 0; // collapse stretchable areas |
michael@0 | 63 | else |
michael@0 | 64 | delta = SkScalarMul(delta, -stretchFactor); // scale fixed areas |
michael@0 | 65 | // if the stretch factor is positive then we use the standard delta for |
michael@0 | 66 | // fixed and scale the stretchable areas to fill the target bounds. |
michael@0 | 67 | } else if (isStretchyVertex) { |
michael@0 | 68 | delta = SkScalarMul(delta, stretchFactor); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | return delta; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | static void fillRow(SkPoint verts[], SkPoint texs[], |
michael@0 | 75 | const SkScalar vy, const SkScalar ty, |
michael@0 | 76 | const SkRect& bounds, const int32_t xDivs[], int numXDivs, |
michael@0 | 77 | const SkScalar stretchX, int width) { |
michael@0 | 78 | SkScalar vx = bounds.fLeft; |
michael@0 | 79 | verts->set(vx, vy); verts++; |
michael@0 | 80 | texs->set(0, ty); texs++; |
michael@0 | 81 | |
michael@0 | 82 | SkScalar prev = 0; |
michael@0 | 83 | for (int x = 0; x < numXDivs; x++) { |
michael@0 | 84 | |
michael@0 | 85 | const SkScalar tx = SkIntToScalar(xDivs[x]); |
michael@0 | 86 | vx += computeVertexDelta(x & 1, tx, prev, stretchX); |
michael@0 | 87 | prev = tx; |
michael@0 | 88 | |
michael@0 | 89 | verts->set(vx, vy); verts++; |
michael@0 | 90 | texs->set(tx, ty); texs++; |
michael@0 | 91 | } |
michael@0 | 92 | verts->set(bounds.fRight, vy); verts++; |
michael@0 | 93 | texs->set(SkIntToScalar(width), ty); texs++; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | struct Mesh { |
michael@0 | 97 | const SkPoint* fVerts; |
michael@0 | 98 | const SkPoint* fTexs; |
michael@0 | 99 | const SkColor* fColors; |
michael@0 | 100 | const uint16_t* fIndices; |
michael@0 | 101 | }; |
michael@0 | 102 | |
michael@0 | 103 | void SkNinePatch::DrawMesh(SkCanvas* canvas, const SkRect& bounds, |
michael@0 | 104 | const SkBitmap& bitmap, |
michael@0 | 105 | const int32_t xDivs[], int numXDivs, |
michael@0 | 106 | const int32_t yDivs[], int numYDivs, |
michael@0 | 107 | const SkPaint* paint) { |
michael@0 | 108 | if (bounds.isEmpty() || bitmap.width() == 0 || bitmap.height() == 0) { |
michael@0 | 109 | return; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // should try a quick-reject test before calling lockPixels |
michael@0 | 113 | SkAutoLockPixels alp(bitmap); |
michael@0 | 114 | // after the lock, it is valid to check |
michael@0 | 115 | if (!bitmap.readyToDraw()) { |
michael@0 | 116 | return; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // check for degenerate divs (just an optimization, not required) |
michael@0 | 120 | { |
michael@0 | 121 | int i; |
michael@0 | 122 | int zeros = 0; |
michael@0 | 123 | for (i = 0; i < numYDivs && yDivs[i] == 0; i++) { |
michael@0 | 124 | zeros += 1; |
michael@0 | 125 | } |
michael@0 | 126 | numYDivs -= zeros; |
michael@0 | 127 | yDivs += zeros; |
michael@0 | 128 | for (i = numYDivs - 1; i >= 0 && yDivs[i] == bitmap.height(); --i) { |
michael@0 | 129 | numYDivs -= 1; |
michael@0 | 130 | } |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | Mesh mesh; |
michael@0 | 134 | |
michael@0 | 135 | const int numXStretch = (numXDivs + 1) >> 1; |
michael@0 | 136 | const int numYStretch = (numYDivs + 1) >> 1; |
michael@0 | 137 | |
michael@0 | 138 | if (numXStretch < 1 && numYStretch < 1) { |
michael@0 | 139 | canvas->drawBitmapRect(bitmap, NULL, bounds, paint); |
michael@0 | 140 | return; |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | if (false) { |
michael@0 | 144 | int i; |
michael@0 | 145 | for (i = 0; i < numXDivs; i++) { |
michael@0 | 146 | SkDebugf("--- xdivs[%d] %d\n", i, xDivs[i]); |
michael@0 | 147 | } |
michael@0 | 148 | for (i = 0; i < numYDivs; i++) { |
michael@0 | 149 | SkDebugf("--- ydivs[%d] %d\n", i, yDivs[i]); |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | SkScalar stretchX = 0, stretchY = 0; |
michael@0 | 154 | |
michael@0 | 155 | if (numXStretch > 0) { |
michael@0 | 156 | int stretchSize = 0; |
michael@0 | 157 | for (int i = 1; i < numXDivs; i += 2) { |
michael@0 | 158 | stretchSize += xDivs[i] - xDivs[i-1]; |
michael@0 | 159 | } |
michael@0 | 160 | const SkScalar fixed = SkIntToScalar(bitmap.width() - stretchSize); |
michael@0 | 161 | if (bounds.width() >= fixed) |
michael@0 | 162 | stretchX = (bounds.width() - fixed) / stretchSize; |
michael@0 | 163 | else // reuse stretchX, but keep it negative as a signal |
michael@0 | 164 | stretchX = SkScalarDiv(-bounds.width(), fixed); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | if (numYStretch > 0) { |
michael@0 | 168 | int stretchSize = 0; |
michael@0 | 169 | for (int i = 1; i < numYDivs; i += 2) { |
michael@0 | 170 | stretchSize += yDivs[i] - yDivs[i-1]; |
michael@0 | 171 | } |
michael@0 | 172 | const SkScalar fixed = SkIntToScalar(bitmap.height() - stretchSize); |
michael@0 | 173 | if (bounds.height() >= fixed) |
michael@0 | 174 | stretchY = (bounds.height() - fixed) / stretchSize; |
michael@0 | 175 | else // reuse stretchX, but keep it negative as a signal |
michael@0 | 176 | stretchY = SkScalarDiv(-bounds.height(), fixed); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | #if 0 |
michael@0 | 180 | SkDebugf("---- drawasamesh [%d %d] -> [%g %g] <%d %d> (%g %g)\n", |
michael@0 | 181 | bitmap.width(), bitmap.height(), |
michael@0 | 182 | SkScalarToFloat(bounds.width()), SkScalarToFloat(bounds.height()), |
michael@0 | 183 | numXDivs + 1, numYDivs + 1, |
michael@0 | 184 | SkScalarToFloat(stretchX), SkScalarToFloat(stretchY)); |
michael@0 | 185 | #endif |
michael@0 | 186 | |
michael@0 | 187 | const int vCount = (numXDivs + 2) * (numYDivs + 2); |
michael@0 | 188 | // number of celss * 2 (tris per cell) * 3 (verts per tri) |
michael@0 | 189 | const int indexCount = (numXDivs + 1) * (numYDivs + 1) * 2 * 3; |
michael@0 | 190 | // allocate 2 times, one for verts, one for texs, plus indices |
michael@0 | 191 | SkAutoMalloc storage(vCount * sizeof(SkPoint) * 2 + |
michael@0 | 192 | indexCount * sizeof(uint16_t)); |
michael@0 | 193 | SkPoint* verts = (SkPoint*)storage.get(); |
michael@0 | 194 | SkPoint* texs = verts + vCount; |
michael@0 | 195 | uint16_t* indices = (uint16_t*)(texs + vCount); |
michael@0 | 196 | |
michael@0 | 197 | mesh.fVerts = verts; |
michael@0 | 198 | mesh.fTexs = texs; |
michael@0 | 199 | mesh.fColors = NULL; |
michael@0 | 200 | mesh.fIndices = NULL; |
michael@0 | 201 | |
michael@0 | 202 | // we use <= for YDivs, since the prebuild indices work for 3x2 and 3x1 too |
michael@0 | 203 | if (numXDivs == 2 && numYDivs <= 2) { |
michael@0 | 204 | mesh.fIndices = g3x3Indices; |
michael@0 | 205 | } else { |
michael@0 | 206 | SkDEBUGCODE(int n =) fillIndices(indices, numXDivs + 1, numYDivs + 1); |
michael@0 | 207 | SkASSERT(n == indexCount); |
michael@0 | 208 | mesh.fIndices = indices; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | SkScalar vy = bounds.fTop; |
michael@0 | 212 | fillRow(verts, texs, vy, 0, bounds, xDivs, numXDivs, |
michael@0 | 213 | stretchX, bitmap.width()); |
michael@0 | 214 | verts += numXDivs + 2; |
michael@0 | 215 | texs += numXDivs + 2; |
michael@0 | 216 | for (int y = 0; y < numYDivs; y++) { |
michael@0 | 217 | const SkScalar ty = SkIntToScalar(yDivs[y]); |
michael@0 | 218 | if (stretchY >= 0) { |
michael@0 | 219 | if (y & 1) { |
michael@0 | 220 | vy += stretchY; |
michael@0 | 221 | } else { |
michael@0 | 222 | vy += ty; |
michael@0 | 223 | } |
michael@0 | 224 | } else { // shrink fixed sections, and collaps stretchy sections |
michael@0 | 225 | if (y & 1) { |
michael@0 | 226 | ;// do nothing |
michael@0 | 227 | } else { |
michael@0 | 228 | vy += SkScalarMul(ty, -stretchY); |
michael@0 | 229 | } |
michael@0 | 230 | } |
michael@0 | 231 | fillRow(verts, texs, vy, ty, bounds, xDivs, numXDivs, |
michael@0 | 232 | stretchX, bitmap.width()); |
michael@0 | 233 | verts += numXDivs + 2; |
michael@0 | 234 | texs += numXDivs + 2; |
michael@0 | 235 | } |
michael@0 | 236 | fillRow(verts, texs, bounds.fBottom, SkIntToScalar(bitmap.height()), |
michael@0 | 237 | bounds, xDivs, numXDivs, stretchX, bitmap.width()); |
michael@0 | 238 | |
michael@0 | 239 | SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
michael@0 | 240 | SkShader::kClamp_TileMode, |
michael@0 | 241 | SkShader::kClamp_TileMode); |
michael@0 | 242 | SkPaint p; |
michael@0 | 243 | if (paint) { |
michael@0 | 244 | p = *paint; |
michael@0 | 245 | } |
michael@0 | 246 | p.setShader(shader)->unref(); |
michael@0 | 247 | canvas->drawVertices(SkCanvas::kTriangles_VertexMode, vCount, |
michael@0 | 248 | mesh.fVerts, mesh.fTexs, mesh.fColors, NULL, |
michael@0 | 249 | mesh.fIndices, indexCount, p); |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 253 | |
michael@0 | 254 | static void drawNineViaRects(SkCanvas* canvas, const SkRect& dst, |
michael@0 | 255 | const SkBitmap& bitmap, const SkIRect& margins, |
michael@0 | 256 | const SkPaint* paint) { |
michael@0 | 257 | const int32_t srcX[4] = { |
michael@0 | 258 | 0, margins.fLeft, bitmap.width() - margins.fRight, bitmap.width() |
michael@0 | 259 | }; |
michael@0 | 260 | const int32_t srcY[4] = { |
michael@0 | 261 | 0, margins.fTop, bitmap.height() - margins.fBottom, bitmap.height() |
michael@0 | 262 | }; |
michael@0 | 263 | SkScalar dstX[4] = { |
michael@0 | 264 | dst.fLeft, dst.fLeft + SkIntToScalar(margins.fLeft), |
michael@0 | 265 | dst.fRight - SkIntToScalar(margins.fRight), dst.fRight |
michael@0 | 266 | }; |
michael@0 | 267 | SkScalar dstY[4] = { |
michael@0 | 268 | dst.fTop, dst.fTop + SkIntToScalar(margins.fTop), |
michael@0 | 269 | dst.fBottom - SkIntToScalar(margins.fBottom), dst.fBottom |
michael@0 | 270 | }; |
michael@0 | 271 | |
michael@0 | 272 | if (dstX[1] > dstX[2]) { |
michael@0 | 273 | dstX[1] = dstX[0] + (dstX[3] - dstX[0]) * SkIntToScalar(margins.fLeft) / |
michael@0 | 274 | (SkIntToScalar(margins.fLeft) + SkIntToScalar(margins.fRight)); |
michael@0 | 275 | dstX[2] = dstX[1]; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | if (dstY[1] > dstY[2]) { |
michael@0 | 279 | dstY[1] = dstY[0] + (dstY[3] - dstY[0]) * SkIntToScalar(margins.fTop) / |
michael@0 | 280 | (SkIntToScalar(margins.fTop) + SkIntToScalar(margins.fBottom)); |
michael@0 | 281 | dstY[2] = dstY[1]; |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | SkIRect s; |
michael@0 | 285 | SkRect d; |
michael@0 | 286 | for (int y = 0; y < 3; y++) { |
michael@0 | 287 | s.fTop = srcY[y]; |
michael@0 | 288 | s.fBottom = srcY[y+1]; |
michael@0 | 289 | d.fTop = dstY[y]; |
michael@0 | 290 | d.fBottom = dstY[y+1]; |
michael@0 | 291 | for (int x = 0; x < 3; x++) { |
michael@0 | 292 | s.fLeft = srcX[x]; |
michael@0 | 293 | s.fRight = srcX[x+1]; |
michael@0 | 294 | d.fLeft = dstX[x]; |
michael@0 | 295 | d.fRight = dstX[x+1]; |
michael@0 | 296 | canvas->drawBitmapRect(bitmap, &s, d, paint); |
michael@0 | 297 | } |
michael@0 | 298 | } |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | void SkNinePatch::DrawNine(SkCanvas* canvas, const SkRect& bounds, |
michael@0 | 302 | const SkBitmap& bitmap, const SkIRect& margins, |
michael@0 | 303 | const SkPaint* paint) { |
michael@0 | 304 | /** Our vertices code has numerical precision problems if the transformed |
michael@0 | 305 | coordinates land directly on a 1/2 pixel boundary. To work around that |
michael@0 | 306 | for now, we only take the vertices case if we are in opengl. Also, |
michael@0 | 307 | when not in GL, the vertices impl is slower (more math) than calling |
michael@0 | 308 | the viaRects code. |
michael@0 | 309 | */ |
michael@0 | 310 | if (false /* is our canvas backed by a gpu?*/) { |
michael@0 | 311 | int32_t xDivs[2]; |
michael@0 | 312 | int32_t yDivs[2]; |
michael@0 | 313 | |
michael@0 | 314 | xDivs[0] = margins.fLeft; |
michael@0 | 315 | xDivs[1] = bitmap.width() - margins.fRight; |
michael@0 | 316 | yDivs[0] = margins.fTop; |
michael@0 | 317 | yDivs[1] = bitmap.height() - margins.fBottom; |
michael@0 | 318 | |
michael@0 | 319 | if (xDivs[0] > xDivs[1]) { |
michael@0 | 320 | xDivs[0] = bitmap.width() * margins.fLeft / |
michael@0 | 321 | (margins.fLeft + margins.fRight); |
michael@0 | 322 | xDivs[1] = xDivs[0]; |
michael@0 | 323 | } |
michael@0 | 324 | if (yDivs[0] > yDivs[1]) { |
michael@0 | 325 | yDivs[0] = bitmap.height() * margins.fTop / |
michael@0 | 326 | (margins.fTop + margins.fBottom); |
michael@0 | 327 | yDivs[1] = yDivs[0]; |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | SkNinePatch::DrawMesh(canvas, bounds, bitmap, |
michael@0 | 331 | xDivs, 2, yDivs, 2, paint); |
michael@0 | 332 | } else { |
michael@0 | 333 | drawNineViaRects(canvas, bounds, bitmap, margins, paint); |
michael@0 | 334 | } |
michael@0 | 335 | } |