blob: cf20e635e22a9eefcb7dab0756ee383539c3daa9 [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
Tim Murrayc11e25c2013-04-09 11:01:01 -070024 * RenderScript objects.
Jason Sams42d6c9e2010-02-02 15:45:58 -080025 *
Jason Samsf64cca92013-04-19 12:56:37 -070026 * This class is only intended to be used to support the
27 * reflected code generated by the RS tool chain. It should not
28 * be called directly.
29 *
Jason Sams42d6c9e2010-02-02 15:45:58 -080030 **/
Jason Sams25430d02010-02-02 15:26:40 -080031public class FieldPacker {
32 public FieldPacker(int len) {
33 mPos = 0;
Stephen Hinese27832a2011-06-02 19:36:41 -070034 mLen = len;
Jason Sams25430d02010-02-02 15:26:40 -080035 mData = new byte[len];
Tim Murray7c4caad2013-04-10 16:21:40 -070036 mAlignment = new BitSet();
Jason Sams25430d02010-02-02 15:26:40 -080037 }
38
Jason Sams5b08a2d2013-02-08 11:22:17 -080039 public FieldPacker(byte[] data) {
40 mPos = 0;
41 mLen = data.length;
42 mData = data;
Tim Murray7c4caad2013-04-10 16:21:40 -070043 mAlignment = new BitSet();
Jason Sams5b08a2d2013-02-08 11:22:17 -080044 }
45
Jason Sams25430d02010-02-02 15:26:40 -080046 public void align(int v) {
Stephen Hinese27832a2011-06-02 19:36:41 -070047 if ((v <= 0) || ((v & (v - 1)) != 0)) {
48 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
49 }
50
Jason Sams25430d02010-02-02 15:26:40 -080051 while ((mPos & (v - 1)) != 0) {
Tim Murray7c4caad2013-04-10 16:21:40 -070052 mAlignment.flip(mPos);
Jason Sams25430d02010-02-02 15:26:40 -080053 mData[mPos++] = 0;
54 }
55 }
56
Tim Murray7c4caad2013-04-10 16:21:40 -070057 public void subalign(int v) {
58 if ((v & (v - 1)) != 0) {
59 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
60 }
61
62 while ((mPos & (v - 1)) != 0) {
63 mPos--;
64 }
65
66 if (mPos > 0) {
67 while (mAlignment.get(mPos - 1) == true) {
68 mPos--;
69 mAlignment.flip(mPos);
70 }
71 }
72
73 }
74
Jason Samsa70f4162010-03-26 15:33:42 -070075 public void reset() {
Jason Sams25430d02010-02-02 15:26:40 -080076 mPos = 0;
77 }
Jason Samsa70f4162010-03-26 15:33:42 -070078 public void reset(int i) {
Stephen Hinese27832a2011-06-02 19:36:41 -070079 if ((i < 0) || (i >= mLen)) {
80 throw new RSIllegalArgumentException("out of range argument: " + i);
81 }
Jason Samsa70f4162010-03-26 15:33:42 -070082 mPos = i;
83 }
Jason Sams25430d02010-02-02 15:26:40 -080084
Jason Sams020bb7b2010-06-17 15:55:00 -070085 public void skip(int i) {
Stephen Hinese27832a2011-06-02 19:36:41 -070086 int res = mPos + i;
Shih-wei Liao6e667252011-06-05 00:51:54 -070087 if ((res < 0) || (res > mLen)) {
Stephen Hinese27832a2011-06-02 19:36:41 -070088 throw new RSIllegalArgumentException("out of range argument: " + i);
89 }
90 mPos = res;
Jason Sams020bb7b2010-06-17 15:55:00 -070091 }
92
Jason Samsa70f4162010-03-26 15:33:42 -070093 public void addI8(byte v) {
Jason Sams25430d02010-02-02 15:26:40 -080094 mData[mPos++] = v;
95 }
96
Tim Murray7c4caad2013-04-10 16:21:40 -070097 public byte subI8() {
98 subalign(1);
99 return mData[--mPos];
100 }
101
Jason Samsa70f4162010-03-26 15:33:42 -0700102 public void addI16(short v) {
Jason Sams25430d02010-02-02 15:26:40 -0800103 align(2);
104 mData[mPos++] = (byte)(v & 0xff);
105 mData[mPos++] = (byte)(v >> 8);
106 }
107
Tim Murray7c4caad2013-04-10 16:21:40 -0700108 public short subI16() {
109 subalign(2);
110 short v = 0;
111 v = (short)((mData[--mPos] & 0xff) << 8);
112 v = (short)(v | (short)(mData[--mPos] & 0xff));
113 return v;
114 }
115
116
Jason Samsa70f4162010-03-26 15:33:42 -0700117 public void addI32(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800118 align(4);
119 mData[mPos++] = (byte)(v & 0xff);
120 mData[mPos++] = (byte)((v >> 8) & 0xff);
121 mData[mPos++] = (byte)((v >> 16) & 0xff);
122 mData[mPos++] = (byte)((v >> 24) & 0xff);
123 }
124
Tim Murray7c4caad2013-04-10 16:21:40 -0700125 public int subI32() {
126 subalign(4);
127 int v = 0;
128 v = ((mData[--mPos] & 0xff) << 24);
129 v = v | ((mData[--mPos] & 0xff) << 16);
130 v = v | ((mData[--mPos] & 0xff) << 8);
131 v = v | ((mData[--mPos] & 0xff));
132 return v;
133 }
134
135
Jason Samsa70f4162010-03-26 15:33:42 -0700136 public void addI64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800137 align(8);
138 mData[mPos++] = (byte)(v & 0xff);
139 mData[mPos++] = (byte)((v >> 8) & 0xff);
140 mData[mPos++] = (byte)((v >> 16) & 0xff);
141 mData[mPos++] = (byte)((v >> 24) & 0xff);
142 mData[mPos++] = (byte)((v >> 32) & 0xff);
143 mData[mPos++] = (byte)((v >> 40) & 0xff);
144 mData[mPos++] = (byte)((v >> 48) & 0xff);
145 mData[mPos++] = (byte)((v >> 56) & 0xff);
146 }
147
Tim Murray7c4caad2013-04-10 16:21:40 -0700148 public long subI64() {
149 subalign(8);
150 long v = 0;
151 byte x = 0;
152 x = ((mData[--mPos]));
153 v = (long)(v | (((long)x) & 0xff) << 56l);
154 x = ((mData[--mPos]));
155 v = (long)(v | (((long)x) & 0xff) << 48l);
156 x = ((mData[--mPos]));
157 v = (long)(v | (((long)x) & 0xff) << 40l);
158 x = ((mData[--mPos]));
159 v = (long)(v | (((long)x) & 0xff) << 32l);
160 x = ((mData[--mPos]));
161 v = (long)(v | (((long)x) & 0xff) << 24l);
162 x = ((mData[--mPos]));
163 v = (long)(v | (((long)x) & 0xff) << 16l);
164 x = ((mData[--mPos]));
165 v = (long)(v | (((long)x) & 0xff) << 8l);
166 x = ((mData[--mPos]));
167 v = (long)(v | (((long)x) & 0xff));
168 return v;
169 }
170
Jason Samsa70f4162010-03-26 15:33:42 -0700171 public void addU8(short v) {
Jason Sams25430d02010-02-02 15:26:40 -0800172 if ((v < 0) || (v > 0xff)) {
Jason Samsee734982010-08-12 12:44:02 -0700173 android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800174 throw new IllegalArgumentException("Saving value out of range for type");
175 }
176 mData[mPos++] = (byte)v;
177 }
178
Jason Samsa70f4162010-03-26 15:33:42 -0700179 public void addU16(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800180 if ((v < 0) || (v > 0xffff)) {
Jason Samsee734982010-08-12 12:44:02 -0700181 android.util.Log.e("rs", "FieldPacker.addU16( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800182 throw new IllegalArgumentException("Saving value out of range for type");
183 }
184 align(2);
185 mData[mPos++] = (byte)(v & 0xff);
186 mData[mPos++] = (byte)(v >> 8);
187 }
188
Jason Samsa70f4162010-03-26 15:33:42 -0700189 public void addU32(long v) {
Jason Samsee734982010-08-12 12:44:02 -0700190 if ((v < 0) || (v > 0xffffffffL)) {
191 android.util.Log.e("rs", "FieldPacker.addU32( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800192 throw new IllegalArgumentException("Saving value out of range for type");
193 }
194 align(4);
195 mData[mPos++] = (byte)(v & 0xff);
196 mData[mPos++] = (byte)((v >> 8) & 0xff);
197 mData[mPos++] = (byte)((v >> 16) & 0xff);
198 mData[mPos++] = (byte)((v >> 24) & 0xff);
199 }
200
Jason Samsa70f4162010-03-26 15:33:42 -0700201 public void addU64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800202 if (v < 0) {
Jason Samsee734982010-08-12 12:44:02 -0700203 android.util.Log.e("rs", "FieldPacker.addU64( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800204 throw new IllegalArgumentException("Saving value out of range for type");
205 }
206 align(8);
207 mData[mPos++] = (byte)(v & 0xff);
208 mData[mPos++] = (byte)((v >> 8) & 0xff);
209 mData[mPos++] = (byte)((v >> 16) & 0xff);
210 mData[mPos++] = (byte)((v >> 24) & 0xff);
211 mData[mPos++] = (byte)((v >> 32) & 0xff);
212 mData[mPos++] = (byte)((v >> 40) & 0xff);
213 mData[mPos++] = (byte)((v >> 48) & 0xff);
214 mData[mPos++] = (byte)((v >> 56) & 0xff);
215 }
216
Jason Samsa70f4162010-03-26 15:33:42 -0700217 public void addF32(float v) {
Jason Sams25430d02010-02-02 15:26:40 -0800218 addI32(Float.floatToRawIntBits(v));
219 }
220
Tim Murray7c4caad2013-04-10 16:21:40 -0700221 public float subF32() {
222 return Float.intBitsToFloat(subI32());
223 }
224
Stephen Hines02f417052010-09-30 15:19:22 -0700225 public void addF64(double v) {
Jason Sams25430d02010-02-02 15:26:40 -0800226 addI64(Double.doubleToRawLongBits(v));
227 }
228
Tim Murray7c4caad2013-04-10 16:21:40 -0700229 public double subF64() {
230 return Double.longBitsToDouble(subI64());
231 }
232
Jason Samsa70f4162010-03-26 15:33:42 -0700233 public void addObj(BaseObj obj) {
234 if (obj != null) {
Tim Murray7a629fa2013-11-19 12:45:54 -0800235 // FIXME: this is fine for 32-bit but needs a path for 64-bit
236 addI32((int)obj.getID(null));
Jason Samsa70f4162010-03-26 15:33:42 -0700237 } else {
238 addI32(0);
239 }
240 }
241
242 public void addF32(Float2 v) {
243 addF32(v.x);
244 addF32(v.y);
245 }
246 public void addF32(Float3 v) {
247 addF32(v.x);
248 addF32(v.y);
249 addF32(v.z);
250 }
251 public void addF32(Float4 v) {
252 addF32(v.x);
253 addF32(v.y);
254 addF32(v.z);
255 addF32(v.w);
256 }
257
Stephen Hines79ad3f22011-06-20 17:27:09 -0700258 public void addF64(Double2 v) {
259 addF64(v.x);
260 addF64(v.y);
261 }
262 public void addF64(Double3 v) {
263 addF64(v.x);
264 addF64(v.y);
265 addF64(v.z);
266 }
267 public void addF64(Double4 v) {
268 addF64(v.x);
269 addF64(v.y);
270 addF64(v.z);
271 addF64(v.w);
272 }
273
Jason Samsa70f4162010-03-26 15:33:42 -0700274 public void addI8(Byte2 v) {
275 addI8(v.x);
276 addI8(v.y);
277 }
278 public void addI8(Byte3 v) {
279 addI8(v.x);
280 addI8(v.y);
281 addI8(v.z);
282 }
283 public void addI8(Byte4 v) {
284 addI8(v.x);
285 addI8(v.y);
286 addI8(v.z);
287 addI8(v.w);
288 }
289
290 public void addU8(Short2 v) {
291 addU8(v.x);
292 addU8(v.y);
293 }
294 public void addU8(Short3 v) {
295 addU8(v.x);
296 addU8(v.y);
297 addU8(v.z);
298 }
299 public void addU8(Short4 v) {
300 addU8(v.x);
301 addU8(v.y);
302 addU8(v.z);
303 addU8(v.w);
304 }
305
306 public void addI16(Short2 v) {
307 addI16(v.x);
308 addI16(v.y);
309 }
310 public void addI16(Short3 v) {
311 addI16(v.x);
312 addI16(v.y);
313 addI16(v.z);
314 }
315 public void addI16(Short4 v) {
316 addI16(v.x);
317 addI16(v.y);
318 addI16(v.z);
319 addI16(v.w);
320 }
321
322 public void addU16(Int2 v) {
323 addU16(v.x);
324 addU16(v.y);
325 }
326 public void addU16(Int3 v) {
327 addU16(v.x);
328 addU16(v.y);
329 addU16(v.z);
330 }
331 public void addU16(Int4 v) {
332 addU16(v.x);
333 addU16(v.y);
334 addU16(v.z);
335 addU16(v.w);
336 }
337
338 public void addI32(Int2 v) {
339 addI32(v.x);
340 addI32(v.y);
341 }
342 public void addI32(Int3 v) {
343 addI32(v.x);
344 addI32(v.y);
345 addI32(v.z);
346 }
347 public void addI32(Int4 v) {
348 addI32(v.x);
349 addI32(v.y);
350 addI32(v.z);
351 addI32(v.w);
352 }
353
Stephen Hinese9f5c182011-01-20 18:17:25 -0800354 public void addU32(Long2 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700355 addU32(v.x);
356 addU32(v.y);
357 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800358 public void addU32(Long3 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700359 addU32(v.x);
360 addU32(v.y);
361 addU32(v.z);
362 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800363 public void addU32(Long4 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700364 addU32(v.x);
365 addU32(v.y);
366 addU32(v.z);
367 addU32(v.w);
368 }
369
Stephen Hines79ad3f22011-06-20 17:27:09 -0700370 public void addI64(Long2 v) {
371 addI64(v.x);
372 addI64(v.y);
373 }
374 public void addI64(Long3 v) {
375 addI64(v.x);
376 addI64(v.y);
377 addI64(v.z);
378 }
379 public void addI64(Long4 v) {
380 addI64(v.x);
381 addI64(v.y);
382 addI64(v.z);
383 addI64(v.w);
384 }
385
386 public void addU64(Long2 v) {
387 addU64(v.x);
388 addU64(v.y);
389 }
390 public void addU64(Long3 v) {
391 addU64(v.x);
392 addU64(v.y);
393 addU64(v.z);
394 }
395 public void addU64(Long4 v) {
396 addU64(v.x);
397 addU64(v.y);
398 addU64(v.z);
399 addU64(v.w);
400 }
401
Tim Murray7c4caad2013-04-10 16:21:40 -0700402
403 public Float2 subFloat2() {
404 Float2 v = new Float2();
405 v.y = subF32();
406 v.x = subF32();
407 return v;
408 }
409 public Float3 subFloat3() {
410 Float3 v = new Float3();
411 v.z = subF32();
412 v.y = subF32();
413 v.x = subF32();
414 return v;
415 }
416 public Float4 subFloat4() {
417 Float4 v = new Float4();
418 v.w = subF32();
419 v.z = subF32();
420 v.y = subF32();
421 v.x = subF32();
422 return v;
423 }
424
425 public Double2 subDouble2() {
426 Double2 v = new Double2();
427 v.y = subF64();
428 v.x = subF64();
429 return v;
430 }
431 public Double3 subDouble3() {
432 Double3 v = new Double3();
433 v.z = subF64();
434 v.y = subF64();
435 v.x = subF64();
436 return v;
437 }
438 public Double4 subDouble4() {
439 Double4 v = new Double4();
440 v.w = subF64();
441 v.z = subF64();
442 v.y = subF64();
443 v.x = subF64();
444 return v;
445 }
446
447 public Byte2 subByte2() {
448 Byte2 v = new Byte2();
449 v.y = subI8();
450 v.x = subI8();
451 return v;
452 }
453 public Byte3 subByte3() {
454 Byte3 v = new Byte3();
455 v.z = subI8();
456 v.y = subI8();
457 v.x = subI8();
458 return v;
459 }
460 public Byte4 subByte4() {
461 Byte4 v = new Byte4();
462 v.w = subI8();
463 v.z = subI8();
464 v.y = subI8();
465 v.x = subI8();
466 return v;
467 }
468
469 public Short2 subShort2() {
470 Short2 v = new Short2();
471 v.y = subI16();
472 v.x = subI16();
473 return v;
474 }
475 public Short3 subShort3() {
476 Short3 v = new Short3();
477 v.z = subI16();
478 v.y = subI16();
479 v.x = subI16();
480 return v;
481 }
482 public Short4 subShort4() {
483 Short4 v = new Short4();
484 v.w = subI16();
485 v.z = subI16();
486 v.y = subI16();
487 v.x = subI16();
488 return v;
489 }
490
491 public Int2 subInt2() {
492 Int2 v = new Int2();
493 v.y = subI32();
494 v.x = subI32();
495 return v;
496 }
497 public Int3 subInt3() {
498 Int3 v = new Int3();
499 v.z = subI32();
500 v.y = subI32();
501 v.x = subI32();
502 return v;
503 }
504 public Int4 subInt4() {
505 Int4 v = new Int4();
506 v.w = subI32();
507 v.z = subI32();
508 v.y = subI32();
509 v.x = subI32();
510 return v;
511 }
512
513 public Long2 subLong2() {
514 Long2 v = new Long2();
515 v.y = subI64();
516 v.x = subI64();
517 return v;
518 }
519 public Long3 subLong3() {
520 Long3 v = new Long3();
521 v.z = subI64();
522 v.y = subI64();
523 v.x = subI64();
524 return v;
525 }
526 public Long4 subLong4() {
527 Long4 v = new Long4();
528 v.w = subI64();
529 v.z = subI64();
530 v.y = subI64();
531 v.x = subI64();
532 return v;
533 }
534
535
536
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800537 public void addMatrix(Matrix4f v) {
538 for (int i=0; i < v.mMat.length; i++) {
539 addF32(v.mMat[i]);
540 }
541 }
542
Tim Murray7c4caad2013-04-10 16:21:40 -0700543 public Matrix4f subMatrix4f() {
544 Matrix4f v = new Matrix4f();
545 for (int i = v.mMat.length - 1; i >= 0; i--) {
546 v.mMat[i] = subF32();
547 }
548 return v;
549 }
550
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800551 public void addMatrix(Matrix3f v) {
552 for (int i=0; i < v.mMat.length; i++) {
553 addF32(v.mMat[i]);
554 }
555 }
556
Tim Murray7c4caad2013-04-10 16:21:40 -0700557 public Matrix3f subMatrix3f() {
558 Matrix3f v = new Matrix3f();
559 for (int i = v.mMat.length - 1; i >= 0; i--) {
560 v.mMat[i] = subF32();
561 }
562 return v;
563 }
564
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800565 public void addMatrix(Matrix2f v) {
566 for (int i=0; i < v.mMat.length; i++) {
567 addF32(v.mMat[i]);
568 }
569 }
570
Tim Murray7c4caad2013-04-10 16:21:40 -0700571 public Matrix2f subMatrix2f() {
572 Matrix2f v = new Matrix2f();
573 for (int i = v.mMat.length - 1; i >= 0; i--) {
574 v.mMat[i] = subF32();
575 }
576 return v;
577 }
578
Jason Samsfae3f6b2010-06-24 13:54:11 -0700579 public void addBoolean(boolean v) {
Jason Sams9e2b0c52010-06-21 18:30:02 -0700580 addI8((byte)(v ? 1 : 0));
Jason Samsf110d4b2010-06-21 17:42:41 -0700581 }
582
Tim Murray7c4caad2013-04-10 16:21:40 -0700583 public boolean subBoolean() {
584 byte v = subI8();
585 if (v == 1) {
586 return true;
587 }
588 return false;
589 }
590
Jason Samsa70f4162010-03-26 15:33:42 -0700591 public final byte[] getData() {
Jason Sams25430d02010-02-02 15:26:40 -0800592 return mData;
593 }
594
595 private final byte mData[];
596 private int mPos;
Stephen Hinese27832a2011-06-02 19:36:41 -0700597 private int mLen;
Tim Murray7c4caad2013-04-10 16:21:40 -0700598 private BitSet mAlignment;
Jason Sams25430d02010-02-02 15:26:40 -0800599
600}
601
602