Encapsulate color into subrun

Change-Id: I0a9a7e4803348f5e24144f355eeaeb7e8857da18
Reviewed-on: https://skia-review.googlesource.com/c/171042
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/src/core/SkGlyphRunPainter.cpp b/src/core/SkGlyphRunPainter.cpp
index 1f36f1c..b1d0a0e 100644
--- a/src/core/SkGlyphRunPainter.cpp
+++ b/src/core/SkGlyphRunPainter.cpp
@@ -470,7 +470,9 @@
 
     // Get the first paint to use as the key paint.
     const SkPaint& listPaint = glyphRunList.paint();
+
     SkPMColor4f filteredColor = generate_filtered_color(listPaint, target->colorSpaceInfo());
+    GrColor color = generate_filtered_color(listPaint, target->colorSpaceInfo()).toBytes_RGBA();
 
     // If we have been abandoned, then don't draw
     if (context->abandoned()) {
@@ -519,35 +521,33 @@
             // TODO we could probably get away reuse most of the time if the pointer is unique,
             // but we'd have to clear the subrun information
             textBlobCache->remove(cacheBlob.get());
-            cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, listPaint);
+            cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, listPaint, color);
             cacheBlob->generateFromGlyphRunList(
                     glyphCache, *context->contextPriv().caps()->shaderCaps(), fOptions,
-                    listPaint, filteredColor, scalerContextFlags, viewMatrix, props,
+                    listPaint, scalerContextFlags, viewMatrix, props,
                     glyphRunList, target->glyphPainter());
         } else {
             textBlobCache->makeMRU(cacheBlob.get());
 
             if (CACHE_SANITY_CHECK) {
-                int glyphCount = glyphRunList.totalGlyphCount();
-                int runCount = glyphRunList.runCount();
-                sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphCount, runCount));
+                sk_sp<GrTextBlob> sanityBlob(textBlobCache->makeBlob(glyphRunList, color));
                 sanityBlob->setupKey(key, blurRec, listPaint);
                 cacheBlob->generateFromGlyphRunList(
                         glyphCache, *context->contextPriv().caps()->shaderCaps(), fOptions,
-                        listPaint, filteredColor, scalerContextFlags, viewMatrix, props, glyphRunList,
+                        listPaint, scalerContextFlags, viewMatrix, props, glyphRunList,
                         target->glyphPainter());
                 GrTextBlob::AssertEqual(*sanityBlob, *cacheBlob);
             }
         }
     } else {
         if (canCache) {
-            cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, listPaint);
+            cacheBlob = textBlobCache->makeCachedBlob(glyphRunList, key, blurRec, listPaint, color);
         } else {
-            cacheBlob = textBlobCache->makeBlob(glyphRunList);
+            cacheBlob = textBlobCache->makeBlob(glyphRunList, color);
         }
         cacheBlob->generateFromGlyphRunList(
                 glyphCache, *context->contextPriv().caps()->shaderCaps(), fOptions, listPaint,
-                filteredColor, scalerContextFlags, viewMatrix, props, glyphRunList,
+                scalerContextFlags, viewMatrix, props, glyphRunList,
                 target->glyphPainter());
     }
 
