Jason Sams | 326e0dd | 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 | 3bce789 | 2010-01-26 11:34:09 -0800 | [diff] [blame] | 20 | #define LOG_NDEBUG 0 |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 21 | #define LOG_TAG "RenderScript" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 22 | |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 24 | |
| 25 | #include "rsStream.h" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 26 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 27 | #include <utils/String8.h> |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 28 | #include <utils/Vector.h> |
| 29 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | #include <stdlib.h> |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 31 | #include <pthread.h> |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 32 | #include <time.h> |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 33 | #include <cutils/atomic.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | |
Jason Sams | a557780 | 2009-07-13 12:20:31 -0700 | [diff] [blame] | 35 | #include <math.h> |
| 36 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace renderscript { |
| 39 | |
| 40 | #if 1 |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 41 | #define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 42 | #else |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 43 | #define rsAssert(v) while (0) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 46 | typedef float rsvF_2 __attribute__ ((vector_size (8))); |
| 47 | typedef float rsvF_4 __attribute__ ((vector_size (16))); |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 48 | typedef uint8_t rsvU8_4 __attribute__ ((vector_size (4))); |
| 49 | |
| 50 | union float2 { |
| 51 | rsvF_2 v; |
| 52 | float f[2]; |
| 53 | }; |
| 54 | |
| 55 | union float4 { |
| 56 | rsvF_4 v; |
| 57 | float f[4]; |
| 58 | }; |
| 59 | |
Jason Sams | be36bf3 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 60 | union uchar4 { |
| 61 | rsvU8_4 v; |
| 62 | uint8_t f[4]; |
| 63 | uint32_t packed; |
| 64 | }; |
| 65 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 66 | template<typename T> |
| 67 | T rsMin(T in1, T in2) |
| 68 | { |
| 69 | if (in1 > in2) { |
| 70 | return in2; |
| 71 | } |
| 72 | return in1; |
| 73 | } |
| 74 | |
| 75 | template<typename T> |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 76 | T rsMax(T in1, T in2) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 77 | if (in1 < in2) { |
| 78 | return in2; |
| 79 | } |
| 80 | return in1; |
| 81 | } |
| 82 | |
| 83 | template<typename T> |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 84 | T rsFindHighBit(T val) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 85 | uint32_t bit = 0; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | while (val > 1) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 87 | bit++; |
| 88 | val>>=1; |
| 89 | } |
| 90 | return bit; |
| 91 | } |
| 92 | |
| 93 | template<typename T> |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 94 | bool rsIsPow2(T val) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 95 | return (val & (val-1)) == 0; |
| 96 | } |
| 97 | |
| 98 | template<typename T> |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 99 | T rsHigherPow2(T v) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 100 | if (rsIsPow2(v)) { |
| 101 | return v; |
| 102 | } |
| 103 | return 1 << (rsFindHighBit(v) + 1); |
| 104 | } |
| 105 | |
| 106 | template<typename T> |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 107 | T rsLowerPow2(T v) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 108 | if (rsIsPow2(v)) { |
| 109 | return v; |
| 110 | } |
| 111 | return 1 << rsFindHighBit(v); |
| 112 | } |
| 113 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 114 | static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 115 | uint16_t t = 0; |
Jason Sams | 52a9f52 | 2009-06-01 17:45:53 -0700 | [diff] [blame] | 116 | t |= b >> 3; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 117 | t |= (g >> 2) << 5; |
Jason Sams | 52a9f52 | 2009-06-01 17:45:53 -0700 | [diff] [blame] | 118 | t |= (r >> 3) << 11; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 119 | return t; |
| 120 | } |
| 121 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 122 | static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 123 | uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f)); |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 124 | uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 125 | uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11)); |
| 126 | return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11); |
| 127 | } |
| 128 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 129 | static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) { |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 130 | uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff); |
| 131 | uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff); |
| 132 | uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff); |
| 133 | uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff); |
| 134 | return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24); |
| 135 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 136 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | #endif //ANDROID_RS_OBJECT_BASE_H |
| 141 | |
| 142 | |