Roll HarfBuzz from 2.6.4 to 2.8.2-147

https://chromium.googlesource.com/external/github.com/harfbuzz/harfbuzz.git/+log/3a74ee528255cc027d84b204a87b5c25e47bff79..368e9578873798e2d17ed78a0474dec7d4e9d6c0

Additionally adds a build option that switches to the new
harfbuzz subsetting api which is coming in the next version
of harfbuzz.

Change-Id: I924a7b4978412d636d4c8d19f5c6021ea3c73d21
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/433737
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index aeb46b46..f737ef0 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1047,6 +1047,9 @@
   if (skia_use_icu && skia_use_harfbuzz && skia_pdf_subset_harfbuzz) {
     deps += [ "//third_party/harfbuzz" ]
     defines = [ "SK_PDF_USE_HARFBUZZ_SUBSET" ]
+    if (skia_subset_harfbuzz_new_api) {
+      defines += [ "SK_HARFBUZZ_SUBSET_NEW_API" ]
+    }
   } else if (skia_use_icu && skia_use_sfntly) {
     deps += [ "//third_party/sfntly" ]
     defines = [ "SK_PDF_USE_SFNTLY" ]
diff --git a/DEPS b/DEPS
index 791541a..a3b11ea 100644
--- a/DEPS
+++ b/DEPS
@@ -24,7 +24,7 @@
   "third_party/externals/egl-registry"    : "https://skia.googlesource.com/external/github.com/KhronosGroup/EGL-Registry@a0bca08de07c7d7651047bedc0b653cfaaa4f2ae",
   "third_party/externals/expat"           : "https://chromium.googlesource.com/external/github.com/libexpat/libexpat.git@a28238bdeebc087071777001245df1876a11f5ee",
   "third_party/externals/freetype"        : "https://chromium.googlesource.com/chromium/src/third_party/freetype2.git@47b1a541cb1943d85da3976b93f9a5ed490288e2",
-  "third_party/externals/harfbuzz"        : "https://chromium.googlesource.com/external/github.com/harfbuzz/harfbuzz.git@3a74ee528255cc027d84b204a87b5c25e47bff79",
+  "third_party/externals/harfbuzz"        : "https://chromium.googlesource.com/external/github.com/harfbuzz/harfbuzz.git@368e9578873798e2d17ed78a0474dec7d4e9d6c0",
   "third_party/externals/icu"             : "https://chromium.googlesource.com/chromium/deps/icu.git@a0718d4f121727e30b8d52c7a189ebf5ab52421f",
   "third_party/externals/imgui"           : "https://skia.googlesource.com/external/github.com/ocornut/imgui.git@9418dcb69355558f70de260483424412c5ca2fce",
   "third_party/externals/libgifcodec"     : "https://skia.googlesource.com/libgifcodec@fd59fa92a0c86788dcdd84d091e1ce81eda06a77",
diff --git a/gn/gn_to_bp.py b/gn/gn_to_bp.py
index a813c73..9b2e999 100755
--- a/gn/gn_to_bp.py
+++ b/gn/gn_to_bp.py
@@ -402,6 +402,9 @@
     # files.
     'target_cpu':                           '"none"',
 
+    # disable harfbuzz new subsetting api until android picks up harfbuzz 3.0.0
+    'skia_subset_harfbuzz_new_api':         'false',
+
     # Use the custom FontMgr, as the framework will handle fonts.
     'skia_enable_fontmgr_custom_directory': 'false',
     'skia_enable_fontmgr_custom_embedded':  'false',
diff --git a/gn/skia.gni b/gn/skia.gni
index d98fdc1..fdb0935 100644
--- a/gn/skia.gni
+++ b/gn/skia.gni
@@ -34,8 +34,6 @@
   skia_include_multiframe_procs = false
   skia_lex = false
   skia_libgifcodec_path = "third_party/externals/libgifcodec"
-  skia_pdf_subset_harfbuzz =
-      false  # TODO: set skia_pdf_subset_harfbuzz to skia_use_harfbuzz.
   skia_qt_path = getenv("QT_PATH")
   skia_skqp_global_error_tolerance = 0
   skia_tools_require_resources = false
@@ -100,6 +98,11 @@
 }
 
 declare_args() {
+  skia_pdf_subset_harfbuzz = skia_use_harfbuzz
+  skia_subset_harfbuzz_new_api = !is_fuchsia
+}
+
+declare_args() {
   skia_compile_sksl_tests = skia_compile_processors
   skia_enable_fontmgr_android = skia_use_expat && skia_use_freetype
   skia_enable_fontmgr_custom_directory = skia_use_freetype && !is_fuchsia
diff --git a/src/pdf/SkPDFSubsetFont.cpp b/src/pdf/SkPDFSubsetFont.cpp
index 81c37ee..3289940 100644
--- a/src/pdf/SkPDFSubsetFont.cpp
+++ b/src/pdf/SkPDFSubsetFont.cpp
@@ -71,11 +71,21 @@
     hb_set_t* glyphs = hb_subset_input_glyph_set(input.get());
     glyphUsage.getSetValues([&glyphs](unsigned gid) { hb_set_add(glyphs, gid);});
 
+#if defined(SK_HARFBUZZ_SUBSET_NEW_API)
+    // TODO: When possible, check if a font is 'tricky' with FT_IS_TRICKY.
+    // If it isn't known if a font is 'tricky', retain the hints.
+    hb_subset_input_set_flags(input.get(), HB_SUBSET_FLAGS_RETAIN_GIDS);
+    HBFace subset(hb_subset_or_fail(face.get(), input.get()));
+#else
     hb_subset_input_set_retain_gids(input.get(), true);
     // TODO: When possible, check if a font is 'tricky' with FT_IS_TRICKY.
     // If it isn't known if a font is 'tricky', retain the hints.
     hb_subset_input_set_drop_hints(input.get(), false);
     HBFace subset(hb_subset(face.get(), input.get()));
+#endif
+    if (!subset) {
+      return nullptr;
+    }
     HBBlob result(hb_face_reference_blob(subset.get()));
     return to_data(std::move(result));
 }
