Chromium Code Reviews| Index: bench/GrResourceCacheBench.cpp |
| diff --git a/bench/GrResourceCacheBench.cpp b/bench/GrResourceCacheBench.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ada4d5814f03c25ec8bc25dfee4e0782598784c4 |
| --- /dev/null |
| +++ b/bench/GrResourceCacheBench.cpp |
| @@ -0,0 +1,269 @@ |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkBenchmark.h" |
| + |
| +#if SK_SUPPORT_GPU |
| + |
| +#include "GrContext.h" |
| +#include "GrResource.h" |
| +#include "GrResourceCache.h" |
| +#include "GrStencilBuffer.h" |
| +#include "GrTexture.h" |
| + |
| +enum { |
| + CACHE_SIZE_COUNT = 2048, |
| + CACHE_SIZE_BYTES = 2 * 1024 * 1024, |
| +}; |
| + |
| +class StencilResource : public GrResource { |
| +public: |
| + SK_DECLARE_INST_COUNT(StencilResource); |
| + StencilResource(GrGpu* gpu, int id) |
| + : INHERITED(gpu, false), |
| + fID(id) { |
| + } |
| + ~StencilResource() { |
| + this->release(); |
| + } |
| + |
| + virtual size_t sizeInBytes() const SK_OVERRIDE { |
| + return 100 + ((fID % 1 == 0) ? -5 : 6); |
| + } |
| + |
| + static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { |
| + return GrStencilBuffer::ComputeKey(width, height, sampleCnt); |
| + } |
| + |
| + int fID; |
| + |
| +private: |
| + typedef GrResource INHERITED; |
| +}; |
| + |
| +class TextureResource : public GrResource { |
| +public: |
| + 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.
|
| + TextureResource(GrGpu* gpu, int id) |
| + : INHERITED(gpu, false), |
| + fID(id) { |
| + } |
| + ~TextureResource() { |
| + this->release(); |
| + } |
| + |
| + virtual size_t sizeInBytes() const SK_OVERRIDE { |
| + return 100 + ((fID % 1 == 0) ? -40 : 33); |
| + } |
| + |
| + static GrResourceKey ComputeKey(const GrTextureDesc& desc) { |
| + return GrTexture::ComputeScratchKey(desc); |
| + } |
| + |
| + int fID; |
| + |
| +private: |
| + typedef GrResource INHERITED; |
| +}; |
| + |
| +SK_DEFINE_INST_COUNT(StencilResource) |
| +SK_DEFINE_INST_COUNT(TextureResource) |
| + |
| + |
| +static void get_stencil(int i, int* w, int* h, int* s) { |
| + *w = i % 1024; |
| + *h = i * 2 % 1024; |
| + *s = i % 1 == 0 ? 0 : 4; |
| +} |
| + |
| +static void get_texture_desc(int i, GrTextureDesc* desc) { |
| + desc->fFlags = kRenderTarget_GrTextureFlagBit | |
| + kNoStencil_GrTextureFlagBit; |
| + desc->fWidth = i % 1024; |
| + desc->fHeight = i * 2 % 1024; |
| + desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); |
| + desc->fSampleCnt = i % 1 == 0 ? 0 : 4; |
| +} |
| + |
| +static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount) { |
| + for (int i = 0; i < resourceCount; ++i) { |
| + int w, h, s; |
| + get_stencil(i, &w, &h, &s); |
| + GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); |
| + GrResource* resource = SkNEW_ARGS(StencilResource, (gpu, i)); |
| + cache->purgeAsNeeded(1, resource->sizeInBytes()); |
| + cache->addResource(key, resource); |
| + resource->unref(); |
| + } |
| + |
| + for (int i = 0; i < resourceCount; ++i) { |
| + GrTextureDesc desc; |
| + get_texture_desc(i, &desc); |
| + GrResourceKey key = TextureResource::ComputeKey(desc); |
| + GrResource* resource = SkNEW_ARGS(TextureResource, (gpu, i)); |
| + cache->purgeAsNeeded(1, resource->sizeInBytes()); |
| + cache->addResource(key, resource); |
| + resource->unref(); |
| + } |
| +} |
| + |
| +static bool check_cache_contents(GrResourceCache* cache, int k) { |
| + // Benchmark find calls that succeed. |
| + { |
| + GrTextureDesc desc; |
| + get_texture_desc(k, &desc); |
| + GrResourceKey key = TextureResource::ComputeKey(desc); |
| + GrResource* item = cache->find(key); |
| + if (NULL == item) { |
| + SkASSERT(false); |
| + return false; |
| + } |
| + if (static_cast<TextureResource*>(item)->fID != k) { |
| + SkASSERT(false); |
| + return false; |
| + } |
| + } |
| + { |
| + int w, h, s; |
| + get_stencil(k, &w, &h, &s); |
| + GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
| + GrResource* item = cache->find(key); |
| + if (NULL == item) { |
| + SkASSERT(false); |
| + return false; |
| + } |
| + if (static_cast<TextureResource*>(item)->fID != k) { |
| + SkASSERT(false); |
| + return false; |
| + } |
| + } |
| + |
| + // Benchmark also find calls that always fail. |
| + { |
| + GrTextureDesc desc; |
| + get_texture_desc(k, &desc); |
| + desc.fHeight |= 1; |
| + GrResourceKey key = TextureResource::ComputeKey(desc); |
| + GrResource* item = cache->find(key); |
| + if (NULL != item) { |
| + SkASSERT(false); |
| + return false; |
| + } |
| + } |
| + { |
| + int w, h, s; |
| + get_stencil(k, &w, &h, &s); |
| + h |= 1; |
| + GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
| + GrResource* item = cache->find(key); |
| + if (NULL != item) { |
| + SkASSERT(false); |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| + |
| +class GrResourceCacheBenchAdd : public SkBenchmark { |
| + enum { |
| + RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, |
| + DUPLICATE_COUNT = CACHE_SIZE_COUNT / 4, |
| + }; |
| + |
| +public: |
| + virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| + return backend == kGPU_Backend; |
| + } |
| + |
| + |
| +protected: |
| + virtual const char* onGetName() SK_OVERRIDE { |
| + return "grresourcecache_add"; |
| + } |
| + |
| + virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| + GrGpu* gpu = fContext->getGpu(); |
| + |
| + for (int i = 0; i < this->getLoops(); ++i) { |
| + 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
|
| + populate_cache(cache, gpu, DUPLICATE_COUNT); |
| + populate_cache(cache, gpu, RESOURCE_COUNT); |
| + |
| + // Check that cache works. |
| + for (int k = 0; k < RESOURCE_COUNT; k += 33) { |
| + 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.
|
| + GrCrash("cache add does not work as expected"); |
| + } |
| + } |
| + cache->purgeAllUnlocked(); |
| + delete cache; |
| + } |
| + } |
| + |
| +private: |
| + |
| + typedef SkBenchmark INHERITED; |
| +}; |
| + |
| + |
| +class GrResourceCacheBenchFind : public SkBenchmark { |
| + enum { |
| + RESOURCE_COUNT = (CACHE_SIZE_COUNT / 2) - 100, |
| + DUPLICATE_COUNT = 100 |
| + }; |
| + |
| +public: |
| + GrResourceCacheBenchFind() |
| + : fCache(NULL) { |
| + } |
| + |
| + virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| + return backend == kGPU_Backend; |
| + } |
| + |
| +protected: |
| + virtual const char* onGetName() SK_OVERRIDE { |
| + return "grresourcecache_find"; |
| + } |
| + |
| + virtual void setContext(GrContext* context) SK_OVERRIDE { |
| + INHERITED::setContext(context); |
| + |
| + 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.
|
| + delete fCache; |
| + } |
| + GrGpu* gpu = fContext->getGpu(); |
| + fCache = new GrResourceCache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES); |
| + populate_cache(fCache, gpu, DUPLICATE_COUNT); |
| + populate_cache(fCache, gpu, RESOURCE_COUNT); |
| + } |
| + |
| + virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| + for (int i = 0; i < this->getLoops(); ++i) { |
| + for (int k = 0; k < RESOURCE_COUNT; ++k) { |
| + if (!check_cache_contents(fCache, k)) { |
| + GrCrash("cache find does not work as expected"); |
| + } |
| + } |
| + } |
| + } |
| + |
| + virtual void postDraw() SK_OVERRIDE { |
| + delete fCache; |
| + fCache = NULL; |
| + } |
| + |
| +private: |
| + GrResourceCache* fCache; |
| + typedef SkBenchmark INHERITED; |
| +}; |
| + |
| +DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
| +DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
| + |
| +#endif |