blob: dccf8278f541083ffc026c8f95a47791177ba534 [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
20namespace {
21struct A {
22 int gStuff[10];
23#if OVERRIDE_NEW
24 void* operator new (size_t size) { return gPool.allocate(size); }
25 void operator delete (void* mem) { if (mem) { return gPool.release(mem); } }
26#endif
27 static GrMemoryPool gPool;
28};
29GrMemoryPool A::gPool(10 * (1 << 10), 10 * (1 << 10));
30}
31
32
33/**
34 * This benchmark creates and deletes objects in stack order
35 */
36class GrMemoryPoolBenchStack : public SkBenchmark {
37 enum {
tomhudson@google.comfc157222012-09-13 15:42:33 +000038 N = SkBENCHLOOP(1 * (1 << 20)),
bsalomon@google.com4da34e32012-06-19 15:40:27 +000039 };
40public:
41 GrMemoryPoolBenchStack(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000042 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000043 }
44protected:
45 virtual const char* onGetName() {
46 return "grmemorypool_stack";
47 }
48
sugoi@google.com77472f02013-03-05 18:50:01 +000049 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000050 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000051 enum {
52 kMaxObjects = 4 * (1 << 10),
53 };
54 A* objects[kMaxObjects];
55
56 // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise,
57 // we allocate. We start allocate-biased and ping-pong to delete-biased
58 SkFixed delThresh = -SK_FixedHalf;
59 enum {
60 kSwitchThreshPeriod = N / (2 * kMaxObjects),
61 };
62 int s = 0;
63
64 int count = 0;
65 for (int i = 0; i < N; i++, ++s) {
66 if (kSwitchThreshPeriod == s) {
67 delThresh = -delThresh;
68 s = 0;
69 }
70 SkFixed del = r.nextSFixed1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000072 (kMaxObjects == count || del < delThresh)) {
73 delete objects[count-1];
74 --count;
75 } else {
76 objects[count] = new A;
77 ++count;
78 }
79 }
80 for (int i = 0; i < count; ++i) {
81 delete objects[i];
82 }
83 }
84
85private:
86 typedef SkBenchmark INHERITED;
87};
88
89/**
90 * This benchmark creates objects and deletes them in random order
91 */
92class GrMemoryPoolBenchRandom : public SkBenchmark {
93 enum {
tomhudson@google.comfc157222012-09-13 15:42:33 +000094 N = SkBENCHLOOP(1 * (1 << 20)),
bsalomon@google.com4da34e32012-06-19 15:40:27 +000095 };
96public:
97 GrMemoryPoolBenchRandom(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000098 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000099 }
100protected:
101 virtual const char* onGetName() {
102 return "grmemorypool_random";
103 }
104
sugoi@google.com77472f02013-03-05 18:50:01 +0000105 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000106 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000107 enum {
108 kMaxObjects = 4 * (1 << 10),
109 };
reed@google.com9d1cff12013-04-18 18:43:26 +0000110 SkAutoTDelete<A> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000111
112 for (int i = 0; i < N; i++) {
113 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
114 if (NULL == objects[idx].get()) {
115 objects[idx].reset(new A);
116 } else {
reed@google.com9d1cff12013-04-18 18:43:26 +0000117 objects[idx].free();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000118 }
119 }
120 }
121
122private:
123 typedef SkBenchmark INHERITED;
124};
125
126/**
127 * This benchmark creates objects and deletes them in queue order
128 */
129class GrMemoryPoolBenchQueue : public SkBenchmark {
130 enum {
tomhudson@google.comfc157222012-09-13 15:42:33 +0000131 N = SkBENCHLOOP((1 << 8)),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000132 M = SkBENCHLOOP(4 * (1 << 10)),
133 };
134public:
135 GrMemoryPoolBenchQueue(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000136 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000137 }
138protected:
139 virtual const char* onGetName() {
140 return "grmemorypool_queue";
141 }
142
sugoi@google.com77472f02013-03-05 18:50:01 +0000143 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000144 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000145 A* objects[M];
146 for (int i = 0; i < N; i++) {
147 uint32_t count = r.nextRangeU(0, M-1);
148 for (uint32_t i = 0; i < count; i++) {
149 objects[i] = new A;
150 }
151 for (uint32_t i = 0; i < count; i++) {
152 delete objects[i];
153 }
154 }
155 }
156
157private:
158 typedef SkBenchmark INHERITED;
159};
160
161///////////////////////////////////////////////////////////////////////////////
162
163static SkBenchmark* Fact1(void* p) { return new GrMemoryPoolBenchStack(p); }
164static SkBenchmark* Fact2(void* p) { return new GrMemoryPoolBenchRandom(p); }
165static SkBenchmark* Fact3(void* p) { return new GrMemoryPoolBenchQueue(p); }
166
167static BenchRegistry gReg01(Fact1);
168static BenchRegistry gReg02(Fact2);
169static BenchRegistry gReg03(Fact3);
170
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000171#endif