1 /*
2 * Copyright (C) 2017 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 #define LOG_TAG "HardwareBuffer"
18
19 #include "jni.h"
20 #include <nativehelper/JNIHelp.h>
21
22 #include "android_os_Parcel.h"
23
24 #include <android/hardware_buffer.h>
25 #include <android_runtime/android_graphics_GraphicBuffer.h>
26 #include <android_runtime/android_hardware_HardwareBuffer.h>
27 #include <android_runtime/AndroidRuntime.h>
28 #include <android_runtime/Log.h>
29 #include <private/android/AHardwareBufferHelpers.h>
30
31 #include <binder/Parcel.h>
32
33 #include <private/gui/ComposerService.h>
34 #include <ui/PixelFormat.h>
35
36 #include <hardware/gralloc1.h>
37 #include <grallocusage/GrallocUsageConversion.h>
38
39 #include "core_jni_helpers.h"
40
41 using namespace android;
42
43 // ----------------------------------------------------------------------------
44 // Defines
45 // ----------------------------------------------------------------------------
46
47 // Debug
48 static constexpr bool kDebugGraphicBuffer = false;
49
50 // ----------------------------------------------------------------------------
51 // Types
52 // ----------------------------------------------------------------------------
53
54 static struct {
55 jclass clazz;
56 jfieldID mNativeObject;
57 jmethodID ctor;
58 } gHardwareBufferClassInfo;
59
60 class GraphicBufferWrapper {
61 public:
GraphicBufferWrapper(const sp<GraphicBuffer> & buffer)62 explicit GraphicBufferWrapper(const sp<GraphicBuffer>& buffer)
63 : buffer(buffer) {}
64
65 sp<GraphicBuffer> buffer;
66 };
67
68 // ----------------------------------------------------------------------------
69 // HardwareBuffer lifecycle
70 // ----------------------------------------------------------------------------
71
android_hardware_HardwareBuffer_create(JNIEnv * env,jobject clazz,jint width,jint height,jint format,jint layers,jlong usage)72 static jlong android_hardware_HardwareBuffer_create(JNIEnv* env, jobject clazz,
73 jint width, jint height, jint format, jint layers, jlong usage) {
74
75 // TODO: update createGraphicBuffer to take two 64-bit values.
76 int pixelFormat = android_hardware_HardwareBuffer_convertToPixelFormat(format);
77 if (pixelFormat == 0) {
78 if (kDebugGraphicBuffer) {
79 ALOGW("createGraphicBufferAlloc() invalid pixel format in HardwareBuffer.create()");
80 }
81 return NULL;
82 }
83
84 uint64_t grallocUsage = AHardwareBuffer_convertToGrallocUsageBits(usage);
85 sp<GraphicBuffer> buffer = new GraphicBuffer(width, height, pixelFormat, layers,
86 grallocUsage, std::string("HardwareBuffer pid [") + std::to_string(getpid()) +"]");
87 status_t error = buffer->initCheck();
88 if (error != OK) {
89 if (kDebugGraphicBuffer) {
90 ALOGW("createGraphicBuffer() failed in HardwareBuffer.create()");
91 }
92 return NULL;
93 }
94
95 GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
96 return reinterpret_cast<jlong>(wrapper);
97 }
98
android_hardware_HardwareBuffer_createFromGraphicBuffer(JNIEnv * env,jobject clazz,jobject graphicBuffer)99 static jlong android_hardware_HardwareBuffer_createFromGraphicBuffer(JNIEnv* env, jobject clazz, jobject graphicBuffer) {
100 sp<GraphicBuffer> buffer(android_graphics_GraphicBuffer_getNativeGraphicsBuffer(env,
101 graphicBuffer));
102 GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
103 return reinterpret_cast<jlong>(wrapper);
104 }
105
destroyWrapper(GraphicBufferWrapper * wrapper)106 static void destroyWrapper(GraphicBufferWrapper* wrapper) {
107 delete wrapper;
108 }
109
android_hardware_HardwareBuffer_getNativeFinalizer(JNIEnv * env,jobject clazz)110 static jlong android_hardware_HardwareBuffer_getNativeFinalizer(JNIEnv* env, jobject clazz) {
111 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&destroyWrapper));
112 }
113
android_hardware_HardwareBuffer_isSupported(JNIEnv * env,jobject clazz,jint width,jint height,jint format,jint layers,jlong usage)114 static jboolean android_hardware_HardwareBuffer_isSupported(JNIEnv* env, jobject clazz,
115 jint width, jint height, jint format, jint layers, jlong usage) {
116
117 AHardwareBuffer_Desc desc;
118 desc.width = width;
119 desc.height = height;
120 desc.format = format;
121 desc.layers = layers;
122 desc.usage = usage;
123 desc.stride = 0;
124 desc.rfu0 = 0;
125 desc.rfu1 = 0;
126 return AHardwareBuffer_isSupported(&desc);
127 }
128
129 //----------------------------------------------------------------------------
130 // Accessors
131 // ----------------------------------------------------------------------------
132
GraphicBufferWrapper_to_GraphicBuffer(jlong nativeObject)133 static inline GraphicBuffer* GraphicBufferWrapper_to_GraphicBuffer(
134 jlong nativeObject) {
135 return reinterpret_cast<GraphicBufferWrapper*>(nativeObject)->buffer.get();
136 }
137
android_hardware_HardwareBuffer_getWidth(JNIEnv * env,jobject clazz,jlong nativeObject)138 static jint android_hardware_HardwareBuffer_getWidth(JNIEnv* env, jobject clazz,
139 jlong nativeObject) {
140 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
141 return static_cast<jint>(buffer->getWidth());
142 }
143
android_hardware_HardwareBuffer_getHeight(JNIEnv * env,jobject clazz,jlong nativeObject)144 static jint android_hardware_HardwareBuffer_getHeight(JNIEnv* env,
145 jobject clazz, jlong nativeObject) {
146 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
147 return static_cast<jint>(buffer->getHeight());
148 }
149
android_hardware_HardwareBuffer_getFormat(JNIEnv * env,jobject clazz,jlong nativeObject)150 static jint android_hardware_HardwareBuffer_getFormat(JNIEnv* env,
151 jobject clazz, jlong nativeObject) {
152 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
153 return static_cast<jint>(android_hardware_HardwareBuffer_convertFromPixelFormat(
154 buffer->getPixelFormat()));
155 }
156
android_hardware_HardwareBuffer_getLayers(JNIEnv * env,jobject clazz,jlong nativeObject)157 static jint android_hardware_HardwareBuffer_getLayers(JNIEnv* env,
158 jobject clazz, jlong nativeObject) {
159 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
160 return static_cast<jint>(buffer->getLayerCount());
161 }
162
android_hardware_HardwareBuffer_getUsage(JNIEnv * env,jobject clazz,jlong nativeObject)163 static jlong android_hardware_HardwareBuffer_getUsage(JNIEnv* env,
164 jobject clazz, jlong nativeObject) {
165 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
166 return static_cast<jlong>(AHardwareBuffer_convertFromGrallocUsageBits(buffer->getUsage()));
167 }
168
android_hardware_HardwareBuffer_estimateSize(jlong nativeObject)169 static jlong android_hardware_HardwareBuffer_estimateSize(jlong nativeObject) {
170 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
171
172 uint32_t bpp = bytesPerPixel(buffer->getPixelFormat());
173 if (bpp == 0) {
174 // If the pixel format is not recognized, use 1 as default.
175 bpp = 1;
176 }
177
178 const uint32_t bufferStride =
179 buffer->getStride() > 0 ? buffer->getStride() : buffer->getWidth();
180 return static_cast<jlong>(static_cast<uint64_t>(buffer->getHeight() * bufferStride * bpp));
181 }
182
android_hardware_HardwareBuffer_getId(jlong nativeObject)183 static jlong android_hardware_HardwareBuffer_getId(jlong nativeObject) {
184 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
185 return static_cast<jlong>(buffer->getId());
186 }
187
188 // ----------------------------------------------------------------------------
189 // Serialization
190 // ----------------------------------------------------------------------------
191
android_hardware_HardwareBuffer_write(JNIEnv * env,jobject clazz,jlong nativeObject,jobject dest)192 static void android_hardware_HardwareBuffer_write(JNIEnv* env, jobject clazz,
193 jlong nativeObject, jobject dest) {
194 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
195 Parcel* parcel = parcelForJavaObject(env, dest);
196 if (parcel) {
197 parcel->write(*buffer);
198 }
199 }
200
android_hardware_HardwareBuffer_read(JNIEnv * env,jobject clazz,jobject in)201 static jlong android_hardware_HardwareBuffer_read(JNIEnv* env, jobject clazz,
202 jobject in) {
203 Parcel* parcel = parcelForJavaObject(env, in);
204 if (parcel) {
205 sp<GraphicBuffer> buffer = new GraphicBuffer();
206 if (parcel->read(*buffer) == STATUS_OK) {
207 return reinterpret_cast<jlong>(new GraphicBufferWrapper(buffer));
208 }
209 }
210 return NULL;
211 }
212
213 // ----------------------------------------------------------------------------
214 // Public functions
215 // ----------------------------------------------------------------------------
216
217 namespace android {
218
android_hardware_HardwareBuffer_getNativeHardwareBuffer(JNIEnv * env,jobject hardwareBufferObj)219 AHardwareBuffer* android_hardware_HardwareBuffer_getNativeHardwareBuffer(
220 JNIEnv* env, jobject hardwareBufferObj) {
221 if (env->IsInstanceOf(hardwareBufferObj, gHardwareBufferClassInfo.clazz)) {
222 GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(
223 env->GetLongField(hardwareBufferObj, gHardwareBufferClassInfo.mNativeObject));
224 return AHardwareBuffer_from_GraphicBuffer(buffer);
225
226 } else {
227 return nullptr;
228 }
229 }
230
android_hardware_HardwareBuffer_createFromAHardwareBuffer(JNIEnv * env,AHardwareBuffer * hardwareBuffer)231 jobject android_hardware_HardwareBuffer_createFromAHardwareBuffer(
232 JNIEnv* env, AHardwareBuffer* hardwareBuffer) {
233 GraphicBuffer* buffer = AHardwareBuffer_to_GraphicBuffer(hardwareBuffer);
234 GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
235 jobject hardwareBufferObj = env->NewObject(gHardwareBufferClassInfo.clazz,
236 gHardwareBufferClassInfo.ctor, reinterpret_cast<jlong>(wrapper));
237 if (hardwareBufferObj == NULL) {
238 delete wrapper;
239 if (env->ExceptionCheck()) {
240 ALOGE("Could not create instance of HardwareBuffer from AHardwareBuffer.");
241 LOGE_EX(env);
242 env->ExceptionClear();
243 }
244 return nullptr;
245 }
246 return hardwareBufferObj;
247 }
248
android_hardware_HardwareBuffer_convertFromPixelFormat(uint32_t format)249 uint32_t android_hardware_HardwareBuffer_convertFromPixelFormat(uint32_t format) {
250 return AHardwareBuffer_convertFromPixelFormat(format);
251 }
252
android_hardware_HardwareBuffer_convertToPixelFormat(uint32_t format)253 uint32_t android_hardware_HardwareBuffer_convertToPixelFormat(uint32_t format) {
254 return AHardwareBuffer_convertToPixelFormat(format);
255 }
256
android_hardware_HardwareBuffer_convertToGrallocUsageBits(uint64_t usage)257 uint64_t android_hardware_HardwareBuffer_convertToGrallocUsageBits(uint64_t usage) {
258 return AHardwareBuffer_convertToGrallocUsageBits(usage);
259 }
260
261 } // namespace android
262
263 // ----------------------------------------------------------------------------
264 // JNI Glue
265 // ----------------------------------------------------------------------------
266
267 const char* const kClassPathName = "android/hardware/HardwareBuffer";
268
269 // clang-format off
270 static const JNINativeMethod gMethods[] = {
271 { "nCreateHardwareBuffer", "(IIIIJ)J",
272 (void*) android_hardware_HardwareBuffer_create },
273 { "nCreateFromGraphicBuffer", "(Landroid/graphics/GraphicBuffer;)J",
274 (void*) android_hardware_HardwareBuffer_createFromGraphicBuffer },
275 { "nGetNativeFinalizer", "()J",
276 (void*) android_hardware_HardwareBuffer_getNativeFinalizer },
277 { "nWriteHardwareBufferToParcel", "(JLandroid/os/Parcel;)V",
278 (void*) android_hardware_HardwareBuffer_write },
279 { "nReadHardwareBufferFromParcel", "(Landroid/os/Parcel;)J",
280 (void*) android_hardware_HardwareBuffer_read },
281 { "nIsSupported", "(IIIIJ)Z",
282 (void*) android_hardware_HardwareBuffer_isSupported },
283
284 // --------------- @FastNative ----------------------
285 { "nGetWidth", "(J)I", (void*) android_hardware_HardwareBuffer_getWidth },
286 { "nGetHeight", "(J)I", (void*) android_hardware_HardwareBuffer_getHeight },
287 { "nGetFormat", "(J)I", (void*) android_hardware_HardwareBuffer_getFormat },
288 { "nGetLayers", "(J)I", (void*) android_hardware_HardwareBuffer_getLayers },
289 { "nGetUsage", "(J)J", (void*) android_hardware_HardwareBuffer_getUsage },
290
291 // --------------- @CriticalNative ----------------------
292 { "nEstimateSize", "(J)J", (void*) android_hardware_HardwareBuffer_estimateSize },
293 { "nGetId", "(J)J", (void*) android_hardware_HardwareBuffer_getId },
294 };
295 // clang-format on
296
register_android_hardware_HardwareBuffer(JNIEnv * env)297 int register_android_hardware_HardwareBuffer(JNIEnv* env) {
298 int err = RegisterMethodsOrDie(env, kClassPathName, gMethods,
299 NELEM(gMethods));
300
301 jclass clazz = FindClassOrDie(env, "android/hardware/HardwareBuffer");
302 gHardwareBufferClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
303 gHardwareBufferClassInfo.mNativeObject = GetFieldIDOrDie(env,
304 gHardwareBufferClassInfo.clazz, "mNativeObject", "J");
305 gHardwareBufferClassInfo.ctor = GetMethodIDOrDie(env,
306 gHardwareBufferClassInfo.clazz, "<init>", "(J)V");
307
308 return err;
309 }
310