Move the bulk glyph calls up the stack

Moving this call up the stack gets thing in a better position
to bulkify this whole call stack.

Clean up some of the API to make more sense.

Change-Id: Iad7aca81f60b74a61344ff0679c1c323c49b7f48
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/268436
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/src/gpu/text/GrStrikeCache.cpp b/src/gpu/text/GrStrikeCache.cpp
index 12f4677..b86acd7 100644
--- a/src/gpu/text/GrStrikeCache.cpp
+++ b/src/gpu/text/GrStrikeCache.cpp
@@ -49,9 +49,8 @@
     }
 }
 
-static void get_packed_glyph_image(const SkGlyph* glyph, int width,
-                                   int height, int dstRB, GrMaskFormat expectedMaskFormat,
-                                   void* dst) {
+static void get_packed_glyph_image(
+        const SkGlyph& glyph, int dstRB, GrMaskFormat expectedMaskFormat, void* dst) {
 
     static constexpr SkMasks masks{
             {0b1111'1000'0000'0000, 11, 5},  // Red
@@ -60,13 +59,13 @@
             {0, 0, 0}                        // Alpha
     };
 
-    SkASSERT(glyph->width() == width);
-    SkASSERT(glyph->height() == height);
+    const int width = glyph.width();
+    const int height = glyph.height();
 
-    const void* src = glyph->image();
+    const void* src = glyph.image();
     SkASSERT(src != nullptr);
 
-    if (kA565_GrMaskFormat == GrGlyph::FormatFromSkGlyph(glyph->maskFormat()) &&
+    if (kA565_GrMaskFormat == GrGlyph::FormatFromSkGlyph(glyph.maskFormat()) &&
         kARGB_GrMaskFormat == expectedMaskFormat) {
         // 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
@@ -86,7 +85,7 @@
                 dst = (char*)dst + argbBpp;
             }
         }
-    } else if (GrGlyph::FormatFromSkGlyph(glyph->maskFormat()) != expectedMaskFormat) {
+    } else if (GrGlyph::FormatFromSkGlyph(glyph.maskFormat()) != expectedMaskFormat) {
         // 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.
@@ -96,11 +95,11 @@
             dst = (char*)dst + dstRB;
         }
     } else {
-        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) {
@@ -153,33 +152,31 @@
     });
 }
 
