From f622a6c8fd176acf9944de8df00d7f0bb56b67d3 Mon Sep 17 00:00:00 2001 From: halcanary Date: Fri, 24 Oct 2014 12:54:53 -0700 Subject: [PATCH] Add `SkIRect bounds()` convenience method to SkImageInfo and SkBitmap. Review URL: https://codereview.chromium.org/646213003 --- gm/texturedomaineffect.cpp | 5 ++--- include/core/SkBitmap.h | 3 +++ include/core/SkImageInfo.h | 4 +++- src/core/SkCanvas.cpp | 2 +- src/lazy/SkCachingPixelRef.cpp | 10 ++++------ src/pdf/SkPDFDevice.cpp | 4 ++-- tests/ImageNewShaderTest.cpp | 2 +- tests/WritePixelsTest.cpp | 2 +- 8 files changed, 17 insertions(+), 15 deletions(-) diff --git a/gm/texturedomaineffect.cpp b/gm/texturedomaineffect.cpp index acf039513d..d235f7f86b 100644 --- a/gm/texturedomaineffect.cpp +++ b/gm/texturedomaineffect.cpp @@ -105,15 +105,14 @@ protected: textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f); const SkIRect texelDomains[] = { - SkIRect::MakeWH(fBmp.width(), fBmp.height()), + fBmp.bounds(), SkIRect::MakeXYWH(fBmp.width() / 4, fBmp.height() / 4, fBmp.width() / 2, fBmp.height() / 2), }; - SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp.width()), - SkIntToScalar(fBmp.height())); + SkRect renderRect = SkRect::Make(fBmp.bounds()); renderRect.outset(kDrawPad, kDrawPad); SkScalar y = kDrawPad + kTestPad; diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h index b36a1fd3fd..cefc30ac82 100644 --- a/include/core/SkBitmap.h +++ b/include/core/SkBitmap.h @@ -220,6 +220,9 @@ public: void getBounds(SkRect* bounds) const; void getBounds(SkIRect* bounds) const; + SkIRect bounds() const { return fInfo.bounds(); } + SkISize dimensions() const { return fInfo.dimensions(); } + bool setInfo(const SkImageInfo&, size_t rowBytes = 0); /** diff --git a/include/core/SkImageInfo.h b/include/core/SkImageInfo.h index 7fedfa1a4a..6fbaf6bbce 100644 --- a/include/core/SkImageInfo.h +++ b/include/core/SkImageInfo.h @@ -9,10 +9,11 @@ #define SkImageInfo_DEFINED #include "SkMath.h" +#include "SkRect.h" #include "SkSize.h" -class SkWriteBuffer; class SkReadBuffer; +class SkWriteBuffer; /** * Describes how to interpret the alpha compoent of a pixel. @@ -207,6 +208,7 @@ public: return SkAlphaTypeIsOpaque(fAlphaType); } + SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); } SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } /** diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp index 2f7fcf4d4b..bb6d1baad2 100644 --- a/src/core/SkCanvas.cpp +++ b/src/core/SkCanvas.cpp @@ -1260,7 +1260,7 @@ void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, SkIPoint offset = SkIPoint::Make(0, 0); SkMatrix matrix = *iter.fMatrix; matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y())); - SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); + const SkIRect clipBounds = bitmap.bounds(); SkAutoTUnref cache(iter.fDevice->getImageFilterCache()); SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); if (filter->filterImage(&proxy, bitmap, ctx, &dst, &offset)) { diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp index 1459567e0b..5ab96562ec 100644 --- a/src/lazy/SkCachingPixelRef.cpp +++ b/src/lazy/SkCachingPixelRef.cpp @@ -45,9 +45,8 @@ bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { } const SkImageInfo& info = this->info(); - if (!SkBitmapCache::Find(this->getGenerationID(), - SkIRect::MakeWH(info.width(), info.height()), - &fLockedBitmap)) { + if (!SkBitmapCache::Find( + this->getGenerationID(), info.bounds(), &fLockedBitmap)) { // Cache has been purged, must re-decode. if (!fLockedBitmap.tryAllocPixels(info, fRowBytes)) { fErrorInDecoding = true; @@ -58,9 +57,8 @@ bool SkCachingPixelRef::onNewLockPixels(LockRec* rec) { return false; } fLockedBitmap.setImmutable(); - SkBitmapCache::Add(this->getGenerationID(), - SkIRect::MakeWH(info.width(), info.height()), - fLockedBitmap); + SkBitmapCache::Add( + this->getGenerationID(), info.bounds(), fLockedBitmap); } // Now bitmap should contain a concrete PixelRef of the decoded image. diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp index 8509de86fc..b14ca72902 100644 --- a/src/pdf/SkPDFDevice.cpp +++ b/src/pdf/SkPDFDevice.cpp @@ -745,7 +745,7 @@ SkPDFDevice::SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize, fInitialTransform.preConcat(initialTransform); fLegacyBitmap.setInfo(info); - SkIRect existingClip = SkIRect::MakeWH(info.width(), info.height()); + SkIRect existingClip = info.bounds(); fExistingClipRegion.setRect(existingClip); this->init(); } @@ -2181,7 +2181,7 @@ void SkPDFDevice::internalDrawBitmap(const SkMatrix& origMatrix, scaled.setScale(SK_Scalar1, -SK_Scalar1); scaled.postTranslate(0, SK_Scalar1); // Scale the image up from 1x1 to WxH. - SkIRect subset = SkIRect::MakeWH(bitmap->width(), bitmap->height()); + SkIRect subset = bitmap->bounds(); scaled.postScale(SkIntToScalar(subset.width()), SkIntToScalar(subset.height())); scaled.postConcat(matrix); diff --git a/tests/ImageNewShaderTest.cpp b/tests/ImageNewShaderTest.cpp index dfc5a24c33..1bc77fba66 100644 --- a/tests/ImageNewShaderTest.cpp +++ b/tests/ImageNewShaderTest.cpp @@ -56,7 +56,7 @@ void runShaderTest(skiatest::Reporter* reporter, SkSurface* sourceSurface, SkSur destinationCanvas->clear(SK_ColorTRANSPARENT); destinationCanvas->drawPaint(paint); - SkIRect rect = SkIRect::MakeWH(info.width(), info.height()); + SkIRect rect = info.bounds(); SkBitmap bmOrig; sourceSurface->getCanvas()->readPixels(rect, &bmOrig); diff --git a/tests/WritePixelsTest.cpp b/tests/WritePixelsTest.cpp index 567d99412e..eca5adec82 100644 --- a/tests/WritePixelsTest.cpp +++ b/tests/WritePixelsTest.cpp @@ -195,7 +195,7 @@ static bool check_write(skiatest::Reporter* reporter, SkCanvas* canvas, const Sk // At some point this will be unsupported, as we won't allow accessBitmap() to magically call // readPixels for the client. SkBitmap secretDevBitmap; - canvas->readPixels(SkIRect::MakeWH(canvasInfo.width(), canvasInfo.height()), &secretDevBitmap); + canvas->readPixels(canvasInfo.bounds(), &secretDevBitmap); SkAutoLockPixels alp(secretDevBitmap); canvasRowBytes = secretDevBitmap.rowBytes();