Store GrSwizzle on proxies for texturing and swizzling.

Bug: skia: 6718
Change-Id: I023d7507da9334e984ac8209a32323d616b3d79d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/214305
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h
index 025273d..1a6962c 100644
--- a/src/gpu/GrGpu.h
+++ b/src/gpu/GrGpu.h
@@ -11,13 +11,13 @@
 #include "include/core/SkPath.h"
 #include "include/core/SkSurface.h"
 #include "include/gpu/GrTypes.h"
+#include "include/private/GrSwizzle.h"
 #include "include/private/SkTArray.h"
 #include "src/gpu/GrAllocator.h"
 #include "src/gpu/GrCaps.h"
 #include "src/gpu/GrGpuCommandBuffer.h"
 #include "src/gpu/GrProgramDesc.h"
 #include "src/gpu/GrSamplePatternDictionary.h"
-#include "src/gpu/GrSwizzle.h"
 #include "src/gpu/GrTextureProducer.h"
 #include "src/gpu/GrXferProcessor.h"
 #include <map>
diff --git a/src/gpu/GrProxyProvider.cpp b/src/gpu/GrProxyProvider.cpp
index bb9ca00..8551983 100644
--- a/src/gpu/GrProxyProvider.cpp
+++ b/src/gpu/GrProxyProvider.cpp
@@ -154,11 +154,15 @@
         SkASSERT(!this->findProxyByUniqueKey(tex->getUniqueKey(), origin));
     }
 #endif
+    GrColorType colorType = GrPixelConfigToColorType(tex->config());
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
 
     if (tex->asRenderTarget()) {
-        return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
+        GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
+        return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin,
+                                                                    texSwizzle, outSwizzle));
     } else {
-        return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
+        return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
     }
 }
 
@@ -448,15 +452,20 @@
                 this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
     }
 
+    GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
+
     if (copyDesc.fFlags & kRenderTarget_GrSurfaceFlag) {
         // We know anything we instantiate later from this deferred path will be
         // both texturable and renderable
+        GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
         return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(*this->caps(), format, copyDesc,
-                                                                    origin, mipMapped,
-                                                                    fit, budgeted, surfaceFlags));
+                                                                    origin, mipMapped, texSwizzle,
+                                                                    outSwizzle, fit, budgeted,
+                                                                    surfaceFlags));
     }
 
-    return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped,
+    return sk_sp<GrTextureProxy>(new GrTextureProxy(format, copyDesc, origin, mipMapped, texSwizzle,
                                                     fit, budgeted, surfaceFlags));
 }
 
@@ -535,7 +544,10 @@
     // Make sure we match how we created the proxy with SkBudgeted::kNo
     SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
 
-    return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin));
+    GrColorType colorType = GrPixelConfigToColorType(tex->config());
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
+
+    return sk_sp<GrTextureProxy>(new GrTextureProxy(std::move(tex), origin, texSwizzle));
 }
 
 sk_sp<GrTextureProxy> GrProxyProvider::wrapRenderableBackendTexture(
@@ -576,7 +588,12 @@
     // Make sure we match how we created the proxy with SkBudgeted::kNo
     SkASSERT(GrBudgetedType::kBudgeted != tex->resourcePriv().budgetedType());
 
-    return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin));
+    GrColorType colorType = GrPixelConfigToColorType(tex->config());
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(tex->backendFormat(), colorType);
+    GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(tex->backendFormat(), colorType);
+
+    return sk_sp<GrTextureProxy>(new GrTextureRenderTargetProxy(std::move(tex), origin, texSwizzle,
+                                                                outSwizzle));
 }
 
 sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendRenderTarget(
@@ -592,8 +609,8 @@
         return nullptr;
     }
 
-#ifdef SK_DEBUG
     GrColorType colorType = GrPixelConfigToColorType(backendRT.config());
+#ifdef SK_DEBUG
     GrPixelConfig testConfig =
             this->caps()->validateBackendRenderTarget(backendRT,
                                                       GrColorTypeToSkColorType(colorType));
@@ -616,7 +633,11 @@
     // Make sure we match how we created the proxy with SkBudgeted::kNo
     SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
 
