blob: db694e9f8b23c5395fafcfac9e7ef4d887b31864 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkTypes_DEFINED
9#define SkTypes_DEFINED
10
bungemanf20488b2015-07-29 11:49:40 -070011// IWYU pragma: begin_exports
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPreConfig.h"
13#include "SkUserConfig.h"
14#include "SkPostConfig.h"
bungemanf20488b2015-07-29 11:49:40 -070015#include <stddef.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +000016#include <stdint.h>
bungemanf20488b2015-07-29 11:49:40 -070017// IWYU pragma: end_exports
18
bungemanf20488b2015-07-29 11:49:40 -070019#include <string.h>
Mike Klein22ce7942018-06-12 11:05:16 -040020#include <utility>
Mike Reedea5e6762017-03-02 20:22:35 +000021
reed@android.com8a1c16f2008-12-17 15:59:43 +000022/** \file SkTypes.h
23*/
24
reed@android.com9aa8b322010-04-13 13:22:54 +000025/** See SkGraphics::GetVersion() to retrieve these at runtime
26 */
27#define SKIA_VERSION_MAJOR 1
28#define SKIA_VERSION_MINOR 0
29#define SKIA_VERSION_PATCH 0
30
reed@android.com8a1c16f2008-12-17 15:59:43 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032/** Called internally if we hit an unrecoverable error.
33 The platform implementation must not return, but should either throw
34 an exception or otherwise exit.
35*/
djsollenf2b340f2016-01-29 08:51:04 -080036SK_API extern void sk_abort_no_print(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
reed@android.com8a1c16f2008-12-17 15:59:43 +000038#define SK_INIT_TO_AVOID_WARNING = 0
39
40#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -070041 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042#endif
43
Mike Klein37bbfe32017-09-28 09:47:45 -040044// SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as stand alone assertion expressions, e.g.
45// uint32_t foo(int x) {
46// SkASSERT(x > 4);
47// return x - 4;
48// }
49// and are also written to be compatible with constexpr functions:
50// constexpr uint32_t foo(int x) {
51// return SkASSERT(x > 4),
52// x - 4;
53// }
caryclarkd6562002016-07-27 12:02:07 -070054#define SkASSERT_RELEASE(cond) \
Mike Klein37bbfe32017-09-28 09:47:45 -040055 static_cast<void>( (cond) ? (void)0 : []{ SK_ABORT("assert(" #cond ")"); }() )
djsollenf2b340f2016-01-29 08:51:04 -080056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057#ifdef SK_DEBUG
Mike Klein37bbfe32017-09-28 09:47:45 -040058 #define SkASSERT(cond) SkASSERT_RELEASE(cond)
59 #define SkASSERTF(cond, fmt, ...) static_cast<void>( (cond) ? (void)0 : [&]{ \
60 SkDebugf(fmt"\n", __VA_ARGS__); \
61 SK_ABORT("assert(" #cond ")"); \
62 }() )
bungeman1f790aa2016-07-20 09:49:10 -070063 #define SkDEBUGFAIL(message) SK_ABORT(message)
herb966e3d32015-09-18 07:00:48 -070064 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
csmartdaltonceeaa782016-08-10 10:07:57 -070065 #define SkDEBUGCODE(...) __VA_ARGS__
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 #define SkDEBUGF(args ) SkDebugf args
67 #define SkAssertResult(cond) SkASSERT(cond)
68#else
Mike Klein37bbfe32017-09-28 09:47:45 -040069 #define SkASSERT(cond) static_cast<void>(0)
70 #define SkASSERTF(cond, fmt, ...) static_cast<void>(0)
tomhudson@google.com0c00f212011-12-28 14:59:50 +000071 #define SkDEBUGFAIL(message)
hsterne6f8ff02016-08-15 15:26:31 -070072 #define SkDEBUGFAILF(fmt, ...)
csmartdaltonceeaa782016-08-10 10:07:57 -070073 #define SkDEBUGCODE(...)
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 #define SkDEBUGF(args)
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
bsalomon9daa4b92016-05-09 09:14:36 -070076 // unlike SkASSERT, this guy executes its condition in the non-debug build.
bsalomon1b4c01c2016-05-09 12:35:17 -070077 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
78 #define SkAssertResult(cond) if (cond) {} do {} while(false)
reed@android.com8a1c16f2008-12-17 15:59:43 +000079#endif
80
Cary Clark99885412018-04-05 13:09:58 -040081// some clients (e.g. third_party/WebKit/Source/platform/fonts/FontCustomPlatformData.h)
82// depend on SkString forward declaration below. Remove this once dependencies are fixed.
83class SkString;
robertphillips@google.com76f9e932013-01-15 20:17:47 +000084
reed@google.com49a5b192012-10-25 17:31:39 +000085/*
86 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
87 *
88 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
89 *
90 */
91#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
92#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
93
94/*
95 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
96 * line number. Easy way to construct
97 * unique names for local functions or
98 * variables.
99 */
100#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
101
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000102/**
103 * For some classes, it's almost always an error to instantiate one without a name, e.g.
104 * {
105 * SkAutoMutexAcquire(&mutex);
106 * <some code>
107 * }
108 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
109 * but instead the mutex is acquired and then immediately released. The correct usage is
110 * {
111 * SkAutoMutexAcquire lock(&mutex);
112 * <some code>
113 * }
114 *
115 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
116 * like this:
117 * class classname {
118 * <your class>
119 * };
120 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
121 *
122 * This won't work with templates, and you must inline the class' constructors and destructors.
123 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
124 */
125#define SK_REQUIRE_LOCAL_VAR(classname) \
bungeman99fe8222015-08-20 07:57:51 -0700126 static_assert(false, "missing name for " #classname)
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000127
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128///////////////////////////////////////////////////////////////////////
129
reed@google.com37a31332011-01-25 14:55:42 +0000130/**
reed@google.com37a31332011-01-25 14:55:42 +0000131 * Fast type for unsigned 8 bits. Use for parameter passing and local
132 * variables, not for storage
133 */
134typedef unsigned U8CPU;
135
136/**
137 * Fast type for signed 16 bits. Use for parameter passing and local variables,
138 * not for storage
139 */
140typedef int S16CPU;
141
142/**
143 * Fast type for unsigned 16 bits. Use for parameter passing and local
144 * variables, not for storage
145 */
146typedef unsigned U16CPU;
147
148/**
reed@google.com37a31332011-01-25 14:55:42 +0000149 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
150 */
151typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153/** Returns 0 or 1 based on the condition
154*/
Ben Wagner5b071782017-08-28 15:06:57 -0400155#define SkToBool(cond) ((cond) != 0)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156
157#define SK_MaxS16 32767
158#define SK_MinS16 -32767
159#define SK_MaxU16 0xFFFF
160#define SK_MinU16 0
161#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000162#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163#define SK_MaxU32 0xFFFFFFFF
164#define SK_MinU32 0
caryclark952538e2016-02-26 05:01:42 -0800165#define SK_NaN32 ((int) (1U << 31))
Mike Reed72818012017-10-09 11:37:44 -0400166#define SK_MaxSizeT SIZE_MAX
Mike Reed3d5a6b52018-01-31 15:55:47 -0500167static constexpr int64_t SK_MaxS64 = 0x7FFFFFFFFFFFFFFF;
168static constexpr int64_t SK_MinS64 = -SK_MaxS64;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169
caryclark3127c992015-12-09 12:02:30 -0800170static inline int32_t SkLeftShift(int32_t value, int32_t shift) {
171 return (int32_t) ((uint32_t) value << shift);
172}
173
174static inline int64_t SkLeftShift(int64_t value, int32_t shift) {
175 return (int64_t) ((uint64_t) value << shift);
176}
177
reed@android.comd4577752009-11-21 02:48:11 +0000178//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179
mtkleinfc00a7c2015-05-07 10:58:44 -0700180/** Returns the number of entries in an array (not a pointer) */
181template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700182#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183
mtkleinb68ce742015-11-24 05:35:58 -0800184// Can be used to bracket data types that must be dense, e.g. hash keys.
185#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
186 #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
187 _Pragma("GCC diagnostic error \"-Wpadded\"")
188 #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop")
189#else
190 #define SK_BEGIN_REQUIRE_DENSE
191 #define SK_END_REQUIRE_DENSE
192#endif
193
mtklein6a259bf2016-09-26 18:20:57 -0700194#define SkAlign2(x) (((x) + 1) >> 1 << 1)
195#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000196
mtklein6a259bf2016-09-26 18:20:57 -0700197#define SkAlign4(x) (((x) + 3) >> 2 << 2)
198#define SkIsAlign4(x) (0 == ((x) & 3))
reed@google.comc6faa5a2012-06-27 15:07:11 +0000199
mtklein6a259bf2016-09-26 18:20:57 -0700200#define SkAlign8(x) (((x) + 7) >> 3 << 3)
201#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000202
mtklein6a259bf2016-09-26 18:20:57 -0700203#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
204#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
mtklein0209e952014-08-28 14:10:05 -0700205
reed@android.com8a1c16f2008-12-17 15:59:43 +0000206typedef uint32_t SkFourByteTag;
207#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
208
209/** 32 bit integer to hold a unicode value
210*/
211typedef int32_t SkUnichar;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700212
halcanaryd0e95a52016-07-25 07:18:12 -0700213/** 16 bit unsigned integer to hold a glyph index
214*/
215typedef uint16_t SkGlyphID;
216
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700217/** 32 bit value to hold a millisecond duration
218 * Note that SK_MSecMax is about 25 days.
219 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220typedef uint32_t SkMSec;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700221/** maximum representable milliseconds; 24d 20h 31m 23.647s.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222*/
223#define SK_MSecMax 0x7FFFFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000225/** The generation IDs in Skia reserve 0 has an invalid marker.
226 */
227#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700228/** The unique IDs in Skia reserve 0 has an invalid marker.
229 */
230#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000231
bsalomon@google.comff436612013-02-27 19:07:32 +0000232/** Generic swap function. Classes with efficient swaps should specialize this function to take
233 their fast path. This function is used by SkTSort. */
Mike Kleinbde64e42016-11-14 08:39:39 -0500234template <typename T> static inline void SkTSwap(T& a, T& b) {
bungeman6f4293a2016-10-26 12:11:28 -0700235 T c(std::move(a));
236 a = std::move(b);
237 b = std::move(c);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238}
239
reed@android.comd4577752009-11-21 02:48:11 +0000240static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800241 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000242 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000244 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246}
247
Mike Kleinbde64e42016-11-14 08:39:39 -0500248template <typename T> static inline T SkTAbs(T value) {
reed@google.com2b57dc62013-01-08 13:23:32 +0000249 if (value < 0) {
250 value = -value;
251 }
252 return value;
253}
254
reed@android.comd4577752009-11-21 02:48:11 +0000255static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 if (a < b)
257 a = b;
258 return a;
259}
260
reed@android.comd4577752009-11-21 02:48:11 +0000261static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000262 if (a > b)
263 a = b;
264 return a;
265}
266
halcanarya0af7712016-05-23 09:11:58 -0700267template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000268 return (a < b) ? a : b;
269}
270
halcanarya0af7712016-05-23 09:11:58 -0700271template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000272 return (b < a) ? a : b;
273}
274
reed@android.comd4577752009-11-21 02:48:11 +0000275static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000276 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000278 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280}
281
bungeman62ce0302015-08-28 09:09:32 -0700282/** Returns value pinned between min and max, inclusively. */
halcanarya0af7712016-05-23 09:11:58 -0700283template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
bungeman62ce0302015-08-28 09:09:32 -0700284 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285}
286
bsalomon5ec26ae2016-02-25 08:33:02 -0800287
288///////////////////////////////////////////////////////////////////////////////
289
290/**
291 * Indicates whether an allocation should count against a cache budget.
292 */
293enum class SkBudgeted : bool {
294 kNo = false,
295 kYes = true
296};
297
robertphillips76948d42016-05-04 12:47:41 -0700298/**
299 * Indicates whether a backing store needs to be an exact match or can be larger
300 * than is strictly necessary
301 */
302enum class SkBackingFit {
303 kApprox,
304 kExact
305};
306
reed@google.com1fcd51e2011-01-05 15:50:27 +0000307///////////////////////////////////////////////////////////////////////////////
308
309/**
310 * Use to cast a pointer to a different type, and maintaining strict-aliasing
311 */
312template <typename Dst> Dst SkTCast(const void* ptr) {
313 union {
314 const void* src;
315 Dst dst;
316 } data;
317 data.src = ptr;
318 return data.dst;
319}
320
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321//////////////////////////////////////////////////////////////////////////////
322
323/** \class SkNoncopyable
324
fmalita055f6b52015-04-09 08:49:32 -0700325SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326be copied. It hides its copy-constructor and its assignment-operator.
327*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000328class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000329public:
Chris Blume2b6be202017-04-25 17:33:13 -0700330 SkNoncopyable() = default;
reed@google.com1fcd51e2011-01-05 15:50:27 +0000331
Chris Blume2b6be202017-04-25 17:33:13 -0700332 SkNoncopyable(SkNoncopyable&&) = default;
333 SkNoncopyable& operator =(SkNoncopyable&&) = default;
334
335 SkNoncopyable(const SkNoncopyable&) = delete;
336 SkNoncopyable& operator=(const SkNoncopyable&) = delete;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337};
338
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339#endif