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 
8 #include "GrOp.h"
9 
10 #include "GrMemoryPool.h"
11 #include "SkSpinlock.h"
12 
13 // TODO I noticed a small benefit to using a larger exclusive pool for ops. Its very small, but
14 // seems to be mostly consistent.  There is a lot in flux right now, but we should really revisit
15 // this.
16 
17 
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
21 // GrContexts and those contexts may be in use concurrently on different threads.
22 namespace {
23 #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
24 static SkSpinlock gOpPoolSpinLock;
25 #endif
26 class MemoryPoolAccessor {
27 public:
28 
29 // We know in the Android framework there is only one GrContext.
30 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
31     MemoryPoolAccessor() {}
32     ~MemoryPoolAccessor() {}
33 #else
34     MemoryPoolAccessor() { gOpPoolSpinLock.acquire(); }
35     ~MemoryPoolAccessor() { gOpPoolSpinLock.release(); }
36 #endif
37 
38     GrMemoryPool* pool() const {
39         static GrMemoryPool gPool(16384, 16384);
40         return &gPool;
41     }
42 };
43 }
44 
45 int32_t GrOp::gCurrOpClassID = GrOp::kIllegalOpID;
46 
47 int32_t GrOp::gCurrOpUniqueID = GrOp::kIllegalOpID;
48 
49 void* GrOp::operator new(size_t size) {
50     return MemoryPoolAccessor().pool()->allocate(size);
51 }
52 
53 void GrOp::operator delete(void* target) {
54     return MemoryPoolAccessor().pool()->release(target);
55 }
56 
57 GrOp::GrOp(uint32_t classID)
58     : fClassID(classID)
59     , fUniqueID(kIllegalOpID) {
60     SkASSERT(classID == SkToU32(fClassID));
61     SkDEBUGCODE(fBoundsFlags = kUninitialized_BoundsFlag);
62 }
63 
64 GrOp::~GrOp() {}
65