-    return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin));
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
+    GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
+
+    return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
+                                                              outSwizzle));
 }
 
 sk_sp<GrSurfaceProxy> GrProxyProvider::wrapBackendTextureAsRenderTarget(
@@ -646,7 +667,12 @@
     // This proxy should be unbudgeted because we're just wrapping an external resource
     SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
 
-    return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin));
+    GrColorType colorType = GrPixelConfigToColorType(rt->config());
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
+    GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
+
+    return sk_sp<GrSurfaceProxy>(new GrRenderTargetProxy(std::move(rt), origin, texSwizzle,
+                                                         outSwizzle));
 }
 
 sk_sp<GrRenderTargetProxy> GrProxyProvider::wrapVulkanSecondaryCBAsRenderTarget(
@@ -674,10 +700,14 @@
     // This proxy should be unbudgeted because we're just wrapping an external resource
     SkASSERT(GrBudgetedType::kBudgeted != rt->resourcePriv().budgetedType());
 
+    GrColorType colorType = GrPixelConfigToColorType(rt->config());
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(rt->backendFormat(), colorType);
+    GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(rt->backendFormat(), colorType);
+
     // All Vulkan surfaces uses top left origins.
     return sk_sp<GrRenderTargetProxy>(
             new GrRenderTargetProxy(std::move(rt),
-                                    kTopLeft_GrSurfaceOrigin,
+                                    kTopLeft_GrSurfaceOrigin, texSwizzle, outSwizzle,
                                     GrRenderTargetProxy::WrapsVkSecondaryCB::kYes));
 }
 
@@ -734,12 +764,17 @@
     }
 #endif
 
+    GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
+    GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
+
     return sk_sp<GrTextureProxy>(
             SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags)
                     ? new GrTextureRenderTargetProxy(std::move(callback), lazyType, format, desc,
-                                                     origin, mipMapped, fit, budgeted, surfaceFlags)
+                                                     origin, mipMapped, texSwizzle, outSwizzle, fit,
+                                                     budgeted, surfaceFlags)
                     : new GrTextureProxy(std::move(callback), lazyType, format, desc, origin,
-                                         mipMapped, fit, budgeted, surfaceFlags));
+                                         mipMapped, texSwizzle, fit, budgeted, surfaceFlags));
 }
 
 sk_sp<GrRenderTargetProxy> GrProxyProvider::createLazyRenderTargetProxy(
@@ -769,13 +804,17 @@
     LazyInstantiationType lazyType = this->renderingDirectly() ? LazyInstantiationType::kSingleUse
                                                                : LazyInstantiationType::kMultipleUse;
 
+    GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
+    GrSwizzle texSwizzle = this->caps()->getTextureSwizzle(format, colorType);
+    GrSwizzle outSwizzle = this->caps()->getOutputSwizzle(format, colorType);
+
     if (textureInfo) {
         // Wrapped vulkan secondary command buffers don't support texturing since we won't have an
         // actual VkImage to texture from.
         SkASSERT(!wrapsVkSecondaryCB);
         return sk_sp<GrRenderTargetProxy>(new GrTextureRenderTargetProxy(
                 std::move(callback), lazyType, format, desc, origin, textureInfo->fMipMapped,
-                fit, budgeted, surfaceFlags));
+                texSwizzle, outSwizzle, fit, budgeted, surfaceFlags));
     }
 
     GrRenderTargetProxy::WrapsVkSecondaryCB vkSCB =
@@ -783,8 +822,8 @@
                                : GrRenderTargetProxy::WrapsVkSecondaryCB::kNo;
 
     return sk_sp<GrRenderTargetProxy>(new GrRenderTargetProxy(
-            std::move(callback), lazyType, format, desc, origin, fit, budgeted, surfaceFlags,
-            vkSCB));
+            std::move(callback), lazyType, format, desc, origin, texSwizzle, outSwizzle, fit,
+            budgeted, surfaceFlags, vkSCB));
 }
 
 sk_sp<GrTextureProxy> GrProxyProvider::MakeFullyLazyProxy(
@@ -804,14 +843,18 @@
     desc.fConfig = config;
     desc.fSampleCnt = sampleCnt;
 
+    GrColorType colorType = GrPixelConfigToColorType(desc.fConfig);
+    GrSwizzle texSwizzle = caps.getTextureSwizzle(format, colorType);
+    GrSwizzle outSwizzle = caps.getOutputSwizzle(format, colorType);
+
     return sk_sp<GrTextureProxy>(
             (Renderable::kYes == renderable)
                     ? new GrTextureRenderTargetProxy(
                               std::move(callback), LazyInstantiationType::kSingleUse, format, desc,
-                              origin, GrMipMapped::kNo, SkBackingFit::kApprox, SkBudgeted::kYes,
-                              surfaceFlags)
+                              origin, GrMipMapped::kNo, texSwizzle, outSwizzle,
+                              SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags)
                     : new GrTextureProxy(std::move(callback), LazyInstantiationType::kSingleUse,
-                                         format, desc, origin, GrMipMapped::kNo,
+                                         format, desc, origin, GrMipMapped::kNo, texSwizzle,
                                          SkBackingFit::kApprox, SkBudgeted::kYes, surfaceFlags));
 }
 
