blob: 0ec63b5815f1a634a61c96358c381d0752836aa1 [file] [log] [blame]
Andreas Huber9266f992016-08-25 11:21:21 -07001/*
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
Andreas Huber906a6792016-09-22 18:14:17 -070019import android.annotation.NonNull;
Steven Moreland4dde8a12018-01-10 15:45:36 -080020import android.annotation.SystemApi;
Steven Morelandcbba4c72019-01-11 10:19:51 -080021import android.annotation.TestApi;
Andreas Huber906a6792016-09-22 18:14:17 -070022
Andreas Huber9266f992016-08-25 11:21:21 -070023import libcore.util.NativeAllocationRegistry;
24
Steven Moreland4dde8a12018-01-10 15:45:36 -080025/**
26 * Represents fixed sized allocation of marshalled data used. Helper methods
27 * allow for access to the unmarshalled data in a variety of ways.
28 *
29 * @hide
30 */
31@SystemApi
Steven Morelandcbba4c72019-01-11 10:19:51 -080032@TestApi
Andreas Huber9266f992016-08-25 11:21:21 -070033public class HwBlob {
34 private static final String TAG = "HwBlob";
35
36 private static final NativeAllocationRegistry sNativeRegistry;
37
38 public HwBlob(int size) {
39 native_setup(size);
40
41 sNativeRegistry.registerNativeAllocation(
42 this,
43 mNativeContext);
44 }
45
Steven Moreland4dde8a12018-01-10 15:45:36 -080046 /**
47 * @param offset offset to unmarshall a boolean from
48 * @return the unmarshalled boolean value
49 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
50 */
Andreas Huber9266f992016-08-25 11:21:21 -070051 public native final boolean getBool(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080052 /**
53 * @param offset offset to unmarshall a byte from
54 * @return the unmarshalled byte value
55 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
56 */
Andreas Huber9266f992016-08-25 11:21:21 -070057 public native final byte getInt8(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080058 /**
59 * @param offset offset to unmarshall a short from
60 * @return the unmarshalled short value
61 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
62 */
Andreas Huber9266f992016-08-25 11:21:21 -070063 public native final short getInt16(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080064 /**
65 * @param offset offset to unmarshall an int from
66 * @return the unmarshalled int value
67 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
68 */
Andreas Huber9266f992016-08-25 11:21:21 -070069 public native final int getInt32(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080070 /**
71 * @param offset offset to unmarshall a long from
72 * @return the unmarshalled long value
73 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
74 */
Andreas Huber9266f992016-08-25 11:21:21 -070075 public native final long getInt64(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080076 /**
77 * @param offset offset to unmarshall a float from
78 * @return the unmarshalled float value
79 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
80 */
Andreas Huber9266f992016-08-25 11:21:21 -070081 public native final float getFloat(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080082 /**
83 * @param offset offset to unmarshall a double from
84 * @return the unmarshalled double value
85 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
86 */
Andreas Huber9266f992016-08-25 11:21:21 -070087 public native final double getDouble(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080088 /**
89 * @param offset offset to unmarshall a string from
90 * @return the unmarshalled string value
91 * @throws IndexOutOfBoundsException when offset is out of this HwBlob
92 */
Andreas Huber9266f992016-08-25 11:21:21 -070093 public native final String getString(long offset);
94
Andreas Huber0eb37e02017-10-31 11:51:50 -070095 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -080096 * Copy the blobs data starting from the given byte offset into the range, copying
97 * a total of size elements.
98 *
99 * @param offset starting location in blob
100 * @param array destination array
101 * @param size total number of elements to copy
102 * @throws IllegalArgumentException array.length < size
103 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jboolean)] out of the blob.
Andreas Huber0eb37e02017-10-31 11:51:50 -0700104 */
105 public native final void copyToBoolArray(long offset, boolean[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800106 /**
107 * Copy the blobs data starting from the given byte offset into the range, copying
108 * a total of size elements.
109 *
110 * @param offset starting location in blob
111 * @param array destination array
112 * @param size total number of elements to copy
113 * @throws IllegalArgumentException array.length < size
114 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jbyte)] out of the blob.
115 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700116 public native final void copyToInt8Array(long offset, byte[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800117 /**
118 * Copy the blobs data starting from the given byte offset into the range, copying
119 * a total of size elements.
120 *
121 * @param offset starting location in blob
122 * @param array destination array
123 * @param size total number of elements to copy
124 * @throws IllegalArgumentException array.length < size
125 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jshort)] out of the blob.
126 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700127 public native final void copyToInt16Array(long offset, short[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800128 /**
129 * Copy the blobs data starting from the given byte offset into the range, copying
130 * a total of size elements.
131 *
132 * @param offset starting location in blob
133 * @param array destination array
134 * @param size total number of elements to copy
135 * @throws IllegalArgumentException array.length < size
136 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jint)] out of the blob.
137 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700138 public native final void copyToInt32Array(long offset, int[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800139 /**
140 * Copy the blobs data starting from the given byte offset into the range, copying
141 * a total of size elements.
142 *
143 * @param offset starting location in blob
144 * @param array destination array
145 * @param size total number of elements to copy
146 * @throws IllegalArgumentException array.length < size
147 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jlong)] out of the blob.
148 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700149 public native final void copyToInt64Array(long offset, long[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800150 /**
151 * Copy the blobs data starting from the given byte offset into the range, copying
152 * a total of size elements.
153 *
154 * @param offset starting location in blob
155 * @param array destination array
156 * @param size total number of elements to copy
157 * @throws IllegalArgumentException array.length < size
158 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jfloat)] out of the blob.
159 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700160 public native final void copyToFloatArray(long offset, float[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800161 /**
162 * Copy the blobs data starting from the given byte offset into the range, copying
163 * a total of size elements.
164 *
165 * @param offset starting location in blob
166 * @param array destination array
167 * @param size total number of elements to copy
168 * @throws IllegalArgumentException array.length < size
169 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob.
170 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700171 public native final void copyToDoubleArray(long offset, double[] array, int size);
172
Steven Moreland4dde8a12018-01-10 15:45:36 -0800173 /**
174 * Writes a boolean value at an offset.
175 *
176 * @param offset location to write value
177 * @param x value to write
178 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jboolean)] is out of range
179 */
Andreas Huber9266f992016-08-25 11:21:21 -0700180 public native final void putBool(long offset, boolean x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800181 /**
182 * Writes a byte value at an offset.
183 *
184 * @param offset location to write value
185 * @param x value to write
186 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jbyte)] is out of range
187 */
Andreas Huber9266f992016-08-25 11:21:21 -0700188 public native final void putInt8(long offset, byte x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800189 /**
190 * Writes a short value at an offset.
191 *
192 * @param offset location to write value
193 * @param x value to write
194 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jshort)] is out of range
195 */
Andreas Huber9266f992016-08-25 11:21:21 -0700196 public native final void putInt16(long offset, short x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800197 /**
198 * Writes a int value at an offset.
199 *
200 * @param offset location to write value
201 * @param x value to write
202 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jint)] is out of range
203 */
Andreas Huber9266f992016-08-25 11:21:21 -0700204 public native final void putInt32(long offset, int x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800205 /**
206 * Writes a long value at an offset.
207 *
208 * @param offset location to write value
209 * @param x value to write
210 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jlong)] is out of range
211 */
Andreas Huber9266f992016-08-25 11:21:21 -0700212 public native final void putInt64(long offset, long x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800213 /**
214 * Writes a float value at an offset.
215 *
216 * @param offset location to write value
217 * @param x value to write
218 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jfloat)] is out of range
219 */
Andreas Huber9266f992016-08-25 11:21:21 -0700220 public native final void putFloat(long offset, float x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800221 /**
222 * Writes a double value at an offset.
223 *
224 * @param offset location to write value
225 * @param x value to write
226 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jdouble)] is out of range
227 */
Andreas Huber9266f992016-08-25 11:21:21 -0700228 public native final void putDouble(long offset, double x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800229 /**
230 * Writes a string value at an offset.
231 *
232 * @param offset location to write value
233 * @param x value to write
234 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jstring)] is out of range
235 */
Andreas Huber9266f992016-08-25 11:21:21 -0700236 public native final void putString(long offset, String x);
Nirav Atre9850dd92018-07-24 17:03:44 -0700237 /**
238 * Writes a native handle (without duplicating the underlying file descriptors) at an offset.
239 *
240 * @param offset location to write value
241 * @param x a {@link NativeHandle} instance to write
242 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jobject)] is out of range
243 */
244 public native final void putNativeHandle(long offset, NativeHandle x);
Andreas Huber9266f992016-08-25 11:21:21 -0700245
Steven Moreland4dde8a12018-01-10 15:45:36 -0800246 /**
247 * Put a boolean array contiguously at an offset in the blob.
248 *
249 * @param offset location to write values
250 * @param x array to write
251 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jboolean)] out of the blob.
252 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700253 public native final void putBoolArray(long offset, boolean[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800254 /**
255 * Put a byte array contiguously at an offset in the blob.
256 *
257 * @param offset location to write values
258 * @param x array to write
259 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jbyte)] out of the blob.
260 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700261 public native final void putInt8Array(long offset, byte[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800262 /**
263 * Put a short array contiguously at an offset in the blob.
264 *
265 * @param offset location to write values
266 * @param x array to write
267 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jshort)] out of the blob.
268 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700269 public native final void putInt16Array(long offset, short[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800270 /**
271 * Put a int array contiguously at an offset in the blob.
272 *
273 * @param offset location to write values
274 * @param x array to write
275 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jint)] out of the blob.
276 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700277 public native final void putInt32Array(long offset, int[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800278 /**
279 * Put a long array contiguously at an offset in the blob.
280 *
281 * @param offset location to write values
282 * @param x array to write
283 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jlong)] out of the blob.
284 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700285 public native final void putInt64Array(long offset, long[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800286 /**
287 * Put a float array contiguously at an offset in the blob.
288 *
289 * @param offset location to write values
290 * @param x array to write
291 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jfloat)] out of the blob.
292 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700293 public native final void putFloatArray(long offset, float[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800294 /**
295 * Put a double array contiguously at an offset in the blob.
296 *
297 * @param offset location to write values
298 * @param x array to write
299 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob.
300 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700301 public native final void putDoubleArray(long offset, double[] x);
302
Steven Moreland4dde8a12018-01-10 15:45:36 -0800303 /**
304 * Write another HwBlob into this blob at the specified location.
305 *
306 * @param offset location to write value
307 * @param blob data to write
308 * @throws IndexOutOfBoundsException if [offset, offset + blob's size] outside of the range of
309 * this blob.
310 */
Andreas Huber9266f992016-08-25 11:21:21 -0700311 public native final void putBlob(long offset, HwBlob blob);
312
Steven Moreland4dde8a12018-01-10 15:45:36 -0800313 /**
314 * @return current handle of HwBlob for reference in a parcelled binder transaction
315 */
Andreas Huber9266f992016-08-25 11:21:21 -0700316 public native final long handle();
317
Steven Moreland4dde8a12018-01-10 15:45:36 -0800318 /**
319 * Convert a primitive to a wrapped array for boolean.
320 *
321 * @param array from array
322 * @return transformed array
323 */
Andreas Huber906a6792016-09-22 18:14:17 -0700324 public static Boolean[] wrapArray(@NonNull boolean[] array) {
325 final int n = array.length;
326 Boolean[] wrappedArray = new Boolean[n];
327 for (int i = 0; i < n; ++i) {
328 wrappedArray[i] = array[i];
329 }
330 return wrappedArray;
331 }
332
Steven Moreland4dde8a12018-01-10 15:45:36 -0800333 /**
334 * Convert a primitive to a wrapped array for long.
335 *
336 * @param array from array
337 * @return transformed array
338 */
Andreas Huber906a6792016-09-22 18:14:17 -0700339 public static Long[] wrapArray(@NonNull long[] array) {
340 final int n = array.length;
341 Long[] wrappedArray = new Long[n];
342 for (int i = 0; i < n; ++i) {
343 wrappedArray[i] = array[i];
344 }
345 return wrappedArray;
346 }
347
Steven Moreland4dde8a12018-01-10 15:45:36 -0800348 /**
349 * Convert a primitive to a wrapped array for byte.
350 *
351 * @param array from array
352 * @return transformed array
353 */
Andreas Huber906a6792016-09-22 18:14:17 -0700354 public static Byte[] wrapArray(@NonNull byte[] array) {
355 final int n = array.length;
356 Byte[] wrappedArray = new Byte[n];
357 for (int i = 0; i < n; ++i) {
358 wrappedArray[i] = array[i];
359 }
360 return wrappedArray;
361 }
362
Steven Moreland4dde8a12018-01-10 15:45:36 -0800363 /**
364 * Convert a primitive to a wrapped array for short.
365 *
366 * @param array from array
367 * @return transformed array
368 */
Andreas Huber906a6792016-09-22 18:14:17 -0700369 public static Short[] wrapArray(@NonNull short[] array) {
370 final int n = array.length;
371 Short[] wrappedArray = new Short[n];
372 for (int i = 0; i < n; ++i) {
373 wrappedArray[i] = array[i];
374 }
375 return wrappedArray;
376 }
377
Steven Moreland4dde8a12018-01-10 15:45:36 -0800378 /**
379 * Convert a primitive to a wrapped array for int.
380 *
381 * @param array from array
382 * @return transformed array
383 */
Andreas Huber906a6792016-09-22 18:14:17 -0700384 public static Integer[] wrapArray(@NonNull int[] array) {
385 final int n = array.length;
386 Integer[] wrappedArray = new Integer[n];
387 for (int i = 0; i < n; ++i) {
388 wrappedArray[i] = array[i];
389 }
390 return wrappedArray;
391 }
392
Steven Moreland4dde8a12018-01-10 15:45:36 -0800393 /**
394 * Convert a primitive to a wrapped array for float.
395 *
396 * @param array from array
397 * @return transformed array
398 */
Andreas Huber906a6792016-09-22 18:14:17 -0700399 public static Float[] wrapArray(@NonNull float[] array) {
400 final int n = array.length;
401 Float[] wrappedArray = new Float[n];
402 for (int i = 0; i < n; ++i) {
403 wrappedArray[i] = array[i];
404 }
405 return wrappedArray;
406 }
407
Steven Moreland4dde8a12018-01-10 15:45:36 -0800408 /**
409 * Convert a primitive to a wrapped array for double.
410 *
411 * @param array from array
412 * @return transformed array
413 */
Andreas Huber906a6792016-09-22 18:14:17 -0700414 public static Double[] wrapArray(@NonNull double[] array) {
415 final int n = array.length;
416 Double[] wrappedArray = new Double[n];
417 for (int i = 0; i < n; ++i) {
418 wrappedArray[i] = array[i];
419 }
420 return wrappedArray;
421 }
422
Andreas Huber9266f992016-08-25 11:21:21 -0700423 // Returns address of the "freeFunction".
424 private static native final long native_init();
425
426 private native final void native_setup(int size);
427
428 static {
429 long freeFunction = native_init();
430
431 sNativeRegistry = new NativeAllocationRegistry(
432 HwBlob.class.getClassLoader(),
433 freeFunction,
434 128 /* size */);
435 }
436
437 private long mNativeContext;
438}
439
440