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 | |
| 9 | #if SK_SUPPORT_GPU |
| 10 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 11 | #include "Benchmark.h" |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 12 | #include "GrGpuResource.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 14 | #include "GrResourceCache.h" |
| 15 | #include "GrStencilBuffer.h" |
| 16 | #include "GrTexture.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 17 | #include "SkCanvas.h" |
| 18 | |
| 19 | enum { |
| 20 | CACHE_SIZE_COUNT = 2048, |
| 21 | CACHE_SIZE_BYTES = 2 * 1024 * 1024, |
| 22 | }; |
| 23 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 24 | class StencilResource : public GrGpuResource { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 25 | public: |
| 26 | SK_DECLARE_INST_COUNT(StencilResource); |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 27 | StencilResource(GrGpu* gpu, int id) |
| 28 | : INHERITED(gpu, false) |
| 29 | , fID(id) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 30 | } |
| 31 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 32 | virtual ~StencilResource() { this->release(); } |
| 33 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 34 | virtual size_t gpuMemorySize() const SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 35 | return 100 + ((fID % 1 == 0) ? -5 : 6); |
| 36 | } |
| 37 | |
| 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 | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 45 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 48 | class TextureResource : public GrGpuResource { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 49 | public: |
| 50 | SK_DECLARE_INST_COUNT(TextureResource); |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 51 | TextureResource(GrGpu* gpu, int id) |
| 52 | : INHERITED(gpu, false) |
| 53 | , fID(id) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 54 | } |
| 55 | |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 56 | virtual ~TextureResource() { this->release(); } |
| 57 | |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 58 | virtual size_t gpuMemorySize() const SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 59 | return 100 + ((fID % 1 == 0) ? -40 : 33); |
| 60 | } |
| 61 | |
| 62 | static GrResourceKey ComputeKey(const GrTextureDesc& desc) { |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 63 | return GrTextureImpl::ComputeScratchKey(desc); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | int fID; |
| 67 | |
| 68 | private: |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 69 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 72 | static void get_stencil(int i, int* w, int* h, int* s) { |
| 73 | *w = i % 1024; |
| 74 | *h = i * 2 % 1024; |
| 75 | *s = i % 1 == 0 ? 0 : 4; |
| 76 | } |
| 77 | |
| 78 | static void get_texture_desc(int i, GrTextureDesc* desc) { |
| 79 | desc->fFlags = kRenderTarget_GrTextureFlagBit | |
| 80 | kNoStencil_GrTextureFlagBit; |
| 81 | desc->fWidth = i % 1024; |
| 82 | desc->fHeight = i * 2 % 1024; |
| 83 | desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); |
| 84 | desc->fSampleCnt = i % 1 == 0 ? 0 : 4; |
| 85 | } |
| 86 | |
| 87 | static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount) { |
| 88 | for (int i = 0; i < resourceCount; ++i) { |
| 89 | int w, h, s; |
| 90 | get_stencil(i, &w, &h, &s); |
| 91 | GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 92 | GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i)); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 93 | cache->purgeAsNeeded(1, resource->gpuMemorySize()); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 94 | cache->addResource(key, resource); |
| 95 | resource->unref(); |
| 96 | } |
| 97 | |
| 98 | for (int i = 0; i < resourceCount; ++i) { |
| 99 | GrTextureDesc desc; |
| 100 | get_texture_desc(i, &desc); |
| 101 | GrResourceKey key = TextureResource::ComputeKey(desc); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 102 | GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i)); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 103 | cache->purgeAsNeeded(1, resource->gpuMemorySize()); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 104 | cache->addResource(key, resource); |
| 105 | resource->unref(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void check_cache_contents_or_die(GrResourceCache* cache, int k) { |
| 110 | // Benchmark find calls that succeed. |
| 111 | { |
| 112 | GrTextureDesc desc; |
| 113 | get_texture_desc(k, &desc); |
| 114 | GrResourceKey key = TextureResource::ComputeKey(desc); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 115 | GrGpuResource* item = cache->find(key); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 116 | if (NULL == item) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 117 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | if (static_cast<TextureResource*>(item)->fID != k) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 121 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | } |
| 125 | { |
| 126 | int w, h, s; |
| 127 | get_stencil(k, &w, &h, &s); |
| 128 | GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 129 | GrGpuResource* item = cache->find(key); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 130 | if (NULL == item) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 131 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | if (static_cast<TextureResource*>(item)->fID != k) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 135 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Benchmark also find calls that always fail. |
| 141 | { |
| 142 | GrTextureDesc desc; |
| 143 | get_texture_desc(k, &desc); |
| 144 | desc.fHeight |= 1; |
| 145 | GrResourceKey key = TextureResource::ComputeKey(desc); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 146 | GrGpuResource* item = cache->find(key); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 147 | if (NULL != item) { |
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 | int w, h, s; |
| 154 | get_stencil(k, &w, &h, &s); |
| 155 | h |= 1; |
| 156 | GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 157 | GrGpuResource* item = cache->find(key); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 158 | if (NULL != item) { |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 159 | SkFAIL("cache add does not work as expected"); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 165 | class GrResourceCacheBenchAdd : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 166 | enum { |
| 167 | RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, |
| 168 | DUPLICATE_COUNT = CACHE_SIZE_COUNT / 4, |
| 169 | }; |
| 170 | |
| 171 | public: |
| 172 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 173 | return backend == kGPU_Backend; |
| 174 | } |
| 175 | |
| 176 | protected: |
| 177 | virtual const char* onGetName() SK_OVERRIDE { |
| 178 | return "grresourcecache_add"; |
| 179 | } |
| 180 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 181 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 182 | GrGpu* gpu = canvas->getGrContext()->getGpu(); |
| 183 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 184 | for (int i = 0; i < loops; ++i) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 185 | GrResourceCache cache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES); |
| 186 | populate_cache(&cache, gpu, DUPLICATE_COUNT); |
| 187 | populate_cache(&cache, gpu, RESOURCE_COUNT); |
| 188 | |
| 189 | // Check that cache works. |
| 190 | for (int k = 0; k < RESOURCE_COUNT; k += 33) { |
| 191 | check_cache_contents_or_die(&cache, k); |
| 192 | } |
| 193 | cache.purgeAllUnlocked(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 198 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 201 | class GrResourceCacheBenchFind : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 202 | enum { |
| 203 | RESOURCE_COUNT = (CACHE_SIZE_COUNT / 2) - 100, |
| 204 | DUPLICATE_COUNT = 100 |
| 205 | }; |
| 206 | |
| 207 | public: |
| 208 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 209 | return backend == kGPU_Backend; |
| 210 | } |
| 211 | |
| 212 | protected: |
| 213 | virtual const char* onGetName() SK_OVERRIDE { |
| 214 | return "grresourcecache_find"; |
| 215 | } |
| 216 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 217 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 218 | GrGpu* gpu = canvas->getGrContext()->getGpu(); |
| 219 | GrResourceCache cache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES); |
| 220 | populate_cache(&cache, gpu, DUPLICATE_COUNT); |
| 221 | populate_cache(&cache, gpu, RESOURCE_COUNT); |
| 222 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 223 | for (int i = 0; i < loops; ++i) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 224 | for (int k = 0; k < RESOURCE_COUNT; ++k) { |
| 225 | check_cache_contents_or_die(&cache, k); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 231 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
| 235 | DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
| 236 | |
| 237 | #endif |