Use shared lock for charsToGlyphs

onCharsToGlyphs show a lot of contention on the exclusive lock.
See:
https://pprof.corp.google.com/user-profile?id=efff7e7bde54d93f4351cd7adabbcbbd&tab=graph

Make the optimistic conversion use a shared lock, and only
move to an exclusive lock if chars are missing.

Bug: b/182395513

Change-Id: Idcb54687aec12a1aeb710c591d1cc5a5bad66682
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/408336
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index 43f7ebc..4ad64a1 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -1754,21 +1754,25 @@
     // can be very slow. If we do need to compute a new glyphID, then
     // access those freetype objects and continue the loop.
 
-    SkAutoMutexExclusive ama(fC2GCacheMutex);
-
     int i;
-    for (i = 0; i < count; ++i) {
-        int index = fC2GCache.findGlyphIndex(uni[i]);
-        if (index < 0) {
-            break;
+    {
+        // Optimistically use a shared lock.
+        SkAutoSharedMutexShared ama(fC2GCacheMutex);
+        for (i = 0; i < count; ++i) {
+            int index = fC2GCache.findGlyphIndex(uni[i]);
+            if (index < 0) {
+                break;
+            }
+            glyphs[i] = SkToU16(index);
         }
-        glyphs[i] = SkToU16(index);
-    }
-    if (i == count) {
-        // we're done, no need to access the freetype objects
-        return;
+        if (i == count) {
+            // we're done, no need to access the freetype objects
+            return;
+        }
     }
 
+    // Need to add more so grab an exclusive lock.
+    SkAutoSharedMutexExclusive ama(fC2GCacheMutex);
     AutoFTAccess fta(this);
     FT_Face face = fta.face();
     if (!face) {
diff --git a/src/ports/SkFontHost_FreeType_common.h b/src/ports/SkFontHost_FreeType_common.h
index fdbc25c..e6b0baa 100644
--- a/src/ports/SkFontHost_FreeType_common.h
+++ b/src/ports/SkFontHost_FreeType_common.h
@@ -11,9 +11,9 @@
 
 #include "include/core/SkTypeface.h"
 #include "include/core/SkTypes.h"
-#include "include/private/SkMutex.h"
 #include "src/core/SkGlyph.h"
 #include "src/core/SkScalerContext.h"
+#include "src/core/SkSharedMutex.h"
 #include "src/utils/SkCharToGlyphCache.h"
 
 #include "include/core/SkFontMgr.h"
@@ -128,7 +128,7 @@
     virtual std::unique_ptr<SkFontData> onMakeFontData() const = 0;
 
 private:
-    mutable SkMutex fC2GCacheMutex;
+    mutable SkSharedMutex fC2GCacheMutex;
     mutable SkCharToGlyphCache fC2GCache;
 
     using INHERITED = SkTypeface;