| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 | 9 |
| 10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| 11 #include "SkImage.h" | 11 #include "SkImage.h" |
| 12 #include "SkImageFilter.h" | 12 #include "SkImageFilter.h" |
| 13 #include "SkImageFilterCacheKey.h" | 13 #include "SkImageFilterCacheKey.h" |
| 14 #include "SkMatrix.h" | 14 #include "SkMatrix.h" |
| 15 #include "SkSpecialImage.h" | 15 #include "SkSpecialImage.h" |
| 16 | 16 |
| 17 static const int kSmallerSize = 10; | 17 static const int kSmallerSize = 10; |
| 18 static const int kPad = 3; | 18 static const int kPad = 3; |
| 19 static const int kFullSize = kSmallerSize + 2 * kPad; | 19 static const int kFullSize = kSmallerSize + 2 * kPad; |
| 20 | 20 |
| 21 static SkBitmap create_bm() { | 21 static SkBitmap create_bm() { |
| 22 SkBitmap bm; | 22 SkBitmap bm; |
| 23 bm.allocN32Pixels(kFullSize, kFullSize, true); | 23 bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 24 bm.eraseColor(SK_ColorTRANSPARENT); | 24 bm.eraseColor(SK_ColorTRANSPARENT); |
| 25 return bm; | 25 return bm; |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Ensure the cache can return a cached image | 28 // Ensure the cache can return a cached image |
| 29 static void test_find_existing(skiatest::Reporter* reporter, | 29 static void test_find_existing(skiatest::Reporter* reporter, |
| 30 SkSpecialImage* image, | 30 const sk_sp<SkSpecialImage>& image, |
| 31 SkSpecialImage* subset) { | 31 const sk_sp<SkSpecialImage>& subset) { |
| 32 static const size_t kCacheSize = 1000000; | 32 static const size_t kCacheSize = 1000000; |
| 33 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); | 33 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); |
| 34 | 34 |
| 35 SkIRect clip = SkIRect::MakeWH(100, 100); | 35 SkIRect clip = SkIRect::MakeWH(100, 100); |
| 36 SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); | 36 SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); |
| 37 SkImageFilter::Cache::Key key2(0, SkMatrix::I(), clip, subset->uniqueID(), s
ubset->subset()); | 37 SkImageFilter::Cache::Key key2(0, SkMatrix::I(), clip, subset->uniqueID(), s
ubset->subset()); |
| 38 | 38 |
| 39 SkIPoint offset = SkIPoint::Make(3, 4); | 39 SkIPoint offset = SkIPoint::Make(3, 4); |
| 40 cache->set(key1, image, offset); | 40 cache->set(key1, image.get(), offset); |
| 41 | 41 |
| 42 SkIPoint foundOffset; | 42 SkIPoint foundOffset; |
| 43 | 43 |
| 44 SkSpecialImage* foundImage = cache->get(key1, &foundOffset); | 44 SkSpecialImage* foundImage = cache->get(key1, &foundOffset); |
| 45 REPORTER_ASSERT(reporter, foundImage); | 45 REPORTER_ASSERT(reporter, foundImage); |
| 46 REPORTER_ASSERT(reporter, offset == foundOffset); | 46 REPORTER_ASSERT(reporter, offset == foundOffset); |
| 47 | 47 |
| 48 REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); | 48 REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // If either id is different or the clip or the matrix are different the | 51 // If either id is different or the clip or the matrix are different the |
| 52 // cached image won't be found. Even if it is caching the same bitmap. | 52 // cached image won't be found. Even if it is caching the same bitmap. |
| 53 static void test_dont_find_if_diff_key(skiatest::Reporter* reporter, | 53 static void test_dont_find_if_diff_key(skiatest::Reporter* reporter, |
| 54 SkSpecialImage* image, | 54 const sk_sp<SkSpecialImage>& image, |
| 55 SkSpecialImage* subset) { | 55 const sk_sp<SkSpecialImage>& subset) { |
| 56 static const size_t kCacheSize = 1000000; | 56 static const size_t kCacheSize = 1000000; |
| 57 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); | 57 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); |
| 58 | 58 |
| 59 SkIRect clip1 = SkIRect::MakeWH(100, 100); | 59 SkIRect clip1 = SkIRect::MakeWH(100, 100); |
| 60 SkIRect clip2 = SkIRect::MakeWH(200, 200); | 60 SkIRect clip2 = SkIRect::MakeWH(200, 200); |
| 61 SkImageFilter::Cache::Key key0(0, SkMatrix::I(), clip1, image->uniqueID(), i
mage->subset()); | 61 SkImageFilter::Cache::Key key0(0, SkMatrix::I(), clip1, image->uniqueID(), i
mage->subset()); |
| 62 SkImageFilter::Cache::Key key1(1, SkMatrix::I(), clip1, image->uniqueID(), i
mage->subset()); | 62 SkImageFilter::Cache::Key key1(1, SkMatrix::I(), clip1, image->uniqueID(), i
mage->subset()); |
| 63 SkImageFilter::Cache::Key key2(0, SkMatrix::MakeTrans(5, 5), clip1, | 63 SkImageFilter::Cache::Key key2(0, SkMatrix::MakeTrans(5, 5), clip1, |
| 64 image->uniqueID(), image->subset()); | 64 image->uniqueID(), image->subset()); |
| 65 SkImageFilter::Cache::Key key3(0, SkMatrix::I(), clip2, image->uniqueID(), i
mage->subset()); | 65 SkImageFilter::Cache::Key key3(0, SkMatrix::I(), clip2, image->uniqueID(), i
mage->subset()); |
| 66 SkImageFilter::Cache::Key key4(0, SkMatrix::I(), clip1, subset->uniqueID(),
subset->subset()); | 66 SkImageFilter::Cache::Key key4(0, SkMatrix::I(), clip1, subset->uniqueID(),
subset->subset()); |
| 67 | 67 |
| 68 SkIPoint offset = SkIPoint::Make(3, 4); | 68 SkIPoint offset = SkIPoint::Make(3, 4); |
| 69 cache->set(key0, image, offset); | 69 cache->set(key0, image.get(), offset); |
| 70 | 70 |
| 71 SkIPoint foundOffset; | 71 SkIPoint foundOffset; |
| 72 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); | 72 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 73 REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); | 73 REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); |
| 74 REPORTER_ASSERT(reporter, !cache->get(key3, &foundOffset)); | 74 REPORTER_ASSERT(reporter, !cache->get(key3, &foundOffset)); |
| 75 REPORTER_ASSERT(reporter, !cache->get(key4, &foundOffset)); | 75 REPORTER_ASSERT(reporter, !cache->get(key4, &foundOffset)); |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Test purging when the max cache size is exceeded | 78 // Test purging when the max cache size is exceeded |
| 79 static void test_internal_purge(skiatest::Reporter* reporter, SkSpecialImage* im
age) { | 79 static void test_internal_purge(skiatest::Reporter* reporter, const sk_sp<SkSpec
ialImage>& image) { |
| 80 SkASSERT(image->getSize()); | 80 SkASSERT(image->getSize()); |
| 81 const size_t kCacheSize = image->getSize() + 10; | 81 const size_t kCacheSize = image->getSize() + 10; |
| 82 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); | 82 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); |
| 83 | 83 |
| 84 SkIRect clip = SkIRect::MakeWH(100, 100); | 84 SkIRect clip = SkIRect::MakeWH(100, 100); |
| 85 SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); | 85 SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); |
| 86 SkImageFilter::Cache::Key key2(1, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); | 86 SkImageFilter::Cache::Key key2(1, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); |
| 87 | 87 |
| 88 SkIPoint offset = SkIPoint::Make(3, 4); | 88 SkIPoint offset = SkIPoint::Make(3, 4); |
| 89 cache->set(key1, image, offset); | 89 cache->set(key1, image.get(), offset); |
| 90 | 90 |
| 91 SkIPoint foundOffset; | 91 SkIPoint foundOffset; |
| 92 | 92 |
| 93 REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset)); | 93 REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset)); |
| 94 | 94 |
| 95 // This should knock the first one out of the cache | 95 // This should knock the first one out of the cache |
| 96 cache->set(key2, image, offset); | 96 cache->set(key2, image.get(), offset); |
| 97 | 97 |
| 98 REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); | 98 REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 99 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); | 99 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // Exercise the purgeByKeys and purge methods | 102 // Exercise the purgeByKeys and purge methods |
| 103 static void test_explicit_purging(skiatest::Reporter* reporter, | 103 static void test_explicit_purging(skiatest::Reporter* reporter, |
| 104 SkSpecialImage* image, | 104 const sk_sp<SkSpecialImage>& image, |
| 105 SkSpecialImage* subset) { | 105 const sk_sp<SkSpecialImage>& subset) { |
| 106 static const size_t kCacheSize = 1000000; | 106 static const size_t kCacheSize = 1000000; |
| 107 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); | 107 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache
Size)); |
| 108 | 108 |
| 109 SkIRect clip = SkIRect::MakeWH(100, 100); | 109 SkIRect clip = SkIRect::MakeWH(100, 100); |
| 110 SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); | 110 SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), im
age->subset()); |
| 111 SkImageFilter::Cache::Key key2(1, SkMatrix::I(), clip, subset->uniqueID(), i
mage->subset()); | 111 SkImageFilter::Cache::Key key2(1, SkMatrix::I(), clip, subset->uniqueID(), i
mage->subset()); |
| 112 | 112 |
| 113 SkIPoint offset = SkIPoint::Make(3, 4); | 113 SkIPoint offset = SkIPoint::Make(3, 4); |
| 114 cache->set(key1, image, offset); | 114 cache->set(key1, image.get(), offset); |
| 115 cache->set(key2, image, offset); | 115 cache->set(key2, image.get(), offset); |
| 116 | 116 |
| 117 SkIPoint foundOffset; | 117 SkIPoint foundOffset; |
| 118 | 118 |
| 119 REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset)); | 119 REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset)); |
| 120 REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); | 120 REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 121 | 121 |
| 122 cache->purgeByKeys(&key1, 1); | 122 cache->purgeByKeys(&key1, 1); |
| 123 | 123 |
| 124 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); | 124 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 125 REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); | 125 REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 126 | 126 |
| 127 cache->purge(); | 127 cache->purge(); |
| 128 | 128 |
| 129 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); | 129 REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 130 REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); | 130 REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 DEF_TEST(ImageFilterCache_RasterBacked, reporter) { | 133 DEF_TEST(ImageFilterCache_RasterBacked, reporter) { |
| 134 SkBitmap srcBM = create_bm(); | 134 SkBitmap srcBM = create_bm(); |
| 135 | 135 |
| 136 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); | 136 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 137 | 137 |
| 138 SkAutoTUnref<SkSpecialImage> fullImg(SkSpecialImage::NewFromRaster(nullptr,
full, srcBM)); | 138 sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromRaster(nullptr, full,
srcBM)); |
| 139 | 139 |
| 140 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 140 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 141 | 141 |
| 142 SkAutoTUnref<SkSpecialImage> subsetImg(SkSpecialImage::NewFromRaster(nullptr
, subset, srcBM)); | 142 sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromRaster(nullptr, subs
et, srcBM)); |
| 143 | 143 |
| 144 test_find_existing(reporter, fullImg, subsetImg); | 144 test_find_existing(reporter, fullImg, subsetImg); |
| 145 test_dont_find_if_diff_key(reporter, fullImg, subsetImg); | 145 test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 146 test_internal_purge(reporter, fullImg); | 146 test_internal_purge(reporter, fullImg); |
| 147 test_explicit_purging(reporter, fullImg, subsetImg); | 147 test_explicit_purging(reporter, fullImg, subsetImg); |
| 148 } | 148 } |
| 149 | 149 |
| 150 | 150 |
| 151 // Shared test code for both the raster and gpu-backed image cases | 151 // Shared test code for both the raster and gpu-backed image cases |
| 152 static void test_image_backed(skiatest::Reporter* reporter, SkImage* srcImage) { | 152 static void test_image_backed(skiatest::Reporter* reporter, const sk_sp<SkImage>
& srcImage) { |
| 153 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); | 153 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 154 | 154 |
| 155 SkAutoTUnref<SkSpecialImage> fullImg(SkSpecialImage::NewFromImage(nullptr, f
ull, srcImage)); | 155 sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromImage(nullptr, full, s
rcImage)); |
| 156 | 156 |
| 157 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | 157 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 158 | 158 |
| 159 SkAutoTUnref<SkSpecialImage> subsetImg(SkSpecialImage::NewFromImage(nullptr, | 159 sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromImage(nullptr, subse
t, srcImage)); |
| 160 subset, | |
| 161 srcImage
)); | |
| 162 | 160 |
| 163 test_find_existing(reporter, fullImg, subsetImg); | 161 test_find_existing(reporter, fullImg, subsetImg); |
| 164 test_dont_find_if_diff_key(reporter, fullImg, subsetImg); | 162 test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 165 test_internal_purge(reporter, fullImg); | 163 test_internal_purge(reporter, fullImg); |
| 166 test_explicit_purging(reporter, fullImg, subsetImg); | 164 test_explicit_purging(reporter, fullImg, subsetImg); |
| 167 } | 165 } |
| 168 | 166 |
| 169 DEF_TEST(ImageFilterCache_ImageBackedRaster, reporter) { | 167 DEF_TEST(ImageFilterCache_ImageBackedRaster, reporter) { |
| 170 SkBitmap srcBM = create_bm(); | 168 SkBitmap srcBM = create_bm(); |
| 171 | 169 |
| 172 sk_sp<SkImage> srcImage(SkImage::MakeFromBitmap(srcBM)); | 170 sk_sp<SkImage> srcImage(SkImage::MakeFromBitmap(srcBM)); |
| 173 | 171 |
| 174 test_image_backed(reporter, srcImage.get()); | 172 test_image_backed(reporter, srcImage); |
| 175 } | 173 } |
| 176 | 174 |
| 177 #if SK_SUPPORT_GPU | 175 #if SK_SUPPORT_GPU |
| 178 #include "GrContext.h" | 176 #include "GrContext.h" |
| 179 | 177 |
| 180 static GrTexture* create_texture(GrContext* context) { | 178 static GrTexture* create_texture(GrContext* context) { |
| 181 SkBitmap srcBM = create_bm(); | 179 SkBitmap srcBM = create_bm(); |
| 182 | 180 |
| 183 GrSurfaceDesc desc; | 181 GrSurfaceDesc desc; |
| 184 desc.fConfig = kSkia8888_GrPixelConfig; | 182 desc.fConfig = kSkia8888_GrPixelConfig; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 200 backendDesc.fFlags = kNone_GrBackendTextureFlag; | 198 backendDesc.fFlags = kNone_GrBackendTextureFlag; |
| 201 backendDesc.fWidth = kFullSize; | 199 backendDesc.fWidth = kFullSize; |
| 202 backendDesc.fHeight = kFullSize; | 200 backendDesc.fHeight = kFullSize; |
| 203 backendDesc.fSampleCnt = 0; | 201 backendDesc.fSampleCnt = 0; |
| 204 backendDesc.fTextureHandle = srcTexture->getTextureHandle(); | 202 backendDesc.fTextureHandle = srcTexture->getTextureHandle(); |
| 205 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context, backendDesc, kPrem
ul_SkAlphaType)); | 203 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context, backendDesc, kPrem
ul_SkAlphaType)); |
| 206 if (!srcImage) { | 204 if (!srcImage) { |
| 207 return; | 205 return; |
| 208 } | 206 } |
| 209 | 207 |
| 210 test_image_backed(reporter, srcImage.get()); | 208 test_image_backed(reporter, srcImage); |
| 211 } | 209 } |
| 212 | 210 |
| 213 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_GPUBacked, reporter, context
) { | 211 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_GPUBacked, reporter, context
) { |
| 214 | 212 |
| 215 SkAutoTUnref<GrTexture> srcTexture(create_texture(context)); | 213 SkAutoTUnref<GrTexture> srcTexture(create_texture(context)); |
| 216 if (!srcTexture) { | 214 if (!srcTexture) { |
| 217 return; | 215 return; |
| 218 } | 216 } |
| 219 | 217 |
| 220 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); | 218 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 221 | 219 |
| 222 SkAutoTUnref<SkSpecialImage> fullImg(SkSpecialImage::NewFromGpu( | 220 sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromGpu(nullptr, full, |
| 223 nullptr, full, | 221 kNeedNewImageUniqu
eID_SpecialImage, |
| 222 srcTexture)); |
| 223 |
| 224 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); |
| 225 |
| 226 sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromGpu(nullptr, subset, |
| 224 kNeedNewImageUni
queID_SpecialImage, | 227 kNeedNewImageUni
queID_SpecialImage, |
| 225 srcTexture)); | 228 srcTexture)); |
| 226 | 229 |
| 227 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller
Size); | |
| 228 | |
| 229 SkAutoTUnref<SkSpecialImage> subsetImg(SkSpecialImage::NewFromGpu( | |
| 230 nullptr, subset, | |
| 231 kNeedNewImageUni
queID_SpecialImage, | |
| 232 srcTexture)); | |
| 233 | |
| 234 test_find_existing(reporter, fullImg, subsetImg); | 230 test_find_existing(reporter, fullImg, subsetImg); |
| 235 test_dont_find_if_diff_key(reporter, fullImg, subsetImg); | 231 test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 236 test_internal_purge(reporter, fullImg); | 232 test_internal_purge(reporter, fullImg); |
| 237 test_explicit_purging(reporter, fullImg, subsetImg); | 233 test_explicit_purging(reporter, fullImg, subsetImg); |
| 238 } | 234 } |
| 239 #endif | 235 #endif |
| 240 | 236 |
| OLD | NEW |