Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "ImageWriter_JNI" |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 19 | #include "android_media_Utils.h" |
| 20 | |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 21 | #include <utils/Condition.h> |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 23 | #include <utils/Mutex.h> |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 24 | #include <utils/String8.h> |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 25 | #include <utils/Thread.h> |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 26 | |
| 27 | #include <gui/IProducerListener.h> |
| 28 | #include <gui/Surface.h> |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 29 | #include <android_runtime/AndroidRuntime.h> |
| 30 | #include <android_runtime/android_view_Surface.h> |
renn | be09219 | 2018-05-07 10:18:05 -0700 | [diff] [blame] | 31 | #include <android_runtime/android_hardware_HardwareBuffer.h> |
| 32 | #include <private/android/AHardwareBufferHelpers.h> |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 33 | #include <jni.h> |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 34 | #include <nativehelper/JNIHelp.h> |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 35 | |
| 36 | #include <stdint.h> |
| 37 | #include <inttypes.h> |
renn | be09219 | 2018-05-07 10:18:05 -0700 | [diff] [blame] | 38 | #include <android/hardware_buffer_jni.h> |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 39 | |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 40 | #include <deque> |
| 41 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 42 | #define IMAGE_BUFFER_JNI_ID "mNativeBuffer" |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 43 | #define IMAGE_FORMAT_UNKNOWN 0 // This is the same value as ImageFormat#UNKNOWN. |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | using namespace android; |
| 47 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 48 | static struct { |
| 49 | jmethodID postEventFromNative; |
| 50 | jfieldID mWriterFormat; |
| 51 | } gImageWriterClassInfo; |
| 52 | |
| 53 | static struct { |
| 54 | jfieldID mNativeBuffer; |
| 55 | jfieldID mNativeFenceFd; |
| 56 | jfieldID mPlanes; |
| 57 | } gSurfaceImageClassInfo; |
| 58 | |
| 59 | static struct { |
| 60 | jclass clazz; |
| 61 | jmethodID ctor; |
| 62 | } gSurfacePlaneClassInfo; |
| 63 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | class JNIImageWriterContext : public BnProducerListener { |
| 67 | public: |
| 68 | JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz); |
| 69 | |
| 70 | virtual ~JNIImageWriterContext(); |
| 71 | |
| 72 | // Implementation of IProducerListener, used to notify the ImageWriter that the consumer |
| 73 | // has returned a buffer and it is ready for ImageWriter to dequeue. |
| 74 | virtual void onBufferReleased(); |
| 75 | |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 76 | void setProducer(const sp<Surface>& producer) { mProducer = producer; } |
| 77 | Surface* getProducer() { return mProducer.get(); } |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 78 | |
| 79 | void setBufferFormat(int format) { mFormat = format; } |
| 80 | int getBufferFormat() { return mFormat; } |
| 81 | |
| 82 | void setBufferWidth(int width) { mWidth = width; } |
| 83 | int getBufferWidth() { return mWidth; } |
| 84 | |
| 85 | void setBufferHeight(int height) { mHeight = height; } |
| 86 | int getBufferHeight() { return mHeight; } |
| 87 | |
| 88 | private: |
| 89 | static JNIEnv* getJNIEnv(bool* needsDetach); |
| 90 | static void detachJNI(); |
| 91 | |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 92 | sp<Surface> mProducer; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 93 | jobject mWeakThiz; |
| 94 | jclass mClazz; |
| 95 | int mFormat; |
| 96 | int mWidth; |
| 97 | int mHeight; |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 98 | |
| 99 | // Class for a shared thread used to detach buffers from buffer queues |
| 100 | // to discard buffers after consumers are done using them. |
| 101 | // This is needed because detaching buffers in onBufferReleased callback |
| 102 | // can lead to deadlock when consumer/producer are on the same process. |
| 103 | class BufferDetacher { |
| 104 | public: |
| 105 | // Called by JNIImageWriterContext ctor. Will start the thread for first ref. |
| 106 | void addRef(); |
| 107 | // Called by JNIImageWriterContext dtor. Will stop the thread after ref goes to 0. |
| 108 | void removeRef(); |
| 109 | // Called by onBufferReleased to signal this thread to detach a buffer |
| 110 | void detach(wp<Surface>); |
| 111 | |
| 112 | private: |
| 113 | |
| 114 | class DetachThread : public Thread { |
| 115 | public: |
| 116 | DetachThread() : Thread(/*canCallJava*/false) {}; |
| 117 | |
| 118 | void detach(wp<Surface>); |
| 119 | |
| 120 | virtual void requestExit() override; |
| 121 | |
| 122 | private: |
| 123 | virtual bool threadLoop() override; |
| 124 | |
| 125 | Mutex mLock; |
| 126 | Condition mCondition; |
| 127 | std::deque<wp<Surface>> mQueue; |
| 128 | |
| 129 | static const nsecs_t kWaitDuration = 20000000; // 20 ms |
| 130 | }; |
| 131 | sp<DetachThread> mThread; |
| 132 | |
| 133 | Mutex mLock; |
| 134 | int mRefCount = 0; |
| 135 | }; |
| 136 | |
| 137 | static BufferDetacher sBufferDetacher; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 140 | JNIImageWriterContext::BufferDetacher JNIImageWriterContext::sBufferDetacher; |
| 141 | |
| 142 | void JNIImageWriterContext::BufferDetacher::addRef() { |
| 143 | Mutex::Autolock l(mLock); |
| 144 | mRefCount++; |
| 145 | if (mRefCount == 1) { |
| 146 | mThread = new DetachThread(); |
| 147 | mThread->run("BufDtchThrd"); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void JNIImageWriterContext::BufferDetacher::removeRef() { |
| 152 | Mutex::Autolock l(mLock); |
| 153 | mRefCount--; |
| 154 | if (mRefCount == 0) { |
| 155 | mThread->requestExit(); |
| 156 | mThread->join(); |
| 157 | mThread.clear(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void JNIImageWriterContext::BufferDetacher::detach(wp<Surface> bq) { |
| 162 | Mutex::Autolock l(mLock); |
| 163 | if (mThread == nullptr) { |
| 164 | ALOGE("%s: buffer detach thread is gone!", __FUNCTION__); |
| 165 | return; |
| 166 | } |
| 167 | mThread->detach(bq); |
| 168 | } |
| 169 | |
| 170 | void JNIImageWriterContext::BufferDetacher::DetachThread::detach(wp<Surface> bq) { |
| 171 | Mutex::Autolock l(mLock); |
| 172 | mQueue.push_back(bq); |
| 173 | mCondition.signal(); |
| 174 | } |
| 175 | |
| 176 | void JNIImageWriterContext::BufferDetacher::DetachThread::requestExit() { |
| 177 | Thread::requestExit(); |
| 178 | { |
| 179 | Mutex::Autolock l(mLock); |
| 180 | mQueue.clear(); |
| 181 | } |
| 182 | mCondition.signal(); |
| 183 | } |
| 184 | |
| 185 | bool JNIImageWriterContext::BufferDetacher::DetachThread::threadLoop() { |
| 186 | Mutex::Autolock l(mLock); |
| 187 | mCondition.waitRelative(mLock, kWaitDuration); |
| 188 | |
| 189 | while (!mQueue.empty()) { |
| 190 | if (exitPending()) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | wp<Surface> wbq = mQueue.front(); |
| 195 | mQueue.pop_front(); |
| 196 | sp<Surface> bq = wbq.promote(); |
| 197 | if (bq != nullptr) { |
| 198 | sp<Fence> fence; |
| 199 | sp<GraphicBuffer> buffer; |
| 200 | ALOGV("%s: One buffer is detached", __FUNCTION__); |
| 201 | mLock.unlock(); |
| 202 | bq->detachNextBuffer(&buffer, &fence); |
| 203 | mLock.lock(); |
| 204 | } |
| 205 | } |
| 206 | return !exitPending(); |
| 207 | } |
| 208 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 209 | JNIImageWriterContext::JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz) : |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 210 | mWeakThiz(env->NewGlobalRef(weakThiz)), |
| 211 | mClazz((jclass)env->NewGlobalRef(clazz)), |
| 212 | mFormat(0), |
| 213 | mWidth(-1), |
| 214 | mHeight(-1) { |
| 215 | sBufferDetacher.addRef(); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | JNIImageWriterContext::~JNIImageWriterContext() { |
| 219 | ALOGV("%s", __FUNCTION__); |
| 220 | bool needsDetach = false; |
| 221 | JNIEnv* env = getJNIEnv(&needsDetach); |
| 222 | if (env != NULL) { |
| 223 | env->DeleteGlobalRef(mWeakThiz); |
| 224 | env->DeleteGlobalRef(mClazz); |
| 225 | } else { |
| 226 | ALOGW("leaking JNI object references"); |
| 227 | } |
| 228 | if (needsDetach) { |
| 229 | detachJNI(); |
| 230 | } |
| 231 | |
| 232 | mProducer.clear(); |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 233 | sBufferDetacher.removeRef(); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | JNIEnv* JNIImageWriterContext::getJNIEnv(bool* needsDetach) { |
| 237 | ALOGV("%s", __FUNCTION__); |
| 238 | LOG_ALWAYS_FATAL_IF(needsDetach == NULL, "needsDetach is null!!!"); |
| 239 | *needsDetach = false; |
| 240 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 241 | if (env == NULL) { |
| 242 | JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL}; |
| 243 | JavaVM* vm = AndroidRuntime::getJavaVM(); |
| 244 | int result = vm->AttachCurrentThread(&env, (void*) &args); |
| 245 | if (result != JNI_OK) { |
| 246 | ALOGE("thread attach failed: %#x", result); |
| 247 | return NULL; |
| 248 | } |
| 249 | *needsDetach = true; |
| 250 | } |
| 251 | return env; |
| 252 | } |
| 253 | |
| 254 | void JNIImageWriterContext::detachJNI() { |
| 255 | ALOGV("%s", __FUNCTION__); |
| 256 | JavaVM* vm = AndroidRuntime::getJavaVM(); |
| 257 | int result = vm->DetachCurrentThread(); |
| 258 | if (result != JNI_OK) { |
| 259 | ALOGE("thread detach failed: %#x", result); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void JNIImageWriterContext::onBufferReleased() { |
| 264 | ALOGV("%s: buffer released", __FUNCTION__); |
| 265 | bool needsDetach = false; |
| 266 | JNIEnv* env = getJNIEnv(&needsDetach); |
| 267 | if (env != NULL) { |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 268 | // Detach the buffer every time when a buffer consumption is done, |
| 269 | // need let this callback give a BufferItem, then only detach if it was attached to this |
| 270 | // Writer. Do the detach unconditionally for opaque format now. see b/19977520 |
| 271 | if (mFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
Yin-Chia Yeh | 6132b02 | 2019-02-14 16:40:20 -0800 | [diff] [blame] | 272 | sBufferDetacher.detach(mProducer); |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 275 | env->CallStaticVoidMethod(mClazz, gImageWriterClassInfo.postEventFromNative, mWeakThiz); |
| 276 | } else { |
| 277 | ALOGW("onBufferReleased event will not posted"); |
| 278 | } |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 279 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 280 | if (needsDetach) { |
| 281 | detachJNI(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // ---------------------------------------------------------------------------- |
| 286 | |
| 287 | extern "C" { |
| 288 | |
| 289 | // -------------------------------Private method declarations-------------- |
| 290 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 291 | static void Image_setNativeContext(JNIEnv* env, jobject thiz, |
| 292 | sp<GraphicBuffer> buffer, int fenceFd); |
| 293 | static void Image_getNativeContext(JNIEnv* env, jobject thiz, |
| 294 | GraphicBuffer** buffer, int* fenceFd); |
| 295 | static void Image_unlockIfLocked(JNIEnv* env, jobject thiz); |
| 296 | |
| 297 | // --------------------------ImageWriter methods--------------------------------------- |
| 298 | |
| 299 | static void ImageWriter_classInit(JNIEnv* env, jclass clazz) { |
| 300 | ALOGV("%s:", __FUNCTION__); |
| 301 | jclass imageClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage"); |
| 302 | LOG_ALWAYS_FATAL_IF(imageClazz == NULL, |
| 303 | "can't find android/media/ImageWriter$WriterSurfaceImage"); |
| 304 | gSurfaceImageClassInfo.mNativeBuffer = env->GetFieldID( |
| 305 | imageClazz, IMAGE_BUFFER_JNI_ID, "J"); |
| 306 | LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeBuffer == NULL, |
| 307 | "can't find android/media/ImageWriter$WriterSurfaceImage.%s", IMAGE_BUFFER_JNI_ID); |
| 308 | |
| 309 | gSurfaceImageClassInfo.mNativeFenceFd = env->GetFieldID( |
| 310 | imageClazz, "mNativeFenceFd", "I"); |
| 311 | LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeFenceFd == NULL, |
| 312 | "can't find android/media/ImageWriter$WriterSurfaceImage.mNativeFenceFd"); |
| 313 | |
| 314 | gSurfaceImageClassInfo.mPlanes = env->GetFieldID( |
| 315 | imageClazz, "mPlanes", "[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;"); |
| 316 | LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mPlanes == NULL, |
| 317 | "can't find android/media/ImageWriter$WriterSurfaceImage.mPlanes"); |
| 318 | |
| 319 | gImageWriterClassInfo.postEventFromNative = env->GetStaticMethodID( |
| 320 | clazz, "postEventFromNative", "(Ljava/lang/Object;)V"); |
| 321 | LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.postEventFromNative == NULL, |
| 322 | "can't find android/media/ImageWriter.postEventFromNative"); |
| 323 | |
| 324 | gImageWriterClassInfo.mWriterFormat = env->GetFieldID( |
| 325 | clazz, "mWriterFormat", "I"); |
| 326 | LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.mWriterFormat == NULL, |
| 327 | "can't find android/media/ImageWriter.mWriterFormat"); |
| 328 | |
| 329 | jclass planeClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage$SurfacePlane"); |
| 330 | LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find SurfacePlane class"); |
| 331 | // FindClass only gives a local reference of jclass object. |
| 332 | gSurfacePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz); |
| 333 | gSurfacePlaneClassInfo.ctor = env->GetMethodID(gSurfacePlaneClassInfo.clazz, "<init>", |
| 334 | "(Landroid/media/ImageWriter$WriterSurfaceImage;IILjava/nio/ByteBuffer;)V"); |
| 335 | LOG_ALWAYS_FATAL_IF(gSurfacePlaneClassInfo.ctor == NULL, |
| 336 | "Can not find SurfacePlane constructor"); |
| 337 | } |
| 338 | |
| 339 | static jlong ImageWriter_init(JNIEnv* env, jobject thiz, jobject weakThiz, jobject jsurface, |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 340 | jint maxImages, jint userFormat) { |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 341 | status_t res; |
| 342 | |
| 343 | ALOGV("%s: maxImages:%d", __FUNCTION__, maxImages); |
| 344 | |
| 345 | sp<Surface> surface(android_view_Surface_getSurface(env, jsurface)); |
| 346 | if (surface == NULL) { |
| 347 | jniThrowException(env, |
| 348 | "java/lang/IllegalArgumentException", |
| 349 | "The surface has been released"); |
| 350 | return 0; |
| 351 | } |
| 352 | sp<IGraphicBufferProducer> bufferProducer = surface->getIGraphicBufferProducer(); |
| 353 | |
| 354 | jclass clazz = env->GetObjectClass(thiz); |
| 355 | if (clazz == NULL) { |
| 356 | jniThrowRuntimeException(env, "Can't find android/graphics/ImageWriter"); |
| 357 | return 0; |
| 358 | } |
| 359 | sp<JNIImageWriterContext> ctx(new JNIImageWriterContext(env, weakThiz, clazz)); |
| 360 | |
| 361 | sp<Surface> producer = new Surface(bufferProducer, /*controlledByApp*/false); |
| 362 | ctx->setProducer(producer); |
| 363 | /** |
| 364 | * NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not connectable |
| 365 | * after disconnect. MEDIA or CAMERA are treated the same internally. The producer listener |
| 366 | * will be cleared after disconnect call. |
| 367 | */ |
Emilian Peev | 2adf403 | 2018-06-05 17:52:16 +0100 | [diff] [blame] | 368 | res = producer->connect(/*api*/NATIVE_WINDOW_API_CAMERA, /*listener*/ctx); |
| 369 | if (res != OK) { |
| 370 | ALOGE("%s: Connecting to surface producer interface failed: %s (%d)", |
| 371 | __FUNCTION__, strerror(-res), res); |
| 372 | jniThrowRuntimeException(env, "Failed to connect to native window"); |
| 373 | return 0; |
| 374 | } |
| 375 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 376 | jlong nativeCtx = reinterpret_cast<jlong>(ctx.get()); |
| 377 | |
| 378 | // Get the dimension and format of the producer. |
| 379 | sp<ANativeWindow> anw = producer; |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 380 | int32_t width, height, surfaceFormat; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 381 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) { |
| 382 | ALOGE("%s: Query Surface width failed: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 383 | jniThrowRuntimeException(env, "Failed to query Surface width"); |
| 384 | return 0; |
| 385 | } |
| 386 | ctx->setBufferWidth(width); |
| 387 | |
| 388 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) { |
| 389 | ALOGE("%s: Query Surface height failed: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 390 | jniThrowRuntimeException(env, "Failed to query Surface height"); |
| 391 | return 0; |
| 392 | } |
| 393 | ctx->setBufferHeight(height); |
| 394 | |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 395 | // Query surface format if no valid user format is specified, otherwise, override surface format |
| 396 | // with user format. |
| 397 | if (userFormat == IMAGE_FORMAT_UNKNOWN) { |
| 398 | if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &surfaceFormat)) != OK) { |
| 399 | ALOGE("%s: Query Surface format failed: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 400 | jniThrowRuntimeException(env, "Failed to query Surface format"); |
| 401 | return 0; |
| 402 | } |
| 403 | } else { |
| 404 | surfaceFormat = userFormat; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 405 | } |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 406 | ctx->setBufferFormat(surfaceFormat); |
| 407 | env->SetIntField(thiz, |
| 408 | gImageWriterClassInfo.mWriterFormat, reinterpret_cast<jint>(surfaceFormat)); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 409 | |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 410 | if (!isFormatOpaque(surfaceFormat)) { |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 411 | res = native_window_set_usage(anw.get(), GRALLOC_USAGE_SW_WRITE_OFTEN); |
| 412 | if (res != OK) { |
| 413 | ALOGE("%s: Configure usage %08x for format %08x failed: %s (%d)", |
Dan Albert | 1102e21 | 2015-06-09 17:35:38 -0700 | [diff] [blame] | 414 | __FUNCTION__, static_cast<unsigned int>(GRALLOC_USAGE_SW_WRITE_OFTEN), |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 415 | surfaceFormat, strerror(-res), res); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 416 | jniThrowRuntimeException(env, "Failed to SW_WRITE_OFTEN configure usage"); |
| 417 | return 0; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | int minUndequeuedBufferCount = 0; |
| 422 | res = anw->query(anw.get(), |
| 423 | NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufferCount); |
| 424 | if (res != OK) { |
| 425 | ALOGE("%s: Query producer undequeued buffer count failed: %s (%d)", |
| 426 | __FUNCTION__, strerror(-res), res); |
| 427 | jniThrowRuntimeException(env, "Query producer undequeued buffer count failed"); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | size_t totalBufferCount = maxImages + minUndequeuedBufferCount; |
| 432 | res = native_window_set_buffer_count(anw.get(), totalBufferCount); |
| 433 | if (res != OK) { |
| 434 | ALOGE("%s: Set buffer count failed: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 435 | jniThrowRuntimeException(env, "Set buffer count failed"); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | if (ctx != 0) { |
| 440 | ctx->incStrong((void*)ImageWriter_init); |
| 441 | } |
| 442 | return nativeCtx; |
| 443 | } |
| 444 | |
| 445 | static void ImageWriter_dequeueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) { |
| 446 | ALOGV("%s", __FUNCTION__); |
| 447 | JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx); |
| 448 | if (ctx == NULL || thiz == NULL) { |
| 449 | jniThrowException(env, "java/lang/IllegalStateException", |
| 450 | "ImageWriterContext is not initialized"); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | sp<ANativeWindow> anw = ctx->getProducer(); |
| 455 | android_native_buffer_t *anb = NULL; |
| 456 | int fenceFd = -1; |
| 457 | status_t res = anw->dequeueBuffer(anw.get(), &anb, &fenceFd); |
| 458 | if (res != OK) { |
Zhijun He | a582714 | 2015-04-22 10:07:42 -0700 | [diff] [blame] | 459 | ALOGE("%s: Dequeue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res); |
Chien-Yu Chen | e0ee630 | 2015-07-06 17:46:05 -0700 | [diff] [blame] | 460 | switch (res) { |
| 461 | case NO_INIT: |
| 462 | jniThrowException(env, "java/lang/IllegalStateException", |
| 463 | "Surface has been abandoned"); |
| 464 | break; |
| 465 | default: |
| 466 | // TODO: handle other error cases here. |
| 467 | jniThrowRuntimeException(env, "dequeue buffer failed"); |
| 468 | } |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 469 | return; |
| 470 | } |
| 471 | // New GraphicBuffer object doesn't own the handle, thus the native buffer |
| 472 | // won't be freed when this object is destroyed. |
Mathias Agopian | 845eef05f | 2017-04-03 17:51:15 -0700 | [diff] [blame] | 473 | sp<GraphicBuffer> buffer(GraphicBuffer::from(anb)); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 474 | |
| 475 | // Note that: |
| 476 | // 1. No need to lock buffer now, will only lock it when the first getPlanes() is called. |
| 477 | // 2. Fence will be saved to mNativeFenceFd, and will consumed by lock/queue/cancel buffer |
| 478 | // later. |
| 479 | // 3. need use lockAsync here, as it will handle the dequeued fence for us automatically. |
| 480 | |
| 481 | // Finally, set the native info into image object. |
| 482 | Image_setNativeContext(env, image, buffer, fenceFd); |
| 483 | } |
| 484 | |
| 485 | static void ImageWriter_close(JNIEnv* env, jobject thiz, jlong nativeCtx) { |
| 486 | ALOGV("%s:", __FUNCTION__); |
| 487 | JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx); |
| 488 | if (ctx == NULL || thiz == NULL) { |
Chien-Yu Chen | 0375cb0 | 2015-06-18 11:55:18 -0700 | [diff] [blame] | 489 | // ImageWriter is already closed. |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | ANativeWindow* producer = ctx->getProducer(); |
| 494 | if (producer != NULL) { |
| 495 | /** |
| 496 | * NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not |
| 497 | * connectable after disconnect. MEDIA or CAMERA are treated the same internally. |
| 498 | * The producer listener will be cleared after disconnect call. |
| 499 | */ |
| 500 | status_t res = native_window_api_disconnect(producer, /*api*/NATIVE_WINDOW_API_CAMERA); |
| 501 | /** |
| 502 | * This is not an error. if client calling process dies, the window will |
| 503 | * also die and all calls to it will return DEAD_OBJECT, thus it's already |
| 504 | * "disconnected" |
| 505 | */ |
| 506 | if (res == DEAD_OBJECT) { |
| 507 | ALOGW("%s: While disconnecting ImageWriter from native window, the" |
| 508 | " native window died already", __FUNCTION__); |
| 509 | } else if (res != OK) { |
| 510 | ALOGE("%s: native window disconnect failed: %s (%d)", |
| 511 | __FUNCTION__, strerror(-res), res); |
| 512 | jniThrowRuntimeException(env, "Native window disconnect failed"); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | ctx->decStrong((void*)ImageWriter_init); |
| 518 | } |
| 519 | |
| 520 | static void ImageWriter_cancelImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) { |
| 521 | ALOGV("%s", __FUNCTION__); |
| 522 | JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx); |
| 523 | if (ctx == NULL || thiz == NULL) { |
Zhijun He | dc6bb24 | 2015-12-03 15:34:34 -0800 | [diff] [blame] | 524 | ALOGW("ImageWriter#close called before Image#close, consider calling Image#close first"); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 525 | return; |
| 526 | } |
| 527 | |
| 528 | sp<ANativeWindow> anw = ctx->getProducer(); |
| 529 | |
| 530 | GraphicBuffer *buffer = NULL; |
| 531 | int fenceFd = -1; |
| 532 | Image_getNativeContext(env, image, &buffer, &fenceFd); |
| 533 | if (buffer == NULL) { |
Zhijun He | dc6bb24 | 2015-12-03 15:34:34 -0800 | [diff] [blame] | 534 | // Cancel an already cancelled image is harmless. |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | |
| 538 | // Unlock the image if it was locked |
| 539 | Image_unlockIfLocked(env, image); |
| 540 | |
| 541 | anw->cancelBuffer(anw.get(), buffer, fenceFd); |
| 542 | |
| 543 | Image_setNativeContext(env, image, NULL, -1); |
| 544 | } |
| 545 | |
| 546 | static void ImageWriter_queueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image, |
Emilian Peev | 750aec6 | 2018-04-06 12:55:00 +0100 | [diff] [blame] | 547 | jlong timestampNs, jint left, jint top, jint right, jint bottom, jint transform, |
| 548 | jint scalingMode) { |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 549 | ALOGV("%s", __FUNCTION__); |
| 550 | JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx); |
| 551 | if (ctx == NULL || thiz == NULL) { |
| 552 | jniThrowException(env, "java/lang/IllegalStateException", |
| 553 | "ImageWriterContext is not initialized"); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | status_t res = OK; |
| 558 | sp<ANativeWindow> anw = ctx->getProducer(); |
| 559 | |
| 560 | GraphicBuffer *buffer = NULL; |
| 561 | int fenceFd = -1; |
| 562 | Image_getNativeContext(env, image, &buffer, &fenceFd); |
| 563 | if (buffer == NULL) { |
| 564 | jniThrowException(env, "java/lang/IllegalStateException", |
| 565 | "Image is not initialized"); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | // Unlock image if it was locked. |
| 570 | Image_unlockIfLocked(env, image); |
| 571 | |
| 572 | // Set timestamp |
Zhijun He | d1cbc68 | 2015-03-23 10:10:04 -0700 | [diff] [blame] | 573 | ALOGV("timestamp to be queued: %" PRId64, timestampNs); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 574 | res = native_window_set_buffers_timestamp(anw.get(), timestampNs); |
| 575 | if (res != OK) { |
| 576 | jniThrowRuntimeException(env, "Set timestamp failed"); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | // Set crop |
| 581 | android_native_rect_t cropRect; |
| 582 | cropRect.left = left; |
| 583 | cropRect.top = top; |
| 584 | cropRect.right = right; |
| 585 | cropRect.bottom = bottom; |
| 586 | res = native_window_set_crop(anw.get(), &cropRect); |
| 587 | if (res != OK) { |
| 588 | jniThrowRuntimeException(env, "Set crop rect failed"); |
| 589 | return; |
| 590 | } |
| 591 | |
Emilian Peev | 450a5ff | 2018-03-19 16:05:10 +0000 | [diff] [blame] | 592 | res = native_window_set_buffers_transform(anw.get(), transform); |
| 593 | if (res != OK) { |
| 594 | jniThrowRuntimeException(env, "Set transform failed"); |
| 595 | return; |
| 596 | } |
| 597 | |
Emilian Peev | 750aec6 | 2018-04-06 12:55:00 +0100 | [diff] [blame] | 598 | res = native_window_set_scaling_mode(anw.get(), scalingMode); |
| 599 | if (res != OK) { |
| 600 | jniThrowRuntimeException(env, "Set scaling mode failed"); |
| 601 | return; |
| 602 | } |
| 603 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 604 | // Finally, queue input buffer |
| 605 | res = anw->queueBuffer(anw.get(), buffer, fenceFd); |
| 606 | if (res != OK) { |
Chien-Yu Chen | e0ee630 | 2015-07-06 17:46:05 -0700 | [diff] [blame] | 607 | ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 608 | switch (res) { |
| 609 | case NO_INIT: |
| 610 | jniThrowException(env, "java/lang/IllegalStateException", |
| 611 | "Surface has been abandoned"); |
| 612 | break; |
| 613 | default: |
| 614 | // TODO: handle other error cases here. |
| 615 | jniThrowRuntimeException(env, "Queue input buffer failed"); |
| 616 | } |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 617 | return; |
| 618 | } |
| 619 | |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 620 | // Clear the image native context: end of this image's lifecycle in public API. |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 621 | Image_setNativeContext(env, image, NULL, -1); |
| 622 | } |
| 623 | |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 624 | static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, |
| 625 | jlong nativeBuffer, jint imageFormat, jlong timestampNs, jint left, jint top, |
Emilian Peev | 750aec6 | 2018-04-06 12:55:00 +0100 | [diff] [blame] | 626 | jint right, jint bottom, jint transform, jint scalingMode) { |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 627 | ALOGV("%s", __FUNCTION__); |
| 628 | JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx); |
| 629 | if (ctx == NULL || thiz == NULL) { |
| 630 | jniThrowException(env, "java/lang/IllegalStateException", |
| 631 | "ImageWriterContext is not initialized"); |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 632 | return -1; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 635 | sp<Surface> surface = ctx->getProducer(); |
| 636 | status_t res = OK; |
Eino-Ville Talvala | 07ad459 | 2017-05-02 12:44:05 -0700 | [diff] [blame] | 637 | if (isFormatOpaque(imageFormat) != isFormatOpaque(ctx->getBufferFormat())) { |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 638 | jniThrowException(env, "java/lang/IllegalStateException", |
Eino-Ville Talvala | 07ad459 | 2017-05-02 12:44:05 -0700 | [diff] [blame] | 639 | "Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa"); |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 640 | return -1; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 643 | // Image is guaranteed to be from ImageReader at this point, so it is safe to |
| 644 | // cast to BufferItem pointer. |
Eino-Ville Talvala | 07ad459 | 2017-05-02 12:44:05 -0700 | [diff] [blame] | 645 | BufferItem* buffer = reinterpret_cast<BufferItem*>(nativeBuffer); |
| 646 | if (buffer == NULL) { |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 647 | jniThrowException(env, "java/lang/IllegalStateException", |
| 648 | "Image is not initialized or already closed"); |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | // Step 1. Attach Image |
Eino-Ville Talvala | 07ad459 | 2017-05-02 12:44:05 -0700 | [diff] [blame] | 653 | res = surface->attachBuffer(buffer->mGraphicBuffer.get()); |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 654 | if (res != OK) { |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 655 | ALOGE("Attach image failed: %s (%d)", strerror(-res), res); |
Chien-Yu Chen | e0ee630 | 2015-07-06 17:46:05 -0700 | [diff] [blame] | 656 | switch (res) { |
| 657 | case NO_INIT: |
| 658 | jniThrowException(env, "java/lang/IllegalStateException", |
| 659 | "Surface has been abandoned"); |
| 660 | break; |
| 661 | default: |
| 662 | // TODO: handle other error cases here. |
| 663 | jniThrowRuntimeException(env, "nativeAttachImage failed!!!"); |
| 664 | } |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 665 | return res; |
| 666 | } |
| 667 | sp < ANativeWindow > anw = surface; |
| 668 | |
Emilian Peev | 750aec6 | 2018-04-06 12:55:00 +0100 | [diff] [blame] | 669 | // Step 2. Set timestamp, crop, transform and scaling mode. Note that we do not need unlock the |
| 670 | // image because it was not locked. |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 671 | ALOGV("timestamp to be queued: %" PRId64, timestampNs); |
| 672 | res = native_window_set_buffers_timestamp(anw.get(), timestampNs); |
| 673 | if (res != OK) { |
| 674 | jniThrowRuntimeException(env, "Set timestamp failed"); |
| 675 | return res; |
| 676 | } |
| 677 | |
| 678 | android_native_rect_t cropRect; |
| 679 | cropRect.left = left; |
| 680 | cropRect.top = top; |
| 681 | cropRect.right = right; |
| 682 | cropRect.bottom = bottom; |
| 683 | res = native_window_set_crop(anw.get(), &cropRect); |
| 684 | if (res != OK) { |
| 685 | jniThrowRuntimeException(env, "Set crop rect failed"); |
| 686 | return res; |
| 687 | } |
| 688 | |
Emilian Peev | 450a5ff | 2018-03-19 16:05:10 +0000 | [diff] [blame] | 689 | res = native_window_set_buffers_transform(anw.get(), transform); |
| 690 | if (res != OK) { |
| 691 | jniThrowRuntimeException(env, "Set transform failed"); |
| 692 | return res; |
| 693 | } |
| 694 | |
Emilian Peev | 750aec6 | 2018-04-06 12:55:00 +0100 | [diff] [blame] | 695 | res = native_window_set_scaling_mode(anw.get(), scalingMode); |
| 696 | if (res != OK) { |
| 697 | jniThrowRuntimeException(env, "Set scaling mode failed"); |
| 698 | return res; |
| 699 | } |
| 700 | |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 701 | // Step 3. Queue Image. |
Eino-Ville Talvala | 07ad459 | 2017-05-02 12:44:05 -0700 | [diff] [blame] | 702 | res = anw->queueBuffer(anw.get(), buffer->mGraphicBuffer.get(), /*fenceFd*/ |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 703 | -1); |
| 704 | if (res != OK) { |
Chien-Yu Chen | e0ee630 | 2015-07-06 17:46:05 -0700 | [diff] [blame] | 705 | ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res); |
| 706 | switch (res) { |
| 707 | case NO_INIT: |
| 708 | jniThrowException(env, "java/lang/IllegalStateException", |
| 709 | "Surface has been abandoned"); |
| 710 | break; |
| 711 | default: |
| 712 | // TODO: handle other error cases here. |
| 713 | jniThrowRuntimeException(env, "Queue input buffer failed"); |
| 714 | } |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 715 | return res; |
| 716 | } |
| 717 | |
| 718 | // Do not set the image native context. Since it would overwrite the existing native context |
| 719 | // of the image that is from ImageReader, the subsequent image close will run into issues. |
| 720 | |
| 721 | return res; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | // --------------------------Image methods--------------------------------------- |
| 725 | |
| 726 | static void Image_getNativeContext(JNIEnv* env, jobject thiz, |
| 727 | GraphicBuffer** buffer, int* fenceFd) { |
| 728 | ALOGV("%s", __FUNCTION__); |
| 729 | if (buffer != NULL) { |
| 730 | GraphicBuffer *gb = reinterpret_cast<GraphicBuffer *> |
| 731 | (env->GetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer)); |
| 732 | *buffer = gb; |
| 733 | } |
| 734 | |
| 735 | if (fenceFd != NULL) { |
| 736 | *fenceFd = reinterpret_cast<jint>(env->GetIntField( |
| 737 | thiz, gSurfaceImageClassInfo.mNativeFenceFd)); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | static void Image_setNativeContext(JNIEnv* env, jobject thiz, |
| 742 | sp<GraphicBuffer> buffer, int fenceFd) { |
| 743 | ALOGV("%s:", __FUNCTION__); |
| 744 | GraphicBuffer* p = NULL; |
| 745 | Image_getNativeContext(env, thiz, &p, /*fenceFd*/NULL); |
| 746 | if (buffer != 0) { |
| 747 | buffer->incStrong((void*)Image_setNativeContext); |
| 748 | } |
| 749 | if (p) { |
| 750 | p->decStrong((void*)Image_setNativeContext); |
| 751 | } |
| 752 | env->SetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer, |
| 753 | reinterpret_cast<jlong>(buffer.get())); |
| 754 | |
| 755 | env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd)); |
| 756 | } |
| 757 | |
| 758 | static void Image_unlockIfLocked(JNIEnv* env, jobject thiz) { |
| 759 | ALOGV("%s", __FUNCTION__); |
| 760 | GraphicBuffer* buffer; |
| 761 | Image_getNativeContext(env, thiz, &buffer, NULL); |
| 762 | if (buffer == NULL) { |
| 763 | jniThrowException(env, "java/lang/IllegalStateException", |
| 764 | "Image is not initialized"); |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | // Is locked? |
| 769 | bool isLocked = false; |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 770 | jobject planes = NULL; |
| 771 | if (!isFormatOpaque(buffer->getPixelFormat())) { |
| 772 | planes = env->GetObjectField(thiz, gSurfaceImageClassInfo.mPlanes); |
| 773 | } |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 774 | isLocked = (planes != NULL); |
| 775 | if (isLocked) { |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 776 | // no need to use fence here, as we it will be consumed by either cancel or queue buffer. |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 777 | status_t res = buffer->unlock(); |
| 778 | if (res != OK) { |
| 779 | jniThrowRuntimeException(env, "unlock buffer failed"); |
John Reck | dcd4c58 | 2019-07-12 13:13:05 -0700 | [diff] [blame^] | 780 | return; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 781 | } |
| 782 | ALOGV("Successfully unlocked the image"); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | static jint Image_getWidth(JNIEnv* env, jobject thiz) { |
| 787 | ALOGV("%s", __FUNCTION__); |
| 788 | GraphicBuffer* buffer; |
| 789 | Image_getNativeContext(env, thiz, &buffer, NULL); |
| 790 | if (buffer == NULL) { |
| 791 | jniThrowException(env, "java/lang/IllegalStateException", |
| 792 | "Image is not initialized"); |
| 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | return buffer->getWidth(); |
| 797 | } |
| 798 | |
| 799 | static jint Image_getHeight(JNIEnv* env, jobject thiz) { |
| 800 | ALOGV("%s", __FUNCTION__); |
| 801 | GraphicBuffer* buffer; |
| 802 | Image_getNativeContext(env, thiz, &buffer, NULL); |
| 803 | if (buffer == NULL) { |
| 804 | jniThrowException(env, "java/lang/IllegalStateException", |
| 805 | "Image is not initialized"); |
| 806 | return -1; |
| 807 | } |
| 808 | |
| 809 | return buffer->getHeight(); |
| 810 | } |
| 811 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 812 | static jint Image_getFormat(JNIEnv* env, jobject thiz) { |
| 813 | ALOGV("%s", __FUNCTION__); |
| 814 | GraphicBuffer* buffer; |
| 815 | Image_getNativeContext(env, thiz, &buffer, NULL); |
| 816 | if (buffer == NULL) { |
| 817 | jniThrowException(env, "java/lang/IllegalStateException", |
| 818 | "Image is not initialized"); |
| 819 | return 0; |
| 820 | } |
| 821 | |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 822 | // ImageWriter doesn't support data space yet, assuming it is unknown. |
Fedor Kudasov | 4d027e9 | 2019-06-24 17:21:57 +0100 | [diff] [blame] | 823 | PublicFormat publicFmt = mapHalFormatDataspaceToPublicFormat(buffer->getPixelFormat(), |
| 824 | HAL_DATASPACE_UNKNOWN); |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 825 | return static_cast<jint>(publicFmt); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 826 | } |
| 827 | |
renn | be09219 | 2018-05-07 10:18:05 -0700 | [diff] [blame] | 828 | static jobject Image_getHardwareBuffer(JNIEnv* env, jobject thiz) { |
| 829 | GraphicBuffer* buffer; |
| 830 | Image_getNativeContext(env, thiz, &buffer, NULL); |
| 831 | if (buffer == NULL) { |
| 832 | jniThrowException(env, "java/lang/IllegalStateException", |
| 833 | "Image is not initialized"); |
| 834 | return NULL; |
| 835 | } |
| 836 | AHardwareBuffer* b = AHardwareBuffer_from_GraphicBuffer(buffer); |
| 837 | // don't user the public AHardwareBuffer_toHardwareBuffer() because this would force us |
| 838 | // to link against libandroid.so |
| 839 | return android_hardware_HardwareBuffer_createFromAHardwareBuffer(env, b); |
| 840 | } |
| 841 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 842 | static void Image_setFenceFd(JNIEnv* env, jobject thiz, int fenceFd) { |
| 843 | ALOGV("%s:", __FUNCTION__); |
| 844 | env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd)); |
| 845 | } |
| 846 | |
| 847 | static void Image_getLockedImage(JNIEnv* env, jobject thiz, LockedImage *image) { |
| 848 | ALOGV("%s", __FUNCTION__); |
| 849 | GraphicBuffer* buffer; |
| 850 | int fenceFd = -1; |
| 851 | Image_getNativeContext(env, thiz, &buffer, &fenceFd); |
| 852 | if (buffer == NULL) { |
| 853 | jniThrowException(env, "java/lang/IllegalStateException", |
| 854 | "Image is not initialized"); |
| 855 | return; |
| 856 | } |
| 857 | |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 858 | // ImageWriter doesn't use crop by itself, app sets it, use the no crop version. |
| 859 | const Rect noCrop(buffer->width, buffer->height); |
| 860 | status_t res = lockImageFromBuffer( |
| 861 | buffer, GRALLOC_USAGE_SW_WRITE_OFTEN, noCrop, fenceFd, image); |
| 862 | // Clear the fenceFd as it is already consumed by lock call. |
| 863 | Image_setFenceFd(env, thiz, /*fenceFd*/-1); |
| 864 | if (res != OK) { |
| 865 | jniThrowExceptionFmt(env, "java/lang/RuntimeException", |
| 866 | "lock buffer failed for format 0x%x", |
| 867 | buffer->getPixelFormat()); |
| 868 | return; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 869 | } |
| 870 | |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 871 | ALOGV("%s: Successfully locked the image", __FUNCTION__); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 872 | // crop, transform, scalingMode, timestamp, and frameNumber should be set by producer, |
| 873 | // and we don't set them here. |
| 874 | } |
| 875 | |
John Reck | dcd4c58 | 2019-07-12 13:13:05 -0700 | [diff] [blame^] | 876 | static bool Image_getLockedImageInfo(JNIEnv* env, LockedImage* buffer, int idx, |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 877 | int32_t writerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) { |
| 878 | ALOGV("%s", __FUNCTION__); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 879 | |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 880 | status_t res = getLockedImageInfo(buffer, idx, writerFormat, base, size, |
| 881 | pixelStride, rowStride); |
| 882 | if (res != OK) { |
| 883 | jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException", |
| 884 | "Pixel format: 0x%x is unsupported", buffer->flexFormat); |
John Reck | dcd4c58 | 2019-07-12 13:13:05 -0700 | [diff] [blame^] | 885 | return false; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 886 | } |
John Reck | dcd4c58 | 2019-07-12 13:13:05 -0700 | [diff] [blame^] | 887 | return true; |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz, |
| 891 | int numPlanes, int writerFormat) { |
| 892 | ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes); |
| 893 | int rowStride, pixelStride; |
| 894 | uint8_t *pData; |
| 895 | uint32_t dataSize; |
| 896 | jobject byteBuffer; |
| 897 | |
| 898 | int format = Image_getFormat(env, thiz); |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 899 | if (isFormatOpaque(format) && numPlanes > 0) { |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 900 | String8 msg; |
| 901 | msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)" |
| 902 | " must be 0", format, numPlanes); |
| 903 | jniThrowException(env, "java/lang/IllegalArgumentException", msg.string()); |
| 904 | return NULL; |
| 905 | } |
| 906 | |
| 907 | jobjectArray surfacePlanes = env->NewObjectArray(numPlanes, gSurfacePlaneClassInfo.clazz, |
| 908 | /*initial_element*/NULL); |
| 909 | if (surfacePlanes == NULL) { |
| 910 | jniThrowRuntimeException(env, "Failed to create SurfacePlane arrays," |
| 911 | " probably out of memory"); |
| 912 | return NULL; |
| 913 | } |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 914 | if (isFormatOpaque(format)) { |
| 915 | return surfacePlanes; |
| 916 | } |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 917 | |
| 918 | // Buildup buffer info: rowStride, pixelStride and byteBuffers. |
| 919 | LockedImage lockedImg = LockedImage(); |
| 920 | Image_getLockedImage(env, thiz, &lockedImg); |
| 921 | |
| 922 | // Create all SurfacePlanes |
Zhijun He | 0ab4162 | 2016-02-25 16:00:38 -0800 | [diff] [blame] | 923 | PublicFormat publicWriterFormat = static_cast<PublicFormat>(writerFormat); |
Fedor Kudasov | 4d027e9 | 2019-06-24 17:21:57 +0100 | [diff] [blame] | 924 | writerFormat = mapPublicFormatToHalFormat(publicWriterFormat); |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 925 | for (int i = 0; i < numPlanes; i++) { |
John Reck | dcd4c58 | 2019-07-12 13:13:05 -0700 | [diff] [blame^] | 926 | if (!Image_getLockedImageInfo(env, &lockedImg, i, writerFormat, |
| 927 | &pData, &dataSize, &pixelStride, &rowStride)) { |
| 928 | return NULL; |
| 929 | } |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 930 | byteBuffer = env->NewDirectByteBuffer(pData, dataSize); |
| 931 | if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) { |
| 932 | jniThrowException(env, "java/lang/IllegalStateException", |
| 933 | "Failed to allocate ByteBuffer"); |
| 934 | return NULL; |
| 935 | } |
| 936 | |
| 937 | // Finally, create this SurfacePlane. |
| 938 | jobject surfacePlane = env->NewObject(gSurfacePlaneClassInfo.clazz, |
| 939 | gSurfacePlaneClassInfo.ctor, thiz, rowStride, pixelStride, byteBuffer); |
| 940 | env->SetObjectArrayElement(surfacePlanes, i, surfacePlane); |
| 941 | } |
| 942 | |
| 943 | return surfacePlanes; |
| 944 | } |
| 945 | |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 946 | } // extern "C" |
| 947 | |
| 948 | // ---------------------------------------------------------------------------- |
| 949 | |
| 950 | static JNINativeMethod gImageWriterMethods[] = { |
| 951 | {"nativeClassInit", "()V", (void*)ImageWriter_classInit }, |
Zhijun He | 916d8ac | 2017-03-22 17:33:47 -0700 | [diff] [blame] | 952 | {"nativeInit", "(Ljava/lang/Object;Landroid/view/Surface;II)J", |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 953 | (void*)ImageWriter_init }, |
Zhijun He | ce9d6f9 | 2015-03-29 16:33:59 -0700 | [diff] [blame] | 954 | {"nativeClose", "(J)V", (void*)ImageWriter_close }, |
Emilian Peev | 750aec6 | 2018-04-06 12:55:00 +0100 | [diff] [blame] | 955 | {"nativeAttachAndQueueImage", "(JJIJIIIIII)I", (void*)ImageWriter_attachAndQueueImage }, |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 956 | {"nativeDequeueInputImage", "(JLandroid/media/Image;)V", (void*)ImageWriter_dequeueImage }, |
Emilian Peev | 750aec6 | 2018-04-06 12:55:00 +0100 | [diff] [blame] | 957 | {"nativeQueueInputImage", "(JLandroid/media/Image;JIIIIII)V", (void*)ImageWriter_queueImage }, |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 958 | {"cancelImage", "(JLandroid/media/Image;)V", (void*)ImageWriter_cancelImage }, |
| 959 | }; |
| 960 | |
| 961 | static JNINativeMethod gImageMethods[] = { |
| 962 | {"nativeCreatePlanes", "(II)[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;", |
renn | be09219 | 2018-05-07 10:18:05 -0700 | [diff] [blame] | 963 | (void*)Image_createSurfacePlanes }, |
| 964 | {"nativeGetWidth", "()I", (void*)Image_getWidth }, |
| 965 | {"nativeGetHeight", "()I", (void*)Image_getHeight }, |
| 966 | {"nativeGetFormat", "()I", (void*)Image_getFormat }, |
| 967 | {"nativeGetHardwareBuffer", "()Landroid/hardware/HardwareBuffer;", |
| 968 | (void*)Image_getHardwareBuffer }, |
Zhijun He | f6a09e5 | 2015-02-24 18:12:23 -0800 | [diff] [blame] | 969 | }; |
| 970 | |
| 971 | int register_android_media_ImageWriter(JNIEnv *env) { |
| 972 | |
| 973 | int ret1 = AndroidRuntime::registerNativeMethods(env, |
| 974 | "android/media/ImageWriter", gImageWriterMethods, NELEM(gImageWriterMethods)); |
| 975 | |
| 976 | int ret2 = AndroidRuntime::registerNativeMethods(env, |
| 977 | "android/media/ImageWriter$WriterSurfaceImage", gImageMethods, NELEM(gImageMethods)); |
| 978 | |
| 979 | return (ret1 || ret2); |
| 980 | } |