SkTypes.h revision 28be72b63e457c680c192a34fb9f58e1c693363f
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 10628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgnamespace { 10728be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 10828be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgtemplate <bool> 10928be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgstruct SkCompileAssert { 11028be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org}; 11128be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 11228be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org} // namespace 11328be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 11428be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org#define SK_COMPILE_ASSERT(expr, msg) \ 11528be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org typedef SkCompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 11628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/////////////////////////////////////////////////////////////////////// 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for signed 8 bits. Use for parameter passing and local variables, not for storage 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int S8CPU; 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for unsigned 8 bits. Use for parameter passing and local variables, not for storage 1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int S16CPU; 1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for signed 16 bits. Use for parameter passing and local variables, not for storage 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef unsigned U8CPU; 1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Fast type for unsigned 16 bits. Use for parameter passing and local variables, not for storage 1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef unsigned U16CPU; 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Meant to be faster than bool (doesn't promise to be 0 or 1, just 0 or non-zero 1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int SkBool; 1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Meant to be a small version of bool, for storage purposes. Will be 0 or 1 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint8_t SkBool8; 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_DEBUG 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int8_t SkToS8(long); 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint8_t SkToU8(size_t); 1428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int16_t SkToS16(long); 1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint16_t SkToU16(size_t); 1448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int32_t SkToS32(long); 1458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t SkToU32(size_t); 1468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 1478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS8(x) ((int8_t)(x)) 1488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU8(x) ((uint8_t)(x)) 1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS16(x) ((int16_t)(x)) 1508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU16(x) ((uint16_t)(x)) 1518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToS32(x) ((int32_t)(x)) 1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SkToU32(x) ((uint32_t)(x)) 1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns 0 or 1 based on the condition 1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkToBool(cond) ((cond) != 0) 1588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS16 32767 1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS16 -32767 1618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU16 0xFFFF 1628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU16 0 1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxS32 0x7FFFFFFF 1648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinS32 0x80000001 1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MaxU32 0xFFFFFFFF 1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MinU32 0 1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_NaN32 0x80000000 1688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 169d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with signed 16bits 170d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 17190209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsS16(long x) { 172d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (int16_t)x == x; 173d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 174d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 175d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com/** Returns true if the value can be represented with unsigned 16bits 176d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com */ 17790209caa686464cad70dd9d60b53c3d967eb57dareed@android.comstatic inline bool SkIsU16(long x) { 178d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com return (uint16_t)x == x; 179d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com} 180d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com 181d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com////////////////////////////////////////////////////////////////////////////// 1828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SK_OFFSETOF 1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com #define SK_OFFSETOF(type, field) ((char*)&(((type*)1)->field) - (char*)1) 1848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns the number of entries in an array (not a pointer) 1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) 1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 2 1918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign2(x) (((x) + 1) >> 1 << 1) 1938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns x rounded up to a multiple of 4 1948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 1958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkAlign4(x) (((x) + 3) >> 2 << 2) 1968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkFourByteTag; 1988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkSetFourByteTag(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d)) 1998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit integer to hold a unicode value 2018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef int32_t SkUnichar; 2038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 32 bit value to hold a millisecond count 2048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtypedef uint32_t SkMSec; 2068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** 1 second measured in milliseconds 2078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSec1 1000 2098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** maximum representable milliseconds 2108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SK_MSecMax 0x7FFFFFFF 2128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 2138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LT(a, b) ((int32_t)(a) - (int32_t)(b) < 0) 2158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0 2168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkMSec_LE(a, b) ((int32_t)(a) - (int32_t)(b) <= 0) 2188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/**************************************************************************** 2208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com The rest of these only build with C++ 2218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 2228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef __cplusplus 2238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Faster than SkToBool for integral conditions. Returns 0 or 1 2258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 226d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int Sk32ToBool(uint32_t n) { 2278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (n | (0-n)) >> 31; 2288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 230d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comtemplate <typename T> inline void SkTSwap(T& a, T& b) { 2318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com T c(a); 2328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com b = c; 2348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 236d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkAbs32(int32_t value) { 2378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < 0) 2398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = -value; 2408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int32_t mask = value >> 31; 2438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (value ^ mask) - mask; 2448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 247d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMax32(int32_t a, int32_t b) { 2488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a < b) 2498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 2518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 253d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkMin32(int32_t a, int32_t b) { 2548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (a > b) 2558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com a = b; 2568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return a; 2578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 259d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkSign32(int32_t a) { 2608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (a >> 31) | ((unsigned) -a >> 31); 2618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 263d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkFastMin32(int32_t value, int32_t max) { 2648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 2668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com int diff = max - value; 2708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // clear diff if it is negative (clear if value > max) 2718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com diff &= (diff >> 31); 2728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value + diff; 2738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 2768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** Returns signed 32 bit value pinned between min and max, inclusively 2778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 278d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline int32_t SkPin32(int32_t value, int32_t min, int32_t max) { 2798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifdef SK_CPU_HAS_CONDITIONAL_INSTR 2808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 2818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 2828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value > max) 2838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else 2858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (value < min) 2868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = min; 2878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else if (value > max) 2888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com value = max; 2898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 2908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return value; 2918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 293d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearShift(uint32_t bits, bool cond, 294d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com unsigned shift) { 2958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkASSERT((int)cond == 0 || (int)cond == 1); 2968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return (bits & ~(1 << shift)) | ((int)cond << shift); 2978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 2988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 299d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.comstatic inline uint32_t SkSetClearMask(uint32_t bits, bool cond, 300d4577757874d1dda1a3bffa3f2347c251859c27ereed@android.com uint32_t mask) { 3018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return cond ? bits | mask : bits & ~mask; 3028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 3038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 3058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/** \class SkNoncopyable 3078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkNoncopyable is the base class for objects that may do not want to 3098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combe copied. It hides its copy-constructor and its assignment-operator. 3108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com*/ 3118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkNoncopyable { 3128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable() {} 3148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable(const SkNoncopyable&); 3178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkNoncopyable& operator=(const SkNoncopyable&); 3188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoFree : SkNoncopyable { 3218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree() : fPtr(NULL) {} 3238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoFree(void* ptr) : fPtr(ptr) {} 3248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoFree() { sk_free(fPtr); } 3258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Return the currently allocate buffer, or null 3278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 3298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Assign a new ptr allocated with sk_malloc (or null), and return the 3318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com previous ptr. Note it is the caller's responsibility to sk_free the 3328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com returned ptr. 3338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* set(void* ptr) { 3358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* prev = fPtr; 3368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = ptr; 3378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return prev; 3388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Transfer ownership of the current ptr to the caller, setting the 3418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com internal reference to null. Note the caller is reponsible for calling 3428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free on the returned address. 3438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* detach() { return this->set(NULL); } 3458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com /** Free the current buffer, and set the internal reference to NULL. Same 3478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com as calling sk_free(detach()) 3488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */ 3498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void free() { 3508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 3518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = NULL; 3528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 3568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 3578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree(const SkAutoFree&); 3588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoFree& operator=(const SkAutoFree&); 3598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkAutoMalloc : public SkAutoFree { 3628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoMalloc(size_t size) 3648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com : SkAutoFree(sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP)) {} 3658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoMalloc(size_t size, unsigned flags) 3678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com : SkAutoFree(sk_malloc_flags(size, flags)) {} 3688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoMalloc() {} 3698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* alloc(size_t size, 3718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com unsigned flags = (SK_MALLOC_THROW | SK_MALLOC_TEMP)) { 3728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(set(sk_malloc_flags(size, flags))); 3738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return get(); 3748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 3778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comtemplate <size_t kSize> class SkAutoSMalloc : SkNoncopyable { 3788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 3798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com explicit SkAutoSMalloc(size_t size) 3808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 3818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (size <= kSize) 3828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = fStorage; 3838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com else 3848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fPtr = sk_malloc_flags(size, SK_MALLOC_THROW | SK_MALLOC_TEMP); 3858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com ~SkAutoSMalloc() 3878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com { 3888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com if (fPtr != (void*)fStorage) 3898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com sk_free(fPtr); 3908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 3918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* get() const { return fPtr; } 3928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 3938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com void* fPtr; 3948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com uint32_t fStorage[(kSize + 3) >> 2]; 3958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // illegal 3968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc(const SkAutoSMalloc&); 3978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkAutoSMalloc& operator=(const SkAutoSMalloc&); 3988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 3998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif /* C++ */ 4018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 4028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif 4038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 404