SkTypes.h revision 9aa8b32233702b19b97bebdc2c702e0c53407d45
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 339aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com/** See SkGraphics::GetVersion() to retrieve these at runtime 349aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com */ 359aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MAJOR 1 369aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_MINOR 0 379aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com#define SKIA_VERSION_PATCH 0 389aa8b32233702b19b97bebdc2c702e0c53407d45reed@android.com 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/* 408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com memory wrappers to be implemented by the porting layer (platform) 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we run out of memory. The platform implementation must 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com not return, but should either throw an exception or otherwise exit. 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_out_of_memory(void); 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Called internally if we hit an unrecoverable error. 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The platform implementation must not return, but should either throw 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception or otherwise exit. 508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_throw(void); 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comenum { 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_TEMP = 0x01, //!< hint to sk_malloc that the requested memory will be freed in the scope of the stack frame 558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SK_MALLOC_THROW = 0x02 //!< instructs sk_malloc to call sk_throw if the memory cannot be allocated. 568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Return a block of memory (at least 4-byte aligned) of at least the 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com specified size. If the requested memory cannot be returned, either 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return null (if SK_MALLOC_TEMP bit is clear) or call sk_throw() 608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com (if SK_MALLOC_TEMP bit is set). To free the memory, call sk_free(). 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_malloc_flags(size_t size, unsigned flags); 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as sk_malloc(), but hard coded to pass SK_MALLOC_THROW as the flag 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_malloc_throw(size_t size); 668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Same as standard realloc(), but this one never returns null on failure. It will throw 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com an exception if it fails. 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void* sk_realloc_throw(void* buffer, size_t size); 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Free memory returned by sk_malloc(). It is safe to pass null. 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comextern void sk_free(void*); 738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 744516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com// bzero is safer than memset, but we can't rely on it, so... sk_bzero() 754516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.comstatic inline void sk_bzero(void* buffer, size_t size) { 764516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com memset(buffer, 0, size); 774516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com} 784516f4786f5dda1b86a8f825b9e8e910d9c2363creed@android.com 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_INIT_TO_AVOID_WARNING = 0 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkDebugf 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void SkDebugf(const char format[], ...); 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) SK_DEBUGBREAK(cond) 898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) code 908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) , type var 918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) , var 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com// #define SkDEBUGF(args ) SkDebugf##args 938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args ) SkDebugf args 948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) SkASSERT(cond) 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkASSERT(cond) 978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGCODE(code) 988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDEBUGF(args) 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkDECLAREPARAM(type, var) 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkPARAM(var) 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // unlike SkASSERT, this guy executes its condition in the non-debug build 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkAssertResult(cond) cond 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for signed 8 bits. Use for parameter passing and local variables, not for storage 1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int S8CPU; 1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for unsigned 8 bits. Use for parameter passing and local variables, not for storage 1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int S16CPU; 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for signed 16 bits. Use for parameter passing and local variables, not for storage 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef unsigned U8CPU; 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for unsigned 16 bits. Use for parameter passing and local variables, not for storage 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef unsigned U16CPU; 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Meant to be faster than bool (doesn't promise to be 0 or 1, just 0 or non-zero 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int SkBool; 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Meant to be a small version of bool, for storage purposes. Will be 0 or 1 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint8_t SkBool8; 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int8_t SkToS8(long); 1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint8_t SkToU8(size_t); 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int16_t SkToS16(long); 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint16_t SkToU16(size_t); 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int32_t SkToS32(long); 1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t SkToU32(size_t); 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS8(x) ((int8_t)(x)) 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU8(x) ((uint8_t)(x)) 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS16(x) ((int16_t)(x)) 1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU16(x) ((uint16_t)(x)) 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS32(x) ((int32_t)(x)) 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU32(x) ((uint32_t)(x)) 1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond) ((cond) != 0) 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16 32767 1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16 -32767 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16 0xFFFF 1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16 0 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32 0x7FFFFFFF 1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS32 0x80000001 1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32 0xFFFFFFFF 1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32 0 1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_NaN32 0x80000000 1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 158d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits 159d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 16090209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) { 161d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (int16_t)x == x; 162d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 163d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 164d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits 165d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 16690209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) { 167d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (uint16_t)x == x; 168d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 169d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 170d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com////////////////////////////////////////////////////////////////////////////// 1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF 1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1) 1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer) 1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) 1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 2 1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x) (((x) + 1) >> 1 << 1) 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 4 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x) (((x) + 3) >> 2 << 2) 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag; 1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar; 1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count 1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec; 1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds 1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000 1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds 1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF 2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/**************************************************************************** 2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The rest of these only build with C++ 2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 215d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) { 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (n | (0-n)) >> 31; 2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 219d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) { 2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com T c(a); 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = c; 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 225d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) { 2268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < 0) 2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = -value; 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int32_t mask = value >> 31; 2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (value ^ mask) - mask; 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 236d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(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 SkMin32(int32_t a, int32_t b) { 2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a > b) 2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 248d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) { 2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (a >> 31) | ((unsigned) -a >> 31); 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 252d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) { 2538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int diff = max - value; 2598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // clear diff if it is negative (clear if value > max) 2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com diff &= (diff >> 31); 2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value + diff; 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 267d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else if (value > max) 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 282d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond, 283d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com unsigned shift) { 2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT((int)cond == 0 || (int)cond == 1); 2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (bits & ~(1 << shift)) | ((int)cond << shift); 2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 288d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond, 289d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com uint32_t mask) { 2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return cond ? bits | mask : bits & ~mask; 2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 2948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable 2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to 2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator. 2998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkNoncopyable { 3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable() {} 3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable(const SkNoncopyable&); 3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable& operator=(const SkNoncopyable&); 3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable { 3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree() : fPtr(NULL) {} 3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoFree() { sk_free(fPtr); } 3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Return the currently allocate buffer, or null 3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Assign a new ptr allocated with sk_malloc (or null), and return the 3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com previous ptr. Note it is the caller's responsibility to sk_free the 3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com returned ptr. 3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* set(void* ptr) { 3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* prev = fPtr; 3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = ptr; 3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prev; 3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Transfer ownership of the current ptr to the caller, setting the 3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com internal reference to null. Note the caller is reponsible for calling 3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free on the returned address. 3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* detach() { return this->set(NULL); } 3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Free the current buffer, and set the internal reference to NULL. Same 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com as calling sk_free(detach()) 3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void free() { 3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = NULL; 3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree(const SkAutoFree&); 3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree& operator=(const SkAutoFree&); 3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoMalloc : public SkAutoFree { 3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoMalloc(size_t size) 3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {} 3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoMalloc(size_t size, unsigned flags) 3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com : SkAutoFree(sk_malloc_flags(size, flags)) {} 3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoMalloc() {} 3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* alloc(size_t size, 3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) { 3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(set(sk_malloc_flags(size, flags))); 3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return get(); 3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable { 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoSMalloc(size_t size) 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (size <= kSize) 3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = fStorage; 3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoSMalloc() 3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (fPtr != (void*)fStorage) 3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t fStorage[(kSize + 3) >> 2]; 3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc(const SkAutoSMalloc&); 3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc& operator=(const SkAutoSMalloc&); 3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */ 3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 393