blob: 395211a23727ea1d266450e64fd5c1bc89a2846d [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
Hal Canary61582512018-06-21 10:33:52 -040019#include <utility>
20
reed@android.com8a1c16f2008-12-17 15:59:43 +000021/** \file SkTypes.h
22*/
23
reed@android.com9aa8b322010-04-13 13:22:54 +000024/** See SkGraphics::GetVersion() to retrieve these at runtime
Hal Canaryc36f7e72018-06-18 15:50:09 -040025*/
reed@android.com9aa8b322010-04-13 13:22:54 +000026#define SKIA_VERSION_MAJOR 1
27#define SKIA_VERSION_MINOR 0
28#define SKIA_VERSION_PATCH 0
29
reed@android.com8a1c16f2008-12-17 15:59:43 +000030
reed@android.com8a1c16f2008-12-17 15:59:43 +000031/** Called internally if we hit an unrecoverable error.
32 The platform implementation must not return, but should either throw
33 an exception or otherwise exit.
34*/
djsollenf2b340f2016-01-29 08:51:04 -080035SK_API extern void sk_abort_no_print(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000036
reed@android.com8a1c16f2008-12-17 15:59:43 +000037#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -070038 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039#endif
40
Mike Klein37bbfe32017-09-28 09:47:45 -040041// SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as stand alone assertion expressions, e.g.
42// uint32_t foo(int x) {
43// SkASSERT(x > 4);
44// return x - 4;
45// }
46// and are also written to be compatible with constexpr functions:
47// constexpr uint32_t foo(int x) {
48// return SkASSERT(x > 4),
49// x - 4;
50// }
caryclarkd6562002016-07-27 12:02:07 -070051#define SkASSERT_RELEASE(cond) \
Mike Klein37bbfe32017-09-28 09:47:45 -040052 static_cast<void>( (cond) ? (void)0 : []{ SK_ABORT("assert(" #cond ")"); }() )
djsollenf2b340f2016-01-29 08:51:04 -080053
reed@android.com8a1c16f2008-12-17 15:59:43 +000054#ifdef SK_DEBUG
Mike Klein37bbfe32017-09-28 09:47:45 -040055 #define SkASSERT(cond) SkASSERT_RELEASE(cond)
56 #define SkASSERTF(cond, fmt, ...) static_cast<void>( (cond) ? (void)0 : [&]{ \
57 SkDebugf(fmt"\n", __VA_ARGS__); \
58 SK_ABORT("assert(" #cond ")"); \
59 }() )
bungeman1f790aa2016-07-20 09:49:10 -070060 #define SkDEBUGFAIL(message) SK_ABORT(message)
herb966e3d32015-09-18 07:00:48 -070061 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
csmartdaltonceeaa782016-08-10 10:07:57 -070062 #define SkDEBUGCODE(...) __VA_ARGS__
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 #define SkDEBUGF(args ) SkDebugf args
64 #define SkAssertResult(cond) SkASSERT(cond)
65#else
Mike Klein37bbfe32017-09-28 09:47:45 -040066 #define SkASSERT(cond) static_cast<void>(0)
67 #define SkASSERTF(cond, fmt, ...) static_cast<void>(0)
tomhudson@google.com0c00f212011-12-28 14:59:50 +000068 #define SkDEBUGFAIL(message)
hsterne6f8ff02016-08-15 15:26:31 -070069 #define SkDEBUGFAILF(fmt, ...)
csmartdaltonceeaa782016-08-10 10:07:57 -070070 #define SkDEBUGCODE(...)
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 #define SkDEBUGF(args)
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
Hal Canaryc36f7e72018-06-18 15:50:09 -040073 // unlike SkASSERT, this macro executes its condition in the non-debug build.
bsalomon1b4c01c2016-05-09 12:35:17 -070074 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
75 #define SkAssertResult(cond) if (cond) {} do {} while(false)
reed@android.com8a1c16f2008-12-17 15:59:43 +000076#endif
77
Hal Canarybc718c12018-06-12 14:16:11 -040078////////////////////////////////////////////////////////////////////////////////
79
Hal Canaryc36f7e72018-06-18 15:50:09 -040080/** Fast type for unsigned 8 bits. Use for parameter passing and local
81 variables, not for storage
82*/
reed@google.com37a31332011-01-25 14:55:42 +000083typedef unsigned U8CPU;
84
Hal Canaryc36f7e72018-06-18 15:50:09 -040085/** Fast type for signed 16 bits. Use for parameter passing and local variables,
86 not for storage
87*/
reed@google.com37a31332011-01-25 14:55:42 +000088typedef int S16CPU;
89
Hal Canaryc36f7e72018-06-18 15:50:09 -040090/** Fast type for unsigned 16 bits. Use for parameter passing and local
91 variables, not for storage
92*/
reed@google.com37a31332011-01-25 14:55:42 +000093typedef unsigned U16CPU;
94
Hal Canaryc36f7e72018-06-18 15:50:09 -040095/** @return false or true based on the condition
reed@android.com8a1c16f2008-12-17 15:59:43 +000096*/
Hal Canaryc36f7e72018-06-18 15:50:09 -040097template <typename T> static constexpr bool SkToBool(const T& x) { return 0 != x; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
Hal Canaryc36f7e72018-06-18 15:50:09 -040099static constexpr int16_t SK_MaxS16 = INT16_MAX;
100static constexpr int16_t SK_MinS16 = -SK_MaxS16;
101
102static constexpr int32_t SK_MaxS32 = INT32_MAX;
103static constexpr int32_t SK_MinS32 = -SK_MaxS32;
104static constexpr int32_t SK_NaN32 = INT32_MIN;
105
Ben Wagnerb0897652018-06-15 15:37:57 +0000106static constexpr int64_t SK_MaxS64 = INT64_MAX;
Mike Reed3d5a6b52018-01-31 15:55:47 -0500107static constexpr int64_t SK_MinS64 = -SK_MaxS64;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400109static inline constexpr int32_t SkLeftShift(int32_t value, int32_t shift) {
caryclark3127c992015-12-09 12:02:30 -0800110 return (int32_t) ((uint32_t) value << shift);
111}
112
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400113static inline constexpr int64_t SkLeftShift(int64_t value, int32_t shift) {
caryclark3127c992015-12-09 12:02:30 -0800114 return (int64_t) ((uint64_t) value << shift);
115}
116
Hal Canaryc36f7e72018-06-18 15:50:09 -0400117////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
Hal Canaryc36f7e72018-06-18 15:50:09 -0400119/** @return the number of entries in an array (not a pointer)
120*/
mtkleinfc00a7c2015-05-07 10:58:44 -0700121template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700122#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123
Hal Canarybc718c12018-06-12 14:16:11 -0400124////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400126template <typename T> static constexpr T SkAlign2(T x) { return (x + 1) >> 1 << 1; }
127template <typename T> static constexpr T SkAlign4(T x) { return (x + 3) >> 2 << 2; }
128template <typename T> static constexpr T SkAlign8(T x) { return (x + 7) >> 3 << 3; }
reed@google.comc6faa5a2012-06-27 15:07:11 +0000129
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400130template <typename T> static constexpr bool SkIsAlign2(T x) { return 0 == (x & 1); }
131template <typename T> static constexpr bool SkIsAlign4(T x) { return 0 == (x & 3); }
132template <typename T> static constexpr bool SkIsAlign8(T x) { return 0 == (x & 7); }
tomhudson@google.com01224d52011-11-28 18:22:01 +0000133
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400134template <typename T> static constexpr T SkAlignPtr(T x) {
Hal Canarybc718c12018-06-12 14:16:11 -0400135 return sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x);
136}
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400137template <typename T> static constexpr bool SkIsAlignPtr(T x) {
Hal Canarybc718c12018-06-12 14:16:11 -0400138 return sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x);
139}
mtklein0209e952014-08-28 14:10:05 -0700140
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141typedef uint32_t SkFourByteTag;
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400142static inline constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d) {
Hal Canarybc718c12018-06-12 14:16:11 -0400143 return (((uint8_t)a << 24) | ((uint8_t)b << 16) | ((uint8_t)c << 8) | (uint8_t)d);
144}
145
146////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147
148/** 32 bit integer to hold a unicode value
149*/
150typedef int32_t SkUnichar;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700151
halcanaryd0e95a52016-07-25 07:18:12 -0700152/** 16 bit unsigned integer to hold a glyph index
153*/
154typedef uint16_t SkGlyphID;
155
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700156/** 32 bit value to hold a millisecond duration
Hal Canaryc36f7e72018-06-18 15:50:09 -0400157 Note that SK_MSecMax is about 25 days.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158*/
Hal Canaryc36f7e72018-06-18 15:50:09 -0400159typedef uint32_t SkMSec;
160
161/** Maximum representable milliseconds; 24d 20h 31m 23.647s.
162*/
163static constexpr SkMSec SK_MSecMax = INT32_MAX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000165/** The generation IDs in Skia reserve 0 has an invalid marker.
Hal Canaryc36f7e72018-06-18 15:50:09 -0400166*/
167static constexpr uint32_t SK_InvalidGenID = 0;
168
bsalomon1c63bf62014-07-22 13:09:46 -0700169/** The unique IDs in Skia reserve 0 has an invalid marker.
Hal Canaryc36f7e72018-06-18 15:50:09 -0400170*/
171static constexpr uint32_t SK_InvalidUniqueID = 0;
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000172
reed@android.comd4577752009-11-21 02:48:11 +0000173static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800174 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000175 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000177 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179}
180
Mike Kleinbde64e42016-11-14 08:39:39 -0500181template <typename T> static inline T SkTAbs(T value) {
reed@google.com2b57dc62013-01-08 13:23:32 +0000182 if (value < 0) {
183 value = -value;
184 }
185 return value;
186}
187
reed@android.comd4577752009-11-21 02:48:11 +0000188static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 if (a < b)
190 a = b;
191 return a;
192}
193
reed@android.comd4577752009-11-21 02:48:11 +0000194static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 if (a > b)
196 a = b;
197 return a;
198}
199
halcanarya0af7712016-05-23 09:11:58 -0700200template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000201 return (a < b) ? a : b;
202}
203
halcanarya0af7712016-05-23 09:11:58 -0700204template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000205 return (b < a) ? a : b;
206}
207
Chris Dalton42c21152018-06-13 15:28:19 -0600208template <typename T> constexpr const T& SkTClamp(const T& x, const T& lo, const T& hi) {
209 return (x < lo) ? lo : SkTMin(x, hi);
210}
211
reed@android.comd4577752009-11-21 02:48:11 +0000212static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000213 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000215 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217}
218
Hal Canaryc36f7e72018-06-18 15:50:09 -0400219/** @return value pinned (clamped) between min and max, inclusively.
220*/
halcanarya0af7712016-05-23 09:11:58 -0700221template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
bungeman62ce0302015-08-28 09:09:32 -0700222 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223}
224
Hal Canaryc36f7e72018-06-18 15:50:09 -0400225////////////////////////////////////////////////////////////////////////////////
bsalomon5ec26ae2016-02-25 08:33:02 -0800226
Hal Canaryc36f7e72018-06-18 15:50:09 -0400227/** Indicates whether an allocation should count against a cache budget.
228*/
bsalomon5ec26ae2016-02-25 08:33:02 -0800229enum class SkBudgeted : bool {
230 kNo = false,
231 kYes = true
232};
233
Hal Canaryc36f7e72018-06-18 15:50:09 -0400234/** Indicates whether a backing store needs to be an exact match or can be
235 larger than is strictly necessary
236*/
robertphillips76948d42016-05-04 12:47:41 -0700237enum class SkBackingFit {
238 kApprox,
239 kExact
240};
241
Hal Canaryc36f7e72018-06-18 15:50:09 -0400242////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243
244/** \class SkNoncopyable
245
Hal Canaryc36f7e72018-06-18 15:50:09 -0400246 SkNoncopyable is the base class for objects that do not want to
247 be copied. It hides its copy-constructor and its assignment-operator.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000249class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250public:
Chris Blume2b6be202017-04-25 17:33:13 -0700251 SkNoncopyable() = default;
reed@google.com1fcd51e2011-01-05 15:50:27 +0000252
Chris Blume2b6be202017-04-25 17:33:13 -0700253 SkNoncopyable(SkNoncopyable&&) = default;
254 SkNoncopyable& operator =(SkNoncopyable&&) = default;
255
Hal Canaryc36f7e72018-06-18 15:50:09 -0400256private:
Chris Blume2b6be202017-04-25 17:33:13 -0700257 SkNoncopyable(const SkNoncopyable&) = delete;
258 SkNoncopyable& operator=(const SkNoncopyable&) = delete;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259};
260
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261#endif