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