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#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
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_COMPATIBILITY_LIB
36#include <android/log.h>
37#endif
38
39#if defined(RS_SERVER) || defined(RS_COMPATIBILITY_LIB)
40
41#define ATRACE_TAG
42#define ATRACE_CALL(...)
43
44#include <string>
45#include <vector>
46#include <algorithm>
47
48#define ALOGE(...) \
49    __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);
50#define ALOGW(...) \
51    __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__);
52#define ALOGD(...) \
53    __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__);
54#define ALOGV(...) \
55    __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__);
56
57namespace android {
58
59    // server has no Vector or String8 classes; implement on top of STL
60    class String8: public std::string {
61    public:
62    String8(const char *ptr) : std::string(ptr) {
63
64        }
65    String8(const char *ptr, size_t len) : std::string(ptr, len) {
66
67        }
68    String8() : std::string() {
69
70        }
71
72        const char* string() const {
73            return this->c_str();
74        }
75
76        void setTo(const char* str, ssize_t len) {
77            this->assign(str, len);
78        }
79        void setTo(const char* str) {
80            this->assign(str);
81        }
82        String8 getPathDir(void) const {
83            const char* cp;
84            const char*const str = this->c_str();
85
86            cp = strrchr(str, OS_PATH_SEPARATOR);
87            if (cp == NULL)
88                return String8("");
89            else
90                return String8(str, cp - str);
91        }
92    };
93
94    template <class T> class Vector: public std::vector<T> {
95    public:
96        void push(T obj) {
97            this->push_back(obj);
98        }
99        void removeAt(uint32_t index) {
100            this->erase(this->begin() + index);
101        }
102        ssize_t add(const T& obj) {
103            this->push_back(obj);
104            return this->size() - 1;
105        }
106        void setCapacity(ssize_t capacity) {
107            this->resize(capacity);
108        }
109
110        T* editArray() {
111            return (T*)(this->begin());
112        }
113
114        const T* array() {
115            return (const T*)(this->begin());
116        }
117
118    };
119
120    template<> class Vector<bool>: public std::vector<char> {
121    public:
122        void push(bool obj) {
123            this->push_back(obj);
124        }
125        void removeAt(uint32_t index) {
126            this->erase(this->begin() + index);
127        }
128        ssize_t add(const bool& obj) {
129            this->push_back(obj);
130            return this->size() - 1;
131        }
132        void setCapacity(ssize_t capacity) {
133            this->resize(capacity);
134        }
135
136        bool* editArray() {
137            return (bool*)(this->begin());
138        }
139
140        const bool* array() {
141            return (const bool*)(this->begin());
142        }
143    };
144
145}
146
147typedef int64_t nsecs_t;  // nano-seconds
148
149enum {
150    SYSTEM_TIME_REALTIME = 0,  // system-wide realtime clock
151    SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point
152    SYSTEM_TIME_PROCESS = 2,   // high-resolution per-process clock
153    SYSTEM_TIME_THREAD = 3,    // high-resolution per-thread clock
154};
155
156static inline nsecs_t systemTime(int clock)
157{
158#if defined(HAVE_POSIX_CLOCKS)
159    static const clockid_t clocks[] = {
160            CLOCK_REALTIME,
161            CLOCK_MONOTONIC,
162            CLOCK_PROCESS_CPUTIME_ID,
163            CLOCK_THREAD_CPUTIME_ID
164    };
165    struct timespec t;
166    t.tv_sec = t.tv_nsec = 0;
167    clock_gettime(clocks[clock], &t);
168    return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
169#else
170    // we don't support the clocks here.
171    struct timeval t;
172    t.tv_sec = t.tv_usec = 0;
173    gettimeofday(&t, NULL);
174    return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
175#endif
176}
177
178static inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
179{
180    return secs/1000000;
181}
182
183
184#endif // RS_SERVER || RS_COMPATIBILITY_LIB
185
186namespace android {
187namespace renderscript {
188
189const char * rsuCopyString(const char *name);
190const char * rsuCopyString(const char *name, size_t len);
191
192#if 1
193#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
194#else
195#define rsAssert(v) while (0)
196#endif
197
198template<typename T>
199T rsMin(T in1, T in2)
200{
201    if (in1 > in2) {
202        return in2;
203    }
204    return in1;
205}
206
207template<typename T>
208T rsMax(T in1, T in2) {
209    if (in1 < in2) {
210        return in2;
211    }
212    return in1;
213}
214
215template<typename T>
216T rsFindHighBit(T val) {
217    uint32_t bit = 0;
218    while (val > 1) {
219        bit++;
220        val>>=1;
221    }
222    return bit;
223}
224
225template<typename T>
226bool rsIsPow2(T val) {
227    return (val & (val-1)) == 0;
228}
229
230template<typename T>
231T rsHigherPow2(T v) {
232    if (rsIsPow2(v)) {
233        return v;
234    }
235    return 1 << (rsFindHighBit(v) + 1);
236}
237
238template<typename T>
239T rsLowerPow2(T v) {
240    if (rsIsPow2(v)) {
241        return v;
242    }
243    return 1 << rsFindHighBit(v);
244}
245
246template<typename T>
247T rsRound(T v, unsigned int r) {
248    // Only valid for rounding up to powers of 2.
249    if ((r & (r - 1)) != 0) {
250        rsAssert(false && "Must be power of 2 for rounding up");
251        return v;
252    }
253    T res = v + (r - 1);
254    if (res < v) {
255        rsAssert(false && "Overflow of rounding operation");
256        return v;
257    }
258    res &= ~(r - 1);
259    return res;
260}
261
262static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
263    uint16_t t = 0;
264    t |= b >> 3;
265    t |= (g >> 2) << 5;
266    t |= (r >> 3) << 11;
267    return t;
268}
269
270static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
271    uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
272    uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
273    uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
274    return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
275}
276
277static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
278    uint32_t r = (i1 & 0xff) +         (i2 & 0xff) +         (i3 & 0xff) +         (i4 & 0xff);
279    uint32_t g = ((i1 >> 8) & 0xff) +  ((i2 >> 8) & 0xff) +  ((i3 >> 8) & 0xff) +  ((i4 >> 8) & 0xff);
280    uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
281    uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
282    return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
283}
284
285}
286}
287
288#endif //ANDROID_RS_OBJECT_BASE_H
289
290
291