Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Unified Diff: src/core/SkTypeface.cpp

Issue 304383005: Port most uses of SkOnce to SkLazyPtr. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add mutex Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkPathRef.cpp ('k') | src/fonts/SkRemotableFontMgr.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkTypeface.cpp
diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp
index cd3953ba98bdfda0b923ca88faafd136be0eb7a9..fd2803b20785106b7417c60af63e9b2a132ec19c 100644
--- a/src/core/SkTypeface.cpp
+++ b/src/core/SkTypeface.cpp
@@ -8,7 +8,7 @@
#include "SkAdvancedTypefaceMetrics.h"
#include "SkFontDescriptor.h"
#include "SkFontHost.h"
-#include "SkOnce.h"
+#include "SkLazyPtr.h"
#include "SkStream.h"
#include "SkTypeface.h"
@@ -74,34 +74,30 @@ protected:
}
};
-static SkTypeface* gDefaultTypefaces[] = { NULL, NULL, NULL, NULL };
-static const size_t FONT_STYLE_COUNT = SK_ARRAY_COUNT(gDefaultTypefaces);
-static SkOnceFlag gDefaultTypefaceOnce[FONT_STYLE_COUNT] = {
- SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT
-};
-template <uintmax_t N> struct SkTIsPow2 {
- static const bool value = (N & (N - 1)) == 0;
-};
-SK_COMPILE_ASSERT(SkTIsPow2<FONT_STYLE_COUNT>::value, FONT_STYLE_COUNT_not_power_of_2);
+SkTypeface* SkTypeface::CreateDefault(int style) {
+ // If backed by fontconfig, it's not safe to call SkFontHost::CreateTypeface 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?
+ SK_DECLARE_STATIC_MUTEX(mutex);
+ SkAutoMutexAcquire lock(&mutex);
-void SkTypeface::create_default_typeface(Style style) {
- if (NULL == gDefaultTypefaces[style]) {
- gDefaultTypefaces[style] = SkFontHost::CreateTypeface(NULL, NULL, style);
- }
- if (NULL == gDefaultTypefaces[style]) {
- // FIXME: Use a singleton for SkEmptyTypeface.
- gDefaultTypefaces[style] = SkEmptyTypeface::Create();
- }
+ SkTypeface* t = SkFontHost::CreateTypeface(NULL, NULL, (Style)style);
+ return t ? t : SkEmptyTypeface::Create();
}
-SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
- SkASSERT((size_t)style < FONT_STYLE_COUNT);
+void SkTypeface::DeleteDefault(SkTypeface* t) {
+ // The SkTypeface returned by SkFontHost::CreateTypeface may _itself_ be a
+ // cleverly-shared singleton. This is less than ideal. This means we
+ // cannot just assert our ownership and SkDELETE(t) like we'd want to.
+ SkSafeUnref(t);
+}
- // mask off any other bits to avoid a crash in SK_RELEASE
- style = (Style)(style & (FONT_STYLE_COUNT - 1));
+SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
+ SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4, CreateDefault, DeleteDefault);
- SkOnce(&gDefaultTypefaceOnce[style], SkTypeface::create_default_typeface, style);
- return gDefaultTypefaces[style];
+ SkASSERT((int)style < 4);
+ return defaults[style];
}
SkTypeface* SkTypeface::RefDefault(Style style) {
« no previous file with comments | « src/core/SkPathRef.cpp ('k') | src/fonts/SkRemotableFontMgr.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698