blob: cc6d6cf68405ee60f1e3387198225b93450f9d5f [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>
Yang Nib8353c52015-02-14 18:00:59 -080022#include <utils/String8.h>
23#include <utils/Vector.h>
Jason Samsf9e077a2013-03-20 19:04:19 -070024#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
Yang Nib8353c52015-02-14 18:00:59 -080057namespace 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 }
65 String8(const char *ptr, size_t len) : std::string(ptr, len) {
66
67 }
68 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 }
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:
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() {
111 return (T*)(this->begin());
112 }
113
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:
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() {
137 return (bool*)(this->begin());
138 }
139
140 const bool* array() {
141 return (const bool*)(this->begin());
142 }
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
Stephen Hinesb0934b62013-07-03 17:27:38 -0700154};
155
156static inline nsecs_t systemTime(int clock)
157{
Elliott Hughesc04fc652015-01-09 16:05:47 -0800158#if defined(__linux__)
Stephen Hinesb0934b62013-07-03 17:27:38 -0700159 static const clockid_t clocks[] = {
160 CLOCK_REALTIME,
161 CLOCK_MONOTONIC,
162 CLOCK_PROCESS_CPUTIME_ID,
Stephen Hines43cfc0c2013-08-15 10:02:11 -0700163 CLOCK_THREAD_CPUTIME_ID
Stephen Hinesb0934b62013-07-03 17:27:38 -0700164 };
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;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700173 gettimeofday(&t, nullptr);
Stephen Hinesb0934b62013-07-03 17:27:38 -0700174 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
175#endif
176}
177
178static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
179{
180 return secs/1000000;
181}
182
Stephen Hinesb0934b62013-07-03 17:27:38 -0700183#endif // RS_SERVER || RS_COMPATIBILITY_LIB
Jason Samsf9e077a2013-03-20 19:04:19 -0700184
185namespace android {
186namespace renderscript {
187
Jason Samsf313dc32013-07-09 14:29:39 -0700188const char * rsuCopyString(const char *name);
189const char * rsuCopyString(const char *name, size_t len);
190
Jason Samsf9e077a2013-03-20 19:04:19 -0700191#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
197template<typename T>
198T rsMin(T in1, T in2)
199{
200 if (in1 > in2) {
201 return in2;
202 }
203 return in1;
204}
205
206template<typename T>
207T rsMax(T in1, T in2) {
208 if (in1 < in2) {
209 return in2;
210 }
211 return in1;
212}
213
214template<typename T>
215T rsFindHighBit(T val) {
216 uint32_t bit = 0;
217 while (val > 1) {
218 bit++;
219 val>>=1;
220 }
221 return bit;
222}
223
224template<typename T>
225bool rsIsPow2(T val) {
226 return (val & (val-1)) == 0;
227}
228
229template<typename T>
230T rsHigherPow2(T v) {
231 if (rsIsPow2(v)) {
232 return v;
233 }
234 return 1 << (rsFindHighBit(v) + 1);
235}
236
237template<typename T>
238T rsLowerPow2(T v) {
239 if (rsIsPow2(v)) {
240 return v;
241 }
242 return 1 << rsFindHighBit(v);
243}
244
245template<typename T>
246T 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
261static 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
269static 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
276static 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
Yang Ni2abfcc62015-02-17 16:05:19 -0800284const char* rsuJoinStrings(int n, const char* const* strs);
285
Jason Samsf9e077a2013-03-20 19:04:19 -0700286}
287}
288
289#endif //ANDROID_RS_OBJECT_BASE_H
Yang Nib8353c52015-02-14 18:00:59 -0800290
291