blob: c1e72c2884ad96af8f76874db3beb491e47ce198 [file] [log] [blame]
Jason Samsf9e077a2013-03-20 19:04:19 -07001/*
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
Stephen Hinesb0934b62013-07-03 17:27:38 -070020#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Jason Samsf9e077a2013-03-20 19:04:19 -070021#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
Stephen Hinesb0934b62013-07-03 17:27:38 -070035#ifdef RS_COMPATIBILITY_LIB
36#include <android/log.h>
37#endif
38
39#if defined(RS_SERVER) || defined(RS_COMPATIBILITY_LIB)
Jason Samsf9e077a2013-03-20 19:04:19 -070040
41#include <string>
42#include <vector>
43#include <algorithm>
44
Stephen Hinesb0934b62013-07-03 17:27:38 -070045#define ALOGE(...) \
46 __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);
47#define ALOGW(...) \
48 __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__);
49#define ALOGD(...) \
50 __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__);
51#define ALOGV(...) \
52 __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__);
53
Jason Samsf9e077a2013-03-20 19:04:19 -070054namespace android {
55
56 // server has no Vector or String8 classes; implement on top of STL
57 class String8: public std::string {
58 public:
59 String8(const char *ptr) : std::string(ptr) {
60
61 }
Stephen Hinesb0934b62013-07-03 17:27:38 -070062 String8(const char *ptr, size_t len) : std::string(ptr, len) {
63
64 }
Jason Samsf9e077a2013-03-20 19:04:19 -070065 String8() : std::string() {
66
67 }
68
69 const char* string() const {
70 return this->c_str();
71 }
72
73 void setTo(const char* str, ssize_t len) {
74 this->assign(str, len);
75 }
76 void setTo(const char* str) {
77 this->assign(str);
78 }
Stephen Hinesb0934b62013-07-03 17:27:38 -070079 String8 getPathDir(void) const {
80 const char* cp;
81 const char*const str = this->c_str();
Jason Samsf9e077a2013-03-20 19:04:19 -070082
Stephen Hinesb0934b62013-07-03 17:27:38 -070083 cp = strrchr(str, OS_PATH_SEPARATOR);
84 if (cp == NULL)
85 return String8("");
86 else
87 return String8(str, cp - str);
88 }
Jason Samsf9e077a2013-03-20 19:04:19 -070089 };
90
91 template <class T> class Vector: public std::vector<T> {
92 public:
93 void push(T obj) {
94 this->push_back(obj);
95 }
96 void removeAt(uint32_t index) {
97 this->erase(this->begin() + index);
98 }
99 ssize_t add(const T& obj) {
100 this->push_back(obj);
101 return this->size() - 1;
102 }
103 void setCapacity(ssize_t capacity) {
104 this->resize(capacity);
105 }
106
107 T* editArray() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700108 return (T*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700109 }
110
111 const T* array() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700112 return (const T*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700113 }
114
115 };
116
117 template<> class Vector<bool>: public std::vector<char> {
118 public:
119 void push(bool obj) {
120 this->push_back(obj);
121 }
122 void removeAt(uint32_t index) {
123 this->erase(this->begin() + index);
124 }
125 ssize_t add(const bool& obj) {
126 this->push_back(obj);
127 return this->size() - 1;
128 }
129 void setCapacity(ssize_t capacity) {
130 this->resize(capacity);
131 }
132
133 bool* editArray() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700134 return (bool*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700135 }
136
137 const bool* array() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700138 return (const bool*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700139 }
140 };
141
142}
143
Stephen Hinesb0934b62013-07-03 17:27:38 -0700144typedef int64_t nsecs_t; // nano-seconds
145
146enum {
147 SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock
148 SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point
149 SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock
150 SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock
Stephen Hinesb0934b62013-07-03 17:27:38 -0700151};
152
153static inline nsecs_t systemTime(int clock)
154{
155#if defined(HAVE_POSIX_CLOCKS)
156 static const clockid_t clocks[] = {
157 CLOCK_REALTIME,
158 CLOCK_MONOTONIC,
159 CLOCK_PROCESS_CPUTIME_ID,
Stephen Hines43cfc0c2013-08-15 10:02:11 -0700160 CLOCK_THREAD_CPUTIME_ID
Stephen Hinesb0934b62013-07-03 17:27:38 -0700161 };
162 struct timespec t;
163 t.tv_sec = t.tv_nsec = 0;
164 clock_gettime(clocks[clock], &t);
165 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
166#else
167 // we don't support the clocks here.
168 struct timeval t;
169 t.tv_sec = t.tv_usec = 0;
170 gettimeofday(&t, NULL);
171 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
172#endif
173}
174
175static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
176{
177 return secs/1000000;
178}
179
180
181#endif // RS_SERVER || RS_COMPATIBILITY_LIB
Jason Samsf9e077a2013-03-20 19:04:19 -0700182
183namespace android {
184namespace renderscript {
185
186#if 1
187#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
188#else
189#define rsAssert(v) while (0)
190#endif
191
192template<typename T>
193T rsMin(T in1, T in2)
194{
195 if (in1 > in2) {
196 return in2;
197 }
198 return in1;
199}
200
201template<typename T>
202T rsMax(T in1, T in2) {
203 if (in1 < in2) {
204 return in2;
205 }
206 return in1;
207}
208
209template<typename T>
210T rsFindHighBit(T val) {
211 uint32_t bit = 0;
212 while (val > 1) {
213 bit++;
214 val>>=1;
215 }
216 return bit;
217}
218
219template<typename T>
220bool rsIsPow2(T val) {
221 return (val & (val-1)) == 0;
222}
223
224template<typename T>
225T rsHigherPow2(T v) {
226 if (rsIsPow2(v)) {
227 return v;
228 }
229 return 1 << (rsFindHighBit(v) + 1);
230}
231
232template<typename T>
233T rsLowerPow2(T v) {
234 if (rsIsPow2(v)) {
235 return v;
236 }
237 return 1 << rsFindHighBit(v);
238}
239
240template<typename T>
241T rsRound(T v, unsigned int r) {
242 // Only valid for rounding up to powers of 2.
243 if ((r & (r - 1)) != 0) {
244 rsAssert(false && "Must be power of 2 for rounding up");
245 return v;
246 }
247 T res = v + (r - 1);
248 if (res < v) {
249 rsAssert(false && "Overflow of rounding operation");
250 return v;
251 }
252 res &= ~(r - 1);
253 return res;
254}
255
256static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
257 uint16_t t = 0;
258 t |= b >> 3;
259 t |= (g >> 2) << 5;
260 t |= (r >> 3) << 11;
261 return t;
262}
263
264static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
265 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
266 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
267 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
268 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
269}
270
271static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
272 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
273 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
274 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
275 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
276 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
277}
278
279}
280}
281
282#endif //ANDROID_RS_OBJECT_BASE_H
283
284