blob: 405651e992a3b0394911e15ffa4a51bbfb30eec6 [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;
Andreas Huber906a6792016-09-22 18:14:17 -070021
Andreas Huber9266f992016-08-25 11:21:21 -070022import libcore.util.NativeAllocationRegistry;
23
Steven Moreland4dde8a12018-01-10 15:45:36 -080024/**
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
Andreas Huber9266f992016-08-25 11:21:21 -070031public 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
Steven Moreland4dde8a12018-01-10 15:45:36 -080044 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070049 public native final boolean getBool(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080050 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070055 public native final byte getInt8(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080056 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070061 public native final short getInt16(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080062 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070067 public native final int getInt32(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080068 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070073 public native final long getInt64(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080074 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070079 public native final float getFloat(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080080 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070085 public native final double getDouble(long offset);
Steven Moreland4dde8a12018-01-10 15:45:36 -080086 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -070091 public native final String getString(long offset);
92
Andreas Huber0eb37e02017-10-31 11:51:50 -070093 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -080094 * 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.
Andreas Huber0eb37e02017-10-31 11:51:50 -0700102 */
103 public native final void copyToBoolArray(long offset, boolean[] array, int size);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800104 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700114 public native final void copyToInt8Array(long offset, byte[] 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(jshort)] out of the blob.
124 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700125 public native final void copyToInt16Array(long offset, short[] 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(jint)] out of the blob.
135 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700136 public native final void copyToInt32Array(long offset, int[] 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(jlong)] out of the blob.
146 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700147 public native final void copyToInt64Array(long offset, long[] 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(jfloat)] out of the blob.
157 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700158 public native final void copyToFloatArray(long offset, float[] 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(jdouble)] out of the blob.
168 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700169 public native final void copyToDoubleArray(long offset, double[] array, int size);
170
Steven Moreland4dde8a12018-01-10 15:45:36 -0800171 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700178 public native final void putBool(long offset, boolean x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800179 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700186 public native final void putInt8(long offset, byte x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800187 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700194 public native final void putInt16(long offset, short x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800195 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700202 public native final void putInt32(long offset, int x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800203 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700210 public native final void putInt64(long offset, long x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800211 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700218 public native final void putFloat(long offset, float x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800219 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700226 public native final void putDouble(long offset, double x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800227 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700234 public native final void putString(long offset, String x);
235
Steven Moreland4dde8a12018-01-10 15:45:36 -0800236 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700243 public native final void putBoolArray(long offset, boolean[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800244 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700251 public native final void putInt8Array(long offset, byte[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800252 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700259 public native final void putInt16Array(long offset, short[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800260 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700267 public native final void putInt32Array(long offset, int[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800268 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700275 public native final void putInt64Array(long offset, long[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800276 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700283 public native final void putFloatArray(long offset, float[] x);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800284 /**
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 */
Andreas Huber0eb37e02017-10-31 11:51:50 -0700291 public native final void putDoubleArray(long offset, double[] x);
292
Steven Moreland4dde8a12018-01-10 15:45:36 -0800293 /**
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 */
Andreas Huber9266f992016-08-25 11:21:21 -0700301 public native final void putBlob(long offset, HwBlob blob);
302
Steven Moreland4dde8a12018-01-10 15:45:36 -0800303 /**
304 * @return current handle of HwBlob for reference in a parcelled binder transaction
305 */
Andreas Huber9266f992016-08-25 11:21:21 -0700306 public native final long handle();
307
Steven Moreland4dde8a12018-01-10 15:45:36 -0800308 /**
309 * Convert a primitive to a wrapped array for boolean.
310 *
311 * @param array from array
312 * @return transformed array
313 */
Andreas Huber906a6792016-09-22 18:14:17 -0700314 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800323 /**
324 * Convert a primitive to a wrapped array for long.
325 *
326 * @param array from array
327 * @return transformed array
328 */
Andreas Huber906a6792016-09-22 18:14:17 -0700329 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800338 /**
339 * Convert a primitive to a wrapped array for byte.
340 *
341 * @param array from array
342 * @return transformed array
343 */
Andreas Huber906a6792016-09-22 18:14:17 -0700344 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800353 /**
354 * Convert a primitive to a wrapped array for short.
355 *
356 * @param array from array
357 * @return transformed array
358 */
Andreas Huber906a6792016-09-22 18:14:17 -0700359 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800368 /**
369 * Convert a primitive to a wrapped array for int.
370 *
371 * @param array from array
372 * @return transformed array
373 */
Andreas Huber906a6792016-09-22 18:14:17 -0700374 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800383 /**
384 * Convert a primitive to a wrapped array for float.
385 *
386 * @param array from array
387 * @return transformed array
388 */
Andreas Huber906a6792016-09-22 18:14:17 -0700389 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
Steven Moreland4dde8a12018-01-10 15:45:36 -0800398 /**
399 * Convert a primitive to a wrapped array for double.
400 *
401 * @param array from array
402 * @return transformed array
403 */
Andreas Huber906a6792016-09-22 18:14:17 -0700404 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
Andreas Huber9266f992016-08-25 11:21:21 -0700413 // 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