1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkTypes_DEFINED
9 #define SkTypes_DEFINED
10
11 // IWYU pragma: begin_exports
12 #include "include/core/SkPreConfig.h"
13 #if defined (SK_USER_CONFIG_HEADER)
14 #include SK_USER_CONFIG_HEADER
15 #else
16 #include "include/config/SkUserConfig.h"
17 #endif
18 #include "include/core/SkPostConfig.h"
19 #include <stddef.h>
20 #include <stdint.h>
21 // IWYU pragma: end_exports
22
23 /** \file SkTypes.h
24 */
25
26 /** Called internally if we hit an unrecoverable error.
27 The platform implementation must not return, but should either throw
28 an exception or otherwise exit.
29 */
30 SK_API extern void sk_abort_no_print(void);
31
32 #ifndef SkDebugf
33 SK_API void SkDebugf(const char format[], ...);
34 #endif
35
36 // SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as stand alone assertion expressions, e.g.
37 // uint32_t foo(int x) {
38 // SkASSERT(x > 4);
39 // return x - 4;
40 // }
41 // and are also written to be compatible with constexpr functions:
42 // constexpr uint32_t foo(int x) {
43 // return SkASSERT(x > 4),
44 // x - 4;
45 // }
46 #define SkASSERT_RELEASE(cond) \
47 static_cast<void>( (cond) ? (void)0 : []{ SK_ABORT("assert(" #cond ")"); }() )
48
49 #ifdef SK_DEBUG
50 #define SkASSERT(cond) SkASSERT_RELEASE(cond)
51 #define SkASSERTF(cond, fmt, ...) static_cast<void>( (cond) ? (void)0 : [&]{ \
52 SkDebugf(fmt"\n", __VA_ARGS__); \
53 SK_ABORT("assert(" #cond ")"); \
54 }() )
55 #define SkDEBUGFAIL(message) SK_ABORT(message)
56 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
57 #define SkDEBUGCODE(...) __VA_ARGS__
58 #define SkDEBUGF(...) SkDebugf(__VA_ARGS__)
59 #define SkAssertResult(cond) SkASSERT(cond)
60 #else
61 #define SkASSERT(cond) static_cast<void>(0)
62 #define SkASSERTF(cond, fmt, ...) static_cast<void>(0)
63 #define SkDEBUGFAIL(message)
64 #define SkDEBUGFAILF(fmt, ...)
65 #define SkDEBUGCODE(...)
66 #define SkDEBUGF(...)
67
68 // unlike SkASSERT, this macro executes its condition in the non-debug build.
69 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
70 #define SkAssertResult(cond) if (cond) {} do {} while(false)
71 #endif
72
73 ////////////////////////////////////////////////////////////////////////////////
74
75 /** Fast type for unsigned 8 bits. Use for parameter passing and local
76 variables, not for storage
77 */
78 typedef unsigned U8CPU;
79
80 /** Fast type for unsigned 16 bits. Use for parameter passing and local
81 variables, not for storage
82 */
83 typedef unsigned U16CPU;
84
85 /** @return false or true based on the condition
86 */
SkToBool(const T & x)87 template <typename T> static constexpr bool SkToBool(const T& x) { return 0 != x; }
88
89 static constexpr int16_t SK_MaxS16 = INT16_MAX;
90 static constexpr int16_t SK_MinS16 = -SK_MaxS16;
91
92 static constexpr int32_t SK_MaxS32 = INT32_MAX;
93 static constexpr int32_t SK_MinS32 = -SK_MaxS32;
94 static constexpr int32_t SK_NaN32 = INT32_MIN;
95
96 static constexpr int64_t SK_MaxS64 = INT64_MAX;
97 static constexpr int64_t SK_MinS64 = -SK_MaxS64;
98
SkLeftShift(int32_t value,int32_t shift)99 static inline constexpr int32_t SkLeftShift(int32_t value, int32_t shift) {
100 return (int32_t) ((uint32_t) value << shift);
101 }
102
SkLeftShift(int64_t value,int32_t shift)103 static inline constexpr int64_t SkLeftShift(int64_t value, int32_t shift) {
104 return (int64_t) ((uint64_t) value << shift);
105 }
106
107 ////////////////////////////////////////////////////////////////////////////////
108
109 /** @return the number of entries in an array (not a pointer)
110 */
111 template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
112 #define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
113
114 ////////////////////////////////////////////////////////////////////////////////
115
SkAlign2(T x)116 template <typename T> static constexpr T SkAlign2(T x) { return (x + 1) >> 1 << 1; }
SkAlign4(T x)117 template <typename T> static constexpr T SkAlign4(T x) { return (x + 3) >> 2 << 2; }
SkAlign8(T x)118 template <typename T> static constexpr T SkAlign8(T x) { return (x + 7) >> 3 << 3; }
119
SkIsAlign2(T x)120 template <typename T> static constexpr bool SkIsAlign2(T x) { return 0 == (x & 1); }
SkIsAlign4(T x)121 template <typename T> static constexpr bool SkIsAlign4(T x) { return 0 == (x & 3); }
SkIsAlign8(T x)122 template <typename T> static constexpr bool SkIsAlign8(T x) { return 0 == (x & 7); }
123
SkAlignPtr(T x)124 template <typename T> static constexpr T SkAlignPtr(T x) {
125 return sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x);
126 }
SkIsAlignPtr(T x)127 template <typename T> static constexpr bool SkIsAlignPtr(T x) {
128 return sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x);
129 }
130
131 typedef uint32_t SkFourByteTag;
SkSetFourByteTag(char a,char b,char c,char d)132 static inline constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d) {
133 return (((uint8_t)a << 24) | ((uint8_t)b << 16) | ((uint8_t)c << 8) | (uint8_t)d);
134 }
135
136 ////////////////////////////////////////////////////////////////////////////////
137
138 /** 32 bit integer to hold a unicode value
139 */
140 typedef int32_t SkUnichar;
141
142 /** 16 bit unsigned integer to hold a glyph index
143 */
144 typedef uint16_t SkGlyphID;
145
146 /** 32 bit value to hold a millisecond duration
147 Note that SK_MSecMax is about 25 days.
148 */
149 typedef uint32_t SkMSec;
150
151 /** Maximum representable milliseconds; 24d 20h 31m 23.647s.
152 */
153 static constexpr SkMSec SK_MSecMax = INT32_MAX;
154
155 /** The generation IDs in Skia reserve 0 has an invalid marker.
156 */
157 static constexpr uint32_t SK_InvalidGenID = 0;
158
159 /** The unique IDs in Skia reserve 0 has an invalid marker.
160 */
161 static constexpr uint32_t SK_InvalidUniqueID = 0;
162
SkAbs32(int32_t value)163 static inline int32_t SkAbs32(int32_t value) {
164 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
165 if (value < 0) {
166 value = -value;
167 }
168 return value;
169 }
170
SkTAbs(T value)171 template <typename T> static inline T SkTAbs(T value) {
172 if (value < 0) {
173 value = -value;
174 }
175 return value;
176 }
177
SkMax32(int32_t a,int32_t b)178 static inline int32_t SkMax32(int32_t a, int32_t b) {
179 if (a < b)
180 a = b;
181 return a;
182 }
183
SkMin32(int32_t a,int32_t b)184 static inline int32_t SkMin32(int32_t a, int32_t b) {
185 if (a > b)
186 a = b;
187 return a;
188 }
189
SkTMin(const T & a,const T & b)190 template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
191 return (a < b) ? a : b;
192 }
193
SkTMax(const T & a,const T & b)194 template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
195 return (b < a) ? a : b;
196 }
197
SkTClamp(const T & x,const T & lo,const T & hi)198 template <typename T> constexpr const T& SkTClamp(const T& x, const T& lo, const T& hi) {
199 return (x < lo) ? lo : SkTMin(x, hi);
200 }
201
202 /** @return value pinned (clamped) between min and max, inclusively.
203 */
SkTPin(const T & value,const T & min,const T & max)204 template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
205 return SkTMax(SkTMin(value, max), min);
206 }
207
208 ////////////////////////////////////////////////////////////////////////////////
209
210 /** Indicates whether an allocation should count against a cache budget.
211 */
212 enum class SkBudgeted : bool {
213 kNo = false,
214 kYes = true
215 };
216
217 /** Indicates whether a backing store needs to be an exact match or can be
218 larger than is strictly necessary
219 */
220 enum class SkBackingFit {
221 kApprox,
222 kExact
223 };
224
225 #endif
226