diff --git a/src/gpu/GrRenderTargetProxy.cpp b/src/gpu/GrRenderTargetProxy.cpp
index bd453c8..c90ff98 100644
--- a/src/gpu/GrRenderTargetProxy.cpp
+++ b/src/gpu/GrRenderTargetProxy.cpp
@@ -21,10 +21,12 @@
 // cases to make the sampleConfig/numSamples stuff more rational.
 GrRenderTargetProxy::GrRenderTargetProxy(const GrCaps& caps, const GrBackendFormat& format,
                                          const GrSurfaceDesc& desc, GrSurfaceOrigin origin,
-                                         SkBackingFit fit, SkBudgeted budgeted,
-                                         GrInternalSurfaceFlags surfaceFlags)
-        : INHERITED(format, desc, origin, fit, budgeted, surfaceFlags)
+                                         const GrSwizzle& textureSwizzle,
+                                         const GrSwizzle& outputSwizzle, SkBackingFit fit,
+                                         SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
+        : INHERITED(format, desc, origin, textureSwizzle, fit, budgeted, surfaceFlags)
         , fSampleCnt(desc.fSampleCnt)
+        , fOutputSwizzle(outputSwizzle)
         , fNeedsStencil(false)
         , fWrapsVkSecondaryCB(WrapsVkSecondaryCB::kNo) {
     // Since we know the newly created render target will be internal, we are able to precompute
@@ -38,12 +40,14 @@
 GrRenderTargetProxy::GrRenderTargetProxy(LazyInstantiateCallback&& callback,
                                          LazyInstantiationType lazyType,
                                          const GrBackendFormat& format, const GrSurfaceDesc& desc,
-                                         GrSurfaceOrigin origin,  SkBackingFit fit,
+                                         GrSurfaceOrigin origin, const GrSwizzle& textureSwizzle,
+                                         const GrSwizzle& outputSwizzle, SkBackingFit fit,
                                          SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags,
                                          WrapsVkSecondaryCB wrapsVkSecondaryCB)
-        : INHERITED(std::move(callback), lazyType, format, desc, origin, fit, budgeted,
-                    surfaceFlags)
+        : INHERITED(std::move(callback), lazyType, format, desc, origin, textureSwizzle, fit,
+                    budgeted, surfaceFlags)
         , fSampleCnt(desc.fSampleCnt)
+        , fOutputSwizzle(outputSwizzle)
         , fNeedsStencil(false)
         , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {
     SkASSERT(SkToBool(kRenderTarget_GrSurfaceFlag & desc.fFlags));
@@ -51,9 +55,12 @@
 
 // Wrapped version
 GrRenderTargetProxy::GrRenderTargetProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin,
+                                         const GrSwizzle& textureSwizzle,
+                                         const GrSwizzle& outputSwizzle,
                                          WrapsVkSecondaryCB wrapsVkSecondaryCB)
-        : INHERITED(std::move(surf), origin, SkBackingFit::kExact)
+        : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact)
         , fSampleCnt(fTarget->asRenderTarget()->numStencilSamples())
+        , fOutputSwizzle(outputSwizzle)
         , fNeedsStencil(false)
         , fWrapsVkSecondaryCB(wrapsVkSecondaryCB) {
 }
diff --git a/src/gpu/GrShaderCaps.h b/src/gpu/GrShaderCaps.h
index 53af67d..cf9005a 100644
--- a/src/gpu/GrShaderCaps.h
+++ b/src/gpu/GrShaderCaps.h
@@ -9,8 +9,8 @@
 #define GrShaderCaps_DEFINED
 
 #include "include/core/SkRefCnt.h"
+#include "include/private/GrSwizzle.h"
 #include "include/private/GrTypesPriv.h"
-#include "src/gpu/GrSwizzle.h"
 #include "src/gpu/glsl/GrGLSL.h"
 
 namespace SkSL {
diff --git a/src/gpu/GrSurfaceProxy.cpp b/src/gpu/GrSurfaceProxy.cpp
index ccf2e55..a2408e5 100644
--- a/src/gpu/GrSurfaceProxy.cpp
+++ b/src/gpu/GrSurfaceProxy.cpp
@@ -54,14 +54,16 @@
 // Lazy-callback version
 GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
                                const GrBackendFormat& format, const GrSurfaceDesc& desc,
-                               GrSurfaceOrigin origin, SkBackingFit fit,
-                               SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
+                               GrSurfaceOrigin origin, const GrSwizzle& textureSwizzle,
+                               SkBackingFit fit, SkBudgeted budgeted,
+                               GrInternalSurfaceFlags surfaceFlags)
         : fSurfaceFlags(surfaceFlags)
         , fFormat(format)
         , fConfig(desc.fConfig)
         , fWidth(desc.fWidth)
         , fHeight(desc.fHeight)
         , fOrigin(origin)
