Extract the CPU code of SkGlyphRunPainter called SkGlyphRunPainterCPU

Simplify the fields used by each, and simplify the CTORs in
the original SkGlyphRunPainter.

TODO: come up with a better no gpu method. Eventually, this
should be pure Sk. There is no need for GPU to make and
serialize a slug only to draw them.

Change-Id: I446fe0c285ae778a14b30e09c8a2cff3cd1a56c1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/538896
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
Auto-Submit: Herb Derby <herb@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/bench/GlyphQuadFillBench.cpp b/bench/GlyphQuadFillBench.cpp
index 0892a6f..84abd9d 100644
--- a/bench/GlyphQuadFillBench.cpp
+++ b/bench/GlyphQuadFillBench.cpp
@@ -46,8 +46,7 @@
         if (canvas) { canvas->getProps(&props); }
 
         auto colorSpace = SkColorSpace::MakeSRGB();
-        SkGlyphRunListPainter painter{props, kUnknown_SkColorType,
-                                      colorSpace.get(), SkStrikeCache::GlobalStrikeCache()};
+        SkGlyphRunListPainter painter{props, colorSpace.get(), SkStrikeCache::GlobalStrikeCache()};
         SkMatrix drawMatrix = view;
         const SkPoint drawOrigin = glyphRunList.origin();
         drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
diff --git a/include/core/SkFont.h b/include/core/SkFont.h
index e9304d9..04e86a8 100644
--- a/include/core/SkFont.h
+++ b/include/core/SkFont.h
@@ -527,7 +527,7 @@
     bool hasSomeAntiAliasing() const;
 
     friend class SkFontPriv;
-    friend class SkGlyphRunListPainter;
+    friend class SkGlyphRunListPainterCPU;
     friend class SkStrikeSpec;
     friend class SkRemoteGlyphCacheTest;
 };
diff --git a/src/core/SkBitmapDevice.cpp b/src/core/SkBitmapDevice.cpp
index 1142878..b1831f0 100644
--- a/src/core/SkBitmapDevice.cpp
+++ b/src/core/SkBitmapDevice.cpp
@@ -215,10 +215,7 @@
         : INHERITED(bitmap.info(), SkSurfaceProps())
         , fBitmap(bitmap)
         , fRCStack(bitmap.width(), bitmap.height())
