blob: 5ad1afa51c81290b4a5127aa16164e35b7b1be36 [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 {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000037public:
38 GrMemoryPoolBenchStack(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000039 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000040 }
41protected:
42 virtual const char* onGetName() {
43 return "grmemorypool_stack";
44 }
45
sugoi@google.com77472f02013-03-05 18:50:01 +000046 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000047 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000048 enum {
49 kMaxObjects = 4 * (1 << 10),
50 };
51 A* objects[kMaxObjects];
52
53 // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise,
54 // we allocate. We start allocate-biased and ping-pong to delete-biased
55 SkFixed delThresh = -SK_FixedHalf;
mtklein@google.comc2897432013-09-10 19:23:38 +000056 const int kSwitchThreshPeriod = this->getLoops() / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000057 int s = 0;
58
59 int count = 0;
mtklein@google.comc2897432013-09-10 19:23:38 +000060 for (int i = 0; i < this->getLoops(); i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000061 if (kSwitchThreshPeriod == s) {
62 delThresh = -delThresh;
63 s = 0;
64 }
65 SkFixed del = r.nextSFixed1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000067 (kMaxObjects == count || del < delThresh)) {
68 delete objects[count-1];
69 --count;
70 } else {
71 objects[count] = new A;
72 ++count;
73 }
74 }
75 for (int i = 0; i < count; ++i) {
76 delete objects[i];
77 }
78 }
79
80private:
81 typedef SkBenchmark INHERITED;
82};
83
84/**
85 * This benchmark creates objects and deletes them in random order
86 */
87class GrMemoryPoolBenchRandom : public SkBenchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000088public:
89 GrMemoryPoolBenchRandom(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +000090 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000091 }
92protected:
93 virtual const char* onGetName() {
94 return "grmemorypool_random";
95 }
96
sugoi@google.com77472f02013-03-05 18:50:01 +000097 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000098 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000099 enum {
100 kMaxObjects = 4 * (1 << 10),
101 };
reed@google.com9d1cff12013-04-18 18:43:26 +0000102 SkAutoTDelete<A> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000103
mtklein@google.comc2897432013-09-10 19:23:38 +0000104 for (int i = 0; i < this->getLoops(); i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000105 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
106 if (NULL == objects[idx].get()) {
107 objects[idx].reset(new A);
108 } else {
reed@google.com9d1cff12013-04-18 18:43:26 +0000109 objects[idx].free();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000110 }
111 }
112 }
113
114private:
115 typedef SkBenchmark INHERITED;
116};
117
118/**
119 * This benchmark creates objects and deletes them in queue order
120 */
121class GrMemoryPoolBenchQueue : public SkBenchmark {
122 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000123 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000124 };
125public:
126 GrMemoryPoolBenchQueue(void* param) : INHERITED(param) {
tomhudson@google.com9dc27132012-09-13 15:50:24 +0000127 fIsRendering = false;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000128 }
129protected:
130 virtual const char* onGetName() {
131 return "grmemorypool_queue";
132 }
133
sugoi@google.com77472f02013-03-05 18:50:01 +0000134 virtual void onDraw(SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000135 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000136 A* objects[M];
mtklein@google.comc2897432013-09-10 19:23:38 +0000137 for (int i = 0; i < this->getLoops(); i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000138 uint32_t count = r.nextRangeU(0, M-1);
139 for (uint32_t i = 0; i < count; i++) {
140 objects[i] = new A;
141 }
142 for (uint32_t i = 0; i < count; i++) {
143 delete objects[i];
144 }
145 }
146 }
147
148private:
149 typedef SkBenchmark INHERITED;
150};
151
152///////////////////////////////////////////////////////////////////////////////
153
154static SkBenchmark* Fact1(void* p) { return new GrMemoryPoolBenchStack(p); }
155static SkBenchmark* Fact2(void* p) { return new GrMemoryPoolBenchRandom(p); }
156static SkBenchmark* Fact3(void* p) { return new GrMemoryPoolBenchQueue(p); }
157
158static BenchRegistry gReg01(Fact1);
159static BenchRegistry gReg02(Fact2);
160static BenchRegistry gReg03(Fact3);
161
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000162#endif