Have GMs read the GrContext via a setter/getter rather than a global.

GetGr's current global state makes things rather tricky to run GPU GMs in
parallel (DM).  This API change will let me feed the right GrContext to the
right GM in DM.

I'm not planning on changing the status quo in GM-the-tool: the new getters and
setters still just return the same global.

BUG=skia:1590
R=bsalomon@google.com, robertphillips@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@11306 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/bleed.cpp b/gm/bleed.cpp
index 2b2d849..dc7e96f 100644
--- a/gm/bleed.cpp
+++ b/gm/bleed.cpp
@@ -12,10 +12,6 @@
 
 #if SK_SUPPORT_GPU
 #include "GrContext.h"
-
-namespace skiagm {
-extern GrContext* GetGr();
-};
 #endif
 
 // Create a black&white checked texture with a 1-pixel red ring
@@ -175,7 +171,7 @@
 
 
 #if SK_SUPPORT_GPU
-        GrContext* ctx = skiagm::GetGr();
+        GrContext* ctx = GM::GetGr(canvas);
         int oldMaxTextureSize = 0;
         if (NULL != ctx) {
             // shrink the max texture size so all our textures can be reasonably sized
diff --git a/gm/gm.cpp b/gm/gm.cpp
index f794fbf..06d3c6d 100644
--- a/gm/gm.cpp
+++ b/gm/gm.cpp
@@ -62,5 +62,18 @@
     canvas->drawRect(r, paint);
 }
 
+#if SK_SUPPORT_GPU
+// canvas could almost be a const&, but accessRenderTarget isn't const.
+/*static*/ GrContext* GM::GetGr(SkCanvas* canvas) {
+    SkASSERT(NULL != canvas);
+    SkBaseDevice* device = canvas->getTopDevice();
+    GrRenderTarget* renderTarget = device->accessRenderTarget();
+    if (NULL != renderTarget) {
+        return renderTarget->getContext();
+    }
+    return NULL;
+}
+#endif
+
 // need to explicitly declare this, or we get some weird infinite loop llist
 template GMRegistry* SkTRegistry<GM*(*)(void*)>::gHead;
diff --git a/gm/gm.h b/gm/gm.h
index 6cd97b5..2886f8c 100644
--- a/gm/gm.h
+++ b/gm/gm.h
@@ -16,6 +16,10 @@
 #include "SkString.h"
 #include "SkTRegistry.h"
 
+#if SK_SUPPORT_GPU
+#include "GrContext.h"
+#endif
+
 #define DEF_GM(code) \
     static skiagm::GM*          SK_MACRO_APPEND_LINE(F_)(void*) { code; } \
     static skiagm::GMRegistry   SK_MACRO_APPEND_LINE(R_)(SK_MACRO_APPEND_LINE(F_));
@@ -93,6 +97,10 @@
             fCanvasIsDeferred = isDeferred;
         }
 
+#if SK_SUPPORT_GPU
+        static GrContext* GetGr(/*very nearly const*/ SkCanvas*);
+#endif
+
     protected:
         static SkString gResourcePath;
 
diff --git a/gm/gmmain.cpp b/gm/gmmain.cpp
index 018dfe6..4939624 100644
--- a/gm/gmmain.cpp
+++ b/gm/gmmain.cpp
@@ -1464,48 +1464,6 @@
     return NULL;
 }
 
-namespace skiagm {
-#if SK_SUPPORT_GPU
-SkAutoTUnref<GrContext> gGrContext;
-/**
- * Sets the global GrContext, accessible by individual GMs
- */
-static void SetGr(GrContext* grContext) {
-    SkSafeRef(grContext);
-    gGrContext.reset(grContext);
-}
-
-/**
- * Gets the global GrContext, can be called by GM tests.
- */
-GrContext* GetGr();
-GrContext* GetGr() {
-    return gGrContext.get();
-}
-
-/**
- * Sets the global GrContext and then resets it to its previous value at
- * destruction.
- */
-class AutoResetGr : SkNoncopyable {
-public:
-    AutoResetGr() : fOld(NULL) {}
-    void set(GrContext* context) {
-        SkASSERT(NULL == fOld);
-        fOld = GetGr();
-        SkSafeRef(fOld);
-        SetGr(context);
-    }
-    ~AutoResetGr() { SetGr(fOld); SkSafeUnref(fOld); }
-private:
-    GrContext* fOld;
-};
-#else
-GrContext* GetGr();
-GrContext* GetGr() { return NULL; }
-#endif
-}
-
 template <typename T> void appendUnique(SkTDArray<T>* array, const T& value) {
     int index = array->find(value);
     if (index < 0) {
@@ -1573,7 +1531,6 @@
         GrSurface* gpuTarget = NULL;
 #if SK_SUPPORT_GPU
         SkAutoTUnref<GrSurface> auGpuTarget;
-        AutoResetGr autogr;
         if ((errorsForThisConfig.isEmpty()) && (kGPU_Backend == config.fBackend)) {
             GrContext* gr = grFactory->get(config.fGLContextType);
             bool grSuccess = false;
@@ -1589,7 +1546,6 @@
                 if (NULL != auGpuTarget) {
                     gpuTarget = auGpuTarget;
                     grSuccess = true;
-                    autogr.set(gr);
                     // Set the user specified cache limits if non-default.
                     size_t bytes;
                     int count;
diff --git a/gm/image.cpp b/gm/image.cpp
index 3ed85f9..6dada0d 100644
--- a/gm/image.cpp
+++ b/gm/image.cpp
@@ -13,10 +13,6 @@
 
 #if SK_SUPPORT_GPU
 #include "GrContext.h"
-
-namespace skiagm {
-extern GrContext* GetGr();
-};
 #endif
 
 static SkData* fileToData(const char path[]) {
@@ -193,7 +189,7 @@
         SkAutoTUnref<SkSurface> surf2(SkSurface::NewPicture(info.fWidth, info.fHeight));
         SkAutoTUnref<SkSurface> surf3(SkSurface::NewPicture(info.fWidth, info.fHeight));
 #if SK_SUPPORT_GPU
-        GrContext* ctx = skiagm::GetGr();
+        GrContext* ctx = GM::GetGr(canvas);
 
         SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info, 0));
 #endif
diff --git a/gm/texdata.cpp b/gm/texdata.cpp
index b2c7139..8ba0c77 100644
--- a/gm/texdata.cpp
+++ b/gm/texdata.cpp
@@ -18,8 +18,6 @@
 
 namespace skiagm {
 
-extern GrContext* GetGr();
-
 static const int S = 200;
 
 class TexDataGM : public GM {
@@ -40,7 +38,7 @@
     virtual void onDraw(SkCanvas* canvas) {
         SkBaseDevice* device = canvas->getTopDevice();
         GrRenderTarget* target = device->accessRenderTarget();
-        GrContext* ctx = GetGr();
+        GrContext* ctx = GM::GetGr(canvas);
         if (ctx && target) {
             SkPMColor gTextureData[(2 * S) * (2 * S)];
             static const int stride = 2 * S;