1f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams/*
2f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * Copyright (C) 2013 The Android Open Source Project
3f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams *
4f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * you may not use this file except in compliance with the License.
6f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * You may obtain a copy of the License at
7f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams *
8f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams *
10f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * Unless required by applicable law or agreed to in writing, software
11f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * See the License for the specific language governing permissions and
14f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams * limitations under the License.
15f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams */
16f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
17f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#ifndef ANDROID_RS_CPP_UTILS_H
18f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#define ANDROID_RS_CPP_UTILS_H
19f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
20b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
21f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <utils/Log.h>
22b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni#include <utils/String8.h>
23b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni#include <utils/Vector.h>
24f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <cutils/atomic.h>
25f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#endif
26f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
27f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <stdint.h>
28f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
29f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <stdlib.h>
30f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <pthread.h>
31f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <time.h>
32f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
33f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <math.h>
34f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
35b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#ifdef RS_COMPATIBILITY_LIB
36b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#include <android/log.h>
37b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#endif
38b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines
39b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#if defined(RS_SERVER) || defined(RS_COMPATIBILITY_LIB)
40f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
41fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray#define ATRACE_TAG
42fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray#define ATRACE_CALL(...)
43fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray
44f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <string>
45f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <vector>
46f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#include <algorithm>
47f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
48b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#define ALOGE(...) \
49b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__);
50b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#define ALOGW(...) \
51b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__);
52b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#define ALOGD(...) \
53b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__);
54b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#define ALOGV(...) \
55b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__);
56b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines
57d614fe9de2415a94bdfc550ee0c03671e378f115Elliott Hughes#if defined(_WIN32)
58d614fe9de2415a94bdfc550ee0c03671e378f115Elliott Hughes#define OS_PATH_SEPARATOR '\\'
59d614fe9de2415a94bdfc550ee0c03671e378f115Elliott Hughes#else
60d614fe9de2415a94bdfc550ee0c03671e378f115Elliott Hughes#define OS_PATH_SEPARATOR '/'
61d614fe9de2415a94bdfc550ee0c03671e378f115Elliott Hughes#endif
62d614fe9de2415a94bdfc550ee0c03671e378f115Elliott Hughes
63b8353c5943f4038fd7f08db3d958390ce9418798Yang Ninamespace android {
64b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
65b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    // server has no Vector or String8 classes; implement on top of STL
66b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    class String8: public std::string {
67b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    public:
68b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    String8(const char *ptr) : std::string(ptr) {
69b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
70b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
71b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    String8(const char *ptr, size_t len) : std::string(ptr, len) {
72b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
73b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
74b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    String8() : std::string() {
75b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
76b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
77b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
78b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        const char* string() const {
79b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            return this->c_str();
80b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
81b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
82b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void setTo(const char* str, ssize_t len) {
83b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->assign(str, len);
84b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
85b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void setTo(const char* str) {
86b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->assign(str);
87b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
88b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        String8 getPathDir(void) const {
89b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            const char* cp;
90b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            const char*const str = this->c_str();
91b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
92b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            cp = strrchr(str, OS_PATH_SEPARATOR);
93b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            if (cp == NULL)
94b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni                return String8("");
95b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            else
96b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni                return String8(str, cp - str);
97b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
98b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    };
99b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
100b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    template <class T> class Vector: public std::vector<T> {
101b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    public:
102b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void push(T obj) {
103b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->push_back(obj);
104b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
105b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void removeAt(uint32_t index) {
106b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->erase(this->begin() + index);
107b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
108b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        ssize_t add(const T& obj) {
109b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->push_back(obj);
110b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            return this->size() - 1;
111b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
112b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void setCapacity(ssize_t capacity) {
113b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->resize(capacity);
114b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
115b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
116b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        T* editArray() {
117b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            return (T*)(this->begin());
118b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
119b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
120b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        const T* array() {
1215abfd9b62b3173a371c42fc92ca8cf978420d694Miao Wang            return this->data();
122b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
123b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
124b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    };
125b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
126b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    template<> class Vector<bool>: public std::vector<char> {
127b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    public:
128b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void push(bool obj) {
129b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->push_back(obj);
130b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
131b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void removeAt(uint32_t index) {
132b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->erase(this->begin() + index);
133b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
134b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        ssize_t add(const bool& obj) {
135b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->push_back(obj);
136b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            return this->size() - 1;
137b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
138b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        void setCapacity(ssize_t capacity) {
139b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni            this->resize(capacity);
140b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
141b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
142b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        bool* editArray() {
143bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang            return (bool*)(&*this->begin());
144b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
145b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
146b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        const bool* array() {
147bc10dff26207bb8c02051b28326bb134a8f28eb3Miao Wang            return (const bool*)(&*this->begin());
148b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        }
149b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    };
150b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
151b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni}
152b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
153b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hinestypedef int64_t nsecs_t;  // nano-seconds
154b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines
155b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hinesenum {
156b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    SYSTEM_TIME_REALTIME = 0,  // system-wide realtime clock
157b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point
158b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    SYSTEM_TIME_PROCESS = 2,   // high-resolution per-process clock
159b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    SYSTEM_TIME_THREAD = 3,    // high-resolution per-thread clock
160b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines};
161b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines
162b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hinesstatic inline nsecs_t systemTime(int clock)
163b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines{
164c04fc654149afa4ec8ba78e23787d3e148adbe2aElliott Hughes#if defined(__linux__)
165b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    static const clockid_t clocks[] = {
166b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines            CLOCK_REALTIME,
167b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines            CLOCK_MONOTONIC,
168b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines            CLOCK_PROCESS_CPUTIME_ID,
16943cfc0cbe6e6e8f585a0ae5f1d9cc2859ab1dda7Stephen Hines            CLOCK_THREAD_CPUTIME_ID
170b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    };
171b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    struct timespec t;
172b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    t.tv_sec = t.tv_nsec = 0;
173b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    clock_gettime(clocks[clock], &t);
174b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec;
175b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#else
176b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    // we don't support the clocks here.
177b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    struct timeval t;
178b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    t.tv_sec = t.tv_usec = 0;
17944bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    gettimeofday(&t, nullptr);
180b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    return nsecs_t(t.tv_sec)*1000000000LL + nsecs_t(t.tv_usec)*1000LL;
181b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#endif
182b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines}
183b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines
184b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hinesstatic inline nsecs_t nanoseconds_to_milliseconds(nsecs_t secs)
185b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines{
186b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines    return secs/1000000;
187b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines}
188b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines
189b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#endif // RS_SERVER || RS_COMPATIBILITY_LIB
190f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
191f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samsnamespace android {
192f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samsnamespace renderscript {
193f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
194f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Samsconst char * rsuCopyString(const char *name);
195f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Samsconst char * rsuCopyString(const char *name, size_t len);
196f313dc32d5ea68a7c48fb4ec6e131ec2fb97ce2dJason Sams
197f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#if 1
198f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
199f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#else
200f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#define rsAssert(v) while (0)
201f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#endif
202f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
203f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samstemplate<typename T>
204f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason SamsT rsMin(T in1, T in2)
205f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams{
206f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    if (in1 > in2) {
207f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        return in2;
208f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    }
209f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return in1;
210f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
211f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
212f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samstemplate<typename T>
213f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason SamsT rsMax(T in1, T in2) {
214f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    if (in1 < in2) {
215f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        return in2;
216f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    }
217f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return in1;
218f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
219f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
220f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samstemplate<typename T>
221f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason SamsT rsFindHighBit(T val) {
222f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t bit = 0;
223f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    while (val > 1) {
224f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        bit++;
225f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        val>>=1;
226f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    }
227f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return bit;
228f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
229f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
230f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samstemplate<typename T>
231f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samsbool rsIsPow2(T val) {
232f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return (val & (val-1)) == 0;
233f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
234f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
235f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samstemplate<typename T>
236f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason SamsT rsHigherPow2(T v) {
237f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    if (rsIsPow2(v)) {
238f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        return v;
239f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    }
240f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return 1 << (rsFindHighBit(v) + 1);
241f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
242f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
243f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samstemplate<typename T>
244f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason SamsT rsLowerPow2(T v) {
245f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    if (rsIsPow2(v)) {
246f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        return v;
247f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    }
248f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return 1 << rsFindHighBit(v);
249f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
250f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
251f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samstemplate<typename T>
252f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason SamsT rsRound(T v, unsigned int r) {
253f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    // Only valid for rounding up to powers of 2.
254f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    if ((r & (r - 1)) != 0) {
255f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        rsAssert(false && "Must be power of 2 for rounding up");
256f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        return v;
257f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    }
258f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    T res = v + (r - 1);
259f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    if (res < v) {
260f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        rsAssert(false && "Overflow of rounding operation");
261f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams        return v;
262f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    }
263f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    res &= ~(r - 1);
264f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return res;
265f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
266f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
267f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samsstatic inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
268f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint16_t t = 0;
269f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    t |= b >> 3;
270f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    t |= (g >> 2) << 5;
271f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    t |= (r >> 3) << 11;
272f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return t;
273f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
274f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
275f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samsstatic inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
276f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
277f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
278f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
279f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
280f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
281f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
282f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Samsstatic inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
283f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t r = (i1 & 0xff) +         (i2 & 0xff) +         (i3 & 0xff) +         (i4 & 0xff);
284f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t g = ((i1 >> 8) & 0xff) +  ((i2 >> 8) & 0xff) +  ((i3 >> 8) & 0xff) +  ((i4 >> 8) & 0xff);
285f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
286f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
287f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams    return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
288f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
289f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
2902abfcc6d129fe3defddef4540aa95cc445c03a7aYang Niconst char* rsuJoinStrings(int n, const char* const* strs);
2912abfcc6d129fe3defddef4540aa95cc445c03a7aYang Ni
2922fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar#ifndef RS_COMPATIBILITY_LIB
2932fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar// Utility to fork/exec a command.
2942fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar//     exe - Command to execute
2952fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar//     nArgs - Number of arguments (excluding the trailing nullptr in args)
2962fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar//     args - Arguments to the command
2972fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainarbool rsuExecuteCommand(const char *exe, int nArgs, const char * const *args);
2982fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar#endif
2992fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar
3002fa8a238dd69afebdeb757adcb1d674043d78e32Pirama Arumuga Nainar
301f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
302f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams}
303f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams
304f9e077a531c6a83e978d55f427b5d8ff6d2c943fJason Sams#endif //ANDROID_RS_OBJECT_BASE_H
305b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
306b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
307