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