Support luminance-alpha GL format for YUVA planes

Bug: skia:10632
Change-Id: Ic91510b55644c15ee4eb5f96e7c35193b5ce49df
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/344761
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/gl/GrGLCaps.cpp b/src/gpu/gl/GrGLCaps.cpp
index a71d452..4efaec2 100644
--- a/src/gpu/gl/GrGLCaps.cpp
+++ b/src/gpu/gl/GrGLCaps.cpp
@@ -1801,6 +1801,79 @@
         }
     }
 
+    // Format: LUMINANCE8_ALPHA8
+    {
+        FormatInfo& info = this->getFormatInfo(GrGLFormat::kLUMINANCE8_ALPHA8);
+        info.fFormatType = FormatType::kNormalizedFixedPoint;
+        info.fInternalFormatForRenderbuffer = GR_GL_LUMINANCE8_ALPHA8;
+        info.fDefaultExternalFormat = GR_GL_LUMINANCE_ALPHA;
+        info.fDefaultExternalType = GR_GL_UNSIGNED_BYTE;
+        info.fDefaultColorType = GrColorType::kGrayAlpha_88;
+        bool la8Supported = false;
+        bool la8SizedFormatSupported = false;
+        if (GR_IS_GR_GL(standard) && !fIsCoreProfile) {
+            la8Supported = true;
+            la8SizedFormatSupported = true;
+        } else if (GR_IS_GR_GL_ES(standard)) {
+            la8Supported = true;
+            // Even on ES3 this extension is required to define LUMINANCE8_ALPHA8.
+            la8SizedFormatSupported = ctxInfo.hasExtension("GL_EXT_texture_storage");
+        } else if (GR_IS_GR_WEBGL(standard)) {
+            la8Supported = true;
+        }
+        if (la8Supported) {
+            info.fFlags = FormatInfo::kTexturable_Flag;
+        }
+        if (texStorageSupported && la8SizedFormatSupported) {
+            info.fFlags |= FormatInfo::kUseTexStorage_Flag;
+            info.fInternalFormatForTexImageOrStorage = GR_GL_LUMINANCE8_ALPHA8;
+        } else if (texImageSupportsSizedInternalFormat && la8SizedFormatSupported) {
+            info.fInternalFormatForTexImageOrStorage = GR_GL_LUMINANCE8_ALPHA8;
+        } else {
+            info.fInternalFormatForTexImageOrStorage = GR_GL_LUMINANCE_ALPHA;
+        }
+        // See note in LUMINANCE8 section about not attaching to framebuffers.
+
+        if (la8Supported) {
+            info.fColorTypeInfoCount = 1;
+            info.fColorTypeInfos = std::make_unique<ColorTypeInfo[]>(info.fColorTypeInfoCount);
+            int ctIdx = 0;
+            // Format: LUMINANCE8_ALPHA8, Surface: kGrayAlpha_88
+            {
+                auto& ctInfo = info.fColorTypeInfos[ctIdx++];
+                ctInfo.fColorType = GrColorType::kGrayAlpha_88;
+                ctInfo.fFlags = ColorTypeInfo::kUploadData_Flag;
+                int idx = static_cast<int>(GrColorType::kGrayAlpha_88);
+                if (fColorTypeToFormatTable[idx] == GrGLFormat::kUnknown) {
+                    this->setColorTypeFormat(GrColorType::kGrayAlpha_88,
+                                             GrGLFormat::kLUMINANCE8_ALPHA8);
+                }
+
+                // External IO ColorTypes:
+                ctInfo.fExternalIOFormatCount = 2;
+                ctInfo.fExternalIOFormats = std::make_unique<ColorTypeInfo::ExternalIOFormats[]>(
+                        ctInfo.fExternalIOFormatCount);
+                int ioIdx = 0;
+                // Format: LUMINANCE8, Surface: kGrayAlpha_88, Data: kGrayAlpha_88
+                {
+                    auto& ioFormat = ctInfo.fExternalIOFormats[ioIdx++];
+                    ioFormat.fColorType = GrColorType::kGrayAlpha_88;
+                    ioFormat.fExternalType = GR_GL_UNSIGNED_BYTE;
+                    ioFormat.fExternalTexImageFormat = GR_GL_LUMINANCE_ALPHA;
+                    ioFormat.fExternalReadFormat = 0;
+                }
+
+                // Format: LUMINANCE8, Surface: kGrayAlpha_88, Data: kRGBA_8888
+                {
+                    auto& ioFormat = ctInfo.fExternalIOFormats[ioIdx++];
+                    ioFormat.fColorType = GrColorType::kRGBA_8888;
+                    ioFormat.fExternalType = GR_GL_UNSIGNED_BYTE;
+                    ioFormat.fExternalTexImageFormat = 0;
+                    ioFormat.fExternalReadFormat = GR_GL_RGBA;
+                }
+            }
+        }
+    }
     // Format: BGRA8
     {
         FormatInfo& info = this->getFormatInfo(GrGLFormat::kBGRA8);
@@ -3220,17 +3293,16 @@
             // The formats don't represent color channels (i.e. may be depth stencil)
             return false;
         }
