Fix the way we patch up the matrix for scaled images that aren't

clamp/clamp

BUG=skia:2904
TBR=reed

Review URL: https://codereview.chromium.org/675823002
This commit is contained in:
humper 2014-10-27 10:32:06 -07:00 committed by Commit bot
parent 4477c3c0e6
commit 535e3b2025
3 changed files with 101 additions and 16 deletions

88
gm/tiledscaledbitmap.cpp Normal file
View File

@ -0,0 +1,88 @@
/*
* 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 "gm.h"
#include "Resources.h"
#include "SkBitmap.h"
#include "SkImageDecoder.h"
#include "SkPaint.h"
#include "SkShader.h"
#include "SkStream.h"
/***
*
* This GM reproduces Skia bug 2904, in which a tiled bitmap shader was failing to draw correctly
* when fractional image scaling was ignored by the high quality bitmap scaler.
*
***/
namespace skiagm {
class TiledScaledBitmapGM : public GM {
public:
TiledScaledBitmapGM() {
}
protected:
virtual SkString onShortName() {
return SkString("tiledscaledbitmap");
}
virtual SkISize onISize() {
return SkISize::Make(1016, 616);
}
virtual uint32_t onGetFlags() const SK_OVERRIDE {
return kSkipTiled_Flag;
}
static SkBitmap make_bm(int width, int height) {
SkBitmap bm;
bm.allocN32Pixels(width, height);
bm.eraseColor(SK_ColorTRANSPARENT);
SkCanvas canvas(bm);
SkPaint paint;
paint.setAntiAlias(true);
canvas.drawCircle(width/2.f, height/2.f, width/4.f, paint);
return bm;
}
virtual void onOnceBeforeDraw() SK_OVERRIDE {
fBitmap = make_bm(360, 288);
}
virtual void onDraw(SkCanvas* canvas) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
SkMatrix mat;
mat.setScale(121.f/360.f, 93.f/288.f);
mat.postTranslate(-72, -72);
SkShader *shader = SkShader::CreateBitmapShader(fBitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &mat);
paint.setShader(shader);
SkSafeUnref(shader);
canvas->drawRectCoords(8,8,1008, 608, paint);
}
private:
SkBitmap fBitmap;
typedef GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
DEF_GM(return SkNEW(TiledScaledBitmapGM);)
}

View File

@ -185,6 +185,7 @@
'../gm/texturedomaineffect.cpp', '../gm/texturedomaineffect.cpp',
'../gm/thinrects.cpp', '../gm/thinrects.cpp',
'../gm/thinstrokedrects.cpp', '../gm/thinstrokedrects.cpp',
'../gm/tiledscaledbitmap.cpp',
'../gm/tileimagefilter.cpp', '../gm/tileimagefilter.cpp',
'../gm/tilemodes.cpp', '../gm/tilemodes.cpp',
'../gm/tilemodes_scaled.cpp', '../gm/tilemodes_scaled.cpp',

View File

@ -131,8 +131,6 @@ static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) {
bool SkBitmapProcState::possiblyScaleImage() { bool SkBitmapProcState::possiblyScaleImage() {
SkASSERT(NULL == fBitmap); SkASSERT(NULL == fBitmap);
fAdjustedMatrix = false;
if (fFilterLevel <= SkPaint::kLow_FilterLevel) { if (fFilterLevel <= SkPaint::kLow_FilterLevel) {
return false; return false;
} }
@ -198,19 +196,10 @@ bool SkBitmapProcState::possiblyScaleImage() {
SkASSERT(fScaledBitmap.getPixels()); SkASSERT(fScaledBitmap.getPixels());
fBitmap = &fScaledBitmap; fBitmap = &fScaledBitmap;
// set the inv matrix type to translate-only; // clean up the inverse matrix by incorporating the scale we just performed.
fInvMatrix.setTranslate(fInvMatrix.getTranslateX() / fInvMatrix.getScaleX(),
fInvMatrix.getTranslateY() / fInvMatrix.getScaleY());
#ifndef SK_IGNORE_PROPER_FRACTIONAL_SCALING fInvMatrix.postScale(roundedDestWidth / fOrigBitmap.width(),
// reintroduce any fractional scaling missed by our integral scale done above. roundedDestHeight / fOrigBitmap.height());
float fractionalScaleX = roundedDestWidth/trueDestWidth;
float fractionalScaleY = roundedDestHeight/trueDestHeight;
fInvMatrix.postScale(fractionalScaleX, fractionalScaleY);
#endif
fAdjustedMatrix = true;
// Set our filter level to low -- the only post-filtering this // Set our filter level to low -- the only post-filtering this
// image might require is some interpolation if the translation // image might require is some interpolation if the translation
@ -374,8 +363,15 @@ bool SkBitmapProcState::chooseProcs(const SkMatrix& inv, const SkPaint& paint) {
bool clampClamp = SkShader::kClamp_TileMode == fTileModeX && bool clampClamp = SkShader::kClamp_TileMode == fTileModeX &&
SkShader::kClamp_TileMode == fTileModeY; SkShader::kClamp_TileMode == fTileModeY;
if (!(fAdjustedMatrix || clampClamp || trivialMatrix)) { // Most of the scanline procs deal with "unit" texture coordinates, as this
fInvMatrix.postIDiv(fOrigBitmap.width(), fOrigBitmap.height()); // makes it easy to perform tiling modes (repeat = (x & 0xFFFF)). To generate
// those, we divide the matrix by its dimensions here.
//
// We don't do this if we're either trivial (can ignore the matrix) or clamping
// in both X and Y since clamping to width,height is just as easy as to 0xFFFF.
if (!(clampClamp || trivialMatrix)) {
fInvMatrix.postIDiv(fBitmap->width(), fBitmap->height());
} }
// Now that all possible changes to the matrix have taken place, check // Now that all possible changes to the matrix have taken place, check