1 /*
2  * Copyright (C) 2013 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 "GraphicBuffer"
18 
19 #include "jni.h"
20 #include <nativehelper/JNIHelp.h>
21 #include <inttypes.h>
22 
23 #include "android_os_Parcel.h"
24 #include <binder/Parcel.h>
25 
26 #include <log/log.h>
27 
28 #include <ui/GraphicBuffer.h>
29 #include <ui/PixelFormat.h>
30 
31 #include <android/native_window.h>
32 #include <android/graphics/canvas.h>
33 #include <android_runtime/android_graphics_GraphicBuffer.h>
34 #include <android_runtime/android_hardware_HardwareBuffer.h>
35 #include <private/android/AHardwareBufferHelpers.h>
36 
37 #include <private/gui/ComposerService.h>
38 
39 #include "core_jni_helpers.h"
40 
41 namespace android {
42 
43 // ----------------------------------------------------------------------------
44 // Defines
45 // ----------------------------------------------------------------------------
46 
47 // Debug
48 static const bool kDebugGraphicBuffer = false;
49 
50 #define LOCK_CANVAS_USAGE (GraphicBuffer::USAGE_SW_READ_OFTEN | GraphicBuffer::USAGE_SW_WRITE_OFTEN)
51 
52 // ----------------------------------------------------------------------------
53 // JNI Helpers
54 // ----------------------------------------------------------------------------
55 
56 static struct {
57     jfieldID mNativeObject;
58     jclass mClass;
59     jmethodID mConstructorMethodID;
60 } gGraphicBufferClassInfo;
61 
62 static struct {
63     jmethodID set;
64     jfieldID left;
65     jfieldID top;
66     jfieldID right;
67     jfieldID bottom;
68 } gRectClassInfo;
69 
70 #define GET_INT(object, field) \
71     env->GetIntField(object, field)
72 
73 #define SET_INT(object, field, value) \
74     env->SetIntField(object, field, value)
75 
76 #define GET_LONG(object, field) \
77     env->GetLongField(object, field)
78 
79 #define SET_LONG(object, field, value) \
80     env->SetLongField(object, field, value)
81 
82 #define INVOKEV(object, method, ...) \
83     env->CallVoidMethod(object, method, __VA_ARGS__)
84 
85 // ----------------------------------------------------------------------------
86 // Types
87 // ----------------------------------------------------------------------------
88 
89 class GraphicBufferWrapper {
90 public:
GraphicBufferWrapper(const sp<GraphicBuffer> & buffer)91     explicit GraphicBufferWrapper(const sp<GraphicBuffer>& buffer): buffer(buffer) {
92         LOG_ALWAYS_FATAL_IF(buffer == nullptr, "creating a null GraphicBuffer");
93     }
get() const94     const sp<GraphicBuffer>& get() const {
95         return buffer;
96     }
97 
98 private:
99     // make sure this is immutable
100     sp<GraphicBuffer> const buffer;
101 };
102 
103 // ----------------------------------------------------------------------------
104 // GraphicBuffer lifecycle
105 // ----------------------------------------------------------------------------
106 
android_graphics_GraphicBuffer_wrap(JNIEnv * env,jobject clazz,jlong unwrapped)107 static jlong android_graphics_GraphicBuffer_wrap(JNIEnv* env, jobject clazz,
108         jlong unwrapped) {
109     sp<GraphicBuffer> b(reinterpret_cast<GraphicBuffer*>(unwrapped));
110     LOG_ALWAYS_FATAL_IF(b == nullptr,
111             "*** android_graphics_GraphicBuffer_wrap() invalid state, b is null, unwrapped=%#" PRIx64, unwrapped);
112     GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(b);
113     return reinterpret_cast<jlong>(wrapper);
114 }
115 
android_graphics_GraphicBuffer_create(JNIEnv * env,jobject clazz,jint width,jint height,jint format,jint usage)116 static jlong android_graphics_GraphicBuffer_create(JNIEnv* env, jobject clazz,
117         jint width, jint height, jint format, jint usage) {
118 
119     sp<GraphicBuffer> buffer = new GraphicBuffer(
120             uint32_t(width), uint32_t(height), PixelFormat(format), uint32_t(usage),
121             std::string("android_graphics_GraphicBuffer_create pid [") +
122                     std::to_string(getpid()) +"]");
123 
124     status_t error = buffer->initCheck();
125     if (error < 0) {
126         ALOGW_IF(kDebugGraphicBuffer, "createGraphicBuffer() failed in GraphicBuffer.create()");
127         return NULL;
128     }
129 
130     GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
131     return reinterpret_cast<jlong>(wrapper);
132 }
133 
android_graphics_GraphicBuffer_destroy(JNIEnv * env,jobject clazz,jlong wrapperHandle)134 static void android_graphics_GraphicBuffer_destroy(JNIEnv* env, jobject clazz,
135         jlong wrapperHandle) {
136     GraphicBufferWrapper* wrapper =
137                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
138     delete wrapper;
139 }
140 
141 // ----------------------------------------------------------------------------
142 // Canvas management
143 // ----------------------------------------------------------------------------
144 
android_graphics_GraphicBuffer_lockCanvas(JNIEnv * env,jobject,jlong wrapperHandle,jobject canvasObj,jobject dirtyRect)145 static jboolean android_graphics_GraphicBuffer_lockCanvas(JNIEnv* env, jobject,
146         jlong wrapperHandle, jobject canvasObj, jobject dirtyRect) {
147 
148     GraphicBufferWrapper* wrapper =
149                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
150     if (!wrapper) {
151         return JNI_FALSE;
152     }
153 
154     sp<GraphicBuffer> buffer(wrapper->get());
155 
156     Rect rect(Rect::EMPTY_RECT);
157     if (dirtyRect) {
158         rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
159         rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
160         rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
161         rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
162     } else {
163         rect.set(Rect(buffer->getWidth(), buffer->getHeight()));
164     }
165 
166     void* bits = NULL;
167     status_t status = buffer->lock(LOCK_CANVAS_USAGE, rect, &bits);
168 
169     if (status) return JNI_FALSE;
170     if (!bits) {
171         buffer->unlock();
172         return JNI_FALSE;
173     }
174 
175     ANativeWindow_Buffer nativeBuffer;
176     nativeBuffer.width = buffer->getWidth();
177     nativeBuffer.height = buffer->getHeight();
178     nativeBuffer.stride = buffer->getStride();
179     nativeBuffer.format = AHardwareBuffer_convertFromPixelFormat(buffer->getPixelFormat());
180     nativeBuffer.bits = bits;
181 
182     graphics::Canvas canvas(env, canvasObj);
183     canvas.setBuffer(&nativeBuffer, ADATASPACE_UNKNOWN);
184     canvas.clipRect({rect.left, rect.top, rect.right, rect.bottom});
185 
186     if (dirtyRect) {
187         INVOKEV(dirtyRect, gRectClassInfo.set,
188                 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
189     }
190 
191     return JNI_TRUE;
192 }
193 
android_graphics_GraphicBuffer_unlockCanvasAndPost(JNIEnv * env,jobject,jlong wrapperHandle,jobject canvasObj)194 static jboolean android_graphics_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobject,
195         jlong wrapperHandle, jobject canvasObj) {
196     // release the buffer from the canvas
197     graphics::Canvas canvas(env, canvasObj);
198     canvas.setBuffer(nullptr, ADATASPACE_UNKNOWN);
199 
200     GraphicBufferWrapper* wrapper =
201                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
202     if (wrapper) {
203         status_t status = wrapper->get()->unlock();
204         return status == 0 ? JNI_TRUE : JNI_FALSE;
205     }
206 
207     return JNI_FALSE;
208 }
209 
210 // ----------------------------------------------------------------------------
211 // Serialization
212 // ----------------------------------------------------------------------------
213 
android_graphics_GraphicBuffer_write(JNIEnv * env,jobject clazz,jlong wrapperHandle,jobject dest)214 static void android_graphics_GraphicBuffer_write(JNIEnv* env, jobject clazz,
215         jlong wrapperHandle, jobject dest) {
216 
217     GraphicBufferWrapper* wrapper =
218                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
219     Parcel* parcel = parcelForJavaObject(env, dest);
220     if (parcel) {
221         parcel->write(*wrapper->get());
222     }
223 }
224 
android_graphics_GraphicBuffer_read(JNIEnv * env,jobject clazz,jobject in)225 static jlong android_graphics_GraphicBuffer_read(JNIEnv* env, jobject clazz,
226         jobject in) {
227 
228     Parcel* parcel = parcelForJavaObject(env, in);
229     if (parcel) {
230         sp<GraphicBuffer> buffer = new GraphicBuffer();
231         parcel->read(*buffer);
232         return reinterpret_cast<jlong>(new GraphicBufferWrapper(buffer));
233     }
234 
235     return NULL;
236 }
237 
238 // ----------------------------------------------------------------------------
239 // External helpers
240 // ----------------------------------------------------------------------------
241 
android_graphics_GraphicBuffer_getNativeGraphicsBuffer(JNIEnv * env,jobject obj)242 sp<GraphicBuffer> android_graphics_GraphicBuffer_getNativeGraphicsBuffer(JNIEnv* env, jobject obj) {
243     if (obj) {
244         jlong nativeObject = env->GetLongField(obj, gGraphicBufferClassInfo.mNativeObject);
245         GraphicBufferWrapper* wrapper = (GraphicBufferWrapper*) nativeObject;
246         if (wrapper != NULL) {
247             sp<GraphicBuffer> buffer(wrapper->get());
248             return buffer;
249         }
250     }
251     return NULL;
252 }
253 
android_graphics_GraphicBuffer_createFromAHardwareBuffer(JNIEnv * env,AHardwareBuffer * hardwareBuffer)254 jobject android_graphics_GraphicBuffer_createFromAHardwareBuffer(JNIEnv* env,
255                                                                  AHardwareBuffer* hardwareBuffer) {
256     GraphicBuffer* buffer = GraphicBuffer::fromAHardwareBuffer(hardwareBuffer);
257     GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
258     jobject obj = env->NewObject(gGraphicBufferClassInfo.mClass,
259             gGraphicBufferClassInfo.mConstructorMethodID, buffer->getWidth(), buffer->getHeight(),
260             buffer->getPixelFormat(), (jint)buffer->getUsage(), reinterpret_cast<jlong>(wrapper));
261     return obj;
262 }
263 
264 // ----------------------------------------------------------------------------
265 // AHB to GraphicBuffer Converter
266 // ----------------------------------------------------------------------------
267 
android_graphics_GraphicBuffer_createFromHardwareBuffer(JNIEnv * env,jobject clazz,jobject hb)268 static jobject android_graphics_GraphicBuffer_createFromHardwareBuffer(JNIEnv* env, jobject clazz,
269                                                                        jobject hb) {
270     AHardwareBuffer* ahb = android_hardware_HardwareBuffer_getNativeHardwareBuffer(env, hb);
271     return android_graphics_GraphicBuffer_createFromAHardwareBuffer(env, ahb);
272 }
273 
274 };
275 
276 using namespace android;
277 // ----------------------------------------------------------------------------
278 // JNI Glue
279 // ----------------------------------------------------------------------------
280 
281 const char* const kClassPathName = "android/graphics/GraphicBuffer";
282 
283 static const JNINativeMethod gMethods[] = {
284     { "nCreateGraphicBuffer",  "(IIII)J", (void*) android_graphics_GraphicBuffer_create },
285     { "nDestroyGraphicBuffer", "(J)V",    (void*) android_graphics_GraphicBuffer_destroy },
286 
287     { "nWriteGraphicBufferToParcel",  "(JLandroid/os/Parcel;)V",
288             (void*) android_graphics_GraphicBuffer_write },
289     { "nReadGraphicBufferFromParcel", "(Landroid/os/Parcel;)J",
290             (void*) android_graphics_GraphicBuffer_read },
291 
292     { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
293             (void*) android_graphics_GraphicBuffer_lockCanvas },
294     { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z",
295             (void*) android_graphics_GraphicBuffer_unlockCanvasAndPost },
296     { "nWrapGraphicBuffer", "(J)J",
297             (void*) android_graphics_GraphicBuffer_wrap },
298     { "nCreateFromHardwareBuffer",
299             "(Landroid/hardware/HardwareBuffer;)Landroid/graphics/GraphicBuffer;",
300             (void*) android_graphics_GraphicBuffer_createFromHardwareBuffer }
301 };
302 
register_android_graphics_GraphicBuffer(JNIEnv * env)303 int register_android_graphics_GraphicBuffer(JNIEnv* env) {
304     gGraphicBufferClassInfo.mClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kClassPathName));
305     gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, gGraphicBufferClassInfo.mClass,
306             "mNativeObject", "J");
307     gGraphicBufferClassInfo.mConstructorMethodID = env->GetMethodID(gGraphicBufferClassInfo.mClass,
308             "<init>", "(IIIIJ)V");
309 
310     jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
311     gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
312     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
313     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
314     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
315     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
316 
317     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
318 }
319