Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 #include "Test.h" | 7 #include "Test.h" |
| 8 #include "SkCanvas.h" | |
| 8 #include "SkGraphics.h" | 9 #include "SkGraphics.h" |
| 9 #include "SkCanvas.h" | 10 #include "SkScaledImageCache.h" |
| 10 | 11 |
| 11 static const int kCanvasSize = 1; | 12 static const int kCanvasSize = 1; |
| 12 static const int kBitmapSize = 16; | 13 static const int kBitmapSize = 16; |
| 13 static const int kScale = 8; | 14 static const int kScale = 8; |
| 14 | 15 |
| 15 static size_t test_scaled_image_cache_useage() { | 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 static bool test_scaled_image_cache_useage() { | |
|
tomhudson
2014/07/18 15:02:44
Nit: if we're cutting the comment in line 44 (see
hal.canary
2014/07/18 16:06:52
Done.
| |
| 16 SkAutoTUnref<SkCanvas> canvas( | 29 SkAutoTUnref<SkCanvas> canvas( |
| 17 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); | 30 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize)); |
| 18 SkBitmap bitmap; | 31 SkBitmap bitmap; |
| 19 SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize)); | 32 SkAssertResult(bitmap.allocN32Pixels(kBitmapSize, kBitmapSize)); |
| 20 bitmap.eraseColor(0xFFFFFFFF); | 33 bitmap.eraseColor(0xFFFFFFFF); |
| 21 SkScalar scaledSize = SkIntToScalar(kScale * kBitmapSize); | 34 SkScalar scale = SkIntToScalar(kScale); |
| 35 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; | |
| 22 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); | 36 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); |
| 23 SkPaint paint; | 37 SkPaint paint; |
| 24 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); | 38 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
| 25 size_t bytesUsed = SkGraphics::GetImageCacheBytesUsed(); | 39 |
| 26 canvas->drawBitmapRect(bitmap, | 40 canvas->drawBitmapRect(bitmap, |
| 27 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), | 41 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), |
| 28 &paint); | 42 &paint); |
| 29 return SkGraphics::GetImageCacheBytesUsed() - bytesUsed; | 43 |
| 44 // Check to see that the scaled bitmap is to be found in the cache. | |
|
tomhudson
2014/07/18 15:02:44
Nit: unnecessary comment? (I love comments, but th
hal.canary
2014/07/18 16:06:52
The comment predated the splitting out of the func
| |
| 45 return is_in_scaled_image_cache(bitmap, scale, scale); | |
| 30 } | 46 } |
| 31 | 47 |
| 32 // http://crbug.com/389439 | 48 // http://crbug.com/389439 |
| 33 DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) { | 49 DEF_TEST(ScaledImageCache_SingleAllocationByteLimit, reporter) { |
| 34 size_t originalByteLimit = SkGraphics::GetImageCacheByteLimit(); | 50 size_t originalByteLimit = SkGraphics::GetImageCacheByteLimit(); |
| 35 size_t originalAllocationLimit = | 51 size_t originalAllocationLimit = |
| 36 SkGraphics::GetImageCacheSingleAllocationByteLimit(); | 52 SkGraphics::GetImageCacheSingleAllocationByteLimit(); |
| 37 | 53 |
| 38 size_t size = kBitmapSize * kScale * kBitmapSize * kScale | 54 size_t size = kBitmapSize * kScale * kBitmapSize * kScale |
| 39 * SkColorTypeBytesPerPixel(kN32_SkColorType); | 55 * SkColorTypeBytesPerPixel(kN32_SkColorType); |
| 40 | 56 |
| 41 SkGraphics::SetImageCacheByteLimit(0); // clear cache | 57 SkGraphics::SetImageCacheByteLimit(0); // clear cache |
| 42 SkGraphics::SetImageCacheByteLimit(2 * size); | 58 SkGraphics::SetImageCacheByteLimit(2 * size); |
| 43 SkGraphics::SetImageCacheSingleAllocationByteLimit(0); | 59 SkGraphics::SetImageCacheSingleAllocationByteLimit(0); |
| 44 | 60 |
| 45 REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage()); | 61 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
| 46 | 62 |
| 47 SkGraphics::SetImageCacheByteLimit(0); // clear cache | 63 SkGraphics::SetImageCacheByteLimit(0); // clear cache |
| 48 SkGraphics::SetImageCacheByteLimit(2 * size); | 64 SkGraphics::SetImageCacheByteLimit(2 * size); |
| 49 SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); | 65 SkGraphics::SetImageCacheSingleAllocationByteLimit(size * 2); |
| 50 | 66 |
| 51 REPORTER_ASSERT(reporter, size == test_scaled_image_cache_useage()); | 67 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); |
| 52 | 68 |
| 53 SkGraphics::SetImageCacheByteLimit(0); // clear cache | 69 SkGraphics::SetImageCacheByteLimit(0); // clear cache |
| 54 SkGraphics::SetImageCacheByteLimit(2 * size); | 70 SkGraphics::SetImageCacheByteLimit(2 * size); |
| 55 SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); | 71 SkGraphics::SetImageCacheSingleAllocationByteLimit(size / 2); |
| 56 | 72 |
| 57 REPORTER_ASSERT(reporter, 0 == test_scaled_image_cache_useage()); | 73 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); |
| 58 | 74 |
| 59 SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit); | 75 SkGraphics::SetImageCacheSingleAllocationByteLimit(originalAllocationLimit); |
| 60 SkGraphics::SetImageCacheByteLimit(originalByteLimit); | 76 SkGraphics::SetImageCacheByteLimit(originalByteLimit); |
| 61 } | 77 } |
| OLD | NEW |