Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 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 | |
Jason Sams | a1d302e | 2010-01-26 11:34:09 -0800 | [diff] [blame] | 20 | #define LOG_NDEBUG 0 |
Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 21 | #define LOG_TAG "RenderScript" |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 22 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 24 | |
| 25 | #include "rsStream.h" |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 26 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 27 | #include <utils/String8.h> |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 28 | #include <utils/Vector.h> |
| 29 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | #include <stdlib.h> |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 31 | #include <pthread.h> |
Jason Sams | 9bee51c | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 32 | #include <time.h> |
Jason Sams | 8e6c17f | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 33 | #include <cutils/atomic.h> |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 35 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | 399bfce | 2009-07-13 12:20:31 -0700 | [diff] [blame] | 36 | #include <EGL/egl.h> |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
Jason Sams | 399bfce | 2009-07-13 12:20:31 -0700 | [diff] [blame] | 39 | #include <math.h> |
| 40 | |
Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 41 | #include "RenderScript.h" |
| 42 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace 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 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 52 | typedef float rsvF_2 __attribute__ ((vector_size (8))); |
| 53 | typedef float rsvF_4 __attribute__ ((vector_size (16))); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 54 | typedef uint8_t rsvU8_4 __attribute__ ((vector_size (4))); |
| 55 | |
| 56 | union float2 { |
| 57 | rsvF_2 v; |
| 58 | float f[2]; |
| 59 | }; |
| 60 | |
| 61 | union float4 { |
| 62 | rsvF_4 v; |
| 63 | float f[4]; |
| 64 | }; |
| 65 | |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 66 | union uchar4 { |
| 67 | rsvU8_4 v; |
| 68 | uint8_t f[4]; |
| 69 | uint32_t packed; |
| 70 | }; |
| 71 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 72 | template<typename T> |
| 73 | T rsMin(T in1, T in2) |
| 74 | { |
| 75 | if (in1 > in2) { |
| 76 | return in2; |
| 77 | } |
| 78 | return in1; |
| 79 | } |
| 80 | |
| 81 | template<typename T> |
| 82 | T rsMax(T in1, T in2) |
| 83 | { |
| 84 | if (in1 < in2) { |
| 85 | return in2; |
| 86 | } |
| 87 | return in1; |
| 88 | } |
| 89 | |
| 90 | template<typename T> |
| 91 | T rsFindHighBit(T val) |
| 92 | { |
| 93 | uint32_t bit = 0; |
| 94 | while(val > 1) { |
| 95 | bit++; |
| 96 | val>>=1; |
| 97 | } |
| 98 | return bit; |
| 99 | } |
| 100 | |
| 101 | template<typename T> |
| 102 | bool rsIsPow2(T val) |
| 103 | { |
| 104 | return (val & (val-1)) == 0; |
| 105 | } |
| 106 | |
| 107 | template<typename T> |
| 108 | T rsHigherPow2(T v) |
| 109 | { |
| 110 | if (rsIsPow2(v)) { |
| 111 | return v; |
| 112 | } |
| 113 | return 1 << (rsFindHighBit(v) + 1); |
| 114 | } |
| 115 | |
| 116 | template<typename T> |
| 117 | T rsLowerPow2(T v) |
| 118 | { |
| 119 | if (rsIsPow2(v)) { |
| 120 | return v; |
| 121 | } |
| 122 | return 1 << rsFindHighBit(v); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) |
| 127 | { |
| 128 | uint16_t t = 0; |
Jason Sams | ffe9f48 | 2009-06-01 17:45:53 -0700 | [diff] [blame] | 129 | t |= b >> 3; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 130 | t |= (g >> 2) << 5; |
Jason Sams | ffe9f48 | 2009-06-01 17:45:53 -0700 | [diff] [blame] | 131 | t |= (r >> 3) << 11; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 132 | return t; |
| 133 | } |
| 134 | |
| 135 | static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) |
| 136 | { |
| 137 | uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f)); |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 138 | uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 139 | uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11)); |
| 140 | return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11); |
| 141 | } |
| 142 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 143 | static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) |
| 144 | { |
| 145 | uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff); |
| 146 | uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff); |
| 147 | uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff); |
| 148 | uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff); |
| 149 | return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24); |
| 150 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 151 | |
| 152 | |
| 153 | |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #endif //ANDROID_RS_OBJECT_BASE_H |
| 158 | |
| 159 | |