From c0af3173314e227611d8c5541ef2deee0052d412 Mon Sep 17 00:00:00 2001 From: "bsalomon@google.com" Date: Fri, 15 Jun 2012 14:10:09 +0000 Subject: [PATCH] TLS GrContext count 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 | 5 +++++ src/gpu/GrContext.cpp | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h index ee6130b13d..0cd5116cff 100644 --- a/include/gpu/GrContext.h +++ b/include/gpu/GrContext.h @@ -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(); /** diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp index 3cc2c89bfb..5a0f68982b 100644 --- a/src/gpu/GrContext.cpp +++ b/src/gpu/GrContext.cpp @@ -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(v); +} +#define THREAD_INSTANCE_COUNT \ + (*reinterpret_cast(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);