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_create(JNIEnv * env,jobject clazz,jint width,jint height,jint format,jint usage)107 static jlong android_graphics_GraphicBuffer_create(JNIEnv* env, jobject clazz,
108         jint width, jint height, jint format, jint usage) {
109 
110     sp<GraphicBuffer> buffer = new GraphicBuffer(
111             uint32_t(width), uint32_t(height), PixelFormat(format), uint32_t(usage),
112             std::string("android_graphics_GraphicBuffer_create pid [") +
113                     std::to_string(getpid()) +"]");
114 
115     status_t error = buffer->initCheck();
116     if (error < 0) {
117         ALOGW_IF(kDebugGraphicBuffer, "createGraphicBuffer() failed in GraphicBuffer.create()");
118         return NULL;
119     }
120 
121     GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
122     return reinterpret_cast<jlong>(wrapper);
123 }
124 
android_graphics_GraphicBuffer_destroy(JNIEnv * env,jobject clazz,jlong wrapperHandle)125 static void android_graphics_GraphicBuffer_destroy(JNIEnv* env, jobject clazz,
126         jlong wrapperHandle) {
127     GraphicBufferWrapper* wrapper =
128                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
129     delete wrapper;
130 }
131 
132 // ----------------------------------------------------------------------------
133 // Canvas management
134 // ----------------------------------------------------------------------------
135 
android_graphics_GraphicBuffer_lockCanvas(JNIEnv * env,jobject,jlong wrapperHandle,jobject canvasObj,jobject dirtyRect)136 static jboolean android_graphics_GraphicBuffer_lockCanvas(JNIEnv* env, jobject,
137         jlong wrapperHandle, jobject canvasObj, jobject dirtyRect) {
138 
139     GraphicBufferWrapper* wrapper =
140                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
141     if (!wrapper) {
142         return JNI_FALSE;
143     }
144 
145     sp<GraphicBuffer> buffer(wrapper->get());
146 
147     Rect rect(Rect::EMPTY_RECT);
148     if (dirtyRect) {
149         rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
150         rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
151         rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
152         rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
153     } else {
154         rect.set(Rect(buffer->getWidth(), buffer->getHeight()));
155     }
156 
157     void* bits = NULL;
158     status_t status = buffer->lock(LOCK_CANVAS_USAGE, rect, &bits);
159 
160     if (status) return JNI_FALSE;
161     if (!bits) {
162         buffer->unlock();
163         return JNI_FALSE;
164     }
165 
166     ANativeWindow_Buffer nativeBuffer;
167     nativeBuffer.width = buffer->getWidth();
168     nativeBuffer.height = buffer->getHeight();
169     nativeBuffer.stride = buffer->getStride();
170     nativeBuffer.format = AHardwareBuffer_convertFromPixelFormat(buffer->getPixelFormat());
171     nativeBuffer.bits = bits;
172 
173     graphics::Canvas canvas(env, canvasObj);
174     canvas.setBuffer(&nativeBuffer, ADATASPACE_UNKNOWN);
175     canvas.clipRect({rect.left, rect.top, rect.right, rect.bottom});
176 
177     if (dirtyRect) {
178         INVOKEV(dirtyRect, gRectClassInfo.set,
179                 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
180     }
181 
182     return JNI_TRUE;
183 }
184 
android_graphics_GraphicBuffer_unlockCanvasAndPost(JNIEnv * env,jobject,jlong wrapperHandle,jobject canvasObj)185 static jboolean android_graphics_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobject,
186         jlong wrapperHandle, jobject canvasObj) {
187     // release the buffer from the canvas
188     graphics::Canvas canvas(env, canvasObj);
189     canvas.setBuffer(nullptr, ADATASPACE_UNKNOWN);
190 
191     GraphicBufferWrapper* wrapper =
192                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
193     if (wrapper) {
194         status_t status = wrapper->get()->unlock();
195         return status == 0 ? JNI_TRUE : JNI_FALSE;
196     }
197 
198     return JNI_FALSE;
199 }
200 
201 // ----------------------------------------------------------------------------
202 // Serialization
203 // ----------------------------------------------------------------------------
204 
android_graphics_GraphicBuffer_write(JNIEnv * env,jobject clazz,jlong wrapperHandle,jobject dest)205 static void android_graphics_GraphicBuffer_write(JNIEnv* env, jobject clazz,
206         jlong wrapperHandle, jobject dest) {
207 
208     GraphicBufferWrapper* wrapper =
209                 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
210     Parcel* parcel = parcelForJavaObject(env, dest);
211     if (parcel) {
212         parcel->write(*wrapper->get());
213     }
214 }
215 
android_graphics_GraphicBuffer_read(JNIEnv * env,jobject clazz,jobject in)216 static jlong android_graphics_GraphicBuffer_read(JNIEnv* env, jobject clazz,
217         jobject in) {
218 
219     Parcel* parcel = parcelForJavaObject(env, in);
220     if (parcel) {
221         sp<GraphicBuffer> buffer = new GraphicBuffer();
222         parcel->read(*buffer);
223         return reinterpret_cast<jlong>(new GraphicBufferWrapper(buffer));
224     }
225 
226     return NULL;
227 }
228 
229 // ----------------------------------------------------------------------------
230 // External helpers
231 // ----------------------------------------------------------------------------
232 
android_graphics_GraphicBuffer_getNativeGraphicsBuffer(JNIEnv * env,jobject obj)233 sp<GraphicBuffer> android_graphics_GraphicBuffer_getNativeGraphicsBuffer(JNIEnv* env, jobject obj) {
234     if (obj) {
235         jlong nativeObject = env->GetLongField(obj, gGraphicBufferClassInfo.mNativeObject);
236         GraphicBufferWrapper* wrapper = (GraphicBufferWrapper*) nativeObject;
237         if (wrapper != NULL) {
238             sp<GraphicBuffer> buffer(wrapper->get());
239             return buffer;
240         }
241     }
242     return NULL;
243 }
244 
android_graphics_GraphicBuffer_createFromAHardwareBuffer(JNIEnv * env,AHardwareBuffer * hardwareBuffer)245 jobject android_graphics_GraphicBuffer_createFromAHardwareBuffer(JNIEnv* env,
246                                                                  AHardwareBuffer* hardwareBuffer) {
247     GraphicBuffer* buffer = GraphicBuffer::fromAHardwareBuffer(hardwareBuffer);
248     GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
249     jobject obj = env->NewObject(gGraphicBufferClassInfo.mClass,
250             gGraphicBufferClassInfo.mConstructorMethodID, buffer->getWidth(), buffer->getHeight(),
251             buffer->getPixelFormat(), (jint)buffer->getUsage(), reinterpret_cast<jlong>(wrapper));
252     return obj;
253 }
254 
255 // ----------------------------------------------------------------------------
256 // AHB to GraphicBuffer Converter
257 // ----------------------------------------------------------------------------
258 
android_graphics_GraphicBuffer_createFromHardwareBuffer(JNIEnv * env,jobject clazz,jobject hb)259 static jobject android_graphics_GraphicBuffer_createFromHardwareBuffer(JNIEnv* env, jobject clazz,
260                                                                        jobject hb) {
261     AHardwareBuffer* ahb = android_hardware_HardwareBuffer_getNativeHardwareBuffer(env, hb);
262     return android_graphics_GraphicBuffer_createFromAHardwareBuffer(env, ahb);
263 }
264 
265 };
266 
267 using namespace android;
268 // ----------------------------------------------------------------------------
269 // JNI Glue
270 // ----------------------------------------------------------------------------
271 
272 const char* const kClassPathName = "android/graphics/GraphicBuffer";
273 
274 static const JNINativeMethod gMethods[] = {
275     { "nCreateGraphicBuffer",  "(IIII)J", (void*) android_graphics_GraphicBuffer_create },
276     { "nDestroyGraphicBuffer", "(J)V",    (void*) android_graphics_GraphicBuffer_destroy },
277 
278     { "nWriteGraphicBufferToParcel",  "(JLandroid/os/Parcel;)V",
279             (void*) android_graphics_GraphicBuffer_write },
280     { "nReadGraphicBufferFromParcel", "(Landroid/os/Parcel;)J",
281             (void*) android_graphics_GraphicBuffer_read },
282 
283     { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
284             (void*) android_graphics_GraphicBuffer_lockCanvas },
285     { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z",
286             (void*) android_graphics_GraphicBuffer_unlockCanvasAndPost },
287     { "nCreateFromHardwareBuffer",
288             "(Landroid/hardware/HardwareBuffer;)Landroid/graphics/GraphicBuffer;",
289             (void*) android_graphics_GraphicBuffer_createFromHardwareBuffer }
290 };
291 
register_android_graphics_GraphicBuffer(JNIEnv * env)292 int register_android_graphics_GraphicBuffer(JNIEnv* env) {
293     gGraphicBufferClassInfo.mClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kClassPathName));
294     gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, gGraphicBufferClassInfo.mClass,
295             "mNativeObject", "J");
296     gGraphicBufferClassInfo.mConstructorMethodID = env->GetMethodID(gGraphicBufferClassInfo.mClass,
297             "<init>", "(IIIIJ)V");
298 
299     jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
300     gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
301     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
302     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
303     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
304     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
305 
306     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
307 }
308