commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 9 | #include "Benchmark.h" |
| 10 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 11 | #if SK_SUPPORT_GPU |
| 12 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 13 | #include "GrGpuResource.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 14 | #include "GrContext.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 15 | #include "GrGpu.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 16 | #include "GrResourceCache.h" |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 17 | #include "GrResourceCache2.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 18 | #include "GrStencilBuffer.h" |
| 19 | #include "GrTexture.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 20 | #include "GrTexturePriv.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 21 | #include "SkCanvas.h" |
| 22 | |
| 23 | enum { |
| 24 | CACHE_SIZE_COUNT = 2048, |
| 25 | CACHE_SIZE_BYTES = 2 * 1024 * 1024, |
| 26 | }; |
| 27 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 28 | class StencilResource : public GrGpuResource { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 29 | public: |
| 30 | SK_DECLARE_INST_COUNT(StencilResource); |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 31 | StencilResource(GrGpu* gpu, int id) |
| 32 | : INHERITED(gpu, false) |
| 33 | , fID(id) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 34 | this->registerWithCache(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 35 | } |
| 36 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 37 | virtual ~StencilResource() { this->release(); } |
| 38 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 39 | virtual size_t gpuMemorySize() const SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 40 | return 100 + ((fID % 1 == 0) ? -5 : 6); |
| 41 | } |
| 42 | |
| 43 | static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { |
| 44 | return GrStencilBuffer::ComputeKey(width, height, sampleCnt); |
| 45 | } |
| 46 | |
| 47 | int fID; |
| 48 | |
| 49 | private: |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 50 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 53 | class TextureResource : public GrGpuResource { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 54 | public: |
| 55 | SK_DECLARE_INST_COUNT(TextureResource); |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 56 | TextureResource(GrGpu* gpu, int id) |
| 57 | : INHERITED(gpu, false) |
| 58 | , fID(id) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 59 | this->registerWithCache(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 60 | } |
| 61 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 62 | virtual ~TextureResource() { this->release(); } |
| 63 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 64 | virtual size_t gpuMemorySize() const SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 65 | return 100 + ((fID % 1 == 0) ? -40 : 33); |
| 66 | } |
| 67 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 68 | static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) { |
bsalomon | 1e2530b | 2014-10-09 09:57:18 -0700 | [diff] [blame] | 69 | GrCacheID::Key key; |
| 70 | memset(&key, 0, sizeof(key)); |
| 71 | key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); |
| 72 | key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; |
| 73 | key.fData32[2] = desc.fFlags; |
| 74 | static int gType = GrResourceKey::GenerateResourceType(); |
| 75 | static int gDomain = GrCacheID::GenerateDomain(); |
| 76 | return GrResourceKey(GrCacheID(gDomain, key), gType, 0); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | int fID; |
| 80 | |
| 81 | private: |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 82 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 85 | static void get_stencil(int i, int* w, int* h, int* s) { |
| 86 | *w = i % 1024; |
| 87 | *h = i * 2 % 1024; |
| 88 | *s = i % 1 == 0 ? 0 : 4; |
| 89 | } |
| 90 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 91 | static void get_texture_desc(int i, GrSurfaceDesc* desc) { |
| 92 | desc->fFlags = kRenderTarget_GrSurfaceFlag | |
| 93 | kNoStencil_GrSurfaceFlag; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 94 | desc->fWidth = i % 1024; |
| 95 | desc->fHeight = i * 2 % 1024; |
| 96 | desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); |
| 97 | desc->fSampleCnt = i % 1 == 0 ? 0 : 4; |
| 98 | } |
| 99 | |
| 100 | static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount) { |
| 101 | for (int i = 0; i < resourceCount; ++i) { |
| 102 | int w, h, s; |
| 103 | get_stencil(i, &w, &h, &s); |
| 104 | GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 105 | GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i)); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 106 | cache->purgeAsNeeded(1, resource->gpuMemorySize()); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 107 | cache->addResource(key, resource); |
| 108 | resource->unref(); |
| 109 | } |
| 110 | |
| 111 | for (int i = 0; i < resourceCount; ++i) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 112 | GrSurfaceDesc desc; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 113 | get_texture_desc(i, &desc); |
| 114 | GrResourceKey key = TextureResource::ComputeKey(desc); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 115 | GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i)); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 116 | cache->purgeAsNeeded(1, resource->gpuMemorySize()); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 117 | cache->addResource(key, resource); |
| 118 | resource->unref(); |
| 119 | } |
| 120 | } |
| 121 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 122 | static void check_cache_contents_or_die(GrResourceCache2* cache, int k) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 123 | // Benchmark find calls that succeed. |
| 124 | { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 125 | GrSurfaceDesc desc; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 126 | get_texture_desc(k, &desc); |
| 127 | GrResourceKey key = TextureResource::ComputeKey(desc); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 128 | SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
| 129 | if (!item) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 130 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 131 | return; |
| 132 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 133 | if (static_cast<TextureResource*>(item.get())->fID != k) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 134 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | } |
| 138 | { |
| 139 | int w, h, s; |
| 140 | get_stencil(k, &w, &h, &s); |
| 141 | GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 142 | SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
| 143 | if (!item) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 144 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 145 | return; |
| 146 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 147 | if (static_cast<TextureResource*>(item.get())->fID != k) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 148 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Benchmark also find calls that always fail. |
| 154 | { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 155 | GrSurfaceDesc desc; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 156 | get_texture_desc(k, &desc); |
| 157 | desc.fHeight |= 1; |
| 158 | GrResourceKey key = TextureResource::ComputeKey(desc); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 159 | SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 160 | if (item) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 161 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | } |
| 165 | { |
| 166 | int w, h, s; |
| 167 | get_stencil(k, &w, &h, &s); |
| 168 | h |= 1; |
| 169 | GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 170 | SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 171 | if (item) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 172 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 178 | class GrResourceCacheBenchAdd : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 179 | enum { |
| 180 | RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | public: |
| 184 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 185 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | protected: |
| 189 | virtual const char* onGetName() SK_OVERRIDE { |
| 190 | return "grresourcecache_add"; |
| 191 | } |
| 192 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 193 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 194 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 195 | if (NULL == context) { |
| 196 | return; |
| 197 | } |
| 198 | // Set the cache budget to be very large so no purging occurs. |
| 199 | context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30); |
| 200 | |
| 201 | GrResourceCache* cache = context->getResourceCache(); |
| 202 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 203 | |
| 204 | // Make sure the cache is empty. |
| 205 | cache->purgeAllUnlocked(); |
| 206 | SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes()); |
| 207 | |
| 208 | GrGpu* gpu = context->getGpu(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 209 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 210 | for (int i = 0; i < loops; ++i) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 211 | SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes()); |
| 212 | |
| 213 | populate_cache(cache, gpu, RESOURCE_COUNT); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 214 | |
| 215 | // Check that cache works. |
| 216 | for (int k = 0; k < RESOURCE_COUNT; k += 33) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 217 | check_cache_contents_or_die(cache2, k); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 218 | } |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 219 | cache->purgeAllUnlocked(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 224 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 227 | class GrResourceCacheBenchFind : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 228 | enum { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 229 | RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | public: |
| 233 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 234 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | protected: |
| 238 | virtual const char* onGetName() SK_OVERRIDE { |
| 239 | return "grresourcecache_find"; |
| 240 | } |
| 241 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 242 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 243 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 244 | if (NULL == context) { |
| 245 | return; |
| 246 | } |
| 247 | // Set the cache budget to be very large so no purging occurs. |
| 248 | context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30); |
| 249 | |
| 250 | GrResourceCache* cache = context->getResourceCache(); |
| 251 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 252 | |
| 253 | // Make sure the cache is empty. |
| 254 | cache->purgeAllUnlocked(); |
| 255 | SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes()); |
| 256 | |
| 257 | GrGpu* gpu = context->getGpu(); |
| 258 | |
| 259 | populate_cache(cache, gpu, RESOURCE_COUNT); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 260 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 261 | for (int i = 0; i < loops; ++i) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 262 | for (int k = 0; k < RESOURCE_COUNT; ++k) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame^] | 263 | check_cache_contents_or_die(cache2, k); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 269 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
| 273 | DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
| 274 | |
| 275 | #endif |