TLS GrContext count
authorbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 15 Jun 2012 14:10:09 +0000 (14:10 +0000)
committerbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Fri, 15 Jun 2012 14:10:09 +0000 (14:10 +0000)
Review URL: http://codereview.appspot.com/6298077/

git-svn-id: http://skia.googlecode.com/svn/trunk@4262 2bbb7eff-a529-9590-31e7-b0007b416f81

include/gpu/GrContext.h
src/gpu/GrContext.cpp

index ee6130b13d83a99ca04743872d4477fc2a6d1de5..0cd5116cffce17cf28b06dc922b6a52f153e7f2b 100644 (file)
@@ -42,6 +42,11 @@ public:
     static GrContext* Create(GrEngine engine,
                              GrPlatform3DContext context3D);
 
+    /**
+     * Returns the number of GrContext instances for the current thread.
+     */
+    static int GetThreadInstanceCount();
+
     virtual ~GrContext();
 
     /**
index 3cc2c89bfb30eb118c11b95466a2cfac1361fe06..5a0f68982b200d76515586cb9277e7d7bd61e8a1 100644 (file)
@@ -24,6 +24,7 @@
 #include "GrStencilBuffer.h"
 #include "GrTextStrike.h"
 #include "SkTLazy.h"
+#include "SkTLS.h"
 #include "SkTrace.h"
 
 #define DEFER_TEXT_RENDERING 1
@@ -68,6 +69,23 @@ GrContext* GrContext::Create(GrEngine engine,
     return ctx;
 }
 
+namespace {
+void* CreateThreadInstanceCount() {
+    return new int(0);
+}
+void DeleteThreadInstanceCount(void* v) {
+    delete reinterpret_cast<int*>(v);
+}
+#define THREAD_INSTANCE_COUNT                                               \
+    (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount,          \
+                                        DeleteThreadInstanceCount)))
+
+}
+
+int GrContext::GetThreadInstanceCount() {
+    return THREAD_INSTANCE_COUNT;
+}
+
 GrContext::~GrContext() {
     this->flush();
 
@@ -87,6 +105,8 @@ GrContext::~GrContext() {
     GrSafeUnref(fPathRendererChain);
     GrSafeUnref(fSoftwarePathRenderer);
     fDrawState->unref();
+
+    --THREAD_INSTANCE_COUNT;
 }
 
 void GrContext::contextLost() {
@@ -1699,6 +1719,8 @@ static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
 }
 
 GrContext::GrContext(GrGpu* gpu) {
+    ++THREAD_INSTANCE_COUNT;
+
     fGpu = gpu;
     fGpu->ref();
     fGpu->setContext(this);