GrResourceCacheBench.cpp revision 0ea80f43a1af05b8157a4ef387223bb5b0da35ed
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#include "Benchmark.h" 10 11#if SK_SUPPORT_GPU 12 13#include "GrGpuResource.h" 14#include "GrContext.h" 15#include "GrGpu.h" 16#include "GrResourceCache.h" 17#include "SkCanvas.h" 18 19enum { 20 CACHE_SIZE_COUNT = 4096, 21}; 22 23class BenchResource : public GrGpuResource { 24public: 25 SK_DECLARE_INST_COUNT(BenchResource); 26 BenchResource (GrGpu* gpu) 27 : INHERITED(gpu, kCached_LifeCycle) { 28 this->registerWithCache(); 29 } 30 31 static void ComputeKey(int i, GrContentKey* key) { 32 static GrContentKey::Domain kDomain = GrContentKey::GenerateDomain(); 33 GrContentKey::Builder builder(key, kDomain, 1); 34 builder[0] = i; 35 } 36 37private: 38 size_t onGpuMemorySize() const SK_OVERRIDE { return 100; } 39 40 typedef GrGpuResource INHERITED; 41}; 42 43static void populate_cache(GrGpu* gpu, int resourceCount) { 44 for (int i = 0; i < resourceCount; ++i) { 45 GrContentKey key; 46 BenchResource::ComputeKey(i, &key); 47 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu)); 48 resource->cacheAccess().setContentKey(key); 49 resource->unref(); 50 } 51} 52 53class GrResourceCacheBenchAdd : public Benchmark { 54public: 55 bool isSuitableFor(Backend backend) SK_OVERRIDE { 56 return backend == kNonRendering_Backend; 57 } 58 59protected: 60 const char* onGetName() SK_OVERRIDE { 61 return "grresourcecache_add"; 62 } 63 64 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 65 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); 66 if (NULL == context) { 67 return; 68 } 69 // Set the cache budget to be very large so no purging occurs. 70 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); 71 72 GrResourceCache* cache = context->getResourceCache(); 73 74 // Make sure the cache is empty. 75 cache->purgeAllUnlocked(); 76 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); 77 78 GrGpu* gpu = context->getGpu(); 79 80 for (int i = 0; i < loops; ++i) { 81 populate_cache(gpu, CACHE_SIZE_COUNT); 82 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount()); 83 } 84 } 85 86private: 87 typedef Benchmark INHERITED; 88}; 89 90class GrResourceCacheBenchFind : public Benchmark { 91public: 92 bool isSuitableFor(Backend backend) SK_OVERRIDE { 93 return backend == kNonRendering_Backend; 94 } 95 96protected: 97 const char* onGetName() SK_OVERRIDE { 98 return "grresourcecache_find"; 99 } 100 101 void onPreDraw() SK_OVERRIDE { 102 fContext.reset(GrContext::CreateMockContext()); 103 if (!fContext) { 104 return; 105 } 106 // Set the cache budget to be very large so no purging occurs. 107 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); 108 109 GrResourceCache* cache = fContext->getResourceCache(); 110 111 // Make sure the cache is empty. 112 cache->purgeAllUnlocked(); 113 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); 114 115 GrGpu* gpu = fContext->getGpu(); 116 117 populate_cache(gpu, CACHE_SIZE_COUNT); 118 } 119 120 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { 121 if (!fContext) { 122 return; 123 } 124 GrResourceCache* cache = fContext->getResourceCache(); 125 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount()); 126 for (int i = 0; i < loops; ++i) { 127 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) { 128 GrContentKey key; 129 BenchResource::ComputeKey(k, &key); 130 SkAutoTUnref<GrGpuResource> resource(cache->findAndRefContentResource(key)); 131 SkASSERT(resource); 132 } 133 } 134 } 135 136private: 137 SkAutoTUnref<GrContext> fContext; 138 typedef Benchmark INHERITED; 139}; 140 141DEF_BENCH( return new GrResourceCacheBenchAdd(); ) 142DEF_BENCH( return new GrResourceCacheBenchFind(); ) 143 144#endif 145