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