blob: 3c6da68f6d300835ef33dfe385db01c91868b2cb [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 Sams3bc47d42009-11-12 15:10:25 -080020#define LOG_TAG "RenderScript"
Jason Sams4b962e52009-06-22 17:15:15 -070021#include <utils/Log.h>
22#include <utils/Vector.h>
Jason Sams43ee06852009-08-12 17:54:11 -070023#include <utils/KeyedVector.h>
24#include <utils/String8.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070025#include <stdlib.h>
Jason Sams4b962e52009-06-22 17:15:15 -070026#include <pthread.h>
Jason Sams9bee51c2009-08-05 13:57:03 -070027#include <time.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070028
Jason Sams399bfce2009-07-13 12:20:31 -070029#include <EGL/egl.h>
30#include <math.h>
31
Jason Samsf29ca502009-06-23 12:22:47 -070032#include "RenderScript.h"
33
Jason Samsd19f10d2009-05-22 14:03:28 -070034namespace android {
35namespace renderscript {
36
37#if 1
38#define rsAssert(v) do {if(!(v)) LOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while(0)
39#else
40#define rsAssert(v) while(0)
41#endif
42
43template<typename T>
44T rsMin(T in1, T in2)
45{
46 if (in1 > in2) {
47 return in2;
48 }
49 return in1;
50}
51
52template<typename T>
53T rsMax(T in1, T in2)
54{
55 if (in1 < in2) {
56 return in2;
57 }
58 return in1;
59}
60
61template<typename T>
62T rsFindHighBit(T val)
63{
64 uint32_t bit = 0;
65 while(val > 1) {
66 bit++;
67 val>>=1;
68 }
69 return bit;
70}
71
72template<typename T>
73bool rsIsPow2(T val)
74{
75 return (val & (val-1)) == 0;
76}
77
78template<typename T>
79T rsHigherPow2(T v)
80{
81 if (rsIsPow2(v)) {
82 return v;
83 }
84 return 1 << (rsFindHighBit(v) + 1);
85}
86
87template<typename T>
88T rsLowerPow2(T v)
89{
90 if (rsIsPow2(v)) {
91 return v;
92 }
93 return 1 << rsFindHighBit(v);
94}
95
96
97static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b)
98{
99 uint16_t t = 0;
Jason Samsffe9f482009-06-01 17:45:53 -0700100 t |= b >> 3;
Jason Samsd19f10d2009-05-22 14:03:28 -0700101 t |= (g >> 2) << 5;
Jason Samsffe9f482009-06-01 17:45:53 -0700102 t |= (r >> 3) << 11;
Jason Samsd19f10d2009-05-22 14:03:28 -0700103 return t;
104}
105
106static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4)
107{
108 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f));
Jason Samse2ae85f2009-06-03 16:04:54 -0700109 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f);
Jason Samsd19f10d2009-05-22 14:03:28 -0700110 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11));
111 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11);
112}
113
Jason Samse2ae85f2009-06-03 16:04:54 -0700114static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4)
115{
116 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff);
117 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff);
118 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff);
119 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff);
120 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24);
121}
Jason Samsd19f10d2009-05-22 14:03:28 -0700122
123
124
125}
126}
127
128#endif //ANDROID_RS_OBJECT_BASE_H
129
130