rsUtils.h revision 0b575de8ed0b628d84d256f5846500b0385979bd
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ANDROID_RS_UTILS_H 18#define ANDROID_RS_UTILS_H 19 20#define LOG_NDEBUG 0 21#define LOG_TAG "RenderScript" 22 23#ifndef RS_SERVER 24#include <utils/Log.h> 25#include <utils/String8.h> 26#include <utils/Vector.h> 27#include <cutils/atomic.h> 28#endif 29 30#include <stdint.h> 31 32#include <stdlib.h> 33#include <pthread.h> 34#include <time.h> 35 36#include <math.h> 37 38#ifdef RS_SERVER 39 40#include <string> 41#include <vector> 42#include <algorithm> 43 44#define ALOGE(...) 45#define ALOGV(...) 46#define ALOGW(...) 47#define ALOGD(...) 48 49namespace android { 50 51 // server has no Vector or String8 classes; implement on top of STL 52 class String8: public std::string { 53 public: 54 String8(const char *ptr) : std::string(ptr) { 55 56 } 57 String8() : std::string() { 58 59 } 60 61 const char* string() const { 62 return this->c_str(); 63 } 64 65 void setTo(const char* str, ssize_t len) { 66 this->assign(str, len); 67 } 68 void setTo(const char* str) { 69 this->assign(str); 70 } 71 72 }; 73 74 template <class T> class Vector: public std::vector<T> { 75 public: 76 void push(T obj) { 77 this->push_back(obj); 78 } 79 void removeAt(uint32_t index) { 80 this->erase(this->begin() + index); 81 } 82 ssize_t add(const T& obj) { 83 this->push_back(obj); 84 return this->size() - 1; 85 } 86 void setCapacity(ssize_t capacity) { 87 this->resize(capacity); 88 } 89 90 T* editArray() { 91 return this->data(); 92 } 93 94 const T* array() { 95 return this->data(); 96 } 97 98 }; 99 100 template<> class Vector<bool>: public std::vector<char> { 101 public: 102 void push(bool obj) { 103 this->push_back(obj); 104 } 105 void removeAt(uint32_t index) { 106 this->erase(this->begin() + index); 107 } 108 ssize_t add(const bool& obj) { 109 this->push_back(obj); 110 return this->size() - 1; 111 } 112 void setCapacity(ssize_t capacity) { 113 this->resize(capacity); 114 } 115 116 bool* editArray() { 117 return (bool*)this->data(); 118 } 119 120 const bool* array() { 121 return (const bool*)this->data(); 122 } 123 }; 124 125} 126 127#endif // RS_SERVER 128 129namespace android { 130namespace renderscript { 131 132#if 1 133#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0) 134#else 135#define rsAssert(v) while (0) 136#endif 137 138template<typename T> 139T rsMin(T in1, T in2) 140{ 141 if (in1 > in2) { 142 return in2; 143 } 144 return in1; 145} 146 147template<typename T> 148T rsMax(T in1, T in2) { 149 if (in1 < in2) { 150 return in2; 151 } 152 return in1; 153} 154 155template<typename T> 156T rsFindHighBit(T val) { 157 uint32_t bit = 0; 158 while (val > 1) { 159 bit++; 160 val>>=1; 161 } 162 return bit; 163} 164 165template<typename T> 166bool rsIsPow2(T val) { 167 return (val & (val-1)) == 0; 168} 169 170template<typename T> 171T rsHigherPow2(T v) { 172 if (rsIsPow2(v)) { 173 return v; 174 } 175 return 1 << (rsFindHighBit(v) + 1); 176} 177 178template<typename T> 179T rsLowerPow2(T v) { 180 if (rsIsPow2(v)) { 181 return v; 182 } 183 return 1 << rsFindHighBit(v); 184} 185 186template<typename T> 187T rsRound(T v, unsigned int r) { 188 // Only valid for rounding up to powers of 2. 189 if ((r & (r - 1)) != 0) { 190 rsAssert(false && "Must be power of 2 for rounding up"); 191 return v; 192 } 193 T res = v + (r - 1); 194 if (res < v) { 195 rsAssert(false && "Overflow of rounding operation"); 196 return v; 197 } 198 res &= ~(r - 1); 199 return res; 200} 201 202static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) { 203 uint16_t t = 0; 204 t |= b >> 3; 205 t |= (g >> 2) << 5; 206 t |= (r >> 3) << 11; 207 return t; 208} 209 210static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) { 211 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f)); 212 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f); 213 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11)); 214 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11); 215} 216 217static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) { 218 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff); 219 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff); 220 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff); 221 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff); 222 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24); 223} 224 225} 226} 227 228#endif //ANDROID_RS_OBJECT_BASE_H 229 230 231