reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkTypes_DEFINED |
| 18 | #define SkTypes_DEFINED |
| 19 | |
| 20 | #include "SkPreConfig.h" |
| 21 | #include "SkUserConfig.h" |
| 22 | #include "SkPostConfig.h" |
| 23 | |
| 24 | #ifndef SK_IGNORE_STDINT_DOT_H |
| 25 | #include <stdint.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | /** \file SkTypes.h |
| 31 | */ |
| 32 | |
reed@android.com | 9aa8b32 | 2010-04-13 13:22:54 +0000 | [diff] [blame] | 33 | /** See SkGraphics::GetVersion() to retrieve these at runtime |
| 34 | */ |
| 35 | #define SKIA_VERSION_MAJOR 1 |
| 36 | #define SKIA_VERSION_MINOR 0 |
| 37 | #define SKIA_VERSION_PATCH 0 |
| 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | /* |
| 40 | memory wrappers to be implemented by the porting layer (platform) |
| 41 | */ |
| 42 | |
| 43 | /** Called internally if we run out of memory. The platform implementation must |
| 44 | not return, but should either throw an exception or otherwise exit. |
| 45 | */ |
| 46 | extern void sk_out_of_memory(void); |
| 47 | /** Called internally if we hit an unrecoverable error. |
| 48 | The platform implementation must not return, but should either throw |
| 49 | an exception or otherwise exit. |
| 50 | */ |
| 51 | extern void sk_throw(void); |
| 52 | |
| 53 | enum { |
| 54 | SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame |
| 55 | SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. |
| 56 | }; |
| 57 | /** Return a block of memory (at least 4-byte aligned) of at least the |
| 58 | specified size. If the requested memory cannot be returned, either |
| 59 | return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw() |
| 60 | (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). |
| 61 | */ |
| 62 | extern void* sk_malloc_flags(size_t size, unsigned flags); |
| 63 | /** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag |
| 64 | */ |
| 65 | extern void* sk_malloc_throw(size_t size); |
| 66 | /** Same as standard realloc(), but this one never returns null on failure. It will throw |
| 67 | an exception if it fails. |
| 68 | */ |
| 69 | extern void* sk_realloc_throw(void* buffer, size_t size); |
| 70 | /** Free memory returned by sk_malloc(). It is safe to pass null. |
| 71 | */ |
| 72 | extern void sk_free(void*); |
| 73 | |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 74 | // bzero is safer than memset, but we can't rely on it, so... sk_bzero() |
| 75 | static inline void sk_bzero(void* buffer, size_t size) { |
| 76 | memset(buffer, 0, size); |
| 77 | } |
| 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | /////////////////////////////////////////////////////////////////////// |
| 80 | |
| 81 | #define SK_INIT_TO_AVOID_WARNING = 0 |
| 82 | |
| 83 | #ifndef SkDebugf |
| 84 | void SkDebugf(const char format[], ...); |
| 85 | #endif |
| 86 | |
| 87 | #ifdef SK_DEBUG |
| 88 | #define SkASSERT(cond) SK_DEBUGBREAK(cond) |
| 89 | #define SkDEBUGCODE(code) code |
| 90 | #define SkDECLAREPARAM(type, var) , type var |
| 91 | #define SkPARAM(var) , var |
| 92 | // #define SkDEBUGF(args ) SkDebugf##args |
| 93 | #define SkDEBUGF(args ) SkDebugf args |
| 94 | #define SkAssertResult(cond) SkASSERT(cond) |
| 95 | #else |
| 96 | #define SkASSERT(cond) |
| 97 | #define SkDEBUGCODE(code) |
| 98 | #define SkDEBUGF(args) |
| 99 | #define SkDECLAREPARAM(type, var) |
| 100 | #define SkPARAM(var) |
| 101 | |
| 102 | // unlike SkASSERT, this guy executes its condition in the non-debug build |
| 103 | #define SkAssertResult(cond) cond |
| 104 | #endif |
| 105 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 106 | namespace { |
| 107 | |
| 108 | template <bool> |
| 109 | struct SkCompileAssert { |
| 110 | }; |
| 111 | |
| 112 | } // namespace |
| 113 | |
| 114 | #define SK_COMPILE_ASSERT(expr, msg) \ |
| 115 | typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] |
| 116 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | /////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | /** Fast type for signed 8 bits. Use for parameter passing and local variables, not for storage |
| 120 | */ |
| 121 | typedef int S8CPU; |
| 122 | /** Fast type for unsigned 8 bits. Use for parameter passing and local variables, not for storage |
| 123 | */ |
| 124 | typedef int S16CPU; |
| 125 | /** Fast type for signed 16 bits. Use for parameter passing and local variables, not for storage |
| 126 | */ |
| 127 | typedef unsigned U8CPU; |
| 128 | /** Fast type for unsigned 16 bits. Use for parameter passing and local variables, not for storage |
| 129 | */ |
| 130 | typedef unsigned U16CPU; |
| 131 | |
| 132 | /** Meant to be faster than bool (doesn't promise to be 0 or 1, just 0 or non-zero |
| 133 | */ |
| 134 | typedef int SkBool; |
| 135 | /** Meant to be a small version of bool, for storage purposes. Will be 0 or 1 |
| 136 | */ |
| 137 | typedef uint8_t SkBool8; |
| 138 | |
| 139 | #ifdef SK_DEBUG |
| 140 | int8_t SkToS8(long); |
| 141 | uint8_t SkToU8(size_t); |
| 142 | int16_t SkToS16(long); |
| 143 | uint16_t SkToU16(size_t); |
| 144 | int32_t SkToS32(long); |
| 145 | uint32_t SkToU32(size_t); |
| 146 | #else |
| 147 | #define SkToS8(x) ((int8_t)(x)) |
| 148 | #define SkToU8(x) ((uint8_t)(x)) |
| 149 | #define SkToS16(x) ((int16_t)(x)) |
| 150 | #define SkToU16(x) ((uint16_t)(x)) |
| 151 | #define SkToS32(x) ((int32_t)(x)) |
| 152 | #define SkToU32(x) ((uint32_t)(x)) |
| 153 | #endif |
| 154 | |
| 155 | /** Returns 0 or 1 based on the condition |
| 156 | */ |
| 157 | #define SkToBool(cond) ((cond) != 0) |
| 158 | |
| 159 | #define SK_MaxS16 32767 |
| 160 | #define SK_MinS16 -32767 |
| 161 | #define SK_MaxU16 0xFFFF |
| 162 | #define SK_MinU16 0 |
| 163 | #define SK_MaxS32 0x7FFFFFFF |
| 164 | #define SK_MinS32 0x80000001 |
| 165 | #define SK_MaxU32 0xFFFFFFFF |
| 166 | #define SK_MinU32 0 |
| 167 | #define SK_NaN32 0x80000000 |
| 168 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 169 | /** Returns true if the value can be represented with signed 16bits |
| 170 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 171 | static inline bool SkIsS16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 172 | return (int16_t)x == x; |
| 173 | } |
| 174 | |
| 175 | /** Returns true if the value can be represented with unsigned 16bits |
| 176 | */ |
reed@android.com | 90209ca | 2009-11-21 19:58:04 +0000 | [diff] [blame] | 177 | static inline bool SkIsU16(long x) { |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 178 | return (uint16_t)x == x; |
| 179 | } |
| 180 | |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | #ifndef SK_OFFSETOF |
| 183 | #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1) |
| 184 | #endif |
| 185 | |
| 186 | /** Returns the number of entries in an array (not a pointer) |
| 187 | */ |
| 188 | #define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) |
| 189 | |
| 190 | /** Returns x rounded up to a multiple of 2 |
| 191 | */ |
| 192 | #define SkAlign2(x) (((x) + 1) >> 1 << 1) |
| 193 | /** Returns x rounded up to a multiple of 4 |
| 194 | */ |
| 195 | #define SkAlign4(x) (((x) + 3) >> 2 << 2) |
| 196 | |
| 197 | typedef uint32_t SkFourByteTag; |
| 198 | #define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) |
| 199 | |
| 200 | /** 32 bit integer to hold a unicode value |
| 201 | */ |
| 202 | typedef int32_t SkUnichar; |
| 203 | /** 32 bit value to hold a millisecond count |
| 204 | */ |
| 205 | typedef uint32_t SkMSec; |
| 206 | /** 1 second measured in milliseconds |
| 207 | */ |
| 208 | #define SK_MSec1 1000 |
| 209 | /** maximum representable milliseconds |
| 210 | */ |
| 211 | #define SK_MSecMax 0x7FFFFFFF |
| 212 | /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 213 | */ |
| 214 | #define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) |
| 215 | /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 216 | */ |
| 217 | #define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) |
| 218 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | /**************************************************************************** |
| 220 | The rest of these only build with C++ |
| 221 | */ |
| 222 | #ifdef __cplusplus |
| 223 | |
| 224 | /** Faster than SkToBool for integral conditions. Returns 0 or 1 |
| 225 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 226 | static inline int Sk32ToBool(uint32_t n) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | return (n | (0-n)) >> 31; |
| 228 | } |
| 229 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 230 | template <typename T> inline void SkTSwap(T& a, T& b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | T c(a); |
| 232 | a = b; |
| 233 | b = c; |
| 234 | } |
| 235 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 236 | static inline int32_t SkAbs32(int32_t value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 238 | if (value < 0) |
| 239 | value = -value; |
| 240 | return value; |
| 241 | #else |
| 242 | int32_t mask = value >> 31; |
| 243 | return (value ^ mask) - mask; |
| 244 | #endif |
| 245 | } |
| 246 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 247 | static inline int32_t SkMax32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 248 | if (a < b) |
| 249 | a = b; |
| 250 | return a; |
| 251 | } |
| 252 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 253 | static inline int32_t SkMin32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | if (a > b) |
| 255 | a = b; |
| 256 | return a; |
| 257 | } |
| 258 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 259 | static inline int32_t SkSign32(int32_t a) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 260 | return (a >> 31) | ((unsigned) -a >> 31); |
| 261 | } |
| 262 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 263 | static inline int32_t SkFastMin32(int32_t value, int32_t max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 265 | if (value > max) |
| 266 | value = max; |
| 267 | return value; |
| 268 | #else |
| 269 | int diff = max - value; |
| 270 | // clear diff if it is negative (clear if value > max) |
| 271 | diff &= (diff >> 31); |
| 272 | return value + diff; |
| 273 | #endif |
| 274 | } |
| 275 | |
| 276 | /** Returns signed 32 bit value pinned between min and max, inclusively |
| 277 | */ |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 278 | static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 279 | #ifdef SK_CPU_HAS_CONDITIONAL_INSTR |
| 280 | if (value < min) |
| 281 | value = min; |
| 282 | if (value > max) |
| 283 | value = max; |
| 284 | #else |
| 285 | if (value < min) |
| 286 | value = min; |
| 287 | else if (value > max) |
| 288 | value = max; |
| 289 | #endif |
| 290 | return value; |
| 291 | } |
| 292 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 293 | static inline uint32_t SkSetClearShift(uint32_t bits, bool cond, |
| 294 | unsigned shift) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | SkASSERT((int)cond == 0 || (int)cond == 1); |
| 296 | return (bits & ~(1 << shift)) | ((int)cond << shift); |
| 297 | } |
| 298 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 299 | static inline uint32_t SkSetClearMask(uint32_t bits, bool cond, |
| 300 | uint32_t mask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 301 | return cond ? bits | mask : bits & ~mask; |
| 302 | } |
| 303 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame^] | 304 | /////////////////////////////////////////////////////////////////////////////// |
| 305 | |
| 306 | /** |
| 307 | * Use to cast a pointer to a different type, and maintaining strict-aliasing |
| 308 | */ |
| 309 | template <typename Dst> Dst SkTCast(const void* ptr) { |
| 310 | union { |
| 311 | const void* src; |
| 312 | Dst dst; |
| 313 | } data; |
| 314 | data.src = ptr; |
| 315 | return data.dst; |
| 316 | } |
| 317 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 318 | ////////////////////////////////////////////////////////////////////////////// |
| 319 | |
| 320 | /** \class SkNoncopyable |
| 321 | |
| 322 | SkNoncopyable is the base class for objects that may do not want to |
| 323 | be copied. It hides its copy-constructor and its assignment-operator. |
| 324 | */ |
| 325 | class SkNoncopyable { |
| 326 | public: |
| 327 | SkNoncopyable() {} |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame^] | 328 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 329 | private: |
| 330 | SkNoncopyable(const SkNoncopyable&); |
| 331 | SkNoncopyable& operator=(const SkNoncopyable&); |
| 332 | }; |
| 333 | |
| 334 | class SkAutoFree : SkNoncopyable { |
| 335 | public: |
| 336 | SkAutoFree() : fPtr(NULL) {} |
| 337 | explicit SkAutoFree(void* ptr) : fPtr(ptr) {} |
| 338 | ~SkAutoFree() { sk_free(fPtr); } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame^] | 339 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | /** Return the currently allocate buffer, or null |
| 341 | */ |
| 342 | void* get() const { return fPtr; } |
| 343 | |
| 344 | /** Assign a new ptr allocated with sk_malloc (or null), and return the |
| 345 | previous ptr. Note it is the caller's responsibility to sk_free the |
| 346 | returned ptr. |
| 347 | */ |
| 348 | void* set(void* ptr) { |
| 349 | void* prev = fPtr; |
| 350 | fPtr = ptr; |
| 351 | return prev; |
| 352 | } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame^] | 353 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 354 | /** Transfer ownership of the current ptr to the caller, setting the |
| 355 | internal reference to null. Note the caller is reponsible for calling |
| 356 | sk_free on the returned address. |
| 357 | */ |
| 358 | void* detach() { return this->set(NULL); } |
| 359 | |
| 360 | /** Free the current buffer, and set the internal reference to NULL. Same |
| 361 | as calling sk_free(detach()) |
| 362 | */ |
| 363 | void free() { |
| 364 | sk_free(fPtr); |
| 365 | fPtr = NULL; |
| 366 | } |
| 367 | |
| 368 | private: |
| 369 | void* fPtr; |
| 370 | // illegal |
| 371 | SkAutoFree(const SkAutoFree&); |
| 372 | SkAutoFree& operator=(const SkAutoFree&); |
| 373 | }; |
| 374 | |
| 375 | class SkAutoMalloc : public SkAutoFree { |
| 376 | public: |
| 377 | explicit SkAutoMalloc(size_t size) |
| 378 | : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {} |
| 379 | |
| 380 | SkAutoMalloc(size_t size, unsigned flags) |
| 381 | : SkAutoFree(sk_malloc_flags(size, flags)) {} |
| 382 | SkAutoMalloc() {} |
| 383 | |
| 384 | void* alloc(size_t size, |
| 385 | unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) { |
| 386 | sk_free(set(sk_malloc_flags(size, flags))); |
| 387 | return get(); |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | template <size_t kSize> class SkAutoSMalloc : SkNoncopyable { |
| 392 | public: |
| 393 | explicit SkAutoSMalloc(size_t size) |
| 394 | { |
| 395 | if (size <= kSize) |
| 396 | fPtr = fStorage; |
| 397 | else |
| 398 | fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); |
| 399 | } |
| 400 | ~SkAutoSMalloc() |
| 401 | { |
| 402 | if (fPtr != (void*)fStorage) |
| 403 | sk_free(fPtr); |
| 404 | } |
| 405 | void* get() const { return fPtr; } |
| 406 | private: |
| 407 | void* fPtr; |
| 408 | uint32_t fStorage[(kSize + 3) >> 2]; |
| 409 | // illegal |
| 410 | SkAutoSMalloc(const SkAutoSMalloc&); |
| 411 | SkAutoSMalloc& operator=(const SkAutoSMalloc&); |
| 412 | }; |
| 413 | |
| 414 | #endif /* C++ */ |
| 415 | |
| 416 | #endif |
| 417 | |