blob: 96526e5fc2abc73aff8def97ae87b68d15534172 [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
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00008// This tests a Gr class
9#if SK_SUPPORT_GPU
10
bsalomon@google.com4da34e32012-06-19 15:40:27 +000011#include "GrMemoryPool.h"
12#include "SkBenchmark.h"
13#include "SkRandom.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000014#include "SkTDArray.h"
reed@google.com9d1cff12013-04-18 18:43:26 +000015#include "SkTemplates.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000016
17// change this to 0 to compare GrMemoryPool to default new / delete
18#define OVERRIDE_NEW 1
19
bsalomon@google.com4da34e32012-06-19 15:40:27 +000020struct A {
21 int gStuff[10];
22#if OVERRIDE_NEW
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000023 void* operator new (size_t size) { return gBenchPool.allocate(size); }
24 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
bsalomon@google.com4da34e32012-06-19 15:40:27 +000025#endif
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000026 static GrMemoryPool gBenchPool;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000027};
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000028GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
bsalomon@google.com4da34e32012-06-19 15:40:27 +000029
30/**
31 * This benchmark creates and deletes objects in stack order
32 */
33class GrMemoryPoolBenchStack : public SkBenchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000034public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000035 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
36 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000037 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038
bsalomon@google.com4da34e32012-06-19 15:40:27 +000039protected:
40 virtual const char* onGetName() {
41 return "grmemorypool_stack";
42 }
43
commit-bot@chromium.org33614712013-12-03 18:17:16 +000044 virtual void onDraw(const int loops, SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000045 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000046 enum {
47 kMaxObjects = 4 * (1 << 10),
48 };
49 A* objects[kMaxObjects];
50
51 // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise,
52 // we allocate. We start allocate-biased and ping-pong to delete-biased
53 SkFixed delThresh = -SK_FixedHalf;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000054 const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000055 int s = 0;
56
57 int count = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 for (int i = 0; i < loops; i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000059 if (kSwitchThreshPeriod == s) {
60 delThresh = -delThresh;
61 s = 0;
62 }
63 SkFixed del = r.nextSFixed1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000065 (kMaxObjects == count || del < delThresh)) {
66 delete objects[count-1];
67 --count;
68 } else {
69 objects[count] = new A;
70 ++count;
71 }
72 }
73 for (int i = 0; i < count; ++i) {
74 delete objects[i];
75 }
76 }
77
78private:
79 typedef SkBenchmark INHERITED;
80};
81
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +000082struct B {
83 int gStuff[10];
84#if OVERRIDE_NEW
85 void* operator new (size_t size) { return gBenchPool.allocate(size); }
86 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
87#endif
88 static GrMemoryPool gBenchPool;
89};
90GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
91
bsalomon@google.com4da34e32012-06-19 15:40:27 +000092/**
93 * This benchmark creates objects and deletes them in random order
94 */
95class GrMemoryPoolBenchRandom : public SkBenchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000096public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000097 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
98 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000099 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000100
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000101protected:
102 virtual const char* onGetName() {
103 return "grmemorypool_random";
104 }
105
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000106 virtual void onDraw(const int loops, SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000107 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000108 enum {
109 kMaxObjects = 4 * (1 << 10),
110 };
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000111 SkAutoTDelete<B> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000112
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000113 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000114 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
115 if (NULL == objects[idx].get()) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000116 objects[idx].reset(new B);
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000117 } else {
reed@google.com9d1cff12013-04-18 18:43:26 +0000118 objects[idx].free();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000119 }
120 }
121 }
122
123private:
124 typedef SkBenchmark INHERITED;
125};
126
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000127struct C {
128 int gStuff[10];
129#if OVERRIDE_NEW
130 void* operator new (size_t size) { return gBenchPool.allocate(size); }
131 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
132#endif
133 static GrMemoryPool gBenchPool;
134};
135GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
136
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000137/**
138 * This benchmark creates objects and deletes them in queue order
139 */
140class GrMemoryPoolBenchQueue : public SkBenchmark {
141 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000142 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000143 };
144public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000145 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
146 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000147 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000148
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000149protected:
150 virtual const char* onGetName() {
151 return "grmemorypool_queue";
152 }
153
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000154 virtual void onDraw(const int loops, SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000155 SkRandom r;
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000156 C* objects[M];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000157 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000158 uint32_t count = r.nextRangeU(0, M-1);
159 for (uint32_t i = 0; i < count; i++) {
commit-bot@chromium.org38aeb0f2014-02-26 23:01:57 +0000160 objects[i] = new C;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000161 }
162 for (uint32_t i = 0; i < count; i++) {
163 delete objects[i];
164 }
165 }
166 }
167
168private:
169 typedef SkBenchmark INHERITED;
170};
171
172///////////////////////////////////////////////////////////////////////////////
173
mtklein@google.com410e6e82013-09-13 19:52:27 +0000174DEF_BENCH( return new GrMemoryPoolBenchStack(); )
175DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
176DEF_BENCH( return new GrMemoryPoolBenchQueue(); )
commit-bot@chromium.org97b4b672013-09-26 19:23:03 +0000177
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000178#endif