blob: 2cc4b9c8a9ef62a646bb098714ce99ec4841037e [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
mtkleincc881da2015-12-08 11:55:17 -080038/**
39 * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
40 *
41 * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
42 * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
43 * memcpy(dst, src, 0);
44 * if (src) {
45 * printf("%x\n", *src);
46 * }
47 * In this code the compiler can assume src is not null and omit the if (src) {...} check,
48 * unconditionally running the printf, crashing the program if src really is null.
49 * Of the compilers we pay attention to only GCC performs this optimization in practice.
50 */
51static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
52 // When we pass >0 len we had better already be passing valid pointers.
53 // So we just need to skip calling memcpy when len == 0.
54 if (len) {
55 memcpy(dst,src,len);
56 }
57 return dst;
58}
59
reed@android.com8a1c16f2008-12-17 15:59:43 +000060/** \file SkTypes.h
61*/
62
reed@android.com9aa8b322010-04-13 13:22:54 +000063/** See SkGraphics::GetVersion() to retrieve these at runtime
64 */
65#define SKIA_VERSION_MAJOR 1
66#define SKIA_VERSION_MINOR 0
67#define SKIA_VERSION_PATCH 0
68
reed@android.com8a1c16f2008-12-17 15:59:43 +000069/*
70 memory wrappers to be implemented by the porting layer (platform)
71*/
72
73/** Called internally if we run out of memory. The platform implementation must
74 not return, but should either throw an exception or otherwise exit.
75*/
reed@google.comde916c82011-10-19 19:50:48 +000076SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000077/** Called internally if we hit an unrecoverable error.
78 The platform implementation must not return, but should either throw
79 an exception or otherwise exit.
80*/
djsollenf2b340f2016-01-29 08:51:04 -080081SK_API extern void sk_abort_no_print(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000082
83enum {
84 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
85 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
86};
87/** Return a block of memory (at least 4-byte aligned) of at least the
88 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000089 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
91*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000092SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000093/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
94*/
reed@google.comde916c82011-10-19 19:50:48 +000095SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000096/** Same as standard realloc(), but this one never returns null on failure. It will throw
97 an exception if it fails.
98*/
reed@google.comde916c82011-10-19 19:50:48 +000099SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100/** Free memory returned by sk_malloc(). It is safe to pass null.
101*/
reed@google.comde916c82011-10-19 19:50:48 +0000102SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103
mtklein@google.com519f9672013-09-20 14:31:45 +0000104/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
105 */
106SK_API extern void* sk_calloc(size_t size);
107
108/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
109 */
110SK_API extern void* sk_calloc_throw(size_t size);
111
reed@android.com4516f472009-06-29 16:25:36 +0000112// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
113static inline void sk_bzero(void* buffer, size_t size) {
mtklein02046c52015-12-09 10:02:14 -0800114 // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0).
115 if (size) {
116 memset(buffer, 0, size);
117 }
reed@android.com4516f472009-06-29 16:25:36 +0000118}
119
reed@google.combdf73612011-09-06 14:56:20 +0000120///////////////////////////////////////////////////////////////////////////////
121
mtklein36352bf2015-03-25 18:17:31 -0700122#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +0000123#include <new>
124
125inline void* operator new(size_t size) {
126 return sk_malloc_throw(size);
127}
128
129inline void operator delete(void* p) {
130 sk_free(p);
131}
132#endif
133
134///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135
136#define SK_INIT_TO_AVOID_WARNING = 0
137
138#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700139 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140#endif
141
caryclarkd6562002016-07-27 12:02:07 -0700142#define SkREQUIRE_SEMICOLON_AFTER(code) do { code } while (false)
143
144#define SkASSERT_RELEASE(cond) \
145 SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT(#cond); } )
djsollenf2b340f2016-01-29 08:51:04 -0800146
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147#ifdef SK_DEBUG
caryclarkd6562002016-07-27 12:02:07 -0700148 #define SkASSERT(cond) \
149 SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT("assert(" #cond ")"); })
150 #define SkASSERTF(cond, fmt, ...) \
151 SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { \
152 SkDebugf(fmt"\n", __VA_ARGS__); \
153 SK_ABORT("assert(" #cond ")"); \
154 })
bungeman1f790aa2016-07-20 09:49:10 -0700155 #define SkDEBUGFAIL(message) SK_ABORT(message)
herb966e3d32015-09-18 07:00:48 -0700156 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
csmartdaltonceeaa782016-08-10 10:07:57 -0700157 #define SkDEBUGCODE(...) __VA_ARGS__
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 #define SkDECLAREPARAM(type, var) , type var
159 #define SkPARAM(var) , var
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 #define SkDEBUGF(args ) SkDebugf args
161 #define SkAssertResult(cond) SkASSERT(cond)
162#else
163 #define SkASSERT(cond)
bungeman1f790aa2016-07-20 09:49:10 -0700164 #define SkASSERTF(cond, fmt, ...)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000165 #define SkDEBUGFAIL(message)
hsterne6f8ff02016-08-15 15:26:31 -0700166 #define SkDEBUGFAILF(fmt, ...)
csmartdaltonceeaa782016-08-10 10:07:57 -0700167 #define SkDEBUGCODE(...)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168 #define SkDEBUGF(args)
169 #define SkDECLAREPARAM(type, var)
170 #define SkPARAM(var)
171
bsalomon9daa4b92016-05-09 09:14:36 -0700172 // unlike SkASSERT, this guy executes its condition in the non-debug build.
bsalomon1b4c01c2016-05-09 12:35:17 -0700173 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
174 #define SkAssertResult(cond) if (cond) {} do {} while(false)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175#endif
176
djsollenf2b340f2016-01-29 08:51:04 -0800177// Legacy macro names for SK_ABORT
178#define SkFAIL(message) SK_ABORT(message)
179#define sk_throw() SK_ABORT("sk_throw")
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000180
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000181#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000182 #define SK_TO_STRING_NONVIRT()
183 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000184 #define SK_TO_STRING_PUREVIRT()
185 #define SK_TO_STRING_OVERRIDE()
186#else
bungemand3ebb482015-08-05 13:57:49 -0700187 class SkString;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000188 // the 'toString' helper functions convert Sk* objects to human-readable
189 // form in developer mode
190 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
191 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
192 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700193 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000194#endif
195
reed@google.com49a5b192012-10-25 17:31:39 +0000196/*
197 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
198 *
199 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
200 *
201 */
202#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
203#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
204
205/*
206 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
207 * line number. Easy way to construct
208 * unique names for local functions or
209 * variables.
210 */
211#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
212
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000213/**
214 * For some classes, it's almost always an error to instantiate one without a name, e.g.
215 * {
216 * SkAutoMutexAcquire(&mutex);
217 * <some code>
218 * }
219 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
220 * but instead the mutex is acquired and then immediately released. The correct usage is
221 * {
222 * SkAutoMutexAcquire lock(&mutex);
223 * <some code>
224 * }
225 *
226 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
227 * like this:
228 * class classname {
229 * <your class>
230 * };
231 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
232 *
233 * This won't work with templates, and you must inline the class' constructors and destructors.
234 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
235 */
236#define SK_REQUIRE_LOCAL_VAR(classname) \
bungeman99fe8222015-08-20 07:57:51 -0700237 static_assert(false, "missing name for " #classname)
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239///////////////////////////////////////////////////////////////////////
240
reed@google.com37a31332011-01-25 14:55:42 +0000241/**
242 * Fast type for signed 8 bits. Use for parameter passing and local variables,
243 * not for storage.
244 */
245typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246
reed@google.com37a31332011-01-25 14:55:42 +0000247/**
248 * Fast type for unsigned 8 bits. Use for parameter passing and local
249 * variables, not for storage
250 */
251typedef unsigned U8CPU;
252
253/**
254 * Fast type for signed 16 bits. Use for parameter passing and local variables,
255 * not for storage
256 */
257typedef int S16CPU;
258
259/**
260 * Fast type for unsigned 16 bits. Use for parameter passing and local
261 * variables, not for storage
262 */
263typedef unsigned U16CPU;
264
265/**
reed@google.com37a31332011-01-25 14:55:42 +0000266 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
267 */
268typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269
bungeman68c14d92016-03-19 15:06:56 -0700270#include "../private/SkTFitsIn.h"
271template <typename D, typename S> D SkTo(S s) {
272 SkASSERT(SkTFitsIn<D>(s));
273 return static_cast<D>(s);
274}
275#define SkToS8(x) SkTo<int8_t>(x)
276#define SkToU8(x) SkTo<uint8_t>(x)
277#define SkToS16(x) SkTo<int16_t>(x)
278#define SkToU16(x) SkTo<uint16_t>(x)
279#define SkToS32(x) SkTo<int32_t>(x)
280#define SkToU32(x) SkTo<uint32_t>(x)
281#define SkToInt(x) SkTo<int>(x)
282#define SkToUInt(x) SkTo<unsigned>(x)
283#define SkToSizeT(x) SkTo<size_t>(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284
285/** Returns 0 or 1 based on the condition
286*/
mtklein5c05d102015-12-02 12:32:02 -0800287#define SkToBool(cond) ((cond) != 0)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000288
289#define SK_MaxS16 32767
290#define SK_MinS16 -32767
291#define SK_MaxU16 0xFFFF
292#define SK_MinU16 0
293#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000294#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295#define SK_MaxU32 0xFFFFFFFF
296#define SK_MinU32 0
caryclark952538e2016-02-26 05:01:42 -0800297#define SK_NaN32 ((int) (1U << 31))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000298
reed@android.comd4577752009-11-21 02:48:11 +0000299/** Returns true if the value can be represented with signed 16bits
300 */
reed@android.com90209ca2009-11-21 19:58:04 +0000301static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000302 return (int16_t)x == x;
303}
304
305/** Returns true if the value can be represented with unsigned 16bits
306 */
reed@android.com90209ca2009-11-21 19:58:04 +0000307static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000308 return (uint16_t)x == x;
309}
310
caryclark3127c992015-12-09 12:02:30 -0800311static inline int32_t SkLeftShift(int32_t value, int32_t shift) {
312 return (int32_t) ((uint32_t) value << shift);
313}
314
315static inline int64_t SkLeftShift(int64_t value, int32_t shift) {
316 return (int64_t) ((uint64_t) value << shift);
317}
318
reed@android.comd4577752009-11-21 02:48:11 +0000319//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320
mtkleinfc00a7c2015-05-07 10:58:44 -0700321/** Returns the number of entries in an array (not a pointer) */
322template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700323#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324
mtkleinb68ce742015-11-24 05:35:58 -0800325// Can be used to bracket data types that must be dense, e.g. hash keys.
326#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
327 #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
328 _Pragma("GCC diagnostic error \"-Wpadded\"")
329 #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop")
330#else
331 #define SK_BEGIN_REQUIRE_DENSE
332 #define SK_END_REQUIRE_DENSE
333#endif
334
mtklein6a259bf2016-09-26 18:20:57 -0700335#define SkAlign2(x) (((x) + 1) >> 1 << 1)
336#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000337
mtklein6a259bf2016-09-26 18:20:57 -0700338#define SkAlign4(x) (((x) + 3) >> 2 << 2)
339#define SkIsAlign4(x) (0 == ((x) & 3))
reed@google.comc6faa5a2012-06-27 15:07:11 +0000340
mtklein6a259bf2016-09-26 18:20:57 -0700341#define SkAlign8(x) (((x) + 7) >> 3 << 3)
342#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000343
mtklein6a259bf2016-09-26 18:20:57 -0700344#define SkAlign16(x) (((x) + 15) >> 4 << 4)
345#define SkIsAlign16(x) (0 == ((x) & 15))
reede2b0a0a2016-03-02 13:03:46 -0800346
mtklein6a259bf2016-09-26 18:20:57 -0700347#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
348#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
mtklein0209e952014-08-28 14:10:05 -0700349
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350typedef uint32_t SkFourByteTag;
351#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
352
353/** 32 bit integer to hold a unicode value
354*/
355typedef int32_t SkUnichar;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700356
halcanaryd0e95a52016-07-25 07:18:12 -0700357/** 16 bit unsigned integer to hold a glyph index
358*/
359typedef uint16_t SkGlyphID;
360
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700361/** 32 bit value to hold a millisecond duration
362 * Note that SK_MSecMax is about 25 days.
363 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364typedef uint32_t SkMSec;
365/** 1 second measured in milliseconds
366*/
367#define SK_MSec1 1000
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700368/** maximum representable milliseconds; 24d 20h 31m 23.647s.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369*/
370#define SK_MSecMax 0x7FFFFFFF
371/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
372*/
373#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
374/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
375*/
376#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
377
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000378/** The generation IDs in Skia reserve 0 has an invalid marker.
379 */
380#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700381/** The unique IDs in Skia reserve 0 has an invalid marker.
382 */
383#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000384
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385/****************************************************************************
386 The rest of these only build with C++
387*/
388#ifdef __cplusplus
389
390/** Faster than SkToBool for integral conditions. Returns 0 or 1
391*/
bsalomon7a5bcc52016-05-24 13:23:56 -0700392static inline constexpr int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000393 return (n | (0-n)) >> 31;
394}
395
bsalomon@google.comff436612013-02-27 19:07:32 +0000396/** Generic swap function. Classes with efficient swaps should specialize this function to take
397 their fast path. This function is used by SkTSort. */
Mike Kleinbde64e42016-11-14 08:39:39 -0500398template <typename T> static inline void SkTSwap(T& a, T& b) {
bungeman6f4293a2016-10-26 12:11:28 -0700399 T c(std::move(a));
400 a = std::move(b);
401 b = std::move(c);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000402}
403
reed@android.comd4577752009-11-21 02:48:11 +0000404static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800405 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000406 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000408 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410}
411
Mike Kleinbde64e42016-11-14 08:39:39 -0500412template <typename T> static inline T SkTAbs(T value) {
reed@google.com2b57dc62013-01-08 13:23:32 +0000413 if (value < 0) {
414 value = -value;
415 }
416 return value;
417}
418
reed@android.comd4577752009-11-21 02:48:11 +0000419static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000420 if (a < b)
421 a = b;
422 return a;
423}
424
reed@android.comd4577752009-11-21 02:48:11 +0000425static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 if (a > b)
427 a = b;
428 return a;
429}
430
halcanarya0af7712016-05-23 09:11:58 -0700431template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000432 return (a < b) ? a : b;
433}
434
halcanarya0af7712016-05-23 09:11:58 -0700435template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000436 return (b < a) ? a : b;
437}
438
reed@android.comd4577752009-11-21 02:48:11 +0000439static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 return (a >> 31) | ((unsigned) -a >> 31);
441}
442
reed@android.comd4577752009-11-21 02:48:11 +0000443static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000444 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000445 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000446 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000448}
449
bungeman62ce0302015-08-28 09:09:32 -0700450/** Returns value pinned between min and max, inclusively. */
halcanarya0af7712016-05-23 09:11:58 -0700451template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
bungeman62ce0302015-08-28 09:09:32 -0700452 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000453}
454
bsalomon5ec26ae2016-02-25 08:33:02 -0800455
456///////////////////////////////////////////////////////////////////////////////
457
458/**
459 * Indicates whether an allocation should count against a cache budget.
460 */
461enum class SkBudgeted : bool {
462 kNo = false,
463 kYes = true
464};
465
robertphillips76948d42016-05-04 12:47:41 -0700466/**
467 * Indicates whether a backing store needs to be an exact match or can be larger
468 * than is strictly necessary
469 */
470enum class SkBackingFit {
471 kApprox,
472 kExact
473};
474
reed@google.com1fcd51e2011-01-05 15:50:27 +0000475///////////////////////////////////////////////////////////////////////////////
476
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000477/** Use to combine multiple bits in a bitmask in a type safe way.
478 */
479template <typename T>
480T SkTBitOr(T a, T b) {
481 return (T)(a | b);
482}
483
reed@google.com1fcd51e2011-01-05 15:50:27 +0000484/**
485 * Use to cast a pointer to a different type, and maintaining strict-aliasing
486 */
487template <typename Dst> Dst SkTCast(const void* ptr) {
488 union {
489 const void* src;
490 Dst dst;
491 } data;
492 data.src = ptr;
493 return data.dst;
494}
495
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496//////////////////////////////////////////////////////////////////////////////
497
498/** \class SkNoncopyable
499
fmalita055f6b52015-04-09 08:49:32 -0700500SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501be copied. It hides its copy-constructor and its assignment-operator.
502*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000503class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504public:
505 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000506
reed@android.com8a1c16f2008-12-17 15:59:43 +0000507private:
508 SkNoncopyable(const SkNoncopyable&);
509 SkNoncopyable& operator=(const SkNoncopyable&);
510};
511
512class SkAutoFree : SkNoncopyable {
513public:
514 SkAutoFree() : fPtr(NULL) {}
515 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
516 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000517
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518 /** Return the currently allocate buffer, or null
519 */
520 void* get() const { return fPtr; }
521
522 /** Assign a new ptr allocated with sk_malloc (or null), and return the
523 previous ptr. Note it is the caller's responsibility to sk_free the
524 returned ptr.
525 */
526 void* set(void* ptr) {
527 void* prev = fPtr;
528 fPtr = ptr;
529 return prev;
530 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000531
reed@android.com8a1c16f2008-12-17 15:59:43 +0000532 /** Transfer ownership of the current ptr to the caller, setting the
533 internal reference to null. Note the caller is reponsible for calling
534 sk_free on the returned address.
535 */
mtklein18300a32016-03-16 13:53:35 -0700536 void* release() { return this->set(NULL); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000537
538 /** Free the current buffer, and set the internal reference to NULL. Same
mtklein18300a32016-03-16 13:53:35 -0700539 as calling sk_free(release())
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540 */
mtklein852f15d2016-03-17 10:51:27 -0700541 void reset() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 sk_free(fPtr);
543 fPtr = NULL;
544 }
545
546private:
547 void* fPtr;
548 // illegal
549 SkAutoFree(const SkAutoFree&);
550 SkAutoFree& operator=(const SkAutoFree&);
551};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000552#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000554/**
555 * Manage an allocated block of heap memory. This object is the sole manager of
556 * the lifetime of the block, so the caller must not call sk_free() or delete
mtklein18300a32016-03-16 13:53:35 -0700557 * on the block, unless release() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000558 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000559class SkAutoMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000560public:
reed@google.com3ab41952011-10-18 18:32:46 +0000561 explicit SkAutoMalloc(size_t size = 0) {
562 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000563 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000564 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000565
566 ~SkAutoMalloc() {
567 sk_free(fPtr);
568 }
569
570 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000571 * Passed to reset to specify what happens if the requested size is smaller
572 * than the current size (and the current block was dynamically allocated).
573 */
574 enum OnShrink {
575 /**
576 * If the requested size is smaller than the current size, and the
577 * current block is dynamically allocated, free the old block and
578 * malloc a new block of the smaller size.
579 */
580 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000581
reed@google.com1c401d82011-10-18 18:52:03 +0000582 /**
583 * If the requested size is smaller than the current size, and the
584 * current block is dynamically allocated, just return the old
585 * block.
586 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000587 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000588 };
589
590 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000591 * Reallocates the block to a new size. The ptr may or may not change.
592 */
mtklein852f15d2016-03-17 10:51:27 -0700593 void* reset(size_t size = 0, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000594 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon49f085d2014-09-05 13:34:00 -0700595 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000596 *didChangeAlloc = false;
597 }
reed@google.com1c401d82011-10-18 18:52:03 +0000598 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000599 }
reed@google.com1c401d82011-10-18 18:52:03 +0000600
601 sk_free(fPtr);
602 fPtr = size ? sk_malloc_throw(size) : NULL;
603 fSize = size;
bsalomon49f085d2014-09-05 13:34:00 -0700604 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000605 *didChangeAlloc = true;
606 }
reed@google.com1c401d82011-10-18 18:52:03 +0000607
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000608 return fPtr;
609 }
610
611 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000612 * Return the allocated block.
613 */
614 void* get() { return fPtr; }
615 const void* get() const { return fPtr; }
616
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000617 /** Transfer ownership of the current ptr to the caller, setting the
618 internal reference to null. Note the caller is reponsible for calling
619 sk_free on the returned address.
620 */
mtklein18300a32016-03-16 13:53:35 -0700621 void* release() {
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000622 void* ptr = fPtr;
623 fPtr = NULL;
624 fSize = 0;
625 return ptr;
626 }
627
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000628private:
629 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000630 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000631};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000632#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000633
reed@google.com63a60602011-03-10 13:07:35 +0000634/**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800635 * Manage an allocated block of memory. If the requested size is <= kSizeRequested (or slightly
636 * more), then the allocation will come from the stack rather than the heap. This object is the
637 * sole manager of the lifetime of the block, so the caller must not call sk_free() or delete on
638 * the block.
reed@google.com63a60602011-03-10 13:07:35 +0000639 */
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800640template <size_t kSizeRequested> class SkAutoSMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000641public:
reed@google.com63a60602011-03-10 13:07:35 +0000642 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800643 * Creates initially empty storage. get() returns a ptr, but it is to a zero-byte allocation.
644 * Must call reset(size) to return an allocated block.
reed@google.com63a60602011-03-10 13:07:35 +0000645 */
646 SkAutoSMalloc() {
647 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000648 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000649 }
reed@google.com63a60602011-03-10 13:07:35 +0000650
651 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800652 * Allocate a block of the specified size. If size <= kSizeRequested (or slightly more), then
653 * the allocation will come from the stack, otherwise it will be dynamically allocated.
reed@google.com63a60602011-03-10 13:07:35 +0000654 */
655 explicit SkAutoSMalloc(size_t size) {
656 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000657 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000658 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000659 }
660
661 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800662 * Free the allocated block (if any). If the block was small enough to have been allocated on
663 * the stack, then this does nothing.
reed@google.com63a60602011-03-10 13:07:35 +0000664 */
665 ~SkAutoSMalloc() {
666 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000667 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000668 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669 }
reed@google.com63a60602011-03-10 13:07:35 +0000670
671 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800672 * Return the allocated block. May return non-null even if the block is of zero size. Since
673 * this may be on the stack or dynamically allocated, the caller must not call sk_free() on it,
674 * but must rely on SkAutoSMalloc to manage it.
reed@google.com63a60602011-03-10 13:07:35 +0000675 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000676 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000677
678 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800679 * Return a new block of the requested size, freeing (as necessary) any previously allocated
680 * block. As with the constructor, if size <= kSizeRequested (or slightly more) then the return
681 * block may be allocated locally, rather than from the heap.
reed@google.com63a60602011-03-10 13:07:35 +0000682 */
reed@google.com1c401d82011-10-18 18:52:03 +0000683 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000684 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
685 bool* didChangeAlloc = NULL) {
686 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000687 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon49f085d2014-09-05 13:34:00 -0700688 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000689 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000690 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000691 if (alloc) {
692 if (fPtr != (void*)fStorage) {
693 sk_free(fPtr);
694 }
reed@google.com1c401d82011-10-18 18:52:03 +0000695
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000696 if (size == kSize) {
697 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
698 fPtr = fStorage;
699 } else {
700 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
701 }
reed@google.com63a60602011-03-10 13:07:35 +0000702
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000703 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000704 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000705 SkASSERT(fSize >= size && fSize >= kSize);
706 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000707 return fPtr;
708 }
709
reed@android.com8a1c16f2008-12-17 15:59:43 +0000710private:
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800711 // Align up to 32 bits.
712 static const size_t kSizeAlign4 = SkAlign4(kSizeRequested);
713#if defined(GOOGLE3)
714 // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions
715 // have multiple large stack allocations.
716 static const size_t kMaxBytes = 4 * 1024;
717 static const size_t kSize = kSizeRequested > kMaxBytes ? kMaxBytes : kSizeAlign4;
718#else
719 static const size_t kSize = kSizeAlign4;
720#endif
721
reed@android.com8a1c16f2008-12-17 15:59:43 +0000722 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000723 size_t fSize; // can be larger than the requested size (see kReuse)
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800724 uint32_t fStorage[kSize >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000725};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000726// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000727
728#endif /* C++ */
729
730#endif