| 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 | 7 |
| 8 #include "Test.h" | 8 #include "Test.h" |
| 9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
| 10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
| 11 #include "SkDiscardableMemoryPool.h" | 11 #include "SkDiscardableMemoryPool.h" |
| 12 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
| 13 #include "SkResourceCache.h" | 13 #include "SkResourceCache.h" |
| 14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
| 15 | 15 |
| 16 static const int kCanvasSize = 1; | 16 static const int kCanvasSize = 1; |
| 17 static const int kBitmapSize = 16; | 17 static const int kBitmapSize = 16; |
| 18 static const int kScale = 8; | 18 static const int kScale = 8; |
| 19 | 19 |
| 20 static bool is_in_scaled_image_cache(const SkBitmap& orig, | 20 static bool is_in_scaled_image_cache(const SkBitmap& orig, |
| 21 SkScalar xScale, | 21 SkScalar xScale, |
| 22 SkScalar yScale) { | 22 SkScalar yScale) { |
| 23 SkBitmap scaled; | 23 SkBitmap scaled; |
| 24 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); | 24 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); |
| 25 float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale); | 25 float roundedImageHeight = SkScalarRoundToScalar(orig.height() * yScale); |
| 26 return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &sca
led); | 26 return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &sca
led); |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Draw a scaled bitmap, then return true iff it has been cached. | 29 // Draw a scaled bitmap, then return true if it has been cached. |
| 30 static bool test_scaled_image_cache_useage() { | 30 static bool test_scaled_image_cache_usage() { |
| 31 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, k
CanvasSize)); | 31 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(kCanvasSize, k
CanvasSize)); |
| 32 SkCanvas* canvas = surface->getCanvas(); | 32 SkCanvas* canvas = surface->getCanvas(); |
| 33 SkBitmap bitmap; | 33 SkBitmap bitmap; |
| 34 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); | 34 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize); |
| 35 bitmap.eraseColor(0xFFFFFFFF); | 35 bitmap.eraseColor(0xFFFFFFFF); |
| 36 SkScalar scale = SkIntToScalar(kScale); | 36 SkScalar xScale = SkIntToScalar(kScale); |
| 37 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale; | 37 SkScalar yScale = xScale / 2; |
| 38 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize)); | 38 SkScalar xScaledSize = SkIntToScalar(kBitmapSize) * xScale; |
| 39 SkScalar yScaledSize = SkIntToScalar(kBitmapSize) * yScale; |
| 40 canvas->clipRect(SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize)); |
| 39 SkPaint paint; | 41 SkPaint paint; |
| 40 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); | 42 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
| 41 | 43 |
| 42 canvas->drawBitmapRect(bitmap, | 44 canvas->drawBitmapRect(bitmap, |
| 43 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize), | 45 SkRect::MakeLTRB(0, 0, xScaledSize, yScaledSize), |
| 44 &paint); | 46 &paint); |
| 45 | 47 |
| 46 return is_in_scaled_image_cache(bitmap, scale, scale); | 48 return is_in_scaled_image_cache(bitmap, xScale, yScale); |
| 47 } | 49 } |
| 48 | 50 |
| 49 // http://crbug.com/389439 | 51 // http://crbug.com/389439 |
| 50 DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) { | 52 DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) { |
| 51 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit(); | 53 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit(); |
| 52 size_t originalAllocationLimit = | 54 size_t originalAllocationLimit = |
| 53 SkGraphics::GetResourceCacheSingleAllocationByteLimit(); | 55 SkGraphics::GetResourceCacheSingleAllocationByteLimit(); |
| 54 | 56 |
| 55 size_t size = kBitmapSize * kScale * kBitmapSize * kScale | 57 size_t size = kBitmapSize * kScale * kBitmapSize * kScale |
| 56 * SkColorTypeBytesPerPixel(kN32_SkColorType); | 58 * SkColorTypeBytesPerPixel(kN32_SkColorType); |
| 57 | 59 |
| 58 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | 60 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 59 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | 61 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 60 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit | 62 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit |
| 61 | 63 |
| 62 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); | 64 REPORTER_ASSERT(reporter, test_scaled_image_cache_usage()); |
| 63 | 65 |
| 64 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | 66 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 65 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | 67 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 66 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big eno
ugh | 68 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big eno
ugh |
| 67 | 69 |
| 68 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage()); | 70 REPORTER_ASSERT(reporter, test_scaled_image_cache_usage()); |
| 69 | 71 |
| 70 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache | 72 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache |
| 71 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); | 73 SkGraphics::SetResourceCacheTotalByteLimit(2 * size); |
| 72 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too sma
ll | 74 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too sma
ll |
| 73 | 75 |
| 74 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage()); | 76 REPORTER_ASSERT(reporter, !test_scaled_image_cache_usage()); |
| 75 | 77 |
| 76 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimi
t); | 78 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimi
t); |
| 77 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); | 79 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit); |
| 78 } | 80 } |
| 79 | 81 |
| 80 ////////////////////////////////////////////////////////////////////////////////
//////// | 82 ////////////////////////////////////////////////////////////////////////////////
//////// |
| 81 | 83 |
| 82 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All
ocator* allocator) { | 84 static void make_bitmap(SkBitmap* bitmap, const SkImageInfo& info, SkBitmap::All
ocator* allocator) { |
| 83 if (allocator) { | 85 if (allocator) { |
| 84 bitmap->setInfo(info); | 86 bitmap->setInfo(info); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); | 213 make_bitmap(&cachedBitmap, SkImageInfo::MakeN32Premul(5, 5), allocator); |
| 212 cachedBitmap.setImmutable(); | 214 cachedBitmap.setImmutable(); |
| 213 cachedBitmap.unlockPixels(); | 215 cachedBitmap.unlockPixels(); |
| 214 | 216 |
| 215 // We can add the bitmap back to the cache and find it again. | 217 // We can add the bitmap back to the cache and find it again. |
| 216 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); | 218 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap, cache)); |
| 217 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); | 219 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm, cache)); |
| 218 | 220 |
| 219 test_mipmapcache(reporter, cache); | 221 test_mipmapcache(reporter, cache); |
| 220 } | 222 } |
| OLD | NEW |