Make mask checking explicit in SkDrawableGlyphBuffer

This is in preparation for not passing SkGlyph* around
because it will not be available from the RemoteStrikes.

Change-Id: I9a7833c7a23726020aefdff41766952458c61f72
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/565636
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/src/core/SkGlyphBuffer.cpp b/src/core/SkGlyphBuffer.cpp
index ceb2d13..7e0c40d 100644
--- a/src/core/SkGlyphBuffer.cpp
+++ b/src/core/SkGlyphBuffer.cpp
@@ -18,6 +18,7 @@
     if (size > fMaxSize) {
         fMultiBuffer.reset(size);
         fPositions.reset(size);
+        fFormats.reset(size);
         fMaxSize = size;
     }
 
@@ -86,6 +87,7 @@
     if (fMaxSize > 200) {
         fMultiBuffer.reset();
         fPositions.reset();
+        fFormats.reset();
         fMaxSize = 0;
     }
     fInputSize = 0;
diff --git a/src/core/SkGlyphBuffer.h b/src/core/SkGlyphBuffer.h
index 30c1357..38d4588 100644
--- a/src/core/SkGlyphBuffer.h
+++ b/src/core/SkGlyphBuffer.h
@@ -167,6 +167,7 @@
         SkASSERT(fAcceptedSize <= from);
         fPositions[fAcceptedSize] = fPositions[from];
         fMultiBuffer[fAcceptedSize] = glyph;
+        fFormats[fAcceptedSize] = glyph->maskFormat();
         fAcceptedSize++;
     }
 
@@ -177,6 +178,13 @@
         return SkZip<SkGlyphVariant, SkPoint>{fAcceptedSize, fMultiBuffer.get(), fPositions};
     }
 
+    SkZip<SkGlyphVariant, SkPoint, SkMask::Format> acceptedWithMaskFormat() {
+        SkASSERT(fPhase == kProcess);
+        SkDEBUGCODE(fPhase = kDraw);
+        return SkZip<SkGlyphVariant, SkPoint, SkMask::Format>{
+                fAcceptedSize, fMultiBuffer.get(), fPositions, fFormats};
+    }
+
     bool empty() const {
         SkASSERT(fPhase == kProcess || fPhase == kDraw);
         return fAcceptedSize == 0;
@@ -197,6 +205,7 @@
     size_t fAcceptedSize{0};
     SkAutoTArray<SkGlyphVariant> fMultiBuffer;
     SkAutoTMalloc<SkPoint> fPositions;
+    SkAutoTMalloc<SkMask::Format> fFormats;
 
 #ifdef SK_DEBUG
     enum {
diff --git a/src/text/gpu/SubRunContainer.cpp b/src/text/gpu/SubRunContainer.cpp
index 51e8844..903e7ab 100644
--- a/src/text/gpu/SubRunContainer.cpp
+++ b/src/text/gpu/SubRunContainer.cpp
@@ -2332,26 +2332,27 @@
 template<typename AddSingleMaskFormat>
 void add_multi_mask_format(
         AddSingleMaskFormat addSingleMaskFormat,
-        const SkZip<SkGlyphVariant, SkPoint>& accepted,
+        const SkZip<SkGlyphVariant, SkPoint, SkMask::Format>& accepted,
         sk_sp<SkStrike>&& strike) {
     if (accepted.empty()) { return; }
 
-    auto glyphSpan = accepted.get<0>();
-    const SkGlyph* glyph = glyphSpan[0];
-    MaskFormat format = Glyph::FormatFromSkGlyph(glyph->maskFormat());
+    auto maskSpan = accepted.get<2>();
+    MaskFormat format = Glyph::FormatFromSkGlyph(maskSpan[0]);
     size_t startIndex = 0;
     for (size_t i = 1; i < accepted.size(); i++) {
-        glyph = glyphSpan[i];
-        MaskFormat nextFormat = Glyph::FormatFromSkGlyph(glyph->maskFormat());
+        MaskFormat nextFormat = Glyph::FormatFromSkGlyph(maskSpan[i]);
         if (format != nextFormat) {
-            auto glyphsWithSameFormat = accepted.subspan(startIndex, i - startIndex);
+            auto interval = accepted.subspan(startIndex, i - startIndex);
+            // Only pass the packed glyph ids and positions.
+            auto glyphsWithSameFormat = SkMakeZip(interval.get<0>(), interval.get<1>());
             // Take a ref on the strike. This should rarely happen.
             addSingleMaskFormat(glyphsWithSameFormat, format, sk_sp<SkStrike>(strike));
             format = nextFormat;
             startIndex = i;
         }
     }
-    auto glyphsWithSameFormat = accepted.last(accepted.size() - startIndex);
+    auto interval = accepted.last(accepted.size() - startIndex);
+    auto glyphsWithSameFormat = SkMakeZip(interval.get<0>(), interval.get<1>());
     addSingleMaskFormat(glyphsWithSameFormat, format, std::move(strike));
 }
 }  // namespace
@@ -2601,7 +2602,7 @@
                                 }
                             };
                     add_multi_mask_format(addGlyphsWithSameFormat,
-                                          accepted->accepted(),
+                                          accepted->acceptedWithMaskFormat(),
                                           strike->getUnderlyingStrike());
                 }
             }
@@ -2785,7 +2786,7 @@
                                 }
                             };
                     add_multi_mask_format(addGlyphsWithSameFormat,
-                                          accepted->accepted(),
+                                          accepted->acceptedWithMaskFormat(),
                                           strike->getUnderlyingStrike());
                 }
             }