+        , fTextureSwizzle(textureSwizzle)
         , fFit(fit)
         , fBudgeted(budgeted)
         , fLazyInstantiateCallback(std::move(callback))
@@ -84,7 +86,8 @@
 }
 
 // Wrapped version
-GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin, SkBackingFit fit)
+GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin,
+                               const GrSwizzle& textureSwizzle, SkBackingFit fit)
         : INHERITED(std::move(surface))
         , fSurfaceFlags(fTarget->surfacePriv().flags())
         , fFormat(fTarget->backendFormat())
@@ -92,6 +95,7 @@
         , fWidth(fTarget->width())
         , fHeight(fTarget->height())
         , fOrigin(origin)
+        , fTextureSwizzle(textureSwizzle)
         , fFit(fit)
         , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted
                             ? SkBudgeted::kYes
diff --git a/src/gpu/GrSwizzle.cpp b/src/gpu/GrSwizzle.cpp
index 21fe834..b720409 100644
--- a/src/gpu/GrSwizzle.cpp
+++ b/src/gpu/GrSwizzle.cpp
@@ -5,7 +5,7 @@
  * found in the LICENSE file.
  */
 
-#include "src/gpu/GrSwizzle.h"
+#include "include/private/GrSwizzle.h"
 #include "src/core/SkRasterPipeline.h"
 
 void GrSwizzle::apply(SkRasterPipeline* pipeline) const {
diff --git a/src/gpu/GrSwizzle.h b/src/gpu/GrSwizzle.h
deleted file mode 100644
index cbee3eb..0000000
--- a/src/gpu/GrSwizzle.h
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * Copyright 2016 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef GrSwizzle_DEFINED
-#define GrSwizzle_DEFINED
-
-#include "include/private/GrColor.h"
-#include "include/private/SkColorData.h"
-
-class SkRasterPipeline;
-
-/** Represents a rgba swizzle. It can be converted either into a string or a eight bit int. */
-class GrSwizzle {
-public:
-    constexpr GrSwizzle() : GrSwizzle("rgba") {}
-    explicit constexpr GrSwizzle(const char c[4]);
-
-    constexpr GrSwizzle(const GrSwizzle&);
-    constexpr GrSwizzle& operator=(const GrSwizzle& that);
-
-    /** Recreates a GrSwizzle from the output of asKey() */
-    constexpr void setFromKey(uint16_t key);
-    constexpr bool operator==(const GrSwizzle& that) const { return fKey == that.fKey; }
-    constexpr bool operator!=(const GrSwizzle& that) const { return !(*this == that); }
-
-    /** Compact representation of the swizzle suitable for a key. */
-    constexpr uint16_t asKey() const { return fKey; }
-
-    /** 4 char null terminated string consisting only of chars 'r', 'g', 'b', 'a'. */
-    constexpr const char* c_str() const { return fSwiz; }
-
-    constexpr char operator[](int i) const {
-        SkASSERT(i >= 0 && i < 4);
-        return fSwiz[i];
-    }
-
-    /** Applies this swizzle to the input color and returns the swizzled color. */
-    constexpr SkPMColor4f applyTo(const SkPMColor4f& color) const;
-
-    void apply(SkRasterPipeline*) const;
-
-    static constexpr GrSwizzle RGBA() { return GrSwizzle("rgba"); }
-    static constexpr GrSwizzle AAAA() { return GrSwizzle("aaaa"); }
-    static constexpr GrSwizzle RRRR() { return GrSwizzle("rrrr"); }
-    static constexpr GrSwizzle RRRA() { return GrSwizzle("rrra"); }
-    static constexpr GrSwizzle BGRA() { return GrSwizzle("bgra"); }
-    static constexpr GrSwizzle RGB1() { return GrSwizzle("rgb1"); }
-
-private:
-    static constexpr float ComponentIndexToFloat(const SkPMColor4f& color, int idx);
-    static constexpr int CToI(char c);
-    static constexpr char IToC(int idx);
-
-    // The normal component swizzles map to key values 0-3. We set the key for constant 1 to the
-    // next int.
-    static const int k1KeyValue = 4;
-    char fSwiz[5];
-    uint16_t fKey;
-};
-
-constexpr GrSwizzle::GrSwizzle(const char c[4])
-        : fSwiz{c[0], c[1], c[2], c[3], '\0'}
-        , fKey((CToI(c[0]) << 0) | (CToI(c[1]) << 4) | (CToI(c[2]) << 8) | (CToI(c[3]) << 12)) {}
-
-constexpr GrSwizzle::GrSwizzle(const GrSwizzle& that)
-        : fSwiz{that.fSwiz[0], that.fSwiz[1], that.fSwiz[2], that.fSwiz[3], '\0'}
-        , fKey(that.fKey) {}
-
-constexpr GrSwizzle& GrSwizzle::operator=(const GrSwizzle& that) {
-    fSwiz[0] = that.fSwiz[0];
-    fSwiz[1] = that.fSwiz[1];
-    fSwiz[2] = that.fSwiz[2];
-    fSwiz[3] = that.fSwiz[3];
-    SkASSERT(fSwiz[4] == '\0');
-    fKey = that.fKey;
-    return *this;
-}
-
-constexpr SkPMColor4f GrSwizzle::applyTo(const SkPMColor4f& color) const {
-    uint32_t key = fKey;
-    // Index of the input color that should be mapped to output r.
-    int idx = (key & 15);
-    float outR = ComponentIndexToFloat(color, idx);
-    key >>= 4;
-    idx = (key & 15);
-    float outG = ComponentIndexToFloat(color, idx);
-    key >>= 4;
-    idx = (key & 15);
-    float outB = ComponentIndexToFloat(color, idx);
-    key >>= 4;
-    idx = (key & 15);
-    float outA = ComponentIndexToFloat(color, idx);
-    return { outR, outG, outB, outA };
-}
-
-/** Recreates a GrSwizzle from the output of asKey() */
-constexpr void GrSwizzle::setFromKey(uint16_t key) {
-    fKey = key;
-    for (int i = 0; i < 4; ++i) {
-        fSwiz[i] = IToC(key & 15);
-        key >>= 4;
-    }
-    SkASSERT(fSwiz[4] == 0);
-}
-
-constexpr float GrSwizzle::ComponentIndexToFloat(const SkPMColor4f& color, int idx) {
-    if (idx <= 3) {
-        return color[idx];
-    }
-    if (idx == k1KeyValue) {
-        return 1.0f;
-    }
-    return -1.0f;
-}
-
-constexpr int GrSwizzle::CToI(char c) {
-    switch (c) {
-        case 'r': return (GrColor_SHIFT_R / 8);
-        case 'g': return (GrColor_SHIFT_G / 8);
-        case 'b': return (GrColor_SHIFT_B / 8);
-        case 'a': return (GrColor_SHIFT_A / 8);
-        case '1': return k1KeyValue;
-        default:  return -1;
-    }
-}
-
-constexpr char GrSwizzle::IToC(int idx) {
-    switch (8 * idx) {
-        case GrColor_SHIFT_R  : return 'r';
-        case GrColor_SHIFT_G  : return 'g';
-        case GrColor_SHIFT_B  : return 'b';
-        case GrColor_SHIFT_A  : return 'a';
-        case (k1KeyValue * 8) : return '1';
-        default:                return -1;
-    }
-}
-
-#endif
diff --git a/src/gpu/GrTextureProxy.cpp b/src/gpu/GrTextureProxy.cpp
index e0c0b47..3a39481 100644
--- a/src/gpu/GrTextureProxy.cpp
+++ b/src/gpu/GrTextureProxy.cpp
@@ -15,24 +15,12 @@
 #include "src/gpu/GrSurfacePriv.h"
 #include "src/gpu/GrTexturePriv.h"
 
-// Deferred version - with data
-GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc,
-                               GrMipMapped mipMapped, SkBackingFit fit, SkBudgeted budgeted,
-                               const void* srcData, size_t /*rowBytes*/,
-                               GrInternalSurfaceFlags surfaceFlags)
-        : INHERITED(format, srcDesc, kTopLeft_GrSurfaceOrigin, fit, budgeted, surfaceFlags)
-        , fMipMapped(mipMapped)
-        , fProxyProvider(nullptr)
-        , fDeferredUploader(nullptr) {
-    SkASSERT(!srcData);  // currently handled in Make()
-}
-
 // Deferred version - no data
 GrTextureProxy::GrTextureProxy(const GrBackendFormat& format, const GrSurfaceDesc& srcDesc,
                                GrSurfaceOrigin origin, GrMipMapped mipMapped,
-                               SkBackingFit fit, SkBudgeted budgeted,
-                               GrInternalSurfaceFlags surfaceFlags)
-        : INHERITED(format, srcDesc, origin, fit, budgeted, surfaceFlags)
+                               const GrSwizzle& textureSwizzle, SkBackingFit fit,
+                               SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
+        : INHERITED(format, srcDesc, origin, textureSwizzle, fit, budgeted, surfaceFlags)
         , fMipMapped(mipMapped)
         , fProxyProvider(nullptr)
         , fDeferredUploader(nullptr) {}
