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