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