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: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 35 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 36 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 37 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 38 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 39 | protected: |
| 40 | virtual const char* onGetName() { |
| 41 | return "grmemorypool_stack"; |
| 42 | } |
| 43 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame^] | 44 | virtual void onDraw(const int loops, SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 45 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 46 | 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.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame^] | 54 | const int kSwitchThreshPeriod = loops / (2 * kMaxObjects); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 55 | int s = 0; |
| 56 | |
| 57 | int count = 0; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame^] | 58 | for (int i = 0; i < loops; i++, ++s) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 59 | if (kSwitchThreshPeriod == s) { |
| 60 | delThresh = -delThresh; |
| 61 | s = 0; |
| 62 | } |
| 63 | SkFixed del = r.nextSFixed1(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 64 | if (count && |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 65 | (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 | |
| 78 | private: |
| 79 | typedef SkBenchmark INHERITED; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * This benchmark creates objects and deletes them in random order |
| 84 | */ |
| 85 | class GrMemoryPoolBenchRandom : public SkBenchmark { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 86 | public: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 87 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 88 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 89 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 90 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 91 | protected: |
| 92 | virtual const char* onGetName() { |
| 93 | return "grmemorypool_random"; |
| 94 | } |
| 95 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame^] | 96 | virtual void onDraw(const int loops, SkCanvas*) { |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 97 | SkRandom r; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 98 | enum { |
| 99 | kMaxObjects = 4 * (1 << 10), |
| 100 | }; |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 101 | SkAutoTDelete<A> objects[kMaxObjects]; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame^] | 103 | for (int i = 0; i < loops; i++) { |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 104 | uint32_t idx = r.nextRangeU(0, kMaxObjects-1); |
| 105 | if (NULL == objects[idx].get()) { |
| 106 | objects[idx].reset(new A); |
| 107 | } else { |
reed@google.com | 9d1cff1 | 2013-04-18 18:43:26 +0000 | [diff] [blame] | 108 | objects[idx].free(); |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | typedef SkBenchmark INHERITED; |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * This benchmark creates objects and deletes them in queue order |
| 119 | */ |
| 120 | class GrMemoryPoolBenchQueue : public SkBenchmark { |
| 121 | enum { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 122 | M = 4 * (1 << 10), |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 123 | }; |
| 124 | public: |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 125 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 126 | return backend == kNonRendering_Backend; |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 127 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 128 | |
bsalomon@google.com | 4da34e3 | 2012-06-19 15:40:27 +0000 | [diff] [blame] | 129 | protected: |
| 130 | virtual const char* onGetName() { |
| 131 | return "grmemorypool_queue"; |
| 132 | } |
| 133 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame^] | 134 | virtual void onDraw(const int loops, 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]; |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame^] | 137 | for (int i = 0; i < loops; 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 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 154 | DEF_BENCH( return new GrMemoryPoolBenchStack(); ) |
| 155 | DEF_BENCH( return new GrMemoryPoolBenchRandom(); ) |
| 156 | DEF_BENCH( return new GrMemoryPoolBenchQueue(); ) |
commit-bot@chromium.org | 97b4b67 | 2013-09-26 19:23:03 +0000 | [diff] [blame] | 157 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 158 | #endif |