Index: src/core/SkTypeface.cpp |
diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp |
index e6a9b4d66cd0f71988cb24e8eb840022ca9316d3..3ed2565ee23715cd1e16c75ad701a0e26c6fd3bc 100644 |
--- a/src/core/SkTypeface.cpp |
+++ b/src/core/SkTypeface.cpp |
@@ -9,9 +9,9 @@ |
#include "SkEndian.h" |
#include "SkFontDescriptor.h" |
#include "SkFontMgr.h" |
+#include "SkLazyPtr.h" |
#include "SkMutex.h" |
#include "SkOTTable_OS_2.h" |
-#include "SkOncePtr.h" |
#include "SkStream.h" |
#include "SkTypeface.h" |
@@ -73,22 +73,33 @@ |
} |
}; |
+namespace { |
+ |
SK_DECLARE_STATIC_MUTEX(gCreateDefaultMutex); |
-SK_DECLARE_STATIC_ONCE_PTR(SkTypeface, defaults[4]); |
+ |
+// As a template arguments, these must have external linkage. |
+SkTypeface* sk_create_default_typeface(int style) { |
+ // It is not safe to call FontConfigTypeface::LegacyCreateTypeface concurrently. |
+ // To be safe, we serialize here with a mutex so only one call to |
+ // CreateTypeface is happening at any given time. |
+ // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? |
+ SkAutoMutexAcquire lock(&gCreateDefaultMutex); |
+ |
+ SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
+ SkTypeface* t = fm->legacyCreateTypeface(nullptr, style); |
+ return t ? t : SkEmptyTypeface::Create(); |
+} |
+ |
+void sk_unref_typeface(SkTypeface* ptr) { SkSafeUnref(ptr); } |
+ |
+} // namespace |
+ |
+SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4, |
+ sk_create_default_typeface, sk_unref_typeface); |
SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { |
SkASSERT((int)style < 4); |
- return defaults[style].get([=]{ |
- // It is not safe to call FontConfigTypeface::LegacyCreateTypeface concurrently. |
- // To be safe, we serialize here with a mutex so only one call to |
- // CreateTypeface is happening at any given time. |
- // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? |
- SkAutoMutexAcquire lock(&gCreateDefaultMutex); |
- |
- SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
- SkTypeface* t = fm->legacyCreateTypeface(nullptr, style); |
- return t ? t : SkEmptyTypeface::Create(); |
- }); |
+ return defaults[style]; |
} |
SkTypeface* SkTypeface::RefDefault(Style style) { |
@@ -314,14 +325,22 @@ |
#include "SkDescriptor.h" |
#include "SkPaint.h" |
+struct SkTypeface::BoundsComputer { |
+ const SkTypeface& fTypeface; |
+ |
+ BoundsComputer(const SkTypeface& tf) : fTypeface(tf) {} |
+ |
+ SkRect* operator()() const { |
+ SkRect* rect = new SkRect; |
+ if (!fTypeface.onComputeBounds(rect)) { |
+ rect->setEmpty(); |
+ } |
+ return rect; |
+ } |
+}; |
+ |
SkRect SkTypeface::getBounds() const { |
- return *fLazyBounds.get([&] { |
- SkRect* rect = new SkRect; |
- if (!this->onComputeBounds(rect)) { |
- rect->setEmpty(); |
- } |
- return rect; |
- }); |
+ return *fLazyBounds.get(BoundsComputer(*this)); |
} |
bool SkTypeface::onComputeBounds(SkRect* bounds) const { |