Jason Sams | f9e077a | 2013-03-20 19:04:19 -0700 | [diff] [blame] | 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 | |
Jason Sams | f9e077a | 2013-03-20 19:04:19 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
Jason Sams | f9e077a | 2013-03-20 19:04:19 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <pthread.h> |
| 23 | #include <time.h> |
Jason Sams | f9e077a | 2013-03-20 19:04:19 -0700 | [diff] [blame] | 24 | #include <math.h> |
| 25 | |
Jason Sams | f9e077a | 2013-03-20 19:04:19 -0700 | [diff] [blame] | 26 | #include <string> |
| 27 | #include <vector> |
| 28 | #include <algorithm> |
| 29 | |
Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 30 | #include <android/log.h> |
| 31 | #include <sys/system_properties.h> |
| 32 | |
Mark Salyzyn | 6e49361 | 2017-01-09 09:09:09 -0800 | [diff] [blame] | 33 | #ifndef ALOGE |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 34 | #define ALOGE(...) \ |
| 35 | __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__); |
Mark Salyzyn | 6e49361 | 2017-01-09 09:09:09 -0800 | [diff] [blame] | 36 | #endif |
| 37 | #ifndef ALOGW |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 38 | #define ALOGW(...) \ |
| 39 | __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__); |
Mark Salyzyn | 6e49361 | 2017-01-09 09:09:09 -0800 | [diff] [blame] | 40 | #endif |
| 41 | #ifndef ALOGD |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 42 | #define ALOGD(...) \ |
| 43 | __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__); |
Mark Salyzyn | 6e49361 | 2017-01-09 09:09:09 -0800 | [diff] [blame] | 44 | #endif |
| 45 | #ifndef ALOGV |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 46 | #define ALOGV(...) \ |
| 47 | __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__); |
Mark Salyzyn | 6e49361 | 2017-01-09 09:09:09 -0800 | [diff] [blame] | 48 | #endif |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | d614fe9 | 2015-07-29 14:05:19 -0700 | [diff] [blame] | 50 | #if defined(_WIN32) |
| 51 | #define OS_PATH_SEPARATOR '\\' |
| 52 | #else |
| 53 | #define OS_PATH_SEPARATOR '/' |
| 54 | #endif |
| 55 | |
Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 56 | |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 57 | namespace android { |
Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 58 | namespace renderscript { |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 59 | |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 60 | typedef int64_t nsecs_t; // nano-seconds |
| 61 | |
| 62 | enum { |
| 63 | SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock |
| 64 | SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point |
| 65 | SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock |
| 66 | SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static inline nsecs_t systemTime(int clock) |
| 70 | { |
Elliott Hughes | c04fc65 | 2015-01-09 16:05:47 -0800 | [diff] [blame] | 71 | #if defined(__linux__) |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 72 | static const clockid_t clocks[] = { |
| 73 | CLOCK_REALTIME, |
| 74 | CLOCK_MONOTONIC, |
| 75 | CLOCK_PROCESS_CPUTIME_ID, |
Stephen Hines | 43cfc0c | 2013-08-15 10:02:11 -0700 | [diff] [blame] | 76 | CLOCK_THREAD_CPUTIME_ID |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 77 | }; |
| 78 | struct timespec t; |
| 79 | t.tv_sec = t.tv_nsec = 0; |
| 80 | clock_gettime(clocks[clock], &t); |
| 81 | return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; |
| 82 | #else |
| 83 | // we don't support the clocks here. |
| 84 | struct timeval t; |
| 85 | t.tv_sec = t.tv_usec = 0; |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame] | 86 | gettimeofday(&t, nullptr); |
Stephen Hines | b0934b6 | 2013-07-03 17:27:38 -0700 | [diff] [blame] | 87 | return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL; |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs) |
| 92 | { |
| 93 | return secs/1000000; |
| 94 | } |
| 95 | |
Jason Sams | f9e077a | 2013-03-20 19:04:19 -0700 | [diff] [blame] | 96 | #if 1 |
| 97 | #define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0) |
| 98 | #else |
| 99 | #define rsAssert(v) while (0) |
| 100 | #endif |
| 101 | |
| 102 | template<typename T> |
| 103 | T rsMin(T in1, T in2) |
| 104 | { |
| 105 | if (in1 > in2) { |
| 106 | return in2; |
| 107 | } |
| 108 | return in1; |
| 109 | } |
| 110 | |
| 111 | template<typename T> |
| 112 | T rsMax(T in1, T in2) { |
| 113 | if (in1 < in2) { |
| 114 | return in2; |
| 115 | } |
| 116 | return in1; |
| 117 | } |
| 118 | |
| 119 | template<typename T> |
| 120 | T rsFindHighBit(T val) { |
| 121 | uint32_t bit = 0; |
| 122 | while (val > 1) { |
| 123 | bit++; |
| 124 | val>>=1; |
| 125 | } |
| 126 | return bit; |
| 127 | } |
| 128 | |
| 129 | template<typename T> |
| 130 | bool rsIsPow2(T val) { |
| 131 | return (val & (val-1)) == 0; |
| 132 | } |
| 133 | |
| 134 | template<typename T> |
| 135 | T rsHigherPow2(T v) { |
| 136 | if (rsIsPow2(v)) { |
| 137 | return v; |
| 138 | } |
| 139 | return 1 << (rsFindHighBit(v) + 1); |
| 140 | } |
| 141 | |
| 142 | template<typename T> |
| 143 | T rsLowerPow2(T v) { |
| 144 | if (rsIsPow2(v)) { |
| 145 | return v; |
| 146 | } |
| 147 | return 1 << rsFindHighBit(v); |
| 148 | } |
| 149 | |
| 150 | template<typename T> |
| 151 | T rsRound(T v, unsigned int r) { |
| 152 | // Only valid for rounding up to powers of 2. |
| 153 | if ((r & (r - 1)) != 0) { |
| 154 | rsAssert(false && "Must be power of 2 for rounding up"); |
| 155 | return v; |
| 156 | } |
| 157 | T res = v + (r - 1); |
| 158 | if (res < v) { |
| 159 | rsAssert(false && "Overflow of rounding operation"); |
| 160 | return v; |
| 161 | } |
| 162 | res &= ~(r - 1); |
| 163 | return res; |
| 164 | } |
| 165 | |
| 166 | static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) { |
| 167 | uint16_t t = 0; |
| 168 | t |= b >> 3; |
| 169 | t |= (g >> 2) << 5; |
| 170 | t |= (r >> 3) << 11; |
| 171 | return t; |
| 172 | } |
| 173 | |
| 174 | static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) { |
| 175 | uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f)); |
| 176 | uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f); |
| 177 | uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11)); |
| 178 | return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11); |
| 179 | } |
| 180 | |
| 181 | static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) { |
| 182 | uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff); |
| 183 | uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff); |
| 184 | uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff); |
| 185 | uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff); |
| 186 | return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24); |
| 187 | } |
| 188 | |
Miao Wang | 82e135c | 2017-02-27 23:35:35 -0800 | [diff] [blame] | 189 | const char * rsuCopyString(const char *name); |
| 190 | const char * rsuCopyString(const char *name, size_t len); |
Yang Ni | 2abfcc6 | 2015-02-17 16:05:19 -0800 | [diff] [blame] | 191 | const char* rsuJoinStrings(int n, const char* const* strs); |
| 192 | |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 193 | #ifndef RS_COMPATIBILITY_LIB |
| 194 | // Utility to fork/exec a command. |
| 195 | // exe - Command to execute |
| 196 | // nArgs - Number of arguments (excluding the trailing nullptr in args) |
| 197 | // args - Arguments to the command |
| 198 | bool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args); |
| 199 | #endif |
| 200 | |
Miao Wang | 2a61168 | 2017-02-27 17:36:40 -0800 | [diff] [blame] | 201 | int property_get(const char *key, char *value, const char *default_value); |
Pirama Arumuga Nainar | 2fa8a23 | 2015-03-25 17:21:40 -0700 | [diff] [blame] | 202 | |
Rahul Chaudhry | 7974fc0 | 2017-02-09 12:33:28 -0800 | [diff] [blame] | 203 | } // namespace renderscript |
| 204 | } // namespace android |
Jason Sams | f9e077a | 2013-03-20 19:04:19 -0700 | [diff] [blame] | 205 | |
| 206 | #endif //ANDROID_RS_OBJECT_BASE_H |
Yang Ni | b8353c5 | 2015-02-14 18:00:59 -0800 | [diff] [blame] | 207 | |
| 208 | |