change asABitmap to isABitmap on shader

BUG=skia:
TBR=

Review URL: https://codereview.chromium.org/1287263005
This commit is contained in:
reed 2015-08-19 11:46:38 -07:00 committed by Commit bot
parent 790d74f7c1
commit f5822825ec
19 changed files with 54 additions and 171 deletions

View File

@ -17,6 +17,7 @@
# Needed until we fix skbug.com/2440.
'SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG',
'SK_IGNORE_LINEONLY_AA_CONVEX_PATH_OPTS',
'SK_SUPPORT_LEGACY_SHADERBITMAPTYPE',
],
},
}

View File

@ -223,6 +223,11 @@ public:
return (flags & kHasSpan16_Flag) != 0;
}
#ifdef SK_SUPPORT_LEGACY_SHADERBITMAPTYPE
public:
#else
protected:
#endif
/**
Gives method bitmap should be read to implement a shader.
Also determines number and interpretation of "extra" parameters returned
@ -231,30 +236,6 @@ public:
enum BitmapType {
kNone_BitmapType, //<! Shader is not represented as a bitmap
kDefault_BitmapType,//<! Access bitmap using local coords transformed
// by matrix. No extras
kRadial_BitmapType, //<! Access bitmap by transforming local coordinates
// by the matrix and taking the distance of result
// from (0,0) as bitmap column. Bitmap is 1 pixel
// tall. No extras
kSweep_BitmapType, //<! Access bitmap by transforming local coordinates
// by the matrix and taking the angle of result
// to (0,0) as bitmap x coord, where angle = 0 is
// bitmap left edge of bitmap = 2pi is the
// right edge. Bitmap is 1 pixel tall. No extras
kTwoPointConical_BitmapType,
//<! Matrix transforms to space where (0,0) is
// the center of the starting circle. The second
// circle will be centered (x, 0) where x may be
// 0.
// Three extra parameters are returned:
// 0: x-offset of second circle center
// to first.
// 1: radius of first circle
// 2: the second radius minus the first radius
kLinear_BitmapType, //<! Access bitmap using local coords transformed
// by matrix. No extras
kLast_BitmapType = kLinear_BitmapType
};
/** Optional methods for shaders that can pretend to be a bitmap/texture
to play along with opengl. Default just returns kNone_BitmapType and
@ -274,6 +255,14 @@ public:
virtual BitmapType asABitmap(SkBitmap* outTexture, SkMatrix* outMatrix,
TileMode xy[2]) const;
public:
bool isABitmap(SkBitmap* bitmap, SkMatrix* matrix, TileMode xy[2]) const {
return this->asABitmap(bitmap, matrix, xy) == kDefault_BitmapType;
}
bool isABitmap() const {
return this->isABitmap(nullptr, nullptr, nullptr);
}
/**
* If the shader subclass can be represented as a gradient, asAGradient
* returns the matching GradientType enum (or kNone_GradientType if it

View File

@ -47,11 +47,6 @@ public:
typedef SkShader::Context INHERITED;
};
// we return false for this, use asAGradient
virtual BitmapType asABitmap(SkBitmap* outTexture,
SkMatrix* outMatrix,
TileMode xy[2]) const override;
GradientType asAGradient(GradientInfo* info) const override;
bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix& viewM,

View File

@ -59,8 +59,7 @@ struct SkBitmapHunter {
const SkPaint* paint = AsPtr(r.paint);
if (paint) {
const SkShader* shader = paint->getShader();
if (shader &&
shader->asABitmap(nullptr, nullptr, nullptr) == SkShader::kDefault_BitmapType) {
if (shader && shader->isABitmap()) {
return true;
}
}

View File

@ -83,12 +83,14 @@ struct BitmapShaderRec : public SkResourceCache::Rec {
result->reset(SkRef(rec.fShader.get()));
SkBitmap tile;
rec.fShader.get()->asABitmap(&tile, NULL, NULL);
// FIXME: this doesn't protect the pixels from being discarded as soon as we unlock.
// Should be handled via a pixel ref generator instead
// (https://code.google.com/p/skia/issues/detail?id=3220).
SkAutoLockPixels alp(tile, true);
return tile.getPixels() != NULL;
if (rec.fShader.get()->isABitmap(&tile, NULL, NULL)) {
// FIXME: this doesn't protect the pixels from being discarded as soon as we unlock.
// Should be handled via a pixel ref generator instead
// (https://code.google.com/p/skia/issues/detail?id=3220).
SkAutoLockPixels alp(tile, true);
return tile.getPixels() != NULL;
}
return false;
}
};

View File

@ -325,12 +325,6 @@ void SkColorShader::ColorShaderContext::shadeSpanAlpha(int x, int y, uint8_t alp
memset(alpha, SkGetPackedA32(fPMColor), count);
}
// if we had a asAColor method, that would be more efficient...
SkShader::BitmapType SkColorShader::asABitmap(SkBitmap* bitmap, SkMatrix* matrix,
TileMode modes[]) const {
return kNone_BitmapType;
}
SkShader::GradientType SkColorShader::asAGradient(GradientInfo* info) const {
if (info) {
if (info->fColors && info->fColorCount >= 1) {

View File

@ -1071,33 +1071,24 @@ HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
SkBitmap outTexture;
SkMatrix outMatrix;
SkShader::TileMode xy[2];
SkShader::BitmapType bitmapType = shader->asABitmap(&outTexture,
&outMatrix,
xy);
switch (bitmapType) {
case SkShader::kDefault_BitmapType: {
//TODO: outMatrix??
SkMatrix localMatrix = shader->getLocalMatrix();
if (parentTransform) {
localMatrix.preConcat(*parentTransform);
}
SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
HR(this->createXpsImageBrush(outTexture,
localMatrix,
xy,
skPaint.getAlpha(),
&tileBrush));
HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
return S_OK;
if (shader->isABitmap(&outTexture, &outMatrix, xy)) {
//TODO: outMatrix??
SkMatrix localMatrix = shader->getLocalMatrix();
if (parentTransform) {
localMatrix.preConcat(*parentTransform);
}
default:
break;
}
HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
HR(this->createXpsImageBrush(outTexture,
localMatrix,
xy,
skPaint.getAlpha(),
&tileBrush));
HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
} else {
HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
}
return S_OK;
}

View File

@ -261,22 +261,6 @@ void SkLinearGradient::LinearGradientContext::shadeSpan(int x, int y, SkPMColor*
}
}
SkShader::BitmapType SkLinearGradient::asABitmap(SkBitmap* bitmap,
SkMatrix* matrix,
TileMode xy[]) const {
if (bitmap) {
this->getGradientTableBitmap(bitmap);
}
if (matrix) {
matrix->preConcat(fPtsToUnit);
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
return kLinear_BitmapType;
}
SkShader::GradientType SkLinearGradient::asAGradient(GradientInfo* info) const {
if (info) {
commonAsAGradient(info);

View File

@ -28,7 +28,6 @@ public:
typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED;
};
BitmapType asABitmap(SkBitmap*, SkMatrix*, TileMode*) const override;
GradientType asAGradient(GradientInfo* info) const override;
bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix& viewM,
const SkMatrix*, GrColor*, GrProcessorDataManager*,

View File

@ -225,23 +225,6 @@ void SkRadialGradient::RadialGradientContext::shadeSpan16(int x, int y, uint16_t
}
}
SkShader::BitmapType SkRadialGradient::asABitmap(SkBitmap* bitmap,
SkMatrix* matrix, SkShader::TileMode* xy) const {
if (bitmap) {
this->getGradientTableBitmap(bitmap);
}
if (matrix) {
matrix->setScale(SkIntToScalar(kCache32Count),
SkIntToScalar(kCache32Count));
matrix->preConcat(fPtsToUnit);
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
return kRadial_BitmapType;
}
SkShader::GradientType SkRadialGradient::asAGradient(GradientInfo* info) const {
if (info) {
commonAsAGradient(info);

View File

@ -28,7 +28,6 @@ public:
typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED;
};
BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix, TileMode* xy) const override;
GradientType asAGradient(GradientInfo* info) const override;
bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix& viewM,
const SkMatrix*, GrColor*, GrProcessorDataManager*,

View File

@ -22,21 +22,6 @@ SkSweepGradient::SkSweepGradient(SkScalar cx, SkScalar cy, const Descriptor& des
fTileMode = SkShader::kClamp_TileMode;
}
SkShader::BitmapType SkSweepGradient::asABitmap(SkBitmap* bitmap,
SkMatrix* matrix, SkShader::TileMode* xy) const {
if (bitmap) {
this->getGradientTableBitmap(bitmap);
}
if (matrix) {
*matrix = fPtsToUnit;
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
return kSweep_BitmapType;
}
SkShader::GradientType SkSweepGradient::asAGradient(GradientInfo* info) const {
if (info) {
commonAsAGradient(info);

View File

@ -28,8 +28,6 @@ public:
typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED;
};
BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix, TileMode* xy) const override;
GradientType asAGradient(GradientInfo* info) const override;
bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix& viewM,

View File

@ -292,35 +292,6 @@ void SkTwoPointConicalGradient::TwoPointConicalGradientContext::shadeSpan(
}
}
SkShader::BitmapType SkTwoPointConicalGradient::asABitmap(
SkBitmap* bitmap, SkMatrix* matrix, SkShader::TileMode* xy) const {
SkPoint diff = fCenter2 - fCenter1;
SkScalar diffLen = 0;
if (bitmap) {
this->getGradientTableBitmap(bitmap);
}
if (matrix) {
diffLen = diff.length();
}
if (matrix) {
if (diffLen) {
SkScalar invDiffLen = SkScalarInvert(diffLen);
// rotate to align circle centers with the x-axis
matrix->setSinCos(-SkScalarMul(invDiffLen, diff.fY),
SkScalarMul(invDiffLen, diff.fX));
} else {
matrix->reset();
}
matrix->preTranslate(-fCenter1.fX, -fCenter1.fY);
}
if (xy) {
xy[0] = fTileMode;
xy[1] = kClamp_TileMode;
}
return kTwoPointConical_BitmapType;
}
// Returns the original non-sorted version of the gradient
SkShader::GradientType SkTwoPointConicalGradient::asAGradient(
GradientInfo* info) const {

View File

@ -58,7 +58,6 @@ public:
typedef SkGradientShaderBase::GradientShaderBaseContext INHERITED;
};
BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix, TileMode* xy) const override;
SkShader::GradientType asAGradient(GradientInfo* info) const override;
bool asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix&, const SkMatrix*,
GrColor*, GrProcessorDataManager*,

View File

@ -1110,10 +1110,10 @@ SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransfor
fType = shader.asAGradient(&fInfo);
if (fType == SkShader::kNone_GradientType) {
SkShader::BitmapType bitmapType;
SkMatrix matrix;
bitmapType = shader.asABitmap(&fImage, &matrix, fImageTileModes);
if (bitmapType != SkShader::kDefault_BitmapType) {
if (shader.isABitmap(&fImage, &matrix, fImageTileModes)) {
SkASSERT(matrix.isIdentity());
} else {
// Generic fallback for unsupported shaders:
// * allocate a bbox-sized bitmap
// * shade the whole area
@ -1153,8 +1153,6 @@ SkPDFShader::State::State(const SkShader& shader, const SkMatrix& canvasTransfor
fShaderTransform.setTranslate(shaderRect.x(), shaderRect.y());
fShaderTransform.preScale(1 / scale.width(), 1 / scale.height());
} else {
SkASSERT(matrix.isIdentity());
}
fPixelGeneration = fImage.getGenerationID();
} else {

View File

@ -1200,23 +1200,20 @@ static int lshader_isOpaque(lua_State* L) {
return shader && shader->isOpaque();
}
static int lshader_asABitmap(lua_State* L) {
static int lshader_isABitmap(lua_State* L) {
SkShader* shader = get_ref<SkShader>(L, 1);
if (shader) {
SkBitmap bm;
SkMatrix matrix;
SkShader::TileMode modes[2];
switch (shader->asABitmap(&bm, &matrix, modes)) {
case SkShader::kDefault_BitmapType:
lua_newtable(L);
setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
setfield_number(L, "width", bm.width());
setfield_number(L, "height", bm.height());
setfield_string(L, "tileX", mode2string(modes[0]));
setfield_string(L, "tileY", mode2string(modes[1]));
return 1;
default:
break;
if (shader->isABitmap(&bm, &matrix, modes)) {
lua_newtable(L);
setfield_number(L, "genID", bm.pixelRef() ? bm.pixelRef()->getGenerationID() : 0);
setfield_number(L, "width", bm.width());
setfield_number(L, "height", bm.height());
setfield_string(L, "tileX", mode2string(modes[0]));
setfield_string(L, "tileY", mode2string(modes[1]));
return 1;
}
}
return 0;
@ -1260,7 +1257,7 @@ static int lshader_gc(lua_State* L) {
static const struct luaL_Reg gSkShader_Methods[] = {
{ "isOpaque", lshader_isOpaque },
{ "asABitmap", lshader_asABitmap },
{ "isABitmap", lshader_isABitmap },
{ "asAGradient", lshader_asAGradient },
{ "__gc", lshader_gc },
{ NULL, NULL }

View File

@ -23,7 +23,7 @@ void CheckShader(SkPaint* paint) {
return;
}
if (shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType) {
if (shader->isABitmap()) {
return;
}
if (shader->asACompose(NULL)) {

View File

@ -71,8 +71,7 @@ static void test_analysis(skiatest::Reporter* reporter) {
SkShader* shader = SkShader::CreateBitmapShader(bitmap, SkShader::kClamp_TileMode,
SkShader::kClamp_TileMode);
paint.setShader(shader)->unref();
REPORTER_ASSERT(reporter,
shader->asABitmap(NULL, NULL, NULL) == SkShader::kDefault_BitmapType);
REPORTER_ASSERT(reporter, shader->isABitmap());
canvas->drawRect(SkRect::MakeWH(10, 10), paint);
}