@@ -178,4 +188,3 @@
     return nullptr;
 }
 #endif  // defined(SK_PDF_USE_SFNTLY)
-
diff --git a/third_party/harfbuzz/BUILD.gn b/third_party/harfbuzz/BUILD.gn
index 173830d..4156607 100644
--- a/third_party/harfbuzz/BUILD.gn
+++ b/third_party/harfbuzz/BUILD.gn
@@ -14,6 +14,9 @@
   system("harfbuzz") {
     include_dirs = [ "/usr/include/harfbuzz" ]
     libs = [ "harfbuzz" ]
+    if (skia_pdf_subset_harfbuzz) {
+      libs += [ "harfbuzz-subset" ]
+    }
   }
 } else {
   third_party("harfbuzz") {
@@ -25,8 +28,8 @@
     defines = [
       #"HAVE_ICU",
       #"HAVE_ICU_BUILTIN",
-      "HAVE_INTEL_ATOMIC_PRIMITIVES",
       "HAVE_OT",
+      "HAVE_CONFIG_OVERRIDE_H",
     ]
     public = [
       "$_src/hb-blob.h",
@@ -181,9 +184,10 @@
       "$_src/hb-ot-shape-complex-myanmar-machine.hh",
       "$_src/hb-ot-shape-complex-myanmar.cc",
       "$_src/hb-ot-shape-complex-myanmar.hh",
+      "$_src/hb-ot-shape-complex-syllabic.cc",
+      "$_src/hb-ot-shape-complex-syllabic.hh",
       "$_src/hb-ot-shape-complex-thai.cc",
       "$_src/hb-ot-shape-complex-use-machine.hh",
-      "$_src/hb-ot-shape-complex-use-table.cc",
       "$_src/hb-ot-shape-complex-use.cc",
       "$_src/hb-ot-shape-complex-use.hh",
       "$_src/hb-ot-shape-complex-vowel-constraints.cc",
@@ -241,6 +245,7 @@
       "$_src/hb-utf.hh",
       "$_src/hb-vector.hh",
       "$_src/hb.hh",
+      "config-override.h",
     ]
   }
 }
diff --git a/third_party/harfbuzz/config-override.h b/third_party/harfbuzz/config-override.h
new file mode 100644
index 0000000..62a7af4
--- /dev/null
+++ b/third_party/harfbuzz/config-override.h
@@ -0,0 +1,10 @@
+/*
+ * Adds a mutex implementation based on c++ mutex to harfbuzz.
+ */
+#include <mutex>
+
+using hb_mutex_impl_t = std::mutex;
+#define hb_mutex_impl_init(M)   HB_STMT_START { new (M) hb_mutex_impl_t;  } HB_STMT_END
+#define hb_mutex_impl_lock(M)   (M)->lock ()
+#define hb_mutex_impl_unlock(M) (M)->unlock ()
+#define hb_mutex_impl_finish(M) HB_STMT_START { (M)->~hb_mutex_impl_t(); } HB_STMT_END