diff options
author | 2015-08-11 07:15:00 -0700 | |
---|---|---|
committer | 2015-08-11 07:15:00 -0700 | |
commit | eedf0fb20489bc6c3706539dd6ab499f41cbe048 (patch) | |
tree | 13c94656ac5006f712df36b9b37b64630ceed056 /include/core | |
parent | 95d79132cf605a3c5589b4ea738ef8417b41c10a (diff) |
move some public headers into private
BUG=skia:
Review URL: https://codereview.chromium.org/1289483002
Diffstat (limited to 'include/core')
-rw-r--r-- | include/core/SkEndian.h | 194 | ||||
-rw-r--r-- | include/core/SkFloatBits.h | 132 | ||||
-rw-r--r-- | include/core/SkFloatingPoint.h | 170 | ||||
-rw-r--r-- | include/core/SkPicture.h | 1 | ||||
-rw-r--r-- | include/core/SkScalar.h | 2 | ||||
-rw-r--r-- | include/core/SkTypeface.h | 2 | ||||
-rw-r--r-- | include/core/SkWeakRefCnt.h | 159 |
7 files changed, 2 insertions, 658 deletions
diff --git a/include/core/SkEndian.h b/include/core/SkEndian.h deleted file mode 100644 index 0955fcc505..0000000000 --- a/include/core/SkEndian.h +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright 2006 The Android Open Source Project - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef SkEndian_DEFINED -#define SkEndian_DEFINED - -#include "SkTypes.h" - -/** \file SkEndian.h - - Macros and helper functions for handling 16 and 32 bit values in - big and little endian formats. -*/ - -#if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN) - #error "can't have both LENDIAN and BENDIAN defined" -#endif - -#if !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN) - #error "need either LENDIAN or BENDIAN defined" -#endif - -/** Swap the two bytes in the low 16bits of the parameters. - e.g. 0x1234 -> 0x3412 -*/ -static inline uint16_t SkEndianSwap16(uint16_t value) { - return static_cast<uint16_t>((value >> 8) | (value << 8)); -} - -template<uint16_t N> struct SkTEndianSwap16 { - static const uint16_t value = static_cast<uint16_t>((N >> 8) | ((N & 0xFF) << 8)); -}; - -/** Vector version of SkEndianSwap16(), which swaps the - low two bytes of each value in the array. -*/ -static inline void SkEndianSwap16s(uint16_t array[], int count) { - SkASSERT(count == 0 || array != NULL); - - while (--count >= 0) { - *array = SkEndianSwap16(*array); - array += 1; - } -} - -/** Reverse all 4 bytes in a 32bit value. - e.g. 0x12345678 -> 0x78563412 -*/ -static inline uint32_t SkEndianSwap32(uint32_t value) { - return ((value & 0xFF) << 24) | - ((value & 0xFF00) << 8) | - ((value & 0xFF0000) >> 8) | - (value >> 24); -} - -template<uint32_t N> struct SkTEndianSwap32 { - static const uint32_t value = ((N & 0xFF) << 24) | - ((N & 0xFF00) << 8) | - ((N & 0xFF0000) >> 8) | - (N >> 24); -}; - -/** Vector version of SkEndianSwap32(), which swaps the - bytes of each value in the array. -*/ -static inline void SkEndianSwap32s(uint32_t array[], int count) { - SkASSERT(count == 0 || array != NULL); - - while (--count >= 0) { - *array = SkEndianSwap32(*array); - array += 1; - } -} - -/** Reverse all 8 bytes in a 64bit value. - e.g. 0x1122334455667788 -> 0x8877665544332211 -*/ -static inline uint64_t SkEndianSwap64(uint64_t value) { - return (((value & 0x00000000000000FFULL) << (8*7)) | - ((value & 0x000000000000FF00ULL) << (8*5)) | - ((value & 0x0000000000FF0000ULL) << (8*3)) | - ((value & 0x00000000FF000000ULL) << (8*1)) | - ((value & 0x000000FF00000000ULL) >> (8*1)) | - ((value & 0x0000FF0000000000ULL) >> (8*3)) | - ((value & 0x00FF000000000000ULL) >> (8*5)) | - ((value) >> (8*7))); -} -template<uint64_t N> struct SkTEndianSwap64 { - static const uint64_t value = (((N & 0x00000000000000FFULL) << (8*7)) | - ((N & 0x000000000000FF00ULL) << (8*5)) | - ((N & 0x0000000000FF0000ULL) << (8*3)) | - ((N & 0x00000000FF000000ULL) << (8*1)) | - ((N & 0x000000FF00000000ULL) >> (8*1)) | - ((N & 0x0000FF0000000000ULL) >> (8*3)) | - ((N & 0x00FF000000000000ULL) >> (8*5)) | - ((N) >> (8*7))); -}; - -/** Vector version of SkEndianSwap64(), which swaps the - bytes of each value in the array. -*/ -static inline void SkEndianSwap64s(uint64_t array[], int count) { - SkASSERT(count == 0 || array != NULL); - - while (--count >= 0) { - *array = SkEndianSwap64(*array); - array += 1; - } -} - -#ifdef SK_CPU_LENDIAN - #define SkEndian_SwapBE16(n) SkEndianSwap16(n) - #define SkEndian_SwapBE32(n) SkEndianSwap32(n) - #define SkEndian_SwapBE64(n) SkEndianSwap64(n) - #define SkEndian_SwapLE16(n) (n) - #define SkEndian_SwapLE32(n) (n) - #define SkEndian_SwapLE64(n) (n) - - #define SkTEndian_SwapBE16(n) SkTEndianSwap16<n>::value - #define SkTEndian_SwapBE32(n) SkTEndianSwap32<n>::value - #define SkTEndian_SwapBE64(n) SkTEndianSwap64<n>::value - #define SkTEndian_SwapLE16(n) (n) - #define SkTEndian_SwapLE32(n) (n) - #define SkTEndian_SwapLE64(n) (n) -#else // SK_CPU_BENDIAN - #define SkEndian_SwapBE16(n) (n) - #define SkEndian_SwapBE32(n) (n) - #define SkEndian_SwapBE64(n) (n) - #define SkEndian_SwapLE16(n) SkEndianSwap16(n) - #define SkEndian_SwapLE32(n) SkEndianSwap32(n) - #define SkEndian_SwapLE64(n) SkEndianSwap64(n) - - #define SkTEndian_SwapBE16(n) (n) - #define SkTEndian_SwapBE32(n) (n) - #define SkTEndian_SwapBE64(n) (n) - #define SkTEndian_SwapLE16(n) SkTEndianSwap16<n>::value - #define SkTEndian_SwapLE32(n) SkTEndianSwap32<n>::value - #define SkTEndian_SwapLE64(n) SkTEndianSwap64<n>::value -#endif - -// When a bytestream is embedded in a 32-bit word, how far we need to -// shift the word to extract each byte from the low 8 bits by anding with 0xff. -#ifdef SK_CPU_LENDIAN - #define SkEndian_Byte0Shift 0 - #define SkEndian_Byte1Shift 8 - #define SkEndian_Byte2Shift 16 - #define SkEndian_Byte3Shift 24 -#else // SK_CPU_BENDIAN - #define SkEndian_Byte0Shift 24 - #define SkEndian_Byte1Shift 16 - #define SkEndian_Byte2Shift 8 - #define SkEndian_Byte3Shift 0 -#endif - - -#if defined(SK_UINT8_BITFIELD_LENDIAN) && defined(SK_UINT8_BITFIELD_BENDIAN) - #error "can't have both bitfield LENDIAN and BENDIAN defined" -#endif - -#if !defined(SK_UINT8_BITFIELD_LENDIAN) && !defined(SK_UINT8_BITFIELD_BENDIAN) - #ifdef SK_CPU_LENDIAN - #define SK_UINT8_BITFIELD_LENDIAN - #else - #define SK_UINT8_BITFIELD_BENDIAN - #endif -#endif - -#ifdef SK_UINT8_BITFIELD_LENDIAN - #define SK_UINT8_BITFIELD(f0, f1, f2, f3, f4, f5, f6, f7) \ - SK_OT_BYTE f0 : 1; \ - SK_OT_BYTE f1 : 1; \ - SK_OT_BYTE f2 : 1; \ - SK_OT_BYTE f3 : 1; \ - SK_OT_BYTE f4 : 1; \ - SK_OT_BYTE f5 : 1; \ - SK_OT_BYTE f6 : 1; \ - SK_OT_BYTE f7 : 1; -#else - #define SK_UINT8_BITFIELD(f0, f1, f2, f3, f4, f5, f6, f7) \ - SK_OT_BYTE f7 : 1; \ - SK_OT_BYTE f6 : 1; \ - SK_OT_BYTE f5 : 1; \ - SK_OT_BYTE f4 : 1; \ - SK_OT_BYTE f3 : 1; \ - SK_OT_BYTE f2 : 1; \ - SK_OT_BYTE f1 : 1; \ - SK_OT_BYTE f0 : 1; -#endif - -#endif diff --git a/include/core/SkFloatBits.h b/include/core/SkFloatBits.h deleted file mode 100644 index 3ddb9ef564..0000000000 --- a/include/core/SkFloatBits.h +++ /dev/null @@ -1,132 +0,0 @@ - -/* - * Copyright 2008 The Android Open Source Project - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - -#ifndef SkFloatBits_DEFINED -#define SkFloatBits_DEFINED - -#include "SkTypes.h" - -/** Convert a sign-bit int (i.e. float interpreted as int) into a 2s compliement - int. This also converts -0 (0x80000000) to 0. Doing this to a float allows - it to be compared using normal C operators (<, <=, etc.) -*/ -static inline int32_t SkSignBitTo2sCompliment(int32_t x) { - if (x < 0) { - x &= 0x7FFFFFFF; - x = -x; - } - return x; -} - -/** Convert a 2s compliment int to a sign-bit (i.e. int interpreted as float). - This undoes the result of SkSignBitTo2sCompliment(). - */ -static inline int32_t Sk2sComplimentToSignBit(int32_t x) { - int sign = x >> 31; - // make x positive - x = (x ^ sign) - sign; - // set the sign bit as needed - x |= sign << 31; - return x; -} - -/** Given the bit representation of a float, return its value cast to an int. - If the value is out of range, or NaN, return return +/- SK_MaxS32 -*/ -int32_t SkFloatBits_toIntCast(int32_t floatBits); - -/** Given the bit representation of a float, return its floor as an int. - If the value is out of range, or NaN, return return +/- SK_MaxS32 - */ -SK_API int32_t SkFloatBits_toIntFloor(int32_t floatBits); - -/** Given the bit representation of a float, return it rounded to an int. - If the value is out of range, or NaN, return return +/- SK_MaxS32 - */ -SK_API int32_t SkFloatBits_toIntRound(int32_t floatBits); - -/** Given the bit representation of a float, return its ceiling as an int. - If the value is out of range, or NaN, return return +/- SK_MaxS32 - */ -SK_API int32_t SkFloatBits_toIntCeil(int32_t floatBits); - - -union SkFloatIntUnion { - float fFloat; - int32_t fSignBitInt; -}; - -// Helper to see a float as its bit pattern (w/o aliasing warnings) -static inline int32_t SkFloat2Bits(float x) { - SkFloatIntUnion data; - data.fFloat = x; - return data.fSignBitInt; -} - -// Helper to see a bit pattern as a float (w/o aliasing warnings) -static inline float SkBits2Float(int32_t floatAsBits) { - SkFloatIntUnion data; - data.fSignBitInt = floatAsBits; - return data.fFloat; -} - -/** Return the float as a 2s compliment int. Just to be used to compare floats - to each other or against positive float-bit-constants (like 0). This does - not return the int equivalent of the float, just something cheaper for - compares-only. - */ -static inline int32_t SkFloatAs2sCompliment(float x) { - return SkSignBitTo2sCompliment(SkFloat2Bits(x)); -} - -/** Return the 2s compliment int as a float. This undos the result of - SkFloatAs2sCompliment - */ -static inline float Sk2sComplimentAsFloat(int32_t x) { - return SkBits2Float(Sk2sComplimentToSignBit(x)); -} - -/** Return x cast to a float (i.e. (float)x) -*/ -float SkIntToFloatCast(int x); - -/** Return the float cast to an int. - If the value is out of range, or NaN, return +/- SK_MaxS32 -*/ -static inline int32_t SkFloatToIntCast(float x) { - return SkFloatBits_toIntCast(SkFloat2Bits(x)); -} - -/** Return the floor of the float as an int. - If the value is out of range, or NaN, return +/- SK_MaxS32 -*/ -static inline int32_t SkFloatToIntFloor(float x) { - return SkFloatBits_toIntFloor(SkFloat2Bits(x)); -} - -/** Return the float rounded to an int. - If the value is out of range, or NaN, return +/- SK_MaxS32 -*/ -static inline int32_t SkFloatToIntRound(float x) { - return SkFloatBits_toIntRound(SkFloat2Bits(x)); -} - -/** Return the ceiling of the float as an int. - If the value is out of range, or NaN, return +/- SK_MaxS32 -*/ -static inline int32_t SkFloatToIntCeil(float x) { - return SkFloatBits_toIntCeil(SkFloat2Bits(x)); -} - -// Scalar wrappers for float-bit routines - -#define SkScalarAs2sCompliment(x) SkFloatAs2sCompliment(x) -#define Sk2sComplimentAsScalar(x) Sk2sComplimentAsFloat(x) - -#endif diff --git a/include/core/SkFloatingPoint.h b/include/core/SkFloatingPoint.h deleted file mode 100644 index f7ee816b12..0000000000 --- a/include/core/SkFloatingPoint.h +++ /dev/null @@ -1,170 +0,0 @@ - -/* - * Copyright 2006 The Android Open Source Project - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - -#ifndef SkFloatingPoint_DEFINED -#define SkFloatingPoint_DEFINED - -#include "SkTypes.h" - -#include <math.h> -#include <float.h> - -// For _POSIX_VERSION -#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) -#include <unistd.h> -#endif - -#include "SkFloatBits.h" - -// C++98 cmath std::pow seems to be the earliest portable way to get float pow. -// However, on Linux including cmath undefines isfinite. -// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608 -static inline float sk_float_pow(float base, float exp) { - return powf(base, exp); -} - -static inline float sk_float_copysign(float x, float y) { -// c++11 contains a 'float copysign(float, float)' function in <cmath>. -// clang-cl reports __cplusplus for clang, not the __cplusplus vc++ version _MSC_VER would report. -#if (defined(_MSC_VER) && defined(__clang__)) -# define SK_BUILD_WITH_CLANG_CL 1 -#else -# define SK_BUILD_WITH_CLANG_CL 0 -#endif -#if (!SK_BUILD_WITH_CLANG_CL && __cplusplus >= 201103L) || (_MSC_VER >= 1800) - return copysignf(x, y); - -// Posix has demanded 'float copysignf(float, float)' (from C99) since Issue 6. -#elif defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L - return copysignf(x, y); - -// Visual studio prior to 13 only has 'double _copysign(double, double)'. -#elif defined(_MSC_VER) - return (float)_copysign(x, y); - -// Otherwise convert to bits and extract sign. -#else - int32_t xbits = SkFloat2Bits(x); - int32_t ybits = SkFloat2Bits(y); - return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000)); -#endif -} - -#define sk_float_sqrt(x) sqrtf(x) -#define sk_float_sin(x) sinf(x) -#define sk_float_cos(x) cosf(x) -#define sk_float_tan(x) tanf(x) -#define sk_float_floor(x) floorf(x) -#define sk_float_ceil(x) ceilf(x) -#ifdef SK_BUILD_FOR_MAC -# define sk_float_acos(x) static_cast<float>(acos(x)) -# define sk_float_asin(x) static_cast<float>(asin(x)) -#else -# define sk_float_acos(x) acosf(x) -# define sk_float_asin(x) asinf(x) -#endif -#define sk_float_atan2(y,x) atan2f(y,x) -#define sk_float_abs(x) fabsf(x) -#define sk_float_mod(x,y) fmodf(x,y) -#define sk_float_exp(x) expf(x) -#define sk_float_log(x) logf(x) - -#define sk_float_round(x) sk_float_floor((x) + 0.5f) - -// can't find log2f on android, but maybe that just a tool bug? -#ifdef SK_BUILD_FOR_ANDROID - static inline float sk_float_log2(float x) { - const double inv_ln_2 = 1.44269504088896; - return (float)(log(x) * inv_ln_2); - } -#else - #define sk_float_log2(x) log2f(x) -#endif - -#ifdef SK_BUILD_FOR_WIN - #define sk_float_isfinite(x) _finite(x) - #define sk_float_isnan(x) _isnan(x) - static inline int sk_float_isinf(float x) { - int32_t bits = SkFloat2Bits(x); - return (bits << 1) == (0xFF << 24); - } -#else - #define sk_float_isfinite(x) isfinite(x) - #define sk_float_isnan(x) isnan(x) - #define sk_float_isinf(x) isinf(x) -#endif - -#define sk_double_isnan(a) sk_float_isnan(a) - -#ifdef SK_USE_FLOATBITS - #define sk_float_floor2int(x) SkFloatToIntFloor(x) - #define sk_float_round2int(x) SkFloatToIntRound(x) - #define sk_float_ceil2int(x) SkFloatToIntCeil(x) -#else - #define sk_float_floor2int(x) (int)sk_float_floor(x) - #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f) - #define sk_float_ceil2int(x) (int)sk_float_ceil(x) -#endif - -#define sk_double_floor(x) floor(x) -#define sk_double_round(x) floor((x) + 0.5) -#define sk_double_ceil(x) ceil(x) -#define sk_double_floor2int(x) (int)floor(x) -#define sk_double_round2int(x) (int)floor((x) + 0.5f) -#define sk_double_ceil2int(x) (int)ceil(x) - -extern const uint32_t gIEEENotANumber; -extern const uint32_t gIEEEInfinity; -extern const uint32_t gIEEENegativeInfinity; - -#define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) -#define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) -#define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity)) - -// We forward declare this to break an #include cycle. -// (SkScalar -> SkFloatingPoint -> SkOpts.h -> SkXfermode -> SkColor -> SkScalar) -namespace SkOpts { extern float (*rsqrt)(float); } - -// Fast, approximate inverse square root. -// Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON. -static inline float sk_float_rsqrt(const float x) { -// We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got -// it at compile time. This is going to be too fast to productively hide behind a function pointer. -// -// We do one step of Newton's method to refine the estimates in the NEON and null paths. No -// refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt. -// -// Optimized constants in the null path courtesy of http://rrrola.wz.cz/inv_sqrt.html -#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 - return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); -#elif defined(SK_ARM_HAS_NEON) - // Get initial estimate. - const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x. - float32x2_t estimate = vrsqrte_f32(xx); - - // One step of Newton's method to refine. - const float32x2_t estimate_sq = vmul_f32(estimate, estimate); - estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); - return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places. -#else - // Perhaps runtime-detected NEON, or a portable fallback. - return SkOpts::rsqrt(x); -#endif -} - -// This is the number of significant digits we can print in a string such that when we read that -// string back we get the floating point number we expect. The minimum value C requires is 6, but -// most compilers support 9 -#ifdef FLT_DECIMAL_DIG -#define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG -#else -#define SK_FLT_DECIMAL_DIG 9 -#endif - -#endif diff --git a/include/core/SkPicture.h b/include/core/SkPicture.h index 358faf9524..baef45ded0 100644 --- a/include/core/SkPicture.h +++ b/include/core/SkPicture.h @@ -9,7 +9,6 @@ #define SkPicture_DEFINED #include "SkImageDecoder.h" -#include "SkLazyPtr.h" #include "SkRefCnt.h" #include "SkTypes.h" diff --git a/include/core/SkScalar.h b/include/core/SkScalar.h index 25d1c611cd..71575a4920 100644 --- a/include/core/SkScalar.h +++ b/include/core/SkScalar.h @@ -9,7 +9,7 @@ #define SkScalar_DEFINED #include "SkFixed.h" -#include "SkFloatingPoint.h" +#include "../private/SkFloatingPoint.h" // TODO: move this sort of check into SkPostConfig.h #define SK_SCALAR_IS_DOUBLE 0 diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index a9204a6df9..863b8abac5 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -14,7 +14,7 @@ #include "SkLazyPtr.h" #include "SkRect.h" #include "SkString.h" -#include "SkWeakRefCnt.h" +#include "../private/SkWeakRefCnt.h" class SkDescriptor; class SkFontData; diff --git a/include/core/SkWeakRefCnt.h b/include/core/SkWeakRefCnt.h deleted file mode 100644 index a550951970..0000000000 --- a/include/core/SkWeakRefCnt.h +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright 2012 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -#ifndef SkWeakRefCnt_DEFINED -#define SkWeakRefCnt_DEFINED - -#include "SkRefCnt.h" -#include "SkAtomics.h" - -/** \class SkWeakRefCnt - - SkWeakRefCnt is the base class for objects that may be shared by multiple - objects. When an existing strong owner wants to share a reference, it calls - ref(). When a strong owner wants to release its reference, it calls - unref(). When the shared object's strong reference count goes to zero as - the result of an unref() call, its (virtual) weak_dispose method is called. - It is an error for the destructor to be called explicitly (or via the - object going out of scope on the stack or calling delete) if - getRefCnt() > 1. - - In addition to strong ownership, an owner may instead obtain a weak - reference by calling weak_ref(). A call to weak_ref() must be balanced by a - call to weak_unref(). To obtain a strong reference from a weak reference, - call try_ref(). If try_ref() returns true, the owner's pointer is now also - a strong reference on which unref() must be called. Note that this does not - affect the original weak reference, weak_unref() must still be called. When - the weak reference count goes to zero, the object is deleted. While the - weak reference count is positive and the strong reference count is zero the - object still exists, but will be in the disposed state. It is up to the - object to define what this means. - - Note that a strong reference implicitly implies a weak reference. As a - result, it is allowable for the owner of a strong ref to call try_ref(). - This will have the same effect as calling ref(), but may be more expensive. - - Example: - - SkWeakRefCnt myRef = strongRef.weak_ref(); - ... // strongRef.unref() may or may not be called - if (myRef.try_ref()) { - ... // use myRef - myRef.unref(); - } else { - // myRef is in the disposed state - } - myRef.weak_unref(); -*/ -class SK_API SkWeakRefCnt : public SkRefCnt { -public: - /** Default construct, initializing the reference counts to 1. - The strong references collectively hold one weak reference. When the - strong reference count goes to zero, the collectively held weak - reference is released. - */ - SkWeakRefCnt() : SkRefCnt(), fWeakCnt(1) {} - - /** Destruct, asserting that the weak reference count is 1. - */ - virtual ~SkWeakRefCnt() { -#ifdef SK_DEBUG - SkASSERT(fWeakCnt == 1); - fWeakCnt = 0; -#endif - } - - /** Return the weak reference count. - */ - int32_t getWeakCnt() const { return fWeakCnt; } - -#ifdef SK_DEBUG - void validate() const { - this->INHERITED::validate(); - SkASSERT(fWeakCnt > 0); - } -#endif - - /** Creates a strong reference from a weak reference, if possible. The - caller must already be an owner. If try_ref() returns true the owner - is in posession of an additional strong reference. Both the original - reference and new reference must be properly unreferenced. If try_ref() - returns false, no strong reference could be created and the owner's - reference is in the same state as before the call. - */ - bool SK_WARN_UNUSED_RESULT try_ref() const { - if (sk_atomic_conditional_inc(&fRefCnt) != 0) { - // Acquire barrier (L/SL), if not provided above. - // Prevents subsequent code from happening before the increment. - sk_membar_acquire__after_atomic_conditional_inc(); - return true; - } - return false; - } - - /** Increment the weak reference count. Must be balanced by a call to - weak_unref(). - */ - void weak_ref() const { - SkASSERT(fRefCnt > 0); - SkASSERT(fWeakCnt > 0); - sk_atomic_inc(&fWeakCnt); // No barrier required. - } - - /** Decrement the weak reference count. If the weak reference count is 1 - before the decrement, then call delete on the object. Note that if this - is the case, then the object needs to have been allocated via new, and - not on the stack. - */ - void weak_unref() const { - SkASSERT(fWeakCnt > 0); - // Release barrier (SL/S), if not provided below. - if (sk_atomic_dec(&fWeakCnt) == 1) { - // Acquire barrier (L/SL), if not provided above. - // Prevents code in destructor from happening before the decrement. - sk_membar_acquire__after_atomic_dec(); -#ifdef SK_DEBUG - // so our destructor won't complain - fWeakCnt = 1; -#endif - this->INHERITED::internal_dispose(); - } - } - - /** Returns true if there are no strong references to the object. When this - is the case all future calls to try_ref() will return false. - */ - bool weak_expired() const { - return fRefCnt == 0; - } - -protected: - /** Called when the strong reference count goes to zero. This allows the - object to free any resources it may be holding. Weak references may - still exist and their level of allowed access to the object is defined - by the object's class. - */ - virtual void weak_dispose() const { - } - -private: - /** Called when the strong reference count goes to zero. Calls weak_dispose - on the object and releases the implicit weak reference held - collectively by the strong references. - */ - void internal_dispose() const override { - weak_dispose(); - weak_unref(); - } - - /* Invariant: fWeakCnt = #weak + (fRefCnt > 0 ? 1 : 0) */ - mutable int32_t fWeakCnt; - - typedef SkRefCnt INHERITED; -}; - -#endif |