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 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 8 | // Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined. |
| 9 | #include "SkTypes.h" |
| 10 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 12 | |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 14 | #include "GrContextFactory.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 15 | #include "GrGpu.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 16 | #include "GrGpuResourceCacheAccess.h" |
| 17 | #include "GrGpuResourcePriv.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 18 | #include "GrRenderTarget.h" |
| 19 | #include "GrRenderTargetPriv.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 20 | #include "GrResourceCache.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 21 | #include "SkCanvas.h" |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 22 | #include "SkGr.h" |
| 23 | #include "SkMessageBus.h" |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 24 | #include "SkSurface.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 25 | #include "Test.h" |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 26 | |
| 27 | static const int gWidth = 640; |
| 28 | static const int gHeight = 480; |
| 29 | |
| 30 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 31 | 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] | 32 | const SkIRect size = SkIRect::MakeWH(gWidth, gHeight); |
| 33 | |
| 34 | SkBitmap src; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 35 | src.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 36 | src.eraseColor(SK_ColorBLACK); |
| 37 | size_t srcSize = src.getSize(); |
| 38 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 39 | size_t initialCacheSize; |
| 40 | context->getResourceCacheUsage(NULL, &initialCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 41 | |
| 42 | int oldMaxNum; |
| 43 | size_t oldMaxBytes; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 44 | context->getResourceCacheLimits(&oldMaxNum, &oldMaxBytes); |
skia.committer@gmail.com | 17f1ae6 | 2013-08-09 07:01:22 +0000 | [diff] [blame] | 45 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 46 | // Set the cache limits so we can fit 10 "src" images and the |
| 47 | // max number of textures doesn't matter |
| 48 | size_t maxCacheSize = initialCacheSize + 10*srcSize; |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 49 | context->setResourceCacheLimits(1000, maxCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 50 | |
| 51 | SkBitmap readback; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame] | 52 | readback.allocN32Pixels(size.width(), size.height()); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 53 | |
| 54 | for (int i = 0; i < 100; ++i) { |
| 55 | canvas->drawBitmap(src, 0, 0); |
| 56 | canvas->readPixels(size, &readback); |
| 57 | |
| 58 | // "modify" the src texture |
| 59 | src.notifyPixelsChanged(); |
| 60 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 61 | size_t curCacheSize; |
| 62 | context->getResourceCacheUsage(NULL, &curCacheSize); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 63 | |
| 64 | // we should never go over the size limit |
| 65 | REPORTER_ASSERT(reporter, curCacheSize <= maxCacheSize); |
| 66 | } |
| 67 | |
commit-bot@chromium.org | 95c2003 | 2014-05-09 14:29:32 +0000 | [diff] [blame] | 68 | context->setResourceCacheLimits(oldMaxNum, oldMaxBytes); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 69 | } |
| 70 | |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 71 | static void test_stencil_buffers(skiatest::Reporter* reporter, GrContext* context) { |
| 72 | GrSurfaceDesc smallDesc; |
| 73 | smallDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 74 | smallDesc.fConfig = kSkia8888_GrPixelConfig; |
| 75 | smallDesc.fWidth = 4; |
| 76 | smallDesc.fHeight = 4; |
| 77 | smallDesc.fSampleCnt = 0; |
| 78 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 79 | GrTextureProvider* cache = context->textureProvider(); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 80 | // Test that two budgeted RTs with the same desc share a stencil buffer. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 81 | SkAutoTUnref<GrTexture> smallRT0(cache->createTexture(smallDesc, true)); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 82 | if (smallRT0 && smallRT0->asRenderTarget()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 83 | smallRT0->asRenderTarget()->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 84 | } |
| 85 | |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 86 | SkAutoTUnref<GrTexture> smallRT1(cache->createTexture(smallDesc, true)); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 87 | if (smallRT1 && smallRT1->asRenderTarget()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 88 | smallRT1->asRenderTarget()->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 89 | } |
| 90 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 91 | REPORTER_ASSERT(reporter, |
| 92 | smallRT0 && smallRT1 && |
| 93 | smallRT0->asRenderTarget() && smallRT1->asRenderTarget() && |
| 94 | smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() == |
| 95 | smallRT1->asRenderTarget()->renderTargetPriv().getStencilAttachment()); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 96 | |
| 97 | // An unbudgeted RT with the same desc should also share. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 98 | SkAutoTUnref<GrTexture> smallRT2(cache->createTexture(smallDesc, false)); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 99 | if (smallRT2 && smallRT2->asRenderTarget()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 100 | smallRT2->asRenderTarget()->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 101 | } |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, |
| 103 | smallRT0 && smallRT2 && |
| 104 | smallRT0->asRenderTarget() && smallRT2->asRenderTarget() && |
| 105 | smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() == |
| 106 | smallRT2->asRenderTarget()->renderTargetPriv().getStencilAttachment()); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 107 | |
| 108 | // An RT with a much larger size should not share. |
| 109 | GrSurfaceDesc bigDesc; |
| 110 | bigDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 111 | bigDesc.fConfig = kSkia8888_GrPixelConfig; |
| 112 | bigDesc.fWidth = 400; |
| 113 | bigDesc.fHeight = 200; |
| 114 | bigDesc.fSampleCnt = 0; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 115 | SkAutoTUnref<GrTexture> bigRT(cache->createTexture(bigDesc, false)); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 116 | if (bigRT && bigRT->asRenderTarget()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 117 | bigRT->asRenderTarget()->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 118 | } |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 119 | REPORTER_ASSERT(reporter, |
| 120 | smallRT0 && bigRT && |
| 121 | smallRT0->asRenderTarget() && bigRT->asRenderTarget() && |
| 122 | smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() != |
| 123 | bigRT->asRenderTarget()->renderTargetPriv().getStencilAttachment()); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 124 | |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 125 | if (context->caps()->maxSampleCount() >= 4) { |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 126 | // An RT with a different sample count should not share. |
| 127 | GrSurfaceDesc smallMSAADesc = smallDesc; |
| 128 | smallMSAADesc.fSampleCnt = 4; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 129 | SkAutoTUnref<GrTexture> smallMSAART0(cache->createTexture(smallMSAADesc, false)); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 130 | if (smallMSAART0 && smallMSAART0->asRenderTarget()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 131 | smallMSAART0->asRenderTarget()->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 132 | } |
bsalomon | b602d4d | 2015-02-19 12:05:58 -0800 | [diff] [blame] | 133 | #ifdef SK_BUILD_FOR_ANDROID |
| 134 | if (!smallMSAART0) { |
| 135 | // The nexus player seems to fail to create MSAA textures. |
| 136 | return; |
| 137 | } |
| 138 | #endif |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, |
| 140 | smallRT0 && smallMSAART0 && |
| 141 | smallRT0->asRenderTarget() && smallMSAART0->asRenderTarget() && |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 142 | smallRT0->asRenderTarget()->renderTargetPriv().getStencilAttachment() != |
| 143 | smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment()); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 144 | // A second MSAA RT should share with the first MSAA RT. |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 145 | SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false)); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 146 | if (smallMSAART1 && smallMSAART1->asRenderTarget()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 147 | smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 148 | } |
| 149 | REPORTER_ASSERT(reporter, |
| 150 | smallMSAART0 && smallMSAART1 && |
| 151 | smallMSAART0->asRenderTarget() && |
| 152 | smallMSAART1->asRenderTarget() && |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 153 | smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() == |
| 154 | smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment()); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 155 | // But not one with a larger sample count should not. (Also check that the request for 4 |
| 156 | // samples didn't get rounded up to >= 8 or else they could share.). |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 157 | if (context->caps()->maxSampleCount() >= 8 && |
| 158 | smallMSAART0 && smallMSAART0->asRenderTarget() && |
vbuzinov | dded696 | 2015-06-12 08:59:45 -0700 | [diff] [blame^] | 159 | smallMSAART0->asRenderTarget()->numColorSamples() < 8) { |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 160 | smallMSAADesc.fSampleCnt = 8; |
bsalomon | d309e7a | 2015-04-30 14:18:54 -0700 | [diff] [blame] | 161 | smallMSAART1.reset(cache->createTexture(smallMSAADesc, false)); |
| 162 | SkAutoTUnref<GrTexture> smallMSAART1(cache->createTexture(smallMSAADesc, false)); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 163 | if (smallMSAART1 && smallMSAART1->asRenderTarget()) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 164 | smallMSAART1->asRenderTarget()->renderTargetPriv().attachStencilAttachment(); |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 165 | } |
| 166 | REPORTER_ASSERT(reporter, |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 167 | smallMSAART0 && smallMSAART1 && |
| 168 | smallMSAART0->asRenderTarget() && |
| 169 | smallMSAART1->asRenderTarget() && |
| 170 | smallMSAART0->asRenderTarget()->renderTargetPriv().getStencilAttachment() != |
| 171 | smallMSAART1->asRenderTarget()->renderTargetPriv().getStencilAttachment()); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 176 | class TestResource : public GrGpuResource { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 177 | static const size_t kDefaultSize = 100; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 178 | enum ScratchConstructor { kScratchConstructor }; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 179 | public: |
| 180 | SK_DECLARE_INST_COUNT(TestResource); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 181 | /** Property that distinctly categorizes the resource. |
| 182 | * For example, textures have width, height, ... */ |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 183 | enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 184 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 185 | TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) |
| 186 | : INHERITED(gpu, lifeCycle) |
| 187 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 188 | , fSize(size) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 189 | , fProperty(kA_SimulatedProperty) { |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 190 | ++fNumAlive; |
| 191 | this->registerWithCache(); |
| 192 | } |
| 193 | |
| 194 | TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) |
| 195 | : INHERITED(gpu, lifeCycle) |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 196 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 197 | , fSize(kDefaultSize) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 198 | , fProperty(kA_SimulatedProperty) { |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 199 | ++fNumAlive; |
| 200 | this->registerWithCache(); |
| 201 | } |
| 202 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 203 | TestResource(GrGpu* gpu) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 204 | : INHERITED(gpu, kCached_LifeCycle) |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 205 | , fToDelete(NULL) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 206 | , fSize(kDefaultSize) |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 207 | , fProperty(kA_SimulatedProperty) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 208 | ++fNumAlive; |
| 209 | this->registerWithCache(); |
| 210 | } |
| 211 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 212 | static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, bool cached = true) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 213 | return SkNEW_ARGS(TestResource, (gpu, property, cached, kScratchConstructor)); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | ~TestResource() { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 217 | --fNumAlive; |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 218 | SkSafeUnref(fToDelete); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 219 | } |
| 220 | |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 221 | void setSize(size_t size) { |
| 222 | fSize = size; |
| 223 | this->didChangeGpuMemorySize(); |
| 224 | } |
| 225 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 226 | static int NumAlive() { return fNumAlive; } |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 227 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 228 | void setUnrefWhenDestroyed(TestResource* resource) { |
| 229 | SkRefCnt_SafeAssign(fToDelete, resource); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 232 | static void ComputeScratchKey(SimulatedProperty property, GrScratchKey* key) { |
| 233 | static GrScratchKey::ResourceType t = GrScratchKey::GenerateResourceType(); |
| 234 | GrScratchKey::Builder builder(key, t, kScratchKeyFieldCnt); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 235 | for (int i = 0; i < kScratchKeyFieldCnt; ++i) { |
| 236 | builder[i] = static_cast<uint32_t>(i + property); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | static size_t ExpectedScratchKeySize() { |
| 241 | return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaDataCnt); |
| 242 | } |
| 243 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 244 | private: |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 245 | static const int kScratchKeyFieldCnt = 6; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 246 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 247 | TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchConstructor) |
| 248 | : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle) |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 249 | , fToDelete(NULL) |
| 250 | , fSize(kDefaultSize) |
| 251 | , fProperty(property) { |
| 252 | GrScratchKey scratchKey; |
| 253 | ComputeScratchKey(fProperty, &scratchKey); |
| 254 | this->setScratchKey(scratchKey); |
| 255 | ++fNumAlive; |
| 256 | this->registerWithCache(); |
| 257 | } |
| 258 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 259 | size_t onGpuMemorySize() const override { return fSize; } |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 260 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 261 | TestResource* fToDelete; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 262 | size_t fSize; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 263 | static int fNumAlive; |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 264 | SimulatedProperty fProperty; |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 265 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 266 | }; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 267 | int TestResource::fNumAlive = 0; |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 268 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 269 | class Mock { |
| 270 | public: |
| 271 | Mock(int maxCnt, size_t maxBytes) { |
| 272 | fContext.reset(GrContext::CreateMockContext()); |
| 273 | SkASSERT(fContext); |
| 274 | fContext->setResourceCacheLimits(maxCnt, maxBytes); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 275 | GrResourceCache* cache = fContext->getResourceCache(); |
| 276 | cache->purgeAllUnlocked(); |
| 277 | SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 278 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 279 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 280 | GrResourceCache* cache() { return fContext->getResourceCache(); } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 281 | |
| 282 | GrContext* context() { return fContext; } |
| 283 | |
| 284 | private: |
| 285 | SkAutoTUnref<GrContext> fContext; |
| 286 | }; |
| 287 | |
| 288 | static void test_no_key(skiatest::Reporter* reporter) { |
| 289 | Mock mock(10, 30000); |
| 290 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 291 | GrResourceCache* cache = mock.cache(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 292 | |
| 293 | // Create a bunch of resources with no keys |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 294 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 295 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 296 | TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 297 | TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 298 | a->setSize(11); |
| 299 | b->setSize(12); |
| 300 | c->setSize(13); |
| 301 | d->setSize(14); |
| 302 | |
| 303 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 304 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 305 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 306 | d->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 307 | |
| 308 | // 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] | 309 | cache->purgeAllUnlocked(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 310 | REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
| 311 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 312 | // Since the resources have neither unique nor scratch keys, delete immediately upon unref. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 313 | |
| 314 | a->unref(); |
| 315 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 316 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 317 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 318 | cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 319 | |
| 320 | c->unref(); |
| 321 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 322 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 323 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 324 | cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 325 | |
| 326 | d->unref(); |
| 327 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 328 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 329 | REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 330 | |
| 331 | b->unref(); |
| 332 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 333 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 334 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 335 | } |
| 336 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 337 | // Each integer passed as a template param creates a new domain. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 338 | template <int> static void make_unique_key(GrUniqueKey* key, int data) { |
| 339 | static GrUniqueKey::Domain d = GrUniqueKey::GenerateDomain(); |
| 340 | GrUniqueKey::Builder builder(key, d, 1); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 341 | builder[0] = data; |
| 342 | } |
| 343 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 344 | static void test_budgeting(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 345 | Mock mock(10, 300); |
| 346 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 347 | GrResourceCache* cache = mock.cache(); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 348 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 349 | GrUniqueKey uniqueKey; |
| 350 | make_unique_key<0>(&uniqueKey, 0); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 351 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 352 | // Create a scratch, a unique, and a wrapped resource |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 353 | TestResource* scratch = |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 354 | TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 355 | scratch->setSize(10); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 356 | TestResource* unique = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 357 | unique->setSize(11); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 358 | unique->resourcePriv().setUniqueKey(uniqueKey); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 359 | TestResource* wrapped = SkNEW_ARGS(TestResource, |
| 360 | (context->getGpu(), GrGpuResource::kWrapped_LifeCycle)); |
| 361 | wrapped->setSize(12); |
| 362 | TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
| 363 | (context->getGpu(), GrGpuResource::kUncached_LifeCycle)); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 364 | unbudgeted->setSize(13); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 365 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 366 | // Make sure we can't add a unique key to the wrapped resource |
| 367 | GrUniqueKey uniqueKey2; |
| 368 | make_unique_key<0>(&uniqueKey2, 1); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 369 | wrapped->resourcePriv().setUniqueKey(uniqueKey2); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 370 | REPORTER_ASSERT(reporter, NULL == cache->findAndRefUniqueResource(uniqueKey2)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 371 | |
| 372 | // Make sure sizes are as we expect |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 373 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 374 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 375 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 376 | cache->getResourceBytes()); |
| 377 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 378 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 379 | cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 380 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 381 | // Our refs mean that the resources are non purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 382 | cache->purgeAllUnlocked(); |
| 383 | REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 384 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 385 | wrapped->gpuMemorySize() + unbudgeted->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 386 | cache->getResourceBytes()); |
| 387 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 388 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 389 | cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 390 | |
| 391 | // Unreffing the wrapped resource should free it right away. |
| 392 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 393 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 394 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + unique->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 395 | unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 396 | |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 397 | // Now try freeing the budgeted resources first |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 398 | wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapped_LifeCycle)); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 399 | scratch->setSize(12); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 400 | unique->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 401 | cache->purgeAllUnlocked(); |
| 402 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 403 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize() + |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 404 | unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
| 405 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 406 | REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 407 | |
| 408 | scratch->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 409 | cache->purgeAllUnlocked(); |
| 410 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 411 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 412 | cache->getResourceBytes()); |
| 413 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 414 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 415 | |
| 416 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 417 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 418 | REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceBytes()); |
| 419 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 420 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 421 | |
| 422 | unbudgeted->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 423 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 424 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 425 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 426 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | dace19e | 2014-11-17 07:34:06 -0800 | [diff] [blame] | 427 | } |
| 428 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 429 | static void test_unbudgeted(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 430 | Mock mock(10, 30000); |
| 431 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 432 | GrResourceCache* cache = mock.cache(); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 433 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 434 | GrUniqueKey uniqueKey; |
| 435 | make_unique_key<0>(&uniqueKey, 0); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 436 | |
| 437 | TestResource* scratch; |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 438 | TestResource* unique; |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 439 | TestResource* wrapped; |
| 440 | TestResource* unbudgeted; |
| 441 | |
| 442 | // A large uncached or wrapped resource shouldn't evict anything. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 443 | scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_SimulatedProperty); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 444 | scratch->setSize(10); |
| 445 | scratch->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 446 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 447 | REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); |
| 448 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 449 | REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 450 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 451 | unique = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 452 | unique->setSize(11); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 453 | unique->resourcePriv().setUniqueKey(uniqueKey); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 454 | unique->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 455 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 456 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 457 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 458 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 459 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 460 | size_t large = 2 * cache->getResourceBytes(); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 461 | unbudgeted = SkNEW_ARGS(TestResource, |
| 462 | (context->getGpu(), large, GrGpuResource::kUncached_LifeCycle)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 463 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 464 | REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 465 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 466 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 467 | |
| 468 | unbudgeted->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 469 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 470 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 471 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 472 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 473 | |
| 474 | wrapped = SkNEW_ARGS(TestResource, |
| 475 | (context->getGpu(), large, GrGpuResource::kWrapped_LifeCycle)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 476 | REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
| 477 | REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
| 478 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 479 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 480 | |
| 481 | wrapped->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 482 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 483 | REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
| 484 | REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
| 485 | REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 486 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 487 | cache->purgeAllUnlocked(); |
| 488 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 489 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 490 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 491 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 492 | } |
| 493 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 494 | // This method can't be static because it needs to friended in GrGpuResource::CacheAccess. |
| 495 | void test_unbudgeted_to_scratch(skiatest::Reporter* reporter); |
| 496 | /*static*/ void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 497 | Mock mock(10, 300); |
| 498 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 499 | GrResourceCache* cache = mock.cache(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 500 | |
| 501 | TestResource* resource = |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 502 | TestResource::CreateScratch(context->getGpu(), TestResource::kA_SimulatedProperty, false); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 503 | GrScratchKey key; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 504 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 505 | |
| 506 | size_t size = resource->gpuMemorySize(); |
| 507 | for (int i = 0; i < 2; ++i) { |
| 508 | // Since this resource is unbudgeted, it should not be reachable as scratch. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 509 | REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 510 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 511 | REPORTER_ASSERT(reporter, !resource->resourcePriv().isBudgeted()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 512 | REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key)); |
| 513 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 514 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 515 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 516 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 517 | |
| 518 | // Once it is unrefed, it should become available as scratch. |
| 519 | resource->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 520 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 521 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 522 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 523 | REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes()); |
| 524 | resource = static_cast<TestResource*>(cache->findAndRefScratchResource(key)); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 525 | REPORTER_ASSERT(reporter, resource); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 526 | REPORTER_ASSERT(reporter, resource->resourcePriv().getScratchKey() == key); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 527 | REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 528 | REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 529 | |
| 530 | if (0 == i) { |
| 531 | // If made unbudgeted, it should return to original state: ref'ed and unbudgeted. Try |
| 532 | // the above tests again. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 533 | resource->resourcePriv().makeUnbudgeted(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 534 | } else { |
| 535 | // After the second time around, try removing the scratch key |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 536 | resource->resourcePriv().removeScratchKey(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 537 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 538 | REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
| 539 | REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
| 540 | REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 541 | REPORTER_ASSERT(reporter, !resource->resourcePriv().getScratchKey().isValid()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 542 | REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 543 | REPORTER_ASSERT(reporter, resource->resourcePriv().isBudgeted()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 544 | |
| 545 | // now when it is unrefed it should die since it has no key. |
| 546 | resource->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 547 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 548 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
| 549 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
| 550 | REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 551 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 552 | } |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
| 556 | Mock mock(5, 30000); |
| 557 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 558 | GrResourceCache* cache = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 559 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 560 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 561 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 562 | TestResource::kB_SimulatedProperty); |
| 563 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 564 | TestResource::kB_SimulatedProperty); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 565 | a->setSize(11); |
| 566 | b->setSize(12); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 567 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 568 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 569 | // Check for negative case consistency. (leaks upon test failure.) |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 570 | REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKey1)); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 571 | |
| 572 | GrScratchKey scratchKey; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 573 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 574 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 575 | // Scratch resources are registered with GrResourceCache just by existing. There are 2. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 576 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 577 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
| 578 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 579 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 580 | cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 581 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 582 | // Our refs mean that the resources are non purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 583 | cache->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 584 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 585 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 586 | |
| 587 | // Unref but don't purge |
| 588 | a->unref(); |
| 589 | b->unref(); |
| 590 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 591 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 592 | |
bsalomon | 63c992f | 2015-01-23 12:47:59 -0800 | [diff] [blame] | 593 | // Purge again. This time resources should be purgeable. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 594 | cache->purgeAllUnlocked(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 595 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 596 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 597 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 598 | } |
| 599 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 600 | static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 601 | Mock mock(5, 30000); |
| 602 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 603 | GrResourceCache* cache = mock.cache(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 604 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 605 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 606 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 607 | TestResource::kB_SimulatedProperty); |
| 608 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 609 | TestResource::kB_SimulatedProperty); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 610 | a->unref(); |
| 611 | b->unref(); |
| 612 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 613 | GrScratchKey scratchKey; |
| 614 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 615 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 616 | // (following leaks upon test failure). |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 617 | REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 618 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 619 | // Scratch resources are registered with GrResourceCache just by existing. There are 2. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 620 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 621 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 622 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(scratchKey));) |
| 623 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 624 | |
| 625 | // Find the first resource and remove its scratch key |
| 626 | GrGpuResource* find; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 627 | find = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 628 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 629 | // It's still alive, but not cached by scratch key anymore |
| 630 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 631 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));) |
| 632 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 633 | |
| 634 | // The cache should immediately delete it when it's unrefed since it isn't accessible. |
| 635 | find->unref(); |
| 636 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 637 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(scratchKey));) |
| 638 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 639 | |
| 640 | // Repeat for the second resource. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 641 | find = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 642 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 643 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 644 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 645 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 646 | |
| 647 | // Should be able to call this multiple times with no problem. |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 648 | find->resourcePriv().removeScratchKey(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 649 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 650 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 651 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 652 | |
| 653 | find->unref(); |
| 654 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 655 | SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(scratchKey));) |
| 656 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 657 | } |
| 658 | |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 659 | static void test_scratch_key_consistency(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 660 | Mock mock(5, 30000); |
| 661 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 662 | GrResourceCache* cache = mock.cache(); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 663 | |
| 664 | // Create two resources that have the same scratch key. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 665 | TestResource* a = TestResource::CreateScratch(context->getGpu(), |
| 666 | TestResource::kB_SimulatedProperty); |
| 667 | TestResource* b = TestResource::CreateScratch(context->getGpu(), |
| 668 | TestResource::kB_SimulatedProperty); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 669 | a->unref(); |
| 670 | b->unref(); |
| 671 | |
| 672 | GrScratchKey scratchKey; |
| 673 | // Ensure that scratch key comparison and assignment is consistent. |
| 674 | GrScratchKey scratchKey1; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 675 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey1); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 676 | GrScratchKey scratchKey2; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 677 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 678 | REPORTER_ASSERT(reporter, scratchKey1.size() == TestResource::ExpectedScratchKeySize()); |
| 679 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey2); |
| 680 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey1); |
| 681 | scratchKey = scratchKey1; |
| 682 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 683 | REPORTER_ASSERT(reporter, scratchKey1 == scratchKey); |
| 684 | REPORTER_ASSERT(reporter, scratchKey == scratchKey1); |
| 685 | REPORTER_ASSERT(reporter, scratchKey2 != scratchKey); |
| 686 | REPORTER_ASSERT(reporter, scratchKey != scratchKey2); |
| 687 | scratchKey = scratchKey2; |
| 688 | REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratchKeySize()); |
| 689 | REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); |
| 690 | REPORTER_ASSERT(reporter, scratchKey != scratchKey1); |
| 691 | REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); |
| 692 | REPORTER_ASSERT(reporter, scratchKey == scratchKey2); |
| 693 | |
| 694 | // Ensure that scratch key lookup is correct for negative case. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 695 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 696 | // (following leaks upon test failure). |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 697 | REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NULL); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 698 | |
| 699 | // 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] | 700 | TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratchKey); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 701 | GrGpuResource* find = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 702 | REPORTER_ASSERT(reporter, find != NULL); |
| 703 | find->unref(); |
| 704 | |
| 705 | scratchKey2 = scratchKey; |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 706 | find = cache->findAndRefScratchResource(scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 707 | REPORTER_ASSERT(reporter, find != NULL); |
| 708 | REPORTER_ASSERT(reporter, find == a || find == b); |
| 709 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 710 | GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 711 | REPORTER_ASSERT(reporter, find2 != NULL); |
| 712 | REPORTER_ASSERT(reporter, find2 == a || find2 == b); |
| 713 | REPORTER_ASSERT(reporter, find2 != find); |
| 714 | find2->unref(); |
| 715 | find->unref(); |
| 716 | } |
| 717 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 718 | static void test_duplicate_unique_key(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 719 | Mock mock(5, 30000); |
| 720 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 721 | GrResourceCache* cache = mock.cache(); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 722 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 723 | GrUniqueKey key; |
| 724 | make_unique_key<0>(&key, 0); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 725 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 726 | // Create two resources that we will attempt to register with the same unique key. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 727 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 728 | a->setSize(11); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 729 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 730 | // Set key on resource a. |
| 731 | a->resourcePriv().setUniqueKey(key); |
| 732 | REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
| 733 | a->unref(); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 734 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 735 | // Make sure that redundantly setting a's key works. |
| 736 | a->resourcePriv().setUniqueKey(key); |
| 737 | REPORTER_ASSERT(reporter, a == cache->findAndRefUniqueResource(key)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 738 | a->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 739 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 740 | REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 741 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 742 | |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 743 | // Create resource b and set the same key. It should replace a's unique key cache entry. |
| 744 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 745 | b->setSize(12); |
| 746 | b->resourcePriv().setUniqueKey(key); |
| 747 | REPORTER_ASSERT(reporter, b == cache->findAndRefUniqueResource(key)); |
| 748 | b->unref(); |
| 749 | |
| 750 | // Still have two resources because a is still reffed. |
| 751 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 752 | REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == cache->getResourceBytes()); |
| 753 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 754 | |
| 755 | a->unref(); |
| 756 | // Now a should be gone. |
| 757 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 758 | REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
| 759 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 760 | |
| 761 | // Now replace b with c, but make sure c can start with one unique key and change it to b's key. |
| 762 | // Also make b be unreffed when replacement occurs. |
| 763 | b->unref(); |
| 764 | TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 765 | GrUniqueKey differentKey; |
| 766 | make_unique_key<0>(&differentKey, 1); |
| 767 | c->setSize(13); |
| 768 | c->resourcePriv().setUniqueKey(differentKey); |
| 769 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
| 770 | REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() == cache->getResourceBytes()); |
| 771 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
| 772 | // c replaces b and b should be immediately purged. |
| 773 | c->resourcePriv().setUniqueKey(key); |
| 774 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 775 | REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 776 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 777 | |
| 778 | // c shouldn't be purged because it is ref'ed. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 779 | cache->purgeAllUnlocked(); |
bsalomon | f99e961 | 2015-02-19 08:24:16 -0800 | [diff] [blame] | 780 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 781 | REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 782 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 783 | |
| 784 | // Drop the ref on c, it should be kept alive because it has a unique key. |
| 785 | c->unref(); |
| 786 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
| 787 | REPORTER_ASSERT(reporter, c->gpuMemorySize() == cache->getResourceBytes()); |
| 788 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 789 | |
| 790 | // Verify that we can find c, then remove its unique key. It should get purged immediately. |
| 791 | REPORTER_ASSERT(reporter, c == cache->findAndRefUniqueResource(key)); |
| 792 | c->resourcePriv().removeUniqueKey(); |
| 793 | c->unref(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 794 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 795 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 796 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 797 | } |
| 798 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 799 | static void test_purge_invalidated(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 800 | Mock mock(5, 30000); |
| 801 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 802 | GrResourceCache* cache = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 803 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 804 | GrUniqueKey key1, key2, key3; |
| 805 | make_unique_key<0>(&key1, 1); |
| 806 | make_unique_key<0>(&key2, 2); |
| 807 | make_unique_key<0>(&key3, 3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 808 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 809 | // Add three resources to the cache. Only c is usable as scratch. |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 810 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 811 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 812 | TestResource* c = TestResource::CreateScratch(context->getGpu(), |
| 813 | TestResource::kA_SimulatedProperty); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 814 | a->resourcePriv().setUniqueKey(key1); |
| 815 | b->resourcePriv().setUniqueKey(key2); |
| 816 | c->resourcePriv().setUniqueKey(key3); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 817 | a->unref(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 818 | // hold b until *after* the message is sent. |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 819 | c->unref(); |
| 820 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 821 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); |
| 822 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2)); |
| 823 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 824 | REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 825 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 826 | typedef GrUniqueKeyInvalidatedMessage Msg; |
| 827 | typedef SkMessageBus<GrUniqueKeyInvalidatedMessage> Bus; |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 828 | |
| 829 | // Invalidate two of the three, they should be purged and no longer accessible via their keys. |
| 830 | Bus::Post(Msg(key1)); |
| 831 | Bus::Post(Msg(key2)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 832 | cache->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 833 | // a should be deleted now, but we still have a ref on b. |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 834 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
| 835 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2)); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 836 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 837 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key3)); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 838 | |
| 839 | // Invalidate the third. |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 840 | Bus::Post(Msg(key3)); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 841 | cache->purgeAsNeeded(); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 842 | // we still have a ref on b, c should be recycled as scratch. |
| 843 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 844 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key3)); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 845 | |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 846 | // make b purgeable. It should be immediately deleted since it has no key. |
| 847 | b->unref(); |
| 848 | REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
| 849 | |
| 850 | // Make sure we actually get to c via it's scratch key, before we say goodbye. |
| 851 | GrScratchKey scratchKey; |
| 852 | TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratchKey); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 853 | GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 854 | REPORTER_ASSERT(reporter, scratch == c); |
| 855 | SkSafeUnref(scratch); |
| 856 | |
| 857 | // Get rid of c. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 858 | cache->purgeAllUnlocked(); |
| 859 | scratch = cache->findAndRefScratchResource(scratchKey); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 860 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 861 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 862 | REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
bsalomon | 23e619c | 2015-02-06 11:54:28 -0800 | [diff] [blame] | 863 | REPORTER_ASSERT(reporter, !scratch); |
| 864 | SkSafeUnref(scratch); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 865 | } |
| 866 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 867 | static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 868 | Mock mock(3, 30000); |
| 869 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 870 | GrResourceCache* cache = mock.cache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 871 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 872 | GrUniqueKey key1, key2; |
| 873 | make_unique_key<0>(&key1, 1); |
| 874 | make_unique_key<0>(&key2, 2); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 875 | |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 876 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 877 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 878 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 879 | a->resourcePriv().setUniqueKey(key1); |
| 880 | b->resourcePriv().setUniqueKey(key2); |
bsalomon | 820dd6c | 2014-11-05 12:09:45 -0800 | [diff] [blame] | 881 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 882 | // Make a cycle |
| 883 | a->setUnrefWhenDestroyed(b); |
| 884 | b->setUnrefWhenDestroyed(a); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 885 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 886 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 887 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 888 | a->unref(); |
| 889 | b->unref(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 890 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 891 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 892 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 893 | cache->purgeAllUnlocked(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 894 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 895 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 896 | // Break the cycle |
| 897 | a->setUnrefWhenDestroyed(NULL); |
| 898 | REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 899 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 900 | cache->purgeAllUnlocked(); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 901 | REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
commit-bot@chromium.org | bd58feb | 2014-01-17 17:56:21 +0000 | [diff] [blame] | 902 | } |
| 903 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 904 | static void test_resource_size_changed(skiatest::Reporter* reporter) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 905 | GrUniqueKey key1, key2; |
| 906 | make_unique_key<0>(&key1, 1); |
| 907 | make_unique_key<0>(&key2, 2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 908 | |
| 909 | // Test changing resources sizes (both increase & decrease). |
| 910 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 911 | Mock mock(3, 30000); |
| 912 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 913 | GrResourceCache* cache = mock.cache(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 914 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 915 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 916 | a->resourcePriv().setUniqueKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 917 | a->unref(); |
| 918 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 919 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 920 | b->resourcePriv().setUniqueKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 921 | b->unref(); |
| 922 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 923 | REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 924 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 925 | { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 926 | SkAutoTUnref<TestResource> find2( |
| 927 | static_cast<TestResource*>(cache->findAndRefUniqueResource(key2))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 928 | find2->setSize(200); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 929 | SkAutoTUnref<TestResource> find1( |
| 930 | static_cast<TestResource*>(cache->findAndRefUniqueResource(key1))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 931 | find1->setSize(50); |
| 932 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 933 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 934 | REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); |
| 935 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // Test increasing a resources size beyond the cache budget. |
| 939 | { |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 940 | Mock mock(2, 300); |
| 941 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 942 | GrResourceCache* cache = mock.cache(); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 943 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 944 | TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 945 | a->setSize(100); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 946 | a->resourcePriv().setUniqueKey(key1); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 947 | a->unref(); |
| 948 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 949 | TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 950 | b->setSize(100); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 951 | b->resourcePriv().setUniqueKey(key2); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 952 | b->unref(); |
| 953 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 954 | REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
| 955 | REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 956 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 957 | { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 958 | SkAutoTUnref<TestResource> find2(static_cast<TestResource*>( |
| 959 | cache->findAndRefUniqueResource(key2))); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 960 | find2->setSize(201); |
| 961 | } |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 962 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 963 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 964 | REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes()); |
| 965 | REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 966 | } |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 967 | } |
| 968 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 969 | static void test_timestamp_wrap(skiatest::Reporter* reporter) { |
| 970 | static const int kCount = 50; |
| 971 | static const int kBudgetCnt = kCount / 2; |
| 972 | static const int kLockedFreq = 8; |
| 973 | static const int kBudgetSize = 0x80000000; |
| 974 | |
| 975 | SkRandom random; |
| 976 | |
| 977 | // Run the test 2*kCount times; |
| 978 | for (int i = 0; i < 2 * kCount; ++i ) { |
| 979 | Mock mock(kBudgetCnt, kBudgetSize); |
| 980 | GrContext* context = mock.context(); |
| 981 | GrResourceCache* cache = mock.cache(); |
| 982 | |
| 983 | // Pick a random number of resources to add before the timestamp will wrap. |
| 984 | cache->changeTimestamp(SK_MaxU32 - random.nextULessThan(kCount + 1)); |
| 985 | |
| 986 | static const int kNumToPurge = kCount - kBudgetCnt; |
| 987 | |
| 988 | SkTDArray<int> shouldPurgeIdxs; |
| 989 | int purgeableCnt = 0; |
| 990 | SkTDArray<GrGpuResource*> resourcesToUnref; |
| 991 | |
| 992 | // Add kCount resources, holding onto resources at random so we have a mix of purgeable and |
| 993 | // unpurgeable resources. |
| 994 | for (int j = 0; j < kCount; ++j) { |
| 995 | GrUniqueKey key; |
| 996 | make_unique_key<0>(&key, j); |
| 997 | |
| 998 | TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 999 | r->resourcePriv().setUniqueKey(key); |
| 1000 | if (random.nextU() % kLockedFreq) { |
| 1001 | // Make this is purgeable. |
| 1002 | r->unref(); |
| 1003 | ++purgeableCnt; |
| 1004 | if (purgeableCnt <= kNumToPurge) { |
| 1005 | *shouldPurgeIdxs.append() = j; |
| 1006 | } |
| 1007 | } else { |
| 1008 | *resourcesToUnref.append() = r; |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | // Verify that the correct resources were purged. |
| 1013 | int currShouldPurgeIdx = 0; |
| 1014 | for (int j = 0; j < kCount; ++j) { |
| 1015 | GrUniqueKey key; |
| 1016 | make_unique_key<0>(&key, j); |
| 1017 | GrGpuResource* res = cache->findAndRefUniqueResource(key); |
| 1018 | if (currShouldPurgeIdx < shouldPurgeIdxs.count() && |
| 1019 | shouldPurgeIdxs[currShouldPurgeIdx] == j) { |
| 1020 | ++currShouldPurgeIdx; |
| 1021 | REPORTER_ASSERT(reporter, NULL == res); |
| 1022 | } else { |
| 1023 | REPORTER_ASSERT(reporter, NULL != res); |
| 1024 | } |
| 1025 | SkSafeUnref(res); |
| 1026 | } |
| 1027 | |
| 1028 | for (int j = 0; j < resourcesToUnref.count(); ++j) { |
| 1029 | resourcesToUnref[j]->unref(); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1034 | static void test_flush(skiatest::Reporter* reporter) { |
| 1035 | Mock mock(1000000, 1000000); |
| 1036 | GrContext* context = mock.context(); |
| 1037 | GrResourceCache* cache = mock.cache(); |
| 1038 | |
| 1039 | // The current cache impl will round the max flush count to the next power of 2. So we choose a |
| 1040 | // power of two here to keep things simpler. |
| 1041 | static const int kFlushCount = 16; |
| 1042 | cache->setLimits(1000000, 1000000, kFlushCount); |
| 1043 | |
| 1044 | { |
| 1045 | // Insert a resource and send a flush notification kFlushCount times. |
| 1046 | for (int i = 0; i < kFlushCount; ++i) { |
| 1047 | TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 1048 | GrUniqueKey k; |
| 1049 | make_unique_key<1>(&k, i); |
| 1050 | r->resourcePriv().setUniqueKey(k); |
| 1051 | r->unref(); |
| 1052 | cache->notifyFlushOccurred(); |
| 1053 | } |
| 1054 | |
| 1055 | // Send flush notifications to the cache. Each flush should purge the oldest resource. |
| 1056 | for (int i = 0; i < kFlushCount - 1; ++i) { |
| 1057 | // The first resource was purged after the last flush in the initial loop, hence the -1. |
| 1058 | REPORTER_ASSERT(reporter, kFlushCount - i - 1 == cache->getResourceCount()); |
| 1059 | for (int j = 0; j < i; ++j) { |
| 1060 | GrUniqueKey k; |
| 1061 | make_unique_key<1>(&k, j); |
| 1062 | GrGpuResource* r = cache->findAndRefUniqueResource(k); |
| 1063 | REPORTER_ASSERT(reporter, !SkToBool(r)); |
| 1064 | SkSafeUnref(r); |
| 1065 | } |
| 1066 | cache->notifyFlushOccurred(); |
| 1067 | } |
| 1068 | |
| 1069 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1070 | cache->purgeAllUnlocked(); |
| 1071 | } |
| 1072 | |
| 1073 | // Do a similar test but where we leave refs on some resources to prevent them from being |
| 1074 | // purged. |
| 1075 | { |
| 1076 | GrGpuResource* refedResources[kFlushCount >> 1]; |
| 1077 | for (int i = 0; i < kFlushCount; ++i) { |
| 1078 | TestResource* r = SkNEW_ARGS(TestResource, (context->getGpu())); |
| 1079 | GrUniqueKey k; |
| 1080 | make_unique_key<1>(&k, i); |
| 1081 | r->resourcePriv().setUniqueKey(k); |
| 1082 | // Leave a ref on every other resource, beginning with the first. |
| 1083 | if (SkToBool(i & 0x1)) { |
| 1084 | refedResources[i/2] = r; |
| 1085 | } else { |
| 1086 | r->unref(); |
| 1087 | } |
| 1088 | cache->notifyFlushOccurred(); |
| 1089 | } |
| 1090 | |
| 1091 | for (int i = 0; i < kFlushCount; ++i) { |
| 1092 | // Should get a resource purged every other flush. |
| 1093 | REPORTER_ASSERT(reporter, kFlushCount - i/2 - 1 == cache->getResourceCount()); |
| 1094 | cache->notifyFlushOccurred(); |
| 1095 | } |
| 1096 | |
| 1097 | // Unref all the resources that we kept refs on in the first loop. |
| 1098 | for (int i = 0; i < kFlushCount >> 1; ++i) { |
| 1099 | refedResources[i]->unref(); |
| 1100 | } |
| 1101 | |
| 1102 | // When we unref'ed them their timestamps got updated. So nothing should be purged until we |
| 1103 | // get kFlushCount additional flushes. Then everything should be purged. |
| 1104 | for (int i = 0; i < kFlushCount; ++i) { |
| 1105 | REPORTER_ASSERT(reporter, kFlushCount >> 1 == cache->getResourceCount()); |
| 1106 | cache->notifyFlushOccurred(); |
| 1107 | } |
| 1108 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1109 | |
| 1110 | cache->purgeAllUnlocked(); |
| 1111 | } |
| 1112 | |
| 1113 | REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
| 1114 | } |
| 1115 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1116 | static void test_large_resource_count(skiatest::Reporter* reporter) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1117 | // Set the cache size to double the resource count because we're going to create 2x that number |
| 1118 | // resources, using two different key domains. Add a little slop to the bytes because we resize |
| 1119 | // down to 1 byte after creating the resource. |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1120 | static const int kResourceCnt = 2000; |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1121 | |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1122 | Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
| 1123 | GrContext* context = mock.context(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1124 | GrResourceCache* cache = mock.cache(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1125 | |
| 1126 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1127 | GrUniqueKey key1, key2; |
| 1128 | make_unique_key<1>(&key1, i); |
| 1129 | make_unique_key<2>(&key2, i); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1130 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 1131 | TestResource* resource; |
| 1132 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1133 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1134 | resource->resourcePriv().setUniqueKey(key1); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1135 | resource->setSize(1); |
| 1136 | resource->unref(); |
| 1137 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1138 | resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1139 | resource->resourcePriv().setUniqueKey(key2); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1140 | resource->setSize(1); |
| 1141 | resource->unref(); |
| 1142 | } |
| 1143 | |
| 1144 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1145 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResourceCnt); |
| 1146 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResourceCnt); |
| 1147 | REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); |
| 1148 | REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1149 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1150 | GrUniqueKey key1, key2; |
| 1151 | make_unique_key<1>(&key1, i); |
| 1152 | make_unique_key<2>(&key2, i); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 1153 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1154 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key1)); |
| 1155 | REPORTER_ASSERT(reporter, cache->hasUniqueKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1158 | cache->purgeAllUnlocked(); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1159 | REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 1160 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0); |
| 1161 | REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0); |
| 1162 | REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0); |
| 1163 | REPORTER_ASSERT(reporter, cache->getResourceCount() == 0); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1164 | |
| 1165 | for (int i = 0; i < kResourceCnt; ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1166 | GrUniqueKey key1, key2; |
| 1167 | make_unique_key<1>(&key1, i); |
| 1168 | make_unique_key<2>(&key2, i); |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 1169 | |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1170 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key1)); |
| 1171 | REPORTER_ASSERT(reporter, !cache->hasUniqueKey(key2)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1172 | } |
| 1173 | } |
| 1174 | |
commit-bot@chromium.org | c665804 | 2014-01-15 23:09:01 +0000 | [diff] [blame] | 1175 | //////////////////////////////////////////////////////////////////////////////// |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 1176 | DEF_GPUTEST(ResourceCache, reporter, factory) { |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1177 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 1178 | GrContextFactory::GLContextType glType = static_cast<GrContextFactory::GLContextType>(type); |
| 1179 | if (!GrContextFactory::IsRenderingGLContext(glType)) { |
| 1180 | continue; |
| 1181 | } |
| 1182 | GrContext* context = factory->get(glType); |
bsalomon | fdcf2c0 | 2014-11-05 12:30:32 -0800 | [diff] [blame] | 1183 | if (NULL == context) { |
| 1184 | continue; |
| 1185 | } |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 1186 | GrSurfaceDesc desc; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1187 | desc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 1188 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1189 | desc.fWidth = gWidth; |
| 1190 | desc.fHeight = gHeight; |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 1191 | SkImageInfo info = SkImageInfo::MakeN32Premul(gWidth, gHeight); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 1192 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, |
| 1193 | SkSurface::kNo_Budgeted, info)); |
reed | 69f6f00 | 2014-09-18 06:09:44 -0700 | [diff] [blame] | 1194 | test_cache(reporter, context, surface->getCanvas()); |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 1195 | test_stencil_buffers(reporter, context); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1196 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 1197 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1198 | // The below tests create their own mock contexts. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1199 | test_no_key(reporter); |
bsalomon | 84c8e62 | 2014-11-17 09:33:27 -0800 | [diff] [blame] | 1200 | test_budgeting(reporter); |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 1201 | test_unbudgeted(reporter); |
bsalomon | c2f35b7 | 2015-01-23 07:19:22 -0800 | [diff] [blame] | 1202 | test_unbudgeted_to_scratch(reporter); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 1203 | test_duplicate_unique_key(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1204 | test_duplicate_scratch_key(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1205 | test_remove_scratch_key(reporter); |
bsalomon | 1c60dfe | 2015-01-21 09:32:40 -0800 | [diff] [blame] | 1206 | test_scratch_key_consistency(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1207 | test_purge_invalidated(reporter); |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 1208 | test_cache_chained_purge(reporter); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 1209 | test_resource_size_changed(reporter); |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 1210 | test_timestamp_wrap(reporter); |
bsalomon | 3f32432 | 2015-04-08 11:01:54 -0700 | [diff] [blame] | 1211 | test_flush(reporter); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 1212 | test_large_resource_count(reporter); |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
commit-bot@chromium.org | c28f555 | 2013-08-08 22:55:21 +0000 | [diff] [blame] | 1215 | #endif |