blob: 5807654ca9ff6609057e89120867b773f8e485d9 [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"
bsalomon3582d3e2015-02-13 14:20:05 -080014#include "GrGpuResourcePriv.h"
tfarinaf168b862014-06-19 12:32:29 -070015#include "GrContext.h"
bsalomonbcf0a522014-10-08 08:40:09 -070016#include "GrGpu.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000018#include "SkCanvas.h"
19
20enum {
bsalomon10e23ca2014-11-25 05:52:06 -080021 CACHE_SIZE_COUNT = 4096,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000022};
23
bsalomon10e23ca2014-11-25 05:52:06 -080024class BenchResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000025public:
bsalomon10e23ca2014-11-25 05:52:06 -080026 SK_DECLARE_INST_COUNT(BenchResource);
27 BenchResource (GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -080028 : INHERITED(gpu, kCached_LifeCycle) {
bsalomon16961262014-08-26 14:01:07 -070029 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000030 }
31
bsalomon8718aaf2015-02-19 07:24:21 -080032 static void ComputeKey(int i, GrUniqueKey* key) {
33 static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
34 GrUniqueKey::Builder builder(key, kDomain, 1);
bsalomon24db3b12015-01-23 04:24:04 -080035 builder[0] = i;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000036 }
37
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038private:
mtklein36352bf2015-03-25 18:17:31 -070039 size_t onGpuMemorySize() const override { return 100; }
bsalomon69ed47f2014-11-12 11:13:39 -080040
bsalomon6d3fe022014-07-25 08:35:45 -070041 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000042};
43
bsalomon71cb0c22014-11-14 12:10:14 -080044static void populate_cache(GrGpu* gpu, int resourceCount) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000045 for (int i = 0; i < resourceCount; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -080046 GrUniqueKey key;
bsalomon24db3b12015-01-23 04:24:04 -080047 BenchResource::ComputeKey(i, &key);
bsalomon10e23ca2014-11-25 05:52:06 -080048 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
bsalomon8718aaf2015-02-19 07:24:21 -080049 resource->resourcePriv().setUniqueKey(key);
bsalomon19cd0f12014-11-24 12:19:05 -080050 resource->unref();
51 }
bsalomon19cd0f12014-11-24 12:19:05 -080052}
53
tfarinaf168b862014-06-19 12:32:29 -070054class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000055public:
mtklein36352bf2015-03-25 18:17:31 -070056 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -080057 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000058 }
59
60protected:
mtklein36352bf2015-03-25 18:17:31 -070061 const char* onGetName() override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000062 return "grresourcecache_add";
63 }
64
mtklein36352bf2015-03-25 18:17:31 -070065 void onDraw(const int loops, SkCanvas* canvas) override {
bsalomon8b79d232014-11-10 10:19:06 -080066 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
67 if (NULL == context) {
68 return;
69 }
70 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -080071 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -080072
bsalomon0ea80f42015-02-11 10:49:59 -080073 GrResourceCache* cache = context->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -080074
75 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -080076 cache->purgeAllUnlocked();
77 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -080078
79 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000080
commit-bot@chromium.org33614712013-12-03 18:17:16 +000081 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -080082 populate_cache(gpu, CACHE_SIZE_COUNT);
bsalomon0ea80f42015-02-11 10:49:59 -080083 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000084 }
85 }
86
87private:
tfarinaf168b862014-06-19 12:32:29 -070088 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000089};
90
tfarinaf168b862014-06-19 12:32:29 -070091class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000092public:
mtklein36352bf2015-03-25 18:17:31 -070093 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -080094 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000095 }
96
97protected:
mtklein36352bf2015-03-25 18:17:31 -070098 const char* onGetName() override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000099 return "grresourcecache_find";
100 }
101
mtklein36352bf2015-03-25 18:17:31 -0700102 void onPreDraw() override {
bsalomon10e23ca2014-11-25 05:52:06 -0800103 fContext.reset(GrContext::CreateMockContext());
104 if (!fContext) {
bsalomon8b79d232014-11-10 10:19:06 -0800105 return;
106 }
107 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -0800108 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800109
bsalomon0ea80f42015-02-11 10:49:59 -0800110 GrResourceCache* cache = fContext->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -0800111
112 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -0800113 cache->purgeAllUnlocked();
114 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800115
bsalomon10e23ca2014-11-25 05:52:06 -0800116 GrGpu* gpu = fContext->getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800117
bsalomon10e23ca2014-11-25 05:52:06 -0800118 populate_cache(gpu, CACHE_SIZE_COUNT);
119 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000120
mtklein36352bf2015-03-25 18:17:31 -0700121 void onDraw(const int loops, SkCanvas* canvas) override {
bsalomon10e23ca2014-11-25 05:52:06 -0800122 if (!fContext) {
123 return;
124 }
bsalomon0ea80f42015-02-11 10:49:59 -0800125 GrResourceCache* cache = fContext->getResourceCache();
126 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000127 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -0800128 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
bsalomon8718aaf2015-02-19 07:24:21 -0800129 GrUniqueKey key;
bsalomon24db3b12015-01-23 04:24:04 -0800130 BenchResource::ComputeKey(k, &key);
bsalomon8718aaf2015-02-19 07:24:21 -0800131 SkAutoTUnref<GrGpuResource> resource(cache->findAndRefUniqueResource(key));
bsalomon10e23ca2014-11-25 05:52:06 -0800132 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