blob: c7ae7eda32d102486944cf372f36d062a624a3f6 [file] [log] [blame]
Zhijun He212e78d2013-06-07 11:36:23 -07001/*
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 <utils/Log.h>
20#include <utils/misc.h>
21#include <utils/List.h>
Jeff Brownef961212013-08-05 20:39:29 -070022#include <utils/String8.h>
Zhijun He212e78d2013-06-07 11:36:23 -070023
24#include <cstdio>
25
26#include <gui/CpuConsumer.h>
27#include <gui/Surface.h>
Zhijun He534046d2013-07-01 11:03:41 -070028#include <camera3.h>
Zhijun He212e78d2013-06-07 11:36:23 -070029
30#include <android_runtime/AndroidRuntime.h>
31#include <android_runtime/android_view_Surface.h>
32
33#include <jni.h>
34#include <JNIHelp.h>
35
Igor Murashkin5096def2014-06-24 10:49:11 -070036#include <stdint.h>
37#include <inttypes.h>
38
Zhijun He212e78d2013-06-07 11:36:23 -070039#define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) )
40
Zhijun He212e78d2013-06-07 11:36:23 -070041#define ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID "mNativeContext"
42#define ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID "mLockedBuffer"
43#define ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID "mTimestamp"
44
45// ----------------------------------------------------------------------------
46
47using namespace android;
48
49enum {
50 IMAGE_READER_MAX_NUM_PLANES = 3,
51};
52
Igor Murashkine3351f12013-09-13 13:08:04 -070053enum {
54 ACQUIRE_SUCCESS = 0,
55 ACQUIRE_NO_BUFFERS = 1,
56 ACQUIRE_MAX_IMAGES = 2,
57};
58
Jeff Brownef961212013-08-05 20:39:29 -070059static struct {
60 jfieldID mNativeContext;
61 jmethodID postEventFromNative;
62} gImageReaderClassInfo;
Zhijun He212e78d2013-06-07 11:36:23 -070063
Jeff Brownef961212013-08-05 20:39:29 -070064static struct {
65 jfieldID mLockedBuffer;
66 jfieldID mTimestamp;
67} gSurfaceImageClassInfo;
68
69static struct {
Zhijun He212e78d2013-06-07 11:36:23 -070070 jclass clazz;
71 jmethodID ctor;
Jeff Brownef961212013-08-05 20:39:29 -070072} gSurfacePlaneClassInfo;
Zhijun He212e78d2013-06-07 11:36:23 -070073
74// ----------------------------------------------------------------------------
75
76class JNIImageReaderContext : public CpuConsumer::FrameAvailableListener
77{
78public:
79 JNIImageReaderContext(JNIEnv* env, jobject weakThiz, jclass clazz, int maxImages);
80
81 virtual ~JNIImageReaderContext();
82
Dan Stoza2c34b5e2014-11-04 11:36:33 -080083 virtual void onFrameAvailable(const BufferItem& item);
Zhijun He212e78d2013-06-07 11:36:23 -070084
85 CpuConsumer::LockedBuffer* getLockedBuffer();
86
87 void returnLockedBuffer(CpuConsumer::LockedBuffer* buffer);
88
Mathias Agopian52a9a102013-08-02 01:38:38 -070089 void setCpuConsumer(const sp<CpuConsumer>& consumer) { mConsumer = consumer; }
Zhijun He212e78d2013-06-07 11:36:23 -070090 CpuConsumer* getCpuConsumer() { return mConsumer.get(); }
91
Dan Stoza5b3c7c12014-03-12 16:44:45 -070092 void setProducer(const sp<IGraphicBufferProducer>& producer) { mProducer = producer; }
93 IGraphicBufferProducer* getProducer() { return mProducer.get(); }
Zhijun He212e78d2013-06-07 11:36:23 -070094
95 void setBufferFormat(int format) { mFormat = format; }
96 int getBufferFormat() { return mFormat; }
97
98 void setBufferWidth(int width) { mWidth = width; }
99 int getBufferWidth() { return mWidth; }
100
101 void setBufferHeight(int height) { mHeight = height; }
102 int getBufferHeight() { return mHeight; }
103
104private:
105 static JNIEnv* getJNIEnv(bool* needsDetach);
106 static void detachJNI();
107
108 List<CpuConsumer::LockedBuffer*> mBuffers;
109 sp<CpuConsumer> mConsumer;
Dan Stoza5b3c7c12014-03-12 16:44:45 -0700110 sp<IGraphicBufferProducer> mProducer;
Zhijun He212e78d2013-06-07 11:36:23 -0700111 jobject mWeakThiz;
112 jclass mClazz;
113 int mFormat;
114 int mWidth;
115 int mHeight;
116};
117
118JNIImageReaderContext::JNIImageReaderContext(JNIEnv* env,
119 jobject weakThiz, jclass clazz, int maxImages) :
120 mWeakThiz(env->NewGlobalRef(weakThiz)),
121 mClazz((jclass)env->NewGlobalRef(clazz)) {
122 for (int i = 0; i < maxImages; i++) {
123 CpuConsumer::LockedBuffer *buffer = new CpuConsumer::LockedBuffer;
124 mBuffers.push_back(buffer);
125 }
126}
127
128JNIEnv* JNIImageReaderContext::getJNIEnv(bool* needsDetach) {
129 LOG_ALWAYS_FATAL_IF(needsDetach == NULL, "needsDetach is null!!!");
130 *needsDetach = false;
131 JNIEnv* env = AndroidRuntime::getJNIEnv();
132 if (env == NULL) {
133 JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL};
134 JavaVM* vm = AndroidRuntime::getJavaVM();
135 int result = vm->AttachCurrentThread(&env, (void*) &args);
136 if (result != JNI_OK) {
137 ALOGE("thread attach failed: %#x", result);
138 return NULL;
139 }
140 *needsDetach = true;
141 }
142 return env;
143}
144
145void JNIImageReaderContext::detachJNI() {
146 JavaVM* vm = AndroidRuntime::getJavaVM();
147 int result = vm->DetachCurrentThread();
148 if (result != JNI_OK) {
149 ALOGE("thread detach failed: %#x", result);
150 }
151}
152
153CpuConsumer::LockedBuffer* JNIImageReaderContext::getLockedBuffer() {
154 if (mBuffers.empty()) {
155 return NULL;
156 }
157 // Return a LockedBuffer pointer and remove it from the list
158 List<CpuConsumer::LockedBuffer*>::iterator it = mBuffers.begin();
159 CpuConsumer::LockedBuffer* buffer = *it;
160 mBuffers.erase(it);
161 return buffer;
162}
163
Igor Murashkind901c032013-08-27 15:19:55 -0700164void JNIImageReaderContext::returnLockedBuffer(CpuConsumer::LockedBuffer* buffer) {
Zhijun He212e78d2013-06-07 11:36:23 -0700165 mBuffers.push_back(buffer);
166}
167
168JNIImageReaderContext::~JNIImageReaderContext() {
169 bool needsDetach = false;
170 JNIEnv* env = getJNIEnv(&needsDetach);
171 if (env != NULL) {
172 env->DeleteGlobalRef(mWeakThiz);
173 env->DeleteGlobalRef(mClazz);
174 } else {
175 ALOGW("leaking JNI object references");
176 }
177 if (needsDetach) {
178 detachJNI();
179 }
180
181 // Delete LockedBuffers
182 for (List<CpuConsumer::LockedBuffer *>::iterator it = mBuffers.begin();
183 it != mBuffers.end(); it++) {
184 delete *it;
185 }
186 mBuffers.clear();
187 mConsumer.clear();
188}
189
Dan Stoza2c34b5e2014-11-04 11:36:33 -0800190void JNIImageReaderContext::onFrameAvailable(const BufferItem& /*item*/)
Zhijun He212e78d2013-06-07 11:36:23 -0700191{
192 ALOGV("%s: frame available", __FUNCTION__);
193 bool needsDetach = false;
194 JNIEnv* env = getJNIEnv(&needsDetach);
195 if (env != NULL) {
Jeff Brownef961212013-08-05 20:39:29 -0700196 env->CallStaticVoidMethod(mClazz, gImageReaderClassInfo.postEventFromNative, mWeakThiz);
Zhijun He212e78d2013-06-07 11:36:23 -0700197 } else {
198 ALOGW("onFrameAvailable event will not posted");
199 }
200 if (needsDetach) {
201 detachJNI();
202 }
203}
204
205// ----------------------------------------------------------------------------
206
207extern "C" {
208
209static JNIImageReaderContext* ImageReader_getContext(JNIEnv* env, jobject thiz)
210{
211 JNIImageReaderContext *ctx;
212 ctx = reinterpret_cast<JNIImageReaderContext *>
Jeff Brownef961212013-08-05 20:39:29 -0700213 (env->GetLongField(thiz, gImageReaderClassInfo.mNativeContext));
Zhijun He212e78d2013-06-07 11:36:23 -0700214 return ctx;
215}
216
217static CpuConsumer* ImageReader_getCpuConsumer(JNIEnv* env, jobject thiz)
218{
219 ALOGV("%s:", __FUNCTION__);
220 JNIImageReaderContext* const ctx = ImageReader_getContext(env, thiz);
221 if (ctx == NULL) {
222 jniThrowRuntimeException(env, "ImageReaderContext is not initialized");
223 return NULL;
224 }
225 return ctx->getCpuConsumer();
226}
227
Dan Stoza5b3c7c12014-03-12 16:44:45 -0700228static IGraphicBufferProducer* ImageReader_getProducer(JNIEnv* env, jobject thiz)
Mathias Agopian52a9a102013-08-02 01:38:38 -0700229{
230 ALOGV("%s:", __FUNCTION__);
231 JNIImageReaderContext* const ctx = ImageReader_getContext(env, thiz);
232 if (ctx == NULL) {
233 jniThrowRuntimeException(env, "ImageReaderContext is not initialized");
234 return NULL;
235 }
Dan Stoza5b3c7c12014-03-12 16:44:45 -0700236 return ctx->getProducer();
Mathias Agopian52a9a102013-08-02 01:38:38 -0700237}
238
Zhijun He212e78d2013-06-07 11:36:23 -0700239static void ImageReader_setNativeContext(JNIEnv* env,
240 jobject thiz, sp<JNIImageReaderContext> ctx)
241{
242 ALOGV("%s:", __FUNCTION__);
243 JNIImageReaderContext* const p = ImageReader_getContext(env, thiz);
244 if (ctx != 0) {
245 ctx->incStrong((void*)ImageReader_setNativeContext);
246 }
247 if (p) {
248 p->decStrong((void*)ImageReader_setNativeContext);
249 }
Jeff Brownef961212013-08-05 20:39:29 -0700250 env->SetLongField(thiz, gImageReaderClassInfo.mNativeContext,
251 reinterpret_cast<jlong>(ctx.get()));
Zhijun He212e78d2013-06-07 11:36:23 -0700252}
253
254static CpuConsumer::LockedBuffer* Image_getLockedBuffer(JNIEnv* env, jobject image)
255{
Jeff Brownef961212013-08-05 20:39:29 -0700256 return reinterpret_cast<CpuConsumer::LockedBuffer*>(
257 env->GetLongField(image, gSurfaceImageClassInfo.mLockedBuffer));
Zhijun He212e78d2013-06-07 11:36:23 -0700258}
259
260static void Image_setBuffer(JNIEnv* env, jobject thiz,
261 const CpuConsumer::LockedBuffer* buffer)
262{
Jeff Brownef961212013-08-05 20:39:29 -0700263 env->SetLongField(thiz, gSurfaceImageClassInfo.mLockedBuffer, reinterpret_cast<jlong>(buffer));
Zhijun He212e78d2013-06-07 11:36:23 -0700264}
265
266// Some formats like JPEG defined with different values between android.graphics.ImageFormat and
267// graphics.h, need convert to the one defined in graphics.h here.
268static int Image_getPixelFormat(JNIEnv* env, int format)
269{
Zhijun Hed1988a92014-07-01 04:15:09 -0700270 int jpegFormat;
Zhijun He212e78d2013-06-07 11:36:23 -0700271 jfieldID fid;
272
273 ALOGV("%s: format = 0x%x", __FUNCTION__, format);
274
275 jclass imageFormatClazz = env->FindClass("android/graphics/ImageFormat");
276 ALOG_ASSERT(imageFormatClazz != NULL);
277
278 fid = env->GetStaticFieldID(imageFormatClazz, "JPEG", "I");
279 jpegFormat = env->GetStaticIntField(imageFormatClazz, fid);
Zhijun He212e78d2013-06-07 11:36:23 -0700280
Zhijun Hea5e15282014-04-24 14:08:51 -0700281 // Translate the JPEG to BLOB for camera purpose.
Zhijun He212e78d2013-06-07 11:36:23 -0700282 if (format == jpegFormat) {
283 format = HAL_PIXEL_FORMAT_BLOB;
284 }
Zhijun He212e78d2013-06-07 11:36:23 -0700285
286 return format;
287}
288
Ruben Brunk31798f32014-09-25 19:56:54 -0700289static uint32_t Image_getJpegSize(CpuConsumer::LockedBuffer* buffer, bool usingRGBAOverride)
Zhijun He534046d2013-07-01 11:03:41 -0700290{
291 ALOG_ASSERT(buffer != NULL, "Input buffer is NULL!!!");
292 uint32_t size = 0;
293 uint32_t width = buffer->width;
294 uint8_t* jpegBuffer = buffer->data;
295
Ruben Brunk31798f32014-09-25 19:56:54 -0700296 if (usingRGBAOverride) {
Ruben Brunk0c798842014-09-30 03:42:13 -0700297 width = (buffer->width + buffer->stride * (buffer->height - 1)) * 4;
Ruben Brunk31798f32014-09-25 19:56:54 -0700298 }
299
Zhijun He534046d2013-07-01 11:03:41 -0700300 // First check for JPEG transport header at the end of the buffer
301 uint8_t* header = jpegBuffer + (width - sizeof(struct camera3_jpeg_blob));
302 struct camera3_jpeg_blob *blob = (struct camera3_jpeg_blob*)(header);
303 if (blob->jpeg_blob_id == CAMERA3_JPEG_BLOB_ID) {
304 size = blob->jpeg_size;
305 ALOGV("%s: Jpeg size = %d", __FUNCTION__, size);
306 }
307
308 // failed to find size, default to whole buffer
309 if (size == 0) {
Igor Murashkin5096def2014-06-24 10:49:11 -0700310 /*
311 * This is a problem because not including the JPEG header
312 * means that in certain rare situations a regular JPEG blob
313 * will be misidentified as having a header, in which case
314 * we will get a garbage size value.
315 */
316 ALOGW("%s: No JPEG header detected, defaulting to size=width=%d",
317 __FUNCTION__, width);
Zhijun He534046d2013-07-01 11:03:41 -0700318 size = width;
319 }
320
321 return size;
322}
323
Ruben Brunk31798f32014-09-25 19:56:54 -0700324static bool usingRGBAToJpegOverride(int32_t bufferFormat, int32_t readerCtxFormat) {
325 return readerCtxFormat == HAL_PIXEL_FORMAT_BLOB && bufferFormat == HAL_PIXEL_FORMAT_RGBA_8888;
326}
327
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700328static int32_t applyFormatOverrides(int32_t bufferFormat, int32_t readerCtxFormat)
329{
330 // Using HAL_PIXEL_FORMAT_RGBA_8888 gralloc buffers containing JPEGs to get around SW
331 // write limitations for some platforms (b/17379185).
Ruben Brunk31798f32014-09-25 19:56:54 -0700332 if (usingRGBAToJpegOverride(bufferFormat, readerCtxFormat)) {
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700333 return HAL_PIXEL_FORMAT_BLOB;
334 }
335 return bufferFormat;
336}
337
Zhijun He212e78d2013-06-07 11:36:23 -0700338static void Image_getLockedBufferInfo(JNIEnv* env, CpuConsumer::LockedBuffer* buffer, int idx,
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700339 uint8_t **base, uint32_t *size, int32_t readerFormat)
Zhijun He212e78d2013-06-07 11:36:23 -0700340{
341 ALOG_ASSERT(buffer != NULL, "Input buffer is NULL!!!");
342 ALOG_ASSERT(base != NULL, "base is NULL!!!");
343 ALOG_ASSERT(size != NULL, "size is NULL!!!");
344 ALOG_ASSERT((idx < IMAGE_READER_MAX_NUM_PLANES) && (idx >= 0));
345
Zhijun He7f4d3142013-07-23 07:54:38 -0700346 ALOGV("%s: buffer: %p", __FUNCTION__, buffer);
Zhijun He212e78d2013-06-07 11:36:23 -0700347
348 uint32_t dataSize, ySize, cSize, cStride;
349 uint8_t *cb, *cr;
350 uint8_t *pData = NULL;
Zhijun He708e3592013-08-05 14:56:11 -0700351 int bytesPerPixel = 0;
Zhijun He212e78d2013-06-07 11:36:23 -0700352
353 dataSize = ySize = cSize = cStride = 0;
Lajos Molnar4fb44262015-01-22 19:03:31 -0800354 int32_t fmt = buffer->flexFormat;
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700355
Ruben Brunk31798f32014-09-25 19:56:54 -0700356 bool usingRGBAOverride = usingRGBAToJpegOverride(fmt, readerFormat);
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700357 fmt = applyFormatOverrides(fmt, readerFormat);
Zhijun He212e78d2013-06-07 11:36:23 -0700358 switch (fmt) {
359 case HAL_PIXEL_FORMAT_YCbCr_420_888:
360 pData =
361 (idx == 0) ?
362 buffer->data :
363 (idx == 1) ?
364 buffer->dataCb :
365 buffer->dataCr;
Lajos Molnar4fb44262015-01-22 19:03:31 -0800366 // only map until last pixel
Zhijun He212e78d2013-06-07 11:36:23 -0700367 if (idx == 0) {
Lajos Molnar4fb44262015-01-22 19:03:31 -0800368 dataSize = buffer->stride * (buffer->height - 1) + buffer->width;
Zhijun He212e78d2013-06-07 11:36:23 -0700369 } else {
Lajos Molnar4fb44262015-01-22 19:03:31 -0800370 dataSize = buffer->chromaStride * (buffer->height / 2 - 1) +
371 buffer->chromaStep * (buffer->width / 2 - 1) + 1;
Zhijun He212e78d2013-06-07 11:36:23 -0700372 }
373 break;
374 // NV21
375 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
376 cr = buffer->data + (buffer->stride * buffer->height);
377 cb = cr + 1;
Lajos Molnar4fb44262015-01-22 19:03:31 -0800378 // only map until last pixel
379 ySize = buffer->width * (buffer->height - 1) + buffer->width;
380 cSize = buffer->width * (buffer->height / 2 - 1) + buffer->width - 1;
Zhijun He212e78d2013-06-07 11:36:23 -0700381
382 pData =
383 (idx == 0) ?
384 buffer->data :
385 (idx == 1) ?
386 cb:
387 cr;
388
389 dataSize = (idx == 0) ? ySize : cSize;
390 break;
391 case HAL_PIXEL_FORMAT_YV12:
392 // Y and C stride need to be 16 pixel aligned.
393 LOG_ALWAYS_FATAL_IF(buffer->stride % 16,
394 "Stride is not 16 pixel aligned %d", buffer->stride);
395
396 ySize = buffer->stride * buffer->height;
397 cStride = ALIGN(buffer->stride / 2, 16);
398 cr = buffer->data + ySize;
399 cSize = cStride * buffer->height / 2;
400 cb = cr + cSize;
401
402 pData =
403 (idx == 0) ?
404 buffer->data :
405 (idx == 1) ?
406 cb :
407 cr;
408 dataSize = (idx == 0) ? ySize : cSize;
409 break;
410 case HAL_PIXEL_FORMAT_Y8:
411 // Single plane, 8bpp.
412 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
413
414 pData = buffer->data;
415 dataSize = buffer->stride * buffer->height;
416 break;
417 case HAL_PIXEL_FORMAT_Y16:
Zhijun He977ad8d2014-04-08 16:59:29 -0700418 bytesPerPixel = 2;
Zhijun He212e78d2013-06-07 11:36:23 -0700419 // Single plane, 16bpp, strides are specified in pixels, not in bytes
420 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
421
422 pData = buffer->data;
Zhijun He977ad8d2014-04-08 16:59:29 -0700423 dataSize = buffer->stride * buffer->height * bytesPerPixel;
Zhijun He212e78d2013-06-07 11:36:23 -0700424 break;
425 case HAL_PIXEL_FORMAT_BLOB:
426 // Used for JPEG data, height must be 1, width == size, single plane.
427 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
428 ALOG_ASSERT(buffer->height == 1, "JPEG should has height value %d", buffer->height);
429
430 pData = buffer->data;
Ruben Brunk31798f32014-09-25 19:56:54 -0700431 dataSize = Image_getJpegSize(buffer, usingRGBAOverride);
Zhijun He212e78d2013-06-07 11:36:23 -0700432 break;
433 case HAL_PIXEL_FORMAT_RAW_SENSOR:
434 // Single plane 16bpp bayer data.
Zhijun He977ad8d2014-04-08 16:59:29 -0700435 bytesPerPixel = 2;
Zhijun He212e78d2013-06-07 11:36:23 -0700436 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
437 pData = buffer->data;
Zhijun He977ad8d2014-04-08 16:59:29 -0700438 dataSize = buffer->stride * buffer->height * bytesPerPixel;
Zhijun He212e78d2013-06-07 11:36:23 -0700439 break;
Zhijun Hed1988a92014-07-01 04:15:09 -0700440 case HAL_PIXEL_FORMAT_RAW10:
441 // Single plane 10bpp bayer data.
442 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
443 LOG_ALWAYS_FATAL_IF(buffer->width % 4,
444 "Width is not multiple of 4 %d", buffer->width);
445 LOG_ALWAYS_FATAL_IF(buffer->height % 2,
446 "Height is not even %d", buffer->height);
Zhijun He4c4064f2014-07-25 09:06:11 -0700447 LOG_ALWAYS_FATAL_IF(buffer->stride < (buffer->width * 10 / 8),
448 "stride (%d) should be at least %d",
449 buffer->stride, buffer->width * 10 / 8);
Zhijun Hed1988a92014-07-01 04:15:09 -0700450 pData = buffer->data;
Zhijun He4c4064f2014-07-25 09:06:11 -0700451 dataSize = buffer->stride * buffer->height;
Zhijun Hed1988a92014-07-01 04:15:09 -0700452 break;
Zhijun He708e3592013-08-05 14:56:11 -0700453 case HAL_PIXEL_FORMAT_RGBA_8888:
454 case HAL_PIXEL_FORMAT_RGBX_8888:
455 // Single plane, 32bpp.
456 bytesPerPixel = 4;
457 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
458 pData = buffer->data;
459 dataSize = buffer->stride * buffer->height * bytesPerPixel;
460 break;
461 case HAL_PIXEL_FORMAT_RGB_565:
462 // Single plane, 16bpp.
463 bytesPerPixel = 2;
464 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
465 pData = buffer->data;
466 dataSize = buffer->stride * buffer->height * bytesPerPixel;
467 break;
468 case HAL_PIXEL_FORMAT_RGB_888:
469 // Single plane, 24bpp.
470 bytesPerPixel = 3;
471 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
472 pData = buffer->data;
473 dataSize = buffer->stride * buffer->height * bytesPerPixel;
474 break;
Zhijun He212e78d2013-06-07 11:36:23 -0700475 default:
476 jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
477 "Pixel format: 0x%x is unsupported", fmt);
478 break;
479 }
480
481 *base = pData;
482 *size = dataSize;
483}
484
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700485static jint Image_imageGetPixelStride(JNIEnv* env, CpuConsumer::LockedBuffer* buffer, int idx,
486 int32_t readerFormat)
Zhijun He212e78d2013-06-07 11:36:23 -0700487{
488 ALOGV("%s: buffer index: %d", __FUNCTION__, idx);
489 ALOG_ASSERT((idx < IMAGE_READER_MAX_NUM_PLANES) && (idx >= 0), "Index is out of range:%d", idx);
490
491 int pixelStride = 0;
492 ALOG_ASSERT(buffer != NULL, "buffer is NULL");
493
Lajos Molnar4fb44262015-01-22 19:03:31 -0800494 int32_t fmt = buffer->flexFormat;
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700495
496 fmt = applyFormatOverrides(fmt, readerFormat);
497
Zhijun He212e78d2013-06-07 11:36:23 -0700498 switch (fmt) {
499 case HAL_PIXEL_FORMAT_YCbCr_420_888:
500 pixelStride = (idx == 0) ? 1 : buffer->chromaStep;
501 break;
502 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
503 pixelStride = (idx == 0) ? 1 : 2;
504 break;
505 case HAL_PIXEL_FORMAT_Y8:
506 // Single plane 8bpp data.
507 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
Zhijun He212e78d2013-06-07 11:36:23 -0700508 break;
509 case HAL_PIXEL_FORMAT_YV12:
510 pixelStride = 1;
511 break;
512 case HAL_PIXEL_FORMAT_BLOB:
Zhijun Hed1988a92014-07-01 04:15:09 -0700513 case HAL_PIXEL_FORMAT_RAW10:
514 // Blob is used for JPEG data, RAW10 is used for 10-bit raw data, they are
515 // single plane, row and pixel strides are 0.
Zhijun He212e78d2013-06-07 11:36:23 -0700516 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
517 pixelStride = 0;
518 break;
519 case HAL_PIXEL_FORMAT_Y16:
520 case HAL_PIXEL_FORMAT_RAW_SENSOR:
Zhijun He708e3592013-08-05 14:56:11 -0700521 case HAL_PIXEL_FORMAT_RGB_565:
Zhijun He212e78d2013-06-07 11:36:23 -0700522 // Single plane 16bpp data.
523 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
524 pixelStride = 2;
525 break;
Zhijun He708e3592013-08-05 14:56:11 -0700526 case HAL_PIXEL_FORMAT_RGBA_8888:
527 case HAL_PIXEL_FORMAT_RGBX_8888:
528 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
529 pixelStride = 4;
530 break;
531 case HAL_PIXEL_FORMAT_RGB_888:
532 // Single plane, 24bpp.
533 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
534 pixelStride = 3;
535 break;
Zhijun He212e78d2013-06-07 11:36:23 -0700536 default:
537 jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
538 "Pixel format: 0x%x is unsupported", fmt);
539 break;
540 }
541
542 return pixelStride;
543}
544
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700545static jint Image_imageGetRowStride(JNIEnv* env, CpuConsumer::LockedBuffer* buffer, int idx,
546 int32_t readerFormat)
Zhijun He212e78d2013-06-07 11:36:23 -0700547{
548 ALOGV("%s: buffer index: %d", __FUNCTION__, idx);
549 ALOG_ASSERT((idx < IMAGE_READER_MAX_NUM_PLANES) && (idx >= 0));
550
551 int rowStride = 0;
552 ALOG_ASSERT(buffer != NULL, "buffer is NULL");
553
Lajos Molnar4fb44262015-01-22 19:03:31 -0800554 int32_t fmt = buffer->flexFormat;
Zhijun He212e78d2013-06-07 11:36:23 -0700555
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700556 fmt = applyFormatOverrides(fmt, readerFormat);
557
Zhijun He212e78d2013-06-07 11:36:23 -0700558 switch (fmt) {
559 case HAL_PIXEL_FORMAT_YCbCr_420_888:
560 rowStride = (idx == 0) ? buffer->stride : buffer->chromaStride;
561 break;
562 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
563 rowStride = buffer->width;
564 break;
565 case HAL_PIXEL_FORMAT_YV12:
566 LOG_ALWAYS_FATAL_IF(buffer->stride % 16,
567 "Stride is not 16 pixel aligned %d", buffer->stride);
568 rowStride = (idx == 0) ? buffer->stride : ALIGN(buffer->stride / 2, 16);
569 break;
570 case HAL_PIXEL_FORMAT_BLOB:
Zhijun Hed1988a92014-07-01 04:15:09 -0700571 // Blob is used for JPEG data, RAW10 is used for 10-bit raw data, they are
572 // single plane, row and pixel strides are 0.
Zhijun He212e78d2013-06-07 11:36:23 -0700573 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
574 rowStride = 0;
575 break;
Zhijun He4c4064f2014-07-25 09:06:11 -0700576 case HAL_PIXEL_FORMAT_RAW10:
577 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
578 rowStride = buffer->stride;
579 break;
Zhijun He212e78d2013-06-07 11:36:23 -0700580 case HAL_PIXEL_FORMAT_Y8:
581 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
582 LOG_ALWAYS_FATAL_IF(buffer->stride % 16,
583 "Stride is not 16 pixel aligned %d", buffer->stride);
584 rowStride = buffer->stride;
585 break;
586 case HAL_PIXEL_FORMAT_Y16:
587 case HAL_PIXEL_FORMAT_RAW_SENSOR:
588 // In native side, strides are specified in pixels, not in bytes.
589 // Single plane 16bpp bayer data. even width/height,
590 // row stride multiple of 16 pixels (32 bytes)
591 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
592 LOG_ALWAYS_FATAL_IF(buffer->stride % 16,
593 "Stride is not 16 pixel aligned %d", buffer->stride);
594 rowStride = buffer->stride * 2;
595 break;
Zhijun He708e3592013-08-05 14:56:11 -0700596 case HAL_PIXEL_FORMAT_RGB_565:
597 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
598 rowStride = buffer->stride * 2;
599 break;
600 case HAL_PIXEL_FORMAT_RGBA_8888:
601 case HAL_PIXEL_FORMAT_RGBX_8888:
602 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
603 rowStride = buffer->stride * 4;
604 break;
605 case HAL_PIXEL_FORMAT_RGB_888:
606 // Single plane, 24bpp.
607 ALOG_ASSERT(idx == 0, "Wrong index: %d", idx);
608 rowStride = buffer->stride * 3;
609 break;
Zhijun He212e78d2013-06-07 11:36:23 -0700610 default:
611 ALOGE("%s Pixel format: 0x%x is unsupported", __FUNCTION__, fmt);
612 jniThrowException(env, "java/lang/UnsupportedOperationException",
613 "unsupported buffer format");
614 break;
615 }
616
617 return rowStride;
618}
619
Ruben Brunkf4a637d2014-11-20 18:01:36 -0800620static int Image_getBufferWidth(CpuConsumer::LockedBuffer* buffer) {
621 if (buffer == NULL) return -1;
622
623 if (!buffer->crop.isEmpty()) {
624 return buffer->crop.getWidth();
625 }
626 return buffer->width;
627}
628
629static int Image_getBufferHeight(CpuConsumer::LockedBuffer* buffer) {
630 if (buffer == NULL) return -1;
631
632 if (!buffer->crop.isEmpty()) {
633 return buffer->crop.getHeight();
634 }
635 return buffer->height;
636}
637
Zhijun He212e78d2013-06-07 11:36:23 -0700638// ----------------------------------------------------------------------------
639
640static void ImageReader_classInit(JNIEnv* env, jclass clazz)
641{
642 ALOGV("%s:", __FUNCTION__);
643
644 jclass imageClazz = env->FindClass("android/media/ImageReader$SurfaceImage");
645 LOG_ALWAYS_FATAL_IF(imageClazz == NULL,
646 "can't find android/graphics/ImageReader$SurfaceImage");
Jeff Brownef961212013-08-05 20:39:29 -0700647 gSurfaceImageClassInfo.mLockedBuffer = env->GetFieldID(
648 imageClazz, ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID, "J");
649 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mLockedBuffer == NULL,
Zhijun He212e78d2013-06-07 11:36:23 -0700650 "can't find android/graphics/ImageReader.%s",
651 ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID);
652
Jeff Brownef961212013-08-05 20:39:29 -0700653 gSurfaceImageClassInfo.mTimestamp = env->GetFieldID(
654 imageClazz, ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID, "J");
655 LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mTimestamp == NULL,
Zhijun He212e78d2013-06-07 11:36:23 -0700656 "can't find android/graphics/ImageReader.%s",
657 ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID);
658
Jeff Brownef961212013-08-05 20:39:29 -0700659 gImageReaderClassInfo.mNativeContext = env->GetFieldID(
660 clazz, ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID, "J");
661 LOG_ALWAYS_FATAL_IF(gImageReaderClassInfo.mNativeContext == NULL,
Zhijun He212e78d2013-06-07 11:36:23 -0700662 "can't find android/graphics/ImageReader.%s",
663 ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID);
664
Jeff Brownef961212013-08-05 20:39:29 -0700665 gImageReaderClassInfo.postEventFromNative = env->GetStaticMethodID(
666 clazz, "postEventFromNative", "(Ljava/lang/Object;)V");
667 LOG_ALWAYS_FATAL_IF(gImageReaderClassInfo.postEventFromNative == NULL,
Zhijun He212e78d2013-06-07 11:36:23 -0700668 "can't find android/graphics/ImageReader.postEventFromNative");
669
670 jclass planeClazz = env->FindClass("android/media/ImageReader$SurfaceImage$SurfacePlane");
671 LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find SurfacePlane class");
672 // FindClass only gives a local reference of jclass object.
Jeff Brownef961212013-08-05 20:39:29 -0700673 gSurfacePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz);
674 gSurfacePlaneClassInfo.ctor = env->GetMethodID(gSurfacePlaneClassInfo.clazz, "<init>",
675 "(Landroid/media/ImageReader$SurfaceImage;III)V");
676 LOG_ALWAYS_FATAL_IF(gSurfacePlaneClassInfo.ctor == NULL,
677 "Can not find SurfacePlane constructor");
Zhijun He212e78d2013-06-07 11:36:23 -0700678}
679
680static void ImageReader_init(JNIEnv* env, jobject thiz, jobject weakThiz,
681 jint width, jint height, jint format, jint maxImages)
682{
683 status_t res;
684 int nativeFormat;
685
686 ALOGV("%s: width:%d, height: %d, format: 0x%x, maxImages:%d",
687 __FUNCTION__, width, height, format, maxImages);
688
689 nativeFormat = Image_getPixelFormat(env, format);
690
Dan Stoza5b3c7c12014-03-12 16:44:45 -0700691 sp<IGraphicBufferProducer> gbProducer;
692 sp<IGraphicBufferConsumer> gbConsumer;
693 BufferQueue::createBufferQueue(&gbProducer, &gbConsumer);
694 sp<CpuConsumer> consumer = new CpuConsumer(gbConsumer, maxImages,
Igor Murashkindd064322013-08-14 19:05:17 -0700695 /*controlledByApp*/true);
Zhijun He212e78d2013-06-07 11:36:23 -0700696 // TODO: throw dvm exOutOfMemoryError?
697 if (consumer == NULL) {
698 jniThrowRuntimeException(env, "Failed to allocate native CpuConsumer");
699 return;
700 }
701
702 jclass clazz = env->GetObjectClass(thiz);
703 if (clazz == NULL) {
704 jniThrowRuntimeException(env, "Can't find android/graphics/ImageReader");
705 return;
706 }
707 sp<JNIImageReaderContext> ctx(new JNIImageReaderContext(env, weakThiz, clazz, maxImages));
708 ctx->setCpuConsumer(consumer);
Dan Stoza5b3c7c12014-03-12 16:44:45 -0700709 ctx->setProducer(gbProducer);
Zhijun He212e78d2013-06-07 11:36:23 -0700710 consumer->setFrameAvailableListener(ctx);
711 ImageReader_setNativeContext(env, thiz, ctx);
712 ctx->setBufferFormat(nativeFormat);
713 ctx->setBufferWidth(width);
714 ctx->setBufferHeight(height);
715
716 // Set the width/height/format to the CpuConsumer
717 res = consumer->setDefaultBufferSize(width, height);
718 if (res != OK) {
719 jniThrowException(env, "java/lang/IllegalStateException",
720 "Failed to set CpuConsumer buffer size");
721 return;
722 }
723 res = consumer->setDefaultBufferFormat(nativeFormat);
724 if (res != OK) {
725 jniThrowException(env, "java/lang/IllegalStateException",
726 "Failed to set CpuConsumer buffer format");
727 }
728}
729
730static void ImageReader_close(JNIEnv* env, jobject thiz)
731{
732 ALOGV("%s:", __FUNCTION__);
733
734 JNIImageReaderContext* const ctx = ImageReader_getContext(env, thiz);
735 if (ctx == NULL) {
736 // ImageReader is already closed.
737 return;
738 }
739
740 CpuConsumer* consumer = ImageReader_getCpuConsumer(env, thiz);
741 if (consumer != NULL) {
742 consumer->abandon();
743 consumer->setFrameAvailableListener(NULL);
744 }
745 ImageReader_setNativeContext(env, thiz, NULL);
746}
747
748static void ImageReader_imageRelease(JNIEnv* env, jobject thiz, jobject image)
749{
750 ALOGV("%s:", __FUNCTION__);
751 JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
752 if (ctx == NULL) {
753 ALOGW("ImageReader#close called before Image#close, consider calling Image#close first");
754 return;
755 }
756
757 CpuConsumer* consumer = ctx->getCpuConsumer();
758 CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, image);
759 if (!buffer) {
760 ALOGW("Image already released!!!");
761 return;
762 }
763 consumer->unlockBuffer(*buffer);
764 Image_setBuffer(env, image, NULL);
765 ctx->returnLockedBuffer(buffer);
766}
767
Igor Murashkine3351f12013-09-13 13:08:04 -0700768static jint ImageReader_imageSetup(JNIEnv* env, jobject thiz,
Zhijun He212e78d2013-06-07 11:36:23 -0700769 jobject image)
770{
771 ALOGV("%s:", __FUNCTION__);
772 JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
773 if (ctx == NULL) {
774 jniThrowRuntimeException(env, "ImageReaderContext is not initialized");
Igor Murashkine3351f12013-09-13 13:08:04 -0700775 return -1;
Zhijun He212e78d2013-06-07 11:36:23 -0700776 }
777
778 CpuConsumer* consumer = ctx->getCpuConsumer();
779 CpuConsumer::LockedBuffer* buffer = ctx->getLockedBuffer();
780 if (buffer == NULL) {
Igor Murashkind901c032013-08-27 15:19:55 -0700781 ALOGW("Unable to acquire a lockedBuffer, very likely client tries to lock more than"
782 " maxImages buffers");
Igor Murashkine3351f12013-09-13 13:08:04 -0700783 return ACQUIRE_MAX_IMAGES;
Zhijun He212e78d2013-06-07 11:36:23 -0700784 }
785 status_t res = consumer->lockNextBuffer(buffer);
786 if (res != NO_ERROR) {
lina.x.pi33477892014-01-27 10:31:44 +0800787 ctx->returnLockedBuffer(buffer);
Jeff Brownf724c272013-08-07 14:17:04 -0700788 if (res != BAD_VALUE /*no buffers*/) {
Igor Murashkindd064322013-08-14 19:05:17 -0700789 if (res == NOT_ENOUGH_DATA) {
Igor Murashkine3351f12013-09-13 13:08:04 -0700790 return ACQUIRE_MAX_IMAGES;
Igor Murashkindd064322013-08-14 19:05:17 -0700791 } else {
792 ALOGE("%s Fail to lockNextBuffer with error: %d ",
793 __FUNCTION__, res);
Igor Murashkine3351f12013-09-13 13:08:04 -0700794 jniThrowExceptionFmt(env, "java/lang/AssertionError",
Igor Murashkindd064322013-08-14 19:05:17 -0700795 "Unknown error (%d) when we tried to lock buffer.",
796 res);
797 }
Jeff Brownf724c272013-08-07 14:17:04 -0700798 }
Igor Murashkine3351f12013-09-13 13:08:04 -0700799 return ACQUIRE_NO_BUFFERS;
Zhijun He212e78d2013-06-07 11:36:23 -0700800 }
801
Lajos Molnar4fb44262015-01-22 19:03:31 -0800802 if (buffer->flexFormat == HAL_PIXEL_FORMAT_YCrCb_420_SP) {
Zhijun He4eda9f52013-09-18 08:00:02 -0700803 jniThrowException(env, "java/lang/UnsupportedOperationException",
804 "NV21 format is not supported by ImageReader");
805 return -1;
806 }
807
Zhijun He212e78d2013-06-07 11:36:23 -0700808 // Check if the left-top corner of the crop rect is origin, we currently assume this point is
809 // zero, will revist this once this assumption turns out problematic.
810 Point lt = buffer->crop.leftTop();
811 if (lt.x != 0 || lt.y != 0) {
Zhijun He4eda9f52013-09-18 08:00:02 -0700812 jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
813 "crop left top corner [%d, %d] need to be at origin", lt.x, lt.y);
Igor Murashkine3351f12013-09-13 13:08:04 -0700814 return -1;
Zhijun He212e78d2013-06-07 11:36:23 -0700815 }
816
817 // Check if the producer buffer configurations match what ImageReader configured.
Ruben Brunkf4a637d2014-11-20 18:01:36 -0800818 int outputWidth = Image_getBufferWidth(buffer);
819 int outputHeight = Image_getBufferHeight(buffer);
Zhijun He534046d2013-07-01 11:03:41 -0700820
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700821 int imgReaderFmt = ctx->getBufferFormat();
Zhijun He212e78d2013-06-07 11:36:23 -0700822 int imageReaderWidth = ctx->getBufferWidth();
823 int imageReaderHeight = ctx->getBufferHeight();
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700824 if ((buffer->format != HAL_PIXEL_FORMAT_BLOB) && (imgReaderFmt != HAL_PIXEL_FORMAT_BLOB) &&
Ruben Brunkf4a637d2014-11-20 18:01:36 -0800825 (imageReaderWidth != outputWidth || imageReaderHeight != outputHeight)) {
826 ALOGV("%s: Producer buffer size: %dx%d, doesn't match ImageReader configured size: %dx%d",
827 __FUNCTION__, outputWidth, outputHeight, imageReaderWidth, imageReaderHeight);
Zhijun He212e78d2013-06-07 11:36:23 -0700828 }
829
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700830 int bufFmt = buffer->format;
Lajos Molnar4fb44262015-01-22 19:03:31 -0800831 if (imgReaderFmt == HAL_PIXEL_FORMAT_YCbCr_420_888) {
832 bufFmt = buffer->flexFormat;
833 }
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700834 if (imgReaderFmt != bufFmt) {
Ruben Brunk91b9aab2014-06-20 00:24:56 -0700835 if (imgReaderFmt == HAL_PIXEL_FORMAT_YCbCr_420_888 && (bufFmt ==
836 HAL_PIXEL_FORMAT_YCrCb_420_SP || bufFmt == HAL_PIXEL_FORMAT_YV12)) {
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700837 // Special casing for when producer switches to a format compatible with flexible YUV
838 // (HAL_PIXEL_FORMAT_YCbCr_420_888).
Ruben Brunk91b9aab2014-06-20 00:24:56 -0700839 ctx->setBufferFormat(bufFmt);
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700840 ALOGD("%s: Overriding buffer format YUV_420_888 to %x.", __FUNCTION__, bufFmt);
841 } else if (imgReaderFmt == HAL_PIXEL_FORMAT_BLOB && bufFmt == HAL_PIXEL_FORMAT_RGBA_8888) {
842 // Using HAL_PIXEL_FORMAT_RGBA_8888 gralloc buffers containing JPEGs to get around SW
843 // write limitations for (b/17379185).
844 ALOGD("%s: Receiving JPEG in HAL_PIXEL_FORMAT_RGBA_8888 buffer.", __FUNCTION__);
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700845 } else {
846 // Return the buffer to the queue.
847 consumer->unlockBuffer(*buffer);
848 ctx->returnLockedBuffer(buffer);
Zhijun He212e78d2013-06-07 11:36:23 -0700849
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700850 // Throw exception
851 ALOGE("Producer output buffer format: 0x%x, ImageReader configured format: 0x%x",
852 buffer->format, ctx->getBufferFormat());
853 String8 msg;
854 msg.appendFormat("The producer output buffer format 0x%x doesn't "
855 "match the ImageReader's configured buffer format 0x%x.",
Lajos Molnar4fb44262015-01-22 19:03:31 -0800856 bufFmt, ctx->getBufferFormat());
Ruben Brunkfeb50af2014-05-09 19:58:49 -0700857 jniThrowException(env, "java/lang/UnsupportedOperationException",
858 msg.string());
859 return -1;
860 }
Zhijun He212e78d2013-06-07 11:36:23 -0700861 }
862 // Set SurfaceImage instance member variables
863 Image_setBuffer(env, image, buffer);
Jeff Brownef961212013-08-05 20:39:29 -0700864 env->SetLongField(image, gSurfaceImageClassInfo.mTimestamp,
865 static_cast<jlong>(buffer->timestamp));
Zhijun He212e78d2013-06-07 11:36:23 -0700866
Igor Murashkine3351f12013-09-13 13:08:04 -0700867 return ACQUIRE_SUCCESS;
Zhijun He212e78d2013-06-07 11:36:23 -0700868}
869
870static jobject ImageReader_getSurface(JNIEnv* env, jobject thiz)
871{
872 ALOGV("%s: ", __FUNCTION__);
873
Dan Stoza5b3c7c12014-03-12 16:44:45 -0700874 IGraphicBufferProducer* gbp = ImageReader_getProducer(env, thiz);
875 if (gbp == NULL) {
Zhijun He212e78d2013-06-07 11:36:23 -0700876 jniThrowRuntimeException(env, "CpuConsumer is uninitialized");
877 return NULL;
878 }
879
880 // Wrap the IGBP in a Java-language Surface.
Dan Stoza5b3c7c12014-03-12 16:44:45 -0700881 return android_view_Surface_createFromIGraphicBufferProducer(env, gbp);
Zhijun He212e78d2013-06-07 11:36:23 -0700882}
883
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700884static jobject Image_createSurfacePlane(JNIEnv* env, jobject thiz, int idx, int readerFormat)
Zhijun He212e78d2013-06-07 11:36:23 -0700885{
886 int rowStride, pixelStride;
887 ALOGV("%s: buffer index: %d", __FUNCTION__, idx);
888
889 CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, thiz);
890
891 ALOG_ASSERT(buffer != NULL);
892 if (buffer == NULL) {
893 jniThrowException(env, "java/lang/IllegalStateException", "Image was released");
894 }
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700895
896 readerFormat = Image_getPixelFormat(env, readerFormat);
897
898 rowStride = Image_imageGetRowStride(env, buffer, idx, readerFormat);
899 pixelStride = Image_imageGetPixelStride(env, buffer, idx, readerFormat);
Zhijun He212e78d2013-06-07 11:36:23 -0700900
Jeff Brownef961212013-08-05 20:39:29 -0700901 jobject surfPlaneObj = env->NewObject(gSurfacePlaneClassInfo.clazz,
902 gSurfacePlaneClassInfo.ctor, thiz, idx, rowStride, pixelStride);
Zhijun He212e78d2013-06-07 11:36:23 -0700903
904 return surfPlaneObj;
905}
906
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700907static jobject Image_getByteBuffer(JNIEnv* env, jobject thiz, int idx, int readerFormat)
Zhijun He212e78d2013-06-07 11:36:23 -0700908{
909 uint8_t *base = NULL;
910 uint32_t size = 0;
911 jobject byteBuffer;
912
913 ALOGV("%s: buffer index: %d", __FUNCTION__, idx);
914
915 CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, thiz);
916
917 if (buffer == NULL) {
918 jniThrowException(env, "java/lang/IllegalStateException", "Image was released");
919 }
920
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700921 readerFormat = Image_getPixelFormat(env, readerFormat);
922
Zhijun He212e78d2013-06-07 11:36:23 -0700923 // Create byteBuffer from native buffer
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700924 Image_getLockedBufferInfo(env, buffer, idx, &base, &size, readerFormat);
Igor Murashkin5096def2014-06-24 10:49:11 -0700925
926 if (size > static_cast<uint32_t>(INT32_MAX)) {
927 // Byte buffer have 'int capacity', so check the range
928 jniThrowExceptionFmt(env, "java/lang/IllegalStateException",
Ruben Brunk31798f32014-09-25 19:56:54 -0700929 "Size too large for bytebuffer capacity %" PRIu32, size);
Igor Murashkin5096def2014-06-24 10:49:11 -0700930 return NULL;
931 }
932
Zhijun He212e78d2013-06-07 11:36:23 -0700933 byteBuffer = env->NewDirectByteBuffer(base, size);
934 // TODO: throw dvm exOutOfMemoryError?
935 if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) {
936 jniThrowException(env, "java/lang/IllegalStateException", "Failed to allocate ByteBuffer");
937 }
938
939 return byteBuffer;
940}
941
Ruben Brunkf4a637d2014-11-20 18:01:36 -0800942static jint Image_getWidth(JNIEnv* env, jobject thiz)
943{
944 CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, thiz);
945 return Image_getBufferWidth(buffer);
946}
947
948static jint Image_getHeight(JNIEnv* env, jobject thiz)
949{
950 CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, thiz);
951 return Image_getBufferHeight(buffer);
952}
953
954
Zhijun He212e78d2013-06-07 11:36:23 -0700955} // extern "C"
956
957// ----------------------------------------------------------------------------
958
Daniel Micay76f6a862015-09-19 17:31:01 -0400959static const JNINativeMethod gImageReaderMethods[] = {
Zhijun He212e78d2013-06-07 11:36:23 -0700960 {"nativeClassInit", "()V", (void*)ImageReader_classInit },
961 {"nativeInit", "(Ljava/lang/Object;IIII)V", (void*)ImageReader_init },
962 {"nativeClose", "()V", (void*)ImageReader_close },
963 {"nativeReleaseImage", "(Landroid/media/Image;)V", (void*)ImageReader_imageRelease },
Ruben Brunkf4a637d2014-11-20 18:01:36 -0800964 {"nativeImageSetup", "(Landroid/media/Image;)I", (void*)ImageReader_imageSetup },
Zhijun He212e78d2013-06-07 11:36:23 -0700965 {"nativeGetSurface", "()Landroid/view/Surface;", (void*)ImageReader_getSurface },
966};
967
Daniel Micay76f6a862015-09-19 17:31:01 -0400968static const JNINativeMethod gImageMethods[] = {
Ruben Brunk0fd198a2014-09-23 23:35:43 -0700969 {"nativeImageGetBuffer", "(II)Ljava/nio/ByteBuffer;", (void*)Image_getByteBuffer },
970 {"nativeCreatePlane", "(II)Landroid/media/ImageReader$SurfaceImage$SurfacePlane;",
Ruben Brunkf4a637d2014-11-20 18:01:36 -0800971 (void*)Image_createSurfacePlane },
972 {"nativeGetWidth", "()I", (void*)Image_getWidth },
973 {"nativeGetHeight", "()I", (void*)Image_getHeight },
Zhijun He212e78d2013-06-07 11:36:23 -0700974};
975
976int register_android_media_ImageReader(JNIEnv *env) {
977
978 int ret1 = AndroidRuntime::registerNativeMethods(env,
979 "android/media/ImageReader", gImageReaderMethods, NELEM(gImageReaderMethods));
980
981 int ret2 = AndroidRuntime::registerNativeMethods(env,
982 "android/media/ImageReader$SurfaceImage", gImageMethods, NELEM(gImageMethods));
983
984 return (ret1 || ret2);
985}