Make GrGLContext be uniquely owned.

Make GrGLContext take GrGLInterface by sk_sp

Change-Id: Iab11b27a7093ec897aaeeab9253958aeaa590b63
Reviewed-on: https://skia-review.googlesource.com/81701
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/gl/GrGLContext.cpp b/src/gpu/gl/GrGLContext.cpp
index 9d18b5f..dd774cd 100644
--- a/src/gpu/gl/GrGLContext.cpp
+++ b/src/gpu/gl/GrGLContext.cpp
@@ -11,36 +11,31 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-GrGLContext* GrGLContext::Create(const GrGLInterface* interface, const GrContextOptions& options) {
-    // We haven't validated the GrGLInterface yet, so check for GetString function pointer
-    if (!interface->fFunctions.fGetString) {
-        return nullptr;
-    }
-    ConstructorArgs args;
-    args.fInterface = interface;
-
-    const GrGLubyte* verUByte;
-    GR_GL_CALL_RET(interface, verUByte, GetString(GR_GL_VERSION));
-    const char* ver = reinterpret_cast<const char*>(verUByte);
-
-    const GrGLubyte* rendererUByte;
-    GR_GL_CALL_RET(interface, rendererUByte, GetString(GR_GL_RENDERER));
-    const char* renderer = reinterpret_cast<const char*>(rendererUByte);
-
+std::unique_ptr<GrGLContext> GrGLContext::Make(sk_sp<const GrGLInterface> interface,
+                                               const GrContextOptions& options) {
     if (!interface->validate()) {
         return nullptr;
     }
 
+    const GrGLubyte* verUByte;
+    GR_GL_CALL_RET(interface.get(), verUByte, GetString(GR_GL_VERSION));
+    const char* ver = reinterpret_cast<const char*>(verUByte);
+
+    const GrGLubyte* rendererUByte;
+    GR_GL_CALL_RET(interface.get(), rendererUByte, GetString(GR_GL_RENDERER));
+    const char* renderer = reinterpret_cast<const char*>(rendererUByte);
+
+    ConstructorArgs args;
     args.fGLVersion = GrGLGetVersionFromString(ver);
     if (GR_GL_INVALID_VER == args.fGLVersion) {
         return nullptr;
     }
 
-    if (!GrGLGetGLSLGeneration(interface, &args.fGLSLGeneration)) {
+    if (!GrGLGetGLSLGeneration(interface.get(), &args.fGLSLGeneration)) {
         return nullptr;
     }
 
-    args.fVendor = GrGLGetVendor(interface);
+    args.fVendor = GrGLGetVendor(interface.get());
 
     args.fRenderer = GrGLGetRendererFromString(renderer);
 
@@ -62,8 +57,9 @@
                       &args.fDriver, &args.fDriverVersion);
 
     args.fContextOptions = &options;
+    args.fInterface = std::move(interface);
 
-    return new GrGLContext(args);
+    return std::unique_ptr<GrGLContext>(new GrGLContext(std::move(args)));
 }
 
 GrGLContext::~GrGLContext() {
@@ -77,8 +73,8 @@
     return fCompiler;
 }
 
-GrGLContextInfo::GrGLContextInfo(const ConstructorArgs& args) {
-    fInterface.reset(SkRef(args.fInterface));
+GrGLContextInfo::GrGLContextInfo(ConstructorArgs&& args) {
+    fInterface = std::move(args.fInterface);
     fGLVersion = args.fGLVersion;
     fGLSLGeneration = args.fGLSLGeneration;
     fVendor = args.fVendor;
diff --git a/src/gpu/gl/GrGLContext.h b/src/gpu/gl/GrGLContext.h
index 4e37125..998a5f5 100644
--- a/src/gpu/gl/GrGLContext.h
+++ b/src/gpu/gl/GrGLContext.h
@@ -23,8 +23,13 @@
  * Encapsulates information about an OpenGL context including the OpenGL
  * version, the GrGLStandard type of the context, and GLSL version.
  */
-class GrGLContextInfo : public SkRefCnt {
+class GrGLContextInfo {
 public:
+    GrGLContextInfo(const GrGLContextInfo&) = delete;
+    GrGLContextInfo& operator=(const GrGLContextInfo&) = delete;
+
+    virtual ~GrGLContextInfo() {}
+
     GrGLStandard standard() const { return fInterface->fStandard; }
     GrGLVersion version() const { return fGLVersion; }
     GrGLSLGeneration glslGeneration() const { return fGLSLGeneration; }
@@ -45,11 +50,9 @@
 
     const GrGLExtensions& extensions() const { return fInterface->fExtensions; }
 
-    virtual ~GrGLContextInfo() {}
-
 protected:
     struct ConstructorArgs {
-        const GrGLInterface*                fInterface;
+        sk_sp<const GrGLInterface>          fInterface;
         GrGLVersion                         fGLVersion;
         GrGLSLGeneration                    fGLSLGeneration;
         GrGLVendor                          fVendor;
@@ -62,7 +65,7 @@
         const  GrContextOptions*            fContextOptions;
     };
 
-    GrGLContextInfo(const ConstructorArgs& args);
+    GrGLContextInfo(ConstructorArgs&&);
 
     sk_sp<const GrGLInterface> fInterface;
     GrGLVersion                fGLVersion;
@@ -86,7 +89,7 @@
      * Creates a GrGLContext from a GrGLInterface and the currently
      * bound OpenGL context accessible by the GrGLInterface.
      */
-    static GrGLContext* Create(const GrGLInterface* interface, const GrContextOptions& options);
+    static std::unique_ptr<GrGLContext> Make(sk_sp<const GrGLInterface>, const GrContextOptions&);
 
     const GrGLInterface* interface() const { return fInterface.get(); }
 
@@ -95,9 +98,7 @@
     ~GrGLContext() override;
 
 private:
-    GrGLContext(const ConstructorArgs& args)
-    : INHERITED(args)
-    , fCompiler(nullptr) {}
+    GrGLContext(ConstructorArgs&& args) : INHERITED(std::move(args)), fCompiler(nullptr) {}
 
     mutable SkSL::Compiler* fCompiler;
 
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index 3309616..ba3f1c2 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -199,25 +199,25 @@
 #ifdef USE_NSIGHT
     const_cast<GrContextOptions&>(options).fSuppressPathRendering = true;
 #endif
-    GrGLContext* glContext = GrGLContext::Create(interface.get(), options);
-    if (glContext) {
-        return sk_sp<GrGpu>(new GrGLGpu(glContext, context));
+    auto glContext = GrGLContext::Make(std::move(interface), options);
+    if (!glContext) {
+        return nullptr;
     }
-    return nullptr;
+    return sk_sp<GrGpu>(new GrGLGpu(std::move(glContext), context));
 }
 
-GrGLGpu::GrGLGpu(GrGLContext* ctx, GrContext* context)
-    : GrGpu(context)
-    , fGLContext(ctx)
-    , fProgramCache(new ProgramCache(this))
-    , fHWProgramID(0)
-    , fTempSrcFBOID(0)
-    , fTempDstFBOID(0)
-    , fStencilClearFBOID(0)
-    , fHWMaxUsedBufferTextureUnit(-1)
-    , fHWMinSampleShading(0.0) {
-    SkASSERT(ctx);
-    fCaps.reset(SkRef(ctx->caps()));
+GrGLGpu::GrGLGpu(std::unique_ptr<GrGLContext> ctx, GrContext* context)
+        : GrGpu(context)
+        , fGLContext(std::move(ctx))
+        , fProgramCache(new ProgramCache(this))
+        , fHWProgramID(0)
+        , fTempSrcFBOID(0)
+        , fTempDstFBOID(0)
+        , fStencilClearFBOID(0)
+        , fHWMaxUsedBufferTextureUnit(-1)
+        , fHWMinSampleShading(0.0) {
+    SkASSERT(fGLContext);
+    fCaps = sk_ref_sp(fGLContext->caps());
 
     fHWBoundTextureUniqueIDs.reset(this->caps()->shaderCaps()->maxCombinedSamplers());
 
diff --git a/src/gpu/gl/GrGLGpu.h b/src/gpu/gl/GrGLGpu.h
index 5c26924..cd78a64 100644
--- a/src/gpu/gl/GrGLGpu.h
+++ b/src/gpu/gl/GrGLGpu.h
@@ -189,7 +189,7 @@
     void insertEventMarker(const char*);
 
 private:
-    GrGLGpu(GrGLContext* ctx, GrContext* context);
+    GrGLGpu(std::unique_ptr<GrGLContext>, GrContext*);
 
     // GrGpu overrides
     void onResetContext(uint32_t resetBits) override;
@@ -415,13 +415,13 @@
 
     void onDumpJSON(SkJSONWriter*) const override;
 
-    sk_sp<GrGLContext>          fGLContext;
-
     bool createCopyProgram(GrTexture* srcTexture);
     bool createMipmapProgram(int progIdx);
     bool createStencilClipClearProgram();
     bool createClearColorProgram();
 
+    std::unique_ptr<GrGLContext> fGLContext;
+
     // GL program-related state
     ProgramCache*               fProgramCache;