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" |
| 13 | #include "SkImageFilterCacheKey.h" |
| 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; |
| 33 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCacheSize)); |
| 34 | |
| 35 | SkIRect clip = SkIRect::MakeWH(100, 100); |
| 36 | SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
| 37 | SkImageFilter::Cache::Key key2(0, SkMatrix::I(), clip, subset->uniqueID(), subset->subset()); |
| 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; |
| 57 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCacheSize)); |
| 58 | |
| 59 | SkIRect clip1 = SkIRect::MakeWH(100, 100); |
| 60 | SkIRect clip2 = SkIRect::MakeWH(200, 200); |
| 61 | SkImageFilter::Cache::Key key0(0, SkMatrix::I(), clip1, image->uniqueID(), image->subset()); |
| 62 | SkImageFilter::Cache::Key key1(1, SkMatrix::I(), clip1, image->uniqueID(), image->subset()); |
| 63 | SkImageFilter::Cache::Key key2(0, SkMatrix::MakeTrans(5, 5), clip1, |
| 64 | image->uniqueID(), image->subset()); |
| 65 | SkImageFilter::Cache::Key key3(0, SkMatrix::I(), clip2, image->uniqueID(), image->subset()); |
| 66 | SkImageFilter::Cache::Key key4(0, SkMatrix::I(), clip1, subset->uniqueID(), subset->subset()); |
| 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; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 82 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCacheSize)); |
| 83 | |
| 84 | SkIRect clip = SkIRect::MakeWH(100, 100); |
| 85 | SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
| 86 | SkImageFilter::Cache::Key key2(1, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
| 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; |
| 107 | SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCacheSize)); |
| 108 | |
| 109 | SkIRect clip = SkIRect::MakeWH(100, 100); |
| 110 | SkImageFilter::Cache::Key key1(0, SkMatrix::I(), clip, image->uniqueID(), image->subset()); |
| 111 | SkImageFilter::Cache::Key key2(1, SkMatrix::I(), clip, subset->uniqueID(), image->subset()); |
| 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 | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 116 | |
| 117 | SkIPoint foundOffset; |
| 118 | |
| 119 | REPORTER_ASSERT(reporter, cache->get(key1, &foundOffset)); |
| 120 | REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 121 | |
| 122 | cache->purgeByKeys(&key1, 1); |
| 123 | |
| 124 | REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 125 | REPORTER_ASSERT(reporter, cache->get(key2, &foundOffset)); |
| 126 | |
| 127 | cache->purge(); |
| 128 | |
| 129 | REPORTER_ASSERT(reporter, !cache->get(key1, &foundOffset)); |
| 130 | REPORTER_ASSERT(reporter, !cache->get(key2, &foundOffset)); |
| 131 | } |
| 132 | |
| 133 | DEF_TEST(ImageFilterCache_RasterBacked, reporter) { |
| 134 | SkBitmap srcBM = create_bm(); |
| 135 | |
| 136 | const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 137 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 138 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromRaster(nullptr, full, srcBM)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 139 | |
| 140 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 141 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 142 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromRaster(nullptr, subset, srcBM)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 143 | |
| 144 | test_find_existing(reporter, fullImg, subsetImg); |
| 145 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 146 | test_internal_purge(reporter, fullImg); |
| 147 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 148 | } |
| 149 | |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 150 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 151 | // Shared test code for both the raster and gpu-backed image cases |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 152 | static void test_image_backed(skiatest::Reporter* reporter, const sk_sp<SkImage>& srcImage) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 153 | const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 154 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 155 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromImage(nullptr, full, srcImage)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 156 | |
| 157 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 158 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 159 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromImage(nullptr, subset, srcImage)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 160 | |
| 161 | test_find_existing(reporter, fullImg, subsetImg); |
| 162 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 163 | test_internal_purge(reporter, fullImg); |
| 164 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 165 | } |
| 166 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 167 | DEF_TEST(ImageFilterCache_ImageBackedRaster, reporter) { |
| 168 | SkBitmap srcBM = create_bm(); |
| 169 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 170 | sk_sp<SkImage> srcImage(SkImage::MakeFromBitmap(srcBM)); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 171 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 172 | test_image_backed(reporter, srcImage); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 173 | } |
| 174 | |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 175 | #if SK_SUPPORT_GPU |
| 176 | #include "GrContext.h" |
| 177 | |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 178 | static GrTexture* create_texture(GrContext* context) { |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 179 | SkBitmap srcBM = create_bm(); |
| 180 | |
| 181 | GrSurfaceDesc desc; |
| 182 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 183 | desc.fFlags = kNone_GrSurfaceFlags; |
| 184 | desc.fWidth = kFullSize; |
| 185 | desc.fHeight = kFullSize; |
| 186 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 187 | return context->textureProvider()->createTexture(desc, SkBudgeted::kNo, srcBM.getPixels(), 0); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_ImageBackedGPU, reporter, context) { |
| 191 | SkAutoTUnref<GrTexture> srcTexture(create_texture(context)); |
| 192 | if (!srcTexture) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | GrBackendTextureDesc backendDesc; |
| 197 | backendDesc.fConfig = kSkia8888_GrPixelConfig; |
| 198 | backendDesc.fFlags = kNone_GrBackendTextureFlag; |
| 199 | backendDesc.fWidth = kFullSize; |
| 200 | backendDesc.fHeight = kFullSize; |
| 201 | backendDesc.fSampleCnt = 0; |
| 202 | backendDesc.fTextureHandle = srcTexture->getTextureHandle(); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 203 | sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context, backendDesc, kPremul_SkAlphaType)); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 204 | if (!srcImage) { |
| 205 | return; |
| 206 | } |
| 207 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 208 | test_image_backed(reporter, srcImage); |
robertphillips | cbc5bcc | 2016-02-19 10:41:12 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_GPUBacked, reporter, context) { |
| 212 | |
| 213 | SkAutoTUnref<GrTexture> srcTexture(create_texture(context)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 214 | if (!srcTexture) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize); |
| 219 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 220 | sk_sp<SkSpecialImage> fullImg(SkSpecialImage::MakeFromGpu(nullptr, full, |
| 221 | kNeedNewImageUniqueID_SpecialImage, |
| 222 | srcTexture)); |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 223 | |
| 224 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 225 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame^] | 226 | sk_sp<SkSpecialImage> subsetImg(SkSpecialImage::MakeFromGpu(nullptr, subset, |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 227 | kNeedNewImageUniqueID_SpecialImage, |
| 228 | srcTexture)); |
| 229 | |
| 230 | test_find_existing(reporter, fullImg, subsetImg); |
| 231 | test_dont_find_if_diff_key(reporter, fullImg, subsetImg); |
| 232 | test_internal_purge(reporter, fullImg); |
| 233 | test_explicit_purging(reporter, fullImg, subsetImg); |
| 234 | } |
| 235 | #endif |
| 236 | |