blob: 2c453bfc4d42946c96f4748020e0b733dda83426 [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 Moreland0ff061a2019-03-04 17:56:30 -080020import android.annotation.Nullable;
Steven Moreland4dde8a12018-01-10 15:45:36 -080021import android.annotation.SystemApi;
Steven Moreland14b9eb62019-01-11 10:19:51 -080022import android.annotation.TestApi;
Andreas Huber906a6792016-09-22 18:14:17 -070023
Andreas Huber9266f992016-08-25 11:21:21 -070024import libcore.util.NativeAllocationRegistry;
25
Steven Moreland4dde8a12018-01-10 15:45:36 -080026/**
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
Steven Moreland14b9eb62019-01-11 10:19:51 -080033@TestApi
Andreas Huber9266f992016-08-25 11:21:21 -070034public class HwBlob {
35 private static final String TAG = "HwBlob";
36
37 private static final NativeAllocationRegistry sNativeRegistry;
38
39 public HwBlob(int size) {
40 native_setup(size);
41
42 sNativeRegistry.registerNativeAllocation(
43 this,
44 mNativeContext);
45 }
46
Steven Moreland4dde8a12018-01-10 15:45:36 -080047 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070052 public native final boolean getBool(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080053 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070058 public native final byte getInt8(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080059 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070064 public native final short getInt16(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080065 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070070 public native final int getInt32(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080071 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070076 public native final long getInt64(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080077 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070082 public native final float getFloat(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080083 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070088 public native final double getDouble(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080089 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070094 public native final String getString(long offset);
95
Andreas Huber0eb37e02017-10-31 11:51:50 -070096 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -080097 * 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.
Andreas Huber0eb37e02017-10-31 11:51:50 -0700105 */
106 public native final void copyToBoolArray(long offset, boolean[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800107 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700117 public native final void copyToInt8Array(long offset, byte[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800118 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700128 public native final void copyToInt16Array(long offset, short[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800129 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700139 public native final void copyToInt32Array(long offset, int[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800140 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700150 public native final void copyToInt64Array(long offset, long[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800151 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700161 public native final void copyToFloatArray(long offset, float[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800162 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700172 public native final void copyToDoubleArray(long offset, double[] array, int size);
173
Steven Moreland4dde8a12018-01-10 15:45:36 -0800174 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700181 public native final void putBool(long offset, boolean x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800182 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700189 public native final void putInt8(long offset, byte x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800190 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700197 public native final void putInt16(long offset, short x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800198 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700205 public native final void putInt32(long offset, int x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800206 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700213 public native final void putInt64(long offset, long x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800214 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700221 public native final void putFloat(long offset, float x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800222 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700229 public native final void putDouble(long offset, double x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800230 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700237 public native final void putString(long offset, String x);
Nirav Atre9850dd92018-07-24 17:03:44 -0700238 /**
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 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800245 public native final void putNativeHandle(long offset, @Nullable NativeHandle x);
Andreas Huber9266f992016-08-25 11:21:21 -0700246
Steven Moreland4dde8a12018-01-10 15:45:36 -0800247 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700254 public native final void putBoolArray(long offset, boolean[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800255 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700262 public native final void putInt8Array(long offset, byte[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800263 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700270 public native final void putInt16Array(long offset, short[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800271 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700278 public native final void putInt32Array(long offset, int[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800279 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700286 public native final void putInt64Array(long offset, long[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800287 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700294 public native final void putFloatArray(long offset, float[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800295 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700302 public native final void putDoubleArray(long offset, double[] x);
303
Steven Moreland4dde8a12018-01-10 15:45:36 -0800304 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700312 public native final void putBlob(long offset, HwBlob blob);
313
Steven Moreland4dde8a12018-01-10 15:45:36 -0800314 /**
315 * @return current handle of HwBlob for reference in a parcelled binder transaction
316 */
Andreas Huber9266f992016-08-25 11:21:21 -0700317 public native final long handle();
318
Steven Moreland4dde8a12018-01-10 15:45:36 -0800319 /**
320 * Convert a primitive to a wrapped array for boolean.
321 *
322 * @param array from array
323 * @return transformed array
324 */
Andreas Huber906a6792016-09-22 18:14:17 -0700325 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800334 /**
335 * Convert a primitive to a wrapped array for long.
336 *
337 * @param array from array
338 * @return transformed array
339 */
Andreas Huber906a6792016-09-22 18:14:17 -0700340 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800349 /**
350 * Convert a primitive to a wrapped array for byte.
351 *
352 * @param array from array
353 * @return transformed array
354 */
Andreas Huber906a6792016-09-22 18:14:17 -0700355 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800364 /**
365 * Convert a primitive to a wrapped array for short.
366 *
367 * @param array from array
368 * @return transformed array
369 */
Andreas Huber906a6792016-09-22 18:14:17 -0700370 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800379 /**
380 * Convert a primitive to a wrapped array for int.
381 *
382 * @param array from array
383 * @return transformed array
384 */
Andreas Huber906a6792016-09-22 18:14:17 -0700385 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800394 /**
395 * Convert a primitive to a wrapped array for float.
396 *
397 * @param array from array
398 * @return transformed array
399 */
Andreas Huber906a6792016-09-22 18:14:17 -0700400 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800409 /**
410 * Convert a primitive to a wrapped array for double.
411 *
412 * @param array from array
413 * @return transformed array
414 */
Andreas Huber906a6792016-09-22 18:14:17 -0700415 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
Andreas Huber9266f992016-08-25 11:21:21 -0700424 // Returns address of the "freeFunction".
425 private static native final long native_init();
426
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