reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkTypes_DEFINED |
| 9 | #define SkTypes_DEFINED |
| 10 | |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 11 | // IWYU pragma: begin_exports |
bsalomon | f48c62f | 2016-07-08 03:28:42 -0700 | [diff] [blame] | 12 | |
| 13 | // In at least two known scenarios when using GCC with libc++: |
| 14 | // * GCC 4.8 targeting ARMv7 with NEON |
| 15 | // * GCC 4.9 targeting ARMv8 64 bit |
| 16 | // we need to typedef float float32_t (or include <arm_neon.h> which does that) |
| 17 | // before #including <memory>. This makes no sense. I'm not very interested in |
| 18 | // understanding why... these are old, bizarre platform configuration that we |
| 19 | // should just let die. |
bungeman | 7ad42cf | 2016-07-08 12:25:37 -0700 | [diff] [blame] | 20 | // See https://llvm.org/bugs/show_bug.cgi?id=25608 . |
bsalomon | f48c62f | 2016-07-08 03:28:42 -0700 | [diff] [blame] | 21 | #include <ciso646> // Include something innocuous to define _LIBCPP_VERISON if it's libc++. |
| 22 | #if defined(__GNUC__) && __GNUC__ == 4 \ |
bungeman | 7ad42cf | 2016-07-08 12:25:37 -0700 | [diff] [blame] | 23 | && ((defined(__arm__) && (defined(__ARM_NEON__) || defined(__ARM_NEON))) || defined(__aarch64__)) \ |
bsalomon | f48c62f | 2016-07-08 03:28:42 -0700 | [diff] [blame] | 24 | && defined(_LIBCPP_VERSION) |
| 25 | typedef float float32_t; |
| 26 | #include <memory> |
| 27 | #endif |
| 28 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | #include "SkPreConfig.h" |
| 30 | #include "SkUserConfig.h" |
| 31 | #include "SkPostConfig.h" |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 32 | #include <stddef.h> |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 33 | #include <stdint.h> |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 34 | // IWYU pragma: end_exports |
| 35 | |
bungeman | f20488b | 2015-07-29 11:49:40 -0700 | [diff] [blame] | 36 | #include <string.h> |
Herb Derby | d7b34a5 | 2017-03-20 11:19:23 -0400 | [diff] [blame] | 37 | // TODO(herb): remove after chromuim skia/ext/SkMemory_new_handler.cpp |
Herb Derby | b549cc3 | 2017-03-27 13:35:15 -0400 | [diff] [blame] | 38 | // has been updated to point to private/SkMalloc.h |
| 39 | #include "../private/SkMalloc.h" |
mtklein | 95cc012 | 2015-04-27 15:11:01 -0700 | [diff] [blame] | 40 | |
Mike Reed | ea5e676 | 2017-03-02 20:22:35 +0000 | [diff] [blame] | 41 | // enable to test new device-base clipping |
| 42 | //#define SK_USE_DEVICE_CLIPPING |
| 43 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | /** \file SkTypes.h |
| 45 | */ |
| 46 | |
reed@android.com | 9aa8b32 | 2010-04-13 13:22:54 +0000 | [diff] [blame] | 47 | /** See SkGraphics::GetVersion() to retrieve these at runtime |
| 48 | */ |
| 49 | #define SKIA_VERSION_MAJOR 1 |
| 50 | #define SKIA_VERSION_MINOR 0 |
| 51 | #define SKIA_VERSION_PATCH 0 |
| 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | /** Called internally if we hit an unrecoverable error. |
| 55 | The platform implementation must not return, but should either throw |
| 56 | an exception or otherwise exit. |
| 57 | */ |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 58 | SK_API extern void sk_abort_no_print(void); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | |
reed@google.com | bdf7361 | 2011-09-06 14:56:20 +0000 | [diff] [blame] | 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | #ifdef override_GLOBAL_NEW |
reed@google.com | bdf7361 | 2011-09-06 14:56:20 +0000 | [diff] [blame] | 63 | #include <new> |
| 64 | |
| 65 | inline void* operator new(size_t size) { |
| 66 | return sk_malloc_throw(size); |
| 67 | } |
| 68 | |
| 69 | inline void operator delete(void* p) { |
| 70 | sk_free(p); |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | |
| 76 | #define SK_INIT_TO_AVOID_WARNING = 0 |
| 77 | |
| 78 | #ifndef SkDebugf |
george | c4ade57 | 2014-08-01 12:02:07 -0700 | [diff] [blame] | 79 | SK_API void SkDebugf(const char format[], ...); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Mike Klein | 37bbfe3 | 2017-09-28 09:47:45 -0400 | [diff] [blame^] | 82 | // SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as stand alone assertion expressions, e.g. |
| 83 | // uint32_t foo(int x) { |
| 84 | // SkASSERT(x > 4); |
| 85 | // return x - 4; |
| 86 | // } |
| 87 | // and are also written to be compatible with constexpr functions: |
| 88 | // constexpr uint32_t foo(int x) { |
| 89 | // return SkASSERT(x > 4), |
| 90 | // x - 4; |
| 91 | // } |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 92 | #define SkASSERT_RELEASE(cond) \ |
Mike Klein | 37bbfe3 | 2017-09-28 09:47:45 -0400 | [diff] [blame^] | 93 | static_cast<void>( (cond) ? (void)0 : []{ SK_ABORT("assert(" #cond ")"); }() ) |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | #ifdef SK_DEBUG |
Mike Klein | 37bbfe3 | 2017-09-28 09:47:45 -0400 | [diff] [blame^] | 96 | #define SkASSERT(cond) SkASSERT_RELEASE(cond) |
| 97 | #define SkASSERTF(cond, fmt, ...) static_cast<void>( (cond) ? (void)0 : [&]{ \ |
| 98 | SkDebugf(fmt"\n", __VA_ARGS__); \ |
| 99 | SK_ABORT("assert(" #cond ")"); \ |
| 100 | }() ) |
bungeman | 1f790aa | 2016-07-20 09:49:10 -0700 | [diff] [blame] | 101 | #define SkDEBUGFAIL(message) SK_ABORT(message) |
herb | 966e3d3 | 2015-09-18 07:00:48 -0700 | [diff] [blame] | 102 | #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__) |
csmartdalton | ceeaa78 | 2016-08-10 10:07:57 -0700 | [diff] [blame] | 103 | #define SkDEBUGCODE(...) __VA_ARGS__ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | #define SkDEBUGF(args ) SkDebugf args |
| 105 | #define SkAssertResult(cond) SkASSERT(cond) |
| 106 | #else |
Mike Klein | 37bbfe3 | 2017-09-28 09:47:45 -0400 | [diff] [blame^] | 107 | #define SkASSERT(cond) static_cast<void>(0) |
| 108 | #define SkASSERTF(cond, fmt, ...) static_cast<void>(0) |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 109 | #define SkDEBUGFAIL(message) |
hstern | e6f8ff0 | 2016-08-15 15:26:31 -0700 | [diff] [blame] | 110 | #define SkDEBUGFAILF(fmt, ...) |
csmartdalton | ceeaa78 | 2016-08-10 10:07:57 -0700 | [diff] [blame] | 111 | #define SkDEBUGCODE(...) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | #define SkDEBUGF(args) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | |
bsalomon | 9daa4b9 | 2016-05-09 09:14:36 -0700 | [diff] [blame] | 114 | // unlike SkASSERT, this guy executes its condition in the non-debug build. |
bsalomon | 1b4c01c | 2016-05-09 12:35:17 -0700 | [diff] [blame] | 115 | // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT. |
| 116 | #define SkAssertResult(cond) if (cond) {} do {} while(false) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 119 | #ifdef SK_IGNORE_TO_STRING |
skia.committer@gmail.com | bc3d92a | 2014-03-14 03:02:26 +0000 | [diff] [blame] | 120 | #define SK_TO_STRING_NONVIRT() |
| 121 | #define SK_TO_STRING_VIRT() |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 122 | #define SK_TO_STRING_PUREVIRT() |
| 123 | #define SK_TO_STRING_OVERRIDE() |
| 124 | #else |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 125 | class SkString; |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 126 | // the 'toString' helper functions convert Sk* objects to human-readable |
| 127 | // form in developer mode |
| 128 | #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const; |
| 129 | #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const; |
| 130 | #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 131 | #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override; |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
reed@google.com | 49a5b19 | 2012-10-25 17:31:39 +0000 | [diff] [blame] | 134 | /* |
| 135 | * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab |
| 136 | * |
| 137 | * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly |
| 138 | * |
| 139 | */ |
| 140 | #define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y) |
| 141 | #define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y |
| 142 | |
| 143 | /* |
| 144 | * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current |
| 145 | * line number. Easy way to construct |
| 146 | * unique names for local functions or |
| 147 | * variables. |
| 148 | */ |
| 149 | #define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__) |
| 150 | |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 151 | /** |
| 152 | * For some classes, it's almost always an error to instantiate one without a name, e.g. |
| 153 | * { |
| 154 | * SkAutoMutexAcquire(&mutex); |
| 155 | * <some code> |
| 156 | * } |
| 157 | * In this case, the writer meant to hold mutex while the rest of the code in the block runs, |
| 158 | * but instead the mutex is acquired and then immediately released. The correct usage is |
| 159 | * { |
| 160 | * SkAutoMutexAcquire lock(&mutex); |
| 161 | * <some code> |
| 162 | * } |
| 163 | * |
| 164 | * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR |
| 165 | * like this: |
| 166 | * class classname { |
| 167 | * <your class> |
| 168 | * }; |
| 169 | * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname) |
| 170 | * |
| 171 | * This won't work with templates, and you must inline the class' constructors and destructors. |
| 172 | * Take a look at SkAutoFree and SkAutoMalloc in this file for examples. |
| 173 | */ |
| 174 | #define SK_REQUIRE_LOCAL_VAR(classname) \ |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 175 | static_assert(false, "missing name for " #classname) |
commit-bot@chromium.org | e61a86c | 2013-11-18 16:03:59 +0000 | [diff] [blame] | 176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | /////////////////////////////////////////////////////////////////////// |
| 178 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 179 | /** |
| 180 | * Fast type for signed 8 bits. Use for parameter passing and local variables, |
| 181 | * not for storage. |
| 182 | */ |
| 183 | typedef int S8CPU; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Fast type for unsigned 8 bits. Use for parameter passing and local |
| 187 | * variables, not for storage |
| 188 | */ |
| 189 | typedef unsigned U8CPU; |
| 190 | |
| 191 | /** |
| 192 | * Fast type for signed 16 bits. Use for parameter passing and local variables, |
| 193 | * not for storage |
| 194 | */ |
| 195 | typedef int S16CPU; |
| 196 | |
| 197 | /** |
| 198 | * Fast type for unsigned 16 bits. Use for parameter passing and local |
| 199 | * variables, not for storage |
| 200 | */ |
| 201 | typedef unsigned U16CPU; |
| 202 | |
| 203 | /** |
reed@google.com | 37a3133 | 2011-01-25 14:55:42 +0000 | [diff] [blame] | 204 | * Meant to be a small version of bool, for storage purposes. Will be 0 or 1 |
| 205 | */ |
| 206 | typedef uint8_t SkBool8; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | |
bungeman | 68c14d9 | 2016-03-19 15:06:56 -0700 | [diff] [blame] | 208 | #include "../private/SkTFitsIn.h" |
| 209 | template <typename D, typename S> D SkTo(S s) { |
| 210 | SkASSERT(SkTFitsIn<D>(s)); |
| 211 | return static_cast<D>(s); |
| 212 | } |
| 213 | #define SkToS8(x) SkTo<int8_t>(x) |
| 214 | #define SkToU8(x) SkTo<uint8_t>(x) |
| 215 | #define SkToS16(x) SkTo<int16_t>(x) |
| 216 | #define SkToU16(x) SkTo<uint16_t>(x) |
| 217 | #define SkToS32(x) SkTo<int32_t>(x) |
| 218 | #define SkToU32(x) SkTo<uint32_t>(x) |
| 219 | #define SkToInt(x) SkTo<int>(x) |
| 220 | #define SkToUInt(x) SkTo<unsigned>(x) |
| 221 | #define SkToSizeT(x) SkTo<size_t>(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | |
| 223 | /** Returns 0 or 1 based on the condition |
| 224 | */ |
Ben Wagner | 5b07178 | 2017-08-28 15:06:57 -0400 | [diff] [blame] | 225 | #define SkToBool(cond) ((cond) != 0) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | |
| 227 | #define SK_MaxS16 32767 |
| 228 | #define SK_MinS16 -32767 |
| 229 | #define SK_MaxU16 0xFFFF |
| 230 | #define SK_MinU16 0 |
| 231 | #define SK_MaxS32 0x7FFFFFFF |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 232 | #define SK_MinS32 -SK_MaxS32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | #define SK_MaxU32 0xFFFFFFFF |
| 234 | #define SK_MinU32 0 |
caryclark | 952538e | 2016-02-26 05:01:42 -0800 | [diff] [blame] | 235 | #define SK_NaN32 ((int) (1U << 31)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 237 | static inline int32_t SkLeftShift(int32_t value, int32_t shift) { |
| 238 | return (int32_t) ((uint32_t) value << shift); |
| 239 | } |
| 240 | |
| 241 | static inline int64_t SkLeftShift(int64_t value, int32_t shift) { |
| 242 | return (int64_t) ((uint64_t) value << shift); |
| 243 | } |
| 244 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 245 | ////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 246 | |
mtklein | fc00a7c | 2015-05-07 10:58:44 -0700 | [diff] [blame] | 247 | /** Returns the number of entries in an array (not a pointer) */ |
| 248 | template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N]; |
caryclark | 95b96d6 | 2015-08-19 10:12:59 -0700 | [diff] [blame] | 249 | #define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array))) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | |
mtklein | b68ce74 | 2015-11-24 05:35:58 -0800 | [diff] [blame] | 251 | // Can be used to bracket data types that must be dense, e.g. hash keys. |
| 252 | #if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work! |
| 253 | #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \ |
| 254 | _Pragma("GCC diagnostic error \"-Wpadded\"") |
| 255 | #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop") |
| 256 | #else |
| 257 | #define SK_BEGIN_REQUIRE_DENSE |
| 258 | #define SK_END_REQUIRE_DENSE |
| 259 | #endif |
| 260 | |
mtklein | 6a259bf | 2016-09-26 18:20:57 -0700 | [diff] [blame] | 261 | #define SkAlign2(x) (((x) + 1) >> 1 << 1) |
| 262 | #define SkIsAlign2(x) (0 == ((x) & 1)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 263 | |
mtklein | 6a259bf | 2016-09-26 18:20:57 -0700 | [diff] [blame] | 264 | #define SkAlign4(x) (((x) + 3) >> 2 << 2) |
| 265 | #define SkIsAlign4(x) (0 == ((x) & 3)) |
reed@google.com | c6faa5a | 2012-06-27 15:07:11 +0000 | [diff] [blame] | 266 | |
mtklein | 6a259bf | 2016-09-26 18:20:57 -0700 | [diff] [blame] | 267 | #define SkAlign8(x) (((x) + 7) >> 3 << 3) |
| 268 | #define SkIsAlign8(x) (0 == ((x) & 7)) |
tomhudson@google.com | 01224d5 | 2011-11-28 18:22:01 +0000 | [diff] [blame] | 269 | |
mtklein | 6a259bf | 2016-09-26 18:20:57 -0700 | [diff] [blame] | 270 | #define SkAlign16(x) (((x) + 15) >> 4 << 4) |
| 271 | #define SkIsAlign16(x) (0 == ((x) & 15)) |
reed | e2b0a0a | 2016-03-02 13:03:46 -0800 | [diff] [blame] | 272 | |
mtklein | 6a259bf | 2016-09-26 18:20:57 -0700 | [diff] [blame] | 273 | #define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x)) |
| 274 | #define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x)) |
mtklein | 0209e95 | 2014-08-28 14:10:05 -0700 | [diff] [blame] | 275 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 276 | typedef uint32_t SkFourByteTag; |
| 277 | #define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) |
| 278 | |
| 279 | /** 32 bit integer to hold a unicode value |
| 280 | */ |
| 281 | typedef int32_t SkUnichar; |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 282 | |
halcanary | d0e95a5 | 2016-07-25 07:18:12 -0700 | [diff] [blame] | 283 | /** 16 bit unsigned integer to hold a glyph index |
| 284 | */ |
| 285 | typedef uint16_t SkGlyphID; |
| 286 | |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 287 | /** 32 bit value to hold a millisecond duration |
| 288 | * Note that SK_MSecMax is about 25 days. |
| 289 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | typedef uint32_t SkMSec; |
| 291 | /** 1 second measured in milliseconds |
| 292 | */ |
| 293 | #define SK_MSec1 1000 |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 294 | /** maximum representable milliseconds; 24d 20h 31m 23.647s. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | */ |
| 296 | #define SK_MSecMax 0x7FFFFFFF |
| 297 | /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 298 | */ |
| 299 | #define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) |
| 300 | /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 |
| 301 | */ |
| 302 | #define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) |
| 303 | |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 304 | /** The generation IDs in Skia reserve 0 has an invalid marker. |
| 305 | */ |
| 306 | #define SK_InvalidGenID 0 |
bsalomon | 1c63bf6 | 2014-07-22 13:09:46 -0700 | [diff] [blame] | 307 | /** The unique IDs in Skia reserve 0 has an invalid marker. |
| 308 | */ |
| 309 | #define SK_InvalidUniqueID 0 |
commit-bot@chromium.org | 2b4e370 | 2014-04-07 18:26:22 +0000 | [diff] [blame] | 310 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | /**************************************************************************** |
| 312 | The rest of these only build with C++ |
| 313 | */ |
| 314 | #ifdef __cplusplus |
| 315 | |
| 316 | /** Faster than SkToBool for integral conditions. Returns 0 or 1 |
| 317 | */ |
bsalomon | 7a5bcc5 | 2016-05-24 13:23:56 -0700 | [diff] [blame] | 318 | static inline constexpr int Sk32ToBool(uint32_t n) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | return (n | (0-n)) >> 31; |
| 320 | } |
| 321 | |
bsalomon@google.com | ff43661 | 2013-02-27 19:07:32 +0000 | [diff] [blame] | 322 | /** Generic swap function. Classes with efficient swaps should specialize this function to take |
| 323 | their fast path. This function is used by SkTSort. */ |
Mike Klein | bde64e4 | 2016-11-14 08:39:39 -0500 | [diff] [blame] | 324 | template <typename T> static inline void SkTSwap(T& a, T& b) { |
bungeman | 6f4293a | 2016-10-26 12:11:28 -0700 | [diff] [blame] | 325 | T c(std::move(a)); |
| 326 | a = std::move(b); |
| 327 | b = std::move(c); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 328 | } |
| 329 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 330 | static inline int32_t SkAbs32(int32_t value) { |
mtklein | 09a22e9 | 2014-11-21 11:38:53 -0800 | [diff] [blame] | 331 | SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated. |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 332 | if (value < 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | value = -value; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 334 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | return value; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Mike Klein | bde64e4 | 2016-11-14 08:39:39 -0500 | [diff] [blame] | 338 | template <typename T> static inline T SkTAbs(T value) { |
reed@google.com | 2b57dc6 | 2013-01-08 13:23:32 +0000 | [diff] [blame] | 339 | if (value < 0) { |
| 340 | value = -value; |
| 341 | } |
| 342 | return value; |
| 343 | } |
| 344 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 345 | static inline int32_t SkMax32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | if (a < b) |
| 347 | a = b; |
| 348 | return a; |
| 349 | } |
| 350 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 351 | static inline int32_t SkMin32(int32_t a, int32_t b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | if (a > b) |
| 353 | a = b; |
| 354 | return a; |
| 355 | } |
| 356 | |
halcanary | a0af771 | 2016-05-23 09:11:58 -0700 | [diff] [blame] | 357 | template <typename T> constexpr const T& SkTMin(const T& a, const T& b) { |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 358 | return (a < b) ? a : b; |
| 359 | } |
| 360 | |
halcanary | a0af771 | 2016-05-23 09:11:58 -0700 | [diff] [blame] | 361 | template <typename T> constexpr const T& SkTMax(const T& a, const T& b) { |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 362 | return (b < a) ? a : b; |
| 363 | } |
| 364 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 365 | static inline int32_t SkSign32(int32_t a) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | return (a >> 31) | ((unsigned) -a >> 31); |
| 367 | } |
| 368 | |
reed@android.com | d457775 | 2009-11-21 02:48:11 +0000 | [diff] [blame] | 369 | 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] | 370 | if (value > max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 371 | value = max; |
commit-bot@chromium.org | 38bad32 | 2013-07-30 13:16:29 +0000 | [diff] [blame] | 372 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | return value; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | } |
| 375 | |
bungeman | 62ce030 | 2015-08-28 09:09:32 -0700 | [diff] [blame] | 376 | /** Returns value pinned between min and max, inclusively. */ |
halcanary | a0af771 | 2016-05-23 09:11:58 -0700 | [diff] [blame] | 377 | template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) { |
bungeman | 62ce030 | 2015-08-28 09:09:32 -0700 | [diff] [blame] | 378 | return SkTMax(SkTMin(value, max), min); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 379 | } |
| 380 | |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 381 | |
| 382 | /////////////////////////////////////////////////////////////////////////////// |
| 383 | |
| 384 | /** |
| 385 | * Indicates whether an allocation should count against a cache budget. |
| 386 | */ |
| 387 | enum class SkBudgeted : bool { |
| 388 | kNo = false, |
| 389 | kYes = true |
| 390 | }; |
| 391 | |
robertphillips | 76948d4 | 2016-05-04 12:47:41 -0700 | [diff] [blame] | 392 | /** |
| 393 | * Indicates whether a backing store needs to be an exact match or can be larger |
| 394 | * than is strictly necessary |
| 395 | */ |
| 396 | enum class SkBackingFit { |
| 397 | kApprox, |
| 398 | kExact |
| 399 | }; |
| 400 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 401 | /////////////////////////////////////////////////////////////////////////////// |
| 402 | |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 403 | /** Use to combine multiple bits in a bitmask in a type safe way. |
| 404 | */ |
| 405 | template <typename T> |
| 406 | T SkTBitOr(T a, T b) { |
| 407 | return (T)(a | b); |
| 408 | } |
| 409 | |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 410 | /** |
| 411 | * Use to cast a pointer to a different type, and maintaining strict-aliasing |
| 412 | */ |
| 413 | template <typename Dst> Dst SkTCast(const void* ptr) { |
| 414 | union { |
| 415 | const void* src; |
| 416 | Dst dst; |
| 417 | } data; |
| 418 | data.src = ptr; |
| 419 | return data.dst; |
| 420 | } |
| 421 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | ////////////////////////////////////////////////////////////////////////////// |
| 423 | |
| 424 | /** \class SkNoncopyable |
| 425 | |
fmalita | 055f6b5 | 2015-04-09 08:49:32 -0700 | [diff] [blame] | 426 | SkNoncopyable is the base class for objects that do not want to |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 427 | be copied. It hides its copy-constructor and its assignment-operator. |
| 428 | */ |
ctguil@chromium.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 429 | class SK_API SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 430 | public: |
Chris Blume | 2b6be20 | 2017-04-25 17:33:13 -0700 | [diff] [blame] | 431 | SkNoncopyable() = default; |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 432 | |
Chris Blume | 2b6be20 | 2017-04-25 17:33:13 -0700 | [diff] [blame] | 433 | SkNoncopyable(SkNoncopyable&&) = default; |
| 434 | SkNoncopyable& operator =(SkNoncopyable&&) = default; |
| 435 | |
| 436 | SkNoncopyable(const SkNoncopyable&) = delete; |
| 437 | SkNoncopyable& operator=(const SkNoncopyable&) = delete; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 438 | }; |
| 439 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | #endif /* C++ */ |
| 441 | |
| 442 | #endif |