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