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