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" |
| 14 | #include "SkTScopedPtr.h" |
| 15 | #include "SkTDArray.h" |
| 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 { |
| 37 | enum { |
| 38 | N = SkBENCHLOOP(5 * (1 << 20)), |
| 39 | }; |
| 40 | public: |
| 41 | GrMemoryPoolBenchStack(void* param) : INHERITED(param) { |
| 42 | } |
| 43 | protected: |
| 44 | virtual const char* onGetName() { |
| 45 | return "grmemorypool_stack"; |
| 46 | } |
| 47 | |
| 48 | virtual void onDraw(SkCanvas* canvas) { |
| 49 | SkRandom r; |
| 50 | enum { |
| 51 | kMaxObjects = 4 * (1 << 10), |
| 52 | }; |
| 53 | A* objects[kMaxObjects]; |
| 54 | |
| 55 | // We delete if a random [-1, 1] fixed pt is < the thresh. Otherwise, |
| 56 | // we allocate. We start allocate-biased and ping-pong to delete-biased |
| 57 | SkFixed delThresh = -SK_FixedHalf; |
| 58 | enum { |
| 59 | kSwitchThreshPeriod = N / (2 * kMaxObjects), |
| 60 | }; |
| 61 | int s = 0; |
| 62 | |
| 63 | int count = 0; |
| 64 | for (int i = 0; i < N; i++, ++s) { |
| 65 | if (kSwitchThreshPeriod == s) { |
| 66 | delThresh = -delThresh; |
| 67 | s = 0; |
| 68 | } |
| 69 | SkFixed del = r.nextSFixed1(); |
| 70 | if (count && |
| 71 | (kMaxObjects == count || del < delThresh)) { |
| 72 | delete objects[count-1]; |
| 73 | --count; |
| 74 | } else { |
| 75 | objects[count] = new A; |
| 76 | ++count; |
| 77 | } |
| 78 | } |
| 79 | for (int i = 0; i < count; ++i) { |
| 80 | delete objects[i]; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private: |
| 85 | typedef SkBenchmark INHERITED; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * This benchmark creates objects and deletes them in random order |
| 90 | */ |
| 91 | class GrMemoryPoolBenchRandom : public SkBenchmark { |
| 92 | enum { |
| 93 | N = SkBENCHLOOP(5 * (1 << 20)), |
| 94 | }; |
| 95 | public: |
| 96 | GrMemoryPoolBenchRandom(void* param) : INHERITED(param) { |
| 97 | } |
| 98 | protected: |
| 99 | virtual const char* onGetName() { |
| 100 | return "grmemorypool_random"; |
| 101 | } |
| 102 | |
| 103 | virtual void onDraw(SkCanvas* canvas) { |
| 104 | SkRandom r; |
| 105 | enum { |
| 106 | kMaxObjects = 4 * (1 << 10), |
| 107 | }; |
| 108 | SkTScopedPtr<A> objects[kMaxObjects]; |
| 109 | |
| 110 | for (int i = 0; i < N; i++) { |
| 111 | uint32_t idx = r.nextRangeU(0, kMaxObjects-1); |
| 112 | if (NULL == objects[idx].get()) { |
| 113 | objects[idx].reset(new A); |
| 114 | } else { |
| 115 | objects[idx].reset(NULL); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | typedef SkBenchmark INHERITED; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * This benchmark creates objects and deletes them in queue order |
| 126 | */ |
| 127 | class GrMemoryPoolBenchQueue : public SkBenchmark { |
| 128 | enum { |
| 129 | N = SkBENCHLOOP((1 << 10)), |
| 130 | M = SkBENCHLOOP(4 * (1 << 10)), |
| 131 | }; |
| 132 | public: |
| 133 | GrMemoryPoolBenchQueue(void* param) : INHERITED(param) { |
| 134 | } |
| 135 | protected: |
| 136 | virtual const char* onGetName() { |
| 137 | return "grmemorypool_queue"; |
| 138 | } |
| 139 | |
| 140 | virtual void onDraw(SkCanvas* canvas) { |
| 141 | SkRandom r; |
| 142 | A* objects[M]; |
| 143 | for (int i = 0; i < N; i++) { |
| 144 | uint32_t count = r.nextRangeU(0, M-1); |
| 145 | for (uint32_t i = 0; i < count; i++) { |
| 146 | objects[i] = new A; |
| 147 | } |
| 148 | for (uint32_t i = 0; i < count; i++) { |
| 149 | delete objects[i]; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | typedef SkBenchmark INHERITED; |
| 156 | }; |
| 157 | |
| 158 | /////////////////////////////////////////////////////////////////////////////// |
| 159 | |
| 160 | static SkBenchmark* Fact1(void* p) { return new GrMemoryPoolBenchStack(p); } |
| 161 | static SkBenchmark* Fact2(void* p) { return new GrMemoryPoolBenchRandom(p); } |
| 162 | static SkBenchmark* Fact3(void* p) { return new GrMemoryPoolBenchQueue(p); } |
| 163 | |
| 164 | static BenchRegistry gReg01(Fact1); |
| 165 | static BenchRegistry gReg02(Fact2); |
| 166 | static BenchRegistry gReg03(Fact3); |
| 167 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame^] | 168 | #endif |