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" |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 10 | #include "SkBitmapCache.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; |
humper | d73c169 | 2014-08-28 14:27:42 -0700 | [diff] [blame] | 20 | float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); |
| 21 | float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale); |
| 22 | return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | // Draw a scaled bitmap, then return true iff it has been cached. |
| 26 | static bool test_scaled_image_cache_useage() { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 27 | SkAutoTUnref<SkCanvas> canvas( |
| 28 | SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); |
| 29 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame^] | 30 | bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
halcanary | 0db38cc | 2014-07-17 10:17:28 -0700 | [diff] [blame] | 31 | bitmap.eraseColor(0xFFFFFFFF); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 32 | SkScalar scale = SkIntToScalar(kScale); |
| 33 | SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 34 | canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); |
| 35 | SkPaint paint; |
| 36 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 37 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 38 | canvas->drawBitmapRect(bitmap, |
| 39 | SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), |
| 40 | &paint); |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 41 | |
| 42 | return is_in_scaled_image_cache(bitmap, scale, scale); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // http://crbug.com/389439 |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 46 | DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) { |
| 47 | size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 48 | size_t originalAllocationLimit = |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 49 | SkGraphics::GetResourceCacheSingleAllocationByteLimit(); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 50 | |
| 51 | size_t size = kBitmapSize * kScale * kBitmapSize * kScale |
| 52 | * SkColorTypeBytesPerPixel(kN32_SkColorType); |
| 53 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 54 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 55 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 56 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 57 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 59 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 60 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 61 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 62 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 63 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 65 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 66 | SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 67 | SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 68 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 69 | |
halcanary | 1d1795b | 2014-07-18 09:18:40 -0700 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 71 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 72 | SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit); |
| 73 | SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 74 | } |