android_media_ImageWriter.cpp revision 916d8ac650865cd05808d48bad68b69bebbc95ab
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)) {
503        // TODO: need implement, see b/19962027
504        jniThrowRuntimeException(env,
505                "nativeAttachImage for non-opaque image is not implement yet!!!");
506        return -1;
507    }
508
509    if (!isFormatOpaque(ctx->getBufferFormat())) {
510        jniThrowException(env, "java/lang/IllegalStateException",
511                "Trying to attach an opaque image into a non-opaque ImageWriter");
512        return -1;
513    }
514
515    // Image is guaranteed to be from ImageReader at this point, so it is safe to
516    // cast to BufferItem pointer.
517    BufferItem* opaqueBuffer = reinterpret_cast<BufferItem*>(nativeBuffer);
518    if (opaqueBuffer == NULL) {
519        jniThrowException(env, "java/lang/IllegalStateException",
520                "Image is not initialized or already closed");
521        return -1;
522    }
523
524    // Step 1. Attach Image
525    res = surface->attachBuffer(opaqueBuffer->mGraphicBuffer.get());
526    if (res != OK) {
527        ALOGE("Attach image failed: %s (%d)", strerror(-res), res);
528        switch (res) {
529            case NO_INIT:
530                jniThrowException(env, "java/lang/IllegalStateException",
531                    "Surface has been abandoned");
532                break;
533            default:
534                // TODO: handle other error cases here.
535                jniThrowRuntimeException(env, "nativeAttachImage failed!!!");
536        }
537        return res;
538    }
539    sp < ANativeWindow > anw = surface;
540
541    // Step 2. Set timestamp and crop. Note that we do not need unlock the image because
542    // it was not locked.
543    ALOGV("timestamp to be queued: %" PRId64, timestampNs);
544    res = native_window_set_buffers_timestamp(anw.get(), timestampNs);
545    if (res != OK) {
546        jniThrowRuntimeException(env, "Set timestamp failed");
547        return res;
548    }
549
550    android_native_rect_t cropRect;
551    cropRect.left = left;
552    cropRect.top = top;
553    cropRect.right = right;
554    cropRect.bottom = bottom;
555    res = native_window_set_crop(anw.get(), &cropRect);
556    if (res != OK) {
557        jniThrowRuntimeException(env, "Set crop rect failed");
558        return res;
559    }
560
561    // Step 3. Queue Image.
562    res = anw->queueBuffer(anw.get(), opaqueBuffer->mGraphicBuffer.get(), /*fenceFd*/
563            -1);
564    if (res != OK) {
565        ALOGE("%s: Queue buffer failed: %s (%d)", __FUNCTION__, strerror(-res), res);
566        switch (res) {
567            case NO_INIT:
568                jniThrowException(env, "java/lang/IllegalStateException",
569                    "Surface has been abandoned");
570                break;
571            default:
572                // TODO: handle other error cases here.
573                jniThrowRuntimeException(env, "Queue input buffer failed");
574        }
575        return res;
576    }
577
578    // Do not set the image native context. Since it would overwrite the existing native context
579    // of the image that is from ImageReader, the subsequent image close will run into issues.
580
581    return res;
582}
583
584// --------------------------Image methods---------------------------------------
585
586static void Image_getNativeContext(JNIEnv* env, jobject thiz,
587        GraphicBuffer** buffer, int* fenceFd) {
588    ALOGV("%s", __FUNCTION__);
589    if (buffer != NULL) {
590        GraphicBuffer *gb = reinterpret_cast<GraphicBuffer *>
591                  (env->GetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer));
592        *buffer = gb;
593    }
594
595    if (fenceFd != NULL) {
596        *fenceFd = reinterpret_cast<jint>(env->GetIntField(
597                thiz, gSurfaceImageClassInfo.mNativeFenceFd));
598    }
599}
600
601static void Image_setNativeContext(JNIEnv* env, jobject thiz,
602        sp<GraphicBuffer> buffer, int fenceFd) {
603    ALOGV("%s:", __FUNCTION__);
604    GraphicBuffer* p = NULL;
605    Image_getNativeContext(env, thiz, &p, /*fenceFd*/NULL);
606    if (buffer != 0) {
607        buffer->incStrong((void*)Image_setNativeContext);
608    }
609    if (p) {
610        p->decStrong((void*)Image_setNativeContext);
611    }
612    env->SetLongField(thiz, gSurfaceImageClassInfo.mNativeBuffer,
613            reinterpret_cast<jlong>(buffer.get()));
614
615    env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
616}
617
618static void Image_unlockIfLocked(JNIEnv* env, jobject thiz) {
619    ALOGV("%s", __FUNCTION__);
620    GraphicBuffer* buffer;
621    Image_getNativeContext(env, thiz, &buffer, NULL);
622    if (buffer == NULL) {
623        jniThrowException(env, "java/lang/IllegalStateException",
624                "Image is not initialized");
625        return;
626    }
627
628    // Is locked?
629    bool isLocked = false;
630    jobject planes = NULL;
631    if (!isFormatOpaque(buffer->getPixelFormat())) {
632        planes = env->GetObjectField(thiz, gSurfaceImageClassInfo.mPlanes);
633    }
634    isLocked = (planes != NULL);
635    if (isLocked) {
636        // no need to use fence here, as we it will be consumed by either cancel or queue buffer.
637        status_t res = buffer->unlock();
638        if (res != OK) {
639            jniThrowRuntimeException(env, "unlock buffer failed");
640        }
641        ALOGV("Successfully unlocked the image");
642    }
643}
644
645static jint Image_getWidth(JNIEnv* env, jobject thiz) {
646    ALOGV("%s", __FUNCTION__);
647    GraphicBuffer* buffer;
648    Image_getNativeContext(env, thiz, &buffer, NULL);
649    if (buffer == NULL) {
650        jniThrowException(env, "java/lang/IllegalStateException",
651                "Image is not initialized");
652        return -1;
653    }
654
655    return buffer->getWidth();
656}
657
658static jint Image_getHeight(JNIEnv* env, jobject thiz) {
659    ALOGV("%s", __FUNCTION__);
660    GraphicBuffer* buffer;
661    Image_getNativeContext(env, thiz, &buffer, NULL);
662    if (buffer == NULL) {
663        jniThrowException(env, "java/lang/IllegalStateException",
664                "Image is not initialized");
665        return -1;
666    }
667
668    return buffer->getHeight();
669}
670
671static jint Image_getFormat(JNIEnv* env, jobject thiz) {
672    ALOGV("%s", __FUNCTION__);
673    GraphicBuffer* buffer;
674    Image_getNativeContext(env, thiz, &buffer, NULL);
675    if (buffer == NULL) {
676        jniThrowException(env, "java/lang/IllegalStateException",
677                "Image is not initialized");
678        return 0;
679    }
680
681    // ImageWriter doesn't support data space yet, assuming it is unknown.
682    PublicFormat publicFmt = android_view_Surface_mapHalFormatDataspaceToPublicFormat(
683            buffer->getPixelFormat(), HAL_DATASPACE_UNKNOWN);
684    return static_cast<jint>(publicFmt);
685}
686
687static void Image_setFenceFd(JNIEnv* env, jobject thiz, int fenceFd) {
688    ALOGV("%s:", __FUNCTION__);
689    env->SetIntField(thiz, gSurfaceImageClassInfo.mNativeFenceFd, reinterpret_cast<jint>(fenceFd));
690}
691
692static void Image_getLockedImage(JNIEnv* env, jobject thiz, LockedImage *image) {
693    ALOGV("%s", __FUNCTION__);
694    GraphicBuffer* buffer;
695    int fenceFd = -1;
696    Image_getNativeContext(env, thiz, &buffer, &fenceFd);
697    if (buffer == NULL) {
698        jniThrowException(env, "java/lang/IllegalStateException",
699                "Image is not initialized");
700        return;
701    }
702
703    // ImageWriter doesn't use crop by itself, app sets it, use the no crop version.
704    const Rect noCrop(buffer->width, buffer->height);
705    status_t res = lockImageFromBuffer(
706            buffer, GRALLOC_USAGE_SW_WRITE_OFTEN, noCrop, fenceFd, image);
707    // Clear the fenceFd as it is already consumed by lock call.
708    Image_setFenceFd(env, thiz, /*fenceFd*/-1);
709    if (res != OK) {
710        jniThrowExceptionFmt(env, "java/lang/RuntimeException",
711                "lock buffer failed for format 0x%x",
712                buffer->getPixelFormat());
713        return;
714    }
715
716    ALOGV("%s: Successfully locked the image", __FUNCTION__);
717    // crop, transform, scalingMode, timestamp, and frameNumber should be set by producer,
718    // and we don't set them here.
719}
720
721static void Image_getLockedImageInfo(JNIEnv* env, LockedImage* buffer, int idx,
722        int32_t writerFormat, uint8_t **base, uint32_t *size, int *pixelStride, int *rowStride) {
723    ALOGV("%s", __FUNCTION__);
724
725    status_t res = getLockedImageInfo(buffer, idx, writerFormat, base, size,
726            pixelStride, rowStride);
727    if (res != OK) {
728        jniThrowExceptionFmt(env, "java/lang/UnsupportedOperationException",
729                             "Pixel format: 0x%x is unsupported", buffer->flexFormat);
730    }
731}
732
733static jobjectArray Image_createSurfacePlanes(JNIEnv* env, jobject thiz,
734        int numPlanes, int writerFormat) {
735    ALOGV("%s: create SurfacePlane array with size %d", __FUNCTION__, numPlanes);
736    int rowStride, pixelStride;
737    uint8_t *pData;
738    uint32_t dataSize;
739    jobject byteBuffer;
740
741    int format = Image_getFormat(env, thiz);
742    if (isFormatOpaque(format) && numPlanes > 0) {
743        String8 msg;
744        msg.appendFormat("Format 0x%x is opaque, thus not writable, the number of planes (%d)"
745                " must be 0", format, numPlanes);
746        jniThrowException(env, "java/lang/IllegalArgumentException", msg.string());
747        return NULL;
748    }
749
750    jobjectArray surfacePlanes = env->NewObjectArray(numPlanes, gSurfacePlaneClassInfo.clazz,
751            /*initial_element*/NULL);
752    if (surfacePlanes == NULL) {
753        jniThrowRuntimeException(env, "Failed to create SurfacePlane arrays,"
754                " probably out of memory");
755        return NULL;
756    }
757    if (isFormatOpaque(format)) {
758        return surfacePlanes;
759    }
760
761    // Buildup buffer info: rowStride, pixelStride and byteBuffers.
762    LockedImage lockedImg = LockedImage();
763    Image_getLockedImage(env, thiz, &lockedImg);
764
765    // Create all SurfacePlanes
766    PublicFormat publicWriterFormat = static_cast<PublicFormat>(writerFormat);
767    writerFormat = android_view_Surface_mapPublicFormatToHalFormat(publicWriterFormat);
768    for (int i = 0; i < numPlanes; i++) {
769        Image_getLockedImageInfo(env, &lockedImg, i, writerFormat,
770                &pData, &dataSize, &pixelStride, &rowStride);
771        byteBuffer = env->NewDirectByteBuffer(pData, dataSize);
772        if ((byteBuffer == NULL) && (env->ExceptionCheck() == false)) {
773            jniThrowException(env, "java/lang/IllegalStateException",
774                    "Failed to allocate ByteBuffer");
775            return NULL;
776        }
777
778        // Finally, create this SurfacePlane.
779        jobject surfacePlane = env->NewObject(gSurfacePlaneClassInfo.clazz,
780                    gSurfacePlaneClassInfo.ctor, thiz, rowStride, pixelStride, byteBuffer);
781        env->SetObjectArrayElement(surfacePlanes, i, surfacePlane);
782    }
783
784    return surfacePlanes;
785}
786
787} // extern "C"
788
789// ----------------------------------------------------------------------------
790
791static JNINativeMethod gImageWriterMethods[] = {
792    {"nativeClassInit",         "()V",                        (void*)ImageWriter_classInit },
793    {"nativeInit",              "(Ljava/lang/Object;Landroid/view/Surface;II)J",
794                                                              (void*)ImageWriter_init },
795    {"nativeClose",              "(J)V",                      (void*)ImageWriter_close },
796    {"nativeAttachAndQueueImage", "(JJIJIIII)I",          (void*)ImageWriter_attachAndQueueImage },
797    {"nativeDequeueInputImage", "(JLandroid/media/Image;)V",  (void*)ImageWriter_dequeueImage },
798    {"nativeQueueInputImage",   "(JLandroid/media/Image;JIIII)V",  (void*)ImageWriter_queueImage },
799    {"cancelImage",             "(JLandroid/media/Image;)V",   (void*)ImageWriter_cancelImage },
800};
801
802static JNINativeMethod gImageMethods[] = {
803    {"nativeCreatePlanes",      "(II)[Landroid/media/ImageWriter$WriterSurfaceImage$SurfacePlane;",
804                                                              (void*)Image_createSurfacePlanes },
805    {"nativeGetWidth",         "()I",                         (void*)Image_getWidth },
806    {"nativeGetHeight",        "()I",                         (void*)Image_getHeight },
807    {"nativeGetFormat",        "()I",                         (void*)Image_getFormat },
808};
809
810int register_android_media_ImageWriter(JNIEnv *env) {
811
812    int ret1 = AndroidRuntime::registerNativeMethods(env,
813                   "android/media/ImageWriter", gImageWriterMethods, NELEM(gImageWriterMethods));
814
815    int ret2 = AndroidRuntime::registerNativeMethods(env,
816                   "android/media/ImageWriter$WriterSurfaceImage", gImageMethods, NELEM(gImageMethods));
817
818    return (ret1 || ret2);
819}
820
821