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