blob: 031e373241cbd324aa87de6c696abf78b60b7e01 [file] [log] [blame]
Zhijun Hef6a09e52015-02-24 18:12:23 -08001/*
2 * Copyright 2015 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_NDEBUG 0
18#define LOG_TAG "ImageWriter_JNI"
Zhijun He0ab41622016-02-25 16:00:38 -080019#include "android_media_Utils.h"
20
Zhijun Hef6a09e52015-02-24 18:12:23 -080021#include <utils/Log.h>
22#include <utils/String8.h>
23
24#include <gui/IProducerListener.h>
25#include <gui/Surface.h>
Zhijun Hef6a09e52015-02-24 18:12:23 -080026#include <android_runtime/AndroidRuntime.h>
27#include <android_runtime/android_view_Surface.h>
rennbe092192018-05-07 10:18:05 -070028#include <android_runtime/android_hardware_HardwareBuffer.h>
29#include <private/android/AHardwareBufferHelpers.h>
Zhijun Hef6a09e52015-02-24 18:12:23 -080030#include <jni.h>
Steven Moreland2279b252017-07-19 09:50:45 -070031#include <nativehelper/JNIHelp.h>
Zhijun Hef6a09e52015-02-24 18:12:23 -080032
33#include <stdint.h>
34#include <inttypes.h>
rennbe092192018-05-07 10:18:05 -070035#include <android/hardware_buffer_jni.h>
Zhijun Hef6a09e52015-02-24 18:12:23 -080036
Zhijun Hef6a09e52015-02-24 18:12:23 -080037#define IMAGE_BUFFER_JNI_ID "mNativeBuffer"
Zhijun He916d8ac2017-03-22 17:33:47 -070038#define IMAGE_FORMAT_UNKNOWN 0 // This is the same value as ImageFormat#UNKNOWN.
Zhijun Hef6a09e52015-02-24 18:12:23 -080039// ----------------------------------------------------------------------------
40
41using namespace android;
42
Zhijun Hef6a09e52015-02-24 18:12:23 -080043static struct {
44 jmethodID postEventFromNative;
45 jfieldID mWriterFormat;
46} gImageWriterClassInfo;
47
48static struct {
49 jfieldID mNativeBuffer;
50 jfieldID mNativeFenceFd;
51 jfieldID mPlanes;
52} gSurfaceImageClassInfo;
53
54static struct {
55 jclass clazz;
56 jmethodID ctor;
57} gSurfacePlaneClassInfo;
58
Zhijun Hef6a09e52015-02-24 18:12:23 -080059// ----------------------------------------------------------------------------
60
61class JNIImageWriterContext : public BnProducerListener {
62public:
63 JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz);
64
65 virtual ~JNIImageWriterContext();
66
67 // Implementation of IProducerListener, used to notify the ImageWriter that the consumer
68 // has returned a buffer and it is ready for ImageWriter to dequeue.
69 virtual void onBufferReleased();
70
Zhijun Hece9d6f92015-03-29 16:33:59 -070071 void setProducer(const sp<Surface>& producer) { mProducer = producer; }
72 Surface* getProducer() { return mProducer.get(); }
Zhijun Hef6a09e52015-02-24 18:12:23 -080073
74 void setBufferFormat(int format) { mFormat = format; }
75 int getBufferFormat() { return mFormat; }
76
77 void setBufferWidth(int width) { mWidth = width; }
78 int getBufferWidth() { return mWidth; }
79
80 void setBufferHeight(int height) { mHeight = height; }
81 int getBufferHeight() { return mHeight; }
82
83private:
84 static JNIEnv* getJNIEnv(bool* needsDetach);
85 static void detachJNI();
86
Zhijun Hece9d6f92015-03-29 16:33:59 -070087 sp<Surface> mProducer;
Zhijun Hef6a09e52015-02-24 18:12:23 -080088 jobject mWeakThiz;
89 jclass mClazz;
90 int mFormat;
91 int mWidth;
92 int mHeight;
93};
94
95JNIImageWriterContext::JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz) :
96 mWeakThiz(env->NewGlobalRef(weakThiz)),
97 mClazz((jclass)env->NewGlobalRef(clazz)),
98 mFormat(0),
99 mWidth(-1),
100 mHeight(-1) {
101}
102
103JNIImageWriterContext::~JNIImageWriterContext() {
104 ALOGV("%s", __FUNCTION__);
105 bool needsDetach = false;
106 JNIEnv* env = getJNIEnv(&needsDetach);
107 if (env != NULL) {
108 env->DeleteGlobalRef(mWeakThiz);
109 env->DeleteGlobalRef(mClazz);
110 } else {
111 ALOGW("leaking JNI object references");
112 }
113 if (needsDetach) {
114 detachJNI();
115 }
116
117 mProducer.clear();
118}
119
120JNIEnv* JNIImageWriterContext::getJNIEnv(bool* needsDetach) {
121 ALOGV("%s", __FUNCTION__);
122 LOG_ALWAYS_FATAL_IF(needsDetach == NULL, "needsDetach is null!!!");
123 *needsDetach = false;
124 JNIEnv* env = AndroidRuntime::getJNIEnv();
125 if (env == NULL) {
126 JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL};
127 JavaVM* vm = AndroidRuntime::getJavaVM();
128 int result = vm->AttachCurrentThread(&env, (void*) &args);
129 if (result != JNI_OK) {
130 ALOGE("thread attach failed: %#x", result);
131 return NULL;
132 }
133 *needsDetach = true;
134 }
135 return env;
136}
137
138void JNIImageWriterContext::detachJNI() {
139 ALOGV("%s", __FUNCTION__);
140 JavaVM* vm = AndroidRuntime::getJavaVM();
141 int result = vm->DetachCurrentThread();
142 if (result != JNI_OK) {
143 ALOGE("thread detach failed: %#x", result);
144 }
145}
146
147void JNIImageWriterContext::onBufferReleased() {
148 ALOGV("%s: buffer released", __FUNCTION__);
149 bool needsDetach = false;
150 JNIEnv* env = getJNIEnv(&needsDetach);
151 if (env != NULL) {
Zhijun Hece9d6f92015-03-29 16:33:59 -0700152 // Detach the buffer every time when a buffer consumption is done,
153 // need let this callback give a BufferItem, then only detach if it was attached to this
154 // Writer. Do the detach unconditionally for opaque format now. see b/19977520
155 if (mFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
156 sp<Fence> fence;
Dan Stoza3316452f2015-04-27 11:31:12 -0700157 sp<GraphicBuffer> buffer;
Zhijun Hece9d6f92015-03-29 16:33:59 -0700158 ALOGV("%s: One buffer is detached", __FUNCTION__);
159 mProducer->detachNextBuffer(&buffer, &fence);
160 }
161
Zhijun Hef6a09e52015-02-24 18:12:23 -0800162 env->CallStaticVoidMethod(mClazz, gImageWriterClassInfo.postEventFromNative, mWeakThiz);
163 } else {
164 ALOGW("onBufferReleased event will not posted");
165 }
Zhijun Hece9d6f92015-03-29 16:33:59 -0700166
Zhijun Hef6a09e52015-02-24 18:12:23 -0800167 if (needsDetach) {
168 detachJNI();
169 }
170}
171
172// ----------------------------------------------------------------------------
173
174extern "C" {
175
176// -------------------------------Private method declarations--------------
177
Zhijun Hef6a09e52015-02-24 18:12:23 -0800178static void Image_setNativeContext(JNIEnv* env, jobject thiz,
179 sp<GraphicBuffer> buffer, int fenceFd);
180static void Image_getNativeContext(JNIEnv* env, jobject thiz,
181 GraphicBuffer** buffer, int* fenceFd);
182static void Image_unlockIfLocked(JNIEnv* env, jobject thiz);
183
184// --------------------------ImageWriter methods---------------------------------------
185
186static void ImageWriter_classInit(JNIEnv* env, jclass clazz) {
187 ALOGV("%s:", __FUNCTION__);
188 jclass imageClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage");
189 LOG_ALWAYS_FATAL_IF(imageClazz == NULL,
190 "can't find android/media/ImageWriter$WriterSurfaceImage");
191 gSurfaceImageClassInfo.mNativeBuffer = env->GetFieldID(
192 imageClazz, IMAGE_BUFFER_JNI_ID, "J");
193 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeBuffer == NULL,
194 "can't find android/media/ImageWriter$WriterSurfaceImage.%s", IMAGE_BUFFER_JNI_ID);
195
196 gSurfaceImageClassInfo.mNativeFenceFd = env->GetFieldID(
197 imageClazz, "mNativeFenceFd", "I");
198 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeFenceFd == NULL,
199 "can't find android/media/ImageWriter$WriterSurfaceImage.mNativeFenceFd");
200
201 gSurfaceImageClassInfo.mPlanes = env->GetFieldID(
202 imageClazz, "mPlanes", "[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;");
203 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mPlanes == NULL,
204 "can't find android/media/ImageWriter$WriterSurfaceImage.mPlanes");
205
206 gImageWriterClassInfo.postEventFromNative = env->GetStaticMethodID(
207 clazz, "postEventFromNative", "(Ljava/lang/Object;)V");
208 LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.postEventFromNative == NULL,
209 "can't find android/media/ImageWriter.postEventFromNative");
210
211 gImageWriterClassInfo.mWriterFormat = env->GetFieldID(
212 clazz, "mWriterFormat", "I");
213 LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.mWriterFormat == NULL,
214 "can't find android/media/ImageWriter.mWriterFormat");
215
216 jclass planeClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage$SurfacePlane");
217 LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find SurfacePlane class");
218 // FindClass only gives a local reference of jclass object.
219 gSurfacePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz);
220 gSurfacePlaneClassInfo.ctor = env->GetMethodID(gSurfacePlaneClassInfo.clazz, "<init>",
221 "(Landroid/media/ImageWriter$WriterSurfaceImage;IILjava/nio/ByteBuffer;)V");
222 LOG_ALWAYS_FATAL_IF(gSurfacePlaneClassInfo.ctor == NULL,
223 "Can not find SurfacePlane constructor");
224}
225
226static jlong ImageWriter_init(JNIEnv* env, jobject thiz, jobject weakThiz, jobject jsurface,
Zhijun He916d8ac2017-03-22 17:33:47 -0700227 jint maxImages, jint userFormat) {
Zhijun Hef6a09e52015-02-24 18:12:23 -0800228 status_t res;
229
230 ALOGV("%s: maxImages:%d", __FUNCTION__, maxImages);
231
232 sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
233 if (surface == NULL) {
234 jniThrowException(env,
235 "java/lang/IllegalArgumentException",
236 "The surface has been released");
237 return 0;
238 }
239 sp<IGraphicBufferProducer> bufferProducer = surface->getIGraphicBufferProducer();
240
241 jclass clazz = env->GetObjectClass(thiz);
242 if (clazz == NULL) {
243 jniThrowRuntimeException(env, "Can't find android/graphics/ImageWriter");
244 return 0;
245 }
246 sp<JNIImageWriterContext> ctx(new JNIImageWriterContext(env, weakThiz, clazz));
247
248 sp<Surface> producer = new Surface(bufferProducer, /*controlledByApp*/false);
249 ctx->setProducer(producer);
250 /**
251 * NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not connectable
252 * after disconnect. MEDIA or CAMERA are treated the same internally. The producer listener
253 * will be cleared after disconnect call.
254 */
Emilian Peev2adf4032018-06-05 17:52:16 +0100255 res = producer->connect(/*api*/NATIVE_WINDOW_API_CAMERA, /*listener*/ctx);
256 if (res != OK) {
257 ALOGE("%s: Connecting to surface producer interface failed: %s (%d)",
258 __FUNCTION__, strerror(-res), res);
259 jniThrowRuntimeException(env, "Failed to connect to native window");
260 return 0;
261 }
262
Zhijun Hef6a09e52015-02-24 18:12:23 -0800263 jlong nativeCtx = reinterpret_cast<jlong>(ctx.get());
264
265 // Get the dimension and format of the producer.
266 sp<ANativeWindow> anw = producer;
Zhijun He916d8ac2017-03-22 17:33:47 -0700267 int32_t width, height, surfaceFormat;
Zhijun Hef6a09e52015-02-24 18:12:23 -0800268 if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
269 ALOGE("%s: Query Surface width failed: %s (%d)", __FUNCTION__, strerror(-res), res);
270 jniThrowRuntimeException(env, "Failed to query Surface width");
271 return 0;
272 }
273 ctx->setBufferWidth(width);
274
275 if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) {
276 ALOGE("%s: Query Surface height failed: %s (%d)", __FUNCTION__, strerror(-res), res);
277 jniThrowRuntimeException(env, "Failed to query Surface height");
278 return 0;
279 }
280 ctx->setBufferHeight(height);
281
Zhijun He916d8ac2017-03-22 17:33:47 -0700282 // Query surface format if no valid user format is specified, otherwise, override surface format
283 // with user format.
284 if (userFormat == IMAGE_FORMAT_UNKNOWN) {
285 if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &surfaceFormat)) != OK) {
286 ALOGE("%s: Query Surface format failed: %s (%d)", __FUNCTION__, strerror(-res), res);
287 jniThrowRuntimeException(env, "Failed to query Surface format");
288 return 0;
289 }
290 } else {
291 surfaceFormat = userFormat;
Zhijun Hef6a09e52015-02-24 18:12:23 -0800292 }
Zhijun He916d8ac2017-03-22 17:33:47 -0700293 ctx->setBufferFormat(surfaceFormat);
294 env->SetIntField(thiz,
295 gImageWriterClassInfo.mWriterFormat, reinterpret_cast<jint>(surfaceFormat));
Zhijun Hef6a09e52015-02-24 18:12:23 -0800296
Zhijun He916d8ac2017-03-22 17:33:47 -0700297 if (!isFormatOpaque(surfaceFormat)) {
Zhijun Hef6a09e52015-02-24 18:12:23 -0800298 res = native_window_set_usage(anw.get(), GRALLOC_USAGE_SW_WRITE_OFTEN);
299 if (res != OK) {
300 ALOGE("%s: Configure usage %08x for format %08x failed: %s (%d)",
Dan Albert1102e212015-06-09 17:35:38 -0700301 __FUNCTION__, static_cast<unsigned int>(GRALLOC_USAGE_SW_WRITE_OFTEN),
Zhijun He916d8ac2017-03-22 17:33:47 -0700302 surfaceFormat, strerror(-res), res);
Zhijun Hef6a09e52015-02-24 18:12:23 -0800303 jniThrowRuntimeException(env, "Failed to SW_WRITE_OFTEN configure usage");
304 return 0;
305 }
306 }
307
308 int minUndequeuedBufferCount = 0;
309 res = anw->query(anw.get(),
310 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufferCount);
311 if (res != OK) {
312 ALOGE("%s: Query producer undequeued buffer count failed: %s (%d)",
313 __FUNCTION__, strerror(-res), res);
314 jniThrowRuntimeException(env, "Query producer undequeued buffer count failed");
315 return 0;
316 }
317
318 size_t totalBufferCount = maxImages + minUndequeuedBufferCount;
319 res = native_window_set_buffer_count(anw.get(), totalBufferCount);
320 if (res != OK) {
321 ALOGE("%s: Set buffer count failed: %s (%d)", __FUNCTION__, strerror(-res), res);
322 jniThrowRuntimeException(env, "Set buffer count failed");
323 return 0;
324 }
325
326 if (ctx != 0) {
327 ctx->incStrong((void*)ImageWriter_init);
328 }
329 return nativeCtx;
330}
331
332static void ImageWriter_dequeueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) {
333 ALOGV("%s", __FUNCTION__);
334 JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
335 if (ctx == NULL || thiz == NULL) {
336 jniThrowException(env, "java/lang/IllegalStateException",
337 "ImageWriterContext is not initialized");
338 return;
339 }
340
341 sp<ANativeWindow> anw = ctx->getProducer();
342 android_native_buffer_t *anb = NULL;
343 int fenceFd = -1;
344 status_t res = anw->dequeueBuffer(anw.get(), &anb, &fenceFd);
345 if (res != OK) {
Zhijun Hea5827142015-04-22 10:07:42 -0700346 ALOGE("%s: Dequeue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
Chien-Yu Chene0ee6302015-07-06 17:46:05 -0700347 switch (res) {
348 case NO_INIT:
349 jniThrowException(env, "java/lang/IllegalStateException",
350 "Surface has been abandoned");
351 break;
352 default:
353 // TODO: handle other error cases here.
354 jniThrowRuntimeException(env, "dequeue buffer failed");
355 }
Zhijun Hef6a09e52015-02-24 18:12:23 -0800356 return;
357 }
358 // New GraphicBuffer object doesn't own the handle, thus the native buffer
359 // won't be freed when this object is destroyed.
Mathias Agopian845eef05f2017-04-03 17:51:15 -0700360 sp<GraphicBuffer> buffer(GraphicBuffer::from(anb));
Zhijun Hef6a09e52015-02-24 18:12:23 -0800361
362 // Note that:
363 // 1. No need to lock buffer now, will only lock it when the first getPlanes() is called.
364 // 2. Fence will be saved to mNativeFenceFd, and will consumed by lock/queue/cancel buffer
365 // later.
366 // 3. need use lockAsync here, as it will handle the dequeued fence for us automatically.
367
368 // Finally, set the native info into image object.
369 Image_setNativeContext(env, image, buffer, fenceFd);
370}
371
372static void ImageWriter_close(JNIEnv* env, jobject thiz, jlong nativeCtx) {
373 ALOGV("%s:", __FUNCTION__);
374 JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
375 if (ctx == NULL || thiz == NULL) {
Chien-Yu Chen0375cb02015-06-18 11:55:18 -0700376 // ImageWriter is already closed.
Zhijun Hef6a09e52015-02-24 18:12:23 -0800377 return;
378 }
379
380 ANativeWindow* producer = ctx->getProducer();
381 if (producer != NULL) {
382 /**
383 * NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not
384 * connectable after disconnect. MEDIA or CAMERA are treated the same internally.
385 * The producer listener will be cleared after disconnect call.
386 */
387 status_t res = native_window_api_disconnect(producer, /*api*/NATIVE_WINDOW_API_CAMERA);
388 /**
389 * This is not an error. if client calling process dies, the window will
390 * also die and all calls to it will return DEAD_OBJECT, thus it's already
391 * "disconnected"
392 */
393 if (res == DEAD_OBJECT) {
394 ALOGW("%s: While disconnecting ImageWriter from native window, the"
395 " native window died already", __FUNCTION__);
396 } else if (res != OK) {
397 ALOGE("%s: native window disconnect failed: %s (%d)",
398 __FUNCTION__, strerror(-res), res);
399 jniThrowRuntimeException(env, "Native window disconnect failed");
400 return;
401 }
402 }
403
404 ctx->decStrong((void*)ImageWriter_init);
405}
406
407static void ImageWriter_cancelImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) {
408 ALOGV("%s", __FUNCTION__);
409 JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
410 if (ctx == NULL || thiz == NULL) {
Zhijun Hedc6bb242015-12-03 15:34:34 -0800411 ALOGW("ImageWriter#close called before Image#close, consider calling Image#close first");
Zhijun Hef6a09e52015-02-24 18:12:23 -0800412 return;
413 }
414
415 sp<ANativeWindow> anw = ctx->getProducer();
416
417 GraphicBuffer *buffer = NULL;
418 int fenceFd = -1;
419 Image_getNativeContext(env, image, &buffer, &fenceFd);
420 if (buffer == NULL) {
Zhijun Hedc6bb242015-12-03 15:34:34 -0800421 // Cancel an already cancelled image is harmless.
Zhijun Hef6a09e52015-02-24 18:12:23 -0800422 return;
423 }
424
425 // Unlock the image if it was locked
426 Image_unlockIfLocked(env, image);
427
428 anw->cancelBuffer(anw.get(), buffer, fenceFd);
429
430 Image_setNativeContext(env, image, NULL, -1);
431}
432
433static void ImageWriter_queueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image,
Emilian Peev750aec62018-04-06 12:55:00 +0100434 jlong timestampNs, jint left, jint top, jint right, jint bottom, jint transform,
435 jint scalingMode) {
Zhijun Hef6a09e52015-02-24 18:12:23 -0800436 ALOGV("%s", __FUNCTION__);
437 JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
438 if (ctx == NULL || thiz == NULL) {
439 jniThrowException(env, "java/lang/IllegalStateException",
440 "ImageWriterContext is not initialized");
441 return;
442 }
443
444 status_t res = OK;
445 sp<ANativeWindow> anw = ctx->getProducer();
446
447 GraphicBuffer *buffer = NULL;
448 int fenceFd = -1;
449 Image_getNativeContext(env, image, &buffer, &fenceFd);
450 if (buffer == NULL) {
451 jniThrowException(env, "java/lang/IllegalStateException",
452 "Image is not initialized");
453 return;
454 }
455
456 // Unlock image if it was locked.
457 Image_unlockIfLocked(env, image);
458
459 // Set timestamp
Zhijun Hed1cbc682015-03-23 10:10:04 -0700460 ALOGV("timestamp to be queued: %" PRId64, timestampNs);
Zhijun Hef6a09e52015-02-24 18:12:23 -0800461 res = native_window_set_buffers_timestamp(anw.get(), timestampNs);
462 if (res != OK) {
463 jniThrowRuntimeException(env, "Set timestamp failed");
464 return;
465 }
466
467 // Set crop
468 android_native_rect_t cropRect;
469 cropRect.left = left;
470 cropRect.top = top;
471 cropRect.right = right;
472 cropRect.bottom = bottom;
473 res = native_window_set_crop(anw.get(), &cropRect);
474 if (res != OK) {
475 jniThrowRuntimeException(env, "Set crop rect failed");
476 return;
477 }
478
Emilian Peev450a5ff2018-03-19 16:05:10 +0000479 res = native_window_set_buffers_transform(anw.get(), transform);
480 if (res != OK) {
481 jniThrowRuntimeException(env, "Set transform failed");
482 return;
483 }
484
Emilian Peev750aec62018-04-06 12:55:00 +0100485 res = native_window_set_scaling_mode(anw.get(), scalingMode);
486 if (res != OK) {
487 jniThrowRuntimeException(env, "Set scaling mode failed");
488 return;
489 }
490
Zhijun Hef6a09e52015-02-24 18:12:23 -0800491 // Finally, queue input buffer
492 res = anw->queueBuffer(anw.get(), buffer, fenceFd);
493 if (res != OK) {
Chien-Yu Chene0ee6302015-07-06 17:46:05 -0700494 ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
495 switch (res) {
496 case NO_INIT:
497 jniThrowException(env, "java/lang/IllegalStateException",
498 "Surface has been abandoned");
499 break;
500 default:
501 // TODO: handle other error cases here.
502 jniThrowRuntimeException(env, "Queue input buffer failed");
503 }
Zhijun Hef6a09e52015-02-24 18:12:23 -0800504 return;
505 }
506
Zhijun Hece9d6f92015-03-29 16:33:59 -0700507 // Clear the image native context: end of this image's lifecycle in public API.
Zhijun Hef6a09e52015-02-24 18:12:23 -0800508 Image_setNativeContext(env, image, NULL, -1);
509}
510
Zhijun Hece9d6f92015-03-29 16:33:59 -0700511static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nativeCtx,
512 jlong nativeBuffer, jint imageFormat, jlong timestampNs, jint left, jint top,
Emilian Peev750aec62018-04-06 12:55:00 +0100513 jint right, jint bottom, jint transform, jint scalingMode) {
Zhijun Hef6a09e52015-02-24 18:12:23 -0800514 ALOGV("%s", __FUNCTION__);
515 JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
516 if (ctx == NULL || thiz == NULL) {
517 jniThrowException(env, "java/lang/IllegalStateException",
518 "ImageWriterContext is not initialized");
Zhijun Hece9d6f92015-03-29 16:33:59 -0700519 return -1;
Zhijun Hef6a09e52015-02-24 18:12:23 -0800520 }
521
Zhijun Hece9d6f92015-03-29 16:33:59 -0700522 sp<Surface> surface = ctx->getProducer();
523 status_t res = OK;
Eino-Ville Talvala07ad4592017-05-02 12:44:05 -0700524 if (isFormatOpaque(imageFormat) != isFormatOpaque(ctx->getBufferFormat())) {
Zhijun Hef6a09e52015-02-24 18:12:23 -0800525 jniThrowException(env, "java/lang/IllegalStateException",
Eino-Ville Talvala07ad4592017-05-02 12:44:05 -0700526 "Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa");
Zhijun Hece9d6f92015-03-29 16:33:59 -0700527 return -1;
Zhijun Hef6a09e52015-02-24 18:12:23 -0800528 }
529
Zhijun Hece9d6f92015-03-29 16:33:59 -0700530 // Image is guaranteed to be from ImageReader at this point, so it is safe to
531 // cast to BufferItem pointer.
Eino-Ville Talvala07ad4592017-05-02 12:44:05 -0700532 BufferItem* buffer = reinterpret_cast<BufferItem*>(nativeBuffer);
533 if (buffer == NULL) {
Zhijun Hece9d6f92015-03-29 16:33:59 -0700534 jniThrowException(env, "java/lang/IllegalStateException",
535 "Image is not initialized or already closed");
536 return -1;
537 }
538
539 // Step 1. Attach Image
Eino-Ville Talvala07ad4592017-05-02 12:44:05 -0700540 res = surface->attachBuffer(buffer->mGraphicBuffer.get());
Zhijun Hece9d6f92015-03-29 16:33:59 -0700541 if (res != OK) {
Zhijun Hece9d6f92015-03-29 16:33:59 -0700542 ALOGE("Attach image failed: %s (%d)", strerror(-res), res);
Chien-Yu Chene0ee6302015-07-06 17:46:05 -0700543 switch (res) {
544 case NO_INIT:
545 jniThrowException(env, "java/lang/IllegalStateException",
546 "Surface has been abandoned");
547 break;
548 default:
549 // TODO: handle other error cases here.
550 jniThrowRuntimeException(env, "nativeAttachImage failed!!!");
551 }
Zhijun Hece9d6f92015-03-29 16:33:59 -0700552 return res;
553 }
554 sp < ANativeWindow > anw = surface;
555
Emilian Peev750aec62018-04-06 12:55:00 +0100556 // Step 2. Set timestamp, crop, transform and scaling mode. Note that we do not need unlock the
557 // image because it was not locked.
Zhijun Hece9d6f92015-03-29 16:33:59 -0700558 ALOGV("timestamp to be queued: %" PRId64, timestampNs);
559 res = native_window_set_buffers_timestamp(anw.get(), timestampNs);
560 if (res != OK) {
561 jniThrowRuntimeException(env, "Set timestamp failed");
562 return res;
563 }
564
565 android_native_rect_t cropRect;
566 cropRect.left = left;
567 cropRect.top = top;
568 cropRect.right = right;
569 cropRect.bottom = bottom;
570 res = native_window_set_crop(anw.get(), &cropRect);
571 if (res != OK) {
572 jniThrowRuntimeException(env, "Set crop rect failed");
573 return res;
574 }
575
Emilian Peev450a5ff2018-03-19 16:05:10 +0000576 res = native_window_set_buffers_transform(anw.get(), transform);
577 if (res != OK) {
578 jniThrowRuntimeException(env, "Set transform failed");
579 return res;
580 }
581
Emilian Peev750aec62018-04-06 12:55:00 +0100582 res = native_window_set_scaling_mode(anw.get(), scalingMode);
583 if (res != OK) {
584 jniThrowRuntimeException(env, "Set scaling mode failed");
585 return res;
586 }
587
Zhijun Hece9d6f92015-03-29 16:33:59 -0700588 // Step 3. Queue Image.
Eino-Ville Talvala07ad4592017-05-02 12:44:05 -0700589 res = anw->queueBuffer(anw.get(), buffer->mGraphicBuffer.get(), /*fenceFd*/
Zhijun Hece9d6f92015-03-29 16:33:59 -0700590 -1);
591 if (res != OK) {
Chien-Yu Chene0ee6302015-07-06 17:46:05 -0700592 ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
593 switch (res) {
594 case NO_INIT:
595 jniThrowException(env, "java/lang/IllegalStateException",
596 "Surface has been abandoned");
597 break;
598 default:
599 // TODO: handle other error cases here.
600 jniThrowRuntimeException(env, "Queue input buffer failed");
601 }
Zhijun Hece9d6f92015-03-29 16:33:59 -0700602 return res;
603 }
604
605 // Do not set the image native context. Since it would overwrite the existing native context
606 // of the image that is from ImageReader, the subsequent image close will run into issues.
607
608 return res;
Zhijun Hef6a09e52015-02-24 18:12:23 -0800609}
610
611// --------------------------Image methods---------------------------------------
612
613static void Image_getNativeContext(JNIEnv* env, jobject thiz,
614 GraphicBuffer** buffer, int* fenceFd) {
615 ALOGV("%s", __FUNCTION__);
616 if (buffer != NULL) {
617 GraphicBuffer *gb = reinterpret_cast<GraphicBuffer *>
618 (env->GetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer));
619 *buffer = gb;
620 }
621
622 if (fenceFd != NULL) {
623 *fenceFd = reinterpret_cast<jint>(env->GetIntField(
624 thiz, gSurfaceImageClassInfo.mNativeFenceFd));
625 }
626}
627
628static void Image_setNativeContext(JNIEnv* env, jobject thiz,
629 sp<GraphicBuffer> buffer, int fenceFd) {
630 ALOGV("%s:", __FUNCTION__);
631 GraphicBuffer* p = NULL;
632 Image_getNativeContext(env, thiz, &p, /*fenceFd*/NULL);
633 if (buffer != 0) {
634 buffer->incStrong((void*)Image_setNativeContext);
635 }
636 if (p) {
637 p->decStrong((void*)Image_setNativeContext);
638 }
639 env->SetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer,
640 reinterpret_cast<jlong>(buffer.get()));
641
642 env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
643}
644
645static void Image_unlockIfLocked(JNIEnv* env, jobject thiz) {
646 ALOGV("%s", __FUNCTION__);
647 GraphicBuffer* buffer;
648 Image_getNativeContext(env, thiz, &buffer, NULL);
649 if (buffer == NULL) {
650 jniThrowException(env, "java/lang/IllegalStateException",
651 "Image is not initialized");
652 return;
653 }
654
655 // Is locked?
656 bool isLocked = false;
Zhijun Hece9d6f92015-03-29 16:33:59 -0700657 jobject planes = NULL;
658 if (!isFormatOpaque(buffer->getPixelFormat())) {
659 planes = env->GetObjectField(thiz, gSurfaceImageClassInfo.mPlanes);
660 }
Zhijun Hef6a09e52015-02-24 18:12:23 -0800661 isLocked = (planes != NULL);
662 if (isLocked) {
Zhijun Hece9d6f92015-03-29 16:33:59 -0700663 // no need to use fence here, as we it will be consumed by either cancel or queue buffer.
Zhijun Hef6a09e52015-02-24 18:12:23 -0800664 status_t res = buffer->unlock();
665 if (res != OK) {
666 jniThrowRuntimeException(env, "unlock buffer failed");
667 }
668 ALOGV("Successfully unlocked the image");
669 }
670}
671
672static jint Image_getWidth(JNIEnv* env, jobject thiz) {
673 ALOGV("%s", __FUNCTION__);
674 GraphicBuffer* buffer;
675 Image_getNativeContext(env, thiz, &buffer, NULL);
676 if (buffer == NULL) {
677 jniThrowException(env, "java/lang/IllegalStateException",
678 "Image is not initialized");
679 return -1;
680 }
681
682 return buffer->getWidth();
683}
684
685static jint Image_getHeight(JNIEnv* env, jobject thiz) {
686 ALOGV("%s", __FUNCTION__);
687 GraphicBuffer* buffer;
688 Image_getNativeContext(env, thiz, &buffer, NULL);
689 if (buffer == NULL) {
690 jniThrowException(env, "java/lang/IllegalStateException",
691 "Image is not initialized");
692 return -1;
693 }
694
695 return buffer->getHeight();
696}
697
Zhijun Hef6a09e52015-02-24 18:12:23 -0800698static jint Image_getFormat(JNIEnv* env, jobject thiz) {
699 ALOGV("%s", __FUNCTION__);
700 GraphicBuffer* buffer;
701 Image_getNativeContext(env, thiz, &buffer, NULL);
702 if (buffer == NULL) {
703 jniThrowException(env, "java/lang/IllegalStateException",
704 "Image is not initialized");
705 return 0;
706 }
707
Zhijun He0ab41622016-02-25 16:00:38 -0800708 // ImageWriter doesn't support data space yet, assuming it is unknown.
709 PublicFormat publicFmt = android_view_Surface_mapHalFormatDataspaceToPublicFormat(
710 buffer->getPixelFormat(), HAL_DATASPACE_UNKNOWN);
711 return static_cast<jint>(publicFmt);
Zhijun Hef6a09e52015-02-24 18:12:23 -0800712}
713
rennbe092192018-05-07 10:18:05 -0700714static jobject Image_getHardwareBuffer(JNIEnv* env, jobject thiz) {
715 GraphicBuffer* buffer;
716 Image_getNativeContext(env, thiz, &buffer, NULL);
717 if (buffer == NULL) {
718 jniThrowException(env, "java/lang/IllegalStateException",
719 "Image is not initialized");
720 return NULL;
721 }
722 AHardwareBuffer* b = AHardwareBuffer_from_GraphicBuffer(buffer);
723 // don't user the public AHardwareBuffer_toHardwareBuffer() because this would force us
724 // to link against libandroid.so
725 return android_hardware_HardwareBuffer_createFromAHardwareBuffer(env, b);
726}
727
Zhijun Hef6a09e52015-02-24 18:12:23 -0800728static void Image_setFenceFd(JNIEnv* env, jobject thiz, int fenceFd) {
729 ALOGV("%s:", __FUNCTION__);
730 env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
731}
732
733static void Image_getLockedImage(JNIEnv* env, jobject thiz, LockedImage *image) {
734 ALOGV("%s", __FUNCTION__);
735 GraphicBuffer* buffer;
736 int fenceFd = -1;
737 Image_getNativeContext(env, thiz, &buffer, &fenceFd);
738 if (buffer == NULL) {
739 jniThrowException(env, "java/lang/IllegalStateException",
740 "Image is not initialized");
741 return;
742 }
743
Zhijun He0ab41622016-02-25 16:00:38 -0800744 // ImageWriter doesn't use crop by itself, app sets it, use the no crop version.
745 const Rect noCrop(buffer->width, buffer->height);
746 status_t res = lockImageFromBuffer(
747 buffer, GRALLOC_USAGE_SW_WRITE_OFTEN, noCrop, fenceFd, image);
748 // Clear the fenceFd as it is already consumed by lock call.
749 Image_setFenceFd(env, thiz, /*fenceFd*/-1);
750 if (res != OK) {
751 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
752 "lock buffer failed for format 0x%x",
753 buffer->getPixelFormat());
754 return;
Zhijun Hef6a09e52015-02-24 18:12:23 -0800755 }
756
Zhijun He0ab41622016-02-25 16:00:38 -0800757 ALOGV("%s: Successfully locked the image", __FUNCTION__);
Zhijun Hef6a09e52015-02-24 18:12:23 -0800758 // crop, transform, scalingMode, timestamp, and frameNumber should be set by producer,
759 // and we don't set them here.
760}
761
Zhijun Hef6a09e52015-02-24 18:12:23 -0800762static void Image_getLockedImageInfo(JNIEnv* env, LockedImage* buffer, int idx,
763 int32_t writerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
764 ALOGV("%s", __FUNCTION__);
Zhijun Hef6a09e52015-02-24 18:12:23 -0800765
Zhijun He0ab41622016-02-25 16:00:38 -0800766 status_t res = getLockedImageInfo(buffer, idx, writerFormat, base, size,
767 pixelStride, rowStride);
768 if (res != OK) {
769 jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
770 "Pixel format: 0x%x is unsupported", buffer->flexFormat);
Zhijun Hef6a09e52015-02-24 18:12:23 -0800771 }
Zhijun Hef6a09e52015-02-24 18:12:23 -0800772}
773
774static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz,
775 int numPlanes, int writerFormat) {
776 ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes);
777 int rowStride, pixelStride;
778 uint8_t *pData;
779 uint32_t dataSize;
780 jobject byteBuffer;
781
782 int format = Image_getFormat(env, thiz);
Zhijun Hece9d6f92015-03-29 16:33:59 -0700783 if (isFormatOpaque(format) && numPlanes > 0) {
Zhijun Hef6a09e52015-02-24 18:12:23 -0800784 String8 msg;
785 msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
786 " must be 0", format, numPlanes);
787 jniThrowException(env, "java/lang/IllegalArgumentException", msg.string());
788 return NULL;
789 }
790
791 jobjectArray surfacePlanes = env->NewObjectArray(numPlanes, gSurfacePlaneClassInfo.clazz,
792 /*initial_element*/NULL);
793 if (surfacePlanes == NULL) {
794 jniThrowRuntimeException(env, "Failed to create SurfacePlane arrays,"
795 " probably out of memory");
796 return NULL;
797 }
Zhijun Hece9d6f92015-03-29 16:33:59 -0700798 if (isFormatOpaque(format)) {
799 return surfacePlanes;
800 }
Zhijun Hef6a09e52015-02-24 18:12:23 -0800801
802 // Buildup buffer info: rowStride, pixelStride and byteBuffers.
803 LockedImage lockedImg = LockedImage();
804 Image_getLockedImage(env, thiz, &lockedImg);
805
806 // Create all SurfacePlanes
Zhijun He0ab41622016-02-25 16:00:38 -0800807 PublicFormat publicWriterFormat = static_cast<PublicFormat>(writerFormat);
808 writerFormat = android_view_Surface_mapPublicFormatToHalFormat(publicWriterFormat);
Zhijun Hef6a09e52015-02-24 18:12:23 -0800809 for (int i = 0; i < numPlanes; i++) {
810 Image_getLockedImageInfo(env, &lockedImg, i, writerFormat,
811 &pData, &dataSize, &pixelStride, &rowStride);
812 byteBuffer = env->NewDirectByteBuffer(pData, dataSize);
813 if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) {
814 jniThrowException(env, "java/lang/IllegalStateException",
815 "Failed to allocate ByteBuffer");
816 return NULL;
817 }
818
819 // Finally, create this SurfacePlane.
820 jobject surfacePlane = env->NewObject(gSurfacePlaneClassInfo.clazz,
821 gSurfacePlaneClassInfo.ctor, thiz, rowStride, pixelStride, byteBuffer);
822 env->SetObjectArrayElement(surfacePlanes, i, surfacePlane);
823 }
824
825 return surfacePlanes;
826}
827
Zhijun Hef6a09e52015-02-24 18:12:23 -0800828} // extern "C"
829
830// ----------------------------------------------------------------------------
831
832static JNINativeMethod gImageWriterMethods[] = {
833 {"nativeClassInit", "()V", (void*)ImageWriter_classInit },
Zhijun He916d8ac2017-03-22 17:33:47 -0700834 {"nativeInit", "(Ljava/lang/Object;Landroid/view/Surface;II)J",
Zhijun Hef6a09e52015-02-24 18:12:23 -0800835 (void*)ImageWriter_init },
Zhijun Hece9d6f92015-03-29 16:33:59 -0700836 {"nativeClose", "(J)V", (void*)ImageWriter_close },
Emilian Peev750aec62018-04-06 12:55:00 +0100837 {"nativeAttachAndQueueImage", "(JJIJIIIIII)I", (void*)ImageWriter_attachAndQueueImage },
Zhijun Hef6a09e52015-02-24 18:12:23 -0800838 {"nativeDequeueInputImage", "(JLandroid/media/Image;)V", (void*)ImageWriter_dequeueImage },
Emilian Peev750aec62018-04-06 12:55:00 +0100839 {"nativeQueueInputImage", "(JLandroid/media/Image;JIIIIII)V", (void*)ImageWriter_queueImage },
Zhijun Hef6a09e52015-02-24 18:12:23 -0800840 {"cancelImage", "(JLandroid/media/Image;)V", (void*)ImageWriter_cancelImage },
841};
842
843static JNINativeMethod gImageMethods[] = {
844 {"nativeCreatePlanes", "(II)[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;",
rennbe092192018-05-07 10:18:05 -0700845 (void*)Image_createSurfacePlanes },
846 {"nativeGetWidth", "()I", (void*)Image_getWidth },
847 {"nativeGetHeight", "()I", (void*)Image_getHeight },
848 {"nativeGetFormat", "()I", (void*)Image_getFormat },
849 {"nativeGetHardwareBuffer", "()Landroid/hardware/HardwareBuffer;",
850 (void*)Image_getHardwareBuffer },
Zhijun Hef6a09e52015-02-24 18:12:23 -0800851};
852
853int register_android_media_ImageWriter(JNIEnv *env) {
854
855 int ret1 = AndroidRuntime::registerNativeMethods(env,
856 "android/media/ImageWriter", gImageWriterMethods, NELEM(gImageWriterMethods));
857
858 int ret2 = AndroidRuntime::registerNativeMethods(env,
859 "android/media/ImageWriter$WriterSurfaceImage", gImageMethods, NELEM(gImageMethods));
860
861 return (ret1 || ret2);
862}