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