diff options
author | mtklein <mtklein@chromium.org> | 2016-05-02 07:19:41 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-05-02 07:19:41 -0700 |
commit | 59c12e3f0071acfa685346487e54a2a311066dac (patch) | |
tree | 5311ab7c77393dc85a8bb979b7c3b7f761a51e0b | |
parent | 57b46159740020ef904aaf41e9e5ae07106591cc (diff) |
remove non-static uses of SkOncePtr
Still slowly working through all the SK_DECLARE_STATIC_FOO macros.
SkOncePtr is complicating things by having SkOncePtr delete its pointer
and SkBaseOncePtr not. Simplify things by removing SkOncePtr, leaving
only the leaky SkBaseOncePtr.
We replace SkOncePtr<T> instead with SkOnce and T. In most cases this
did not need to be a pointer, and in some cases here we're even saving
a few bytes by replacing SkOncePtr<T> with SkOnce and a T.
The dependency map of SK_DECLARE_STATIC_FOO is:
SkBaseMutex -> SkBaseSemaphore -> SkBaseOncePtr
They're intertwined enough that I think I've got to do all three in one
next CL.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1939503002
Review-Url: https://codereview.chromium.org/1939503002
-rw-r--r-- | include/core/SkColorTable.h | 5 | ||||
-rw-r--r-- | include/core/SkTypeface.h | 5 | ||||
-rw-r--r-- | include/gpu/GrProcessor.h | 1 | ||||
-rw-r--r-- | include/private/SkOncePtr.h | 42 | ||||
-rw-r--r-- | src/core/SkBigPicture.cpp | 7 | ||||
-rw-r--r-- | src/core/SkBigPicture.h | 7 | ||||
-rw-r--r-- | src/core/SkColorTable.cpp | 10 | ||||
-rw-r--r-- | src/core/SkTypeface.cpp | 9 | ||||
-rw-r--r-- | src/image/SkSurface.cpp | 1 | ||||
-rw-r--r-- | tests/OncePtrTest.cpp | 21 |
10 files changed, 27 insertions, 81 deletions
diff --git a/include/core/SkColorTable.h b/include/core/SkColorTable.h index ccea7ed550..39553badfc 100644 --- a/include/core/SkColorTable.h +++ b/include/core/SkColorTable.h @@ -10,7 +10,7 @@ #ifndef SkColorTable_DEFINED #define SkColorTable_DEFINED -#include "../private/SkOncePtr.h" +#include "../private/SkOnce.h" #include "SkColor.h" #include "SkFlattenable.h" #include "SkImageInfo.h" @@ -62,7 +62,8 @@ private: SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc); SkPMColor* fColors; - SkOncePtr<uint16_t[]> f16BitCache; + mutable uint16_t* f16BitCache = nullptr; + mutable SkOnce f16BitCacheOnce; int fCount; void init(const SkPMColor* colors, int count); diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index 0fbf6a3e0a..f22d2bd14f 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -8,7 +8,7 @@ #ifndef SkTypeface_DEFINED #define SkTypeface_DEFINED -#include "../private/SkOncePtr.h" +#include "../private/SkOnce.h" #include "../private/SkWeakRefCnt.h" #include "SkFontStyle.h" #include "SkRect.h" @@ -398,9 +398,10 @@ private: static SkTypeface* CreateDefault(int style); // SkLazyPtr requires an int, not a Style. static void DeleteDefault(SkTypeface*); - SkOncePtr<SkRect> fLazyBounds; SkFontID fUniqueID; SkFontStyle fStyle; + mutable SkRect fBounds; + mutable SkOnce fBoundsOnce; bool fIsFixedPitch; friend class SkPaint; diff --git a/include/gpu/GrProcessor.h b/include/gpu/GrProcessor.h index 27fce2e267..d374a7f3d0 100644 --- a/include/gpu/GrProcessor.h +++ b/include/gpu/GrProcessor.h @@ -15,6 +15,7 @@ #include "GrBufferAccess.h" #include "SkMath.h" #include "SkString.h" +#include "../private/SkAtomics.h" class GrContext; class GrCoordTransform; diff --git a/include/private/SkOncePtr.h b/include/private/SkOncePtr.h index b60d968b4a..921c6a6534 100644 --- a/include/private/SkOncePtr.h +++ b/include/private/SkOncePtr.h @@ -11,51 +11,9 @@ #include "../private/SkAtomics.h" #include <memory> -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 SkBaseOncePtr<type> name; -// Use this for a local or member pointer that's initialized exactly once when you call get(). -template <typename T, typename Delete = std::default_delete<T>> -class SkOncePtr : SkNoncopyable { -public: - SkOncePtr() { sk_bzero(this, sizeof(*this)); } - ~SkOncePtr() { - if (T* ptr = (T*)*this) { - Delete()(ptr); - } - } - - template <typename F> - T* get(const F& f) const { - return fOnce.get(f); - } - - operator T*() const { - return (T*)fOnce; - } - -private: - SkBaseOncePtr<T> fOnce; -}; - -// If you ask for SkOncePtr<T[]>, we'll clean up with delete[] by default. -template <typename T> -class SkOncePtr<T[]> : public SkOncePtr<T, std::default_delete<T[]>> {}; - -/* TODO(mtklein): in next CL -typedef SkBaseOncePtr<void> SkOnceFlag; -#define SK_DECLARE_STATIC_ONCE(name) namespace {} static SkOnceFlag name - -template <typename F> -inline void SkOnce(SkOnceFlag* once, const F& f) { - once->get([&]{ f(); return (void*)2; }); -} -*/ - -// Implementation details below here! No peeking! - template <typename T> class SkBaseOncePtr { public: diff --git a/src/core/SkBigPicture.cpp b/src/core/SkBigPicture.cpp index 70f68db337..da3d3a8418 100644 --- a/src/core/SkBigPicture.cpp +++ b/src/core/SkBigPicture.cpp @@ -58,7 +58,8 @@ void SkBigPicture::partialPlayback(SkCanvas* canvas, } const SkBigPicture::Analysis& SkBigPicture::analysis() const { - return *fAnalysis.get([&]{ return new Analysis(*fRecord); }); + fAnalysisOnce([this] { fAnalysis.init(*fRecord); }); + return fAnalysis; } SkRect SkBigPicture::cullRect() const { return fCullRect; } @@ -80,8 +81,8 @@ SkPicture const* const* SkBigPicture::drawablePicts() const { return fDrawablePicts ? fDrawablePicts->begin() : nullptr; } -SkBigPicture::Analysis::Analysis(const SkRecord& record) { - TRACE_EVENT0("disabled-by-default-skia", "SkBigPicture::Analysis::Analysis()"); +void SkBigPicture::Analysis::init(const SkRecord& record) { + TRACE_EVENT0("disabled-by-default-skia", "SkBigPicture::Analysis::init()"); SkTextHunter text; SkBitmapHunter bitmap; SkPathCounter path; diff --git a/src/core/SkBigPicture.h b/src/core/SkBigPicture.h index 0834709f8a..f9997f705b 100644 --- a/src/core/SkBigPicture.h +++ b/src/core/SkBigPicture.h @@ -8,7 +8,7 @@ #ifndef SkBigPicture_DEFINED #define SkBigPicture_DEFINED -#include "SkOncePtr.h" +#include "SkOnce.h" #include "SkPicture.h" #include "SkRect.h" #include "SkTemplates.h" @@ -65,7 +65,7 @@ public: private: struct Analysis { - explicit Analysis(const SkRecord&); + void init(const SkRecord&); bool suitableForGpuRasterization(const char** reason) const; @@ -81,7 +81,8 @@ private: const SkRect fCullRect; const size_t fApproxBytesUsedBySubPictures; - SkOncePtr<const Analysis> fAnalysis; + mutable SkOnce fAnalysisOnce; + mutable Analysis fAnalysis; SkAutoTUnref<const SkRecord> fRecord; SkAutoTDelete<const SnapshotArray> fDrawablePicts; SkAutoTUnref<const SkBBoxHierarchy> fBBH; diff --git a/src/core/SkColorTable.cpp b/src/core/SkColorTable.cpp index d7253e1d8c..296b31c369 100644 --- a/src/core/SkColorTable.cpp +++ b/src/core/SkColorTable.cpp @@ -41,19 +41,19 @@ SkColorTable::SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc) SkColorTable::~SkColorTable() { sk_free(fColors); - // f16BitCache frees itself + sk_free(f16BitCache); } #include "SkColorPriv.h" const uint16_t* SkColorTable::read16BitCache() const { - return f16BitCache.get([&]{ - auto cache = new uint16_t[fCount]; + f16BitCacheOnce([this] { + f16BitCache = (uint16_t*)sk_malloc_throw(fCount * sizeof(uint16_t)); for (int i = 0; i < fCount; i++) { - cache[i] = SkPixel32ToPixel16_ToU16(fColors[i]); + f16BitCache[i] = SkPixel32ToPixel16_ToU16(fColors[i]); } - return cache; }); + return f16BitCache; } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp index d768efc603..3830c46c7a 100644 --- a/src/core/SkTypeface.cpp +++ b/src/core/SkTypeface.cpp @@ -318,13 +318,12 @@ bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count, #include "SkPaint.h" SkRect SkTypeface::getBounds() const { - return *fLazyBounds.get([&] { - SkRect* rect = new SkRect; - if (!this->onComputeBounds(rect)) { - rect->setEmpty(); + fBoundsOnce([this] { + if (!this->onComputeBounds(&fBounds)) { + fBounds.setEmpty(); } - return rect; }); + return fBounds; } bool SkTypeface::onComputeBounds(SkRect* bounds) const { diff --git a/src/image/SkSurface.cpp b/src/image/SkSurface.cpp index d6f69806ee..8f6e308976 100644 --- a/src/image/SkSurface.cpp +++ b/src/image/SkSurface.cpp @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include "SkAtomics.h" #include "SkSurface_Base.h" #include "SkImagePriv.h" #include "SkCanvas.h" diff --git a/tests/OncePtrTest.cpp b/tests/OncePtrTest.cpp index d01cee09fe..0aacdf53ee 100644 --- a/tests/OncePtrTest.cpp +++ b/tests/OncePtrTest.cpp @@ -9,9 +9,9 @@ #include "SkOncePtr.h" #include "SkTaskGroup.h" -DEF_TEST(OncePtr, r) { - SkOncePtr<int> once; +SK_DECLARE_STATIC_ONCE_PTR(int, once); +DEF_TEST(OncePtr, r) { static SkAtomic<int> calls(0); auto create = [&] { calls.fetch_add(1); @@ -24,20 +24,3 @@ DEF_TEST(OncePtr, r) { }); REPORTER_ASSERT(r, calls.load() == 1); } - -/* TODO(mtklein): next CL - -SK_DECLARE_STATIC_ONCE(once_noptr); -DEF_TEST(OnceNoPtr, r) { - static SkAtomic<int> calls(0); - - SkAtomic<int> force_a_race(sk_num_cores()); - SkTaskGroup().batch(sk_num_cores()*4, [&](size_t) { - force_a_race.fetch_add(-1); - while (force_a_race.load() > 0); - - SkOnce(&once_noptr, [&] { calls.fetch_add(1); }); - }); - REPORTER_ASSERT(r, calls.load() == 1); -} -*/ |