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/tests/GrContextFactoryTest.cpp b/tests/GrContextFactoryTest.cpp
index 8e6a964..b0869d9 100644
--- a/tests/GrContextFactoryTest.cpp
+++ b/tests/GrContextFactoryTest.cpp
@@ -98,7 +98,6 @@
 
 DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, /*factory*/) {
     GrContextFactory testFactory;
-    GrContextFactory::ContextOverrides noOverrides = GrContextFactory::ContextOverrides::kNone;
 
     for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
         GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
@@ -112,7 +111,7 @@
         testFactory.abandonContexts();
 
         // Test that creating a context in a share group with an abandoned context fails.
-        ContextInfo info2 = testFactory.getContextInfo(ctxType, noOverrides, info1.grContext());
+        ContextInfo info2 = testFactory.getSharedContextInfo(info1.grContext());
         REPORTER_ASSERT(reporter, !info2.grContext());
         info1.grContext()->unref();
 
@@ -120,7 +119,7 @@
         ContextInfo info3 = testFactory.getContextInfo(ctxType);
 
         // Creating a context in a share group may fail, but should never crash.
-        ContextInfo info4 = testFactory.getContextInfo(ctxType, noOverrides, info3.grContext());
+        ContextInfo info4 = testFactory.getSharedContextInfo(info3.grContext());
         if (!info4.grContext()) {
             continue;
         }
@@ -128,23 +127,11 @@
         REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
 
         // Passing a different index should create a new (unique) context.
-        ContextInfo info5 = testFactory.getContextInfo(ctxType, noOverrides, info3.grContext(), 1);
+        ContextInfo info5 = testFactory.getSharedContextInfo(info3.grContext(), 1);
         REPORTER_ASSERT(reporter, info5.grContext());
         REPORTER_ASSERT(reporter, info5.testContext());
         REPORTER_ASSERT(reporter, info5.grContext() != info4.grContext());
         REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
-
-        // Creating a shared context with a different type should always fail.
-        for (int j = 0; j < GrContextFactory::kContextTypeCnt; ++j) {
-            if (i == j) {
-                continue;
-            }
-            GrContextFactory::ContextType differentCtxType =
-                    static_cast<GrContextFactory::ContextType>(j);
-            ContextInfo info6 = testFactory.getContextInfo(differentCtxType, noOverrides,
-                                                           info3.grContext());
-            REPORTER_ASSERT(reporter, !info6.grContext());
-        }
     }
 }
 
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;