Make GrContextThreadSafeProxy not a GrContext_Base

Once this API is retracted, we can rename it to something more sane.
The code base has some `fContextInfo` ivars of this type, suggesting it
was previously named ContextInfo. It could be a ContextGroup or something else.

Bug: skia:10318
Change-Id: I3471e2172f46163f98a94780f0d7eb3431894cda
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/293556
Reviewed-by: Robert Phillips <robertphillips@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
Auto-Submit: Adlai Holler <adlai@google.com>
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 956a2e5..5bff036 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -48,8 +48,7 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t contextID)
-        : INHERITED(backend, options, contextID) {
+GrContext::GrContext(sk_sp<GrContextThreadSafeProxy> proxy) : INHERITED(std::move(proxy)) {
     fResourceCache = nullptr;
     fResourceProvider = nullptr;
 }
@@ -64,15 +63,14 @@
     delete fResourceCache;
 }
 
-bool GrContext::init(sk_sp<const GrCaps> caps) {
+bool GrContext::init() {
     ASSERT_SINGLE_OWNER
     SkASSERT(this->proxyProvider());
 
-    if (!INHERITED::init(std::move(caps))) {
+    if (!INHERITED::init()) {
         return false;
     }
 
-    SkASSERT(this->caps());
     SkASSERT(this->getTextBlobCache());
 
     if (fGpu) {
@@ -104,7 +102,7 @@
 }
 
 sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
-    return fThreadSafeProxy;
+    return INHERITED::threadSafeProxy();
 }
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/GrContextPriv.h b/src/gpu/GrContextPriv.h
index d97f7ad..c5d3f72 100644
--- a/src/gpu/GrContextPriv.h
+++ b/src/gpu/GrContextPriv.h
@@ -74,7 +74,7 @@
     /**
      * Create a GrContext without a resource cache
      */
-    static sk_sp<GrContext> MakeDDL(const sk_sp<GrContextThreadSafeProxy>&);
+    static sk_sp<GrContext> MakeDDL(sk_sp<GrContextThreadSafeProxy>);
 
     /**
      * Finalizes all pending reads and writes to the surfaces and also performs an MSAA resolves
diff --git a/src/gpu/GrContextThreadSafeProxy.cpp b/src/gpu/GrContextThreadSafeProxy.cpp
index af92057..6fd86bd 100644
--- a/src/gpu/GrContextThreadSafeProxy.cpp
+++ b/src/gpu/GrContextThreadSafeProxy.cpp
@@ -19,16 +19,24 @@
 #include "src/gpu/vk/GrVkCaps.h"
 #endif
 
+static int32_t next_id() {
+    static std::atomic<int32_t> nextID{1};
+    int32_t id;
+    do {
+        id = nextID++;
+    } while (id == SK_InvalidGenID);
+    return id;
+}
+
 GrContextThreadSafeProxy::GrContextThreadSafeProxy(GrBackendApi backend,
-                                                   const GrContextOptions& options,
-                                                   uint32_t contextID)
-        : INHERITED(backend, options, contextID) {
+                                                   const GrContextOptions& options)
+        : fBackend(backend), fOptions(options), fContextID(next_id()) {
 }
 
 GrContextThreadSafeProxy::~GrContextThreadSafeProxy() = default;
 
-bool GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
-    return INHERITED::init(std::move(caps));
+void GrContextThreadSafeProxy::init(sk_sp<const GrCaps> caps) {
+    fCaps = std::move(caps);
 }
 
 SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
@@ -38,6 +46,7 @@
                                      const SkSurfaceProps& surfaceProps,
                                      bool isMipMapped, bool willUseGLFBO0, bool isTextureable,
                                      GrProtected isProtected) {
+    SkASSERT(fCaps);
     if (!backendFormat.isValid()) {
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
@@ -49,39 +58,39 @@
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
-    if (!this->caps()->mipMapSupport()) {
+    if (!fCaps->mipMapSupport()) {
         isMipMapped = false;
     }
 
     GrColorType grColorType = SkColorTypeToGrColorType(ii.colorType());
 
-    if (!this->caps()->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
+    if (!fCaps->areColorTypeAndFormatCompatible(grColorType, backendFormat)) {
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
-    if (!this->caps()->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
+    if (!fCaps->isFormatAsColorTypeRenderable(grColorType, backendFormat, sampleCnt)) {
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
-    sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendFormat);
+    sampleCnt = fCaps->getRenderTargetSampleCount(sampleCnt, backendFormat);
     SkASSERT(sampleCnt);
 
     if (willUseGLFBO0 && isTextureable) {
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
-    if (isTextureable && !this->caps()->isFormatTexturable(backendFormat)) {
+    if (isTextureable && !fCaps->isFormatTexturable(backendFormat)) {
         // Skia doesn't agree that this is textureable.
         return SkSurfaceCharacterization(); // return an invalid characterization
     }
 
     if (GrBackendApi::kVulkan == backendFormat.backend()) {
-        if (GrBackendApi::kVulkan != this->backend()) {
+        if (GrBackendApi::kVulkan != fBackend) {
             return SkSurfaceCharacterization(); // return an invalid characterization
         }
 
 #ifdef SK_VULKAN
-        const GrVkCaps* vkCaps = (const GrVkCaps*) this->caps();
+        const GrVkCaps* vkCaps = (const GrVkCaps*) fCaps.get();
 
         // The protection status of the characterization and the context need to match
         if (isProtected != GrProtected(vkCaps->supportsProtectedMemory())) {
@@ -101,6 +110,22 @@
                                      surfaceProps);
 }
 
+GrBackendFormat GrContextThreadSafeProxy::defaultBackendFormat(SkColorType skColorType,
+                                                               GrRenderable renderable) const {
+    SkASSERT(fCaps);
+    GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
+
+    GrBackendFormat format = fCaps->getDefaultBackendFormat(grColorType, renderable);
+    if (!format.isValid()) {
+        return GrBackendFormat();
+    }
+
+    SkASSERT(renderable == GrRenderable::kNo ||
+             fCaps->isFormatAsColorTypeRenderable(grColorType, format));
+
+    return format;
+}
+
 void GrContextThreadSafeProxy::abandonContext() {
     fAbandoned.store(true, std::memory_order_relaxed);
 }
@@ -112,15 +137,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 sk_sp<GrContextThreadSafeProxy> GrContextThreadSafeProxyPriv::Make(
                              GrBackendApi backend,
-                             const GrContextOptions& options,
-                             uint32_t contextID,
-                             sk_sp<const GrCaps> caps) {
-    sk_sp<GrContextThreadSafeProxy> proxy(new GrContextThreadSafeProxy(backend, options,
-                                                                       contextID));
-
-    if (!proxy->init(std::move(caps))) {
-        return nullptr;
-    }
-    return proxy;
+                             const GrContextOptions& options) {
+    return sk_sp<GrContextThreadSafeProxy>(new GrContextThreadSafeProxy(backend, options));
 }
 
diff --git a/src/gpu/GrContextThreadSafeProxyPriv.h b/src/gpu/GrContextThreadSafeProxyPriv.h
index 00662b7..c4873bb 100644
--- a/src/gpu/GrContextThreadSafeProxyPriv.h
+++ b/src/gpu/GrContextThreadSafeProxyPriv.h
@@ -9,6 +9,7 @@
 #define GrContextThreadSafeProxyPriv_DEFINED
 
 #include "include/gpu/GrContextThreadSafeProxy.h"
+#include "include/private/GrContext_Base.h"
 
 #include "src/gpu/GrCaps.h"
 
@@ -19,24 +20,24 @@
  */
 class GrContextThreadSafeProxyPriv {
 public:
-    // from GrContext_Base
-    uint32_t contextID() const { return fProxy->contextID(); }
+    void init(sk_sp<const GrCaps> caps) const { fProxy->init(std::move(caps)); }
 
-    bool matches(GrContext_Base* candidate) const { return fProxy->matches(candidate); }
+    bool matches(GrContext_Base* candidate) const {
+        return fProxy == candidate->threadSafeProxy().get();
+    }
 
-    const GrContextOptions& options() const { return fProxy->options(); }
+    GrBackend backend() const { return fProxy->fBackend; }
+    const GrContextOptions& options() const { return fProxy->fOptions; }
+    uint32_t contextID() const { return fProxy->fContextID; }
 
-    const GrCaps* caps() const { return fProxy->caps(); }
-    sk_sp<const GrCaps> refCaps() const { return fProxy->refCaps(); }
+    const GrCaps* caps() const { return fProxy->fCaps.get(); }
+    sk_sp<const GrCaps> refCaps() const { return fProxy->fCaps; }
 
     void abandonContext() { fProxy->abandonContext(); }
     bool abandoned() const { return fProxy->abandoned(); }
 
     // GrContextThreadSafeProxyPriv
-    static sk_sp<GrContextThreadSafeProxy> Make(GrBackendApi,
-                                                const GrContextOptions&,
-                                                uint32_t contextID,
-                                                sk_sp<const GrCaps>);
+    static sk_sp<GrContextThreadSafeProxy> Make(GrBackendApi, const GrContextOptions&);
 
 private:
     explicit GrContextThreadSafeProxyPriv(GrContextThreadSafeProxy* proxy) : fProxy(proxy) {}
diff --git a/src/gpu/GrContext_Base.cpp b/src/gpu/GrContext_Base.cpp
index 24a7f20..3426299 100644
--- a/src/gpu/GrContext_Base.cpp
+++ b/src/gpu/GrContext_Base.cpp
@@ -13,52 +13,31 @@
 #include "src/gpu/GrShaderUtils.h"
 #include "src/gpu/effects/GrSkSLFP.h"
 
-static int32_t next_id() {
-    static std::atomic<int32_t> nextID{1};
-    int32_t id;
-    do {
-        id = nextID++;
-    } while (id == SK_InvalidGenID);
-    return id;
-}
-
-GrContext_Base::GrContext_Base(GrBackendApi backend,
-                               const GrContextOptions& options,
-                               uint32_t contextID)
-        : fBackend(backend)
-        , fOptions(options)
-        , fContextID(SK_InvalidGenID == contextID ? next_id() : contextID) {
+GrContext_Base::GrContext_Base(sk_sp<GrContextThreadSafeProxy> proxy)
+        : fThreadSafeProxy(std::move(proxy)) {
 }
 
 GrContext_Base::~GrContext_Base() { }
 
-bool GrContext_Base::init(sk_sp<const GrCaps> caps) {
-    SkASSERT(caps);
-    // We either are a thread safe proxy and we don't have one, or we aren't and we do.
-    SkASSERT((nullptr == this->asThreadSafeProxy()) != (nullptr == fThreadSafeProxy));
+bool GrContext_Base::init() {
+    SkASSERT(fThreadSafeProxy->isValid());
 
-    fCaps = std::move(caps);
     return true;
 }
 
-const GrCaps* GrContext_Base::caps() const { return fCaps.get(); }
-sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fCaps; }
+uint32_t GrContext_Base::contextID() const { return fThreadSafeProxy->priv().contextID(); }
+GrBackendApi GrContext_Base::backend() const { return fThreadSafeProxy->priv().backend(); }
+
+const GrContextOptions& GrContext_Base::options() const {
+    return fThreadSafeProxy->priv().options();
+}
+
+const GrCaps* GrContext_Base::caps() const { return fThreadSafeProxy->priv().caps(); }
+sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fThreadSafeProxy->priv().refCaps(); }
 
 GrBackendFormat GrContext_Base::defaultBackendFormat(SkColorType skColorType,
                                                      GrRenderable renderable) const {
-    const GrCaps* caps = this->caps();
-
-    GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
-
-    GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, renderable);
-    if (!format.isValid()) {
-        return GrBackendFormat();
-    }
-
-    SkASSERT(renderable == GrRenderable::kNo ||
-             caps->isFormatAsColorTypeRenderable(grColorType, format));
-
-    return format;
+    return fThreadSafeProxy->defaultBackendFormat(skColorType, renderable);
 }
 
 GrBackendFormat GrContext_Base::compressedBackendFormat(SkImage::CompressionType c) const {
@@ -70,6 +49,8 @@
     return format;
 }
 
+sk_sp<GrContextThreadSafeProxy> GrContext_Base::threadSafeProxy() { return fThreadSafeProxy; }
+
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 sk_sp<const GrCaps> GrBaseContextPriv::refCaps() const {
     return fContext->refCaps();
diff --git a/src/gpu/GrDDLContext.cpp b/src/gpu/GrDDLContext.cpp
index 09f7f4f..159d99d 100644
--- a/src/gpu/GrDDLContext.cpp
+++ b/src/gpu/GrDDLContext.cpp
@@ -21,8 +21,7 @@
 class GrDDLContext final : public GrContext {
 public:
     GrDDLContext(sk_sp<GrContextThreadSafeProxy> proxy)
-            : INHERITED(proxy->backend(), proxy->priv().options(), proxy->priv().contextID()) {
-        fThreadSafeProxy = std::move(proxy);
+        : INHERITED(std::move(proxy)) {
     }
 
     ~GrDDLContext() override {}
@@ -47,10 +46,8 @@
     // GrRecordingContext!
     GrContext* asDirectContext() override { return nullptr; }
 
-    bool init(sk_sp<const GrCaps> caps) override {
-        SkASSERT(caps);
-
-        if (!INHERITED::init(std::move(caps))) {
+    bool init() override {
+        if (!INHERITED::init()) {
             return false;
         }
 
@@ -58,8 +55,6 @@
         // splitting.
         this->setupDrawingManager(true, true);
 
-        SkASSERT(this->caps());
-
         return true;
     }
 
@@ -152,10 +147,10 @@
     typedef GrContext INHERITED;
 };
 
-sk_sp<GrContext> GrContextPriv::MakeDDL(const sk_sp<GrContextThreadSafeProxy>& proxy) {
-    sk_sp<GrContext> context(new GrDDLContext(proxy));
+sk_sp<GrContext> GrContextPriv::MakeDDL(sk_sp<GrContextThreadSafeProxy> proxy) {
+    sk_sp<GrContext> context(new GrDDLContext(std::move(proxy)));
 
-    if (!context->init(proxy->priv().refCaps())) {
+    if (!context->init()) {
         return nullptr;
     }
     return context;
diff --git a/src/gpu/GrImageContext.cpp b/src/gpu/GrImageContext.cpp
index 560ca6c..8993a38 100644
--- a/src/gpu/GrImageContext.cpp
+++ b/src/gpu/GrImageContext.cpp
@@ -14,10 +14,8 @@
 #include "src/gpu/effects/GrSkSLFP.h"
 
 ///////////////////////////////////////////////////////////////////////////////////////////////////
-GrImageContext::GrImageContext(GrBackendApi backend,
-                               const GrContextOptions& options,
-                               uint32_t contextID)
-            : INHERITED(backend, options, contextID) {
+GrImageContext::GrImageContext(sk_sp<GrContextThreadSafeProxy> proxy)
+            : INHERITED(std::move(proxy)) {
     fProxyProvider.reset(new GrProxyProvider(this));
 }
 
diff --git a/src/gpu/GrLegacyDirectContext.cpp b/src/gpu/GrLegacyDirectContext.cpp
index 595807e..07be4fb 100644
--- a/src/gpu/GrLegacyDirectContext.cpp
+++ b/src/gpu/GrLegacyDirectContext.cpp
@@ -40,7 +40,7 @@
 class GrLegacyDirectContext : public GrContext {
 public:
     GrLegacyDirectContext(GrBackendApi backend, const GrContextOptions& options)
-            : INHERITED(backend, options)
+            : INHERITED(GrContextThreadSafeProxyPriv::Make(backend, options))
             , fAtlasManager(nullptr) {
     }
 
@@ -72,16 +72,14 @@
     }
 
 protected:
-    bool init(sk_sp<const GrCaps> caps) override {
-        SkASSERT(caps);
-        SkASSERT(!fThreadSafeProxy);
+    bool init() override {
+        const GrGpu* gpu = this->priv().getGpu();
+        if (!gpu) {
+            return false;
+        }
 
-        fThreadSafeProxy = GrContextThreadSafeProxyPriv::Make(this->backend(),
-                                                              this->options(),
-                                                              this->contextID(),
-                                                              caps);
-
-        if (!INHERITED::init(std::move(caps))) {
+        fThreadSafeProxy->priv().init(gpu->refCaps());
+        if (!INHERITED::init()) {
             return false;
         }
 
@@ -94,8 +92,6 @@
 
         this->setupDrawingManager(true, reduceOpsTaskSplitting);
 
-        SkASSERT(this->caps());
-
         GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
         if (GrContextOptions::Enable::kNo == this->options().fAllowMultipleGlyphCacheTextures ||
             // multitexturing supported only if range can represent the index + texcoords fully
@@ -144,11 +140,7 @@
     sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kOpenGL, options));
 
     context->fGpu = GrGLGpu::Make(std::move(glInterface), options, context.get());
-    if (!context->fGpu) {
-        return nullptr;
-    }
-
-    if (!context->init(context->fGpu->refCaps())) {
+    if (!context->init()) {
         return nullptr;
     }
     return context;
@@ -165,11 +157,7 @@
     sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMock, options));
 
     context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
-    if (!context->fGpu) {
-        return nullptr;
-    }
-
-    if (!context->init(context->fGpu->refCaps())) {
+    if (!context->init()) {
         return nullptr;
     }
 
@@ -191,13 +179,10 @@
     sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kVulkan, options));
 
     context->fGpu = GrVkGpu::Make(backendContext, options, context.get());
-    if (!context->fGpu) {
+    if (!context->init()) {
         return nullptr;
     }
 
-    if (!context->init(context->fGpu->refCaps())) {
-        return nullptr;
-    }
     return context;
 #else
     return nullptr;
@@ -214,13 +199,10 @@
     sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kMetal, options));
 
     context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
-    if (!context->fGpu) {
+    if (!context->init()) {
         return nullptr;
     }
 
-    if (!context->init(context->fGpu->refCaps())) {
-        return nullptr;
-    }
     return context;
 }
 #endif
@@ -236,13 +218,10 @@
     sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDirect3D, options));
 
     context->fGpu = GrD3DGpu::Make(backendContext, options, context.get());
-    if (!context->fGpu) {
+    if (!context->init()) {
         return nullptr;
     }
 
-    if (!context->init(context->fGpu->refCaps())) {
-        return nullptr;
-    }
     return context;
 }
 #endif
@@ -257,13 +236,10 @@
     sk_sp<GrContext> context(new GrLegacyDirectContext(GrBackendApi::kDawn, options));
 
     context->fGpu = GrDawnGpu::Make(device, options, context.get());
-    if (!context->fGpu) {
+    if (!context->init()) {
         return nullptr;
     }
 
-    if (!context->init(context->fGpu->refCaps())) {
-        return nullptr;
-    }
     return context;
 }
 #endif
diff --git a/src/gpu/GrRecordingContext.cpp b/src/gpu/GrRecordingContext.cpp
index a588ebc..1743c04 100644
--- a/src/gpu/GrRecordingContext.cpp
+++ b/src/gpu/GrRecordingContext.cpp
@@ -8,6 +8,7 @@
 #include "include/private/GrRecordingContext.h"
 
 #include "include/gpu/GrContext.h"
+#include "include/gpu/GrContextThreadSafeProxy.h"
 #include "src/core/SkArenaAlloc.h"
 #include "src/gpu/GrAuditTrail.h"
 #include "src/gpu/GrCaps.h"
@@ -35,18 +36,16 @@
 
 GrRecordingContext::ProgramData::~ProgramData() = default;
 
-GrRecordingContext::GrRecordingContext(GrBackendApi backend,
-                                       const GrContextOptions& options,
-                                       uint32_t contextID)
-        : INHERITED(backend, options, contextID)
+GrRecordingContext::GrRecordingContext(sk_sp<GrContextThreadSafeProxy> proxy)
+        : INHERITED(std::move(proxy))
         , fAuditTrail(new GrAuditTrail()) {
 }
 
 GrRecordingContext::~GrRecordingContext() = default;
 
-bool GrRecordingContext::init(sk_sp<const GrCaps> caps) {
+bool GrRecordingContext::init() {
 
-    if (!INHERITED::init(std::move(caps))) {
+    if (!INHERITED::init()) {
         return false;
     }