diff options
author | halcanary <halcanary@google.com> | 2014-07-18 09:18:40 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-07-18 09:18:41 -0700 |
commit | 1d1795b199f216e17ff6b63f8c8c1f653666cb79 (patch) | |
tree | 326e009de413a8d684a23a33d6aa4eafd7b11c44 | |
parent | 63f413ce94358e3e76dca66218c7da20fec18569 (diff) |
Make ScaledImageCache unit test less dependent on global state.
motivation: The ASAN bot routinely trips this unit test.
R=tomhudson@chromium.org, tomhudson@google.com
Author: halcanary@google.com
Review URL: https://codereview.chromium.org/401833002
-rw-r--r-- | tests/ScaledImageCache.cpp | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/tests/ScaledImageCache.cpp b/tests/ScaledImageCache.cpp index 9515e134e4..ee7143faec 100644 --- a/tests/ScaledImageCache.cpp +++ b/tests/ScaledImageCache.cpp @@ -5,57 +5,73 @@ * found in the LICENSE file. */ #include "Test.h" -#include "SkGraphics.h" #include "SkCanvas.h" +#include "SkGraphics.h" +#include "SkScaledImageCache.h" static const int kCanvasSize = 1; static const int kBitmapSize = 16; static const int kScale = 8; -static size_t test_scaled_image_cache_useage() { +static bool is_in_scaled_image_cache(const SkBitmap& orig, + SkScalar xScale, + SkScalar yScale) { + SkBitmap scaled; + SkScaledImageCache::ID* id = SkScaledImageCache::FindAndLock( + orig, SkScalarInvert(xScale), SkScalarInvert(yScale), &scaled); + if (id) { + SkScaledImageCache::Unlock(id); + } + return id != NULL; +} + +// Draw a scaled bitmap, then return true iff it has been cached. +static bool test_scaled_image_cache_useage() { SkAutoTUnref<SkCanvas> canvas( SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); SkBitmap bitmap; SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize)); bitmap.eraseColor(0xFFFFFFFF); - SkScalar scaledSize = SkIntToScalar(kScale * kBitmapSize); + SkScalar scale = SkIntToScalar(kScale); + SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); SkPaint paint; paint.setFilterLevel(SkPaint::kHigh_FilterLevel); - size_t bytesUsed = SkGraphics::GetImageCacheBytesUsed(); + canvas->drawBitmapRect(bitmap, SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), &paint); - return SkGraphics::GetImageCacheBytesUsed() - bytesUsed; + + return is_in_scaled_image_cache(bitmap, scale, scale); } // http://crbug.com/389439 DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) { - size_t originalByteLimit = SkGraphics::GetImageCacheByteLimit(); + size_t originalByteLimit = SkGraphics::GetImageCacheTotalByteLimit(); size_t originalAllocationLimit = SkGraphics::GetImageCacheSingleAllocationByteLimit(); size_t size = kBitmapSize * kScale * kBitmapSize * kScale * SkColorTypeBytesPerPixel(kN32_SkColorType); - SkGraphics::SetImageCacheByteLimit(0); // clear cache - SkGraphics::SetImageCacheByteLimit(2 * size); - SkGraphics::SetImageCacheSingleAllocationByteLimit(0); + SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache + SkGraphics::SetImageCacheTotalByteLimit(2 * size); + SkGraphics::SetImageCacheSingleAllocationByteLimit(0); // No limit - REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage()); + REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); - SkGraphics::SetImageCacheByteLimit(0); // clear cache - SkGraphics::SetImageCacheByteLimit(2 * size); - SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); + SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache + SkGraphics::SetImageCacheTotalByteLimit(2 * size); + SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); // big enough - REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage()); + REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); - SkGraphics::SetImageCacheByteLimit(0); // clear cache - SkGraphics::SetImageCacheByteLimit(2 * size); - SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); + SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache + SkGraphics::SetImageCacheTotalByteLimit(2 * size); + SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); // too small - REPORTER_ASSERT(reporter, 0 == test_scaled_image_cache_useage()); + REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit); - SkGraphics::SetImageCacheByteLimit(originalByteLimit); + SkGraphics::SetImageCacheTotalByteLimit(originalByteLimit); } |