blob: 9048c31827606fab0660403d2d3bdbec494582ce [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) {
Stephen Hinesfcb31612014-06-23 17:35:04 -070040 // Advance mPos to the end of the buffer, since we are copying in the
41 // full data input.
42 mPos = data.length;
Jason Sams5b08a2d2013-02-08 11:22:17 -080043 mLen = data.length;
44 mData = data;
Tim Murray7c4caad2013-04-10 16:21:40 -070045 mAlignment = new BitSet();
Stephen Hinesfcb31612014-06-23 17:35:04 -070046 // TODO: We should either have an actual FieldPacker copy constructor
47 // or drop support for computing alignment like this. As it stands,
48 // subAlign() can never work correctly for copied FieldPacker objects.
Jason Sams5b08a2d2013-02-08 11:22:17 -080049 }
50
Jason Sams25430d02010-02-02 15:26:40 -080051 public void align(int v) {
Stephen Hinese27832a2011-06-02 19:36:41 -070052 if ((v <= 0) || ((v & (v - 1)) != 0)) {
53 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
54 }
55
Jason Sams25430d02010-02-02 15:26:40 -080056 while ((mPos & (v - 1)) != 0) {
Tim Murray7c4caad2013-04-10 16:21:40 -070057 mAlignment.flip(mPos);
Jason Sams25430d02010-02-02 15:26:40 -080058 mData[mPos++] = 0;
59 }
60 }
61
Tim Murray7c4caad2013-04-10 16:21:40 -070062 public void subalign(int v) {
63 if ((v & (v - 1)) != 0) {
64 throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
65 }
66
67 while ((mPos & (v - 1)) != 0) {
68 mPos--;
69 }
70
71 if (mPos > 0) {
72 while (mAlignment.get(mPos - 1) == true) {
73 mPos--;
74 mAlignment.flip(mPos);
75 }
76 }
77
78 }
79
Jason Samsa70f4162010-03-26 15:33:42 -070080 public void reset() {
Jason Sams25430d02010-02-02 15:26:40 -080081 mPos = 0;
82 }
Jason Samsa70f4162010-03-26 15:33:42 -070083 public void reset(int i) {
Stephen Hinesfa1275a2014-06-17 17:25:04 -070084 if ((i < 0) || (i > mLen)) {
Stephen Hinese27832a2011-06-02 19:36:41 -070085 throw new RSIllegalArgumentException("out of range argument: " + i);
86 }
Jason Samsa70f4162010-03-26 15:33:42 -070087 mPos = i;
88 }
Jason Sams25430d02010-02-02 15:26:40 -080089
Jason Sams020bb7b2010-06-17 15:55:00 -070090 public void skip(int i) {
Stephen Hinese27832a2011-06-02 19:36:41 -070091 int res = mPos + i;
Shih-wei Liao6e667252011-06-05 00:51:54 -070092 if ((res < 0) || (res > mLen)) {
Stephen Hinese27832a2011-06-02 19:36:41 -070093 throw new RSIllegalArgumentException("out of range argument: " + i);
94 }
95 mPos = res;
Jason Sams020bb7b2010-06-17 15:55:00 -070096 }
97
Jason Samsa70f4162010-03-26 15:33:42 -070098 public void addI8(byte v) {
Jason Sams25430d02010-02-02 15:26:40 -080099 mData[mPos++] = v;
100 }
101
Tim Murray7c4caad2013-04-10 16:21:40 -0700102 public byte subI8() {
103 subalign(1);
104 return mData[--mPos];
105 }
106
Jason Samsa70f4162010-03-26 15:33:42 -0700107 public void addI16(short v) {
Jason Sams25430d02010-02-02 15:26:40 -0800108 align(2);
109 mData[mPos++] = (byte)(v & 0xff);
110 mData[mPos++] = (byte)(v >> 8);
111 }
112
Tim Murray7c4caad2013-04-10 16:21:40 -0700113 public short subI16() {
114 subalign(2);
115 short v = 0;
116 v = (short)((mData[--mPos] & 0xff) << 8);
117 v = (short)(v | (short)(mData[--mPos] & 0xff));
118 return v;
119 }
120
121
Jason Samsa70f4162010-03-26 15:33:42 -0700122 public void addI32(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800123 align(4);
124 mData[mPos++] = (byte)(v & 0xff);
125 mData[mPos++] = (byte)((v >> 8) & 0xff);
126 mData[mPos++] = (byte)((v >> 16) & 0xff);
127 mData[mPos++] = (byte)((v >> 24) & 0xff);
128 }
129
Tim Murray7c4caad2013-04-10 16:21:40 -0700130 public int subI32() {
131 subalign(4);
132 int v = 0;
133 v = ((mData[--mPos] & 0xff) << 24);
134 v = v | ((mData[--mPos] & 0xff) << 16);
135 v = v | ((mData[--mPos] & 0xff) << 8);
136 v = v | ((mData[--mPos] & 0xff));
137 return v;
138 }
139
140
Jason Samsa70f4162010-03-26 15:33:42 -0700141 public void addI64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800142 align(8);
143 mData[mPos++] = (byte)(v & 0xff);
144 mData[mPos++] = (byte)((v >> 8) & 0xff);
145 mData[mPos++] = (byte)((v >> 16) & 0xff);
146 mData[mPos++] = (byte)((v >> 24) & 0xff);
147 mData[mPos++] = (byte)((v >> 32) & 0xff);
148 mData[mPos++] = (byte)((v >> 40) & 0xff);
149 mData[mPos++] = (byte)((v >> 48) & 0xff);
150 mData[mPos++] = (byte)((v >> 56) & 0xff);
151 }
152
Tim Murray7c4caad2013-04-10 16:21:40 -0700153 public long subI64() {
154 subalign(8);
155 long v = 0;
156 byte x = 0;
157 x = ((mData[--mPos]));
158 v = (long)(v | (((long)x) & 0xff) << 56l);
159 x = ((mData[--mPos]));
160 v = (long)(v | (((long)x) & 0xff) << 48l);
161 x = ((mData[--mPos]));
162 v = (long)(v | (((long)x) & 0xff) << 40l);
163 x = ((mData[--mPos]));
164 v = (long)(v | (((long)x) & 0xff) << 32l);
165 x = ((mData[--mPos]));
166 v = (long)(v | (((long)x) & 0xff) << 24l);
167 x = ((mData[--mPos]));
168 v = (long)(v | (((long)x) & 0xff) << 16l);
169 x = ((mData[--mPos]));
170 v = (long)(v | (((long)x) & 0xff) << 8l);
171 x = ((mData[--mPos]));
172 v = (long)(v | (((long)x) & 0xff));
173 return v;
174 }
175
Jason Samsa70f4162010-03-26 15:33:42 -0700176 public void addU8(short v) {
Jason Sams25430d02010-02-02 15:26:40 -0800177 if ((v < 0) || (v > 0xff)) {
Jason Samsee734982010-08-12 12:44:02 -0700178 android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800179 throw new IllegalArgumentException("Saving value out of range for type");
180 }
181 mData[mPos++] = (byte)v;
182 }
183
Jason Samsa70f4162010-03-26 15:33:42 -0700184 public void addU16(int v) {
Jason Sams25430d02010-02-02 15:26:40 -0800185 if ((v < 0) || (v > 0xffff)) {
Jason Samsee734982010-08-12 12:44:02 -0700186 android.util.Log.e("rs", "FieldPacker.addU16( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800187 throw new IllegalArgumentException("Saving value out of range for type");
188 }
189 align(2);
190 mData[mPos++] = (byte)(v & 0xff);
191 mData[mPos++] = (byte)(v >> 8);
192 }
193
Jason Samsa70f4162010-03-26 15:33:42 -0700194 public void addU32(long v) {
Jason Samsee734982010-08-12 12:44:02 -0700195 if ((v < 0) || (v > 0xffffffffL)) {
196 android.util.Log.e("rs", "FieldPacker.addU32( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800197 throw new IllegalArgumentException("Saving value out of range for type");
198 }
199 align(4);
200 mData[mPos++] = (byte)(v & 0xff);
201 mData[mPos++] = (byte)((v >> 8) & 0xff);
202 mData[mPos++] = (byte)((v >> 16) & 0xff);
203 mData[mPos++] = (byte)((v >> 24) & 0xff);
204 }
205
Jason Samsa70f4162010-03-26 15:33:42 -0700206 public void addU64(long v) {
Jason Sams25430d02010-02-02 15:26:40 -0800207 if (v < 0) {
Jason Samsee734982010-08-12 12:44:02 -0700208 android.util.Log.e("rs", "FieldPacker.addU64( " + v + " )");
Jason Sams25430d02010-02-02 15:26:40 -0800209 throw new IllegalArgumentException("Saving value out of range for type");
210 }
211 align(8);
212 mData[mPos++] = (byte)(v & 0xff);
213 mData[mPos++] = (byte)((v >> 8) & 0xff);
214 mData[mPos++] = (byte)((v >> 16) & 0xff);
215 mData[mPos++] = (byte)((v >> 24) & 0xff);
216 mData[mPos++] = (byte)((v >> 32) & 0xff);
217 mData[mPos++] = (byte)((v >> 40) & 0xff);
218 mData[mPos++] = (byte)((v >> 48) & 0xff);
219 mData[mPos++] = (byte)((v >> 56) & 0xff);
220 }
221
Jason Samsa70f4162010-03-26 15:33:42 -0700222 public void addF32(float v) {
Jason Sams25430d02010-02-02 15:26:40 -0800223 addI32(Float.floatToRawIntBits(v));
224 }
225
Tim Murray7c4caad2013-04-10 16:21:40 -0700226 public float subF32() {
227 return Float.intBitsToFloat(subI32());
228 }
229
Stephen Hines02f417052010-09-30 15:19:22 -0700230 public void addF64(double v) {
Jason Sams25430d02010-02-02 15:26:40 -0800231 addI64(Double.doubleToRawLongBits(v));
232 }
233
Tim Murray7c4caad2013-04-10 16:21:40 -0700234 public double subF64() {
235 return Double.longBitsToDouble(subI64());
236 }
237
Jason Samsa70f4162010-03-26 15:33:42 -0700238 public void addObj(BaseObj obj) {
239 if (obj != null) {
Tim Murrayf0c62b22014-05-16 11:47:26 -0700240 if (RenderScript.sPointerSize == 8) {
241 addI64(obj.getID(null));
Tim Murray4154bbd2014-06-12 14:17:53 -0700242 addI64(0);
243 addI64(0);
244 addI64(0);
Tim Murrayf0c62b22014-05-16 11:47:26 -0700245 }
246 else {
247 addI32((int)obj.getID(null));
248 }
Jason Samsa70f4162010-03-26 15:33:42 -0700249 } else {
Tim Murrayf0c62b22014-05-16 11:47:26 -0700250 if (RenderScript.sPointerSize == 8) {
251 addI64(0);
Tim Murray4154bbd2014-06-12 14:17:53 -0700252 addI64(0);
253 addI64(0);
254 addI64(0);
Tim Murrayf0c62b22014-05-16 11:47:26 -0700255 } else {
256 addI32(0);
257 }
Jason Samsa70f4162010-03-26 15:33:42 -0700258 }
259 }
260
261 public void addF32(Float2 v) {
262 addF32(v.x);
263 addF32(v.y);
264 }
265 public void addF32(Float3 v) {
266 addF32(v.x);
267 addF32(v.y);
268 addF32(v.z);
269 }
270 public void addF32(Float4 v) {
271 addF32(v.x);
272 addF32(v.y);
273 addF32(v.z);
274 addF32(v.w);
275 }
276
Stephen Hines79ad3f22011-06-20 17:27:09 -0700277 public void addF64(Double2 v) {
278 addF64(v.x);
279 addF64(v.y);
280 }
281 public void addF64(Double3 v) {
282 addF64(v.x);
283 addF64(v.y);
284 addF64(v.z);
285 }
286 public void addF64(Double4 v) {
287 addF64(v.x);
288 addF64(v.y);
289 addF64(v.z);
290 addF64(v.w);
291 }
292
Jason Samsa70f4162010-03-26 15:33:42 -0700293 public void addI8(Byte2 v) {
294 addI8(v.x);
295 addI8(v.y);
296 }
297 public void addI8(Byte3 v) {
298 addI8(v.x);
299 addI8(v.y);
300 addI8(v.z);
301 }
302 public void addI8(Byte4 v) {
303 addI8(v.x);
304 addI8(v.y);
305 addI8(v.z);
306 addI8(v.w);
307 }
308
309 public void addU8(Short2 v) {
310 addU8(v.x);
311 addU8(v.y);
312 }
313 public void addU8(Short3 v) {
314 addU8(v.x);
315 addU8(v.y);
316 addU8(v.z);
317 }
318 public void addU8(Short4 v) {
319 addU8(v.x);
320 addU8(v.y);
321 addU8(v.z);
322 addU8(v.w);
323 }
324
325 public void addI16(Short2 v) {
326 addI16(v.x);
327 addI16(v.y);
328 }
329 public void addI16(Short3 v) {
330 addI16(v.x);
331 addI16(v.y);
332 addI16(v.z);
333 }
334 public void addI16(Short4 v) {
335 addI16(v.x);
336 addI16(v.y);
337 addI16(v.z);
338 addI16(v.w);
339 }
340
341 public void addU16(Int2 v) {
342 addU16(v.x);
343 addU16(v.y);
344 }
345 public void addU16(Int3 v) {
346 addU16(v.x);
347 addU16(v.y);
348 addU16(v.z);
349 }
350 public void addU16(Int4 v) {
351 addU16(v.x);
352 addU16(v.y);
353 addU16(v.z);
354 addU16(v.w);
355 }
356
357 public void addI32(Int2 v) {
358 addI32(v.x);
359 addI32(v.y);
360 }
361 public void addI32(Int3 v) {
362 addI32(v.x);
363 addI32(v.y);
364 addI32(v.z);
365 }
366 public void addI32(Int4 v) {
367 addI32(v.x);
368 addI32(v.y);
369 addI32(v.z);
370 addI32(v.w);
371 }
372
Stephen Hinese9f5c182011-01-20 18:17:25 -0800373 public void addU32(Long2 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700374 addU32(v.x);
375 addU32(v.y);
376 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800377 public void addU32(Long3 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700378 addU32(v.x);
379 addU32(v.y);
380 addU32(v.z);
381 }
Stephen Hinese9f5c182011-01-20 18:17:25 -0800382 public void addU32(Long4 v) {
Jason Samsa70f4162010-03-26 15:33:42 -0700383 addU32(v.x);
384 addU32(v.y);
385 addU32(v.z);
386 addU32(v.w);
387 }
388
Stephen Hines79ad3f22011-06-20 17:27:09 -0700389 public void addI64(Long2 v) {
390 addI64(v.x);
391 addI64(v.y);
392 }
393 public void addI64(Long3 v) {
394 addI64(v.x);
395 addI64(v.y);
396 addI64(v.z);
397 }
398 public void addI64(Long4 v) {
399 addI64(v.x);
400 addI64(v.y);
401 addI64(v.z);
402 addI64(v.w);
403 }
404
405 public void addU64(Long2 v) {
406 addU64(v.x);
407 addU64(v.y);
408 }
409 public void addU64(Long3 v) {
410 addU64(v.x);
411 addU64(v.y);
412 addU64(v.z);
413 }
414 public void addU64(Long4 v) {
415 addU64(v.x);
416 addU64(v.y);
417 addU64(v.z);
418 addU64(v.w);
419 }
420
Tim Murray7c4caad2013-04-10 16:21:40 -0700421
422 public Float2 subFloat2() {
423 Float2 v = new Float2();
424 v.y = subF32();
425 v.x = subF32();
426 return v;
427 }
428 public Float3 subFloat3() {
429 Float3 v = new Float3();
430 v.z = subF32();
431 v.y = subF32();
432 v.x = subF32();
433 return v;
434 }
435 public Float4 subFloat4() {
436 Float4 v = new Float4();
437 v.w = subF32();
438 v.z = subF32();
439 v.y = subF32();
440 v.x = subF32();
441 return v;
442 }
443
444 public Double2 subDouble2() {
445 Double2 v = new Double2();
446 v.y = subF64();
447 v.x = subF64();
448 return v;
449 }
450 public Double3 subDouble3() {
451 Double3 v = new Double3();
452 v.z = subF64();
453 v.y = subF64();
454 v.x = subF64();
455 return v;
456 }
457 public Double4 subDouble4() {
458 Double4 v = new Double4();
459 v.w = subF64();
460 v.z = subF64();
461 v.y = subF64();
462 v.x = subF64();
463 return v;
464 }
465
466 public Byte2 subByte2() {
467 Byte2 v = new Byte2();
468 v.y = subI8();
469 v.x = subI8();
470 return v;
471 }
472 public Byte3 subByte3() {
473 Byte3 v = new Byte3();
474 v.z = subI8();
475 v.y = subI8();
476 v.x = subI8();
477 return v;
478 }
479 public Byte4 subByte4() {
480 Byte4 v = new Byte4();
481 v.w = subI8();
482 v.z = subI8();
483 v.y = subI8();
484 v.x = subI8();
485 return v;
486 }
487
488 public Short2 subShort2() {
489 Short2 v = new Short2();
490 v.y = subI16();
491 v.x = subI16();
492 return v;
493 }
494 public Short3 subShort3() {
495 Short3 v = new Short3();
496 v.z = subI16();
497 v.y = subI16();
498 v.x = subI16();
499 return v;
500 }
501 public Short4 subShort4() {
502 Short4 v = new Short4();
503 v.w = subI16();
504 v.z = subI16();
505 v.y = subI16();
506 v.x = subI16();
507 return v;
508 }
509
510 public Int2 subInt2() {
511 Int2 v = new Int2();
512 v.y = subI32();
513 v.x = subI32();
514 return v;
515 }
516 public Int3 subInt3() {
517 Int3 v = new Int3();
518 v.z = subI32();
519 v.y = subI32();
520 v.x = subI32();
521 return v;
522 }
523 public Int4 subInt4() {
524 Int4 v = new Int4();
525 v.w = subI32();
526 v.z = subI32();
527 v.y = subI32();
528 v.x = subI32();
529 return v;
530 }
531
532 public Long2 subLong2() {
533 Long2 v = new Long2();
534 v.y = subI64();
535 v.x = subI64();
536 return v;
537 }
538 public Long3 subLong3() {
539 Long3 v = new Long3();
540 v.z = subI64();
541 v.y = subI64();
542 v.x = subI64();
543 return v;
544 }
545 public Long4 subLong4() {
546 Long4 v = new Long4();
547 v.w = subI64();
548 v.z = subI64();
549 v.y = subI64();
550 v.x = subI64();
551 return v;
552 }
553
554
555
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800556 public void addMatrix(Matrix4f v) {
557 for (int i=0; i < v.mMat.length; i++) {
558 addF32(v.mMat[i]);
559 }
560 }
561
Tim Murray7c4caad2013-04-10 16:21:40 -0700562 public Matrix4f subMatrix4f() {
563 Matrix4f v = new Matrix4f();
564 for (int i = v.mMat.length - 1; i >= 0; i--) {
565 v.mMat[i] = subF32();
566 }
567 return v;
568 }
569
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800570 public void addMatrix(Matrix3f v) {
571 for (int i=0; i < v.mMat.length; i++) {
572 addF32(v.mMat[i]);
573 }
574 }
575
Tim Murray7c4caad2013-04-10 16:21:40 -0700576 public Matrix3f subMatrix3f() {
577 Matrix3f v = new Matrix3f();
578 for (int i = v.mMat.length - 1; i >= 0; i--) {
579 v.mMat[i] = subF32();
580 }
581 return v;
582 }
583
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800584 public void addMatrix(Matrix2f v) {
585 for (int i=0; i < v.mMat.length; i++) {
586 addF32(v.mMat[i]);
587 }
588 }
589
Tim Murray7c4caad2013-04-10 16:21:40 -0700590 public Matrix2f subMatrix2f() {
591 Matrix2f v = new Matrix2f();
592 for (int i = v.mMat.length - 1; i >= 0; i--) {
593 v.mMat[i] = subF32();
594 }
595 return v;
596 }
597
Jason Samsfae3f6b2010-06-24 13:54:11 -0700598 public void addBoolean(boolean v) {
Jason Sams9e2b0c52010-06-21 18:30:02 -0700599 addI8((byte)(v ? 1 : 0));
Jason Samsf110d4b2010-06-21 17:42:41 -0700600 }
601
Tim Murray7c4caad2013-04-10 16:21:40 -0700602 public boolean subBoolean() {
603 byte v = subI8();
604 if (v == 1) {
605 return true;
606 }
607 return false;
608 }
609
Jason Samsa70f4162010-03-26 15:33:42 -0700610 public final byte[] getData() {
Jason Sams25430d02010-02-02 15:26:40 -0800611 return mData;
612 }
613
Stephen Hinesfa1275a2014-06-17 17:25:04 -0700614 /**
615 * Get the actual length used for the FieldPacker.
616 *
617 * @hide
618 */
619 public int getPos() {
620 return mPos;
621 }
622
Jason Sams25430d02010-02-02 15:26:40 -0800623 private final byte mData[];
624 private int mPos;
Stephen Hinese27832a2011-06-02 19:36:41 -0700625 private int mLen;
Tim Murray7c4caad2013-04-10 16:21:40 -0700626 private BitSet mAlignment;
Jason Sams25430d02010-02-02 15:26:40 -0800627
628}
629
630