blob: 88226f0a16657be87808880f2021e6bf0b69c278 [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;
20
Andreas Huber9266f992016-08-25 11:21:21 -070021import libcore.util.NativeAllocationRegistry;
22
23/** @hide */
24public class HwBlob {
25 private static final String TAG = "HwBlob";
26
27 private static final NativeAllocationRegistry sNativeRegistry;
28
29 public HwBlob(int size) {
30 native_setup(size);
31
32 sNativeRegistry.registerNativeAllocation(
33 this,
34 mNativeContext);
35 }
36
37 public native final boolean getBool(long offset);
38 public native final byte getInt8(long offset);
39 public native final short getInt16(long offset);
40 public native final int getInt32(long offset);
41 public native final long getInt64(long offset);
42 public native final float getFloat(long offset);
43 public native final double getDouble(long offset);
44 public native final String getString(long offset);
45
46 public native final void putBool(long offset, boolean x);
47 public native final void putInt8(long offset, byte x);
48 public native final void putInt16(long offset, short x);
49 public native final void putInt32(long offset, int x);
50 public native final void putInt64(long offset, long x);
51 public native final void putFloat(long offset, float x);
52 public native final void putDouble(long offset, double x);
53 public native final void putString(long offset, String x);
54
55 public native final void putBlob(long offset, HwBlob blob);
56
57 public native final long handle();
58
Andreas Huber906a6792016-09-22 18:14:17 -070059 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
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
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
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
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
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
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
Andreas Huber9266f992016-08-25 11:21:21 -0700122 // Returns address of the "freeFunction".
123 private static native final long native_init();
124
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