blob: 8dbd51b0166e9710f87d1babfbc5034c392f145a [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkTypes_DEFINED
18#define SkTypes_DEFINED
19
20#include "SkPreConfig.h"
21#include "SkUserConfig.h"
22#include "SkPostConfig.h"
23
24#ifndef SK_IGNORE_STDINT_DOT_H
25 #include <stdint.h>
26#endif
27
28#include <stdio.h>
29
30/** \file SkTypes.h
31*/
32
reed@android.com9aa8b322010-04-13 13:22:54 +000033/** See SkGraphics::GetVersion() to retrieve these at runtime
34 */
35#define SKIA_VERSION_MAJOR 1
36#define SKIA_VERSION_MINOR 0
37#define SKIA_VERSION_PATCH 0
38
reed@android.com8a1c16f2008-12-17 15:59:43 +000039/*
40 memory wrappers to be implemented by the porting layer (platform)
41*/
42
43/** Called internally if we run out of memory. The platform implementation must
44 not return, but should either throw an exception or otherwise exit.
45*/
46extern void sk_out_of_memory(void);
47/** Called internally if we hit an unrecoverable error.
48 The platform implementation must not return, but should either throw
49 an exception or otherwise exit.
50*/
51extern void sk_throw(void);
52
53enum {
54 SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame
55 SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated.
56};
57/** Return a block of memory (at least 4-byte aligned) of at least the
58 specified size. If the requested memory cannot be returned, either
59 return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw()
60 (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free().
61*/
62extern void* sk_malloc_flags(size_t size, unsigned flags);
63/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag
64*/
65extern void* sk_malloc_throw(size_t size);
66/** Same as standard realloc(), but this one never returns null on failure. It will throw
67 an exception if it fails.
68*/
69extern void* sk_realloc_throw(void* buffer, size_t size);
70/** Free memory returned by sk_malloc(). It is safe to pass null.
71*/
72extern void sk_free(void*);
73
reed@android.com4516f472009-06-29 16:25:36 +000074// bzero is safer than memset, but we can't rely on it, so... sk_bzero()
75static inline void sk_bzero(void* buffer, size_t size) {
76 memset(buffer, 0, size);
77}
78
reed@android.com8a1c16f2008-12-17 15:59:43 +000079///////////////////////////////////////////////////////////////////////
80
81#define SK_INIT_TO_AVOID_WARNING = 0
82
83#ifndef SkDebugf
84 void SkDebugf(const char format[], ...);
85#endif
86
87#ifdef SK_DEBUG
88 #define SkASSERT(cond) SK_DEBUGBREAK(cond)
89 #define SkDEBUGCODE(code) code
90 #define SkDECLAREPARAM(type, var) , type var
91 #define SkPARAM(var) , var
92// #define SkDEBUGF(args ) SkDebugf##args
93 #define SkDEBUGF(args ) SkDebugf args
94 #define SkAssertResult(cond) SkASSERT(cond)
95#else
96 #define SkASSERT(cond)
97 #define SkDEBUGCODE(code)
98 #define SkDEBUGF(args)
99 #define SkDECLAREPARAM(type, var)
100 #define SkPARAM(var)
101
102 // unlike SkASSERT, this guy executes its condition in the non-debug build
103 #define SkAssertResult(cond) cond
104#endif
105
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000106namespace {
107
108template <bool>
109struct SkCompileAssert {
110};
111
112} // namespace
113
114#define SK_COMPILE_ASSERT(expr, msg) \
115 typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
116
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117///////////////////////////////////////////////////////////////////////
118
reed@google.com37a31332011-01-25 14:55:42 +0000119/**
120 * Fast type for signed 8 bits. Use for parameter passing and local variables,
121 * not for storage.
122 */
123typedef int S8CPU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
reed@google.com37a31332011-01-25 14:55:42 +0000125/**
126 * Fast type for unsigned 8 bits. Use for parameter passing and local
127 * variables, not for storage
128 */
129typedef unsigned U8CPU;
130
131/**
132 * Fast type for signed 16 bits. Use for parameter passing and local variables,
133 * not for storage
134 */
135typedef int S16CPU;
136
137/**
138 * Fast type for unsigned 16 bits. Use for parameter passing and local
139 * variables, not for storage
140 */
141typedef unsigned U16CPU;
142
143/**
144 * Meant to be faster than bool (doesn't promise to be 0 or 1,
145 * just 0 or non-zero
146 */
147typedef int SkBool;
148
149/**
150 * Meant to be a small version of bool, for storage purposes. Will be 0 or 1
151 */
152typedef uint8_t SkBool8;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153
154#ifdef SK_DEBUG
155 int8_t SkToS8(long);
156 uint8_t SkToU8(size_t);
157 int16_t SkToS16(long);
158 uint16_t SkToU16(size_t);
159 int32_t SkToS32(long);
160 uint32_t SkToU32(size_t);
161#else
162 #define SkToS8(x) ((int8_t)(x))
163 #define SkToU8(x) ((uint8_t)(x))
164 #define SkToS16(x) ((int16_t)(x))
165 #define SkToU16(x) ((uint16_t)(x))
166 #define SkToS32(x) ((int32_t)(x))
167 #define SkToU32(x) ((uint32_t)(x))
168#endif
169
170/** Returns 0 or 1 based on the condition
171*/
172#define SkToBool(cond) ((cond) != 0)
173
174#define SK_MaxS16 32767
175#define SK_MinS16 -32767
176#define SK_MaxU16 0xFFFF
177#define SK_MinU16 0
178#define SK_MaxS32 0x7FFFFFFF
179#define SK_MinS32 0x80000001
180#define SK_MaxU32 0xFFFFFFFF
181#define SK_MinU32 0
182#define SK_NaN32 0x80000000
183
reed@android.comd4577752009-11-21 02:48:11 +0000184/** Returns true if the value can be represented with signed 16bits
185 */
reed@android.com90209ca2009-11-21 19:58:04 +0000186static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000187 return (int16_t)x == x;
188}
189
190/** Returns true if the value can be represented with unsigned 16bits
191 */
reed@android.com90209ca2009-11-21 19:58:04 +0000192static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000193 return (uint16_t)x == x;
194}
195
196//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197#ifndef SK_OFFSETOF
198 #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1)
199#endif
200
201/** Returns the number of entries in an array (not a pointer)
202*/
203#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
204
205/** Returns x rounded up to a multiple of 2
206*/
207#define SkAlign2(x) (((x) + 1) >> 1 << 1)
208/** Returns x rounded up to a multiple of 4
209*/
210#define SkAlign4(x) (((x) + 3) >> 2 << 2)
211
212typedef uint32_t SkFourByteTag;
213#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
214
215/** 32 bit integer to hold a unicode value
216*/
217typedef int32_t SkUnichar;
218/** 32 bit value to hold a millisecond count
219*/
220typedef uint32_t SkMSec;
221/** 1 second measured in milliseconds
222*/
223#define SK_MSec1 1000
224/** maximum representable milliseconds
225*/
226#define SK_MSecMax 0x7FFFFFFF
227/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
228*/
229#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
230/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
231*/
232#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
233
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234/****************************************************************************
235 The rest of these only build with C++
236*/
237#ifdef __cplusplus
238
239/** Faster than SkToBool for integral conditions. Returns 0 or 1
240*/
reed@android.comd4577752009-11-21 02:48:11 +0000241static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 return (n | (0-n)) >> 31;
243}
244
reed@android.comd4577752009-11-21 02:48:11 +0000245template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 T c(a);
247 a = b;
248 b = c;
249}
250
reed@android.comd4577752009-11-21 02:48:11 +0000251static inline int32_t SkAbs32(int32_t value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
253 if (value < 0)
254 value = -value;
255 return value;
256#else
257 int32_t mask = value >> 31;
258 return (value ^ mask) - mask;
259#endif
260}
261
reed@android.comd4577752009-11-21 02:48:11 +0000262static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263 if (a < b)
264 a = b;
265 return a;
266}
267
reed@android.comd4577752009-11-21 02:48:11 +0000268static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 if (a > b)
270 a = b;
271 return a;
272}
273
reed@android.comd4577752009-11-21 02:48:11 +0000274static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 return (a >> 31) | ((unsigned) -a >> 31);
276}
277
reed@android.comd4577752009-11-21 02:48:11 +0000278static inline int32_t SkFastMin32(int32_t value, int32_t max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
280 if (value > max)
281 value = max;
282 return value;
283#else
284 int diff = max - value;
285 // clear diff if it is negative (clear if value > max)
286 diff &= (diff >> 31);
287 return value + diff;
288#endif
289}
290
291/** Returns signed 32 bit value pinned between min and max, inclusively
292*/
reed@android.comd4577752009-11-21 02:48:11 +0000293static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000294#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
295 if (value < min)
296 value = min;
297 if (value > max)
298 value = max;
299#else
300 if (value < min)
301 value = min;
302 else if (value > max)
303 value = max;
304#endif
305 return value;
306}
307
reed@android.comd4577752009-11-21 02:48:11 +0000308static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
309 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 SkASSERT((int)cond == 0 || (int)cond == 1);
311 return (bits & ~(1 << shift)) | ((int)cond << shift);
312}
313
reed@android.comd4577752009-11-21 02:48:11 +0000314static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
315 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316 return cond ? bits | mask : bits & ~mask;
317}
318
reed@google.com1fcd51e2011-01-05 15:50:27 +0000319///////////////////////////////////////////////////////////////////////////////
320
321/**
322 * Use to cast a pointer to a different type, and maintaining strict-aliasing
323 */
324template <typename Dst> Dst SkTCast(const void* ptr) {
325 union {
326 const void* src;
327 Dst dst;
328 } data;
329 data.src = ptr;
330 return data.dst;
331}
332
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333//////////////////////////////////////////////////////////////////////////////
334
335/** \class SkNoncopyable
336
337SkNoncopyable is the base class for objects that may do not want to
338be copied. It hides its copy-constructor and its assignment-operator.
339*/
340class SkNoncopyable {
341public:
342 SkNoncopyable() {}
reed@google.com1fcd51e2011-01-05 15:50:27 +0000343
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344private:
345 SkNoncopyable(const SkNoncopyable&);
346 SkNoncopyable& operator=(const SkNoncopyable&);
347};
348
349class SkAutoFree : SkNoncopyable {
350public:
351 SkAutoFree() : fPtr(NULL) {}
352 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
353 ~SkAutoFree() { sk_free(fPtr); }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000354
reed@android.com8a1c16f2008-12-17 15:59:43 +0000355 /** Return the currently allocate buffer, or null
356 */
357 void* get() const { return fPtr; }
358
359 /** Assign a new ptr allocated with sk_malloc (or null), and return the
360 previous ptr. Note it is the caller's responsibility to sk_free the
361 returned ptr.
362 */
363 void* set(void* ptr) {
364 void* prev = fPtr;
365 fPtr = ptr;
366 return prev;
367 }
reed@google.com1fcd51e2011-01-05 15:50:27 +0000368
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369 /** Transfer ownership of the current ptr to the caller, setting the
370 internal reference to null. Note the caller is reponsible for calling
371 sk_free on the returned address.
372 */
373 void* detach() { return this->set(NULL); }
374
375 /** Free the current buffer, and set the internal reference to NULL. Same
376 as calling sk_free(detach())
377 */
378 void free() {
379 sk_free(fPtr);
380 fPtr = NULL;
381 }
382
383private:
384 void* fPtr;
385 // illegal
386 SkAutoFree(const SkAutoFree&);
387 SkAutoFree& operator=(const SkAutoFree&);
388};
389
390class SkAutoMalloc : public SkAutoFree {
391public:
392 explicit SkAutoMalloc(size_t size)
393 : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
394
395 SkAutoMalloc(size_t size, unsigned flags)
396 : SkAutoFree(sk_malloc_flags(size, flags)) {}
397 SkAutoMalloc() {}
398
399 void* alloc(size_t size,
400 unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
401 sk_free(set(sk_malloc_flags(size, flags)));
402 return get();
403 }
404};
405
406template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
407public:
408 explicit SkAutoSMalloc(size_t size)
409 {
410 if (size <= kSize)
411 fPtr = fStorage;
412 else
413 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
414 }
415 ~SkAutoSMalloc()
416 {
417 if (fPtr != (void*)fStorage)
418 sk_free(fPtr);
419 }
420 void* get() const { return fPtr; }
421private:
422 void* fPtr;
423 uint32_t fStorage[(kSize + 3) >> 2];
424 // illegal
425 SkAutoSMalloc(const SkAutoSMalloc&);
426 SkAutoSMalloc& operator=(const SkAutoSMalloc&);
427};
428
429#endif /* C++ */
430
431#endif
432