robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #include "Test.h" |
| 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkImage.h" |
| 12 | #include "SkImageFilter.h" |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 13 | #include "SkImageFilterCache.h" |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 14 | #include "SkMatrix.h" |
| 15 | #include "SkSpecialImage.h" |
| 16 | |
| 17 | static const int kSmallerSize = 10; |
| 18 | static const int kPad = 3; |
| 19 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 20 | |
| 21 | static SkBitmap create_bm() { |
| 22 | SkBitmap bm; |
| 23 | bm.allocN32Pixels(kFullSize, kFullSize, true); |
| 24 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 25 | return bm; |
| 26 | } |
| 27 | |
| 28 | // Ensure the cache can return a cached image |
| 29 | static void test_find_existing(skiatest::Reporter* reporter, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 30 | const sk_sp<SkSpecialImage>& image, |
| 31 | const sk_sp<SkSpecialImage>& subset) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 32 | static const size_t kCacheSize = 1000000; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 33 | sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 34 | |
| 35 | SkIRect clip = SkIRect::MakeWH(100, 100); |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 36 | SkImageFilterCacheKey key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
| 37 | SkImageFilterCacheKey key2(0, SkMatrix::I(), clip, subset->uniqueID(), subset->subset()); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 38 | |
| 39 | SkIPoint offset = SkIPoint::Make(3, 4); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 40 | cache->set(key1, image.get(), offset); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 41 | |
| 42 | SkIPoint foundOffset; |
| 43 | |
Florin Malita | a022e2d | 2017-01-12 12:06:14 -0500 | [diff] [blame] | 44 | sk_sp<SkSpecialImage> foundImage = cache->get(key1, &foundOffset); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, foundImage); |
| 46 | REPORTER_ASSERT(reporter, offset == foundOffset); |
| 47 | |
| 48 | REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); |
| 49 | } |
| 50 | |
| 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. |
| 53 | static void test_dont_find_if_diff_key(skiatest::Reporter* reporter, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 54 | const sk_sp<SkSpecialImage>& image, |
| 55 | const sk_sp<SkSpecialImage>& subset) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 56 | static const size_t kCacheSize = 1000000; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 57 | sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 58 | |
| 59 | SkIRect clip1 = SkIRect::MakeWH(100, 100); |
| 60 | SkIRect clip2 = SkIRect::MakeWH(200, 200); |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 61 | SkImageFilterCacheKey key0(0, SkMatrix::I(), clip1, image->uniqueID(), image->subset()); |
| 62 | SkImageFilterCacheKey key1(1, SkMatrix::I(), clip1, image->uniqueID(), image->subset()); |
| 63 | SkImageFilterCacheKey key2(0, SkMatrix::MakeTrans(5, 5), clip1, |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 64 | image->uniqueID(), image->subset()); |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 65 | SkImageFilterCacheKey key3(0, SkMatrix::I(), clip2, image->uniqueID(), image->subset()); |
| 66 | SkImageFilterCacheKey key4(0, SkMatrix::I(), clip1, subset->uniqueID(), subset->subset()); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 67 | |
| 68 | SkIPoint offset = SkIPoint::Make(3, 4); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 69 | cache->set(key0, image.get(), offset); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 70 | |
| 71 | SkIPoint foundOffset; |
| 72 | REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 73 | REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); |
| 74 | REPORTER_ASSERT(reporter, !cache->get(key3, &foundOffset)); |
| 75 | REPORTER_ASSERT(reporter, !cache->get(key4, &foundOffset)); |
| 76 | } |
| 77 | |
| 78 | // Test purging when the max cache size is exceeded |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 79 | static void test_internal_purge(skiatest::Reporter* reporter, const sk_sp<SkSpecialImage>& image) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 80 | SkASSERT(image->getSize()); |
mtklein | 03762fe | 2016-02-21 13:36:50 -0800 | [diff] [blame] | 81 | const size_t kCacheSize = image->getSize() + 10; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 82 | sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 83 | |
| 84 | SkIRect clip = SkIRect::MakeWH(100, 100); |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 85 | SkImageFilterCacheKey key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
| 86 | SkImageFilterCacheKey key2(1, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 87 | |
| 88 | SkIPoint offset = SkIPoint::Make(3, 4); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 89 | cache->set(key1, image.get(), offset); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 90 | |
| 91 | SkIPoint foundOffset; |
| 92 | |
| 93 | REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset)); |
| 94 | |
| 95 | // This should knock the first one out of the cache |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 96 | cache->set(key2, image.get(), offset); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 97 | |
| 98 | REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 99 | REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 100 | } |
| 101 | |
| 102 | // Exercise the purgeByKeys and purge methods |
| 103 | static void test_explicit_purging(skiatest::Reporter* reporter, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 104 | const sk_sp<SkSpecialImage>& image, |
| 105 | const sk_sp<SkSpecialImage>& subset) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 106 | static const size_t kCacheSize = 1000000; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 107 | sk_sp<SkImageFilterCache> cache(SkImageFilterCache::Create(kCacheSize)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 108 | |
| 109 | SkIRect clip = SkIRect::MakeWH(100, 100); |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 110 | SkImageFilterCacheKey key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
| 111 | SkImageFilterCacheKey key2(1, SkMatrix::I(), clip, subset->uniqueID(), image->subset()); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 112 | |
| 113 | SkIPoint offset = SkIPoint::Make(3, 4); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 114 | cache->set(key1, image.get(), offset); |
| 115 | cache->set(key2, image.get(), offset); |
robertphillips | bde57ed | 2016-04-18 14:49:57 -0700 | [diff] [blame] | 116 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->count());) |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 117 | |
| 118 | SkIPoint foundOffset; |
| 119 | |
| 120 | REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset)); |
| 121 | REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 122 | |
| 123 | cache->purgeByKeys(&key1, 1); |
robertphillips | bde57ed | 2016-04-18 14:49:57 -0700 | [diff] [blame] | 124 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->count());) |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 125 | |
| 126 | REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 127 | REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 128 | |
| 129 | cache->purge(); |
robertphillips | bde57ed | 2016-04-18 14:49:57 -0700 | [diff] [blame] | 130 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->count());) |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 131 | |
| 132 | REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 133 | REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); |
| 134 | } |
| 135 | |
| 136 | DEF_TEST(ImageFilterCache_RasterBacked, reporter) { |
| 137 | SkBitmap srcBM = create_bm(); |
| 138 | |
| 139 | const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 140 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 141 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromRaster(full, srcBM)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 142 | |
| 143 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 144 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 145 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromRaster(subset, srcBM)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 146 | |
| 147 | test_find_existing(reporter, fullImg, subsetImg); |
| 148 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 149 | test_internal_purge(reporter, fullImg); |
| 150 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 151 | } |
| 152 | |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 153 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 154 | // Shared test code for both the raster and gpu-backed image cases |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 155 | static void test_image_backed(skiatest::Reporter* reporter, const sk_sp<SkImage>& srcImage) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 156 | const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 157 | SkColorSpace* legacyColorSpace = nullptr; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 158 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 159 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromImage(full, srcImage, legacyColorSpace)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 160 | |
| 161 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 162 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 163 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromImage(subset, srcImage, |
| 164 | legacyColorSpace)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 165 | |
| 166 | test_find_existing(reporter, fullImg, subsetImg); |
| 167 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 168 | test_internal_purge(reporter, fullImg); |
| 169 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 170 | } |
| 171 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 172 | DEF_TEST(ImageFilterCache_ImageBackedRaster, reporter) { |
| 173 | SkBitmap srcBM = create_bm(); |
| 174 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 175 | sk_sp<SkImage> srcImage(SkImage::MakeFromBitmap(srcBM)); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 176 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 177 | test_image_backed(reporter, srcImage); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 178 | } |
| 179 | |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 180 | #if SK_SUPPORT_GPU |
| 181 | #include "GrContext.h" |
| 182 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 183 | static GrTexture* create_texture(GrContext* context) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 184 | SkBitmap srcBM = create_bm(); |
| 185 | |
| 186 | GrSurfaceDesc desc; |
Brian Osman | 273f7d4 | 2016-10-14 16:48:17 -0400 | [diff] [blame] | 187 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 188 | desc.fFlags = kNone_GrSurfaceFlags; |
| 189 | desc.fWidth = kFullSize; |
| 190 | desc.fHeight = kFullSize; |
| 191 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 192 | return context->textureProvider()->createTexture(desc, SkBudgeted::kNo, srcBM.getPixels(), 0); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 193 | } |
| 194 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 195 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_ImageBackedGPU, reporter, ctxInfo) { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 196 | sk_sp<GrTexture> srcTexture(create_texture(ctxInfo.grContext())); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 197 | if (!srcTexture) { |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | GrBackendTextureDesc backendDesc; |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 202 | backendDesc.fConfig = kRGBA_8888_GrPixelConfig; |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 203 | backendDesc.fFlags = kNone_GrBackendTextureFlag; |
| 204 | backendDesc.fWidth = kFullSize; |
| 205 | backendDesc.fHeight = kFullSize; |
| 206 | backendDesc.fSampleCnt = 0; |
| 207 | backendDesc.fTextureHandle = srcTexture->getTextureHandle(); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 208 | sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(ctxInfo.grContext(), backendDesc, kPremul_SkAlphaType)); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 209 | if (!srcImage) { |
| 210 | return; |
| 211 | } |
| 212 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 213 | test_image_backed(reporter, srcImage); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 214 | } |
| 215 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 216 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_GPUBacked, reporter, ctxInfo) { |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 217 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 218 | sk_sp<GrTexture> srcTexture(create_texture(ctxInfo.grContext())); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 219 | if (!srcTexture) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 224 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 225 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromGpu(full, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 226 | kNeedNewImageUniqueID_SpecialImage, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 227 | srcTexture, nullptr)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 228 | |
| 229 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 230 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 231 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromGpu(subset, |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 232 | kNeedNewImageUniqueID_SpecialImage, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 233 | srcTexture, nullptr)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 234 | |
| 235 | test_find_existing(reporter, fullImg, subsetImg); |
| 236 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 237 | test_internal_purge(reporter, fullImg); |
| 238 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 239 | } |
| 240 | #endif |