-GrDrawOpAtlas::ErrorCode GrTextStrike::addGlyphToAtlas(
-                                   GrResourceProvider* resourceProvider,
-                                   GrDeferredUploadTarget* target,
-                                   GrAtlasManager* fullAtlasManager,
-                                   GrGlyph* glyph,
-                                   SkBulkGlyphMetricsAndImages* metricsAndImages,
-                                   GrMaskFormat expectedMaskFormat,
-                                   bool isScaledGlyph) {
-    SkASSERT(glyph);
-    SkASSERT(metricsAndImages);
-    SkASSERT(fCache.findOrNull(glyph->fPackedID));
+GrDrawOpAtlas::ErrorCode GrTextStrike::addGlyphToAtlas(const SkGlyph& skGlyph,
+                                                       GrMaskFormat expectedMaskFormat,
+                                                       bool isScaledGlyph,
+                                                       GrResourceProvider* resourceProvider,
+                                                       GrDeferredUploadTarget* target,
+                                                       GrAtlasManager* fullAtlasManager,
+                                                       GrGlyph* grGlyph) {
+    SkASSERT(grGlyph != nullptr);
+    SkASSERT(fCache.findOrNull(grGlyph->fPackedID));
+    SkASSERT(grGlyph->width() == skGlyph.width());
+    SkASSERT(grGlyph->height() == skGlyph.height());
+    SkASSERT(skGlyph.image() != nullptr);
 
     expectedMaskFormat = fullAtlasManager->resolveMaskFormat(expectedMaskFormat);
     int bytesPerPixel = GrMaskFormatBytesPerPixel(expectedMaskFormat);
 
-    bool isSDFGlyph = glyph->maskStyle() == GrGlyph::kDistance_MaskStyle;
-    // Add 1 pixel padding around glyph if needed.
+    bool isSDFGlyph = grGlyph->maskStyle() == GrGlyph::kDistance_MaskStyle;
+    // Add 1 pixel padding around grGlyph if needed.
     bool addPad = isScaledGlyph && !isSDFGlyph;
-    const int width = addPad ? glyph->width() + 2 : glyph->width();
-    const int height = addPad ? glyph->height() + 2 : glyph->height();
+    const int width = addPad ? grGlyph->width() + 2 : grGlyph->width();
+    const int height = addPad ? grGlyph->height() + 2 : grGlyph->height();
     int rowBytes = width * bytesPerPixel;
     size_t size = height * rowBytes;
 
-    const SkGlyph* skGlyph = metricsAndImages->glyph(glyph->fPackedID);
-    if (skGlyph->image() == nullptr) { return GrDrawOpAtlas::ErrorCode::kError; }
-
-    // Temporary storage for normalizing glyph image.
+    // Temporary storage for normalizing grGlyph image.
     SkAutoSMalloc<1024> storage(size);
     void* dataPtr = storage.get();
     if (addPad) {
@@ -188,19 +185,18 @@
         dataPtr = (char*)(dataPtr) + rowBytes + bytesPerPixel;
     }
 
-    get_packed_glyph_image(skGlyph, glyph->width(), glyph->height(),
-            rowBytes, expectedMaskFormat, dataPtr);
+    get_packed_glyph_image(skGlyph, rowBytes, expectedMaskFormat, dataPtr);
 
     GrDrawOpAtlas::ErrorCode result = fullAtlasManager->addToAtlas(
-            resourceProvider, &glyph->fPlotLocator, target, expectedMaskFormat,
+            resourceProvider, &grGlyph->fPlotLocator, target, expectedMaskFormat,
             width, height,
-            storage.get(), &glyph->fAtlasLocation);
+            storage.get(), &grGlyph->fAtlasLocation);
     if (GrDrawOpAtlas::ErrorCode::kSucceeded == result) {
         if (addPad) {
-            glyph->fAtlasLocation.fX += 1;
-            glyph->fAtlasLocation.fY += 1;
+            grGlyph->fAtlasLocation.fX += 1;
+            grGlyph->fAtlasLocation.fY += 1;
         }
-        SkASSERT(GrDrawOpAtlas::kInvalidPlotLocator != glyph->fPlotLocator);
+        SkASSERT(grGlyph->fPlotLocator != GrDrawOpAtlas::kInvalidPlotLocator);
         fAtlasedGlyphs++;
     }
     return result;
diff --git a/src/gpu/text/GrStrikeCache.h b/src/gpu/text/GrStrikeCache.h
index f3696b2..7ff6559 100644
--- a/src/gpu/text/GrStrikeCache.h
+++ b/src/gpu/text/GrStrikeCache.h
@@ -44,11 +44,13 @@
     // happen.
     // TODO we can handle some of these cases if we really want to, but the long term solution is to
     // get the actual glyph image itself when we get the glyph metrics.
-    GrDrawOpAtlas::ErrorCode addGlyphToAtlas(GrResourceProvider*, GrDeferredUploadTarget*,
-                                             GrAtlasManager*, GrGlyph*,
-                                             SkBulkGlyphMetricsAndImages*,
+    GrDrawOpAtlas::ErrorCode addGlyphToAtlas(const SkGlyph&,
                                              GrMaskFormat expectedMaskFormat,
-                                             bool isScaledGlyph);
+                                             bool isScaledGlyph,
+                                             GrResourceProvider*,
+                                             GrDeferredUploadTarget*,
+                                             GrAtlasManager*,
+                                             GrGlyph*);
 
     // testing
     int countGlyphs() const { return fCache.count(); }
diff --git a/src/gpu/text/GrTextBlob.cpp b/src/gpu/text/GrTextBlob.cpp
index ee9623c..8ead191 100644
--- a/src/gpu/text/GrTextBlob.cpp
+++ b/src/gpu/text/GrTextBlob.cpp
@@ -790,19 +790,22 @@
     auto tokenTracker = fUploadTarget->tokenTracker();
     int i = begin;
     for (; i < end; i++) {
-        GrGlyph* glyph = fSubRun->fGlyphs[i];
-        SkASSERT(glyph && glyph->fMaskFormat == fSubRun->maskFormat());
+        GrGlyph* grGlyph = fSubRun->fGlyphs[i];
+        SkASSERT(grGlyph && grGlyph->fMaskFormat == fSubRun->maskFormat());
 
-        if (!fFullAtlasManager->hasGlyph(glyph)) {
-            code = grStrike->addGlyphToAtlas(
-                    fResourceProvider, fUploadTarget, fFullAtlasManager, glyph,
-                    fMetricsAndImages.get(), fSubRun->maskFormat(), fSubRun->needsTransform());
+        if (!fFullAtlasManager->hasGlyph(grGlyph)) {
+            const SkGlyph& skGlyph = *fMetricsAndImages->glyph(grGlyph->fPackedID);
+            if (skGlyph.image() == nullptr) { return {false, 0}; }
+            code = grStrike->addGlyphToAtlas(skGlyph,
+                    fSubRun->maskFormat(),
+                    fSubRun->needsTransform(),
+                    fResourceProvider, fUploadTarget, fFullAtlasManager, grGlyph);
             if (code != GrDrawOpAtlas::ErrorCode::kSucceeded) {
                 break;
             }
         }
         fFullAtlasManager->addGlyphToBulkAndSetUseToken(
-                fSubRun->bulkUseToken(), glyph, tokenTracker->nextDrawToken());
+                fSubRun->bulkUseToken(), grGlyph, tokenTracker->nextDrawToken());
     }
     int glyphsPlacedInAtlas = i - begin;