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 | |
| 21 | SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy) |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 22 | : fPicture(SkRef(picture)) |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 23 | , fTmx(tmx) |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 24 | , fTmy(tmy) { } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 25 | |
| 26 | SkPictureShader::SkPictureShader(SkReadBuffer& buffer) |
| 27 | : INHERITED(buffer) { |
| 28 | fTmx = static_cast<SkShader::TileMode>(buffer.read32()); |
| 29 | fTmy = static_cast<SkShader::TileMode>(buffer.read32()); |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 30 | fPicture = SkPicture::CreateFromBuffer(buffer); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | SkPictureShader::~SkPictureShader() { |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 34 | fPicture->unref(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy) { |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 38 | if (!picture || 0 == picture->width() || 0 == picture->height()) { |
| 39 | return NULL; |
| 40 | } |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 41 | return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy)); |
| 42 | } |
| 43 | |
| 44 | void SkPictureShader::flatten(SkWriteBuffer& buffer) const { |
| 45 | this->INHERITED::flatten(buffer); |
| 46 | |
| 47 | buffer.write32(fTmx); |
| 48 | buffer.write32(fTmy); |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 49 | fPicture->flatten(buffer); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 50 | } |
| 51 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 52 | SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix) const { |
commit-bot@chromium.org | 855e88e | 2014-04-21 19:33:12 +0000 | [diff] [blame] | 53 | SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 54 | |
| 55 | SkMatrix m; |
| 56 | if (this->hasLocalMatrix()) { |
| 57 | m.setConcat(matrix, this->getLocalMatrix()); |
| 58 | } else { |
| 59 | m = matrix; |
| 60 | } |
| 61 | |
| 62 | // Use a rotation-invariant scale |
| 63 | SkPoint scale; |
| 64 | if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { |
| 65 | // Decomposition failed, use an approximation. |
| 66 | scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()), |
| 67 | SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY())); |
| 68 | } |
| 69 | SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height()); |
| 70 | |
| 71 | SkISize tileSize = scaledSize.toRound(); |
| 72 | if (tileSize.isEmpty()) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 73 | return NULL; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // The actual scale, compensating for rounding. |
| 77 | SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(), |
| 78 | SkIntToScalar(tileSize.height()) / fPicture->height()); |
| 79 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 80 | SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); |
| 81 | |
| 82 | if (!fCachedBitmapShader || tileScale != fCachedTileScale || |
| 83 | this->getLocalMatrix() != fCachedLocalMatrix) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 84 | SkBitmap bm; |
| 85 | if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 86 | return NULL; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 87 | } |
| 88 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 89 | |
| 90 | SkCanvas canvas(bm); |
| 91 | canvas.scale(tileScale.width(), tileScale.height()); |
| 92 | canvas.drawPicture(*fPicture); |
| 93 | |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 94 | fCachedTileScale = tileScale; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 95 | fCachedLocalMatrix = this->getLocalMatrix(); |
| 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 | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 109 | SkShader* SkPictureShader::validInternal(const ContextRec& rec, SkMatrix* totalInverse) const { |
| 110 | if (!this->INHERITED::validContext(rec, totalInverse)) { |
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 | |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 114 | SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix)); |
| 115 | if (!bitmapShader || !bitmapShader->validContext(rec)) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 116 | return NULL; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 117 | } |
| 118 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 119 | return bitmapShader.detach(); |
| 120 | } |
| 121 | |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 122 | bool SkPictureShader::validContext(const ContextRec& rec, SkMatrix* totalInverse) const { |
| 123 | SkAutoTUnref<SkShader> shader(this->validInternal(rec, totalInverse)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 124 | return shader != NULL; |
| 125 | } |
| 126 | |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 127 | SkShader::Context* SkPictureShader::createContext(const ContextRec& rec, void* storage) const { |
| 128 | SkAutoTUnref<SkShader> bitmapShader(this->validInternal(rec, NULL)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 129 | if (!bitmapShader) { |
| 130 | return NULL; |
commit-bot@chromium.org | 68ea91a | 2014-04-17 21:02:29 +0000 | [diff] [blame] | 131 | } |
| 132 | |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 133 | return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, (*this, rec, bitmapShader.detach())); |
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 | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 140 | SkPictureShader::PictureShaderContext::PictureShaderContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 141 | const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) |
| 142 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 143 | , fBitmapShader(bitmapShader) |
| 144 | { |
| 145 | SkASSERT(fBitmapShader); |
| 146 | fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize()); |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame^] | 147 | fBitmapShaderContext = fBitmapShader->createContext(rec, fBitmapShaderContextStorage); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 148 | SkASSERT(fBitmapShaderContext); |
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() { |
| 152 | fBitmapShaderContext->~Context(); |
| 153 | sk_free(fBitmapShaderContextStorage); |
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 | uint32_t SkPictureShader::PictureShaderContext::getFlags() const { |
| 157 | return fBitmapShaderContext->getFlags(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 158 | } |
| 159 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 160 | SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) { |
| 161 | return fBitmapShaderContext->asAShadeProc(ctx); |
| 162 | } |
| 163 | |
| 164 | void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 165 | SkASSERT(fBitmapShaderContext); |
| 166 | fBitmapShaderContext->shadeSpan(x, y, dstC, count); |
| 167 | } |
| 168 | |
| 169 | void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) { |
| 170 | SkASSERT(fBitmapShaderContext); |
| 171 | fBitmapShaderContext->shadeSpan16(x, y, dstC, count); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | #ifndef SK_IGNORE_TO_STRING |
| 175 | void SkPictureShader::toString(SkString* str) const { |
| 176 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 177 | "clamp", "repeat", "mirror" |
| 178 | }; |
| 179 | |
| 180 | str->appendf("PictureShader: [%d:%d] ", |
| 181 | fPicture ? fPicture->width() : 0, |
| 182 | fPicture ? fPicture->height() : 0); |
| 183 | |
| 184 | str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
| 185 | |
| 186 | this->INHERITED::toString(str); |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #if SK_SUPPORT_GPU |
| 191 | GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 192 | SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix())); |
| 193 | if (!bitmapShader) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 194 | return NULL; |
| 195 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 196 | return bitmapShader->asNewEffect(context, paint); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 197 | } |
| 198 | #endif |