#ifndef SkColorTable_DEFINED
#define SkColorTable_DEFINED
+#include "../private/SkOncePtr.h"
#include "SkColor.h"
#include "SkFlattenable.h"
#include "SkImageInfo.h"
-#include "SkLazyPtr.h"
/** \class SkColorTable
static SkColorTable* Create(SkReadBuffer&);
private:
- static void Free16BitCache(uint16_t*);
-
enum AllocatedWithMalloc {
kAllocatedWithMalloc
};
// assumes ownership of colors (assumes it was allocated w/ malloc)
SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc);
+ struct Free16BitCache { void operator()(uint16_t* cache) const { sk_free(cache); } };
+
SkPMColor* fColors;
- SkLazyPtr<uint16_t, Free16BitCache> f16BitCache;
+ SkOncePtr<uint16_t, Free16BitCache> f16BitCache;
int fCount;
void init(const SkPMColor* colors, int count);
+++ /dev/null
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkLazyPtr_DEFINED
-#define SkLazyPtr_DEFINED
-
-/** Declare a lazily-chosen static pointer (or array of pointers) of type T.
- *
- * Example usage:
- *
- * Foo* GetSingletonFoo() {
- * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton); // Created with new, destroyed with delete.
- * return singleton.get();
- * }
- *
- * These macros take an optional T* (*Create)() and void (*Destroy)(T*) at the end.
- * If not given, we'll use new and delete.
- * These options are most useful when T doesn't have a public constructor or destructor.
- * Create comes first, so you may use a custom Create with a default Destroy, but not vice versa.
- *
- * Foo* CustomCreate() { return ...; }
- * void CustomDestroy(Foo* ptr) { ... }
- * Foo* GetSingletonFooWithCustomCleanup() {
- * SK_DECLARE_STATIC_LAZY_PTR(Foo, singleton, CustomCreate, CustomDestroy);
- * return singleton.get();
- * }
- *
- * If you have a bunch of related static pointers of the same type, you can
- * declare an array of lazy pointers together, and we'll pass the index to Create().
- *
- * Foo* CreateFoo(int i) { return ...; }
- * Foo* GetCachedFoo(Foo::Enum enumVal) {
- * SK_DECLARE_STATIC_LAZY_PTR_ARRAY(Foo, Foo::kEnumCount, cachedFoos, CreateFoo);
- * return cachedFoos[enumVal];
- * }
- *
- *
- * You can think of SK_DECLARE_STATIC_LAZY_PTR as a cheaper specialization of
- * SkOnce. There is no mutex or extra storage used past the pointer itself.
- *
- * We may call Create more than once, but all threads will see the same pointer
- * returned from get(). Any extra calls to Create will be cleaned up.
- *
- * These macros must be used in a global scope, not in function scope or as a class member.
- */
-
-#define SK_DECLARE_STATIC_LAZY_PTR(T, name, ...) \
- namespace {} static Private::SkStaticLazyPtr<T, ##__VA_ARGS__> name
-
-#define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \
- namespace {} static Private::SkStaticLazyPtrArray<T, N, ##__VA_ARGS__> name
-
-// namespace {} forces these macros to only be legal in global scopes. Chrome has thread-safety
-// problems with them in function-local statics because it uses -fno-threadsafe-statics, and even
-// in builds with threadsafe statics, those threadsafe statics are just unnecessary overhead.
-
-// Everything below here is private implementation details. Don't touch, don't even look.
-
-#include "SkAtomics.h"
-
-// See FIXME below.
-class SkFontConfigInterfaceDirect;
-
-namespace Private {
-
-// Set *dst to ptr if *dst is NULL. Returns value of *dst, destroying ptr if not swapped in.
-// Issues acquire memory barrier on failure, release on success.
-template <typename P, void (*Destroy)(P)>
-static P try_cas(P* dst, P ptr) {
- P prev = NULL;
- if (sk_atomic_compare_exchange(dst, &prev, ptr,
- sk_memory_order_release/*on success*/,
- sk_memory_order_acquire/*on failure*/)) {
- // We need a release barrier before returning ptr. The compare_exchange provides it.
- SkASSERT(!prev);
- return ptr;
- } else {
- Destroy(ptr);
- // We need an acquire barrier before returning prev. The compare_exchange provided it.
- SkASSERT(prev);
- return prev;
- }
-}
-
-template <typename T>
-T* sk_new() {
- return new T;
-}
-template <typename T>
-void sk_delete(T* ptr) {
- delete ptr;
-}
-
-// We're basing these implementations here on this article:
-// http://preshing.com/20140709/the-purpose-of-memory_order_consume-in-cpp11/
-//
-// Because the users of SkLazyPtr and SkLazyPtrArray will read the pointers
-// _through_ our atomically set pointer, there is a data dependency between our
-// atomic and the guarded data, and so we only need writer-releases /
-// reader-consumes memory pairing rather than the more general write-releases /
-// reader-acquires convention.
-//
-// This is nice, because a consume load is free on all our platforms: x86,
-// ARM, MIPS. In contrast, an acquire load issues a memory barrier on non-x86.
-
-template <typename T>
-T consume_load(T* ptr) {
-#if defined(THREAD_SANITIZER)
- // TSAN gets anxious if we don't tell it what we're actually doing, a consume load.
- return sk_atomic_load(ptr, sk_memory_order_consume);
-#else
- // All current compilers blindly upgrade consume memory order to acquire memory order.
- // For our purposes, though, no memory barrier is required, so we lie and use relaxed.
- return sk_atomic_load(ptr, sk_memory_order_relaxed);
-#endif
-}
-
-// This has no constructor and must be zero-initalized (the macro above does this).
-template <typename T, T* (*Create)() = sk_new<T>, void (*Destroy)(T*) = sk_delete<T> >
-class SkStaticLazyPtr {
-public:
- T* get() {
- // If fPtr has already been filled, we need a consume barrier when loading it.
- // If not, we need a release barrier when setting it. try_cas will do that.
- T* ptr = consume_load(&fPtr);
- return ptr ? ptr : try_cas<T*, Destroy>(&fPtr, Create());
- }
-
-private:
- T* fPtr;
-};
-
-template <typename T>
-T* sk_new_arg(int i) {
- return new T(i);
-}
-
-// This has no constructor and must be zero-initalized (the macro above does this).
-template <typename T, int N, T* (*Create)(int) = sk_new_arg<T>, void (*Destroy)(T*) = sk_delete<T> >
-class SkStaticLazyPtrArray {
-public:
- T* operator[](int i) {
- SkASSERT(i >= 0 && i < N);
- // If fPtr has already been filled, we need an consume barrier when loading it.
- // If not, we need a release barrier when setting it. try_cas will do that.
- T* ptr = consume_load(&fArray[i]);
- return ptr ? ptr : try_cas<T*, Destroy>(&fArray[i], Create(i));
- }
-
-private:
- T* fArray[N];
-};
-
-} // namespace Private
-
-// This version is suitable for use as a class member.
-// It's much the same as above except:
-// - it has a constructor to zero itself;
-// - it has a destructor to clean up;
-// - get() calls SkNew(T) to create the pointer;
-// - get(functor) calls functor to create the pointer.
-template <typename T, void (*Destroy)(T*) = Private::sk_delete<T> >
-class SkLazyPtr : SkNoncopyable {
-public:
- SkLazyPtr() : fPtr(NULL) {}
- ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } }
-
- T* get() const {
- T* ptr = Private::consume_load(&fPtr);
- return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, new T);
- }
-
- template <typename Create>
- T* get(const Create& create) const {
- T* ptr = Private::consume_load(&fPtr);
- return ptr ? ptr : Private::try_cas<T*, Destroy>(&fPtr, create());
- }
-
-private:
- mutable T* fPtr;
-};
-
-
-#endif//SkLazyPtr_DEFINED
#ifndef SkTypeface_DEFINED
#define SkTypeface_DEFINED
+#include "../private/SkOncePtr.h"
+#include "../private/SkWeakRefCnt.h"
#include "SkFontStyle.h"
-#include "SkLazyPtr.h"
#include "SkRect.h"
#include "SkString.h"
-#include "../private/SkWeakRefCnt.h"
class SkDescriptor;
class SkFontData;
static SkTypeface* CreateDefault(int style); // SkLazyPtr requires an int, not a Style.
static void DeleteDefault(SkTypeface*);
- struct BoundsComputer;
-// friend struct BoundsComputer;
-
- SkLazyPtr<SkRect> fLazyBounds;
+ SkOncePtr<SkRect> fLazyBounds;
SkFontID fUniqueID;
SkFontStyle fStyle;
bool fIsFixedPitch;
#define SkOncePtr_DEFINED
#include "SkAtomics.h"
+#include "SkUniquePtr.h"
-template <typename T> class SkStaticOnce;
+template <typename T> class SkBaseOncePtr;
// Use this to create a global static pointer that's intialized exactly once when you call get().
-#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkStaticOnce<type> name
+#define SK_DECLARE_STATIC_ONCE_PTR(type, name) namespace {} static SkBaseOncePtr<type> name
// Use this for a local or member pointer that's initialized exactly once when you call get().
-template <typename T>
+template <typename T, typename Delete = skstd::default_delete<T>>
class SkOncePtr : SkNoncopyable {
public:
SkOncePtr() { sk_bzero(this, sizeof(*this)); }
-
- // SkOncePtr does not have a destructor and does not clean up the pointer. But you may, e.g.
- // delete (T*)fOncePtr;
- // SkSafeUnref((T*)fOncePtr);
- // etc.
+ ~SkOncePtr() {
+ if (T* ptr = (T*)*this) {
+ Delete()(ptr);
+ }
+ }
template <typename F>
T* get(const F& f) const {
}
private:
- SkStaticOnce<T> fOnce;
+ SkBaseOncePtr<T> fOnce;
};
/* TODO(mtklein): in next CL
-typedef SkStaticOnce<void> SkOnceFlag;
+typedef SkBaseOncePtr<void> SkOnceFlag;
#define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name
template <typename F>
// Implementation details below here! No peeking!
template <typename T>
-class SkStaticOnce {
+class SkBaseOncePtr {
public:
template <typename F>
T* get(const F& f) const {
}
const SkBigPicture::Analysis& SkBigPicture::analysis() const {
- auto create = [&]() { return new Analysis(*fRecord); };
- return *fAnalysis.get(create);
+ return *fAnalysis.get([&]{ return new Analysis(*fRecord); });
}
SkRect SkBigPicture::cullRect() const { return fCullRect; }
#ifndef SkBigPicture_DEFINED
#define SkBigPicture_DEFINED
-#include "SkLazyPtr.h"
+#include "SkOncePtr.h"
#include "SkPicture.h"
#include "SkTemplates.h"
const SkRect fCullRect;
const size_t fApproxBytesUsedBySubPictures;
- SkLazyPtr<const Analysis> fAnalysis;
+ SkOncePtr<const Analysis> fAnalysis;
SkAutoTUnref<const SkRecord> fRecord;
SkAutoTDelete<const SnapshotArray> fDrawablePicts;
SkAutoTUnref<const SkBBoxHierarchy> fBBH;
#include "SkColorPriv.h"
-namespace {
-struct Build16BitCache {
- const SkPMColor* fColors;
- int fCount;
-
- uint16_t* operator()() const {
+const uint16_t* SkColorTable::read16BitCache() const {
+ return f16BitCache.get([&]{
uint16_t* cache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t));
for (int i = 0; i < fCount; i++) {
cache[i] = SkPixel32ToPixel16_ToU16(fColors[i]);
}
return cache;
- }
-};
-}//namespace
-
-void SkColorTable::Free16BitCache(uint16_t* cache) { sk_free(cache); }
-
-const uint16_t* SkColorTable::read16BitCache() const {
- const Build16BitCache create = { fColors, fCount };
- return f16BitCache.get(create);
+ });
}
///////////////////////////////////////////////////////////////////////////////
*/
#include "SkData.h"
-#include "SkLazyPtr.h"
#include "SkOSFile.h"
+#include "SkOncePtr.h"
#include "SkReadBuffer.h"
#include "SkStream.h"
#include "SkWriteBuffer.h"
///////////////////////////////////////////////////////////////////////////////
-// As a template argument these must have external linkage.
-SkData* sk_new_empty_data() { return new SkData(nullptr, 0, nullptr, nullptr); }
-namespace { void sk_unref_data(SkData* ptr) { return SkSafeUnref(ptr); } }
-
-SK_DECLARE_STATIC_LAZY_PTR(SkData, empty, sk_new_empty_data, sk_unref_data);
-
+SK_DECLARE_STATIC_ONCE_PTR(SkData, gEmpty);
SkData* SkData::NewEmpty() {
- return SkRef(empty.get());
+ return SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nullptr); }));
}
// assumes fPtr was allocated via sk_malloc
#include "SkFontDescriptor.h"
#include "SkFontMgr.h"
-#include "SkLazyPtr.h"
+#include "SkOncePtr.h"
#include "SkStream.h"
#include "SkTypes.h"
return this->onLegacyCreateTypeface(familyName, styleBits);
}
-// As a template argument this must have external linkage.
-SkFontMgr* sk_fontmgr_create_default() {
- SkFontMgr* fm = SkFontMgr::Factory();
- return fm ? fm : new SkEmptyFontMgr;
-}
-
-SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, sk_fontmgr_create_default);
-
+SK_DECLARE_STATIC_ONCE_PTR(SkFontMgr, singleton);
SkFontMgr* SkFontMgr::RefDefault() {
- return SkRef(singleton.get());
+ return SkRef(singleton.get([]{
+ SkFontMgr* fm = SkFontMgr::Factory();
+ return fm ? fm : new SkEmptyFontMgr;
+ }));
}
#include "SkGlyphCache.h"
#include "SkGlyphCache_Globals.h"
#include "SkGraphics.h"
-#include "SkLazyPtr.h"
+#include "SkOncePtr.h"
#include "SkPath.h"
#include "SkTemplates.h"
#include "SkTraceMemoryDump.h"
namespace {
-SkGlyphCache_Globals* create_globals() { return new SkGlyphCache_Globals; }
-
const char gGlyphCacheDumpName[] = "skia/sk_glyph_cache";
// Used to pass context to the sk_trace_dump_visitor.
} // namespace
-SK_DECLARE_STATIC_LAZY_PTR(SkGlyphCache_Globals, globals, create_globals);
-
// Returns the shared globals
+SK_DECLARE_STATIC_ONCE_PTR(SkGlyphCache_Globals, globals);
static SkGlyphCache_Globals& get_globals() {
- return *globals.get();
+ return *globals.get([]{ return new SkGlyphCache_Globals; });
}
///////////////////////////////////////////////////////////////////////////////
#include "SkBitmapDevice.h"
#include "SkChecksum.h"
#include "SkDevice.h"
-#include "SkLazyPtr.h"
#include "SkMatrixImageFilter.h"
#include "SkMutex.h"
+#include "SkOncePtr.h"
#include "SkReadBuffer.h"
#include "SkRect.h"
#include "SkTDynamicHash.h"
mutable SkMutex fMutex;
};
-SkImageFilter::Cache* CreateCache() {
- return SkImageFilter::Cache::Create(kDefaultCacheSize);
-}
-
} // namespace
SkImageFilter::Cache* SkImageFilter::Cache::Create(size_t maxBytes) {
return new CacheImpl(maxBytes);
}
-SK_DECLARE_STATIC_LAZY_PTR(SkImageFilter::Cache, cache, CreateCache);
-
+SK_DECLARE_STATIC_ONCE_PTR(SkImageFilter::Cache, cache);
SkImageFilter::Cache* SkImageFilter::Cache::Get() {
- return cache.get();
+ return cache.get([]{ return SkImageFilter::Cache::Create(kDefaultCacheSize); });
}
void SkImageFilter::PurgeCache() {
- cache.get()->purge();
+ Cache::Get()->purge();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef SkMessageBus_DEFINED
#define SkMessageBus_DEFINED
-#include "SkLazyPtr.h"
#include "SkMutex.h"
+#include "SkOncePtr.h"
#include "SkTArray.h"
#include "SkTDArray.h"
#include "SkTypes.h"
SkMessageBus();
static SkMessageBus* Get();
- // Allow SkLazyPtr to call SkMessageBus::SkMessageBus().
- template <typename T> friend T* Private::sk_new();
-
SkTDArray<Inbox*> fInboxes;
SkMutex fInboxesMutex;
};
// 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. NOTE: at most one per file will compile.
-#define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \
- SK_DECLARE_STATIC_LAZY_PTR(SkMessageBus<Message>, bus); \
- template <> \
- SkMessageBus<Message>* SkMessageBus<Message>::Get() { \
- return bus.get(); \
+#define DECLARE_SKMESSAGEBUS_MESSAGE(Message) \
+ SK_DECLARE_STATIC_ONCE_PTR(SkMessageBus<Message>, bus); \
+ template <> \
+ SkMessageBus<Message>* SkMessageBus<Message>::Get() { \
+ return bus.get([]{ return new SkMessageBus<Message>(); }); \
}
// ----------------------- Implementation of SkMessageBus::Inbox -----------------------
#include "SkCanvas.h"
#include "SkTLazy.h"
-#include "SkLazyPtr.h"
#include "SkMiniRecorder.h"
+#include "SkOncePtr.h"
#include "SkPicture.h"
#include "SkPictureCommon.h"
#include "SkRecordDraw.h"
int numSlowPaths() const override { return 0; }
bool willPlayBackBitmaps() const override { return false; }
};
-SK_DECLARE_STATIC_LAZY_PTR(SkEmptyPicture, gEmptyPicture);
+SK_DECLARE_STATIC_ONCE_PTR(SkEmptyPicture, gEmptyPicture);
template <typename T>
class SkMiniPicture final : public SkPicture {
return new SkMiniPicture<Type>(cull, reinterpret_cast<Type*>(fBuffer.get()))
switch (fState) {
- case State::kEmpty: return SkRef(gEmptyPicture.get());
+ case State::kEmpty: return SkRef(gEmptyPicture.get([]{ return new SkEmptyPicture; }));
CASE(DrawBitmapRectFixedSize);
CASE(DrawPath);
CASE(DrawRect);
*/
#include "SkBuffer.h"
-#include "SkLazyPtr.h"
+#include "SkOncePtr.h"
#include "SkPath.h"
#include "SkPathRef.h"
SkDEBUGCODE(fEditorsAttached = 0x7777777;)
}
-// As a template argument, this must have external linkage.
-SkPathRef* sk_create_empty_pathref() {
- SkPathRef* empty = new SkPathRef;
- empty->computeBounds(); // Avoids races later to be the first to do this.
- return empty;
-}
-
-SK_DECLARE_STATIC_LAZY_PTR(SkPathRef, empty, sk_create_empty_pathref);
-
+SK_DECLARE_STATIC_ONCE_PTR(SkPathRef, empty);
SkPathRef* SkPathRef::CreateEmpty() {
- return SkRef(empty.get());
+ return SkRef(empty.get([]{
+ SkPathRef* pr = new SkPathRef;
+ pr->computeBounds(); // Avoids races later to be the first to do this.
+ return pr;
+ }));
}
void SkPathRef::CreateTransformedCopy(SkAutoTUnref<SkPathRef>* dst,
}
void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) {
- if (nullptr == listener || this == empty.get()) {
+ if (nullptr == listener || this == (SkPathRef*)empty) {
delete listener;
return;
}
#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"
}
};
-namespace {
-
SK_DECLARE_STATIC_MUTEX(gCreateDefaultMutex);
-
-// 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);
+SK_DECLARE_STATIC_ONCE_PTR(SkTypeface, defaults[4]);
SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
SkASSERT((int)style < 4);
- return defaults[style];
+ 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();
+ });
}
SkTypeface* SkTypeface::RefDefault(Style style) {
#include "SkDescriptor.h"
#include "SkPaint.h"
-struct SkTypeface::BoundsComputer {
- const SkTypeface& fTypeface;
-
- BoundsComputer(const SkTypeface& tf) : fTypeface(tf) {}
-
- SkRect* operator()() const {
+SkRect SkTypeface::getBounds() const {
+ return *fLazyBounds.get([&] {
SkRect* rect = new SkRect;
- if (!fTypeface.onComputeBounds(rect)) {
+ if (!this->onComputeBounds(rect)) {
rect->setEmpty();
}
return rect;
- }
-};
-
-SkRect SkTypeface::getBounds() const {
- return *fLazyBounds.get(BoundsComputer(*this));
+ });
}
bool SkTypeface::onComputeBounds(SkRect* bounds) const {
#include "SkXfermode.h"
#include "SkXfermode_proccoeff.h"
#include "SkColorPriv.h"
-#include "SkLazyPtr.h"
#include "SkMathPriv.h"
+#include "SkOncePtr.h"
#include "SkOpts.h"
#include "SkReadBuffer.h"
#include "SkString.h"
#endif
-// Technically, can't be static and passed as a template parameter. So we use anonymous namespace.
-namespace {
-SkXfermode* create_mode(int iMode) {
- SkXfermode::Mode mode = (SkXfermode::Mode)iMode;
-
- ProcCoeff rec = gProcCoeffs[mode];
- if (auto xfermode = SkOpts::create_xfermode(rec, mode)) {
- return xfermode;
- }
- return new SkProcCoeffXfermode(rec, mode);
-}
-} // namespace
-
-SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkXfermode, cached, SkXfermode::kLastMode + 1, create_mode);
+SK_DECLARE_STATIC_ONCE_PTR(SkXfermode, cached[SkXfermode::kLastMode + 1]);
SkXfermode* SkXfermode::Create(Mode mode) {
SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount);
return nullptr;
}
- return SkSafeRef(cached[mode]);
+ return SkSafeRef(cached[mode].get([=]{
+ ProcCoeff rec = gProcCoeffs[mode];
+ if (auto xfermode = SkOpts::create_xfermode(rec, mode)) {
+ return xfermode;
+ }
+ return (SkXfermode*) new SkProcCoeffXfermode(rec, mode);
+ }));
}
SkXfermodeProc SkXfermode::GetProc(Mode mode) {
* found in the LICENSE file.
*/
+#include "SkOncePtr.h"
#include "SkRemotableFontMgr.h"
-#include "SkLazyPtr.h"
-
SkRemotableFontIdentitySet::SkRemotableFontIdentitySet(int count, SkFontIdentity** data)
: fCount(count), fData(count)
{
*data = fData;
}
-// As a template argument, this must have external linkage.
-SkRemotableFontIdentitySet* sk_remotable_font_identity_set_new() {
- return new SkRemotableFontIdentitySet;
-}
-
-SK_DECLARE_STATIC_LAZY_PTR(SkRemotableFontIdentitySet, empty, sk_remotable_font_identity_set_new);
+SK_DECLARE_STATIC_ONCE_PTR(SkRemotableFontIdentitySet, empty);
SkRemotableFontIdentitySet* SkRemotableFontIdentitySet::NewEmpty() {
- return SkRef(empty.get());
+ return SkRef(empty.get([]{ return new SkRemotableFontIdentitySet; }));
}
#include "SkDiscardableMemory.h"
#include "SkDiscardableMemoryPool.h"
#include "SkImageGenerator.h"
-#include "SkLazyPtr.h"
#include "SkMutex.h"
+#include "SkOncePtr.h"
#include "SkTInternalLList.h"
// Note:
this->dumpDownTo(0);
}
-////////////////////////////////////////////////////////////////////////////////
-SK_DECLARE_STATIC_MUTEX(gMutex);
-SkDiscardableMemoryPool* create_global_pool() {
- return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE,
- &gMutex);
-}
-
} // namespace
SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkBaseMutex* mutex) {
return new DiscardableMemoryPool(size, mutex);
}
-SK_DECLARE_STATIC_LAZY_PTR(SkDiscardableMemoryPool, global, create_global_pool);
+SK_DECLARE_STATIC_MUTEX(gMutex);
+SK_DECLARE_STATIC_ONCE_PTR(SkDiscardableMemoryPool, global);
SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() {
- return global.get();
+ return global.get([] {
+ return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE,
+ &gMutex);
+ });
}
-
-////////////////////////////////////////////////////////////////////////////////
#include "SkBlitRow.h"
#include "SkBlitRow_opts_SSE2.h"
#include "SkBlitRow_opts_SSE4.h"
-#include "SkLazyPtr.h"
+#include "SkOncePtr.h"
#include "SkRTConf.h"
#if defined(_MSC_VER) && defined(_WIN64)
/* Fetch the SIMD level directly from the CPU, at run-time.
* Only checks the levels needed by the optimizations in this file.
*/
-namespace { // get_SIMD_level() technically must have external linkage, so no static.
-int* get_SIMD_level() {
+static int* get_SIMD_level() {
int cpu_info[4] = { 0, 0, 0, 0 };
getcpuid(1, cpu_info);
}
return level;
}
-} // namespace
-SK_DECLARE_STATIC_LAZY_PTR(int, gSIMDLevel, get_SIMD_level);
+SK_DECLARE_STATIC_ONCE_PTR(int, gSIMDLevel);
/* Verify that the requested SIMD level is supported in the build.
* If not, check if the platform supports it.
*/
return false;
#else
- return minLevel <= *gSIMDLevel.get();
+ return minLevel <= *gSIMDLevel.get(get_SIMD_level);
#endif
}
}
*/
#include "SkData.h"
-#include "SkLazyPtr.h"
+#include "SkOncePtr.h"
#include "SkPDFCanon.h"
#include "SkPDFFormXObject.h"
#include "SkPDFGraphicState.h"
return pdfGraphicState;
}
-namespace {
-SkPDFObject* create_invert_function() {
+static SkPDFObject* create_invert_function() {
// Acrobat crashes if we use a type 0 function, kpdf crashes if we use
// a type 2 function, so we use a type 4 function.
SkAutoTUnref<SkPDFArray> domainAndRange(new SkPDFArray);
return invertFunction;
}
-template <typename T> void unref(T* ptr) { ptr->unref(); }
-} // namespace
-
-SK_DECLARE_STATIC_LAZY_PTR(SkPDFObject,
- invertFunction,
- create_invert_function,
- unref<SkPDFObject>);
+SK_DECLARE_STATIC_ONCE_PTR(SkPDFObject, invertFunction);
// static
SkPDFDict* SkPDFGraphicState::GetSMaskGraphicState(SkPDFFormXObject* sMask,
}
sMaskDict->insertObjRef("G", SkRef(sMask));
if (invert) {
- sMaskDict->insertObjRef("TR", SkRef(invertFunction.get()));
+ sMaskDict->insertObjRef("TR", SkRef(invertFunction.get(create_invert_function)));
}
SkPDFDict* result = new SkPDFDict("ExtGState");
return result;
}
-namespace {
-SkPDFDict* create_no_smask_graphic_state() {
+static SkPDFDict* create_no_smask_graphic_state() {
SkPDFDict* noSMaskGS = new SkPDFDict("ExtGState");
noSMaskGS->insertName("SMask", "None");
return noSMaskGS;
}
-} // namespace
-SK_DECLARE_STATIC_LAZY_PTR(SkPDFDict,
- noSMaskGraphicState,
- create_no_smask_graphic_state,
- unref<SkPDFDict>);
+SK_DECLARE_STATIC_ONCE_PTR(SkPDFDict, noSMaskGraphicState);
// static
SkPDFDict* SkPDFGraphicState::GetNoSMaskGraphicState() {
- return SkRef(noSMaskGraphicState.get());
+ return SkRef(noSMaskGraphicState.get(create_no_smask_graphic_state));
}
void SkPDFGraphicState::emitObject(
#include "SkPDFShader.h"
#include "SkData.h"
+#include "SkOncePtr.h"
#include "SkPDFCanon.h"
#include "SkPDFDevice.h"
#include "SkPDFFormXObject.h"
return true;
}
-namespace {
-SkPDFObject* create_range_object() {
+static SkPDFObject* create_range_object() {
SkPDFArray* range = new SkPDFArray;
range->reserve(6);
range->appendInt(0);
range->appendInt(1);
return range;
}
-
-template <typename T> void unref(T* ptr) { ptr->unref();}
-} // namespace
-
-SK_DECLARE_STATIC_LAZY_PTR(SkPDFObject, rangeObject,
- create_range_object, unref<SkPDFObject>);
+SK_DECLARE_STATIC_ONCE_PTR(SkPDFObject, rangeObject);
static SkPDFStream* make_ps_function(const SkString& psCode,
SkPDFArray* domain) {
SkPDFStream* result = new SkPDFStream(funcData.get());
result->insertInt("FunctionType", 4);
result->insertObject("Domain", SkRef(domain));
- result->insertObject("Range", SkRef(rangeObject.get()));
+ result->insertObject("Range", SkRef(rangeObject.get(create_range_object)));
return result;
}
#include "SkAtomics.h"
#include "SkEventTracer.h"
-#include "SkLazyPtr.h"
+#include "SkOncePtr.h"
#include <stdlib.h>
// We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTracer.
static SkEventTracer* gUserTracer = nullptr;
-SK_DECLARE_STATIC_LAZY_PTR(SkDefaultEventTracer, gDefaultTracer);
+SK_DECLARE_STATIC_ONCE_PTR(SkDefaultEventTracer, gDefaultTracer);
void SkEventTracer::SetInstance(SkEventTracer* tracer) {
SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire));
if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acquire)) {
return tracer;
}
- return gDefaultTracer.get();
+ return gDefaultTracer.get([]{ return new SkDefaultEventTracer; });
}
+++ /dev/null
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "Test.h"
-#include "SkLazyPtr.h"
-#include "SkRunnable.h"
-#include "SkTaskGroup.h"
-
-namespace {
-
-struct CreateIntFromFloat {
- CreateIntFromFloat(float val) : fVal(val) {}
- int* operator()() const { return new int((int)fVal); }
- float fVal;
-};
-
-// As a template argument this must have external linkage.
-void custom_destroy(int* ptr) { *ptr = 99; }
-
-} // namespace
-
-DEF_TEST(LazyPtr, r) {
- // Basic usage: calls new int.
- SkLazyPtr<int> lazy;
- int* ptr = lazy.get();
- REPORTER_ASSERT(r, ptr);
- REPORTER_ASSERT(r, lazy.get() == ptr);
-
- // Advanced usage: calls a functor.
- SkLazyPtr<int> lazyFunctor;
- int* six = lazyFunctor.get(CreateIntFromFloat(6.4f));
- REPORTER_ASSERT(r, six);
- REPORTER_ASSERT(r, 6 == *six);
-
- // Just makes sure this is safe.
- SkLazyPtr<double> neverRead;
-
- // SkLazyPtr supports custom destroy methods.
- {
- SkLazyPtr<int, custom_destroy> customDestroy;
- ptr = customDestroy.get();
- // custom_destroy called here.
- }
- REPORTER_ASSERT(r, ptr);
- REPORTER_ASSERT(r, 99 == *ptr);
- // Since custom_destroy didn't actually delete ptr, we do now.
- delete ptr;
-}
-
-DEF_TEST(LazyPtr_Threaded, r) {
- static const int kRacers = 321;
-
- // Race to intialize the pointer by calling .get().
- SkLazyPtr<int> lazy;
- int* seen[kRacers];
-
- sk_parallel_for(kRacers, [&](int i) {
- seen[i] = lazy.get();
- });
-
- // lazy.get() should return the same pointer to all threads.
- for (int i = 1; i < kRacers; i++) {
- REPORTER_ASSERT(r, seen[i] != nullptr);
- REPORTER_ASSERT(r, seen[i] == seen[0]);
- }
-}
REPORTER_ASSERT(r, *n == 5);
});
REPORTER_ASSERT(r, calls.load() == 1);
-
- delete (int*)once;
}
/* TODO(mtklein): next CL