3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
13 #include "GrGpuResource.h"
14 #include "GrGpuResourcePriv.h"
15 #include "GrContext.h"
17 #include "GrResourceCache.h"
21 CACHE_SIZE_COUNT = 4096,
24 class BenchResource : public GrGpuResource {
26 SK_DECLARE_INST_COUNT(BenchResource);
27 BenchResource (GrGpu* gpu)
28 : INHERITED(gpu, kCached_LifeCycle) {
29 this->registerWithCache();
32 static void ComputeKey(int i, GrUniqueKey* key) {
33 static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
34 GrUniqueKey::Builder builder(key, kDomain, 1);
39 size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
41 typedef GrGpuResource INHERITED;
44 static void populate_cache(GrGpu* gpu, int resourceCount) {
45 for (int i = 0; i < resourceCount; ++i) {
47 BenchResource::ComputeKey(i, &key);
48 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
49 resource->resourcePriv().setUniqueKey(key);
54 class GrResourceCacheBenchAdd : public Benchmark {
56 bool isSuitableFor(Backend backend) SK_OVERRIDE {
57 return backend == kNonRendering_Backend;
61 const char* onGetName() SK_OVERRIDE {
62 return "grresourcecache_add";
65 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
66 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
67 if (NULL == context) {
70 // Set the cache budget to be very large so no purging occurs.
71 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
73 GrResourceCache* cache = context->getResourceCache();
75 // Make sure the cache is empty.
76 cache->purgeAllUnlocked();
77 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
79 GrGpu* gpu = context->getGpu();
81 for (int i = 0; i < loops; ++i) {
82 populate_cache(gpu, CACHE_SIZE_COUNT);
83 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
88 typedef Benchmark INHERITED;
91 class GrResourceCacheBenchFind : public Benchmark {
93 bool isSuitableFor(Backend backend) SK_OVERRIDE {
94 return backend == kNonRendering_Backend;
98 const char* onGetName() SK_OVERRIDE {
99 return "grresourcecache_find";
102 void onPreDraw() SK_OVERRIDE {
103 fContext.reset(GrContext::CreateMockContext());
107 // Set the cache budget to be very large so no purging occurs.
108 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
110 GrResourceCache* cache = fContext->getResourceCache();
112 // Make sure the cache is empty.
113 cache->purgeAllUnlocked();
114 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
116 GrGpu* gpu = fContext->getGpu();
118 populate_cache(gpu, CACHE_SIZE_COUNT);
121 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
125 GrResourceCache* cache = fContext->getResourceCache();
126 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
127 for (int i = 0; i < loops; ++i) {
128 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
130 BenchResource::ComputeKey(k, &key);
131 SkAutoTUnref<GrGpuResource> resource(cache->findAndRefUniqueResource(key));
138 SkAutoTUnref<GrContext> fContext;
139 typedef Benchmark INHERITED;
142 DEF_BENCH( return new GrResourceCacheBenchAdd(); )
143 DEF_BENCH( return new GrResourceCacheBenchFind(); )