Chromium Code Reviews| Index: src/core/SkPictureShader.cpp |
| diff --git a/src/core/SkPictureShader.cpp b/src/core/SkPictureShader.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b688ef0717b46ac7dad89e435176785659dd0320 |
| --- /dev/null |
| +++ b/src/core/SkPictureShader.cpp |
| @@ -0,0 +1,176 @@ |
| +/* |
| + * Copyright 2014 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkPictureShader.h" |
| + |
| +#include "SkBitmap.h" |
| +#include "SkBitmapProcShader.h" |
| +#include "SkCanvas.h" |
| +#include "SkPicture.h" |
| +#include "SkReadBuffer.h" |
| + |
| +#if SK_SUPPORT_GPU |
| +#include "GrContext.h" |
| +#endif |
| + |
| +SkPictureShader::SkPictureShader(SkPicture* picture, TileMode tmx, TileMode tmy) |
| + : fPicture(picture) |
| + , fTmx(tmx) |
| + , fTmy(tmy) { |
| + SkSafeRef(fPicture); |
| +} |
| + |
| +SkPictureShader::SkPictureShader(SkReadBuffer& buffer) |
| + : INHERITED(buffer) { |
| + fTmx = static_cast<SkShader::TileMode>(buffer.read32()); |
| + fTmy = static_cast<SkShader::TileMode>(buffer.read32()); |
| + if (buffer.readBool()) { |
| + fPicture = SkPicture::CreateFromBuffer(buffer); |
| + } else { |
| + fPicture = NULL; |
| + } |
| +} |
| + |
| +SkPictureShader::~SkPictureShader() { |
| + SkSafeUnref(fPicture); |
| +} |
| + |
| +void SkPictureShader::flatten(SkWriteBuffer& buffer) const { |
| + this->INHERITED::flatten(buffer); |
| + |
| + buffer.write32(fTmx); |
| + buffer.write32(fTmy); |
| + buffer.writeBool(fPicture != NULL); |
|
robertphillips
2014/04/02 18:15:35
NULL != ?
f(malita)
2014/04/07 15:06:24
Done.
|
| + if (fPicture) { |
| + fPicture->flatten(buffer); |
| + } |
| +} |
| + |
| +bool SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const { |
| + if (!fPicture || (0 == fPicture->width() && 0 == fPicture->height())) { |
| + return false; |
| + } |
| + |
| + SkMatrix m; |
|
robertphillips
2014/04/02 18:15:35
this->
|
| + if (hasLocalMatrix()) { |
|
robertphillips
2014/04/02 18:15:35
this->
|
| + m.setConcat(matrix, getLocalMatrix()); |
| + } else { |
| + m = matrix; |
| + } |
| + |
| + // Rotation-invariant scale |
| + float xScale = sqrtf(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()); |
| + float yScale = sqrtf(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY()); |
|
reed1
2014/04/07 14:03:15
SkScalarSqrt
f(malita)
2014/04/07 15:06:24
Done.
|
| + SkSize mappedSize = SkSize::Make(xScale * fPicture->width(), yScale * fPicture->height()); |
| + |
| + SkISize tileSize = mappedSize.toRound(); |
| + if (tileSize.isEmpty()) { |
| + return false; |
| + } |
| + |
| + // The actual scale, compensating for rounding. |
| + SkSize tileScale = SkSize::Make(SkIntToScalar(tileSize.width()) / fPicture->width(), |
| + SkIntToScalar(tileSize.height()) / fPicture->height()); |
| + |
| + if (!fCachedShader || tileScale != fCachedTileScale) { |
| + SkBitmap bm; |
| + if (!bm.allocN32Pixels(tileSize.width(), tileSize.height())) { |
| + return false; |
| + } |
| + bm.eraseColor(SK_ColorTRANSPARENT); |
| + |
| + SkCanvas canvas(bm); |
| + canvas.scale(tileScale.width(), tileScale.height()); |
| + canvas.drawPicture(*fPicture); |
| + |
| + fCachedShader.reset(CreateBitmapShader(bm, fTmx, fTmy)); |
| + fCachedTileScale = tileScale; |
| + } |
| + |
| + SkMatrix shaderMatrix = getLocalMatrix(); |
| + shaderMatrix.preScale(1 / tileScale.width(), 1 / tileScale.height()); |
| + fCachedShader->setLocalMatrix(shaderMatrix); |
| + |
| + return true; |
| +} |
| + |
| +bool SkPictureShader::setContext(const SkBitmap& device, |
| + const SkPaint& paint, |
| + const SkMatrix& matrix) { |
|
robertphillips
2014/04/02 18:15:35
this->
|
| + if (!buildBitmapShader(matrix)) { |
| + return false; |
| + } |
| + |
| + if (!this->INHERITED::setContext(device, paint, matrix)) { |
| + return false; |
| + } |
| + |
| + SkASSERT(fCachedShader); |
| + if (!fCachedShader->setContext(device, paint, matrix)) { |
| + this->INHERITED::endContext(); |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void SkPictureShader::endContext() { |
| + SkASSERT(fCachedShader); |
| + fCachedShader->endContext(); |
| + |
| + this->INHERITED::endContext(); |
| +} |
| + |
| +uint32_t SkPictureShader::getFlags() { |
|
robertphillips
2014/04/02 18:15:35
NULL !=
f(malita)
2014/04/07 15:06:24
Done.
|
| + if (fCachedShader) { |
| + return fCachedShader->getFlags(); |
| + } |
| + return 0; |
| +} |
| + |
| +SkShader::ShadeProc SkPictureShader::asAShadeProc(void** ctx) { |
| + if (fCachedShader) { |
| + return fCachedShader->asAShadeProc(ctx); |
| + } |
| + return NULL; |
| +} |
| + |
| +void SkPictureShader::shadeSpan(int x, int y, SkPMColor dstC[], int count) { |
| + SkASSERT(fCachedShader); |
| + fCachedShader->shadeSpan(x, y, dstC, count); |
| +} |
| + |
| +void SkPictureShader::shadeSpan16(int x, int y, uint16_t dstC[], int count) { |
| + SkASSERT(fCachedShader); |
| + fCachedShader->shadeSpan16(x, y, dstC, count); |
| +} |
| + |
| +#ifndef SK_IGNORE_TO_STRING |
| +void SkPictureShader::toString(SkString* str) const { |
| + static const char* gTileModeName[SkShader::kTileModeCount] = { |
| + "clamp", "repeat", "mirror" |
| + }; |
| + |
| + str->appendf("PictureShader: [%d:%d] ", |
| + fPicture ? fPicture->width() : 0, |
| + fPicture ? fPicture->height() : 0); |
| + |
| + str->appendf("(%s, %s)", gTileModeName[fTmx], gTileModeName[fTmy]); |
| + |
| + this->INHERITED::toString(str); |
| +} |
| +#endif |
| + |
| +#if SK_SUPPORT_GPU |
| +GrEffectRef* SkPictureShader::asNewEffect(GrContext* context, const SkPaint& paint) const { |
|
robertphillips
2014/04/02 18:15:35
this->
|
| + if (!buildBitmapShader(context->getMatrix())) { |
| + return NULL; |
| + } |
| + SkASSERT(fCachedShader); |
| + return fCachedShader->asNewEffect(context, paint); |
| +} |
| +#endif |