blob: 3b60af58fb4d809860399bbf4959031544a956ef [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"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070022
Jason Sams1aa5a4e2009-06-22 17:15:15 -070023#include <utils/Log.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070024
25#include "rsStream.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070026
Jason Sams8b2c0652009-08-12 17:54:11 -070027#include <utils/String8.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070028#include <utils/Vector.h>
29
Jason Sams326e0dd2009-05-22 14:03:28 -070030#include <stdlib.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070031#include <pthread.h>
Jason Samscfb1d112009-08-05 13:57:03 -070032#include <time.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070033#include <cutils/atomic.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070034
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080035#ifndef ANDROID_RS_SERIALIZE
Jason Samsa5577802009-07-13 12:20:31 -070036#include <EGL/egl.h>
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070037#endif
38
Jason Samsa5577802009-07-13 12:20:31 -070039#include <math.h>
40
Jason Sams992a0b72009-06-23 12:22:47 -070041#include "RenderScript.h"
42
Jason Sams326e0dd2009-05-22 14:03:28 -070043namespace android {
44namespace renderscript {
45
46#if 1
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080047#define rsAssert(v) do {if(!(v)) LOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0)
Jason Sams326e0dd2009-05-22 14:03:28 -070048#else
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080049#define rsAssert(v) while (0)
Jason Sams326e0dd2009-05-22 14:03:28 -070050#endif
51
Jason Samsbe36bf32010-05-11 14:03:58 -070052typedef float rsvF_2 __attribute__ ((vector_size (8)));
53typedef float rsvF_4 __attribute__ ((vector_size (16)));
Jason Samsbe36bf32010-05-11 14:03:58 -070054typedef uint8_t rsvU8_4 __attribute__ ((vector_size (4)));
55
56union float2 {
57 rsvF_2 v;
58 float f[2];
59};
60
61union float4 {
62 rsvF_4 v;
63 float f[4];
64};
65
Jason Samsbe36bf32010-05-11 14:03:58 -070066union uchar4 {
67 rsvU8_4 v;
68 uint8_t f[4];
69 uint32_t packed;
70};
71
Jason Sams326e0dd2009-05-22 14:03:28 -070072template<typename T>
73T rsMin(T in1, T in2)
74{
75 if (in1 > in2) {
76 return in2;
77 }
78 return in1;
79}
80
81template<typename T>
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080082T rsMax(T in1, T in2) {
Jason Sams326e0dd2009-05-22 14:03:28 -070083 if (in1 < in2) {
84 return in2;
85 }
86 return in1;
87}
88
89template<typename T>
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080090T rsFindHighBit(T val) {
Jason Sams326e0dd2009-05-22 14:03:28 -070091 uint32_t bit = 0;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080092 while (val > 1) {
Jason Sams326e0dd2009-05-22 14:03:28 -070093 bit++;
94 val>>=1;
95 }
96 return bit;
97}
98
99template<typename T>
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800100bool rsIsPow2(T val) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700101 return (val & (val-1)) == 0;
102}
103
104template<typename T>
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105T rsHigherPow2(T v) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700106 if (rsIsPow2(v)) {
107 return v;
108 }
109 return 1 << (rsFindHighBit(v) + 1);
110}
111
112template<typename T>
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800113T rsLowerPow2(T v) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700114 if (rsIsPow2(v)) {
115 return v;
116 }
117 return 1 << rsFindHighBit(v);
118}
119
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800120static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700121 uint16_t t = 0;
Jason Sams52a9f522009-06-01 17:45:53 -0700122 t |= b >> 3;
Jason Sams326e0dd2009-05-22 14:03:28 -0700123 t |= (g >> 2) << 5;
Jason Sams52a9f522009-06-01 17:45:53 -0700124 t |= (r >> 3) << 11;
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 return t;
126}
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
Jason Sams565ac362009-06-03 16:04:54 -0700130 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
Jason Sams326e0dd2009-05-22 14:03:28 -0700131 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
132 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
133}
134
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800135static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) {
Jason Sams565ac362009-06-03 16:04:54 -0700136 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
137 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
138 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
139 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
140 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
141}
Jason Sams326e0dd2009-05-22 14:03:28 -0700142
Jason Sams326e0dd2009-05-22 14:03:28 -0700143}
144}
145
146#endif //ANDROID_RS_OBJECT_BASE_H
147
148