blob: a12629f7d9ba66da314bfcd727421f2371c01479 [file] [log] [blame]
Romain Guy3b748a42013-04-17 18:54:38 -07001/*
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 "JNIHelp.h"
21
22#include "android_os_Parcel.h"
23#include "android_view_GraphicBuffer.h"
Florin Malita5c3d9272014-05-08 10:35:36 -040024#include "android/graphics/GraphicsJNI.h"
Romain Guy3b748a42013-04-17 18:54:38 -070025
26#include <android_runtime/AndroidRuntime.h>
27
28#include <binder/Parcel.h>
29
30#include <ui/GraphicBuffer.h>
31#include <ui/PixelFormat.h>
32
33#include <gui/IGraphicBufferAlloc.h>
34#include <gui/ISurfaceComposer.h>
35
36#include <SkCanvas.h>
37#include <SkBitmap.h>
38
39#include <private/gui/ComposerService.h>
40
Andreas Gampe987f79f2014-11-18 17:29:46 -080041#include "core_jni_helpers.h"
42
Romain Guy3b748a42013-04-17 18:54:38 -070043namespace android {
44
45// ----------------------------------------------------------------------------
46// Defines
47// ----------------------------------------------------------------------------
48
49// Debug
Andreas Gampeed6b9df2014-11-20 22:02:20 -080050static const bool kDebugGraphicBuffer = false;
Romain Guy3b748a42013-04-17 18:54:38 -070051
52#define LOCK_CANVAS_USAGE GraphicBuffer::USAGE_SW_READ_OFTEN | GraphicBuffer::USAGE_SW_WRITE_OFTEN
53
54// ----------------------------------------------------------------------------
55// JNI Helpers
56// ----------------------------------------------------------------------------
57
58static struct {
59 jfieldID mNativeObject;
60} gGraphicBufferClassInfo;
61
62static struct {
63 jmethodID set;
64 jfieldID left;
65 jfieldID top;
66 jfieldID right;
67 jfieldID bottom;
68} gRectClassInfo;
69
John Reck9d4efdf2015-04-17 20:45:40 +000070static struct {
71 jfieldID mSurfaceFormat;
72 jmethodID setNativeBitmap;
73} gCanvasClassInfo;
74
Romain Guy3b748a42013-04-17 18:54:38 -070075#define GET_INT(object, field) \
76 env->GetIntField(object, field)
77
78#define SET_INT(object, field, value) \
79 env->SetIntField(object, field, value)
80
Ashok Bhat36bef0b2014-01-20 20:08:01 +000081#define GET_LONG(object, field) \
82 env->GetLongField(object, field)
83
84#define SET_LONG(object, field, value) \
85 env->SetLongField(object, field, value)
86
Romain Guy3b748a42013-04-17 18:54:38 -070087#define INVOKEV(object, method, ...) \
88 env->CallVoidMethod(object, method, __VA_ARGS__)
89
90// ----------------------------------------------------------------------------
91// Types
92// ----------------------------------------------------------------------------
93
94class GraphicBufferWrapper {
95public:
96 GraphicBufferWrapper(const sp<GraphicBuffer>& buffer): buffer(buffer) {
97 }
98
99 sp<GraphicBuffer> buffer;
100};
101
102// ----------------------------------------------------------------------------
103// GraphicBuffer lifecycle
104// ----------------------------------------------------------------------------
105
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000106static jlong android_view_GraphiceBuffer_create(JNIEnv* env, jobject clazz,
Romain Guy3b748a42013-04-17 18:54:38 -0700107 jint width, jint height, jint format, jint usage) {
108
109 sp<ISurfaceComposer> composer(ComposerService::getComposerService());
110 sp<IGraphicBufferAlloc> alloc(composer->createGraphicBufferAlloc());
111 if (alloc == NULL) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800112 if (kDebugGraphicBuffer) {
113 ALOGW("createGraphicBufferAlloc() failed in GraphicBuffer.create()");
114 }
Romain Guy3b748a42013-04-17 18:54:38 -0700115 return NULL;
116 }
117
118 status_t error;
119 sp<GraphicBuffer> buffer(alloc->createGraphicBuffer(width, height, format, usage, &error));
120 if (buffer == NULL) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800121 if (kDebugGraphicBuffer) {
122 ALOGW("createGraphicBuffer() failed in GraphicBuffer.create()");
123 }
Romain Guy3b748a42013-04-17 18:54:38 -0700124 return NULL;
125 }
126
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000127 GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
128 return reinterpret_cast<jlong>(wrapper);
Romain Guy3b748a42013-04-17 18:54:38 -0700129}
130
131static void android_view_GraphiceBuffer_destroy(JNIEnv* env, jobject clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000132 jlong wrapperHandle) {
133 GraphicBufferWrapper* wrapper =
134 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
Romain Guy3b748a42013-04-17 18:54:38 -0700135 delete wrapper;
136}
137
138// ----------------------------------------------------------------------------
139// Canvas management
140// ----------------------------------------------------------------------------
141
Mike Reedb9330552014-06-16 17:31:48 -0400142static inline SkColorType convertPixelFormat(int32_t format) {
Romain Guy3b748a42013-04-17 18:54:38 -0700143 switch (format) {
144 case PIXEL_FORMAT_RGBA_8888:
Mike Reedb9330552014-06-16 17:31:48 -0400145 return kN32_SkColorType;
Romain Guy3b748a42013-04-17 18:54:38 -0700146 case PIXEL_FORMAT_RGBX_8888:
Mike Reedb9330552014-06-16 17:31:48 -0400147 return kN32_SkColorType;
Romain Guy3b748a42013-04-17 18:54:38 -0700148 case PIXEL_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -0400149 return kRGB_565_SkColorType;
Romain Guy3b748a42013-04-17 18:54:38 -0700150 default:
Mike Reedb9330552014-06-16 17:31:48 -0400151 return kUnknown_SkColorType;
Romain Guy3b748a42013-04-17 18:54:38 -0700152 }
153}
154
155static jboolean android_view_GraphicBuffer_lockCanvas(JNIEnv* env, jobject,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000156 jlong wrapperHandle, jobject canvas, jobject dirtyRect) {
Romain Guy3b748a42013-04-17 18:54:38 -0700157
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000158 GraphicBufferWrapper* wrapper =
159 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
Romain Guy3b748a42013-04-17 18:54:38 -0700160 if (!wrapper) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000161 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700162 }
163
164 sp<GraphicBuffer> buffer(wrapper->buffer);
165
166 Rect rect;
167 if (dirtyRect) {
168 rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
169 rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
170 rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
171 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
172 } else {
173 rect.set(Rect(buffer->getWidth(), buffer->getHeight()));
174 }
175
176 void* bits = NULL;
177 status_t status = buffer->lock(LOCK_CANVAS_USAGE, rect, &bits);
178
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000179 if (status) return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700180 if (!bits) {
181 buffer->unlock();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000182 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700183 }
184
185 ssize_t bytesCount = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat());
186
187 SkBitmap bitmap;
Mike Reedb9330552014-06-16 17:31:48 -0400188 bitmap.setInfo(SkImageInfo::Make(buffer->getWidth(), buffer->getHeight(),
189 convertPixelFormat(buffer->getPixelFormat()),
190 kPremul_SkAlphaType),
191 bytesCount);
Romain Guy3b748a42013-04-17 18:54:38 -0700192
193 if (buffer->getWidth() > 0 && buffer->getHeight() > 0) {
194 bitmap.setPixels(bits);
195 } else {
196 bitmap.setPixels(NULL);
197 }
198
John Reck9d4efdf2015-04-17 20:45:40 +0000199 SET_INT(canvas, gCanvasClassInfo.mSurfaceFormat, buffer->getPixelFormat());
200 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, reinterpret_cast<jlong>(&bitmap));
201
202 SkRect clipRect;
203 clipRect.set(rect.left, rect.top, rect.right, rect.bottom);
204 SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
205 nativeCanvas->clipRect(clipRect);
Romain Guy3b748a42013-04-17 18:54:38 -0700206
207 if (dirtyRect) {
208 INVOKEV(dirtyRect, gRectClassInfo.set,
209 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
210 }
211
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000212 return JNI_TRUE;
Romain Guy3b748a42013-04-17 18:54:38 -0700213}
214
215static jboolean android_view_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobject,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000216 jlong wrapperHandle, jobject canvas) {
Romain Guy3b748a42013-04-17 18:54:38 -0700217
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000218 GraphicBufferWrapper* wrapper =
219 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
John Reck9d4efdf2015-04-17 20:45:40 +0000220 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, (jlong)0);
Romain Guy3b748a42013-04-17 18:54:38 -0700221
222 if (wrapper) {
223 status_t status = wrapper->buffer->unlock();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000224 return status == 0 ? JNI_TRUE : JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700225 }
226
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000227 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700228}
229
230// ----------------------------------------------------------------------------
231// Serialization
232// ----------------------------------------------------------------------------
233
234static void android_view_GraphiceBuffer_write(JNIEnv* env, jobject clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000235 jlong wrapperHandle, jobject dest) {
236 GraphicBufferWrapper* wrapper =
237 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
Romain Guy3b748a42013-04-17 18:54:38 -0700238 Parcel* parcel = parcelForJavaObject(env, dest);
239 if (parcel) {
240 parcel->write(*wrapper->buffer);
241 }
242}
243
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000244static jlong android_view_GraphiceBuffer_read(JNIEnv* env, jobject clazz,
Romain Guy3b748a42013-04-17 18:54:38 -0700245 jobject in) {
246
247 Parcel* parcel = parcelForJavaObject(env, in);
248 if (parcel) {
249 sp<GraphicBuffer> buffer = new GraphicBuffer();
250 parcel->read(*buffer);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000251 return reinterpret_cast<jlong>(new GraphicBufferWrapper(buffer));
Romain Guy3b748a42013-04-17 18:54:38 -0700252 }
253
254 return NULL;
255}
256
257// ----------------------------------------------------------------------------
258// External helpers
259// ----------------------------------------------------------------------------
260
261sp<GraphicBuffer> graphicBufferForJavaObject(JNIEnv* env, jobject obj) {
262 if (obj) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000263 jlong nativeObject = env->GetLongField(obj, gGraphicBufferClassInfo.mNativeObject);
Romain Guy3b748a42013-04-17 18:54:38 -0700264 GraphicBufferWrapper* wrapper = (GraphicBufferWrapper*) nativeObject;
265 if (wrapper != NULL) {
266 sp<GraphicBuffer> buffer(wrapper->buffer);
267 return buffer;
268 }
269 }
270 return NULL;
271}
272
273// ----------------------------------------------------------------------------
274// JNI Glue
275// ----------------------------------------------------------------------------
276
Romain Guy3b748a42013-04-17 18:54:38 -0700277const char* const kClassPathName = "android/view/GraphicBuffer";
278
279static JNINativeMethod gMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000280 { "nCreateGraphicBuffer", "(IIII)J", (void*) android_view_GraphiceBuffer_create },
281 { "nDestroyGraphicBuffer", "(J)V", (void*) android_view_GraphiceBuffer_destroy },
Romain Guy3b748a42013-04-17 18:54:38 -0700282
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000283 { "nWriteGraphicBufferToParcel", "(JLandroid/os/Parcel;)V",
Romain Guy3b748a42013-04-17 18:54:38 -0700284 (void*) android_view_GraphiceBuffer_write },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000285 { "nReadGraphicBufferFromParcel", "(Landroid/os/Parcel;)J",
Romain Guy3b748a42013-04-17 18:54:38 -0700286 (void*) android_view_GraphiceBuffer_read },
287
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000288 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
Romain Guy3b748a42013-04-17 18:54:38 -0700289 (void*) android_view_GraphicBuffer_lockCanvas },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000290 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z",
Romain Guy3b748a42013-04-17 18:54:38 -0700291 (void*) android_view_GraphicBuffer_unlockCanvasAndPost },
292};
293
294int register_android_view_GraphicBuffer(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800295 jclass clazz = FindClassOrDie(env, "android/view/GraphicBuffer");
296 gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, clazz, "mNativeObject", "J");
Romain Guy3b748a42013-04-17 18:54:38 -0700297
Andreas Gampe987f79f2014-11-18 17:29:46 -0800298 clazz = FindClassOrDie(env, "android/graphics/Rect");
299 gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
300 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
301 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
302 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
303 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
Romain Guy3b748a42013-04-17 18:54:38 -0700304
John Reck9d4efdf2015-04-17 20:45:40 +0000305 clazz = FindClassOrDie(env, "android/graphics/Canvas");
306 gCanvasClassInfo.mSurfaceFormat = GetFieldIDOrDie(env, clazz, "mSurfaceFormat", "I");
307 gCanvasClassInfo.setNativeBitmap = GetMethodIDOrDie(env, clazz, "setNativeBitmap", "(J)V");
308
Andreas Gampe987f79f2014-11-18 17:29:46 -0800309 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
Romain Guy3b748a42013-04-17 18:54:38 -0700310}
311
312};