SkTypes.h revision 90209caa686464cad70dd9d60b53c3d967eb57da
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2006 The Android Open Source Project 38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License"); 58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License. 68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at 78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * http://www.apache.org/licenses/LICENSE-2.0 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS, 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License. 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkTypes_DEFINED 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkTypes_DEFINED 198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPreConfig.h" 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUserConfig.h" 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPostConfig.h" 238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_IGNORE_STDINT_DOT_H 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #include <stdint.h> 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include <stdio.h> 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \file SkTypes.h 318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memory wrappers to be implemented by the porting layer (platform) 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com not return, but should either throw an exception or otherwise exit. 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_out_of_memory(void); 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error. 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The platform implementation must not return, but should either throw 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception or otherwise exit. 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_throw(void); 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum { 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com specified size. If the requested memory cannot be returned, either 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw() 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_malloc_flags(size_t size, unsigned flags); 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_malloc_throw(size_t size); 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception if it fails. 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_realloc_throw(void* buffer, size_t size); 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null. 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_free(void*); 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 684516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero() 694516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) { 704516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com memset(buffer, 0, size); 714516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com} 724516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING = 0 768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void SkDebugf(const char format[], ...); 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) SK_DEBUGBREAK(cond) 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) code 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) , type var 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) , var 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// #define SkDEBUGF(args ) SkDebugf##args 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args ) SkDebugf args 888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) SkASSERT(cond) 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args) 938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // unlike SkASSERT, this guy executes its condition in the non-debug build 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) cond 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for signed 8 bits. Use for parameter passing and local variables, not for storage 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int S8CPU; 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for unsigned 8 bits. Use for parameter passing and local variables, not for storage 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int S16CPU; 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for signed 16 bits. Use for parameter passing and local variables, not for storage 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef unsigned U8CPU; 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for unsigned 16 bits. Use for parameter passing and local variables, not for storage 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef unsigned U16CPU; 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Meant to be faster than bool (doesn't promise to be 0 or 1, just 0 or non-zero 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int SkBool; 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Meant to be a small version of bool, for storage purposes. Will be 0 or 1 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint8_t SkBool8; 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int8_t SkToS8(long); 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint8_t SkToU8(size_t); 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int16_t SkToS16(long); 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint16_t SkToU16(size_t); 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int32_t SkToS32(long); 1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t SkToU32(size_t); 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS8(x) ((int8_t)(x)) 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU8(x) ((uint8_t)(x)) 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS16(x) ((int16_t)(x)) 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU16(x) ((uint16_t)(x)) 1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS32(x) ((int32_t)(x)) 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU32(x) ((uint32_t)(x)) 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition 1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond) ((cond) != 0) 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16 32767 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16 -32767 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16 0xFFFF 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16 0 1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32 0x7FFFFFFF 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS32 0x80000001 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32 0xFFFFFFFF 1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32 0 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_NaN32 0x80000000 1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 152d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits 153d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 15490209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) { 155d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (int16_t)x == x; 156d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 157d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 158d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits 159d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 16090209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) { 161d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (uint16_t)x == x; 162d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 163d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 164d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com////////////////////////////////////////////////////////////////////////////// 1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF 1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1) 1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer) 1708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) 1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 2 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x) (((x) + 1) >> 1 << 1) 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 4 1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x) (((x) + 3) >> 2 << 2) 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag; 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar; 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count 1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec; 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000 1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds 1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF 1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/**************************************************************************** 2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The rest of these only build with C++ 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 209d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) { 2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (n | (0-n)) >> 31; 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 213d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) { 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com T c(a); 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = c; 2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 219d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) { 2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < 0) 2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = -value; 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int32_t mask = value >> 31; 2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (value ^ mask) - mask; 2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 230d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) { 2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a < b) 2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 236d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) { 2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a > b) 2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 242d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) { 2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (a >> 31) | ((unsigned) -a >> 31); 2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 246d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) { 2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int diff = max - value; 2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // clear diff if it is negative (clear if value > max) 2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com diff &= (diff >> 31); 2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value + diff; 2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively 2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 261d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else if (value > max) 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 276d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond, 277d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com unsigned shift) { 2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT((int)cond == 0 || (int)cond == 1); 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (bits & ~(1 << shift)) | ((int)cond << shift); 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 282d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond, 283d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com uint32_t mask) { 2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return cond ? bits | mask : bits & ~mask; 2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable 2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator. 2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkNoncopyable { 2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable() {} 2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable(const SkNoncopyable&); 3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable& operator=(const SkNoncopyable&); 3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable { 3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree() : fPtr(NULL) {} 3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoFree() { sk_free(fPtr); } 3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Return the currently allocate buffer, or null 3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Assign a new ptr allocated with sk_malloc (or null), and return the 3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com previous ptr. Note it is the caller's responsibility to sk_free the 3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com returned ptr. 3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* set(void* ptr) { 3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* prev = fPtr; 3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = ptr; 3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prev; 3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Transfer ownership of the current ptr to the caller, setting the 3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com internal reference to null. Note the caller is reponsible for calling 3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free on the returned address. 3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* detach() { return this->set(NULL); } 3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Free the current buffer, and set the internal reference to NULL. Same 3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com as calling sk_free(detach()) 3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void free() { 3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = NULL; 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree(const SkAutoFree&); 3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree& operator=(const SkAutoFree&); 3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoMalloc : public SkAutoFree { 3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoMalloc(size_t size) 3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {} 3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoMalloc(size_t size, unsigned flags) 3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com : SkAutoFree(sk_malloc_flags(size, flags)) {} 3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoMalloc() {} 3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* alloc(size_t size, 3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) { 3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(set(sk_malloc_flags(size, flags))); 3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return get(); 3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable { 3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoSMalloc(size_t size) 3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (size <= kSize) 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = fStorage; 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoSMalloc() 3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (fPtr != (void*)fStorage) 3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t fStorage[(kSize + 3) >> 2]; 3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc(const SkAutoSMalloc&); 3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc& operator=(const SkAutoSMalloc&); 3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */ 3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 387