blob: 2a593c5e383fcc8418b3df39ab43f6ceb41a0034 [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)
bsalomon5236cf42015-01-14 10:42:08 -080027 : INHERITED(gpu, kCached_LifeCycle) {
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 gDomain = GrCacheID::GenerateDomain();
bsalomon7775c852014-12-30 12:50:52 -080036 return GrResourceKey(GrCacheID(gDomain, key), 0);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000037 }
38
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000039
40private:
bsalomon10e23ca2014-11-25 05:52:06 -080041 size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
bsalomon69ed47f2014-11-12 11:13:39 -080042
bsalomon6d3fe022014-07-25 08:35:45 -070043 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000044};
45
bsalomon71cb0c22014-11-14 12:10:14 -080046static void populate_cache(GrGpu* gpu, int resourceCount) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000047 for (int i = 0; i < resourceCount; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -080048 GrResourceKey key = BenchResource::ComputeKey(i);
49 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
bsalomon19cd0f12014-11-24 12:19:05 -080050 resource->cacheAccess().setContentKey(key);
51 resource->unref();
52 }
bsalomon19cd0f12014-11-24 12:19:05 -080053}
54
tfarinaf168b862014-06-19 12:32:29 -070055class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000056public:
bsalomon10e23ca2014-11-25 05:52:06 -080057 bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080058 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000059 }
60
61protected:
bsalomon10e23ca2014-11-25 05:52:06 -080062 const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000063 return "grresourcecache_add";
64 }
65
bsalomon10e23ca2014-11-25 05:52:06 -080066 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080067 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
68 if (NULL == context) {
69 return;
70 }
71 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -080072 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -080073
bsalomon8b79d232014-11-10 10:19:06 -080074 GrResourceCache2* cache2 = context->getResourceCache2();
75
76 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -080077 cache2->purgeAllUnlocked();
78 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -080079
80 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000081
commit-bot@chromium.org33614712013-12-03 18:17:16 +000082 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -080083 populate_cache(gpu, CACHE_SIZE_COUNT);
84 SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000085 }
86 }
87
88private:
tfarinaf168b862014-06-19 12:32:29 -070089 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000090};
91
tfarinaf168b862014-06-19 12:32:29 -070092class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000093public:
bsalomon10e23ca2014-11-25 05:52:06 -080094 bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080095 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000096 }
97
98protected:
bsalomon10e23ca2014-11-25 05:52:06 -080099 const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000100 return "grresourcecache_find";
101 }
102
bsalomon10e23ca2014-11-25 05:52:06 -0800103 void onPreDraw() SK_OVERRIDE {
104 fContext.reset(GrContext::CreateMockContext());
105 if (!fContext) {
bsalomon8b79d232014-11-10 10:19:06 -0800106 return;
107 }
108 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -0800109 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800110
bsalomon10e23ca2014-11-25 05:52:06 -0800111 GrResourceCache2* cache2 = fContext->getResourceCache2();
bsalomon8b79d232014-11-10 10:19:06 -0800112
113 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -0800114 cache2->purgeAllUnlocked();
115 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800116
bsalomon10e23ca2014-11-25 05:52:06 -0800117 GrGpu* gpu = fContext->getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800118
bsalomon10e23ca2014-11-25 05:52:06 -0800119 populate_cache(gpu, CACHE_SIZE_COUNT);
120 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000121
bsalomon10e23ca2014-11-25 05:52:06 -0800122 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
123 if (!fContext) {
124 return;
125 }
126 GrResourceCache2* cache2 = fContext->getResourceCache2();
127 SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000128 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -0800129 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
130 GrResourceKey key = BenchResource::ComputeKey(k);
131 SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentResource(key));
132 SkASSERT(resource);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000133 }
134 }
135 }
136
137private:
bsalomon10e23ca2014-11-25 05:52:06 -0800138 SkAutoTUnref<GrContext> fContext;
tfarinaf168b862014-06-19 12:32:29 -0700139 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000140};
141
142DEF_BENCH( return new GrResourceCacheBenchAdd(); )
143DEF_BENCH( return new GrResourceCacheBenchFind(); )
144
145#endif