rsUtils.h revision a5577808737a394bdb156fbeb80018d0a4d8438d
1/*
2 * Copyright (C) 2009 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_UTILS_H
18#define ANDROID_RS_UTILS_H
19
20#define LOG_NDEBUG 0
21#define LOG_TAG "rs"
22#include <utils/Log.h>
23#include <utils/Vector.h>
24#include <stdlib.h>
25#include <pthread.h>
26
27#include <EGL/egl.h>
28#include <math.h>
29
30#include "RenderScript.h"
31
32namespace android {
33namespace renderscript {
34
35#if 1
36#define rsAssert(v) do {if(!(v)) LOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while(0)
37#else
38#define rsAssert(v) while(0)
39#endif
40
41template<typename T>
42T rsMin(T in1, T in2)
43{
44    if (in1 > in2) {
45        return in2;
46    }
47    return in1;
48}
49
50template<typename T>
51T rsMax(T in1, T in2)
52{
53    if (in1 < in2) {
54        return in2;
55    }
56    return in1;
57}
58
59template<typename T>
60T rsFindHighBit(T val)
61{
62    uint32_t bit = 0;
63    while(val > 1) {
64        bit++;
65        val>>=1;
66    }
67    return bit;
68}
69
70template<typename T>
71bool rsIsPow2(T val)
72{
73    return (val & (val-1)) == 0;
74}
75
76template<typename T>
77T rsHigherPow2(T v)
78{
79    if (rsIsPow2(v)) {
80        return v;
81    }
82    return 1 << (rsFindHighBit(v) + 1);
83}
84
85template<typename T>
86T rsLowerPow2(T v)
87{
88    if (rsIsPow2(v)) {
89        return v;
90    }
91    return 1 << rsFindHighBit(v);
92}
93
94
95static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b)
96{
97    uint16_t t = 0;
98    t |= b >> 3;
99    t |= (g >> 2) << 5;
100    t |= (r >> 3) << 11;
101    return t;
102}
103
104static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4)
105{
106    uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
107    uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
108    uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
109    return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
110}
111
112static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4)
113{
114    uint32_t r = (i1 & 0xff) +         (i2 & 0xff) +         (i3 & 0xff) +         (i4 & 0xff);
115    uint32_t g = ((i1 >> 8) & 0xff) +  ((i2 >> 8) & 0xff) +  ((i3 >> 8) & 0xff) +  ((i4 >> 8) & 0xff);
116    uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
117    uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
118    return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
119}
120
121
122
123}
124}
125
126#endif //ANDROID_RS_OBJECT_BASE_H
127
128
129