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