Make fallback descriptors explicit

Have the subruns carry a pointer to the descriptor they need.
This simplifies the regen code, and makes ARGB fallback more explicit.

Change-Id: If11477cb9ecccc90ae262327e0579f4ab249836e
Reviewed-on: https://skia-review.googlesource.com/c/171586
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/src/core/SkGlyphRunPainter.cpp b/src/core/SkGlyphRunPainter.cpp
index 3122217..b6c74fe 100644
--- a/src/core/SkGlyphRunPainter.cpp
+++ b/src/core/SkGlyphRunPainter.cpp
@@ -428,7 +428,7 @@
                 }
             }
         } else {
-            SkAssertResult(glyph.fMaskFormat == SkMask::kARGB32_Format);
+            SkASSERT(glyph.fMaskFormat == SkMask::kARGB32_Format);
             SkScalar largestDimension = std::max(glyph.fWidth, glyph.fHeight);
             maxFallbackDimension = std::max(maxFallbackDimension, largestDimension);
             fARGBGlyphsIDs.push_back(glyphID);
@@ -642,7 +642,7 @@
 
         SubRun* subRun = &fSubRunInfo.back();
         if (fInitialized && subRun->maskFormat() != format) {
-            subRun = &pushBackSubRun();
+            subRun = pushBackSubRun(fDescriptor);
             subRun->setStrike(strike);
         } else if (!fInitialized) {
             subRun->setStrike(strike);
@@ -673,12 +673,14 @@
                     SkSpan<const SkPoint> positions, SkScalar textScale,
                     const SkMatrix& glyphCacheMatrix,
                     SkGlyphRunListPainter::NeedsTransform needsTransform) const {
-            fRun->initOverride();
             fBlob->setHasBitmap();
             fRun->setSubRunHasW(glyphCacheMatrix.hasPerspective());
+            auto subRun = fRun->initARGBFallback();
             SkExclusiveStrikePtr fallbackCache =
                     fRun->setupCache(fallbackPaint, fProps, fScalerContextFlags, glyphCacheMatrix);
             sk_sp<GrTextStrike> strike = fGlyphCache->getStrike(fallbackCache.get());
+            SkASSERT(strike != nullptr);
+            subRun->setStrike(strike);
             const SkPoint* glyphPos = positions.data();
             for (auto glyphID : glyphIDs) {
                 const SkGlyph& glyph = fallbackCache->getGlyphIDMetrics(glyphID);
diff --git a/src/gpu/text/GrTextBlob.cpp b/src/gpu/text/GrTextBlob.cpp
index e54ab06..184ee7a 100644
--- a/src/gpu/text/GrTextBlob.cpp
+++ b/src/gpu/text/GrTextBlob.cpp
@@ -64,7 +64,7 @@
                                                  const SkMatrix& viewMatrix) {
 
     // if we have an override descriptor for the run, then we should use that
-    SkAutoDescriptor* desc = fOverrideDescriptor.get() ? fOverrideDescriptor.get() : &fDescriptor;
+    SkAutoDescriptor* desc = fARGBFallbackDescriptor.get() ? fARGBFallbackDescriptor.get() : &fDescriptor;
     SkScalerContextEffects effects;
     SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
         skPaint, props, scalerContextFlags, viewMatrix, desc, &effects);
@@ -415,13 +415,13 @@
         SkASSERT_RELEASE(rRun.fDescriptor.getDesc());
         SkASSERT_RELEASE(*lRun.fDescriptor.getDesc() == *rRun.fDescriptor.getDesc());
 
-        if (lRun.fOverrideDescriptor.get()) {
-            SkASSERT_RELEASE(lRun.fOverrideDescriptor->getDesc());
-            SkASSERT_RELEASE(rRun.fOverrideDescriptor.get() && rRun.fOverrideDescriptor->getDesc());
-            SkASSERT_RELEASE(*lRun.fOverrideDescriptor->getDesc() ==
-                             *rRun.fOverrideDescriptor->getDesc());
+        if (lRun.fARGBFallbackDescriptor.get()) {
+            SkASSERT_RELEASE(lRun.fARGBFallbackDescriptor->getDesc());
+            SkASSERT_RELEASE(rRun.fARGBFallbackDescriptor.get() && rRun.fARGBFallbackDescriptor->getDesc());
+            SkASSERT_RELEASE(*lRun.fARGBFallbackDescriptor->getDesc() ==
+                             *rRun.fARGBFallbackDescriptor->getDesc());
         } else {
-            SkASSERT_RELEASE(!rRun.fOverrideDescriptor.get());
+            SkASSERT_RELEASE(!rRun.fARGBFallbackDescriptor.get());
         }
 
         // color can be changed
diff --git a/src/gpu/text/GrTextBlob.h b/src/gpu/text/GrTextBlob.h
index 96bb9b6..739a079 100644
--- a/src/gpu/text/GrTextBlob.h
+++ b/src/gpu/text/GrTextBlob.h
@@ -276,7 +276,9 @@
 
     class SubRun {
     public:
-        explicit SubRun(Run* run) : fRun{run} {}
+        SubRun(Run* run, const SkAutoDescriptor& desc)
+            : fRun{run}
+            , fDesc{desc} {}
 
         void appendGlyph(GrTextBlob* blob, GrGlyph* glyph, SkRect dstRect);
 
@@ -339,6 +341,9 @@
         bool hasWCoord() const { return fFlags.hasWCoord; }
         void setNeedsTransform(bool needsTransform) { fFlags.needsTransform = needsTransform; }
         bool needsTransform() const { return fFlags.needsTransform; }
+        void setFallback() {fFlags.argbFallback = true;}
+
+        const SkDescriptor* desc() const { return fDesc.getDesc(); }
 
     private:
         GrDrawOpAtlas::BulkUseTokenUpdater fBulkUseToken;
@@ -360,8 +365,10 @@
             bool antiAliased:1;
             bool hasWCoord:1;
             bool needsTransform:1;
-        } fFlags{false, false, false, false, false};
+            bool argbFallback:1;
+        } fFlags{false, false, false, false, false, false};
         Run* const fRun;
+        const SkAutoDescriptor& fDesc;
     };  // SubRunInfo
 
 
@@ -395,7 +402,7 @@
         explicit Run(GrTextBlob* blob)
         : fBlob{blob} {
             // To ensure we always have one subrun, we push back a fresh run here
-            fSubRunInfo.emplace_back(this);
+            fSubRunInfo.emplace_back(this, fDescriptor);
         }
 
         // sets the last subrun of runIndex to use w values
@@ -406,10 +413,13 @@
 
         // inits the override descriptor on the current run.  All following subruns must use this
         // descriptor
-        void initOverride() {
+        SubRun* initARGBFallback() {
+            fARGBFallbackDescriptor.reset(new SkAutoDescriptor{});
             // Push back a new subrun to fill and set the override descriptor
-            this->pushBackSubRun();
-            fOverrideDescriptor.reset(new SkAutoDescriptor);
+            SubRun* subRun = this->pushBackSubRun(*fARGBFallbackDescriptor);
+            subRun->setMaskFormat(kARGB_GrMaskFormat);
+            subRun->setFallback();
+            return subRun;
         }
 
         // Appends a glyph to the blob as a path only.
@@ -443,13 +453,13 @@
             subRun.setHasWCoord(hasWCoord);
         }
 
-        SubRun& pushBackSubRun() {
+        SubRun* pushBackSubRun(const SkAutoDescriptor& desc) {
             // Forward glyph / vertex information to seed the new sub run
-            SubRun& newSubRun = fSubRunInfo.emplace_back(this);
+            SubRun& newSubRun = fSubRunInfo.emplace_back(this, desc);
             const SubRun& prevSubRun = fSubRunInfo.fromBack(1);
 
             newSubRun.setAsSuccessor(prevSubRun);
-            return newSubRun;
+            return &newSubRun;
         }
 
         // Any glyphs that can't be rendered with the base or override descriptor
@@ -479,9 +489,9 @@
 
         // Distance field text cannot draw coloremoji, and so has to fall back.  However,
         // though the distance field text and the coloremoji may share the same run, they
-        // will have different descriptors.  If fOverrideDescriptor is non-nullptr, then it
+        // will have different descriptors.  If fARGBFallbackDescriptor is non-nullptr, then it
         // will be used in place of the run's descriptor to regen texture coords
-        std::unique_ptr<SkAutoDescriptor> fOverrideDescriptor; // df properties
+        std::unique_ptr<SkAutoDescriptor> fARGBFallbackDescriptor;
 
         SkTArray<PathGlyph> fPathGlyphs;
 
diff --git a/src/gpu/text/GrTextBlobVertexRegenerator.cpp b/src/gpu/text/GrTextBlobVertexRegenerator.cpp
index 20fc31f..0cfddf6 100644
--- a/src/gpu/text/GrTextBlobVertexRegenerator.cpp
+++ b/src/gpu/text/GrTextBlobVertexRegenerator.cpp
@@ -238,9 +238,7 @@
     if (regenTexCoords) {
         fSubRun->resetBulkUseToken();
 
-        const SkDescriptor* desc = (fRun->fOverrideDescriptor && !fSubRun->drawAsDistanceFields())
-                                           ? fRun->fOverrideDescriptor->getDesc()
-                                           : fRun->fDescriptor.getDesc();
+        const SkDescriptor* desc = fSubRun->desc();
 
         if (!*fLazyCache || (*fLazyCache)->getDescriptor() != *desc) {
             SkScalerContextEffects effects;