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