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_CPP_UTILS_H 18 #define ANDROID_RS_CPP_UTILS_H 19 20 #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) 21 #include <utils/Log.h> 22 #include <utils/String8.h> 23 #include <utils/Vector.h> 24 #include <cutils/atomic.h> 25 #endif 26 27 #include <stdint.h> 28 29 #include <stdlib.h> 30 #include <pthread.h> 31 #include <time.h> 32 33 #include <math.h> 34 35 #ifdef RS_COMPATIBILITY_LIB 36 #include <android/log.h> 37 #endif 38 39 #if defined(RS_SERVER) || defined(RS_COMPATIBILITY_LIB) 40 41 #define ATRACE_TAG 42 #define ATRACE_CALL(...) 43 44 #include <string> 45 #include <vector> 46 #include <algorithm> 47 48 #define ALOGE(...) \ 49 __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__); 50 #define ALOGW(...) \ 51 __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__); 52 #define ALOGD(...) \ 53 __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__); 54 #define ALOGV(...) \ 55 __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__); 56 57 #if defined(_WIN32) 58 #define OS_PATH_SEPARATOR '\\' 59 #else 60 #define OS_PATH_SEPARATOR '/' 61 #endif 62 63 namespace android { 64 65 // server has no Vector or String8 classes; implement on top of STL 66 class String8: public std::string { 67 public: 68 String8(const char *ptr) : std::string(ptr) { 69 70 } 71 String8(const char *ptr, size_t len) : std::string(ptr, len) { 72 73 } 74 String8() : std::string() { 75 76 } 77 78 const char* string() const { 79 return this->c_str(); 80 } 81 82 void setTo(const char* str, ssize_t len) { 83 this->assign(str, len); 84 } 85 void setTo(const char* str) { 86 this->assign(str); 87 } 88 String8 getPathDir(void) const { 89 const char* cp; 90 const char*const str = this->c_str(); 91 92 cp = strrchr(str, OS_PATH_SEPARATOR); 93 if (cp == NULL) 94 return String8(""); 95 else 96 return String8(str, cp - str); 97 } 98 }; 99 100 template <class T> class Vector: public std::vector<T> { 101 public: 102 void push(T 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 T& 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 T* editArray() { 117 return (T*)(this->begin()); 118 } 119 120 const T* array() { 121 return this->data(); 122 } 123 124 }; 125 126 template<> class Vector<bool>: public std::vector<char> { 127 public: 128 void push(bool obj) { 129 this->push_back(obj); 130 } 131 void removeAt(uint32_t index) { 132 this->erase(this->begin() + index); 133 } 134 ssize_t add(const bool& obj) { 135 this->push_back(obj); 136 return this->size() - 1; 137 } 138 void setCapacity(ssize_t capacity) { 139 this->resize(capacity); 140 } 141 142 bool* editArray() { 143 return (bool*)(&*this->begin()); 144 } 145 146 const bool* array() { 147 return (const bool*)(&*this->begin()); 148 } 149 }; 150 151 } 152 153 typedef int64_t nsecs_t; // nano-seconds 154 155 enum { 156 SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock 157 SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point 158 SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock 159 SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock 160 }; 161 162 static inline nsecs_t systemTime(int clock) 163 { 164 #if defined(__linux__) 165 static const clockid_t clocks[] = { 166 CLOCK_REALTIME, 167 CLOCK_MONOTONIC, 168 CLOCK_PROCESS_CPUTIME_ID, 169 CLOCK_THREAD_CPUTIME_ID 170 }; 171 struct timespec t; 172 t.tv_sec = t.tv_nsec = 0; 173 clock_gettime(clocks[clock], &t); 174 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; 175 #else 176 // we don't support the clocks here. 177 struct timeval t; 178 t.tv_sec = t.tv_usec = 0; 179 gettimeofday(&t, nullptr); 180 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; 181 #endif 182 } 183 184 static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) 185 { 186 return secs/1000000; 187 } 188 189 #endif // RS_SERVER || RS_COMPATIBILITY_LIB 190 191 namespace android { 192 namespace renderscript { 193 194 const char * rsuCopyString(const char *name); 195 const char * rsuCopyString(const char *name, size_t len); 196 197 #if 1 198 #define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0) 199 #else 200 #define rsAssert(v) while (0) 201 #endif 202 203 template<typename T> 204 T rsMin(T in1, T in2) 205 { 206 if (in1 > in2) { 207 return in2; 208 } 209 return in1; 210 } 211 212 template<typename T> 213 T rsMax(T in1, T in2) { 214 if (in1 < in2) { 215 return in2; 216 } 217 return in1; 218 } 219 220 template<typename T> 221 T rsFindHighBit(T val) { 222 uint32_t bit = 0; 223 while (val > 1) { 224 bit++; 225 val>>=1; 226 } 227 return bit; 228 } 229 230 template<typename T> 231 bool rsIsPow2(T val) { 232 return (val & (val-1)) == 0; 233 } 234 235 template<typename T> 236 T rsHigherPow2(T v) { 237 if (rsIsPow2(v)) { 238 return v; 239 } 240 return 1 << (rsFindHighBit(v) + 1); 241 } 242 243 template<typename T> 244 T rsLowerPow2(T v) { 245 if (rsIsPow2(v)) { 246 return v; 247 } 248 return 1 << rsFindHighBit(v); 249 } 250 251 template<typename T> 252 T rsRound(T v, unsigned int r) { 253 // Only valid for rounding up to powers of 2. 254 if ((r & (r - 1)) != 0) { 255 rsAssert(false && "Must be power of 2 for rounding up"); 256 return v; 257 } 258 T res = v + (r - 1); 259 if (res < v) { 260 rsAssert(false && "Overflow of rounding operation"); 261 return v; 262 } 263 res &= ~(r - 1); 264 return res; 265 } 266 267 static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) { 268 uint16_t t = 0; 269 t |= b >> 3; 270 t |= (g >> 2) << 5; 271 t |= (r >> 3) << 11; 272 return t; 273 } 274 275 static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) { 276 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f)); 277 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f); 278 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11)); 279 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11); 280 } 281 282 static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) { 283 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff); 284 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff); 285 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff); 286 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff); 287 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24); 288 } 289 290 const char* rsuJoinStrings(int n, const char* const* strs); 291 292 #ifndef RS_COMPATIBILITY_LIB 293 // Utility to fork/exec a command. 294 // exe - Command to execute 295 // nArgs - Number of arguments (excluding the trailing nullptr in args) 296 // args - Arguments to the command 297 bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args); 298 #endif 299 300 301 } 302 } 303 304 #endif //ANDROID_RS_OBJECT_BASE_H 305 306 307