blob: 62ecfd0d57365f430bab6cadd57964d5c36f1cc2 [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
Mike Klein81d35a72020-02-05 10:17:57 -060011/** \file SkTypes.h
12*/
13
14// Pre-SkUserConfig.h setup.
15
16// Allows embedders that want to disable macros that take arguments to just
17// define that symbol to be one of these
18#define SK_NOTHING_ARG1(arg1)
19#define SK_NOTHING_ARG2(arg1, arg2)
20#define SK_NOTHING_ARG3(arg1, arg2, arg3)
21
22#if !defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_WIN) && \
23 !defined(SK_BUILD_FOR_UNIX) && !defined(SK_BUILD_FOR_MAC)
24
25 #ifdef __APPLE__
26 #include "TargetConditionals.h"
27 #endif
28
29 #if defined(_WIN32) || defined(__SYMBIAN32__)
30 #define SK_BUILD_FOR_WIN
31 #elif defined(ANDROID) || defined(__ANDROID__)
32 #define SK_BUILD_FOR_ANDROID
33 #elif defined(linux) || defined(__linux) || defined(__FreeBSD__) || \
34 defined(__OpenBSD__) || defined(__sun) || defined(__NetBSD__) || \
35 defined(__DragonFly__) || defined(__Fuchsia__) || \
36 defined(__GLIBC__) || defined(__GNU__) || defined(__unix__)
37 #define SK_BUILD_FOR_UNIX
38 #elif TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
39 #define SK_BUILD_FOR_IOS
40 #else
41 #define SK_BUILD_FOR_MAC
42 #endif
43
44#endif
45
46#if defined(SK_BUILD_FOR_WIN) && !defined(__clang__)
47 #if !defined(SK_RESTRICT)
48 #define SK_RESTRICT __restrict
49 #endif
50 #if !defined(SK_WARN_UNUSED_RESULT)
51 #define SK_WARN_UNUSED_RESULT
52 #endif
53#endif
54
55#if !defined(SK_RESTRICT)
56 #define SK_RESTRICT __restrict__
57#endif
58
59#if !defined(SK_WARN_UNUSED_RESULT)
60 #define SK_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
61#endif
62
63#if !defined(SK_CPU_BENDIAN) && !defined(SK_CPU_LENDIAN)
64 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
65 #define SK_CPU_BENDIAN
66 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
67 #define SK_CPU_LENDIAN
68 #elif defined(__sparc) || defined(__sparc__) || \
69 defined(_POWER) || defined(__powerpc__) || \
70 defined(__ppc__) || defined(__hppa) || \
71 defined(__PPC__) || defined(__PPC64__) || \
72 defined(_MIPSEB) || defined(__ARMEB__) || \
73 defined(__s390__) || \
74 (defined(__sh__) && defined(__BIG_ENDIAN__)) || \
75 (defined(__ia64) && defined(__BIG_ENDIAN__))
76 #define SK_CPU_BENDIAN
77 #else
78 #define SK_CPU_LENDIAN
79 #endif
80#endif
81
82#if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
83 #define SK_CPU_X86 1
84#endif
85
86/**
87 * SK_CPU_SSE_LEVEL
88 *
89 * If defined, SK_CPU_SSE_LEVEL should be set to the highest supported level.
90 * On non-intel CPU this should be undefined.
91 */
92#define SK_CPU_SSE_LEVEL_SSE1 10
93#define SK_CPU_SSE_LEVEL_SSE2 20
94#define SK_CPU_SSE_LEVEL_SSE3 30
95#define SK_CPU_SSE_LEVEL_SSSE3 31
96#define SK_CPU_SSE_LEVEL_SSE41 41
97#define SK_CPU_SSE_LEVEL_SSE42 42
98#define SK_CPU_SSE_LEVEL_AVX 51
99#define SK_CPU_SSE_LEVEL_AVX2 52
Mike Klein51d35ed2020-04-24 08:16:22 -0500100#define SK_CPU_SSE_LEVEL_SKX 60
Mike Klein81d35a72020-02-05 10:17:57 -0600101
102// When targetting iOS and using gyp to generate the build files, it is not
103// possible to select files to build depending on the architecture (i.e. it
104// is not possible to use hand optimized assembly implementation). In that
105// configuration SK_BUILD_NO_OPTS is defined. Remove optimisation then.
106#ifdef SK_BUILD_NO_OPTS
107 #define SK_CPU_SSE_LEVEL 0
108#endif
109
110// Are we in GCC/Clang?
111#ifndef SK_CPU_SSE_LEVEL
112 // These checks must be done in descending order to ensure we set the highest
113 // available SSE level.
Mike Klein51d35ed2020-04-24 08:16:22 -0500114 #if defined(__AVX512F__) && defined(__AVX512DQ__) && defined(__AVX512CD__) && \
115 defined(__AVX512BW__) && defined(__AVX512VL__)
116 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SKX
Mike Klein81d35a72020-02-05 10:17:57 -0600117 #elif defined(__AVX2__)
118 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_AVX2
119 #elif defined(__AVX__)
120 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_AVX
121 #elif defined(__SSE4_2__)
122 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSE42
123 #elif defined(__SSE4_1__)
124 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSE41
125 #elif defined(__SSSE3__)
126 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSSE3
127 #elif defined(__SSE3__)
128 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSE3
129 #elif defined(__SSE2__)
130 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSE2
131 #endif
132#endif
133
134// Are we in VisualStudio?
135#ifndef SK_CPU_SSE_LEVEL
136 // These checks must be done in descending order to ensure we set the highest
137 // available SSE level. 64-bit intel guarantees at least SSE2 support.
Mike Klein51d35ed2020-04-24 08:16:22 -0500138 #if defined(__AVX512F__) && defined(__AVX512DQ__) && defined(__AVX512CD__) && \
139 defined(__AVX512BW__) && defined(__AVX512VL__)
140 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SKX
141 #elif defined(__AVX2__)
Mike Klein81d35a72020-02-05 10:17:57 -0600142 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_AVX2
143 #elif defined(__AVX__)
144 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_AVX
145 #elif defined(_M_X64) || defined(_M_AMD64)
146 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSE2
147 #elif defined(_M_IX86_FP)
148 #if _M_IX86_FP >= 2
149 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSE2
150 #elif _M_IX86_FP == 1
151 #define SK_CPU_SSE_LEVEL SK_CPU_SSE_LEVEL_SSE1
152 #endif
153 #endif
154#endif
155
156// ARM defines
157#if defined(__arm__) && (!defined(__APPLE__) || !TARGET_IPHONE_SIMULATOR)
158 #define SK_CPU_ARM32
159#elif defined(__aarch64__) && !defined(SK_BUILD_NO_OPTS)
160 #define SK_CPU_ARM64
161#endif
162
163// All 64-bit ARM chips have NEON. Many 32-bit ARM chips do too.
164#if !defined(SK_ARM_HAS_NEON) && !defined(SK_BUILD_NO_OPTS) && defined(__ARM_NEON)
165 #define SK_ARM_HAS_NEON
166#endif
167
Mike Klein62a0f152020-08-25 16:40:25 -0500168#if defined(__ARM_FEATURE_CRC32)
Mike Klein81d35a72020-02-05 10:17:57 -0600169 #define SK_ARM_HAS_CRC32
170#endif
171
172
173// DLL/.so exports.
174#if !defined(SKIA_IMPLEMENTATION)
175 #define SKIA_IMPLEMENTATION 0
176#endif
177#if !defined(SK_API)
178 #if defined(SKIA_DLL)
179 #if defined(_MSC_VER)
180 #if SKIA_IMPLEMENTATION
181 #define SK_API __declspec(dllexport)
182 #else
183 #define SK_API __declspec(dllimport)
184 #endif
185 #else
186 #define SK_API __attribute__((visibility("default")))
187 #endif
188 #else
189 #define SK_API
190 #endif
191#endif
192
193// SK_SPI is functionally identical to SK_API, but used within src to clarify that it's less stable
194#if !defined(SK_SPI)
195 #define SK_SPI SK_API
196#endif
197
bungemanf20488b2015-07-29 11:49:40 -0700198// IWYU pragma: begin_exports
Mike Kleindbbefa82019-04-23 11:11:35 -0500199#if defined (SK_USER_CONFIG_HEADER)
200 #include SK_USER_CONFIG_HEADER
201#else
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500202 #include "include/config/SkUserConfig.h"
Mike Kleindbbefa82019-04-23 11:11:35 -0500203#endif
bungemanf20488b2015-07-29 11:49:40 -0700204#include <stddef.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +0000205#include <stdint.h>
bungemanf20488b2015-07-29 11:49:40 -0700206// IWYU pragma: end_exports
207
Mike Klein81d35a72020-02-05 10:17:57 -0600208// Post SkUserConfig.h checks and such.
209#if !defined(SK_DEBUG) && !defined(SK_RELEASE)
210 #ifdef NDEBUG
211 #define SK_RELEASE
212 #else
213 #define SK_DEBUG
214 #endif
215#endif
216
217#if defined(SK_DEBUG) && defined(SK_RELEASE)
218# error "cannot define both SK_DEBUG and SK_RELEASE"
219#elif !defined(SK_DEBUG) && !defined(SK_RELEASE)
220# error "must define either SK_DEBUG or SK_RELEASE"
221#endif
222
223#if defined(SK_CPU_LENDIAN) && defined(SK_CPU_BENDIAN)
224# error "cannot define both SK_CPU_LENDIAN and SK_CPU_BENDIAN"
225#elif !defined(SK_CPU_LENDIAN) && !defined(SK_CPU_BENDIAN)
226# error "must define either SK_CPU_LENDIAN or SK_CPU_BENDIAN"
227#endif
228
229#if defined(SK_CPU_BENDIAN) && !defined(I_ACKNOWLEDGE_SKIA_DOES_NOT_SUPPORT_BIG_ENDIAN)
230 #error "The Skia team is not endian-savvy enough to support big-endian CPUs."
231 #error "If you still want to use Skia,"
232 #error "please define I_ACKNOWLEDGE_SKIA_DOES_NOT_SUPPORT_BIG_ENDIAN."
233#endif
234
235#if !defined(SK_ATTRIBUTE)
236# if defined(__clang__) || defined(__GNUC__)
237# define SK_ATTRIBUTE(attr) __attribute__((attr))
238# else
239# define SK_ATTRIBUTE(attr)
240# endif
241#endif
242
243#if !defined(SK_SUPPORT_GPU)
244# define SK_SUPPORT_GPU 1
245#endif
246
Brian Salomon17dc63f2020-07-30 13:54:53 -0400247#if !SK_SUPPORT_GPU
Brian Salomonf4ba4ec2020-03-19 15:54:28 -0400248# undef SK_GL
249# undef SK_VULKAN
250# undef SK_METAL
251# undef SK_DAWN
252# undef SK_DIRECT3D
Mike Klein81d35a72020-02-05 10:17:57 -0600253#endif
254
Mike Klein81d35a72020-02-05 10:17:57 -0600255#if !defined(SkUNREACHABLE)
256# if defined(_MSC_VER) && !defined(__clang__)
Ben Wagner07953612020-06-17 11:24:53 -0400257# include <intrin.h>
258# define FAST_FAIL_INVALID_ARG 5
259// See https://developercommunity.visualstudio.com/content/problem/1128631/code-flow-doesnt-see-noreturn-with-extern-c.html
260// for why this is wrapped. Hopefully removable after msvc++ 19.27 is no longer supported.
261[[noreturn]] static inline void sk_fast_fail() { __fastfail(FAST_FAIL_INVALID_ARG); }
262# define SkUNREACHABLE sk_fast_fail()
Mike Klein81d35a72020-02-05 10:17:57 -0600263# else
Ben Wagner07953612020-06-17 11:24:53 -0400264# define SkUNREACHABLE __builtin_trap()
Mike Klein81d35a72020-02-05 10:17:57 -0600265# endif
266#endif
267
268#if defined(SK_BUILD_FOR_GOOGLE3)
269 void SkDebugfForDumpStackTrace(const char* data, void* unused);
270 void DumpStackTrace(int skip_count, void w(const char*, void*), void* arg);
271# define SK_DUMP_GOOGLE3_STACK() DumpStackTrace(0, SkDebugfForDumpStackTrace, nullptr)
272#else
273# define SK_DUMP_GOOGLE3_STACK()
274#endif
275
Mike Klein81d35a72020-02-05 10:17:57 -0600276#ifndef SK_ABORT
John Stiles616da102020-06-12 14:07:41 -0400277# ifdef SK_BUILD_FOR_WIN
278 // This style lets Visual Studio follow errors back to the source file.
279# define SK_DUMP_LINE_FORMAT "%s(%d)"
280# else
281# define SK_DUMP_LINE_FORMAT "%s:%d"
282# endif
283# define SK_ABORT(message, ...) \
Mike Klein81d35a72020-02-05 10:17:57 -0600284 do { \
John Stiles616da102020-06-12 14:07:41 -0400285 SkDebugf(SK_DUMP_LINE_FORMAT ": fatal error: \"" message "\"\n", \
286 __FILE__, __LINE__, ##__VA_ARGS__); \
287 SK_DUMP_GOOGLE3_STACK(); \
288 sk_abort_no_print(); \
Mike Klein81d35a72020-02-05 10:17:57 -0600289 } while (false)
290#endif
291
Mike Reeda04c91e2020-02-10 16:51:10 +0000292// If SK_R32_SHIFT is set, we'll use that to choose RGBA or BGRA.
293// If not, we'll default to RGBA everywhere except BGRA on Windows.
294#if defined(SK_R32_SHIFT)
295 static_assert(SK_R32_SHIFT == 0 || SK_R32_SHIFT == 16, "");
296#elif defined(SK_BUILD_FOR_WIN)
297 #define SK_R32_SHIFT 16
298#else
299 #define SK_R32_SHIFT 0
300#endif
301
302#if defined(SK_B32_SHIFT)
303 static_assert(SK_B32_SHIFT == (16-SK_R32_SHIFT), "");
304#else
305 #define SK_B32_SHIFT (16-SK_R32_SHIFT)
306#endif
307
Mike Klein81d35a72020-02-05 10:17:57 -0600308#define SK_G32_SHIFT 8
309#define SK_A32_SHIFT 24
310
Mike Reeda04c91e2020-02-10 16:51:10 +0000311
Mike Klein81d35a72020-02-05 10:17:57 -0600312/**
John Stiles9f987c32020-05-27 14:12:26 -0400313 * SK_PMCOLOR_BYTE_ORDER can be used to query the byte order of SkPMColor at compile time.
Mike Klein81d35a72020-02-05 10:17:57 -0600314 */
315#ifdef SK_CPU_BENDIAN
316# define SK_PMCOLOR_BYTE_ORDER(C0, C1, C2, C3) \
317 (SK_ ## C3 ## 32_SHIFT == 0 && \
318 SK_ ## C2 ## 32_SHIFT == 8 && \
319 SK_ ## C1 ## 32_SHIFT == 16 && \
320 SK_ ## C0 ## 32_SHIFT == 24)
321#else
322# define SK_PMCOLOR_BYTE_ORDER(C0, C1, C2, C3) \
323 (SK_ ## C0 ## 32_SHIFT == 0 && \
324 SK_ ## C1 ## 32_SHIFT == 8 && \
325 SK_ ## C2 ## 32_SHIFT == 16 && \
326 SK_ ## C3 ## 32_SHIFT == 24)
327#endif
328
329#if defined SK_DEBUG && defined SK_BUILD_FOR_WIN
330 #ifdef free
331 #undef free
332 #endif
333 #include <crtdbg.h>
334 #undef free
335#endif
336
337#if !defined(SK_UNUSED)
338# if !defined(__clang__) && defined(_MSC_VER)
339# define SK_UNUSED __pragma(warning(suppress:4189))
340# else
341# define SK_UNUSED SK_ATTRIBUTE(unused)
342# endif
343#endif
344
345/**
346 * If your judgment is better than the compiler's (i.e. you've profiled it),
347 * you can use SK_ALWAYS_INLINE to force inlining. E.g.
348 * inline void someMethod() { ... } // may not be inlined
349 * SK_ALWAYS_INLINE void someMethod() { ... } // should always be inlined
350 */
351#if !defined(SK_ALWAYS_INLINE)
352# if defined(SK_BUILD_FOR_WIN)
353# define SK_ALWAYS_INLINE __forceinline
354# else
355# define SK_ALWAYS_INLINE SK_ATTRIBUTE(always_inline) inline
356# endif
357#endif
358
359/**
360 * If your judgment is better than the compiler's (i.e. you've profiled it),
361 * you can use SK_NEVER_INLINE to prevent inlining.
362 */
363#if !defined(SK_NEVER_INLINE)
364# if defined(SK_BUILD_FOR_WIN)
365# define SK_NEVER_INLINE __declspec(noinline)
366# else
367# define SK_NEVER_INLINE SK_ATTRIBUTE(noinline)
368# endif
369#endif
370
371#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
372 #define SK_PREFETCH(ptr) _mm_prefetch(reinterpret_cast<const char*>(ptr), _MM_HINT_T0)
373#elif defined(__GNUC__)
374 #define SK_PREFETCH(ptr) __builtin_prefetch(ptr)
375#else
376 #define SK_PREFETCH(ptr)
377#endif
378
379#ifndef SK_PRINTF_LIKE
380# if defined(__clang__) || defined(__GNUC__)
381# define SK_PRINTF_LIKE(A, B) __attribute__((format(printf, (A), (B))))
382# else
383# define SK_PRINTF_LIKE(A, B)
384# endif
385#endif
386
Mike Klein81d35a72020-02-05 10:17:57 -0600387#ifndef SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
388 #define SK_ALLOW_STATIC_GLOBAL_INITIALIZERS 0
389#endif
390
391#if !defined(SK_GAMMA_EXPONENT)
392 #define SK_GAMMA_EXPONENT (0.0f) // SRGB
393#endif
394
395#ifndef GR_TEST_UTILS
396# define GR_TEST_UTILS 0
397#endif
398
Greg Daniel65476e02020-10-27 09:20:20 -0400399#if defined(SK_HISTOGRAM_ENUMERATION) || \
400 defined(SK_HISTOGRAM_BOOLEAN) || \
Greg Daniel771c7502020-10-29 10:24:54 -0400401 defined(SK_HISTOGRAM_EXACT_LINEAR) || \
402 defined(SK_HISTOGRAM_MEMORY_KB)
Mike Klein81d35a72020-02-05 10:17:57 -0600403# define SK_HISTOGRAMS_ENABLED 1
404#else
405# define SK_HISTOGRAMS_ENABLED 0
406#endif
407
408#ifndef SK_HISTOGRAM_BOOLEAN
Greg Daniel65476e02020-10-27 09:20:20 -0400409# define SK_HISTOGRAM_BOOLEAN(name, sample)
Mike Klein81d35a72020-02-05 10:17:57 -0600410#endif
411
412#ifndef SK_HISTOGRAM_ENUMERATION
Greg Daniel65476e02020-10-27 09:20:20 -0400413# define SK_HISTOGRAM_ENUMERATION(name, sample, enum_size)
414#endif
415
416#ifndef SK_HISTOGRAM_EXACT_LINEAR
Greg Daniel771c7502020-10-29 10:24:54 -0400417# define SK_HISTOGRAM_EXACT_LINEAR(name, sample, value_max)
Mike Klein81d35a72020-02-05 10:17:57 -0600418#endif
419
Greg Daniel771c7502020-10-29 10:24:54 -0400420#ifndef SK_HISTOGRAM_MEMORY_KB
421# define SK_HISTOGRAM_MEMORY_KB(name, sample)
422#endif
423
424#define SK_HISTOGRAM_PERCENTAGE(name, percent_as_int) \
425 SK_HISTOGRAM_EXACT_LINEAR(name, percent_as_int, 101)
426
Mike Klein81d35a72020-02-05 10:17:57 -0600427#ifndef SK_DISABLE_LEGACY_SHADERCONTEXT
428#define SK_ENABLE_LEGACY_SHADERCONTEXT
429#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000430
Chinmay Gardef832c0a2020-03-18 14:37:23 -0700431#ifdef SK_ENABLE_API_AVAILABLE
432#define SK_API_AVAILABLE API_AVAILABLE
433#else
434#define SK_API_AVAILABLE(...)
435#endif
436
Kevin Lubick493f89e2020-09-14 08:37:35 -0400437#if defined(SK_BUILD_FOR_LIBFUZZER) || defined(SK_BUILD_FOR_AFL_FUZZ)
438 #define SK_BUILD_FOR_FUZZER
439#endif
440
reed@android.com8a1c16f2008-12-17 15:59:43 +0000441/** Called internally if we hit an unrecoverable error.
442 The platform implementation must not return, but should either throw
443 an exception or otherwise exit.
444*/
Ben Wagner1e67f7f2020-07-28 10:26:57 -0400445[[noreturn]] SK_API extern void sk_abort_no_print(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700448 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449#endif
Kevin Lubick272e8bc2020-09-15 10:05:41 -0400450#if defined(SK_BUILD_FOR_LIBFUZZER)
451 SK_API inline void SkDebugf(const char format[], ...) {}
452#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453
Mike Klein37bbfe32017-09-28 09:47:45 -0400454// SkASSERT, SkASSERTF and SkASSERT_RELEASE can be used as stand alone assertion expressions, e.g.
455// uint32_t foo(int x) {
456// SkASSERT(x > 4);
457// return x - 4;
458// }
459// and are also written to be compatible with constexpr functions:
460// constexpr uint32_t foo(int x) {
461// return SkASSERT(x > 4),
462// x - 4;
463// }
caryclarkd6562002016-07-27 12:02:07 -0700464#define SkASSERT_RELEASE(cond) \
John Stiles616da102020-06-12 14:07:41 -0400465 static_cast<void>( (cond) ? (void)0 : []{ SK_ABORT("assert(%s)", #cond); }() )
djsollenf2b340f2016-01-29 08:51:04 -0800466
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467#ifdef SK_DEBUG
Mike Klein37bbfe32017-09-28 09:47:45 -0400468 #define SkASSERT(cond) SkASSERT_RELEASE(cond)
469 #define SkASSERTF(cond, fmt, ...) static_cast<void>( (cond) ? (void)0 : [&]{ \
470 SkDebugf(fmt"\n", __VA_ARGS__); \
John Stiles616da102020-06-12 14:07:41 -0400471 SK_ABORT("assert(%s)", #cond); \
Mike Klein37bbfe32017-09-28 09:47:45 -0400472 }() )
John Stiles616da102020-06-12 14:07:41 -0400473 #define SkDEBUGFAIL(message) SK_ABORT("%s", message)
474 #define SkDEBUGFAILF(fmt, ...) SK_ABORT(fmt, ##__VA_ARGS__)
csmartdaltonceeaa782016-08-10 10:07:57 -0700475 #define SkDEBUGCODE(...) __VA_ARGS__
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400476 #define SkDEBUGF(...) SkDebugf(__VA_ARGS__)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477 #define SkAssertResult(cond) SkASSERT(cond)
478#else
Mike Klein37bbfe32017-09-28 09:47:45 -0400479 #define SkASSERT(cond) static_cast<void>(0)
480 #define SkASSERTF(cond, fmt, ...) static_cast<void>(0)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000481 #define SkDEBUGFAIL(message)
hsterne6f8ff02016-08-15 15:26:31 -0700482 #define SkDEBUGFAILF(fmt, ...)
csmartdaltonceeaa782016-08-10 10:07:57 -0700483 #define SkDEBUGCODE(...)
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400484 #define SkDEBUGF(...)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485
Hal Canaryc36f7e72018-06-18 15:50:09 -0400486 // unlike SkASSERT, this macro executes its condition in the non-debug build.
bsalomon1b4c01c2016-05-09 12:35:17 -0700487 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
488 #define SkAssertResult(cond) if (cond) {} do {} while(false)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489#endif
490
Hal Canarybc718c12018-06-12 14:16:11 -0400491////////////////////////////////////////////////////////////////////////////////
492
Hal Canaryc36f7e72018-06-18 15:50:09 -0400493/** Fast type for unsigned 8 bits. Use for parameter passing and local
494 variables, not for storage
495*/
reed@google.com37a31332011-01-25 14:55:42 +0000496typedef unsigned U8CPU;
497
Hal Canaryc36f7e72018-06-18 15:50:09 -0400498/** Fast type for unsigned 16 bits. Use for parameter passing and local
499 variables, not for storage
500*/
reed@google.com37a31332011-01-25 14:55:42 +0000501typedef unsigned U16CPU;
502
Hal Canaryc36f7e72018-06-18 15:50:09 -0400503/** @return false or true based on the condition
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504*/
John Stilesfe0de302020-08-14 10:52:06 -0400505template <typename T> static constexpr bool SkToBool(const T& x) {
506 return 0 != x; // NOLINT(modernize-use-nullptr)
507}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000508
Hal Canaryc36f7e72018-06-18 15:50:09 -0400509static constexpr int16_t SK_MaxS16 = INT16_MAX;
510static constexpr int16_t SK_MinS16 = -SK_MaxS16;
511
512static constexpr int32_t SK_MaxS32 = INT32_MAX;
513static constexpr int32_t SK_MinS32 = -SK_MaxS32;
514static constexpr int32_t SK_NaN32 = INT32_MIN;
515
Ben Wagnerb0897652018-06-15 15:37:57 +0000516static constexpr int64_t SK_MaxS64 = INT64_MAX;
Mike Reed3d5a6b52018-01-31 15:55:47 -0500517static constexpr int64_t SK_MinS64 = -SK_MaxS64;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518
Mike Kleind9187c02018-09-07 17:32:47 +0000519static inline constexpr int32_t SkLeftShift(int32_t value, int32_t shift) {
520 return (int32_t) ((uint32_t) value << shift);
521}
522
523static inline constexpr int64_t SkLeftShift(int64_t value, int32_t shift) {
524 return (int64_t) ((uint64_t) value << shift);
525}
526
Hal Canaryc36f7e72018-06-18 15:50:09 -0400527////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528
Hal Canaryc36f7e72018-06-18 15:50:09 -0400529/** @return the number of entries in an array (not a pointer)
530*/
mtkleinfc00a7c2015-05-07 10:58:44 -0700531template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700532#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533
Mike Kleind9187c02018-09-07 17:32:47 +0000534////////////////////////////////////////////////////////////////////////////////
535
536template <typename T> static constexpr T SkAlign2(T x) { return (x + 1) >> 1 << 1; }
537template <typename T> static constexpr T SkAlign4(T x) { return (x + 3) >> 2 << 2; }
538template <typename T> static constexpr T SkAlign8(T x) { return (x + 7) >> 3 << 3; }
539
540template <typename T> static constexpr bool SkIsAlign2(T x) { return 0 == (x & 1); }
541template <typename T> static constexpr bool SkIsAlign4(T x) { return 0 == (x & 3); }
542template <typename T> static constexpr bool SkIsAlign8(T x) { return 0 == (x & 7); }
543
544template <typename T> static constexpr T SkAlignPtr(T x) {
545 return sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x);
546}
547template <typename T> static constexpr bool SkIsAlignPtr(T x) {
548 return sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x);
549}
550
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551typedef uint32_t SkFourByteTag;
Hal Canarybb9ee9b2018-06-12 16:47:47 -0400552static inline constexpr SkFourByteTag SkSetFourByteTag(char a, char b, char c, char d) {
Mike Klein89c909e2020-06-01 15:50:19 -0500553 return (((uint32_t)a << 24) | ((uint32_t)b << 16) | ((uint32_t)c << 8) | (uint32_t)d);
Hal Canarybc718c12018-06-12 14:16:11 -0400554}
555
556////////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000557
558/** 32 bit integer to hold a unicode value
559*/
560typedef int32_t SkUnichar;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700561
halcanaryd0e95a52016-07-25 07:18:12 -0700562/** 16 bit unsigned integer to hold a glyph index
563*/
564typedef uint16_t SkGlyphID;
565
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700566/** 32 bit value to hold a millisecond duration
Hal Canaryc36f7e72018-06-18 15:50:09 -0400567 Note that SK_MSecMax is about 25 days.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568*/
Hal Canaryc36f7e72018-06-18 15:50:09 -0400569typedef uint32_t SkMSec;
570
571/** Maximum representable milliseconds; 24d 20h 31m 23.647s.
572*/
573static constexpr SkMSec SK_MSecMax = INT32_MAX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000574
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000575/** The generation IDs in Skia reserve 0 has an invalid marker.
Hal Canaryc36f7e72018-06-18 15:50:09 -0400576*/
577static constexpr uint32_t SK_InvalidGenID = 0;
578
bsalomon1c63bf62014-07-22 13:09:46 -0700579/** The unique IDs in Skia reserve 0 has an invalid marker.
Hal Canaryc36f7e72018-06-18 15:50:09 -0400580*/
581static constexpr uint32_t SK_InvalidUniqueID = 0;
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000582
Mike Kleind9187c02018-09-07 17:32:47 +0000583static inline int32_t SkAbs32(int32_t value) {
584 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
585 if (value < 0) {
586 value = -value;
587 }
588 return value;
589}
590
591template <typename T> static inline T SkTAbs(T value) {
592 if (value < 0) {
593 value = -value;
594 }
595 return value;
596}
597
Hal Canaryc36f7e72018-06-18 15:50:09 -0400598////////////////////////////////////////////////////////////////////////////////
bsalomon5ec26ae2016-02-25 08:33:02 -0800599
Hal Canaryc36f7e72018-06-18 15:50:09 -0400600/** Indicates whether an allocation should count against a cache budget.
601*/
bsalomon5ec26ae2016-02-25 08:33:02 -0800602enum class SkBudgeted : bool {
603 kNo = false,
604 kYes = true
605};
606
Hal Canaryc36f7e72018-06-18 15:50:09 -0400607/** Indicates whether a backing store needs to be an exact match or can be
608 larger than is strictly necessary
609*/
robertphillips76948d42016-05-04 12:47:41 -0700610enum class SkBackingFit {
611 kApprox,
612 kExact
613};
614
reed@android.com8a1c16f2008-12-17 15:59:43 +0000615#endif