SkPictureShader.cpp revision c09abe66c1388c5da456c7686eab7a7928e9e9d6
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 21SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy) 22 : fPicture(SkRef(picture)) 23 , fTmx(tmx) 24 , fTmy(tmy) { } 25 26SkPictureShader::SkPictureShader(SkReadBuffer& buffer) 27 : INHERITED(buffer) { 28 fTmx = static_cast<SkShader::TileMode>(buffer.read32()); 29 fTmy = static_cast<SkShader::TileMode>(buffer.read32()); 30 fPicture = SkPicture::CreateFromBuffer(buffer); 31} 32 33SkPictureShader::~SkPictureShader() { 34 fPicture->unref(); 35} 36 37SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy) { 38 if (!picture || 0 == picture->width() || 0 == picture->height()) { 39 return NULL; 40 } 41 return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy)); 42} 43 44void SkPictureShader::flatten(SkWriteBuffer& buffer) const { 45 this->INHERITED::flatten(buffer); 46 47 buffer.write32(fTmx); 48 buffer.write32(fTmy); 49 fPicture->flatten(buffer); 50} 51 52SkShader* SkPictureShader::refBitmapShader(const SkMatrix& matrix) const { 53 SkASSERT(fPicture && fPicture->width() > 0 && fPicture->height() > 0); 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()) { 73 return NULL; 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 80 SkAutoMutexAcquire ama(fCachedBitmapShaderMutex); 81 82 if (!fCachedBitmapShader || tileScale != fCachedTileScale || 83 this->getLocalMatrix() != fCachedLocalMatrix) { 84 SkBitmap bm; 85 if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { 86 return NULL; 87 } 88 bm.eraseColor(SK_ColorTRANSPARENT); 89 90 SkCanvas canvas(bm); 91 canvas.scale(tileScale.width(), tileScale.height()); 92 canvas.drawPicture(*fPicture); 93 94 fCachedTileScale = tileScale; 95 fCachedLocalMatrix = this->getLocalMatrix(); 96 97 SkMatrix shaderMatrix = this->getLocalMatrix(); 98 shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); 99 fCachedBitmapShader.reset(CreateBitmapShader(bm, fTmx, fTmy, &shaderMatrix)); 100 } 101 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; 107} 108 109SkShader* SkPictureShader::validInternal(const ContextRec& rec, SkMatrix* totalInverse) const { 110 if (!this->INHERITED::validContext(rec, totalInverse)) { 111 return NULL; 112 } 113 114 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(*rec.fMatrix)); 115 if (!bitmapShader || !bitmapShader->validContext(rec)) { 116 return NULL; 117 } 118 119 return bitmapShader.detach(); 120} 121 122bool SkPictureShader::validContext(const ContextRec& rec, SkMatrix* totalInverse) const { 123 SkAutoTUnref<SkShader> shader(this->validInternal(rec, totalInverse)); 124 return shader != NULL; 125} 126 127SkShader::Context* SkPictureShader::createContext(const ContextRec& rec, void* storage) const { 128 SkAutoTUnref<SkShader> bitmapShader(this->validInternal(rec, NULL)); 129 if (!bitmapShader) { 130 return NULL; 131 } 132 133 return SkNEW_PLACEMENT_ARGS(storage, PictureShaderContext, (*this, rec, bitmapShader.detach())); 134} 135 136size_t SkPictureShader::contextSize() const { 137 return sizeof(PictureShaderContext); 138} 139 140SkPictureShader::PictureShaderContext::PictureShaderContext( 141 const SkPictureShader& shader, const ContextRec& rec, SkShader* bitmapShader) 142 : INHERITED(shader, rec) 143 , fBitmapShader(bitmapShader) 144{ 145 SkASSERT(fBitmapShader); 146 fBitmapShaderContextStorage = sk_malloc_throw(fBitmapShader->contextSize()); 147 fBitmapShaderContext = fBitmapShader->createContext(rec, fBitmapShaderContextStorage); 148 SkASSERT(fBitmapShaderContext); 149} 150 151SkPictureShader::PictureShaderContext::~PictureShaderContext() { 152 fBitmapShaderContext->~Context(); 153 sk_free(fBitmapShaderContextStorage); 154} 155 156uint32_t SkPictureShader::PictureShaderContext::getFlags() const { 157 return fBitmapShaderContext->getFlags(); 158} 159 160SkShader::Context::ShadeProc SkPictureShader::PictureShaderContext::asAShadeProc(void** ctx) { 161 return fBitmapShaderContext->asAShadeProc(ctx); 162} 163 164void SkPictureShader::PictureShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { 165 SkASSERT(fBitmapShaderContext); 166 fBitmapShaderContext->shadeSpan(x, y, dstC, count); 167} 168 169void SkPictureShader::PictureShaderContext::shadeSpan16(int x, int y, uint16_t dstC[], int count) { 170 SkASSERT(fBitmapShaderContext); 171 fBitmapShaderContext->shadeSpan16(x, y, dstC, count); 172} 173 174#ifndef SK_IGNORE_TO_STRING 175void 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 191GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const { 192 SkAutoTUnref<SkShader> bitmapShader(this->refBitmapShader(context->getMatrix())); 193 if (!bitmapShader) { 194 return NULL; 195 } 196 return bitmapShader->asNewEffect(context, paint); 197} 198#endif 199