-        // The dst channels have to be a subset of the srcChannels, unless the dst is going to
-        // gray.
+        // The dst channels have to be a subset of the srcChannels, except R, RG, or RGB, channels
+        // can go to LUM. (See expansion of Table 3.9 in EXT_texture_rg).
         if ((dstChannels & srcChannels) != srcChannels) {
-            if (dstChannels == kGray_SkColorChannelFlag) {
-                // We can't copy Alpha into a Luminance channel
-                if (srcChannels == kAlpha_8_SkColorType) {
+            if (dstChannels == kGray_SkColorChannelFlag ||
+                dstChannels == kGrayAlpha_SkColorChannelFlags) {
+                // The dst can't have gray if the src is alpha-only.
+                if (srcChannels == kAlpha_SkColorChannelFlag) {
                     return false;
                 }
             } else {
-                // Currently we don't support any LA formats
-                SkASSERT((dstChannels & kGray_SkColorChannelFlag) == 0);
                 return false;
             }
         }
@@ -4528,6 +4600,8 @@
           GrBackendFormat::MakeGL(GR_GL_LUMINANCE8, GR_GL_TEXTURE_2D) },
         { GrColorType::kGray_8,
           GrBackendFormat::MakeGL(GR_GL_R8, GR_GL_TEXTURE_2D) },
+        { GrColorType::kGrayAlpha_88,
+          GrBackendFormat::MakeGL(GR_GL_LUMINANCE8_ALPHA8, GR_GL_TEXTURE_2D) },
         { GrColorType::kAlpha_F16,
           GrBackendFormat::MakeGL(GR_GL_R16F, GR_GL_TEXTURE_2D) },
         { GrColorType::kAlpha_F16,
diff --git a/src/gpu/gl/GrGLDefines.h b/src/gpu/gl/GrGLDefines.h
index 8fde6be..76c785a 100644
--- a/src/gpu/gl/GrGLDefines.h
+++ b/src/gpu/gl/GrGLDefines.h
@@ -467,6 +467,7 @@
 
 /* Luminance sized formats */
 #define GR_GL_LUMINANCE8                     0x8040
+#define GR_GL_LUMINANCE8_ALPHA8              0x8045
 #define GR_GL_LUMINANCE16F                   0x881E
 
 /* Alpha sized formats */
diff --git a/src/gpu/gl/GrGLUtil.cpp b/src/gpu/gl/GrGLUtil.cpp
index 3340ee9..73387ba 100644
--- a/src/gpu/gl/GrGLUtil.cpp
+++ b/src/gpu/gl/GrGLUtil.cpp
@@ -629,6 +629,7 @@
         case GrGLFormat::kR8:
         case GrGLFormat::kALPHA8:
         case GrGLFormat::kLUMINANCE8:
+        case GrGLFormat::kLUMINANCE8_ALPHA8:
         case GrGLFormat::kBGRA8:
         case GrGLFormat::kRGB565:
         case GrGLFormat::kRGBA16F:
diff --git a/src/gpu/gl/GrGLUtil.h b/src/gpu/gl/GrGLUtil.h
index 760847d..c423e44 100644
--- a/src/gpu/gl/GrGLUtil.h
+++ b/src/gpu/gl/GrGLUtil.h
@@ -45,6 +45,7 @@
         case GrGLFormat::kR8:                    return kRed_SkColorChannelFlag;
         case GrGLFormat::kALPHA8:                return kAlpha_SkColorChannelFlag;
         case GrGLFormat::kLUMINANCE8:            return kGray_SkColorChannelFlag;
+        case GrGLFormat::kLUMINANCE8_ALPHA8:     return kGrayAlpha_SkColorChannelFlags;
         case GrGLFormat::kBGRA8:                 return kRGBA_SkColorChannelFlags;
         case GrGLFormat::kRGB565:                return kRGB_SkColorChannelFlags;
         case GrGLFormat::kRGBA16F:               return kRGBA_SkColorChannelFlags;
@@ -327,6 +328,7 @@
         case GR_GL_R8:                   return GrGLFormat::kR8;
         case GR_GL_ALPHA8:               return GrGLFormat::kALPHA8;
         case GR_GL_LUMINANCE8:           return GrGLFormat::kLUMINANCE8;
+        case GR_GL_LUMINANCE8_ALPHA8:    return GrGLFormat::kLUMINANCE8_ALPHA8;
         case GR_GL_BGRA8:                return GrGLFormat::kBGRA8;
         case GR_GL_RGB565:               return GrGLFormat::kRGB565;
         case GR_GL_RGBA16F:              return GrGLFormat::kRGBA16F;
@@ -361,6 +363,7 @@
         case GrGLFormat::kR8:                   return GR_GL_R8;
         case GrGLFormat::kALPHA8:               return GR_GL_ALPHA8;
         case GrGLFormat::kLUMINANCE8:           return GR_GL_LUMINANCE8;
+        case GrGLFormat::kLUMINANCE8_ALPHA8:    return GR_GL_LUMINANCE8_ALPHA8;
         case GrGLFormat::kBGRA8:                return GR_GL_BGRA8;
         case GrGLFormat::kRGB565:               return GR_GL_RGB565;
         case GrGLFormat::kRGBA16F:              return GR_GL_RGBA16F;
@@ -393,6 +396,7 @@
         case GrGLFormat::kR8:                   return 1;
         case GrGLFormat::kALPHA8:               return 1;
         case GrGLFormat::kLUMINANCE8:           return 1;
+        case GrGLFormat::kLUMINANCE8_ALPHA8:    return 2;
         case GrGLFormat::kBGRA8:                return 4;
         case GrGLFormat::kRGB565:               return 2;
         case GrGLFormat::kRGBA16F:              return 8;
@@ -436,6 +440,7 @@
         case GrGLFormat::kR8:
         case GrGLFormat::kALPHA8:
         case GrGLFormat::kLUMINANCE8:
+        case GrGLFormat::kLUMINANCE8_ALPHA8:
         case GrGLFormat::kBGRA8:
         case GrGLFormat::kRGB565:
         case GrGLFormat::kRGBA16F:
@@ -468,6 +473,7 @@
         case GrGLFormat::kR8:
         case GrGLFormat::kALPHA8:
         case GrGLFormat::kLUMINANCE8:
+        case GrGLFormat::kLUMINANCE8_ALPHA8:
         case GrGLFormat::kBGRA8:
         case GrGLFormat::kRGB565:
         case GrGLFormat::kRGBA16F:
@@ -502,6 +508,7 @@
     case GrGLFormat::kR8:
     case GrGLFormat::kALPHA8:
     case GrGLFormat::kLUMINANCE8:
+    case GrGLFormat::kLUMINANCE8_ALPHA8:
     case GrGLFormat::kBGRA8:
     case GrGLFormat::kRGB565:
     case GrGLFormat::kRGBA16F:
@@ -531,6 +538,7 @@
         case GR_GL_R8:                   return "R8";
         case GR_GL_ALPHA8:               return "ALPHA8";
         case GR_GL_LUMINANCE8:           return "LUMINANCE8";
+        case GR_GL_LUMINANCE8_ALPHA8:    return "LUMINANCE8_ALPHA8";
         case GR_GL_BGRA8:                return "BGRA8";
         case GR_GL_RGB565:               return "RGB565";
         case GR_GL_RGBA16F:              return "RGBA16F";