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 | |
| 44 | SkSpecialImage* foundImage = cache->get(key1, &foundOffset); |
| 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); |
| 157 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 158 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromImage(full, srcImage)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 159 | |
| 160 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 161 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 162 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromImage(subset, srcImage)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 163 | |
| 164 | test_find_existing(reporter, fullImg, subsetImg); |
| 165 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 166 | test_internal_purge(reporter, fullImg); |
| 167 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 168 | } |
| 169 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 170 | DEF_TEST(ImageFilterCache_ImageBackedRaster, reporter) { |
| 171 | SkBitmap srcBM = create_bm(); |
| 172 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 173 | sk_sp<SkImage> srcImage(SkImage::MakeFromBitmap(srcBM)); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 174 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 175 | test_image_backed(reporter, srcImage); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 176 | } |
| 177 | |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 178 | #if SK_SUPPORT_GPU |
| 179 | #include "GrContext.h" |
| 180 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 181 | static GrTexture* create_texture(GrContext* context) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 182 | SkBitmap srcBM = create_bm(); |
| 183 | |
| 184 | GrSurfaceDesc desc; |
Brian Osman | 273f7d4 | 2016-10-14 16:48:17 -0400 | [diff] [blame] | 185 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 186 | desc.fFlags = kNone_GrSurfaceFlags; |
| 187 | desc.fWidth = kFullSize; |
| 188 | desc.fHeight = kFullSize; |
| 189 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 190 | return context->textureProvider()->createTexture(desc, SkBudgeted::kNo, srcBM.getPixels(), 0); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 191 | } |
| 192 | |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 193 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_ImageBackedGPU, reporter, ctxInfo) { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame^] | 194 | sk_sp<GrTexture> srcTexture(create_texture(ctxInfo.grContext())); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 195 | if (!srcTexture) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | GrBackendTextureDesc backendDesc; |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 200 | backendDesc.fConfig = kRGBA_8888_GrPixelConfig; |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 201 | backendDesc.fFlags = kNone_GrBackendTextureFlag; |
| 202 | backendDesc.fWidth = kFullSize; |
| 203 | backendDesc.fHeight = kFullSize; |
| 204 | backendDesc.fSampleCnt = 0; |
| 205 | backendDesc.fTextureHandle = srcTexture->getTextureHandle(); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 206 | sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(ctxInfo.grContext(), backendDesc, kPremul_SkAlphaType)); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 207 | if (!srcImage) { |
| 208 | return; |
| 209 | } |
| 210 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 211 | test_image_backed(reporter, srcImage); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 212 | } |
| 213 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 214 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_GPUBacked, reporter, ctxInfo) { |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 215 | |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 216 | sk_sp<GrTexture> srcTexture(create_texture(ctxInfo.grContext())); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 217 | if (!srcTexture) { |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 222 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 223 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromGpu(full, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 224 | kNeedNewImageUniqueID_SpecialImage, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 225 | srcTexture, nullptr)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 226 | |
| 227 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 228 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 229 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromGpu(subset, |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 230 | kNeedNewImageUniqueID_SpecialImage, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 231 | srcTexture, nullptr)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 232 | |
| 233 | test_find_existing(reporter, fullImg, subsetImg); |
| 234 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 235 | test_internal_purge(reporter, fullImg); |
| 236 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 237 | } |
| 238 | #endif |