halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #include "Test.h" |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 8 | #include "SkCanvas.h" |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 9 | #include "SkGraphics.h" |
| 10 | #include "SkScaledImageCache.h" |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 11 | |
| 12 | static const int kCanvasSize = 1; |
| 13 | static const int kBitmapSize = 16; |
| 14 | static const int kScale = 8; |
| 15 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 16 | static bool is_in_scaled_image_cache(const SkBitmap& orig, |
| 17 | SkScalar xScale, |
| 18 | SkScalar yScale) { |
| 19 | SkBitmap scaled; |
| 20 | SkScaledImageCache::ID* id = SkScaledImageCache::FindAndLock( |
| 21 | orig, SkScalarInvert(xScale), SkScalarInvert(yScale), &scaled); |
| 22 | if (id) { |
| 23 | SkScaledImageCache::Unlock(id); |
| 24 | } |
| 25 | return id != NULL; |
| 26 | } |
| 27 | |
| 28 | // Draw a scaled bitmap, then return true iff it has been cached. |
| 29 | static bool test_scaled_image_cache_useage() { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 30 | SkAutoTUnref<SkCanvas> canvas( |
| 31 | SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); |
| 32 | SkBitmap bitmap; |
| 33 | SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize)); |
halcanary | 0db38cc | 2014-07-17 10:17:28 -0700 | [diff] [blame] | 34 | bitmap.eraseColor(0xFFFFFFFF); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 35 | SkScalar scale = SkIntToScalar(kScale); |
| 36 | SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 37 | canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); |
| 38 | SkPaint paint; |
| 39 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 40 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 41 | canvas->drawBitmapRect(bitmap, |
| 42 | SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), |
| 43 | &paint); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 44 | |
| 45 | return is_in_scaled_image_cache(bitmap, scale, scale); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // http://crbug.com/389439 |
| 49 | DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) { |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 50 | size_t originalByteLimit = SkGraphics::GetImageCacheTotalByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 51 | size_t originalAllocationLimit = |
| 52 | SkGraphics::GetImageCacheSingleAllocationByteLimit(); |
| 53 | |
| 54 | size_t size = kBitmapSize * kScale * kBitmapSize * kScale |
| 55 | * SkColorTypeBytesPerPixel(kN32_SkColorType); |
| 56 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 57 | SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache |
| 58 | SkGraphics::SetImageCacheTotalByteLimit(2 * size); |
| 59 | SkGraphics::SetImageCacheSingleAllocationByteLimit(0); // No limit |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 60 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 61 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 62 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 63 | SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache |
| 64 | SkGraphics::SetImageCacheTotalByteLimit(2 * size); |
| 65 | SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); // big enough |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 66 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 68 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 69 | SkGraphics::SetImageCacheTotalByteLimit(0); // clear cache |
| 70 | SkGraphics::SetImageCacheTotalByteLimit(2 * size); |
| 71 | SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); // too small |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 72 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 73 | REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 74 | |
| 75 | SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 76 | SkGraphics::SetImageCacheTotalByteLimit(originalByteLimit); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 77 | } |