blob: 394577498f9c09e6e889406b5911d9c24e3a2c99 [file] [log] [blame]
bsalomon@google.com4da34e32012-06-19 15:40:27 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
mtkleinf0599002015-07-13 06:18:39 -07008#include "SkTypes.h"
9
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010// This tests a Gr class
11#if SK_SUPPORT_GPU
12
tfarinaf168b862014-06-19 12:32:29 -070013#include "Benchmark.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000014#include "GrMemoryPool.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000015#include "SkRandom.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000016#include "SkTDArray.h"
reed@google.com9d1cff12013-04-18 18:43:26 +000017#include "SkTemplates.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000018
19// change this to 0 to compare GrMemoryPool to default new / delete
20#define OVERRIDE_NEW 1
21
bsalomon@google.com4da34e32012-06-19 15:40:27 +000022struct A {
23 int gStuff[10];
24#if OVERRIDE_NEW
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000025 void* operator new (size_t size) { return gBenchPool.allocate(size); }
26 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
bsalomon@google.com4da34e32012-06-19 15:40:27 +000027#endif
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000028 static GrMemoryPool gBenchPool;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000029};
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000030GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
bsalomon@google.com4da34e32012-06-19 15:40:27 +000031
32/**
33 * This benchmark creates and deletes objects in stack order
34 */
tfarinaf168b862014-06-19 12:32:29 -070035class GrMemoryPoolBenchStack : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000036public:
mtklein36352bf2015-03-25 18:17:31 -070037 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000039 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000040
bsalomon@google.com4da34e32012-06-19 15:40:27 +000041protected:
mtkleinf0599002015-07-13 06:18:39 -070042 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000043 return "grmemorypool_stack";
44 }
45
mtkleinf0599002015-07-13 06:18:39 -070046 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000047 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000048 enum {
49 kMaxObjects = 4 * (1 << 10),
50 };
51 A* objects[kMaxObjects];
52
53 // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise,
54 // we allocate. We start allocate-biased and ping-pong to delete-biased
55 SkFixed delThresh = -SK_FixedHalf;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000056 const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000057 int s = 0;
58
59 int count = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000060 for (int i = 0; i < loops; i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000061 if (kSwitchThreshPeriod == s) {
62 delThresh = -delThresh;
63 s = 0;
64 }
65 SkFixed del = r.nextSFixed1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000067 (kMaxObjects == count || del < delThresh)) {
68 delete objects[count-1];
69 --count;
70 } else {
71 objects[count] = new A;
72 ++count;
73 }
74 }
75 for (int i = 0; i < count; ++i) {
76 delete objects[i];
77 }
78 }
79
80private:
tfarinaf168b862014-06-19 12:32:29 -070081 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000082};
83
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000084struct B {
85 int gStuff[10];
86#if OVERRIDE_NEW
87 void* operator new (size_t size) { return gBenchPool.allocate(size); }
88 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
89#endif
90 static GrMemoryPool gBenchPool;
91};
92GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
93
bsalomon@google.com4da34e32012-06-19 15:40:27 +000094/**
95 * This benchmark creates objects and deletes them in random order
96 */
tfarinaf168b862014-06-19 12:32:29 -070097class GrMemoryPoolBenchRandom : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000098public:
mtklein36352bf2015-03-25 18:17:31 -070099 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000100 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000101 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000102
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000103protected:
mtkleinf0599002015-07-13 06:18:39 -0700104 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000105 return "grmemorypool_random";
106 }
107
mtkleinf0599002015-07-13 06:18:39 -0700108 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000109 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000110 enum {
111 kMaxObjects = 4 * (1 << 10),
112 };
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000113 SkAutoTDelete<B> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000114
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000115 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000116 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
117 if (NULL == objects[idx].get()) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000118 objects[idx].reset(new B);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000119 } else {
reed@google.com9d1cff12013-04-18 18:43:26 +0000120 objects[idx].free();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000121 }
122 }
123 }
124
125private:
tfarinaf168b862014-06-19 12:32:29 -0700126 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000127};
128
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000129struct C {
130 int gStuff[10];
131#if OVERRIDE_NEW
132 void* operator new (size_t size) { return gBenchPool.allocate(size); }
133 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
134#endif
135 static GrMemoryPool gBenchPool;
136};
137GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
138
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000139/**
140 * This benchmark creates objects and deletes them in queue order
141 */
tfarinaf168b862014-06-19 12:32:29 -0700142class GrMemoryPoolBenchQueue : public Benchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000143 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000144 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000145 };
146public:
mtklein36352bf2015-03-25 18:17:31 -0700147 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000148 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000149 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000150
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000151protected:
mtkleinf0599002015-07-13 06:18:39 -0700152 const char* onGetName() override {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000153 return "grmemorypool_queue";
154 }
155
mtkleinf0599002015-07-13 06:18:39 -0700156 void onDraw(const int loops, SkCanvas*) override {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000157 SkRandom r;
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000158 C* objects[M];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000159 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000160 uint32_t count = r.nextRangeU(0, M-1);
161 for (uint32_t i = 0; i < count; i++) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000162 objects[i] = new C;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000163 }
164 for (uint32_t i = 0; i < count; i++) {
165 delete objects[i];
166 }
167 }
168 }
169
170private:
tfarinaf168b862014-06-19 12:32:29 -0700171 typedef Benchmark INHERITED;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000172};
173
174///////////////////////////////////////////////////////////////////////////////
175
mtklein@google.com410e6e82013-09-13 19:52:27 +0000176DEF_BENCH( return new GrMemoryPoolBenchStack(); )
177DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
178DEF_BENCH( return new GrMemoryPoolBenchQueue(); )
commit-bot@chromium.org97b4b672013-09-26 19:23:03 +0000179
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000180#endif