Use asNewCustomStage instead of asABitmap in SkGpuDevice, also removed now-unecessary twoPointRadialParams parameter from asABitmap.
In SkGpuDevice we still fall back on using asABitmap for effects that don't have asNewCustomStage implemented, but it still simplifies things a fair amount.
Review URL: https://codereview.appspot.com/6430060
git-svn-id: http://skia.googlecode.com/svn/trunk@4755 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkColorShader.h b/include/core/SkColorShader.h
index e64a183..0823b70 100644
--- a/include/core/SkColorShader.h
+++ b/include/core/SkColorShader.h
@@ -44,8 +44,7 @@
// we return false for this, use asAGradient
virtual BitmapType asABitmap(SkBitmap* outTexture,
SkMatrix* outMatrix,
- TileMode xy[2],
- SkScalar* twoPointRadialParams) const SK_OVERRIDE;
+ TileMode xy[2]) const SK_OVERRIDE;
virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE;
diff --git a/include/core/SkShader.h b/include/core/SkShader.h
index 1ba1d67..57d920c 100644
--- a/include/core/SkShader.h
+++ b/include/core/SkShader.h
@@ -246,7 +246,7 @@
about the first point.
*/
virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
- TileMode xy[2], SkScalar* twoPointRadialParams) const;
+ TileMode xy[2]) const;
/**
* If the shader subclass can be represented as a gradient, asAGradient
diff --git a/src/core/SkBitmapProcShader.cpp b/src/core/SkBitmapProcShader.cpp
index f76bc09..9b88c68 100644
--- a/src/core/SkBitmapProcShader.cpp
+++ b/src/core/SkBitmapProcShader.cpp
@@ -53,8 +53,7 @@
SkShader::BitmapType SkBitmapProcShader::asABitmap(SkBitmap* texture,
SkMatrix* texM,
- TileMode xy[],
- SkScalar* twoPointRadialParams) const {
+ TileMode xy[]) const {
if (texture) {
*texture = fRawBitmap;
}
diff --git a/src/core/SkBitmapProcShader.h b/src/core/SkBitmapProcShader.h
index 26b4cc2..e5e5f13 100644
--- a/src/core/SkBitmapProcShader.h
+++ b/src/core/SkBitmapProcShader.h
@@ -25,8 +25,7 @@
virtual void shadeSpan16(int x, int y, uint16_t dstC[], int count);
virtual void beginSession();
virtual void endSession();
- virtual BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*,
- SkScalar* twoPointRadialParams) const;
+ virtual BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*) const;
static bool CanDo(const SkBitmap&, TileMode tx, TileMode ty);
diff --git a/src/core/SkShader.cpp b/src/core/SkShader.cpp
index a0ae49d..cf522b6 100644
--- a/src/core/SkShader.cpp
+++ b/src/core/SkShader.cpp
@@ -192,7 +192,7 @@
//////////////////////////////////////////////////////////////////////////////
SkShader::BitmapType SkShader::asABitmap(SkBitmap*, SkMatrix*,
- TileMode*, SkScalar*) const {
+ TileMode*) const {
return kNone_BitmapType;
}
@@ -316,8 +316,7 @@
// if we had a asAColor method, that would be more efficient...
SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
- TileMode modes[],
- SkScalar* twoPointRadialParams) const {
+ TileMode modes[]) const {
return kNone_BitmapType;
}
diff --git a/src/device/xps/SkXPSDevice.cpp b/src/device/xps/SkXPSDevice.cpp
index 7b6aa45..8f2c6dd 100644
--- a/src/device/xps/SkXPSDevice.cpp
+++ b/src/device/xps/SkXPSDevice.cpp
@@ -1014,11 +1014,9 @@
SkBitmap outTexture;
SkMatrix outMatrix;
SkShader::TileMode xy[2];
- SkScalar twoPointRadialParams[3];
SkShader::BitmapType bitmapType = shader->asABitmap(&outTexture,
&outMatrix,
- xy,
- twoPointRadialParams);
+ xy);
switch (bitmapType) {
case SkShader::kNone_BitmapType:
break;
diff --git a/src/effects/SkGradientShader.cpp b/src/effects/SkGradientShader.cpp
index 2b54bd1..7a55bbb 100644
--- a/src/effects/SkGradientShader.cpp
+++ b/src/effects/SkGradientShader.cpp
@@ -112,7 +112,6 @@
};
///////////////////////////////////////////////////////////////////////////////
-///////////////////////////////////////////////////////////////////////////////
class Gradient_Shader : public SkShader {
public:
@@ -796,8 +795,7 @@
virtual bool setContext(const SkBitmap&, const SkPaint&, const SkMatrix&) 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;
- virtual BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*,
- SkScalar* twoPointRadialParams) const SK_OVERRIDE;
+ virtual BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*) const SK_OVERRIDE;
virtual GradientType asAGradient(GradientInfo* info) const SK_OVERRIDE;
virtual GrCustomStage* asNewCustomStage(GrContext* context,
GrSamplerState* sampler) const SK_OVERRIDE;
@@ -1025,8 +1023,7 @@
SkShader::BitmapType Linear_Gradient::asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
- TileMode xy[],
- SkScalar* twoPointRadialParams) const {
+ TileMode xy[]) const {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
@@ -1056,7 +1053,7 @@
sampler->setWrapX(sk_tile_mode_to_grwrap(fTileMode));
sampler->setWrapY(sk_tile_mode_to_grwrap(kClamp_TileMode));
sampler->setFilter(GrSamplerState::kBilinear_Filter);
- return SkNEW_ARGS(GrLinearGradient, (context, *this));
+ return SkNEW_ARGS(GrLinearGradient, (context, *this, sampler));
}
static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other,
@@ -1442,9 +1439,7 @@
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
- TileMode* xy,
- SkScalar* twoPointRadialParams)
- const SK_OVERRIDE {
+ TileMode* xy) const SK_OVERRIDE {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
@@ -1476,7 +1471,7 @@
sampler->setWrapX(sk_tile_mode_to_grwrap(fTileMode));
sampler->setWrapY(sk_tile_mode_to_grwrap(kClamp_TileMode));
sampler->setFilter(GrSamplerState::kBilinear_Filter);
- return SkNEW_ARGS(GrRadialGradient, (context, *this));
+ return SkNEW_ARGS(GrRadialGradient, (context, *this, sampler));
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Radial_Gradient)
@@ -1886,13 +1881,12 @@
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
- TileMode* xy,
- SkScalar* twoPointRadialParams) const {
+ TileMode* xy) const {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
SkScalar diffL = 0; // just to avoid gcc warning
- if (matrix || twoPointRadialParams) {
+ if (matrix) {
diffL = SkScalarSqrt(SkScalarSquare(fDiff.fX) +
SkScalarSquare(fDiff.fY));
}
@@ -1910,11 +1904,6 @@
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
- if (NULL != twoPointRadialParams) {
- twoPointRadialParams[0] = diffL;
- twoPointRadialParams[1] = fStartRadius;
- twoPointRadialParams[2] = fDiffRadius;
- }
return kTwoPointRadial_BitmapType;
}
@@ -1936,7 +1925,7 @@
if (0 != diffLen) {
SkScalar invDiffLen = SkScalarInvert(diffLen);
sampler->matrix()->setSinCos(-SkScalarMul(invDiffLen, fDiff.fY),
- SkScalarMul(invDiffLen, fDiff.fX));
+ SkScalarMul(invDiffLen, fDiff.fX));
} else {
sampler->matrix()->reset();
}
@@ -1944,7 +1933,8 @@
sampler->setWrapX(sk_tile_mode_to_grwrap(fTileMode));
sampler->setWrapY(sk_tile_mode_to_grwrap(kClamp_TileMode));
sampler->setFilter(GrSamplerState::kBilinear_Filter);
- return SkNEW_ARGS(GrRadial2Gradient, (context, *this));
+ return SkNEW_ARGS(GrRadial2Gradient, (context, *this, sampler,
+ diffLen, fStartRadius, fDiffRadius));
}
virtual void shadeSpan(int x, int y, SkPMColor* dstCParam,
@@ -2349,8 +2339,7 @@
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
- TileMode* xy,
- SkScalar* twoPointRadialParams) const {
+ TileMode* xy) const {
SkPoint diff = fCenter2 - fCenter1;
SkScalar diffRadius = fRadius2 - fRadius1;
@@ -2360,7 +2349,7 @@
if (bitmap) {
this->commonAsABitmap(bitmap);
}
- if (matrix || twoPointRadialParams) {
+ if (matrix) {
diffLen = diff.length();
}
if (matrix) {
@@ -2378,12 +2367,6 @@
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
- if (NULL != twoPointRadialParams) {
- twoPointRadialParams[0] = diffLen;
- twoPointRadialParams[1] = startRadius;
- twoPointRadialParams[2] = diffRadius;
- }
-
return kTwoPointConical_BitmapType;
}
@@ -2415,7 +2398,8 @@
sampler->setWrapX(sk_tile_mode_to_grwrap(fTileMode));
sampler->setWrapY(sk_tile_mode_to_grwrap(kClamp_TileMode));
sampler->setFilter(GrSamplerState::kBilinear_Filter);
- return SkNEW_ARGS(GrConical2Gradient, (context, *this));
+ return SkNEW_ARGS(GrConical2Gradient, (context, *this, sampler,
+ diffLen, fRadius1, fRadius2 - fRadius1));
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Two_Point_Conical_Gradient)
@@ -2462,8 +2446,7 @@
virtual BitmapType asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
- TileMode* xy,
- SkScalar* twoPointRadialParams) const SK_OVERRIDE {
+ TileMode* xy) const SK_OVERRIDE {
if (bitmap) {
this->commonAsABitmap(bitmap);
}
@@ -2491,7 +2474,7 @@
sampler->setWrapX(sk_tile_mode_to_grwrap(fTileMode));
sampler->setWrapY(sk_tile_mode_to_grwrap(kClamp_TileMode));
sampler->setFilter(GrSamplerState::kBilinear_Filter);
- return SkNEW_ARGS(GrSweepGradient, (context, *this));
+ return SkNEW_ARGS(GrSweepGradient, (context, *this, sampler));
}
SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sweep_Gradient)
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 57d9fbd..80e3f37 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -596,12 +596,26 @@
return false;
}
+ GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
+ GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
+
+ if (NULL != stage) {
+ sampler->setCustomStage(stage)->unref();
+ SkMatrix localM;
+ if (shader->getLocalMatrix(&localM)) {
+ SkMatrix inverse;
+ if (localM.invert(&inverse)) {
+ sampler->matrix()->preConcat(inverse);
+ }
+ }
+ return true;
+ }
+
SkBitmap bitmap;
- SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
+ SkMatrix* matrix = sampler->matrix();
SkShader::TileMode tileModes[2];
- SkScalar twoPointParams[3];
SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
- tileModes, twoPointParams);
+ tileModes);
if (SkShader::kNone_BitmapType == bmptype) {
SkShader::GradientInfo info;
@@ -627,13 +641,10 @@
}
// Must set wrap and filter on the sampler before requesting a texture.
- GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
- GrSamplerState::Filter filter;
- if (SkShader::kDefault_BitmapType == bmptype && !skPaint.isFilterBitmap()) {
- filter = GrSamplerState::kNearest_Filter;
- } else {
+ GrSamplerState::Filter filter = GrSamplerState::kNearest_Filter;
+ if (skPaint.isFilterBitmap()) {
filter = GrSamplerState::kBilinear_Filter;
}
sampler->setFilter(filter);
@@ -644,34 +655,7 @@
return false;
}
- switch (bmptype) {
- case SkShader::kRadial_BitmapType:
- sampler->setCustomStage(SkNEW_ARGS(GrRadialGradient, (texture)))->unref();
- break;
- case SkShader::kSweep_BitmapType:
- sampler->setCustomStage(SkNEW_ARGS(GrSweepGradient, (texture)))->unref();
- break;
- case SkShader::kTwoPointRadial_BitmapType:
- sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
- (texture,
- twoPointParams[0],
- twoPointParams[1],
- twoPointParams[2] < 0)))->unref();
- break;
- case SkShader::kTwoPointConical_BitmapType:
- sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
- (texture,
- twoPointParams[0],
- twoPointParams[1],
- twoPointParams[2])))->unref();
- break;
- case SkShader::kLinear_BitmapType:
- sampler->setCustomStage(SkNEW_ARGS(GrLinearGradient, (texture)))->unref();
- break;
- default:
- sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
- break;
- }
+ sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
// since our texture coords will be in local space, we wack the texture
// matrix to map them back into 0...1 before we load it
@@ -686,9 +670,6 @@
GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
matrix->postScale(sx, sy);
- } else if (SkShader::kRadial_BitmapType == bmptype) {
- GrScalar s = SkFloatToScalar(1.f / bitmap.width());
- matrix->postScale(s, s);
}
return true;
diff --git a/src/gpu/effects/GrGradientEffects.cpp b/src/gpu/effects/GrGradientEffects.cpp
index 2faeec0..34bff09 100644
--- a/src/gpu/effects/GrGradientEffects.cpp
+++ b/src/gpu/effects/GrGradientEffects.cpp
@@ -9,6 +9,7 @@
#include "gl/GrGLProgramStage.h"
#include "GrProgramStageFactory.h"
#include "SkGr.h"
+#include "../core/SkShader.h"
// Base class for GL gradient custom stages
class GrGLGradientStage : public GrGLProgramStage {
@@ -51,7 +52,9 @@
SkSafeRef(fTexture);
}
-GrGradientEffect::GrGradientEffect(GrContext* ctx, const SkShader& shader)
+GrGradientEffect::GrGradientEffect(GrContext* ctx,
+ const SkShader& shader,
+ GrSamplerState* sampler)
: fTexture (NULL)
, fUseTexture (false) {
// TODO: check for simple cases where we don't need a texture:
@@ -60,16 +63,10 @@
//if (info.fColorCount == 2) { ...
SkBitmap bitmap;
- shader.asABitmap(&bitmap, NULL, NULL, NULL);
-
- // Note: we just construct a default sampler state here, which isn't great,
- // however, as long as the bitmap has power-of-two dimensions, which should
- // be the case for gradient bitmaps, it should be fine
- GrAssert(SkIsPow2(bitmap.width()) && SkIsPow2(bitmap.height()));
- GrSamplerState sampler;
+ shader.asABitmap(&bitmap, NULL, NULL);
GrContext::TextureCacheEntry entry = GrLockCachedBitmapTexture(ctx, bitmap,
- &sampler);
+ sampler);
fTexture = entry.texture();
SkSafeRef(fTexture);
fUseTexture = true;
@@ -133,8 +130,10 @@
: INHERITED(texture) {
}
-GrLinearGradient::GrLinearGradient(GrContext* ctx, const SkShader& shader)
- : INHERITED(ctx, shader) {
+GrLinearGradient::GrLinearGradient(GrContext* ctx,
+ const SkShader& shader,
+ GrSamplerState* sampler)
+ : INHERITED(ctx, shader, sampler) {
}
GrLinearGradient::~GrLinearGradient() {
@@ -188,8 +187,9 @@
}
-GrRadialGradient::GrRadialGradient(GrContext* ctx, const SkShader& shader)
- : INHERITED(ctx, shader) {
+GrRadialGradient::GrRadialGradient(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler)
+ : INHERITED(ctx, shader, sampler) {
}
GrRadialGradient::~GrRadialGradient() {
@@ -445,17 +445,16 @@
}
-GrRadial2Gradient::GrRadial2Gradient(GrContext* ctx, const SkShader& shader)
- : INHERITED(ctx, shader) {
- SkShader::GradientInfo info;
- info.fColorCount = 0;
- shader.asAGradient(&info);
- fCenterX1 = SkPoint::Distance(info.fPoint[0], info.fPoint[1]);
- SkScalar diffRadius = info.fRadius[1] - info.fRadius[0];
- fPosRoot = diffRadius < 0;
- SkScalar inv = 0 == diffRadius ? 0 : SkScalarInvert(diffRadius);
- fRadius0 = SkScalarMul(info.fRadius[0], inv);
- fCenterX1 = SkScalarMul(fCenterX1, inv);
+GrRadial2Gradient::GrRadial2Gradient(GrContext* ctx,
+ const SkShader& shader,
+ GrSamplerState* sampler,
+ SkScalar center,
+ SkScalar startRadius,
+ SkScalar diffRadius)
+ : INHERITED(ctx, shader, sampler)
+ , fCenterX1(center)
+ , fRadius0(startRadius)
+ , fPosRoot(diffRadius < 0) {
}
GrRadial2Gradient::~GrRadial2Gradient() {
@@ -776,14 +775,16 @@
}
-GrConical2Gradient::GrConical2Gradient(GrContext* ctx, const SkShader& shader)
- : INHERITED(ctx, shader) {
- SkShader::GradientInfo info;
- info.fColorCount = 0;
- shader.asAGradient(&info);
- fCenterX1 = SkPoint::Distance(info.fPoint[0], info.fPoint[1]);
- fRadius0 = info.fRadius[0];
- fDiffRadius = info.fRadius[1] - info.fRadius[0];
+GrConical2Gradient::GrConical2Gradient(GrContext* ctx,
+ const SkShader& shader,
+ GrSamplerState* sampler,
+ SkScalar center,
+ SkScalar startRadius,
+ SkScalar diffRadius)
+ : INHERITED(ctx, shader, sampler)
+ , fCenterX1(center)
+ , fRadius0(startRadius)
+ , fDiffRadius(diffRadius) {
}
GrConical2Gradient::~GrConical2Gradient() {
@@ -846,8 +847,9 @@
}
-GrSweepGradient::GrSweepGradient(GrContext* ctx, const SkShader& shader)
- : INHERITED(ctx, shader) {
+GrSweepGradient::GrSweepGradient(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler)
+ : INHERITED(ctx, shader, sampler) {
}
GrSweepGradient::~GrSweepGradient() {
diff --git a/src/gpu/effects/GrGradientEffects.h b/src/gpu/effects/GrGradientEffects.h
index fdd5d52..190768d 100644
--- a/src/gpu/effects/GrGradientEffects.h
+++ b/src/gpu/effects/GrGradientEffects.h
@@ -11,7 +11,9 @@
#include "GrSingleTextureEffect.h"
#include "GrTypes.h"
#include "GrScalar.h"
-#include "SkShader.h"
+
+class GrSamplerState;
+class SkShader;
/*
* The intepretation of the texture matrix depends on the sample mode. The
@@ -41,7 +43,8 @@
public:
GrGradientEffect(GrTexture* texture);
- GrGradientEffect(GrContext* ctx, const SkShader& shader);
+ GrGradientEffect(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler);
virtual ~GrGradientEffect();
@@ -66,7 +69,8 @@
public:
GrLinearGradient(GrTexture* texture);
- GrLinearGradient(GrContext* ctx, const SkShader& shader);
+ GrLinearGradient(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler);
virtual ~GrLinearGradient();
static const char* Name() { return "Linear Gradient"; }
@@ -86,7 +90,8 @@
public:
GrRadialGradient(GrTexture* texture);
- GrRadialGradient(GrContext* ctx, const SkShader& shader);
+ GrRadialGradient(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler);
virtual ~GrRadialGradient();
static const char* Name() { return "Radial Gradient"; }
@@ -106,7 +111,9 @@
public:
GrRadial2Gradient(GrTexture* texture, GrScalar center, GrScalar radius, bool posRoot);
- GrRadial2Gradient(GrContext* ctx, const SkShader& shader);
+ GrRadial2Gradient(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler, SkScalar center,
+ SkScalar radius, SkScalar diffRadius);
virtual ~GrRadial2Gradient();
static const char* Name() { return "Two-Point Radial Gradient"; }
@@ -143,7 +150,9 @@
public:
GrConical2Gradient(GrTexture* texture, GrScalar center, GrScalar radius, GrScalar diffRadius);
- GrConical2Gradient(GrContext* ctx, const SkShader& shader);
+ GrConical2Gradient(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler, SkScalar center,
+ SkScalar radius, SkScalar diffRadius);
virtual ~GrConical2Gradient();
static const char* Name() { return "Two-Point Conical Gradient"; }
@@ -180,7 +189,8 @@
public:
GrSweepGradient(GrTexture* texture);
- GrSweepGradient(GrContext* ctx, const SkShader& shader);
+ GrSweepGradient(GrContext* ctx, const SkShader& shader,
+ GrSamplerState* sampler);
virtual ~GrSweepGradient();
static const char* Name() { return "Sweep Gradient"; }
diff --git a/src/pdf/SkPDFShader.cpp b/src/pdf/SkPDFShader.cpp
index cbbf5c6..09a4352 100644
--- a/src/pdf/SkPDFShader.cpp
+++ b/src/pdf/SkPDFShader.cpp
@@ -944,7 +944,7 @@
if (fType == SkShader::kNone_GradientType) {
SkShader::BitmapType bitmapType;
SkMatrix matrix;
- bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes, NULL);
+ bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes);
if (bitmapType != SkShader::kDefault_BitmapType) {
fImage.reset();
return;