Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Functions
GrMemoryPoolTest.cpp File Reference
#include "include/private/base/SkTArray.h"
#include "include/private/base/SkTDArray.h"
#include "src/base/SkRandom.h"
#include "src/gpu/ganesh/GrMemoryPool.h"
#include "tests/Test.h"
#include <array>
#include <cstddef>
#include <cstdint>
#include <memory>

Go to the source code of this file.

Classes

class  A
 
class  B
 
class  C
 
class  D
 
class  E
 
class  Rec
 
class  AutoPoolReleaser
 

Functions

 DEF_TEST (GrMemoryPool, reporter)
 
 DEF_TEST (GrMemoryPoolAPI, reporter)
 

Function Documentation

◆ DEF_TEST() [1/2]

DEF_TEST ( GrMemoryPool  ,
reporter   
)

Definition at line 181 of file GrMemoryPoolTest.cpp.

181 {
182 // prealloc and min alloc sizes for the pool
183 static const size_t gSizes[][2] = {
184 {0, 0},
185 {10 * sizeof(A), 20 * sizeof(A)},
186 {100 * sizeof(A), 100 * sizeof(A)},
187 {500 * sizeof(A), 500 * sizeof(A)},
188 {10000 * sizeof(A), 0},
189 {1, 100 * sizeof(A)},
190 };
191
192 // different percentages of creation vs deletion
193 static const float gCreateFraction[] = {1.f, .95f, 0.75f, .5f};
194 // number of create/destroys per test
195 static const int kNumIters = 20000;
196 // check that all the values stored in A objects are correct after this
197 // number of iterations
198 static const int kCheckPeriod = 500;
199
200 SkRandom r;
201 for (size_t s = 0; s < std::size(gSizes); ++s) {
202 A::SetAllocator(gSizes[s][0], gSizes[s][1]);
204 for (size_t c = 0; c < std::size(gCreateFraction); ++c) {
205 SkTDArray<Rec> instanceRecs;
206 for (int i = 0; i < kNumIters; ++i) {
207 float createOrDestroy = r.nextUScalar1();
208 if (createOrDestroy < gCreateFraction[c] ||
209 0 == instanceRecs.size()) {
210 Rec* rec = instanceRecs.append();
211 rec->fInstance = A::Create(&r);
212 rec->fValue = static_cast<int>(r.nextU());
213 rec->fInstance->setValues(rec->fValue);
214 } else {
215 int d = r.nextRangeU(0, instanceRecs.size() - 1);
216 Rec& rec = instanceRecs[d];
217 REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
218 delete rec.fInstance;
219 instanceRecs.removeShuffle(d);
220 }
221 if (0 == i % kCheckPeriod) {
223 for (Rec& rec : instanceRecs) {
224 REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
225 }
226 }
227 }
228 for (Rec& rec : instanceRecs) {
229 REPORTER_ASSERT(reporter, rec.fInstance->checkValues(rec.fValue));
230 delete rec.fInstance;
231 }
232 }
233 }
234}
reporter
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static A * Create(SkRandom *r)
static void ValidatePool()
static void SetAllocator(size_t preallocSize, size_t minAllocSize)
uint32_t nextU()
Definition SkRandom.h:42
SkScalar nextUScalar1()
Definition SkRandom.h:101
uint32_t nextRangeU(uint32_t min, uint32_t max)
Definition SkRandom.h:80
int size() const
Definition SkTDArray.h:138
T * append()
Definition SkTDArray.h:191
void removeShuffle(int index)
Definition SkTDArray.h:214
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
struct MyStruct s

◆ DEF_TEST() [2/2]

DEF_TEST ( GrMemoryPoolAPI  ,
reporter   
)

Definition at line 255 of file GrMemoryPoolTest.cpp.

255 {
256 constexpr size_t kSmallestMinAllocSize = GrMemoryPool::kMinAllocationSize;
257
258 // Allocates memory until pool adds a new block (pool->size() changes).
259 auto allocateMemory = [](GrMemoryPool& pool, AutoPoolReleaser& r) {
260 size_t origPoolSize = pool.size();
261 while (pool.size() == origPoolSize) {
262 r.add(pool.allocate(31));
263 }
264 };
265
266 // Effective prealloc space capacity is >= kMinAllocationSize.
267 {
268 auto pool = GrMemoryPool::Make(0, 0);
269 REPORTER_ASSERT(reporter, pool->preallocSize() == kSmallestMinAllocSize);
270 }
271
272 // Effective block size capacity >= kMinAllocationSize.
273 {
274 auto pool = GrMemoryPool::Make(kSmallestMinAllocSize, kSmallestMinAllocSize / 2);
276
277 allocateMemory(*pool, r);
278 REPORTER_ASSERT(reporter, pool->size() == kSmallestMinAllocSize);
279 }
280
281 // Pool allocates exactly preallocSize on creation.
282 {
283 constexpr size_t kPreallocSize = kSmallestMinAllocSize * 5;
284 auto pool = GrMemoryPool::Make(kPreallocSize, 0);
285 REPORTER_ASSERT(reporter, pool->preallocSize() == kPreallocSize);
286 }
287
288 // Pool allocates exactly minAllocSize when it expands.
289 {
290 constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 7;
291 auto pool = GrMemoryPool::Make(0, kMinAllocSize);
293 REPORTER_ASSERT(reporter, pool->size() == 0);
294
295 allocateMemory(*pool, r);
296 REPORTER_ASSERT(reporter, pool->size() == kMinAllocSize);
297
298 allocateMemory(*pool, r);
299 REPORTER_ASSERT(reporter, pool->size() == 2 * kMinAllocSize);
300 }
301
302 // When asked to allocate amount > minAllocSize, pool allocates larger block
303 // to accommodate all internal structures.
304 {
305 constexpr size_t kMinAllocSize = kSmallestMinAllocSize * 2;
306 auto pool = GrMemoryPool::Make(kSmallestMinAllocSize, kMinAllocSize);
308
309 REPORTER_ASSERT(reporter, pool->size() == 0);
310
311 constexpr size_t hugeSize = 10 * kMinAllocSize;
312 r.add(pool->allocate(hugeSize));
313 REPORTER_ASSERT(reporter, pool->size() > hugeSize);
314
315 // Block size allocated to accommodate huge request doesn't include any extra
316 // space, so next allocation request allocates a new block.
317 size_t hugeBlockSize = pool->size();
318 r.add(pool->allocate(0));
319 REPORTER_ASSERT(reporter, pool->size() == hugeBlockSize + kMinAllocSize);
320 }
321}
AutoreleasePool pool
static std::unique_ptr< GrMemoryPool > Make(size_t preallocSize, size_t minAllocSize)
static constexpr size_t kMinAllocationSize