rsUtils.h revision fb6b614bcea88a587a7ea4530be45ff0ffa0210e
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 "RenderScript"
22
23#include <utils/Log.h>
24
25#include "rsStream.h"
26#include "rsFileA3DDecls.h"
27
28#include <utils/String8.h>
29#include <utils/Vector.h>
30
31#include <stdlib.h>
32#include <pthread.h>
33#include <time.h>
34
35#ifndef ANDROID_RS_BUILD_FOR_HOST
36#include <EGL/egl.h>
37#endif
38
39#include <math.h>
40
41#include "RenderScript.h"
42
43namespace android {
44namespace renderscript {
45
46#if 1
47#define rsAssert(v) do {if(!(v)) LOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while(0)
48#else
49#define rsAssert(v) while(0)
50#endif
51
52typedef float rsvF_2 __attribute__ ((vector_size (8)));
53typedef float rsvF_4 __attribute__ ((vector_size (16)));
54typedef float rsvF_8 __attribute__ ((vector_size (32)));
55typedef float rsvF_16 __attribute__ ((vector_size (64)));
56typedef uint8_t rsvU8_4 __attribute__ ((vector_size (4)));
57
58union float2 {
59    rsvF_2 v;
60    float f[2];
61};
62
63union float4 {
64    rsvF_4 v;
65    float f[4];
66};
67
68union float8 {
69    rsvF_8 v;
70    float f[8];
71};
72
73union float16 {
74    rsvF_16 v;
75    float f[16];
76};
77
78union uchar4 {
79    rsvU8_4 v;
80    uint8_t f[4];
81    uint32_t packed;
82};
83
84template<typename T>
85T rsMin(T in1, T in2)
86{
87    if (in1 > in2) {
88        return in2;
89    }
90    return in1;
91}
92
93template<typename T>
94T rsMax(T in1, T in2)
95{
96    if (in1 < in2) {
97        return in2;
98    }
99    return in1;
100}
101
102template<typename T>
103T rsFindHighBit(T val)
104{
105    uint32_t bit = 0;
106    while(val > 1) {
107        bit++;
108        val>>=1;
109    }
110    return bit;
111}
112
113template<typename T>
114bool rsIsPow2(T val)
115{
116    return (val & (val-1)) == 0;
117}
118
119template<typename T>
120T rsHigherPow2(T v)
121{
122    if (rsIsPow2(v)) {
123        return v;
124    }
125    return 1 << (rsFindHighBit(v) + 1);
126}
127
128template<typename T>
129T rsLowerPow2(T v)
130{
131    if (rsIsPow2(v)) {
132        return v;
133    }
134    return 1 << rsFindHighBit(v);
135}
136
137
138static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b)
139{
140    uint16_t t = 0;
141    t |= b >> 3;
142    t |= (g >> 2) << 5;
143    t |= (r >> 3) << 11;
144    return t;
145}
146
147static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4)
148{
149    uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
150    uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
151    uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
152    return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
153}
154
155static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4)
156{
157    uint32_t r = (i1 & 0xff) +         (i2 & 0xff) +         (i3 & 0xff) +         (i4 & 0xff);
158    uint32_t g = ((i1 >> 8) & 0xff) +  ((i2 >> 8) & 0xff) +  ((i3 >> 8) & 0xff) +  ((i4 >> 8) & 0xff);
159    uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
160    uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
161    return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
162}
163
164
165
166}
167}
168
169#endif //ANDROID_RS_OBJECT_BASE_H
170
171
172