Fixes to shared context test API

Fixes a bug in Windows shared context creation, and makes the API
less fiddly.

BUG=skia:

Change-Id: Ia32b2e3b4816e0b8d7e9be92c22a182ca1393177
Reviewed-on: https://skia-review.googlesource.com/8965
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/tools/gpu/GrContextFactory.cpp b/tools/gpu/GrContextFactory.cpp
index 637c569..0c6c87a 100644
--- a/tools/gpu/GrContextFactory.cpp
+++ b/tools/gpu/GrContextFactory.cpp
@@ -103,8 +103,8 @@
     GrContextFactory::kGLES_ContextType;
 #endif
 
-ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides,
-                                             GrContext* shareContext, uint32_t shareIndex) {
+ContextInfo GrContextFactory::getContextInfoInternal(ContextType type, ContextOverrides overrides,
+                                                     GrContext* shareContext, uint32_t shareIndex) {
     // (shareIndex != 0) -> (shareContext != nullptr)
     SkASSERT((shareIndex == 0) || (shareContext != nullptr));
 
@@ -129,10 +129,7 @@
                 break;
             }
         }
-
-        if (!masterContext || masterContext->fType != type) {
-            return ContextInfo();
-        }
+        SkASSERT(masterContext && masterContext->fType == type);
     }
 
     std::unique_ptr<TestContext> testCtx;
@@ -273,4 +270,20 @@
     return ContextInfo(context.fBackend, context.fTestContext, context.fGrContext);
 }
 
+ContextInfo GrContextFactory::getContextInfo(ContextType type, ContextOverrides overrides) {
+    return this->getContextInfoInternal(type, overrides, nullptr, 0);
+}
+
+ContextInfo GrContextFactory::getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex) {
+    SkASSERT(shareContext);
+    for (int i = 0; i < fContexts.count(); ++i) {
+        if (!fContexts[i].fAbandoned && fContexts[i].fGrContext == shareContext) {
+            return this->getContextInfoInternal(fContexts[i].fType, fContexts[i].fOverrides,
+                                                shareContext, shareIndex);
+        }
+    }
+
+    return ContextInfo();
+}
+
 }  // namespace sk_gpu_test
diff --git a/tools/gpu/GrContextFactory.h b/tools/gpu/GrContextFactory.h
index e42f34c..b1a4e1a 100644
--- a/tools/gpu/GrContextFactory.h
+++ b/tools/gpu/GrContextFactory.h
@@ -144,13 +144,17 @@
 
     /**
      * Get a context initialized with a type of GL context. It also makes the GL context current.
-     * If shareContextInfo is supplied, then a context is created or returned in the same share
-     * group (able to share resources). To get multiple contexts in a single share group, pass the
-     * same shareContextInfo, with different values for shareIndex.
      */
     ContextInfo getContextInfo(ContextType type,
-                               ContextOverrides overrides = ContextOverrides::kNone,
-                               GrContext* shareContext = nullptr, uint32_t shareIndex = 0);
+                               ContextOverrides overrides = ContextOverrides::kNone);
+
+    /**
+     * Get a context in the same share group as the passed in GrContext, with the same type and
+     * overrides. To get multiple contexts in a single share group, pass the same shareContext,
+     * with different values for shareIndex.
+     */
+    ContextInfo getSharedContextInfo(GrContext* shareContext, uint32_t shareIndex = 0);
+
     /**
      * Get a GrContext initialized with a type of GL context. It also makes the GL context current.
      */
@@ -160,6 +164,9 @@
     const GrContextOptions& getGlobalOptions() const { return fGlobalOptions; }
 
 private:
+    ContextInfo getContextInfoInternal(ContextType type, ContextOverrides overrides,
+                                       GrContext* shareContext, uint32_t shareIndex);
+
     struct Context {
         ContextType       fType;
         ContextOverrides  fOverrides;
diff --git a/tools/gpu/gl/win/CreatePlatformGLTestContext_win.cpp b/tools/gpu/gl/win/CreatePlatformGLTestContext_win.cpp
index af35b7b..49d7743 100644
--- a/tools/gpu/gl/win/CreatePlatformGLTestContext_win.cpp
+++ b/tools/gpu/gl/win/CreatePlatformGLTestContext_win.cpp
@@ -85,7 +85,11 @@
         kGLES_GrGLStandard == forcedGpuAPI ?
         kGLES_SkWGLContextRequest : kGLPreferCompatibilityProfile_SkWGLContextRequest;
 
-    HGLRC winShareContext = shareContext ? shareContext->fGlRenderContext : nullptr;
+    HGLRC winShareContext = nullptr;
+    if (shareContext) {
+        winShareContext = shareContext->fPbufferContext ? shareContext->fPbufferContext->getGLRC()
+                                                        : shareContext->fGlRenderContext;
+    }
     fPbufferContext = SkWGLPbufferContext::Create(fDeviceContext, 0, contextType, winShareContext);
 
     HDC dc;