blob: c3e0d13aecdf4e8d02efcc629793c3e6bc1c1263 [file] [log] [blame]
Jason Samsd01d9702009-12-23 14:35:29 -08001/*
Jason Samsbc0ca6b2013-02-15 18:13:43 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Samsd01d9702009-12-23 14:35:29 -08003 *
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
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080019namespace android {
20namespace renderscript {
Jason Samsd01d9702009-12-23 14:35:29 -080021
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080022Component::Component() {
Jason Samsd01d9702009-12-23 14:35:29 -080023 set(RS_TYPE_NONE, RS_KIND_USER, false, 1);
24}
25
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080026Component::~Component() {
Jason Samsd01d9702009-12-23 14:35:29 -080027}
28
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080029void Component::set(RsDataType dt, RsDataKind dk, bool norm, uint32_t vecSize) {
Jason Samsd01d9702009-12-23 14:35:29 -080030 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080042 switch (mKind) {
Jason Samsd01d9702009-12-23 14:35:29 -080043 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;
Jason Samsbc0ca6b2013-02-15 18:13:43 -080064 case RS_KIND_PIXEL_YUV:
65 mIsPixel = true;
66 rsAssert(mVectorSize == 1);
67 rsAssert(mNormalized == true);
68 break;
69
Jason Samsd01d9702009-12-23 14:35:29 -080070 default:
Alex Sakhartchouk25a59d02011-12-29 11:17:38 -080071 rsAssert(mKind != RS_KIND_INVALID);
Jason Samsd01d9702009-12-23 14:35:29 -080072 break;
73 }
74
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080075 switch (mType) {
Jason Samsd01d9702009-12-23 14:35:29 -080076 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;
Jason Sams39cd3172010-08-25 14:31:48 -070096
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
Jason Samsd01d9702009-12-23 14:35:29 -0800116 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:
Stephen Hinesae8b7952012-04-11 18:09:54 -0700126 case RS_TYPE_FONT:
Jason Samsd01d9702009-12-23 14:35:29 -0800127 rsAssert(mVectorSize == 1);
128 rsAssert(mNormalized == false);
129 rsAssert(mKind == RS_KIND_USER);
Stephen Hines7202c922014-06-25 15:21:47 -0700130#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.
Jason Samsd01d9702009-12-23 14:35:29 -0800136 mBits = 32;
137 mTypeBits = 32;
Stephen Hines7202c922014-06-25 15:21:47 -0700138#endif // __LP64__
Jason Samsd01d9702009-12-23 14:35:29 -0800139 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;
Jason Sams5e9811f2010-06-21 17:42:41 -0700181
182 case RS_TYPE_BOOLEAN:
183 mTypeBits = 8;
184 break;
Alex Sakhartchouk25a59d02011-12-29 11:17:38 -0800185 default:
186 rsAssert(mType != RS_TYPE_INVALID);
Stephen Hinesae8b7952012-04-11 18:09:54 -0700187 rsAssert(0);
Alex Sakhartchouk25a59d02011-12-29 11:17:38 -0800188 break;
Jason Samsd01d9702009-12-23 14:35:29 -0800189 }
190
Alex Sakhartchouk2d1220c2011-11-15 15:15:21 -0800191 mBitsUnpadded = mTypeBits * mVectorSize;
192 mBits = mTypeBits * rsHigherPow2(mVectorSize);
Jason Samsd01d9702009-12-23 14:35:29 -0800193}
194
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800195bool Component::isReference() const {
Jason Samse3929c92010-08-09 18:13:33 -0700196 return (mType >= RS_TYPE_ELEMENT);
197}
Jason Samsd01d9702009-12-23 14:35:29 -0800198
Jason Sams39cd3172010-08-25 14:31:48 -0700199static const char * gTypeBasicStrings[] = {
Jason Samsd01d9702009-12-23 14:35:29 -0800200 "NONE",
201 "F16",
202 "F32",
203 "F64",
204 "S8",
205 "S16",
206 "S32",
207 "S64",
208 "U8",
209 "U16",
210 "U32",
211 "U64",
Jason Sams5e9811f2010-06-21 17:42:41 -0700212 "BOOLEAN",
Jason Samsd01d9702009-12-23 14:35:29 -0800213 "UP_565",
214 "UP_5551",
215 "UP_4444",
Jason Sams39cd3172010-08-25 14:31:48 -0700216 "MATRIX_4X4",
217 "MATRIX_3X3",
218 "MATRIX_2X2",
219};
220
221static const char * gTypeObjStrings[] = {
Jason Samsd01d9702009-12-23 14:35:29 -0800222 "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
234static 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800249void Component::dumpLOGV(const char *prefix) const {
Jason Sams39cd3172010-08-25 14:31:48 -0700250 if (mType >= RS_TYPE_ELEMENT) {
Steve Block65982012011-10-20 11:56:00 +0100251 ALOGV("%s Component: %s, %s, vectorSize=%i, bits=%i",
Jason Sams39cd3172010-08-25 14:31:48 -0700252 prefix, gTypeObjStrings[mType - RS_TYPE_ELEMENT], gKindStrings[mKind], mVectorSize, mBits);
253 } else {
Steve Block65982012011-10-20 11:56:00 +0100254 ALOGV("%s Component: %s, %s, vectorSize=%i, bits=%i",
Jason Sams39cd3172010-08-25 14:31:48 -0700255 prefix, gTypeBasicStrings[mType], gKindStrings[mKind], mVectorSize, mBits);
256 }
Jason Samsd01d9702009-12-23 14:35:29 -0800257}
258
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800259void Component::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700260 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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800266void Component::loadFromStream(IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700267 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
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800276} // namespace renderscript
277} // namespace android