blob: 1a165a148a97ac56d6139b15ed77a8f58452b95b [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>
bsalomonef3fcd82014-12-12 08:51:38 -080017#include <sys/types.h>
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
mtklein95cc0122015-04-27 15:11:01 -070019#if defined(SK_ARM_HAS_NEON)
20 #include <arm_neon.h>
21#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
22 #include <immintrin.h>
23#endif
bungemanf20488b2015-07-29 11:49:40 -070024// IWYU pragma: end_exports
25
26#include <stdlib.h>
27#include <string.h>
mtklein95cc0122015-04-27 15:11:01 -070028
reed@android.com8a1c16f2008-12-17 15:59:43 +000029/** \file SkTypes.h
30*/
31
reed@android.com9aa8b322010-04-13 13:22:54 +000032/** See SkGraphics::GetVersion() to retrieve these at runtime
33 */
34#define SKIA_VERSION_MAJOR 1
35#define SKIA_VERSION_MINOR 0
36#define SKIA_VERSION_PATCH 0
37
reed@android.com8a1c16f2008-12-17 15:59:43 +000038/*
39 memory wrappers to be implemented by the porting layer (platform)
40*/
41
42/** Called internally if we run out of memory. The platform implementation must
43 not return, but should either throw an exception or otherwise exit.
44*/
reed@google.comde916c82011-10-19 19:50:48 +000045SK_API extern void sk_out_of_memory(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000046/** Called internally if we hit an unrecoverable error.
47 The platform implementation must not return, but should either throw
48 an exception or otherwise exit.
49*/
reed@google.comde916c82011-10-19 19:50:48 +000050SK_API extern void sk_throw(void);
reed@android.com8a1c16f2008-12-17 15:59:43 +000051
52enum {
53 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
54 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
55};
56/** Return a block of memory (at least 4-byte aligned) of at least the
57 specified size. If the requested memory cannot be returned, either
mtklein@google.com519f9672013-09-20 14:31:45 +000058 return null (if SK_MALLOC_TEMP bit is clear) or throw an exception
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
60*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000061SK_API extern void* sk_malloc_flags(size_t size, unsigned flags);
reed@android.com8a1c16f2008-12-17 15:59:43 +000062/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
63*/
reed@google.comde916c82011-10-19 19:50:48 +000064SK_API extern void* sk_malloc_throw(size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065/** Same as standard realloc(), but this one never returns null on failure. It will throw
66 an exception if it fails.
67*/
reed@google.comde916c82011-10-19 19:50:48 +000068SK_API extern void* sk_realloc_throw(void* buffer, size_t size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000069/** Free memory returned by sk_malloc(). It is safe to pass null.
70*/
reed@google.comde916c82011-10-19 19:50:48 +000071SK_API extern void sk_free(void*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
mtklein@google.com519f9672013-09-20 14:31:45 +000073/** Much like calloc: returns a pointer to at least size zero bytes, or NULL on failure.
74 */
75SK_API extern void* sk_calloc(size_t size);
76
77/** Same as sk_calloc, but throws an exception instead of returning NULL on failure.
78 */
79SK_API extern void* sk_calloc_throw(size_t size);
80
reed@android.com4516f472009-06-29 16:25:36 +000081// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
82static inline void sk_bzero(void* buffer, size_t size) {
83 memset(buffer, 0, size);
84}
85
reed@google.combdf73612011-09-06 14:56:20 +000086///////////////////////////////////////////////////////////////////////////////
87
mtklein36352bf2015-03-25 18:17:31 -070088#ifdef override_GLOBAL_NEW
reed@google.combdf73612011-09-06 14:56:20 +000089#include <new>
90
91inline void* operator new(size_t size) {
92 return sk_malloc_throw(size);
93}
94
95inline void operator delete(void* p) {
96 sk_free(p);
97}
98#endif
99
100///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101
102#define SK_INIT_TO_AVOID_WARNING = 0
103
104#ifndef SkDebugf
georgec4ade572014-08-01 12:02:07 -0700105 SK_API void SkDebugf(const char format[], ...);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106#endif
107
108#ifdef SK_DEBUG
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000109 #define SkASSERT(cond) SK_ALWAYSBREAK(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000110 #define SkDEBUGFAIL(message) SkASSERT(false && message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 #define SkDEBUGCODE(code) code
112 #define SkDECLAREPARAM(type, var) , type var
113 #define SkPARAM(var) , var
114// #define SkDEBUGF(args ) SkDebugf##args
115 #define SkDEBUGF(args ) SkDebugf args
116 #define SkAssertResult(cond) SkASSERT(cond)
117#else
118 #define SkASSERT(cond)
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000119 #define SkDEBUGFAIL(message)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 #define SkDEBUGCODE(code)
121 #define SkDEBUGF(args)
122 #define SkDECLAREPARAM(type, var)
123 #define SkPARAM(var)
124
125 // unlike SkASSERT, this guy executes its condition in the non-debug build
126 #define SkAssertResult(cond) cond
127#endif
128
commit-bot@chromium.org45d1d1d2014-04-30 18:24:16 +0000129#define SkFAIL(message) SK_ALWAYSBREAK(false && message)
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000130
mtkleinb59161f2014-06-18 07:54:47 -0700131// We want to evaluate cond only once, and inside the SkASSERT somewhere so we see its string form.
132// So we use the comma operator to make an SkDebugf that always returns false: we'll evaluate cond,
133// and if it's true the assert passes; if it's false, we'll print the message and the assert fails.
134#define SkASSERTF(cond, fmt, ...) SkASSERT((cond) || (SkDebugf(fmt"\n", __VA_ARGS__), false))
135
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000136#ifdef SK_DEVELOPER
137 #define SkDEVCODE(code) code
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000138#else
139 #define SkDEVCODE(code)
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000140#endif
141
142#ifdef SK_IGNORE_TO_STRING
skia.committer@gmail.combc3d92a2014-03-14 03:02:26 +0000143 #define SK_TO_STRING_NONVIRT()
144 #define SK_TO_STRING_VIRT()
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000145 #define SK_TO_STRING_PUREVIRT()
146 #define SK_TO_STRING_OVERRIDE()
147#else
148 // the 'toString' helper functions convert Sk* objects to human-readable
149 // form in developer mode
150 #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
151 #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
152 #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
mtklein36352bf2015-03-25 18:17:31 -0700153 #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000154#endif
155
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000156template <bool>
157struct SkCompileAssert {
158};
159
bungeman@google.com562b2e62014-03-12 21:41:06 +0000160// Uses static_cast<bool>(expr) instead of bool(expr) due to
161// https://connect.microsoft.com/VisualStudio/feedback/details/832915
bungeman@google.com9c28fa52014-03-13 17:51:05 +0000162
163// The extra parentheses in SkCompileAssert<(...)> are a work around for
164// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57771
165// which was fixed in gcc 4.8.2.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000166#define SK_COMPILE_ASSERT(expr, msg) \
bungeman@google.com2f582092014-03-12 21:56:23 +0000167 typedef SkCompileAssert<(static_cast<bool>(expr))> \
bungeman@google.com562b2e62014-03-12 21:41:06 +0000168 msg[static_cast<bool>(expr) ? 1 : -1] SK_UNUSED
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000169
reed@google.com49a5b192012-10-25 17:31:39 +0000170/*
171 * Usage: SK_MACRO_CONCAT(a, b) to construct the symbol ab
172 *
173 * SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
174 *
175 */
176#define SK_MACRO_CONCAT(X, Y) SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
177#define SK_MACRO_CONCAT_IMPL_PRIV(X, Y) X ## Y
178
179/*
180 * Usage: SK_MACRO_APPEND_LINE(foo) to make foo123, where 123 is the current
181 * line number. Easy way to construct
182 * unique names for local functions or
183 * variables.
184 */
185#define SK_MACRO_APPEND_LINE(name) SK_MACRO_CONCAT(name, __LINE__)
186
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000187/**
188 * For some classes, it's almost always an error to instantiate one without a name, e.g.
189 * {
190 * SkAutoMutexAcquire(&mutex);
191 * <some code>
192 * }
193 * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
194 * but instead the mutex is acquired and then immediately released. The correct usage is
195 * {
196 * SkAutoMutexAcquire lock(&mutex);
197 * <some code>
198 * }
199 *
200 * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
201 * like this:
202 * class classname {
203 * <your class>
204 * };
205 * #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
206 *
207 * This won't work with templates, and you must inline the class' constructors and destructors.
208 * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
209 */
210#define SK_REQUIRE_LOCAL_VAR(classname) \
211 SK_COMPILE_ASSERT(false, missing_name_for_##classname)
212
reed@android.com8a1c16f2008-12-17 15:59:43 +0000213///////////////////////////////////////////////////////////////////////
214
reed@google.com37a31332011-01-25 14:55:42 +0000215/**
216 * Fast type for signed 8 bits. Use for parameter passing and local variables,
217 * not for storage.
218 */
219typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220
reed@google.com37a31332011-01-25 14:55:42 +0000221/**
222 * Fast type for unsigned 8 bits. Use for parameter passing and local
223 * variables, not for storage
224 */
225typedef unsigned U8CPU;
226
227/**
228 * Fast type for signed 16 bits. Use for parameter passing and local variables,
229 * not for storage
230 */
231typedef int S16CPU;
232
233/**
234 * Fast type for unsigned 16 bits. Use for parameter passing and local
235 * variables, not for storage
236 */
237typedef unsigned U16CPU;
238
239/**
240 * Meant to be faster than bool (doesn't promise to be 0 or 1,
241 * just 0 or non-zero
242 */
243typedef int SkBool;
244
245/**
246 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
247 */
248typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249
250#ifdef SK_DEBUG
bungeman@google.com777ded02013-07-19 22:30:11 +0000251 SK_API int8_t SkToS8(intmax_t);
252 SK_API uint8_t SkToU8(uintmax_t);
253 SK_API int16_t SkToS16(intmax_t);
254 SK_API uint16_t SkToU16(uintmax_t);
255 SK_API int32_t SkToS32(intmax_t);
256 SK_API uint32_t SkToU32(uintmax_t);
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000257 SK_API int SkToInt(intmax_t);
reed@google.com7fa2a652014-01-27 13:42:58 +0000258 SK_API unsigned SkToUInt(uintmax_t);
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000259 SK_API size_t SkToSizeT(uintmax_t);
bsalomonef3fcd82014-12-12 08:51:38 -0800260 SK_API off_t SkToOffT(intmax_t x);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000261#else
262 #define SkToS8(x) ((int8_t)(x))
263 #define SkToU8(x) ((uint8_t)(x))
264 #define SkToS16(x) ((int16_t)(x))
265 #define SkToU16(x) ((uint16_t)(x))
266 #define SkToS32(x) ((int32_t)(x))
267 #define SkToU32(x) ((uint32_t)(x))
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +0000268 #define SkToInt(x) ((int)(x))
reed@google.com7fa2a652014-01-27 13:42:58 +0000269 #define SkToUInt(x) ((unsigned)(x))
commit-bot@chromium.org490fb6b2014-03-06 17:16:26 +0000270 #define SkToSizeT(x) ((size_t)(x))
bsalomonef3fcd82014-12-12 08:51:38 -0800271 #define SkToOffT(x) ((off_t)(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272#endif
273
274/** Returns 0 or 1 based on the condition
275*/
276#define SkToBool(cond) ((cond) != 0)
277
278#define SK_MaxS16 32767
279#define SK_MinS16 -32767
280#define SK_MaxU16 0xFFFF
281#define SK_MinU16 0
282#define SK_MaxS32 0x7FFFFFFF
caryclark@google.com594dd3c2012-09-24 19:33:57 +0000283#define SK_MinS32 -SK_MaxS32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284#define SK_MaxU32 0xFFFFFFFF
285#define SK_MinU32 0
humper@google.com0e515772013-01-07 19:54:40 +0000286#define SK_NaN32 (1 << 31)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287
reed@android.comd4577752009-11-21 02:48:11 +0000288/** Returns true if the value can be represented with signed 16bits
289 */
reed@android.com90209ca2009-11-21 19:58:04 +0000290static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000291 return (int16_t)x == x;
292}
293
294/** Returns true if the value can be represented with unsigned 16bits
295 */
reed@android.com90209ca2009-11-21 19:58:04 +0000296static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000297 return (uint16_t)x == x;
298}
299
300//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301#ifndef SK_OFFSETOF
reed@google.com56d3a232012-03-21 12:25:48 +0000302 #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1)->field) - (char*)1)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303#endif
304
mtkleinfc00a7c2015-05-07 10:58:44 -0700305/** Returns the number of entries in an array (not a pointer) */
306template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
307#define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000308
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309#define SkAlign2(x) (((x) + 1) >> 1 << 1)
reed@google.comc6faa5a2012-06-27 15:07:11 +0000310#define SkIsAlign2(x) (0 == ((x) & 1))
reed@android.com8a1c16f2008-12-17 15:59:43 +0000311
reed@google.comc6faa5a2012-06-27 15:07:11 +0000312#define SkAlign4(x) (((x) + 3) >> 2 << 2)
313#define SkIsAlign4(x) (0 == ((x) & 3))
314
315#define SkAlign8(x) (((x) + 7) >> 3 << 3)
316#define SkIsAlign8(x) (0 == ((x) & 7))
tomhudson@google.com01224d52011-11-28 18:22:01 +0000317
mtklein0209e952014-08-28 14:10:05 -0700318#define SkAlignPtr(x) (sizeof(void*) == 8 ? SkAlign8(x) : SkAlign4(x))
319#define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
320
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321typedef uint32_t SkFourByteTag;
322#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
323
324/** 32 bit integer to hold a unicode value
325*/
326typedef int32_t SkUnichar;
327/** 32 bit value to hold a millisecond count
328*/
329typedef uint32_t SkMSec;
330/** 1 second measured in milliseconds
331*/
332#define SK_MSec1 1000
333/** maximum representable milliseconds
334*/
335#define SK_MSecMax 0x7FFFFFFF
336/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
337*/
338#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
339/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
340*/
341#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
342
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000343/** The generation IDs in Skia reserve 0 has an invalid marker.
344 */
345#define SK_InvalidGenID 0
bsalomon1c63bf62014-07-22 13:09:46 -0700346/** The unique IDs in Skia reserve 0 has an invalid marker.
347 */
348#define SK_InvalidUniqueID 0
commit-bot@chromium.org2b4e3702014-04-07 18:26:22 +0000349
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350/****************************************************************************
351 The rest of these only build with C++
352*/
353#ifdef __cplusplus
354
355/** Faster than SkToBool for integral conditions. Returns 0 or 1
356*/
reed@android.comd4577752009-11-21 02:48:11 +0000357static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000358 return (n | (0-n)) >> 31;
359}
360
bsalomon@google.comff436612013-02-27 19:07:32 +0000361/** Generic swap function. Classes with efficient swaps should specialize this function to take
362 their fast path. This function is used by SkTSort. */
reed@android.comd4577752009-11-21 02:48:11 +0000363template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 T c(a);
365 a = b;
366 b = c;
367}
368
reed@android.comd4577752009-11-21 02:48:11 +0000369static inline int32_t SkAbs32(int32_t value) {
mtklein09a22e92014-11-21 11:38:53 -0800370 SkASSERT(value != SK_NaN32); // The most negative int32_t can't be negated.
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000371 if (value < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 value = -value;
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000373 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000374 return value;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375}
376
reed@google.com2b57dc62013-01-08 13:23:32 +0000377template <typename T> inline T SkTAbs(T value) {
378 if (value < 0) {
379 value = -value;
380 }
381 return value;
382}
383
reed@android.comd4577752009-11-21 02:48:11 +0000384static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000385 if (a < b)
386 a = b;
387 return a;
388}
389
reed@android.comd4577752009-11-21 02:48:11 +0000390static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391 if (a > b)
392 a = b;
393 return a;
394}
395
caryclark@google.com3b97af52013-04-23 11:56:44 +0000396template <typename T> const T& SkTMin(const T& a, const T& b) {
397 return (a < b) ? a : b;
398}
399
400template <typename T> const T& SkTMax(const T& a, const T& b) {
401 return (b < a) ? a : b;
402}
403
reed@android.comd4577752009-11-21 02:48:11 +0000404static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000405 return (a >> 31) | ((unsigned) -a >> 31);
406}
407
reed@android.comd4577752009-11-21 02:48:11 +0000408static inline int32_t SkFastMin32(int32_t value, int32_t max) {
commit-bot@chromium.org38bad322013-07-30 13:16:29 +0000409 if (value > max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410 value = max;
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
bungemanfd0ecf42015-04-16 12:18:28 -0700415template <typename T> static inline const T& SkTPin(const T& x, const T& min, const T& max) {
416 return SkTMax(SkTMin(x, max), min);
417}
418
419/** Returns signed 32 bit value pinned between min and max, inclusively. */
reed@android.comd4577752009-11-21 02:48:11 +0000420static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
bungemanfd0ecf42015-04-16 12:18:28 -0700421 return SkTPin(value, min, max);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000422}
423
reed@android.comd4577752009-11-21 02:48:11 +0000424static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
425 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 SkASSERT((int)cond == 0 || (int)cond == 1);
427 return (bits & ~(1 << shift)) | ((int)cond << shift);
428}
429
reed@android.comd4577752009-11-21 02:48:11 +0000430static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
431 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000432 return cond ? bits | mask : bits & ~mask;
433}
434
reed@google.com1fcd51e2011-01-05 15:50:27 +0000435///////////////////////////////////////////////////////////////////////////////
436
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +0000437/** Use to combine multiple bits in a bitmask in a type safe way.
438 */
439template <typename T>
440T SkTBitOr(T a, T b) {
441 return (T)(a | b);
442}
443
reed@google.com1fcd51e2011-01-05 15:50:27 +0000444/**
445 * Use to cast a pointer to a different type, and maintaining strict-aliasing
446 */
447template <typename Dst> Dst SkTCast(const void* ptr) {
448 union {
449 const void* src;
450 Dst dst;
451 } data;
452 data.src = ptr;
453 return data.dst;
454}
455
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456//////////////////////////////////////////////////////////////////////////////
457
458/** \class SkNoncopyable
459
fmalita055f6b52015-04-09 08:49:32 -0700460SkNoncopyable is the base class for objects that do not want to
reed@android.com8a1c16f2008-12-17 15:59:43 +0000461be copied. It hides its copy-constructor and its assignment-operator.
462*/
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +0000463class SK_API SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464public:
465 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000466
reed@android.com8a1c16f2008-12-17 15:59:43 +0000467private:
468 SkNoncopyable(const SkNoncopyable&);
469 SkNoncopyable& operator=(const SkNoncopyable&);
470};
471
472class SkAutoFree : SkNoncopyable {
473public:
474 SkAutoFree() : fPtr(NULL) {}
475 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
476 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000477
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 /** Return the currently allocate buffer, or null
479 */
480 void* get() const { return fPtr; }
481
482 /** Assign a new ptr allocated with sk_malloc (or null), and return the
483 previous ptr. Note it is the caller's responsibility to sk_free the
484 returned ptr.
485 */
486 void* set(void* ptr) {
487 void* prev = fPtr;
488 fPtr = ptr;
489 return prev;
490 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000491
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492 /** Transfer ownership of the current ptr to the caller, setting the
493 internal reference to null. Note the caller is reponsible for calling
494 sk_free on the returned address.
495 */
496 void* detach() { return this->set(NULL); }
497
498 /** Free the current buffer, and set the internal reference to NULL. Same
499 as calling sk_free(detach())
500 */
501 void free() {
502 sk_free(fPtr);
503 fPtr = NULL;
504 }
505
506private:
507 void* fPtr;
508 // illegal
509 SkAutoFree(const SkAutoFree&);
510 SkAutoFree& operator=(const SkAutoFree&);
511};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000512#define SkAutoFree(...) SK_REQUIRE_LOCAL_VAR(SkAutoFree)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000514/**
515 * Manage an allocated block of heap memory. This object is the sole manager of
516 * the lifetime of the block, so the caller must not call sk_free() or delete
reed@google.com1c401d82011-10-18 18:52:03 +0000517 * on the block, unless detach() was called.
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000518 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +0000519class SkAutoMalloc : SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520public:
reed@google.com3ab41952011-10-18 18:32:46 +0000521 explicit SkAutoMalloc(size_t size = 0) {
522 fPtr = size ? sk_malloc_throw(size) : NULL;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000523 fSize = size;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 }
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000525
526 ~SkAutoMalloc() {
527 sk_free(fPtr);
528 }
529
530 /**
reed@google.com1c401d82011-10-18 18:52:03 +0000531 * Passed to reset to specify what happens if the requested size is smaller
532 * than the current size (and the current block was dynamically allocated).
533 */
534 enum OnShrink {
535 /**
536 * If the requested size is smaller than the current size, and the
537 * current block is dynamically allocated, free the old block and
538 * malloc a new block of the smaller size.
539 */
540 kAlloc_OnShrink,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000541
reed@google.com1c401d82011-10-18 18:52:03 +0000542 /**
543 * If the requested size is smaller than the current size, and the
544 * current block is dynamically allocated, just return the old
545 * block.
546 */
tomhudson@google.com1f902872012-06-01 13:15:47 +0000547 kReuse_OnShrink
reed@google.com1c401d82011-10-18 18:52:03 +0000548 };
549
550 /**
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000551 * Reallocates the block to a new size. The ptr may or may not change.
552 */
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000553 void* reset(size_t size, OnShrink shrink = kAlloc_OnShrink, bool* didChangeAlloc = NULL) {
reed@google.com1c401d82011-10-18 18:52:03 +0000554 if (size == fSize || (kReuse_OnShrink == shrink && size < fSize)) {
bsalomon49f085d2014-09-05 13:34:00 -0700555 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000556 *didChangeAlloc = false;
557 }
reed@google.com1c401d82011-10-18 18:52:03 +0000558 return fPtr;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000559 }
reed@google.com1c401d82011-10-18 18:52:03 +0000560
561 sk_free(fPtr);
562 fPtr = size ? sk_malloc_throw(size) : NULL;
563 fSize = size;
bsalomon49f085d2014-09-05 13:34:00 -0700564 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000565 *didChangeAlloc = true;
566 }
reed@google.com1c401d82011-10-18 18:52:03 +0000567
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000568 return fPtr;
569 }
570
571 /**
572 * Releases the block back to the heap
573 */
574 void free() {
575 this->reset(0);
576 }
577
578 /**
579 * Return the allocated block.
580 */
581 void* get() { return fPtr; }
582 const void* get() const { return fPtr; }
583
bsalomon@google.com6dcd27c2011-09-06 15:02:33 +0000584 /** Transfer ownership of the current ptr to the caller, setting the
585 internal reference to null. Note the caller is reponsible for calling
586 sk_free on the returned address.
587 */
588 void* detach() {
589 void* ptr = fPtr;
590 fPtr = NULL;
591 fSize = 0;
592 return ptr;
593 }
594
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000595private:
596 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000597 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000599#define SkAutoMalloc(...) SK_REQUIRE_LOCAL_VAR(SkAutoMalloc)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000600
reed@google.com63a60602011-03-10 13:07:35 +0000601/**
602 * Manage an allocated block of memory. If the requested size is <= kSize, then
603 * the allocation will come from the stack rather than the heap. This object
604 * is the sole manager of the lifetime of the block, so the caller must not
605 * call sk_free() or delete on the block.
606 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000607template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
608public:
reed@google.com63a60602011-03-10 13:07:35 +0000609 /**
610 * Creates initially empty storage. get() returns a ptr, but it is to
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000611 * a zero-byte allocation. Must call reset(size) to return an allocated
reed@google.com63a60602011-03-10 13:07:35 +0000612 * block.
613 */
614 SkAutoSMalloc() {
615 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000616 fSize = kSize;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000617 }
reed@google.com63a60602011-03-10 13:07:35 +0000618
619 /**
620 * Allocate a block of the specified size. If size <= kSize, then the
621 * allocation will come from the stack, otherwise it will be dynamically
622 * allocated.
623 */
624 explicit SkAutoSMalloc(size_t size) {
625 fPtr = fStorage;
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000626 fSize = kSize;
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000627 this->reset(size);
reed@google.com63a60602011-03-10 13:07:35 +0000628 }
629
630 /**
631 * Free the allocated block (if any). If the block was small enought to
632 * have been allocated on the stack (size <= kSize) then this does nothing.
633 */
634 ~SkAutoSMalloc() {
635 if (fPtr != (void*)fStorage) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000636 sk_free(fPtr);
reed@google.com63a60602011-03-10 13:07:35 +0000637 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000638 }
reed@google.com63a60602011-03-10 13:07:35 +0000639
640 /**
641 * Return the allocated block. May return non-null even if the block is
642 * of zero size. Since this may be on the stack or dynamically allocated,
643 * the caller must not call sk_free() on it, but must rely on SkAutoSMalloc
644 * to manage it.
645 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000646 void* get() const { return fPtr; }
reed@google.com63a60602011-03-10 13:07:35 +0000647
648 /**
649 * Return a new block of the requested size, freeing (as necessary) any
650 * previously allocated block. As with the constructor, if size <= kSize
651 * then the return block may be allocated locally, rather than from the
652 * heap.
653 */
reed@google.com1c401d82011-10-18 18:52:03 +0000654 void* reset(size_t size,
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000655 SkAutoMalloc::OnShrink shrink = SkAutoMalloc::kAlloc_OnShrink,
656 bool* didChangeAlloc = NULL) {
657 size = (size < kSize) ? kSize : size;
robertphillips@google.com0f2b1952013-05-23 14:59:40 +0000658 bool alloc = size != fSize && (SkAutoMalloc::kAlloc_OnShrink == shrink || size > fSize);
bsalomon49f085d2014-09-05 13:34:00 -0700659 if (didChangeAlloc) {
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000660 *didChangeAlloc = alloc;
reed@google.com1c401d82011-10-18 18:52:03 +0000661 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000662 if (alloc) {
663 if (fPtr != (void*)fStorage) {
664 sk_free(fPtr);
665 }
reed@google.com1c401d82011-10-18 18:52:03 +0000666
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000667 if (size == kSize) {
668 SkASSERT(fPtr != fStorage); // otherwise we lied when setting didChangeAlloc.
669 fPtr = fStorage;
670 } else {
671 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
672 }
reed@google.com63a60602011-03-10 13:07:35 +0000673
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000674 fSize = size;
reed@google.com63a60602011-03-10 13:07:35 +0000675 }
bsalomon@google.com9eb66452013-05-22 13:35:37 +0000676 SkASSERT(fSize >= size && fSize >= kSize);
677 SkASSERT((fPtr == fStorage) || fSize > kSize);
reed@google.com63a60602011-03-10 13:07:35 +0000678 return fPtr;
679 }
680
reed@android.com8a1c16f2008-12-17 15:59:43 +0000681private:
682 void* fPtr;
reed@google.com1c401d82011-10-18 18:52:03 +0000683 size_t fSize; // can be larger than the requested size (see kReuse)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000684 uint32_t fStorage[(kSize + 3) >> 2];
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000686// Can't guard the constructor because it's a template class.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687
688#endif /* C++ */
689
690#endif