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