commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 8 | #if SK_SUPPORT_GPU |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 9 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 10 | #include "GrContextFactory.h" |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 11 | #include "GrResourceCache.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 12 | #include "SkGpuDevice.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 13 | #include "Test.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 14 | |
| 15 | static const int gWidth = 640; |
| 16 | static const int gHeight = 480; |
| 17 | |
| 18 | //////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 19 | static void test_cache(skiatest::Reporter* reporter, |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 20 | GrContext* context, |
| 21 | SkCanvas* canvas) { |
| 22 | const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
| 23 | |
| 24 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 25 | src.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 26 | src.eraseColor(SK_ColorBLACK); |
| 27 | size_t srcSize = src.getSize(); |
| 28 | |
| 29 | size_t initialCacheSize = context->getGpuTextureCacheBytes(); |
| 30 | |
| 31 | int oldMaxNum; |
| 32 | size_t oldMaxBytes; |
| 33 | context->getTextureCacheLimits(&oldMaxNum, &oldMaxBytes); |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 34 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 35 | // Set the cache limits so we can fit 10 "src" images and the |
| 36 | // max number of textures doesn't matter |
| 37 | size_t maxCacheSize = initialCacheSize + 10*srcSize; |
| 38 | context->setTextureCacheLimits(1000, maxCacheSize); |
| 39 | |
| 40 | SkBitmap readback; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 41 | readback.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 42 | |
| 43 | for (int i = 0; i < 100; ++i) { |
| 44 | canvas->drawBitmap(src, 0, 0); |
| 45 | canvas->readPixels(size, &readback); |
| 46 | |
| 47 | // "modify" the src texture |
| 48 | src.notifyPixelsChanged(); |
| 49 | |
| 50 | size_t curCacheSize = context->getGpuTextureCacheBytes(); |
| 51 | |
| 52 | // we should never go over the size limit |
| 53 | REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 54 | } |
| 55 | |
| 56 | context->setTextureCacheLimits(oldMaxNum, oldMaxBytes); |
| 57 | } |
| 58 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 59 | class TestResource : public GrCacheable { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 60 | static const size_t kDefaultSize = 100; |
| 61 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 62 | public: |
| 63 | SK_DECLARE_INST_COUNT(TestResource); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 64 | TestResource(size_t size = kDefaultSize) |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 65 | : fCache(NULL) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 66 | , fToDelete(NULL) |
| 67 | , fSize(size) { |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 68 | ++fAlive; |
| 69 | } |
| 70 | |
| 71 | ~TestResource() { |
| 72 | --fAlive; |
| 73 | if (NULL != fToDelete) { |
| 74 | // Breaks our little 2-element cycle below. |
| 75 | fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
| 76 | fCache->deleteResource(fToDelete->getCacheEntry()); |
| 77 | } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 78 | } |
| 79 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 80 | void setSize(size_t size) { |
| 81 | fSize = size; |
| 82 | this->didChangeGpuMemorySize(); |
| 83 | } |
| 84 | |
| 85 | size_t gpuMemorySize() const SK_OVERRIDE { return fSize; } |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 86 | |
| 87 | bool isValidOnGpu() const SK_OVERRIDE { return true; } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 88 | |
| 89 | static int alive() { return fAlive; } |
| 90 | |
| 91 | void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) { |
| 92 | fCache = cache; |
| 93 | fToDelete = resource; |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | GrResourceCache* fCache; |
| 98 | TestResource* fToDelete; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 99 | size_t fSize; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 100 | static int fAlive; |
| 101 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 102 | typedef GrCacheable INHERITED; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 103 | }; |
| 104 | int TestResource::fAlive = 0; |
| 105 | |
| 106 | static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* context) { |
| 107 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 108 | GrCacheID::Key keyData; |
| 109 | keyData.fData64[0] = 5; |
| 110 | keyData.fData64[1] = 18; |
| 111 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 112 | GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 113 | |
| 114 | GrResourceCache cache(5, 30000); |
| 115 | |
| 116 | // Add two resources with the same key that delete each other from the cache when destroyed. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 117 | TestResource* a = new TestResource(); |
| 118 | TestResource* b = new TestResource(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 119 | cache.addResource(key, a); |
| 120 | cache.addResource(key, b); |
| 121 | // Circle back. |
| 122 | a->setDeleteWhenDestroyed(&cache, b); |
| 123 | b->setDeleteWhenDestroyed(&cache, a); |
| 124 | a->unref(); |
| 125 | b->unref(); |
| 126 | |
| 127 | // Add a third independent resource also with the same key. |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 128 | GrCacheable* r = new TestResource(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 129 | cache.addResource(key, r); |
| 130 | r->unref(); |
| 131 | |
| 132 | // Invalidate all three, all three should be purged and destroyed. |
| 133 | REPORTER_ASSERT(reporter, 3 == TestResource::alive()); |
| 134 | const GrResourceInvalidatedMessage msg = { key }; |
| 135 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); |
| 136 | cache.purgeAsNeeded(); |
| 137 | REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 138 | } |
| 139 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 140 | static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, |
| 141 | GrContext* context) { |
| 142 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 143 | GrCacheID::Key keyData; |
| 144 | keyData.fData64[0] = 5; |
| 145 | keyData.fData64[1] = 0; |
| 146 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 147 | |
| 148 | GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 149 | |
| 150 | { |
| 151 | { |
| 152 | GrResourceCache cache(3, 30000); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 153 | TestResource* a = new TestResource(); |
| 154 | TestResource* b = new TestResource(); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 155 | cache.addResource(key, a); |
| 156 | cache.addResource(key, b); |
| 157 | |
| 158 | a->setDeleteWhenDestroyed(&cache, b); |
| 159 | b->setDeleteWhenDestroyed(&cache, a); |
| 160 | |
| 161 | a->unref(); |
| 162 | b->unref(); |
| 163 | REPORTER_ASSERT(reporter, 2 == TestResource::alive()); |
| 164 | } |
| 165 | REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 166 | } |
| 167 | { |
| 168 | GrResourceCache cache(3, 30000); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 169 | TestResource* a = new TestResource(); |
| 170 | TestResource* b = new TestResource(); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 171 | cache.addResource(key, a); |
| 172 | cache.addResource(key, b); |
| 173 | |
| 174 | a->setDeleteWhenDestroyed(&cache, b); |
| 175 | b->setDeleteWhenDestroyed(&cache, a); |
| 176 | |
| 177 | a->unref(); |
| 178 | b->unref(); |
| 179 | |
| 180 | cache.deleteResource(a->getCacheEntry()); |
| 181 | |
| 182 | REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 183 | } |
| 184 | } |
| 185 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 186 | static void test_resource_size_changed(skiatest::Reporter* reporter, |
| 187 | GrContext* context) { |
| 188 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 189 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 190 | |
| 191 | GrCacheID::Key key1Data; |
| 192 | key1Data.fData64[0] = 0; |
| 193 | key1Data.fData64[1] = 0; |
| 194 | GrResourceKey key1(GrCacheID(domain, key1Data), t, 0); |
| 195 | |
| 196 | GrCacheID::Key key2Data; |
| 197 | key2Data.fData64[0] = 1; |
| 198 | key2Data.fData64[1] = 0; |
| 199 | GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); |
| 200 | |
| 201 | // Test changing resources sizes (both increase & decrease). |
| 202 | { |
| 203 | GrResourceCache cache(2, 300); |
| 204 | |
| 205 | TestResource* a = new TestResource(0); |
| 206 | a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. |
| 207 | cache.addResource(key1, a); |
| 208 | a->unref(); |
| 209 | |
| 210 | TestResource* b = new TestResource(0); |
| 211 | b->setSize(100); |
| 212 | cache.addResource(key2, b); |
| 213 | b->unref(); |
| 214 | |
| 215 | REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); |
| 216 | REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 217 | |
| 218 | static_cast<TestResource*>(cache.find(key2))->setSize(200); |
| 219 | static_cast<TestResource*>(cache.find(key1))->setSize(50); |
| 220 | |
| 221 | REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes()); |
| 222 | REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 223 | } |
| 224 | |
| 225 | // Test increasing a resources size beyond the cache budget. |
| 226 | { |
| 227 | GrResourceCache cache(2, 300); |
| 228 | |
| 229 | TestResource* a = new TestResource(100); |
| 230 | cache.addResource(key1, a); |
| 231 | a->unref(); |
| 232 | |
| 233 | TestResource* b = new TestResource(100); |
| 234 | cache.addResource(key2, b); |
| 235 | b->unref(); |
| 236 | |
| 237 | REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); |
| 238 | REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 239 | |
| 240 | static_cast<TestResource*>(cache.find(key2))->setSize(201); |
| 241 | REPORTER_ASSERT(reporter, NULL == cache.find(key1)); |
| 242 | |
| 243 | REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes()); |
| 244 | REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount()); |
| 245 | } |
| 246 | |
| 247 | // Test changing the size of an exclusively-held resource. |
| 248 | { |
| 249 | GrResourceCache cache(2, 300); |
| 250 | |
| 251 | TestResource* a = new TestResource(100); |
| 252 | cache.addResource(key1, a); |
| 253 | cache.makeExclusive(a->getCacheEntry()); |
| 254 | |
| 255 | TestResource* b = new TestResource(100); |
| 256 | cache.addResource(key2, b); |
| 257 | b->unref(); |
| 258 | |
| 259 | REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); |
| 260 | REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 261 | REPORTER_ASSERT(reporter, NULL == cache.find(key1)); |
| 262 | |
| 263 | a->setSize(200); |
| 264 | |
| 265 | REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes()); |
| 266 | REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 267 | // Internal resource cache validation will test the detached size (debug mode only). |
| 268 | |
| 269 | cache.makeNonExclusive(a->getCacheEntry()); |
| 270 | a->unref(); |
| 271 | |
| 272 | REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes()); |
| 273 | REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); |
| 274 | REPORTER_ASSERT(reporter, NULL != cache.find(key1)); |
| 275 | // Internal resource cache validation will test the detached size (debug mode only). |
| 276 | } |
| 277 | } |
| 278 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 279 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 280 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 281 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 282 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 283 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 284 | continue; |
| 285 | } |
| 286 | GrContext* context = factory->get(glType); |
| 287 | if (NULL == context) { |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | GrTextureDesc desc; |
| 292 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 293 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 294 | desc.fWidth = gWidth; |
| 295 | desc.fHeight = gHeight; |
| 296 | |
| 297 | SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0)); |
| 298 | SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get()))); |
| 299 | SkCanvas canvas(device.get()); |
| 300 | |
| 301 | test_cache(reporter, context, &canvas); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 302 | test_purge_invalidated(reporter, context); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 303 | test_cache_delete_on_destruction(reporter, context); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 304 | test_resource_size_changed(reporter, context); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 308 | #endif |