findImage(const SkGlyph&) -> prepareImage(SkGlyph*)

Because it mutates the glyph.

Change-Id: Ic7ce320350764454d7a76335828d398f19b149d6
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/223797
Commit-Queue: Herb Derby <herb@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/src/core/SkStrike.cpp b/src/core/SkStrike.cpp
index 6694218..ec09209 100644
--- a/src/core/SkStrike.cpp
+++ b/src/core/SkStrike.cpp
@@ -45,6 +45,7 @@
 }
 
 SkGlyph* SkStrike::glyph(SkPackedGlyphID packedGlyphID) {
+    VALIDATE();
     SkGlyph* glyph = fGlyphMap.findOrNull(packedGlyphID);
     if (glyph == nullptr) {
         glyph = this->makeGlyph(packedGlyphID);
@@ -110,7 +111,6 @@
 
 SkSpan<const SkGlyph*> SkStrike::metrics(
         SkSpan<const SkGlyphID>glyphIDs, const SkGlyph* results[]) {
-
     size_t glyphCount = 0;
     for (auto glyphID : glyphIDs) {
         SkGlyph* glyphPtr = this->glyph(glyphID);
@@ -130,8 +130,8 @@
     return {advances, glyphIDs.size()};
 }
 
-const void* SkStrike::findImage(const SkGlyph& glyphRef) {
-    SkGlyph* glyph = const_cast<SkGlyph*>(&glyphRef);
+
+const void* SkStrike::prepareImage(SkGlyph* glyph) {
     if (glyph->setImage(&fAlloc, fScalerContext.get())) {
         fMemoryUsed += glyph->imageSize();
     }
@@ -190,21 +190,20 @@
         if (SkScalarsAreFinite(position.x(), position.y())) {
             // This assumes that the strike has no sub-pixel positioning for glyphs that are
             // transformed from source space to device space.
-            const SkGlyph& glyph = this->getGlyphMetrics(glyphIDs[i], position);
-            if (!glyph.isEmpty()) {
-                result[drawableGlyphCount++] = {i, &glyph, position};
-                if (glyph.maxDimension() <= maxDimension) {
-                    // Glyph fits in the atlas, good to go.
+            SkGlyph* glyph = this->glyph(glyphIDs[i], position);
+            if (!glyph->isEmpty()) {
+                result[drawableGlyphCount++] = {i, glyph, position};
+                if (glyph->maxDimension() <= maxDimension) {
+                    // Glyph is an allowable size, good to go.
                     if (detail == SkStrikeInterface::kImageIfNeeded) {
-                        this->findImage(glyph);
+                        this->prepareImage(glyph);
                     }
-                } else if (glyph.fMaskFormat != SkMask::kARGB32_Format) {
+                } else if (!glyph->isColor()) {
                     // The out of atlas glyph is not color so we can draw it using paths.
-                    this->preparePath(const_cast<SkGlyph*>(&glyph));
+                    this->preparePath(glyph);
                 } else {
                     // This will be handled by the fallback strike.
-                    SkASSERT(glyph.maxDimension() > maxDimension
-                             && glyph.fMaskFormat == SkMask::kARGB32_Format);
+                    SkASSERT(glyph->maxDimension() > maxDimension && glyph->isColor());
                 }
             }
         }
diff --git a/src/core/SkStrike.h b/src/core/SkStrike.h
index dc9227f..8f614f6 100644
--- a/src/core/SkStrike.h
+++ b/src/core/SkStrike.h
@@ -40,15 +40,19 @@
              const SkFontMetrics&);
 
     // Return a glyph. Create it if it doesn't exist, and initialize the glyph with metrics and
-    // advances.
+    // advances using a scaler.
     SkGlyph* glyph(SkPackedGlyphID packedID);
     SkGlyph* glyph(SkGlyphID glyphID);
-    SkGlyph* glyphFromPrototype(const SkGlyphPrototype& p, void* image = nullptr);
     SkGlyph* glyph(SkGlyphID, SkPoint);
 
+    // Return a glyph.  Create it if it doesn't exist, and initialize with the prototype.
+    SkGlyph* glyphFromPrototype(const SkGlyphPrototype& p, void* image = nullptr);
+
     // Return a glyph or nullptr if it does not exits in the strike.
     SkGlyph* glyphOrNull(SkPackedGlyphID id) const;
 
+    const void* prepareImage(SkGlyph* glyph);
+
     // Lookup (or create if needed) the toGlyph using toID. If that glyph is not initialized with
     // an image, then use the information in from to initialize the width, height top, left,
     // format and image of the toGlyph. This is mainly used preserving the glyph if it was
@@ -70,11 +74,6 @@
     /** Return the number of glyphs currently cached. */
     int countCachedGlyphs() const;
 
-    /** Return the image associated with the glyph. If it has not been generated this will
-        trigger that.
-    */
-    const void* findImage(const SkGlyph&);
-
     /** If the advance axis intersects the glyph's path, append the positions scaled and offset
         to the array (if non-null), and set the count to the updated array length.
     */
diff --git a/src/gpu/GrGlyph.h b/src/gpu/GrGlyph.h
index 19687ce..96ea558 100644
--- a/src/gpu/GrGlyph.h
+++ b/src/gpu/GrGlyph.h
@@ -22,8 +22,8 @@
         kDistance_MaskStyle
     };
 
-    static GrMaskFormat FormatFromSkGlyph(const SkGlyph& glyph) {
-        switch (glyph.maskFormat()) {
+    static GrMaskFormat FormatFromSkGlyph(SkMask::Format format) {
+        switch (format) {
             case SkMask::kBW_Format:
             case SkMask::kSDF_Format:
                 // fall through to kA8 -- we store BW and SDF glyphs in our 8-bit cache
@@ -49,7 +49,7 @@
 
     GrGlyph(const SkGlyph& skGlyph)
         : fPackedID{skGlyph.getPackedID()}
-        , fMaskFormat{FormatFromSkGlyph(skGlyph)}
+        , fMaskFormat{FormatFromSkGlyph(skGlyph.maskFormat())}
         , fMaskStyle{MaskStyleFromSkGlyph(skGlyph)}
         , fBounds{GrIRect16::Make(skGlyph.iRect())} {}
 
diff --git a/src/gpu/text/GrStrikeCache.cpp b/src/gpu/text/GrStrikeCache.cpp
index 09081c4..f445c65 100644
--- a/src/gpu/text/GrStrikeCache.cpp
+++ b/src/gpu/text/GrStrikeCache.cpp
@@ -80,20 +80,20 @@
     }
 }
 
-static bool get_packed_glyph_image(SkStrike* cache, const SkGlyph& glyph, int width,
+static bool get_packed_glyph_image(SkStrike* cache, SkGlyph* glyph, int width,
                                    int height, int dstRB, GrMaskFormat expectedMaskFormat,
                                    void* dst, const SkMasks& masks) {
-    SkASSERT(glyph.width() == width);
-    SkASSERT(glyph.height() == height);
-    const void* src = cache->findImage(glyph);
-    if (nullptr == src) {
+    SkASSERT(glyph->width() == width);
+    SkASSERT(glyph->height() == height);
+    const void* src = cache->prepareImage(glyph);
+    if (src == nullptr) {
         return false;
     }
 
     // Convert if the glyph uses a 565 mask format since it is using LCD text rendering but the
     // expected format is 8888 (will happen on macOS with Metal since that combination does not
     // support 565).
-    if (kA565_GrMaskFormat == GrGlyph::FormatFromSkGlyph(glyph) &&
+    if (kA565_GrMaskFormat == GrGlyph::FormatFromSkGlyph(glyph->maskFormat()) &&
         kARGB_GrMaskFormat == expectedMaskFormat) {
         const int a565Bpp = GrMaskFormatBytesPerPixel(kA565_GrMaskFormat);
         const int argbBpp = GrMaskFormatBytesPerPixel(kARGB_GrMaskFormat);
@@ -116,7 +116,7 @@
     // crbug:510931
     // Retrieving the image from the cache can actually change the mask format.  This case is very
     // uncommon so for now we just draw a clear box for these glyphs.
-    if (GrGlyph::FormatFromSkGlyph(glyph) != expectedMaskFormat) {
+    if (GrGlyph::FormatFromSkGlyph(glyph->maskFormat()) != expectedMaskFormat) {
         const int bpp = GrMaskFormatBytesPerPixel(expectedMaskFormat);
         for (int y = 0; y < height; y++) {
             sk_bzero(dst, width * bpp);
@@ -125,11 +125,11 @@
         return true;
     }
 
-    int srcRB = glyph.rowBytes();
+    int srcRB = glyph->rowBytes();
     // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to
     // check the glyph's format, not the strike's format, and to be able to convert to any of the
     // GrMaskFormats.
-    if (glyph.maskFormat() == SkMask::kBW_Format) {
+    if (glyph->maskFormat() == SkMask::kBW_Format) {
         // expand bits to our mask type
         const uint8_t* bits = reinterpret_cast<const uint8_t*>(src);
         switch (expectedMaskFormat) {
@@ -221,7 +221,7 @@
         sk_bzero(dataPtr, size);
         dataPtr = (char*)(dataPtr) + rowBytes + bytesPerPixel;
     }
-    if (!get_packed_glyph_image(skStrikeCache, *skGlyph, glyph->width(), glyph->height(),
+    if (!get_packed_glyph_image(skStrikeCache, skGlyph, glyph->width(), glyph->height(),
                                 rowBytes, expectedMaskFormat,
                                 dataPtr, glyphCache->getMasks())) {
         return GrDrawOpAtlas::ErrorCode::kError;
diff --git a/src/pdf/SkPDFFont.cpp b/src/pdf/SkPDFFont.cpp
index 716e64a..b9c8195 100644
--- a/src/pdf/SkPDFFont.cpp
+++ b/src/pdf/SkPDFFont.cpp
@@ -448,7 +448,7 @@
     SkIPoint fOffset;
 };
 static ImageAndOffset to_image(SkGlyphID gid, SkStrike* cache) {
-    (void)cache->findImage(*cache->glyph(gid));
+    (void)cache->prepareImage(cache->glyph(gid));
     SkMask mask = cache->glyph(gid)->mask();
     if (!mask.fImage) {
         return {nullptr, {0, 0}};