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 namespace android {
20 namespace 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     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 #ifdef __LP64__
131         // Large object pointers on 64-bit targets.
132         mBits = 256;
133         mTypeBits = 256;
134 #else
135         // Single pointer objects on 32-bit targets.
136         mBits = 32;
137         mTypeBits = 32;
138 #endif  // __LP64__
139         return;
140 
141     case RS_TYPE_FLOAT_16:
142         mTypeBits = 16;
143         mIsFloat = true;
144         break;
145     case RS_TYPE_FLOAT_32:
146         mTypeBits = 32;
147         mIsFloat = true;
148         break;
149     case RS_TYPE_FLOAT_64:
150         mTypeBits = 64;
151         mIsFloat = true;
152         break;
153     case RS_TYPE_SIGNED_8:
154         mTypeBits = 8;
155         mIsSigned = true;
156         break;
157     case RS_TYPE_SIGNED_16:
158         mTypeBits = 16;
159         mIsSigned = true;
160         break;
161     case RS_TYPE_SIGNED_32:
162         mTypeBits = 32;
163         mIsSigned = true;
164         break;
165     case RS_TYPE_SIGNED_64:
166         mTypeBits = 64;
167         mIsSigned = true;
168         break;
169     case RS_TYPE_UNSIGNED_8:
170         mTypeBits = 8;
171         break;
172     case RS_TYPE_UNSIGNED_16:
173         mTypeBits = 16;
174         break;
175     case RS_TYPE_UNSIGNED_32:
176         mTypeBits = 32;
177         break;
178     case RS_TYPE_UNSIGNED_64:
179         mTypeBits = 64;
180         break;
181 
182     case RS_TYPE_BOOLEAN:
183         mTypeBits = 8;
184         break;
185     default:
186         rsAssert(mType != RS_TYPE_INVALID);
187         rsAssert(0);
188         break;
189     }
190 
191     mBitsUnpadded = mTypeBits * mVectorSize;
192     mBits = mTypeBits * rsHigherPow2(mVectorSize);
193 }
194 
isReference() const195 bool Component::isReference() const {
196     return (mType >= RS_TYPE_ELEMENT);
197 }
198 
199 static const char * gTypeBasicStrings[] = {
200     "NONE",
201     "F16",
202     "F32",
203     "F64",
204     "S8",
205     "S16",
206     "S32",
207     "S64",
208     "U8",
209     "U16",
210     "U32",
211     "U64",
212     "BOOLEAN",
213     "UP_565",
214     "UP_5551",
215     "UP_4444",
216     "MATRIX_4X4",
217     "MATRIX_3X3",
218     "MATRIX_2X2",
219 };
220 
221 static const char * gTypeObjStrings[] = {
222     "ELEMENT",
223     "TYPE",
224     "ALLOCATION",
225     "SAMPLER",
226     "SCRIPT",
227     "MESH",
228     "PROGRAM_FRAGMENT",
229     "PROGRAM_VERTEX",
230     "PROGRAM_RASTER",
231     "PROGRAM_STORE",
232 };
233 
234 static const char * gKindStrings[] = {
235     "USER",
236     "COLOR",
237     "POSITION",
238     "TEXTURE",
239     "NORMAL",
240     "INDEX",
241     "POINT_SIZE",
242     "PIXEL_L",
243     "PIXEL_A",
244     "PIXEL_LA",
245     "PIXEL_RGB",
246     "PIXEL_RGBA",
247 };
248 
dumpLOGV(const char * prefix) const249 void Component::dumpLOGV(const char *prefix) const {
250     if (mType >= RS_TYPE_ELEMENT) {
251         ALOGV("%s   Component: %s, %s, vectorSize=%i, bits=%i",
252              prefix, gTypeObjStrings[mType - RS_TYPE_ELEMENT], gKindStrings[mKind], mVectorSize, mBits);
253     } else {
254         ALOGV("%s   Component: %s, %s, vectorSize=%i, bits=%i",
255              prefix, gTypeBasicStrings[mType], gKindStrings[mKind], mVectorSize, mBits);
256     }
257 }
258 
serialize(OStream * stream) const259 void Component::serialize(OStream *stream) const {
260     stream->addU8((uint8_t)mType);
261     stream->addU8((uint8_t)mKind);
262     stream->addU8((uint8_t)(mNormalized ? 1 : 0));
263     stream->addU32(mVectorSize);
264 }
265 
loadFromStream(IStream * stream)266 void Component::loadFromStream(IStream *stream) {
267     mType = (RsDataType)stream->loadU8();
268     mKind = (RsDataKind)stream->loadU8();
269     uint8_t temp = stream->loadU8();
270     mNormalized = temp != 0;
271     mVectorSize = stream->loadU32();
272 
273     set(mType, mKind, mNormalized, mVectorSize);
274 }
275 
276 } // namespace renderscript
277 } // namespace android
278