blob: e30dd3052f4842448e971303c405b5d40dd560ec [file] [log] [blame]
commit-bot@chromium.org644629c2013-11-21 06:21:58 +00001
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
bsalomon8b79d232014-11-10 10:19:06 -08009#include "Benchmark.h"
10
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000011#if SK_SUPPORT_GPU
12
bsalomon6d3fe022014-07-25 08:35:45 -070013#include "GrGpuResource.h"
tfarinaf168b862014-06-19 12:32:29 -070014#include "GrContext.h"
bsalomonbcf0a522014-10-08 08:40:09 -070015#include "GrGpu.h"
bsalomon8b79d232014-11-10 10:19:06 -080016#include "GrResourceCache2.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000017#include "SkCanvas.h"
18
19enum {
bsalomon10e23ca2014-11-25 05:52:06 -080020 CACHE_SIZE_COUNT = 4096,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000021};
22
bsalomon10e23ca2014-11-25 05:52:06 -080023class BenchResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000024public:
bsalomon10e23ca2014-11-25 05:52:06 -080025 SK_DECLARE_INST_COUNT(BenchResource);
26 BenchResource (GrGpu* gpu)
27 : INHERITED(gpu, false) {
bsalomon16961262014-08-26 14:01:07 -070028 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000029 }
30
bsalomon10e23ca2014-11-25 05:52:06 -080031 static GrResourceKey ComputeKey(int i) {
bsalomon91175f12014-11-24 07:05:15 -080032 GrCacheID::Key key;
33 memset(&key, 0, sizeof(key));
bsalomon10e23ca2014-11-25 05:52:06 -080034 key.fData32[0] = i;
bsalomon91175f12014-11-24 07:05:15 -080035 static int gType = GrResourceKey::GenerateResourceType();
36 static int gDomain = GrCacheID::GenerateDomain();
37 return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038 }
39
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000040
41private:
bsalomon10e23ca2014-11-25 05:52:06 -080042 size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
bsalomon69ed47f2014-11-12 11:13:39 -080043
bsalomon6d3fe022014-07-25 08:35:45 -070044 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000045};
46
bsalomon71cb0c22014-11-14 12:10:14 -080047static void populate_cache(GrGpu* gpu, int resourceCount) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000048 for (int i = 0; i < resourceCount; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -080049 GrResourceKey key = BenchResource::ComputeKey(i);
50 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
bsalomon19cd0f12014-11-24 12:19:05 -080051 resource->cacheAccess().setContentKey(key);
52 resource->unref();
53 }
bsalomon19cd0f12014-11-24 12:19:05 -080054}
55
tfarinaf168b862014-06-19 12:32:29 -070056class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000057public:
bsalomon10e23ca2014-11-25 05:52:06 -080058 bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080059 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000060 }
61
62protected:
bsalomon10e23ca2014-11-25 05:52:06 -080063 const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000064 return "grresourcecache_add";
65 }
66
bsalomon10e23ca2014-11-25 05:52:06 -080067 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080068 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
69 if (NULL == context) {
70 return;
71 }
72 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -080073 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -080074
bsalomon8b79d232014-11-10 10:19:06 -080075 GrResourceCache2* cache2 = context->getResourceCache2();
76
77 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -080078 cache2->purgeAllUnlocked();
79 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -080080
81 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000082
commit-bot@chromium.org33614712013-12-03 18:17:16 +000083 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -080084 populate_cache(gpu, CACHE_SIZE_COUNT);
85 SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000086 }
87 }
88
89private:
tfarinaf168b862014-06-19 12:32:29 -070090 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000091};
92
tfarinaf168b862014-06-19 12:32:29 -070093class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000094public:
bsalomon10e23ca2014-11-25 05:52:06 -080095 bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080096 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000097 }
98
99protected:
bsalomon10e23ca2014-11-25 05:52:06 -0800100 const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000101 return "grresourcecache_find";
102 }
103
bsalomon10e23ca2014-11-25 05:52:06 -0800104 void onPreDraw() SK_OVERRIDE {
105 fContext.reset(GrContext::CreateMockContext());
106 if (!fContext) {
bsalomon8b79d232014-11-10 10:19:06 -0800107 return;
108 }
109 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -0800110 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800111
bsalomon10e23ca2014-11-25 05:52:06 -0800112 GrResourceCache2* cache2 = fContext->getResourceCache2();
bsalomon8b79d232014-11-10 10:19:06 -0800113
114 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -0800115 cache2->purgeAllUnlocked();
116 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800117
bsalomon10e23ca2014-11-25 05:52:06 -0800118 GrGpu* gpu = fContext->getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800119
bsalomon10e23ca2014-11-25 05:52:06 -0800120 populate_cache(gpu, CACHE_SIZE_COUNT);
121 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000122
bsalomon10e23ca2014-11-25 05:52:06 -0800123 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());
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000129 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -0800130 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
131 GrResourceKey key = BenchResource::ComputeKey(k);
132 SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentResource(key));
133 SkASSERT(resource);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000134 }
135 }
136 }
137
138private:
bsalomon10e23ca2014-11-25 05:52:06 -0800139 SkAutoTUnref<GrContext> fContext;
tfarinaf168b862014-06-19 12:32:29 -0700140 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000141};
142
143DEF_BENCH( return new GrResourceCacheBenchAdd(); )
144DEF_BENCH( return new GrResourceCacheBenchFind(); )
145
146#endif