@@ -40,17 +28,19 @@
 // Lazy-callback version
 GrTextureProxy::GrTextureProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
                                const GrBackendFormat& format, const GrSurfaceDesc& desc,
-                               GrSurfaceOrigin origin, GrMipMapped mipMapped, SkBackingFit fit,
-                               SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
-        : INHERITED(std::move(callback), lazyType, format, desc, origin, fit, budgeted,
+                               GrSurfaceOrigin origin, GrMipMapped mipMapped,
+                               const GrSwizzle& texSwizzle, SkBackingFit fit, SkBudgeted budgeted,
+                               GrInternalSurfaceFlags surfaceFlags)
+        : INHERITED(std::move(callback), lazyType, format, desc, origin, texSwizzle, fit, budgeted,
                     surfaceFlags)
         , fMipMapped(mipMapped)
         , fProxyProvider(nullptr)
         , fDeferredUploader(nullptr) {}
 
 // Wrapped version
-GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin)
-        : INHERITED(std::move(surf), origin, SkBackingFit::kExact)
+GrTextureProxy::GrTextureProxy(sk_sp<GrSurface> surf, GrSurfaceOrigin origin,
+                               const GrSwizzle& textureSwizzle)
+        : INHERITED(std::move(surf), origin, textureSwizzle, SkBackingFit::kExact)
         , fMipMapped(fTarget->asTexture()->texturePriv().mipMapped())
         , fProxyProvider(nullptr)
         , fDeferredUploader(nullptr) {
diff --git a/src/gpu/GrTextureRenderTargetProxy.cpp b/src/gpu/GrTextureRenderTargetProxy.cpp
index 7bf9002..8366e75 100644
--- a/src/gpu/GrTextureRenderTargetProxy.cpp
+++ b/src/gpu/GrTextureRenderTargetProxy.cpp
@@ -23,13 +23,17 @@
                                                        const GrSurfaceDesc& desc,
                                                        GrSurfaceOrigin origin,
                                                        GrMipMapped mipMapped,
+                                                       const GrSwizzle& texSwizzle,
+                                                       const GrSwizzle& outSwizzle,
                                                        SkBackingFit fit,
                                                        SkBudgeted budgeted,
                                                        GrInternalSurfaceFlags surfaceFlags)
