1 /* 2 * Copyright (C) 2013 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 #include "rsComponent.h" 18 19 using namespace android; 20 using namespace android::renderscript; 21 22 Component::Component() { 23 set(RS_TYPE_NONE, RS_KIND_USER, false, 1); 24 } 25 26 Component::~Component() { 27 } 28 29 void Component::set(RsDataType dt, RsDataKind dk, bool norm, uint32_t vecSize) { 30 mType = dt; 31 mKind = dk; 32 mNormalized = norm; 33 mVectorSize = vecSize; 34 rsAssert(vecSize <= 4); 35 36 mBits = 0; 37 mTypeBits = 0; 38 mIsFloat = false; 39 mIsSigned = false; 40 mIsPixel = false; 41 42 switch (mKind) { 43 case RS_KIND_PIXEL_L: 44 case RS_KIND_PIXEL_A: 45 mIsPixel = true; 46 rsAssert(mVectorSize == 1); 47 rsAssert(mNormalized == true); 48 break; 49 case RS_KIND_PIXEL_LA: 50 mIsPixel = true; 51 rsAssert(mVectorSize == 2); 52 rsAssert(mNormalized == true); 53 break; 54 case RS_KIND_PIXEL_RGB: 55 mIsPixel = true; 56 rsAssert(mVectorSize == 3); 57 rsAssert(mNormalized == true); 58 break; 59 case RS_KIND_PIXEL_RGBA: 60 mIsPixel = true; 61 rsAssert(mVectorSize == 4); 62 rsAssert(mNormalized == true); 63 break; 64 case RS_KIND_PIXEL_YUV: 65 mIsPixel = true; 66 rsAssert(mVectorSize == 1); 67 rsAssert(mNormalized == true); 68 break; 69 70 default: 71 rsAssert(mKind != RS_KIND_INVALID); 72 break; 73 } 74 75 switch (mType) { 76 case RS_TYPE_NONE: 77 return; 78 case RS_TYPE_UNSIGNED_5_6_5: 79 mVectorSize = 3; 80 mBits = 16; 81 mNormalized = true; 82 rsAssert(mKind == RS_KIND_PIXEL_RGB); 83 return; 84 case RS_TYPE_UNSIGNED_5_5_5_1: 85 mVectorSize = 4; 86 mBits = 16; 87 mNormalized = true; 88 rsAssert(mKind == RS_KIND_PIXEL_RGBA); 89 return; 90 case RS_TYPE_UNSIGNED_4_4_4_4: 91 mVectorSize = 4; 92 mBits = 16; 93 mNormalized = true; 94 rsAssert(mKind == RS_KIND_PIXEL_RGBA); 95 return; 96 97 case RS_TYPE_MATRIX_4X4: 98 mTypeBits = 16 * 32; 99 rsAssert(mVectorSize == 1); 100 rsAssert(mNormalized == false); 101 rsAssert(mKind == RS_KIND_USER); 102 break; 103 case RS_TYPE_MATRIX_3X3: 104 mTypeBits = 9 * 32; 105 rsAssert(mVectorSize == 1); 106 rsAssert(mNormalized == false); 107 rsAssert(mKind == RS_KIND_USER); 108 break; 109 case RS_TYPE_MATRIX_2X2: 110 mTypeBits = 4 * 32; 111 rsAssert(mVectorSize == 1); 112 rsAssert(mNormalized == false); 113 rsAssert(mKind == RS_KIND_USER); 114 break; 115 116 case RS_TYPE_ELEMENT: 117 case RS_TYPE_TYPE: 118 case RS_TYPE_ALLOCATION: 119 case RS_TYPE_SAMPLER: 120 case RS_TYPE_SCRIPT: 121 case RS_TYPE_MESH: 122 case RS_TYPE_PROGRAM_FRAGMENT: 123 case RS_TYPE_PROGRAM_VERTEX: 124 case RS_TYPE_PROGRAM_RASTER: 125 case RS_TYPE_PROGRAM_STORE: 126 case RS_TYPE_FONT: 127 rsAssert(mVectorSize == 1); 128 rsAssert(mNormalized == false); 129 rsAssert(mKind == RS_KIND_USER); 130 mBits = 32; 131 mTypeBits = 32; 132 return; 133 134 case RS_TYPE_FLOAT_16: 135 mTypeBits = 16; 136 mIsFloat = true; 137 break; 138 case RS_TYPE_FLOAT_32: 139 mTypeBits = 32; 140 mIsFloat = true; 141 break; 142 case RS_TYPE_FLOAT_64: 143 mTypeBits = 64; 144 mIsFloat = true; 145 break; 146 case RS_TYPE_SIGNED_8: 147 mTypeBits = 8; 148 mIsSigned = true; 149 break; 150 case RS_TYPE_SIGNED_16: 151 mTypeBits = 16; 152 mIsSigned = true; 153 break; 154 case RS_TYPE_SIGNED_32: 155 mTypeBits = 32; 156 mIsSigned = true; 157 break; 158 case RS_TYPE_SIGNED_64: 159 mTypeBits = 64; 160 mIsSigned = true; 161 break; 162 case RS_TYPE_UNSIGNED_8: 163 mTypeBits = 8; 164 break; 165 case RS_TYPE_UNSIGNED_16: 166 mTypeBits = 16; 167 break; 168 case RS_TYPE_UNSIGNED_32: 169 mTypeBits = 32; 170 break; 171 case RS_TYPE_UNSIGNED_64: 172 mTypeBits = 64; 173 break; 174 175 case RS_TYPE_BOOLEAN: 176 mTypeBits = 8; 177 break; 178 default: 179 rsAssert(mType != RS_TYPE_INVALID); 180 rsAssert(0); 181 break; 182 } 183 184 mBitsUnpadded = mTypeBits * mVectorSize; 185 mBits = mTypeBits * rsHigherPow2(mVectorSize); 186 } 187 188 bool Component::isReference() const { 189 return (mType >= RS_TYPE_ELEMENT); 190 } 191 192 static const char * gTypeBasicStrings[] = { 193 "NONE", 194 "F16", 195 "F32", 196 "F64", 197 "S8", 198 "S16", 199 "S32", 200 "S64", 201 "U8", 202 "U16", 203 "U32", 204 "U64", 205 "BOOLEAN", 206 "UP_565", 207 "UP_5551", 208 "UP_4444", 209 "MATRIX_4X4", 210 "MATRIX_3X3", 211 "MATRIX_2X2", 212 }; 213 214 static const char * gTypeObjStrings[] = { 215 "ELEMENT", 216 "TYPE", 217 "ALLOCATION", 218 "SAMPLER", 219 "SCRIPT", 220 "MESH", 221 "PROGRAM_FRAGMENT", 222 "PROGRAM_VERTEX", 223 "PROGRAM_RASTER", 224 "PROGRAM_STORE", 225 }; 226 227 static const char * gKindStrings[] = { 228 "USER", 229 "COLOR", 230 "POSITION", 231 "TEXTURE", 232 "NORMAL", 233 "INDEX", 234 "POINT_SIZE", 235 "PIXEL_L", 236 "PIXEL_A", 237 "PIXEL_LA", 238 "PIXEL_RGB", 239 "PIXEL_RGBA", 240 }; 241 242 void Component::dumpLOGV(const char *prefix) const { 243 if (mType >= RS_TYPE_ELEMENT) { 244 ALOGV("%s Component: %s, %s, vectorSize=%i, bits=%i", 245 prefix, gTypeObjStrings[mType - RS_TYPE_ELEMENT], gKindStrings[mKind], mVectorSize, mBits); 246 } else { 247 ALOGV("%s Component: %s, %s, vectorSize=%i, bits=%i", 248 prefix, gTypeBasicStrings[mType], gKindStrings[mKind], mVectorSize, mBits); 249 } 250 } 251 252 void Component::serialize(OStream *stream) const { 253 stream->addU8((uint8_t)mType); 254 stream->addU8((uint8_t)mKind); 255 stream->addU8((uint8_t)(mNormalized ? 1 : 0)); 256 stream->addU32(mVectorSize); 257 } 258 259 void Component::loadFromStream(IStream *stream) { 260 mType = (RsDataType)stream->loadU8(); 261 mKind = (RsDataKind)stream->loadU8(); 262 uint8_t temp = stream->loadU8(); 263 mNormalized = temp != 0; 264 mVectorSize = stream->loadU32(); 265 266 set(mType, mKind, mNormalized, mVectorSize); 267 } 268 269 270 271 272