blob: 1f8ecaaf632c066249cf936129de6a81d6b725b5 [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
106///////////////////////////////////////////////////////////////////////
107
108/** Fast type for signed 8 bits. Use for parameter passing and local variables, not for storage
109*/
110typedef int S8CPU;
111/** Fast type for unsigned 8 bits. Use for parameter passing and local variables, not for storage
112*/
113typedef int S16CPU;
114/** Fast type for signed 16 bits. Use for parameter passing and local variables, not for storage
115*/
116typedef unsigned U8CPU;
117/** Fast type for unsigned 16 bits. Use for parameter passing and local variables, not for storage
118*/
119typedef unsigned U16CPU;
120
121/** Meant to be faster than bool (doesn't promise to be 0 or 1, just 0 or non-zero
122*/
123typedef int SkBool;
124/** Meant to be a small version of bool, for storage purposes. Will be 0 or 1
125*/
126typedef uint8_t SkBool8;
127
128#ifdef SK_DEBUG
129 int8_t SkToS8(long);
130 uint8_t SkToU8(size_t);
131 int16_t SkToS16(long);
132 uint16_t SkToU16(size_t);
133 int32_t SkToS32(long);
134 uint32_t SkToU32(size_t);
135#else
136 #define SkToS8(x) ((int8_t)(x))
137 #define SkToU8(x) ((uint8_t)(x))
138 #define SkToS16(x) ((int16_t)(x))
139 #define SkToU16(x) ((uint16_t)(x))
140 #define SkToS32(x) ((int32_t)(x))
141 #define SkToU32(x) ((uint32_t)(x))
142#endif
143
144/** Returns 0 or 1 based on the condition
145*/
146#define SkToBool(cond) ((cond) != 0)
147
148#define SK_MaxS16 32767
149#define SK_MinS16 -32767
150#define SK_MaxU16 0xFFFF
151#define SK_MinU16 0
152#define SK_MaxS32 0x7FFFFFFF
153#define SK_MinS32 0x80000001
154#define SK_MaxU32 0xFFFFFFFF
155#define SK_MinU32 0
156#define SK_NaN32 0x80000000
157
reed@android.comd4577752009-11-21 02:48:11 +0000158/** Returns true if the value can be represented with signed 16bits
159 */
reed@android.com90209ca2009-11-21 19:58:04 +0000160static inline bool SkIsS16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000161 return (int16_t)x == x;
162}
163
164/** Returns true if the value can be represented with unsigned 16bits
165 */
reed@android.com90209ca2009-11-21 19:58:04 +0000166static inline bool SkIsU16(long x) {
reed@android.comd4577752009-11-21 02:48:11 +0000167 return (uint16_t)x == x;
168}
169
170//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000171#ifndef SK_OFFSETOF
172 #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1)
173#endif
174
175/** Returns the number of entries in an array (not a pointer)
176*/
177#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0]))
178
179/** Returns x rounded up to a multiple of 2
180*/
181#define SkAlign2(x) (((x) + 1) >> 1 << 1)
182/** Returns x rounded up to a multiple of 4
183*/
184#define SkAlign4(x) (((x) + 3) >> 2 << 2)
185
186typedef uint32_t SkFourByteTag;
187#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
188
189/** 32 bit integer to hold a unicode value
190*/
191typedef int32_t SkUnichar;
192/** 32 bit value to hold a millisecond count
193*/
194typedef uint32_t SkMSec;
195/** 1 second measured in milliseconds
196*/
197#define SK_MSec1 1000
198/** maximum representable milliseconds
199*/
200#define SK_MSecMax 0x7FFFFFFF
201/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
202*/
203#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0)
204/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
205*/
206#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0)
207
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208/****************************************************************************
209 The rest of these only build with C++
210*/
211#ifdef __cplusplus
212
213/** Faster than SkToBool for integral conditions. Returns 0 or 1
214*/
reed@android.comd4577752009-11-21 02:48:11 +0000215static inline int Sk32ToBool(uint32_t n) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000216 return (n | (0-n)) >> 31;
217}
218
reed@android.comd4577752009-11-21 02:48:11 +0000219template <typename T> inline void SkTSwap(T& a, T& b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 T c(a);
221 a = b;
222 b = c;
223}
224
reed@android.comd4577752009-11-21 02:48:11 +0000225static inline int32_t SkAbs32(int32_t value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000226#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
227 if (value < 0)
228 value = -value;
229 return value;
230#else
231 int32_t mask = value >> 31;
232 return (value ^ mask) - mask;
233#endif
234}
235
reed@android.comd4577752009-11-21 02:48:11 +0000236static inline int32_t SkMax32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 if (a < b)
238 a = b;
239 return a;
240}
241
reed@android.comd4577752009-11-21 02:48:11 +0000242static inline int32_t SkMin32(int32_t a, int32_t b) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 if (a > b)
244 a = b;
245 return a;
246}
247
reed@android.comd4577752009-11-21 02:48:11 +0000248static inline int32_t SkSign32(int32_t a) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 return (a >> 31) | ((unsigned) -a >> 31);
250}
251
reed@android.comd4577752009-11-21 02:48:11 +0000252static inline int32_t SkFastMin32(int32_t value, int32_t max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
254 if (value > max)
255 value = max;
256 return value;
257#else
258 int diff = max - value;
259 // clear diff if it is negative (clear if value > max)
260 diff &= (diff >> 31);
261 return value + diff;
262#endif
263}
264
265/** Returns signed 32 bit value pinned between min and max, inclusively
266*/
reed@android.comd4577752009-11-21 02:48:11 +0000267static inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000268#ifdef SK_CPU_HAS_CONDITIONAL_INSTR
269 if (value < min)
270 value = min;
271 if (value > max)
272 value = max;
273#else
274 if (value < min)
275 value = min;
276 else if (value > max)
277 value = max;
278#endif
279 return value;
280}
281
reed@android.comd4577752009-11-21 02:48:11 +0000282static inline uint32_t SkSetClearShift(uint32_t bits, bool cond,
283 unsigned shift) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 SkASSERT((int)cond == 0 || (int)cond == 1);
285 return (bits & ~(1 << shift)) | ((int)cond << shift);
286}
287
reed@android.comd4577752009-11-21 02:48:11 +0000288static inline uint32_t SkSetClearMask(uint32_t bits, bool cond,
289 uint32_t mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 return cond ? bits | mask : bits & ~mask;
291}
292
293//////////////////////////////////////////////////////////////////////////////
294
295/** \class SkNoncopyable
296
297SkNoncopyable is the base class for objects that may do not want to
298be copied. It hides its copy-constructor and its assignment-operator.
299*/
300class SkNoncopyable {
301public:
302 SkNoncopyable() {}
303
304private:
305 SkNoncopyable(const SkNoncopyable&);
306 SkNoncopyable& operator=(const SkNoncopyable&);
307};
308
309class SkAutoFree : SkNoncopyable {
310public:
311 SkAutoFree() : fPtr(NULL) {}
312 explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
313 ~SkAutoFree() { sk_free(fPtr); }
314
315 /** Return the currently allocate buffer, or null
316 */
317 void* get() const { return fPtr; }
318
319 /** Assign a new ptr allocated with sk_malloc (or null), and return the
320 previous ptr. Note it is the caller's responsibility to sk_free the
321 returned ptr.
322 */
323 void* set(void* ptr) {
324 void* prev = fPtr;
325 fPtr = ptr;
326 return prev;
327 }
328
329 /** Transfer ownership of the current ptr to the caller, setting the
330 internal reference to null. Note the caller is reponsible for calling
331 sk_free on the returned address.
332 */
333 void* detach() { return this->set(NULL); }
334
335 /** Free the current buffer, and set the internal reference to NULL. Same
336 as calling sk_free(detach())
337 */
338 void free() {
339 sk_free(fPtr);
340 fPtr = NULL;
341 }
342
343private:
344 void* fPtr;
345 // illegal
346 SkAutoFree(const SkAutoFree&);
347 SkAutoFree& operator=(const SkAutoFree&);
348};
349
350class SkAutoMalloc : public SkAutoFree {
351public:
352 explicit SkAutoMalloc(size_t size)
353 : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {}
354
355 SkAutoMalloc(size_t size, unsigned flags)
356 : SkAutoFree(sk_malloc_flags(size, flags)) {}
357 SkAutoMalloc() {}
358
359 void* alloc(size_t size,
360 unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) {
361 sk_free(set(sk_malloc_flags(size, flags)));
362 return get();
363 }
364};
365
366template <size_t kSize> class SkAutoSMalloc : SkNoncopyable {
367public:
368 explicit SkAutoSMalloc(size_t size)
369 {
370 if (size <= kSize)
371 fPtr = fStorage;
372 else
373 fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP);
374 }
375 ~SkAutoSMalloc()
376 {
377 if (fPtr != (void*)fStorage)
378 sk_free(fPtr);
379 }
380 void* get() const { return fPtr; }
381private:
382 void* fPtr;
383 uint32_t fStorage[(kSize + 3) >> 2];
384 // illegal
385 SkAutoSMalloc(const SkAutoSMalloc&);
386 SkAutoSMalloc& operator=(const SkAutoSMalloc&);
387};
388
389#endif /* C++ */
390
391#endif
392