From a0c814cffb7ba91e1c3b533e68ab591d9cee8f2b Mon Sep 17 00:00:00 2001 From: reed Date: Wed, 22 Oct 2014 13:20:58 -0700 Subject: Add SkTypeface::getBounds() mirrored from https://codereview.chromium.org/666303002/ BUG=skia: TBR= Review URL: https://codereview.chromium.org/676523002 --- include/core/SkLazyPtr.h | 166 ++++++++++++++++++++++++++++++++++++++++++++ include/core/SkThreadPriv.h | 23 ++++++ include/core/SkTypeface.h | 20 +++++- 3 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 include/core/SkLazyPtr.h create mode 100644 include/core/SkThreadPriv.h (limited to 'include') diff --git a/include/core/SkLazyPtr.h b/include/core/SkLazyPtr.h new file mode 100644 index 0000000000..896dfbf88d --- /dev/null +++ b/include/core/SkLazyPtr.h @@ -0,0 +1,166 @@ +/* + * 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 SkNEW, destroyed with SkDELETE. + * return singleton.get(); + * } + * + * These macros take an optional T* (*Create)() and void (*Destroy)(T*) at the end. + * If not given, we'll use SkNEW and SkDELETE. + * 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 name + +#define SK_DECLARE_STATIC_LAZY_PTR_ARRAY(T, name, N, ...) \ + namespace {} static Private::SkStaticLazyPtrArray 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 "SkDynamicAnnotations.h" +#include "SkThread.h" +#include "SkThreadPriv.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 the same memory barriers as sk_atomic_cas: acquire on failure, release on success. +template +static P try_cas(void** dst, P ptr) { + P prev = (P)sk_atomic_cas(dst, NULL, ptr); + + if (prev) { + // We need an acquire barrier before returning prev, which sk_atomic_cas provided. + Destroy(ptr); + return prev; + } else { + // We need a release barrier before returning ptr, which sk_atomic_cas provided. + return ptr; + } +} + +template T* sk_new() { return SkNEW(T); } +template void sk_delete(T* ptr) { SkDELETE(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 sk_consume_load is free on all our platforms: x86, +// ARM, MIPS. In contrast, sk_acquire_load issues a memory barrier on non-x86. + +// This has no constructor and must be zero-initalized (the macro above does this). +template , void (*Destroy)(T*) = sk_delete > +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 = (T*)sk_consume_load(&fPtr); + return ptr ? ptr : try_cas(&fPtr, Create()); + } + +private: + void* fPtr; +}; + +template T* sk_new_arg(int i) { return SkNEW_ARGS(T, (i)); } + +// This has no constructor and must be zero-initalized (the macro above does this). +template , void (*Destroy)(T*) = sk_delete > +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 = (T*)sk_consume_load(&fArray[i]); + return ptr ? ptr : try_cas(&fArray[i], Create(i)); + } + +private: + void* 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 > +class SkLazyPtr : SkNoncopyable { +public: + SkLazyPtr() : fPtr(NULL) {} + ~SkLazyPtr() { if (fPtr) { Destroy((T*)fPtr); } } + + T* get() const { + T* ptr = (T*)sk_consume_load(&fPtr); + return ptr ? ptr : Private::try_cas(&fPtr, SkNEW(T)); + } + + template + T* get(const Create& create) const { + T* ptr = (T*)sk_consume_load(&fPtr); + return ptr ? ptr : Private::try_cas(&fPtr, create()); + } + +private: + mutable void* fPtr; +}; + + +#endif//SkLazyPtr_DEFINED diff --git a/include/core/SkThreadPriv.h b/include/core/SkThreadPriv.h new file mode 100644 index 0000000000..c44cca5350 --- /dev/null +++ b/include/core/SkThreadPriv.h @@ -0,0 +1,23 @@ +/* + * 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 SkThreadPriv_DEFINED +#define SkThreadPriv_DEFINED + +#include "SkTypes.h" + +// SK_ATOMICS_PLATFORM_H must provide inline implementations for the following declarations. + +/** Atomic compare and set, for pointers. + * If *addr == before, set *addr to after. Always returns previous value of *addr. + * This must issue a release barrier on success, acquire on failure, and always a compiler barrier. + */ +static void* sk_atomic_cas(void** addr, void* before, void* after); + +#include SK_ATOMICS_PLATFORM_H + +#endif//SkThreadPriv_DEFINED diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index f67623674a..c3ff3641b7 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -12,6 +12,7 @@ #include "SkAdvancedTypefaceMetrics.h" #include "SkFontStyle.h" +#include "SkLazyPtr.h" #include "SkWeakRefCnt.h" class SkDescriptor; @@ -282,6 +283,13 @@ public: SkScalerContext* createScalerContext(const SkDescriptor*, bool allowFailure = false) const; + /** + * Return a rectangle (scaled to 1-pt) that represents the union of the bounds of all + * of the glyphs, but each one positioned at (0,). This may be conservatively large, and + * will not take into account any hinting or other size-specific adjustments. + */ + SkRect getBounds() const; + // PRIVATE / EXPERIMENTAL -- do not call void filterRec(SkScalerContextRec* rec) const { this->onFilterRec(rec); @@ -333,6 +341,8 @@ protected: virtual size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const = 0; + virtual bool onComputeBounds(SkRect*) const; + private: friend class SkGTypeface; friend class SkPDFFont; @@ -359,9 +369,13 @@ private: static SkTypeface* CreateDefault(int style); // SkLazyPtr requires an int, not a Style. static void DeleteDefault(SkTypeface*); - SkFontID fUniqueID; - SkFontStyle fStyle; - bool fIsFixedPitch; + struct BoundsComputer; +// friend struct BoundsComputer; + + SkLazyPtr fLazyBounds; + SkFontID fUniqueID; + SkFontStyle fStyle; + bool fIsFixedPitch; friend class SkPaint; friend class SkGlyphCache; // GetDefaultTypeface -- cgit v1.2.3