2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 // This tests a Gr class
11 #include "Benchmark.h"
12 #include "GrMemoryPool.h"
14 #include "SkTDArray.h"
15 #include "SkTemplates.h"
17 // change this to 0 to compare GrMemoryPool to default new / delete
18 #define OVERRIDE_NEW 1
23 void* operator new (size_t size) { return gBenchPool.allocate(size); }
24 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
26 static GrMemoryPool gBenchPool;
28 GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
31 * This benchmark creates and deletes objects in stack order
33 class GrMemoryPoolBenchStack : public Benchmark {
35 bool isSuitableFor(Backend backend) SK_OVERRIDE {
36 return backend == kNonRendering_Backend;
40 virtual const char* onGetName() {
41 return "grmemorypool_stack";
44 virtual void onDraw(const int loops, SkCanvas*) {
47 kMaxObjects = 4 * (1 << 10),
49 A* objects[kMaxObjects];
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;
54 const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
58 for (int i = 0; i < loops; i++, ++s) {
59 if (kSwitchThreshPeriod == s) {
60 delThresh = -delThresh;
63 SkFixed del = r.nextSFixed1();
65 (kMaxObjects == count || del < delThresh)) {
66 delete objects[count-1];
69 objects[count] = new A;
73 for (int i = 0; i < count; ++i) {
79 typedef Benchmark INHERITED;
85 void* operator new (size_t size) { return gBenchPool.allocate(size); }
86 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
88 static GrMemoryPool gBenchPool;
90 GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
93 * This benchmark creates objects and deletes them in random order
95 class GrMemoryPoolBenchRandom : public Benchmark {
97 bool isSuitableFor(Backend backend) SK_OVERRIDE {
98 return backend == kNonRendering_Backend;
102 virtual const char* onGetName() {
103 return "grmemorypool_random";
106 virtual void onDraw(const int loops, SkCanvas*) {
109 kMaxObjects = 4 * (1 << 10),
111 SkAutoTDelete<B> objects[kMaxObjects];
113 for (int i = 0; i < loops; i++) {
114 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
115 if (NULL == objects[idx].get()) {
116 objects[idx].reset(new B);
124 typedef Benchmark INHERITED;
130 void* operator new (size_t size) { return gBenchPool.allocate(size); }
131 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem); } }
133 static GrMemoryPool gBenchPool;
135 GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10));
138 * This benchmark creates objects and deletes them in queue order
140 class GrMemoryPoolBenchQueue : public Benchmark {
145 bool isSuitableFor(Backend backend) SK_OVERRIDE {
146 return backend == kNonRendering_Backend;
150 virtual const char* onGetName() {
151 return "grmemorypool_queue";
154 virtual void onDraw(const int loops, SkCanvas*) {
157 for (int i = 0; i < loops; i++) {
158 uint32_t count = r.nextRangeU(0, M-1);
159 for (uint32_t i = 0; i < count; i++) {
162 for (uint32_t i = 0; i < count; i++) {
169 typedef Benchmark INHERITED;
172 ///////////////////////////////////////////////////////////////////////////////
174 DEF_BENCH( return new GrMemoryPoolBenchStack(); )
175 DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
176 DEF_BENCH( return new GrMemoryPoolBenchQueue(); )