epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkTypes_DEFINED |
| 11 | #define SkTypes_DEFINED |
| 12 | |
| 13 | #include "SkPreConfig.h" |
| 14 | #include "SkUserConfig.h" |
| 15 | #include "SkPostConfig.h" |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame^] | 16 | #include <stdint.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
| 18 | /** \file SkTypes.h |
| 19 | */ |
| 20 | |
reed@android.com | 9aa8b32 | 2010-04-13 13:22:54 +0000 | [diff] [blame] | 21 | /** See SkGraphics::GetVersion() to retrieve these at runtime |
| 22 | */ |
| 23 | #define SKIA_VERSION_MAJOR 1 |
| 24 | #define SKIA_VERSION_MINOR 0 |
| 25 | #define SKIA_VERSION_PATCH 0 |
| 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | /* |
| 28 | memory wrappers to be implemented by the porting layer (platform) |
| 29 | */ |
| 30 | |
| 31 | /** Called internally if we run out of memory. The platform implementation must |
| 32 | not return, but should either throw an exception or otherwise exit. |
| 33 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 34 | SK_API extern void sk_out_of_memory(void); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | /** Called internally if we hit an unrecoverable error. |
| 36 | The platform implementation must not return, but should either throw |
| 37 | an exception or otherwise exit. |
| 38 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 39 | SK_API extern void sk_throw(void); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | |
| 41 | enum { |
| 42 | SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame |
| 43 | SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. |
| 44 | }; |
| 45 | /** Return a block of memory (at least 4-byte aligned) of at least the |
| 46 | specified size. If the requested memory cannot be returned, either |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 47 | return null (if SK_MALLOC_TEMP bit is clear) or throw an exception |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). |
| 49 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 50 | SK_API extern void* sk_malloc_flags(size_t size, unsigned flags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | /** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag |
| 52 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 53 | SK_API extern void* sk_malloc_throw(size_t size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | /** Same as standard realloc(), but this one never returns null on failure. It will throw |
| 55 | an exception if it fails. |
| 56 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 57 | SK_API extern void* sk_realloc_throw(void* buffer, size_t size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | /** Free memory returned by sk_malloc(). It is safe to pass null. |
| 59 | */ |
reed@google.com | de916c8 | 2011-10-19 19:50:48 +0000 | [diff] [blame] | 60 | SK_API extern void sk_free(void*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | |
mtklein@google.com | 519f967 | 2013-09-20 14:31:45 +0000 | [diff] [blame] | 62 | /** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure. |
| 63 | */ |
| 64 | SK_API extern void* sk_calloc(size_t size); |
| 65 | |
| 66 | /** Same as sk_calloc, but throws an exception instead of returning NULL on failure. |
| 67 | */ |
| 68 | SK_API extern void* sk_calloc_throw(size_t size); |
| 69 | |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 70 | // bzero is safer than memset, but we can't rely on it, so... sk_bzero() |
| 71 | static inline void sk_bzero(void* buffer, size_t size) { |
| 72 | memset(buffer, 0, size); |
| 73 | } |
| 74 | |
reed@google.com | bdf7361 | 2011-09-06 14:56:20 +0000 | [diff] [blame] | 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | |
| 77 | #ifdef SK_OVERRIDE_GLOBAL_NEW |
| 78 | #include <new> |
| 79 | |
| 80 | inline void* operator new(size_t size) { |
| 81 | return sk_malloc_throw(size); |
| 82 | } |
| 83 | |
| 84 | inline void operator delete(void* p) { |
| 85 | sk_free(p); |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | |
| 91 | #define SK_INIT_TO_AVOID_WARNING = 0 |
| 92 | |
| 93 | #ifndef SkDebugf |
| 94 | void SkDebugf(const char format[], ...); |
| 95 | #endif |
| 96 | |
| 97 | #ifdef SK_DEBUG |
| 98 | #define SkASSERT(cond) SK_DEBUGBREAK(cond) |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 99 | #define SkDEBUGFAIL(message) SkASSERT(false && message) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | #define SkDEBUGCODE(code) code |
| 101 | #define SkDECLAREPARAM(type, var) , type var |
| 102 | #define SkPARAM(var) , var |
| 103 | // #define SkDEBUGF(args ) SkDebugf##args |
| 104 | #define SkDEBUGF(args ) SkDebugf args |
| 105 | #define SkAssertResult(cond) SkASSERT(cond) |
| 106 | #else |
| 107 | #define SkASSERT(cond) |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 108 | #define SkDEBUGFAIL(message) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | #define SkDEBUGCODE(code) |
| 110 | #define SkDEBUGF(args) |
| 111 | #define SkDECLAREPARAM(type, var) |
| 112 | #define SkPARAM(var) |
| 113 | |
| 114 | // unlike SkASSERT, this guy executes its condition in the non-debug build |
| 115 | #define SkAssertResult(cond) cond |
| 116 | #endif |
| 117 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 118 | #ifdef SK_DEVELOPER |
| 119 | #define SkDEVCODE(code) code |
| 120 | // the 'toString' helper functions convert Sk* objects to human-readable |
| 121 | // form in developer mode |
| 122 | #define SK_DEVELOPER_TO_STRING() virtual void toString(SkString* str) const SK_OVERRIDE; |
| 123 | #else |
| 124 | #define SkDEVCODE(code) |
| 125 | #define SK_DEVELOPER_TO_STRING() |
| 126 | #endif |
| 127 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 128 | template <bool> |
| 129 | struct SkCompileAssert { |
| 130 | }; |
| 131 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 132 | #define SK_COMPILE_ASSERT(expr, msg) \ |
bungeman@google.com | 5ebbbe5 | 2013-08-06 18:32:29 +0000 | [diff] [blame] | 133 | typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] SK_UNUSED |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 134 | |
reed@google.com | 49a5b19 | 2012-10-25 17:31:39 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab |
| 137 | * |
| 138 | * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly |
| 139 | * |
| 140 | */ |
| 141 | #define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) |
| 142 | #define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y |
| 143 | |
| 144 | /* |
| 145 | * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current |
| 146 | * line number. Easy way to construct |
| 147 | * unique names for local functions or |
| 148 | * variables. |
| 149 | */ |
| 150 | #define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) |
| 151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////// |
| 153 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 154 | /** |
| 155 | * Fast type for signed 8 bits. Use for parameter passing and local variables, |
| 156 | * not for storage. |
| 157 | */ |
| 158 | typedef int S8CPU; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 160 | /** |
| 161 | * Fast type for unsigned 8 bits. Use for parameter passing and local |
| 162 | * variables, not for storage |
| 163 | */ |
| 164 | typedef unsigned U8CPU; |
| 165 | |
| 166 | /** |
| 167 | * Fast type for signed 16 bits. Use for parameter passing and local variables, |
| 168 | * not for storage |
| 169 | */ |
| 170 | typedef int S16CPU; |
| 171 | |
| 172 | /** |
| 173 | * Fast type for unsigned 16 bits. Use for parameter passing and local |
| 174 | * variables, not for storage |
| 175 | */ |
| 176 | typedef unsigned U16CPU; |
| 177 | |
| 178 | /** |
| 179 | * Meant to be faster than bool (doesn't promise to be 0 or 1, |
| 180 | * just 0 or non-zero |
| 181 | */ |
| 182 | typedef int SkBool; |
| 183 | |
| 184 | /** |
| 185 | * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 |
| 186 | */ |
| 187 | typedef uint8_t SkBool8; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 188 | |
| 189 | #ifdef SK_DEBUG |
bungeman@google.com | 777ded0 | 2013-07-19 22:30:11 +0000 | [diff] [blame] | 190 | SK_API int8_t SkToS8(intmax_t); |
| 191 | SK_API uint8_t SkToU8(uintmax_t); |
| 192 | SK_API int16_t SkToS16(intmax_t); |
| 193 | SK_API uint16_t SkToU16(uintmax_t); |
| 194 | SK_API int32_t SkToS32(intmax_t); |
| 195 | SK_API uint32_t SkToU32(uintmax_t); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | #else |
| 197 | #define SkToS8(x) ((int8_t)(x)) |
| 198 | #define SkToU8(x) ((uint8_t)(x)) |
| 199 | #define SkToS16(x) ((int16_t)(x)) |
| 200 | #define SkToU16(x) ((uint16_t)(x)) |
| 201 | #define SkToS32(x) ((int32_t)(x)) |
| 202 | #define SkToU32(x) ((uint32_t)(x)) |
| 203 | #endif |
| 204 | |
| 205 | /** Returns 0 or 1 based on the condition |
| 206 | */ |
| 207 | #define SkToBool(cond) ((cond) != 0) |
| 208 | |
| 209 | #define SK_MaxS16 32767 |
| 210 | #define SK_MinS16 -32767 |
| 211 | #define SK_MaxU16 0xFFFF |
| 212 | #define SK_MinU16 0 |
| 213 | #define SK_MaxS32 0x7FFFFFFF |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 214 | #define SK_MinS32 -SK_MaxS32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | #define SK_MaxU32 0xFFFFFFFF |
| 216 | #define SK_MinU32 0 |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 217 | #define SK_NaN32 (1 << 31) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 219 | /** Returns true if the value can be represented with signed 16bits |
| 220 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 221 | static inline bool SkIsS16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 222 | return (int16_t)x == x; |
| 223 | } |
| 224 | |
| 225 | /** Returns true if the value can be represented with unsigned 16bits |
| 226 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 227 | static inline bool SkIsU16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 228 | return (uint16_t)x == x; |
| 229 | } |
| 230 | |
| 231 | ////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 232 | #ifndef SK_OFFSETOF |
reed@google.com | 56d3a23 | 2012-03-21 12:25:48 +0000 | [diff] [blame] | 233 | #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
| 236 | /** Returns the number of entries in an array (not a pointer) |
| 237 | */ |
| 238 | #define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) |
| 239 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | #define SkAlign2(x) (((x) + 1) >> 1 << 1) |
reed@google.com | c6faa5a | 2012-06-27 15:07:11 +0000 | [diff] [blame] | 241 | #define SkIsAlign2(x) (0 == ((x) & 1)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 242 | |
reed@google.com | c6faa5a | 2012-06-27 15:07:11 +0000 | [diff] [blame] | 243 | #define SkAlign4(x) (((x) + 3) >> 2 << 2) |
| 244 | #define SkIsAlign4(x) (0 == ((x) & 3)) |
| 245 | |
| 246 | #define SkAlign8(x) (((x) + 7) >> 3 << 3) |
| 247 | #define SkIsAlign8(x) (0 == ((x) & 7)) |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 248 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 249 | typedef uint32_t SkFourByteTag; |
| 250 | #define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) |
| 251 | |
| 252 | /** 32 bit integer to hold a unicode value |
| 253 | */ |
| 254 | typedef int32_t SkUnichar; |
| 255 | /** 32 bit value to hold a millisecond count |
| 256 | */ |
| 257 | typedef uint32_t SkMSec; |
| 258 | /** 1 second measured in milliseconds |
| 259 | */ |
| 260 | #define SK_MSec1 1000 |
| 261 | /** maximum representable milliseconds |
| 262 | */ |
| 263 | #define SK_MSecMax 0x7FFFFFFF |
| 264 | /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 265 | */ |
| 266 | #define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) |
| 267 | /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 268 | */ |
| 269 | #define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) |
| 270 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | /**************************************************************************** |
| 272 | The rest of these only build with C++ |
| 273 | */ |
| 274 | #ifdef __cplusplus |
| 275 | |
| 276 | /** Faster than SkToBool for integral conditions. Returns 0 or 1 |
| 277 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 278 | static inline int Sk32ToBool(uint32_t n) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | return (n | (0-n)) >> 31; |
| 280 | } |
| 281 | |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 282 | /** Generic swap function. Classes with efficient swaps should specialize this function to take |
| 283 | their fast path. This function is used by SkTSort. */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 284 | template <typename T> inline void SkTSwap(T& a, T& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 285 | T c(a); |
| 286 | a = b; |
| 287 | b = c; |
| 288 | } |
| 289 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 290 | static inline int32_t SkAbs32(int32_t value) { |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 291 | if (value < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | value = -value; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 293 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | return value; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | } |
| 296 | |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 297 | template <typename T> inline T SkTAbs(T value) { |
| 298 | if (value < 0) { |
| 299 | value = -value; |
| 300 | } |
| 301 | return value; |
| 302 | } |
| 303 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 304 | static inline int32_t SkMax32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 305 | if (a < b) |
| 306 | a = b; |
| 307 | return a; |
| 308 | } |
| 309 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 310 | static inline int32_t SkMin32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | if (a > b) |
| 312 | a = b; |
| 313 | return a; |
| 314 | } |
| 315 | |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 316 | template <typename T> const T& SkTMin(const T& a, const T& b) { |
| 317 | return (a < b) ? a : b; |
| 318 | } |
| 319 | |
| 320 | template <typename T> const T& SkTMax(const T& a, const T& b) { |
| 321 | return (b < a) ? a : b; |
| 322 | } |
| 323 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 324 | static inline int32_t SkSign32(int32_t a) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 325 | return (a >> 31) | ((unsigned) -a >> 31); |
| 326 | } |
| 327 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 328 | static inline int32_t SkFastMin32(int32_t value, int32_t max) { |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 329 | if (value > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | value = max; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 331 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | return value; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** Returns signed 32 bit value pinned between min and max, inclusively |
| 336 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 337 | static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 338 | if (value < min) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | value = min; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 340 | } |
| 341 | if (value > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | value = max; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 343 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 344 | return value; |
| 345 | } |
| 346 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 347 | static inline uint32_t SkSetClearShift(uint32_t bits, bool cond, |
| 348 | unsigned shift) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | SkASSERT((int)cond == 0 || (int)cond == 1); |
| 350 | return (bits & ~(1 << shift)) | ((int)cond << shift); |
| 351 | } |
| 352 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 353 | static inline uint32_t SkSetClearMask(uint32_t bits, bool cond, |
| 354 | uint32_t mask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | return cond ? bits | mask : bits & ~mask; |
| 356 | } |
| 357 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 358 | /////////////////////////////////////////////////////////////////////////////// |
| 359 | |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 360 | /** Use to combine multiple bits in a bitmask in a type safe way. |
| 361 | */ |
| 362 | template <typename T> |
| 363 | T SkTBitOr(T a, T b) { |
| 364 | return (T)(a | b); |
| 365 | } |
| 366 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 367 | /** |
| 368 | * Use to cast a pointer to a different type, and maintaining strict-aliasing |
| 369 | */ |
| 370 | template <typename Dst> Dst SkTCast(const void* ptr) { |
| 371 | union { |
| 372 | const void* src; |
| 373 | Dst dst; |
| 374 | } data; |
| 375 | data.src = ptr; |
| 376 | return data.dst; |
| 377 | } |
| 378 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | ////////////////////////////////////////////////////////////////////////////// |
| 380 | |
| 381 | /** \class SkNoncopyable |
| 382 | |
| 383 | SkNoncopyable is the base class for objects that may do not want to |
| 384 | be copied. It hides its copy-constructor and its assignment-operator. |
| 385 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 386 | class SK_API SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 387 | public: |
| 388 | SkNoncopyable() {} |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 389 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 390 | private: |
| 391 | SkNoncopyable(const SkNoncopyable&); |
| 392 | SkNoncopyable& operator=(const SkNoncopyable&); |
| 393 | }; |
| 394 | |
| 395 | class SkAutoFree : SkNoncopyable { |
| 396 | public: |
| 397 | SkAutoFree() : fPtr(NULL) {} |
| 398 | explicit SkAutoFree(void* ptr) : fPtr(ptr) {} |
| 399 | ~SkAutoFree() { sk_free(fPtr); } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 400 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 401 | /** Return the currently allocate buffer, or null |
| 402 | */ |
| 403 | void* get() const { return fPtr; } |
| 404 | |
| 405 | /** Assign a new ptr allocated with sk_malloc (or null), and return the |
| 406 | previous ptr. Note it is the caller's responsibility to sk_free the |
| 407 | returned ptr. |
| 408 | */ |
| 409 | void* set(void* ptr) { |
| 410 | void* prev = fPtr; |
| 411 | fPtr = ptr; |
| 412 | return prev; |
| 413 | } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 414 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 415 | /** Transfer ownership of the current ptr to the caller, setting the |
| 416 | internal reference to null. Note the caller is reponsible for calling |
| 417 | sk_free on the returned address. |
| 418 | */ |
| 419 | void* detach() { return this->set(NULL); } |
| 420 | |
| 421 | /** Free the current buffer, and set the internal reference to NULL. Same |
| 422 | as calling sk_free(detach()) |
| 423 | */ |
| 424 | void free() { |
| 425 | sk_free(fPtr); |
| 426 | fPtr = NULL; |
| 427 | } |
| 428 | |
| 429 | private: |
| 430 | void* fPtr; |
| 431 | // illegal |
| 432 | SkAutoFree(const SkAutoFree&); |
| 433 | SkAutoFree& operator=(const SkAutoFree&); |
| 434 | }; |
| 435 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 436 | /** |
| 437 | * Manage an allocated block of heap memory. This object is the sole manager of |
| 438 | * the lifetime of the block, so the caller must not call sk_free() or delete |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 439 | * on the block, unless detach() was called. |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 440 | */ |
| 441 | class SkAutoMalloc : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | public: |
reed@google.com | 3ab4195 | 2011-10-18 18:32:46 +0000 | [diff] [blame] | 443 | explicit SkAutoMalloc(size_t size = 0) { |
| 444 | fPtr = size ? sk_malloc_throw(size) : NULL; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 445 | fSize = size; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | } |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 447 | |
| 448 | ~SkAutoMalloc() { |
| 449 | sk_free(fPtr); |
| 450 | } |
| 451 | |
| 452 | /** |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 453 | * Passed to reset to specify what happens if the requested size is smaller |
| 454 | * than the current size (and the current block was dynamically allocated). |
| 455 | */ |
| 456 | enum OnShrink { |
| 457 | /** |
| 458 | * If the requested size is smaller than the current size, and the |
| 459 | * current block is dynamically allocated, free the old block and |
| 460 | * malloc a new block of the smaller size. |
| 461 | */ |
| 462 | kAlloc_OnShrink, |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 463 | |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 464 | /** |
| 465 | * If the requested size is smaller than the current size, and the |
| 466 | * current block is dynamically allocated, just return the old |
| 467 | * block. |
| 468 | */ |
tomhudson@google.com | 1f90287 | 2012-06-01 13:15:47 +0000 | [diff] [blame] | 469 | kReuse_OnShrink |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | /** |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 473 | * Reallocates the block to a new size. The ptr may or may not change. |
| 474 | */ |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 475 | void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) { |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 476 | if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) { |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 477 | if (NULL != didChangeAlloc) { |
| 478 | *didChangeAlloc = false; |
| 479 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 480 | return fPtr; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 481 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 482 | |
| 483 | sk_free(fPtr); |
| 484 | fPtr = size ? sk_malloc_throw(size) : NULL; |
| 485 | fSize = size; |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 486 | if (NULL != didChangeAlloc) { |
| 487 | *didChangeAlloc = true; |
| 488 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 489 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 490 | return fPtr; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Releases the block back to the heap |
| 495 | */ |
| 496 | void free() { |
| 497 | this->reset(0); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Return the allocated block. |
| 502 | */ |
| 503 | void* get() { return fPtr; } |
| 504 | const void* get() const { return fPtr; } |
| 505 | |
bsalomon@google.com | 6dcd27c | 2011-09-06 15:02:33 +0000 | [diff] [blame] | 506 | /** Transfer ownership of the current ptr to the caller, setting the |
| 507 | internal reference to null. Note the caller is reponsible for calling |
| 508 | sk_free on the returned address. |
| 509 | */ |
| 510 | void* detach() { |
| 511 | void* ptr = fPtr; |
| 512 | fPtr = NULL; |
| 513 | fSize = 0; |
| 514 | return ptr; |
| 515 | } |
| 516 | |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 517 | private: |
| 518 | void* fPtr; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 519 | size_t fSize; // can be larger than the requested size (see kReuse) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 520 | }; |
| 521 | |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 522 | /** |
| 523 | * Manage an allocated block of memory. If the requested size is <= kSize, then |
| 524 | * the allocation will come from the stack rather than the heap. This object |
| 525 | * is the sole manager of the lifetime of the block, so the caller must not |
| 526 | * call sk_free() or delete on the block. |
| 527 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | template <size_t kSize> class SkAutoSMalloc : SkNoncopyable { |
| 529 | public: |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 530 | /** |
| 531 | * Creates initially empty storage. get() returns a ptr, but it is to |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 532 | * a zero-byte allocation. Must call reset(size) to return an allocated |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 533 | * block. |
| 534 | */ |
| 535 | SkAutoSMalloc() { |
| 536 | fPtr = fStorage; |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 537 | fSize = kSize; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 538 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 539 | |
| 540 | /** |
| 541 | * Allocate a block of the specified size. If size <= kSize, then the |
| 542 | * allocation will come from the stack, otherwise it will be dynamically |
| 543 | * allocated. |
| 544 | */ |
| 545 | explicit SkAutoSMalloc(size_t size) { |
| 546 | fPtr = fStorage; |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 547 | fSize = kSize; |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 548 | this->reset(size); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Free the allocated block (if any). If the block was small enought to |
| 553 | * have been allocated on the stack (size <= kSize) then this does nothing. |
| 554 | */ |
| 555 | ~SkAutoSMalloc() { |
| 556 | if (fPtr != (void*)fStorage) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 557 | sk_free(fPtr); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 558 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 559 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 560 | |
| 561 | /** |
| 562 | * Return the allocated block. May return non-null even if the block is |
| 563 | * of zero size. Since this may be on the stack or dynamically allocated, |
| 564 | * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc |
| 565 | * to manage it. |
| 566 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 567 | void* get() const { return fPtr; } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 568 | |
| 569 | /** |
| 570 | * Return a new block of the requested size, freeing (as necessary) any |
| 571 | * previously allocated block. As with the constructor, if size <= kSize |
| 572 | * then the return block may be allocated locally, rather than from the |
| 573 | * heap. |
| 574 | */ |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 575 | void* reset(size_t size, |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 576 | SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink, |
| 577 | bool* didChangeAlloc = NULL) { |
| 578 | size = (size < kSize) ? kSize : size; |
robertphillips@google.com | 0f2b195 | 2013-05-23 14:59:40 +0000 | [diff] [blame] | 579 | bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize); |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 580 | if (NULL != didChangeAlloc) { |
| 581 | *didChangeAlloc = alloc; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 582 | } |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 583 | if (alloc) { |
| 584 | if (fPtr != (void*)fStorage) { |
| 585 | sk_free(fPtr); |
| 586 | } |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 587 | |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 588 | if (size == kSize) { |
| 589 | SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc. |
| 590 | fPtr = fStorage; |
| 591 | } else { |
| 592 | fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); |
| 593 | } |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 594 | |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 595 | fSize = size; |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 596 | } |
bsalomon@google.com | 9eb6645 | 2013-05-22 13:35:37 +0000 | [diff] [blame] | 597 | SkASSERT(fSize >= size && fSize >= kSize); |
| 598 | SkASSERT((fPtr == fStorage) || fSize > kSize); |
reed@google.com | 63a6060 | 2011-03-10 13:07:35 +0000 | [diff] [blame] | 599 | return fPtr; |
| 600 | } |
| 601 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 602 | private: |
| 603 | void* fPtr; |
reed@google.com | 1c401d8 | 2011-10-18 18:52:03 +0000 | [diff] [blame] | 604 | size_t fSize; // can be larger than the requested size (see kReuse) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 605 | uint32_t fStorage[(kSize + 3) >> 2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | #endif /* C++ */ |
| 609 | |
| 610 | #endif |