Dealing with SkScalar values in cache

Change-Id: I0da874242f243b3d3e71834240adc424bcb965c8
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/266558
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Julia Lavrova <jlavrova@google.com>
diff --git a/modules/skparagraph/src/ParagraphCache.cpp b/modules/skparagraph/src/ParagraphCache.cpp
index e897882..364a169 100644
--- a/modules/skparagraph/src/ParagraphCache.cpp
+++ b/modules/skparagraph/src/ParagraphCache.cpp
@@ -5,6 +5,14 @@
 namespace skia {
 namespace textlayout {
 
+namespace {
+    SkScalar relax(SkScalar a) {
+        // This rounding is done to match Flutter tests. Must be removed..
+      auto threshold = SkIntToScalar(1 << 12);
+      return SkScalarRoundToScalar(a * threshold)/threshold;
+    }
+}
+
 class ParagraphCacheKey {
 public:
     ParagraphCacheKey(const ParagraphImpl* paragraph)
@@ -50,24 +58,26 @@
     for (auto& ph : key.fPlaceholders) {
         hash = mix(hash, SkGoodHash()(ph.fRange.start));
         hash = mix(hash, SkGoodHash()(ph.fRange.end));
-        hash = mix(hash, SkGoodHash()(ph.fStyle.fBaselineOffset));
+        hash = mix(hash, SkGoodHash()(relax(ph.fStyle.fBaselineOffset)));
         hash = mix(hash, SkGoodHash()(ph.fStyle.fBaseline));
         hash = mix(hash, SkGoodHash()(ph.fStyle.fAlignment));
-        hash = mix(hash, SkGoodHash()(ph.fStyle.fHeight));
-        hash = mix(hash, SkGoodHash()(ph.fStyle.fWidth));
+        hash = mix(hash, SkGoodHash()(relax(ph.fStyle.fHeight)));
+        hash = mix(hash, SkGoodHash()(relax(ph.fStyle.fWidth)));
     }
     for (auto& ts : key.fTextStyles) {
         if (ts.fStyle.isPlaceholder()) {
             continue;
         }
-        hash = mix(hash, SkGoodHash()(ts.fStyle.getLetterSpacing()));
-        hash = mix(hash, SkGoodHash()(ts.fStyle.getWordSpacing()));
+        hash = mix(hash, SkGoodHash()(relax(ts.fStyle.getLetterSpacing())));
+        hash = mix(hash, SkGoodHash()(relax(ts.fStyle.getWordSpacing())));
+        hash = mix(hash, SkGoodHash()(ts.fStyle.getLocale()));
+        hash = mix(hash, SkGoodHash()(relax(ts.fStyle.getHeight())));
         hash = mix(hash, SkGoodHash()(ts.fRange));
         for (auto& ff : ts.fStyle.getFontFamilies()) {
             hash = mix(hash, SkGoodHash()(ff));
         }
         hash = mix(hash, SkGoodHash()(ts.fStyle.getFontStyle()));
-        hash = mix(hash, SkGoodHash()(ts.fStyle.getFontSize()));
+        hash = mix(hash, SkGoodHash()(relax(ts.fStyle.getFontSize())));
         hash = mix(hash, SkGoodHash()(ts.fRange.start));
         hash = mix(hash, SkGoodHash()(ts.fRange.end));
     }
diff --git a/modules/skparagraph/src/TextStyle.cpp b/modules/skparagraph/src/TextStyle.cpp
index d4d1b81..1cae43d 100644
--- a/modules/skparagraph/src/TextStyle.cpp
+++ b/modules/skparagraph/src/TextStyle.cpp
@@ -107,10 +107,10 @@
     return !fIsPlaceholder && !that.fIsPlaceholder &&
            fFontStyle == that.fFontStyle &&
            fFontFamilies == that.fFontFamilies &&
-           fLetterSpacing == that.fLetterSpacing &&
-           fWordSpacing == that.fWordSpacing &&
-           fHeight == that.fHeight &&
-           fFontSize == that.fFontSize &&
+           SkScalarNearlyEqual(fLetterSpacing, that.fLetterSpacing) &&
+           SkScalarNearlyEqual(fWordSpacing, that.fWordSpacing) &&
+           SkScalarNearlyEqual(fHeight, that.fHeight) &&
+           SkScalarNearlyEqual(fFontSize, that.fFontSize) &&
            fLocale == that.fLocale;
 }
 
@@ -179,11 +179,11 @@
 }
 
 bool PlaceholderStyle::equals(const PlaceholderStyle& other) const {
-    return this->fWidth == other.fWidth &&
-           this->fHeight == other.fHeight &&
-           this->fAlignment == other.fAlignment &&
-           this->fBaseline == other.fBaseline &&
-           this->fBaselineOffset == other.fBaselineOffset;
+    return SkScalarNearlyEqual(fWidth, other.fWidth) &&
+           SkScalarNearlyEqual(fHeight, other.fHeight) &&
+           fAlignment == other.fAlignment &&
+           fBaseline == other.fBaseline &&
+           SkScalarNearlyEqual(fBaselineOffset, other.fBaselineOffset);
 }
 
 }  // namespace textlayout