Use global GrMemoryPools protected by mutex for GrProcessor/GrBatch
authorbsalomon <bsalomon@google.com>
Mon, 9 Mar 2015 19:15:53 +0000 (12:15 -0700)
committerCommit bot <commit-bot@chromium.org>
Mon, 9 Mar 2015 19:15:53 +0000 (12:15 -0700)
BUG=chromium:464892

Review URL: https://codereview.chromium.org/991943002

src/gpu/GrBatch.cpp
src/gpu/GrProcessor.cpp

index e1650a6bd31af3d6a5bd188c28b0ee7979889785..4df765e7b2f9f8b8823f4716eeed2866ac9a1e08 100644 (file)
@@ -1,35 +1,38 @@
 #include "GrBatch.h"
 
 #include "GrMemoryPool.h"
-#include "SkTLS.h"
+#include "SkMutex.h"
 
 // TODO I noticed a small benefit to using a larger exclusive pool for batches.  Its very small,
 // but seems to be mostly consistent.  There is a lot in flux right now, but we should really
 // revisit this when batch is everywhere
 
-class GrBatch_Globals {
+
+// We use a global pool protected by a mutex. Chrome may use the same GrContext on different
+// threads. The GrContext is not used concurrently on different threads and there is a memory
+// barrier between accesses of a context on different threads. Also, there may be multiple
+// GrContexts and those contexts may be in use concurrently on different threads.
+namespace {
+SK_DECLARE_STATIC_MUTEX(gBatchPoolMutex);
+class MemoryPoolAccessor {
 public:
-    static GrMemoryPool* GetTLS() {
-        return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
-    }
+    MemoryPoolAccessor() { gBatchPoolMutex.acquire(); }
 
-private:
-    static void* CreateTLS() {
-        return SkNEW_ARGS(GrMemoryPool, (16384, 16384));
-    }
+    ~MemoryPoolAccessor() { gBatchPoolMutex.release(); }
 
-    static void DeleteTLS(void* pool) {
-        SkDELETE(reinterpret_cast<GrMemoryPool*>(pool));
+    GrMemoryPool* pool() const {
+        static GrMemoryPool gPool(16384, 16384);
+        return &gPool;
     }
 };
+}
 
-int32_t GrBatch::gCurrBatchClassID =
-        GrBatch::kIllegalBatchClassID;
+int32_t GrBatch::gCurrBatchClassID = GrBatch::kIllegalBatchClassID;
 
 void* GrBatch::operator new(size_t size) {
-    return GrBatch_Globals::GetTLS()->allocate(size);
+    return MemoryPoolAccessor().pool()->allocate(size);
 }
 
 void GrBatch::operator delete(void* target) {
-    GrBatch_Globals::GetTLS()->release(target);
+    return MemoryPoolAccessor().pool()->release(target);
 }
index 08f437d50a06e58fa9376c18ad4d68a433ee88bb..3b8cb679fd47b887897be5bb27262c5c9d935dbe 100644 (file)
@@ -12,7 +12,7 @@
 #include "GrInvariantOutput.h"
 #include "GrMemoryPool.h"
 #include "GrXferProcessor.h"
-#include "SkTLS.h"
+#include "SkMutex.h"
 
 #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
 
@@ -96,24 +96,27 @@ const SkMatrix& TestMatrix(SkRandom* random) {
 }
 }
 
-class GrProcessor_Globals {
+
+// We use a global pool protected by a mutex. Chrome may use the same GrContext on different
+// threads. The GrContext is not used concurrently on different threads and there is a memory
+// barrier between accesses of a context on different threads. Also, there may be multiple
+// GrContexts and those contexts may be in use concurrently on different threads.
+namespace {
+SK_DECLARE_STATIC_MUTEX(gProcessorPoolMutex);
+class MemoryPoolAccessor {
 public:
-    static GrMemoryPool* GetTLS() {
-        return (GrMemoryPool*)SkTLS::Get(CreateTLS, DeleteTLS);
-    }
+    MemoryPoolAccessor() { gProcessorPoolMutex.acquire(); }
 
-private:
-    static void* CreateTLS() {
-        return SkNEW_ARGS(GrMemoryPool, (4096, 4096));
-    }
+    ~MemoryPoolAccessor() { gProcessorPoolMutex.release(); }
 
-    static void DeleteTLS(void* pool) {
-        SkDELETE(reinterpret_cast<GrMemoryPool*>(pool));
+    GrMemoryPool* pool() const {
+        static GrMemoryPool gPool(4096, 4096);
+        return &gPool;
     }
 };
+}
 
-int32_t GrProcessor::gCurrProcessorClassID =
-        GrProcessor::kIllegalProcessorClassID;
+int32_t GrProcessor::gCurrProcessorClassID = GrProcessor::kIllegalProcessorClassID;
 
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -125,11 +128,11 @@ void GrProcessor::addTextureAccess(const GrTextureAccess* access) {
 }
 
 void* GrProcessor::operator new(size_t size) {
-    return GrProcessor_Globals::GetTLS()->allocate(size);
+    return MemoryPoolAccessor().pool()->allocate(size);
 }
 
 void GrProcessor::operator delete(void* target) {
-    GrProcessor_Globals::GetTLS()->release(target);
+    return MemoryPoolAccessor().pool()->release(target);
 }
 
 bool GrProcessor::hasSameTextureAccesses(const GrProcessor& that) const {