blob: f207f2ff06b23fc116a295f53632c9636493c70b [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
Mark Salyzyn6e493612017-01-09 09:09:09 -080048#ifndef ALOGE
Stephen Hinesb0934b62013-07-03 17:27:38 -070049#define ALOGE(...) \
50 __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);
Mark Salyzyn6e493612017-01-09 09:09:09 -080051#endif
52#ifndef ALOGW
Stephen Hinesb0934b62013-07-03 17:27:38 -070053#define ALOGW(...) \
54 __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__);
Mark Salyzyn6e493612017-01-09 09:09:09 -080055#endif
56#ifndef ALOGD
Stephen Hinesb0934b62013-07-03 17:27:38 -070057#define ALOGD(...) \
58 __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__);
Mark Salyzyn6e493612017-01-09 09:09:09 -080059#endif
60#ifndef ALOGV
Stephen Hinesb0934b62013-07-03 17:27:38 -070061#define ALOGV(...) \
62 __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__);
Mark Salyzyn6e493612017-01-09 09:09:09 -080063#endif
Stephen Hinesb0934b62013-07-03 17:27:38 -070064
Elliott Hughesd614fe92015-07-29 14:05:19 -070065#if defined(_WIN32)
66#define OS_PATH_SEPARATOR '\\'
67#else
68#define OS_PATH_SEPARATOR '/'
69#endif
70
Yang Nib8353c52015-02-14 18:00:59 -080071namespace android {
72
73 // server has no Vector or String8 classes; implement on top of STL
74 class String8: public std::string {
75 public:
Chih-Hung Hsieh10ab8bb2016-07-01 12:20:20 -070076 explicit String8(const char *ptr) : std::string(ptr) {
Yang Nib8353c52015-02-14 18:00:59 -080077
78 }
79 String8(const char *ptr, size_t len) : std::string(ptr, len) {
80
81 }
82 String8() : std::string() {
83
84 }
85
86 const char* string() const {
87 return this->c_str();
88 }
89
90 void setTo(const char* str, ssize_t len) {
91 this->assign(str, len);
92 }
93 void setTo(const char* str) {
94 this->assign(str);
95 }
96 String8 getPathDir(void) const {
97 const char* cp;
98 const char*const str = this->c_str();
99
100 cp = strrchr(str, OS_PATH_SEPARATOR);
101 if (cp == NULL)
102 return String8("");
103 else
104 return String8(str, cp - str);
105 }
106 };
107
108 template <class T> class Vector: public std::vector<T> {
109 public:
110 void push(T obj) {
111 this->push_back(obj);
112 }
113 void removeAt(uint32_t index) {
114 this->erase(this->begin() + index);
115 }
116 ssize_t add(const T& obj) {
117 this->push_back(obj);
118 return this->size() - 1;
119 }
120 void setCapacity(ssize_t capacity) {
121 this->resize(capacity);
122 }
123
124 T* editArray() {
125 return (T*)(this->begin());
126 }
127
128 const T* array() {
Miao Wangabb806d2016-02-19 18:19:32 -0800129 return this->data();
Yang Nib8353c52015-02-14 18:00:59 -0800130 }
131
132 };
133
134 template<> class Vector<bool>: public std::vector<char> {
135 public:
136 void push(bool obj) {
137 this->push_back(obj);
138 }
139 void removeAt(uint32_t index) {
140 this->erase(this->begin() + index);
141 }
142 ssize_t add(const bool& obj) {
143 this->push_back(obj);
144 return this->size() - 1;
145 }
146 void setCapacity(ssize_t capacity) {
147 this->resize(capacity);
148 }
149
150 bool* editArray() {
Miao Wangbc10dff2015-04-03 17:44:55 -0700151 return (bool*)(&*this->begin());
Yang Nib8353c52015-02-14 18:00:59 -0800152 }
153
154 const bool* array() {
Miao Wangbc10dff2015-04-03 17:44:55 -0700155 return (const bool*)(&*this->begin());
Yang Nib8353c52015-02-14 18:00:59 -0800156 }
157 };
158
159}
160
Stephen Hinesb0934b62013-07-03 17:27:38 -0700161typedef int64_t nsecs_t; // nano-seconds
162
163enum {
164 SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock
165 SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point
166 SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock
167 SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock
Stephen Hinesb0934b62013-07-03 17:27:38 -0700168};
169
170static inline nsecs_t systemTime(int clock)
171{
Elliott Hughesc04fc652015-01-09 16:05:47 -0800172#if defined(__linux__)
Stephen Hinesb0934b62013-07-03 17:27:38 -0700173 static const clockid_t clocks[] = {
174 CLOCK_REALTIME,
175 CLOCK_MONOTONIC,
176 CLOCK_PROCESS_CPUTIME_ID,
Stephen Hines43cfc0c2013-08-15 10:02:11 -0700177 CLOCK_THREAD_CPUTIME_ID
Stephen Hinesb0934b62013-07-03 17:27:38 -0700178 };
179 struct timespec t;
180 t.tv_sec = t.tv_nsec = 0;
181 clock_gettime(clocks[clock], &t);
182 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
183#else
184 // we don't support the clocks here.
185 struct timeval t;
186 t.tv_sec = t.tv_usec = 0;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700187 gettimeofday(&t, nullptr);
Stephen Hinesb0934b62013-07-03 17:27:38 -0700188 return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
189#endif
190}
191
192static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
193{
194 return secs/1000000;
195}
196
Stephen Hinesb0934b62013-07-03 17:27:38 -0700197#endif // RS_SERVER || RS_COMPATIBILITY_LIB
Jason Samsf9e077a2013-03-20 19:04:19 -0700198
199namespace android {
200namespace renderscript {
201
Jason Samsf313dc32013-07-09 14:29:39 -0700202const char * rsuCopyString(const char *name);
203const char * rsuCopyString(const char *name, size_t len);
204
Jason Samsf9e077a2013-03-20 19:04:19 -0700205#if 1
206#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
207#else
208#define rsAssert(v) while (0)
209#endif
210
211template<typename T>
212T rsMin(T in1, T in2)
213{
214 if (in1 > in2) {
215 return in2;
216 }
217 return in1;
218}
219
220template<typename T>
221T rsMax(T in1, T in2) {
222 if (in1 < in2) {
223 return in2;
224 }
225 return in1;
226}
227
228template<typename T>
229T rsFindHighBit(T val) {
230 uint32_t bit = 0;
231 while (val > 1) {
232 bit++;
233 val>>=1;
234 }
235 return bit;
236}
237
238template<typename T>
239bool rsIsPow2(T val) {
240 return (val & (val-1)) == 0;
241}
242
243template<typename T>
244T rsHigherPow2(T v) {
245 if (rsIsPow2(v)) {
246 return v;
247 }
248 return 1 << (rsFindHighBit(v) + 1);
249}
250
251template<typename T>
252T rsLowerPow2(T v) {
253 if (rsIsPow2(v)) {
254 return v;
255 }
256 return 1 << rsFindHighBit(v);
257}
258
259template<typename T>
260T rsRound(T v, unsigned int r) {
261 // Only valid for rounding up to powers of 2.
262 if ((r & (r - 1)) != 0) {
263 rsAssert(false && "Must be power of 2 for rounding up");
264 return v;
265 }
266 T res = v + (r - 1);
267 if (res < v) {
268 rsAssert(false && "Overflow of rounding operation");
269 return v;
270 }
271 res &= ~(r - 1);
272 return res;
273}
274
275static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
276 uint16_t t = 0;
277 t |= b >> 3;
278 t |= (g >> 2) << 5;
279 t |= (r >> 3) << 11;
280 return t;
281}
282
283static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
284 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
285 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
286 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
287 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
288}
289
290static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
291 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
292 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
293 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
294 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
295 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
296}
297
Yang Ni2abfcc62015-02-17 16:05:19 -0800298const char* rsuJoinStrings(int n, const char* const* strs);
299
Pirama Arumuga Nainar2fa8a232015-03-25 17:21:40 -0700300#ifndef RS_COMPATIBILITY_LIB
301// Utility to fork/exec a command.
302// exe - Command to execute
303// nArgs - Number of arguments (excluding the trailing nullptr in args)
304// args - Arguments to the command
305bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args);
306#endif
307
308
Jason Samsf9e077a2013-03-20 19:04:19 -0700309}
310}
311
312#endif //ANDROID_RS_OBJECT_BASE_H
Yang Nib8353c52015-02-14 18:00:59 -0800313
314