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