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 | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 94 | fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy)); |
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 | fCachedLocalMatrix = this->getLocalMatrix(); |
| 97 | |
| 98 | SkMatrix shaderMatrix = this->getLocalMatrix(); |
| 99 | shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
| 100 | fCachedBitmapShader->setLocalMatrix(shaderMatrix); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 101 | } |
| 102 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 103 | // Increment the ref counter inside the mutex to ensure the returned pointer is still valid. |
| 104 | // Otherwise, the pointer may have been overwritten on a different thread before the object's |
| 105 | // ref count was incremented. |
| 106 | fCachedBitmapShader.get()->ref(); |
| 107 | return fCachedBitmapShader; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 108 | } |
| 109 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 110 | SkShader* SkPictureShader::validInternal(const SkBitmap& device, const SkPaint& paint, |
| 111 | const SkMatrix& matrix, SkMatrix* totalInverse) const { |
| 112 | if (!this->INHERITED::validContext(device, paint, matrix, totalInverse)) { |
| 113 | return NULL; |
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 | SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(matrix)); |
| 117 | if (!bitmapShader || !bitmapShader->validContext(device, paint, matrix)) { |
| 118 | return NULL; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 119 | } |
| 120 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 121 | return bitmapShader.detach(); |
| 122 | } |
| 123 | |
| 124 | bool SkPictureShader::validContext(const SkBitmap& device, const SkPaint& paint, |
| 125 | const SkMatrix& matrix, SkMatrix* totalInverse) const { |
| 126 | SkAutoTUnref<SkShader> shader(this->validInternal(device, paint, matrix, totalInverse)); |
| 127 | return shader != NULL; |
| 128 | } |
| 129 | |
| 130 | SkShader::Context* SkPictureShader::createContext(const SkBitmap& device, const SkPaint& paint, |
| 131 | const SkMatrix& matrix, void* storage) const { |
| 132 | SkAutoTUnref<SkShader> bitmapShader(this->validInternal(device, paint, matrix, NULL)); |
| 133 | if (!bitmapShader) { |
| 134 | return NULL; |
commit-bot@chromium.org | 68ea91a | 2014-04-17 21:02:29 +0000 | [diff] [blame] | 135 | } |
| 136 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 137 | return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, |
| 138 | (*this, device, paint, matrix, bitmapShader.detach())); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 139 | } |
| 140 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 141 | size_t SkPictureShader::contextSize() const { |
| 142 | return sizeof(PictureShaderContext); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 143 | } |
| 144 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 145 | SkPictureShader::PictureShaderContext::PictureShaderContext( |
| 146 | const SkPictureShader& shader, const SkBitmap& device, |
| 147 | const SkPaint& paint, const SkMatrix& matrix, SkShader* bitmapShader) |
| 148 | : INHERITED(shader, device, paint, matrix) |
| 149 | , fBitmapShader(bitmapShader) |
| 150 | { |
| 151 | SkASSERT(fBitmapShader); |
| 152 | fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize()); |
| 153 | fBitmapShaderContext = fBitmapShader->createContext( |
| 154 | device, paint, matrix, fBitmapShaderContextStorage); |
| 155 | SkASSERT(fBitmapShaderContext); |
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 | SkPictureShader::PictureShaderContext::~PictureShaderContext() { |
| 159 | fBitmapShaderContext->~Context(); |
| 160 | sk_free(fBitmapShaderContextStorage); |
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 | uint32_t SkPictureShader::PictureShaderContext::getFlags() const { |
| 164 | return fBitmapShaderContext->getFlags(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 165 | } |
| 166 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 167 | SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) { |
| 168 | return fBitmapShaderContext->asAShadeProc(ctx); |
| 169 | } |
| 170 | |
| 171 | void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 172 | SkASSERT(fBitmapShaderContext); |
| 173 | fBitmapShaderContext->shadeSpan(x, y, dstC, count); |
| 174 | } |
| 175 | |
| 176 | void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) { |
| 177 | SkASSERT(fBitmapShaderContext); |
| 178 | fBitmapShaderContext->shadeSpan16(x, y, dstC, count); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | #ifndef SK_IGNORE_TO_STRING |
| 182 | void SkPictureShader::toString(SkString* str) const { |
| 183 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 184 | "clamp", "repeat", "mirror" |
| 185 | }; |
| 186 | |
| 187 | str->appendf("PictureShader: [%d:%d] ", |
| 188 | fPicture ? fPicture->width() : 0, |
| 189 | fPicture ? fPicture->height() : 0); |
| 190 | |
| 191 | str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
| 192 | |
| 193 | this->INHERITED::toString(str); |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | #if SK_SUPPORT_GPU |
| 198 | GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 199 | SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix())); |
| 200 | if (!bitmapShader) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 201 | return NULL; |
| 202 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame^] | 203 | return bitmapShader->asNewEffect(context, paint); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 204 | } |
| 205 | #endif |