mtklein 2014-06-02 08:44:27 -07:00 committed by Commit bot
parent 309e869124
commit 78358bf624
15 changed files with 98 additions and 124 deletions

View File

@ -418,7 +418,7 @@ private:
/** /**
* Called the first time someone calls CreateEmpty to actually create the singleton. * Called the first time someone calls CreateEmpty to actually create the singleton.
*/ */
static void CreateEmptyImpl(int/*unused*/); static SkPathRef* CreateEmptyImpl();
void setIsOval(bool isOval) { fIsOval = isOval; } void setIsOval(bool isOval) { fIsOval = isOval; }

View File

@ -339,7 +339,8 @@ private:
uint32_t glyphIDsCount = 0) const; uint32_t glyphIDsCount = 0) const;
private: private:
static void create_default_typeface(Style style); static SkTypeface* CreateDefault(int style); // SkLazyPtr requires an int, not a Style.
static void DeleteDefault(SkTypeface*);
SkFontID fUniqueID; SkFontID fUniqueID;
Style fStyle; Style fStyle;

View File

@ -131,7 +131,7 @@ protected:
unsigned styleBits) const = 0; unsigned styleBits) const = 0;
private: private:
static SkFontMgr* Factory(); // implemented by porting layer static SkFontMgr* Factory(); // implemented by porting layer
friend void set_up_default(SkFontMgr** singleton); static SkFontMgr* CreateDefault();
typedef SkRefCnt INHERITED; typedef SkRefCnt INHERITED;
}; };

View File

@ -11,7 +11,6 @@
#include "SkDataTable.h" #include "SkDataTable.h"
#include "SkFontMgr.h" #include "SkFontMgr.h"
#include "SkFontStyle.h" #include "SkFontStyle.h"
#include "SkOnce.h"
#include "SkRemotableFontMgr.h" #include "SkRemotableFontMgr.h"
#include "SkTArray.h" #include "SkTArray.h"
#include "SkTypeface.h" #include "SkTypeface.h"

View File

@ -46,7 +46,7 @@ public:
private: private:
SkRemotableFontIdentitySet() : fCount(0), fData() { } SkRemotableFontIdentitySet() : fCount(0), fData() { }
static void NewEmptyImpl(int); static SkRemotableFontIdentitySet* NewEmptyImpl();
int fCount; int fCount;
SkAutoTMalloc<SkFontIdentity> fData; SkAutoTMalloc<SkFontIdentity> fData;

View File

@ -6,7 +6,7 @@
*/ */
#include "SkFontLCDConfig.h" #include "SkFontLCDConfig.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation; static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder; static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
@ -198,19 +198,14 @@ SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
return this->onLegacyCreateTypeface(familyName, styleBits); return this->onLegacyCreateTypeface(familyName, styleBits);
} }
void set_up_default(SkFontMgr** singleton) { SkFontMgr* SkFontMgr::CreateDefault() {
*singleton = SkFontMgr::Factory(); SkFontMgr* fm = SkFontMgr::Factory();
// we never want to return NULL return fm ? fm : SkNEW(SkEmptyFontMgr);
if (NULL == *singleton) {
*singleton = SkNEW(SkEmptyFontMgr);
}
} }
SkFontMgr* SkFontMgr::RefDefault() { SkFontMgr* SkFontMgr::RefDefault() {
static SkFontMgr* gFM = NULL; SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, CreateDefault);
SK_DECLARE_STATIC_ONCE(once); return SkRef(singleton.get());
SkOnce(&once, set_up_default, &gFM);
return SkRef(gFM);
} }
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////

View File

