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 | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 52 | bool SkPictureShader::buildBitmapShader(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 | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 73 | return false; |
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 | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 80 | if (!fCachedShader || tileScale != fCachedTileScale) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 81 | SkBitmap bm; |
| 82 | if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 83 | return false; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 84 | } |
| 85 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 86 | |
| 87 | SkCanvas canvas(bm); |
| 88 | canvas.scale(tileScale.width(), tileScale.height()); |
| 89 | canvas.drawPicture(*fPicture); |
| 90 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 91 | fCachedShader.reset(CreateBitmapShader(bm, fTmx, fTmy)); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 92 | fCachedTileScale = tileScale; |
| 93 | } |
| 94 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 95 | SkMatrix shaderMatrix = this->getLocalMatrix(); |
| 96 | shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
| 97 | fCachedShader->setLocalMatrix(shaderMatrix); |
| 98 | |
| 99 | return true; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 100 | } |
| 101 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 102 | bool SkPictureShader::setContext(const SkBitmap& device, |
| 103 | const SkPaint& paint, |
| 104 | const SkMatrix& matrix) { |
| 105 | if (!this->buildBitmapShader(matrix)) { |
| 106 | return false; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 107 | } |
| 108 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 109 | if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 110 | return false; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 111 | } |
| 112 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 113 | SkASSERT(fCachedShader); |
| 114 | if (!fCachedShader->setContext(device, paint, matrix)) { |
| 115 | this->INHERITED::endContext(); |
| 116 | return false; |
commit-bot@chromium.org | 68ea91a | 2014-04-17 21:02:29 +0000 | [diff] [blame] | 117 | } |
| 118 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 119 | return true; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 120 | } |
| 121 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 122 | void SkPictureShader::endContext() { |
| 123 | SkASSERT(fCachedShader); |
| 124 | fCachedShader->endContext(); |
| 125 | |
| 126 | this->INHERITED::endContext(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 127 | } |
| 128 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 129 | uint32_t SkPictureShader::getFlags() { |
| 130 | if (NULL != fCachedShader) { |
| 131 | return fCachedShader->getFlags(); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 132 | } |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 133 | return 0; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 134 | } |
| 135 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 136 | SkShader::ShadeProc SkPictureShader::asAShadeProc(void** ctx) { |
| 137 | if (fCachedShader) { |
| 138 | return fCachedShader->asAShadeProc(ctx); |
| 139 | } |
| 140 | return NULL; |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 141 | } |
| 142 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 143 | void SkPictureShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 144 | SkASSERT(fCachedShader); |
| 145 | fCachedShader->shadeSpan(x, y, dstC, count); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 146 | } |
| 147 | |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 148 | void SkPictureShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) { |
| 149 | SkASSERT(fCachedShader); |
| 150 | fCachedShader->shadeSpan16(x, y, dstC, count); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | #ifndef SK_IGNORE_TO_STRING |
| 154 | void SkPictureShader::toString(SkString* str) const { |
| 155 | static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 156 | "clamp", "repeat", "mirror" |
| 157 | }; |
| 158 | |
| 159 | str->appendf("PictureShader: [%d:%d] ", |
| 160 | fPicture ? fPicture->width() : 0, |
| 161 | fPicture ? fPicture->height() : 0); |
| 162 | |
| 163 | str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
| 164 | |
| 165 | this->INHERITED::toString(str); |
| 166 | } |
| 167 | #endif |
| 168 | |
| 169 | #if SK_SUPPORT_GPU |
| 170 | GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const { |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 171 | if (!this->buildBitmapShader(context->getMatrix())) { |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 172 | return NULL; |
| 173 | } |
commit-bot@chromium.org | 53783b0 | 2014-04-17 21:09:49 +0000 | [diff] [blame] | 174 | SkASSERT(fCachedShader); |
| 175 | return fCachedShader->asNewEffect(context, paint); |
commit-bot@chromium.org | c5d9bb0 | 2014-04-08 15:19:34 +0000 | [diff] [blame] | 176 | } |
| 177 | #endif |