blob: 154227b2a7864596556e120eecb8c8a0dbe6fd40 [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);
Ytai Ben-Tsvi4659e182019-11-06 09:53:34 -080095 /**
96 * For embedded fields that follow a two-step approach for reading, first obtain their field
97 * handle using this method, and pass that field handle to the respective
98 * HwParcel.readEmbedded*() method.
99 * @param offset The field offset.
100 * @return The field handle.
101 */
102 public native final long getFieldHandle(long offset);
Andreas Huber9266f992016-08-25 11:21:21 -0700103
Andreas Huber0eb37e02017-10-31 11:51:50 -0700104 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800105 * 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(jboolean)] out of the blob.
Andreas Huber0eb37e02017-10-31 11:51:50 -0700113 */
114 public native final void copyToBoolArray(long offset, boolean[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800115 /**
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(jbyte)] out of the blob.
124 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700125 public native final void copyToInt8Array(long offset, byte[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800126 /**
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(jshort)] out of the blob.
135 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700136 public native final void copyToInt16Array(long offset, short[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800137 /**
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(jint)] out of the blob.
146 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700147 public native final void copyToInt32Array(long offset, int[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800148 /**
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(jlong)] out of the blob.
157 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700158 public native final void copyToInt64Array(long offset, long[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800159 /**
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(jfloat)] out of the blob.
168 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700169 public native final void copyToFloatArray(long offset, float[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800170 /**
171 * Copy the blobs data starting from the given byte offset into the range, copying
172 * a total of size elements.
173 *
174 * @param offset starting location in blob
175 * @param array destination array
176 * @param size total number of elements to copy
177 * @throws IllegalArgumentException array.length < size
178 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob.
179 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700180 public native final void copyToDoubleArray(long offset, double[] array, int size);
181
Steven Moreland4dde8a12018-01-10 15:45:36 -0800182 /**
183 * Writes a boolean 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(jboolean)] is out of range
188 */
Andreas Huber9266f992016-08-25 11:21:21 -0700189 public native final void putBool(long offset, boolean x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800190 /**
191 * Writes a byte 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(jbyte)] is out of range
196 */
Andreas Huber9266f992016-08-25 11:21:21 -0700197 public native final void putInt8(long offset, byte x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800198 /**
199 * Writes a short 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(jshort)] is out of range
204 */
Andreas Huber9266f992016-08-25 11:21:21 -0700205 public native final void putInt16(long offset, short x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800206 /**
207 * Writes a int 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(jint)] is out of range
212 */
Andreas Huber9266f992016-08-25 11:21:21 -0700213 public native final void putInt32(long offset, int x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800214 /**
215 * Writes a long 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(jlong)] is out of range
220 */
Andreas Huber9266f992016-08-25 11:21:21 -0700221 public native final void putInt64(long offset, long x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800222 /**
223 * Writes a float 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(jfloat)] is out of range
228 */
Andreas Huber9266f992016-08-25 11:21:21 -0700229 public native final void putFloat(long offset, float x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800230 /**
231 * Writes a double 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(jdouble)] is out of range
236 */
Andreas Huber9266f992016-08-25 11:21:21 -0700237 public native final void putDouble(long offset, double x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800238 /**
239 * Writes a string value at an offset.
240 *
241 * @param offset location to write value
242 * @param x value to write
243 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jstring)] is out of range
244 */
Andreas Huber9266f992016-08-25 11:21:21 -0700245 public native final void putString(long offset, String x);
Nirav Atre9850dd92018-07-24 17:03:44 -0700246 /**
247 * Writes a native handle (without duplicating the underlying file descriptors) at an offset.
248 *
249 * @param offset location to write value
250 * @param x a {@link NativeHandle} instance to write
251 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jobject)] is out of range
252 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800253 public native final void putNativeHandle(long offset, @Nullable NativeHandle x);
Andreas Huber9266f992016-08-25 11:21:21 -0700254
Steven Moreland4dde8a12018-01-10 15:45:36 -0800255 /**
256 * Put a boolean 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(jboolean)] out of the blob.
261 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700262 public native final void putBoolArray(long offset, boolean[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800263 /**
264 * Put a byte 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(jbyte)] out of the blob.
269 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700270 public native final void putInt8Array(long offset, byte[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800271 /**
272 * Put a short 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(jshort)] out of the blob.
277 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700278 public native final void putInt16Array(long offset, short[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800279 /**
280 * Put a int 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(jint)] out of the blob.
285 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700286 public native final void putInt32Array(long offset, int[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800287 /**
288 * Put a long 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(jlong)] out of the blob.
293 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700294 public native final void putInt64Array(long offset, long[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800295 /**
296 * Put a float 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(jfloat)] out of the blob.
301 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700302 public native final void putFloatArray(long offset, float[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800303 /**
304 * Put a double array contiguously at an offset in the blob.
305 *
306 * @param offset location to write values
307 * @param x array to write
308 * @throws IndexOutOfBoundsException [offset, offset + size * sizeof(jdouble)] out of the blob.
309 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700310 public native final void putDoubleArray(long offset, double[] x);
311
Steven Moreland4dde8a12018-01-10 15:45:36 -0800312 /**
313 * Write another HwBlob into this blob at the specified location.
314 *
315 * @param offset location to write value
316 * @param blob data to write
317 * @throws IndexOutOfBoundsException if [offset, offset + blob's size] outside of the range of
318 * this blob.
319 */
Andreas Huber9266f992016-08-25 11:21:21 -0700320 public native final void putBlob(long offset, HwBlob blob);
321
Steven Moreland4dde8a12018-01-10 15:45:36 -0800322 /**
Ytai Ben-Tsvi4659e182019-11-06 09:53:34 -0800323 * Writes a HidlMemory instance (without duplicating the underlying file descriptors) at an
324 * offset.
325 *
326 * @param offset location to write value
327 * @param mem a {@link HidlMemory} instance to write
328 * @throws IndexOutOfBoundsException when [offset, offset + sizeof(jobject)] is out of range
329 */
330 public final void putHidlMemory(long offset, @NonNull HidlMemory mem) {
331 putNativeHandle(offset + 0 /* offset of 'handle' field. */, mem.getHandle());
332 putInt64(offset + 16 /* offset of 'size' field. */, mem.getSize());
333 putString(offset + 24 /* offset of 'name' field. */, mem.getName());
334 }
335
336 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800337 * @return current handle of HwBlob for reference in a parcelled binder transaction
338 */
Andreas Huber9266f992016-08-25 11:21:21 -0700339 public native final long handle();
340
Steven Moreland4dde8a12018-01-10 15:45:36 -0800341 /**
342 * Convert a primitive to a wrapped array for boolean.
343 *
344 * @param array from array
345 * @return transformed array
346 */
Andreas Huber906a6792016-09-22 18:14:17 -0700347 public static Boolean[] wrapArray(@NonNull boolean[] array) {
348 final int n = array.length;
349 Boolean[] wrappedArray = new Boolean[n];
350 for (int i = 0; i < n; ++i) {
351 wrappedArray[i] = array[i];
352 }
353 return wrappedArray;
354 }
355
Steven Moreland4dde8a12018-01-10 15:45:36 -0800356 /**
357 * Convert a primitive to a wrapped array for long.
358 *
359 * @param array from array
360 * @return transformed array
361 */
Andreas Huber906a6792016-09-22 18:14:17 -0700362 public static Long[] wrapArray(@NonNull long[] array) {
363 final int n = array.length;
364 Long[] wrappedArray = new Long[n];
365 for (int i = 0; i < n; ++i) {
366 wrappedArray[i] = array[i];
367 }
368 return wrappedArray;
369 }
370
Steven Moreland4dde8a12018-01-10 15:45:36 -0800371 /**
372 * Convert a primitive to a wrapped array for byte.
373 *
374 * @param array from array
375 * @return transformed array
376 */
Andreas Huber906a6792016-09-22 18:14:17 -0700377 public static Byte[] wrapArray(@NonNull byte[] array) {
378 final int n = array.length;
379 Byte[] wrappedArray = new Byte[n];
380 for (int i = 0; i < n; ++i) {
381 wrappedArray[i] = array[i];
382 }
383 return wrappedArray;
384 }
385
Steven Moreland4dde8a12018-01-10 15:45:36 -0800386 /**
387 * Convert a primitive to a wrapped array for short.
388 *
389 * @param array from array
390 * @return transformed array
391 */
Andreas Huber906a6792016-09-22 18:14:17 -0700392 public static Short[] wrapArray(@NonNull short[] array) {
393 final int n = array.length;
394 Short[] wrappedArray = new Short[n];
395 for (int i = 0; i < n; ++i) {
396 wrappedArray[i] = array[i];
397 }
398 return wrappedArray;
399 }
400
Steven Moreland4dde8a12018-01-10 15:45:36 -0800401 /**
402 * Convert a primitive to a wrapped array for int.
403 *
404 * @param array from array
405 * @return transformed array
406 */
Andreas Huber906a6792016-09-22 18:14:17 -0700407 public static Integer[] wrapArray(@NonNull int[] array) {
408 final int n = array.length;
409 Integer[] wrappedArray = new Integer[n];
410 for (int i = 0; i < n; ++i) {
411 wrappedArray[i] = array[i];
412 }
413 return wrappedArray;
414 }
415
Steven Moreland4dde8a12018-01-10 15:45:36 -0800416 /**
417 * Convert a primitive to a wrapped array for float.
418 *
419 * @param array from array
420 * @return transformed array
421 */
Andreas Huber906a6792016-09-22 18:14:17 -0700422 public static Float[] wrapArray(@NonNull float[] array) {
423 final int n = array.length;
424 Float[] wrappedArray = new Float[n];
425 for (int i = 0; i < n; ++i) {
426 wrappedArray[i] = array[i];
427 }
428 return wrappedArray;
429 }
430
Steven Moreland4dde8a12018-01-10 15:45:36 -0800431 /**
432 * Convert a primitive to a wrapped array for double.
433 *
434 * @param array from array
435 * @return transformed array
436 */
Andreas Huber906a6792016-09-22 18:14:17 -0700437 public static Double[] wrapArray(@NonNull double[] array) {
438 final int n = array.length;
439 Double[] wrappedArray = new Double[n];
440 for (int i = 0; i < n; ++i) {
441 wrappedArray[i] = array[i];
442 }
443 return wrappedArray;
444 }
445
Andreas Huber9266f992016-08-25 11:21:21 -0700446 // Returns address of the "freeFunction".
447 private static native final long native_init();
448
449 private native final void native_setup(int size);
450
451 static {
452 long freeFunction = native_init();
453
454 sNativeRegistry = new NativeAllocationRegistry(
455 HwBlob.class.getClassLoader(),
456 freeFunction,
457 128 /* size */);
458 }
459
460 private long mNativeContext;
461}
462
463