@ -11,7 +11,7 @@
#include "SkGlyphCache_Globals.h" #include "SkGlyphCache_Globals.h"
#include "SkDistanceFieldGen.h" #include "SkDistanceFieldGen.h"
#include "SkGraphics.h" #include "SkGraphics.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
#include "SkPaint.h" #include "SkPaint.h"
#include "SkPath.h" #include "SkPath.h"
#include "SkTemplates.h" #include "SkTemplates.h"
@ -21,18 +21,18 @@
//#define SPEW_PURGE_STATUS //#define SPEW_PURGE_STATUS
//#define RECORD_HASH_EFFICIENCY //#define RECORD_HASH_EFFICIENCY
static void create_globals(SkGlyphCache_Globals** globals) { namespace {
*globals = SkNEW_ARGS(SkGlyphCache_Globals, (SkGlyphCache_Globals::kYes_UseMutex));
SkGlyphCache_Globals* create_globals() {
return SkNEW_ARGS(SkGlyphCache_Globals, (SkGlyphCache_Globals::kYes_UseMutex));
} }
} // namespace
// Returns the shared globals // Returns the shared globals
static SkGlyphCache_Globals& getSharedGlobals() { static SkGlyphCache_Globals& getSharedGlobals() {
// we leak this, so we don't incur any shutdown cost of the destructor SK_DECLARE_STATIC_LAZY_PTR(SkGlyphCache_Globals, globals, create_globals);
static SkGlyphCache_Globals* gGlobals = NULL; return *globals.get();
SK_DECLARE_STATIC_ONCE(once);
SkOnce(&once, create_globals, &gGlobals);
SkASSERT(NULL != gGlobals);
return *gGlobals;
} }
// Returns the TLS globals (if set), or the shared globals // Returns the TLS globals (if set), or the shared globals

View File

