Home | History | Annotate | Download | only in camera2
      1 /*
      2  * Copyright (C) 2012 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 "Camera2_test"
     18 //#define LOG_NDEBUG 0
     19 
     20 #include <utils/Log.h>
     21 #include <gtest/gtest.h>
     22 #include <iostream>
     23 #include <fstream>
     24 
     25 #include <utils/Vector.h>
     26 #include <gui/CpuConsumer.h>
     27 #include <ui/PixelFormat.h>
     28 #include <system/camera_metadata.h>
     29 
     30 #include "camera2_utils.h"
     31 #include "TestExtensions.h"
     32 
     33 namespace android {
     34 namespace camera2 {
     35 namespace tests {
     36 
     37 class Camera2Test: public testing::Test {
     38   public:
     39     void SetUpModule() {
     40         int res;
     41 
     42         hw_module_t *module = NULL;
     43         res = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
     44                 (const hw_module_t **)&module);
     45 
     46         ASSERT_EQ(0, res)
     47                 << "Failure opening camera hardware module: " << res;
     48         ASSERT_TRUE(NULL != module)
     49                 << "No camera module was set by hw_get_module";
     50 
     51         IF_ALOGV() {
     52             std::cout << "  Camera module name: "
     53                     << module->name << std::endl;
     54             std::cout << "  Camera module author: "
     55                     << module->author << std::endl;
     56             std::cout << "  Camera module API version: 0x" << std::hex
     57                     << module->module_api_version << std::endl;
     58             std::cout << "  Camera module HAL API version: 0x" << std::hex
     59                     << module->hal_api_version << std::endl;
     60         }
     61 
     62         int16_t version2_0 = CAMERA_MODULE_API_VERSION_2_0;
     63         ASSERT_LE(version2_0, module->module_api_version)
     64                 << "Camera module version is 0x"
     65                 << std::hex << module->module_api_version
     66                 << ", should be at least 2.0. (0x"
     67                 << std::hex << CAMERA_MODULE_API_VERSION_2_0 << ")";
     68 
     69         sCameraModule = reinterpret_cast<camera_module_t*>(module);
     70 
     71         sNumCameras = sCameraModule->get_number_of_cameras();
     72         ASSERT_LT(0, sNumCameras) << "No camera devices available!";
     73 
     74         IF_ALOGV() {
     75             std::cout << "  Camera device count: " << sNumCameras << std::endl;
     76         }
     77 
     78         sCameraSupportsHal2 = new bool[sNumCameras];
     79 
     80         for (int i = 0; i < sNumCameras; i++) {
     81             camera_info info;
     82             res = sCameraModule->get_camera_info(i, &info);
     83             ASSERT_EQ(0, res)
     84                     << "Failure getting camera info for camera " << i;
     85             IF_ALOGV() {
     86                 std::cout << "  Camera device: " << std::dec
     87                           << i << std::endl;;
     88                 std::cout << "    Facing: " << std::dec
     89                           << info.facing  << std::endl;
     90                 std::cout << "    Orientation: " << std::dec
     91                           << info.orientation  << std::endl;
     92                 std::cout << "    Version: 0x" << std::hex <<
     93                         info.device_version  << std::endl;
     94             }
     95             if (info.device_version >= CAMERA_DEVICE_API_VERSION_2_0 &&
     96                     info.device_version < CAMERA_DEVICE_API_VERSION_3_0) {
     97                 sCameraSupportsHal2[i] = true;
     98                 ASSERT_TRUE(NULL != info.static_camera_characteristics);
     99                 IF_ALOGV() {
    100                     std::cout << "    Static camera metadata:"  << std::endl;
    101                     dump_indented_camera_metadata(info.static_camera_characteristics,
    102                             0, 1, 6);
    103                 }
    104             } else {
    105                 sCameraSupportsHal2[i] = false;
    106             }
    107         }
    108     }
    109 
    110     void TearDownModule() {
    111         hw_module_t *module = reinterpret_cast<hw_module_t*>(sCameraModule);
    112         ASSERT_EQ(0, HWModuleHelpers::closeModule(module));
    113     }
    114 
    115     static const camera_module_t *getCameraModule() {
    116         return sCameraModule;
    117     }
    118 
    119     static int getNumCameras() {
    120         return sNumCameras;
    121     }
    122 
    123     static bool isHal2Supported(int id) {
    124         return sCameraSupportsHal2[id];
    125     }
    126 
    127     static camera2_device_t *openCameraDevice(int id) {
    128         ALOGV("Opening camera %d", id);
    129         if (NULL == sCameraSupportsHal2) return NULL;
    130         if (id >= sNumCameras) return NULL;
    131         if (!sCameraSupportsHal2[id]) return NULL;
    132 
    133         hw_device_t *device = NULL;
    134         const camera_module_t *cam_module = getCameraModule();
    135         if (cam_module == NULL) {
    136             return NULL;
    137         }
    138 
    139         char camId[10];
    140         int res;
    141 
    142         snprintf(camId, 10, "%d", id);
    143         res = cam_module->common.methods->open(
    144             (const hw_module_t*)cam_module,
    145             camId,
    146             &device);
    147         if (res != NO_ERROR || device == NULL) {
    148             return NULL;
    149         }
    150         camera2_device_t *cam_device =
    151                 reinterpret_cast<camera2_device_t*>(device);
    152         return cam_device;
    153     }
    154 
    155     static status_t configureCameraDevice(camera2_device_t *dev,
    156             MetadataQueue &requestQueue,
    157             MetadataQueue  &frameQueue,
    158             NotifierListener &listener) {
    159 
    160         status_t err;
    161 
    162         err = dev->ops->set_request_queue_src_ops(dev,
    163                 requestQueue.getToConsumerInterface());
    164         if (err != OK) return err;
    165 
    166         requestQueue.setFromConsumerInterface(dev);
    167 
    168         err = dev->ops->set_frame_queue_dst_ops(dev,
    169                 frameQueue.getToProducerInterface());
    170         if (err != OK) return err;
    171 
    172         err = listener.getNotificationsFrom(dev);
    173         if (err != OK) return err;
    174 
    175         vendor_tag_query_ops_t *vendor_metadata_tag_ops;
    176         err = dev->ops->get_metadata_vendor_tag_ops(dev, &vendor_metadata_tag_ops);
    177         if (err != OK) return err;
    178 
    179         err = set_camera_metadata_vendor_tag_ops(vendor_metadata_tag_ops);
    180         if (err != OK) return err;
    181 
    182         return OK;
    183     }
    184 
    185     static status_t closeCameraDevice(camera2_device_t **cam_dev) {
    186         int res;
    187         if (*cam_dev == NULL ) return OK;
    188 
    189         ALOGV("Closing camera %p", cam_dev);
    190 
    191         hw_device_t *dev = reinterpret_cast<hw_device_t *>(*cam_dev);
    192         res = dev->close(dev);
    193         *cam_dev = NULL;
    194         return res;
    195     }
    196 
    197     void setUpCamera(int id) {
    198         ASSERT_GT(sNumCameras, id);
    199         status_t res;
    200 
    201         if (mDevice != NULL) {
    202             closeCameraDevice(&mDevice);
    203         }
    204         mDevice = openCameraDevice(id);
    205         ASSERT_TRUE(NULL != mDevice) << "Failed to open camera device";
    206 
    207         camera_info info;
    208         res = sCameraModule->get_camera_info(id, &info);
    209         ASSERT_EQ(OK, res);
    210 
    211         mStaticInfo = info.static_camera_characteristics;
    212 
    213         res = configureCameraDevice(mDevice,
    214                 mRequests,
    215                 mFrames,
    216                 mNotifications);
    217         ASSERT_EQ(OK, res) << "Failure to configure camera device";
    218 
    219     }
    220 
    221     void setUpStream(sp<IGraphicBufferProducer> consumer,
    222             int width, int height, int format, int *id) {
    223         status_t res;
    224 
    225         StreamAdapter* stream = new StreamAdapter(consumer);
    226 
    227         ALOGV("Creating stream, format 0x%x, %d x %d", format, width, height);
    228         res = stream->connectToDevice(mDevice, width, height, format);
    229         ASSERT_EQ(NO_ERROR, res) << "Failed to connect to stream: "
    230                                  << strerror(-res);
    231         mStreams.push_back(stream);
    232 
    233         *id = stream->getId();
    234     }
    235 
    236     void disconnectStream(int id) {
    237         status_t res;
    238         unsigned int i=0;
    239         for (; i < mStreams.size(); i++) {
    240             if (mStreams[i]->getId() == id) {
    241                 res = mStreams[i]->disconnect();
    242                 ASSERT_EQ(NO_ERROR, res) <<
    243                         "Failed to disconnect stream " << id;
    244                 break;
    245             }
    246         }
    247         ASSERT_GT(mStreams.size(), i) << "Stream id not found:" << id;
    248     }
    249 
    250     void getResolutionList(int32_t format,
    251             const int32_t **list,
    252             size_t *count) {
    253         ALOGV("Getting resolutions for format %x", format);
    254         status_t res;
    255         if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
    256             camera_metadata_ro_entry_t availableFormats;
    257             res = find_camera_metadata_ro_entry(mStaticInfo,
    258                     ANDROID_SCALER_AVAILABLE_FORMATS,
    259                     &availableFormats);
    260             ASSERT_EQ(OK, res);
    261 
    262             uint32_t formatIdx;
    263             for (formatIdx=0; formatIdx < availableFormats.count; formatIdx++) {
    264                 if (availableFormats.data.i32[formatIdx] == format) break;
    265             }
    266             ASSERT_NE(availableFormats.count, formatIdx)
    267                 << "No support found for format 0x" << std::hex << format;
    268         }
    269 
    270         camera_metadata_ro_entry_t availableSizes;
    271         if (format == HAL_PIXEL_FORMAT_RAW_SENSOR) {
    272             res = find_camera_metadata_ro_entry(mStaticInfo,
    273                     ANDROID_SCALER_AVAILABLE_RAW_SIZES,
    274                     &availableSizes);
    275         } else if (format == HAL_PIXEL_FORMAT_BLOB) {
    276             res = find_camera_metadata_ro_entry(mStaticInfo,
    277                     ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
    278                     &availableSizes);
    279         } else {
    280             res = find_camera_metadata_ro_entry(mStaticInfo,
    281                     ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
    282                     &availableSizes);
    283         }
    284         ASSERT_EQ(OK, res);
    285 
    286         *list = availableSizes.data.i32;
    287         *count = availableSizes.count;
    288     }
    289 
    290     status_t waitUntilDrained() {
    291         static const uint32_t kSleepTime = 50000; // 50 ms
    292         static const uint32_t kMaxSleepTime = 10000000; // 10 s
    293         ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
    294 
    295         // TODO: Set up notifications from HAL, instead of sleeping here
    296         uint32_t totalTime = 0;
    297         while (mDevice->ops->get_in_progress_count(mDevice) > 0) {
    298             usleep(kSleepTime);
    299             totalTime += kSleepTime;
    300             if (totalTime > kMaxSleepTime) {
    301                 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
    302                         mDevice->ops->get_in_progress_count(mDevice), totalTime);
    303                 return TIMED_OUT;
    304             }
    305         }
    306         ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
    307         return OK;
    308     }
    309 
    310     virtual void SetUp() {
    311         TEST_EXTENSION_FORKING_SET_UP;
    312 
    313         SetUpModule();
    314 
    315         const ::testing::TestInfo* const testInfo =
    316                 ::testing::UnitTest::GetInstance()->current_test_info();
    317         (void)testInfo;
    318 
    319         ALOGV("*** Starting test %s in test case %s", testInfo->name(),
    320               testInfo->test_case_name());
    321         mDevice = NULL;
    322     }
    323 
    324     virtual void TearDown() {
    325         TEST_EXTENSION_FORKING_TEAR_DOWN;
    326 
    327         for (unsigned int i = 0; i < mStreams.size(); i++) {
    328             delete mStreams[i];
    329         }
    330         if (mDevice != NULL) {
    331             closeCameraDevice(&mDevice);
    332         }
    333 
    334         TearDownModule();
    335     }
    336 
    337     camera2_device    *mDevice;
    338     const camera_metadata_t *mStaticInfo;
    339 
    340     MetadataQueue    mRequests;
    341     MetadataQueue    mFrames;
    342     NotifierListener mNotifications;
    343 
    344     Vector<StreamAdapter*> mStreams;
    345 
    346   private:
    347     static camera_module_t *sCameraModule;
    348     static int              sNumCameras;
    349     static bool            *sCameraSupportsHal2;
    350 };
    351 
    352 camera_module_t *Camera2Test::sCameraModule = NULL;
    353 bool *Camera2Test::sCameraSupportsHal2      = NULL;
    354 int Camera2Test::sNumCameras                = 0;
    355 
    356 static const nsecs_t USEC = 1000;
    357 static const nsecs_t MSEC = 1000*USEC;
    358 static const nsecs_t SEC = 1000*MSEC;
    359 
    360 
    361 TEST_F(Camera2Test, OpenClose) {
    362 
    363     TEST_EXTENSION_FORKING_INIT;
    364 
    365     status_t res;
    366 
    367     for (int id = 0; id < getNumCameras(); id++) {
    368         if (!isHal2Supported(id)) continue;
    369 
    370         camera2_device_t *d = openCameraDevice(id);
    371         ASSERT_TRUE(NULL != d) << "Failed to open camera device";
    372 
    373         res = closeCameraDevice(&d);
    374         ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
    375     }
    376 }
    377 
    378 TEST_F(Camera2Test, Capture1Raw) {
    379 
    380     TEST_EXTENSION_FORKING_INIT;
    381 
    382     status_t res;
    383 
    384     for (int id = 0; id < getNumCameras(); id++) {
    385         if (!isHal2Supported(id)) continue;
    386 
    387         ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
    388 
    389         sp<CpuConsumer> rawConsumer = new CpuConsumer(1);
    390         sp<FrameWaiter> rawWaiter = new FrameWaiter();
    391         rawConsumer->setFrameAvailableListener(rawWaiter);
    392 
    393         const int32_t *rawResolutions;
    394         size_t   rawResolutionsCount;
    395 
    396         int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
    397 
    398         getResolutionList(format,
    399                 &rawResolutions, &rawResolutionsCount);
    400 
    401         if (rawResolutionsCount <= 0) {
    402             const ::testing::TestInfo* const test_info =
    403                 ::testing::UnitTest::GetInstance()->current_test_info();
    404             std::cerr << "Skipping test "
    405                       << test_info->test_case_name() << "."
    406                       << test_info->name()
    407                       << " because the optional format was not available: "
    408                       << "RAW_SENSOR" << std::endl;
    409             return;
    410         }
    411 
    412         ASSERT_LT((size_t)0, rawResolutionsCount);
    413 
    414         // Pick first available raw resolution
    415         int width = rawResolutions[0];
    416         int height = rawResolutions[1];
    417 
    418         int streamId;
    419         ASSERT_NO_FATAL_FAILURE(
    420             setUpStream(rawConsumer->getProducerInterface(),
    421                     width, height, format, &streamId) );
    422 
    423         camera_metadata_t *request;
    424         request = allocate_camera_metadata(20, 2000);
    425 
    426         uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
    427         add_camera_metadata_entry(request,
    428                 ANDROID_REQUEST_METADATA_MODE,
    429                 (void**)&metadataMode, 1);
    430         uint32_t outputStreams = streamId;
    431         add_camera_metadata_entry(request,
    432                 ANDROID_REQUEST_OUTPUT_STREAMS,
    433                 (void**)&outputStreams, 1);
    434 
    435         uint64_t exposureTime = 10*MSEC;
    436         add_camera_metadata_entry(request,
    437                 ANDROID_SENSOR_EXPOSURE_TIME,
    438                 (void**)&exposureTime, 1);
    439         uint64_t frameDuration = 30*MSEC;
    440         add_camera_metadata_entry(request,
    441                 ANDROID_SENSOR_FRAME_DURATION,
    442                 (void**)&frameDuration, 1);
    443         uint32_t sensitivity = 100;
    444         add_camera_metadata_entry(request,
    445                 ANDROID_SENSOR_SENSITIVITY,
    446                 (void**)&sensitivity, 1);
    447         uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
    448         add_camera_metadata_entry(request,
    449                 ANDROID_REQUEST_TYPE,
    450                 (void**)&requestType, 1);
    451 
    452         uint32_t hourOfDay = 12;
    453         add_camera_metadata_entry(request,
    454                 0x80000000, // EMULATOR_HOUROFDAY
    455                 &hourOfDay, 1);
    456 
    457         IF_ALOGV() {
    458             std::cout << "Input request: " << std::endl;
    459             dump_indented_camera_metadata(request, 0, 1, 2);
    460         }
    461 
    462         res = mRequests.enqueue(request);
    463         ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
    464 
    465         res = mFrames.waitForBuffer(exposureTime + SEC);
    466         ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
    467 
    468         camera_metadata_t *frame;
    469         res = mFrames.dequeue(&frame);
    470         ASSERT_EQ(NO_ERROR, res);
    471         ASSERT_TRUE(frame != NULL);
    472 
    473         IF_ALOGV() {
    474             std::cout << "Output frame:" << std::endl;
    475             dump_indented_camera_metadata(frame, 0, 1, 2);
    476         }
    477 
    478         res = rawWaiter->waitForFrame(exposureTime + SEC);
    479         ASSERT_EQ(NO_ERROR, res);
    480 
    481         CpuConsumer::LockedBuffer buffer;
    482         res = rawConsumer->lockNextBuffer(&buffer);
    483         ASSERT_EQ(NO_ERROR, res);
    484 
    485         IF_ALOGV() {
    486             const char *dumpname =
    487                     "/data/local/tmp/camera2_test-capture1raw-dump.raw";
    488             ALOGV("Dumping raw buffer to %s", dumpname);
    489             // Write to file
    490             std::ofstream rawFile(dumpname);
    491             size_t bpp = 2;
    492             for (unsigned int y = 0; y < buffer.height; y++) {
    493                 rawFile.write(
    494                         (const char *)(buffer.data + y * buffer.stride * bpp),
    495                         buffer.width * bpp);
    496             }
    497             rawFile.close();
    498         }
    499 
    500         res = rawConsumer->unlockBuffer(buffer);
    501         ASSERT_EQ(NO_ERROR, res);
    502 
    503         ASSERT_EQ(OK, waitUntilDrained());
    504         ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
    505 
    506         res = closeCameraDevice(&mDevice);
    507         ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
    508 
    509     }
    510 }
    511 
    512 TEST_F(Camera2Test, CaptureBurstRaw) {
    513 
    514     TEST_EXTENSION_FORKING_INIT;
    515 
    516     status_t res;
    517 
    518     for (int id = 0; id < getNumCameras(); id++) {
    519         if (!isHal2Supported(id)) continue;
    520 
    521         ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
    522 
    523         sp<CpuConsumer> rawConsumer = new CpuConsumer(1);
    524         sp<FrameWaiter> rawWaiter = new FrameWaiter();
    525         rawConsumer->setFrameAvailableListener(rawWaiter);
    526 
    527         const int32_t *rawResolutions;
    528         size_t    rawResolutionsCount;
    529 
    530         int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
    531 
    532         getResolutionList(format,
    533                 &rawResolutions, &rawResolutionsCount);
    534 
    535         if (rawResolutionsCount <= 0) {
    536             const ::testing::TestInfo* const test_info =
    537                 ::testing::UnitTest::GetInstance()->current_test_info();
    538             std::cerr << "Skipping test "
    539                       << test_info->test_case_name() << "."
    540                       << test_info->name()
    541                       << " because the optional format was not available: "
    542                       << "RAW_SENSOR" << std::endl;
    543             return;
    544         }
    545 
    546         ASSERT_LT((uint32_t)0, rawResolutionsCount);
    547 
    548         // Pick first available raw resolution
    549         int width = rawResolutions[0];
    550         int height = rawResolutions[1];
    551 
    552         int streamId;
    553         ASSERT_NO_FATAL_FAILURE(
    554             setUpStream(rawConsumer->getProducerInterface(),
    555                     width, height, format, &streamId) );
    556 
    557         camera_metadata_t *request;
    558         request = allocate_camera_metadata(20, 2000);
    559 
    560         uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
    561         add_camera_metadata_entry(request,
    562                 ANDROID_REQUEST_METADATA_MODE,
    563                 (void**)&metadataMode, 1);
    564         uint32_t outputStreams = streamId;
    565         add_camera_metadata_entry(request,
    566                 ANDROID_REQUEST_OUTPUT_STREAMS,
    567                 (void**)&outputStreams, 1);
    568 
    569         uint64_t frameDuration = 30*MSEC;
    570         add_camera_metadata_entry(request,
    571                 ANDROID_SENSOR_FRAME_DURATION,
    572                 (void**)&frameDuration, 1);
    573         uint32_t sensitivity = 100;
    574         add_camera_metadata_entry(request,
    575                 ANDROID_SENSOR_SENSITIVITY,
    576                 (void**)&sensitivity, 1);
    577         uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
    578         add_camera_metadata_entry(request,
    579                 ANDROID_REQUEST_TYPE,
    580                 (void**)&requestType, 1);
    581 
    582         uint32_t hourOfDay = 12;
    583         add_camera_metadata_entry(request,
    584                 0x80000000, // EMULATOR_HOUROFDAY
    585                 &hourOfDay, 1);
    586 
    587         IF_ALOGV() {
    588             std::cout << "Input request template: " << std::endl;
    589             dump_indented_camera_metadata(request, 0, 1, 2);
    590         }
    591 
    592         int numCaptures = 10;
    593 
    594         // Enqueue numCaptures requests with increasing exposure time
    595 
    596         uint64_t exposureTime = 100 * USEC;
    597         for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) {
    598             camera_metadata_t *req;
    599             req = allocate_camera_metadata(20, 2000);
    600             append_camera_metadata(req, request);
    601 
    602             add_camera_metadata_entry(req,
    603                     ANDROID_SENSOR_EXPOSURE_TIME,
    604                     (void**)&exposureTime, 1);
    605             exposureTime *= 2;
    606 
    607             res = mRequests.enqueue(req);
    608             ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: "
    609                     << strerror(-res);
    610         }
    611 
    612         // Get frames and image buffers one by one
    613         uint64_t expectedExposureTime = 100 * USEC;
    614         for (int frameCount = 0; frameCount < 10; frameCount++) {
    615             res = mFrames.waitForBuffer(SEC + expectedExposureTime);
    616             ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
    617 
    618             camera_metadata_t *frame;
    619             res = mFrames.dequeue(&frame);
    620             ASSERT_EQ(NO_ERROR, res);
    621             ASSERT_TRUE(frame != NULL);
    622 
    623             camera_metadata_entry_t frameNumber;
    624             res = find_camera_metadata_entry(frame,
    625                     ANDROID_REQUEST_FRAME_COUNT,
    626                     &frameNumber);
    627             ASSERT_EQ(NO_ERROR, res);
    628             ASSERT_EQ(frameCount, *frameNumber.data.i32);
    629 
    630             res = rawWaiter->waitForFrame(SEC + expectedExposureTime);
    631             ASSERT_EQ(NO_ERROR, res) <<
    632                     "Never got raw data for capture " << frameCount;
    633 
    634             CpuConsumer::LockedBuffer buffer;
    635             res = rawConsumer->lockNextBuffer(&buffer);
    636             ASSERT_EQ(NO_ERROR, res);
    637 
    638             IF_ALOGV() {
    639                 char dumpname[60];
    640                 snprintf(dumpname, 60,
    641                         "/data/local/tmp/camera2_test-"
    642                         "captureBurstRaw-dump_%d.raw",
    643                         frameCount);
    644                 ALOGV("Dumping raw buffer to %s", dumpname);
    645                 // Write to file
    646                 std::ofstream rawFile(dumpname);
    647                 for (unsigned int y = 0; y < buffer.height; y++) {
    648                     rawFile.write(
    649                             (const char *)(buffer.data + y * buffer.stride * 2),
    650                             buffer.width * 2);
    651                 }
    652                 rawFile.close();
    653             }
    654 
    655             res = rawConsumer->unlockBuffer(buffer);
    656             ASSERT_EQ(NO_ERROR, res);
    657 
    658             expectedExposureTime *= 2;
    659         }
    660     }
    661 }
    662 
    663 TEST_F(Camera2Test, ConstructDefaultRequests) {
    664 
    665     TEST_EXTENSION_FORKING_INIT;
    666 
    667     status_t res;
    668 
    669     for (int id = 0; id < getNumCameras(); id++) {
    670         if (!isHal2Supported(id)) continue;
    671 
    672         ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
    673 
    674         for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT;
    675              i++) {
    676             camera_metadata_t *request = NULL;
    677             res = mDevice->ops->construct_default_request(mDevice,
    678                     i,
    679                     &request);
    680             EXPECT_EQ(NO_ERROR, res) <<
    681                     "Unable to construct request from template type " << i;
    682             EXPECT_TRUE(request != NULL);
    683             EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request));
    684             EXPECT_LT((size_t)0, get_camera_metadata_data_count(request));
    685 
    686             IF_ALOGV() {
    687                 std::cout << "  ** Template type " << i << ":"<<std::endl;
    688                 dump_indented_camera_metadata(request, 0, 2, 4);
    689             }
    690 
    691             free_camera_metadata(request);
    692         }
    693     }
    694 }
    695 
    696 TEST_F(Camera2Test, Capture1Jpeg) {
    697     status_t res;
    698 
    699     for (int id = 0; id < getNumCameras(); id++) {
    700         if (!isHal2Supported(id)) continue;
    701 
    702         ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
    703 
    704         sp<CpuConsumer> jpegConsumer = new CpuConsumer(1);
    705         sp<FrameWaiter> jpegWaiter = new FrameWaiter();
    706         jpegConsumer->setFrameAvailableListener(jpegWaiter);
    707 
    708         const int32_t *jpegResolutions;
    709         size_t   jpegResolutionsCount;
    710 
    711         int format = HAL_PIXEL_FORMAT_BLOB;
    712 
    713         getResolutionList(format,
    714                 &jpegResolutions, &jpegResolutionsCount);
    715         ASSERT_LT((size_t)0, jpegResolutionsCount);
    716 
    717         // Pick first available JPEG resolution
    718         int width = jpegResolutions[0];
    719         int height = jpegResolutions[1];
    720 
    721         int streamId;
    722         ASSERT_NO_FATAL_FAILURE(
    723             setUpStream(jpegConsumer->getProducerInterface(),
    724                     width, height, format, &streamId) );
    725 
    726         camera_metadata_t *request;
    727         request = allocate_camera_metadata(20, 2000);
    728 
    729         uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
    730         add_camera_metadata_entry(request,
    731                 ANDROID_REQUEST_METADATA_MODE,
    732                 (void**)&metadataMode, 1);
    733         uint32_t outputStreams = streamId;
    734         add_camera_metadata_entry(request,
    735                 ANDROID_REQUEST_OUTPUT_STREAMS,
    736                 (void**)&outputStreams, 1);
    737 
    738         uint64_t exposureTime = 10*MSEC;
    739         add_camera_metadata_entry(request,
    740                 ANDROID_SENSOR_EXPOSURE_TIME,
    741                 (void**)&exposureTime, 1);
    742         uint64_t frameDuration = 30*MSEC;
    743         add_camera_metadata_entry(request,
    744                 ANDROID_SENSOR_FRAME_DURATION,
    745                 (void**)&frameDuration, 1);
    746         uint32_t sensitivity = 100;
    747         add_camera_metadata_entry(request,
    748                 ANDROID_SENSOR_SENSITIVITY,
    749                 (void**)&sensitivity, 1);
    750         uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
    751         add_camera_metadata_entry(request,
    752                 ANDROID_REQUEST_TYPE,
    753                 (void**)&requestType, 1);
    754 
    755         uint32_t hourOfDay = 12;
    756         add_camera_metadata_entry(request,
    757                 0x80000000, // EMULATOR_HOUROFDAY
    758                 &hourOfDay, 1);
    759 
    760         IF_ALOGV() {
    761             std::cout << "Input request: " << std::endl;
    762             dump_indented_camera_metadata(request, 0, 1, 4);
    763         }
    764 
    765         res = mRequests.enqueue(request);
    766         ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
    767 
    768         res = mFrames.waitForBuffer(exposureTime + SEC);
    769         ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
    770 
    771         camera_metadata_t *frame;
    772         res = mFrames.dequeue(&frame);
    773         ASSERT_EQ(NO_ERROR, res);
    774         ASSERT_TRUE(frame != NULL);
    775 
    776         IF_ALOGV() {
    777             std::cout << "Output frame:" << std::endl;
    778             dump_indented_camera_metadata(frame, 0, 1, 4);
    779         }
    780 
    781         res = jpegWaiter->waitForFrame(exposureTime + SEC);
    782         ASSERT_EQ(NO_ERROR, res);
    783 
    784         CpuConsumer::LockedBuffer buffer;
    785         res = jpegConsumer->lockNextBuffer(&buffer);
    786         ASSERT_EQ(NO_ERROR, res);
    787 
    788         IF_ALOGV() {
    789             const char *dumpname =
    790                     "/data/local/tmp/camera2_test-capture1jpeg-dump.jpeg";
    791             ALOGV("Dumping raw buffer to %s", dumpname);
    792             // Write to file
    793             std::ofstream jpegFile(dumpname);
    794             size_t bpp = 1;
    795             for (unsigned int y = 0; y < buffer.height; y++) {
    796                 jpegFile.write(
    797                         (const char *)(buffer.data + y * buffer.stride * bpp),
    798                         buffer.width * bpp);
    799             }
    800             jpegFile.close();
    801         }
    802 
    803         res = jpegConsumer->unlockBuffer(buffer);
    804         ASSERT_EQ(NO_ERROR, res);
    805 
    806         ASSERT_EQ(OK, waitUntilDrained());
    807         ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
    808 
    809         res = closeCameraDevice(&mDevice);
    810         ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
    811 
    812     }
    813 }
    814 
    815 } // namespace tests
    816 } // namespace camera2
    817 } // namespace android
    818