joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 8 | #include "GrBatch.h" |
| 9 | |
| 10 | #include "GrMemoryPool.h" |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 11 | #include "SkSpinlock.h" |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 12 | |
| 13 | // TODO I noticed a small benefit to using a larger exclusive pool for batches. Its very small, |
| 14 | // but seems to be mostly consistent. There is a lot in flux right now, but we should really |
| 15 | // revisit this when batch is everywhere |
| 16 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 17 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 18 | // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on |
| 19 | // different threads. The GrContext is not used concurrently on different threads and there is a |
| 20 | // memory barrier between accesses of a context on different threads. Also, there may be multiple |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 21 | // GrContexts and those contexts may be in use concurrently on different threads. |
| 22 | namespace { |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 23 | SK_DECLARE_STATIC_SPINLOCK(gBatchSpinlock); |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 24 | class MemoryPoolAccessor { |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 25 | public: |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 26 | MemoryPoolAccessor() { gBatchSpinlock.acquire(); } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 27 | |
joshualitt | 23ac62c | 2015-03-30 09:53:47 -0700 | [diff] [blame] | 28 | ~MemoryPoolAccessor() { gBatchSpinlock.release(); } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 29 | |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 30 | GrMemoryPool* pool() const { |
| 31 | static GrMemoryPool gPool(16384, 16384); |
| 32 | return &gPool; |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 33 | } |
| 34 | }; |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 35 | } |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 36 | |
joshualitt | ca1f07e | 2015-08-07 08:11:19 -0700 | [diff] [blame] | 37 | int32_t GrBatch::gCurrBatchClassID = GrBatch::kIllegalBatchID; |
| 38 | |
| 39 | GrBATCH_SPEW(int32_t GrBatch::gCurrBatchUniqueID = GrBatch::kIllegalBatchID;) |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 40 | |
| 41 | void* GrBatch::operator new(size_t size) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 42 | return MemoryPoolAccessor().pool()->allocate(size); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void GrBatch::operator delete(void* target) { |
bsalomon | 5baedd6 | 2015-03-09 12:15:53 -0700 | [diff] [blame] | 46 | return MemoryPoolAccessor().pool()->release(target); |
joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame] | 47 | } |
bsalomon | b5238a7 | 2015-05-05 07:49:49 -0700 | [diff] [blame] | 48 | |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 49 | GrBatch::GrBatch(uint32_t classID) |
| 50 | : fClassID(classID) |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 51 | #if GR_BATCH_SPEW |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 52 | , fUniqueID(GenBatchID()) |
bsalomon | a387a11 | 2015-08-11 14:47:42 -0700 | [diff] [blame] | 53 | #endif |
| 54 | { |
| 55 | SkDEBUGCODE(fUsed = false;) |
| 56 | } |
| 57 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 58 | GrBatch::~GrBatch() {} |