Stop using GrPixelConfig in Metal & Dawn's GrProgramDesc creation

In order to be able to create GrProgramDescs pre-flush we would like as little reliance on the GrRenderTarget as possible (also GrPixelConfig is going away).

Bug: skia:9455
Change-Id: Ie35c87758bb1ed97c99c6e9e130eaeb3355f4139
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/256457
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Commit-Queue: Robert Phillips <robertphillips@google.com>
diff --git a/src/gpu/GrDDLContext.cpp b/src/gpu/GrDDLContext.cpp
index 4d14504..b1fc62e 100644
--- a/src/gpu/GrDDLContext.cpp
+++ b/src/gpu/GrDDLContext.cpp
@@ -22,29 +22,29 @@
         fThreadSafeProxy = std::move(proxy);
     }
 
-    ~GrDDLContext() override { }
+    ~GrDDLContext() final { }
 
-    void abandonContext() override {
+    void abandonContext() final {
         SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense
         INHERITED::abandonContext();
     }
 
-    void releaseResourcesAndAbandonContext() override {
+    void releaseResourcesAndAbandonContext() final {
         SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense
         INHERITED::releaseResourcesAndAbandonContext();
     }
 
-    void freeGpuResources() override {
+    void freeGpuResources() final {
         SkASSERT(0); // freeing resources in a DDL Recorder doesn't make a whole lot of sense
         INHERITED::freeGpuResources();
     }
 
-protected:
+private:
     // TODO: Here we're pretending this isn't derived from GrContext. Switch this to be derived from
     // GrRecordingContext!
-    GrContext* asDirectContext() override { return nullptr; }
+    GrContext* asDirectContext() final { return nullptr; }
 
-    bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) override {
+    bool init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) final {
         SkASSERT(caps && FPFactoryCache);
         SkASSERT(fThreadSafeProxy); // should've been set in the ctor
 
@@ -61,12 +61,11 @@
         return true;
     }
 
-    GrAtlasManager* onGetAtlasManager() override {
+    GrAtlasManager* onGetAtlasManager() final {
         SkASSERT(0);   // the DDL Recorders should never invoke this
         return nullptr;
     }
 
-private:
     typedef GrContext INHERITED;
 };
 
diff --git a/src/gpu/GrDrawingManager.cpp b/src/gpu/GrDrawingManager.cpp
index 07db44e..94c7015 100644
--- a/src/gpu/GrDrawingManager.cpp
+++ b/src/gpu/GrDrawingManager.cpp
@@ -593,7 +593,7 @@
 }
 
 void GrDrawingManager::copyRenderTasksFromDDL(const SkDeferredDisplayList* ddl,
-                                          GrRenderTargetProxy* newDest) {
+                                              GrRenderTargetProxy* newDest) {
     SkDEBUGCODE(this->validate());
 
     if (fActiveOpsTask) {
diff --git a/src/gpu/GrOpFlushState.cpp b/src/gpu/GrOpFlushState.cpp
index 12fe0e2..60ffa51 100644
--- a/src/gpu/GrOpFlushState.cpp
+++ b/src/gpu/GrOpFlushState.cpp
@@ -56,7 +56,8 @@
 
         GrProgramInfo programInfo(this->proxy()->numSamples(),
                                   this->proxy()->numStencilSamples(),
-                                  this->proxy()->origin(),
+                                  this->proxy()->backendFormat(),
+                                  this->view()->origin(),
                                   pipeline,
                                   fCurrDraw->fGeometryProcessor,
                                   fCurrDraw->fFixedDynamicState,
diff --git a/src/gpu/GrProgramDesc.cpp b/src/gpu/GrProgramDesc.cpp
index d0b4f23..1821790 100644
--- a/src/gpu/GrProgramDesc.cpp
+++ b/src/gpu/GrProgramDesc.cpp
@@ -174,6 +174,12 @@
 bool GrProgramDesc::Build(GrProgramDesc* desc, const GrRenderTarget* renderTarget,
                           const GrProgramInfo& programInfo, const GrCaps& caps) {
 
+#ifdef SK_DEBUG
+    if (renderTarget) {
+        SkASSERT(programInfo.backendFormat() == renderTarget->backendFormat());
+    }
+#endif
+
     // The descriptor is used as a cache key. Thus when a field of the
     // descriptor will not affect program generation (because of the attribute
     // bindings in use or other descriptor field settings) it should be set
diff --git a/src/gpu/GrProgramInfo.h b/src/gpu/GrProgramInfo.h
index a5ce0da..2cad573 100644
--- a/src/gpu/GrProgramInfo.h
+++ b/src/gpu/GrProgramInfo.h
@@ -19,6 +19,7 @@
 public:
     GrProgramInfo(int numSamples,
                   int numStencilSamples,
+                  const GrBackendFormat& backendFormat,
                   GrSurfaceOrigin origin,
                   const GrPipeline* pipeline,
                   const GrPrimitiveProcessor* primProc,
@@ -28,6 +29,7 @@
                   GrPrimitiveType primitiveType)
             : fNumRasterSamples(pipeline->isStencilEnabled() ? numStencilSamples : numSamples)
             , fIsMixedSampled(fNumRasterSamples > numSamples)
+            , fBackendFormat(backendFormat)
             , fOrigin(origin)
             , fPipeline(pipeline)
             , fPrimProc(primProc)
@@ -50,6 +52,8 @@
 
     int numRasterSamples() const { return fNumRasterSamples;  }
     bool isMixedSampled() const { return fIsMixedSampled; }
+    // The backend format of the destination render target [proxy]
+    const GrBackendFormat& backendFormat() const { return fBackendFormat; }
     GrSurfaceOrigin origin() const { return fOrigin;  }
     const GrPipeline& pipeline() const { return *fPipeline; }
     const GrPrimitiveProcessor& primProc() const { return *fPrimProc; }
@@ -121,6 +125,7 @@
 private:
     const int                             fNumRasterSamples;
     const bool                            fIsMixedSampled;
+    const GrBackendFormat                 fBackendFormat;
     const GrSurfaceOrigin                 fOrigin;
     const GrPipeline*                     fPipeline;
     const GrPrimitiveProcessor*           fPrimProc;
diff --git a/src/gpu/ccpr/GrCCCoverageProcessor.cpp b/src/gpu/ccpr/GrCCCoverageProcessor.cpp
index e6929bc..be90497 100644
--- a/src/gpu/ccpr/GrCCCoverageProcessor.cpp
+++ b/src/gpu/ccpr/GrCCCoverageProcessor.cpp
@@ -207,7 +207,8 @@
 
     GrProgramInfo programInfo(flushState->proxy()->numSamples(),
                               flushState->proxy()->numStencilSamples(),
-                              flushState->drawOpArgs().origin(),
+                              flushState->proxy()->backendFormat(),
+                              flushState->view()->origin(),
                               &pipeline,
                               this,
                               nullptr,
diff --git a/src/gpu/ccpr/GrCCPathProcessor.cpp b/src/gpu/ccpr/GrCCPathProcessor.cpp
index be92d8b..0c97eaf 100644
--- a/src/gpu/ccpr/GrCCPathProcessor.cpp
+++ b/src/gpu/ccpr/GrCCPathProcessor.cpp
@@ -143,7 +143,8 @@
 
     GrProgramInfo programInfo(flushState->proxy()->numSamples(),
                               flushState->proxy()->numStencilSamples(),
-                              flushState->drawOpArgs().origin(),
+                              flushState->proxy()->backendFormat(),
+                              flushState->view()->origin(),
                               &pipeline,
                               this,
                               fixedDynamicState,
diff --git a/src/gpu/ccpr/GrCCStroker.cpp b/src/gpu/ccpr/GrCCStroker.cpp
index d2b21ec..ccb687b 100644
--- a/src/gpu/ccpr/GrCCStroker.cpp
+++ b/src/gpu/ccpr/GrCCStroker.cpp
@@ -781,7 +781,8 @@
 
     GrProgramInfo programInfo(flushState->proxy()->numSamples(),
                               flushState->proxy()->numStencilSamples(),
-                              flushState->drawOpArgs().origin(),
+                              flushState->proxy()->backendFormat(),
+                              flushState->view()->origin(),
                               &pipeline,
                               &processor,
                               nullptr,
diff --git a/src/gpu/ccpr/GrStencilAtlasOp.cpp b/src/gpu/ccpr/GrStencilAtlasOp.cpp
index 9c146a8..0c38f07 100644
--- a/src/gpu/ccpr/GrStencilAtlasOp.cpp
+++ b/src/gpu/ccpr/GrStencilAtlasOp.cpp
@@ -157,7 +157,8 @@
 
     GrProgramInfo programInfo(flushState->proxy()->numSamples(),
                               flushState->proxy()->numStencilSamples(),
-                              flushState->drawOpArgs().origin(),
+                              flushState->proxy()->backendFormat(),
+                              flushState->view()->origin(),
                               &resolvePipeline,
                               &primProc,
                               &scissorRectState,
diff --git a/src/gpu/dawn/GrDawnGpu.cpp b/src/gpu/dawn/GrDawnGpu.cpp
index 009b450..ba9a2ff 100644
--- a/src/gpu/dawn/GrDawnGpu.cpp
+++ b/src/gpu/dawn/GrDawnGpu.cpp
@@ -93,12 +93,18 @@
         if (!GrProgramDesc::Build(desc, rt, programInfo, caps)) {
             return false;
         }
+
+        wgpu::TextureFormat format;
+        if (!programInfo.backendFormat().asDawnFormat(&format)) {
+            return false;
+        }
+
         GrProcessorKeyBuilder b(&desc->key());
 
         GrStencilSettings stencil = programInfo.nonGLStencilSettings();
         stencil.genKey(&b);
 
-        b.add32(rt->config());
+        b.add32(static_cast<uint32_t>(format));
         b.add32(static_cast<int32_t>(hasDepthStencil));
         b.add32(get_blend_info_key(programInfo.pipeline()));
         b.add32(static_cast<uint32_t>(programInfo.primitiveType()));
@@ -630,7 +636,8 @@
     }
 
     wgpu::TextureFormat colorFormat;
-    SkAssertResult(GrPixelConfigToDawnFormat(rt->config(), &colorFormat));
+    SkAssertResult(programInfo.backendFormat().asDawnFormat(&colorFormat));
+
     wgpu::TextureFormat stencilFormat = wgpu::TextureFormat::Depth24PlusStencil8;
 
     sk_sp<GrDawnProgram> program = GrDawnProgramBuilder::Build(
diff --git a/src/gpu/mtl/GrMtlPipelineStateBuilder.mm b/src/gpu/mtl/GrMtlPipelineStateBuilder.mm
index a0a673f..83eb7da 100644
--- a/src/gpu/mtl/GrMtlPipelineStateBuilder.mm
+++ b/src/gpu/mtl/GrMtlPipelineStateBuilder.mm
@@ -296,12 +296,10 @@
 }
 
 static MTLRenderPipelineColorAttachmentDescriptor* create_color_attachment(
-        GrPixelConfig config, const GrPipeline& pipeline) {
+        MTLPixelFormat format, const GrPipeline& pipeline) {
     auto mtlColorAttachment = [[MTLRenderPipelineColorAttachmentDescriptor alloc] init];
 
     // pixel format
-    MTLPixelFormat format;
-    SkAssertResult(GrPixelConfigToMTLFormat(config, &format));
     mtlColorAttachment.pixelFormat = format;
 
     // blending
@@ -391,7 +389,13 @@
     pipelineDescriptor.vertexFunction = vertexFunction;
     pipelineDescriptor.fragmentFunction = fragmentFunction;
     pipelineDescriptor.vertexDescriptor = create_vertex_descriptor(programInfo.primProc());
-    pipelineDescriptor.colorAttachments[0] = create_color_attachment(renderTarget->config(),
+
+    MTLPixelFormat pixelFormat = GrBackendFormatAsMTLPixelFormat(renderTarget->backendFormat());
+    if (pixelFormat == MTLPixelFormatInvalid) {
+        return nullptr;
+    }
+
+    pipelineDescriptor.colorAttachments[0] = create_color_attachment(pixelFormat,
                                                                      programInfo.pipeline());
     pipelineDescriptor.sampleCount = programInfo.numRasterSamples();
     bool hasStencilAttachment = SkToBool(renderTarget->renderTargetPriv().getStencilAttachment());
@@ -456,13 +460,18 @@
 
     GrProcessorKeyBuilder b(&desc->key());
 
-    b.add32(renderTarget->config());
+    b.add32(programInfo.backendFormat().asMtlFormat());
+
     b.add32(programInfo.numRasterSamples());
 
-    bool hasStencilAttachment = SkToBool(renderTarget->renderTargetPriv().getStencilAttachment());
-    SkASSERT(!programInfo.pipeline().isStencilEnabled() || hasStencilAttachment);
+#ifdef SK_DEBUG
+    if (renderTarget && programInfo.pipeline().isStencilEnabled()) {
+        SkASSERT(renderTarget->renderTargetPriv().getStencilAttachment());
+    }
+#endif
 
-    b.add32(hasStencilAttachment ? caps.preferredStencilFormat().fInternalFormat
+    b.add32(programInfo.pipeline().isStencilEnabled()
+                                 ? caps.preferredStencilFormat().fInternalFormat
                                  : MTLPixelFormatInvalid);
     b.add32((uint32_t)programInfo.pipeline().isStencilEnabled());
     // Stencil samples don't seem to be tracked in the MTLRenderPipeline
diff --git a/src/gpu/ops/GrDrawPathOp.cpp b/src/gpu/ops/GrDrawPathOp.cpp
index 51f5291..f2fdbb2 100644
--- a/src/gpu/ops/GrDrawPathOp.cpp
+++ b/src/gpu/ops/GrDrawPathOp.cpp
@@ -103,7 +103,8 @@
 
     GrProgramInfo programInfo(state->proxy()->numSamples(),
                               state->proxy()->numStencilSamples(),
-                              state->drawOpArgs().origin(),
+                              state->proxy()->backendFormat(),
+                              state->view()->origin(),
                               &pipeline,
                               pathProc.get(),
                               fixedDynamicState,
diff --git a/src/gpu/ops/GrFillRRectOp.cpp b/src/gpu/ops/GrFillRRectOp.cpp
index c0ba65b..5858dc2 100644
--- a/src/gpu/ops/GrFillRRectOp.cpp
+++ b/src/gpu/ops/GrFillRRectOp.cpp
@@ -781,6 +781,7 @@
     GrRenderTargetProxy* dstProxy = dstView->asRenderTargetProxy();
     return arena->make<GrProgramInfo>(dstProxy->numSamples(),
                                       dstProxy->numStencilSamples(),
+                                      dstProxy->backendFormat(),
                                       dstView->origin(),
                                       pipeline,
                                       geomProc,