blob: 20c3f7f0a0961f2daf0b2144688779048f3e7104 [file] [log] [blame]
halcanary805ef152014-07-17 06:58:01 -07001/*
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"
halcanary805ef152014-07-17 06:58:01 -07008#include "SkCanvas.h"
halcanary1d1795b2014-07-18 09:18:40 -07009#include "SkGraphics.h"
reed04617132014-08-21 09:46:49 -070010#include "SkBitmapCache.h"
danakj790ffe32014-09-11 10:49:52 -070011#include "SkDiscardableMemoryPool.h"
halcanary805ef152014-07-17 06:58:01 -070012
13static const int kCanvasSize = 1;
14static const int kBitmapSize = 16;
15static const int kScale = 8;
16
halcanary1d1795b2014-07-18 09:18:40 -070017static bool is_in_scaled_image_cache(const SkBitmap& orig,
18 SkScalar xScale,
19 SkScalar yScale) {
20 SkBitmap scaled;
humperd73c1692014-08-28 14:27:42 -070021 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale);
22 float roundedImageHeight = SkScalarRoundToScalar(orig.height() * xScale);
23 return SkBitmapCache::Find(orig, roundedImageWidth, roundedImageHeight, &scaled);
halcanary1d1795b2014-07-18 09:18:40 -070024}
25
26// Draw a scaled bitmap, then return true iff it has been cached.
27static bool test_scaled_image_cache_useage() {
halcanary805ef152014-07-17 06:58:01 -070028 SkAutoTUnref<SkCanvas> canvas(
29 SkCanvas::NewRasterN32(kCanvasSize, kCanvasSize));
30 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -070031 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
halcanary0db38cc2014-07-17 10:17:28 -070032 bitmap.eraseColor(0xFFFFFFFF);
halcanary1d1795b2014-07-18 09:18:40 -070033 SkScalar scale = SkIntToScalar(kScale);
34 SkScalar scaledSize = SkIntToScalar(kBitmapSize) * scale;
halcanary805ef152014-07-17 06:58:01 -070035 canvas->clipRect(SkRect::MakeLTRB(0, 0, scaledSize, scaledSize));
36 SkPaint paint;
37 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
halcanary1d1795b2014-07-18 09:18:40 -070038
halcanary805ef152014-07-17 06:58:01 -070039 canvas->drawBitmapRect(bitmap,
40 SkRect::MakeLTRB(0, 0, scaledSize, scaledSize),
41 &paint);
halcanary1d1795b2014-07-18 09:18:40 -070042
43 return is_in_scaled_image_cache(bitmap, scale, scale);
halcanary805ef152014-07-17 06:58:01 -070044}
45
46// http://crbug.com/389439
reed011f39a2014-08-28 13:35:23 -070047DEF_TEST(ResourceCache_SingleAllocationByteLimit, reporter) {
48 size_t originalByteLimit = SkGraphics::GetResourceCacheTotalByteLimit();
halcanary805ef152014-07-17 06:58:01 -070049 size_t originalAllocationLimit =
reed011f39a2014-08-28 13:35:23 -070050 SkGraphics::GetResourceCacheSingleAllocationByteLimit();
halcanary805ef152014-07-17 06:58:01 -070051
52 size_t size = kBitmapSize * kScale * kBitmapSize * kScale
53 * SkColorTypeBytesPerPixel(kN32_SkColorType);
54
reed011f39a2014-08-28 13:35:23 -070055 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
56 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
57 SkGraphics::SetResourceCacheSingleAllocationByteLimit(0); // No limit
halcanary805ef152014-07-17 06:58:01 -070058
halcanary1d1795b2014-07-18 09:18:40 -070059 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070060
reed011f39a2014-08-28 13:35:23 -070061 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
62 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
63 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size * 2); // big enough
halcanary805ef152014-07-17 06:58:01 -070064
halcanary1d1795b2014-07-18 09:18:40 -070065 REPORTER_ASSERT(reporter, test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070066
reed011f39a2014-08-28 13:35:23 -070067 SkGraphics::SetResourceCacheTotalByteLimit(0); // clear cache
68 SkGraphics::SetResourceCacheTotalByteLimit(2 * size);
69 SkGraphics::SetResourceCacheSingleAllocationByteLimit(size / 2); // too small
halcanary805ef152014-07-17 06:58:01 -070070
halcanary1d1795b2014-07-18 09:18:40 -070071 REPORTER_ASSERT(reporter, !test_scaled_image_cache_useage());
halcanary805ef152014-07-17 06:58:01 -070072
reed011f39a2014-08-28 13:35:23 -070073 SkGraphics::SetResourceCacheSingleAllocationByteLimit(originalAllocationLimit);
74 SkGraphics::SetResourceCacheTotalByteLimit(originalByteLimit);
halcanary805ef152014-07-17 06:58:01 -070075}
piotaixr42b0dfe2014-09-03 11:33:13 -070076
mtklein26abcf12014-09-04 10:50:53 -070077#if 0 // skia:2912
78
piotaixr42b0dfe2014-09-03 11:33:13 -070079static SkBitmap createAllocatedBitmap(const SkImageInfo& info) {
80 SkBitmap cachedBitmap;
81 cachedBitmap.setInfo(info);
82 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator();
bsalomon49f085d2014-09-05 13:34:00 -070083 if (allocator) {
piotaixr42b0dfe2014-09-03 11:33:13 -070084 allocator->allocPixelRef(&cachedBitmap, 0);
85 } else {
86 cachedBitmap.allocPixels();
87 }
88
89 return cachedBitmap;
90}
91
92// http://skbug.com/2894
93DEF_TEST(BitmapCache_add_rect, reporter) {
94 SkBitmap bm;
95 SkIRect rect = SkIRect::MakeWH(5, 5);
96 SkBitmap cachedBitmap = createAllocatedBitmap(SkImageInfo::MakeN32Premul(5, 5));
97 cachedBitmap.setImmutable();
98
99 // Wrong subset size
100 REPORTER_ASSERT(reporter, ! SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeWH(4, 6), cachedBitmap));
101 REPORTER_ASSERT(reporter, ! SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
102 // Wrong offset value
103 REPORTER_ASSERT(reporter, ! SkBitmapCache::Add(cachedBitmap.getGenerationID(), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap));
104 REPORTER_ASSERT(reporter, ! SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
105
106 // Should not be in the cache
107 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
108
109 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap));
110 // Should be in the cache, we just added it
111 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
112}
danakj790ffe32014-09-11 10:49:52 -0700113
114DEF_TEST(BitmapCache_discarded_bitmap, reporter) {
115 SkBitmap bm;
116 SkIRect rect = SkIRect::MakeWH(5, 5);
117 SkBitmap cachedBitmap = createAllocatedBitmap(SkImageInfo::MakeN32Premul(5, 5));
118 cachedBitmap.setImmutable();
119 cachedBitmap.unlockPixels();
120
121 // Add a bitmap to the cache.
122 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap));
123 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
124
125 // Finding more than once works fine.
126 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
127 bm.unlockPixels();
128
129 // Drop the pixels in the bitmap.
130 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() > 0);
131 SkGetGlobalDiscardableMemoryPool()->dumpPool();
132 REPORTER_ASSERT(reporter, SkGetGlobalDiscardableMemoryPool()->getRAMUsed() == 0);
133
134 // The bitmap is not in the cache since it has been dropped.
135 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
136
137 cachedBitmap = createAllocatedBitmap(SkImageInfo::MakeN32Premul(5, 5));
138 cachedBitmap.setImmutable();
139 cachedBitmap.unlockPixels();
140
141 // We can add the bitmap back to the cache and find it again.
142 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(), rect, cachedBitmap));
143 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID(), rect, &bm));
144}
mtklein26abcf12014-09-04 10:50:53 -0700145#endif