blob: 122075463fa42f07f259bf7a4e3f6a22e970b6d8 [file] [log] [blame]
bsalomon@google.com4da34e32012-06-19 15:40:27 +00001/*
2 * Copyright 2011 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/private/SkTArray.h"
9#include "include/private/SkTDArray.h"
10#include "include/private/SkTemplates.h"
11#include "include/utils/SkRandom.h"
12#include "src/gpu/GrMemoryPool.h"
13#include "tests/Test.h"
bsalomon@google.com4da34e32012-06-19 15:40:27 +000014
bsalomon@google.com4da34e32012-06-19 15:40:27 +000015// A is the top of an inheritance tree of classes that overload op new and
16// and delete to use a GrMemoryPool. The objects have values of different types
17// that can be set and checked.
18class A {
19public:
Mike Kleinfc6c37b2016-09-27 09:34:10 -040020 A() {}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000021 virtual void setValues(int v) {
Brian Osman50ea3c02019-02-04 10:01:53 -050022 fChar = static_cast<char>(v & 0xFF);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000023 }
24 virtual bool checkValues(int v) {
Brian Osman50ea3c02019-02-04 10:01:53 -050025 return fChar == static_cast<char>(v & 0xFF);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000026 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040027 virtual ~A() {}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000028
29 void* operator new(size_t size) {
Brian Salomon6986c652019-12-12 10:58:47 -050030 if (!gPool) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000031 return ::operator new(size);
32 } else {
33 return gPool->allocate(size);
34 }
35 }
36
37 void operator delete(void* p) {
Brian Salomon6986c652019-12-12 10:58:47 -050038 if (!gPool) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +000039 ::operator delete(p);
40 } else {
41 return gPool->release(p);
42 }
43 }
44
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000045 static A* Create(SkRandom* r);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000046
47 static void SetAllocator(size_t preallocSize, size_t minAllocSize) {
Brian Salomon6986c652019-12-12 10:58:47 -050048 gPool = GrMemoryPool::Make(preallocSize, minAllocSize);
bsalomon@google.com4da34e32012-06-19 15:40:27 +000049 }
50
Brian Salomon6986c652019-12-12 10:58:47 -050051 static void ResetAllocator() { gPool.reset(); }
bsalomon@google.com4da34e32012-06-19 15:40:27 +000052
Michael Ludwigcd019792020-03-17 10:14:48 -040053 static void ValidatePool() {
54#ifdef SK_DEBUG
55 gPool->validate();
56#endif
57 }
58
bsalomon@google.com4da34e32012-06-19 15:40:27 +000059private:
Ben Wagner145dbcd2016-11-03 14:40:50 -040060 static std::unique_ptr<GrMemoryPool> gPool;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000061 char fChar;
62};
commit-bot@chromium.orgab1c1382013-12-05 12:08:12 +000063
Ben Wagner145dbcd2016-11-03 14:40:50 -040064std::unique_ptr<GrMemoryPool> A::gPool;
bsalomon@google.com4da34e32012-06-19 15:40:27 +000065
66class B : public A {
67public:
Mike Kleinfc6c37b2016-09-27 09:34:10 -040068 B() {}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000069 virtual void setValues(int v) {
70 fDouble = static_cast<double>(v);
71 this->INHERITED::setValues(v);
72 }
73 virtual bool checkValues(int v) {
74 return fDouble == static_cast<double>(v) &&
75 this->INHERITED::checkValues(v);
76 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040077 virtual ~B() {}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000078
79private:
80 double fDouble;
81
82 typedef A INHERITED;
83};
84
85class C : public A {
86public:
Mike Kleinfc6c37b2016-09-27 09:34:10 -040087 C() {}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000088 virtual void setValues(int v) {
89 fInt64 = static_cast<int64_t>(v);
90 this->INHERITED::setValues(v);
91 }
92 virtual bool checkValues(int v) {
93 return fInt64 == static_cast<int64_t>(v) &&
94 this->INHERITED::checkValues(v);
95 }
Mike Kleinfc6c37b2016-09-27 09:34:10 -040096 virtual ~C() {}
bsalomon@google.com4da34e32012-06-19 15:40:27 +000097
98private:
99 int64_t fInt64;
100
101 typedef A INHERITED;
102};
103
104// D derives from C and owns a dynamically created B
105class D : public C {
106public:
107 D() {
108 fB = new B();
109 }
110 virtual void setValues(int v) {
bsalomonebc1c102015-08-06 17:33:16 -0700111 fVoidStar = reinterpret_cast<void*>(static_cast<intptr_t>(v));
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000112 this->INHERITED::setValues(v);
113 fB->setValues(v);
114 }
115 virtual bool checkValues(int v) {
bsalomonebc1c102015-08-06 17:33:16 -0700116 return fVoidStar == reinterpret_cast<void*>(static_cast<intptr_t>(v)) &&
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000117 fB->checkValues(v) &&
118 this->INHERITED::checkValues(v);
119 }
120 virtual ~D() {
121 delete fB;
122 }
123private:
124 void* fVoidStar;
125 B* fB;
126
127 typedef C INHERITED;
128};
129
130class E : public A {
131public:
132 E() {}
133 virtual void setValues(int v) {
134 for (size_t i = 0; i < SK_ARRAY_COUNT(fIntArray); ++i) {
135 fIntArray[i] = v;
136 }
137 this->INHERITED::setValues(v);
138 }
139 virtual bool checkValues(int v) {
140 bool ok = true;
141 for (size_t i = 0; ok && i < SK_ARRAY_COUNT(fIntArray); ++i) {
142 if (fIntArray[i] != v) {
143 ok = false;
144 }
145 }
146 return ok && this->INHERITED::checkValues(v);
147 }
148 virtual ~E() {}
149private:
150 int fIntArray[20];
151
152 typedef A INHERITED;
153};
154
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000155A* A::Create(SkRandom* r) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000156 switch (r->nextRangeU(0, 4)) {
157 case 0:
158 return new A;
159 case 1:
160 return new B;
161 case 2:
162 return new C;
163 case 3:
164 return new D;
165 case 4:
166 return new E;
167 default:
168 // suppress warning
halcanary96fcdcc2015-08-27 07:41:13 -0700169 return nullptr;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000170 }
171}
commit-bot@chromium.orgddf94cf2013-10-12 17:25:17 +0000172
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000173struct Rec {
174 A* fInstance;
175 int fValue;
176};
177
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000178DEF_TEST(GrMemoryPool, reporter) {
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000179 // prealloc and min alloc sizes for the pool
180 static const size_t gSizes[][2] = {
181 {0, 0},
182 {10 * sizeof(A), 20 * sizeof(A)},
183 {100 * sizeof(A), 100 * sizeof(A)},
184 {500 * sizeof(A), 500 * sizeof(A)},
185 {10000 * sizeof(A), 0},
186 {1, 100 * sizeof(A)},
187 };
Michael Ludwigcd019792020-03-17 10:14:48 -0400188
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000189 // different percentages of creation vs deletion
190 static const float gCreateFraction[] = {1.f, .95f, 0.75f, .5f};
191 // number of create/destroys per test
192 static const int kNumIters = 20000;
193 // check that all the values stored in A objects are correct after this
194 // number of iterations
195 static const int kCheckPeriod = 500;
196
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000197 SkRandom r;
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000198 for (size_t s = 0; s < SK_ARRAY_COUNT(gSizes); ++s) {
199 A::SetAllocator(gSizes[s][0], gSizes[s][1]);
Michael Ludwigcd019792020-03-17 10:14:48 -0400200 A::ValidatePool();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000201 for (size_t c = 0; c < SK_ARRAY_COUNT(gCreateFraction); ++c) {
202 SkTDArray<Rec> instanceRecs;
203 for (int i = 0; i < kNumIters; ++i) {
204 float createOrDestroy = r.nextUScalar1();
205 if (createOrDestroy < gCreateFraction[c] ||
206 0 == instanceRecs.count()) {
207 Rec* rec = instanceRecs.append();
208 rec->fInstance = A::Create(&r);
209 rec->fValue = static_cast<int>(r.nextU());
210 rec->fInstance->setValues(rec->fValue);
211 } else {
212 int d = r.nextRangeU(0, instanceRecs.count() - 1);
213 Rec& rec = instanceRecs[d];
214 REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
215 delete rec.fInstance;
216 instanceRecs.removeShuffle(d);
217 }
218 if (0 == i % kCheckPeriod) {
Michael Ludwigcd019792020-03-17 10:14:48 -0400219 A::ValidatePool();
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000220 for (int r = 0; r < instanceRecs.count(); ++r) {
221 Rec& rec = instanceRecs[r];
222 REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
223 }
224 }
225 }
226 for (int i = 0; i < instanceRecs.count(); ++i) {
227 Rec& rec = instanceRecs[i];
228 REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
229 delete rec.fInstance;
230 }
bsalomon@google.com4da34e32012-06-19 15:40:27 +0000231 }
232 }
233}
234
dskibae4cd0062016-11-29 06:50:35 -0800235// GrMemoryPool requires that it's empty at the point of destruction. This helps
236// achieving that by releasing all added memory in the destructor.
237class AutoPoolReleaser {
238public:
239 AutoPoolReleaser(GrMemoryPool& pool): fPool(pool) {
240 }
241 ~AutoPoolReleaser() {
242 for (void* ptr: fAllocated) {
243 fPool.release(ptr);
244 }
245 }
246 void add(void* ptr) {
247 fAllocated.push_back(ptr);
248 }
249private:
250 GrMemoryPool& fPool;
251 SkTArray<void*> fAllocated;
252};
253
254DEF_TEST(GrMemoryPoolAPI, reporter) {
Brian Salomon6986c652019-12-12 10:58:47 -0500255 constexpr size_t kSmallestMinAllocSize = GrMemoryPool::kMinAllocationSize;
dskibae4cd0062016-11-29 06:50:35 -0800256
Brian Salomon6986c652019-12-12 10:58:47 -0500257 // Allocates memory until pool adds a new block (pool->size() changes).
dskibae4cd0062016-11-29 06:50:35 -0800258 auto allocateMemory = [](GrMemoryPool& pool, AutoPoolReleaser& r) {
259 size_t origPoolSize = pool.size();
260 while (pool.size() == origPoolSize) {
261 r.add(pool.allocate(31));
262 }
263 };
264
Brian Salomon6986c652019-12-12 10:58:47 -0500265 // Effective prealloc space capacity is >= kMinAllocationSize.
dskibae4cd0062016-11-29 06:50:35 -0800266 {
Brian Salomon6986c652019-12-12 10:58:47 -0500267 auto pool = GrMemoryPool::Make(0, 0);
268 REPORTER_ASSERT(reporter, pool->preallocSize() == kSmallestMinAllocSize);
dskibae4cd0062016-11-29 06:50:35 -0800269 }
270
Brian Salomon6986c652019-12-12 10:58:47 -0500271 // Effective block size capacity >= kMinAllocationSize.
dskibae4cd0062016-11-29 06:50:35 -0800272 {
Brian Salomon6986c652019-12-12 10:58:47 -0500273 auto pool = GrMemoryPool::Make(kSmallestMinAllocSize, kSmallestMinAllocSize / 2);
274 AutoPoolReleaser r(*pool);
dskibae4cd0062016-11-29 06:50:35 -0800275
Brian Salomon6986c652019-12-12 10:58:47 -0500276 allocateMemory(*pool, r);
277 REPORTER_ASSERT(reporter, pool->size() == kSmallestMinAllocSize);
dskibae4cd0062016-11-29 06:50:35 -0800278 }
279
280 // Pool allocates exactly preallocSize on creation.
281 {
282 constexpr size_t kPreallocSize = kSmallestMinAllocSize * 5;
Brian Salomon6986c652019-12-12 10:58:47 -0500283 auto pool = GrMemoryPool::Make(kPreallocSize, 0);
284 REPORTER_ASSERT(reporter, pool->preallocSize() == kPreallocSize);
dskibae4cd0062016-11-29 06:50:35 -0800285 }
286
287 // Pool allocates exactly minAllocSize when it expands.
288 {
289 constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 7;
Brian Salomon6986c652019-12-12 10:58:47 -0500290 auto pool = GrMemoryPool::Make(0, kMinAllocSize);
291 AutoPoolReleaser r(*pool);
Michael Ludwigcd019792020-03-17 10:14:48 -0400292 REPORTER_ASSERT(reporter, pool->size() == 0);
dskibae4cd0062016-11-29 06:50:35 -0800293
Brian Salomon6986c652019-12-12 10:58:47 -0500294 allocateMemory(*pool, r);
295 REPORTER_ASSERT(reporter, pool->size() == kMinAllocSize);
dskibae4cd0062016-11-29 06:50:35 -0800296
Brian Salomon6986c652019-12-12 10:58:47 -0500297 allocateMemory(*pool, r);
298 REPORTER_ASSERT(reporter, pool->size() == 2 * kMinAllocSize);
dskibae4cd0062016-11-29 06:50:35 -0800299 }
300
301 // When asked to allocate amount > minAllocSize, pool allocates larger block
302 // to accommodate all internal structures.
303 {
304 constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 2;
Brian Salomon6986c652019-12-12 10:58:47 -0500305 auto pool = GrMemoryPool::Make(kSmallestMinAllocSize, kMinAllocSize);
306 AutoPoolReleaser r(*pool);
dskibae4cd0062016-11-29 06:50:35 -0800307
Brian Salomon6986c652019-12-12 10:58:47 -0500308 REPORTER_ASSERT(reporter, pool->size() == 0);
dskibae4cd0062016-11-29 06:50:35 -0800309
310 constexpr size_t hugeSize = 10 * kMinAllocSize;
Brian Salomon6986c652019-12-12 10:58:47 -0500311 r.add(pool->allocate(hugeSize));
312 REPORTER_ASSERT(reporter, pool->size() > hugeSize);
dskibae4cd0062016-11-29 06:50:35 -0800313
314 // Block size allocated to accommodate huge request doesn't include any extra
315 // space, so next allocation request allocates a new block.
Brian Salomon6986c652019-12-12 10:58:47 -0500316 size_t hugeBlockSize = pool->size();
317 r.add(pool->allocate(0));
318 REPORTER_ASSERT(reporter, pool->size() == hugeBlockSize + kMinAllocSize);
dskibae4cd0062016-11-29 06:50:35 -0800319 }
320}