• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
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 package android.renderscript;
18 
19 import android.util.Log;
20 import java.util.BitSet;
21 
22 /**
23  * Utility class for packing arguments and structures from Android system objects to
24  * RenderScript objects.
25  *
26  * 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  *
30  **/
31 public class FieldPacker {
FieldPacker(int len)32     public FieldPacker(int len) {
33         mPos = 0;
34         mLen = len;
35         mData = new byte[len];
36         mAlignment = new BitSet();
37     }
38 
FieldPacker(byte[] data)39     public FieldPacker(byte[] data) {
40         mPos = 0;
41         mLen = data.length;
42         mData = data;
43         mAlignment = new BitSet();
44     }
45 
align(int v)46     public void align(int v) {
47         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 
51         while ((mPos & (v - 1)) != 0) {
52             mAlignment.flip(mPos);
53             mData[mPos++] = 0;
54         }
55     }
56 
subalign(int v)57     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 
reset()75     public void reset() {
76         mPos = 0;
77     }
reset(int i)78     public void reset(int i) {
79         if ((i < 0) || (i >= mLen)) {
80             throw new RSIllegalArgumentException("out of range argument: " + i);
81         }
82         mPos = i;
83     }
84 
skip(int i)85     public void skip(int i) {
86         int res = mPos + i;
87         if ((res < 0) || (res > mLen)) {
88             throw new RSIllegalArgumentException("out of range argument: " + i);
89         }
90         mPos = res;
91     }
92 
addI8(byte v)93     public void addI8(byte v) {
94         mData[mPos++] = v;
95     }
96 
subI8()97     public byte subI8() {
98         subalign(1);
99         return mData[--mPos];
100     }
101 
addI16(short v)102     public void addI16(short v) {
103         align(2);
104         mData[mPos++] = (byte)(v & 0xff);
105         mData[mPos++] = (byte)(v >> 8);
106     }
107 
subI16()108     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 
addI32(int v)117     public void addI32(int v) {
118         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 
subI32()125     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 
addI64(long v)136     public void addI64(long v) {
137         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 
subI64()148     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 
addU8(short v)171     public void addU8(short v) {
172         if ((v < 0) || (v > 0xff)) {
173             android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )");
174             throw new IllegalArgumentException("Saving value out of range for type");
175         }
176         mData[mPos++] = (byte)v;
177     }
178 
addU16(int v)179     public void addU16(int v) {
180         if ((v < 0) || (v > 0xffff)) {
181             android.util.Log.e("rs", "FieldPacker.addU16( " + v + " )");
182             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 
addU32(long v)189     public void addU32(long v) {
190         if ((v < 0) || (v > 0xffffffffL)) {
191             android.util.Log.e("rs", "FieldPacker.addU32( " + v + " )");
192             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 
addU64(long v)201     public void addU64(long v) {
202         if (v < 0) {
203             android.util.Log.e("rs", "FieldPacker.addU64( " + v + " )");
204             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 
addF32(float v)217     public void addF32(float v) {
218         addI32(Float.floatToRawIntBits(v));
219     }
220 
subF32()221     public float subF32() {
222         return Float.intBitsToFloat(subI32());
223     }
224 
addF64(double v)225     public void addF64(double v) {
226         addI64(Double.doubleToRawLongBits(v));
227     }
228 
subF64()229     public double subF64() {
230         return Double.longBitsToDouble(subI64());
231     }
232 
addObj(BaseObj obj)233     public void addObj(BaseObj obj) {
234         if (obj != null) {
235             addI32(obj.getID(null));
236         } else {
237             addI32(0);
238         }
239     }
240 
addF32(Float2 v)241     public void addF32(Float2 v) {
242         addF32(v.x);
243         addF32(v.y);
244     }
addF32(Float3 v)245     public void addF32(Float3 v) {
246         addF32(v.x);
247         addF32(v.y);
248         addF32(v.z);
249     }
addF32(Float4 v)250     public void addF32(Float4 v) {
251         addF32(v.x);
252         addF32(v.y);
253         addF32(v.z);
254         addF32(v.w);
255     }
256 
addF64(Double2 v)257     public void addF64(Double2 v) {
258         addF64(v.x);
259         addF64(v.y);
260     }
addF64(Double3 v)261     public void addF64(Double3 v) {
262         addF64(v.x);
263         addF64(v.y);
264         addF64(v.z);
265     }
addF64(Double4 v)266     public void addF64(Double4 v) {
267         addF64(v.x);
268         addF64(v.y);
269         addF64(v.z);
270         addF64(v.w);
271     }
272 
addI8(Byte2 v)273     public void addI8(Byte2 v) {
274         addI8(v.x);
275         addI8(v.y);
276     }
addI8(Byte3 v)277     public void addI8(Byte3 v) {
278         addI8(v.x);
279         addI8(v.y);
280         addI8(v.z);
281     }
addI8(Byte4 v)282     public void addI8(Byte4 v) {
283         addI8(v.x);
284         addI8(v.y);
285         addI8(v.z);
286         addI8(v.w);
287     }
288 
addU8(Short2 v)289     public void addU8(Short2 v) {
290         addU8(v.x);
291         addU8(v.y);
292     }
addU8(Short3 v)293     public void addU8(Short3 v) {
294         addU8(v.x);
295         addU8(v.y);
296         addU8(v.z);
297     }
addU8(Short4 v)298     public void addU8(Short4 v) {
299         addU8(v.x);
300         addU8(v.y);
301         addU8(v.z);
302         addU8(v.w);
303     }
304 
addI16(Short2 v)305     public void addI16(Short2 v) {
306         addI16(v.x);
307         addI16(v.y);
308     }
addI16(Short3 v)309     public void addI16(Short3 v) {
310         addI16(v.x);
311         addI16(v.y);
312         addI16(v.z);
313     }
addI16(Short4 v)314     public void addI16(Short4 v) {
315         addI16(v.x);
316         addI16(v.y);
317         addI16(v.z);
318         addI16(v.w);
319     }
320 
addU16(Int2 v)321     public void addU16(Int2 v) {
322         addU16(v.x);
323         addU16(v.y);
324     }
addU16(Int3 v)325     public void addU16(Int3 v) {
326         addU16(v.x);
327         addU16(v.y);
328         addU16(v.z);
329     }
addU16(Int4 v)330     public void addU16(Int4 v) {
331         addU16(v.x);
332         addU16(v.y);
333         addU16(v.z);
334         addU16(v.w);
335     }
336 
addI32(Int2 v)337     public void addI32(Int2 v) {
338         addI32(v.x);
339         addI32(v.y);
340     }
addI32(Int3 v)341     public void addI32(Int3 v) {
342         addI32(v.x);
343         addI32(v.y);
344         addI32(v.z);
345     }
addI32(Int4 v)346     public void addI32(Int4 v) {
347         addI32(v.x);
348         addI32(v.y);
349         addI32(v.z);
350         addI32(v.w);
351     }
352 
addU32(Long2 v)353     public void addU32(Long2 v) {
354         addU32(v.x);
355         addU32(v.y);
356     }
addU32(Long3 v)357     public void addU32(Long3 v) {
358         addU32(v.x);
359         addU32(v.y);
360         addU32(v.z);
361     }
addU32(Long4 v)362     public void addU32(Long4 v) {
363         addU32(v.x);
364         addU32(v.y);
365         addU32(v.z);
366         addU32(v.w);
367     }
368 
addI64(Long2 v)369     public void addI64(Long2 v) {
370         addI64(v.x);
371         addI64(v.y);
372     }
addI64(Long3 v)373     public void addI64(Long3 v) {
374         addI64(v.x);
375         addI64(v.y);
376         addI64(v.z);
377     }
addI64(Long4 v)378     public void addI64(Long4 v) {
379         addI64(v.x);
380         addI64(v.y);
381         addI64(v.z);
382         addI64(v.w);
383     }
384 
addU64(Long2 v)385     public void addU64(Long2 v) {
386         addU64(v.x);
387         addU64(v.y);
388     }
addU64(Long3 v)389     public void addU64(Long3 v) {
390         addU64(v.x);
391         addU64(v.y);
392         addU64(v.z);
393     }
addU64(Long4 v)394     public void addU64(Long4 v) {
395         addU64(v.x);
396         addU64(v.y);
397         addU64(v.z);
398         addU64(v.w);
399     }
400 
401 
subFloat2()402     public Float2 subFloat2() {
403         Float2 v = new Float2();
404         v.y = subF32();
405         v.x = subF32();
406         return v;
407     }
subFloat3()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     }
subFloat4()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 
subDouble2()424     public Double2 subDouble2() {
425         Double2 v = new Double2();
426         v.y = subF64();
427         v.x = subF64();
428         return v;
429     }
subDouble3()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     }
subDouble4()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 
subByte2()446     public Byte2 subByte2() {
447         Byte2 v = new Byte2();
448         v.y = subI8();
449         v.x = subI8();
450         return v;
451     }
subByte3()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     }
subByte4()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 
subShort2()468     public Short2 subShort2() {
469         Short2 v = new Short2();
470         v.y = subI16();
471         v.x = subI16();
472         return v;
473     }
subShort3()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     }
subShort4()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 
subInt2()490     public Int2 subInt2() {
491         Int2 v = new Int2();
492         v.y = subI32();
493         v.x = subI32();
494         return v;
495     }
subInt3()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     }
subInt4()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 
subLong2()512     public Long2 subLong2() {
513         Long2 v = new Long2();
514         v.y = subI64();
515         v.x = subI64();
516         return v;
517     }
subLong3()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     }
subLong4()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 
addMatrix(Matrix4f v)536     public void addMatrix(Matrix4f v) {
537         for (int i=0; i < v.mMat.length; i++) {
538             addF32(v.mMat[i]);
539         }
540     }
541 
subMatrix4f()542     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 
addMatrix(Matrix3f v)550     public void addMatrix(Matrix3f v) {
551         for (int i=0; i < v.mMat.length; i++) {
552             addF32(v.mMat[i]);
553         }
554     }
555 
subMatrix3f()556     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 
addMatrix(Matrix2f v)564     public void addMatrix(Matrix2f v) {
565         for (int i=0; i < v.mMat.length; i++) {
566             addF32(v.mMat[i]);
567         }
568     }
569 
subMatrix2f()570     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 
addBoolean(boolean v)578     public void addBoolean(boolean v) {
579         addI8((byte)(v ? 1 : 0));
580     }
581 
subBoolean()582     public boolean subBoolean() {
583         byte v = subI8();
584         if (v == 1) {
585             return true;
586         }
587         return false;
588     }
589 
getData()590     public final byte[] getData() {
591         return mData;
592     }
593 
594     private final byte mData[];
595     private int mPos;
596     private int mLen;
597     private BitSet mAlignment;
598 
599 }
600 
601 
602