-        : GrSurfaceProxy(format, desc, origin, fit, budgeted, surfaceFlags)
+        : GrSurfaceProxy(format, desc, origin, texSwizzle, fit, budgeted, surfaceFlags)
         // for now textures w/ data are always wrapped
-        , GrRenderTargetProxy(caps, format, desc, origin, fit, budgeted, surfaceFlags)
-        , GrTextureProxy(format, desc, origin, mipMapped, fit, budgeted, surfaceFlags) {}
+        , GrRenderTargetProxy(caps, format, desc, origin, texSwizzle, outSwizzle, fit, budgeted,
+                              surfaceFlags)
+        , GrTextureProxy(format, desc, origin, mipMapped, texSwizzle, fit, budgeted, surfaceFlags) {
+}
 
 // Lazy-callback version
 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(LazyInstantiateCallback&& callback,
@@ -38,26 +42,30 @@
                                                        const GrSurfaceDesc& desc,
                                                        GrSurfaceOrigin origin,
                                                        GrMipMapped mipMapped,
+                                                       const GrSwizzle& texSwizzle,
+                                                       const GrSwizzle& outSwizzle,
                                                        SkBackingFit fit,
                                                        SkBudgeted budgeted,
                                                        GrInternalSurfaceFlags surfaceFlags)
-        : GrSurfaceProxy(std::move(callback), lazyType, format, desc, origin, fit, budgeted,
-                         surfaceFlags)
+        : GrSurfaceProxy(std::move(callback), lazyType, format, desc, origin, texSwizzle, fit,
+                         budgeted, surfaceFlags)
         // Since we have virtual inheritance, we initialize GrSurfaceProxy directly. Send null
         // callbacks to the texture and RT proxies simply to route to the appropriate constructors.
