blob: 8baf246d34c5b80bdc808874495276e57b48b6f8 [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
bsalomonf48c62f2016-07-08 03:28:42 -070012
13// In at least two known scenarios when using GCC with libc++:
14// * GCC 4.8 targeting ARMv7 with NEON
15// * GCC 4.9 targeting ARMv8 64 bit
16// we need to typedef float float32_t (or include <arm_neon.h> which does that)
17// before #including <memory>. This makes no sense. I'm not very interested in
18// understanding why... these are old, bizarre platform configuration that we
19// should just let die.
bungeman7ad42cf2016-07-08 12:25:37 -070020// See https://llvm.org/bugs/show_bug.cgi?id=25608 .
bsalomonf48c62f2016-07-08 03:28:42 -070021#include <ciso646> // Include something innocuous to define _LIBCPP_VERISON if it's libc++.
22#if defined(__GNUC__) && __GNUC__ == 4 \
bungeman7ad42cf2016-07-08 12:25:37 -070023 && ((defined(__arm__) && (defined(__ARM_NEON__) || defined(__ARM_NEON))) || defined(__aarch64__)) \
bsalomonf48c62f2016-07-08 03:28:42 -070024 && defined(_LIBCPP_VERSION)
25 typedef float float32_t;
26 #include <memory>
27#endif
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#include "SkPreConfig.h"
30#include "SkUserConfig.h"
31#include "SkPostConfig.h"
bungemanf20488b2015-07-29 11:49:40 -070032#include <stddef.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +000033#include <stdint.h>
bungemanf20488b2015-07-29 11:49:40 -070034// IWYU pragma: end_exports
35
bungemanf20488b2015-07-29 11:49:40 -070036#include <string.h>
mtklein95cc0122015-04-27 15:11:01 -070037
Mike Reedea5e6762017-03-02 20:22:35 +000038// enable to test new device-base clipping
39//#define SK_USE_DEVICE_CLIPPING
40
mtkleincc881da2015-12-08 11:55:17 -080041/**
42 * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
43 *
44 * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
45 * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
46 * memcpy(dst, src, 0);
47 * if (src) {
48 * printf("%x\n", *src);
49 * }
50 * In this code the compiler can assume src is not null and omit the if (src) {...} check,
51 * unconditionally running the printf, crashing the program if src really is null.
52 * Of the compilers we pay attention to only GCC performs this optimization in practice.
53 */
54static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
55 // When we pass >0 len we had better already be passing valid pointers.
56 // So we just need to skip calling memcpy when len == 0.
57 if (len) {
58 memcpy(dst,src,len);
59 }
60 return dst;
61}
62
reed@android.com8a1c16f2008-12-17 15:59:43 +000063/** \file SkTypes.h
64*/
65
reed@android.com9aa8b322010-04-13 13:22:54 +000066/** See SkGraphics::GetVersion() to retrieve these at runtime
67 */
68#define SKIA_VERSION_MAJOR 1
69#define SKIA_VERSION_MINOR 0
70#define SKIA_VERSION_PATCH 0
71
reed@android.com8a1c16f2008-12-17 15:59:43 +000072/*
73 memory wrappers to be implemented by the porting layer (platform)
74*/
75
76/** Called internally if we run out of memory. The platform implementation must
77 not return, but should either throw an exception or otherwise exit.
78*/
reed@google.comde916c82011-10-19 19:50:48 +000079SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080/** Called internally if we hit an unrecoverable error.
81 The platform implementation must not return, but should either throw
82 an exception or otherwise exit.
83*/
djsollenf2b340f2016-01-29 08:51:04 -080084SK_API extern void sk_abort_no_print(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
86enum {
87 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
88 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
89};
90/** Return a block of memory (at least 4-byte aligned) of at least the
91 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000092 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
94*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000095SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
97*/
reed@google.comde916c82011-10-19 19:50:48 +000098SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000099/** Same as standard realloc(), but this one never returns null on failure. It will throw
100 an exception if it fails.
101*/
reed@google.comde916c82011-10-19 19:50:48 +0000102SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103/** Free memory returned by sk_malloc(). It is safe to pass null.
104*/
reed@google.comde916c82011-10-19 19:50:48 +0000105SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106
mtklein@google.com519f9672013-09-20 14:31:45 +0000107/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
108 */
109SK_API extern void* sk_calloc(size_t size);
110
111/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
112 */
113SK_API extern void* sk_calloc_throw(size_t size);
114
reed@android.com4516f472009-06-29 16:25:36 +0000115// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
116static inline void sk_bzero(void* buffer, size_t size) {
mtklein02046c52015-12-09 10:02:14 -0800117 // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0).
118 if (size) {
119 memset(buffer, 0, size);
120 }
reed@android.com4516f472009-06-29 16:25:36 +0000121}
122
reed@google.combdf73612011-09-06 14:56:20 +0000123///////////////////////////////////////////////////////////////////////////////
124
mtklein36352bf2015-03-25 18:17:31 -0700125#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +0000126#include <new>
127
128inline void* operator new(size_t size) {
129 return sk_malloc_throw(size);
130}
131
132inline void operator delete(void* p) {
133 sk_free(p);
134}
135#endif
136
137///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000138
139#define SK_INIT_TO_AVOID_WARNING = 0
140
141#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700142 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000143#endif
144
caryclarkd6562002016-07-27 12:02:07 -0700145#define SkREQUIRE_SEMICOLON_AFTER(code) do { code } while (false)
146
147#define SkASSERT_RELEASE(cond) \
148 SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT(#cond); } )
djsollenf2b340f2016-01-29 08:51:04 -0800149
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150#ifdef SK_DEBUG
caryclarkd6562002016-07-27 12:02:07 -0700151 #define SkASSERT(cond) \
152 SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT("assert(" #cond ")"); })
153 #define SkASSERTF(cond, fmt, ...) \
154 SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { \
155 SkDebugf(fmt"\n", __VA_ARGS__); \
156 SK_ABORT("assert(" #cond ")"); \
157 })
bungeman1f790aa2016-07-20 09:49:10 -0700158 #define SkDEBUGFAIL(message) SK_ABORT(message)
herb966e3d32015-09-18 07:00:48 -0700159 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
csmartdaltonceeaa782016-08-10 10:07:57 -0700160 #define SkDEBUGCODE(...) __VA_ARGS__
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 #define SkDECLAREPARAM(type, var) , type var
162 #define SkPARAM(var) , var
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 #define SkDEBUGF(args ) SkDebugf args
164 #define SkAssertResult(cond) SkASSERT(cond)
165#else
166 #define SkASSERT(cond)
bungeman1f790aa2016-07-20 09:49:10 -0700167 #define SkASSERTF(cond, fmt, ...)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000168 #define SkDEBUGFAIL(message)
hsterne6f8ff02016-08-15 15:26:31 -0700169 #define SkDEBUGFAILF(fmt, ...)
csmartdaltonceeaa782016-08-10 10:07:57 -0700170 #define SkDEBUGCODE(...)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171 #define SkDEBUGF(args)
172 #define SkDECLAREPARAM(type, var)
173 #define SkPARAM(var)
174
bsalomon9daa4b92016-05-09 09:14:36 -0700175 // unlike SkASSERT, this guy executes its condition in the non-debug build.
bsalomon1b4c01c2016-05-09 12:35:17 -0700176 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
177 #define SkAssertResult(cond) if (cond) {} do {} while(false)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178#endif
179
djsollenf2b340f2016-01-29 08:51:04 -0800180// Legacy macro names for SK_ABORT
181#define SkFAIL(message) SK_ABORT(message)
182#define sk_throw() SK_ABORT("sk_throw")
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000183
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000184#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000185 #define SK_TO_STRING_NONVIRT()
186 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000187 #define SK_TO_STRING_PUREVIRT()
188 #define SK_TO_STRING_OVERRIDE()
189#else
bungemand3ebb482015-08-05 13:57:49 -0700190 class SkString;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000191 // the 'toString' helper functions convert Sk* objects to human-readable
192 // form in developer mode
193 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
194 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
195 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700196 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000197#endif
198
reed@google.com49a5b192012-10-25 17:31:39 +0000199/*
200 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
201 *
202 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
203 *
204 */
205#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
206#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
207
208/*
209 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
210 * line number. Easy way to construct
211 * unique names for local functions or
212 * variables.
213 */
214#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
215
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000216/**
217 * For some classes, it's almost always an error to instantiate one without a name, e.g.
218 * {
219 * SkAutoMutexAcquire(&mutex);
220 * <some code>
221 * }
222 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
223 * but instead the mutex is acquired and then immediately released. The correct usage is
224 * {
225 * SkAutoMutexAcquire lock(&mutex);
226 * <some code>
227 * }
228 *
229 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
230 * like this:
231 * class classname {
232 * <your class>
233 * };
234 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
235 *
236 * This won't work with templates, and you must inline the class' constructors and destructors.
237 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
238 */
239#define SK_REQUIRE_LOCAL_VAR(classname) \
bungeman99fe8222015-08-20 07:57:51 -0700240 static_assert(false, "missing name for " #classname)
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000241
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242///////////////////////////////////////////////////////////////////////
243
reed@google.com37a31332011-01-25 14:55:42 +0000244/**
245 * Fast type for signed 8 bits. Use for parameter passing and local variables,
246 * not for storage.
247 */
248typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249
reed@google.com37a31332011-01-25 14:55:42 +0000250/**
251 * Fast type for unsigned 8 bits. Use for parameter passing and local
252 * variables, not for storage
253 */
254typedef unsigned U8CPU;
255
256/**
257 * Fast type for signed 16 bits. Use for parameter passing and local variables,
258 * not for storage
259 */
260typedef int S16CPU;
261
262/**
263 * Fast type for unsigned 16 bits. Use for parameter passing and local
264 * variables, not for storage
265 */
266typedef unsigned U16CPU;
267
268/**
reed@google.com37a31332011-01-25 14:55:42 +0000269 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
270 */
271typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272
bungeman68c14d92016-03-19 15:06:56 -0700273#include "../private/SkTFitsIn.h"
274template <typename D, typename S> D SkTo(S s) {
275 SkASSERT(SkTFitsIn<D>(s));
276 return static_cast<D>(s);
277}
278#define SkToS8(x) SkTo<int8_t>(x)
279#define SkToU8(x) SkTo<uint8_t>(x)
280#define SkToS16(x) SkTo<int16_t>(x)
281#define SkToU16(x) SkTo<uint16_t>(x)
282#define SkToS32(x) SkTo<int32_t>(x)
283#define SkToU32(x) SkTo<uint32_t>(x)
284#define SkToInt(x) SkTo<int>(x)
285#define SkToUInt(x) SkTo<unsigned>(x)
286#define SkToSizeT(x) SkTo<size_t>(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287
288/** Returns 0 or 1 based on the condition
289*/
mtklein5c05d102015-12-02 12:32:02 -0800290#define SkToBool(cond) ((cond) != 0)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291
292#define SK_MaxS16 32767
293#define SK_MinS16 -32767
294#define SK_MaxU16 0xFFFF
295#define SK_MinU16 0
296#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000297#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298#define SK_MaxU32 0xFFFFFFFF
299#define SK_MinU32 0
caryclark952538e2016-02-26 05:01:42 -0800300#define SK_NaN32 ((int) (1U << 31))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301
reed@android.comd4577752009-11-21 02:48:11 +0000302/** Returns true if the value can be represented with signed 16bits
303 */
reed@android.com90209ca2009-11-21 19:58:04 +0000304static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000305 return (int16_t)x == x;
306}
307
308/** Returns true if the value can be represented with unsigned 16bits
309 */
reed@android.com90209ca2009-11-21 19:58:04 +0000310static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000311 return (uint16_t)x == x;
312}
313
caryclark3127c992015-12-09 12:02:30 -0800314static inline int32_t SkLeftShift(int32_t value, int32_t shift) {
315 return (int32_t) ((uint32_t) value << shift);
316}
317
318static inline int64_t SkLeftShift(int64_t value, int32_t shift) {
319 return (int64_t) ((uint64_t) value << shift);
320}
321
reed@android.comd4577752009-11-21 02:48:11 +0000322//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323
mtkleinfc00a7c2015-05-07 10:58:44 -0700324/** Returns the number of entries in an array (not a pointer) */
325template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700326#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000327
mtkleinb68ce742015-11-24 05:35:58 -0800328// Can be used to bracket data types that must be dense, e.g. hash keys.
329#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
330 #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
331 _Pragma("GCC diagnostic error \"-Wpadded\"")
332 #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop")
333#else
334 #define SK_BEGIN_REQUIRE_DENSE
335 #define SK_END_REQUIRE_DENSE
336#endif
337
mtklein6a259bf2016-09-26 18:20:57 -0700338#define SkAlign2(x) (((x) + 1) >> 1 << 1)
339#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000340
mtklein6a259bf2016-09-26 18:20:57 -0700341#define SkAlign4(x) (((x) + 3) >> 2 << 2)
342#define SkIsAlign4(x) (0 == ((x) & 3))
reed@google.comc6faa5a2012-06-27 15:07:11 +0000343
mtklein6a259bf2016-09-26 18:20:57 -0700344#define SkAlign8(x) (((x) + 7) >> 3 << 3)
345#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000346
mtklein6a259bf2016-09-26 18:20:57 -0700347#define SkAlign16(x) (((x) + 15) >> 4 << 4)
348#define SkIsAlign16(x) (0 == ((x) & 15))
reede2b0a0a2016-03-02 13:03:46 -0800349
mtklein6a259bf2016-09-26 18:20:57 -0700350#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
351#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
mtklein0209e952014-08-28 14:10:05 -0700352
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353typedef uint32_t SkFourByteTag;
354#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
355
356/** 32 bit integer to hold a unicode value
357*/
358typedef int32_t SkUnichar;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700359
halcanaryd0e95a52016-07-25 07:18:12 -0700360/** 16 bit unsigned integer to hold a glyph index
361*/
362typedef uint16_t SkGlyphID;
363
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700364/** 32 bit value to hold a millisecond duration
365 * Note that SK_MSecMax is about 25 days.
366 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367typedef uint32_t SkMSec;
368/** 1 second measured in milliseconds
369*/
370#define SK_MSec1 1000
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700371/** maximum representable milliseconds; 24d 20h 31m 23.647s.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372*/
373#define SK_MSecMax 0x7FFFFFFF
374/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
375*/
376#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
377/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
378*/
379#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
380
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000381/** The generation IDs in Skia reserve 0 has an invalid marker.
382 */
383#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700384/** The unique IDs in Skia reserve 0 has an invalid marker.
385 */
386#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000387
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388/****************************************************************************
389 The rest of these only build with C++
390*/
391#ifdef __cplusplus
392
393/** Faster than SkToBool for integral conditions. Returns 0 or 1
394*/
bsalomon7a5bcc52016-05-24 13:23:56 -0700395static inline constexpr int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 return (n | (0-n)) >> 31;
397}
398
bsalomon@google.comff436612013-02-27 19:07:32 +0000399/** Generic swap function. Classes with efficient swaps should specialize this function to take
400 their fast path. This function is used by SkTSort. */
Mike Kleinbde64e42016-11-14 08:39:39 -0500401template <typename T> static inline void SkTSwap(T& a, T& b) {
bungeman6f4293a2016-10-26 12:11:28 -0700402 T c(std::move(a));
403 a = std::move(b);
404 b = std::move(c);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405}
406
reed@android.comd4577752009-11-21 02:48:11 +0000407static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800408 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000409 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000411 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000412 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000413}
414
Mike Kleinbde64e42016-11-14 08:39:39 -0500415template <typename T> static inline T SkTAbs(T value) {
reed@google.com2b57dc62013-01-08 13:23:32 +0000416 if (value < 0) {
417 value = -value;
418 }
419 return value;
420}
421
reed@android.comd4577752009-11-21 02:48:11 +0000422static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423 if (a < b)
424 a = b;
425 return a;
426}
427
reed@android.comd4577752009-11-21 02:48:11 +0000428static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 if (a > b)
430 a = b;
431 return a;
432}
433
halcanarya0af7712016-05-23 09:11:58 -0700434template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000435 return (a < b) ? a : b;
436}
437
halcanarya0af7712016-05-23 09:11:58 -0700438template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000439 return (b < a) ? a : b;
440}
441
reed@android.comd4577752009-11-21 02:48:11 +0000442static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 return (a >> 31) | ((unsigned) -a >> 31);
444}
445
reed@android.comd4577752009-11-21 02:48:11 +0000446static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000447 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000449 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000450 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451}
452
bungeman62ce0302015-08-28 09:09:32 -0700453/** Returns value pinned between min and max, inclusively. */
halcanarya0af7712016-05-23 09:11:58 -0700454template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
bungeman62ce0302015-08-28 09:09:32 -0700455 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456}
457
bsalomon5ec26ae2016-02-25 08:33:02 -0800458
459///////////////////////////////////////////////////////////////////////////////
460
461/**
462 * Indicates whether an allocation should count against a cache budget.
463 */
464enum class SkBudgeted : bool {
465 kNo = false,
466 kYes = true
467};
468
robertphillips76948d42016-05-04 12:47:41 -0700469/**
470 * Indicates whether a backing store needs to be an exact match or can be larger
471 * than is strictly necessary
472 */
473enum class SkBackingFit {
474 kApprox,
475 kExact
476};
477
reed@google.com1fcd51e2011-01-05 15:50:27 +0000478///////////////////////////////////////////////////////////////////////////////
479
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000480/** Use to combine multiple bits in a bitmask in a type safe way.
481 */
482template <typename T>
483T SkTBitOr(T a, T b) {
484 return (T)(a | b);
485}
486
reed@google.com1fcd51e2011-01-05 15:50:27 +0000487/**
488 * Use to cast a pointer to a different type, and maintaining strict-aliasing
489 */
490template <typename Dst> Dst SkTCast(const void* ptr) {
491 union {
492 const void* src;
493 Dst dst;
494 } data;
495 data.src = ptr;
496 return data.dst;
497}
498
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499//////////////////////////////////////////////////////////////////////////////
500
501/** \class SkNoncopyable
502
fmalita055f6b52015-04-09 08:49:32 -0700503SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504be copied. It hides its copy-constructor and its assignment-operator.
505*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000506class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507public:
508 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000509
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510private:
511 SkNoncopyable(const SkNoncopyable&);
512 SkNoncopyable& operator=(const SkNoncopyable&);
513};
514
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515#endif /* C++ */
516
517#endif