| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkPictureShader.h" | 8 #include "SkPictureShader.h" |
| 9 | 9 |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 } | 42 } |
| 43 | 43 |
| 44 void SkPictureShader::flatten(SkWriteBuffer& buffer) const { | 44 void SkPictureShader::flatten(SkWriteBuffer& buffer) const { |
| 45 this->INHERITED::flatten(buffer); | 45 this->INHERITED::flatten(buffer); |
| 46 | 46 |
| 47 buffer.write32(fTmx); | 47 buffer.write32(fTmx); |
| 48 buffer.write32(fTmy); | 48 buffer.write32(fTmy); |
| 49 fPicture->flatten(buffer); | 49 fPicture->flatten(buffer); |
| 50 } | 50 } |
| 51 | 51 |
| 52 SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix) const { | 52 bool SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const { |
| 53 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); | 53 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); |
| 54 | 54 |
| 55 SkMatrix m; | 55 SkMatrix m; |
| 56 if (this->hasLocalMatrix()) { | 56 if (this->hasLocalMatrix()) { |
| 57 m.setConcat(matrix, this->getLocalMatrix()); | 57 m.setConcat(matrix, this->getLocalMatrix()); |
| 58 } else { | 58 } else { |
| 59 m = matrix; | 59 m = matrix; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Use a rotation-invariant scale | 62 // Use a rotation-invariant scale |
| 63 SkPoint scale; | 63 SkPoint scale; |
| 64 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { | 64 if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { |
| 65 // Decomposition failed, use an approximation. | 65 // Decomposition failed, use an approximation. |
| 66 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.
getSkewX()), | 66 scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.
getSkewX()), |
| 67 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.
getSkewY())); | 67 SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.
getSkewY())); |
| 68 } | 68 } |
| 69 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() *
fPicture->height()); | 69 SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() *
fPicture->height()); |
| 70 | 70 |
| 71 SkISize tileSize = scaledSize.toRound(); | 71 SkISize tileSize = scaledSize.toRound(); |
| 72 if (tileSize.isEmpty()) { | 72 if (tileSize.isEmpty()) { |
| 73 return NULL; | 73 return false; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // The actual scale, compensating for rounding. | 76 // The actual scale, compensating for rounding. |
| 77 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->
width(), | 77 SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->
width(), |
| 78 SkIntToScalar(tileSize.height()) / fPicture-
>height()); | 78 SkIntToScalar(tileSize.height()) / fPicture-
>height()); |
| 79 | 79 |
| 80 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); | 80 if (!fCachedShader || tileScale != fCachedTileScale) { |
| 81 | |
| 82 if (!fCachedBitmapShader || tileScale != fCachedTileScale || | |
| 83 this->getLocalMatrix() != fCachedLocalMatrix) { | |
| 84 SkBitmap bm; | 81 SkBitmap bm; |
| 85 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { | 82 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { |
| 86 return NULL; | 83 return false; |
| 87 } | 84 } |
| 88 bm.eraseColor(SK_ColorTRANSPARENT); | 85 bm.eraseColor(SK_ColorTRANSPARENT); |
| 89 | 86 |
| 90 SkCanvas canvas(bm); | 87 SkCanvas canvas(bm); |
| 91 canvas.scale(tileScale.width(), tileScale.height()); | 88 canvas.scale(tileScale.width(), tileScale.height()); |
| 92 canvas.drawPicture(*fPicture); | 89 canvas.drawPicture(*fPicture); |
| 93 | 90 |
| 94 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy)); | 91 fCachedShader.reset(CreateBitmapShader(bm, fTmx, fTmy)); |
| 95 fCachedTileScale = tileScale; | 92 fCachedTileScale = tileScale; |
| 96 fCachedLocalMatrix = this->getLocalMatrix(); | |
| 97 | |
| 98 SkMatrix shaderMatrix = this->getLocalMatrix(); | |
| 99 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); | |
| 100 fCachedBitmapShader->setLocalMatrix(shaderMatrix); | |
| 101 } | 93 } |
| 102 | 94 |
| 103 // Increment the ref counter inside the mutex to ensure the returned pointer
is still valid. | 95 SkMatrix shaderMatrix = this->getLocalMatrix(); |
| 104 // Otherwise, the pointer may have been overwritten on a different thread be
fore the object's | 96 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
| 105 // ref count was incremented. | 97 fCachedShader->setLocalMatrix(shaderMatrix); |
| 106 fCachedBitmapShader.get()->ref(); | 98 |
| 107 return fCachedBitmapShader; | 99 return true; |
| 108 } | 100 } |
| 109 | 101 |
| 110 SkShader* SkPictureShader::validInternal(const SkBitmap& device, const SkPaint&
paint, | 102 bool SkPictureShader::setContext(const SkBitmap& device, |
| 111 const SkMatrix& matrix, SkMatrix* total
Inverse) const { | 103 const SkPaint& paint, |
| 112 if (!this->INHERITED::validContext(device, paint, matrix, totalInverse)) { | 104 const SkMatrix& matrix) { |
| 113 return NULL; | 105 if (!this->buildBitmapShader(matrix)) { |
| 106 return false; |
| 114 } | 107 } |
| 115 | 108 |
| 116 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(matrix)); | 109 if (!this->INHERITED::setContext(device, paint, matrix)) { |
| 117 if (!bitmapShader || !bitmapShader->validContext(device, paint, matrix)) { | 110 return false; |
| 118 return NULL; | |
| 119 } | 111 } |
| 120 | 112 |
| 121 return bitmapShader.detach(); | 113 SkASSERT(fCachedShader); |
| 114 if (!fCachedShader->setContext(device, paint, matrix)) { |
| 115 this->INHERITED::endContext(); |
| 116 return false; |
| 117 } |
| 118 |
| 119 return true; |
| 122 } | 120 } |
| 123 | 121 |
| 124 bool SkPictureShader::validContext(const SkBitmap& device, const SkPaint& paint, | 122 void SkPictureShader::endContext() { |
| 125 const SkMatrix& matrix, SkMatrix* totalInvers
e) const { | 123 SkASSERT(fCachedShader); |
| 126 SkAutoTUnref<SkShader> shader(this->validInternal(device, paint, matrix, tot
alInverse)); | 124 fCachedShader->endContext(); |
| 127 return shader != NULL; | 125 |
| 126 this->INHERITED::endContext(); |
| 128 } | 127 } |
| 129 | 128 |
| 130 SkShader::Context* SkPictureShader::createContext(const SkBitmap& device, const
SkPaint& paint, | 129 uint32_t SkPictureShader::getFlags() { |
| 131 const SkMatrix& matrix, void*
storage) const { | 130 if (NULL != fCachedShader) { |
| 132 SkAutoTUnref<SkShader> bitmapShader(this->validInternal(device, paint, matri
x, NULL)); | 131 return fCachedShader->getFlags(); |
| 133 if (!bitmapShader) { | |
| 134 return NULL; | |
| 135 } | 132 } |
| 136 | 133 return 0; |
| 137 return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, | |
| 138 (*this, device, paint, matrix, bitmapShader.deta
ch())); | |
| 139 } | 134 } |
| 140 | 135 |
| 141 size_t SkPictureShader::contextSize() const { | 136 SkShader::ShadeProc SkPictureShader::asAShadeProc(void** ctx) { |
| 142 return sizeof(PictureShaderContext); | 137 if (fCachedShader) { |
| 138 return fCachedShader->asAShadeProc(ctx); |
| 139 } |
| 140 return NULL; |
| 143 } | 141 } |
| 144 | 142 |
| 145 SkPictureShader::PictureShaderContext::PictureShaderContext( | 143 void SkPictureShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| 146 const SkPictureShader& shader, const SkBitmap& device, | 144 SkASSERT(fCachedShader); |
| 147 const SkPaint& paint, const SkMatrix& matrix, SkShader* bitmapShader) | 145 fCachedShader->shadeSpan(x, y, dstC, count); |
| 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); | |
| 156 } | 146 } |
| 157 | 147 |
| 158 SkPictureShader::PictureShaderContext::~PictureShaderContext() { | 148 void SkPictureShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) { |
| 159 fBitmapShaderContext->~Context(); | 149 SkASSERT(fCachedShader); |
| 160 sk_free(fBitmapShaderContextStorage); | 150 fCachedShader->shadeSpan16(x, y, dstC, count); |
| 161 } | |
| 162 | |
| 163 uint32_t SkPictureShader::PictureShaderContext::getFlags() const { | |
| 164 return fBitmapShaderContext->getFlags(); | |
| 165 } | |
| 166 | |
| 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 ds
tC[], 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 d
stC[], int count) { | |
| 177 SkASSERT(fBitmapShaderContext); | |
| 178 fBitmapShaderContext->shadeSpan16(x, y, dstC, count); | |
| 179 } | 151 } |
| 180 | 152 |
| 181 #ifndef SK_IGNORE_TO_STRING | 153 #ifndef SK_IGNORE_TO_STRING |
| 182 void SkPictureShader::toString(SkString* str) const { | 154 void SkPictureShader::toString(SkString* str) const { |
| 183 static const char* gTileModeName[SkShader::kTileModeCount] = { | 155 static const char* gTileModeName[SkShader::kTileModeCount] = { |
| 184 "clamp", "repeat", "mirror" | 156 "clamp", "repeat", "mirror" |
| 185 }; | 157 }; |
| 186 | 158 |
| 187 str->appendf("PictureShader: [%d:%d] ", | 159 str->appendf("PictureShader: [%d:%d] ", |
| 188 fPicture ? fPicture->width() : 0, | 160 fPicture ? fPicture->width() : 0, |
| 189 fPicture ? fPicture->height() : 0); | 161 fPicture ? fPicture->height() : 0); |
| 190 | 162 |
| 191 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); | 163 str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
| 192 | 164 |
| 193 this->INHERITED::toString(str); | 165 this->INHERITED::toString(str); |
| 194 } | 166 } |
| 195 #endif | 167 #endif |
| 196 | 168 |
| 197 #if SK_SUPPORT_GPU | 169 #if SK_SUPPORT_GPU |
| 198 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai
nt) const { | 170 GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& pai
nt) const { |
| 199 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix
())); | 171 if (!this->buildBitmapShader(context->getMatrix())) { |
| 200 if (!bitmapShader) { | |
| 201 return NULL; | 172 return NULL; |
| 202 } | 173 } |
| 203 return bitmapShader->asNewEffect(context, paint); | 174 SkASSERT(fCachedShader); |
| 175 return fCachedShader->asNewEffect(context, paint); |
| 204 } | 176 } |
| 205 #endif | 177 #endif |
| OLD | NEW |