Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 8 #include "SkBenchmark.h" | |
| 9 | |
| 10 #if SK_SUPPORT_GPU | |
| 11 | |
| 12 #include "GrContext.h" | |
| 13 #include "GrResource.h" | |
| 14 #include "GrResourceCache.h" | |
| 15 #include "GrStencilBuffer.h" | |
| 16 #include "GrTexture.h" | |
| 17 | |
| 18 enum { | |
| 19 CACHE_SIZE_COUNT = 2048, | |
| 20 CACHE_SIZE_BYTES = 2 * 1024 * 1024, | |
| 21 }; | |
| 22 | |
| 23 class StencilResource : public GrResource { | |
| 24 public: | |
| 25 SK_DECLARE_INST_COUNT(StencilResource); | |
| 26 StencilResource(GrGpu* gpu, int id) | |
| 27 : INHERITED(gpu, false), | |
| 28 fID(id) { | |
| 29 } | |
| 30 ~StencilResource() { | |
| 31 this->release(); | |
| 32 } | |
| 33 | |
| 34 virtual size_t sizeInBytes() const SK_OVERRIDE { | |
| 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: | |
| 45 typedef GrResource INHERITED; | |
| 46 }; | |
| 47 | |
| 48 class TextureResource : public GrResource { | |
| 49 public: | |
| 50 SK_DECLARE_INST_COUNT(StencilResource); | |
|
mtklein
2013/11/19 12:59:01
I'm not too clear on what the instance counting is
Kimmo Kinnunen
2013/11/19 14:12:49
Done.
| |
| 51 TextureResource(GrGpu* gpu, int id) | |
| 52 : INHERITED(gpu, false), | |
| 53 fID(id) { | |
| 54 } | |
| 55 ~TextureResource() { | |
| 56 this->release(); | |
| 57 } | |
| 58 | |
| 59 virtual size_t sizeInBytes() const SK_OVERRIDE { | |
| 60 return 100 + ((fID % 1 == 0) ? -40 : 33); | |
| 61 } | |
| 62 | |
| 63 static GrResourceKey ComputeKey(const GrTextureDesc& desc) { | |
| 64 return GrTexture::ComputeScratchKey(desc); | |
| 65 } | |
| 66 | |
| 67 int fID; | |
| 68 | |
| 69 private: | |
| 70 typedef GrResource INHERITED; | |
| 71 }; | |
| 72 | |
| 73 SK_DEFINE_INST_COUNT(StencilResource) | |
| 74 SK_DEFINE_INST_COUNT(TextureResource) | |
| 75 | |
| 76 | |
| 77 static void get_stencil(int i, int* w, int* h, int* s) { | |
| 78 *w = i % 1024; | |
| 79 *h = i * 2 % 1024; | |
| 80 *s = i % 1 == 0 ? 0 : 4; | |
| 81 } | |
| 82 | |
| 83 static void get_texture_desc(int i, GrTextureDesc* desc) { | |
| 84 desc->fFlags = kRenderTarget_GrTextureFlagBit | | |
| 85 kNoStencil_GrTextureFlagBit; | |
| 86 desc->fWidth = i % 1024; | |
| 87 desc->fHeight = i * 2 % 1024; | |
| 88 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); | |
| 89 desc->fSampleCnt = i % 1 == 0 ? 0 : 4; | |
| 90 } | |
| 91 | |
| 92 static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount ) { | |
| 93 for (int i = 0; i < resourceCount; ++i) { | |
| 94 int w, h, s; | |
| 95 get_stencil(i, &w, &h, &s); | |
| 96 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); | |
| 97 GrResource* resource = SkNEW_ARGS(StencilResource, (gpu, i)); | |
| 98 cache->purgeAsNeeded(1, resource->sizeInBytes()); | |
| 99 cache->addResource(key, resource); | |
| 100 resource->unref(); | |
| 101 } | |
| 102 | |
| 103 for (int i = 0; i < resourceCount; ++i) { | |
| 104 GrTextureDesc desc; | |
| 105 get_texture_desc(i, &desc); | |
| 106 GrResourceKey key = TextureResource::ComputeKey(desc); | |
| 107 GrResource* resource = SkNEW_ARGS(TextureResource, (gpu, i)); | |
| 108 cache->purgeAsNeeded(1, resource->sizeInBytes()); | |
| 109 cache->addResource(key, resource); | |
| 110 resource->unref(); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 static bool check_cache_contents(GrResourceCache* cache, int k) { | |
| 115 // Benchmark find calls that succeed. | |
| 116 { | |
| 117 GrTextureDesc desc; | |
| 118 get_texture_desc(k, &desc); | |
| 119 GrResourceKey key = TextureResource::ComputeKey(desc); | |
| 120 GrResource* item = cache->find(key); | |
| 121 if (NULL == item) { | |
| 122 SkASSERT(false); | |
| 123 return false; | |
| 124 } | |
| 125 if (static_cast<TextureResource*>(item)->fID != k) { | |
| 126 SkASSERT(false); | |
| 127 return false; | |
| 128 } | |
| 129 } | |
| 130 { | |
| 131 int w, h, s; | |
| 132 get_stencil(k, &w, &h, &s); | |
| 133 GrResourceKey key = StencilResource::ComputeKey(w, h, s); | |
| 134 GrResource* item = cache->find(key); | |
| 135 if (NULL == item) { | |
| 136 SkASSERT(false); | |
| 137 return false; | |
| 138 } | |
| 139 if (static_cast<TextureResource*>(item)->fID != k) { | |
| 140 SkASSERT(false); | |
| 141 return false; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 // Benchmark also find calls that always fail. | |
| 146 { | |
| 147 GrTextureDesc desc; | |
| 148 get_texture_desc(k, &desc); | |
| 149 desc.fHeight |= 1; | |
| 150 GrResourceKey key = TextureResource::ComputeKey(desc); | |
| 151 GrResource* item = cache->find(key); | |
| 152 if (NULL != item) { | |
| 153 SkASSERT(false); | |
| 154 return false; | |
| 155 } | |
| 156 } | |
| 157 { | |
| 158 int w, h, s; | |
| 159 get_stencil(k, &w, &h, &s); | |
| 160 h |= 1; | |
| 161 GrResourceKey key = StencilResource::ComputeKey(w, h, s); | |
| 162 GrResource* item = cache->find(key); | |
| 163 if (NULL != item) { | |
| 164 SkASSERT(false); | |
| 165 return false; | |
| 166 } | |
| 167 } | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 171 | |
| 172 class GrResourceCacheBenchAdd : public SkBenchmark { | |
| 173 enum { | |
| 174 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, | |
| 175 DUPLICATE_COUNT = CACHE_SIZE_COUNT / 4, | |
| 176 }; | |
| 177 | |
| 178 public: | |
| 179 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | |
| 180 return backend == kGPU_Backend; | |
| 181 } | |
| 182 | |
| 183 | |
| 184 protected: | |
| 185 virtual const char* onGetName() SK_OVERRIDE { | |
| 186 return "grresourcecache_add"; | |
| 187 } | |
| 188 | |
| 189 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
| 190 GrGpu* gpu = fContext->getGpu(); | |
| 191 | |
| 192 for (int i = 0; i < this->getLoops(); ++i) { | |
| 193 GrResourceCache* cache = new GrResourceCache(CACHE_SIZE_COUNT, CACHE _SIZE_BYTES); | |
|
mtklein
2013/11/19 12:59:01
Any reason this can't go on the stack?
Kimmo Kinnunen
2013/11/19 14:12:49
Done. (It was like that to match the actual usage
| |
| 194 populate_cache(cache, gpu, DUPLICATE_COUNT); | |
| 195 populate_cache(cache, gpu, RESOURCE_COUNT); | |
| 196 | |
| 197 // Check that cache works. | |
| 198 for (int k = 0; k < RESOURCE_COUNT; k += 33) { | |
| 199 if (!check_cache_contents(cache, k)) { | |
|
mtklein
2013/11/19 12:59:01
I think we can just rely on one layer or checks?
Kimmo Kinnunen
2013/11/19 14:12:49
Done.
| |
| 200 GrCrash("cache add does not work as expected"); | |
| 201 } | |
| 202 } | |
| 203 cache->purgeAllUnlocked(); | |
| 204 delete cache; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 private: | |
| 209 | |
| 210 typedef SkBenchmark INHERITED; | |
| 211 }; | |
| 212 | |
| 213 | |
| 214 class GrResourceCacheBenchFind : public SkBenchmark { | |
| 215 enum { | |
| 216 RESOURCE_COUNT = (CACHE_SIZE_COUNT / 2) - 100, | |
| 217 DUPLICATE_COUNT = 100 | |
| 218 }; | |
| 219 | |
| 220 public: | |
| 221 GrResourceCacheBenchFind() | |
| 222 : fCache(NULL) { | |
| 223 } | |
| 224 | |
| 225 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | |
| 226 return backend == kGPU_Backend; | |
| 227 } | |
| 228 | |
| 229 protected: | |
| 230 virtual const char* onGetName() SK_OVERRIDE { | |
| 231 return "grresourcecache_find"; | |
| 232 } | |
| 233 | |
| 234 virtual void setContext(GrContext* context) SK_OVERRIDE { | |
| 235 INHERITED::setContext(context); | |
| 236 | |
| 237 if (fCache) { | |
|
mtklein
2013/11/19 12:59:01
For something like this, would prefer it if you ma
Kimmo Kinnunen
2013/11/19 14:12:49
Done.
| |
| 238 delete fCache; | |
| 239 } | |
| 240 GrGpu* gpu = fContext->getGpu(); | |
| 241 fCache = new GrResourceCache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES); | |
| 242 populate_cache(fCache, gpu, DUPLICATE_COUNT); | |
| 243 populate_cache(fCache, gpu, RESOURCE_COUNT); | |
| 244 } | |
| 245 | |
| 246 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { | |
| 247 for (int i = 0; i < this->getLoops(); ++i) { | |
| 248 for (int k = 0; k < RESOURCE_COUNT; ++k) { | |
| 249 if (!check_cache_contents(fCache, k)) { | |
| 250 GrCrash("cache find does not work as expected"); | |
| 251 } | |
| 252 } | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 virtual void postDraw() SK_OVERRIDE { | |
| 257 delete fCache; | |
| 258 fCache = NULL; | |
| 259 } | |
| 260 | |
| 261 private: | |
| 262 GrResourceCache* fCache; | |
| 263 typedef SkBenchmark INHERITED; | |
| 264 }; | |
| 265 | |
| 266 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) | |
| 267 DEF_BENCH( return new GrResourceCacheBenchFind(); ) | |
| 268 | |
| 269 #endif | |
| OLD | NEW |