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 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 11 | #include "GrContextFactory.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 12 | #include "GrGpu.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 13 | #include "GrGpuResourceCacheAccess.h" |
| 14 | #include "GrGpuResourcePriv.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 15 | #include "GrResourceCache.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 16 | #include "SkCanvas.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 17 | #include "SkGr.h" |
| 18 | #include "SkMessageBus.h" |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 19 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 20 | #include "Test.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 21 | |
| 22 | static const int gWidth = 640; |
| 23 | static const int gHeight = 480; |
| 24 | |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 26 | static void test_cache(skiatest::Reporter* reporter, GrContext* context, SkCanvas* canvas) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 27 | const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
| 28 | |
| 29 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 30 | src.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 31 | src.eraseColor(SK_ColorBLACK); |
| 32 | size_t srcSize = src.getSize(); |
| 33 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 34 | size_t initialCacheSize; |
| 35 | context->getResourceCacheUsage(NULL, &initialCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 36 | |
| 37 | int oldMaxNum; |
| 38 | size_t oldMaxBytes; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 39 | context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes); |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 41 | // Set the cache limits so we can fit 10 "src" images and the |
| 42 | // max number of textures doesn't matter |
| 43 | size_t maxCacheSize = initialCacheSize + 10*srcSize; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 44 | context->setResourceCacheLimits(1000, maxCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 45 | |
| 46 | SkBitmap readback; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 47 | readback.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 48 | |
| 49 | for (int i = 0; i < 100; ++i) { |
| 50 | canvas->drawBitmap(src, 0, 0); |
| 51 | canvas->readPixels(size, &readback); |
| 52 | |
| 53 | // "modify" the src texture |
| 54 | src.notifyPixelsChanged(); |
| 55 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 56 | size_t curCacheSize; |
| 57 | context->getResourceCacheUsage(NULL, &curCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 58 | |
| 59 | // we should never go over the size limit |
| 60 | REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 61 | } |
| 62 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 63 | context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 64 | } |
| 65 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 66 | class TestResource : public GrGpuResource { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 67 | static const size_t kDefaultSize = 100; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 68 | enum ScratchConstructor { kScratchConstructor }; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 69 | public: |
| 70 | SK_DECLARE_INST_COUNT(TestResource); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 71 | /** Property that distinctly categorizes the resource. |
| 72 | * For example, textures have width, height, ... */ |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 73 | enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 74 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 75 | TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) |
| 76 | : INHERITED(gpu, lifeCycle) |
| 77 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 78 | , fSize(size) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 79 | , fProperty(kA_SimulatedProperty) { |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 80 | ++fNumAlive; |
| 81 | this->registerWithCache(); |
| 82 | } |
| 83 | |
| 84 | TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) |
| 85 | : INHERITED(gpu, lifeCycle) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 86 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 87 | , fSize(kDefaultSize) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 88 | , fProperty(kA_SimulatedProperty) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 89 | ++fNumAlive; |
| 90 | this->registerWithCache(); |
| 91 | } |
| 92 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 93 | TestResource(GrGpu* gpu) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 94 | : INHERITED(gpu, kCached_LifeCycle) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 95 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 96 | , fSize(kDefaultSize) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 97 | , fProperty(kA_SimulatedProperty) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 98 | ++fNumAlive; |
| 99 | this->registerWithCache(); |
| 100 | } |
| 101 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 102 | static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 103 | return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor)); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ~TestResource() { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 107 | --fNumAlive; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 108 | SkSafeUnref(fToDelete); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 109 | } |
| 110 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 111 | void setSize(size_t size) { |
| 112 | fSize = size; |
| 113 | this->didChangeGpuMemorySize(); |
| 114 | } |
| 115 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 116 | static int NumAlive() { return fNumAlive; } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 117 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 118 | void setUnrefWhenDestroyed(TestResource* resource) { |
| 119 | SkRefCnt_SafeAssign(fToDelete, resource); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 120 | } |
| 121 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 122 | static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) { |
| 123 | static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType(); |
| 124 | GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 125 | for (int i = 0; i < kScratchKeyFieldCnt; ++i) { |
| 126 | builder[i] = static_cast<uint32_t>(i + property); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | static size_t ExpectedScratchKeySize() { |
| 131 | return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt); |
| 132 | } |
| 133 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 134 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 135 | static const int kScratchKeyFieldCnt = 6; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 136 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 137 | TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor) |
| 138 | : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 139 | , fToDelete(NULL) |
| 140 | , fSize(kDefaultSize) |
| 141 | , fProperty(property) { |
| 142 | GrScratchKey scratchKey; |
| 143 | ComputeScratchKey(fProperty, &scratchKey); |
| 144 | this->setScratchKey(scratchKey); |
| 145 | ++fNumAlive; |
| 146 | this->registerWithCache(); |
| 147 | } |
| 148 | |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 149 | size_t onGpuMemorySize() const SK_OVERRIDE { return fSize; } |
| 150 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 151 | TestResource* fToDelete; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 152 | size_t fSize; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 153 | static int fNumAlive; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 154 | SimulatedProperty fProperty; |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 155 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 156 | }; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 157 | int TestResource::fNumAlive = 0; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 158 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 159 | class Mock { |
| 160 | public: |
| 161 | Mock(int maxCnt, size_t maxBytes) { |
| 162 | fContext.reset(GrContext::CreateMockContext()); |
| 163 | SkASSERT(fContext); |
| 164 | fContext->setResourceCacheLimits(maxCnt, maxBytes); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 165 | GrResourceCache* cache = fContext->getResourceCache(); |
| 166 | cache->purgeAllUnlocked(); |
| 167 | SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 168 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 169 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 170 | GrResourceCache* cache() { return fContext->getResourceCache(); } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 171 | |
| 172 | GrContext* context() { return fContext; } |
| 173 | |
| 174 | private: |
| 175 | SkAutoTUnref<GrContext> fContext; |
| 176 | }; |
| 177 | |
| 178 | static void test_no_key(skiatest::Reporter* reporter) { |
| 179 | Mock mock(10, 30000); |
| 180 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 181 | GrResourceCache* cache = mock.cache(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 182 | |
| 183 | // Create a bunch of resources with no keys |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 184 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 185 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 186 | TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 187 | TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 188 | a->setSize(11); |
| 189 | b->setSize(12); |
| 190 | c->setSize(13); |
| 191 | d->setSize(14); |
| 192 | |
| 193 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 195 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 196 | d->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 197 | |
| 198 | // Should be safe to purge without deleting the resources since we still have refs. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 199 | cache->purgeAllUnlocked(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 200 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 201 | |
| 202 | // Since the resources have neither content nor scratch keys, delete immediately upon unref. |
| 203 | |
| 204 | a->unref(); |
| 205 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 206 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 207 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 208 | cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 209 | |
| 210 | c->unref(); |
| 211 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 212 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 213 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 214 | cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 215 | |
| 216 | d->unref(); |
| 217 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 218 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 219 | REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 220 | |
| 221 | b->unref(); |
| 222 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 223 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 224 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 225 | } |
| 226 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 227 | // Each integer passed as a template param creates a new domain. |
| 228 | template <int> static void make_content_key(GrContentKey* key, int data) { |
| 229 | static GrContentKey::Domain d = GrContentKey::GenerateDomain(); |
| 230 | GrContentKey::Builder builder(key, d, 1); |
| 231 | builder[0] = data; |
| 232 | } |
| 233 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 234 | static void test_budgeting(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 235 | Mock mock(10, 300); |
| 236 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 237 | GrResourceCache* cache = mock.cache(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 238 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 239 | GrContentKey contentKey; |
| 240 | make_content_key<0>(&contentKey, 0); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 241 | |
| 242 | // Create a scratch, a content, and a wrapped resource |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 243 | TestResource* scratch = |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 244 | TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 245 | scratch->setSize(10); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 246 | TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 247 | content->setSize(11); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 248 | REPORTER_ASSERT(reporter, content->resourcePriv().setContentKey(contentKey)); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 249 | TestResource* wrapped = SkNEW_ARGS(TestResource, |
| 250 | (context->getGpu(), GrGpuResource::kWrapped_LifeCycle)); |
| 251 | wrapped->setSize(12); |
| 252 | TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
| 253 | (context->getGpu(), GrGpuResource::kUncached_LifeCycle)); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 254 | unbudgeted->setSize(13); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 255 | |
| 256 | // Make sure we can't add a content key to the wrapped resource |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 257 | GrContentKey contentKey2; |
| 258 | make_content_key<0>(&contentKey2, 1); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 259 | REPORTER_ASSERT(reporter, !wrapped->resourcePriv().setContentKey(contentKey2)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 260 | REPORTER_ASSERT(reporter, NULL == cache->findAndRefContentResource(contentKey2)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 261 | |
| 262 | // Make sure sizes are as we expect |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 264 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 265 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 266 | cache->getResourceBytes()); |
| 267 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 268 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 269 | cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 270 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 271 | // Our refs mean that the resources are non purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 272 | cache->purgeAllUnlocked(); |
| 273 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 274 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 275 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 276 | cache->getResourceBytes()); |
| 277 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 278 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 279 | cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 280 | |
| 281 | // Unreffing the wrapped resource should free it right away. |
| 282 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 283 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 284 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 285 | unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 286 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 287 | // Now try freeing the budgeted resources first |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 288 | wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 289 | scratch->setSize(12); |
| 290 | content->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 291 | cache->purgeAllUnlocked(); |
| 292 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 293 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 294 | unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
| 295 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 296 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 297 | |
| 298 | scratch->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 299 | cache->purgeAllUnlocked(); |
| 300 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 301 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 302 | cache->getResourceBytes()); |
| 303 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 304 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 305 | |
| 306 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 307 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 308 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
| 309 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 310 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 311 | |
| 312 | unbudgeted->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 313 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 314 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 315 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 316 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 317 | } |
| 318 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 319 | static void test_unbudgeted(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 320 | Mock mock(10, 30000); |
| 321 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 322 | GrResourceCache* cache = mock.cache(); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 323 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 324 | GrContentKey contentKey; |
| 325 | make_content_key<0>(&contentKey, 0); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 326 | |
| 327 | TestResource* scratch; |
| 328 | TestResource* content; |
| 329 | TestResource* wrapped; |
| 330 | TestResource* unbudgeted; |
| 331 | |
| 332 | // A large uncached or wrapped resource shouldn't evict anything. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 333 | scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 334 | scratch->setSize(10); |
| 335 | scratch->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 336 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 337 | REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); |
| 338 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 339 | REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 340 | |
| 341 | content = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 342 | content->setSize(11); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 343 | REPORTER_ASSERT(reporter, content->resourcePriv().setContentKey(contentKey)); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 344 | content->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 345 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 346 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 347 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 348 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 349 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 350 | size_t large = 2 * cache->getResourceBytes(); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 351 | unbudgeted = SkNEW_ARGS(TestResource, |
| 352 | (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 353 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 354 | REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 355 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 356 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 357 | |
| 358 | unbudgeted->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 359 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 360 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 361 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 362 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 363 | |
| 364 | wrapped = SkNEW_ARGS(TestResource, |
| 365 | (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 366 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 367 | REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 368 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 369 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 370 | |
| 371 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 372 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 373 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 374 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 375 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 376 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 377 | cache->purgeAllUnlocked(); |
| 378 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 379 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 380 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 381 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 382 | } |
| 383 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 384 | // This method can't be static because it needs to friended in GrGpuResource::CacheAccess. |
| 385 | void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); |
| 386 | /*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 387 | Mock mock(10, 300); |
| 388 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 389 | GrResourceCache* cache = mock.cache(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 390 | |
| 391 | TestResource* resource = |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 392 | TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 393 | GrScratchKey key; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 394 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 395 | |
| 396 | size_t size = resource->gpuMemorySize(); |
| 397 | for (int i = 0; i < 2; ++i) { |
| 398 | // Since this resource is unbudgeted, it should not be reachable as scratch. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 399 | REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 400 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 401 | REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 402 | REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key)); |
| 403 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 404 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 405 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 406 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 407 | |
| 408 | // Once it is unrefed, it should become available as scratch. |
| 409 | resource->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 410 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 411 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 412 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 413 | REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes()); |
| 414 | resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key)); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 415 | REPORTER_ASSERT(reporter, resource); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 416 | REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 417 | REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 418 | REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 419 | |
| 420 | if (0 == i) { |
| 421 | // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try |
| 422 | // the above tests again. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 423 | resource->resourcePriv().makeUnbudgeted(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 424 | } else { |
| 425 | // After the second time around, try removing the scratch key |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 426 | resource->resourcePriv().removeScratchKey(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 427 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 428 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 429 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 430 | REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 431 | REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 432 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 433 | REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 434 | |
| 435 | // now when it is unrefed it should die since it has no key. |
| 436 | resource->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 437 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 438 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 439 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 440 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 441 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 442 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 446 | Mock mock(5, 30000); |
| 447 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 448 | GrResourceCache* cache = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 449 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 450 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 451 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 452 | TestResource::kB_SimulatedProperty); |
| 453 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 454 | TestResource::kB_SimulatedProperty); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 455 | a->setSize(11); |
| 456 | b->setSize(12); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 457 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 458 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 459 | // Check for negative case consistency. (leaks upon test failure.) |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 460 | REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1)); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 461 | |
| 462 | GrScratchKey scratchKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 463 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 464 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 465 | // Scratch resources are registered with GrResourceCache just by existing. There are 2. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 466 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 467 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
| 468 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 469 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 470 | cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 471 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 472 | // Our refs mean that the resources are non purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 473 | cache->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 474 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 475 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 476 | |
| 477 | // Unref but don't purge |
| 478 | a->unref(); |
| 479 | b->unref(); |
| 480 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 481 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 482 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 483 | // Purge again. This time resources should be purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 484 | cache->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 485 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 486 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 487 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 488 | } |
| 489 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 490 | static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 491 | Mock mock(5, 30000); |
| 492 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 493 | GrResourceCache* cache = mock.cache(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 494 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 495 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 496 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 497 | TestResource::kB_SimulatedProperty); |
| 498 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 499 | TestResource::kB_SimulatedProperty); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 500 | a->unref(); |
| 501 | b->unref(); |
| 502 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 503 | |
| 504 | GrScratchKey scratchKey; |
| 505 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 506 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 507 | // (following leaks upon test failure). |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 508 | REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 509 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 510 | // Scratch resources are registered with GrResourceCache just by existing. There are 2. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 511 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 512 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 513 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
| 514 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 515 | |
| 516 | // Find the first resource and remove its scratch key |
| 517 | GrGpuResource* find; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 518 | find = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 519 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 520 | // It's still alive, but not cached by scratch key anymore |
| 521 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 522 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));) |
| 523 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 524 | |
| 525 | // The cache should immediately delete it when it's unrefed since it isn't accessible. |
| 526 | find->unref(); |
| 527 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 528 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));) |
| 529 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 530 | |
| 531 | // Repeat for the second resource. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 532 | find = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 533 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 534 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 535 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 536 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 537 | |
| 538 | // Should be able to call this multiple times with no problem. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 539 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 540 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 541 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 542 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 543 | |
| 544 | find->unref(); |
| 545 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 546 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 547 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 548 | } |
| 549 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 550 | static void test_scratch_key_consistency(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 551 | Mock mock(5, 30000); |
| 552 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 553 | GrResourceCache* cache = mock.cache(); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 554 | |
| 555 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 556 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 557 | TestResource::kB_SimulatedProperty); |
| 558 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 559 | TestResource::kB_SimulatedProperty); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 560 | a->unref(); |
| 561 | b->unref(); |
| 562 | |
| 563 | GrScratchKey scratchKey; |
| 564 | // Ensure that scratch key comparison and assignment is consistent. |
| 565 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 566 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 567 | GrScratchKey scratchKey2; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 568 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 569 | REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize()); |
| 570 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2); |
| 571 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1); |
| 572 | scratchKey = scratchKey1; |
| 573 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 574 | REPORTER_ASSERT(reporter, scratchKey1 == scratchKey); |
| 575 | REPORTER_ASSERT(reporter, scratchKey == scratchKey1); |
| 576 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey); |
| 577 | REPORTER_ASSERT(reporter, scratchKey != scratchKey2); |
| 578 | scratchKey = scratchKey2; |
| 579 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 580 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); |
| 581 | REPORTER_ASSERT(reporter, scratchKey != scratchKey1); |
| 582 | REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); |
| 583 | REPORTER_ASSERT(reporter, scratchKey == scratchKey2); |
| 584 | |
| 585 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 586 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 587 | // (following leaks upon test failure). |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 588 | REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 589 | |
| 590 | // Find the first resource with a scratch key and a copy of a scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 591 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 592 | GrGpuResource* find = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 593 | REPORTER_ASSERT(reporter, find != NULL); |
| 594 | find->unref(); |
| 595 | |
| 596 | scratchKey2 = scratchKey; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 597 | find = cache->findAndRefScratchResource(scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 598 | REPORTER_ASSERT(reporter, find != NULL); |
| 599 | REPORTER_ASSERT(reporter, find == a || find == b); |
| 600 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 601 | GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 602 | REPORTER_ASSERT(reporter, find2 != NULL); |
| 603 | REPORTER_ASSERT(reporter, find2 == a || find2 == b); |
| 604 | REPORTER_ASSERT(reporter, find2 != find); |
| 605 | find2->unref(); |
| 606 | find->unref(); |
| 607 | } |
| 608 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 609 | static void test_duplicate_content_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 610 | Mock mock(5, 30000); |
| 611 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 612 | GrResourceCache* cache = mock.cache(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 613 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 614 | GrContentKey key; |
| 615 | make_content_key<0>(&key, 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 616 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 617 | // Create two resources that we will attempt to register with the same content key. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 618 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 619 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 620 | a->setSize(11); |
| 621 | b->setSize(12); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 622 | |
| 623 | // Can't set the same content key on two resources. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 624 | REPORTER_ASSERT(reporter, a->resourcePriv().setContentKey(key)); |
| 625 | REPORTER_ASSERT(reporter, !b->resourcePriv().setContentKey(key)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 626 | |
| 627 | // Still have two resources because b is still reffed. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 628 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 629 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 630 | cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 631 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 632 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 633 | b->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 634 | // Now b should be gone. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 635 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 636 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 637 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 638 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 639 | cache->purgeAllUnlocked(); |
| 640 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 641 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 642 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 643 | |
| 644 | // Drop the ref on a but it isn't immediately purged as it still has a valid scratch key. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 645 | a->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 646 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 647 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 648 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 649 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 650 | cache->purgeAllUnlocked(); |
| 651 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 652 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 653 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 654 | } |
| 655 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 656 | static void test_purge_invalidated(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 657 | Mock mock(5, 30000); |
| 658 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 659 | GrResourceCache* cache = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 660 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 661 | GrContentKey key1, key2, key3; |
| 662 | make_content_key<0>(&key1, 1); |
| 663 | make_content_key<0>(&key2, 2); |
| 664 | make_content_key<0>(&key3, 3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 665 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 666 | // Add three resources to the cache. Only c is usable as scratch. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 667 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 668 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 669 | TestResource* c = TestResource::CreateScratch(context->getGpu(), |
| 670 | TestResource::kA_SimulatedProperty); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 671 | a->resourcePriv().setContentKey(key1); |
| 672 | b->resourcePriv().setContentKey(key2); |
| 673 | c->resourcePriv().setContentKey(key3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 674 | a->unref(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 675 | // hold b until *after* the message is sent. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 676 | c->unref(); |
| 677 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 678 | REPORTER_ASSERT(reporter, cache->hasContentKey(key1)); |
| 679 | REPORTER_ASSERT(reporter, cache->hasContentKey(key2)); |
| 680 | REPORTER_ASSERT(reporter, cache->hasContentKey(key3)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 681 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 682 | |
| 683 | typedef GrContentKeyInvalidatedMessage Msg; |
| 684 | typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus; |
| 685 | |
| 686 | // Invalidate two of the three, they should be purged and no longer accessible via their keys. |
| 687 | Bus::Post(Msg(key1)); |
| 688 | Bus::Post(Msg(key2)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 689 | cache->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 690 | // a should be deleted now, but we still have a ref on b. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 691 | REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); |
| 692 | REPORTER_ASSERT(reporter, !cache->hasContentKey(key2)); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 693 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 694 | REPORTER_ASSERT(reporter, cache->hasContentKey(key3)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 695 | |
| 696 | // Invalidate the third. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 697 | Bus::Post(Msg(key3)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 698 | cache->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 699 | // we still have a ref on b, c should be recycled as scratch. |
| 700 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 701 | REPORTER_ASSERT(reporter, !cache->hasContentKey(key3)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 702 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 703 | // make b purgeable. It should be immediately deleted since it has no key. |
| 704 | b->unref(); |
| 705 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 706 | |
| 707 | // Make sure we actually get to c via it's scratch key, before we say goodbye. |
| 708 | GrScratchKey scratchKey; |
| 709 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 710 | GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 711 | REPORTER_ASSERT(reporter, scratch == c); |
| 712 | SkSafeUnref(scratch); |
| 713 | |
| 714 | // Get rid of c. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 715 | cache->purgeAllUnlocked(); |
| 716 | scratch = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 717 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 718 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 719 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 720 | REPORTER_ASSERT(reporter, !scratch); |
| 721 | SkSafeUnref(scratch); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 722 | } |
| 723 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 724 | static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 725 | Mock mock(3, 30000); |
| 726 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 727 | GrResourceCache* cache = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 728 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 729 | GrContentKey key1, key2; |
| 730 | make_content_key<0>(&key1, 1); |
| 731 | make_content_key<0>(&key2, 2); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 732 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 733 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 734 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 735 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 736 | a->resourcePriv().setContentKey(key1); |
| 737 | b->resourcePriv().setContentKey(key2); |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 738 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 739 | // Make a cycle |
| 740 | a->setUnrefWhenDestroyed(b); |
| 741 | b->setUnrefWhenDestroyed(a); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 742 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 743 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 744 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 745 | a->unref(); |
| 746 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 747 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 748 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 749 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 750 | cache->purgeAllUnlocked(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 751 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 752 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 753 | // Break the cycle |
| 754 | a->setUnrefWhenDestroyed(NULL); |
| 755 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 756 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 757 | cache->purgeAllUnlocked(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 758 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 759 | } |
| 760 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 761 | static void test_resource_size_changed(skiatest::Reporter* reporter) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 762 | GrContentKey key1, key2; |
| 763 | make_content_key<0>(&key1, 1); |
| 764 | make_content_key<0>(&key2, 2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 765 | |
| 766 | // Test changing resources sizes (both increase & decrease). |
| 767 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 768 | Mock mock(3, 30000); |
| 769 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 770 | GrResourceCache* cache = mock.cache(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 771 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 772 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 773 | a->resourcePriv().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 774 | a->unref(); |
| 775 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 776 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 777 | b->resourcePriv().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 778 | b->unref(); |
| 779 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 780 | REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 781 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 782 | { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 783 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->findAndRefContentResource(key2))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 784 | find2->setSize(200); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 785 | SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache->findAndRefContentResource(key1))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 786 | find1->setSize(50); |
| 787 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 788 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 789 | REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); |
| 790 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // Test increasing a resources size beyond the cache budget. |
| 794 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 795 | Mock mock(2, 300); |
| 796 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 797 | GrResourceCache* cache = mock.cache(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 798 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 799 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 800 | a->setSize(100); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 801 | a->resourcePriv().setContentKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 802 | a->unref(); |
| 803 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 804 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 805 | b->setSize(100); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 806 | b->resourcePriv().setContentKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 807 | b->unref(); |
| 808 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 809 | REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 810 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 811 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 812 | { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 813 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->findAndRefContentResource(key2))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 814 | find2->setSize(201); |
| 815 | } |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 816 | REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 817 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 818 | REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes()); |
| 819 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 820 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 821 | } |
| 822 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 823 | static void test_large_resource_count(skiatest::Reporter* reporter) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 824 | // Set the cache size to double the resource count because we're going to create 2x that number |
| 825 | // resources, using two different key domains. Add a little slop to the bytes because we resize |
| 826 | // down to 1 byte after creating the resource. |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 827 | static const int kResourceCnt = 2000; |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 828 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 829 | Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
| 830 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 831 | GrResourceCache* cache = mock.cache(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 832 | |
| 833 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 834 | GrContentKey key1, key2; |
| 835 | make_content_key<1>(&key1, i); |
| 836 | make_content_key<2>(&key2, i); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 837 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 838 | TestResource* resource; |
| 839 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 840 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 841 | resource->resourcePriv().setContentKey(key1); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 842 | resource->setSize(1); |
| 843 | resource->unref(); |
| 844 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 845 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 846 | resource->resourcePriv().setContentKey(key2); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 847 | resource->setSize(1); |
| 848 | resource->unref(); |
| 849 | } |
| 850 | |
| 851 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 852 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt); |
| 853 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt); |
| 854 | REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); |
| 855 | REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 856 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 857 | GrContentKey key1, key2; |
| 858 | make_content_key<1>(&key1, i); |
| 859 | make_content_key<2>(&key2, i); |
| 860 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 861 | REPORTER_ASSERT(reporter, cache->hasContentKey(key1)); |
| 862 | REPORTER_ASSERT(reporter, cache->hasContentKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 863 | } |
| 864 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 865 | cache->purgeAllUnlocked(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 866 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 867 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0); |
| 868 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0); |
| 869 | REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0); |
| 870 | REPORTER_ASSERT(reporter, cache->getResourceCount() == 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 871 | |
| 872 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 873 | GrContentKey key1, key2; |
| 874 | make_content_key<1>(&key1, i); |
| 875 | make_content_key<2>(&key2, i); |
| 876 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 877 | REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); |
| 878 | REPORTER_ASSERT(reporter, !cache->hasContentKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 883 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 884 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 885 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 886 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 887 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 888 | continue; |
| 889 | } |
| 890 | GrContext* context = factory->get(glType); |
bsalomon | fdcf2c0 | 2014-11-05 12:30:32 -0800 | [diff] [blame] | 891 | if (NULL == context) { |
| 892 | continue; |
| 893 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 894 | GrSurfaceDesc desc; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 895 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 896 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 897 | desc.fWidth = gWidth; |
| 898 | desc.fHeight = gHeight; |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 899 | SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 900 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, |
| 901 | SkSurface::kNo_Budgeted, info)); |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 902 | test_cache(reporter, context, surface->getCanvas()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 903 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 904 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 905 | // The below tests create their own mock contexts. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 906 | test_no_key(reporter); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 907 | test_budgeting(reporter); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 908 | test_unbudgeted(reporter); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 909 | test_unbudgeted_to_scratch(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 910 | test_duplicate_content_key(reporter); |
| 911 | test_duplicate_scratch_key(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 912 | test_remove_scratch_key(reporter); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 913 | test_scratch_key_consistency(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 914 | test_purge_invalidated(reporter); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 915 | test_cache_chained_purge(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 916 | test_resource_size_changed(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 917 | test_large_resource_count(reporter); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 918 | } |
| 919 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 920 | #endif |