blob: 36b52aa9d0ccf34e7953b1569f5c945078dc433a [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
Tim Murrayfa85e912013-05-23 13:19:36 -070041#define ATRACE_TAG
42#define ATRACE_CALL(...)
43
Jason Samsf9e077a2013-03-20 19:04:19 -070044#include <string>
45#include <vector>
46#include <algorithm>
47
Stephen Hinesb0934b62013-07-03 17:27:38 -070048#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
Jason Samsf9e077a2013-03-20 19:04:19 -070057namespace android {
58
59 // server has no Vector or String8 classes; implement on top of STL
60 class String8: public std::string {
61 public:
62 String8(const char *ptr) : std::string(ptr) {
63
64 }
Stephen Hinesb0934b62013-07-03 17:27:38 -070065 String8(const char *ptr, size_t len) : std::string(ptr, len) {
66
67 }
Jason Samsf9e077a2013-03-20 19:04:19 -070068 String8() : std::string() {
69
70 }
71
72 const char* string() const {
73 return this->c_str();
74 }
75
76 void setTo(const char* str, ssize_t len) {
77 this->assign(str, len);
78 }
79 void setTo(const char* str) {
80 this->assign(str);
81 }
Stephen Hinesb0934b62013-07-03 17:27:38 -070082 String8 getPathDir(void) const {
83 const char* cp;
84 const char*const str = this->c_str();
Jason Samsf9e077a2013-03-20 19:04:19 -070085
Stephen Hinesb0934b62013-07-03 17:27:38 -070086 cp = strrchr(str, OS_PATH_SEPARATOR);
87 if (cp == NULL)
88 return String8("");
89 else
90 return String8(str, cp - str);
91 }
Jason Samsf9e077a2013-03-20 19:04:19 -070092 };
93
94 template <class T> class Vector: public std::vector<T> {
95 public:
96 void push(T obj) {
97 this->push_back(obj);
98 }
99 void removeAt(uint32_t index) {
100 this->erase(this->begin() + index);
101 }
102 ssize_t add(const T& obj) {
103 this->push_back(obj);
104 return this->size() - 1;
105 }
106 void setCapacity(ssize_t capacity) {
107 this->resize(capacity);
108 }
109
110 T* editArray() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700111 return (T*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700112 }
113
114 const T* array() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700115 return (const T*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700116 }
117
118 };
119
120 template<> class Vector<bool>: public std::vector<char> {
121 public:
122 void push(bool obj) {
123 this->push_back(obj);
124 }
125 void removeAt(uint32_t index) {
126 this->erase(this->begin() + index);
127 }
128 ssize_t add(const bool& obj) {
129 this->push_back(obj);
130 return this->size() - 1;
131 }
132 void setCapacity(ssize_t capacity) {
133 this->resize(capacity);
134 }
135
136 bool* editArray() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700137 return (bool*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700138 }
139
140 const bool* array() {
Stephen Hinesb0934b62013-07-03 17:27:38 -0700141 return (const bool*)(this->begin());
Jason Samsf9e077a2013-03-20 19:04:19 -0700142 }
143 };
144
145}
146
Stephen Hinesb0934b62013-07-03 17:27:38 -0700147typedef int64_t nsecs_t; // nano-seconds
148
149enum {
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 SYSTEM_TIME_BOOTTIME = 4 // same as SYSTEM_TIME_MONOTONIC, but including CPU suspend time
155};
156
157static inline nsecs_t systemTime(int clock)
158{
159#if defined(HAVE_POSIX_CLOCKS)
160 static const clockid_t clocks[] = {
161 CLOCK_REALTIME,
162 CLOCK_MONOTONIC,
163 CLOCK_PROCESS_CPUTIME_ID,
164 CLOCK_THREAD_CPUTIME_ID,
165 CLOCK_BOOTTIME
166 };
167 struct timespec t;
168 t.tv_sec = t.tv_nsec = 0;
169 clock_gettime(clocks[clock], &t);
170 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
171#else
172 // we don't support the clocks here.
173 struct timeval t;
174 t.tv_sec = t.tv_usec = 0;
175 gettimeofday(&t, NULL);
176 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
177#endif
178}
179
180static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
181{
182 return secs/1000000;
183}
184
185
186#endif // RS_SERVER || RS_COMPATIBILITY_LIB
Jason Samsf9e077a2013-03-20 19:04:19 -0700187
188namespace android {
189namespace renderscript {
190
Jason Samsf313dc32013-07-09 14:29:39 -0700191const char * rsuCopyString(const char *name);
192const char * rsuCopyString(const char *name, size_t len);
193
Jason Samsf9e077a2013-03-20 19:04:19 -0700194#if 1
195#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
196#else
197#define rsAssert(v) while (0)
198#endif
199
200template<typename T>
201T rsMin(T in1, T in2)
202{
203 if (in1 > in2) {
204 return in2;
205 }
206 return in1;
207}
208
209template<typename T>
210T rsMax(T in1, T in2) {
211 if (in1 < in2) {
212 return in2;
213 }
214 return in1;
215}
216
217template<typename T>
218T rsFindHighBit(T val) {
219 uint32_t bit = 0;
220 while (val > 1) {
221 bit++;
222 val>>=1;
223 }
224 return bit;
225}
226
227template<typename T>
228bool rsIsPow2(T val) {
229 return (val & (val-1)) == 0;
230}
231
232template<typename T>
233T rsHigherPow2(T v) {
234 if (rsIsPow2(v)) {
235 return v;
236 }
237 return 1 << (rsFindHighBit(v) + 1);
238}
239
240template<typename T>
241T rsLowerPow2(T v) {
242 if (rsIsPow2(v)) {
243 return v;
244 }
245 return 1 << rsFindHighBit(v);
246}
247
248template<typename T>
249T rsRound(T v, unsigned int r) {
250 // Only valid for rounding up to powers of 2.
251 if ((r & (r - 1)) != 0) {
252 rsAssert(false && "Must be power of 2 for rounding up");
253 return v;
254 }
255 T res = v + (r - 1);
256 if (res < v) {
257 rsAssert(false && "Overflow of rounding operation");
258 return v;
259 }
260 res &= ~(r - 1);
261 return res;
262}
263
264static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
265 uint16_t t = 0;
266 t |= b >> 3;
267 t |= (g >> 2) << 5;
268 t |= (r >> 3) << 11;
269 return t;
270}
271
272static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
273 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
274 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
275 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
276 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
277}
278
279static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
280 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
281 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
282 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
283 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
284 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
285}
286
287}
288}
289
290#endif //ANDROID_RS_OBJECT_BASE_H
291
292