xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrProcessor.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2012 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 "src/base/SkSpinlock.h"
9 #include "src/gpu/ganesh/GrMemoryPool.h"
10 #include "src/gpu/ganesh/GrProcessor.h"
11 
12 #include <memory>
13 
14 // We use a global pool protected by a mutex(spinlock). Chrome may use the same GrContext on
15 // different threads. The GrContext is not used concurrently on different threads and there is a
16 // memory barrier between accesses of a context on different threads. Also, there may be multiple
17 // GrContexts and those contexts may be in use concurrently on different threads.
18 namespace {
19 #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
20 static SkSpinlock gProcessorSpinlock;
21 #endif
22 class MemoryPoolAccessor {
23 public:
24 
25 // We know in the Android framework there is only one GrContext.
26 #if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
MemoryPoolAccessor()27     MemoryPoolAccessor() {}
~MemoryPoolAccessor()28     ~MemoryPoolAccessor() {}
29 #else
30     MemoryPoolAccessor() { gProcessorSpinlock.acquire(); }
31     ~MemoryPoolAccessor() { gProcessorSpinlock.release(); }
32 #endif
33 
pool() const34     GrMemoryPool* pool() const {
35         static GrMemoryPool* gPool = GrMemoryPool::Make(4096, 4096).release();
36         return gPool;
37     }
38 };
39 }  // namespace
40 
41 ///////////////////////////////////////////////////////////////////////////////
42 
operator new(size_t size)43 void* GrProcessor::operator new(size_t size) { return MemoryPoolAccessor().pool()->allocate(size); }
44 
operator new(size_t object_size,size_t footer_size)45 void* GrProcessor::operator new(size_t object_size, size_t footer_size) {
46     return MemoryPoolAccessor().pool()->allocate(object_size + footer_size);
47 }
48 
operator delete(void * target)49 void GrProcessor::operator delete(void* target) {
50     return MemoryPoolAccessor().pool()->release(target);
51 }
52