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