-        , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, fit,
-                              budgeted, surfaceFlags, WrapsVkSecondaryCB::kNo)
+        , GrRenderTargetProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, texSwizzle,
+                              outSwizzle, fit, budgeted, surfaceFlags, WrapsVkSecondaryCB::kNo)
         , GrTextureProxy(LazyInstantiateCallback(), lazyType, format, desc, origin, mipMapped,
-                         fit, budgeted, surfaceFlags) {}
+                         texSwizzle, fit, budgeted, surfaceFlags) {}
 
 // Wrapped version
 // This class is virtually derived from GrSurfaceProxy (via both GrTextureProxy and
 // GrRenderTargetProxy) so its constructor must be explicitly called.
 GrTextureRenderTargetProxy::GrTextureRenderTargetProxy(sk_sp<GrSurface> surf,
-                                                       GrSurfaceOrigin origin)
-        : GrSurfaceProxy(surf, origin, SkBackingFit::kExact)
-        , GrRenderTargetProxy(surf, origin)
-        , GrTextureProxy(surf, origin) {
+                                                       GrSurfaceOrigin origin,
+                                                       const GrSwizzle& texSwizzle,
+                                                       const GrSwizzle& outSwizzle)
+        : GrSurfaceProxy(surf, origin, texSwizzle, SkBackingFit::kExact)
+        , GrRenderTargetProxy(surf, origin, texSwizzle, outSwizzle)
+        , GrTextureProxy(surf, origin, texSwizzle) {
     SkASSERT(surf->asTexture());
     SkASSERT(surf->asRenderTarget());
 }
diff --git a/src/gpu/GrTextureRenderTargetProxy.h b/src/gpu/GrTextureRenderTargetProxy.h
index 852309b..1afd456 100644
--- a/src/gpu/GrTextureRenderTargetProxy.h
+++ b/src/gpu/GrTextureRenderTargetProxy.h
@@ -29,16 +29,20 @@
 
     // Deferred version
     GrTextureRenderTargetProxy(const GrCaps&, const GrBackendFormat&, const GrSurfaceDesc&,
-                               GrSurfaceOrigin, GrMipMapped, SkBackingFit, SkBudgeted,
+                               GrSurfaceOrigin, GrMipMapped, const GrSwizzle& textureSwizzle,
+                               const GrSwizzle& outputSwizzle, SkBackingFit, SkBudgeted,
                                GrInternalSurfaceFlags);
 
     // Lazy-callback version
     GrTextureRenderTargetProxy(LazyInstantiateCallback&&, LazyInstantiationType,
                                const GrBackendFormat&, const GrSurfaceDesc& desc, GrSurfaceOrigin,
-                               GrMipMapped, SkBackingFit, SkBudgeted, GrInternalSurfaceFlags);
+                               GrMipMapped, const GrSwizzle& textureSwizzle,
+                               const GrSwizzle& outputSwizzle, SkBackingFit, SkBudgeted,
+                               GrInternalSurfaceFlags);
 
     // Wrapped version
-    GrTextureRenderTargetProxy(sk_sp<GrSurface>, GrSurfaceOrigin);
+    GrTextureRenderTargetProxy(sk_sp<GrSurface>, GrSurfaceOrigin, const GrSwizzle& textureSwizzle,
+                               const GrSwizzle& outputSwizzle);
 
     bool instantiate(GrResourceProvider*) override;
     sk_sp<GrSurface> createSurface(GrResourceProvider*) const override;
diff --git a/src/gpu/gl/GrGLCaps.h b/src/gpu/gl/GrGLCaps.h
index fe1a9f2..87d9043 100644
--- a/src/gpu/gl/GrGLCaps.h
+++ b/src/gpu/gl/GrGLCaps.h
@@ -10,11 +10,11 @@
 #define GrGLCaps_DEFINED
 
 #include <functional>
+#include "include/private/GrSwizzle.h"
 #include "include/private/SkChecksum.h"
 #include "include/private/SkTArray.h"
 #include "include/private/SkTHash.h"
 #include "src/gpu/GrCaps.h"
-#include "src/gpu/GrSwizzle.h"
 #include "src/gpu/gl/GrGLStencilAttachment.h"
 
 class GrGLContextInfo;
diff --git a/src/gpu/gl/GrGLTypesPriv.cpp b/src/gpu/gl/GrGLTypesPriv.cpp
index 8c66fa5..65a7608 100644
--- a/src/gpu/gl/GrGLTypesPriv.cpp
+++ b/src/gpu/gl/GrGLTypesPriv.cpp
@@ -7,7 +7,7 @@
 
 #include "include/core/SkScalar.h"
 #include "include/private/GrGLTypesPriv.h"
