blob: 19c19ffcf1d05d1d988a4c6172ff507c05a811c0 [file] [log] [blame]
joshualitt23ac62c2015-03-30 09:53:47 -07001/*
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
joshualitt4d8da812015-01-28 12:53:54 -08008#include "GrBatch.h"
9
10#include "GrMemoryPool.h"
joshualitt23ac62c2015-03-30 09:53:47 -070011#include "SkSpinlock.h"
joshualitt4d8da812015-01-28 12:53:54 -080012
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
bsalomon5baedd62015-03-09 12:15:53 -070017
joshualitt23ac62c2015-03-30 09:53:47 -070018// 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
bsalomon5baedd62015-03-09 12:15:53 -070021// GrContexts and those contexts may be in use concurrently on different threads.
22namespace {
joshualitt23ac62c2015-03-30 09:53:47 -070023SK_DECLARE_STATIC_SPINLOCK(gBatchSpinlock);
bsalomon5baedd62015-03-09 12:15:53 -070024class MemoryPoolAccessor {
joshualitt4d8da812015-01-28 12:53:54 -080025public:
joshualitt23ac62c2015-03-30 09:53:47 -070026 MemoryPoolAccessor() { gBatchSpinlock.acquire(); }
joshualitt4d8da812015-01-28 12:53:54 -080027
joshualitt23ac62c2015-03-30 09:53:47 -070028 ~MemoryPoolAccessor() { gBatchSpinlock.release(); }
joshualitt4d8da812015-01-28 12:53:54 -080029
bsalomon5baedd62015-03-09 12:15:53 -070030 GrMemoryPool* pool() const {
31 static GrMemoryPool gPool(16384, 16384);
32 return &gPool;
joshualitt4d8da812015-01-28 12:53:54 -080033 }
34};
bsalomon5baedd62015-03-09 12:15:53 -070035}
joshualitt4d8da812015-01-28 12:53:54 -080036
joshualittca1f07e2015-08-07 08:11:19 -070037int32_t GrBatch::gCurrBatchClassID = GrBatch::kIllegalBatchID;
38
39GrBATCH_SPEW(int32_t GrBatch::gCurrBatchUniqueID = GrBatch::kIllegalBatchID;)
joshualitt4d8da812015-01-28 12:53:54 -080040
41void* GrBatch::operator new(size_t size) {
bsalomon5baedd62015-03-09 12:15:53 -070042 return MemoryPoolAccessor().pool()->allocate(size);
joshualitt4d8da812015-01-28 12:53:54 -080043}
44
45void GrBatch::operator delete(void* target) {
bsalomon5baedd62015-03-09 12:15:53 -070046 return MemoryPoolAccessor().pool()->release(target);
joshualitt4d8da812015-01-28 12:53:54 -080047}
bsalomonb5238a72015-05-05 07:49:49 -070048
reed1b55a962015-09-17 20:16:13 -070049GrBatch::GrBatch(uint32_t classID)
50 : fClassID(classID)
bsalomona387a112015-08-11 14:47:42 -070051#if GR_BATCH_SPEW
reed1b55a962015-09-17 20:16:13 -070052 , fUniqueID(GenBatchID())
bsalomona387a112015-08-11 14:47:42 -070053#endif
54{
55 SkDEBUGCODE(fUsed = false;)
56}
57
bsalomonabd30f52015-08-13 13:34:48 -070058GrBatch::~GrBatch() {}