@@ -620,9 +620,7 @@
 void GrTextBlob::Run::appendGlyph(GrTextBlob* blob,
                                   const sk_sp<GrTextStrike>& strike,
                                   const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
-                                  SkPoint origin,
-                                  const SkPMColor4f& color4f,
-                                  SkScalar textRatio, bool needsTransform) {
+                                  SkPoint origin, SkScalar textRatio, bool needsTransform) {
 
     GrGlyph::PackedID id = GrGlyph::Pack(skGlyph.getGlyphID(),
                                          skGlyph.getSubXFixed(),
@@ -640,14 +638,11 @@
 
     SkRect glyphRect = rect_to_draw(skGlyph, origin, textRatio, isDFT);
     if (!glyphRect.isEmpty()) {
-        // TODO4F: Preserve float colors
-        GrColor color = color4f.toBytes_RGBA();
-
         GrMaskFormat format = glyph->fMaskFormat;
 
         SubRun* subRun = &fSubRunInfo.back();
         if (fInitialized && subRun->maskFormat() != format) {
-            subRun = pushBackSubRun(fDescriptor);
+            subRun = pushBackSubRun(fDescriptor, fColor);
             subRun->setStrike(strike);
         } else if (!fInitialized) {
             subRun->setStrike(strike);
@@ -655,7 +650,6 @@
 
         fInitialized = true;
         subRun->setMaskFormat(format);
-        subRun->setColor(color);
         subRun->setNeedsTransform(needsTransform);
 
         subRun->appendGlyph(blob, glyph, glyphRect);
@@ -667,7 +661,6 @@
                                           const GrShaderCaps& shaderCaps,
                                           const GrTextContext::Options& options,
                                           const SkPaint& paint,
-                                          const SkPMColor4f& filteredColor,
                                           SkScalerContextFlags scalerContextFlags,
                                           const SkMatrix& viewMatrix,
                                           const SkSurfaceProps& props,
@@ -691,7 +684,7 @@
                 const SkGlyph& glyph = fallbackCache->getGlyphIDMetrics(glyphID);
                 fRun->appendGlyph(fBlob, strike, glyph,
                                  GrGlyph::kCoverage_MaskStyle,
-                                 *glyphPos, fFilteredColor, textScale, needsTransform);
+                                 *glyphPos, textScale, needsTransform);
                 glyphPos++;
             }
         }
@@ -701,7 +694,6 @@
         const SkSurfaceProps& fProps;
         const SkScalerContextFlags fScalerContextFlags;
         GrGlyphCache* const fGlyphCache;
-        SkPMColor4f fFilteredColor;
     };
 
     SkPoint origin = glyphRunList.origin();
@@ -739,11 +731,11 @@
                 auto perEmpty = [](const SkGlyph&, SkPoint) {};
 
                 auto perSDF =
-                    [this, run, &currStrike, &filteredColor, textScale]
+                    [this, run, &currStrike, textScale]
                     (const SkGlyph& glyph, SkPoint position) {
                         run->appendGlyph(this, currStrike,
                                     glyph, GrGlyph::kDistance_MaskStyle, position,
-                                    filteredColor, textScale, true);
+                                    textScale, true);
                     };
 
                 auto perPath =
@@ -756,7 +748,7 @@
                     };
 
                 ARGBFallbackHelper argbFallback{this, run, props, scalerContextFlags,
-                                                glyphCache, filteredColor};
+                                                glyphCache};
 
                 glyphPainter->drawGlyphRunAsSDFWithARGBFallback(
                     cache.get(), glyphRun, origin, runPaint, viewMatrix, textScale,
@@ -790,7 +782,7 @@
             };
 
             ARGBFallbackHelper argbFallback{this, run, props, scalerContextFlags,
-                                            glyphCache, filteredColor};
+                                            glyphCache};
 
             glyphPainter->drawGlyphRunAsPathWithARGBFallback(
                 pathCache.get(), glyphRun, origin, runPaint, viewMatrix, textScale,
@@ -806,13 +798,13 @@
             auto perEmpty = [](const SkGlyph&, SkPoint) {};
 
             auto perGlyph =
-                [this, run, &currStrike, &filteredColor]
+                [this, run, &currStrike]
                 (const SkGlyph& glyph, SkPoint mappedPt) {
                     SkPoint pt{SkScalarFloorToScalar(mappedPt.fX),
                                SkScalarFloorToScalar(mappedPt.fY)};
                     run->appendGlyph(this, currStrike,
                                      glyph, GrGlyph::kCoverage_MaskStyle, pt,
-                                     filteredColor, SK_Scalar1, false);
+                                     SK_Scalar1, false);
                 };
 
             auto perPath =
@@ -852,22 +844,22 @@
     size_t textLen = (int)strlen(text);
 
     SkPMColor4f filteredColor = generate_filtered_color(skPaint, rtc->colorSpaceInfo());
+    GrColor color = filteredColor.toBytes_RGBA();
 
     auto origin = SkPoint::Make(x, y);
     SkGlyphRunBuilder builder;
     builder.drawText(skPaint, text, textLen, origin);
 
