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"
19#include "android_media_Utils.h"
20
21#include <utils/Log.h>
22#include <utils/String8.h>
23
24#include <gui/IProducerListener.h>
25#include <gui/Surface.h>
26#include <android_runtime/AndroidRuntime.h>
27#include <android_runtime/android_view_Surface.h>
28#include <camera3.h>
29#include <jni.h>
30#include <JNIHelp.h>
31
32#include <stdint.h>
33#include <inttypes.h>
34
35#define IMAGE_BUFFER_JNI_ID           "mNativeBuffer"
36#define IMAGE_FORMAT_UNKNOWN          0 // This is the same value as ImageFormat#UNKNOWN.
37// ----------------------------------------------------------------------------
38
39using namespace android;
40
41static struct {
42    jmethodID postEventFromNative;
43    jfieldID mWriterFormat;
44} gImageWriterClassInfo;
45
46static struct {
47    jfieldID mNativeBuffer;
48    jfieldID mNativeFenceFd;
49    jfieldID mPlanes;
50} gSurfaceImageClassInfo;
51
52static struct {
53    jclass clazz;
54    jmethodID ctor;
55} gSurfacePlaneClassInfo;
56
57// ----------------------------------------------------------------------------
58
59class JNIImageWriterContext : public BnProducerListener {
60public:
61    JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz);
62
63    virtual ~JNIImageWriterContext();
64
65    // Implementation of IProducerListener, used to notify the ImageWriter that the consumer
66    // has returned a buffer and it is ready for ImageWriter to dequeue.
67    virtual void onBufferReleased();
68
69    void setProducer(const sp<Surface>& producer) { mProducer = producer; }
70    Surface* getProducer() { return mProducer.get(); }
71
72    void setBufferFormat(int format) { mFormat = format; }
73    int getBufferFormat() { return mFormat; }
74
75    void setBufferWidth(int width) { mWidth = width; }
76    int getBufferWidth() { return mWidth; }
77
78    void setBufferHeight(int height) { mHeight = height; }
79    int getBufferHeight() { return mHeight; }
80
81private:
82    static JNIEnv* getJNIEnv(bool* needsDetach);
83    static void detachJNI();
84
85    sp<Surface> mProducer;
86    jobject mWeakThiz;
87    jclass mClazz;
88    int mFormat;
89    int mWidth;
90    int mHeight;
91};
92
93JNIImageWriterContext::JNIImageWriterContext(JNIEnv* env, jobject weakThiz, jclass clazz) :
94    mWeakThiz(env->NewGlobalRef(weakThiz)),
95    mClazz((jclass)env->NewGlobalRef(clazz)),
96    mFormat(0),
97    mWidth(-1),
98    mHeight(-1) {
99}
100
101JNIImageWriterContext::~JNIImageWriterContext() {
102    ALOGV("%s", __FUNCTION__);
103    bool needsDetach = false;
104    JNIEnv* env = getJNIEnv(&needsDetach);
105    if (env != NULL) {
106        env->DeleteGlobalRef(mWeakThiz);
107        env->DeleteGlobalRef(mClazz);
108    } else {
109        ALOGW("leaking JNI object references");
110    }
111    if (needsDetach) {
112        detachJNI();
113    }
114
115    mProducer.clear();
116}
117
118JNIEnv* JNIImageWriterContext::getJNIEnv(bool* needsDetach) {
119    ALOGV("%s", __FUNCTION__);
120    LOG_ALWAYS_FATAL_IF(needsDetach == NULL, "needsDetach is null!!!");
121    *needsDetach = false;
122    JNIEnv* env = AndroidRuntime::getJNIEnv();
123    if (env == NULL) {
124        JavaVMAttachArgs args = {JNI_VERSION_1_4, NULL, NULL};
125        JavaVM* vm = AndroidRuntime::getJavaVM();
126        int result = vm->AttachCurrentThread(&env, (void*) &args);
127        if (result != JNI_OK) {
128            ALOGE("thread attach failed: %#x", result);
129            return NULL;
130        }
131        *needsDetach = true;
132    }
133    return env;
134}
135
136void JNIImageWriterContext::detachJNI() {
137    ALOGV("%s", __FUNCTION__);
138    JavaVM* vm = AndroidRuntime::getJavaVM();
139    int result = vm->DetachCurrentThread();
140    if (result != JNI_OK) {
141        ALOGE("thread detach failed: %#x", result);
142    }
143}
144
145void JNIImageWriterContext::onBufferReleased() {
146    ALOGV("%s: buffer released", __FUNCTION__);
147    bool needsDetach = false;
148    JNIEnv* env = getJNIEnv(&needsDetach);
149    if (env != NULL) {
150        // Detach the buffer every time when a buffer consumption is done,
151        // need let this callback give a BufferItem, then only detach if it was attached to this
152        // Writer. Do the detach unconditionally for opaque format now. see b/19977520
153        if (mFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
154            sp<Fence> fence;
155            sp<GraphicBuffer> buffer;
156            ALOGV("%s: One buffer is detached", __FUNCTION__);
157            mProducer->detachNextBuffer(&buffer, &fence);
158        }
159
160        env->CallStaticVoidMethod(mClazz, gImageWriterClassInfo.postEventFromNative, mWeakThiz);
161    } else {
162        ALOGW("onBufferReleased event will not posted");
163    }
164
165    if (needsDetach) {
166        detachJNI();
167    }
168}
169
170// ----------------------------------------------------------------------------
171
172extern "C" {
173
174// -------------------------------Private method declarations--------------
175
176static void Image_setNativeContext(JNIEnv* env, jobject thiz,
177        sp<GraphicBuffer> buffer, int fenceFd);
178static void Image_getNativeContext(JNIEnv* env, jobject thiz,
179        GraphicBuffer** buffer, int* fenceFd);
180static void Image_unlockIfLocked(JNIEnv* env, jobject thiz);
181
182// --------------------------ImageWriter methods---------------------------------------
183
184static void ImageWriter_classInit(JNIEnv* env, jclass clazz) {
185    ALOGV("%s:", __FUNCTION__);
186    jclass imageClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage");
187    LOG_ALWAYS_FATAL_IF(imageClazz == NULL,
188            "can't find android/media/ImageWriter$WriterSurfaceImage");
189    gSurfaceImageClassInfo.mNativeBuffer = env->GetFieldID(
190            imageClazz, IMAGE_BUFFER_JNI_ID, "J");
191    LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeBuffer == NULL,
192            "can't find android/media/ImageWriter$WriterSurfaceImage.%s", IMAGE_BUFFER_JNI_ID);
193
194    gSurfaceImageClassInfo.mNativeFenceFd = env->GetFieldID(
195            imageClazz, "mNativeFenceFd", "I");
196    LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mNativeFenceFd == NULL,
197            "can't find android/media/ImageWriter$WriterSurfaceImage.mNativeFenceFd");
198
199    gSurfaceImageClassInfo.mPlanes = env->GetFieldID(
200            imageClazz, "mPlanes", "[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;");
201    LOG_ALWAYS_FATAL_IF(gSurfaceImageClassInfo.mPlanes == NULL,
202            "can't find android/media/ImageWriter$WriterSurfaceImage.mPlanes");
203
204    gImageWriterClassInfo.postEventFromNative = env->GetStaticMethodID(
205            clazz, "postEventFromNative", "(Ljava/lang/Object;)V");
206    LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.postEventFromNative == NULL,
207                        "can't find android/media/ImageWriter.postEventFromNative");
208
209    gImageWriterClassInfo.mWriterFormat = env->GetFieldID(
210            clazz, "mWriterFormat", "I");
211    LOG_ALWAYS_FATAL_IF(gImageWriterClassInfo.mWriterFormat == NULL,
212                        "can't find android/media/ImageWriter.mWriterFormat");
213
214    jclass planeClazz = env->FindClass("android/media/ImageWriter$WriterSurfaceImage$SurfacePlane");
215    LOG_ALWAYS_FATAL_IF(planeClazz == NULL, "Can not find SurfacePlane class");
216    // FindClass only gives a local reference of jclass object.
217    gSurfacePlaneClassInfo.clazz = (jclass) env->NewGlobalRef(planeClazz);
218    gSurfacePlaneClassInfo.ctor = env->GetMethodID(gSurfacePlaneClassInfo.clazz, "<init>",
219            "(Landroid/media/ImageWriter$WriterSurfaceImage;IILjava/nio/ByteBuffer;)V");
220    LOG_ALWAYS_FATAL_IF(gSurfacePlaneClassInfo.ctor == NULL,
221            "Can not find SurfacePlane constructor");
222}
223
224static jlong ImageWriter_init(JNIEnv* env, jobject thiz, jobject weakThiz, jobject jsurface,
225        jint maxImages, jint userFormat) {
226    status_t res;
227
228    ALOGV("%s: maxImages:%d", __FUNCTION__, maxImages);
229
230    sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
231    if (surface == NULL) {
232        jniThrowException(env,
233                "java/lang/IllegalArgumentException",
234                "The surface has been released");
235        return 0;
236     }
237    sp<IGraphicBufferProducer> bufferProducer = surface->getIGraphicBufferProducer();
238
239    jclass clazz = env->GetObjectClass(thiz);
240    if (clazz == NULL) {
241        jniThrowRuntimeException(env, "Can't find android/graphics/ImageWriter");
242        return 0;
243    }
244    sp<JNIImageWriterContext> ctx(new JNIImageWriterContext(env, weakThiz, clazz));
245
246    sp<Surface> producer = new Surface(bufferProducer, /*controlledByApp*/false);
247    ctx->setProducer(producer);
248    /**
249     * NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not connectable
250     * after disconnect. MEDIA or CAMERA are treated the same internally. The producer listener
251     * will be cleared after disconnect call.
252     */
253    producer->connect(/*api*/NATIVE_WINDOW_API_CAMERA, /*listener*/ctx);
254    jlong nativeCtx = reinterpret_cast<jlong>(ctx.get());
255
256    // Get the dimension and format of the producer.
257    sp<ANativeWindow> anw = producer;
258    int32_t width, height, surfaceFormat;
259    if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
260        ALOGE("%s: Query Surface width failed: %s (%d)", __FUNCTION__, strerror(-res), res);
261        jniThrowRuntimeException(env, "Failed to query Surface width");
262        return 0;
263    }
264    ctx->setBufferWidth(width);
265
266    if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) {
267        ALOGE("%s: Query Surface height failed: %s (%d)", __FUNCTION__, strerror(-res), res);
268        jniThrowRuntimeException(env, "Failed to query Surface height");
269        return 0;
270    }
271    ctx->setBufferHeight(height);
272
273    // Query surface format if no valid user format is specified, otherwise, override surface format
274    // with user format.
275    if (userFormat == IMAGE_FORMAT_UNKNOWN) {
276        if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &surfaceFormat)) != OK) {
277            ALOGE("%s: Query Surface format failed: %s (%d)", __FUNCTION__, strerror(-res), res);
278            jniThrowRuntimeException(env, "Failed to query Surface format");
279            return 0;
280        }
281    } else {
282        surfaceFormat = userFormat;
283    }
284    ctx->setBufferFormat(surfaceFormat);
285    env->SetIntField(thiz,
286            gImageWriterClassInfo.mWriterFormat, reinterpret_cast<jint>(surfaceFormat));
287
288    if (!isFormatOpaque(surfaceFormat)) {
289        res = native_window_set_usage(anw.get(), GRALLOC_USAGE_SW_WRITE_OFTEN);
290        if (res != OK) {
291            ALOGE("%s: Configure usage %08x for format %08x failed: %s (%d)",
292                  __FUNCTION__, static_cast<unsigned int>(GRALLOC_USAGE_SW_WRITE_OFTEN),
293                  surfaceFormat, strerror(-res), res);
294            jniThrowRuntimeException(env, "Failed to SW_WRITE_OFTEN configure usage");
295            return 0;
296        }
297    }
298
299    int minUndequeuedBufferCount = 0;
300    res = anw->query(anw.get(),
301                NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &minUndequeuedBufferCount);
302    if (res != OK) {
303        ALOGE("%s: Query producer undequeued buffer count failed: %s (%d)",
304                __FUNCTION__, strerror(-res), res);
305        jniThrowRuntimeException(env, "Query producer undequeued buffer count failed");
306        return 0;
307     }
308
309    size_t totalBufferCount = maxImages + minUndequeuedBufferCount;
310    res = native_window_set_buffer_count(anw.get(), totalBufferCount);
311    if (res != OK) {
312        ALOGE("%s: Set buffer count failed: %s (%d)", __FUNCTION__, strerror(-res), res);
313        jniThrowRuntimeException(env, "Set buffer count failed");
314        return 0;
315    }
316
317    if (ctx != 0) {
318        ctx->incStrong((void*)ImageWriter_init);
319    }
320    return nativeCtx;
321}
322
323static void ImageWriter_dequeueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) {
324    ALOGV("%s", __FUNCTION__);
325    JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
326    if (ctx == NULL || thiz == NULL) {
327        jniThrowException(env, "java/lang/IllegalStateException",
328                "ImageWriterContext is not initialized");
329        return;
330    }
331
332    sp<ANativeWindow> anw = ctx->getProducer();
333    android_native_buffer_t *anb = NULL;
334    int fenceFd = -1;
335    status_t res = anw->dequeueBuffer(anw.get(), &anb, &fenceFd);
336    if (res != OK) {
337        ALOGE("%s: Dequeue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
338        switch (res) {
339            case NO_INIT:
340                jniThrowException(env, "java/lang/IllegalStateException",
341                    "Surface has been abandoned");
342                break;
343            default:
344                // TODO: handle other error cases here.
345                jniThrowRuntimeException(env, "dequeue buffer failed");
346        }
347        return;
348    }
349    // New GraphicBuffer object doesn't own the handle, thus the native buffer
350    // won't be freed when this object is destroyed.
351    sp<GraphicBuffer> buffer(GraphicBuffer::from(anb));
352
353    // Note that:
354    // 1. No need to lock buffer now, will only lock it when the first getPlanes() is called.
355    // 2. Fence will be saved to mNativeFenceFd, and will consumed by lock/queue/cancel buffer
356    //    later.
357    // 3. need use lockAsync here, as it will handle the dequeued fence for us automatically.
358
359    // Finally, set the native info into image object.
360    Image_setNativeContext(env, image, buffer, fenceFd);
361}
362
363static void ImageWriter_close(JNIEnv* env, jobject thiz, jlong nativeCtx) {
364    ALOGV("%s:", __FUNCTION__);
365    JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
366    if (ctx == NULL || thiz == NULL) {
367        // ImageWriter is already closed.
368        return;
369    }
370
371    ANativeWindow* producer = ctx->getProducer();
372    if (producer != NULL) {
373        /**
374         * NATIVE_WINDOW_API_CPU isn't a good choice here, as it makes the bufferQueue not
375         * connectable after disconnect. MEDIA or CAMERA are treated the same internally.
376         * The producer listener will be cleared after disconnect call.
377         */
378        status_t res = native_window_api_disconnect(producer, /*api*/NATIVE_WINDOW_API_CAMERA);
379        /**
380         * This is not an error. if client calling process dies, the window will
381         * also die and all calls to it will return DEAD_OBJECT, thus it's already
382         * "disconnected"
383         */
384        if (res == DEAD_OBJECT) {
385            ALOGW("%s: While disconnecting ImageWriter from native window, the"
386                    " native window died already", __FUNCTION__);
387        } else if (res != OK) {
388            ALOGE("%s: native window disconnect failed: %s (%d)",
389                    __FUNCTION__, strerror(-res), res);
390            jniThrowRuntimeException(env, "Native window disconnect failed");
391            return;
392        }
393    }
394
395    ctx->decStrong((void*)ImageWriter_init);
396}
397
398static void ImageWriter_cancelImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image) {
399    ALOGV("%s", __FUNCTION__);
400    JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
401    if (ctx == NULL || thiz == NULL) {
402        ALOGW("ImageWriter#close called before Image#close, consider calling Image#close first");
403        return;
404    }
405
406    sp<ANativeWindow> anw = ctx->getProducer();
407
408    GraphicBuffer *buffer = NULL;
409    int fenceFd = -1;
410    Image_getNativeContext(env, image, &buffer, &fenceFd);
411    if (buffer == NULL) {
412        // Cancel an already cancelled image is harmless.
413        return;
414    }
415
416    // Unlock the image if it was locked
417    Image_unlockIfLocked(env, image);
418
419    anw->cancelBuffer(anw.get(), buffer, fenceFd);
420
421    Image_setNativeContext(env, image, NULL, -1);
422}
423
424static void ImageWriter_queueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, jobject image,
425        jlong timestampNs, jint left, jint top, jint right, jint bottom) {
426    ALOGV("%s", __FUNCTION__);
427    JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
428    if (ctx == NULL || thiz == NULL) {
429        jniThrowException(env, "java/lang/IllegalStateException",
430                "ImageWriterContext is not initialized");
431        return;
432    }
433
434    status_t res = OK;
435    sp<ANativeWindow> anw = ctx->getProducer();
436
437    GraphicBuffer *buffer = NULL;
438    int fenceFd = -1;
439    Image_getNativeContext(env, image, &buffer, &fenceFd);
440    if (buffer == NULL) {
441        jniThrowException(env, "java/lang/IllegalStateException",
442                "Image is not initialized");
443        return;
444    }
445
446    // Unlock image if it was locked.
447    Image_unlockIfLocked(env, image);
448
449    // Set timestamp
450    ALOGV("timestamp to be queued: %" PRId64, timestampNs);
451    res = native_window_set_buffers_timestamp(anw.get(), timestampNs);
452    if (res != OK) {
453        jniThrowRuntimeException(env, "Set timestamp failed");
454        return;
455    }
456
457    // Set crop
458    android_native_rect_t cropRect;
459    cropRect.left = left;
460    cropRect.top = top;
461    cropRect.right = right;
462    cropRect.bottom = bottom;
463    res = native_window_set_crop(anw.get(), &cropRect);
464    if (res != OK) {
465        jniThrowRuntimeException(env, "Set crop rect failed");
466        return;
467    }
468
469    // Finally, queue input buffer
470    res = anw->queueBuffer(anw.get(), buffer, fenceFd);
471    if (res != OK) {
472        ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
473        switch (res) {
474            case NO_INIT:
475                jniThrowException(env, "java/lang/IllegalStateException",
476                    "Surface has been abandoned");
477                break;
478            default:
479                // TODO: handle other error cases here.
480                jniThrowRuntimeException(env, "Queue input buffer failed");
481        }
482        return;
483    }
484
485    // Clear the image native context: end of this image's lifecycle in public API.
486    Image_setNativeContext(env, image, NULL, -1);
487}
488
489static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nativeCtx,
490        jlong nativeBuffer, jint imageFormat, jlong timestampNs, jint left, jint top,
491        jint right, jint bottom) {
492    ALOGV("%s", __FUNCTION__);
493    JNIImageWriterContext* const ctx = reinterpret_cast<JNIImageWriterContext *>(nativeCtx);
494    if (ctx == NULL || thiz == NULL) {
495        jniThrowException(env, "java/lang/IllegalStateException",
496                "ImageWriterContext is not initialized");
497        return -1;
498    }
499
500    sp<Surface> surface = ctx->getProducer();
501    status_t res = OK;
502    if (isFormatOpaque(imageFormat) != isFormatOpaque(ctx->getBufferFormat())) {
503        jniThrowException(env, "java/lang/IllegalStateException",
504                "Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa");
505        return -1;
506    }
507
508    // Image is guaranteed to be from ImageReader at this point, so it is safe to
509    // cast to BufferItem pointer.
510    BufferItem* buffer = reinterpret_cast<BufferItem*>(nativeBuffer);
511    if (buffer == NULL) {
512        jniThrowException(env, "java/lang/IllegalStateException",
513                "Image is not initialized or already closed");
514        return -1;
515    }
516
517    // Step 1. Attach Image
518    res = surface->attachBuffer(buffer->mGraphicBuffer.get());
519    if (res != OK) {
520        ALOGE("Attach image failed: %s (%d)", strerror(-res), res);
521        switch (res) {
522            case NO_INIT:
523                jniThrowException(env, "java/lang/IllegalStateException",
524                    "Surface has been abandoned");
525                break;
526            default:
527                // TODO: handle other error cases here.
528                jniThrowRuntimeException(env, "nativeAttachImage failed!!!");
529        }
530        return res;
531    }
532    sp < ANativeWindow > anw = surface;
533
534    // Step 2. Set timestamp and crop. Note that we do not need unlock the image because
535    // it was not locked.
536    ALOGV("timestamp to be queued: %" PRId64, timestampNs);
537    res = native_window_set_buffers_timestamp(anw.get(), timestampNs);
538    if (res != OK) {
539        jniThrowRuntimeException(env, "Set timestamp failed");
540        return res;
541    }
542
543    android_native_rect_t cropRect;
544    cropRect.left = left;
545    cropRect.top = top;
546    cropRect.right = right;
547    cropRect.bottom = bottom;
548    res = native_window_set_crop(anw.get(), &cropRect);
549    if (res != OK) {
550        jniThrowRuntimeException(env, "Set crop rect failed");
551        return res;
552    }
553
554    // Step 3. Queue Image.
555    res = anw->queueBuffer(anw.get(), buffer->mGraphicBuffer.get(), /*fenceFd*/
556            -1);
557    if (res != OK) {
558        ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
559        switch (res) {
560            case NO_INIT:
561                jniThrowException(env, "java/lang/IllegalStateException",
562                    "Surface has been abandoned");
563                break;
564            default:
565                // TODO: handle other error cases here.
566                jniThrowRuntimeException(env, "Queue input buffer failed");
567        }
568        return res;
569    }
570
571    // Do not set the image native context. Since it would overwrite the existing native context
572    // of the image that is from ImageReader, the subsequent image close will run into issues.
573
574    return res;
575}
576
577// --------------------------Image methods---------------------------------------
578
579static void Image_getNativeContext(JNIEnv* env, jobject thiz,
580        GraphicBuffer** buffer, int* fenceFd) {
581    ALOGV("%s", __FUNCTION__);
582    if (buffer != NULL) {
583        GraphicBuffer *gb = reinterpret_cast<GraphicBuffer *>
584                  (env->GetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer));
585        *buffer = gb;
586    }
587
588    if (fenceFd != NULL) {
589        *fenceFd = reinterpret_cast<jint>(env->GetIntField(
590                thiz, gSurfaceImageClassInfo.mNativeFenceFd));
591    }
592}
593
594static void Image_setNativeContext(JNIEnv* env, jobject thiz,
595        sp<GraphicBuffer> buffer, int fenceFd) {
596    ALOGV("%s:", __FUNCTION__);
597    GraphicBuffer* p = NULL;
598    Image_getNativeContext(env, thiz, &p, /*fenceFd*/NULL);
599    if (buffer != 0) {
600        buffer->incStrong((void*)Image_setNativeContext);
601    }
602    if (p) {
603        p->decStrong((void*)Image_setNativeContext);
604    }
605    env->SetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer,
606            reinterpret_cast<jlong>(buffer.get()));
607
608    env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
609}
610
611static void Image_unlockIfLocked(JNIEnv* env, jobject thiz) {
612    ALOGV("%s", __FUNCTION__);
613    GraphicBuffer* buffer;
614    Image_getNativeContext(env, thiz, &buffer, NULL);
615    if (buffer == NULL) {
616        jniThrowException(env, "java/lang/IllegalStateException",
617                "Image is not initialized");
618        return;
619    }
620
621    // Is locked?
622    bool isLocked = false;
623    jobject planes = NULL;
624    if (!isFormatOpaque(buffer->getPixelFormat())) {
625        planes = env->GetObjectField(thiz, gSurfaceImageClassInfo.mPlanes);
626    }
627    isLocked = (planes != NULL);
628    if (isLocked) {
629        // no need to use fence here, as we it will be consumed by either cancel or queue buffer.
630        status_t res = buffer->unlock();
631        if (res != OK) {
632            jniThrowRuntimeException(env, "unlock buffer failed");
633        }
634        ALOGV("Successfully unlocked the image");
635    }
636}
637
638static jint Image_getWidth(JNIEnv* env, jobject thiz) {
639    ALOGV("%s", __FUNCTION__);
640    GraphicBuffer* buffer;
641    Image_getNativeContext(env, thiz, &buffer, NULL);
642    if (buffer == NULL) {
643        jniThrowException(env, "java/lang/IllegalStateException",
644                "Image is not initialized");
645        return -1;
646    }
647
648    return buffer->getWidth();
649}
650
651static jint Image_getHeight(JNIEnv* env, jobject thiz) {
652    ALOGV("%s", __FUNCTION__);
653    GraphicBuffer* buffer;
654    Image_getNativeContext(env, thiz, &buffer, NULL);
655    if (buffer == NULL) {
656        jniThrowException(env, "java/lang/IllegalStateException",
657                "Image is not initialized");
658        return -1;
659    }
660
661    return buffer->getHeight();
662}
663
664static jint Image_getFormat(JNIEnv* env, jobject thiz) {
665    ALOGV("%s", __FUNCTION__);
666    GraphicBuffer* buffer;
667    Image_getNativeContext(env, thiz, &buffer, NULL);
668    if (buffer == NULL) {
669        jniThrowException(env, "java/lang/IllegalStateException",
670                "Image is not initialized");
671        return 0;
672    }
673
674    // ImageWriter doesn't support data space yet, assuming it is unknown.
675    PublicFormat publicFmt = android_view_Surface_mapHalFormatDataspaceToPublicFormat(
676            buffer->getPixelFormat(), HAL_DATASPACE_UNKNOWN);
677    return static_cast<jint>(publicFmt);
678}
679
680static void Image_setFenceFd(JNIEnv* env, jobject thiz, int fenceFd) {
681    ALOGV("%s:", __FUNCTION__);
682    env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
683}
684
685static void Image_getLockedImage(JNIEnv* env, jobject thiz, LockedImage *image) {
686    ALOGV("%s", __FUNCTION__);
687    GraphicBuffer* buffer;
688    int fenceFd = -1;
689    Image_getNativeContext(env, thiz, &buffer, &fenceFd);
690    if (buffer == NULL) {
691        jniThrowException(env, "java/lang/IllegalStateException",
692                "Image is not initialized");
693        return;
694    }
695
696    // ImageWriter doesn't use crop by itself, app sets it, use the no crop version.
697    const Rect noCrop(buffer->width, buffer->height);
698    status_t res = lockImageFromBuffer(
699            buffer, GRALLOC_USAGE_SW_WRITE_OFTEN, noCrop, fenceFd, image);
700    // Clear the fenceFd as it is already consumed by lock call.
701    Image_setFenceFd(env, thiz, /*fenceFd*/-1);
702    if (res != OK) {
703        jniThrowExceptionFmt(env, "java/lang/RuntimeException",
704                "lock buffer failed for format 0x%x",
705                buffer->getPixelFormat());
706        return;
707    }
708
709    ALOGV("%s: Successfully locked the image", __FUNCTION__);
710    // crop, transform, scalingMode, timestamp, and frameNumber should be set by producer,
711    // and we don't set them here.
712}
713
714static void Image_getLockedImageInfo(JNIEnv* env, LockedImage* buffer, int idx,
715        int32_t writerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
716    ALOGV("%s", __FUNCTION__);
717
718    status_t res = getLockedImageInfo(buffer, idx, writerFormat, base, size,
719            pixelStride, rowStride);
720    if (res != OK) {
721        jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
722                             "Pixel format: 0x%x is unsupported", buffer->flexFormat);
723    }
724}
725
726static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz,
727        int numPlanes, int writerFormat) {
728    ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes);
729    int rowStride, pixelStride;
730    uint8_t *pData;
731    uint32_t dataSize;
732    jobject byteBuffer;
733
734    int format = Image_getFormat(env, thiz);
735    if (isFormatOpaque(format) && numPlanes > 0) {
736        String8 msg;
737        msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
738                " must be 0", format, numPlanes);
739        jniThrowException(env, "java/lang/IllegalArgumentException", msg.string());
740        return NULL;
741    }
742
743    jobjectArray surfacePlanes = env->NewObjectArray(numPlanes, gSurfacePlaneClassInfo.clazz,
744            /*initial_element*/NULL);
745    if (surfacePlanes == NULL) {
746        jniThrowRuntimeException(env, "Failed to create SurfacePlane arrays,"
747                " probably out of memory");
748        return NULL;
749    }
750    if (isFormatOpaque(format)) {
751        return surfacePlanes;
752    }
753
754    // Buildup buffer info: rowStride, pixelStride and byteBuffers.
755    LockedImage lockedImg = LockedImage();
756    Image_getLockedImage(env, thiz, &lockedImg);
757
758    // Create all SurfacePlanes
759    PublicFormat publicWriterFormat = static_cast<PublicFormat>(writerFormat);
760    writerFormat = android_view_Surface_mapPublicFormatToHalFormat(publicWriterFormat);
761    for (int i = 0; i < numPlanes; i++) {
762        Image_getLockedImageInfo(env, &lockedImg, i, writerFormat,
763                &pData, &dataSize, &pixelStride, &rowStride);
764        byteBuffer = env->NewDirectByteBuffer(pData, dataSize);
765        if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) {
766            jniThrowException(env, "java/lang/IllegalStateException",
767                    "Failed to allocate ByteBuffer");
768            return NULL;
769        }
770
771        // Finally, create this SurfacePlane.
772        jobject surfacePlane = env->NewObject(gSurfacePlaneClassInfo.clazz,
773                    gSurfacePlaneClassInfo.ctor, thiz, rowStride, pixelStride, byteBuffer);
774        env->SetObjectArrayElement(surfacePlanes, i, surfacePlane);
775    }
776
777    return surfacePlanes;
778}
779
780} // extern "C"
781
782// ----------------------------------------------------------------------------
783
784static JNINativeMethod gImageWriterMethods[] = {
785    {"nativeClassInit",         "()V",                        (void*)ImageWriter_classInit },
786    {"nativeInit",              "(Ljava/lang/Object;Landroid/view/Surface;II)J",
787                                                              (void*)ImageWriter_init },
788    {"nativeClose",              "(J)V",                      (void*)ImageWriter_close },
789    {"nativeAttachAndQueueImage", "(JJIJIIII)I",          (void*)ImageWriter_attachAndQueueImage },
790    {"nativeDequeueInputImage", "(JLandroid/media/Image;)V",  (void*)ImageWriter_dequeueImage },
791    {"nativeQueueInputImage",   "(JLandroid/media/Image;JIIII)V",  (void*)ImageWriter_queueImage },
792    {"cancelImage",             "(JLandroid/media/Image;)V",   (void*)ImageWriter_cancelImage },
793};
794
795static JNINativeMethod gImageMethods[] = {
796    {"nativeCreatePlanes",      "(II)[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;",
797                                                              (void*)Image_createSurfacePlanes },
798    {"nativeGetWidth",         "()I",                         (void*)Image_getWidth },
799    {"nativeGetHeight",        "()I",                         (void*)Image_getHeight },
800    {"nativeGetFormat",        "()I",                         (void*)Image_getFormat },
801};
802
803int register_android_media_ImageWriter(JNIEnv *env) {
804
805    int ret1 = AndroidRuntime::registerNativeMethods(env,
806                   "android/media/ImageWriter", gImageWriterMethods, NELEM(gImageWriterMethods));
807
808    int ret2 = AndroidRuntime::registerNativeMethods(env,
809                   "android/media/ImageWriter$WriterSurfaceImage", gImageMethods, NELEM(gImageMethods));
810
811    return (ret1 || ret2);
812}
813