Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 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 <utils/Log.h> |
| 20 | #include <utils/misc.h> |
| 21 | #include <utils/List.h> |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 22 | #include <utils/String8.h> |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 23 | |
| 24 | #include <cstdio> |
| 25 | |
| 26 | #include <gui/CpuConsumer.h> |
| 27 | #include <gui/Surface.h> |
Zhijun He | 534046d | 2013-07-01 11:03:41 -0700 | [diff] [blame] | 28 | #include <camera3.h> |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 29 | |
| 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 Murashkin | 5096def | 2014-06-24 10:49:11 -0700 | [diff] [blame] | 36 | #include <stdint.h> |
| 37 | #include <inttypes.h> |
| 38 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 39 | #define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) ) |
| 40 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 41 | #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 | |
| 47 | using namespace android; |
| 48 | |
| 49 | enum { |
| 50 | IMAGE_READER_MAX_NUM_PLANES = 3, |
| 51 | }; |
| 52 | |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 53 | enum { |
| 54 | ACQUIRE_SUCCESS = 0, |
| 55 | ACQUIRE_NO_BUFFERS = 1, |
| 56 | ACQUIRE_MAX_IMAGES = 2, |
| 57 | }; |
| 58 | |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 59 | static struct { |
| 60 | jfieldID mNativeContext; |
| 61 | jmethodID postEventFromNative; |
| 62 | } gImageReaderClassInfo; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 63 | |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 64 | static struct { |
| 65 | jfieldID mLockedBuffer; |
| 66 | jfieldID mTimestamp; |
| 67 | } gSurfaceImageClassInfo; |
| 68 | |
| 69 | static struct { |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 70 | jclass clazz; |
| 71 | jmethodID ctor; |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 72 | } gSurfacePlaneClassInfo; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 73 | |
| 74 | // ---------------------------------------------------------------------------- |
| 75 | |
| 76 | class JNIImageReaderContext : public CpuConsumer::FrameAvailableListener |
| 77 | { |
| 78 | public: |
| 79 | JNIImageReaderContext(JNIEnv* env, jobject weakThiz, jclass clazz, int maxImages); |
| 80 | |
| 81 | virtual ~JNIImageReaderContext(); |
| 82 | |
| 83 | virtual void onFrameAvailable(); |
| 84 | |
| 85 | CpuConsumer::LockedBuffer* getLockedBuffer(); |
| 86 | |
| 87 | void returnLockedBuffer(CpuConsumer::LockedBuffer* buffer); |
| 88 | |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 89 | void setCpuConsumer(const sp<CpuConsumer>& consumer) { mConsumer = consumer; } |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 90 | CpuConsumer* getCpuConsumer() { return mConsumer.get(); } |
| 91 | |
Dan Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 92 | void setProducer(const sp<IGraphicBufferProducer>& producer) { mProducer = producer; } |
| 93 | IGraphicBufferProducer* getProducer() { return mProducer.get(); } |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 94 | |
| 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 | |
| 104 | private: |
| 105 | static JNIEnv* getJNIEnv(bool* needsDetach); |
| 106 | static void detachJNI(); |
| 107 | |
| 108 | List<CpuConsumer::LockedBuffer*> mBuffers; |
| 109 | sp<CpuConsumer> mConsumer; |
Dan Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 110 | sp<IGraphicBufferProducer> mProducer; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 111 | jobject mWeakThiz; |
| 112 | jclass mClazz; |
| 113 | int mFormat; |
| 114 | int mWidth; |
| 115 | int mHeight; |
| 116 | }; |
| 117 | |
| 118 | JNIImageReaderContext::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 | |
| 128 | JNIEnv* 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 | |
| 145 | void 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 | |
| 153 | CpuConsumer::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 Murashkin | d901c03 | 2013-08-27 15:19:55 -0700 | [diff] [blame] | 164 | void JNIImageReaderContext::returnLockedBuffer(CpuConsumer::LockedBuffer* buffer) { |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 165 | mBuffers.push_back(buffer); |
| 166 | } |
| 167 | |
| 168 | JNIImageReaderContext::~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 | |
| 190 | void JNIImageReaderContext::onFrameAvailable() |
| 191 | { |
| 192 | ALOGV("%s: frame available", __FUNCTION__); |
| 193 | bool needsDetach = false; |
| 194 | JNIEnv* env = getJNIEnv(&needsDetach); |
| 195 | if (env != NULL) { |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 196 | env->CallStaticVoidMethod(mClazz, gImageReaderClassInfo.postEventFromNative, mWeakThiz); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 197 | } else { |
| 198 | ALOGW("onFrameAvailable event will not posted"); |
| 199 | } |
| 200 | if (needsDetach) { |
| 201 | detachJNI(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // ---------------------------------------------------------------------------- |
| 206 | |
| 207 | extern "C" { |
| 208 | |
| 209 | static JNIImageReaderContext* ImageReader_getContext(JNIEnv* env, jobject thiz) |
| 210 | { |
| 211 | JNIImageReaderContext *ctx; |
| 212 | ctx = reinterpret_cast<JNIImageReaderContext *> |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 213 | (env->GetLongField(thiz, gImageReaderClassInfo.mNativeContext)); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 214 | return ctx; |
| 215 | } |
| 216 | |
| 217 | static 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 Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 228 | static IGraphicBufferProducer* ImageReader_getProducer(JNIEnv* env, jobject thiz) |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 229 | { |
| 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 Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 236 | return ctx->getProducer(); |
Mathias Agopian | 52a9a10 | 2013-08-02 01:38:38 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 239 | static 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 Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 250 | env->SetLongField(thiz, gImageReaderClassInfo.mNativeContext, |
| 251 | reinterpret_cast<jlong>(ctx.get())); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static CpuConsumer::LockedBuffer* Image_getLockedBuffer(JNIEnv* env, jobject image) |
| 255 | { |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 256 | return reinterpret_cast<CpuConsumer::LockedBuffer*>( |
| 257 | env->GetLongField(image, gSurfaceImageClassInfo.mLockedBuffer)); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static void Image_setBuffer(JNIEnv* env, jobject thiz, |
| 261 | const CpuConsumer::LockedBuffer* buffer) |
| 262 | { |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 263 | env->SetLongField(thiz, gSurfaceImageClassInfo.mLockedBuffer, reinterpret_cast<jlong>(buffer)); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 264 | } |
| 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. |
| 268 | static int Image_getPixelFormat(JNIEnv* env, int format) |
| 269 | { |
Zhijun He | d1988a9 | 2014-07-01 04:15:09 -0700 | [diff] [blame] | 270 | int jpegFormat; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 271 | 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 He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 280 | |
Zhijun He | a5e1528 | 2014-04-24 14:08:51 -0700 | [diff] [blame] | 281 | // Translate the JPEG to BLOB for camera purpose. |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 282 | if (format == jpegFormat) { |
| 283 | format = HAL_PIXEL_FORMAT_BLOB; |
| 284 | } |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 285 | |
| 286 | return format; |
| 287 | } |
| 288 | |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame^] | 289 | static uint32_t Image_getJpegSize(CpuConsumer::LockedBuffer* buffer, bool usingRGBAOverride) |
Zhijun He | 534046d | 2013-07-01 11:03:41 -0700 | [diff] [blame] | 290 | { |
| 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 Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame^] | 296 | if (usingRGBAOverride) { |
| 297 | width *= 4; |
| 298 | } |
| 299 | |
Zhijun He | 534046d | 2013-07-01 11:03:41 -0700 | [diff] [blame] | 300 | // 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 Murashkin | 5096def | 2014-06-24 10:49:11 -0700 | [diff] [blame] | 310 | /* |
| 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 He | 534046d | 2013-07-01 11:03:41 -0700 | [diff] [blame] | 318 | size = width; |
| 319 | } |
| 320 | |
| 321 | return size; |
| 322 | } |
| 323 | |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame^] | 324 | static bool usingRGBAToJpegOverride(int32_t bufferFormat, int32_t readerCtxFormat) { |
| 325 | return readerCtxFormat == HAL_PIXEL_FORMAT_BLOB && bufferFormat == HAL_PIXEL_FORMAT_RGBA_8888; |
| 326 | } |
| 327 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 328 | static 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 Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame^] | 332 | if (usingRGBAToJpegOverride(bufferFormat, readerCtxFormat)) { |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 333 | return HAL_PIXEL_FORMAT_BLOB; |
| 334 | } |
| 335 | return bufferFormat; |
| 336 | } |
| 337 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 338 | static void Image_getLockedBufferInfo(JNIEnv* env, CpuConsumer::LockedBuffer* buffer, int idx, |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 339 | uint8_t **base, uint32_t *size, int32_t readerFormat) |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 340 | { |
| 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 He | 7f4d314 | 2013-07-23 07:54:38 -0700 | [diff] [blame] | 346 | ALOGV("%s: buffer: %p", __FUNCTION__, buffer); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 347 | |
| 348 | uint32_t dataSize, ySize, cSize, cStride; |
| 349 | uint8_t *cb, *cr; |
| 350 | uint8_t *pData = NULL; |
Zhijun He | 708e359 | 2013-08-05 14:56:11 -0700 | [diff] [blame] | 351 | int bytesPerPixel = 0; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 352 | |
| 353 | dataSize = ySize = cSize = cStride = 0; |
| 354 | int32_t fmt = buffer->format; |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 355 | |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame^] | 356 | bool usingRGBAOverride = usingRGBAToJpegOverride(fmt, readerFormat); |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 357 | fmt = applyFormatOverrides(fmt, readerFormat); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 358 | 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; |
| 366 | if (idx == 0) { |
| 367 | dataSize = buffer->stride * buffer->height; |
| 368 | } else { |
| 369 | dataSize = buffer->chromaStride * buffer->height / 2; |
| 370 | } |
| 371 | break; |
| 372 | // NV21 |
| 373 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 374 | cr = buffer->data + (buffer->stride * buffer->height); |
| 375 | cb = cr + 1; |
| 376 | ySize = buffer->width * buffer->height; |
| 377 | cSize = buffer->width * buffer->height / 2; |
| 378 | |
| 379 | pData = |
| 380 | (idx == 0) ? |
| 381 | buffer->data : |
| 382 | (idx == 1) ? |
| 383 | cb: |
| 384 | cr; |
| 385 | |
| 386 | dataSize = (idx == 0) ? ySize : cSize; |
| 387 | break; |
| 388 | case HAL_PIXEL_FORMAT_YV12: |
| 389 | // Y and C stride need to be 16 pixel aligned. |
| 390 | LOG_ALWAYS_FATAL_IF(buffer->stride % 16, |
| 391 | "Stride is not 16 pixel aligned %d", buffer->stride); |
| 392 | |
| 393 | ySize = buffer->stride * buffer->height; |
| 394 | cStride = ALIGN(buffer->stride / 2, 16); |
| 395 | cr = buffer->data + ySize; |
| 396 | cSize = cStride * buffer->height / 2; |
| 397 | cb = cr + cSize; |
| 398 | |
| 399 | pData = |
| 400 | (idx == 0) ? |
| 401 | buffer->data : |
| 402 | (idx == 1) ? |
| 403 | cb : |
| 404 | cr; |
| 405 | dataSize = (idx == 0) ? ySize : cSize; |
| 406 | break; |
| 407 | case HAL_PIXEL_FORMAT_Y8: |
| 408 | // Single plane, 8bpp. |
| 409 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 410 | |
| 411 | pData = buffer->data; |
| 412 | dataSize = buffer->stride * buffer->height; |
| 413 | break; |
| 414 | case HAL_PIXEL_FORMAT_Y16: |
Zhijun He | 977ad8d | 2014-04-08 16:59:29 -0700 | [diff] [blame] | 415 | bytesPerPixel = 2; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 416 | // Single plane, 16bpp, strides are specified in pixels, not in bytes |
| 417 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 418 | |
| 419 | pData = buffer->data; |
Zhijun He | 977ad8d | 2014-04-08 16:59:29 -0700 | [diff] [blame] | 420 | dataSize = buffer->stride * buffer->height * bytesPerPixel; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 421 | break; |
| 422 | case HAL_PIXEL_FORMAT_BLOB: |
| 423 | // Used for JPEG data, height must be 1, width == size, single plane. |
| 424 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 425 | ALOG_ASSERT(buffer->height == 1, "JPEG should has height value %d", buffer->height); |
| 426 | |
| 427 | pData = buffer->data; |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame^] | 428 | dataSize = Image_getJpegSize(buffer, usingRGBAOverride); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 429 | break; |
| 430 | case HAL_PIXEL_FORMAT_RAW_SENSOR: |
| 431 | // Single plane 16bpp bayer data. |
Zhijun He | 977ad8d | 2014-04-08 16:59:29 -0700 | [diff] [blame] | 432 | bytesPerPixel = 2; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 433 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 434 | pData = buffer->data; |
Zhijun He | 977ad8d | 2014-04-08 16:59:29 -0700 | [diff] [blame] | 435 | dataSize = buffer->stride * buffer->height * bytesPerPixel; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 436 | break; |
Zhijun He | d1988a9 | 2014-07-01 04:15:09 -0700 | [diff] [blame] | 437 | case HAL_PIXEL_FORMAT_RAW10: |
| 438 | // Single plane 10bpp bayer data. |
| 439 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 440 | LOG_ALWAYS_FATAL_IF(buffer->width % 4, |
| 441 | "Width is not multiple of 4 %d", buffer->width); |
| 442 | LOG_ALWAYS_FATAL_IF(buffer->height % 2, |
| 443 | "Height is not even %d", buffer->height); |
Zhijun He | 4c4064f | 2014-07-25 09:06:11 -0700 | [diff] [blame] | 444 | LOG_ALWAYS_FATAL_IF(buffer->stride < (buffer->width * 10 / 8), |
| 445 | "stride (%d) should be at least %d", |
| 446 | buffer->stride, buffer->width * 10 / 8); |
Zhijun He | d1988a9 | 2014-07-01 04:15:09 -0700 | [diff] [blame] | 447 | pData = buffer->data; |
Zhijun He | 4c4064f | 2014-07-25 09:06:11 -0700 | [diff] [blame] | 448 | dataSize = buffer->stride * buffer->height; |
Zhijun He | d1988a9 | 2014-07-01 04:15:09 -0700 | [diff] [blame] | 449 | break; |
Zhijun He | 708e359 | 2013-08-05 14:56:11 -0700 | [diff] [blame] | 450 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 451 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 452 | // Single plane, 32bpp. |
| 453 | bytesPerPixel = 4; |
| 454 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 455 | pData = buffer->data; |
| 456 | dataSize = buffer->stride * buffer->height * bytesPerPixel; |
| 457 | break; |
| 458 | case HAL_PIXEL_FORMAT_RGB_565: |
| 459 | // Single plane, 16bpp. |
| 460 | bytesPerPixel = 2; |
| 461 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 462 | pData = buffer->data; |
| 463 | dataSize = buffer->stride * buffer->height * bytesPerPixel; |
| 464 | break; |
| 465 | case HAL_PIXEL_FORMAT_RGB_888: |
| 466 | // Single plane, 24bpp. |
| 467 | bytesPerPixel = 3; |
| 468 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 469 | pData = buffer->data; |
| 470 | dataSize = buffer->stride * buffer->height * bytesPerPixel; |
| 471 | break; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 472 | default: |
| 473 | jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException", |
| 474 | "Pixel format: 0x%x is unsupported", fmt); |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | *base = pData; |
| 479 | *size = dataSize; |
| 480 | } |
| 481 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 482 | static jint Image_imageGetPixelStride(JNIEnv* env, CpuConsumer::LockedBuffer* buffer, int idx, |
| 483 | int32_t readerFormat) |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 484 | { |
| 485 | ALOGV("%s: buffer index: %d", __FUNCTION__, idx); |
| 486 | ALOG_ASSERT((idx < IMAGE_READER_MAX_NUM_PLANES) && (idx >= 0), "Index is out of range:%d", idx); |
| 487 | |
| 488 | int pixelStride = 0; |
| 489 | ALOG_ASSERT(buffer != NULL, "buffer is NULL"); |
| 490 | |
| 491 | int32_t fmt = buffer->format; |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 492 | |
| 493 | fmt = applyFormatOverrides(fmt, readerFormat); |
| 494 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 495 | switch (fmt) { |
| 496 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
| 497 | pixelStride = (idx == 0) ? 1 : buffer->chromaStep; |
| 498 | break; |
| 499 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 500 | pixelStride = (idx == 0) ? 1 : 2; |
| 501 | break; |
| 502 | case HAL_PIXEL_FORMAT_Y8: |
| 503 | // Single plane 8bpp data. |
| 504 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 505 | pixelStride; |
| 506 | break; |
| 507 | case HAL_PIXEL_FORMAT_YV12: |
| 508 | pixelStride = 1; |
| 509 | break; |
| 510 | case HAL_PIXEL_FORMAT_BLOB: |
Zhijun He | d1988a9 | 2014-07-01 04:15:09 -0700 | [diff] [blame] | 511 | case HAL_PIXEL_FORMAT_RAW10: |
| 512 | // Blob is used for JPEG data, RAW10 is used for 10-bit raw data, they are |
| 513 | // single plane, row and pixel strides are 0. |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 514 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 515 | pixelStride = 0; |
| 516 | break; |
| 517 | case HAL_PIXEL_FORMAT_Y16: |
| 518 | case HAL_PIXEL_FORMAT_RAW_SENSOR: |
Zhijun He | 708e359 | 2013-08-05 14:56:11 -0700 | [diff] [blame] | 519 | case HAL_PIXEL_FORMAT_RGB_565: |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 520 | // Single plane 16bpp data. |
| 521 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 522 | pixelStride = 2; |
| 523 | break; |
Zhijun He | 708e359 | 2013-08-05 14:56:11 -0700 | [diff] [blame] | 524 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 525 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 526 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 527 | pixelStride = 4; |
| 528 | break; |
| 529 | case HAL_PIXEL_FORMAT_RGB_888: |
| 530 | // Single plane, 24bpp. |
| 531 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 532 | pixelStride = 3; |
| 533 | break; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 534 | default: |
| 535 | jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException", |
| 536 | "Pixel format: 0x%x is unsupported", fmt); |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | return pixelStride; |
| 541 | } |
| 542 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 543 | static jint Image_imageGetRowStride(JNIEnv* env, CpuConsumer::LockedBuffer* buffer, int idx, |
| 544 | int32_t readerFormat) |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 545 | { |
| 546 | ALOGV("%s: buffer index: %d", __FUNCTION__, idx); |
| 547 | ALOG_ASSERT((idx < IMAGE_READER_MAX_NUM_PLANES) && (idx >= 0)); |
| 548 | |
| 549 | int rowStride = 0; |
| 550 | ALOG_ASSERT(buffer != NULL, "buffer is NULL"); |
| 551 | |
| 552 | int32_t fmt = buffer->format; |
| 553 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 554 | fmt = applyFormatOverrides(fmt, readerFormat); |
| 555 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 556 | switch (fmt) { |
| 557 | case HAL_PIXEL_FORMAT_YCbCr_420_888: |
| 558 | rowStride = (idx == 0) ? buffer->stride : buffer->chromaStride; |
| 559 | break; |
| 560 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 561 | rowStride = buffer->width; |
| 562 | break; |
| 563 | case HAL_PIXEL_FORMAT_YV12: |
| 564 | LOG_ALWAYS_FATAL_IF(buffer->stride % 16, |
| 565 | "Stride is not 16 pixel aligned %d", buffer->stride); |
| 566 | rowStride = (idx == 0) ? buffer->stride : ALIGN(buffer->stride / 2, 16); |
| 567 | break; |
| 568 | case HAL_PIXEL_FORMAT_BLOB: |
Zhijun He | d1988a9 | 2014-07-01 04:15:09 -0700 | [diff] [blame] | 569 | // Blob is used for JPEG data, RAW10 is used for 10-bit raw data, they are |
| 570 | // single plane, row and pixel strides are 0. |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 571 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 572 | rowStride = 0; |
| 573 | break; |
Zhijun He | 4c4064f | 2014-07-25 09:06:11 -0700 | [diff] [blame] | 574 | case HAL_PIXEL_FORMAT_RAW10: |
| 575 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 576 | rowStride = buffer->stride; |
| 577 | break; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 578 | case HAL_PIXEL_FORMAT_Y8: |
| 579 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 580 | LOG_ALWAYS_FATAL_IF(buffer->stride % 16, |
| 581 | "Stride is not 16 pixel aligned %d", buffer->stride); |
| 582 | rowStride = buffer->stride; |
| 583 | break; |
| 584 | case HAL_PIXEL_FORMAT_Y16: |
| 585 | case HAL_PIXEL_FORMAT_RAW_SENSOR: |
| 586 | // In native side, strides are specified in pixels, not in bytes. |
| 587 | // Single plane 16bpp bayer data. even width/height, |
| 588 | // row stride multiple of 16 pixels (32 bytes) |
| 589 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 590 | LOG_ALWAYS_FATAL_IF(buffer->stride % 16, |
| 591 | "Stride is not 16 pixel aligned %d", buffer->stride); |
| 592 | rowStride = buffer->stride * 2; |
| 593 | break; |
Zhijun He | 708e359 | 2013-08-05 14:56:11 -0700 | [diff] [blame] | 594 | case HAL_PIXEL_FORMAT_RGB_565: |
| 595 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 596 | rowStride = buffer->stride * 2; |
| 597 | break; |
| 598 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 599 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 600 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 601 | rowStride = buffer->stride * 4; |
| 602 | break; |
| 603 | case HAL_PIXEL_FORMAT_RGB_888: |
| 604 | // Single plane, 24bpp. |
| 605 | ALOG_ASSERT(idx == 0, "Wrong index: %d", idx); |
| 606 | rowStride = buffer->stride * 3; |
| 607 | break; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 608 | default: |
| 609 | ALOGE("%s Pixel format: 0x%x is unsupported", __FUNCTION__, fmt); |
| 610 | jniThrowException(env, "java/lang/UnsupportedOperationException", |
| 611 | "unsupported buffer format"); |
| 612 | break; |
| 613 | } |
| 614 | |
| 615 | return rowStride; |
| 616 | } |
| 617 | |
| 618 | // ---------------------------------------------------------------------------- |
| 619 | |
| 620 | static void ImageReader_classInit(JNIEnv* env, jclass clazz) |
| 621 | { |
| 622 | ALOGV("%s:", __FUNCTION__); |
| 623 | |
| 624 | jclass imageClazz = env->FindClass("android/media/ImageReader$SurfaceImage"); |
| 625 | LOG_ALWAYS_FATAL_IF(imageClazz == NULL, |
| 626 | "can't find android/graphics/ImageReader$SurfaceImage"); |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 627 | gSurfaceImageClassInfo.mLockedBuffer = env->GetFieldID( |
| 628 | imageClazz, ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID, "J"); |
| 629 | LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mLockedBuffer == NULL, |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 630 | "can't find android/graphics/ImageReader.%s", |
| 631 | ANDROID_MEDIA_SURFACEIMAGE_BUFFER_JNI_ID); |
| 632 | |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 633 | gSurfaceImageClassInfo.mTimestamp = env->GetFieldID( |
| 634 | imageClazz, ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID, "J"); |
| 635 | LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mTimestamp == NULL, |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 636 | "can't find android/graphics/ImageReader.%s", |
| 637 | ANDROID_MEDIA_SURFACEIMAGE_TS_JNI_ID); |
| 638 | |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 639 | gImageReaderClassInfo.mNativeContext = env->GetFieldID( |
| 640 | clazz, ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID, "J"); |
| 641 | LOG_ALWAYS_FATAL_IF(gImageReaderClassInfo.mNativeContext == NULL, |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 642 | "can't find android/graphics/ImageReader.%s", |
| 643 | ANDROID_MEDIA_IMAGEREADER_CTX_JNI_ID); |
| 644 | |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 645 | gImageReaderClassInfo.postEventFromNative = env->GetStaticMethodID( |
| 646 | clazz, "postEventFromNative", "(Ljava/lang/Object;)V"); |
| 647 | LOG_ALWAYS_FATAL_IF(gImageReaderClassInfo.postEventFromNative == NULL, |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 648 | "can't find android/graphics/ImageReader.postEventFromNative"); |
| 649 | |
| 650 | jclass planeClazz = env->FindClass("android/media/ImageReader$SurfaceImage$SurfacePlane"); |
| 651 | LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find SurfacePlane class"); |
| 652 | // FindClass only gives a local reference of jclass object. |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 653 | gSurfacePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz); |
| 654 | gSurfacePlaneClassInfo.ctor = env->GetMethodID(gSurfacePlaneClassInfo.clazz, "<init>", |
| 655 | "(Landroid/media/ImageReader$SurfaceImage;III)V"); |
| 656 | LOG_ALWAYS_FATAL_IF(gSurfacePlaneClassInfo.ctor == NULL, |
| 657 | "Can not find SurfacePlane constructor"); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static void ImageReader_init(JNIEnv* env, jobject thiz, jobject weakThiz, |
| 661 | jint width, jint height, jint format, jint maxImages) |
| 662 | { |
| 663 | status_t res; |
| 664 | int nativeFormat; |
| 665 | |
| 666 | ALOGV("%s: width:%d, height: %d, format: 0x%x, maxImages:%d", |
| 667 | __FUNCTION__, width, height, format, maxImages); |
| 668 | |
| 669 | nativeFormat = Image_getPixelFormat(env, format); |
| 670 | |
Dan Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 671 | sp<IGraphicBufferProducer> gbProducer; |
| 672 | sp<IGraphicBufferConsumer> gbConsumer; |
| 673 | BufferQueue::createBufferQueue(&gbProducer, &gbConsumer); |
| 674 | sp<CpuConsumer> consumer = new CpuConsumer(gbConsumer, maxImages, |
Igor Murashkin | dd06432 | 2013-08-14 19:05:17 -0700 | [diff] [blame] | 675 | /*controlledByApp*/true); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 676 | // TODO: throw dvm exOutOfMemoryError? |
| 677 | if (consumer == NULL) { |
| 678 | jniThrowRuntimeException(env, "Failed to allocate native CpuConsumer"); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | jclass clazz = env->GetObjectClass(thiz); |
| 683 | if (clazz == NULL) { |
| 684 | jniThrowRuntimeException(env, "Can't find android/graphics/ImageReader"); |
| 685 | return; |
| 686 | } |
| 687 | sp<JNIImageReaderContext> ctx(new JNIImageReaderContext(env, weakThiz, clazz, maxImages)); |
| 688 | ctx->setCpuConsumer(consumer); |
Dan Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 689 | ctx->setProducer(gbProducer); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 690 | consumer->setFrameAvailableListener(ctx); |
| 691 | ImageReader_setNativeContext(env, thiz, ctx); |
| 692 | ctx->setBufferFormat(nativeFormat); |
| 693 | ctx->setBufferWidth(width); |
| 694 | ctx->setBufferHeight(height); |
| 695 | |
| 696 | // Set the width/height/format to the CpuConsumer |
| 697 | res = consumer->setDefaultBufferSize(width, height); |
| 698 | if (res != OK) { |
| 699 | jniThrowException(env, "java/lang/IllegalStateException", |
| 700 | "Failed to set CpuConsumer buffer size"); |
| 701 | return; |
| 702 | } |
| 703 | res = consumer->setDefaultBufferFormat(nativeFormat); |
| 704 | if (res != OK) { |
| 705 | jniThrowException(env, "java/lang/IllegalStateException", |
| 706 | "Failed to set CpuConsumer buffer format"); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | static void ImageReader_close(JNIEnv* env, jobject thiz) |
| 711 | { |
| 712 | ALOGV("%s:", __FUNCTION__); |
| 713 | |
| 714 | JNIImageReaderContext* const ctx = ImageReader_getContext(env, thiz); |
| 715 | if (ctx == NULL) { |
| 716 | // ImageReader is already closed. |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | CpuConsumer* consumer = ImageReader_getCpuConsumer(env, thiz); |
| 721 | if (consumer != NULL) { |
| 722 | consumer->abandon(); |
| 723 | consumer->setFrameAvailableListener(NULL); |
| 724 | } |
| 725 | ImageReader_setNativeContext(env, thiz, NULL); |
| 726 | } |
| 727 | |
| 728 | static void ImageReader_imageRelease(JNIEnv* env, jobject thiz, jobject image) |
| 729 | { |
| 730 | ALOGV("%s:", __FUNCTION__); |
| 731 | JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz); |
| 732 | if (ctx == NULL) { |
| 733 | ALOGW("ImageReader#close called before Image#close, consider calling Image#close first"); |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | CpuConsumer* consumer = ctx->getCpuConsumer(); |
| 738 | CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, image); |
| 739 | if (!buffer) { |
| 740 | ALOGW("Image already released!!!"); |
| 741 | return; |
| 742 | } |
| 743 | consumer->unlockBuffer(*buffer); |
| 744 | Image_setBuffer(env, image, NULL); |
| 745 | ctx->returnLockedBuffer(buffer); |
| 746 | } |
| 747 | |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 748 | static jint ImageReader_imageSetup(JNIEnv* env, jobject thiz, |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 749 | jobject image) |
| 750 | { |
| 751 | ALOGV("%s:", __FUNCTION__); |
| 752 | JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz); |
| 753 | if (ctx == NULL) { |
| 754 | jniThrowRuntimeException(env, "ImageReaderContext is not initialized"); |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 755 | return -1; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | CpuConsumer* consumer = ctx->getCpuConsumer(); |
| 759 | CpuConsumer::LockedBuffer* buffer = ctx->getLockedBuffer(); |
| 760 | if (buffer == NULL) { |
Igor Murashkin | d901c03 | 2013-08-27 15:19:55 -0700 | [diff] [blame] | 761 | ALOGW("Unable to acquire a lockedBuffer, very likely client tries to lock more than" |
| 762 | " maxImages buffers"); |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 763 | return ACQUIRE_MAX_IMAGES; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 764 | } |
| 765 | status_t res = consumer->lockNextBuffer(buffer); |
| 766 | if (res != NO_ERROR) { |
lina.x.pi | 3347789 | 2014-01-27 10:31:44 +0800 | [diff] [blame] | 767 | ctx->returnLockedBuffer(buffer); |
Jeff Brown | f724c27 | 2013-08-07 14:17:04 -0700 | [diff] [blame] | 768 | if (res != BAD_VALUE /*no buffers*/) { |
Igor Murashkin | dd06432 | 2013-08-14 19:05:17 -0700 | [diff] [blame] | 769 | if (res == NOT_ENOUGH_DATA) { |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 770 | return ACQUIRE_MAX_IMAGES; |
Igor Murashkin | dd06432 | 2013-08-14 19:05:17 -0700 | [diff] [blame] | 771 | } else { |
| 772 | ALOGE("%s Fail to lockNextBuffer with error: %d ", |
| 773 | __FUNCTION__, res); |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 774 | jniThrowExceptionFmt(env, "java/lang/AssertionError", |
Igor Murashkin | dd06432 | 2013-08-14 19:05:17 -0700 | [diff] [blame] | 775 | "Unknown error (%d) when we tried to lock buffer.", |
| 776 | res); |
| 777 | } |
Jeff Brown | f724c27 | 2013-08-07 14:17:04 -0700 | [diff] [blame] | 778 | } |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 779 | return ACQUIRE_NO_BUFFERS; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Zhijun He | 4eda9f5 | 2013-09-18 08:00:02 -0700 | [diff] [blame] | 782 | if (buffer->format == HAL_PIXEL_FORMAT_YCrCb_420_SP) { |
| 783 | jniThrowException(env, "java/lang/UnsupportedOperationException", |
| 784 | "NV21 format is not supported by ImageReader"); |
| 785 | return -1; |
| 786 | } |
| 787 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 788 | // Check if the left-top corner of the crop rect is origin, we currently assume this point is |
| 789 | // zero, will revist this once this assumption turns out problematic. |
| 790 | Point lt = buffer->crop.leftTop(); |
| 791 | if (lt.x != 0 || lt.y != 0) { |
Zhijun He | 4eda9f5 | 2013-09-18 08:00:02 -0700 | [diff] [blame] | 792 | jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException", |
| 793 | "crop left top corner [%d, %d] need to be at origin", lt.x, lt.y); |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 794 | return -1; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | // Check if the producer buffer configurations match what ImageReader configured. |
| 798 | // We want to fail for the very first image because this case is too bad. |
Zhijun He | 534046d | 2013-07-01 11:03:41 -0700 | [diff] [blame] | 799 | int outputWidth = buffer->width; |
| 800 | int outputHeight = buffer->height; |
| 801 | |
Zhijun He | 3768213 | 2013-09-10 17:50:34 -0700 | [diff] [blame] | 802 | // Correct width/height when crop is set. |
Zhijun He | 9e6d073 | 2013-09-16 16:03:36 -0700 | [diff] [blame] | 803 | if (!buffer->crop.isEmpty()) { |
Zhijun He | cfa5533 | 2013-09-16 09:49:28 -0700 | [diff] [blame] | 804 | outputWidth = buffer->crop.getWidth(); |
| 805 | outputHeight = buffer->crop.getHeight(); |
Zhijun He | 534046d | 2013-07-01 11:03:41 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 808 | int imgReaderFmt = ctx->getBufferFormat(); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 809 | int imageReaderWidth = ctx->getBufferWidth(); |
| 810 | int imageReaderHeight = ctx->getBufferHeight(); |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 811 | if ((buffer->format != HAL_PIXEL_FORMAT_BLOB) && (imgReaderFmt != HAL_PIXEL_FORMAT_BLOB) && |
Zhijun He | 3768213 | 2013-09-10 17:50:34 -0700 | [diff] [blame] | 812 | (imageReaderWidth != outputWidth || imageReaderHeight > outputHeight)) { |
| 813 | /** |
| 814 | * For video decoder, the buffer height is actually the vertical stride, |
| 815 | * which is always >= actual image height. For future, decoder need provide |
| 816 | * right crop rectangle to CpuConsumer to indicate the actual image height, |
| 817 | * see bug 9563986. After this bug is fixed, we can enforce the height equal |
| 818 | * check. Right now, only make sure buffer height is no less than ImageReader |
| 819 | * height. |
| 820 | */ |
| 821 | jniThrowExceptionFmt(env, "java/lang/IllegalStateException", |
| 822 | "Producer buffer size: %dx%d, doesn't match ImageReader configured size: %dx%d", |
| 823 | outputWidth, outputHeight, imageReaderWidth, imageReaderHeight); |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 824 | return -1; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 827 | int bufFmt = buffer->format; |
| 828 | if (imgReaderFmt != bufFmt) { |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 829 | |
Ruben Brunk | 91b9aab | 2014-06-20 00:24:56 -0700 | [diff] [blame] | 830 | if (imgReaderFmt == HAL_PIXEL_FORMAT_YCbCr_420_888 && (bufFmt == |
| 831 | HAL_PIXEL_FORMAT_YCrCb_420_SP || bufFmt == HAL_PIXEL_FORMAT_YV12)) { |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 832 | // Special casing for when producer switches to a format compatible with flexible YUV |
| 833 | // (HAL_PIXEL_FORMAT_YCbCr_420_888). |
Ruben Brunk | 91b9aab | 2014-06-20 00:24:56 -0700 | [diff] [blame] | 834 | ctx->setBufferFormat(bufFmt); |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 835 | ALOGD("%s: Overriding buffer format YUV_420_888 to %x.", __FUNCTION__, bufFmt); |
| 836 | } else if (imgReaderFmt == HAL_PIXEL_FORMAT_BLOB && bufFmt == HAL_PIXEL_FORMAT_RGBA_8888) { |
| 837 | // Using HAL_PIXEL_FORMAT_RGBA_8888 gralloc buffers containing JPEGs to get around SW |
| 838 | // write limitations for (b/17379185). |
| 839 | ALOGD("%s: Receiving JPEG in HAL_PIXEL_FORMAT_RGBA_8888 buffer.", __FUNCTION__); |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 840 | } else { |
| 841 | // Return the buffer to the queue. |
| 842 | consumer->unlockBuffer(*buffer); |
| 843 | ctx->returnLockedBuffer(buffer); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 844 | |
Ruben Brunk | feb50af | 2014-05-09 19:58:49 -0700 | [diff] [blame] | 845 | // Throw exception |
| 846 | ALOGE("Producer output buffer format: 0x%x, ImageReader configured format: 0x%x", |
| 847 | buffer->format, ctx->getBufferFormat()); |
| 848 | String8 msg; |
| 849 | msg.appendFormat("The producer output buffer format 0x%x doesn't " |
| 850 | "match the ImageReader's configured buffer format 0x%x.", |
| 851 | buffer->format, ctx->getBufferFormat()); |
| 852 | jniThrowException(env, "java/lang/UnsupportedOperationException", |
| 853 | msg.string()); |
| 854 | return -1; |
| 855 | } |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 856 | } |
| 857 | // Set SurfaceImage instance member variables |
| 858 | Image_setBuffer(env, image, buffer); |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 859 | env->SetLongField(image, gSurfaceImageClassInfo.mTimestamp, |
| 860 | static_cast<jlong>(buffer->timestamp)); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 861 | |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 862 | return ACQUIRE_SUCCESS; |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static jobject ImageReader_getSurface(JNIEnv* env, jobject thiz) |
| 866 | { |
| 867 | ALOGV("%s: ", __FUNCTION__); |
| 868 | |
Dan Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 869 | IGraphicBufferProducer* gbp = ImageReader_getProducer(env, thiz); |
| 870 | if (gbp == NULL) { |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 871 | jniThrowRuntimeException(env, "CpuConsumer is uninitialized"); |
| 872 | return NULL; |
| 873 | } |
| 874 | |
| 875 | // Wrap the IGBP in a Java-language Surface. |
Dan Stoza | 5b3c7c1 | 2014-03-12 16:44:45 -0700 | [diff] [blame] | 876 | return android_view_Surface_createFromIGraphicBufferProducer(env, gbp); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 879 | static jobject Image_createSurfacePlane(JNIEnv* env, jobject thiz, int idx, int readerFormat) |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 880 | { |
| 881 | int rowStride, pixelStride; |
| 882 | ALOGV("%s: buffer index: %d", __FUNCTION__, idx); |
| 883 | |
| 884 | CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, thiz); |
| 885 | |
| 886 | ALOG_ASSERT(buffer != NULL); |
| 887 | if (buffer == NULL) { |
| 888 | jniThrowException(env, "java/lang/IllegalStateException", "Image was released"); |
| 889 | } |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 890 | |
| 891 | readerFormat = Image_getPixelFormat(env, readerFormat); |
| 892 | |
| 893 | rowStride = Image_imageGetRowStride(env, buffer, idx, readerFormat); |
| 894 | pixelStride = Image_imageGetPixelStride(env, buffer, idx, readerFormat); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 895 | |
Jeff Brown | ef96121 | 2013-08-05 20:39:29 -0700 | [diff] [blame] | 896 | jobject surfPlaneObj = env->NewObject(gSurfacePlaneClassInfo.clazz, |
| 897 | gSurfacePlaneClassInfo.ctor, thiz, idx, rowStride, pixelStride); |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 898 | |
| 899 | return surfPlaneObj; |
| 900 | } |
| 901 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 902 | static jobject Image_getByteBuffer(JNIEnv* env, jobject thiz, int idx, int readerFormat) |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 903 | { |
| 904 | uint8_t *base = NULL; |
| 905 | uint32_t size = 0; |
| 906 | jobject byteBuffer; |
| 907 | |
| 908 | ALOGV("%s: buffer index: %d", __FUNCTION__, idx); |
| 909 | |
| 910 | CpuConsumer::LockedBuffer* buffer = Image_getLockedBuffer(env, thiz); |
| 911 | |
| 912 | if (buffer == NULL) { |
| 913 | jniThrowException(env, "java/lang/IllegalStateException", "Image was released"); |
| 914 | } |
| 915 | |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 916 | readerFormat = Image_getPixelFormat(env, readerFormat); |
| 917 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 918 | // Create byteBuffer from native buffer |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 919 | Image_getLockedBufferInfo(env, buffer, idx, &base, &size, readerFormat); |
Igor Murashkin | 5096def | 2014-06-24 10:49:11 -0700 | [diff] [blame] | 920 | |
| 921 | if (size > static_cast<uint32_t>(INT32_MAX)) { |
| 922 | // Byte buffer have 'int capacity', so check the range |
| 923 | jniThrowExceptionFmt(env, "java/lang/IllegalStateException", |
Ruben Brunk | 31798f3 | 2014-09-25 19:56:54 -0700 | [diff] [blame^] | 924 | "Size too large for bytebuffer capacity %" PRIu32, size); |
Igor Murashkin | 5096def | 2014-06-24 10:49:11 -0700 | [diff] [blame] | 925 | return NULL; |
| 926 | } |
| 927 | |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 928 | byteBuffer = env->NewDirectByteBuffer(base, size); |
| 929 | // TODO: throw dvm exOutOfMemoryError? |
| 930 | if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) { |
| 931 | jniThrowException(env, "java/lang/IllegalStateException", "Failed to allocate ByteBuffer"); |
| 932 | } |
| 933 | |
| 934 | return byteBuffer; |
| 935 | } |
| 936 | |
| 937 | } // extern "C" |
| 938 | |
| 939 | // ---------------------------------------------------------------------------- |
| 940 | |
| 941 | static JNINativeMethod gImageReaderMethods[] = { |
| 942 | {"nativeClassInit", "()V", (void*)ImageReader_classInit }, |
| 943 | {"nativeInit", "(Ljava/lang/Object;IIII)V", (void*)ImageReader_init }, |
| 944 | {"nativeClose", "()V", (void*)ImageReader_close }, |
| 945 | {"nativeReleaseImage", "(Landroid/media/Image;)V", (void*)ImageReader_imageRelease }, |
Igor Murashkin | e3351f1 | 2013-09-13 13:08:04 -0700 | [diff] [blame] | 946 | {"nativeImageSetup", "(Landroid/media/Image;)I", (void*)ImageReader_imageSetup }, |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 947 | {"nativeGetSurface", "()Landroid/view/Surface;", (void*)ImageReader_getSurface }, |
| 948 | }; |
| 949 | |
| 950 | static JNINativeMethod gImageMethods[] = { |
Ruben Brunk | 0fd198a | 2014-09-23 23:35:43 -0700 | [diff] [blame] | 951 | {"nativeImageGetBuffer", "(II)Ljava/nio/ByteBuffer;", (void*)Image_getByteBuffer }, |
| 952 | {"nativeCreatePlane", "(II)Landroid/media/ImageReader$SurfaceImage$SurfacePlane;", |
Zhijun He | 212e78d | 2013-06-07 11:36:23 -0700 | [diff] [blame] | 953 | (void*)Image_createSurfacePlane }, |
| 954 | }; |
| 955 | |
| 956 | int register_android_media_ImageReader(JNIEnv *env) { |
| 957 | |
| 958 | int ret1 = AndroidRuntime::registerNativeMethods(env, |
| 959 | "android/media/ImageReader", gImageReaderMethods, NELEM(gImageReaderMethods)); |
| 960 | |
| 961 | int ret2 = AndroidRuntime::registerNativeMethods(env, |
| 962 | "android/media/ImageReader$SurfaceImage", gImageMethods, NELEM(gImageMethods)); |
| 963 | |
| 964 | return (ret1 || ret2); |
| 965 | } |