-#include "src/gpu/GrSwizzle.h"
+#include "include/private/GrSwizzle.h"
 #include "src/gpu/gl/GrGLDefines.h"
 
 GrGLTextureParameters::SamplerOverriddenState::SamplerOverriddenState()
diff --git a/src/gpu/gl/builders/GrGLProgramBuilder.cpp b/src/gpu/gl/builders/GrGLProgramBuilder.cpp
index bed69a3..dc2afe7 100644
--- a/src/gpu/gl/builders/GrGLProgramBuilder.cpp
+++ b/src/gpu/gl/builders/GrGLProgramBuilder.cpp
@@ -8,6 +8,7 @@
 #include "src/gpu/gl/builders/GrGLProgramBuilder.h"
 
 #include "include/gpu/GrContext.h"
+#include "include/private/GrSwizzle.h"
 #include "src/core/SkATrace.h"
 #include "src/core/SkAutoMalloc.h"
 #include "src/core/SkReader32.h"
@@ -20,7 +21,6 @@
 #include "src/gpu/GrProgramDesc.h"
 #include "src/gpu/GrShaderCaps.h"
 #include "src/gpu/GrShaderUtils.h"
-#include "src/gpu/GrSwizzle.h"
 #include "src/gpu/gl/GrGLGpu.h"
 #include "src/gpu/gl/GrGLProgram.h"
 #include "src/gpu/gl/builders/GrGLProgramBuilder.h"
diff --git a/src/gpu/glsl/GrGLSLShaderBuilder.cpp b/src/gpu/glsl/GrGLSLShaderBuilder.cpp
index bd30080..5a82058 100644
--- a/src/gpu/glsl/GrGLSLShaderBuilder.cpp
+++ b/src/gpu/glsl/GrGLSLShaderBuilder.cpp
@@ -5,12 +5,13 @@
  * found in the LICENSE file.
  */
 
+#include "src/gpu/glsl/GrGLSLShaderBuilder.h"
+
+#include "include/private/GrSwizzle.h"
 #include "src/gpu/GrShaderCaps.h"
 #include "src/gpu/GrShaderVar.h"
-#include "src/gpu/GrSwizzle.h"
 #include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
 #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
-#include "src/gpu/glsl/GrGLSLShaderBuilder.h"
 
 GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program)
     : fProgramBuilder(program)
diff --git a/src/gpu/glsl/GrGLSLUniformHandler.h b/src/gpu/glsl/GrGLSLUniformHandler.h
index 1a5c951..cee6a5a 100644
--- a/src/gpu/glsl/GrGLSLUniformHandler.h
+++ b/src/gpu/glsl/GrGLSLUniformHandler.h
@@ -8,8 +8,8 @@
 #ifndef GrGLSLUniformHandler_DEFINED
 #define GrGLSLUniformHandler_DEFINED
 
+#include "include/private/GrSwizzle.h"
 #include "src/gpu/GrShaderVar.h"
-#include "src/gpu/GrSwizzle.h"
 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
 
 // variable names beginning with this prefix will not be mangled
diff --git a/src/gpu/mtl/GrMtlCaps.mm b/src/gpu/mtl/GrMtlCaps.mm
index da2e374..bdebe19 100644
--- a/src/gpu/mtl/GrMtlCaps.mm
+++ b/src/gpu/mtl/GrMtlCaps.mm
@@ -657,7 +657,7 @@
 static GrSwizzle get_swizzle(const GrBackendFormat& format, GrColorType colorType,
                              bool forOutput) {
     SkASSERT(format.getMtlFormat());
-    MTLPixelFormat mtlFormat = static_cast<MTLPixelFormat>(*format.getVkFormat());
+    MTLPixelFormat mtlFormat = static_cast<MTLPixelFormat>(*format.getMtlFormat());
 
     SkASSERT(format_color_type_valid_pair(mtlFormat, colorType));
 
diff --git a/src/gpu/vk/GrVkCaps.cpp b/src/gpu/vk/GrVkCaps.cpp
index 5db4d45..3798eb9 100644
--- a/src/gpu/vk/GrVkCaps.cpp
+++ b/src/gpu/vk/GrVkCaps.cpp
@@ -682,7 +682,7 @@
     }
 }
 
-// These are all the valid VkFormats that we support in Skia. They are roughly order from most
+// These are all the valid VkFormats that we support in Skia. They are roughly ordered from most
 // frequently used to least to improve look up times in arrays.
 static constexpr VkFormat kVkFormats[] = {
     VK_FORMAT_R8G8B8A8_UNORM,