blob: decd0c79bd09785da92f8b50ac4f63a3ca3d2ac6 [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 android.util.Log;
20import java.util.BitSet;
Jason Sams25430d02010-02-02 15:26:40 -080021
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070022/**
Robert Ly11518ac2011-02-09 13:57:06 -080023 * Utility class for packing arguments and structures from Android system objects to
24 * Renderscript objects.
Jason Sams42d6c9e2010-02-02 15:45:58 -080025 *
26 **/
Jason Sams25430d02010-02-02 15:26:40 -080027public class FieldPacker {
28 public FieldPacker(int len) {
29 mPos = 0;
Stephen Hinese27832a2011-06-02 19:36:41 -070030 mLen = len;
Jason Sams25430d02010-02-02 15:26:40 -080031 mData = new byte[len];
Tim Murray7c4caad2013-04-10 16:21:40 -070032 mAlignment = new BitSet();
Jason Sams25430d02010-02-02 15:26:40 -080033 }
34
Jason Sams5b08a2d2013-02-08 11:22:17 -080035 public FieldPacker(byte[] data) {
36 mPos = 0;
37 mLen = data.length;
38 mData = data;
Tim Murray7c4caad2013-04-10 16:21:40 -070039 mAlignment = new BitSet();
Jason Sams5b08a2d2013-02-08 11:22:17 -080040 }
41
Jason Sams25430d02010-02-02 15:26:40 -080042 public void align(int v) {
Stephen Hinese27832a2011-06-02 19:36:41 -070043 if ((v <= 0) || ((v & (v - 1)) != 0)) {
44 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
45 }
46
Jason Sams25430d02010-02-02 15:26:40 -080047 while ((mPos & (v - 1)) != 0) {
Tim Murray7c4caad2013-04-10 16:21:40 -070048 mAlignment.flip(mPos);
Jason Sams25430d02010-02-02 15:26:40 -080049 mData[mPos++] = 0;
50 }
51 }
52
Tim Murray7c4caad2013-04-10 16:21:40 -070053 public void subalign(int v) {
54 if ((v & (v - 1)) != 0) {
55 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
56 }
57
58 while ((mPos & (v - 1)) != 0) {
59 mPos--;
60 }
61
62 if (mPos > 0) {
63 while (mAlignment.get(mPos - 1) == true) {
64 mPos--;
65 mAlignment.flip(mPos);
66 }
67 }
68
69 }
70
Jason Samsa70f4162010-03-26 15:33:42 -070071 public void reset() {
Jason Sams25430d02010-02-02 15:26:40 -080072 mPos = 0;
73 }
Jason Samsa70f4162010-03-26 15:33:42 -070074 public void reset(int i) {
Stephen Hinese27832a2011-06-02 19:36:41 -070075 if ((i < 0) || (i >= mLen)) {
76 throw new RSIllegalArgumentException("out of range argument: " + i);
77 }
Jason Samsa70f4162010-03-26 15:33:42 -070078 mPos = i;
79 }
Jason Sams25430d02010-02-02 15:26:40 -080080
Jason Sams020bb7b2010-06-17 15:55:00 -070081 public void skip(int i) {
Stephen Hinese27832a2011-06-02 19:36:41 -070082 int res = mPos + i;
Shih-wei Liao6e667252011-06-05 00:51:54 -070083 if ((res < 0) || (res > mLen)) {
Stephen Hinese27832a2011-06-02 19:36:41 -070084 throw new RSIllegalArgumentException("out of range argument: " + i);
85 }
86 mPos = res;
Jason Sams020bb7b2010-06-17 15:55:00 -070087 }
88
Jason Samsa70f4162010-03-26 15:33:42 -070089 public void addI8(byte v) {
Jason Sams25430d02010-02-02 15:26:40 -080090 mData[mPos++] = v;
91 }
92
Tim Murray7c4caad2013-04-10 16:21:40 -070093 public byte subI8() {
94 subalign(1);
95 return mData[--mPos];
96 }
97
Jason Samsa70f4162010-03-26 15:33:42 -070098 public void addI16(short v) {
Jason Sams25430d02010-02-02 15:26:40 -080099 align(2);
100 mData[mPos++] = (byte)(v & 0xff);
101 mData[mPos++] = (byte)(v >> 8);
102 }
103
Tim Murray7c4caad2013-04-10 16:21:40 -0700104 public short subI16() {
105 subalign(2);
106 short v = 0;
107 v = (short)((mData[--mPos] & 0xff) << 8);
108 v = (short)(v | (short)(mData[--mPos] & 0xff));
109 return v;
110 }
111
112
Jason Samsa70f4162010-03-26 15:33:42 -0700113 public void addI32(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800114 align(4);
115 mData[mPos++] = (byte)(v & 0xff);
116 mData[mPos++] = (byte)((v >> 8) & 0xff);
117 mData[mPos++] = (byte)((v >> 16) & 0xff);
118 mData[mPos++] = (byte)((v >> 24) & 0xff);
119 }
120
Tim Murray7c4caad2013-04-10 16:21:40 -0700121 public int subI32() {
122 subalign(4);
123 int v = 0;
124 v = ((mData[--mPos] & 0xff) << 24);
125 v = v | ((mData[--mPos] & 0xff) << 16);
126 v = v | ((mData[--mPos] & 0xff) << 8);
127 v = v | ((mData[--mPos] & 0xff));
128 return v;
129 }
130
131
Jason Samsa70f4162010-03-26 15:33:42 -0700132 public void addI64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800133 align(8);
134 mData[mPos++] = (byte)(v & 0xff);
135 mData[mPos++] = (byte)((v >> 8) & 0xff);
136 mData[mPos++] = (byte)((v >> 16) & 0xff);
137 mData[mPos++] = (byte)((v >> 24) & 0xff);
138 mData[mPos++] = (byte)((v >> 32) & 0xff);
139 mData[mPos++] = (byte)((v >> 40) & 0xff);
140 mData[mPos++] = (byte)((v >> 48) & 0xff);
141 mData[mPos++] = (byte)((v >> 56) & 0xff);
142 }
143
Tim Murray7c4caad2013-04-10 16:21:40 -0700144 public long subI64() {
145 subalign(8);
146 long v = 0;
147 byte x = 0;
148 x = ((mData[--mPos]));
149 v = (long)(v | (((long)x) & 0xff) << 56l);
150 x = ((mData[--mPos]));
151 v = (long)(v | (((long)x) & 0xff) << 48l);
152 x = ((mData[--mPos]));
153 v = (long)(v | (((long)x) & 0xff) << 40l);
154 x = ((mData[--mPos]));
155 v = (long)(v | (((long)x) & 0xff) << 32l);
156 x = ((mData[--mPos]));
157 v = (long)(v | (((long)x) & 0xff) << 24l);
158 x = ((mData[--mPos]));
159 v = (long)(v | (((long)x) & 0xff) << 16l);
160 x = ((mData[--mPos]));
161 v = (long)(v | (((long)x) & 0xff) << 8l);
162 x = ((mData[--mPos]));
163 v = (long)(v | (((long)x) & 0xff));
164 return v;
165 }
166
Jason Samsa70f4162010-03-26 15:33:42 -0700167 public void addU8(short v) {
Jason Sams25430d02010-02-02 15:26:40 -0800168 if ((v < 0) || (v > 0xff)) {
Jason Samsee734982010-08-12 12:44:02 -0700169 android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800170 throw new IllegalArgumentException("Saving value out of range for type");
171 }
172 mData[mPos++] = (byte)v;
173 }
174
Jason Samsa70f4162010-03-26 15:33:42 -0700175 public void addU16(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800176 if ((v < 0) || (v > 0xffff)) {
Jason Samsee734982010-08-12 12:44:02 -0700177 android.util.Log.e("rs", "FieldPacker.addU16( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800178 throw new IllegalArgumentException("Saving value out of range for type");
179 }
180 align(2);
181 mData[mPos++] = (byte)(v & 0xff);
182 mData[mPos++] = (byte)(v >> 8);
183 }
184
Jason Samsa70f4162010-03-26 15:33:42 -0700185 public void addU32(long v) {
Jason Samsee734982010-08-12 12:44:02 -0700186 if ((v < 0) || (v > 0xffffffffL)) {
187 android.util.Log.e("rs", "FieldPacker.addU32( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800188 throw new IllegalArgumentException("Saving value out of range for type");
189 }
190 align(4);
191 mData[mPos++] = (byte)(v & 0xff);
192 mData[mPos++] = (byte)((v >> 8) & 0xff);
193 mData[mPos++] = (byte)((v >> 16) & 0xff);
194 mData[mPos++] = (byte)((v >> 24) & 0xff);
195 }
196
Jason Samsa70f4162010-03-26 15:33:42 -0700197 public void addU64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800198 if (v < 0) {
Jason Samsee734982010-08-12 12:44:02 -0700199 android.util.Log.e("rs", "FieldPacker.addU64( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800200 throw new IllegalArgumentException("Saving value out of range for type");
201 }
202 align(8);
203 mData[mPos++] = (byte)(v & 0xff);
204 mData[mPos++] = (byte)((v >> 8) & 0xff);
205 mData[mPos++] = (byte)((v >> 16) & 0xff);
206 mData[mPos++] = (byte)((v >> 24) & 0xff);
207 mData[mPos++] = (byte)((v >> 32) & 0xff);
208 mData[mPos++] = (byte)((v >> 40) & 0xff);
209 mData[mPos++] = (byte)((v >> 48) & 0xff);
210 mData[mPos++] = (byte)((v >> 56) & 0xff);
211 }
212
Jason Samsa70f4162010-03-26 15:33:42 -0700213 public void addF32(float v) {
Jason Sams25430d02010-02-02 15:26:40 -0800214 addI32(Float.floatToRawIntBits(v));
215 }
216
Tim Murray7c4caad2013-04-10 16:21:40 -0700217 public float subF32() {
218 return Float.intBitsToFloat(subI32());
219 }
220
Stephen Hines02f417052010-09-30 15:19:22 -0700221 public void addF64(double v) {
Jason Sams25430d02010-02-02 15:26:40 -0800222 addI64(Double.doubleToRawLongBits(v));
223 }
224
Tim Murray7c4caad2013-04-10 16:21:40 -0700225 public double subF64() {
226 return Double.longBitsToDouble(subI64());
227 }
228
Jason Samsa70f4162010-03-26 15:33:42 -0700229 public void addObj(BaseObj obj) {
230 if (obj != null) {
Jason Samse07694b2012-04-03 15:36:36 -0700231 addI32(obj.getID(null));
Jason Samsa70f4162010-03-26 15:33:42 -0700232 } else {
233 addI32(0);
234 }
235 }
236
237 public void addF32(Float2 v) {
238 addF32(v.x);
239 addF32(v.y);
240 }
241 public void addF32(Float3 v) {
242 addF32(v.x);
243 addF32(v.y);
244 addF32(v.z);
245 }
246 public void addF32(Float4 v) {
247 addF32(v.x);
248 addF32(v.y);
249 addF32(v.z);
250 addF32(v.w);
251 }
252
Stephen Hines79ad3f22011-06-20 17:27:09 -0700253 public void addF64(Double2 v) {
254 addF64(v.x);
255 addF64(v.y);
256 }
257 public void addF64(Double3 v) {
258 addF64(v.x);
259 addF64(v.y);
260 addF64(v.z);
261 }
262 public void addF64(Double4 v) {
263 addF64(v.x);
264 addF64(v.y);
265 addF64(v.z);
266 addF64(v.w);
267 }
268
Jason Samsa70f4162010-03-26 15:33:42 -0700269 public void addI8(Byte2 v) {
270 addI8(v.x);
271 addI8(v.y);
272 }
273 public void addI8(Byte3 v) {
274 addI8(v.x);
275 addI8(v.y);
276 addI8(v.z);
277 }
278 public void addI8(Byte4 v) {
279 addI8(v.x);
280 addI8(v.y);
281 addI8(v.z);
282 addI8(v.w);
283 }
284
285 public void addU8(Short2 v) {
286 addU8(v.x);
287 addU8(v.y);
288 }
289 public void addU8(Short3 v) {
290 addU8(v.x);
291 addU8(v.y);
292 addU8(v.z);
293 }
294 public void addU8(Short4 v) {
295 addU8(v.x);
296 addU8(v.y);
297 addU8(v.z);
298 addU8(v.w);
299 }
300
301 public void addI16(Short2 v) {
302 addI16(v.x);
303 addI16(v.y);
304 }
305 public void addI16(Short3 v) {
306 addI16(v.x);
307 addI16(v.y);
308 addI16(v.z);
309 }
310 public void addI16(Short4 v) {
311 addI16(v.x);
312 addI16(v.y);
313 addI16(v.z);
314 addI16(v.w);
315 }
316
317 public void addU16(Int2 v) {
318 addU16(v.x);
319 addU16(v.y);
320 }
321 public void addU16(Int3 v) {
322 addU16(v.x);
323 addU16(v.y);
324 addU16(v.z);
325 }
326 public void addU16(Int4 v) {
327 addU16(v.x);
328 addU16(v.y);
329 addU16(v.z);
330 addU16(v.w);
331 }
332
333 public void addI32(Int2 v) {
334 addI32(v.x);
335 addI32(v.y);
336 }
337 public void addI32(Int3 v) {
338 addI32(v.x);
339 addI32(v.y);
340 addI32(v.z);
341 }
342 public void addI32(Int4 v) {
343 addI32(v.x);
344 addI32(v.y);
345 addI32(v.z);
346 addI32(v.w);
347 }
348
Stephen Hinese9f5c182011-01-20 18:17:25 -0800349 public void addU32(Long2 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700350 addU32(v.x);
351 addU32(v.y);
352 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800353 public void addU32(Long3 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700354 addU32(v.x);
355 addU32(v.y);
356 addU32(v.z);
357 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800358 public void addU32(Long4 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700359 addU32(v.x);
360 addU32(v.y);
361 addU32(v.z);
362 addU32(v.w);
363 }
364
Stephen Hines79ad3f22011-06-20 17:27:09 -0700365 public void addI64(Long2 v) {
366 addI64(v.x);
367 addI64(v.y);
368 }
369 public void addI64(Long3 v) {
370 addI64(v.x);
371 addI64(v.y);
372 addI64(v.z);
373 }
374 public void addI64(Long4 v) {
375 addI64(v.x);
376 addI64(v.y);
377 addI64(v.z);
378 addI64(v.w);
379 }
380
381 public void addU64(Long2 v) {
382 addU64(v.x);
383 addU64(v.y);
384 }
385 public void addU64(Long3 v) {
386 addU64(v.x);
387 addU64(v.y);
388 addU64(v.z);
389 }
390 public void addU64(Long4 v) {
391 addU64(v.x);
392 addU64(v.y);
393 addU64(v.z);
394 addU64(v.w);
395 }
396
Tim Murray7c4caad2013-04-10 16:21:40 -0700397
398 public Float2 subFloat2() {
399 Float2 v = new Float2();
400 v.y = subF32();
401 v.x = subF32();
402 return v;
403 }
404 public Float3 subFloat3() {
405 Float3 v = new Float3();
406 v.z = subF32();
407 v.y = subF32();
408 v.x = subF32();
409 return v;
410 }
411 public Float4 subFloat4() {
412 Float4 v = new Float4();
413 v.w = subF32();
414 v.z = subF32();
415 v.y = subF32();
416 v.x = subF32();
417 return v;
418 }
419
420 public Double2 subDouble2() {
421 Double2 v = new Double2();
422 v.y = subF64();
423 v.x = subF64();
424 return v;
425 }
426 public Double3 subDouble3() {
427 Double3 v = new Double3();
428 v.z = subF64();
429 v.y = subF64();
430 v.x = subF64();
431 return v;
432 }
433 public Double4 subDouble4() {
434 Double4 v = new Double4();
435 v.w = subF64();
436 v.z = subF64();
437 v.y = subF64();
438 v.x = subF64();
439 return v;
440 }
441
442 public Byte2 subByte2() {
443 Byte2 v = new Byte2();
444 v.y = subI8();
445 v.x = subI8();
446 return v;
447 }
448 public Byte3 subByte3() {
449 Byte3 v = new Byte3();
450 v.z = subI8();
451 v.y = subI8();
452 v.x = subI8();
453 return v;
454 }
455 public Byte4 subByte4() {
456 Byte4 v = new Byte4();
457 v.w = subI8();
458 v.z = subI8();
459 v.y = subI8();
460 v.x = subI8();
461 return v;
462 }
463
464 public Short2 subShort2() {
465 Short2 v = new Short2();
466 v.y = subI16();
467 v.x = subI16();
468 return v;
469 }
470 public Short3 subShort3() {
471 Short3 v = new Short3();
472 v.z = subI16();
473 v.y = subI16();
474 v.x = subI16();
475 return v;
476 }
477 public Short4 subShort4() {
478 Short4 v = new Short4();
479 v.w = subI16();
480 v.z = subI16();
481 v.y = subI16();
482 v.x = subI16();
483 return v;
484 }
485
486 public Int2 subInt2() {
487 Int2 v = new Int2();
488 v.y = subI32();
489 v.x = subI32();
490 return v;
491 }
492 public Int3 subInt3() {
493 Int3 v = new Int3();
494 v.z = subI32();
495 v.y = subI32();
496 v.x = subI32();
497 return v;
498 }
499 public Int4 subInt4() {
500 Int4 v = new Int4();
501 v.w = subI32();
502 v.z = subI32();
503 v.y = subI32();
504 v.x = subI32();
505 return v;
506 }
507
508 public Long2 subLong2() {
509 Long2 v = new Long2();
510 v.y = subI64();
511 v.x = subI64();
512 return v;
513 }
514 public Long3 subLong3() {
515 Long3 v = new Long3();
516 v.z = subI64();
517 v.y = subI64();
518 v.x = subI64();
519 return v;
520 }
521 public Long4 subLong4() {
522 Long4 v = new Long4();
523 v.w = subI64();
524 v.z = subI64();
525 v.y = subI64();
526 v.x = subI64();
527 return v;
528 }
529
530
531
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800532 public void addMatrix(Matrix4f v) {
533 for (int i=0; i < v.mMat.length; i++) {
534 addF32(v.mMat[i]);
535 }
536 }
537
Tim Murray7c4caad2013-04-10 16:21:40 -0700538 public Matrix4f subMatrix4f() {
539 Matrix4f v = new Matrix4f();
540 for (int i = v.mMat.length - 1; i >= 0; i--) {
541 v.mMat[i] = subF32();
542 }
543 return v;
544 }
545
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800546 public void addMatrix(Matrix3f v) {
547 for (int i=0; i < v.mMat.length; i++) {
548 addF32(v.mMat[i]);
549 }
550 }
551
Tim Murray7c4caad2013-04-10 16:21:40 -0700552 public Matrix3f subMatrix3f() {
553 Matrix3f v = new Matrix3f();
554 for (int i = v.mMat.length - 1; i >= 0; i--) {
555 v.mMat[i] = subF32();
556 }
557 return v;
558 }
559
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800560 public void addMatrix(Matrix2f v) {
561 for (int i=0; i < v.mMat.length; i++) {
562 addF32(v.mMat[i]);
563 }
564 }
565
Tim Murray7c4caad2013-04-10 16:21:40 -0700566 public Matrix2f subMatrix2f() {
567 Matrix2f v = new Matrix2f();
568 for (int i = v.mMat.length - 1; i >= 0; i--) {
569 v.mMat[i] = subF32();
570 }
571 return v;
572 }
573
Jason Samsfae3f6b2010-06-24 13:54:11 -0700574 public void addBoolean(boolean v) {
Jason Sams9e2b0c52010-06-21 18:30:02 -0700575 addI8((byte)(v ? 1 : 0));
Jason Samsf110d4b2010-06-21 17:42:41 -0700576 }
577
Tim Murray7c4caad2013-04-10 16:21:40 -0700578 public boolean subBoolean() {
579 byte v = subI8();
580 if (v == 1) {
581 return true;
582 }
583 return false;
584 }
585
Jason Samsa70f4162010-03-26 15:33:42 -0700586 public final byte[] getData() {
Jason Sams25430d02010-02-02 15:26:40 -0800587 return mData;
588 }
589
590 private final byte mData[];
591 private int mPos;
Stephen Hinese27832a2011-06-02 19:36:41 -0700592 private int mLen;
Tim Murray7c4caad2013-04-10 16:21:40 -0700593 private BitSet mAlignment;
Jason Sams25430d02010-02-02 15:26:40 -0800594
595}
596
597