bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 1 | /* |
| 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.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 8 | // This tests a Gr class |
| 9 | #if SK_SUPPORT_GPU |
| 10 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 11 | #include "GrMemoryPool.h" |
| 12 | #include "SkBenchmark.h" |
| 13 | #include "SkRandom.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 14 | #include "SkTDArray.h" |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 15 | #include "SkTemplates.h" |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 16 | |
| 17 | // change this to 0 to compare GrMemoryPool to default new / delete |
| 18 | #define OVERRIDE_NEW 1 |
| 19 | |
| 20 | namespace { |
| 21 | struct 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 | }; |
| 29 | GrMemoryPool A::gPool(10 * (1 << 10), 10 * (1 << 10)); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * This benchmark creates and deletes objects in stack order |
| 35 | */ |
| 36 | class GrMemoryPoolBenchStack : public SkBenchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 37 | public: |
| 38 | GrMemoryPoolBenchStack(void* param) : INHERITED(param) { |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 39 | fIsRendering = false; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 40 | } |
| 41 | protected: |
| 42 | virtual const char* onGetName() { |
| 43 | return "grmemorypool_stack"; |
| 44 | } |
| 45 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 46 | virtual void onDraw(SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 47 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 48 | 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.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 56 | const int kSwitchThreshPeriod = this->getLoops() / (2 * kMaxObjects); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 57 | int s = 0; |
| 58 | |
| 59 | int count = 0; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 60 | for (int i = 0; i < this->getLoops(); i++, ++s) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 61 | if (kSwitchThreshPeriod == s) { |
| 62 | delThresh = -delThresh; |
| 63 | s = 0; |
| 64 | } |
| 65 | SkFixed del = r.nextSFixed1(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 66 | if (count && |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 67 | (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 | |
| 80 | private: |
| 81 | typedef SkBenchmark INHERITED; |
| 82 | }; |
| 83 | |
| 84 | /** |
| 85 | * This benchmark creates objects and deletes them in random order |
| 86 | */ |
| 87 | class GrMemoryPoolBenchRandom : public SkBenchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 88 | public: |
| 89 | GrMemoryPoolBenchRandom(void* param) : INHERITED(param) { |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 90 | fIsRendering = false; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 91 | } |
| 92 | protected: |
| 93 | virtual const char* onGetName() { |
| 94 | return "grmemorypool_random"; |
| 95 | } |
| 96 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 97 | virtual void onDraw(SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 98 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 99 | enum { |
| 100 | kMaxObjects = 4 * (1 << 10), |
| 101 | }; |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 102 | SkAutoTDelete<A> objects[kMaxObjects]; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 103 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 104 | for (int i = 0; i < this->getLoops(); i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 105 | uint32_t idx = r.nextRangeU(0, kMaxObjects-1); |
| 106 | if (NULL == objects[idx].get()) { |
| 107 | objects[idx].reset(new A); |
| 108 | } else { |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 109 | objects[idx].free(); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | typedef SkBenchmark INHERITED; |
| 116 | }; |
| 117 | |
| 118 | /** |
| 119 | * This benchmark creates objects and deletes them in queue order |
| 120 | */ |
| 121 | class GrMemoryPoolBenchQueue : public SkBenchmark { |
| 122 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 123 | M = 4 * (1 << 10), |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 124 | }; |
| 125 | public: |
| 126 | GrMemoryPoolBenchQueue(void* param) : INHERITED(param) { |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 127 | fIsRendering = false; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 128 | } |
| 129 | protected: |
| 130 | virtual const char* onGetName() { |
| 131 | return "grmemorypool_queue"; |
| 132 | } |
| 133 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 134 | virtual void onDraw(SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 135 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 136 | A* objects[M]; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame^] | 137 | for (int i = 0; i < this->getLoops(); i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 138 | 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 | |
| 148 | private: |
| 149 | typedef SkBenchmark INHERITED; |
| 150 | }; |
| 151 | |
| 152 | /////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | static SkBenchmark* Fact1(void* p) { return new GrMemoryPoolBenchStack(p); } |
| 155 | static SkBenchmark* Fact2(void* p) { return new GrMemoryPoolBenchRandom(p); } |
| 156 | static SkBenchmark* Fact3(void* p) { return new GrMemoryPoolBenchQueue(p); } |
| 157 | |
| 158 | static BenchRegistry gReg01(Fact1); |
| 159 | static BenchRegistry gReg02(Fact2); |
| 160 | static BenchRegistry gReg03(Fact3); |
| 161 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 162 | #endif |