rsCppUtils.h revision fa85e9143c6f6a227c67bab5f08ff86194abdf2a
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#define ATRACE_TAG
38#define ATRACE_CALL(...)
39
40#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
127#if 1
128#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
129#else
130#define rsAssert(v) while (0)
131#endif
132
133template<typename T>
134T rsMin(T in1, T in2)
135{
136    if (in1 > in2) {
137        return in2;
138    }
139    return in1;
140}
141
142template<typename T>
143T rsMax(T in1, T in2) {
144    if (in1 < in2) {
145        return in2;
146    }
147    return in1;
148}
149
150template<typename T>
151T rsFindHighBit(T val) {
152    uint32_t bit = 0;
153    while (val > 1) {
154        bit++;
155        val>>=1;
156    }
157    return bit;
158}
159
160template<typename T>
161bool rsIsPow2(T val) {
162    return (val & (val-1)) == 0;
163}
164
165template<typename T>
166T rsHigherPow2(T v) {
167    if (rsIsPow2(v)) {
168        return v;
169    }
170    return 1 << (rsFindHighBit(v) + 1);
171}
172
173template<typename T>
174T rsLowerPow2(T v) {
175    if (rsIsPow2(v)) {
176        return v;
177    }
178    return 1 << rsFindHighBit(v);
179}
180
181template<typename T>
182T rsRound(T v, unsigned int r) {
183    // Only valid for rounding up to powers of 2.
184    if ((r & (r - 1)) != 0) {
185        rsAssert(false && "Must be power of 2 for rounding up");
186        return v;
187    }
188    T res = v + (r - 1);
189    if (res < v) {
190        rsAssert(false && "Overflow of rounding operation");
191        return v;
192    }
193    res &= ~(r - 1);
194    return res;
195}
196
197static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
198    uint16_t t = 0;
199    t |= b >> 3;
200    t |= (g >> 2) << 5;
201    t |= (r >> 3) << 11;
202    return t;
203}
204
205static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
206    uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
207    uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
208    uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
209    return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
210}
211
212static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
213    uint32_t r = (i1 & 0xff) +         (i2 & 0xff) +         (i3 & 0xff) +         (i4 & 0xff);
214    uint32_t g = ((i1 >> 8) & 0xff) +  ((i2 >> 8) & 0xff) +  ((i3 >> 8) & 0xff) +  ((i4 >> 8) & 0xff);
215    uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
216    uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
217    return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
218}
219
220}
221}
222
223#endif //ANDROID_RS_OBJECT_BASE_H
224
225
226