Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: tests/ImageFilterCacheTest.cpp

Issue 1709263002: Update SkImageFilter's cache to handle SkSpecialImages and add unit test (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix comment Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkSpecialImage.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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,
30 SkSpecialImage* image,
31 SkSpecialImage* subset) {
32 static const size_t kCacheSize = 1000000;
33 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache Size));
34
35 SkIRect clip = SkIRect::MakeWH(100, 100);
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());
38
39 SkIPoint offset = SkIPoint::Make(3, 4);
40 cache->set(key1, image, offset);
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,
54 SkSpecialImage* image,
55 SkSpecialImage* subset) {
56 static const size_t kCacheSize = 1000000;
57 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache Size));
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(), 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,
64 image->uniqueID(), image->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());
67
68 SkIPoint offset = SkIPoint::Make(3, 4);
69 cache->set(key0, image, offset);
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
79 static void test_internal_purge(skiatest::Reporter* reporter, SkSpecialImage* im age) {
80 SkASSERT(image->getSize());
81 static const size_t kCacheSize = image->getSize() + 10;
82 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache Size));
83
84 SkIRect clip = SkIRect::MakeWH(100, 100);
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());
87
88 SkIPoint offset = SkIPoint::Make(3, 4);
89 cache->set(key1, image, offset);
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
96 cache->set(key2, image, offset);
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,
104 SkSpecialImage* image,
105 SkSpecialImage* subset) {
106 static const size_t kCacheSize = 1000000;
107 SkAutoTUnref<SkImageFilter::Cache> cache(SkImageFilter::Cache::Create(kCache Size));
108
109 SkIRect clip = SkIRect::MakeWH(100, 100);
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());
112
113 SkIPoint offset = SkIPoint::Make(3, 4);
114 cache->set(key1, image, offset);
115 cache->set(key2, image, offset);
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();
Stephen White 2016/02/18 21:14:28 Not sure I understand why these paths are testing
robertphillips 2016/02/19 15:27:10 They should be testing the raster, gpu, and SkImag
Stephen White 2016/02/19 15:32:09 Shouldn't we be creating an SkImage, then doing a
robertphillips 2016/02/19 16:04:52 Right - once images are everywhere the _RasterBack
135
136 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize);
137
138 SkAutoTUnref<SkSpecialImage> fullImg(SkSpecialImage::NewFromRaster(nullptr, full, srcBM));
139
140 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller Size);
141
142 SkAutoTUnref<SkSpecialImage> subsetImg(SkSpecialImage::NewFromRaster(nullptr , subset, srcBM));
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
150 DEF_TEST(ImageFilterCache_ImageBacked, reporter) {
151 SkBitmap srcBM = create_bm();
152
153 SkAutoTUnref<SkImage> srcImage(SkImage::NewFromBitmap(srcBM));
154
155 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize);
156
157 SkAutoTUnref<SkSpecialImage> fullImg(SkSpecialImage::NewFromImage(full, srcI mage));
158
159 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller Size);
160
161 SkAutoTUnref<SkSpecialImage> subsetImg(SkSpecialImage::NewFromImage(subset, srcImage));
162
163 test_find_existing(reporter, fullImg, subsetImg);
164 test_dont_find_if_diff_key(reporter, fullImg, subsetImg);
165 test_internal_purge(reporter, fullImg);
166 test_explicit_purging(reporter, fullImg, subsetImg);
167 }
168
169 #if SK_SUPPORT_GPU
170 #include "GrContext.h"
171
172 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageFilterCache_GPUBacked, reporter, context ) {
173 SkBitmap srcBM = create_bm();
174
175 GrSurfaceDesc desc;
176 desc.fConfig = kSkia8888_GrPixelConfig;
177 desc.fFlags = kNone_GrSurfaceFlags;
178 desc.fWidth = kFullSize;
179 desc.fHeight = kFullSize;
180
181 SkAutoTUnref<GrTexture> srcTexture(context->textureProvider()->createTexture (desc, false,
182 srcBM.getPixels(),
183 0));
184 if (!srcTexture) {
185 return;
186 }
187
188 const SkIRect& full = SkIRect::MakeWH(kFullSize, kFullSize);
189
190 SkAutoTUnref<SkSpecialImage> fullImg(SkSpecialImage::NewFromGpu(
191 nullptr, full,
192 kNeedNewImageUni queID_SpecialImage,
193 srcTexture));
194
195 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmaller Size);
196
197 SkAutoTUnref<SkSpecialImage> subsetImg(SkSpecialImage::NewFromGpu(
198 nullptr, subset,
199 kNeedNewImageUni queID_SpecialImage,
200 srcTexture));
201
202 test_find_existing(reporter, fullImg, subsetImg);
203 test_dont_find_if_diff_key(reporter, fullImg, subsetImg);
204 test_internal_purge(reporter, fullImg);
205 test_explicit_purging(reporter, fullImg, subsetImg);
206 }
207 #endif
208
OLDNEW
« no previous file with comments | « src/core/SkSpecialImage.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698