commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "SkPictureShader.h" |
| 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkBitmapProcShader.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkMatrixUtils.h" |
| 14 | #include "SkPicture.h" |
| 15 | #include "SkReadBuffer.h" |
| 16 | |
| 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrContext.h" |
| 19 | #endif |
| 20 | |
robertphillips | e846499 | 2014-07-14 07:53:26 -0700 | [diff] [blame] | 21 | SkPictureShader::SkPictureShader(const SkPicture* picture, TileMode tmx, TileMode tmy, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 22 | const SkMatrix* localMatrix, const SkRect* tile) |
commit-bot@chromium.org | 5aacfe9 | 2014-05-02 21:23:52 +0000 | [diff] [blame] | 23 | : INHERITED(localMatrix) |
| 24 | , fPicture(SkRef(picture)) |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 25 | , fTmx(tmx) |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 26 | , fTmy(tmy) { |
| 27 | fTile = tile ? *tile : SkRect::MakeWH(SkIntToScalar(picture->width()), |
| 28 | SkIntToScalar(picture->height())); |
| 29 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 30 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 31 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
| 32 | SkPictureShader::SkPictureShader(SkReadBuffer& buffer) : INHERITED(buffer) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 33 | fTmx = static_cast<SkShader::TileMode>(buffer.read32()); |
| 34 | fTmy = static_cast<SkShader::TileMode>(buffer.read32()); |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 35 | buffer.readRect(&fTile); |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 36 | fPicture = SkPicture::CreateFromBuffer(buffer); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 37 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 38 | #endif |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 39 | |
| 40 | SkPictureShader::~SkPictureShader() { |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 41 | fPicture->unref(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 42 | } |
| 43 | |
robertphillips | e846499 | 2014-07-14 07:53:26 -0700 | [diff] [blame] | 44 | SkPictureShader* SkPictureShader::Create(const SkPicture* picture, TileMode tmx, TileMode tmy, |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 45 | const SkMatrix* localMatrix, const SkRect* tile) { |
| 46 | if (!picture || 0 == picture->width() || 0 == picture->height() |
| 47 | || (NULL != tile && tile->isEmpty())) { |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 48 | return NULL; |
| 49 | } |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 50 | return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy, localMatrix, tile)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 51 | } |
| 52 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 53 | SkFlattenable* SkPictureShader::CreateProc(SkReadBuffer& buffer) { |
| 54 | SkMatrix lm; |
| 55 | buffer.readMatrix(&lm); |
| 56 | TileMode mx = (TileMode)buffer.read32(); |
| 57 | TileMode my = (TileMode)buffer.read32(); |
| 58 | SkRect tile; |
| 59 | buffer.readRect(&tile); |
| 60 | SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromBuffer(buffer)); |
| 61 | return SkPictureShader::Create(picture, mx, my, &lm, &tile); |
| 62 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 63 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame^] | 64 | void SkPictureShader::flatten(SkWriteBuffer& buffer) const { |
| 65 | buffer.writeMatrix(this->getLocalMatrix()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 66 | buffer.write32(fTmx); |
| 67 | buffer.write32(fTmy); |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 68 | buffer.writeRect(fTile); |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 69 | fPicture->flatten(buffer); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 70 | } |
| 71 | |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 72 | SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix, const SkMatrix* localM) const { |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 73 | SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 74 | |
| 75 | SkMatrix m; |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame] | 76 | m.setConcat(matrix, this->getLocalMatrix()); |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 77 | if (localM) { |
| 78 | m.preConcat(*localM); |
| 79 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 80 | |
| 81 | // Use a rotation-invariant scale |
| 82 | SkPoint scale; |
| 83 | if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { |
| 84 | // Decomposition failed, use an approximation. |
| 85 | scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()), |
| 86 | SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY())); |
| 87 | } |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 88 | SkSize scaledSize = SkSize::Make(scale.x() * fTile.width(), scale.y() * fTile.height()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 89 | |
fmalita | bb204f4 | 2014-08-07 08:39:24 -0700 | [diff] [blame] | 90 | // Clamp the tile size to about 16M pixels |
| 91 | static const SkScalar kMaxTileArea = 4096 * 4096; |
| 92 | SkScalar tileArea = SkScalarMul(scaledSize.width(), scaledSize.height()); |
| 93 | if (tileArea > kMaxTileArea) { |
| 94 | SkScalar clampScale = SkScalarSqrt(SkScalarDiv(kMaxTileArea, tileArea)); |
| 95 | scaledSize.set(SkScalarMul(scaledSize.width(), clampScale), |
| 96 | SkScalarMul(scaledSize.height(), clampScale)); |
| 97 | } |
| 98 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 99 | SkISize tileSize = scaledSize.toRound(); |
| 100 | if (tileSize.isEmpty()) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 101 | return NULL; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 102 | } |
| 103 | |
fmalita | bb204f4 | 2014-08-07 08:39:24 -0700 | [diff] [blame] | 104 | // The actual scale, compensating for rounding & clamping. |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 105 | SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fTile.width(), |
| 106 | SkIntToScalar(tileSize.height()) / fTile.height()); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 108 | SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); |
| 109 | |
scroggo | 1b0aa00 | 2014-07-11 12:19:00 -0700 | [diff] [blame] | 110 | if (!fCachedBitmapShader || tileScale != fCachedTileScale) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 111 | SkBitmap bm; |
| 112 | if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 113 | return NULL; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 114 | } |
| 115 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 116 | |
| 117 | SkCanvas canvas(bm); |
| 118 | canvas.scale(tileScale.width(), tileScale.height()); |
fmalita | b5f7826 | 2014-08-06 13:07:15 -0700 | [diff] [blame] | 119 | canvas.translate(fTile.x(), fTile.y()); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 120 | canvas.drawPicture(fPicture); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 121 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 122 | fCachedTileScale = tileScale; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 123 | |
| 124 | SkMatrix shaderMatrix = this->getLocalMatrix(); |
| 125 | shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 126 | fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 127 | } |
| 128 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 129 | // Increment the ref counter inside the mutex to ensure the returned pointer is still valid. |
| 130 | // Otherwise, the pointer may have been overwritten on a different thread before the object's |
| 131 | // ref count was incremented. |
| 132 | fCachedBitmapShader.get()->ref(); |
| 133 | return fCachedBitmapShader; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 134 | } |
| 135 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 136 | size_t SkPictureShader::contextSize() const { |
| 137 | return sizeof(PictureShaderContext); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 138 | } |
| 139 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 140 | SkShader::Context* SkPictureShader::onCreateContext(const ContextRec& rec, void* storage) const { |
commit-bot@chromium.org | 80116dc | 2014-05-06 17:16:03 +0000 | [diff] [blame] | 141 | SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix, rec.fLocalMatrix)); |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 142 | if (NULL == bitmapShader.get()) { |
| 143 | return NULL; |
| 144 | } |
commit-bot@chromium.org | f03f9ff | 2014-05-06 13:43:17 +0000 | [diff] [blame] | 145 | return PictureShaderContext::Create(storage, *this, rec, bitmapShader); |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | ///////////////////////////////////////////////////////////////////////////////////////// |
| 149 | |
| 150 | SkShader::Context* SkPictureShader::PictureShaderContext::Create(void* storage, |
| 151 | const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) { |
| 152 | PictureShaderContext* ctx = SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, |
| 153 | (shader, rec, bitmapShader)); |
| 154 | if (NULL == ctx->fBitmapShaderContext) { |
| 155 | ctx->~PictureShaderContext(); |
| 156 | ctx = NULL; |
| 157 | } |
| 158 | return ctx; |
| 159 | } |
| 160 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 161 | SkPictureShader::PictureShaderContext::PictureShaderContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 162 | const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) |
| 163 | : INHERITED(shader, rec) |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 164 | , fBitmapShader(SkRef(bitmapShader)) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 165 | { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 166 | fBitmapShaderContextStorage = sk_malloc_throw(bitmapShader->contextSize()); |
| 167 | fBitmapShaderContext = bitmapShader->createContext(rec, fBitmapShaderContextStorage); |
| 168 | //if fBitmapShaderContext is null, we are invalid |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 169 | } |
| 170 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 171 | SkPictureShader::PictureShaderContext::~PictureShaderContext() { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 172 | if (fBitmapShaderContext) { |
| 173 | fBitmapShaderContext->~Context(); |
| 174 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 175 | sk_free(fBitmapShaderContextStorage); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 176 | } |
| 177 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 178 | uint32_t SkPictureShader::PictureShaderContext::getFlags() const { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 179 | SkASSERT(fBitmapShaderContext); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 180 | return fBitmapShaderContext->getFlags(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 181 | } |
| 182 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 183 | SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 184 | SkASSERT(fBitmapShaderContext); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 185 | return fBitmapShaderContext->asAShadeProc(ctx); |
| 186 | } |
| 187 | |
| 188 | void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 189 | SkASSERT(fBitmapShaderContext); |
| 190 | fBitmapShaderContext->shadeSpan(x, y, dstC, count); |
| 191 | } |
| 192 | |
| 193 | void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) { |
| 194 | SkASSERT(fBitmapShaderContext); |
| 195 | fBitmapShaderContext->shadeSpan16(x, y, dstC, count); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | #ifndef SK_IGNORE_TO_STRING |
| 199 | void SkPictureShader::toString(SkString* str) const { |
| 200 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 201 | "clamp", "repeat", "mirror" |
| 202 | }; |
| 203 | |
| 204 | str->appendf("PictureShader: [%d:%d] ", |
| 205 | fPicture ? fPicture->width() : 0, |
| 206 | fPicture ? fPicture->height() : 0); |
| 207 | |
| 208 | str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
| 209 | |
| 210 | this->INHERITED::toString(str); |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | #if SK_SUPPORT_GPU |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 215 | bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 216 | const SkMatrix* localMatrix, GrColor* paintColor, |
| 217 | GrEffect** effect) const { |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 218 | SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix(), localMatrix)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 219 | if (!bitmapShader) { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 220 | return false; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 221 | } |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 222 | return bitmapShader->asNewEffect(context, paint, NULL, paintColor, effect); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 223 | } |
| 224 | #else |
| 225 | bool SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 226 | const SkMatrix* localMatrix, GrColor* paintColor, |
| 227 | GrEffect** effect) const { |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 228 | SkDEBUGFAIL("Should not call in GPU-less build"); |
| 229 | return false; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 230 | } |
| 231 | #endif |