android_hardware_camera2_legacy_LegacyCameraDevice.cpp revision db2b5f447254e1f2248024879648e36211cb4e33
1/*
2 * Copyright (C) 2014 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_TAG "Legacy-CameraDevice-JNI"
18// #define LOG_NDEBUG 0
19#include <utils/Log.h>
20#include <utils/Errors.h>
21#include <utils/Trace.h>
22#include <camera/CameraUtils.h>
23
24#include "jni.h"
25#include "JNIHelp.h"
26#include "core_jni_helpers.h"
27#include "android_runtime/android_view_Surface.h"
28#include "android_runtime/android_graphics_SurfaceTexture.h"
29
30#include <gui/Surface.h>
31#include <gui/IGraphicBufferProducer.h>
32#include <ui/GraphicBuffer.h>
33#include <system/window.h>
34#include <hardware/camera3.h>
35#include <system/camera_metadata.h>
36
37#include <stdint.h>
38#include <inttypes.h>
39
40using namespace android;
41
42// fully-qualified class name
43#define CAMERA_DEVICE_CLASS_NAME "android/hardware/camera2/legacy/LegacyCameraDevice"
44#define CAMERA_DEVICE_BUFFER_SLACK  3
45#define DONT_CARE 0
46
47#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
48
49#define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) )
50
51/**
52 * Convert from RGB 888 to Y'CbCr using the conversion specified in JFIF v1.02
53 */
54static void rgbToYuv420(uint8_t* rgbBuf, size_t width, size_t height, uint8_t* yPlane,
55        uint8_t* crPlane, uint8_t* cbPlane, size_t chromaStep, size_t yStride, size_t chromaStride) {
56    uint8_t R, G, B;
57    size_t index = 0;
58    for (size_t j = 0; j < height; j++) {
59        uint8_t* cr = crPlane;
60        uint8_t* cb = cbPlane;
61        uint8_t* y = yPlane;
62        bool jEven = (j & 1) == 0;
63        for (size_t i = 0; i < width; i++) {
64            R = rgbBuf[index++];
65            G = rgbBuf[index++];
66            B = rgbBuf[index++];
67            *y++ = (77 * R + 150 * G +  29 * B) >> 8;
68            if (jEven && (i & 1) == 0) {
69                *cb = (( -43 * R - 85 * G + 128 * B) >> 8) + 128;
70                *cr = (( 128 * R - 107 * G - 21 * B) >> 8) + 128;
71                cr += chromaStep;
72                cb += chromaStep;
73            }
74            // Skip alpha
75            index++;
76        }
77        yPlane += yStride;
78        if (jEven) {
79            crPlane += chromaStride;
80            cbPlane += chromaStride;
81        }
82    }
83}
84
85static void rgbToYuv420(uint8_t* rgbBuf, size_t width, size_t height, android_ycbcr* ycbcr) {
86    size_t cStep = ycbcr->chroma_step;
87    size_t cStride = ycbcr->cstride;
88    size_t yStride = ycbcr->ystride;
89    ALOGV("%s: yStride is: %zu, cStride is: %zu, cStep is: %zu", __FUNCTION__, yStride, cStride,
90            cStep);
91    rgbToYuv420(rgbBuf, width, height, reinterpret_cast<uint8_t*>(ycbcr->y),
92            reinterpret_cast<uint8_t*>(ycbcr->cr), reinterpret_cast<uint8_t*>(ycbcr->cb),
93            cStep, yStride, cStride);
94}
95
96static status_t configureSurface(const sp<ANativeWindow>& anw,
97                                 int32_t width,
98                                 int32_t height,
99                                 int32_t pixelFmt,
100                                 int32_t maxBufferSlack) {
101    status_t err = NO_ERROR;
102    err = native_window_set_buffers_dimensions(anw.get(), width, height);
103    if (err != NO_ERROR) {
104        ALOGE("%s: Failed to set native window buffer dimensions, error %s (%d).", __FUNCTION__,
105                strerror(-err), err);
106        return err;
107    }
108
109    err = native_window_set_buffers_format(anw.get(), pixelFmt);
110    if (err != NO_ERROR) {
111        ALOGE("%s: Failed to set native window buffer format, error %s (%d).", __FUNCTION__,
112                strerror(-err), err);
113        return err;
114    }
115
116    err = native_window_set_usage(anw.get(), GRALLOC_USAGE_SW_WRITE_OFTEN);
117    if (err != NO_ERROR) {
118        ALOGE("%s: Failed to set native window usage flag, error %s (%d).", __FUNCTION__,
119                strerror(-err), err);
120        return err;
121    }
122
123    int minUndequeuedBuffers;
124    err = anw.get()->query(anw.get(),
125            NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
126            &minUndequeuedBuffers);
127    if (err != NO_ERROR) {
128        ALOGE("%s: Failed to get native window min undequeued buffers, error %s (%d).",
129                __FUNCTION__, strerror(-err), err);
130        return err;
131    }
132
133    ALOGV("%s: Setting buffer count to %d, size to (%dx%d), fmt (0x%x)", __FUNCTION__,
134          maxBufferSlack + 1 + minUndequeuedBuffers,
135          width, height, pixelFmt);
136    err = native_window_set_buffer_count(anw.get(), maxBufferSlack + 1 + minUndequeuedBuffers);
137    if (err != NO_ERROR) {
138        ALOGE("%s: Failed to set native window buffer count, error %s (%d).", __FUNCTION__,
139                strerror(-err), err);
140        return err;
141    }
142    return NO_ERROR;
143}
144
145/**
146 * Produce a frame in the given surface.
147 *
148 * Args:
149 *    anw - a surface to produce a frame in.
150 *    pixelBuffer - image buffer to generate a frame from.
151 *    width - width of the pixelBuffer in pixels.
152 *    height - height of the pixelBuffer in pixels.
153 *    pixelFmt - format of the pixelBuffer, one of:
154 *               HAL_PIXEL_FORMAT_YCrCb_420_SP,
155 *               HAL_PIXEL_FORMAT_YCbCr_420_888,
156 *               HAL_PIXEL_FORMAT_BLOB
157 *    bufSize - the size of the pixelBuffer in bytes.
158 */
159static status_t produceFrame(const sp<ANativeWindow>& anw,
160                             uint8_t* pixelBuffer,
161                             int32_t bufWidth, // Width of the pixelBuffer
162                             int32_t bufHeight, // Height of the pixelBuffer
163                             int32_t pixelFmt, // Format of the pixelBuffer
164                             int32_t bufSize) {
165    ATRACE_CALL();
166    status_t err = NO_ERROR;
167    ANativeWindowBuffer* anb;
168    ALOGV("%s: Dequeue buffer from %p %dx%d (fmt=%x, size=%x)",
169            __FUNCTION__, anw.get(), bufWidth, bufHeight, pixelFmt, bufSize);
170
171    if (anw == 0) {
172        ALOGE("%s: anw must not be NULL", __FUNCTION__);
173        return BAD_VALUE;
174    } else if (pixelBuffer == NULL) {
175        ALOGE("%s: pixelBuffer must not be NULL", __FUNCTION__);
176        return BAD_VALUE;
177    } else if (bufWidth < 0) {
178        ALOGE("%s: width must be non-negative", __FUNCTION__);
179        return BAD_VALUE;
180    } else if (bufHeight < 0) {
181        ALOGE("%s: height must be non-negative", __FUNCTION__);
182        return BAD_VALUE;
183    } else if (bufSize < 0) {
184        ALOGE("%s: bufSize must be non-negative", __FUNCTION__);
185        return BAD_VALUE;
186    }
187
188    size_t width = static_cast<size_t>(bufWidth);
189    size_t height = static_cast<size_t>(bufHeight);
190    size_t bufferLength = static_cast<size_t>(bufSize);
191
192    // TODO: Switch to using Surface::lock and Surface::unlockAndPost
193    err = native_window_dequeue_buffer_and_wait(anw.get(), &anb);
194    if (err != NO_ERROR) return err;
195
196    sp<GraphicBuffer> buf(new GraphicBuffer(anb, /*keepOwnership*/false));
197    uint32_t grallocBufWidth = buf->getWidth();
198    uint32_t grallocBufHeight = buf->getHeight();
199    uint32_t grallocBufStride = buf->getStride();
200    if (grallocBufWidth != width || grallocBufHeight != height) {
201        ALOGE("%s: Received gralloc buffer with bad dimensions %" PRIu32 "x%" PRIu32
202                ", expecting dimensions %zu x %zu",  __FUNCTION__, grallocBufWidth,
203                grallocBufHeight, width, height);
204        return BAD_VALUE;
205    }
206
207    int32_t bufFmt = 0;
208    err = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &bufFmt);
209    if (err != NO_ERROR) {
210        ALOGE("%s: Error while querying surface pixel format %s (%d).", __FUNCTION__,
211                strerror(-err), err);
212        return err;
213    }
214
215    uint64_t tmpSize = (pixelFmt == HAL_PIXEL_FORMAT_BLOB) ? grallocBufWidth :
216            4 * grallocBufHeight * grallocBufWidth;
217    if (bufFmt != pixelFmt) {
218        if (bufFmt == HAL_PIXEL_FORMAT_RGBA_8888 && pixelFmt == HAL_PIXEL_FORMAT_BLOB) {
219            ALOGV("%s: Using BLOB to RGBA format override.", __FUNCTION__);
220            tmpSize = 4 * (grallocBufWidth + grallocBufStride * (grallocBufHeight - 1));
221        } else {
222            ALOGW("%s: Format mismatch in produceFrame: expecting format %#" PRIx32
223                    ", but received buffer with format %#" PRIx32, __FUNCTION__, pixelFmt, bufFmt);
224        }
225    }
226
227    if (tmpSize > SIZE_MAX) {
228        ALOGE("%s: Overflow calculating size, buffer with dimens %zu x %zu is absurdly large...",
229                __FUNCTION__, width, height);
230        return BAD_VALUE;
231    }
232
233    size_t totalSizeBytes = tmpSize;
234
235    ALOGV("%s: Pixel format chosen: %x", __FUNCTION__, pixelFmt);
236    switch(pixelFmt) {
237        case HAL_PIXEL_FORMAT_YCrCb_420_SP: {
238            if (bufferLength < totalSizeBytes) {
239                ALOGE("%s: PixelBuffer size %zu too small for given dimensions",
240                        __FUNCTION__, bufferLength);
241                return BAD_VALUE;
242            }
243            uint8_t* img = NULL;
244            ALOGV("%s: Lock buffer from %p for write", __FUNCTION__, anw.get());
245            err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
246            if (err != NO_ERROR) return err;
247
248            uint8_t* yPlane = img;
249            uint8_t* uPlane = img + height * width;
250            uint8_t* vPlane = uPlane + 1;
251            size_t chromaStep = 2;
252            size_t yStride = width;
253            size_t chromaStride = width;
254
255            rgbToYuv420(pixelBuffer, width, height, yPlane,
256                    uPlane, vPlane, chromaStep, yStride, chromaStride);
257            break;
258        }
259        case HAL_PIXEL_FORMAT_YV12: {
260            if (bufferLength < totalSizeBytes) {
261                ALOGE("%s: PixelBuffer size %zu too small for given dimensions",
262                        __FUNCTION__, bufferLength);
263                return BAD_VALUE;
264            }
265
266            if ((width & 1) || (height & 1)) {
267                ALOGE("%s: Dimens %zu x %zu are not divisible by 2.", __FUNCTION__, width, height);
268                return BAD_VALUE;
269            }
270
271            uint8_t* img = NULL;
272            ALOGV("%s: Lock buffer from %p for write", __FUNCTION__, anw.get());
273            err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
274            if (err != NO_ERROR) {
275                ALOGE("%s: Error %s (%d) while locking gralloc buffer for write.", __FUNCTION__,
276                        strerror(-err), err);
277                return err;
278            }
279
280            uint32_t stride = buf->getStride();
281            ALOGV("%s: stride is: %" PRIu32, __FUNCTION__, stride);
282            LOG_ALWAYS_FATAL_IF(stride % 16, "Stride is not 16 pixel aligned %d", stride);
283
284            uint32_t cStride = ALIGN(stride / 2, 16);
285            size_t chromaStep = 1;
286
287            uint8_t* yPlane = img;
288            uint8_t* crPlane = img + static_cast<uint32_t>(height) * stride;
289            uint8_t* cbPlane = crPlane + cStride * static_cast<uint32_t>(height) / 2;
290
291            rgbToYuv420(pixelBuffer, width, height, yPlane,
292                    crPlane, cbPlane, chromaStep, stride, cStride);
293            break;
294        }
295        case HAL_PIXEL_FORMAT_YCbCr_420_888: {
296            // Software writes with YCbCr_420_888 format are unsupported
297            // by the gralloc module for now
298            if (bufferLength < totalSizeBytes) {
299                ALOGE("%s: PixelBuffer size %zu too small for given dimensions",
300                        __FUNCTION__, bufferLength);
301                return BAD_VALUE;
302            }
303            android_ycbcr ycbcr = android_ycbcr();
304            ALOGV("%s: Lock buffer from %p for write", __FUNCTION__, anw.get());
305
306            err = buf->lockYCbCr(GRALLOC_USAGE_SW_WRITE_OFTEN, &ycbcr);
307            if (err != NO_ERROR) {
308                ALOGE("%s: Failed to lock ycbcr buffer, error %s (%d).", __FUNCTION__,
309                        strerror(-err), err);
310                return err;
311            }
312            rgbToYuv420(pixelBuffer, width, height, &ycbcr);
313            break;
314        }
315        case HAL_PIXEL_FORMAT_BLOB: {
316            int8_t* img = NULL;
317            struct camera3_jpeg_blob footer = {
318                .jpeg_blob_id = CAMERA3_JPEG_BLOB_ID,
319                .jpeg_size = (uint32_t)bufferLength
320            };
321
322            size_t totalJpegSize = bufferLength + sizeof(footer);
323            totalJpegSize = (totalJpegSize + 3) & ~0x3; // round up to nearest octonibble
324
325            if (totalJpegSize > totalSizeBytes) {
326                ALOGE("%s: Pixel buffer needs size %zu, cannot fit in gralloc buffer of size %zu",
327                        __FUNCTION__, totalJpegSize, totalSizeBytes);
328                return BAD_VALUE;
329            }
330
331            err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
332            if (err != NO_ERROR) {
333                ALOGE("%s: Failed to lock buffer, error %s (%d).", __FUNCTION__, strerror(-err),
334                        err);
335                return err;
336            }
337
338            memcpy(img, pixelBuffer, bufferLength);
339            memcpy(img + totalSizeBytes - sizeof(footer), &footer, sizeof(footer));
340            break;
341        }
342        default: {
343            ALOGE("%s: Invalid pixel format in produceFrame: %x", __FUNCTION__, pixelFmt);
344            return BAD_VALUE;
345        }
346    }
347
348    ALOGV("%s: Unlock buffer from %p", __FUNCTION__, anw.get());
349    err = buf->unlock();
350    if (err != NO_ERROR) {
351        ALOGE("%s: Failed to unlock buffer, error %s (%d).", __FUNCTION__, strerror(-err), err);
352        return err;
353    }
354
355    ALOGV("%s: Queue buffer to %p", __FUNCTION__, anw.get());
356    err = anw->queueBuffer(anw.get(), buf->getNativeBuffer(), /*fenceFd*/-1);
357    if (err != NO_ERROR) {
358        ALOGE("%s: Failed to queue buffer, error %s (%d).", __FUNCTION__, strerror(-err), err);
359        return err;
360    }
361    return NO_ERROR;
362}
363
364static sp<ANativeWindow> getNativeWindow(JNIEnv* env, jobject surface) {
365    sp<ANativeWindow> anw;
366    if (surface) {
367        anw = android_view_Surface_getNativeWindow(env, surface);
368        if (env->ExceptionCheck()) {
369            return NULL;
370        }
371    } else {
372        jniThrowNullPointerException(env, "surface");
373        return NULL;
374    }
375    if (anw == NULL) {
376        ALOGE("%s: Surface had no valid native window.", __FUNCTION__);
377        return NULL;
378    }
379    return anw;
380}
381
382static sp<ANativeWindow> getNativeWindowFromTexture(JNIEnv* env, jobject surfaceTexture) {
383    sp<ANativeWindow> anw;
384    if (surfaceTexture) {
385        anw = android_SurfaceTexture_getNativeWindow(env, surfaceTexture);
386        if (env->ExceptionCheck()) {
387            return NULL;
388        }
389    } else {
390        jniThrowNullPointerException(env, "surfaceTexture");
391        return NULL;
392    }
393    if (anw == NULL) {
394        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
395                "SurfaceTexture had no valid native window.");
396        return NULL;
397    }
398    return anw;
399}
400
401static sp<Surface> getSurface(JNIEnv* env, jobject surface) {
402    sp<Surface> s;
403    if (surface) {
404        s = android_view_Surface_getSurface(env, surface);
405        if (env->ExceptionCheck()) {
406            return NULL;
407        }
408    } else {
409        jniThrowNullPointerException(env, "surface");
410        return NULL;
411    }
412    if (s == NULL) {
413        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
414                "Surface had no valid native Surface.");
415        return NULL;
416    }
417    return s;
418}
419
420extern "C" {
421
422static jint LegacyCameraDevice_nativeDetectSurfaceType(JNIEnv* env, jobject thiz, jobject surface) {
423    ALOGV("nativeDetectSurfaceType");
424    sp<ANativeWindow> anw;
425    if ((anw = getNativeWindow(env, surface)) == NULL) {
426        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
427        return BAD_VALUE;
428    }
429    int32_t fmt = 0;
430    status_t err = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &fmt);
431    if(err != NO_ERROR) {
432        ALOGE("%s: Error while querying surface pixel format %s (%d).", __FUNCTION__, strerror(-err),
433                err);
434        return err;
435    }
436    return fmt;
437}
438
439static jint LegacyCameraDevice_nativeDetectSurfaceDimens(JNIEnv* env, jobject thiz,
440          jobject surface, jintArray dimens) {
441    ALOGV("nativeGetSurfaceDimens");
442
443    if (dimens == NULL) {
444        ALOGE("%s: Null dimens argument passed to nativeDetectSurfaceDimens", __FUNCTION__);
445        return BAD_VALUE;
446    }
447
448    if (env->GetArrayLength(dimens) < 2) {
449        ALOGE("%s: Invalid length of dimens argument in nativeDetectSurfaceDimens", __FUNCTION__);
450        return BAD_VALUE;
451    }
452
453    sp<ANativeWindow> anw;
454    if ((anw = getNativeWindow(env, surface)) == NULL) {
455        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
456        return BAD_VALUE;
457    }
458    int32_t dimenBuf[2];
459    status_t err = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, dimenBuf);
460    if(err != NO_ERROR) {
461        ALOGE("%s: Error while querying surface width %s (%d).", __FUNCTION__, strerror(-err),
462                err);
463        return err;
464    }
465    err = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, dimenBuf + 1);
466    if(err != NO_ERROR) {
467        ALOGE("%s: Error while querying surface height %s (%d).", __FUNCTION__, strerror(-err),
468                err);
469        return err;
470    }
471    env->SetIntArrayRegion(dimens, /*start*/0, /*length*/ARRAY_SIZE(dimenBuf), dimenBuf);
472    return NO_ERROR;
473}
474
475static jint LegacyCameraDevice_nativeDetectSurfaceUsageFlags(JNIEnv* env, jobject thiz,
476          jobject surface) {
477    ALOGV("nativeDetectSurfaceUsageFlags");
478
479    sp<ANativeWindow> anw;
480    if ((anw = getNativeWindow(env, surface)) == NULL) {
481        jniThrowException(env, "Ljava/lang/UnsupportedOperationException;",
482            "Could not retrieve native window from surface.");
483        return BAD_VALUE;
484    }
485    int32_t usage = 0;
486    status_t err = anw->query(anw.get(), NATIVE_WINDOW_CONSUMER_USAGE_BITS, &usage);
487    if(err != NO_ERROR) {
488        jniThrowException(env, "Ljava/lang/UnsupportedOperationException;",
489            "Error while querying surface usage bits");
490        return err;
491    }
492    return usage;
493}
494
495static jint LegacyCameraDevice_nativeDetectTextureDimens(JNIEnv* env, jobject thiz,
496        jobject surfaceTexture, jintArray dimens) {
497    ALOGV("nativeDetectTextureDimens");
498    sp<ANativeWindow> anw;
499    if ((anw = getNativeWindowFromTexture(env, surfaceTexture)) == NULL) {
500        ALOGE("%s: Could not retrieve native window from SurfaceTexture.", __FUNCTION__);
501        return BAD_VALUE;
502    }
503
504    int32_t dimenBuf[2];
505    status_t err = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, dimenBuf);
506    if(err != NO_ERROR) {
507        ALOGE("%s: Error while querying SurfaceTexture width %s (%d)", __FUNCTION__,
508                strerror(-err), err);
509        return err;
510    }
511
512    err = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, dimenBuf + 1);
513    if(err != NO_ERROR) {
514        ALOGE("%s: Error while querying SurfaceTexture height %s (%d)", __FUNCTION__,
515                strerror(-err), err);
516        return err;
517    }
518
519    env->SetIntArrayRegion(dimens, /*start*/0, /*length*/ARRAY_SIZE(dimenBuf), dimenBuf);
520    if (env->ExceptionCheck()) {
521        return BAD_VALUE;
522    }
523    return NO_ERROR;
524}
525
526static jint LegacyCameraDevice_nativeConfigureSurface(JNIEnv* env, jobject thiz, jobject surface,
527        jint width, jint height, jint pixelFormat) {
528    ALOGV("nativeConfigureSurface");
529    sp<ANativeWindow> anw;
530    if ((anw = getNativeWindow(env, surface)) == NULL) {
531        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
532        return BAD_VALUE;
533    }
534    status_t err = configureSurface(anw, width, height, pixelFormat, CAMERA_DEVICE_BUFFER_SLACK);
535    if (err != NO_ERROR) {
536        ALOGE("%s: Error while configuring surface %s (%d).", __FUNCTION__, strerror(-err), err);
537        return err;
538    }
539    return NO_ERROR;
540}
541
542static jint LegacyCameraDevice_nativeProduceFrame(JNIEnv* env, jobject thiz, jobject surface,
543        jbyteArray pixelBuffer, jint width, jint height, jint pixelFormat) {
544    ALOGV("nativeProduceFrame");
545    sp<ANativeWindow> anw;
546
547    if ((anw = getNativeWindow(env, surface)) == NULL) {
548        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
549        return BAD_VALUE;
550    }
551
552    if (pixelBuffer == NULL) {
553        jniThrowNullPointerException(env, "pixelBuffer");
554        return DONT_CARE;
555    }
556
557    int32_t bufSize = static_cast<int32_t>(env->GetArrayLength(pixelBuffer));
558    jbyte* pixels = env->GetByteArrayElements(pixelBuffer, /*is_copy*/NULL);
559
560    if (pixels == NULL) {
561        jniThrowNullPointerException(env, "pixels");
562        return DONT_CARE;
563    }
564
565    status_t err = produceFrame(anw, reinterpret_cast<uint8_t*>(pixels), width, height,
566            pixelFormat, bufSize);
567    env->ReleaseByteArrayElements(pixelBuffer, pixels, JNI_ABORT);
568
569    if (err != NO_ERROR) {
570        ALOGE("%s: Error while producing frame %s (%d).", __FUNCTION__, strerror(-err), err);
571        return err;
572    }
573    return NO_ERROR;
574}
575
576static jint LegacyCameraDevice_nativeSetSurfaceFormat(JNIEnv* env, jobject thiz, jobject surface,
577        jint pixelFormat) {
578    ALOGV("nativeSetSurfaceType");
579    sp<ANativeWindow> anw;
580    if ((anw = getNativeWindow(env, surface)) == NULL) {
581        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
582        return BAD_VALUE;
583    }
584    status_t err = native_window_set_buffers_format(anw.get(), pixelFormat);
585    if (err != NO_ERROR) {
586        ALOGE("%s: Error while setting surface format %s (%d).", __FUNCTION__, strerror(-err), err);
587        return err;
588    }
589    return NO_ERROR;
590}
591
592static jint LegacyCameraDevice_nativeSetSurfaceDimens(JNIEnv* env, jobject thiz, jobject surface,
593        jint width, jint height) {
594    ALOGV("nativeSetSurfaceDimens");
595    sp<ANativeWindow> anw;
596    if ((anw = getNativeWindow(env, surface)) == NULL) {
597        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
598        return BAD_VALUE;
599    }
600
601    // Set user dimensions only
602    // The producer dimensions are owned by GL
603    status_t err = native_window_set_buffers_user_dimensions(anw.get(), width, height);
604    if (err != NO_ERROR) {
605        ALOGE("%s: Error while setting surface user dimens %s (%d).", __FUNCTION__, strerror(-err),
606                err);
607        return err;
608    }
609    return NO_ERROR;
610}
611
612static jlong LegacyCameraDevice_nativeGetSurfaceId(JNIEnv* env, jobject thiz, jobject surface) {
613    ALOGV("nativeGetSurfaceId");
614    sp<Surface> s;
615    if ((s = getSurface(env, surface)) == NULL) {
616        ALOGE("%s: Could not retrieve native Surface from surface.", __FUNCTION__);
617        return 0;
618    }
619    sp<IGraphicBufferProducer> gbp = s->getIGraphicBufferProducer();
620    if (gbp == NULL) {
621        ALOGE("%s: Could not retrieve IGraphicBufferProducer from surface.", __FUNCTION__);
622        return 0;
623    }
624    sp<IBinder> b = IInterface::asBinder(gbp);
625    if (b == NULL) {
626        ALOGE("%s: Could not retrieve IBinder from surface.", __FUNCTION__);
627        return 0;
628    }
629    /*
630     * FIXME: Use better unique ID for surfaces than native IBinder pointer.  Fix also in the camera
631     * service (CameraDeviceClient.h).
632     */
633    return reinterpret_cast<jlong>(b.get());
634}
635
636static jint LegacyCameraDevice_nativeSetSurfaceOrientation(JNIEnv* env, jobject thiz,
637        jobject surface, jint facing, jint orientation) {
638    ALOGV("nativeSetSurfaceOrientation");
639    sp<ANativeWindow> anw;
640    if ((anw = getNativeWindow(env, surface)) == NULL) {
641        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
642        return BAD_VALUE;
643    }
644
645    status_t err = NO_ERROR;
646    CameraMetadata staticMetadata;
647
648    int32_t orientVal = static_cast<int32_t>(orientation);
649    uint8_t facingVal = static_cast<uint8_t>(facing);
650    staticMetadata.update(ANDROID_SENSOR_ORIENTATION, &orientVal, 1);
651    staticMetadata.update(ANDROID_LENS_FACING, &facingVal, 1);
652
653    int32_t transform = 0;
654
655    if ((err = CameraUtils::getRotationTransform(staticMetadata, /*out*/&transform)) != NO_ERROR) {
656        ALOGE("%s: Invalid rotation transform %s (%d)", __FUNCTION__, strerror(-err),
657                err);
658        return err;
659    }
660
661    ALOGV("%s: Setting buffer sticky transform to %d", __FUNCTION__, transform);
662
663    if ((err = native_window_set_buffers_sticky_transform(anw.get(), transform)) != NO_ERROR) {
664        ALOGE("%s: Unable to configure surface transform, error %s (%d)", __FUNCTION__,
665                strerror(-err), err);
666        return err;
667    }
668
669    return NO_ERROR;
670}
671
672static jint LegacyCameraDevice_nativeSetNextTimestamp(JNIEnv* env, jobject thiz, jobject surface,
673        jlong timestamp) {
674    ALOGV("nativeSetNextTimestamp");
675    sp<ANativeWindow> anw;
676    if ((anw = getNativeWindow(env, surface)) == NULL) {
677        ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__);
678        return BAD_VALUE;
679    }
680
681    status_t err = NO_ERROR;
682
683    if ((err = native_window_set_buffers_timestamp(anw.get(), static_cast<int64_t>(timestamp))) !=
684            NO_ERROR) {
685        ALOGE("%s: Unable to set surface timestamp, error %s (%d)", __FUNCTION__, strerror(-err),
686                err);
687        return err;
688    }
689    return NO_ERROR;
690}
691
692static jint LegacyCameraDevice_nativeGetJpegFooterSize(JNIEnv* env, jobject thiz) {
693    ALOGV("nativeGetJpegFooterSize");
694    return static_cast<jint>(sizeof(struct camera3_jpeg_blob));
695}
696
697} // extern "C"
698
699static JNINativeMethod gCameraDeviceMethods[] = {
700    { "nativeDetectSurfaceType",
701    "(Landroid/view/Surface;)I",
702    (void *)LegacyCameraDevice_nativeDetectSurfaceType },
703    { "nativeDetectSurfaceDimens",
704    "(Landroid/view/Surface;[I)I",
705    (void *)LegacyCameraDevice_nativeDetectSurfaceDimens },
706    { "nativeConfigureSurface",
707    "(Landroid/view/Surface;III)I",
708    (void *)LegacyCameraDevice_nativeConfigureSurface },
709    { "nativeProduceFrame",
710    "(Landroid/view/Surface;[BIII)I",
711    (void *)LegacyCameraDevice_nativeProduceFrame },
712    { "nativeSetSurfaceFormat",
713    "(Landroid/view/Surface;I)I",
714    (void *)LegacyCameraDevice_nativeSetSurfaceFormat },
715    { "nativeSetSurfaceDimens",
716    "(Landroid/view/Surface;II)I",
717    (void *)LegacyCameraDevice_nativeSetSurfaceDimens },
718    { "nativeGetSurfaceId",
719    "(Landroid/view/Surface;)J",
720    (void *)LegacyCameraDevice_nativeGetSurfaceId },
721    { "nativeDetectTextureDimens",
722    "(Landroid/graphics/SurfaceTexture;[I)I",
723    (void *)LegacyCameraDevice_nativeDetectTextureDimens },
724    { "nativeSetSurfaceOrientation",
725    "(Landroid/view/Surface;II)I",
726    (void *)LegacyCameraDevice_nativeSetSurfaceOrientation },
727    { "nativeSetNextTimestamp",
728    "(Landroid/view/Surface;J)I",
729    (void *)LegacyCameraDevice_nativeSetNextTimestamp },
730    { "nativeGetJpegFooterSize",
731    "()I",
732    (void *)LegacyCameraDevice_nativeGetJpegFooterSize },
733    { "nativeDetectSurfaceUsageFlags",
734    "(Landroid/view/Surface;)I",
735    (void *)LegacyCameraDevice_nativeDetectSurfaceUsageFlags },
736};
737
738// Get all the required offsets in java class and register native functions
739int register_android_hardware_camera2_legacy_LegacyCameraDevice(JNIEnv* env)
740{
741    // Register native functions
742    return RegisterMethodsOrDie(env,
743            CAMERA_DEVICE_CLASS_NAME,
744            gCameraDeviceMethods,
745            NELEM(gCameraDeviceMethods));
746}
747