blob: 21f686d89d289cba8bcc29e737a7f8bd442cd083 [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:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000035 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
36 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000037 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038
bsalomon@google.com4da34e32012-06-19 15:40:27 +000039protected:
40 virtual const char* onGetName() {
41 return "grmemorypool_stack";
42 }
43
commit-bot@chromium.org33614712013-12-03 18:17:16 +000044 virtual void onDraw(const int loops, SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000045 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000046 enum {
47 kMaxObjects = 4 * (1 << 10),
48 };
49 A* objects[kMaxObjects];
50
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;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000054 const int kSwitchThreshPeriod = loops / (2 * kMaxObjects);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000055 int s = 0;
56
57 int count = 0;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 for (int i = 0; i < loops; i++, ++s) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000059 if (kSwitchThreshPeriod == s) {
60 delThresh = -delThresh;
61 s = 0;
62 }
63 SkFixed del = r.nextSFixed1();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064 if (count &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +000065 (kMaxObjects == count || del < delThresh)) {
66 delete objects[count-1];
67 --count;
68 } else {
69 objects[count] = new A;
70 ++count;
71 }
72 }
73 for (int i = 0; i < count; ++i) {
74 delete objects[i];
75 }
76 }
77
78private:
79 typedef SkBenchmark INHERITED;
80};
81
82/**
83 * This benchmark creates objects and deletes them in random order
84 */
85class GrMemoryPoolBenchRandom : public SkBenchmark {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000086public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000087 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
88 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000089 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000090
bsalomon@google.com4da34e32012-06-19 15:40:27 +000091protected:
92 virtual const char* onGetName() {
93 return "grmemorypool_random";
94 }
95
commit-bot@chromium.org33614712013-12-03 18:17:16 +000096 virtual void onDraw(const int loops, SkCanvas*) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000097 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000098 enum {
99 kMaxObjects = 4 * (1 << 10),
100 };
reed@google.com9d1cff12013-04-18 18:43:26 +0000101 SkAutoTDelete<A> objects[kMaxObjects];
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000102
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000103 for (int i = 0; i < loops; i++) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000104 uint32_t idx = r.nextRangeU(0, kMaxObjects-1);
105 if (NULL == objects[idx].get()) {
106 objects[idx].reset(new A);
107 } else {
reed@google.com9d1cff12013-04-18 18:43:26 +0000108 objects[idx].free();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000109 }
110 }
111 }
112
113private:
114 typedef SkBenchmark INHERITED;
115};
116
117/**
118 * This benchmark creates objects and deletes them in queue order
119 */
120class GrMemoryPoolBenchQueue : public SkBenchmark {
121 enum {
mtklein@google.comc2897432013-09-10 19:23:38 +0000122 M = 4 * (1 << 10),
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000123 };
124public:
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000125 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
126 return backend == kNonRendering_Backend;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000127 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000128
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000129protected:
130 virtual const char* onGetName() {
131 return "grmemorypool_queue";
132 }
133
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000134 virtual void onDraw(const int loops, 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];
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000137 for (int i = 0; i < loops; 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
mtklein@google.com410e6e82013-09-13 19:52:27 +0000154DEF_BENCH( return new GrMemoryPoolBenchStack(); )
155DEF_BENCH( return new GrMemoryPoolBenchRandom(); )
156DEF_BENCH( return new GrMemoryPoolBenchQueue(); )
commit-bot@chromium.org97b4b672013-09-26 19:23:03 +0000157
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000158#endif