Revert "Revert "Add method to sk_gpu_test::TestContext to automatically restore the previous context.""

This reverts commit 1e09e461d2ffcf8b07242cfe93dd7d12c4d75866.

Change-Id: I95d5544a7baaa078536790493ce4119816a77e94
Reviewed-on: https://skia-review.googlesource.com/72903
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/tools/gpu/GrContextFactory.cpp b/tools/gpu/GrContextFactory.cpp
index 21766db..00c4b65 100644
--- a/tools/gpu/GrContextFactory.cpp
+++ b/tools/gpu/GrContextFactory.cpp
@@ -52,8 +52,9 @@
 
 void GrContextFactory::destroyContexts() {
     for (Context& context : fContexts) {
+        SkScopeExit restore(nullptr);
         if (context.fTestContext) {
-            context.fTestContext->makeCurrent();
+            restore = context.fTestContext->makeCurrentAndAutoRestore();
         }
         if (!context.fGrContext->unique()) {
             context.fGrContext->releaseResourcesAndAbandonContext();
@@ -69,7 +70,7 @@
     for (Context& context : fContexts) {
         if (!context.fAbandoned) {
             if (context.fTestContext) {
-                context.fTestContext->makeCurrent();
+                auto restore = context.fTestContext->makeCurrentAndAutoRestore();
                 context.fTestContext->testAbandon();
                 delete(context.fTestContext);
                 context.fTestContext = nullptr;
@@ -82,9 +83,10 @@
 
 void GrContextFactory::releaseResourcesAndAbandonContexts() {
     for (Context& context : fContexts) {
+        SkScopeExit restore(nullptr);
         if (!context.fAbandoned) {
             if (context.fTestContext) {
-                context.fTestContext->makeCurrent();
+                restore = context.fTestContext->makeCurrentAndAutoRestore();
             }
             context.fGrContext->releaseResourcesAndAbandonContext();
             context.fAbandoned = true;
@@ -237,7 +239,7 @@
         default:
             return ContextInfo();
     }
-    testCtx->makeCurrent();
+
     SkASSERT(testCtx && testCtx->backend() == backend);
     GrContextOptions grOptions = fGlobalOptions;
     if (ContextOverrides::kDisableNVPR & overrides) {
@@ -252,7 +254,11 @@
     if (ContextOverrides::kAvoidStencilBuffers & overrides) {
         grOptions.fAvoidStencilBuffers = true;
     }
-    sk_sp<GrContext> grCtx = testCtx->makeGrContext(grOptions);
+    sk_sp<GrContext> grCtx;
+    {
+        auto restore = testCtx->makeCurrentAndAutoRestore();
+        grCtx = testCtx->makeGrContext(grOptions);
+    }
     if (!grCtx.get()) {
         return ContextInfo();
     }
@@ -282,6 +288,7 @@
     context.fShareContext = shareContext;
     context.fShareIndex = shareIndex;
     context.fOptions = grOptions;
+    context.fTestContext->makeCurrent();
     return ContextInfo(context.fType, context.fTestContext, context.fGrContext, context.fOptions);
 }