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 | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 59 | class TestResource : public GrResource { |
| 60 | public: |
| 61 | SK_DECLARE_INST_COUNT(TestResource); |
| 62 | explicit TestResource(GrGpu* gpu) |
| 63 | : INHERITED(gpu, false) |
| 64 | , fCache(NULL) |
| 65 | , fToDelete(NULL) { |
| 66 | ++fAlive; |
| 67 | } |
| 68 | |
| 69 | ~TestResource() { |
| 70 | --fAlive; |
| 71 | if (NULL != fToDelete) { |
| 72 | // Breaks our little 2-element cycle below. |
| 73 | fToDelete->setDeleteWhenDestroyed(NULL, NULL); |
| 74 | fCache->deleteResource(fToDelete->getCacheEntry()); |
| 75 | } |
| 76 | this->release(); |
| 77 | } |
| 78 | |
| 79 | size_t sizeInBytes() const SK_OVERRIDE { return 100; } |
| 80 | |
| 81 | static int alive() { return fAlive; } |
| 82 | |
| 83 | void setDeleteWhenDestroyed(GrResourceCache* cache, TestResource* resource) { |
| 84 | fCache = cache; |
| 85 | fToDelete = resource; |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | GrResourceCache* fCache; |
| 90 | TestResource* fToDelete; |
| 91 | static int fAlive; |
| 92 | |
| 93 | typedef GrResource INHERITED; |
| 94 | }; |
| 95 | int TestResource::fAlive = 0; |
| 96 | |
| 97 | static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* context) { |
| 98 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 99 | GrCacheID::Key keyData; |
| 100 | keyData.fData64[0] = 5; |
| 101 | keyData.fData64[1] = 18; |
| 102 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 103 | GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 104 | |
| 105 | GrResourceCache cache(5, 30000); |
| 106 | |
| 107 | // Add two resources with the same key that delete each other from the cache when destroyed. |
| 108 | TestResource* a = new TestResource(context->getGpu()); |
| 109 | TestResource* b = new TestResource(context->getGpu()); |
| 110 | cache.addResource(key, a); |
| 111 | cache.addResource(key, b); |
| 112 | // Circle back. |
| 113 | a->setDeleteWhenDestroyed(&cache, b); |
| 114 | b->setDeleteWhenDestroyed(&cache, a); |
| 115 | a->unref(); |
| 116 | b->unref(); |
| 117 | |
| 118 | // Add a third independent resource also with the same key. |
| 119 | GrResource* r = new TestResource(context->getGpu()); |
| 120 | cache.addResource(key, r); |
| 121 | r->unref(); |
| 122 | |
| 123 | // Invalidate all three, all three should be purged and destroyed. |
| 124 | REPORTER_ASSERT(reporter, 3 == TestResource::alive()); |
| 125 | const GrResourceInvalidatedMessage msg = { key }; |
| 126 | SkMessageBus<GrResourceInvalidatedMessage>::Post(msg); |
| 127 | cache.purgeAsNeeded(); |
| 128 | REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 129 | } |
| 130 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 131 | static void test_cache_delete_on_destruction(skiatest::Reporter* reporter, |
| 132 | GrContext* context) { |
| 133 | GrCacheID::Domain domain = GrCacheID::GenerateDomain(); |
| 134 | GrCacheID::Key keyData; |
| 135 | keyData.fData64[0] = 5; |
| 136 | keyData.fData64[1] = 0; |
| 137 | GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); |
| 138 | |
| 139 | GrResourceKey key(GrCacheID(domain, keyData), t, 0); |
| 140 | |
| 141 | { |
| 142 | { |
| 143 | GrResourceCache cache(3, 30000); |
| 144 | TestResource* a = new TestResource(context->getGpu()); |
| 145 | TestResource* b = new TestResource(context->getGpu()); |
| 146 | cache.addResource(key, a); |
| 147 | cache.addResource(key, b); |
| 148 | |
| 149 | a->setDeleteWhenDestroyed(&cache, b); |
| 150 | b->setDeleteWhenDestroyed(&cache, a); |
| 151 | |
| 152 | a->unref(); |
| 153 | b->unref(); |
| 154 | REPORTER_ASSERT(reporter, 2 == TestResource::alive()); |
| 155 | } |
| 156 | REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 157 | } |
| 158 | { |
| 159 | GrResourceCache cache(3, 30000); |
| 160 | TestResource* a = new TestResource(context->getGpu()); |
| 161 | TestResource* b = new TestResource(context->getGpu()); |
| 162 | cache.addResource(key, a); |
| 163 | cache.addResource(key, b); |
| 164 | |
| 165 | a->setDeleteWhenDestroyed(&cache, b); |
| 166 | b->setDeleteWhenDestroyed(&cache, a); |
| 167 | |
| 168 | a->unref(); |
| 169 | b->unref(); |
| 170 | |
| 171 | cache.deleteResource(a->getCacheEntry()); |
| 172 | |
| 173 | REPORTER_ASSERT(reporter, 0 == TestResource::alive()); |
| 174 | } |
| 175 | } |
| 176 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 177 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 178 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 179 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 180 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 181 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 182 | continue; |
| 183 | } |
| 184 | GrContext* context = factory->get(glType); |
| 185 | if (NULL == context) { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | GrTextureDesc desc; |
| 190 | desc.fConfig = kSkia8888_GrPixelConfig; |
| 191 | desc.fFlags = kRenderTarget_GrTextureFlagBit; |
| 192 | desc.fWidth = gWidth; |
| 193 | desc.fHeight = gHeight; |
| 194 | |
| 195 | SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0)); |
| 196 | SkAutoTUnref<SkGpuDevice> device(SkNEW_ARGS(SkGpuDevice, (context, texture.get()))); |
| 197 | SkCanvas canvas(device.get()); |
| 198 | |
| 199 | test_cache(reporter, context, &canvas); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 200 | test_purge_invalidated(reporter, context); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 201 | test_cache_delete_on_destruction(reporter, context); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 205 | #endif |