Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 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 | |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Steven Moreland | cbba4c7 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 22 | |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 23 | import libcore.util.NativeAllocationRegistry; |
| 24 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 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 |
Steven Moreland | cbba4c7 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 32 | @TestApi |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 33 | public 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 46 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 51 | public native final boolean getBool(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 52 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 57 | public native final byte getInt8(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 58 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 63 | public native final short getInt16(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 64 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 69 | public native final int getInt32(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 70 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 75 | public native final long getInt64(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 76 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 81 | public native final float getFloat(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 82 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 87 | public native final double getDouble(long offset); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 88 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 93 | public native final String getString(long offset); |
| 94 | |
Andreas Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 95 | /** |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 96 | * 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 104 | */ |
| 105 | public native final void copyToBoolArray(long offset, boolean[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 106 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 116 | public native final void copyToInt8Array(long offset, byte[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 117 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 127 | public native final void copyToInt16Array(long offset, short[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 128 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 138 | public native final void copyToInt32Array(long offset, int[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 139 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 149 | public native final void copyToInt64Array(long offset, long[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 150 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 160 | public native final void copyToFloatArray(long offset, float[] array, int size); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 161 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 171 | public native final void copyToDoubleArray(long offset, double[] array, int size); |
| 172 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 173 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 180 | public native final void putBool(long offset, boolean x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 181 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 188 | public native final void putInt8(long offset, byte x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 189 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 196 | public native final void putInt16(long offset, short x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 197 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 204 | public native final void putInt32(long offset, int x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 205 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 212 | public native final void putInt64(long offset, long x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 213 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 220 | public native final void putFloat(long offset, float x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 221 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 228 | public native final void putDouble(long offset, double x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 229 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 236 | public native final void putString(long offset, String x); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 237 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 245 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 246 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 253 | public native final void putBoolArray(long offset, boolean[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 254 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 261 | public native final void putInt8Array(long offset, byte[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 262 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 269 | public native final void putInt16Array(long offset, short[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 270 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 277 | public native final void putInt32Array(long offset, int[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 278 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 285 | public native final void putInt64Array(long offset, long[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 286 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 293 | public native final void putFloatArray(long offset, float[] x); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 294 | /** |
| 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 Huber | 0eb37e0 | 2017-10-31 11:51:50 -0700 | [diff] [blame] | 301 | public native final void putDoubleArray(long offset, double[] x); |
| 302 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 303 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 311 | public native final void putBlob(long offset, HwBlob blob); |
| 312 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 313 | /** |
| 314 | * @return current handle of HwBlob for reference in a parcelled binder transaction |
| 315 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 316 | public native final long handle(); |
| 317 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 318 | /** |
| 319 | * Convert a primitive to a wrapped array for boolean. |
| 320 | * |
| 321 | * @param array from array |
| 322 | * @return transformed array |
| 323 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 324 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 333 | /** |
| 334 | * Convert a primitive to a wrapped array for long. |
| 335 | * |
| 336 | * @param array from array |
| 337 | * @return transformed array |
| 338 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 339 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 348 | /** |
| 349 | * Convert a primitive to a wrapped array for byte. |
| 350 | * |
| 351 | * @param array from array |
| 352 | * @return transformed array |
| 353 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 354 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 363 | /** |
| 364 | * Convert a primitive to a wrapped array for short. |
| 365 | * |
| 366 | * @param array from array |
| 367 | * @return transformed array |
| 368 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 369 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 378 | /** |
| 379 | * Convert a primitive to a wrapped array for int. |
| 380 | * |
| 381 | * @param array from array |
| 382 | * @return transformed array |
| 383 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 384 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 393 | /** |
| 394 | * Convert a primitive to a wrapped array for float. |
| 395 | * |
| 396 | * @param array from array |
| 397 | * @return transformed array |
| 398 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 399 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 408 | /** |
| 409 | * Convert a primitive to a wrapped array for double. |
| 410 | * |
| 411 | * @param array from array |
| 412 | * @return transformed array |
| 413 | */ |
Andreas Huber | 906a679 | 2016-09-22 18:14:17 -0700 | [diff] [blame] | 414 | 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 423 | // 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 | |