1 /*
2  * Copyright (C) 2016 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.os;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.annotation.TestApi;
23 
24 import libcore.util.NativeAllocationRegistry;
25 
26 /**
27  * Represents fixed sized allocation of marshalled data used. Helper methods
28  * allow for access to the unmarshalled data in a variety of ways.
29  *
30  * @hide
31  */
32 @SystemApi
33 @TestApi
34 public class HwBlob {
35     private static final String TAG = "HwBlob";
36 
37     private static final NativeAllocationRegistry sNativeRegistry;
38 
HwBlob(int size)39     public HwBlob(int size) {
40         native_setup(size);
41 
42         sNativeRegistry.registerNativeAllocation(
43                 this,
44                 mNativeContext);
45     }
46 
47     /**
48      * @param offset offset to unmarshall a boolean from
49      * @return the unmarshalled boolean value
50      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
51      */
getBool(long offset)52     public native final boolean getBool(long offset);
53     /**
54      * @param offset offset to unmarshall a byte from
55      * @return the unmarshalled byte value
56      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
57      */
getInt8(long offset)58     public native final byte getInt8(long offset);
59     /**
60      * @param offset offset to unmarshall a short from
61      * @return the unmarshalled short value
62      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
63      */
getInt16(long offset)64     public native final short getInt16(long offset);
65     /**
66      * @param offset offset to unmarshall an int from
67      * @return the unmarshalled int value
68      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
69      */
getInt32(long offset)70     public native final int getInt32(long offset);
71     /**
72      * @param offset offset to unmarshall a long from
73      * @return the unmarshalled long value
74      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
75      */
getInt64(long offset)76     public native final long getInt64(long offset);
77     /**
78      * @param offset offset to unmarshall a float from
79      * @return the unmarshalled float value
80      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
81      */
getFloat(long offset)82     public native final float getFloat(long offset);
83     /**
84      * @param offset offset to unmarshall a double from
85      * @return the unmarshalled double value
86      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
87      */
getDouble(long offset)88     public native final double getDouble(long offset);
89     /**
90      * @param offset offset to unmarshall a string from
91      * @return the unmarshalled string value
92      * @throws IndexOutOfBoundsException when offset is out of this HwBlob
93      */
getString(long offset)94     public native final String getString(long offset);
95 
96     /**
97      * Copy the blobs data starting from the given byte offset into the range, copying
98      * a total of size elements.
99      *
100      * @param offset starting location in blob
101      * @param array destination array
102      * @param size total number of elements to copy
103      * @throws IllegalArgumentException array.length < size
104      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jboolean)] out of the blob.
105      */
copyToBoolArray(long offset, boolean[] array, int size)106     public native final void copyToBoolArray(long offset, boolean[] array, int size);
107     /**
108      * Copy the blobs data starting from the given byte offset into the range, copying
109      * a total of size elements.
110      *
111      * @param offset starting location in blob
112      * @param array destination array
113      * @param size total number of elements to copy
114      * @throws IllegalArgumentException array.length < size
115      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jbyte)] out of the blob.
116      */
copyToInt8Array(long offset, byte[] array, int size)117     public native final void copyToInt8Array(long offset, byte[] array, int size);
118     /**
119      * Copy the blobs data starting from the given byte offset into the range, copying
120      * a total of size elements.
121      *
122      * @param offset starting location in blob
123      * @param array destination array
124      * @param size total number of elements to copy
125      * @throws IllegalArgumentException array.length < size
126      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jshort)] out of the blob.
127      */
copyToInt16Array(long offset, short[] array, int size)128     public native final void copyToInt16Array(long offset, short[] array, int size);
129     /**
130      * Copy the blobs data starting from the given byte offset into the range, copying
131      * a total of size elements.
132      *
133      * @param offset starting location in blob
134      * @param array destination array
135      * @param size total number of elements to copy
136      * @throws IllegalArgumentException array.length < size
137      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jint)] out of the blob.
138      */
copyToInt32Array(long offset, int[] array, int size)139     public native final void copyToInt32Array(long offset, int[] array, int size);
140     /**
141      * Copy the blobs data starting from the given byte offset into the range, copying
142      * a total of size elements.
143      *
144      * @param offset starting location in blob
145      * @param array destination array
146      * @param size total number of elements to copy
147      * @throws IllegalArgumentException array.length < size
148      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jlong)] out of the blob.
149      */
copyToInt64Array(long offset, long[] array, int size)150     public native final void copyToInt64Array(long offset, long[] array, int size);
151     /**
152      * Copy the blobs data starting from the given byte offset into the range, copying
153      * a total of size elements.
154      *
155      * @param offset starting location in blob
156      * @param array destination array
157      * @param size total number of elements to copy
158      * @throws IllegalArgumentException array.length < size
159      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jfloat)] out of the blob.
160      */
copyToFloatArray(long offset, float[] array, int size)161     public native final void copyToFloatArray(long offset, float[] array, int size);
162     /**
163      * Copy the blobs data starting from the given byte offset into the range, copying
164      * a total of size elements.
165      *
166      * @param offset starting location in blob
167      * @param array destination array
168      * @param size total number of elements to copy
169      * @throws IllegalArgumentException array.length < size
170      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob.
171      */
copyToDoubleArray(long offset, double[] array, int size)172     public native final void copyToDoubleArray(long offset, double[] array, int size);
173 
174     /**
175      * Writes a boolean value at an offset.
176      *
177      * @param offset location to write value
178      * @param x value to write
179      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jboolean)] is out of range
180      */
putBool(long offset, boolean x)181     public native final void putBool(long offset, boolean x);
182     /**
183      * Writes a byte value at an offset.
184      *
185      * @param offset location to write value
186      * @param x value to write
187      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jbyte)] is out of range
188      */
putInt8(long offset, byte x)189     public native final void putInt8(long offset, byte x);
190     /**
191      * Writes a short value at an offset.
192      *
193      * @param offset location to write value
194      * @param x value to write
195      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jshort)] is out of range
196      */
putInt16(long offset, short x)197     public native final void putInt16(long offset, short x);
198     /**
199      * Writes a int value at an offset.
200      *
201      * @param offset location to write value
202      * @param x value to write
203      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jint)] is out of range
204      */
putInt32(long offset, int x)205     public native final void putInt32(long offset, int x);
206     /**
207      * Writes a long value at an offset.
208      *
209      * @param offset location to write value
210      * @param x value to write
211      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jlong)] is out of range
212      */
putInt64(long offset, long x)213     public native final void putInt64(long offset, long x);
214     /**
215      * Writes a float value at an offset.
216      *
217      * @param offset location to write value
218      * @param x value to write
219      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jfloat)] is out of range
220      */
putFloat(long offset, float x)221     public native final void putFloat(long offset, float x);
222     /**
223      * Writes a double value at an offset.
224      *
225      * @param offset location to write value
226      * @param x value to write
227      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jdouble)] is out of range
228      */
putDouble(long offset, double x)229     public native final void putDouble(long offset, double x);
230     /**
231      * Writes a string value at an offset.
232      *
233      * @param offset location to write value
234      * @param x value to write
235      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jstring)] is out of range
236      */
putString(long offset, String x)237     public native final void putString(long offset, String x);
238     /**
239      * Writes a native handle (without duplicating the underlying file descriptors) at an offset.
240      *
241      * @param offset location to write value
242      * @param x a {@link NativeHandle} instance to write
243      * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jobject)] is out of range
244      */
putNativeHandle(long offset, @Nullable NativeHandle x)245     public native final void putNativeHandle(long offset, @Nullable NativeHandle x);
246 
247     /**
248      * Put a boolean array contiguously at an offset in the blob.
249      *
250      * @param offset location to write values
251      * @param x array to write
252      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jboolean)] out of the blob.
253      */
putBoolArray(long offset, boolean[] x)254     public native final void putBoolArray(long offset, boolean[] x);
255     /**
256      * Put a byte array contiguously at an offset in the blob.
257      *
258      * @param offset location to write values
259      * @param x array to write
260      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jbyte)] out of the blob.
261      */
putInt8Array(long offset, byte[] x)262     public native final void putInt8Array(long offset, byte[] x);
263     /**
264      * Put a short array contiguously at an offset in the blob.
265      *
266      * @param offset location to write values
267      * @param x array to write
268      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jshort)] out of the blob.
269      */
putInt16Array(long offset, short[] x)270     public native final void putInt16Array(long offset, short[] x);
271     /**
272      * Put a int array contiguously at an offset in the blob.
273      *
274      * @param offset location to write values
275      * @param x array to write
276      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jint)] out of the blob.
277      */
putInt32Array(long offset, int[] x)278     public native final void putInt32Array(long offset, int[] x);
279     /**
280      * Put a long array contiguously at an offset in the blob.
281      *
282      * @param offset location to write values
283      * @param x array to write
284      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jlong)] out of the blob.
285      */
putInt64Array(long offset, long[] x)286     public native final void putInt64Array(long offset, long[] x);
287     /**
288      * Put a float array contiguously at an offset in the blob.
289      *
290      * @param offset location to write values
291      * @param x array to write
292      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jfloat)] out of the blob.
293      */
putFloatArray(long offset, float[] x)294     public native final void putFloatArray(long offset, float[] x);
295     /**
296      * Put a double array contiguously at an offset in the blob.
297      *
298      * @param offset location to write values
299      * @param x array to write
300      * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob.
301      */
putDoubleArray(long offset, double[] x)302     public native final void putDoubleArray(long offset, double[] x);
303 
304     /**
305      * Write another HwBlob into this blob at the specified location.
306      *
307      * @param offset location to write value
308      * @param blob data to write
309      * @throws IndexOutOfBoundsException if [offset, offset + blob's size] outside of the range of
310      *     this blob.
311      */
putBlob(long offset, HwBlob blob)312     public native final void putBlob(long offset, HwBlob blob);
313 
314     /**
315      * @return current handle of HwBlob for reference in a parcelled binder transaction
316      */
handle()317     public native final long handle();
318 
319     /**
320      * Convert a primitive to a wrapped array for boolean.
321      *
322      * @param array from array
323      * @return transformed array
324      */
wrapArray(@onNull boolean[] array)325     public static Boolean[] wrapArray(@NonNull boolean[] array) {
326         final int n = array.length;
327         Boolean[] wrappedArray = new Boolean[n];
328         for (int i = 0; i < n; ++i) {
329           wrappedArray[i] = array[i];
330         }
331         return wrappedArray;
332     }
333 
334     /**
335      * Convert a primitive to a wrapped array for long.
336      *
337      * @param array from array
338      * @return transformed array
339      */
wrapArray(@onNull long[] array)340     public static Long[] wrapArray(@NonNull long[] array) {
341         final int n = array.length;
342         Long[] wrappedArray = new Long[n];
343         for (int i = 0; i < n; ++i) {
344           wrappedArray[i] = array[i];
345         }
346         return wrappedArray;
347     }
348 
349     /**
350      * Convert a primitive to a wrapped array for byte.
351      *
352      * @param array from array
353      * @return transformed array
354      */
wrapArray(@onNull byte[] array)355     public static Byte[] wrapArray(@NonNull byte[] array) {
356         final int n = array.length;
357         Byte[] wrappedArray = new Byte[n];
358         for (int i = 0; i < n; ++i) {
359           wrappedArray[i] = array[i];
360         }
361         return wrappedArray;
362     }
363 
364     /**
365      * Convert a primitive to a wrapped array for short.
366      *
367      * @param array from array
368      * @return transformed array
369      */
wrapArray(@onNull short[] array)370     public static Short[] wrapArray(@NonNull short[] array) {
371         final int n = array.length;
372         Short[] wrappedArray = new Short[n];
373         for (int i = 0; i < n; ++i) {
374           wrappedArray[i] = array[i];
375         }
376         return wrappedArray;
377     }
378 
379     /**
380      * Convert a primitive to a wrapped array for int.
381      *
382      * @param array from array
383      * @return transformed array
384      */
wrapArray(@onNull int[] array)385     public static Integer[] wrapArray(@NonNull int[] array) {
386         final int n = array.length;
387         Integer[] wrappedArray = new Integer[n];
388         for (int i = 0; i < n; ++i) {
389           wrappedArray[i] = array[i];
390         }
391         return wrappedArray;
392     }
393 
394     /**
395      * Convert a primitive to a wrapped array for float.
396      *
397      * @param array from array
398      * @return transformed array
399      */
wrapArray(@onNull float[] array)400     public static Float[] wrapArray(@NonNull float[] array) {
401         final int n = array.length;
402         Float[] wrappedArray = new Float[n];
403         for (int i = 0; i < n; ++i) {
404           wrappedArray[i] = array[i];
405         }
406         return wrappedArray;
407     }
408 
409     /**
410      * Convert a primitive to a wrapped array for double.
411      *
412      * @param array from array
413      * @return transformed array
414      */
wrapArray(@onNull double[] array)415     public static Double[] wrapArray(@NonNull double[] array) {
416         final int n = array.length;
417         Double[] wrappedArray = new Double[n];
418         for (int i = 0; i < n; ++i) {
419           wrappedArray[i] = array[i];
420         }
421         return wrappedArray;
422     }
423 
424     // Returns address of the "freeFunction".
native_init()425     private static native final long native_init();
426 
native_setup(int size)427     private native final void native_setup(int size);
428 
429     static {
430         long freeFunction = native_init();
431 
432         sNativeRegistry = new NativeAllocationRegistry(
433                 HwBlob.class.getClassLoader(),
434                 freeFunction,
435                 128 /* size */);
436     }
437 
438     private long mNativeContext;
439 }
440 
441 
442