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 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 20 | struct 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 | }; |
| 28 | GrMemoryPool A::gPool(10 * (1 << 10), 10 * (1 << 10)); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * This benchmark creates and deletes objects in stack order |
| 32 | */ |
| 33 | class GrMemoryPoolBenchStack : public SkBenchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 34 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 35 | GrMemoryPoolBenchStack() { |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 36 | fIsRendering = false; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 37 | } |
| 38 | protected: |
| 39 | virtual const char* onGetName() { |
| 40 | return "grmemorypool_stack"; |
| 41 | } |
| 42 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 43 | virtual void onDraw(SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 44 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 45 | enum { |
| 46 | kMaxObjects = 4 * (1 << 10), |
| 47 | }; |
| 48 | A* objects[kMaxObjects]; |
| 49 | |
| 50 | // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise, |
| 51 | // we allocate. We start allocate-biased and ping-pong to delete-biased |
| 52 | SkFixed delThresh = -SK_FixedHalf; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 53 | const int kSwitchThreshPeriod = this->getLoops() / (2 * kMaxObjects); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 54 | int s = 0; |
| 55 | |
| 56 | int count = 0; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 57 | for (int i = 0; i < this->getLoops(); i++, ++s) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 58 | if (kSwitchThreshPeriod == s) { |
| 59 | delThresh = -delThresh; |
| 60 | s = 0; |
| 61 | } |
| 62 | SkFixed del = r.nextSFixed1(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 63 | if (count && |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 64 | (kMaxObjects == count || del < delThresh)) { |
| 65 | delete objects[count-1]; |
| 66 | --count; |
| 67 | } else { |
| 68 | objects[count] = new A; |
| 69 | ++count; |
| 70 | } |
| 71 | } |
| 72 | for (int i = 0; i < count; ++i) { |
| 73 | delete objects[i]; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | typedef SkBenchmark INHERITED; |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * This benchmark creates objects and deletes them in random order |
| 83 | */ |
| 84 | class GrMemoryPoolBenchRandom : public SkBenchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 85 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 86 | GrMemoryPoolBenchRandom() { |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 87 | fIsRendering = false; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 88 | } |
| 89 | protected: |
| 90 | virtual const char* onGetName() { |
| 91 | return "grmemorypool_random"; |
| 92 | } |
| 93 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 94 | virtual void onDraw(SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 95 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 96 | enum { |
| 97 | kMaxObjects = 4 * (1 << 10), |
| 98 | }; |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 99 | SkAutoTDelete<A> objects[kMaxObjects]; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 100 | |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 101 | for (int i = 0; i < this->getLoops(); i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 102 | uint32_t idx = r.nextRangeU(0, kMaxObjects-1); |
| 103 | if (NULL == objects[idx].get()) { |
| 104 | objects[idx].reset(new A); |
| 105 | } else { |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 106 | objects[idx].free(); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | typedef SkBenchmark INHERITED; |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * This benchmark creates objects and deletes them in queue order |
| 117 | */ |
| 118 | class GrMemoryPoolBenchQueue : public SkBenchmark { |
| 119 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 120 | M = 4 * (1 << 10), |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 121 | }; |
| 122 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 123 | GrMemoryPoolBenchQueue() { |
tomhudson@google.com | 9dc2713 | 2012-09-13 15:50:24 +0000 | [diff] [blame] | 124 | fIsRendering = false; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 125 | } |
| 126 | protected: |
| 127 | virtual const char* onGetName() { |
| 128 | return "grmemorypool_queue"; |
| 129 | } |
| 130 | |
sugoi@google.com | 77472f0 | 2013-03-05 18:50:01 +0000 | [diff] [blame] | 131 | virtual void onDraw(SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 132 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 133 | A* objects[M]; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 134 | for (int i = 0; i < this->getLoops(); i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 135 | uint32_t count = r.nextRangeU(0, M-1); |
| 136 | for (uint32_t i = 0; i < count; i++) { |
| 137 | objects[i] = new A; |
| 138 | } |
| 139 | for (uint32_t i = 0; i < count; i++) { |
| 140 | delete objects[i]; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | typedef SkBenchmark INHERITED; |
| 147 | }; |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 151 | DEF_BENCH( return new GrMemoryPoolBenchStack(); ) |
| 152 | DEF_BENCH( return new GrMemoryPoolBenchRandom(); ) |
| 153 | DEF_BENCH( return new GrMemoryPoolBenchQueue(); ) |
commit-bot@chromium.org | 97b4b67 | 2013-09-26 19:23:03 +0000 | [diff] [blame] | 154 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 155 | #endif |