Make the GrDirectContext a thing and move it to include/private

This is somewhat of a departure from the original context refactoring
plan (i.e., keep GrLegacyDirectContext hidden and switch GrContext over
to be the GrDirectContext at some point). Having a GrDirectContext
earlier will allow us to change some important signatures earlier
(e.g., asDirectContext) and, hopefully, clarify some of the confusion
about the context class hierarchy.

Additionally, this will let us make onGpuSetup take a direct context -
clarifying its purpose vis a vis onDraw (which now takes a recording
context).

Change-Id: I8298a0649bc95843d20bee33ba7fe1d7e73bb839
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/299768
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/gn/gpu.gni b/gn/gpu.gni
index 196b054..702fdca 100644
--- a/gn/gpu.gni
+++ b/gn/gpu.gni
@@ -21,6 +21,7 @@
 
   # Private includes
   "$_include/private/GrContext_Base.h",
+  "$_include/private/GrDirectContext.h",
   "$_include/private/GrGLTypesPriv.h",
   "$_include/private/GrImageContext.h",
   "$_include/private/GrRecordingContext.h",
@@ -81,6 +82,7 @@
   "$_src/gpu/GrDefaultGeoProcFactory.h",
   "$_src/gpu/GrDeferredProxyUploader.h",
   "$_src/gpu/GrDeferredUpload.h",
+  "$_src/gpu/GrDirectContext.cpp",
   "$_src/gpu/GrDistanceFieldGenFromVector.cpp",
   "$_src/gpu/GrDistanceFieldGenFromVector.h",
   "$_src/gpu/GrDrawOpAtlas.cpp",
@@ -115,7 +117,6 @@
   "$_src/gpu/GrImageInfo.h",
   "$_src/gpu/GrImageTextureMaker.cpp",
   "$_src/gpu/GrImageTextureMaker.h",
-  "$_src/gpu/GrLegacyDirectContext.cpp",
   "$_src/gpu/GrManagedResource.cpp",
   "$_src/gpu/GrManagedResource.h",
   "$_src/gpu/GrMemoryPool.cpp",
diff --git a/include/private/GrDirectContext.h b/include/private/GrDirectContext.h
new file mode 100644
index 0000000..93f077f
--- /dev/null
+++ b/include/private/GrDirectContext.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2020 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef GrDirectContext_DEFINED
+#define GrDirectContext_DEFINED
+
+#include "include/gpu/GrContext.h"
+
+class GrAtlasManager;
+
+class GrDirectContext : public GrContext {
+public:
+    GrDirectContext(GrBackendApi backend, const GrContextOptions& options);
+
+    ~GrDirectContext() override;
+
+    void abandonContext() override;
+
+    void releaseResourcesAndAbandonContext() override;
+
+    void freeGpuResources() override;
+
+protected:
+    bool init() override;
+
+    GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
+
+private:
+    GrAtlasManager* fAtlasManager;
+
+    typedef GrContext INHERITED;
+};
+
+
+#endif
diff --git a/src/gpu/GrLegacyDirectContext.cpp b/src/gpu/GrDirectContext.cpp
similarity index 64%
rename from src/gpu/GrLegacyDirectContext.cpp
rename to src/gpu/GrDirectContext.cpp
index 4b027ea..472c3c6 100644
--- a/src/gpu/GrLegacyDirectContext.cpp
+++ b/src/gpu/GrDirectContext.cpp
@@ -6,7 +6,7 @@
  */
 
 
-#include "include/gpu/GrContext.h"
+#include "include/private/GrDirectContext.h"
 
 #include "include/gpu/GrContextThreadSafeProxy.h"
 #include "src/gpu/GrContextPriv.h"
@@ -44,88 +44,77 @@
 static const bool kDefaultReduceOpsTaskSplitting = false;
 #endif
 
-class GrLegacyDirectContext : public GrContext {
-public:
-    GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
-            : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options))
-            , fAtlasManager(nullptr) {
-    }
+GrDirectContext::GrDirectContext(GrBackendApi backend, const GrContextOptions& options)
+        : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options))
+        , fAtlasManager(nullptr) {
+}
 
-    ~GrLegacyDirectContext() override {
-        // this if-test protects against the case where the context is being destroyed
-        // before having been fully created
-        if (this->priv().getGpu()) {
-            this->flushAndSubmit();
-        }
-
-        delete fAtlasManager;
-    }
-
-    void abandonContext() override {
-        INHERITED::abandonContext();
-        fAtlasManager->freeAll();
-    }
-
-    void releaseResourcesAndAbandonContext() override {
-        INHERITED::releaseResourcesAndAbandonContext();
-        fAtlasManager->freeAll();
-    }
-
-    void freeGpuResources() override {
+GrDirectContext::~GrDirectContext() {
+    // this if-test protects against the case where the context is being destroyed
+    // before having been fully created
+    if (this->priv().getGpu()) {
         this->flushAndSubmit();
-        fAtlasManager->freeAll();
-
-        INHERITED::freeGpuResources();
     }
 
-protected:
-    bool init() override {
-        const GrGpu* gpu = this->priv().getGpu();
-        if (!gpu) {
-            return false;
-        }
+    delete fAtlasManager;
+}
 
-        fThreadSafeProxy->priv().init(gpu->refCaps());
-        if (!INHERITED::init()) {
-            return false;
-        }
+void GrDirectContext::abandonContext() {
+    INHERITED::abandonContext();
+    fAtlasManager->freeAll();
+}
 
-        bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
-        if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
-            reduceOpsTaskSplitting = false;
-        } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
-            reduceOpsTaskSplitting = true;
-        }
+void GrDirectContext::releaseResourcesAndAbandonContext() {
+    INHERITED::releaseResourcesAndAbandonContext();
+    fAtlasManager->freeAll();
+}
 
