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