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 #include "android/graphics/GraphicsJNI.h"
24 #include "android/graphics/GraphicBuffer.h"
25 
26 #include <android/hardware_buffer.h>
27 #include <android_runtime/android_hardware_HardwareBuffer.h>
28 #include <android_runtime/AndroidRuntime.h>
29 #include <android_runtime/Log.h>
30 #include <private/android/AHardwareBufferHelpers.h>
31 
32 #include <binder/Parcel.h>
33 
34 #include <ui/GraphicBuffer.h>
35 #include <private/gui/ComposerService.h>
36 
37 #include <hardware/gralloc1.h>
38 #include <grallocusage/GrallocUsageConversion.h>
39 
40 #include "core_jni_helpers.h"
41 
42 using namespace android;
43 
44 // ----------------------------------------------------------------------------
45 // Defines
46 // ----------------------------------------------------------------------------
47 
48 // Debug
49 static constexpr bool kDebugGraphicBuffer = false;
50 
51 // ----------------------------------------------------------------------------
52 // Types
53 // ----------------------------------------------------------------------------
54 
55 static struct {
56     jclass clazz;
57     jfieldID mNativeObject;
58     jmethodID ctor;
59 } gHardwareBufferClassInfo;
60 
61 class GraphicBufferWrapper {
62 public:
GraphicBufferWrapper(const sp<GraphicBuffer> & buffer)63     explicit GraphicBufferWrapper(const sp<GraphicBuffer>& buffer)
64             : buffer(buffer) {}
65 
66     sp<GraphicBuffer> buffer;
67 };
68 
69 // ----------------------------------------------------------------------------
70 // HardwareBuffer lifecycle
71 // ----------------------------------------------------------------------------
72 
android_hardware_HardwareBuffer_create(JNIEnv * env,jobject clazz,jint width,jint height,jint format,jint layers,jlong usage)73 static jlong android_hardware_HardwareBuffer_create(JNIEnv* env, jobject clazz,
74         jint width, jint height, jint format, jint layers, jlong usage) {
75 
76     // TODO: update createGraphicBuffer to take two 64-bit values.
77     int pixelFormat = android_hardware_HardwareBuffer_convertToPixelFormat(format);
78     if (pixelFormat == 0) {
79         if (kDebugGraphicBuffer) {
80             ALOGW("createGraphicBufferAlloc() invalid pixel format in HardwareBuffer.create()");
81         }
82         return NULL;
83     }
84 
85     uint64_t grallocUsage = AHardwareBuffer_convertToGrallocUsageBits(usage);
86     sp<GraphicBuffer> buffer = new GraphicBuffer(width, height, pixelFormat, layers,
87             grallocUsage, std::string("HardwareBuffer pid [") + std::to_string(getpid()) +"]");
88     status_t error = buffer->initCheck();
89     if (error < 0) {
90         if (kDebugGraphicBuffer) {
91             ALOGW("createGraphicBuffer() failed in HardwareBuffer.create()");
92         }
93         return NULL;
94     }
95 
96     GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
97     return reinterpret_cast<jlong>(wrapper);
98 }
99 
android_hardware_HardwareBuffer_createFromGraphicBuffer(JNIEnv * env,jobject clazz,jobject graphicBuffer)100 static jlong android_hardware_HardwareBuffer_createFromGraphicBuffer(JNIEnv* env, jobject clazz, jobject graphicBuffer) {
101     sp<GraphicBuffer> buffer(graphicBufferForJavaObject(env, 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 AHardwareBuffer_convertFromGrallocUsageBits(buffer->getUsage());
167 }
168 
169 // ----------------------------------------------------------------------------
170 // Serialization
171 // ----------------------------------------------------------------------------
172 
android_hardware_HardwareBuffer_write(JNIEnv * env,jobject clazz,jlong nativeObject,jobject dest)173 static void android_hardware_HardwareBuffer_write(JNIEnv* env, jobject clazz,
174         jlong nativeObject, jobject dest) {
175     GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(nativeObject);
176     Parcel* parcel = parcelForJavaObject(env, dest);
177     if (parcel) {
178         parcel->write(*buffer);
179     }
180 }
181 
android_hardware_HardwareBuffer_read(JNIEnv * env,jobject clazz,jobject in)182 static jlong android_hardware_HardwareBuffer_read(JNIEnv* env, jobject clazz,
183         jobject in) {
184     Parcel* parcel = parcelForJavaObject(env, in);
185     if (parcel) {
186         sp<GraphicBuffer> buffer = new GraphicBuffer();
187         parcel->read(*buffer);
188         return reinterpret_cast<jlong>(new GraphicBufferWrapper(buffer));
189     }
190 
191     return NULL;
192 }
193 
194 // ----------------------------------------------------------------------------
195 // Public functions
196 // ----------------------------------------------------------------------------
197 
198 namespace android {
199 
android_hardware_HardwareBuffer_getNativeHardwareBuffer(JNIEnv * env,jobject hardwareBufferObj)200 AHardwareBuffer* android_hardware_HardwareBuffer_getNativeHardwareBuffer(
201         JNIEnv* env, jobject hardwareBufferObj) {
202     if (env->IsInstanceOf(hardwareBufferObj, gHardwareBufferClassInfo.clazz)) {
203         GraphicBuffer* buffer = GraphicBufferWrapper_to_GraphicBuffer(
204                 env->GetLongField(hardwareBufferObj, gHardwareBufferClassInfo.mNativeObject));
205         return AHardwareBuffer_from_GraphicBuffer(buffer);
206 
207     } else {
208         return nullptr;
209     }
210 }
211 
android_hardware_HardwareBuffer_createFromAHardwareBuffer(JNIEnv * env,AHardwareBuffer * hardwareBuffer)212 jobject android_hardware_HardwareBuffer_createFromAHardwareBuffer(
213         JNIEnv* env, AHardwareBuffer* hardwareBuffer) {
214     GraphicBuffer* buffer = AHardwareBuffer_to_GraphicBuffer(hardwareBuffer);
215     GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
216     jobject hardwareBufferObj = env->NewObject(gHardwareBufferClassInfo.clazz,
217             gHardwareBufferClassInfo.ctor, reinterpret_cast<jlong>(wrapper));
218     if (hardwareBufferObj == NULL) {
219         delete wrapper;
220         if (env->ExceptionCheck()) {
221             ALOGE("Could not create instance of HardwareBuffer from AHardwareBuffer.");
222             LOGE_EX(env);
223             env->ExceptionClear();
224         }
225         return nullptr;
226     }
227     return hardwareBufferObj;
228 }
229 
android_hardware_HardwareBuffer_convertFromPixelFormat(uint32_t format)230 uint32_t android_hardware_HardwareBuffer_convertFromPixelFormat(uint32_t format) {
231     return AHardwareBuffer_convertFromPixelFormat(format);
232 }
233 
android_hardware_HardwareBuffer_convertToPixelFormat(uint32_t format)234 uint32_t android_hardware_HardwareBuffer_convertToPixelFormat(uint32_t format) {
235     return AHardwareBuffer_convertToPixelFormat(format);
236 }
237 
android_hardware_HardwareBuffer_convertToGrallocUsageBits(uint64_t usage)238 uint64_t android_hardware_HardwareBuffer_convertToGrallocUsageBits(uint64_t usage) {
239     return AHardwareBuffer_convertToGrallocUsageBits(usage);
240 }
241 
242 }  // namespace android
243 
244 // ----------------------------------------------------------------------------
245 // JNI Glue
246 // ----------------------------------------------------------------------------
247 
248 const char* const kClassPathName = "android/hardware/HardwareBuffer";
249 
250 static const JNINativeMethod gMethods[] = {
251     { "nCreateHardwareBuffer",  "(IIIIJ)J",
252             (void*) android_hardware_HardwareBuffer_create },
253     { "nCreateFromGraphicBuffer", "(Landroid/graphics/GraphicBuffer;)J",
254             (void*) android_hardware_HardwareBuffer_createFromGraphicBuffer },
255     { "nGetNativeFinalizer", "()J",
256             (void*) android_hardware_HardwareBuffer_getNativeFinalizer },
257     { "nWriteHardwareBufferToParcel",  "(JLandroid/os/Parcel;)V",
258             (void*) android_hardware_HardwareBuffer_write },
259     { "nReadHardwareBufferFromParcel", "(Landroid/os/Parcel;)J",
260             (void*) android_hardware_HardwareBuffer_read },
261     { "nIsSupported",  "(IIIIJ)Z",
262             (void*) android_hardware_HardwareBuffer_isSupported },
263 
264     // --------------- @FastNative ----------------------
265     { "nGetWidth", "(J)I",      (void*) android_hardware_HardwareBuffer_getWidth },
266     { "nGetHeight", "(J)I",     (void*) android_hardware_HardwareBuffer_getHeight },
267     { "nGetFormat", "(J)I",     (void*) android_hardware_HardwareBuffer_getFormat },
268     { "nGetLayers", "(J)I",     (void*) android_hardware_HardwareBuffer_getLayers },
269     { "nGetUsage", "(J)J",      (void*) android_hardware_HardwareBuffer_getUsage },
270 };
271 
register_android_hardware_HardwareBuffer(JNIEnv * env)272 int register_android_hardware_HardwareBuffer(JNIEnv* env) {
273     int err = RegisterMethodsOrDie(env, kClassPathName, gMethods,
274             NELEM(gMethods));
275 
276     jclass clazz = FindClassOrDie(env, "android/hardware/HardwareBuffer");
277     gHardwareBufferClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
278     gHardwareBufferClassInfo.mNativeObject = GetFieldIDOrDie(env,
279             gHardwareBufferClassInfo.clazz, "mNativeObject", "J");
280     gHardwareBufferClassInfo.ctor = GetMethodIDOrDie(env,
281             gHardwareBufferClassInfo.clazz, "<init>", "(J)V");
282 
283     return err;
284 }
285