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 "SkCanvas.h" |
9 #include "SkGraphics.h" | 9 #include "SkGraphics.h" |
10 #include "SkBitmapCache.h" | 10 #include "SkBitmapCache.h" |
| 11 #include "SkDiscardableMemoryPool.h" |
11 | 12 |
12 static const int kCanvasSize = 1; | 13 static const int kCanvasSize = 1; |
13 static const int kBitmapSize = 16; | 14 static const int kBitmapSize = 16; |
14 static const int kScale = 8; | 15 static const int kScale = 8; |
15 | 16 |
16 static bool is_in_scaled_image_cache(const SkBitmap& orig, | 17 static bool is_in_scaled_image_cache(const SkBitmap& orig, |
17 SkScalar xScale, | 18 SkScalar xScale, |
18 SkScalar yScale) { | 19 SkScalar yScale) { |
19 SkBitmap scaled; | 20 SkBitmap scaled; |
20 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); | 21 float roundedImageWidth = SkScalarRoundToScalar(orig.width() * xScale); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 REPORTER_ASSERT(reporter, ! SkBitmapCache::Add(cachedBitmap.getGenerationID(
), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap)); | 103 REPORTER_ASSERT(reporter, ! SkBitmapCache::Add(cachedBitmap.getGenerationID(
), SkIRect::MakeXYWH(-1, 0, 5, 5), cachedBitmap)); |
103 REPORTER_ASSERT(reporter, ! SkBitmapCache::Find(cachedBitmap.getGenerationID
(), rect, &bm)); | 104 REPORTER_ASSERT(reporter, ! SkBitmapCache::Find(cachedBitmap.getGenerationID
(), rect, &bm)); |
104 | 105 |
105 // Should not be in the cache | 106 // Should not be in the cache |
106 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(
), rect, &bm)); | 107 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(cachedBitmap.getGenerationID(
), rect, &bm)); |
107 | 108 |
108 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap)); | 109 REPORTER_ASSERT(reporter, SkBitmapCache::Add(cachedBitmap.getGenerationID(),
rect, cachedBitmap)); |
109 // Should be in the cache, we just added it | 110 // Should be in the cache, we just added it |
110 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm)); | 111 REPORTER_ASSERT(reporter, SkBitmapCache::Find(cachedBitmap.getGenerationID()
, rect, &bm)); |
111 } | 112 } |
| 113 |
| 114 DEF_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 } |
112 #endif | 145 #endif |
OLD | NEW |