Revert of SkOncePtr -> SkOnce (patchset #1 id:1 of https://codereview.chromium.org/1949313003/ )
Reason for revert:
ok, I have no idea what's going on
Original issue's description:
> Reland of SkOncePtr -> SkOnce (patchset #1 id:1 of https://codereview.chromium.org/1945293004/ )
>
> Reason for revert:
> previous revert unneeded (I think)
>
> Original issue's description:
> > Revert of SkOncePtr -> SkOnce (patchset #4 id:60001 of https://codereview.chromium.org/1953533002/ )
> >
> > Reason for revert:
> > broken the Mac and Linux builders, e.g.:
> >
> > https://build.chromium.org/p/chromium/builders/Mac/builds/15151
> > https://build.chromium.org/p/chromium/builders/Linux%20x64/builds/19052
> >
> > Original issue's description:
> > > SkOncePtr -> SkOnce
> > >
> > > It's always nice to kill off a synchronization primitive.
> > > And while less terse, I think this new code reads more clearly.
> > >
> > > ... and, SkOncePtr's tests were the only thing now using sk_num_cores()
> > > outside of SkTaskGroup, so I've hidden it as static inside SkTaskGroup.cpp.
> > >
> > > BUG=skia:
> > > GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1953533002
> > > CQ_EXTRA_TRYBOTS=client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-SKNX_NO_SIMD-Trybot
> > >
> > > Committed: https://skia.googlesource.com/skia/+/9bd3fc7fa9bb7cad050bd619aa93d4c48ebb5c02
> >
> > TBR=herb@google.com,mtklein@chromium.org
> > # Skipping CQ checks because original CL landed less than 1 days ago.
> > NOPRESUBMIT=true
> > NOTREECHECKS=true
> > NOTRY=true
> > BUG=skia:
> >
> > Committed: https://skia.googlesource.com/skia/+/7eb33da7eede34c050b865dbb1b60c3dcea7191b
>
> TBR=herb@google.com,mtklein@chromium.org
> # Skipping CQ checks because original CL landed less than 1 days ago.
> NOPRESUBMIT=true
> NOTREECHECKS=true
> NOTRY=true
> BUG=skia:
>
> Committed: https://skia.googlesource.com/skia/+/82da9a8aa0ce648f36882830765b42e0ada6c0fa
TBR=herb@google.com,mtklein@chromium.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:
Review-Url: https://codereview.chromium.org/1948313002
diff --git a/gyp/core.gypi b/gyp/core.gypi
index aeffa2b..6322c54 100644
--- a/gyp/core.gypi
+++ b/gyp/core.gypi
@@ -422,6 +422,7 @@
'<(skia_include_path)/private/SkMiniRecorder.h',
'<(skia_include_path)/private/SkMutex.h',
'<(skia_include_path)/private/SkOnce.h',
+ '<(skia_include_path)/private/SkOncePtr.h',
'<(skia_include_path)/private/SkRecords.h',
'<(skia_include_path)/private/SkSemaphore.h',
'<(skia_include_path)/private/SkSpinlock.h',
diff --git a/include/private/SkOncePtr.h b/include/private/SkOncePtr.h
new file mode 100644
index 0000000..921c6a6
--- /dev/null
+++ b/include/private/SkOncePtr.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkOncePtr_DEFINED
+#define SkOncePtr_DEFINED
+
+#include "../private/SkAtomics.h"
+#include <memory>
+
+// 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 SkBaseOncePtr<type> name;
+
+template <typename T>
+class SkBaseOncePtr {
+public:
+ template <typename F>
+ T* get(const F& f) const {
+ uintptr_t state = sk_atomic_load(&fState, sk_memory_order_acquire);
+ if (state < 2) {
+ if (state == 0) {
+ // It looks like no one has tried to create our pointer yet.
+ // We try to claim that task by atomically swapping our state from '0' to '1'.
+ // See SkOnce.h for why we use an acquire memory order here rather than relaxed.
+ if (sk_atomic_compare_exchange(
+ &fState, &state, (uintptr_t)1, sk_memory_order_acquire, sk_memory_order_acquire)) {
+ // We've claimed it. Create our pointer and store it into fState.
+ state = (uintptr_t)f();
+ SkASSERT(state > 1);
+ sk_atomic_store(&fState, state, sk_memory_order_release);
+ } else {
+ // Someone else claimed it.
+ // We fall through to the spin loop just below to wait for them to finish.
+ }
+ }
+
+ while (state == 1) {
+ // State '1' is our busy-but-not-done state.
+ // Some other thread has claimed the job of creating our pointer.
+ // We just need to wait for it to finish.
+ state = sk_atomic_load(&fState, sk_memory_order_acquire);
+ }
+
+ // We shouldn't be able to get here without having created our pointer.
+ SkASSERT(state > 1);
+ }
+ return (T*)state;
+ }
+
+ operator T*() const {
+ auto state = sk_atomic_load(&fState, sk_memory_order_acquire);
+ return state < 2 ? nullptr : (T*)state;
+ // TODO: If state == 1 spin until it's not?
+ }
+
+ // fState == 0 --> we have not created our ptr yet
+ // fState == 1 --> someone is in the middle of creating our ptr
+ // else --> (T*)fState is our ptr
+ mutable uintptr_t fState;
+};
+
+#endif//SkOncePtr_DEFINED
diff --git a/src/core/SkColorSpace.cpp b/src/core/SkColorSpace.cpp
index 889746f..55ab4c1 100644
--- a/src/core/SkColorSpace.cpp
+++ b/src/core/SkColorSpace.cpp
@@ -7,7 +7,7 @@
#include "SkAtomics.h"
#include "SkColorSpace.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
static bool color_space_almost_equal(float a, float b) {
return SkTAbs(a - b) < 0.01f;
@@ -43,6 +43,8 @@
0.1430f, 0.0606f, 0.7139f, // * B
};
+SK_DECLARE_STATIC_ONCE_PTR(SkColorSpace, sRGB);
+
sk_sp<SkColorSpace> SkColorSpace::NewRGB(SkGammas gammas, const SkMatrix44& toXYZD50) {
// Check if we really have sRGB
if (color_space_almost_equal(2.2f, gammas.fRed.fValue) &&
@@ -72,17 +74,13 @@
}
sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) {
- static SkOnce once;
- static SkColorSpace* sRGB;
-
switch (named) {
case kSRGB_Named: {
- once([] {
- SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor);
- srgbToxyzD50.set3x3ColMajorf(gSRGB_toXYZD50);
- sRGB = new SkColorSpace(SkGammas(2.2f, 2.2f, 2.2f), srgbToxyzD50, kSRGB_Named);
- });
- return sk_ref_sp(sRGB);
+ SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor);
+ srgbToxyzD50.set3x3ColMajorf(gSRGB_toXYZD50);
+ return sk_ref_sp(sRGB.get([=]{
+ return new SkColorSpace(SkGammas(2.2f, 2.2f, 2.2f), srgbToxyzD50, kSRGB_Named);
+ }));
}
default:
break;
diff --git a/src/core/SkData.cpp b/src/core/SkData.cpp
index ea2da3d..995a30a 100644
--- a/src/core/SkData.cpp
+++ b/src/core/SkData.cpp
@@ -7,7 +7,7 @@
#include "SkData.h"
#include "SkOSFile.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkReadBuffer.h"
#include "SkStream.h"
#include "SkWriteBuffer.h"
@@ -80,12 +80,10 @@
///////////////////////////////////////////////////////////////////////////////
+SK_DECLARE_STATIC_ONCE_PTR(SkData, gEmpty);
sk_sp<SkData> SkData::MakeEmpty() {
- static SkOnce once;
- static SkData* empty;
-
- once([]{ empty = new SkData(nullptr, 0, nullptr, nullptr); });
- return sk_ref_sp(empty);
+ SkData* data = SkRef(gEmpty.get([]{return new SkData(nullptr, 0, nullptr, nullptr); }));
+ return sk_sp<SkData>(data);
}
// assumes fPtr was allocated via sk_malloc
diff --git a/src/core/SkFontMgr.cpp b/src/core/SkFontMgr.cpp
index 0f00667..ebb9a89 100644
--- a/src/core/SkFontMgr.cpp
+++ b/src/core/SkFontMgr.cpp
@@ -7,7 +7,7 @@
#include "SkFontDescriptor.h"
#include "SkFontMgr.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkStream.h"
#include "SkTypes.h"
@@ -169,15 +169,12 @@
return this->onLegacyCreateTypeface(familyName, style);
}
+SK_DECLARE_STATIC_ONCE_PTR(SkFontMgr, singleton);
SkFontMgr* SkFontMgr::RefDefault() {
- static SkOnce once;
- static SkFontMgr* singleton;
-
- once([]{
+ return SkRef(singleton.get([]{
SkFontMgr* fm = SkFontMgr::Factory();
- singleton = fm ? fm : new SkEmptyFontMgr;
- });
- return SkRef(singleton);
+ return fm ? fm : new SkEmptyFontMgr;
+ }));
}
/**
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index 7256de6..309f494 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -8,7 +8,7 @@
#include "SkGlyphCache.h"
#include "SkGlyphCache_Globals.h"
#include "SkGraphics.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkPath.h"
#include "SkTemplates.h"
#include "SkTraceMemoryDump.h"
@@ -23,12 +23,9 @@
} // namespace
// Returns the shared globals
+SK_DECLARE_STATIC_ONCE_PTR(SkGlyphCache_Globals, globals);
static SkGlyphCache_Globals& get_globals() {
- static SkOnce once;
- static SkGlyphCache_Globals* globals;
-
- once([]{ globals = new SkGlyphCache_Globals; });
- return *globals;
+ return *globals.get([]{ return new SkGlyphCache_Globals; });
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/src/core/SkImageFilterCache.cpp b/src/core/SkImageFilterCache.cpp
index ba8a32c..d87cf6b 100644
--- a/src/core/SkImageFilterCache.cpp
+++ b/src/core/SkImageFilterCache.cpp
@@ -9,7 +9,7 @@
#include "SkChecksum.h"
#include "SkMutex.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkRefCnt.h"
#include "SkSpecialImage.h"
#include "SkTDynamicHash.h"
@@ -125,10 +125,7 @@
return new CacheImpl(maxBytes);
}
+SK_DECLARE_STATIC_ONCE_PTR(SkImageFilterCache, cache);
SkImageFilterCache* SkImageFilterCache::Get() {
- static SkOnce once;
- static SkImageFilterCache* cache;
-
- once([]{ cache = SkImageFilterCache::Create(kDefaultCacheSize); });
- return cache;
+ return cache.get([]{ return SkImageFilterCache::Create(kDefaultCacheSize); });
}
diff --git a/src/core/SkMessageBus.h b/src/core/SkMessageBus.h
index 79f5c02..a2802f0 100644
--- a/src/core/SkMessageBus.h
+++ b/src/core/SkMessageBus.h
@@ -9,7 +9,7 @@
#define SkMessageBus_DEFINED
#include "SkMutex.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkTArray.h"
#include "SkTDArray.h"
#include "SkTypes.h"
@@ -47,12 +47,10 @@
// 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_ONCE_PTR(SkMessageBus<Message>, bus); \
template <> \
SkMessageBus<Message>* SkMessageBus<Message>::Get() { \
- static SkOnce once; \
- static SkMessageBus<Message>* bus; \
- once([] { bus = new SkMessageBus<Message>(); }); \
- return bus; \
+ return bus.get([]{ return new SkMessageBus<Message>(); }); \
}
// ----------------------- Implementation of SkMessageBus::Inbox -----------------------
diff --git a/src/core/SkMiniRecorder.cpp b/src/core/SkMiniRecorder.cpp
index 378f430..2d5b094 100644
--- a/src/core/SkMiniRecorder.cpp
+++ b/src/core/SkMiniRecorder.cpp
@@ -8,7 +8,7 @@
#include "SkCanvas.h"
#include "SkTLazy.h"
#include "SkMiniRecorder.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkPicture.h"
#include "SkPictureCommon.h"
#include "SkRecordDraw.h"
@@ -27,6 +27,7 @@
int numSlowPaths() const override { return 0; }
bool willPlayBackBitmaps() const override { return false; }
};
+SK_DECLARE_STATIC_ONCE_PTR(SkEmptyPicture, gEmptyPicture);
template <typename T>
class SkMiniPicture final : public SkPicture {
@@ -106,13 +107,8 @@
fState = State::kEmpty; \
return sk_make_sp<SkMiniPicture<Type>>(cull, reinterpret_cast<Type*>(fBuffer.get()))
- static SkOnce once;
- static SkPicture* empty;
-
switch (fState) {
- case State::kEmpty:
- once([]{ empty = new SkEmptyPicture; });
- return sk_ref_sp(empty);
+ case State::kEmpty: return sk_ref_sp(gEmptyPicture.get([]{ return new SkEmptyPicture; }));
CASE(DrawBitmapRectFixedSize);
CASE(DrawPath);
CASE(DrawRect);
diff --git a/src/core/SkPathRef.cpp b/src/core/SkPathRef.cpp
index 2fc2de9..b180551 100644
--- a/src/core/SkPathRef.cpp
+++ b/src/core/SkPathRef.cpp
@@ -6,7 +6,7 @@
*/
#include "SkBuffer.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkPath.h"
#include "SkPathRef.h"
#include <limits>
@@ -45,15 +45,13 @@
SkDEBUGCODE(fEditorsAttached = 0x7777777;)
}
-static SkPathRef* gEmpty = nullptr;
-
+SK_DECLARE_STATIC_ONCE_PTR(SkPathRef, empty);
SkPathRef* SkPathRef::CreateEmpty() {
- static SkOnce once;
- once([]{
- gEmpty = new SkPathRef;
- gEmpty->computeBounds(); // Avoids races later to be the first to do this.
- });
- return SkRef(gEmpty);
+ 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,
@@ -471,7 +469,7 @@
}
void SkPathRef::addGenIDChangeListener(GenIDChangeListener* listener) {
- if (nullptr == listener || this == gEmpty) {
+ if (nullptr == listener || this == (SkPathRef*)empty) {
delete listener;
return;
}
diff --git a/src/core/SkTaskGroup.cpp b/src/core/SkTaskGroup.cpp
index 7a58c86..2ae1b59 100644
--- a/src/core/SkTaskGroup.cpp
+++ b/src/core/SkTaskGroup.cpp
@@ -14,25 +14,25 @@
#include "SkThreadUtils.h"
#if defined(SK_BUILD_FOR_WIN32)
- static void query_num_cores(int* cores) {
+ static void query_num_cores(int* num_cores) {
SYSTEM_INFO sysinfo;
GetNativeSystemInfo(&sysinfo);
- *cores = sysinfo.dwNumberOfProcessors;
+ *num_cores = sysinfo.dwNumberOfProcessors;
}
#else
#include <unistd.h>
- static void query_num_cores(int* cores) {
- *cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
+ static void query_num_cores(int* num_cores) {
+ *num_cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
}
#endif
-static int num_cores() {
- // We cache num_cores() so we only query the OS once.
- static int cores = 0;
+int sk_num_cores() {
+ // We cache sk_num_cores() so we only query the OS once.
+ static int num_cores = 0;
static SkOnce once;
- once(query_num_cores, &cores);
- SkASSERT(cores > 0);
- return cores;
+ once(query_num_cores, &num_cores);
+ SkASSERT(num_cores > 0);
+ return num_cores;
}
namespace {
@@ -98,7 +98,7 @@
explicit ThreadPool(int threads) {
if (threads == -1) {
- threads = num_cores();
+ threads = sk_num_cores();
}
for (int i = 0; i < threads; i++) {
fThreads.push(new SkThread(&ThreadPool::Loop, this));
diff --git a/src/core/SkTaskGroup.h b/src/core/SkTaskGroup.h
index 0f793f3..0d86cdb 100644
--- a/src/core/SkTaskGroup.h
+++ b/src/core/SkTaskGroup.h
@@ -39,4 +39,7 @@
SkAtomic<int32_t> fPending;
};
+// Returns best estimate of number of CPU cores available to use.
+int sk_num_cores();
+
#endif//SkTaskGroup_DEFINED
diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp
index 75bb05c..3830c46 100644
--- a/src/core/SkTypeface.cpp
+++ b/src/core/SkTypeface.cpp
@@ -11,7 +11,7 @@
#include "SkFontMgr.h"
#include "SkMutex.h"
#include "SkOTTable_OS_2.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkStream.h"
#include "SkTypeface.h"
@@ -79,23 +79,21 @@
}
SK_DECLARE_STATIC_MUTEX(gCreateDefaultMutex);
+SK_DECLARE_STATIC_ONCE_PTR(SkTypeface, defaults[4]);
SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
- static SkOnce once[4];
- static SkTypeface* defaults[4];
-
SkASSERT((int)style < 4);
- once[style]([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, SkFontStyle::FromOldStyle(style));
- defaults[style] = t ? t : SkEmptyTypeface::Create();
+ return t ? t : SkEmptyTypeface::Create();
});
- return defaults[style];
}
SkTypeface* SkTypeface::RefDefault(Style style) {
diff --git a/src/core/SkXfermode.cpp b/src/core/SkXfermode.cpp
index b6d2388..1cfe864 100644
--- a/src/core/SkXfermode.cpp
+++ b/src/core/SkXfermode.cpp
@@ -9,7 +9,7 @@
#include "SkXfermode_proccoeff.h"
#include "SkColorPriv.h"
#include "SkMathPriv.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkOpts.h"
#include "SkReadBuffer.h"
#include "SkString.h"
@@ -1303,7 +1303,11 @@
#endif
+SK_DECLARE_STATIC_ONCE_PTR(SkXfermode, cached[SkXfermode::kLastMode + 1]);
+
sk_sp<SkXfermode> SkXfermode::Make(Mode mode) {
+ SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount);
+
if ((unsigned)mode >= kModeCount) {
// report error
return nullptr;
@@ -1315,20 +1319,13 @@
return nullptr;
}
- SkASSERT(SK_ARRAY_COUNT(gProcCoeffs) == kModeCount);
-
- static SkOnce once[SkXfermode::kLastMode+1];
- static SkXfermode* cached[SkXfermode::kLastMode+1];
-
- once[mode]([mode] {
+ return sk_ref_sp(cached[mode].get([=]{
ProcCoeff rec = gProcCoeffs[mode];
if (auto xfermode = SkOpts::create_xfermode(rec, mode)) {
- cached[mode] = xfermode;
- } else {
- cached[mode] = new SkProcCoeffXfermode(rec, mode);
+ return xfermode;
}
- });
- return sk_ref_sp(cached[mode]);
+ return (SkXfermode*) new SkProcCoeffXfermode(rec, mode);
+ }));
}
SkXfermodeProc SkXfermode::GetProc(Mode mode) {
diff --git a/src/fonts/SkRemotableFontMgr.cpp b/src/fonts/SkRemotableFontMgr.cpp
index aca8a0b..41e3bc3 100644
--- a/src/fonts/SkRemotableFontMgr.cpp
+++ b/src/fonts/SkRemotableFontMgr.cpp
@@ -5,7 +5,7 @@
* found in the LICENSE file.
*/
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkRemotableFontMgr.h"
SkRemotableFontIdentitySet::SkRemotableFontIdentitySet(int count, SkFontIdentity** data)
@@ -15,9 +15,7 @@
*data = fData;
}
+SK_DECLARE_STATIC_ONCE_PTR(SkRemotableFontIdentitySet, empty);
SkRemotableFontIdentitySet* SkRemotableFontIdentitySet::NewEmpty() {
- static SkOnce once;
- static SkRemotableFontIdentitySet* empty;
- once([]{ empty = new SkRemotableFontIdentitySet; });
- return SkRef(empty);
+ return SkRef(empty.get([]{ return new SkRemotableFontIdentitySet; }));
}
diff --git a/src/lazy/SkDiscardableMemoryPool.cpp b/src/lazy/SkDiscardableMemoryPool.cpp
index 6ced5bf..1f3bcf9 100644
--- a/src/lazy/SkDiscardableMemoryPool.cpp
+++ b/src/lazy/SkDiscardableMemoryPool.cpp
@@ -9,7 +9,7 @@
#include "SkDiscardableMemoryPool.h"
#include "SkImageGenerator.h"
#include "SkMutex.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkTInternalLList.h"
// Note:
@@ -246,13 +246,11 @@
}
SK_DECLARE_STATIC_MUTEX(gMutex);
+SK_DECLARE_STATIC_ONCE_PTR(SkDiscardableMemoryPool, global);
SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() {
- static SkOnce once;
- static SkDiscardableMemoryPool* global;
- once([]{
- global = SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE,
- &gMutex);
+ return global.get([] {
+ return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE,
+ &gMutex);
});
- return global;
}
diff --git a/src/opts/opts_check_x86.cpp b/src/opts/opts_check_x86.cpp
index dacb49b..2cb2e65 100644
--- a/src/opts/opts_check_x86.cpp
+++ b/src/opts/opts_check_x86.cpp
@@ -13,6 +13,7 @@
#include "SkBlitRow.h"
#include "SkBlitRow_opts_SSE2.h"
#include "SkCpu.h"
+#include "SkOncePtr.h"
#include "SkRTConf.h"
diff --git a/src/ports/SkFontHost_mac.cpp b/src/ports/SkFontHost_mac.cpp
index 8593b39..72d4690 100644
--- a/src/ports/SkFontHost_mac.cpp
+++ b/src/ports/SkFontHost_mac.cpp
@@ -35,7 +35,7 @@
#include "SkOTTable_hhea.h"
#include "SkOTTable_loca.h"
#include "SkOTUtils.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkSFNTHeader.h"
@@ -778,12 +778,12 @@
return &legacy_CTFontDrawGlyphs;
}
+SK_DECLARE_STATIC_ONCE_PTR(CTFontDrawGlyphsProc, gCTFontDrawGlyphs);
+
CGRGBPixel* Offscreen::getCG(const SkScalerContext_Mac& context, const SkGlyph& glyph,
CGGlyph glyphID, size_t* rowBytesPtr,
bool generateA8FromLCD) {
- static SkOnce once;
- static CTFontDrawGlyphsProc* ctFontDrawGlyphs;
- once([]{ ctFontDrawGlyphs = choose_CTFontDrawGlyphs(); });
+ auto ctFontDrawGlyphs = gCTFontDrawGlyphs.get(choose_CTFontDrawGlyphs);
if (!fRGBSpace) {
//It doesn't appear to matter what color space is specified.
diff --git a/src/utils/SkEventTracer.cpp b/src/utils/SkEventTracer.cpp
index 0a748d1..32a0207 100644
--- a/src/utils/SkEventTracer.cpp
+++ b/src/utils/SkEventTracer.cpp
@@ -7,7 +7,7 @@
#include "SkAtomics.h"
#include "SkEventTracer.h"
-#include "SkOnce.h"
+#include "SkOncePtr.h"
#include <stdlib.h>
@@ -39,8 +39,9 @@
}
};
-// We prefer gUserTracer if it's been set, otherwise we fall back on a default tracer;
+// We prefer gUserTracer if it's been set, otherwise we fall back on gDefaultTracer.
static SkEventTracer* gUserTracer = nullptr;
+SK_DECLARE_STATIC_ONCE_PTR(SkDefaultEventTracer, gDefaultTracer);
void SkEventTracer::SetInstance(SkEventTracer* tracer) {
SkASSERT(nullptr == sk_atomic_load(&gUserTracer, sk_memory_order_acquire));
@@ -53,8 +54,5 @@
if (SkEventTracer* tracer = sk_atomic_load(&gUserTracer, sk_memory_order_acquire)) {
return tracer;
}
- static SkOnce once;
- static SkDefaultEventTracer* defaultTracer;
- once([] { defaultTracer = new SkDefaultEventTracer; });
- return defaultTracer;
+ return gDefaultTracer.get([]{ return new SkDefaultEventTracer; });
}
diff --git a/tests/OncePtrTest.cpp b/tests/OncePtrTest.cpp
new file mode 100644
index 0000000..0aacdf5
--- /dev/null
+++ b/tests/OncePtrTest.cpp
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2015 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 "SkOncePtr.h"
+#include "SkTaskGroup.h"
+
+SK_DECLARE_STATIC_ONCE_PTR(int, once);
+
+DEF_TEST(OncePtr, r) {
+ static SkAtomic<int> calls(0);
+ auto create = [&] {
+ calls.fetch_add(1);
+ return new int(5);
+ };
+
+ SkTaskGroup().batch(sk_num_cores()*4, [&](size_t) {
+ int* n = once.get(create);
+ REPORTER_ASSERT(r, *n == 5);
+ });
+ REPORTER_ASSERT(r, calls.load() == 1);
+}