blob: 4e95c69f3229cdfc63b95cd2e1fa6bdaeccd8caf [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkTypes_DEFINED
9#define SkTypes_DEFINED
10
bungemanf20488b2015-07-29 11:49:40 -070011// IWYU pragma: begin_exports
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkPreConfig.h"
13#include "SkUserConfig.h"
14#include "SkPostConfig.h"
bungemanf20488b2015-07-29 11:49:40 -070015#include <stddef.h>
bungeman@google.comfab44db2013-10-11 18:50:45 +000016#include <stdint.h>
bungemanf20488b2015-07-29 11:49:40 -070017// IWYU pragma: end_exports
18
bungemanf20488b2015-07-29 11:49:40 -070019#include <string.h>
mtklein95cc0122015-04-27 15:11:01 -070020
mtkleincc881da2015-12-08 11:55:17 -080021/**
22 * sk_careful_memcpy() is just like memcpy(), but guards against undefined behavior.
23 *
24 * It is undefined behavior to call memcpy() with null dst or src, even if len is 0.
25 * If an optimizer is "smart" enough, it can exploit this to do unexpected things.
26 * memcpy(dst, src, 0);
27 * if (src) {
28 * printf("%x\n", *src);
29 * }
30 * In this code the compiler can assume src is not null and omit the if (src) {...} check,
31 * unconditionally running the printf, crashing the program if src really is null.
32 * Of the compilers we pay attention to only GCC performs this optimization in practice.
33 */
34static inline void* sk_careful_memcpy(void* dst, const void* src, size_t len) {
35 // When we pass >0 len we had better already be passing valid pointers.
36 // So we just need to skip calling memcpy when len == 0.
37 if (len) {
38 memcpy(dst,src,len);
39 }
40 return dst;
41}
42
reed@android.com8a1c16f2008-12-17 15:59:43 +000043/** \file SkTypes.h
44*/
45
reed@android.com9aa8b322010-04-13 13:22:54 +000046/** See SkGraphics::GetVersion() to retrieve these at runtime
47 */
48#define SKIA_VERSION_MAJOR 1
49#define SKIA_VERSION_MINOR 0
50#define SKIA_VERSION_PATCH 0
51
reed@android.com8a1c16f2008-12-17 15:59:43 +000052/*
53 memory wrappers to be implemented by the porting layer (platform)
54*/
55
56/** Called internally if we run out of memory. The platform implementation must
57 not return, but should either throw an exception or otherwise exit.
58*/
reed@google.comde916c82011-10-19 19:50:48 +000059SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000060/** Called internally if we hit an unrecoverable error.
61 The platform implementation must not return, but should either throw
62 an exception or otherwise exit.
63*/
djsollenf2b340f2016-01-29 08:51:04 -080064SK_API extern void sk_abort_no_print(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
66enum {
67 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
68 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
69};
70/** Return a block of memory (at least 4-byte aligned) of at least the
71 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000072 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
74*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000075SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000076/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
77*/
reed@google.comde916c82011-10-19 19:50:48 +000078SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000079/** Same as standard realloc(), but this one never returns null on failure. It will throw
80 an exception if it fails.
81*/
reed@google.comde916c82011-10-19 19:50:48 +000082SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083/** Free memory returned by sk_malloc(). It is safe to pass null.
84*/
reed@google.comde916c82011-10-19 19:50:48 +000085SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
mtklein@google.com519f9672013-09-20 14:31:45 +000087/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
88 */
89SK_API extern void* sk_calloc(size_t size);
90
91/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
92 */
93SK_API extern void* sk_calloc_throw(size_t size);
94
reed@android.com4516f472009-06-29 16:25:36 +000095// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
96static inline void sk_bzero(void* buffer, size_t size) {
mtklein02046c52015-12-09 10:02:14 -080097 // Please c.f. sk_careful_memcpy. It's undefined behavior to call memset(null, 0, 0).
98 if (size) {
99 memset(buffer, 0, size);
100 }
reed@android.com4516f472009-06-29 16:25:36 +0000101}
102
reed@google.combdf73612011-09-06 14:56:20 +0000103///////////////////////////////////////////////////////////////////////////////
104
mtklein36352bf2015-03-25 18:17:31 -0700105#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +0000106#include <new>
107
108inline void* operator new(size_t size) {
109 return sk_malloc_throw(size);
110}
111
112inline void operator delete(void* p) {
113 sk_free(p);
114}
115#endif
116
117///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118
119#define SK_INIT_TO_AVOID_WARNING = 0
120
121#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700122 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123#endif
124
djsollenf2b340f2016-01-29 08:51:04 -0800125#define SkASSERT_RELEASE(cond) if(!(cond)) { SK_ABORT(#cond); }
126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127#ifdef SK_DEBUG
caryclarkae922622016-06-03 09:42:53 -0700128 #if defined SK_BUILD_FOR_WIN
129 #define SkASSERT(cond) if(!(cond)) { __debugbreak(); }
130 #else
131 #define SkASSERT(cond) SkASSERT_RELEASE(cond)
132 #endif
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000133 #define SkDEBUGFAIL(message) SkASSERT(false && message)
herb966e3d32015-09-18 07:00:48 -0700134 #define SkDEBUGFAILF(fmt, ...) SkASSERTF(false, fmt, ##__VA_ARGS__)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 #define SkDEBUGCODE(code) code
136 #define SkDECLAREPARAM(type, var) , type var
137 #define SkPARAM(var) , var
138// #define SkDEBUGF(args ) SkDebugf##args
139 #define SkDEBUGF(args ) SkDebugf args
140 #define SkAssertResult(cond) SkASSERT(cond)
141#else
142 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000143 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 #define SkDEBUGCODE(code)
145 #define SkDEBUGF(args)
146 #define SkDECLAREPARAM(type, var)
147 #define SkPARAM(var)
148
bsalomon9daa4b92016-05-09 09:14:36 -0700149 // unlike SkASSERT, this guy executes its condition in the non-debug build.
bsalomon1b4c01c2016-05-09 12:35:17 -0700150 // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
151 #define SkAssertResult(cond) if (cond) {} do {} while(false)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152#endif
153
djsollenf2b340f2016-01-29 08:51:04 -0800154// Legacy macro names for SK_ABORT
155#define SkFAIL(message) SK_ABORT(message)
156#define sk_throw() SK_ABORT("sk_throw")
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000157
mtkleinb59161f2014-06-18 07:54:47 -0700158// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
159// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
160// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
161#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
162
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000163#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000164 #define SK_TO_STRING_NONVIRT()
165 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000166 #define SK_TO_STRING_PUREVIRT()
167 #define SK_TO_STRING_OVERRIDE()
168#else
bungemand3ebb482015-08-05 13:57:49 -0700169 class SkString;
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000170 // the 'toString' helper functions convert Sk* objects to human-readable
171 // form in developer mode
172 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
173 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
174 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700175 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000176#endif
177
reed@google.com49a5b192012-10-25 17:31:39 +0000178/*
179 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
180 *
181 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
182 *
183 */
184#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
185#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
186
187/*
188 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
189 * line number. Easy way to construct
190 * unique names for local functions or
191 * variables.
192 */
193#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
194
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000195/**
196 * For some classes, it's almost always an error to instantiate one without a name, e.g.
197 * {
198 * SkAutoMutexAcquire(&mutex);
199 * <some code>
200 * }
201 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
202 * but instead the mutex is acquired and then immediately released. The correct usage is
203 * {
204 * SkAutoMutexAcquire lock(&mutex);
205 * <some code>
206 * }
207 *
208 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
209 * like this:
210 * class classname {
211 * <your class>
212 * };
213 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
214 *
215 * This won't work with templates, and you must inline the class' constructors and destructors.
216 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
217 */
218#define SK_REQUIRE_LOCAL_VAR(classname) \
bungeman99fe8222015-08-20 07:57:51 -0700219 static_assert(false, "missing name for " #classname)
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000220
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221///////////////////////////////////////////////////////////////////////
222
reed@google.com37a31332011-01-25 14:55:42 +0000223/**
224 * Fast type for signed 8 bits. Use for parameter passing and local variables,
225 * not for storage.
226 */
227typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000228
reed@google.com37a31332011-01-25 14:55:42 +0000229/**
230 * Fast type for unsigned 8 bits. Use for parameter passing and local
231 * variables, not for storage
232 */
233typedef unsigned U8CPU;
234
235/**
236 * Fast type for signed 16 bits. Use for parameter passing and local variables,
237 * not for storage
238 */
239typedef int S16CPU;
240
241/**
242 * Fast type for unsigned 16 bits. Use for parameter passing and local
243 * variables, not for storage
244 */
245typedef unsigned U16CPU;
246
247/**
reed@google.com37a31332011-01-25 14:55:42 +0000248 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
249 */
250typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251
bungeman68c14d92016-03-19 15:06:56 -0700252#include "../private/SkTFitsIn.h"
253template <typename D, typename S> D SkTo(S s) {
254 SkASSERT(SkTFitsIn<D>(s));
255 return static_cast<D>(s);
256}
257#define SkToS8(x) SkTo<int8_t>(x)
258#define SkToU8(x) SkTo<uint8_t>(x)
259#define SkToS16(x) SkTo<int16_t>(x)
260#define SkToU16(x) SkTo<uint16_t>(x)
261#define SkToS32(x) SkTo<int32_t>(x)
262#define SkToU32(x) SkTo<uint32_t>(x)
263#define SkToInt(x) SkTo<int>(x)
264#define SkToUInt(x) SkTo<unsigned>(x)
265#define SkToSizeT(x) SkTo<size_t>(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266
267/** Returns 0 or 1 based on the condition
268*/
mtklein5c05d102015-12-02 12:32:02 -0800269#define SkToBool(cond) ((cond) != 0)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000270
271#define SK_MaxS16 32767
272#define SK_MinS16 -32767
273#define SK_MaxU16 0xFFFF
274#define SK_MinU16 0
275#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000276#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277#define SK_MaxU32 0xFFFFFFFF
278#define SK_MinU32 0
caryclark952538e2016-02-26 05:01:42 -0800279#define SK_NaN32 ((int) (1U << 31))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280
reed@android.comd4577752009-11-21 02:48:11 +0000281/** Returns true if the value can be represented with signed 16bits
282 */
reed@android.com90209ca2009-11-21 19:58:04 +0000283static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000284 return (int16_t)x == x;
285}
286
287/** Returns true if the value can be represented with unsigned 16bits
288 */
reed@android.com90209ca2009-11-21 19:58:04 +0000289static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000290 return (uint16_t)x == x;
291}
292
caryclark3127c992015-12-09 12:02:30 -0800293static inline int32_t SkLeftShift(int32_t value, int32_t shift) {
294 return (int32_t) ((uint32_t) value << shift);
295}
296
297static inline int64_t SkLeftShift(int64_t value, int32_t shift) {
298 return (int64_t) ((uint64_t) value << shift);
299}
300
reed@android.comd4577752009-11-21 02:48:11 +0000301//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302
mtkleinfc00a7c2015-05-07 10:58:44 -0700303/** Returns the number of entries in an array (not a pointer) */
304template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
caryclark95b96d62015-08-19 10:12:59 -0700305#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000306
mtkleinb68ce742015-11-24 05:35:58 -0800307// Can be used to bracket data types that must be dense, e.g. hash keys.
308#if defined(__clang__) // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
309 #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
310 _Pragma("GCC diagnostic error \"-Wpadded\"")
311 #define SK_END_REQUIRE_DENSE _Pragma("GCC diagnostic pop")
312#else
313 #define SK_BEGIN_REQUIRE_DENSE
314 #define SK_END_REQUIRE_DENSE
315#endif
316
reed@android.com8a1c16f2008-12-17 15:59:43 +0000317#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000318#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319
reed@google.comc6faa5a2012-06-27 15:07:11 +0000320#define SkAlign4(x) (((x) + 3) >> 2 << 2)
321#define SkIsAlign4(x) (0 == ((x) & 3))
322
323#define SkAlign8(x) (((x) + 7) >> 3 << 3)
324#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000325
reede2b0a0a2016-03-02 13:03:46 -0800326#define SkAlign16(x) (((x) + 15) >> 4 << 4)
327#define SkIsAlign16(x) (0 == ((x) & 15))
328
mtklein0209e952014-08-28 14:10:05 -0700329#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
330#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
331
reed@android.com8a1c16f2008-12-17 15:59:43 +0000332typedef uint32_t SkFourByteTag;
333#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
334
335/** 32 bit integer to hold a unicode value
336*/
337typedef int32_t SkUnichar;
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700338
339/** 32 bit value to hold a millisecond duration
340 * Note that SK_MSecMax is about 25 days.
341 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342typedef uint32_t SkMSec;
343/** 1 second measured in milliseconds
344*/
345#define SK_MSec1 1000
benjaminwagnerec4d4d72016-03-25 12:59:53 -0700346/** maximum representable milliseconds; 24d 20h 31m 23.647s.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347*/
348#define SK_MSecMax 0x7FFFFFFF
349/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
350*/
351#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
352/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
353*/
354#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
355
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000356/** The generation IDs in Skia reserve 0 has an invalid marker.
357 */
358#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700359/** The unique IDs in Skia reserve 0 has an invalid marker.
360 */
361#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000362
reed@android.com8a1c16f2008-12-17 15:59:43 +0000363/****************************************************************************
364 The rest of these only build with C++
365*/
366#ifdef __cplusplus
367
368/** Faster than SkToBool for integral conditions. Returns 0 or 1
369*/
bsalomon7a5bcc52016-05-24 13:23:56 -0700370static inline constexpr int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000371 return (n | (0-n)) >> 31;
372}
373
bsalomon@google.comff436612013-02-27 19:07:32 +0000374/** Generic swap function. Classes with efficient swaps should specialize this function to take
375 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000376template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377 T c(a);
378 a = b;
379 b = c;
380}
381
reed@android.comd4577752009-11-21 02:48:11 +0000382static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800383 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000384 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000386 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000387 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000388}
389
reed@google.com2b57dc62013-01-08 13:23:32 +0000390template <typename T> inline T SkTAbs(T value) {
391 if (value < 0) {
392 value = -value;
393 }
394 return value;
395}
396
reed@android.comd4577752009-11-21 02:48:11 +0000397static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398 if (a < b)
399 a = b;
400 return a;
401}
402
reed@android.comd4577752009-11-21 02:48:11 +0000403static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 if (a > b)
405 a = b;
406 return a;
407}
408
halcanarya0af7712016-05-23 09:11:58 -0700409template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000410 return (a < b) ? a : b;
411}
412
halcanarya0af7712016-05-23 09:11:58 -0700413template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
caryclark@google.com3b97af52013-04-23 11:56:44 +0000414 return (b < a) ? a : b;
415}
416
reed@android.comd4577752009-11-21 02:48:11 +0000417static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 return (a >> 31) | ((unsigned) -a >> 31);
419}
420
reed@android.comd4577752009-11-21 02:48:11 +0000421static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000422 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423 value = max;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000424 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426}
427
bungeman62ce0302015-08-28 09:09:32 -0700428/** Returns value pinned between min and max, inclusively. */
halcanarya0af7712016-05-23 09:11:58 -0700429template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
bungeman62ce0302015-08-28 09:09:32 -0700430 return SkTMax(SkTMin(value, max), min);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431}
432
bsalomon5ec26ae2016-02-25 08:33:02 -0800433
434///////////////////////////////////////////////////////////////////////////////
435
436/**
437 * Indicates whether an allocation should count against a cache budget.
438 */
439enum class SkBudgeted : bool {
440 kNo = false,
441 kYes = true
442};
443
robertphillips76948d42016-05-04 12:47:41 -0700444/**
445 * Indicates whether a backing store needs to be an exact match or can be larger
446 * than is strictly necessary
447 */
448enum class SkBackingFit {
449 kApprox,
450 kExact
451};
452
reed@google.com1fcd51e2011-01-05 15:50:27 +0000453///////////////////////////////////////////////////////////////////////////////
454
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000455/** Use to combine multiple bits in a bitmask in a type safe way.
456 */
457template <typename T>
458T SkTBitOr(T a, T b) {
459 return (T)(a | b);
460}
461
reed@google.com1fcd51e2011-01-05 15:50:27 +0000462/**
463 * Use to cast a pointer to a different type, and maintaining strict-aliasing
464 */
465template <typename Dst> Dst SkTCast(const void* ptr) {
466 union {
467 const void* src;
468 Dst dst;
469 } data;
470 data.src = ptr;
471 return data.dst;
472}
473
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474//////////////////////////////////////////////////////////////////////////////
475
476/** \class SkNoncopyable
477
fmalita055f6b52015-04-09 08:49:32 -0700478SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000479be copied. It hides its copy-constructor and its assignment-operator.
480*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000481class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482public:
483 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000484
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485private:
486 SkNoncopyable(const SkNoncopyable&);
487 SkNoncopyable& operator=(const SkNoncopyable&);
488};
489
490class SkAutoFree : SkNoncopyable {
491public:
492 SkAutoFree() : fPtr(NULL) {}
493 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
494 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000495
reed@android.com8a1c16f2008-12-17 15:59:43 +0000496 /** Return the currently allocate buffer, or null
497 */
498 void* get() const { return fPtr; }
499
500 /** Assign a new ptr allocated with sk_malloc (or null), and return the
501 previous ptr. Note it is the caller's responsibility to sk_free the
502 returned ptr.
503 */
504 void* set(void* ptr) {
505 void* prev = fPtr;
506 fPtr = ptr;
507 return prev;
508 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000509
reed@android.com8a1c16f2008-12-17 15:59:43 +0000510 /** Transfer ownership of the current ptr to the caller, setting the
511 internal reference to null. Note the caller is reponsible for calling
512 sk_free on the returned address.
513 */
mtklein18300a32016-03-16 13:53:35 -0700514 void* release() { return this->set(NULL); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515
516 /** Free the current buffer, and set the internal reference to NULL. Same
mtklein18300a32016-03-16 13:53:35 -0700517 as calling sk_free(release())
reed@android.com8a1c16f2008-12-17 15:59:43 +0000518 */
mtklein852f15d2016-03-17 10:51:27 -0700519 void reset() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 sk_free(fPtr);
521 fPtr = NULL;
522 }
523
524private:
525 void* fPtr;
526 // illegal
527 SkAutoFree(const SkAutoFree&);
528 SkAutoFree& operator=(const SkAutoFree&);
529};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000530#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000531
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000532/**
533 * Manage an allocated block of heap memory. This object is the sole manager of
534 * the lifetime of the block, so the caller must not call sk_free() or delete
mtklein18300a32016-03-16 13:53:35 -0700535 * on the block, unless release() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000536 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000537class SkAutoMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000538public:
reed@google.com3ab41952011-10-18 18:32:46 +0000539 explicit SkAutoMalloc(size_t size = 0) {
540 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000541 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000543
544 ~SkAutoMalloc() {
545 sk_free(fPtr);
546 }
547
548 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000549 * Passed to reset to specify what happens if the requested size is smaller
550 * than the current size (and the current block was dynamically allocated).
551 */
552 enum OnShrink {
553 /**
554 * If the requested size is smaller than the current size, and the
555 * current block is dynamically allocated, free the old block and
556 * malloc a new block of the smaller size.
557 */
558 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000559
reed@google.com1c401d82011-10-18 18:52:03 +0000560 /**
561 * If the requested size is smaller than the current size, and the
562 * current block is dynamically allocated, just return the old
563 * block.
564 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000565 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000566 };
567
568 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000569 * Reallocates the block to a new size. The ptr may or may not change.
570 */
mtklein852f15d2016-03-17 10:51:27 -0700571 void* reset(size_t size = 0, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000572 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon49f085d2014-09-05 13:34:00 -0700573 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000574 *didChangeAlloc = false;
575 }
reed@google.com1c401d82011-10-18 18:52:03 +0000576 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000577 }
reed@google.com1c401d82011-10-18 18:52:03 +0000578
579 sk_free(fPtr);
580 fPtr = size ? sk_malloc_throw(size) : NULL;
581 fSize = size;
bsalomon49f085d2014-09-05 13:34:00 -0700582 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000583 *didChangeAlloc = true;
584 }
reed@google.com1c401d82011-10-18 18:52:03 +0000585
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000586 return fPtr;
587 }
588
589 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000590 * Return the allocated block.
591 */
592 void* get() { return fPtr; }
593 const void* get() const { return fPtr; }
594
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000595 /** Transfer ownership of the current ptr to the caller, setting the
596 internal reference to null. Note the caller is reponsible for calling
597 sk_free on the returned address.
598 */
mtklein18300a32016-03-16 13:53:35 -0700599 void* release() {
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000600 void* ptr = fPtr;
601 fPtr = NULL;
602 fSize = 0;
603 return ptr;
604 }
605
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000606private:
607 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000608 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000609};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000610#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000611
reed@google.com63a60602011-03-10 13:07:35 +0000612/**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800613 * Manage an allocated block of memory. If the requested size is <= kSizeRequested (or slightly
614 * more), then the allocation will come from the stack rather than the heap. This object is the
615 * sole manager of the lifetime of the block, so the caller must not call sk_free() or delete on
616 * the block.
reed@google.com63a60602011-03-10 13:07:35 +0000617 */
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800618template <size_t kSizeRequested> class SkAutoSMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619public:
reed@google.com63a60602011-03-10 13:07:35 +0000620 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800621 * Creates initially empty storage. get() returns a ptr, but it is to a zero-byte allocation.
622 * Must call reset(size) to return an allocated block.
reed@google.com63a60602011-03-10 13:07:35 +0000623 */
624 SkAutoSMalloc() {
625 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000626 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 }
reed@google.com63a60602011-03-10 13:07:35 +0000628
629 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800630 * Allocate a block of the specified size. If size <= kSizeRequested (or slightly more), then
631 * the allocation will come from the stack, otherwise it will be dynamically allocated.
reed@google.com63a60602011-03-10 13:07:35 +0000632 */
633 explicit SkAutoSMalloc(size_t size) {
634 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000635 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000636 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000637 }
638
639 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800640 * Free the allocated block (if any). If the block was small enough to have been allocated on
641 * the stack, then this does nothing.
reed@google.com63a60602011-03-10 13:07:35 +0000642 */
643 ~SkAutoSMalloc() {
644 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000645 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000646 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 }
reed@google.com63a60602011-03-10 13:07:35 +0000648
649 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800650 * Return the allocated block. May return non-null even if the block is of zero size. Since
651 * this may be on the stack or dynamically allocated, the caller must not call sk_free() on it,
652 * but must rely on SkAutoSMalloc to manage it.
reed@google.com63a60602011-03-10 13:07:35 +0000653 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000654 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000655
656 /**
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800657 * Return a new block of the requested size, freeing (as necessary) any previously allocated
658 * block. As with the constructor, if size <= kSizeRequested (or slightly more) then the return
659 * block may be allocated locally, rather than from the heap.
reed@google.com63a60602011-03-10 13:07:35 +0000660 */
reed@google.com1c401d82011-10-18 18:52:03 +0000661 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000662 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
663 bool* didChangeAlloc = NULL) {
664 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000665 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon49f085d2014-09-05 13:34:00 -0700666 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000667 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000668 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000669 if (alloc) {
670 if (fPtr != (void*)fStorage) {
671 sk_free(fPtr);
672 }
reed@google.com1c401d82011-10-18 18:52:03 +0000673
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000674 if (size == kSize) {
675 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
676 fPtr = fStorage;
677 } else {
678 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
679 }
reed@google.com63a60602011-03-10 13:07:35 +0000680
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000681 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000682 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000683 SkASSERT(fSize >= size && fSize >= kSize);
684 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000685 return fPtr;
686 }
687
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688private:
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800689 // Align up to 32 bits.
690 static const size_t kSizeAlign4 = SkAlign4(kSizeRequested);
691#if defined(GOOGLE3)
692 // Stack frame size is limited for GOOGLE3. 4k is less than the actual max, but some functions
693 // have multiple large stack allocations.
694 static const size_t kMaxBytes = 4 * 1024;
695 static const size_t kSize = kSizeRequested > kMaxBytes ? kMaxBytes : kSizeAlign4;
696#else
697 static const size_t kSize = kSizeAlign4;
698#endif
699
reed@android.com8a1c16f2008-12-17 15:59:43 +0000700 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000701 size_t fSize; // can be larger than the requested size (see kReuse)
benjaminwagnerf49c75a2016-02-05 07:02:38 -0800702 uint32_t fStorage[kSize >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000703};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000704// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705
706#endif /* C++ */
707
708#endif