rsUtils.h revision 94999c3c7e3dcb6b6c0a9fe5785a0d3216357b0e
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 20#define LOG_NDEBUG 0 21#define LOG_TAG "RenderScript" 22 23#include <utils/Log.h> 24 25#include "rsStream.h" 26 27#include <utils/String8.h> 28#include <utils/Vector.h> 29 30#include <stdlib.h> 31#include <pthread.h> 32#include <time.h> 33#include <cutils/atomic.h> 34 35#include <math.h> 36 37namespace android { 38namespace renderscript { 39 40#if 1 41#define rsAssert(v) do {if(!(v)) ALOGE("rsAssert failed: %s, in %s at %i", #v, __FILE__, __LINE__);} while (0) 42#else 43#define rsAssert(v) while (0) 44#endif 45 46template<typename T> 47T rsMin(T in1, T in2) 48{ 49 if (in1 > in2) { 50 return in2; 51 } 52 return in1; 53} 54 55template<typename T> 56T rsMax(T in1, T in2) { 57 if (in1 < in2) { 58 return in2; 59 } 60 return in1; 61} 62 63template<typename T> 64T rsFindHighBit(T val) { 65 uint32_t bit = 0; 66 while (val > 1) { 67 bit++; 68 val>>=1; 69 } 70 return bit; 71} 72 73template<typename T> 74bool rsIsPow2(T val) { 75 return (val & (val-1)) == 0; 76} 77 78template<typename T> 79T rsHigherPow2(T v) { 80 if (rsIsPow2(v)) { 81 return v; 82 } 83 return 1 << (rsFindHighBit(v) + 1); 84} 85 86template<typename T> 87T rsLowerPow2(T v) { 88 if (rsIsPow2(v)) { 89 return v; 90 } 91 return 1 << rsFindHighBit(v); 92} 93 94template<typename T> 95T rsRound(T v, unsigned int r) { 96 // Only valid for rounding up to powers of 2. 97 if ((r & (r - 1)) != 0) { 98 rsAssert(false && "Must be power of 2 for rounding up"); 99 return v; 100 } 101 T res = v + (r - 1); 102 if (res < v) { 103 rsAssert(false && "Overflow of rounding operation"); 104 return v; 105 } 106 res &= ~(r - 1); 107 return res; 108} 109 110static inline uint16_t rs888to565(uint32_t r, uint32_t g, uint32_t b) { 111 uint16_t t = 0; 112 t |= b >> 3; 113 t |= (g >> 2) << 5; 114 t |= (r >> 3) << 11; 115 return t; 116} 117 118static inline uint16_t rsBoxFilter565(uint16_t i1, uint16_t i2, uint16_t i3, uint16_t i4) { 119 uint32_t r = ((i1 & 0x1f) + (i2 & 0x1f) + (i3 & 0x1f) + (i4 & 0x1f)); 120 uint32_t g = ((i1 >> 5) & 0x3f) + ((i2 >> 5) & 0x3f) + ((i3 >> 5) & 0x3f) + ((i4 >> 5) & 0x3f); 121 uint32_t b = ((i1 >> 11) + (i2 >> 11) + (i3 >> 11) + (i4 >> 11)); 122 return (r >> 2) | ((g >> 2) << 5) | ((b >> 2) << 11); 123} 124 125static inline uint32_t rsBoxFilter8888(uint32_t i1, uint32_t i2, uint32_t i3, uint32_t i4) { 126 uint32_t r = (i1 & 0xff) + (i2 & 0xff) + (i3 & 0xff) + (i4 & 0xff); 127 uint32_t g = ((i1 >> 8) & 0xff) + ((i2 >> 8) & 0xff) + ((i3 >> 8) & 0xff) + ((i4 >> 8) & 0xff); 128 uint32_t b = ((i1 >> 16) & 0xff) + ((i2 >> 16) & 0xff) + ((i3 >> 16) & 0xff) + ((i4 >> 16) & 0xff); 129 uint32_t a = ((i1 >> 24) & 0xff) + ((i2 >> 24) & 0xff) + ((i3 >> 24) & 0xff) + ((i4 >> 24) & 0xff); 130 return (r >> 2) | ((g >> 2) << 8) | ((b >> 2) << 16) | ((a >> 2) << 24); 131} 132 133} 134} 135 136#endif //ANDROID_RS_OBJECT_BASE_H 137 138 139