blob: 723ab242c016287fa85c5503493de3957e9153fe [file] [log] [blame]
Jason Sams25430d02010-02-02 15:26:40 -08001/*
Jason Sams5b08a2d2013-02-08 11:22:17 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams25430d02010-02-02 15:26:40 -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
17package android.renderscript;
18
Tim Murray7c4caad2013-04-10 16:21:40 -070019import java.util.BitSet;
Jason Sams25430d02010-02-02 15:26:40 -080020
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070021/**
Robert Ly11518ac2011-02-09 13:57:06 -080022 * Utility class for packing arguments and structures from Android system objects to
Tim Murrayc11e25c2013-04-09 11:01:01 -070023 * RenderScript objects.
Jason Sams42d6c9e2010-02-02 15:45:58 -080024 *
Jason Samsf64cca92013-04-19 12:56:37 -070025 * This class is only intended to be used to support the
26 * reflected code generated by the RS tool chain. It should not
27 * be called directly.
28 *
Jason Sams42d6c9e2010-02-02 15:45:58 -080029 **/
Jason Sams25430d02010-02-02 15:26:40 -080030public class FieldPacker {
31 public FieldPacker(int len) {
32 mPos = 0;
Stephen Hinese27832a2011-06-02 19:36:41 -070033 mLen = len;
Jason Sams25430d02010-02-02 15:26:40 -080034 mData = new byte[len];
Tim Murray7c4caad2013-04-10 16:21:40 -070035 mAlignment = new BitSet();
Jason Sams25430d02010-02-02 15:26:40 -080036 }
37
Jason Sams5b08a2d2013-02-08 11:22:17 -080038 public FieldPacker(byte[] data) {
39 mPos = 0;
40 mLen = data.length;
41 mData = data;
Tim Murray7c4caad2013-04-10 16:21:40 -070042 mAlignment = new BitSet();
Jason Sams5b08a2d2013-02-08 11:22:17 -080043 }
44
Jason Sams25430d02010-02-02 15:26:40 -080045 public void align(int v) {
Stephen Hinese27832a2011-06-02 19:36:41 -070046 if ((v <= 0) || ((v & (v - 1)) != 0)) {
47 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
48 }
49
Jason Sams25430d02010-02-02 15:26:40 -080050 while ((mPos & (v - 1)) != 0) {
Tim Murray7c4caad2013-04-10 16:21:40 -070051 mAlignment.flip(mPos);
Jason Sams25430d02010-02-02 15:26:40 -080052 mData[mPos++] = 0;
53 }
54 }
55
Tim Murray7c4caad2013-04-10 16:21:40 -070056 public void subalign(int v) {
57 if ((v & (v - 1)) != 0) {
58 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
59 }
60
61 while ((mPos & (v - 1)) != 0) {
62 mPos--;
63 }
64
65 if (mPos > 0) {
66 while (mAlignment.get(mPos - 1) == true) {
67 mPos--;
68 mAlignment.flip(mPos);
69 }
70 }
71
72 }
73
Jason Samsa70f4162010-03-26 15:33:42 -070074 public void reset() {
Jason Sams25430d02010-02-02 15:26:40 -080075 mPos = 0;
76 }
Jason Samsa70f4162010-03-26 15:33:42 -070077 public void reset(int i) {
Stephen Hinese27832a2011-06-02 19:36:41 -070078 if ((i < 0) || (i >= mLen)) {
79 throw new RSIllegalArgumentException("out of range argument: " + i);
80 }
Jason Samsa70f4162010-03-26 15:33:42 -070081 mPos = i;
82 }
Jason Sams25430d02010-02-02 15:26:40 -080083
Jason Sams020bb7b2010-06-17 15:55:00 -070084 public void skip(int i) {
Stephen Hinese27832a2011-06-02 19:36:41 -070085 int res = mPos + i;
Shih-wei Liao6e667252011-06-05 00:51:54 -070086 if ((res < 0) || (res > mLen)) {
Stephen Hinese27832a2011-06-02 19:36:41 -070087 throw new RSIllegalArgumentException("out of range argument: " + i);
88 }
89 mPos = res;
Jason Sams020bb7b2010-06-17 15:55:00 -070090 }
91
Jason Samsa70f4162010-03-26 15:33:42 -070092 public void addI8(byte v) {
Jason Sams25430d02010-02-02 15:26:40 -080093 mData[mPos++] = v;
94 }
95
Tim Murray7c4caad2013-04-10 16:21:40 -070096 public byte subI8() {
97 subalign(1);
98 return mData[--mPos];
99 }
100
Jason Samsa70f4162010-03-26 15:33:42 -0700101 public void addI16(short v) {
Jason Sams25430d02010-02-02 15:26:40 -0800102 align(2);
103 mData[mPos++] = (byte)(v & 0xff);
104 mData[mPos++] = (byte)(v >> 8);
105 }
106
Tim Murray7c4caad2013-04-10 16:21:40 -0700107 public short subI16() {
108 subalign(2);
109 short v = 0;
110 v = (short)((mData[--mPos] & 0xff) << 8);
111 v = (short)(v | (short)(mData[--mPos] & 0xff));
112 return v;
113 }
114
115
Jason Samsa70f4162010-03-26 15:33:42 -0700116 public void addI32(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800117 align(4);
118 mData[mPos++] = (byte)(v & 0xff);
119 mData[mPos++] = (byte)((v >> 8) & 0xff);
120 mData[mPos++] = (byte)((v >> 16) & 0xff);
121 mData[mPos++] = (byte)((v >> 24) & 0xff);
122 }
123
Tim Murray7c4caad2013-04-10 16:21:40 -0700124 public int subI32() {
125 subalign(4);
126 int v = 0;
127 v = ((mData[--mPos] & 0xff) << 24);
128 v = v | ((mData[--mPos] & 0xff) << 16);
129 v = v | ((mData[--mPos] & 0xff) << 8);
130 v = v | ((mData[--mPos] & 0xff));
131 return v;
132 }
133
134
Jason Samsa70f4162010-03-26 15:33:42 -0700135 public void addI64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800136 align(8);
137 mData[mPos++] = (byte)(v & 0xff);
138 mData[mPos++] = (byte)((v >> 8) & 0xff);
139 mData[mPos++] = (byte)((v >> 16) & 0xff);
140 mData[mPos++] = (byte)((v >> 24) & 0xff);
141 mData[mPos++] = (byte)((v >> 32) & 0xff);
142 mData[mPos++] = (byte)((v >> 40) & 0xff);
143 mData[mPos++] = (byte)((v >> 48) & 0xff);
144 mData[mPos++] = (byte)((v >> 56) & 0xff);
145 }
146
Tim Murray7c4caad2013-04-10 16:21:40 -0700147 public long subI64() {
148 subalign(8);
149 long v = 0;
150 byte x = 0;
151 x = ((mData[--mPos]));
152 v = (long)(v | (((long)x) & 0xff) << 56l);
153 x = ((mData[--mPos]));
154 v = (long)(v | (((long)x) & 0xff) << 48l);
155 x = ((mData[--mPos]));
156 v = (long)(v | (((long)x) & 0xff) << 40l);
157 x = ((mData[--mPos]));
158 v = (long)(v | (((long)x) & 0xff) << 32l);
159 x = ((mData[--mPos]));
160 v = (long)(v | (((long)x) & 0xff) << 24l);
161 x = ((mData[--mPos]));
162 v = (long)(v | (((long)x) & 0xff) << 16l);
163 x = ((mData[--mPos]));
164 v = (long)(v | (((long)x) & 0xff) << 8l);
165 x = ((mData[--mPos]));
166 v = (long)(v | (((long)x) & 0xff));
167 return v;
168 }
169
Jason Samsa70f4162010-03-26 15:33:42 -0700170 public void addU8(short v) {
Jason Sams25430d02010-02-02 15:26:40 -0800171 if ((v < 0) || (v > 0xff)) {
Jason Samsee734982010-08-12 12:44:02 -0700172 android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800173 throw new IllegalArgumentException("Saving value out of range for type");
174 }
175 mData[mPos++] = (byte)v;
176 }
177
Jason Samsa70f4162010-03-26 15:33:42 -0700178 public void addU16(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800179 if ((v < 0) || (v > 0xffff)) {
Jason Samsee734982010-08-12 12:44:02 -0700180 android.util.Log.e("rs", "FieldPacker.addU16( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800181 throw new IllegalArgumentException("Saving value out of range for type");
182 }
183 align(2);
184 mData[mPos++] = (byte)(v & 0xff);
185 mData[mPos++] = (byte)(v >> 8);
186 }
187
Jason Samsa70f4162010-03-26 15:33:42 -0700188 public void addU32(long v) {
Jason Samsee734982010-08-12 12:44:02 -0700189 if ((v < 0) || (v > 0xffffffffL)) {
190 android.util.Log.e("rs", "FieldPacker.addU32( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800191 throw new IllegalArgumentException("Saving value out of range for type");
192 }
193 align(4);
194 mData[mPos++] = (byte)(v & 0xff);
195 mData[mPos++] = (byte)((v >> 8) & 0xff);
196 mData[mPos++] = (byte)((v >> 16) & 0xff);
197 mData[mPos++] = (byte)((v >> 24) & 0xff);
198 }
199
Jason Samsa70f4162010-03-26 15:33:42 -0700200 public void addU64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800201 if (v < 0) {
Jason Samsee734982010-08-12 12:44:02 -0700202 android.util.Log.e("rs", "FieldPacker.addU64( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800203 throw new IllegalArgumentException("Saving value out of range for type");
204 }
205 align(8);
206 mData[mPos++] = (byte)(v & 0xff);
207 mData[mPos++] = (byte)((v >> 8) & 0xff);
208 mData[mPos++] = (byte)((v >> 16) & 0xff);
209 mData[mPos++] = (byte)((v >> 24) & 0xff);
210 mData[mPos++] = (byte)((v >> 32) & 0xff);
211 mData[mPos++] = (byte)((v >> 40) & 0xff);
212 mData[mPos++] = (byte)((v >> 48) & 0xff);
213 mData[mPos++] = (byte)((v >> 56) & 0xff);
214 }
215
Jason Samsa70f4162010-03-26 15:33:42 -0700216 public void addF32(float v) {
Jason Sams25430d02010-02-02 15:26:40 -0800217 addI32(Float.floatToRawIntBits(v));
218 }
219
Tim Murray7c4caad2013-04-10 16:21:40 -0700220 public float subF32() {
221 return Float.intBitsToFloat(subI32());
222 }
223
Stephen Hines02f417052010-09-30 15:19:22 -0700224 public void addF64(double v) {
Jason Sams25430d02010-02-02 15:26:40 -0800225 addI64(Double.doubleToRawLongBits(v));
226 }
227
Tim Murray7c4caad2013-04-10 16:21:40 -0700228 public double subF64() {
229 return Double.longBitsToDouble(subI64());
230 }
231
Jason Samsa70f4162010-03-26 15:33:42 -0700232 public void addObj(BaseObj obj) {
233 if (obj != null) {
Tim Murray460a0492013-11-19 12:45:54 -0800234 // FIXME: this is fine for 32-bit but needs a path for 64-bit
235 addI32((int)obj.getID(null));
Jason Samsa70f4162010-03-26 15:33:42 -0700236 } else {
237 addI32(0);
238 }
239 }
240
241 public void addF32(Float2 v) {
242 addF32(v.x);
243 addF32(v.y);
244 }
245 public void addF32(Float3 v) {
246 addF32(v.x);
247 addF32(v.y);
248 addF32(v.z);
249 }
250 public void addF32(Float4 v) {
251 addF32(v.x);
252 addF32(v.y);
253 addF32(v.z);
254 addF32(v.w);
255 }
256
Stephen Hines79ad3f22011-06-20 17:27:09 -0700257 public void addF64(Double2 v) {
258 addF64(v.x);
259 addF64(v.y);
260 }
261 public void addF64(Double3 v) {
262 addF64(v.x);
263 addF64(v.y);
264 addF64(v.z);
265 }
266 public void addF64(Double4 v) {
267 addF64(v.x);
268 addF64(v.y);
269 addF64(v.z);
270 addF64(v.w);
271 }
272
Jason Samsa70f4162010-03-26 15:33:42 -0700273 public void addI8(Byte2 v) {
274 addI8(v.x);
275 addI8(v.y);
276 }
277 public void addI8(Byte3 v) {
278 addI8(v.x);
279 addI8(v.y);
280 addI8(v.z);
281 }
282 public void addI8(Byte4 v) {
283 addI8(v.x);
284 addI8(v.y);
285 addI8(v.z);
286 addI8(v.w);
287 }
288
289 public void addU8(Short2 v) {
290 addU8(v.x);
291 addU8(v.y);
292 }
293 public void addU8(Short3 v) {
294 addU8(v.x);
295 addU8(v.y);
296 addU8(v.z);
297 }
298 public void addU8(Short4 v) {
299 addU8(v.x);
300 addU8(v.y);
301 addU8(v.z);
302 addU8(v.w);
303 }
304
305 public void addI16(Short2 v) {
306 addI16(v.x);
307 addI16(v.y);
308 }
309 public void addI16(Short3 v) {
310 addI16(v.x);
311 addI16(v.y);
312 addI16(v.z);
313 }
314 public void addI16(Short4 v) {
315 addI16(v.x);
316 addI16(v.y);
317 addI16(v.z);
318 addI16(v.w);
319 }
320
321 public void addU16(Int2 v) {
322 addU16(v.x);
323 addU16(v.y);
324 }
325 public void addU16(Int3 v) {
326 addU16(v.x);
327 addU16(v.y);
328 addU16(v.z);
329 }
330 public void addU16(Int4 v) {
331 addU16(v.x);
332 addU16(v.y);
333 addU16(v.z);
334 addU16(v.w);
335 }
336
337 public void addI32(Int2 v) {
338 addI32(v.x);
339 addI32(v.y);
340 }
341 public void addI32(Int3 v) {
342 addI32(v.x);
343 addI32(v.y);
344 addI32(v.z);
345 }
346 public void addI32(Int4 v) {
347 addI32(v.x);
348 addI32(v.y);
349 addI32(v.z);
350 addI32(v.w);
351 }
352
Stephen Hinese9f5c182011-01-20 18:17:25 -0800353 public void addU32(Long2 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700354 addU32(v.x);
355 addU32(v.y);
356 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800357 public void addU32(Long3 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700358 addU32(v.x);
359 addU32(v.y);
360 addU32(v.z);
361 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800362 public void addU32(Long4 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700363 addU32(v.x);
364 addU32(v.y);
365 addU32(v.z);
366 addU32(v.w);
367 }
368
Stephen Hines79ad3f22011-06-20 17:27:09 -0700369 public void addI64(Long2 v) {
370 addI64(v.x);
371 addI64(v.y);
372 }
373 public void addI64(Long3 v) {
374 addI64(v.x);
375 addI64(v.y);
376 addI64(v.z);
377 }
378 public void addI64(Long4 v) {
379 addI64(v.x);
380 addI64(v.y);
381 addI64(v.z);
382 addI64(v.w);
383 }
384
385 public void addU64(Long2 v) {
386 addU64(v.x);
387 addU64(v.y);
388 }
389 public void addU64(Long3 v) {
390 addU64(v.x);
391 addU64(v.y);
392 addU64(v.z);
393 }
394 public void addU64(Long4 v) {
395 addU64(v.x);
396 addU64(v.y);
397 addU64(v.z);
398 addU64(v.w);
399 }
400
Tim Murray7c4caad2013-04-10 16:21:40 -0700401
402 public Float2 subFloat2() {
403 Float2 v = new Float2();
404 v.y = subF32();
405 v.x = subF32();
406 return v;
407 }
408 public Float3 subFloat3() {
409 Float3 v = new Float3();
410 v.z = subF32();
411 v.y = subF32();
412 v.x = subF32();
413 return v;
414 }
415 public Float4 subFloat4() {
416 Float4 v = new Float4();
417 v.w = subF32();
418 v.z = subF32();
419 v.y = subF32();
420 v.x = subF32();
421 return v;
422 }
423
424 public Double2 subDouble2() {
425 Double2 v = new Double2();
426 v.y = subF64();
427 v.x = subF64();
428 return v;
429 }
430 public Double3 subDouble3() {
431 Double3 v = new Double3();
432 v.z = subF64();
433 v.y = subF64();
434 v.x = subF64();
435 return v;
436 }
437 public Double4 subDouble4() {
438 Double4 v = new Double4();
439 v.w = subF64();
440 v.z = subF64();
441 v.y = subF64();
442 v.x = subF64();
443 return v;
444 }
445
446 public Byte2 subByte2() {
447 Byte2 v = new Byte2();
448 v.y = subI8();
449 v.x = subI8();
450 return v;
451 }
452 public Byte3 subByte3() {
453 Byte3 v = new Byte3();
454 v.z = subI8();
455 v.y = subI8();
456 v.x = subI8();
457 return v;
458 }
459 public Byte4 subByte4() {
460 Byte4 v = new Byte4();
461 v.w = subI8();
462 v.z = subI8();
463 v.y = subI8();
464 v.x = subI8();
465 return v;
466 }
467
468 public Short2 subShort2() {
469 Short2 v = new Short2();
470 v.y = subI16();
471 v.x = subI16();
472 return v;
473 }
474 public Short3 subShort3() {
475 Short3 v = new Short3();
476 v.z = subI16();
477 v.y = subI16();
478 v.x = subI16();
479 return v;
480 }
481 public Short4 subShort4() {
482 Short4 v = new Short4();
483 v.w = subI16();
484 v.z = subI16();
485 v.y = subI16();
486 v.x = subI16();
487 return v;
488 }
489
490 public Int2 subInt2() {
491 Int2 v = new Int2();
492 v.y = subI32();
493 v.x = subI32();
494 return v;
495 }
496 public Int3 subInt3() {
497 Int3 v = new Int3();
498 v.z = subI32();
499 v.y = subI32();
500 v.x = subI32();
501 return v;
502 }
503 public Int4 subInt4() {
504 Int4 v = new Int4();
505 v.w = subI32();
506 v.z = subI32();
507 v.y = subI32();
508 v.x = subI32();
509 return v;
510 }
511
512 public Long2 subLong2() {
513 Long2 v = new Long2();
514 v.y = subI64();
515 v.x = subI64();
516 return v;
517 }
518 public Long3 subLong3() {
519 Long3 v = new Long3();
520 v.z = subI64();
521 v.y = subI64();
522 v.x = subI64();
523 return v;
524 }
525 public Long4 subLong4() {
526 Long4 v = new Long4();
527 v.w = subI64();
528 v.z = subI64();
529 v.y = subI64();
530 v.x = subI64();
531 return v;
532 }
533
534
535
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800536 public void addMatrix(Matrix4f v) {
537 for (int i=0; i < v.mMat.length; i++) {
538 addF32(v.mMat[i]);
539 }
540 }
541
Tim Murray7c4caad2013-04-10 16:21:40 -0700542 public Matrix4f subMatrix4f() {
543 Matrix4f v = new Matrix4f();
544 for (int i = v.mMat.length - 1; i >= 0; i--) {
545 v.mMat[i] = subF32();
546 }
547 return v;
548 }
549
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800550 public void addMatrix(Matrix3f v) {
551 for (int i=0; i < v.mMat.length; i++) {
552 addF32(v.mMat[i]);
553 }
554 }
555
Tim Murray7c4caad2013-04-10 16:21:40 -0700556 public Matrix3f subMatrix3f() {
557 Matrix3f v = new Matrix3f();
558 for (int i = v.mMat.length - 1; i >= 0; i--) {
559 v.mMat[i] = subF32();
560 }
561 return v;
562 }
563
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800564 public void addMatrix(Matrix2f v) {
565 for (int i=0; i < v.mMat.length; i++) {
566 addF32(v.mMat[i]);
567 }
568 }
569
Tim Murray7c4caad2013-04-10 16:21:40 -0700570 public Matrix2f subMatrix2f() {
571 Matrix2f v = new Matrix2f();
572 for (int i = v.mMat.length - 1; i >= 0; i--) {
573 v.mMat[i] = subF32();
574 }
575 return v;
576 }
577
Jason Samsfae3f6b2010-06-24 13:54:11 -0700578 public void addBoolean(boolean v) {
Jason Sams9e2b0c52010-06-21 18:30:02 -0700579 addI8((byte)(v ? 1 : 0));
Jason Samsf110d4b2010-06-21 17:42:41 -0700580 }
581
Tim Murray7c4caad2013-04-10 16:21:40 -0700582 public boolean subBoolean() {
583 byte v = subI8();
584 if (v == 1) {
585 return true;
586 }
587 return false;
588 }
589
Jason Samsa70f4162010-03-26 15:33:42 -0700590 public final byte[] getData() {
Jason Sams25430d02010-02-02 15:26:40 -0800591 return mData;
592 }
593
594 private final byte mData[];
595 private int mPos;
Stephen Hinese27832a2011-06-02 19:36:41 -0700596 private int mLen;
Tim Murray7c4caad2013-04-10 16:21:40 -0700597 private BitSet mAlignment;
Jason Sams25430d02010-02-02 15:26:40 -0800598
599}
600
601