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 21 import libcore.util.NativeAllocationRegistry; 22 23 /** @hide */ 24 public class HwBlob { 25 private static final String TAG = "HwBlob"; 26 27 private static final NativeAllocationRegistry sNativeRegistry; 28 HwBlob(int size)29 public HwBlob(int size) { 30 native_setup(size); 31 32 sNativeRegistry.registerNativeAllocation( 33 this, 34 mNativeContext); 35 } 36 getBool(long offset)37 public native final boolean getBool(long offset); getInt8(long offset)38 public native final byte getInt8(long offset); getInt16(long offset)39 public native final short getInt16(long offset); getInt32(long offset)40 public native final int getInt32(long offset); getInt64(long offset)41 public native final long getInt64(long offset); getFloat(long offset)42 public native final float getFloat(long offset); getDouble(long offset)43 public native final double getDouble(long offset); getString(long offset)44 public native final String getString(long offset); 45 putBool(long offset, boolean x)46 public native final void putBool(long offset, boolean x); putInt8(long offset, byte x)47 public native final void putInt8(long offset, byte x); putInt16(long offset, short x)48 public native final void putInt16(long offset, short x); putInt32(long offset, int x)49 public native final void putInt32(long offset, int x); putInt64(long offset, long x)50 public native final void putInt64(long offset, long x); putFloat(long offset, float x)51 public native final void putFloat(long offset, float x); putDouble(long offset, double x)52 public native final void putDouble(long offset, double x); putString(long offset, String x)53 public native final void putString(long offset, String x); 54 putBlob(long offset, HwBlob blob)55 public native final void putBlob(long offset, HwBlob blob); 56 handle()57 public native final long handle(); 58 wrapArray(@onNull boolean[] array)59 public static Boolean[] wrapArray(@NonNull boolean[] array) { 60 final int n = array.length; 61 Boolean[] wrappedArray = new Boolean[n]; 62 for (int i = 0; i < n; ++i) { 63 wrappedArray[i] = array[i]; 64 } 65 return wrappedArray; 66 } 67 wrapArray(@onNull long[] array)68 public static Long[] wrapArray(@NonNull long[] array) { 69 final int n = array.length; 70 Long[] wrappedArray = new Long[n]; 71 for (int i = 0; i < n; ++i) { 72 wrappedArray[i] = array[i]; 73 } 74 return wrappedArray; 75 } 76 wrapArray(@onNull byte[] array)77 public static Byte[] wrapArray(@NonNull byte[] array) { 78 final int n = array.length; 79 Byte[] wrappedArray = new Byte[n]; 80 for (int i = 0; i < n; ++i) { 81 wrappedArray[i] = array[i]; 82 } 83 return wrappedArray; 84 } 85 wrapArray(@onNull short[] array)86 public static Short[] wrapArray(@NonNull short[] array) { 87 final int n = array.length; 88 Short[] wrappedArray = new Short[n]; 89 for (int i = 0; i < n; ++i) { 90 wrappedArray[i] = array[i]; 91 } 92 return wrappedArray; 93 } 94 wrapArray(@onNull int[] array)95 public static Integer[] wrapArray(@NonNull int[] array) { 96 final int n = array.length; 97 Integer[] wrappedArray = new Integer[n]; 98 for (int i = 0; i < n; ++i) { 99 wrappedArray[i] = array[i]; 100 } 101 return wrappedArray; 102 } 103 wrapArray(@onNull float[] array)104 public static Float[] wrapArray(@NonNull float[] array) { 105 final int n = array.length; 106 Float[] wrappedArray = new Float[n]; 107 for (int i = 0; i < n; ++i) { 108 wrappedArray[i] = array[i]; 109 } 110 return wrappedArray; 111 } 112 wrapArray(@onNull double[] array)113 public static Double[] wrapArray(@NonNull double[] array) { 114 final int n = array.length; 115 Double[] wrappedArray = new Double[n]; 116 for (int i = 0; i < n; ++i) { 117 wrappedArray[i] = array[i]; 118 } 119 return wrappedArray; 120 } 121 122 // Returns address of the "freeFunction". native_init()123 private static native final long native_init(); 124 native_setup(int size)125 private native final void native_setup(int size); 126 127 static { 128 long freeFunction = native_init(); 129 130 sNativeRegistry = new NativeAllocationRegistry( 131 HwBlob.class.getClassLoader(), 132 freeFunction, 133 128 /* size */); 134 } 135 136 private long mNativeContext; 137 } 138 139 140