@ -64,7 +64,6 @@
// See FIXME below. // See FIXME below.
class SkFontConfigInterface; class SkFontConfigInterface;
class SkTypeface;
namespace Private { namespace Private {
@ -100,7 +99,6 @@ public:
#ifdef SK_DEVELOPER #ifdef SK_DEVELOPER
// FIXME: We know we leak refs on some classes. For now, let them leak. // FIXME: We know we leak refs on some classes. For now, let them leak.
void cleanup(SkFontConfigInterface*) {} void cleanup(SkFontConfigInterface*) {}
void cleanup(SkTypeface*) {}
template <typename U> void cleanup(U* ptr) { Destroy(ptr); } template <typename U> void cleanup(U* ptr) { Destroy(ptr); }
~SkLazyPtr() { ~SkLazyPtr() {

View File

@ -7,7 +7,7 @@
#include "SkMatrix.h" #include "SkMatrix.h"
#include "SkFloatBits.h" #include "SkFloatBits.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
#include "SkString.h" #include "SkString.h"
// In a few places, we performed the following // In a few places, we performed the following
@ -1558,29 +1558,33 @@ bool SkMatrix::getMinMaxScales(SkScalar scaleFactors[2]) const {
return get_scale_factor<kBoth_MinMaxOrBoth>(this->getType(), fMat, scaleFactors); return get_scale_factor<kBoth_MinMaxOrBoth>(this->getType(), fMat, scaleFactors);
} }
static void reset_identity_matrix(SkMatrix* identity) { namespace {
identity->reset();
SkMatrix* create_identity() {
SkMatrix* m = SkNEW(SkMatrix);
m->reset();
return m;
} }
SkMatrix* create_invalid() {
SkMatrix* m = SkNEW(SkMatrix);
m->setAll(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax,
SK_ScalarMax, SK_ScalarMax, SK_ScalarMax,
SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
m->getType(); // Force the type to be computed.
return m;
}
} // namespace
const SkMatrix& SkMatrix::I() { const SkMatrix& SkMatrix::I() {
// If you can use C++11 now, you might consider replacing this with a constexpr constructor. SK_DECLARE_STATIC_LAZY_PTR(SkMatrix, identity, create_identity);
static SkMatrix gIdentity; return *identity.get();
SK_DECLARE_STATIC_ONCE(once);
SkOnce(&once, reset_identity_matrix, &gIdentity);
return gIdentity;
} }
const SkMatrix& SkMatrix::InvalidMatrix() { const SkMatrix& SkMatrix::InvalidMatrix() {
static SkMatrix gInvalid; SK_DECLARE_STATIC_LAZY_PTR(SkMatrix, invalid, create_invalid);
static bool gOnce; return *invalid.get();
if (!gOnce) {
gInvalid.setAll(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax,
SK_ScalarMax, SK_ScalarMax, SK_ScalarMax,
SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
gInvalid.getType(); // force the type to be computed
gOnce = true;
}
return gInvalid;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -8,7 +8,7 @@
#ifndef SkMessageBus_DEFINED #ifndef SkMessageBus_DEFINED
#define SkMessageBus_DEFINED #define SkMessageBus_DEFINED
#include "SkOnce.h" #include "SkLazyPtr.h"
#include "SkTDArray.h" #include "SkTDArray.h"
#include "SkThread.h" #include "SkThread.h"
#include "SkTypes.h" #include "SkTypes.h"
@ -38,7 +38,7 @@ public:
private: private:
SkMessageBus(); SkMessageBus();
static SkMessageBus* Get(); static SkMessageBus* Get();
static void New(SkMessageBus**); static SkMessageBus* New();
SkTDArray<Inbox*> fInboxes; SkTDArray<Inbox*> fInboxes;
SkMutex fInboxesMutex; SkMutex fInboxesMutex;
@ -46,14 +46,11 @@ private:
// This must go in a single .cpp file, not some .h, or we risk creating more than one global // This must go in a single .cpp file, not some .h, or we risk creating more than one global
// SkMessageBus per type when using shared libraries. // SkMessageBus per type when using shared libraries.
#define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \ #define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \
template <> \ template <> \
SkMessageBus<Message>* SkMessageBus<Message>::Get() { \ SkMessageBus<Message>* SkMessageBus<Message>::Get() { \
static SkMessageBus<Message>* bus = NULL; \ SK_DECLARE_STATIC_LAZY_PTR(SkMessageBus<Message>, bus, New); \
SK_DECLARE_STATIC_ONCE(once); \ return bus.get(); \
SkOnce(&once, &New, &bus); \
SkASSERT(bus != NULL); \
return bus; \
} }
// ----------------------- Implementation of SkMessageBus::Inbox ----------------------- // ----------------------- Implementation of SkMessageBus::Inbox -----------------------
@ -100,8 +97,8 @@ template <typename Message>
SkMessageBus<Message>::SkMessageBus() {} SkMessageBus<Message>::SkMessageBus() {}
template <typename Message> template <typename Message>
/*static*/ void SkMessageBus<Message>::New(SkMessageBus<Message>** bus) { /*static*/ SkMessageBus<Message>* SkMessageBus<Message>::New() {
*bus = new SkMessageBus<Message>(); return SkNEW(SkMessageBus<Message>);
} }
template <typename Message> template <typename Message>

View File

@ -6,7 +6,7 @@
*/ */
#include "SkBuffer.h" #include "SkBuffer.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
#include "SkPath.h" #include "SkPath.h"
#include "SkPathRef.h" #include "SkPathRef.h"
@ -28,18 +28,16 @@ SkPathRef::Editor::Editor(SkAutoTUnref<SkPathRef>* pathRef,
} }
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
static SkPathRef* gEmptyPathRef = NULL;
static void cleanup_gEmptyPathRef() { gEmptyPathRef->unref(); }
void SkPathRef::CreateEmptyImpl(int) { SkPathRef* SkPathRef::CreateEmptyImpl() {
gEmptyPathRef = SkNEW(SkPathRef); SkPathRef* p = SkNEW(SkPathRef);
gEmptyPathRef->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty. p->computeBounds(); // Preemptively avoid a race to clear fBoundsIsDirty.
return p;
} }
SkPathRef* SkPathRef::CreateEmpty() { SkPathRef* SkPathRef::CreateEmpty() {
SK_DECLARE_STATIC_ONCE(once); SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, CreateEmptyImpl);
SkOnce(&once, SkPathRef::CreateEmptyImpl, 0, cleanup_gEmptyPathRef); return SkRef(empty.get());
return SkRef(gEmptyPathRef);
} }
void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst, void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,

View File

@ -8,7 +8,7 @@
#include "SkAdvancedTypefaceMetrics.h" #include "SkAdvancedTypefaceMetrics.h"
#include "SkFontDescriptor.h" #include "SkFontDescriptor.h"
#include "SkFontHost.h" #include "SkFontHost.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
#include "SkStream.h" #include "SkStream.h"
#include "SkTypeface.h" #include "SkTypeface.h"
@ -74,34 +74,30 @@ protected:
} }
}; };
static SkTypeface* gDefaultTypefaces[] = { NULL, NULL, NULL, NULL }; SkTypeface* SkTypeface::CreateDefault(int style) {
static const size_t FONT_STYLE_COUNT = SK_ARRAY_COUNT(gDefaultTypefaces); // If backed by fontconfig, it's not safe to call SkFontHost::CreateTypeface concurrently.
static SkOnceFlag gDefaultTypefaceOnce[FONT_STYLE_COUNT] = { // To be safe, we serialize here with a mutex so only one call to
SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT // CreateTypeface is happening at any given time.
}; // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe?
template <uintmax_t N> struct SkTIsPow2 { SK_DECLARE_STATIC_MUTEX(mutex);
static const bool value = (N & (N - 1)) == 0; SkAutoMutexAcquire lock(&mutex);
};
SK_COMPILE_ASSERT(SkTIsPow2<FONT_STYLE_COUNT>::value, FONT_STYLE_COUNT_not_power_of_2);
void SkTypeface::create_default_typeface(Style style) { SkTypeface* t = SkFontHost::CreateTypeface(NULL, NULL, (Style)style);
if (NULL == gDefaultTypefaces[style]) { return t ? t : SkEmptyTypeface::Create();
gDefaultTypefaces[style] = SkFontHost::CreateTypeface(NULL, NULL, style); }
}
if (NULL == gDefaultTypefaces[style]) { void SkTypeface::DeleteDefault(SkTypeface* t) {
// FIXME: Use a singleton for SkEmptyTypeface. // The SkTypeface returned by SkFontHost::CreateTypeface may _itself_ be a
gDefaultTypefaces[style] = SkEmptyTypeface::Create(); // 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);
} }
SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
SkASSERT((size_t)style < FONT_STYLE_COUNT); SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4, CreateDefault, DeleteDefault);
// mask off any other bits to avoid a crash in SK_RELEASE SkASSERT((int)style < 4);
style = (Style)(style & (FONT_STYLE_COUNT - 1)); return defaults[style];
SkOnce(&gDefaultTypefaceOnce[style], SkTypeface::create_default_typeface, style);
return gDefaultTypefaces[style];
} }
SkTypeface* SkTypeface::RefDefault(Style style) { SkTypeface* SkTypeface::RefDefault(Style style) {

View File

@ -7,7 +7,7 @@
#include "SkRemotableFontMgr.h" #include "SkRemotableFontMgr.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
SkRemotableFontIdentitySet::SkRemotableFontIdentitySet(int count, SkFontIdentity** data) SkRemotableFontIdentitySet::SkRemotableFontIdentitySet(int count, SkFontIdentity** data)
: fCount(count), fData(count) : fCount(count), fData(count)
@ -16,17 +16,11 @@ SkRemotableFontIdentitySet::SkRemotableFontIdentitySet(int count, SkFontIdentity
*data = fData; *data = fData;
} }
static SkRemotableFontIdentitySet* gEmptyRemotableFontIdentitySet = NULL; SkRemotableFontIdentitySet* SkRemotableFontIdentitySet::NewEmptyImpl() {
static void cleanup_gEmptyRemotableFontIdentitySet() { gEmptyRemotableFontIdentitySet->unref(); } return SkNEW(SkRemotableFontIdentitySet);
void SkRemotableFontIdentitySet::NewEmptyImpl(int) {
gEmptyRemotableFontIdentitySet = new SkRemotableFontIdentitySet();
} }
SkRemotableFontIdentitySet* SkRemotableFontIdentitySet::NewEmpty() { SkRemotableFontIdentitySet* SkRemotableFontIdentitySet::NewEmpty() {
SK_DECLARE_STATIC_ONCE(once); SK_DECLARE_STATIC_LAZY_PTR(SkRemotableFontIdentitySet, empty, NewEmptyImpl);
SkOnce(&once, SkRemotableFontIdentitySet::NewEmptyImpl, 0, return SkRef(empty.get());
cleanup_gEmptyRemotableFontIdentitySet);
gEmptyRemotableFontIdentitySet->ref();
return gEmptyRemotableFontIdentitySet;
} }

View File

@ -7,7 +7,7 @@
#include "SkDiscardableMemory.h" #include "SkDiscardableMemory.h"
#include "SkDiscardableMemoryPool.h" #include "SkDiscardableMemoryPool.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
#include "SkTInternalLList.h" #include "SkTInternalLList.h"
#include "SkThread.h" #include "SkThread.h"
@ -248,27 +248,20 @@ void DiscardableMemoryPool::dumpPool() {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
SK_DECLARE_STATIC_MUTEX(gMutex); SK_DECLARE_STATIC_MUTEX(gMutex);
SkDiscardableMemoryPool* gPool = NULL; SkDiscardableMemoryPool* create_global_pool() {
void create_global_pool(int) { return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE,
SkASSERT(NULL == gPool); &gMutex);
gPool = SkDiscardableMemoryPool::Create(
SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE, &gMutex);
}
void cleanup_global_pool() {
gPool->unref();
} }
} // namespace } // namespace
SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create( SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkBaseMutex* mutex) {
size_t size, SkBaseMutex* mutex) {
return SkNEW_ARGS(DiscardableMemoryPool, (size, mutex)); return SkNEW_ARGS(DiscardableMemoryPool, (size, mutex));
} }
SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() { SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() {
SK_DECLARE_STATIC_ONCE(create_pool_once); SK_DECLARE_STATIC_LAZY_PTR(SkDiscardableMemoryPool, global, create_global_pool);
SkOnce(&create_pool_once, create_global_pool, 0, &cleanup_global_pool); return global.get();
SkASSERT(NULL != gPool);
return gPool;
} }
// defined in SkImageGenerator.h // defined in SkImageGenerator.h

View File

@ -15,7 +15,7 @@
#include "SkBuffer.h" #include "SkBuffer.h"
#include "SkFontConfigInterface.h" #include "SkFontConfigInterface.h"
#include "SkOnce.h" #include "SkLazyPtr.h"
#include "SkStream.h" #include "SkStream.h"
size_t SkFontConfigInterface::FontIdentity::writeToMemory(void* addr) const { size_t SkFontConfigInterface::FontIdentity::writeToMemory(void* addr) const {
@ -124,14 +124,13 @@ private:
SkMutex mutex_; SkMutex mutex_;
}; };
static void create_singleton_direct_interface(SkFontConfigInterface** singleton) { namespace {
*singleton = new SkFontConfigInterfaceDirect; SkFontConfigInterface* create_direct() { return SkNEW(SkFontConfigInterfaceDirect); }
} } // namespace
SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() { SkFontConfigInterface* SkFontConfigInterface::GetSingletonDirectInterface() {
static SkFontConfigInterface* gDirect; SK_DECLARE_STATIC_LAZY_PTR(SkFontConfigInterface, direct, create_direct);
SK_DECLARE_STATIC_ONCE(once); return direct.get();
SkOnce(&once, create_singleton_direct_interface, &gDirect);
return gDirect;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////