From 983dc2541a729609037a05eba731b3eb9788c517 Mon Sep 17 00:00:00 2001 From: fmalita Date: Thu, 7 Jan 2016 08:02:25 -0800 Subject: SkTreatAsSprite should take AA into account Currently we always call SkTreatAsSprite with 0 subpixel bits, which means subpixel translations are ignored. This is incorrect for the anti-aliased case (drawSprite always pixel-snaps, so we lose edge AA). The CL updates SkTreatAsSprite to take an SkPaint argument and use 8 subpixel bits when AA is requested. Also remove unused SkTreatAsSpriteFilter. BUG=skia:4761 R=reed@google.com GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1566943002 Review URL: https://codereview.chromium.org/1566943002 --- src/core/SkCanvas.cpp | 3 +-- src/core/SkDraw.cpp | 14 +++----------- src/core/SkMatrix.cpp | 12 +++++++++--- src/core/SkMatrixUtils.h | 29 ++++++----------------------- 4 files changed, 19 insertions(+), 39 deletions(-) (limited to 'src/core') diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp index bc354ab3c3..82ceba4fa9 100644 --- a/src/core/SkCanvas.cpp +++ b/src/core/SkCanvas.cpp @@ -2222,8 +2222,7 @@ bool SkCanvas::canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const } const SkMatrix& ctm = this->getTotalMatrix(); - const unsigned kSubpixelBits = 0; // matching SkDraw::drawBitmap() - if (!SkTreatAsSprite(ctm, w, h, kSubpixelBits)) { + if (!SkTreatAsSprite(ctm, SkISize::Make(w, h), paint)) { return false; } diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp index 249795d0d1..246c31ba51 100644 --- a/src/core/SkDraw.cpp +++ b/src/core/SkDraw.cpp @@ -1172,20 +1172,11 @@ void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint, proc(*devPathPtr, *fRC, blitter); } -/** For the purposes of drawing bitmaps, if a matrix is "almost" translate - go ahead and treat it as if it were, so that subsequent code can go fast. - */ -static bool just_translate(const SkMatrix& matrix, const SkBitmap& bitmap) { - unsigned bits = 0; // TODO: find a way to allow the caller to tell us to - // respect filtering. - return SkTreatAsSprite(matrix, bitmap.width(), bitmap.height(), bits); -} - void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap, const SkPaint& paint) const { SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType); - if (just_translate(*fMatrix, bitmap)) { + if (SkTreatAsSprite(*fMatrix, bitmap.dimensions(), paint)) { int ix = SkScalarRoundToInt(fMatrix->getTranslateX()); int iy = SkScalarRoundToInt(fMatrix->getTranslateY()); @@ -1300,7 +1291,8 @@ void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix, return; } - if (bitmap.colorType() != kAlpha_8_SkColorType && just_translate(matrix, bitmap)) { + if (bitmap.colorType() != kAlpha_8_SkColorType + && SkTreatAsSprite(matrix, bitmap.dimensions(), paint)) { // // It is safe to call lock pixels now, since we know the matrix is // (more or less) identity. diff --git a/src/core/SkMatrix.cpp b/src/core/SkMatrix.cpp index dfeb721d45..13c626af47 100644 --- a/src/core/SkMatrix.cpp +++ b/src/core/SkMatrix.cpp @@ -1623,8 +1623,14 @@ void SkMatrix::toString(SkString* str) const { #include "SkMatrixUtils.h" -bool SkTreatAsSprite(const SkMatrix& mat, int width, int height, - unsigned subpixelBits) { +bool SkTreatAsSprite(const SkMatrix& mat, const SkISize& size, const SkPaint& paint) { + // Our path aa is 2-bits, and our rect aa is 8, so we could use 8, + // but in practice 4 seems enough (still looks smooth) and allows + // more slightly fractional cases to fall into the fast (sprite) case. + static const unsigned kAntiAliasSubpixelBits = 4; + + const unsigned subpixelBits = paint.isAntiAlias() ? kAntiAliasSubpixelBits : 0; + // quick reject on affine or perspective if (mat.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) { return false; @@ -1641,7 +1647,7 @@ bool SkTreatAsSprite(const SkMatrix& mat, int width, int height, } SkRect dst; - SkIRect isrc = { 0, 0, width, height }; + SkIRect isrc = SkIRect::MakeSize(size); { SkRect src; diff --git a/src/core/SkMatrixUtils.h b/src/core/SkMatrixUtils.h index d1b6658d07..0e01fbe953 100644 --- a/src/core/SkMatrixUtils.h +++ b/src/core/SkMatrixUtils.h @@ -8,37 +8,20 @@ #ifndef SkMatrixUtils_DEFINED #define SkMatrixUtils_DEFINED -#include "SkMatrix.h" +#include "SkSize.h" -/** - * Number of subpixel bits used in skia's bilerp. - * See SkBitmapProcState_procs.h and SkBitmapProcState_filter.h - */ -#define kSkSubPixelBitsForBilerp 4 +class SkMatrix; +class SkPaint; /** - * Given a matrix and width/height, return true if the computed dst-rect would + * Given a matrix, size and paint, return true if the computed dst-rect would * align such that there is a 1-to-1 coorspondence between src and dst pixels. * This can be called by drawing code to see if drawBitmap can be turned into * drawSprite (which is faster). * - * The src-rect is defined to be { 0, 0, width, height } - * - * The "closeness" test is based on the subpixelBits parameter. Pass 0 for - * round-to-nearest behavior (e.g. nearest neighbor sampling). Pass the number - * of subpixel-bits to simulate filtering. - */ -bool SkTreatAsSprite(const SkMatrix&, int width, int height, - unsigned subpixelBits); - -/** - * Calls SkTreatAsSprite() with default subpixelBits value to match Skia's - * filter-bitmap implementation (i.e. kSkSubPixelBitsForBilerp). + * The src-rect is defined to be { 0, 0, size.width(), size.height() } */ -static inline bool SkTreatAsSpriteFilter(const SkMatrix& matrix, - int width, int height) { - return SkTreatAsSprite(matrix, width, height, kSkSubPixelBitsForBilerp); -} +bool SkTreatAsSprite(const SkMatrix&, const SkISize& size, const SkPaint& paint); /** Decomposes the upper-left 2x2 of the matrix into a rotation (represented by the cosine and sine of the rotation angle), followed by a non-uniform scale, -- cgit v1.2.3