blob: 98fd6e5bded6fd3c455b44d2fb24f18904c78843 [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"
14#include "SkTScopedPtr.h"
15#include "SkTDArray.h"
16
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 {
38 N = SkBENCHLOOP(5 * (1 << 20)),
39 };
40public:
41 GrMemoryPoolBenchStack(void* param) : INHERITED(param) {
42 }
43protected:
44 virtual const char* onGetName() {
45 return "grmemorypool_stack";
46 }
47
48 virtual void onDraw(SkCanvas* canvas) {
49 SkRandom r;
50 enum {
51 kMaxObjects = 4 * (1 << 10),
52 };
53 A* objects[kMaxObjects];
54
55 // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise,
56 // we allocate. We start allocate-biased and ping-pong to delete-biased
57 SkFixed delThresh = -SK_FixedHalf;
58 enum {
59 kSwitchThreshPeriod = N / (2 * kMaxObjects),
60 };
61 int s = 0;
62
63 int count = 0;
64 for (int i = 0; i < N; i++, ++s) {
65 if (kSwitchThreshPeriod == s) {
66 delThresh = -delThresh;
67 s = 0;
68 }
69 SkFixed del = r.nextSFixed1();
70 if (count &&
71 (kMaxObjects == count || del < delThresh)) {
72 delete objects[count-1];
73 --count;
74 } else {
75 objects[count] = new A;
76 ++count;
77 }
78 }
79 for (int i = 0; i < count; ++i) {
80 delete objects[i];
81 }
82 }
83
84private:
85 typedef SkBenchmark INHERITED;
86};
87
88/**
89 * This benchmark creates objects and deletes them in random order
90 */
91class GrMemoryPoolBenchRandom : public SkBenchmark {
92 enum {
93 N = SkBENCHLOOP(5 * (1 << 20)),
94 };
95public:
96 GrMemoryPoolBenchRandom(void* param) : INHERITED(param) {
97 }
98protected:
99 virtual const char* onGetName() {
100 return "grmemorypool_random";
101 }
102
103 virtual void onDraw(SkCanvas* canvas) {
104 SkRandom r;
105 enum {
106 kMaxObjects = 4 * (1 << 10),
107 };
108 SkTScopedPtr<A> objects[kMaxObjects];
109
110 for (int i = 0; i < N; i++) {
111 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
112 if (NULL == objects[idx].get()) {
113 objects[idx].reset(new A);
114 } else {
115 objects[idx].reset(NULL);
116 }
117 }
118 }
119
120private:
121 typedef SkBenchmark INHERITED;
122};
123
124/**
125 * This benchmark creates objects and deletes them in queue order
126 */
127class GrMemoryPoolBenchQueue : public SkBenchmark {
128 enum {
129 N = SkBENCHLOOP((1 << 10)),
130 M = SkBENCHLOOP(4 * (1 << 10)),
131 };
132public:
133 GrMemoryPoolBenchQueue(void* param) : INHERITED(param) {
134 }
135protected:
136 virtual const char* onGetName() {
137 return "grmemorypool_queue";
138 }
139
140 virtual void onDraw(SkCanvas* canvas) {
141 SkRandom r;
142 A* objects[M];
143 for (int i = 0; i < N; i++) {
144 uint32_t count = r.nextRangeU(0, M-1);
145 for (uint32_t i = 0; i < count; i++) {
146 objects[i] = new A;
147 }
148 for (uint32_t i = 0; i < count; i++) {
149 delete objects[i];
150 }
151 }
152 }
153
154private:
155 typedef SkBenchmark INHERITED;
156};
157
158///////////////////////////////////////////////////////////////////////////////
159
160static SkBenchmark* Fact1(void* p) { return new GrMemoryPoolBenchStack(p); }
161static SkBenchmark* Fact2(void* p) { return new GrMemoryPoolBenchRandom(p); }
162static SkBenchmark* Fact3(void* p) { return new GrMemoryPoolBenchQueue(p); }
163
164static BenchRegistry gReg01(Fact1);
165static BenchRegistry gReg02(Fact2);
166static BenchRegistry gReg03(Fact3);
167
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000168#endif