blob: eeba9e0a785c5c749483f8dea83b6edeb9d89f9f [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#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -070039 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040#endif
41
Mike Klein37bbfe32017-09-28 09:47:45 -040042// SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as stand alone assertion expressions, e.g.
43// uint32_t foo(int x) {
44// SkASSERT(x > 4);
45// return x - 4;
46// }
47// and are also written to be compatible with constexpr functions:
48// constexpr uint32_t foo(int x) {
49// return SkASSERT(x > 4),
50// x - 4;
51// }
caryclarkd6562002016-07-27 12:02:07 -070052#define SkASSERT_RELEASE(cond) \
Mike Klein37bbfe32017-09-28 09:47:45 -040053 static_cast<void>( (cond) ? (void)0 : []{ SK_ABORT("assert(" #cond ")"); }() )
djsollenf2b340f2016-01-29 08:51:04 -080054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055#ifdef SK_DEBUG
Mike Klein37bbfe32017-09-28 09:47:45 -040056 #define SkASSERT(cond) SkASSERT_RELEASE(cond)
57 #define SkASSERTF(cond, fmt, ...) static_cast<void>( (cond) ? (void)0 : [&]{ \
58 SkDebugf(fmt"\n", __VA_ARGS__); \
59 SK_ABORT("assert(" #cond ")"); \
60 }() )
bungeman1f790aa2016-07-20 09:49:10 -070061 #define SkDEBUGFAIL(message) SK_ABORT(message)
herb966e3d32015-09-18 07:00:48 -070062 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
csmartdaltonceeaa782016-08-10 10:07:57 -070063 #define SkDEBUGCODE(...) __VA_ARGS__
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 #define SkDEBUGF(args ) SkDebugf args
65 #define SkAssertResult(cond) SkASSERT(cond)
66#else
Mike Klein37bbfe32017-09-28 09:47:45 -040067 #define SkASSERT(cond) static_cast<void>(0)
68 #define SkASSERTF(cond, fmt, ...) static_cast<void>(0)
tomhudson@google.com0c00f212011-12-28 14:59:50 +000069 #define SkDEBUGFAIL(message)
hsterne6f8ff02016-08-15 15:26:31 -070070 #define SkDEBUGFAILF(fmt, ...)
csmartdaltonceeaa782016-08-10 10:07:57 -070071 #define SkDEBUGCODE(...)
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 #define SkDEBUGF(args)
reed@android.com8a1c16f2008-12-17 15:59:43 +000073
bsalomon9daa4b92016-05-09 09:14:36 -070074 // unlike SkASSERT, this guy executes its condition in the non-debug build.
bsalomon1b4c01c2016-05-09 12:35:17 -070075 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
76 #define SkAssertResult(cond) if (cond) {} do {} while(false)
reed@android.com8a1c16f2008-12-17 15:59:43 +000077#endif
78
Hal Canarybc718c12018-06-12 14:16:11 -040079////////////////////////////////////////////////////////////////////////////////
80
reed@google.com37a31332011-01-25 14:55:42 +000081/**
reed@google.com37a31332011-01-25 14:55:42 +000082 * Fast type for unsigned 8 bits. Use for parameter passing and local
83 * variables, not for storage
84 */
85typedef unsigned U8CPU;
86
87/**
88 * Fast type for signed 16 bits. Use for parameter passing and local variables,
89 * not for storage
90 */
91typedef int S16CPU;
92
93/**
94 * Fast type for unsigned 16 bits. Use for parameter passing and local
95 * variables, not for storage
96 */
97typedef unsigned U16CPU;
98
Hal Canaryfdcfb8b2018-06-13 09:42:32 -040099/**
100 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
101 */
102typedef uint8_t SkBool8;
103
104#include "../private/SkTFitsIn.h"
105template <typename D, typename S> constexpr D SkTo(S s) {
106 return SkASSERT(SkTFitsIn<D>(s)),
107 static_cast<D>(s);
108}
109#define SkToS8(x) SkTo<int8_t>(x)
110#define SkToU8(x) SkTo<uint8_t>(x)
111#define SkToS16(x) SkTo<int16_t>(x)
112#define SkToU16(x) SkTo<uint16_t>(x)
113#define SkToS32(x) SkTo<int32_t>(x)
114#define SkToU32(x) SkTo<uint32_t>(x)
115#define SkToInt(x) SkTo<int>(x)
116#define SkToUInt(x) SkTo<unsigned>(x)
117#define SkToSizeT(x) SkTo<size_t>(x)
118
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119/** Returns 0 or 1 based on the condition
120*/
Ben Wagner5b071782017-08-28 15:06:57 -0400121#define SkToBool(cond) ((cond) != 0)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000122
123#define SK_MaxS16 32767
124#define SK_MinS16 -32767
125#define SK_MaxU16 0xFFFF
126#define SK_MinU16 0
127#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000128#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129#define SK_MaxU32 0xFFFFFFFF
130#define SK_MinU32 0
caryclark952538e2016-02-26 05:01:42 -0800131#define SK_NaN32 ((int) (1U << 31))
Mike Reed72818012017-10-09 11:37:44 -0400132#define SK_MaxSizeT SIZE_MAX
Mike Reed3d5a6b52018-01-31 15:55:47 -0500133static constexpr int64_t SK_MaxS64 = 0x7FFFFFFFFFFFFFFF;
134static constexpr int64_t SK_MinS64 = -SK_MaxS64;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400136static inline constexpr int32_t SkLeftShift(int32_t value, int32_t shift) {
caryclark3127c992015-12-09 12:02:30 -0800137 return (int32_t) ((uint32_t) value << shift);
138}
139
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400140static inline constexpr int64_t SkLeftShift(int64_t value, int32_t shift) {
caryclark3127c992015-12-09 12:02:30 -0800141 return (int64_t) ((uint64_t) value << shift);
142}
143
reed@android.comd4577752009-11-21 02:48:11 +0000144//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145
mtkleinfc00a7c2015-05-07 10:58:44 -0700146/** Returns the number of entries in an array (not a pointer) */
147template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700148#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
Hal Canarybc718c12018-06-12 14:16:11 -0400150////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000151
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400152template <typename T> static constexpr T SkAlign2(T x) { return (x + 1) >> 1 << 1; }
153template <typename T> static constexpr T SkAlign4(T x) { return (x + 3) >> 2 << 2; }
154template <typename T> static constexpr T SkAlign8(T x) { return (x + 7) >> 3 << 3; }
reed@google.comc6faa5a2012-06-27 15:07:11 +0000155
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400156template <typename T> static constexpr bool SkIsAlign2(T x) { return 0 == (x & 1); }
157template <typename T> static constexpr bool SkIsAlign4(T x) { return 0 == (x & 3); }
158template <typename T> static constexpr bool SkIsAlign8(T x) { return 0 == (x & 7); }
tomhudson@google.com01224d52011-11-28 18:22:01 +0000159
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400160template <typename T> static constexpr T SkAlignPtr(T x) {
Hal Canarybc718c12018-06-12 14:16:11 -0400161 return sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x);
162}
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400163template <typename T> static constexpr bool SkIsAlignPtr(T x) {
Hal Canarybc718c12018-06-12 14:16:11 -0400164 return sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x);
165}
mtklein0209e952014-08-28 14:10:05 -0700166
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167typedef uint32_t SkFourByteTag;
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400168static inline constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d) {
Hal Canarybc718c12018-06-12 14:16:11 -0400169 return (((uint8_t)a << 24) | ((uint8_t)b << 16) | ((uint8_t)c << 8) | (uint8_t)d);
170}
171
172////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173
174/** 32 bit integer to hold a unicode value
175*/
176typedef int32_t SkUnichar;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700177
halcanaryd0e95a52016-07-25 07:18:12 -0700178/** 16 bit unsigned integer to hold a glyph index
179*/
180typedef uint16_t SkGlyphID;
181
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700182/** 32 bit value to hold a millisecond duration
183 * Note that SK_MSecMax is about 25 days.
184 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000185typedef uint32_t SkMSec;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700186/** maximum representable milliseconds; 24d 20h 31m 23.647s.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187*/
188#define SK_MSecMax 0x7FFFFFFF
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000190/** The generation IDs in Skia reserve 0 has an invalid marker.
191 */
192#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700193/** The unique IDs in Skia reserve 0 has an invalid marker.
194 */
195#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000196
bsalomon@google.comff436612013-02-27 19:07:32 +0000197/** Generic swap function. Classes with efficient swaps should specialize this function to take
198 their fast path. This function is used by SkTSort. */
Mike Kleinbde64e42016-11-14 08:39:39 -0500199template <typename T> static inline void SkTSwap(T& a, T& b) {
bungeman6f4293a2016-10-26 12:11:28 -0700200 T c(std::move(a));
201 a = std::move(b);
202 b = std::move(c);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203}
204
reed@android.comd4577752009-11-21 02:48:11 +0000205static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800206 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000207 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000209 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000210 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000211}
212
Mike Kleinbde64e42016-11-14 08:39:39 -0500213template <typename T> static inline T SkTAbs(T value) {
reed@google.com2b57dc62013-01-08 13:23:32 +0000214 if (value < 0) {
215 value = -value;
216 }
217 return value;
218}
219
reed@android.comd4577752009-11-21 02:48:11 +0000220static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 if (a < b)
222 a = b;
223 return a;
224}
225
reed@android.comd4577752009-11-21 02:48:11 +0000226static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 if (a > b)
228 a = b;
229 return a;
230}
231
halcanarya0af7712016-05-23 09:11:58 -0700232template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000233 return (a < b) ? a : b;
234}
235
halcanarya0af7712016-05-23 09:11:58 -0700236template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000237 return (b < a) ? a : b;
238}
239
Chris Dalton42c21152018-06-13 15:28:19 -0600240template <typename T> constexpr const T& SkTClamp(const T& x, const T& lo, const T& hi) {
241 return (x < lo) ? lo : SkTMin(x, hi);
242}
243
reed@android.comd4577752009-11-21 02:48:11 +0000244static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000245 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000247 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249}
250
bungeman62ce0302015-08-28 09:09:32 -0700251/** Returns value pinned between min and max, inclusively. */
halcanarya0af7712016-05-23 09:11:58 -0700252template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
bungeman62ce0302015-08-28 09:09:32 -0700253 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254}
255
bsalomon5ec26ae2016-02-25 08:33:02 -0800256
257///////////////////////////////////////////////////////////////////////////////
258
259/**
260 * Indicates whether an allocation should count against a cache budget.
261 */
262enum class SkBudgeted : bool {
263 kNo = false,
264 kYes = true
265};
266
robertphillips76948d42016-05-04 12:47:41 -0700267/**
268 * Indicates whether a backing store needs to be an exact match or can be larger
269 * than is strictly necessary
270 */
271enum class SkBackingFit {
272 kApprox,
273 kExact
274};
275
reed@google.com1fcd51e2011-01-05 15:50:27 +0000276///////////////////////////////////////////////////////////////////////////////
277
278/**
279 * Use to cast a pointer to a different type, and maintaining strict-aliasing
280 */
281template <typename Dst> Dst SkTCast(const void* ptr) {
282 union {
283 const void* src;
284 Dst dst;
285 } data;
286 data.src = ptr;
287 return data.dst;
288}
289
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290//////////////////////////////////////////////////////////////////////////////
291
292/** \class SkNoncopyable
293
fmalita055f6b52015-04-09 08:49:32 -0700294SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295be copied. It hides its copy-constructor and its assignment-operator.
296*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000297class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298public:
Chris Blume2b6be202017-04-25 17:33:13 -0700299 SkNoncopyable() = default;
reed@google.com1fcd51e2011-01-05 15:50:27 +0000300
Chris Blume2b6be202017-04-25 17:33:13 -0700301 SkNoncopyable(SkNoncopyable&&) = default;
302 SkNoncopyable& operator =(SkNoncopyable&&) = default;
303
304 SkNoncopyable(const SkNoncopyable&) = delete;
305 SkNoncopyable& operator=(const SkNoncopyable&) = delete;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306};
307
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308#endif