blob: 0adf92abaa6f2f49ade2f7a6f5b056401a56c763 [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
23 void* operator new (size_t size) { return gPool.allocate(size); }
24 void operator delete (void* mem) { if (mem) { return gPool.release(mem); } }
25#endif
26 static GrMemoryPool gPool;
27};
28GrMemoryPool A::gPool(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:
mtklein@google.com410e6e82013-09-13 19:52:27 +000035 GrMemoryPoolBenchStack() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000036 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000037 }
38protected:
39 virtual const char* onGetName() {
40 return "grmemorypool_stack";
41 }
42
sugoi@google.com77472f02013-03-05 18:50:01 +000043 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000044 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000045 enum {
46 kMaxObjects = 4 * (1 << 10),
47 };
48 A* objects[kMaxObjects];
49
50 // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise,
51 // we allocate. We start allocate-biased and ping-pong to delete-biased
52 SkFixed delThresh = -SK_FixedHalf;
mtklein@google.comc2897432013-09-10 19:23:38 +000053 const int kSwitchThreshPeriod = this->getLoops() / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000054 int s = 0;
55
56 int count = 0;
mtklein@google.comc2897432013-09-10 19:23:38 +000057 for (int i = 0; i < this->getLoops(); i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000058 if (kSwitchThreshPeriod == s) {
59 delThresh = -delThresh;
60 s = 0;
61 }
62 SkFixed del = r.nextSFixed1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000063 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000064 (kMaxObjects == count || del < delThresh)) {
65 delete objects[count-1];
66 --count;
67 } else {
68 objects[count] = new A;
69 ++count;
70 }
71 }
72 for (int i = 0; i < count; ++i) {
73 delete objects[i];
74 }
75 }
76
77private:
78 typedef SkBenchmark INHERITED;
79};
80
81/**
82 * This benchmark creates objects and deletes them in random order
83 */
84class GrMemoryPoolBenchRandom : public SkBenchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000085public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000086 GrMemoryPoolBenchRandom() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000087 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000088 }
89protected:
90 virtual const char* onGetName() {
91 return "grmemorypool_random";
92 }
93
sugoi@google.com77472f02013-03-05 18:50:01 +000094 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000095 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000096 enum {
97 kMaxObjects = 4 * (1 << 10),
98 };
reed@google.com9d1cff12013-04-18 18:43:26 +000099 SkAutoTDelete<A> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000100
mtklein@google.comc2897432013-09-10 19:23:38 +0000101 for (int i = 0; i < this->getLoops(); i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000102 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
103 if (NULL == objects[idx].get()) {
104 objects[idx].reset(new A);
105 } else {
reed@google.com9d1cff12013-04-18 18:43:26 +0000106 objects[idx].free();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000107 }
108 }
109 }
110
111private:
112 typedef SkBenchmark INHERITED;
113};
114
115/**
116 * This benchmark creates objects and deletes them in queue order
117 */
118class GrMemoryPoolBenchQueue : public SkBenchmark {
119 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000120 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000121 };
122public:
mtklein@google.com410e6e82013-09-13 19:52:27 +0000123 GrMemoryPoolBenchQueue() {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000124 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000125 }
126protected:
127 virtual const char* onGetName() {
128 return "grmemorypool_queue";
129 }
130
sugoi@google.com77472f02013-03-05 18:50:01 +0000131 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000132 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000133 A* objects[M];
mtklein@google.comc2897432013-09-10 19:23:38 +0000134 for (int i = 0; i < this->getLoops(); i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000135 uint32_t count = r.nextRangeU(0, M-1);
136 for (uint32_t i = 0; i < count; i++) {
137 objects[i] = new A;
138 }
139 for (uint32_t i = 0; i < count; i++) {
140 delete objects[i];
141 }
142 }
143 }
144
145private:
146 typedef SkBenchmark INHERITED;
147};
148
149///////////////////////////////////////////////////////////////////////////////
150
mtklein@google.com410e6e82013-09-13 19:52:27 +0000151DEF_BENCH( return new GrMemoryPoolBenchStack(); )
152DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
153DEF_BENCH( return new GrMemoryPoolBenchQueue(); )
commit-bot@chromium.org97b4b672013-09-26 19:23:03 +0000154
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000155#endif