rsCppUtils.h 5.4 KB

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