From c5d9bb0f677069f62ec76373b9730e70e7352455 Mon Sep 17 00:00:00 2001 From: "commit-bot@chromium.org" Date: Tue, 8 Apr 2014 15:19:34 +0000 Subject: Initial picture shader implementation This CL adds an SkPictureShader class to support SkPicture-based patterns. The implementation renders the picture into an SkBitmap tile and then delegates to SkBitmapProcShader for the actual operation. R=bsalomon@google.com, reed@google.com, robertphillips@google.com Committed: http://code.google.com/p/skia/source/detail?r=14085 Author: fmalita@chromium.org Review URL: https://codereview.chromium.org/221923007 git-svn-id: http://skia.googlecode.com/svn/trunk@14092 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/core/SkPictureShader.cpp | 185 ++++++++++++++++++++++++++ src/core/SkPictureShader.h | 60 +++++++++ src/core/SkShader.cpp | 6 + src/ports/SkGlobalInitialization_chromium.cpp | 2 + src/ports/SkGlobalInitialization_default.cpp | 2 + 5 files changed, 255 insertions(+) create mode 100644 src/core/SkPictureShader.cpp create mode 100644 src/core/SkPictureShader.h (limited to 'src') diff --git a/src/core/SkPictureShader.cpp b/src/core/SkPictureShader.cpp new file mode 100644 index 0000000000..15df3a37a5 --- /dev/null +++ b/src/core/SkPictureShader.cpp @@ -0,0 +1,185 @@ +/* + * 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 "SkMatrixUtils.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(buffer.read32()); + fTmy = static_cast(buffer.read32()); + if (buffer.readBool()) { + fPicture = SkPicture::CreateFromBuffer(buffer); + } else { + fPicture = NULL; + } +} + +SkPictureShader::~SkPictureShader() { + SkSafeUnref(fPicture); +} + +SkPictureShader* SkPictureShader::Create(SkPicture* picture, TileMode tmx, TileMode tmy) { + return SkNEW_ARGS(SkPictureShader, (picture, tmx, tmy)); +} + +void SkPictureShader::flatten(SkWriteBuffer& buffer) const { + this->INHERITED::flatten(buffer); + + buffer.write32(fTmx); + buffer.write32(fTmy); + buffer.writeBool(NULL != fPicture); + if (fPicture) { + fPicture->flatten(buffer); + } +} + +bool SkPictureShader::buildBitmapShader(const SkMatrix& matrix) const { + if (!fPicture || (0 == fPicture->width() && 0 == fPicture->height())) { + return false; + } + + SkMatrix m; + if (this->hasLocalMatrix()) { + m.setConcat(matrix, this->getLocalMatrix()); + } else { + m = matrix; + } + + // Use a rotation-invariant scale + SkPoint scale; + if (!SkDecomposeUpper2x2(m, NULL, &scale, NULL)) { + // Decomposition failed, use an approximation. + scale.set(SkScalarSqrt(m.getScaleX() * m.getScaleX() + m.getSkewX() * m.getSkewX()), + SkScalarSqrt(m.getScaleY() * m.getScaleY() + m.getSkewY() * m.getSkewY())); + } + SkSize scaledSize = SkSize::Make(scale.x() * fPicture->width(), scale.y() * fPicture->height()); + + SkISize tileSize = scaledSize.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 = this->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) { + if (!this->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() { + if (NULL != 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 { + if (!this->buildBitmapShader(context->getMatrix())) { + return NULL; + } + SkASSERT(fCachedShader); + return fCachedShader->asNewEffect(context, paint); +} +#endif diff --git a/src/core/SkPictureShader.h b/src/core/SkPictureShader.h new file mode 100644 index 0000000000..ea74b56d73 --- /dev/null +++ b/src/core/SkPictureShader.h @@ -0,0 +1,60 @@ +/* + * Copyright 2014 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkPictureShader_DEFINED +#define SkPictureShader_DEFINED + +#include "SkShader.h" + +class SkBitmap; +class SkPicture; + +/* + * An SkPictureShader can be used to draw SkPicture-based patterns. + * + * The SkPicture is first rendered into a tile, which is then used to shade the area according + * to specified tiling rules. + */ +class SkPictureShader : public SkShader { +public: + static SkPictureShader* Create(SkPicture*, TileMode, TileMode); + virtual ~SkPictureShader(); + + virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) SK_OVERRIDE; + virtual void endContext() SK_OVERRIDE; + virtual uint32_t getFlags() SK_OVERRIDE; + + virtual ShadeProc asAShadeProc(void** ctx) SK_OVERRIDE; + virtual void shadeSpan(int x, int y, SkPMColor dstC[], int count) SK_OVERRIDE; + virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count) SK_OVERRIDE; + + SK_TO_STRING_OVERRIDE() + SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPictureShader) + +#if SK_SUPPORT_GPU + GrEffectRef* asNewEffect(GrContext*, const SkPaint&) const SK_OVERRIDE; +#endif + +protected: + SkPictureShader(SkReadBuffer&); + virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; + +private: + SkPictureShader(SkPicture*, TileMode, TileMode); + + bool buildBitmapShader(const SkMatrix&) const; + + SkPicture* fPicture; + TileMode fTmx, fTmy; + + mutable SkAutoTUnref fCachedShader; + mutable SkSize fCachedTileScale; + + typedef SkShader INHERITED; +}; + +#endif // SkPictureShader_DEFINED diff --git a/src/core/SkShader.cpp b/src/core/SkShader.cpp index 31b208e2cc..e337b7d906 100644 --- a/src/core/SkShader.cpp +++ b/src/core/SkShader.cpp @@ -9,6 +9,8 @@ #include "SkReadBuffer.h" #include "SkMallocPixelRef.h" #include "SkPaint.h" +#include "SkPicture.h" +#include "SkPictureShader.h" #include "SkScalar.h" #include "SkShader.h" #include "SkWriteBuffer.h" @@ -179,6 +181,10 @@ SkShader* SkShader::CreateBitmapShader(const SkBitmap& src, return ::CreateBitmapShader(src, tmx, tmy, NULL); } +SkShader* SkShader::CreatePictureShader(SkPicture* src, TileMode tmx, TileMode tmy) { + return SkPictureShader::Create(src, tmx, tmy); +} + #ifndef SK_IGNORE_TO_STRING void SkShader::toString(SkString* str) const { if (this->hasLocalMatrix()) { diff --git a/src/ports/SkGlobalInitialization_chromium.cpp b/src/ports/SkGlobalInitialization_chromium.cpp index d0900a8f09..a3ef5b24ef 100644 --- a/src/ports/SkGlobalInitialization_chromium.cpp +++ b/src/ports/SkGlobalInitialization_chromium.cpp @@ -51,6 +51,7 @@ #include "SkOnce.h" #include "SkPerlinNoiseShader.h" #include "SkPictureImageFilter.h" +#include "SkPictureShader.h" #include "SkPixelXorXfermode.h" #include "SkRectShaderImageFilter.h" #include "SkStippleMaskFilter.h" @@ -90,6 +91,7 @@ static void InitializeFlattenables(int*) { SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath2DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPerlinNoiseShader) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPictureImageFilter) + SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPictureShader) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPixelXorXfermode) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRectShaderImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkStippleMaskFilter) diff --git a/src/ports/SkGlobalInitialization_default.cpp b/src/ports/SkGlobalInitialization_default.cpp index d0900a8f09..a3ef5b24ef 100644 --- a/src/ports/SkGlobalInitialization_default.cpp +++ b/src/ports/SkGlobalInitialization_default.cpp @@ -51,6 +51,7 @@ #include "SkOnce.h" #include "SkPerlinNoiseShader.h" #include "SkPictureImageFilter.h" +#include "SkPictureShader.h" #include "SkPixelXorXfermode.h" #include "SkRectShaderImageFilter.h" #include "SkStippleMaskFilter.h" @@ -90,6 +91,7 @@ static void InitializeFlattenables(int*) { SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPath2DPathEffect) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPerlinNoiseShader) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPictureImageFilter) + SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPictureShader) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPixelXorXfermode) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRectShaderImageFilter) SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkStippleMaskFilter) -- cgit v1.2.3