Debian Codegraph @

media/jni/android_media_ImageReader.cpp

raw symbols SCIP
1 /*
2 * Copyright 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_NDEBUG 0
18 #define LOG_TAG "ImageReader_JNI"
19 #include "android_media_Utils.h"
20 #include <cutils/atomic.h>
21 #include <utils/Log.h>
22 #include <utils/misc.h>
23 #include <utils/List.h>
24 #include <utils/String8.h>
25
26 #include <cstdio>
27
28 #include <gui/BufferItemConsumer.h>
29 #include <gui/Surface.h>
30
31 #include <android_runtime/AndroidRuntime.h>
32 #include <android_runtime/android_view_Surface.h>
33 #include <android_runtime/android_graphics_GraphicBuffer.h>
34 #include <android_runtime/android_hardware_HardwareBuffer.h>
35 #include <grallocusage/GrallocUsageConversion.h>
36
37 #include <private/android/AHardwareBufferHelpers.h>
38
39 #include <jni.h>
40 #include <nativehelper/JNIHelp.h>
41
42 #include <stdint.h>
43 #include <inttypes.h>
44 #include <android/hardware_buffer_jni.h>
45
46 #include <ui/Rect.h>
47
48 #define ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID "mNativeContext"
49 #define ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID "mNativeBuffer"
50 #define ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID "mTimestamp"
51 #define ANDROID_MEDIA_SURFACEIMAGE_DS_JNI_ID "mDataSpace"
52 #define ANDROID_MEDIA_SURFACEIMAGE_TF_JNI_ID "mTransform"
53 #define ANDROID_MEDIA_SURFACEIMAGE_SM_JNI_ID "mScalingMode"
54
55 #define CONSUMER_BUFFER_USAGE_UNKNOWN 0;
56 // ----------------------------------------------------------------------------
57
58 using namespace android;
59
60
61 enum {
62 ACQUIRE_SUCCESS = 0,
63 ACQUIRE_NO_BUFFERS = 1,
64 ACQUIRE_MAX_IMAGES = 2,
65 };
66
67 static struct {
68 jfieldID mNativeContext;
69 jmethodID postEventFromNative;
70 } gImageReaderClassInfo;
71
72 static struct {
73 jfieldID mNativeBuffer;
74 jfieldID mTimestamp;
75 jfieldID mDataSpace;
76 jfieldID mTransform;
77 jfieldID mScalingMode;
78 jfieldID mPlanes;
79 } gSurfaceImageClassInfo;
80
81 static struct {
82 jclass clazz;
83 jmethodID ctor;
84 } gSurfacePlaneClassInfo;
85
86 static struct {
87 jclass clazz;
88 jmethodID ctor;
89 } gImagePlaneClassInfo;
90
91 // Get an ID that's unique within this process.
92 static int32_t createProcessUniqueId() {
93 static volatile int32_t globalCounter = 0;
94 return android_atomic_inc(&globalCounter);
95 }
96
97 // ----------------------------------------------------------------------------
98
99 class JNIImageReaderContext : public ConsumerBase::FrameAvailableListener
100 {
101 public:
102 JNIImageReaderContext(JNIEnv* env, jobject weakThiz, jclass clazz, int maxImages);
103
104 virtual ~JNIImageReaderContext();
105
106 virtual void onFrameAvailable(const BufferItem& item);
107
108 BufferItem* getBufferItem();
109 void returnBufferItem(BufferItem* buffer);
110
111
112 void setBufferConsumer(const sp<BufferItemConsumer>& consumer) { mConsumer = consumer; }
113 BufferItemConsumer* getBufferConsumer() { return mConsumer.get(); }
114
115 void setProducer(const sp<IGraphicBufferProducer>& producer) { mProducer = producer; }
116 IGraphicBufferProducer* getProducer() { return mProducer.get(); }
117
118 void setBufferFormat(int format) { mFormat = format; }
119 int getBufferFormat() { return mFormat; }
120
121 void setBufferDataspace(android_dataspace dataSpace) { mDataSpace = dataSpace; }
122 android_dataspace getBufferDataspace() { return mDataSpace; }
123
124 void setBufferWidth(int width) { mWidth = width; }
125 int getBufferWidth() { return mWidth; }
126
127 void setBufferHeight(int height) { mHeight = height; }
128 int getBufferHeight() { return mHeight; }
129
130 private:
131 static JNIEnv* getJNIEnv(bool* needsDetach);
132 static void detachJNI();
133
134 List<BufferItem*> mBuffers;
135 sp<BufferItemConsumer> mConsumer;
136 sp<IGraphicBufferProducer> mProducer;
137 jobject mWeakThiz;
138 jclass mClazz;
139 int mFormat;
140 android_dataspace mDataSpace;
141 int mWidth;
142 int mHeight;
143 };
144
145 JNIImageReaderContext::JNIImageReaderContext(JNIEnv* env,
146 jobject weakThiz, jclass clazz, int maxImages) :
147 mWeakThiz(env->NewGlobalRef(weakThiz)),
148 mClazz((jclass)env->NewGlobalRef(clazz)),
149 mFormat(0),
150 mDataSpace(HAL_DATASPACE_UNKNOWN),
151 mWidth(-1),
152 mHeight(-1) {
153 for (int i = 0; i < maxImages; i++) {
154 BufferItem* buffer = new BufferItem;
155 mBuffers.push_back(buffer);
156 }
157 }
158
159 JNIEnv* JNIImageReaderContext::getJNIEnv(bool* needsDetach) {
160 LOG_ALWAYS_FATAL_IF(needsDetach == NULL, "needsDetach is null!!!");
161 *needsDetach = false;
162 JNIEnv* env = AndroidRuntime::getJNIEnv();
163 if (env == NULL) {
164 JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL};
165 JavaVM* vm = AndroidRuntime::getJavaVM();
166 int result = vm->AttachCurrentThread(&env, (void*) &args);
167 if (result != JNI_OK) {
168 ALOGE("thread attach failed: %#x", result);
169 return NULL;
170 }
171 *needsDetach = true;
172 }
173 return env;
174 }
175
176 void JNIImageReaderContext::detachJNI() {
177 JavaVM* vm = AndroidRuntime::getJavaVM();
178 int result = vm->DetachCurrentThread();
179 if (result != JNI_OK) {
180 ALOGE("thread detach failed: %#x", result);
181 }
182 }
183
184 BufferItem* JNIImageReaderContext::getBufferItem() {
185 if (mBuffers.empty()) {
186 return NULL;
187 }
188 // Return a BufferItem pointer and remove it from the list
189 List<BufferItem*>::iterator it = mBuffers.begin();
190 BufferItem* buffer = *it;
191 mBuffers.erase(it);
192 return buffer;
193 }
194
195 void JNIImageReaderContext::returnBufferItem(BufferItem* buffer) {
196 buffer->mGraphicBuffer = nullptr;
197 mBuffers.push_back(buffer);
198 }
199
200 JNIImageReaderContext::~JNIImageReaderContext() {
201 bool needsDetach = false;
202 JNIEnv* env = getJNIEnv(&needsDetach);
203 if (env != NULL) {
204 env->DeleteGlobalRef(mWeakThiz);
205 env->DeleteGlobalRef(mClazz);
206 } else {
207 ALOGW("leaking JNI object references");
208 }
209 if (needsDetach) {
210 detachJNI();
211 }
212
213 // Delete buffer items.
214 for (List<BufferItem *>::iterator it = mBuffers.begin();
215 it != mBuffers.end(); it++) {
216 delete *it;
217 }
218
219 if (mConsumer != 0) {
220 mConsumer.clear();
221 }
222 }
223
224 void JNIImageReaderContext::onFrameAvailable(const BufferItem& /*item*/)
225 {
226 ALOGV("%s: frame available", __FUNCTION__);
227 bool needsDetach = false;
228 JNIEnv* env = getJNIEnv(&needsDetach);
229 if (env != NULL) {
230 env->CallStaticVoidMethod(mClazz, gImageReaderClassInfo.postEventFromNative, mWeakThiz);
231 } else {
232 ALOGW("onFrameAvailable event will not posted");
233 }
234 if (needsDetach) {
235 detachJNI();
236 }
237 }
238
239 // ----------------------------------------------------------------------------
240
241 extern "C" {
242
243 static JNIImageReaderContext* ImageReader_getContext(JNIEnv* env, jobject thiz)
244 {
245 JNIImageReaderContext *ctx;
246 ctx = reinterpret_cast<JNIImageReaderContext *>
247 (env->GetLongField(thiz, gImageReaderClassInfo.mNativeContext));
248 return ctx;
249 }
250
251 static IGraphicBufferProducer* ImageReader_getProducer(JNIEnv* env, jobject thiz)
252 {
253 ALOGV("%s:", __FUNCTION__);
254 JNIImageReaderContext* const ctx = ImageReader_getContext(env, thiz);
255 if (ctx == NULL) {
256 jniThrowRuntimeException(env, "ImageReaderContext is not initialized");
257 return NULL;
258 }
259
260 return ctx->getProducer();
261 }
262
263 static void ImageReader_setNativeContext(JNIEnv* env,
264 jobject thiz, sp<JNIImageReaderContext> ctx)
265 {
266 ALOGV("%s:", __FUNCTION__);
267 JNIImageReaderContext* const p = ImageReader_getContext(env, thiz);
268 if (ctx != 0) {
269 ctx->incStrong((void*)ImageReader_setNativeContext);
270 }
271 if (p) {
272 p->decStrong((void*)ImageReader_setNativeContext);
273 }
274 env->SetLongField(thiz, gImageReaderClassInfo.mNativeContext,
275 reinterpret_cast<jlong>(ctx.get()));
276 }
277
278 static BufferItemConsumer* ImageReader_getBufferConsumer(JNIEnv* env, jobject thiz)
279 {
280 ALOGV("%s:", __FUNCTION__);
281 JNIImageReaderContext* const ctx = ImageReader_getContext(env, thiz);
282 if (ctx == NULL) {
283 jniThrowRuntimeException(env, "ImageReaderContext is not initialized");
284 return NULL;
285 }
286
287 return ctx->getBufferConsumer();
288 }
289
290 static void Image_setBufferItem(JNIEnv* env, jobject thiz,
291 const BufferItem* buffer)
292 {
293 env->SetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer, reinterpret_cast<jlong>(buffer));
294 }
295
296 static BufferItem* Image_getBufferItem(JNIEnv* env, jobject image)
297 {
298 return reinterpret_cast<BufferItem*>(
299 env->GetLongField(image, gSurfaceImageClassInfo.mNativeBuffer));
300 }
301
302
303 // ----------------------------------------------------------------------------
304
305 static void ImageReader_classInit(JNIEnv* env, jclass clazz)
306 {
307 ALOGV("%s:", __FUNCTION__);
308
309 jclass imageClazz = env->FindClass("android/media/ImageReader$SurfaceImage");
310 LOG_ALWAYS_FATAL_IF(imageClazz == NULL,
311 "can't find android/graphics/ImageReader$SurfaceImage");
312 gSurfaceImageClassInfo.mNativeBuffer = env->GetFieldID(
313 imageClazz, ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID, "J");
314 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeBuffer == NULL,
315 "can't find android/graphics/ImageReader.%s",
316 ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID);
317
318 gSurfaceImageClassInfo.mTimestamp = env->GetFieldID(
319 imageClazz, ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID, "J");
320 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mTimestamp == NULL,
321 "can't find android/graphics/ImageReader.%s",
322 ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID);
323
324 gSurfaceImageClassInfo.mDataSpace = env->GetFieldID(
325 imageClazz, ANDROID_MEDIA_SURFACEIMAGE_DS_JNI_ID, "I");
326 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mDataSpace == NULL,
327 "can't find android/graphics/ImageReader.%s",
328 ANDROID_MEDIA_SURFACEIMAGE_DS_JNI_ID);
329
330 gSurfaceImageClassInfo.mTransform = env->GetFieldID(
331 imageClazz, ANDROID_MEDIA_SURFACEIMAGE_TF_JNI_ID, "I");
332 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mTransform == NULL,
333 "can't find android/graphics/ImageReader.%s",
334 ANDROID_MEDIA_SURFACEIMAGE_TF_JNI_ID);
335
336 gSurfaceImageClassInfo.mScalingMode = env->GetFieldID(
337 imageClazz, ANDROID_MEDIA_SURFACEIMAGE_SM_JNI_ID, "I");
338 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mScalingMode == NULL,
339 "can't find android/graphics/ImageReader.%s",
340 ANDROID_MEDIA_SURFACEIMAGE_SM_JNI_ID);
341
342 gSurfaceImageClassInfo.mPlanes = env->GetFieldID(
343 imageClazz, "mPlanes", "[Landroid/media/ImageReader$SurfaceImage$SurfacePlane;");
344 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mPlanes == NULL,
345 "can't find android/media/ImageReader$ReaderSurfaceImage.mPlanes");
346
347 gImageReaderClassInfo.mNativeContext = env->GetFieldID(
348 clazz, ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID, "J");
349 LOG_ALWAYS_FATAL_IF(gImageReaderClassInfo.mNativeContext == NULL,
350 "can't find android/graphics/ImageReader.%s",
351 ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID);
352
353 gImageReaderClassInfo.postEventFromNative = env->GetStaticMethodID(
354 clazz, "postEventFromNative", "(Ljava/lang/Object;)V");
355 LOG_ALWAYS_FATAL_IF(gImageReaderClassInfo.postEventFromNative == NULL,
356 "can't find android/graphics/ImageReader.postEventFromNative");
357
358 jclass planeClazz = env->FindClass("android/media/ImageReader$SurfaceImage$SurfacePlane");
359 LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find SurfacePlane class");
360 // FindClass only gives a local reference of jclass object.
361 gSurfacePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz);
362 gSurfacePlaneClassInfo.ctor = env->GetMethodID(gSurfacePlaneClassInfo.clazz, "<init>",
363 "(Landroid/media/ImageReader$SurfaceImage;IILjava/nio/ByteBuffer;)V");
364 LOG_ALWAYS_FATAL_IF(gSurfacePlaneClassInfo.ctor == NULL,
365 "Can not find SurfacePlane constructor");
366
367 planeClazz = env->FindClass("android/media/ImageReader$ImagePlane");
368 LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find ImagePlane class");
369 // FindClass only gives a local reference of jclass object.
370 gImagePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz);
371 gImagePlaneClassInfo.ctor = env->GetMethodID(gImagePlaneClassInfo.clazz, "<init>",
372 "(IILjava/nio/ByteBuffer;)V");
373 LOG_ALWAYS_FATAL_IF(gImagePlaneClassInfo.ctor == NULL,
374 "Can not find ImagePlane constructor");
375 }
376
377 static void ImageReader_init(JNIEnv* env, jobject thiz, jobject weakThiz, jint width, jint height,
378 jint maxImages, jlong ndkUsage, jint nativeHalFormat, jint dataSpace) {
379 status_t res;
380
381 ALOGV("%s: width:%d, height: %d, nativeHalFormat: %d, maxImages:%d",
382 __FUNCTION__, width, height, nativeHalFormat, maxImages);
383
384 android_dataspace nativeDataspace = static_cast<android_dataspace>(dataSpace);
385
386 jclass clazz = env->GetObjectClass(thiz);
387 if (clazz == NULL) {
388 jniThrowRuntimeException(env, "Can't find android/graphics/ImageReader");
389 return;
390 }
391 sp<JNIImageReaderContext> ctx(new JNIImageReaderContext(env, weakThiz, clazz, maxImages));
392
393 sp<IGraphicBufferProducer> gbProducer;
394 sp<IGraphicBufferConsumer> gbConsumer;
395 BufferQueue::createBufferQueue(&gbProducer, &gbConsumer);
396 sp<BufferItemConsumer> bufferConsumer;
397 String8 consumerName = String8::format("ImageReader-%dx%df%xm%d-%d-%d",
398 width, height, nativeHalFormat, maxImages, getpid(),
399 createProcessUniqueId());
400 uint64_t consumerUsage =
401 android_hardware_HardwareBuffer_convertToGrallocUsageBits(ndkUsage);
402
403 bufferConsumer = new BufferItemConsumer(gbConsumer, consumerUsage, maxImages,
404 /*controlledByApp*/true);
405 if (bufferConsumer == nullptr) {
406 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
407 "Failed to allocate native buffer consumer for hal format 0x%x and usage 0x%x",
408 nativeHalFormat, consumerUsage);
409 return;
410 }
411
412 if (consumerUsage & GRALLOC_USAGE_PROTECTED) {
413 gbConsumer->setConsumerIsProtected(true);
414 }
415
416 ctx->setBufferConsumer(bufferConsumer);
417 bufferConsumer->setName(consumerName);
418
419 ctx->setProducer(gbProducer);
420 bufferConsumer->setFrameAvailableListener(ctx);
421 ImageReader_setNativeContext(env, thiz, ctx);
422 ctx->setBufferFormat(nativeHalFormat);
423 ctx->setBufferDataspace(nativeDataspace);
424 ctx->setBufferWidth(width);
425 ctx->setBufferHeight(height);
426
427 // Set the width/height/format/dataspace to the bufferConsumer.
428 res = bufferConsumer->setDefaultBufferSize(width, height);
429 if (res != OK) {
430 jniThrowExceptionFmt(env, "java/lang/IllegalStateException",
431 "Failed to set buffer consumer default size (%dx%d) for Hal format 0x%x",
432 width, height, nativeHalFormat);
433 return;
434 }
435 res = bufferConsumer->setDefaultBufferFormat(nativeHalFormat);
436 if (res != OK) {
437 jniThrowExceptionFmt(env, "java/lang/IllegalStateException",
438 "Failed to set buffer consumer default Halformat 0x%x", nativeHalFormat);
439 return;
440 }
441 res = bufferConsumer->setDefaultBufferDataSpace(nativeDataspace);
442 if (res != OK) {
443 jniThrowExceptionFmt(env, "java/lang/IllegalStateException",
444 "Failed to set buffer consumer default dataSpace 0x%x", nativeDataspace);
445 return;
446 }
447 }
448
449 static void ImageReader_close(JNIEnv* env, jobject thiz)
450 {
451 ALOGV("%s:", __FUNCTION__);
452
453 JNIImageReaderContext* const ctx = ImageReader_getContext(env, thiz);
454 if (ctx == NULL) {
455 // ImageReader is already closed.
456 return;
457 }
458
459 BufferItemConsumer* consumer = NULL;
460 consumer = ImageReader_getBufferConsumer(env, thiz);
461
462 if (consumer != NULL) {
463 consumer->abandon();
464 consumer->setFrameAvailableListener(NULL);
465 }
466 ImageReader_setNativeContext(env, thiz, NULL);
467 }
468
469 static sp<Fence> Image_unlockIfLocked(JNIEnv* env, jobject image) {
470 ALOGV("%s", __FUNCTION__);
471 BufferItem* buffer = Image_getBufferItem(env, image);
472 if (buffer == NULL) {
473 jniThrowException(env, "java/lang/IllegalStateException",
474 "Image is not initialized");
475 return Fence::NO_FENCE;
476 }
477
478 // Is locked?
479 bool wasBufferLocked = false;
480 jobject planes = NULL;
481 if (!isFormatOpaque(buffer->mGraphicBuffer->getPixelFormat())) {
482 planes = env->GetObjectField(image, gSurfaceImageClassInfo.mPlanes);
483 }
484 wasBufferLocked = (planes != NULL);
485 if (wasBufferLocked) {
486 status_t res = OK;
487 int fenceFd = -1;
488 if (wasBufferLocked) {
489 res = buffer->mGraphicBuffer->unlockAsync(&fenceFd);
490 if (res != OK) {
491 jniThrowRuntimeException(env, "unlock buffer failed");
492 return Fence::NO_FENCE;
493 }
494 }
495 sp<Fence> releaseFence = new Fence(fenceFd);
496 return releaseFence;
497 ALOGV("Successfully unlocked the image");
498 }
499 return Fence::NO_FENCE;
500 }
501
502 static void ImageReader_imageRelease(JNIEnv* env, jobject thiz, jobject image)
503 {
504 ALOGV("%s:", __FUNCTION__);
505 JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
506 if (ctx == NULL) {
507 ALOGW("ImageReader#close called before Image#close, consider calling Image#close first");
508 return;
509 }
510
511 BufferItemConsumer* bufferConsumer = ctx->getBufferConsumer();
512 BufferItem* buffer = Image_getBufferItem(env, image);
513 if (buffer == nullptr) {
514 // Release an already closed image is harmless.
515 return;
516 }
517
518 sp<Fence> releaseFence = Image_unlockIfLocked(env, image);
519 bufferConsumer->releaseBuffer(*buffer, releaseFence);
520 Image_setBufferItem(env, image, NULL);
521 ctx->returnBufferItem(buffer);
522 ALOGV("%s: Image (format: 0x%x) has been released", __FUNCTION__, ctx->getBufferFormat());
523 }
524
525 static jint ImageReader_imageSetup(JNIEnv* env, jobject thiz, jobject image) {
526 ALOGV("%s:", __FUNCTION__);
527 JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
528 if (ctx == NULL) {
529 jniThrowException(env, "java/lang/IllegalStateException",
530 "ImageReader is not initialized or was already closed");
531 return -1;
532 }
533
534 BufferItemConsumer* bufferConsumer = ctx->getBufferConsumer();
535 BufferItem* buffer = ctx->getBufferItem();
536 if (buffer == NULL) {
537 ALOGW("Unable to acquire a buffer item, very likely client tried to acquire more than"
538 " maxImages buffers");
539 return ACQUIRE_MAX_IMAGES;
540 }
541
542 status_t res = bufferConsumer->acquireBuffer(buffer, 0);
543 if (res != OK) {
544 ctx->returnBufferItem(buffer);
545 if (res != BufferQueue::NO_BUFFER_AVAILABLE) {
546 if (res == INVALID_OPERATION) {
547 // Max number of images were already acquired.
548 ALOGE("%s: Max number of buffers allowed are already acquired : %s (%d)",
549 __FUNCTION__, strerror(-res), res);
550 return ACQUIRE_MAX_IMAGES;
551 } else {
552 ALOGE("%s: Acquire image failed with some unknown error: %s (%d)",
553 __FUNCTION__, strerror(-res), res);
554 jniThrowExceptionFmt(env, "java/lang/IllegalStateException",
555 "Unknown error (%d) when we tried to acquire an image.",
556 res);
557 return ACQUIRE_NO_BUFFERS;
558 }
559 }
560 // This isn't really an error case, as the application may acquire buffer at any time.
561 return ACQUIRE_NO_BUFFERS;
562 }
563
564 // Add some extra checks for non-opaque formats.
565 if (!isFormatOpaque(ctx->getBufferFormat())) {
566 // Check if the left-top corner of the crop rect is origin, we currently assume this point is
567 // zero, will revisit this once this assumption turns out problematic.
568 Point lt = buffer->mCrop.leftTop();
569 if (lt.x != 0 || lt.y != 0) {
570 jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
571 "crop left top corner [%d, %d] need to be at origin", lt.x, lt.y);
572 return -1;
573 }
574
575 // Check if the producer buffer configurations match what ImageReader configured.
576 int outputWidth = getBufferWidth(buffer);
577 int outputHeight = getBufferHeight(buffer);
578
579 int imgReaderHalFmt = ctx->getBufferFormat();
580 int imageReaderWidth = ctx->getBufferWidth();
581 int imageReaderHeight = ctx->getBufferHeight();
582 int bufferFormat = buffer->mGraphicBuffer->getPixelFormat();
583 if ((bufferFormat != HAL_PIXEL_FORMAT_BLOB) && (imgReaderHalFmt != HAL_PIXEL_FORMAT_BLOB) &&
584 (imageReaderWidth != outputWidth || imageReaderHeight != outputHeight)) {
585 ALOGV("%s: Producer buffer size: %dx%d, doesn't match ImageReader configured size: %dx%d",
586 __FUNCTION__, outputWidth, outputHeight, imageReaderWidth, imageReaderHeight);
587 }
588 if (imgReaderHalFmt != bufferFormat) {
589 if (imgReaderHalFmt == HAL_PIXEL_FORMAT_YCbCr_420_888 &&
590 isPossiblyYUV(bufferFormat)) {
591 // Treat formats that are compatible with flexible YUV
592 // (HAL_PIXEL_FORMAT_YCbCr_420_888) as HAL_PIXEL_FORMAT_YCbCr_420_888.
593 ALOGV("%s: Treat buffer format to 0x%x as HAL_PIXEL_FORMAT_YCbCr_420_888",
594 __FUNCTION__, bufferFormat);
595 } else if (imgReaderHalFmt == HAL_PIXEL_FORMAT_YCBCR_P010 &&
596 isPossibly10BitYUV(bufferFormat)) {
597 // Treat formats that are compatible with flexible 10-bit YUV
598 // (HAL_PIXEL_FORMAT_YCBCR_P010) as HAL_PIXEL_FORMAT_YCBCR_P010.
599 ALOGV("%s: Treat buffer format to 0x%x as HAL_PIXEL_FORMAT_YCBCR_P010",
600 __FUNCTION__, bufferFormat);
601 } else if (imgReaderHalFmt == HAL_PIXEL_FORMAT_BLOB &&
602 bufferFormat == HAL_PIXEL_FORMAT_RGBA_8888) {
603 // Using HAL_PIXEL_FORMAT_RGBA_8888 Gralloc buffers containing JPEGs to get around
604 // SW write limitations for (b/17379185).
605 ALOGV("%s: Receiving JPEG in HAL_PIXEL_FORMAT_RGBA_8888 buffer.", __FUNCTION__);
606 } else {
607 // Return the buffer to the queue. No need to provide fence, as this buffer wasn't
608 // used anywhere yet.
609 bufferConsumer->releaseBuffer(*buffer);
610 ctx->returnBufferItem(buffer);
611
612 // Throw exception
613 ALOGE("Producer output buffer format: 0x%x, ImageReader configured format: 0x%x",
614 bufferFormat, ctx->getBufferFormat());
615 String8 msg;
616 msg.appendFormat("The producer output buffer format 0x%x doesn't "
617 "match the ImageReader's configured buffer format 0x%x.",
618 bufferFormat, ctx->getBufferFormat());
619 jniThrowException(env, "java/lang/UnsupportedOperationException",
620 msg.string());
621 return -1;
622 }
623 }
624
625 }
626
627 // Set SurfaceImage instance member variables
628 Image_setBufferItem(env, image, buffer);
629 env->SetLongField(image, gSurfaceImageClassInfo.mTimestamp,
630 static_cast<jlong>(buffer->mTimestamp));
631 env->SetIntField(image, gSurfaceImageClassInfo.mDataSpace,
632 static_cast<jint>(buffer->mDataSpace));
633 auto transform = buffer->mTransform;
634 if (buffer->mTransformToDisplayInverse) {
635 transform |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
636 }
637 env->SetIntField(image, gSurfaceImageClassInfo.mTransform,
638 static_cast<jint>(transform));
639 env->SetIntField(image, gSurfaceImageClassInfo.mScalingMode,
640 static_cast<jint>(buffer->mScalingMode));
641
642 return ACQUIRE_SUCCESS;
643 }
644
645 static jint ImageReader_detachImage(JNIEnv* env, jobject thiz, jobject image) {
646 ALOGV("%s:", __FUNCTION__);
647 JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
648 if (ctx == NULL) {
649 jniThrowException(env, "java/lang/IllegalStateException", "ImageReader was already closed");
650 return -1;
651 }
652
653 BufferItemConsumer* bufferConsumer = ctx->getBufferConsumer();
654 BufferItem* buffer = Image_getBufferItem(env, image);
655 if (!buffer) {
656 ALOGE(
657 "Image already released and can not be detached from ImageReader!!!");
658 jniThrowException(env, "java/lang/IllegalStateException",
659 "Image detach from ImageReader failed: buffer was already released");
660 return -1;
661 }
662
663 status_t res = OK;
664 Image_unlockIfLocked(env, image);
665 res = bufferConsumer->detachBuffer(buffer->mSlot);
666 if (res != OK) {
667 ALOGE("Image detach failed: %s (%d)!!!", strerror(-res), res);
668 jniThrowRuntimeException(env,
669 "nativeDetachImage failed for image!!!");
670 return res;
671 }
672 return OK;
673 }
674
675 static void ImageReader_discardFreeBuffers(JNIEnv* env, jobject thiz) {
676 ALOGV("%s:", __FUNCTION__);
677 JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
678 if (ctx == NULL) {
679 jniThrowException(env, "java/lang/IllegalStateException", "ImageReader was already closed");
680 return;
681 }
682
683 BufferItemConsumer* bufferConsumer = ctx->getBufferConsumer();
684 status_t res = bufferConsumer->discardFreeBuffers();
685 if (res != OK) {
686 ALOGE("Buffer discard failed: %s (%d)", strerror(-res), res);
687 jniThrowRuntimeException(env,
688 "nativeDicardFreebuffers failed");
689 }
690 }
691
692 static jobject ImageReader_getSurface(JNIEnv* env, jobject thiz)
693 {
694 ALOGV("%s: ", __FUNCTION__);
695
696 IGraphicBufferProducer* gbp = ImageReader_getProducer(env, thiz);
697 if (gbp == NULL) {
698 jniThrowRuntimeException(env, "Buffer consumer is uninitialized");
699 return NULL;
700 }
701
702 // Wrap the IGBP in a Java-language Surface.
703 return android_view_Surface_createFromIGraphicBufferProducer(env, gbp);
704 }
705
706 static void Image_getLockedImage(JNIEnv* env, jobject thiz, LockedImage *image,
707 uint64_t ndkReaderUsage) {
708 ALOGV("%s", __FUNCTION__);
709 BufferItem* buffer = Image_getBufferItem(env, thiz);
710 if (buffer == NULL) {
711 jniThrowException(env, "java/lang/IllegalStateException",
712 "Image is not initialized");
713 return;
714 }
715
716 uint32_t lockUsage;
717 if ((ndkReaderUsage & (AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY
718 | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN)) != 0) {
719 lockUsage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN;
720 } else {
721 lockUsage = GRALLOC_USAGE_SW_READ_OFTEN;
722 }
723
724 status_t res = lockImageFromBuffer(buffer, lockUsage, buffer->mFence->dup(), image);
725
726 if (res != OK) {
727 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
728 "lock buffer failed for format 0x%x",
729 buffer->mGraphicBuffer->getPixelFormat());
730 return;
731 }
732
733 // Carry over some fields from BufferItem.
734 image->crop = buffer->mCrop;
735 image->transform = buffer->mTransform;
736 image->scalingMode = buffer->mScalingMode;
737 image->timestamp = buffer->mTimestamp;
738 image->dataSpace = buffer->mDataSpace;
739 image->frameNumber = buffer->mFrameNumber;
740
741 ALOGV("%s: Successfully locked the image", __FUNCTION__);
742 // crop, transform, scalingMode, timestamp, and frameNumber should be set by producer,
743 // and we don't set them here.
744 }
745
746 static bool Image_getLockedImageInfo(JNIEnv* env, LockedImage* buffer, int idx,
747 int32_t writerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
748 ALOGV("%s", __FUNCTION__);
749
750 status_t res = getLockedImageInfo(buffer, idx, writerFormat, base, size,
751 pixelStride, rowStride);
752 if (res != OK) {
753 jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
754 "Pixel format: 0x%x is unsupported", buffer->flexFormat);
755 return false;
756 }
757 return true;
758 }
759
760 static void ImageReader_unlockGraphicBuffer(JNIEnv* env, jobject /*thiz*/,
761 jobject buffer) {
762 sp<GraphicBuffer> graphicBuffer =
763 android_graphics_GraphicBuffer_getNativeGraphicsBuffer(env, buffer);
764 if (graphicBuffer.get() == NULL) {
765 jniThrowRuntimeException(env, "Invalid graphic buffer!");
766 }
767
768 status_t res = graphicBuffer->unlock();
769 if (res != OK) {
770 jniThrowRuntimeException(env, "unlock buffer failed");
771 }
772 }
773
774 static jobjectArray ImageReader_createImagePlanes(JNIEnv* env, jobject /*thiz*/,
775 int numPlanes, jobject buffer, int fenceFd, int format, int cropLeft, int cropTop,
776 int cropRight, int cropBottom)
777 {
778 ALOGV("%s: create ImagePlane array with size %d", __FUNCTION__, numPlanes);
779 int rowStride = 0;
780 int pixelStride = 0;
781 uint8_t *pData = NULL;
782 uint32_t dataSize = 0;
783 jobject byteBuffer = NULL;
784
785 PublicFormat publicReaderFormat = static_cast<PublicFormat>(format);
786 int halReaderFormat = mapPublicFormatToHalFormat(publicReaderFormat);
787
788 if (isFormatOpaque(halReaderFormat) && numPlanes > 0) {
789 String8 msg;
790 msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
791 " must be 0", halReaderFormat, numPlanes);
792 jniThrowException(env, "java/lang/IllegalArgumentException", msg.string());
793 return NULL;
794 }
795
796 jobjectArray imagePlanes = env->NewObjectArray(numPlanes, gImagePlaneClassInfo.clazz,
797 /*initial_element*/NULL);
798 if (imagePlanes == NULL) {
799 jniThrowRuntimeException(env, "Failed to create ImagePlane arrays,"
800 " probably out of memory");
801 return NULL;
802 }
803 if (isFormatOpaque(halReaderFormat)) {
804 // Return 0 element surface array.
805 return imagePlanes;
806 }
807
808 LockedImage lockedImg = LockedImage();
809 uint32_t lockUsage = GRALLOC_USAGE_SW_READ_OFTEN;
810
811 Rect cropRect(cropLeft, cropTop, cropRight, cropBottom);
812 status_t res = lockImageFromBuffer(
813 android_graphics_GraphicBuffer_getNativeGraphicsBuffer(env, buffer), lockUsage,
814 cropRect, fenceFd, &lockedImg);
815 if (res != OK) {
816 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
817 "lock buffer failed for format 0x%x", format);
818 return nullptr;
819 }
820
821 // Create all ImagePlanes
822 for (int i = 0; i < numPlanes; i++) {
823 if (!Image_getLockedImageInfo(env, &lockedImg, i, halReaderFormat,
824 &pData, &dataSize, &pixelStride, &rowStride)) {
825 return NULL;
826 }
827 byteBuffer = env->NewDirectByteBuffer(pData, dataSize);
828 if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) {
829 jniThrowException(env, "java/lang/IllegalStateException",
830 "Failed to allocate ByteBuffer");
831 return NULL;
832 }
833
834 // Finally, create this ImagePlane.
835 jobject imagePlane = env->NewObject(gImagePlaneClassInfo.clazz,
836 gImagePlaneClassInfo.ctor, rowStride, pixelStride, byteBuffer);
837 env->SetObjectArrayElement(imagePlanes, i, imagePlane);
838 }
839
840 return imagePlanes;
841 }
842
843 static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz,
844 int numPlanes, int halReaderFormat, uint64_t ndkReaderUsage)
845 {
846 ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes);
847 int rowStride = 0;
848 int pixelStride = 0;
849 uint8_t *pData = NULL;
850 uint32_t dataSize = 0;
851 jobject byteBuffer = NULL;
852
853 if (isFormatOpaque(halReaderFormat) && numPlanes > 0) {
854 String8 msg;
855 msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
856 " must be 0", halReaderFormat, numPlanes);
857 jniThrowException(env, "java/lang/IllegalArgumentException", msg.string());
858 return NULL;
859 }
860
861 jobjectArray surfacePlanes = env->NewObjectArray(numPlanes, gSurfacePlaneClassInfo.clazz,
862 /*initial_element*/NULL);
863 if (surfacePlanes == NULL) {
864 jniThrowRuntimeException(env, "Failed to create SurfacePlane arrays,"
865 " probably out of memory");
866 return NULL;
867 }
868 if (isFormatOpaque(halReaderFormat)) {
869 // Return 0 element surface array.
870 return surfacePlanes;
871 }
872
873 LockedImage lockedImg = LockedImage();
874 Image_getLockedImage(env, thiz, &lockedImg, ndkReaderUsage);
875 if (env->ExceptionCheck()) {
876 return NULL;
877 }
878 // Create all SurfacePlanes
879 for (int i = 0; i < numPlanes; i++) {
880 if (!Image_getLockedImageInfo(env, &lockedImg, i, halReaderFormat,
881 &pData, &dataSize, &pixelStride, &rowStride)) {
882 return NULL;
883 }
884 byteBuffer = env->NewDirectByteBuffer(pData, dataSize);
885 if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) {
886 jniThrowException(env, "java/lang/IllegalStateException",
887 "Failed to allocate ByteBuffer");
888 return NULL;
889 }
890
891 // Finally, create this SurfacePlane.
892 jobject surfacePlane = env->NewObject(gSurfacePlaneClassInfo.clazz,
893 gSurfacePlaneClassInfo.ctor, thiz, rowStride, pixelStride, byteBuffer);
894 env->SetObjectArrayElement(surfacePlanes, i, surfacePlane);
895 }
896
897 return surfacePlanes;
898 }
899
900 static jint Image_getWidth(JNIEnv* env, jobject thiz)
901 {
902 BufferItem* buffer = Image_getBufferItem(env, thiz);
903 return getBufferWidth(buffer);
904 }
905
906 static jint Image_getHeight(JNIEnv* env, jobject thiz)
907 {
908 BufferItem* buffer = Image_getBufferItem(env, thiz);
909 return getBufferHeight(buffer);
910 }
911
912 static jint Image_getFenceFd(JNIEnv* env, jobject thiz)
913 {
914 BufferItem* buffer = Image_getBufferItem(env, thiz);
915 if ((buffer != NULL) && (buffer->mFence.get() != NULL)){
916 return buffer->mFence->get();
917 }
918
919 return -1;
920 }
921
922 static jint Image_getFormat(JNIEnv* env, jobject thiz, jint readerFormat)
923 {
924 if (isFormatOpaque(readerFormat)) {
925 // Assuming opaque reader produce opaque images.
926 return static_cast<jint>(PublicFormat::PRIVATE);
927 } else {
928 BufferItem* buffer = Image_getBufferItem(env, thiz);
929 int readerHalFormat = mapPublicFormatToHalFormat(static_cast<PublicFormat>(readerFormat));
930 int32_t fmt = applyFormatOverrides(
931 buffer->mGraphicBuffer->getPixelFormat(), readerHalFormat);
932 // Override the image format to HAL_PIXEL_FORMAT_YCbCr_420_888 if the actual format is
933 // NV21 or YV12. This could only happen when the Gralloc HAL version is v0.1 thus doesn't
934 // support lockycbcr(), the CpuConsumer need to use the lock() method in the
935 // lockNextBuffer() call. For Gralloc HAL v0.2 or newer, this format should already be
936 // overridden to HAL_PIXEL_FORMAT_YCbCr_420_888 for the flexible YUV compatible formats.
937 if (isPossiblyYUV(fmt)) {
938 fmt = HAL_PIXEL_FORMAT_YCbCr_420_888;
939 }
940 PublicFormat publicFmt = mapHalFormatDataspaceToPublicFormat(fmt, buffer->mDataSpace);
941 return static_cast<jint>(publicFmt);
942 }
943 }
944
945 static jobject Image_getHardwareBuffer(JNIEnv* env, jobject thiz) {
946 BufferItem* buffer = Image_getBufferItem(env, thiz);
947 AHardwareBuffer* b = AHardwareBuffer_from_GraphicBuffer(buffer->mGraphicBuffer.get());
948 // don't user the public AHardwareBuffer_toHardwareBuffer() because this would force us
949 // to link against libandroid.so
950 return android_hardware_HardwareBuffer_createFromAHardwareBuffer(env, b);
951 }
952
953 } // extern "C"
954
955 // ----------------------------------------------------------------------------
956
957 static const JNINativeMethod gImageReaderMethods[] = {
958 {"nativeClassInit", "()V", (void*)ImageReader_classInit },
959 {"nativeInit", "(Ljava/lang/Object;IIIJII)V", (void*)ImageReader_init },
960 {"nativeClose", "()V", (void*)ImageReader_close },
961 {"nativeReleaseImage", "(Landroid/media/Image;)V", (void*)ImageReader_imageRelease },
962 {"nativeImageSetup", "(Landroid/media/Image;)I", (void*)ImageReader_imageSetup },
963 {"nativeGetSurface", "()Landroid/view/Surface;", (void*)ImageReader_getSurface },
964 {"nativeDetachImage", "(Landroid/media/Image;)I", (void*)ImageReader_detachImage },
965 {"nativeCreateImagePlanes",
966 "(ILandroid/graphics/GraphicBuffer;IIIIII)[Landroid/media/ImageReader$ImagePlane;",
967 (void*)ImageReader_createImagePlanes },
968 {"nativeUnlockGraphicBuffer",
969 "(Landroid/graphics/GraphicBuffer;)V", (void*)ImageReader_unlockGraphicBuffer },
970 {"nativeDiscardFreeBuffers", "()V", (void*)ImageReader_discardFreeBuffers }
971 };
972
973 static const JNINativeMethod gImageMethods[] = {
974 {"nativeCreatePlanes", "(IIJ)[Landroid/media/ImageReader$SurfaceImage$SurfacePlane;",
975 (void*)Image_createSurfacePlanes },
976 {"nativeGetWidth", "()I", (void*)Image_getWidth },
977 {"nativeGetHeight", "()I", (void*)Image_getHeight },
978 {"nativeGetFormat", "(I)I", (void*)Image_getFormat },
979 {"nativeGetFenceFd", "()I", (void*)Image_getFenceFd },
980 {"nativeGetHardwareBuffer", "()Landroid/hardware/HardwareBuffer;",
981 (void*)Image_getHardwareBuffer },
982 };
983
984 int register_android_media_ImageReader(JNIEnv *env) {
985
986 int ret1 = AndroidRuntime::registerNativeMethods(env,
987 "android/media/ImageReader", gImageReaderMethods, NELEM(gImageReaderMethods));
988
989 int ret2 = AndroidRuntime::registerNativeMethods(env,
990 "android/media/ImageReader$SurfaceImage", gImageMethods, NELEM(gImageMethods));
991
992 return (ret1 || ret2);
993 }