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
20 #ifndef RS_SERVER
21 #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
35 #ifdef RS_SERVER
36
37 #include <string>
38 #include <vector>
39 #include <algorithm>
40
41 namespace android {
42
43 // server has no Vector or String8 classes; implement on top of STL
44 class String8: public std::string {
45 public:
String8(const char * ptr)46 String8(const char *ptr) : std::string(ptr) {
47
48 }
String8()49 String8() : std::string() {
50
51 }
52
string()53 const char* string() const {
54 return this->c_str();
55 }
56
setTo(const char * str,ssize_t len)57 void setTo(const char* str, ssize_t len) {
58 this->assign(str, len);
59 }
setTo(const char * str)60 void setTo(const char* str) {
61 this->assign(str);
62 }
63
64 };
65
66 template <class T> class Vector: public std::vector<T> {
67 public:
push(T obj)68 void push(T obj) {
69 this->push_back(obj);
70 }
removeAt(uint32_t index)71 void removeAt(uint32_t index) {
72 this->erase(this->begin() + index);
73 }
add(const T & obj)74 ssize_t add(const T& obj) {
75 this->push_back(obj);
76 return this->size() - 1;
77 }
setCapacity(ssize_t capacity)78 void setCapacity(ssize_t capacity) {
79 this->resize(capacity);
80 }
81
editArray()82 T* editArray() {
83 return this->data();
84 }
85
array()86 const T* array() {
87 return this->data();
88 }
89
90 };
91
92 template<> class Vector<bool>: public std::vector<char> {
93 public:
push(bool obj)94 void push(bool obj) {
95 this->push_back(obj);
96 }
removeAt(uint32_t index)97 void removeAt(uint32_t index) {
98 this->erase(this->begin() + index);
99 }
add(const bool & obj)100 ssize_t add(const bool& obj) {
101 this->push_back(obj);
102 return this->size() - 1;
103 }
setCapacity(ssize_t capacity)104 void setCapacity(ssize_t capacity) {
105 this->resize(capacity);
106 }
107
editArray()108 bool* editArray() {
109 return (bool*)this->data();
110 }
111
array()112 const bool* array() {
113 return (const bool*)this->data();
114 }
115 };
116
117 }
118
119 #endif // RS_SERVER
120
121 namespace android {
122 namespace renderscript {
123
124 #if 1
125 #define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
126 #else
127 #define rsAssert(v) while (0)
128 #endif
129
130 template<typename T>
rsMin(T in1,T in2)131 T rsMin(T in1, T in2)
132 {
133 if (in1 > in2) {
134 return in2;
135 }
136 return in1;
137 }
138
139 template<typename T>
rsMax(T in1,T in2)140 T rsMax(T in1, T in2) {
141 if (in1 < in2) {
142 return in2;
143 }
144 return in1;
145 }
146
147 template<typename T>
rsFindHighBit(T val)148 T rsFindHighBit(T val) {
149 uint32_t bit = 0;
150 while (val > 1) {
151 bit++;
152 val>>=1;
153 }
154 return bit;
155 }
156
157 template<typename T>
rsIsPow2(T val)158 bool rsIsPow2(T val) {
159 return (val & (val-1)) == 0;
160 }
161
162 template<typename T>
rsHigherPow2(T v)163 T rsHigherPow2(T v) {
164 if (rsIsPow2(v)) {
165 return v;
166 }
167 return 1 << (rsFindHighBit(v) + 1);
168 }
169
170 template<typename T>
rsLowerPow2(T v)171 T rsLowerPow2(T v) {
172 if (rsIsPow2(v)) {
173 return v;
174 }
175 return 1 << rsFindHighBit(v);
176 }
177
178 template<typename T>
rsRound(T v,unsigned int r)179 T rsRound(T v, unsigned int r) {
180 // Only valid for rounding up to powers of 2.
181 if ((r & (r - 1)) != 0) {
182 rsAssert(false && "Must be power of 2 for rounding up");
183 return v;
184 }
185 T res = v + (r - 1);
186 if (res < v) {
187 rsAssert(false && "Overflow of rounding operation");
188 return v;
189 }
190 res &= ~(r - 1);
191 return res;
192 }
193
rs888to565(uint32_t r,uint32_t g,uint32_t b)194 static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
195 uint16_t t = 0;
196 t |= b >> 3;
197 t |= (g >> 2) << 5;
198 t |= (r >> 3) << 11;
199 return t;
200 }
201
rsBoxFilter565(uint16_t i1,uint16_t i2,uint16_t i3,uint16_t i4)202 static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
203 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
204 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
205 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
206 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
207 }
208
rsBoxFilter8888(uint32_t i1,uint32_t i2,uint32_t i3,uint32_t i4)209 static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
210 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
211 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
212 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
213 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
214 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
215 }
216
217 }
218 }
219
220 #endif //ANDROID_RS_OBJECT_BASE_H
221
222
223