blob: adddf492bb1ea6cf0f2bc5ed0334d8f42d4183e8 [file] [log] [blame]
Jason Sams326e0dd2009-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 Sams3bce7892010-01-26 11:34:09 -080020#define LOG_NDEBUG 0
Jason Sams613cad12009-11-12 15:10:25 -080021#define LOG_TAG "RenderScript"
Jason Sams1aa5a4e2009-06-22 17:15:15 -070022#include <utils/Log.h>
23#include <utils/Vector.h>
Jason Sams8b2c0652009-08-12 17:54:11 -070024#include <utils/KeyedVector.h>
25#include <utils/String8.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070026#include <stdlib.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070027#include <pthread.h>
Jason Samscfb1d112009-08-05 13:57:03 -070028#include <time.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070029
Jason Samsa5577802009-07-13 12:20:31 -070030#include <EGL/egl.h>
31#include <math.h>
32
Jason Sams992a0b72009-06-23 12:22:47 -070033#include "RenderScript.h"
34
Jason Sams326e0dd2009-05-22 14:03:28 -070035namespace android {
36namespace renderscript {
37
38#if 1
39#define rsAssert(v) do {if(!(v)) LOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while(0)
40#else
41#define rsAssert(v) while(0)
42#endif
43
Jason Samsbe36bf32010-05-11 14:03:58 -070044typedef float rsvF_2 __attribute__ ((vector_size (8)));
45typedef float rsvF_4 __attribute__ ((vector_size (16)));
46typedef float rsvF_8 __attribute__ ((vector_size (32)));
47typedef float rsvF_16 __attribute__ ((vector_size (64)));
48typedef uint8_t rsvU8_4 __attribute__ ((vector_size (4)));
49
50union float2 {
51 rsvF_2 v;
52 float f[2];
53};
54
55union float4 {
56 rsvF_4 v;
57 float f[4];
58};
59
60union float8 {
61 rsvF_8 v;
62 float f[8];
63};
64
65union float16 {
66 rsvF_16 v;
67 float f[16];
68};
69
70union uchar4 {
71 rsvU8_4 v;
72 uint8_t f[4];
73 uint32_t packed;
74};
75
Jason Sams326e0dd2009-05-22 14:03:28 -070076template<typename T>
77T rsMin(T in1, T in2)
78{
79 if (in1 > in2) {
80 return in2;
81 }
82 return in1;
83}
84
85template<typename T>
86T rsMax(T in1, T in2)
87{
88 if (in1 < in2) {
89 return in2;
90 }
91 return in1;
92}
93
94template<typename T>
95T rsFindHighBit(T val)
96{
97 uint32_t bit = 0;
98 while(val > 1) {
99 bit++;
100 val>>=1;
101 }
102 return bit;
103}
104
105template<typename T>
106bool rsIsPow2(T val)
107{
108 return (val & (val-1)) == 0;
109}
110
111template<typename T>
112T rsHigherPow2(T v)
113{
114 if (rsIsPow2(v)) {
115 return v;
116 }
117 return 1 << (rsFindHighBit(v) + 1);
118}
119
120template<typename T>
121T rsLowerPow2(T v)
122{
123 if (rsIsPow2(v)) {
124 return v;
125 }
126 return 1 << rsFindHighBit(v);
127}
128
129
130static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b)
131{
132 uint16_t t = 0;
Jason Sams52a9f522009-06-01 17:45:53 -0700133 t |= b >> 3;
Jason Sams326e0dd2009-05-22 14:03:28 -0700134 t |= (g >> 2) << 5;
Jason Sams52a9f522009-06-01 17:45:53 -0700135 t |= (r >> 3) << 11;
Jason Sams326e0dd2009-05-22 14:03:28 -0700136 return t;
137}
138
139static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4)
140{
141 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
Jason Sams565ac362009-06-03 16:04:54 -0700142 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
Jason Sams326e0dd2009-05-22 14:03:28 -0700143 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
144 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
145}
146
Jason Sams565ac362009-06-03 16:04:54 -0700147static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4)
148{
149 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
150 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
151 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
152 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
153 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
154}
Jason Sams326e0dd2009-05-22 14:03:28 -0700155
156
157
158}
159}
160
161#endif //ANDROID_RS_OBJECT_BASE_H
162
163