-        , fGlyphPainter(this->surfaceProps(),
-                        bitmap.colorType(),
-                        bitmap.colorSpace(),
-                        SkStrikeCache::GlobalStrikeCache()) {
+        , fGlyphPainter(this->surfaceProps(), bitmap.colorType(), bitmap.colorSpace()) {
     SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
 }
 
@@ -232,10 +229,7 @@
         , fBitmap(bitmap)
         , fRasterHandle(hndl)
         , fRCStack(bitmap.width(), bitmap.height())
-        , fGlyphPainter(this->surfaceProps(),
-                        bitmap.colorType(),
-                        bitmap.colorSpace(),
-                        SkStrikeCache::GlobalStrikeCache()) {
+        , fGlyphPainter(this->surfaceProps(), bitmap.colorType(), bitmap.colorSpace()) {
     SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
 }
 
diff --git a/src/core/SkBitmapDevice.h b/src/core/SkBitmapDevice.h
index 70a86bf..8d2d9d3 100644
--- a/src/core/SkBitmapDevice.h
+++ b/src/core/SkBitmapDevice.h
@@ -157,7 +157,7 @@
     SkBitmap    fBitmap;
     void*       fRasterHandle = nullptr;
     SkRasterClipStack  fRCStack;
-    SkGlyphRunListPainter fGlyphPainter;
+    SkGlyphRunListPainterCPU fGlyphPainter;
 
 
     using INHERITED = SkBaseDevice;
diff --git a/src/core/SkChromeRemoteGlyphCache.cpp b/src/core/SkChromeRemoteGlyphCache.cpp
index a430882..b832b5d 100644
--- a/src/core/SkChromeRemoteGlyphCache.cpp
+++ b/src/core/SkChromeRemoteGlyphCache.cpp
@@ -798,9 +798,8 @@
             : SkNoPixelsDevice(SkIRect::MakeSize(dimensions), props, std::move(colorSpace))
             , fStrikeServerImpl(server)
             , fDFTSupport(DFTSupport)
-            , fPainter{props, kUnknown_SkColorType, imageInfo().colorSpace(), fStrikeServerImpl}
-            , fConvertPainter{props, kUnknown_SkColorType, imageInfo().colorSpace(),
-                              SkStrikeCache::GlobalStrikeCache()} {
+            , fPainter{props, imageInfo().colorSpace(), fStrikeServerImpl}
+            , fConvertPainter{props, imageInfo().colorSpace(), SkStrikeCache::GlobalStrikeCache()} {
         SkASSERT(fStrikeServerImpl != nullptr);
     }
 
diff --git a/src/core/SkDraw.h b/src/core/SkDraw.h
index fa8f2e8..3b880ce 100644
--- a/src/core/SkDraw.h
+++ b/src/core/SkDraw.h
@@ -30,7 +30,7 @@
 class SkRRect;
 class SkVertices;
 
-class SkDraw : public SkGlyphRunListPainter::BitmapDevicePainter {
+class SkDraw : public SkGlyphRunListPainterCPU::BitmapDevicePainter {
 public:
     SkDraw();
 
@@ -62,7 +62,7 @@
                        const SkSamplingOptions&, const SkPaint&) const override;
     void    drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
     void    drawGlyphRunList(SkCanvas* canvas,
-                             SkGlyphRunListPainter* glyphPainter,
+                             SkGlyphRunListPainterCPU* glyphPainter,
                              const SkGlyphRunList& glyphRunList,
                              const SkPaint& paint) const;
     /* If skipColorXform, skips color conversion when assigning per-vertex colors */
diff --git a/src/core/SkDraw_text.cpp b/src/core/SkDraw_text.cpp
index e7dc520..ea55898 100644
--- a/src/core/SkDraw_text.cpp
+++ b/src/core/SkDraw_text.cpp
@@ -106,7 +106,7 @@
 }
 
 void SkDraw::drawGlyphRunList(SkCanvas* canvas,
-                              SkGlyphRunListPainter* glyphPainter,
+                              SkGlyphRunListPainterCPU* glyphPainter,
                               const SkGlyphRunList& glyphRunList,
                               const SkPaint& paint) const {
 
diff --git a/src/core/SkGlyphRunPainter.cpp b/src/core/SkGlyphRunPainter.cpp
index 57f3ca1..c86c6a6 100644
--- a/src/core/SkGlyphRunPainter.cpp
+++ b/src/core/SkGlyphRunPainter.cpp
@@ -37,18 +37,9 @@
 #include <cinttypes>
 #include <climits>
 
-// -- SkGlyphRunListPainter ------------------------------------------------------------------------
-SkGlyphRunListPainter::SkGlyphRunListPainter(const SkSurfaceProps& props,
-                                             SkColorType colorType,
-                                             SkScalerContextFlags flags,
-                                             SkStrikeForGPUCacheInterface* strikeCache)
-        : fDeviceProps{props}
-        ,  fBitmapFallbackProps{SkSurfaceProps{props.flags(), kUnknown_SkPixelGeometry}}
-        ,  fColorType{colorType}, fScalerContextFlags{flags}
-        ,  fStrikeCache{strikeCache} {}
-
+namespace {
 // TODO: unify with code in GrSDFTControl.cpp
-static SkScalerContextFlags compute_scaler_context_flags(const SkColorSpace* cs) {
+SkScalerContextFlags compute_scaler_context_flags(const SkColorSpace* cs) {
     // If we're doing linear blending, then we can disable the gamma hacks.
     // Otherwise, leave them on. In either case, we still want the contrast boost:
     // TODO: Can we be even smarter about mask gamma based on the dest transfer function?
@@ -58,40 +49,28 @@
         return SkScalerContextFlags::kFakeGammaAndBoostContrast;
     }
 }
+}  // namespace
 
-SkGlyphRunListPainter::SkGlyphRunListPainter(const SkSurfaceProps& props,
-                                             SkColorType colorType,
-                                             SkColorSpace* cs,
-                                             SkStrikeForGPUCacheInterface* strikeCache)
-        : SkGlyphRunListPainter(props, colorType, compute_scaler_context_flags(cs), strikeCache) {}
+// -- SkGlyphRunListPainterCPU ---------------------------------------------------------------------
+SkGlyphRunListPainterCPU::SkGlyphRunListPainterCPU(const SkSurfaceProps& props,
+                                                   SkColorType colorType,
+                                                   SkColorSpace* cs)
+        : fDeviceProps{props}
+        , fBitmapFallbackProps{SkSurfaceProps{props.flags(), kUnknown_SkPixelGeometry}}
+        , fColorType{colorType}
+        , fScalerContextFlags{compute_scaler_context_flags(cs)} {}
 
-#if SK_SUPPORT_GPU
-SkGlyphRunListPainter::SkGlyphRunListPainter(const SkSurfaceProps& props, const GrColorInfo& csi)
-        : SkGlyphRunListPainter(props,
-                                kUnknown_SkColorType,
-                                compute_scaler_context_flags(csi.colorSpace()),
-                                SkStrikeCache::GlobalStrikeCache()) {}
-
-SkGlyphRunListPainter::SkGlyphRunListPainter(const skgpu::v1::SurfaceDrawContext& sdc)
-        : SkGlyphRunListPainter{sdc.surfaceProps(), sdc.colorInfo()} {}
-
-#endif // SK_SUPPORT_GPU
-
-void SkGlyphRunListPainter::drawForBitmapDevice(
+void SkGlyphRunListPainterCPU::drawForBitmapDevice(
         SkCanvas* canvas, const BitmapDevicePainter* bitmapDevice,
         const SkGlyphRunList& glyphRunList, const SkPaint& paint, const SkMatrix& drawMatrix) {
     auto bufferScope = SkSubRunBuffers::EnsureBuffers(glyphRunList);
     auto [accepted, rejected] = bufferScope.buffers();
 
-    // TODO: fStrikeCache is only used for GPU, and some compilers complain about it during the no
-    //  gpu build. Remove when SkGlyphRunListPainter is split into GPU and CPU version.
-    (void)fStrikeCache;
-
     // The bitmap blitters can only draw lcd text to a N32 bitmap in srcOver. Otherwise,
     // convert the lcd text into A8 text. The props communicates this to the scaler.
     auto& props = (kN32_SkColorType == fColorType && paint.isSrcOver())
-                  ? fDeviceProps
-                  : fBitmapFallbackProps;
+                          ? fDeviceProps
+                          : fBitmapFallbackProps;
 
     SkPoint drawOrigin = glyphRunList.origin();
     SkMatrix positionMatrix{drawMatrix};
@@ -119,10 +98,10 @@
 
             const bool stroking = pathPaint.getStyle() != SkPaint::kFill_Style;
             const bool hairline = pathPaint.getStrokeWidth() == 0;
-            const bool needsExactCTM = pathPaint.getShader()
-                                    || pathPaint.getPathEffect()
-                                    || pathPaint.getMaskFilter()
-                                    || (stroking && !hairline);
+            const bool needsExactCTM = pathPaint.getShader()     ||
+                                       pathPaint.getPathEffect() ||
+                                       pathPaint.getMaskFilter() ||
+                                       (stroking && !hairline);
             if (!needsExactCTM) {
                 for (auto [variant, pos] : accepted->accepted()) {
                     const SkPath* path = variant.glyph()->path();
@@ -135,7 +114,7 @@
                     canvas->drawPath(*path, pathPaint);
                 }
             } else {
-               for (auto [variant, pos] : accepted->accepted()) {
+                for (auto [variant, pos] : accepted->accepted()) {
                     const SkPath* path = variant.glyph()->path();
                     SkMatrix m;
                     SkPoint translate = drawOrigin + pos;
@@ -247,8 +226,8 @@
 
                 // Since the glyph in the cache is scaled by maxScale, its top left vector is too
                 // long. Reduce it to find proper positions on the device.
-                SkPoint realPos = srcPos
-                        + SkPoint::Make(mask.fBounds.left(), mask.fBounds.top()) * (1.0f/maxScale);
+                SkPoint realPos =
+                    srcPos + SkPoint::Make(mask.fBounds.left(), mask.fBounds.top())*(1.0f/maxScale);
 
                 // Calculate the preConcat matrix for drawBitmap to get the rectangle from the
                 // glyph cache (which is multiplied by maxScale) to land in the right place.
@@ -269,11 +248,28 @@
     }
 }
 
+// -- SkGlyphRunListPainter ------------------------------------------------------------------------
+SkGlyphRunListPainter::SkGlyphRunListPainter(const SkSurfaceProps& props,
+                                             SkScalerContextFlags flags,
+                                             SkStrikeForGPUCacheInterface* strikeCache)
+        : fDeviceProps{props}
+        , fScalerContextFlags{flags}
+        , fStrikeCache{strikeCache} {}
+
+SkGlyphRunListPainter::SkGlyphRunListPainter(const SkSurfaceProps& props,
+                                             const SkColorSpace* colorSpace,
+                                             SkStrikeForGPUCacheInterface* strikeCache)
+        : SkGlyphRunListPainter{props, compute_scaler_context_flags(colorSpace), strikeCache} {}
+
+#if SK_SUPPORT_GPU
+SkGlyphRunListPainter::SkGlyphRunListPainter(const skgpu::v1::SurfaceDrawContext& sdc)
+        : SkGlyphRunListPainter{sdc.surfaceProps(),
+                                compute_scaler_context_flags(sdc.colorInfo().colorSpace()),
+                                SkStrikeCache::GlobalStrikeCache()} {}
+
 // Use the following in your args.gn to dump telemetry for diagnosing chrome Renderer/GPU
 // differences.
 // extra_cflags = ["-D", "SK_TRACE_GLYPH_RUN_PROCESS"]
-
-#if SK_SUPPORT_GPU
 void SkGlyphRunListPainter::categorizeGlyphRunList(SkGlyphRunPainterInterface* process,
                                                    const SkGlyphRunList& glyphRunList,
                                                    const SkMatrix& positionMatrix,
diff --git a/src/core/SkGlyphRunPainter.h b/src/core/SkGlyphRunPainter.h
index 36df729..51a7035 100644
--- a/src/core/SkGlyphRunPainter.h
+++ b/src/core/SkGlyphRunPainter.h
@@ -50,21 +50,9 @@
     inline static constexpr uint16_t kSkSideTooBigForAtlas = 256;
 };
 
-class SkGlyphRunListPainter {
+// -- SkGlyphRunListPainterCPU ---------------------------------------------------------------------
+class SkGlyphRunListPainterCPU {
 public:
-    // Constructor for SkBitmpapDevice.
-    SkGlyphRunListPainter(const SkSurfaceProps& props,
-                          SkColorType colorType,
-                          SkColorSpace* cs,
-                          SkStrikeForGPUCacheInterface* strikeCache);
-
-#if SK_SUPPORT_GPU
-    // The following two ctors are used exclusively by the GPU, and will always use the global
-    // strike cache.
-    SkGlyphRunListPainter(const SkSurfaceProps&, const GrColorInfo&);
-    explicit SkGlyphRunListPainter(const skgpu::v1::SurfaceDrawContext&);
-#endif  // SK_SUPPORT_GPU
-
     class BitmapDevicePainter {
     public:
         BitmapDevicePainter() = default;
@@ -76,9 +64,33 @@
                                 const SkSamplingOptions&, const SkPaint&) const = 0;
     };
 
+    SkGlyphRunListPainterCPU(const SkSurfaceProps& props,
+                             SkColorType colorType,
+                             SkColorSpace* cs);
+
     void drawForBitmapDevice(
             SkCanvas* canvas, const BitmapDevicePainter* bitmapDevice,
             const SkGlyphRunList& glyphRunList, const SkPaint& paint, const SkMatrix& drawMatrix);
+private:
+    // The props as on the actual device.
+    const SkSurfaceProps fDeviceProps;
+
+    // The props for when the bitmap device can't draw LCD text.
+    const SkSurfaceProps fBitmapFallbackProps;
+    const SkColorType fColorType;
+    const SkScalerContextFlags fScalerContextFlags;
+};
+
+class SkGlyphRunListPainter {
+public:
+    SkGlyphRunListPainter(const SkSurfaceProps& props,
+                          const SkColorSpace* colorSpace,
+                          SkStrikeForGPUCacheInterface* strikeCache);
+#if SK_SUPPORT_GPU
+    // The following ctor is used exclusively by the GPU, and will always use the global
+    // strike cache.
+    explicit SkGlyphRunListPainter(const skgpu::v1::SurfaceDrawContext&);
+#endif  // SK_SUPPORT_GPU
 
 #if SK_SUPPORT_GPU
     // A nullptr for process means that the calls to the cache will be performed, but none of the
@@ -93,17 +105,15 @@
 #endif  // SK_SUPPORT_GPU
 
 private:
-    SkGlyphRunListPainter(const SkSurfaceProps& props, SkColorType colorType,
-                          SkScalerContextFlags flags, SkStrikeForGPUCacheInterface* strikeCache);
+    SkGlyphRunListPainter(const SkSurfaceProps& props,
+                          SkScalerContextFlags flags,
+                          SkStrikeForGPUCacheInterface* strikeCache);
 
     // The props as on the actual device.
-    const SkSurfaceProps fDeviceProps;
-    // The props for when the bitmap device can't draw LCD text.
-    const SkSurfaceProps fBitmapFallbackProps;
-    const SkColorType fColorType;
-    const SkScalerContextFlags fScalerContextFlags;
+    [[maybe_unused]] const SkSurfaceProps fDeviceProps;
+    [[maybe_unused]] const SkScalerContextFlags fScalerContextFlags;
 
-    SkStrikeForGPUCacheInterface* const fStrikeCache;
+    [[maybe_unused]] SkStrikeForGPUCacheInterface* const fStrikeCache;
 };
 
 // SkGlyphRunPainterInterface are all the ways that Ganesh generates glyphs. The first
diff --git a/src/core/SkOverdrawCanvas.cpp b/src/core/SkOverdrawCanvas.cpp
index 36e5b9d..b9b3e68 100644
--- a/src/core/SkOverdrawCanvas.cpp
+++ b/src/core/SkOverdrawCanvas.cpp
@@ -43,12 +43,12 @@
 }
 
 namespace {
-class TextDevice : public SkNoPixelsDevice, public SkGlyphRunListPainter::BitmapDevicePainter {
+class TextDevice : public SkNoPixelsDevice, public SkGlyphRunListPainterCPU::BitmapDevicePainter {
 public:
     TextDevice(SkCanvas* overdrawCanvas, const SkSurfaceProps& props)
             : SkNoPixelsDevice{SkIRect::MakeWH(32767, 32767), props},
               fOverdrawCanvas{overdrawCanvas},
-              fPainter{props, kN32_SkColorType, nullptr, SkStrikeCache::GlobalStrikeCache()} {}
+              fPainter{props, kN32_SkColorType, nullptr} {}
 
     void paintMasks(SkDrawableGlyphBuffer* accepted, const SkPaint& paint) const override {
         for (auto t : accepted->accepted()) {
@@ -73,7 +73,7 @@
 
 private:
     SkCanvas* const fOverdrawCanvas;
-    SkGlyphRunListPainter fPainter;
+    SkGlyphRunListPainterCPU fPainter;
 };
 }  // namespace