blob: b0eb520206a00bb29af539fb67748813813c3d8d [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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
Jason Samsa1d302e2010-01-26 11:34:09 -080020#define LOG_NDEBUG 0
Jason Sams3bc47d42009-11-12 15:10:25 -080021#define LOG_TAG "RenderScript"
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070022
Jason Sams4b962e52009-06-22 17:15:15 -070023#include <utils/Log.h>
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070024
25#include "rsStream.h"
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070026
Jason Sams43ee06852009-08-12 17:54:11 -070027#include <utils/String8.h>
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070028#include <utils/Vector.h>
29
Jason Samsd19f10d2009-05-22 14:03:28 -070030#include <stdlib.h>
Jason Sams4b962e52009-06-22 17:15:15 -070031#include <pthread.h>
Jason Sams9bee51c2009-08-05 13:57:03 -070032#include <time.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070033
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070034#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams399bfce2009-07-13 12:20:31 -070035#include <EGL/egl.h>
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070036#endif
37
Jason Sams399bfce2009-07-13 12:20:31 -070038#include <math.h>
39
Jason Samsf29ca502009-06-23 12:22:47 -070040#include "RenderScript.h"
41
Jason Samsd19f10d2009-05-22 14:03:28 -070042namespace android {
43namespace renderscript {
44
45#if 1
46#define rsAssert(v) do {if(!(v)) LOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while(0)
47#else
48#define rsAssert(v) while(0)
49#endif
50
Jason Sams4d339932010-05-11 14:03:58 -070051typedef float rsvF_2 __attribute__ ((vector_size (8)));
52typedef float rsvF_4 __attribute__ ((vector_size (16)));
53typedef float rsvF_8 __attribute__ ((vector_size (32)));
54typedef float rsvF_16 __attribute__ ((vector_size (64)));
55typedef uint8_t rsvU8_4 __attribute__ ((vector_size (4)));
56
57union float2 {
58 rsvF_2 v;
59 float f[2];
60};
61
62union float4 {
63 rsvF_4 v;
64 float f[4];
65};
66
67union float8 {
68 rsvF_8 v;
69 float f[8];
70};
71
72union float16 {
73 rsvF_16 v;
74 float f[16];
75};
76
77union uchar4 {
78 rsvU8_4 v;
79 uint8_t f[4];
80 uint32_t packed;
81};
82
Jason Samsd19f10d2009-05-22 14:03:28 -070083template<typename T>
84T rsMin(T in1, T in2)
85{
86 if (in1 > in2) {
87 return in2;
88 }
89 return in1;
90}
91
92template<typename T>
93T rsMax(T in1, T in2)
94{
95 if (in1 < in2) {
96 return in2;
97 }
98 return in1;
99}
100
101template<typename T>
102T rsFindHighBit(T val)
103{
104 uint32_t bit = 0;
105 while(val > 1) {
106 bit++;
107 val>>=1;
108 }
109 return bit;
110}
111
112template<typename T>
113bool rsIsPow2(T val)
114{
115 return (val & (val-1)) == 0;
116}
117
118template<typename T>
119T rsHigherPow2(T v)
120{
121 if (rsIsPow2(v)) {
122 return v;
123 }
124 return 1 << (rsFindHighBit(v) + 1);
125}
126
127template<typename T>
128T rsLowerPow2(T v)
129{
130 if (rsIsPow2(v)) {
131 return v;
132 }
133 return 1 << rsFindHighBit(v);
134}
135
136
137static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b)
138{
139 uint16_t t = 0;
Jason Samsffe9f482009-06-01 17:45:53 -0700140 t |= b >> 3;
Jason Samsd19f10d2009-05-22 14:03:28 -0700141 t |= (g >> 2) << 5;
Jason Samsffe9f482009-06-01 17:45:53 -0700142 t |= (r >> 3) << 11;
Jason Samsd19f10d2009-05-22 14:03:28 -0700143 return t;
144}
145
146static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4)
147{
148 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
Jason Samse2ae85f2009-06-03 16:04:54 -0700149 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
Jason Samsd19f10d2009-05-22 14:03:28 -0700150 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
151 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
152}
153
Jason Samse2ae85f2009-06-03 16:04:54 -0700154static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4)
155{
156 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
157 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
158 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
159 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
160 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
161}
Jason Samsd19f10d2009-05-22 14:03:28 -0700162
163
164
165}
166}
167
168#endif //ANDROID_RS_OBJECT_BASE_H
169
170