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