Metal: Use GrMtlFramebuffer than than GrRenderTarget where possible.

Bug: skia:12186
Change-Id: Ifb08a05619f856e1299ecddab0a88a0ed132dd57
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/430636
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
diff --git a/src/gpu/mtl/GrMtlOpsRenderPass.mm b/src/gpu/mtl/GrMtlOpsRenderPass.mm
index f232a99..a1b64aa 100644
--- a/src/gpu/mtl/GrMtlOpsRenderPass.mm
+++ b/src/gpu/mtl/GrMtlOpsRenderPass.mm
@@ -45,7 +45,7 @@
 }
 
 void GrMtlOpsRenderPass::submit() {
-    if (!fRenderTarget) {
+    if (!fFramebuffer) {
         return;
     }
     SkIRect iBounds;
@@ -87,7 +87,7 @@
         return false;
     }
 
-    fActivePipelineState->setData(fRenderTarget, programInfo);
+    fActivePipelineState->setData(fFramebuffer.get(), programInfo);
     fCurrentVertexStride = programInfo.geomProc().vertexStride();
 
     if (!fActiveRenderCmdEncoder) {
@@ -113,10 +113,11 @@
 
     if (!programInfo.pipeline().isScissorTestEnabled()) {
         // "Disable" scissor by setting it to the full pipeline bounds.
+        SkISize dimensions = fFramebuffer->colorAttachment()->dimensions();
         GrMtlPipelineState::SetDynamicScissorRectState(fActiveRenderCmdEncoder,
-                                                       fRenderTarget, fOrigin,
-                                                       SkIRect::MakeWH(fRenderTarget->width(),
-                                                                       fRenderTarget->height()));
+                                                       dimensions, fOrigin,
+                                                       SkIRect::MakeWH(dimensions.width(),
+                                                                       dimensions.height()));
     }
 
     fActivePrimitiveType = gr_to_mtl_primitive(programInfo.primitiveType());
@@ -127,7 +128,8 @@
 void GrMtlOpsRenderPass::onSetScissorRect(const SkIRect& scissor) {
     SkASSERT(fActivePipelineState);
     SkASSERT(fActiveRenderCmdEncoder);
-    GrMtlPipelineState::SetDynamicScissorRectState(fActiveRenderCmdEncoder, fRenderTarget,
+    GrMtlPipelineState::SetDynamicScissorRectState(fActiveRenderCmdEncoder,
+                                                   fFramebuffer->colorAttachment()->dimensions(),
                                                    fOrigin, scissor);
 }
 
@@ -166,7 +168,7 @@
     // Partial clears are not supported
     SkASSERT(!scissor.enabled());
 
-    GrAttachment* sb = fRenderTarget->getStencilAttachment();
+    GrAttachment* sb = fFramebuffer->stencilAttachment();
     // this should only be called internally when we know we have a
     // stencil buffer.
     SkASSERT(sb);
@@ -201,10 +203,12 @@
     encoder->pushDebugGroup(@"initRenderState");
 #endif
     encoder->setFrontFacingWinding(MTLWindingCounterClockwise);
+    SkISize colorAttachmentDimensions = fFramebuffer->colorAttachment()->dimensions();
     // Strictly speaking we shouldn't have to set this, as the default viewport is the size of
     // the drawable used to generate the renderCommandEncoder -- but just in case.
     MTLViewport viewport = { 0.0, 0.0,
-                             (double) fRenderTarget->width(), (double) fRenderTarget->height(),
+                             (double) colorAttachmentDimensions.width(),
+                             (double) colorAttachmentDimensions.height(),
                              0.0, 1.0 };
     encoder->setViewport(viewport);
 #ifdef SK_ENABLE_MTL_DEBUG_INFO
@@ -237,15 +241,15 @@
 
     fRenderPassDesc = [MTLRenderPassDescriptor new];
     auto colorAttachment = fRenderPassDesc.colorAttachments[0];
-    colorAttachment.texture =
-            static_cast<GrMtlRenderTarget*>(fRenderTarget)->colorMTLTexture();
+    auto* color = fFramebuffer->colorAttachment();
+    colorAttachment.texture = color->mtlTexture();
     const std::array<float, 4>& clearColor = colorInfo.fClearColor;
     colorAttachment.clearColor =
             MTLClearColorMake(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
     colorAttachment.loadAction = mtlLoadAction[static_cast<int>(colorInfo.fLoadOp)];
     colorAttachment.storeAction = mtlStoreAction[static_cast<int>(colorInfo.fStoreOp)];
 
-    auto* stencil = static_cast<GrMtlAttachment*>(fRenderTarget->getStencilAttachment());
+    auto* stencil = fFramebuffer->stencilAttachment();
     auto mtlStencil = fRenderPassDesc.stencilAttachment;
     if (stencil) {
         mtlStencil.texture = stencil->mtlTexture();
@@ -256,8 +260,8 @@
 
     // Manage initial clears
     if (colorInfo.fLoadOp == GrLoadOp::kClear || stencilInfo.fLoadOp == GrLoadOp::kClear)  {
-        fBounds = SkRect::MakeWH(fRenderTarget->width(),
-                                 fRenderTarget->height());
+        fBounds = SkRect::MakeWH(color->dimensions().width(),
+                                 color->dimensions().height());
         this->precreateCmdEncoder();
         if (colorInfo.fLoadOp == GrLoadOp::kClear) {
             colorAttachment.loadAction = MTLLoadActionLoad;
diff --git a/src/gpu/mtl/GrMtlPipelineState.h b/src/gpu/mtl/GrMtlPipelineState.h
index 6544c8b..5ca9ce4 100644
--- a/src/gpu/mtl/GrMtlPipelineState.h
+++ b/src/gpu/mtl/GrMtlPipelineState.h
@@ -16,6 +16,7 @@
 
 #import <Metal/Metal.h>
 
+class GrMtlFramebuffer;
 class GrMtlGpu;
 class GrMtlPipelineStateDataManager;
 class GrMtlRenderCommandEncoder;
@@ -46,7 +47,7 @@
 
     id<MTLRenderPipelineState> mtlPipelineState() { return fPipelineState; }
 
-    void setData(const GrRenderTarget*, const GrProgramInfo&);
+    void setData(GrMtlFramebuffer*, const GrProgramInfo&);
 
     void setTextures(const GrGeometryProcessor&,
                      const GrPipeline&,
@@ -58,7 +59,7 @@
                       const GrXferProcessor&);
 
     static void SetDynamicScissorRectState(GrMtlRenderCommandEncoder* renderCmdEncoder,
-                                           const GrRenderTarget* renderTarget,
+                                           SkISize colorAttachmentDimensions,
                                            GrSurfaceOrigin rtOrigin,
                                            SkIRect scissorRect);
 
@@ -82,7 +83,7 @@
         }
     };
 
-    void setRenderTargetState(const GrRenderTarget*, GrSurfaceOrigin);
+    void setRenderTargetState(SkISize colorAttachmentDimensions, GrSurfaceOrigin);
 
     void bindUniforms(GrMtlRenderCommandEncoder*);
 
diff --git a/src/gpu/mtl/GrMtlPipelineState.mm b/src/gpu/mtl/GrMtlPipelineState.mm
index b0f7bf1..5daf6ea 100644
--- a/src/gpu/mtl/GrMtlPipelineState.mm
+++ b/src/gpu/mtl/GrMtlPipelineState.mm
@@ -7,6 +7,7 @@
 
 #include "src/gpu/mtl/GrMtlPipelineState.h"
 
+#include "src/gpu/GrBackendUtils.h"
 #include "src/gpu/GrPipeline.h"
 #include "src/gpu/GrRenderTarget.h"
 #include "src/gpu/GrTexture.h"
@@ -15,6 +16,7 @@
 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
 #include "src/gpu/glsl/GrGLSLXferProcessor.h"
 #include "src/gpu/mtl/GrMtlBuffer.h"
+#include "src/gpu/mtl/GrMtlFramebuffer.h"
 #include "src/gpu/mtl/GrMtlGpu.h"
 #include "src/gpu/mtl/GrMtlRenderCommandEncoder.h"
 #include "src/gpu/mtl/GrMtlTexture.h"
@@ -55,9 +57,11 @@
     (void) fPixelFormat; // Suppress unused-var warning.
 }
 
-void GrMtlPipelineState::setData(const GrRenderTarget* renderTarget,
+void GrMtlPipelineState::setData(GrMtlFramebuffer* framebuffer,
                                  const GrProgramInfo& programInfo) {
-    this->setRenderTargetState(renderTarget, programInfo.origin());
+    SkISize colorAttachmentDimensions = framebuffer->colorAttachment()->dimensions();
+
+    this->setRenderTargetState(colorAttachmentDimensions, programInfo.origin());
     fGeometryProcessor->setData(fDataManager, *fGpu->caps()->shaderCaps(), programInfo.geomProc());
 
     for (int i = 0; i < programInfo.pipeline().numFragmentProcessors(); ++i) {
@@ -74,8 +78,9 @@
 
 #ifdef SK_DEBUG
     if (programInfo.isStencilEnabled()) {
-        SkASSERT(renderTarget->getStencilAttachment());
-        SkASSERT(renderTarget->numStencilBits(renderTarget->numSamples() > 1) == 8);
+        SkDEBUGCODE(const GrAttachment* stencil = framebuffer->stencilAttachment());
+        SkASSERT(stencil);
+        SkASSERT(GrBackendFormatStencilBits(stencil->backendFormat()) == 8);
     }
 #endif
 
@@ -125,24 +130,24 @@
     }
 }
 
-void GrMtlPipelineState::setRenderTargetState(const GrRenderTarget* rt, GrSurfaceOrigin origin) {
-    // Set RT adjustment and RT flip
-    SkISize dimensions = rt->dimensions();
+void GrMtlPipelineState::setRenderTargetState(SkISize colorAttachmentDimensions,
+                                              GrSurfaceOrigin origin) {
     SkASSERT(fBuiltinUniformHandles.fRTAdjustmentUni.isValid());
     if (fRenderTargetState.fRenderTargetOrigin != origin ||
-        fRenderTargetState.fRenderTargetSize != dimensions) {
-        fRenderTargetState.fRenderTargetSize = dimensions;
+        fRenderTargetState.fRenderTargetSize != colorAttachmentDimensions) {
+        fRenderTargetState.fRenderTargetSize = colorAttachmentDimensions;
         fRenderTargetState.fRenderTargetOrigin = origin;
 
         // The client will mark a swap buffer as kTopLeft when making a SkSurface because
         // Metal's framebuffer space has (0, 0) at the top left. This agrees with Skia's device
         // coords. However, in NDC (-1, -1) is the bottom left. So we flip when origin is kTopLeft.
         bool flip = (origin == kTopLeft_GrSurfaceOrigin);
-        std::array<float, 4> v = SkSL::Compiler::GetRTAdjustVector(dimensions, flip);
+        std::array<float, 4> v = SkSL::Compiler::GetRTAdjustVector(colorAttachmentDimensions, flip);
         fDataManager.set4fv(fBuiltinUniformHandles.fRTAdjustmentUni, 1, v.data());
         if (fBuiltinUniformHandles.fRTFlipUni.isValid()) {
             // Note above that framebuffer space has origin top left. So we need !flip here.
-            std::array<float, 2> d = SkSL::Compiler::GetRTFlipVector(rt->height(), !flip);
+            std::array<float, 2> d =
+                    SkSL::Compiler::GetRTFlipVector(colorAttachmentDimensions.height(), !flip);
             fDataManager.set2fv(fBuiltinUniformHandles.fRTFlipUni, 1, d.data());
         }
     }
@@ -189,10 +194,11 @@
 }
 
 void GrMtlPipelineState::SetDynamicScissorRectState(GrMtlRenderCommandEncoder* renderCmdEncoder,
-                                                    const GrRenderTarget* renderTarget,
+                                                    SkISize colorAttachmentDimensions,
                                                     GrSurfaceOrigin rtOrigin,
                                                     SkIRect scissorRect) {
-    if (!scissorRect.intersect(SkIRect::MakeWH(renderTarget->width(), renderTarget->height()))) {
+    if (!scissorRect.intersect(SkIRect::MakeWH(colorAttachmentDimensions.width(),
+                                               colorAttachmentDimensions.height()))) {
         scissorRect.setEmpty();
     }
 
@@ -203,7 +209,7 @@
         scissor.y = scissorRect.fTop;
     } else {
         SkASSERT(kBottomLeft_GrSurfaceOrigin == rtOrigin);
-        scissor.y = renderTarget->height() - scissorRect.fBottom;
+        scissor.y = colorAttachmentDimensions.height() - scissorRect.fBottom;
     }
     scissor.height = scissorRect.height();