-        this->setupDrawingManager(true, reduceOpsTaskSplitting);
+void GrDirectContext::freeGpuResources() {
+    this->flushAndSubmit();
+    fAtlasManager->freeAll();
 
-        GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
-        if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
-            // multitexturing supported only if range can represent the index + texcoords fully
-            !(this->caps()->shaderCaps()->floatIs32Bits() ||
-              this->caps()->shaderCaps()->integerSupport())) {
-            allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
-        } else {
-            allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
-        }
+    INHERITED::freeGpuResources();
+}
 
-        GrProxyProvider* proxyProvider = this->priv().proxyProvider();
-
-        fAtlasManager = new GrAtlasManager(proxyProvider,
-                                           this->options().fGlyphCacheTextureMaximumBytes,
-                                           allowMultitexturing);
-        this->priv().addOnFlushCallbackObject(fAtlasManager);
-
-        return true;
+bool GrDirectContext::init() {
+    const GrGpu* gpu = this->priv().getGpu();
+    if (!gpu) {
+        return false;
     }
 
-    GrAtlasManager* onGetAtlasManager() override { return fAtlasManager; }
+    fThreadSafeProxy->priv().init(gpu->refCaps());
+    if (!INHERITED::init()) {
+        return false;
+    }
 
-private:
-    GrAtlasManager* fAtlasManager;
+    bool reduceOpsTaskSplitting = kDefaultReduceOpsTaskSplitting;
+    if (GrContextOptions::Enable::kNo == this->options().fReduceOpsTaskSplitting) {
+        reduceOpsTaskSplitting = false;
+    } else if (GrContextOptions::Enable::kYes == this->options().fReduceOpsTaskSplitting) {
+        reduceOpsTaskSplitting = true;
+    }
 
-    typedef GrContext INHERITED;
-};
+    this->setupDrawingManager(true, reduceOpsTaskSplitting);
+
+    GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
+    if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
+        // multitexturing supported only if range can represent the index + texcoords fully
+        !(this->caps()->shaderCaps()->floatIs32Bits() ||
+        this->caps()->shaderCaps()->integerSupport())) {
+        allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
+    } else {
+        allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
+    }
+
+    GrProxyProvider* proxyProvider = this->priv().proxyProvider();
+
+    fAtlasManager = new GrAtlasManager(proxyProvider,
+                                       this->options().fGlyphCacheTextureMaximumBytes,
+                                       allowMultitexturing);
+    this->priv().addOnFlushCallbackObject(fAtlasManager);
+
+    return true;
+}
 
 #ifdef SK_GL
 sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface) {
@@ -173,7 +162,7 @@
 
 sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> glInterface,
                                    const GrContextOptions& options) {
-    sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
+    sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kOpenGL, options));
 #if GR_TEST_UTILS
     if (options.fRandomGLOOM) {
         auto copy = sk_make_sp<GrGLInterface>(*glInterface);
@@ -201,7 +190,7 @@
 
 sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
                                      const GrContextOptions& options) {
-    sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
+    sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMock, options));
 
     context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
     if (!context->init()) {
@@ -223,7 +212,7 @@
 sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext& backendContext,
                                        const GrContextOptions& options) {
 #ifdef SK_VULKAN
-    sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
+    sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kVulkan, options));
 
     context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
     if (!context->init()) {
@@ -243,7 +232,7 @@
 }
 
 sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
-    sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
+    sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kMetal, options));
 
     context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
     if (!context->init()) {
@@ -262,7 +251,7 @@
 
 sk_sp<GrContext> GrContext::MakeDirect3D(const GrD3DBackendContext& backendContext,
                                          const GrContextOptions& options) {
-    sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDirect3D, options));
+    sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kDirect3D, options));
 
     context->fGpu = GrD3DGpu::Make(backendContext, options, context.get());
     if (!context->init()) {
@@ -280,7 +269,7 @@
 }
 
 sk_sp<GrContext> GrContext::MakeDawn(const wgpu::Device& device, const GrContextOptions& options) {
-    sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
+    sk_sp<GrContext> context(new GrDirectContext(GrBackendApi::kDawn, options));
 
     context->fGpu = GrDawnGpu::Make(device, options, context.get());
     if (!context->init()) {