blob: b6d50898a05792ac1b6de905dd8ad8756427f67e [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"
Steven Moreland60cc6c02017-08-25 15:49:25 -070020#include <nativehelper/JNIHelp.h>
Mathias Agopian113fd302017-05-25 18:31:04 -070021#include <inttypes.h>
Romain Guy3b748a42013-04-17 18:54:38 -070022
23#include "android_os_Parcel.h"
Romain Guy3b748a42013-04-17 18:54:38 -070024#include <binder/Parcel.h>
25
Mathias Agopian113fd302017-05-25 18:31:04 -070026#include <log/log.h>
27
Romain Guy3b748a42013-04-17 18:54:38 -070028#include <ui/GraphicBuffer.h>
29#include <ui/PixelFormat.h>
30
Derek Sollenbergerc287a772019-08-02 13:44:31 -040031#include <android/native_window.h>
32#include <android/graphics/canvas.h>
33#include <android_runtime/android_graphics_GraphicBuffer.h>
34#include <private/android/AHardwareBufferHelpers.h>
Romain Guy3b748a42013-04-17 18:54:38 -070035
36#include <private/gui/ComposerService.h>
37
Andreas Gampe987f79f2014-11-18 17:29:46 -080038#include "core_jni_helpers.h"
39
Romain Guy3b748a42013-04-17 18:54:38 -070040namespace android {
41
42// ----------------------------------------------------------------------------
43// Defines
44// ----------------------------------------------------------------------------
45
46// Debug
Andreas Gampeed6b9df2014-11-20 22:02:20 -080047static const bool kDebugGraphicBuffer = false;
Romain Guy3b748a42013-04-17 18:54:38 -070048
Chih-Hung Hsiehcef190d2016-05-19 15:25:50 -070049#define LOCK_CANVAS_USAGE (GraphicBuffer::USAGE_SW_READ_OFTEN | GraphicBuffer::USAGE_SW_WRITE_OFTEN)
Romain Guy3b748a42013-04-17 18:54:38 -070050
51// ----------------------------------------------------------------------------
52// JNI Helpers
53// ----------------------------------------------------------------------------
54
55static struct {
56 jfieldID mNativeObject;
sergeyv6e3658a2017-01-04 16:57:51 -080057 jclass mClass;
58 jmethodID mConstructorMethodID;
Romain Guy3b748a42013-04-17 18:54:38 -070059} gGraphicBufferClassInfo;
60
61static struct {
62 jmethodID set;
63 jfieldID left;
64 jfieldID top;
65 jfieldID right;
66 jfieldID bottom;
67} gRectClassInfo;
68
Romain Guy3b748a42013-04-17 18:54:38 -070069#define GET_INT(object, field) \
70 env->GetIntField(object, field)
71
72#define SET_INT(object, field, value) \
73 env->SetIntField(object, field, value)
74
Ashok Bhat36bef0b2014-01-20 20:08:01 +000075#define GET_LONG(object, field) \
76 env->GetLongField(object, field)
77
78#define SET_LONG(object, field, value) \
79 env->SetLongField(object, field, value)
80
Romain Guy3b748a42013-04-17 18:54:38 -070081#define INVOKEV(object, method, ...) \
82 env->CallVoidMethod(object, method, __VA_ARGS__)
83
84// ----------------------------------------------------------------------------
85// Types
86// ----------------------------------------------------------------------------
87
88class GraphicBufferWrapper {
89public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -070090 explicit GraphicBufferWrapper(const sp<GraphicBuffer>& buffer): buffer(buffer) {
Mathias Agopian113fd302017-05-25 18:31:04 -070091 LOG_ALWAYS_FATAL_IF(buffer == nullptr, "creating a null GraphicBuffer");
92 }
93 const sp<GraphicBuffer>& get() const {
94 return buffer;
Romain Guy3b748a42013-04-17 18:54:38 -070095 }
96
Mathias Agopian113fd302017-05-25 18:31:04 -070097private:
98 // make sure this is immutable
99 sp<GraphicBuffer> const buffer;
Romain Guy3b748a42013-04-17 18:54:38 -0700100};
101
102// ----------------------------------------------------------------------------
103// GraphicBuffer lifecycle
104// ----------------------------------------------------------------------------
105
Robert Carr6486d312017-01-09 19:48:29 -0800106static jlong android_graphics_GraphicBuffer_wrap(JNIEnv* env, jobject clazz,
107 jlong unwrapped) {
108 sp<GraphicBuffer> b(reinterpret_cast<GraphicBuffer*>(unwrapped));
Mathias Agopian113fd302017-05-25 18:31:04 -0700109 LOG_ALWAYS_FATAL_IF(b == nullptr,
110 "*** android_graphics_GraphicBuffer_wrap() invalid state, b is null, unwrapped=%#" PRIx64, unwrapped);
Robert Carr6486d312017-01-09 19:48:29 -0800111 GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(b);
112 return reinterpret_cast<jlong>(wrapper);
113}
114
sergeyv0a0f2312017-01-04 13:58:52 -0800115static jlong android_graphics_GraphicBuffer_create(JNIEnv* env, jobject clazz,
Romain Guy3b748a42013-04-17 18:54:38 -0700116 jint width, jint height, jint format, jint usage) {
117
Mathias Agopian2bd7d982017-02-27 19:34:45 -0800118 sp<GraphicBuffer> buffer = new GraphicBuffer(
119 uint32_t(width), uint32_t(height), PixelFormat(format), uint32_t(usage),
120 std::string("android_graphics_GraphicBuffer_create pid [") +
121 std::to_string(getpid()) +"]");
Romain Guy3b748a42013-04-17 18:54:38 -0700122
Mathias Agopian2bd7d982017-02-27 19:34:45 -0800123 status_t error = buffer->initCheck();
124 if (error < 0) {
125 ALOGW_IF(kDebugGraphicBuffer, "createGraphicBuffer() failed in GraphicBuffer.create()");
Romain Guy3b748a42013-04-17 18:54:38 -0700126 return NULL;
127 }
128
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000129 GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
130 return reinterpret_cast<jlong>(wrapper);
Romain Guy3b748a42013-04-17 18:54:38 -0700131}
132
sergeyv0a0f2312017-01-04 13:58:52 -0800133static void android_graphics_GraphicBuffer_destroy(JNIEnv* env, jobject clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000134 jlong wrapperHandle) {
135 GraphicBufferWrapper* wrapper =
136 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
Romain Guy3b748a42013-04-17 18:54:38 -0700137 delete wrapper;
138}
139
140// ----------------------------------------------------------------------------
141// Canvas management
142// ----------------------------------------------------------------------------
143
sergeyv0a0f2312017-01-04 13:58:52 -0800144static jboolean android_graphics_GraphicBuffer_lockCanvas(JNIEnv* env, jobject,
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400145 jlong wrapperHandle, jobject canvasObj, jobject dirtyRect) {
Romain Guy3b748a42013-04-17 18:54:38 -0700146
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000147 GraphicBufferWrapper* wrapper =
148 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
Romain Guy3b748a42013-04-17 18:54:38 -0700149 if (!wrapper) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000150 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700151 }
152
Mathias Agopian113fd302017-05-25 18:31:04 -0700153 sp<GraphicBuffer> buffer(wrapper->get());
Romain Guy3b748a42013-04-17 18:54:38 -0700154
Pablo Ceballos82702922015-08-07 17:28:03 -0700155 Rect rect(Rect::EMPTY_RECT);
Romain Guy3b748a42013-04-17 18:54:38 -0700156 if (dirtyRect) {
157 rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
158 rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
159 rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
160 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
161 } else {
162 rect.set(Rect(buffer->getWidth(), buffer->getHeight()));
163 }
164
165 void* bits = NULL;
166 status_t status = buffer->lock(LOCK_CANVAS_USAGE, rect, &bits);
167
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000168 if (status) return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700169 if (!bits) {
170 buffer->unlock();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000171 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700172 }
173
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400174 ANativeWindow_Buffer nativeBuffer;
175 nativeBuffer.width = buffer->getWidth();
176 nativeBuffer.height = buffer->getHeight();
177 nativeBuffer.stride = buffer->getStride();
178 nativeBuffer.format = AHardwareBuffer_convertFromPixelFormat(buffer->getPixelFormat());
179 nativeBuffer.bits = bits;
Romain Guy3b748a42013-04-17 18:54:38 -0700180
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400181 graphics::Canvas canvas(env, canvasObj);
182 canvas.setBuffer(&nativeBuffer, ADATASPACE_UNKNOWN);
183 canvas.clipRect({rect.left, rect.top, rect.right, rect.bottom});
Romain Guy3b748a42013-04-17 18:54:38 -0700184
185 if (dirtyRect) {
186 INVOKEV(dirtyRect, gRectClassInfo.set,
187 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
188 }
189
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000190 return JNI_TRUE;
Romain Guy3b748a42013-04-17 18:54:38 -0700191}
192
sergeyv0a0f2312017-01-04 13:58:52 -0800193static jboolean android_graphics_GraphicBuffer_unlockCanvasAndPost(JNIEnv* env, jobject,
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400194 jlong wrapperHandle, jobject canvasObj) {
195 // release the buffer from the canvas
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400196 graphics::Canvas canvas(env, canvasObj);
197 canvas.setBuffer(nullptr, ADATASPACE_UNKNOWN);
Romain Guy3b748a42013-04-17 18:54:38 -0700198
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000199 GraphicBufferWrapper* wrapper =
200 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
Romain Guy3b748a42013-04-17 18:54:38 -0700201 if (wrapper) {
Mathias Agopian113fd302017-05-25 18:31:04 -0700202 status_t status = wrapper->get()->unlock();
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000203 return status == 0 ? JNI_TRUE : JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700204 }
205
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000206 return JNI_FALSE;
Romain Guy3b748a42013-04-17 18:54:38 -0700207}
208
209// ----------------------------------------------------------------------------
210// Serialization
211// ----------------------------------------------------------------------------
212
sergeyv0a0f2312017-01-04 13:58:52 -0800213static void android_graphics_GraphicBuffer_write(JNIEnv* env, jobject clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000214 jlong wrapperHandle, jobject dest) {
Mathias Agopian113fd302017-05-25 18:31:04 -0700215
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000216 GraphicBufferWrapper* wrapper =
217 reinterpret_cast<GraphicBufferWrapper*>(wrapperHandle);
Romain Guy3b748a42013-04-17 18:54:38 -0700218 Parcel* parcel = parcelForJavaObject(env, dest);
219 if (parcel) {
Mathias Agopian113fd302017-05-25 18:31:04 -0700220 parcel->write(*wrapper->get());
Romain Guy3b748a42013-04-17 18:54:38 -0700221 }
222}
223
sergeyv0a0f2312017-01-04 13:58:52 -0800224static jlong android_graphics_GraphicBuffer_read(JNIEnv* env, jobject clazz,
Romain Guy3b748a42013-04-17 18:54:38 -0700225 jobject in) {
226
227 Parcel* parcel = parcelForJavaObject(env, in);
228 if (parcel) {
229 sp<GraphicBuffer> buffer = new GraphicBuffer();
230 parcel->read(*buffer);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000231 return reinterpret_cast<jlong>(new GraphicBufferWrapper(buffer));
Romain Guy3b748a42013-04-17 18:54:38 -0700232 }
233
234 return NULL;
235}
236
237// ----------------------------------------------------------------------------
238// External helpers
239// ----------------------------------------------------------------------------
240
Derek Sollenbergere78f7c92019-07-31 15:18:47 -0400241sp<GraphicBuffer> android_graphics_GraphicBuffer_getNativeGraphicsBuffer(JNIEnv* env, jobject obj) {
Romain Guy3b748a42013-04-17 18:54:38 -0700242 if (obj) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000243 jlong nativeObject = env->GetLongField(obj, gGraphicBufferClassInfo.mNativeObject);
Romain Guy3b748a42013-04-17 18:54:38 -0700244 GraphicBufferWrapper* wrapper = (GraphicBufferWrapper*) nativeObject;
245 if (wrapper != NULL) {
Mathias Agopian113fd302017-05-25 18:31:04 -0700246 sp<GraphicBuffer> buffer(wrapper->get());
Romain Guy3b748a42013-04-17 18:54:38 -0700247 return buffer;
248 }
249 }
250 return NULL;
251}
sergeyv6e3658a2017-01-04 16:57:51 -0800252
Derek Sollenbergere78f7c92019-07-31 15:18:47 -0400253jobject android_graphics_GraphicBuffer_createFromAHardwareBuffer(JNIEnv* env,
254 AHardwareBuffer* hardwareBuffer) {
255 GraphicBuffer* buffer = GraphicBuffer::fromAHardwareBuffer(hardwareBuffer);
sergeyv6e3658a2017-01-04 16:57:51 -0800256 GraphicBufferWrapper* wrapper = new GraphicBufferWrapper(buffer);
257 jobject obj = env->NewObject(gGraphicBufferClassInfo.mClass,
258 gGraphicBufferClassInfo.mConstructorMethodID, buffer->getWidth(), buffer->getHeight(),
Mathias Agopian113fd302017-05-25 18:31:04 -0700259 buffer->getPixelFormat(), (jint)buffer->getUsage(), reinterpret_cast<jlong>(wrapper));
sergeyv6e3658a2017-01-04 16:57:51 -0800260 return obj;
261}
262
sergeyv0a0f2312017-01-04 13:58:52 -0800263};
Romain Guy3b748a42013-04-17 18:54:38 -0700264
sergeyv0a0f2312017-01-04 13:58:52 -0800265using namespace android;
Romain Guy3b748a42013-04-17 18:54:38 -0700266// ----------------------------------------------------------------------------
267// JNI Glue
268// ----------------------------------------------------------------------------
269
sergeyv0a0f2312017-01-04 13:58:52 -0800270const char* const kClassPathName = "android/graphics/GraphicBuffer";
Romain Guy3b748a42013-04-17 18:54:38 -0700271
Daniel Micay76f6a862015-09-19 17:31:01 -0400272static const JNINativeMethod gMethods[] = {
sergeyv0a0f2312017-01-04 13:58:52 -0800273 { "nCreateGraphicBuffer", "(IIII)J", (void*) android_graphics_GraphicBuffer_create },
274 { "nDestroyGraphicBuffer", "(J)V", (void*) android_graphics_GraphicBuffer_destroy },
Romain Guy3b748a42013-04-17 18:54:38 -0700275
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000276 { "nWriteGraphicBufferToParcel", "(JLandroid/os/Parcel;)V",
sergeyv0a0f2312017-01-04 13:58:52 -0800277 (void*) android_graphics_GraphicBuffer_write },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000278 { "nReadGraphicBufferFromParcel", "(Landroid/os/Parcel;)J",
sergeyv0a0f2312017-01-04 13:58:52 -0800279 (void*) android_graphics_GraphicBuffer_read },
Romain Guy3b748a42013-04-17 18:54:38 -0700280
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000281 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
sergeyv0a0f2312017-01-04 13:58:52 -0800282 (void*) android_graphics_GraphicBuffer_lockCanvas },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000283 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)Z",
Robert Carr6486d312017-01-09 19:48:29 -0800284 (void*) android_graphics_GraphicBuffer_unlockCanvasAndPost },
285 { "nWrapGraphicBuffer", "(J)J",
286 (void*) android_graphics_GraphicBuffer_wrap }
Romain Guy3b748a42013-04-17 18:54:38 -0700287};
288
sergeyv0a0f2312017-01-04 13:58:52 -0800289int register_android_graphics_GraphicBuffer(JNIEnv* env) {
sergeyv6e3658a2017-01-04 16:57:51 -0800290 gGraphicBufferClassInfo.mClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kClassPathName));
291 gGraphicBufferClassInfo.mNativeObject = GetFieldIDOrDie(env, gGraphicBufferClassInfo.mClass,
292 "mNativeObject", "J");
293 gGraphicBufferClassInfo.mConstructorMethodID = env->GetMethodID(gGraphicBufferClassInfo.mClass,
294 "<init>", "(IIIIJ)V");
Romain Guy3b748a42013-04-17 18:54:38 -0700295
sergeyv6e3658a2017-01-04 16:57:51 -0800296 jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
Andreas Gampe987f79f2014-11-18 17:29:46 -0800297 gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
298 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
299 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
300 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
301 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
Romain Guy3b748a42013-04-17 18:54:38 -0700302
Andreas Gampe987f79f2014-11-18 17:29:46 -0800303 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
Robert Carr6486d312017-01-09 19:48:29 -0800304}