-
     auto glyphRunList = builder.useGlyphRunList();
     sk_sp<GrTextBlob> blob;
     if (!glyphRunList.empty()) {
-        blob = context->contextPriv().getTextBlobCache()->makeBlob(glyphRunList);
+        blob = context->contextPriv().getTextBlobCache()->makeBlob(glyphRunList, color);
         // Use the text and textLen below, because we don't want to mess with the paint.
         SkScalerContextFlags scalerContextFlags =
                 ComputeScalerContextFlags(rtc->colorSpaceInfo());
         blob->generateFromGlyphRunList(
                 glyphCache, *context->contextPriv().caps()->shaderCaps(), textContext->fOptions,
-                skPaint, filteredColor, scalerContextFlags, viewMatrix, surfaceProps,
+                skPaint, scalerContextFlags, viewMatrix, surfaceProps,
                 glyphRunList, rtc->textTarget()->glyphPainter());
     }
 
diff --git a/src/gpu/text/GrTextBlob.cpp b/src/gpu/text/GrTextBlob.cpp
index e9421ff..f14c728 100644
--- a/src/gpu/text/GrTextBlob.cpp
+++ b/src/gpu/text/GrTextBlob.cpp
@@ -25,7 +25,7 @@
     return ((s + (N-1)) / N) * N;
 }
 
-sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount) {
+sk_sp<GrTextBlob> GrTextBlob::Make(int glyphCount, int runCount, GrColor color) {
     // We allocate size for the GrTextBlob itself, plus size for the vertices array,
     // and size for the glyphIds array.
     size_t verticesCount = glyphCount * kVerticesPerGlyph * kMaxVASize;
@@ -52,7 +52,7 @@
 
     // Initialize runs
     for (int i = 0; i < runCount; i++) {
-        new (&blob->fRuns[i]) GrTextBlob::Run{blob.get()};
+        new (&blob->fRuns[i]) GrTextBlob::Run{blob.get(), color};
     }
     blob->fRunCountLimit = runCount;
     return blob;
diff --git a/src/gpu/text/GrTextBlob.h b/src/gpu/text/GrTextBlob.h
index 9bcee21..559c113 100644
--- a/src/gpu/text/GrTextBlob.h
+++ b/src/gpu/text/GrTextBlob.h
@@ -59,14 +59,13 @@
                                   const GrShaderCaps& shaderCaps,
                                   const GrTextContext::Options& options,
                                   const SkPaint& paint,
-                                  const SkPMColor4f& filteredColor,
                                   SkScalerContextFlags scalerContextFlags,
                                   const SkMatrix& viewMatrix,
                                   const SkSurfaceProps& props,
                                   const SkGlyphRunList& glyphRunList,
                                   SkGlyphRunListPainter* glyphPainter);
 
-    static sk_sp<GrTextBlob> Make(int glyphCount, int runCount);
+    static sk_sp<GrTextBlob> Make(int glyphCount, int runCount, GrColor color);
 
     /**
      * We currently force regeneration of a blob if old or new matrix differ in having perspective.
@@ -276,10 +275,15 @@
 
     class SubRun {
     public:
-        SubRun(Run* run, const SkAutoDescriptor& desc)
-            : fRun{run}
+        SubRun(Run* run, const SkAutoDescriptor& desc, GrColor color)
+            : fColor{color}
+            , fRun{run}
             , fDesc{desc} {}
 
+        // When used with emplace_back, this constructs a SubRun from the last SubRun in an array.
+        //SubRun(SkSTArray<1, SubRun>* subRunList)
+        //    : fColor{subRunList->fromBack(1).fColor} { }
+
         void appendGlyph(GrTextBlob* blob, GrGlyph* glyph, SkRect dstRect);
 
         // TODO when this object is more internal, drop the privacy
@@ -396,10 +400,10 @@
      * would greatly increase the memory of these cached items.
      */
     struct Run {
-        explicit Run(GrTextBlob* blob)
-        : fBlob{blob} {
+        explicit Run(GrTextBlob* blob, GrColor color)
+        : fBlob{blob}, fColor{color} {
             // To ensure we always have one subrun, we push back a fresh run here
-            fSubRunInfo.emplace_back(this, fDescriptor);
+            fSubRunInfo.emplace_back(this, fDescriptor, color);
         }
 
         // sets the last subrun of runIndex to use w values
@@ -413,7 +417,7 @@
         SubRun* initARGBFallback() {
             fARGBFallbackDescriptor.reset(new SkAutoDescriptor{});
             // Push back a new subrun to fill and set the override descriptor
-            SubRun* subRun = this->pushBackSubRun(*fARGBFallbackDescriptor);
+            SubRun* subRun = this->pushBackSubRun(*fARGBFallbackDescriptor, fColor);
             subRun->setMaskFormat(kARGB_GrMaskFormat);
             subRun->setFallback();
             return subRun;
@@ -428,9 +432,7 @@
         void appendGlyph(GrTextBlob* blob,
                          const sk_sp<GrTextStrike>& strike,
                          const SkGlyph& skGlyph, GrGlyph::MaskStyle maskStyle,
-                         SkPoint origin,
-                         const SkPMColor4f& color,
-                         SkScalar textRatio, bool needsTransform);
+                         SkPoint origin, SkScalar textRatio, bool needsTransform);
 
         SkExclusiveStrikePtr setupCache(const SkPaint& skPaint,
                                         const SkSurfaceProps& props,
@@ -450,11 +452,13 @@
             subRun.setHasWCoord(hasWCoord);
         }
 
-        SubRun* pushBackSubRun(const SkAutoDescriptor& desc) {
+        SubRun* pushBackSubRun(const SkAutoDescriptor& desc, GrColor color) {
             // Forward glyph / vertex information to seed the new sub run
-            SubRun& newSubRun = fSubRunInfo.emplace_back(this, desc);
+            SubRun& newSubRun = fSubRunInfo.emplace_back(this, desc, color);
+
             const SubRun& prevSubRun = fSubRunInfo.fromBack(1);
 
+            // Forward glyph / vertex information to seed the new sub run
             newSubRun.setAsSuccessor(prevSubRun);
             return &newSubRun;
         }
@@ -496,6 +500,7 @@
         bool fInitialized{false};
 
         GrTextBlob* const fBlob;
+        GrColor fColor;
     };  // Run
 
     inline std::unique_ptr<GrAtlasTextOp> makeOp(
diff --git a/src/gpu/text/GrTextBlobCache.h b/src/gpu/text/GrTextBlobCache.h
index fb991db..4e9b5ce 100644
--- a/src/gpu/text/GrTextBlobCache.h
+++ b/src/gpu/text/GrTextBlobCache.h
@@ -33,38 +33,16 @@
     }
     ~GrTextBlobCache();
 
-    // creates an uncached blob
-    sk_sp<GrTextBlob> makeBlob(int glyphCount, int runCount) {
-        return GrTextBlob::Make(glyphCount, runCount);
-    }
-
-    sk_sp<GrTextBlob> makeBlob(const SkTextBlob* blob) {
-        int glyphCount = 0;
-        int runCount = 0;
-        BlobGlyphCount(&glyphCount, &runCount, blob);
-        return GrTextBlob::Make(glyphCount, runCount);
-    }
-
-    sk_sp<GrTextBlob> makeCachedBlob(const SkTextBlob* blob,
-                                          const GrTextBlob::Key& key,
-                                          const SkMaskFilterBase::BlurRec& blurRec,
-                                          const SkPaint& paint) {
-        sk_sp<GrTextBlob> cacheBlob(this->makeBlob(blob));
-        cacheBlob->setupKey(key, blurRec, paint);
-        this->add(cacheBlob);
-        blob->notifyAddedToCache(fUniqueID);
-        return cacheBlob;
-    }
-
-    sk_sp<GrTextBlob> makeBlob(const SkGlyphRunList& glyphRunList) {
-        return GrTextBlob::Make(glyphRunList.totalGlyphCount(), glyphRunList.size());
+    sk_sp<GrTextBlob> makeBlob(const SkGlyphRunList& glyphRunList, GrColor color) {
+        return GrTextBlob::Make(glyphRunList.totalGlyphCount(), glyphRunList.size(), color);
     }
 
     sk_sp<GrTextBlob> makeCachedBlob(const SkGlyphRunList& glyphRunList,
                                      const GrTextBlob::Key& key,
                                      const SkMaskFilterBase::BlurRec& blurRec,
-                                     const SkPaint& paint) {
-        sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList));
+                                     const SkPaint& paint,
+                                     GrColor color) {
+        sk_sp<GrTextBlob> cacheBlob(makeBlob(glyphRunList, color));
         cacheBlob->setupKey(key, blurRec, paint);
         this->add(cacheBlob);
         glyphRunList.temporaryShuntBlobNotifyAddedToCache(fUniqueID);