Home | History | Annotate | Download | only in camera
      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 /*
     18  * Contains implementation of a class EmulatedFakeCamera2 that encapsulates
     19  * functionality of an advanced fake camera.
     20  */
     21 
     22 #include <inttypes.h>
     23 
     24 //#define LOG_NDEBUG 0
     25 #define LOG_TAG "EmulatedCamera_FakeCamera2"
     26 #include <utils/Log.h>
     27 
     28 #include "EmulatedFakeCamera2.h"
     29 #include "EmulatedCameraFactory.h"
     30 #include <ui/Rect.h>
     31 #include <ui/GraphicBufferMapper.h>
     32 #include "gralloc_cb.h"
     33 
     34 #define ERROR_CAMERA_NOT_PRESENT -EPIPE
     35 
     36 #define CAMERA2_EXT_TRIGGER_TESTING_DISCONNECT 0xFFFFFFFF
     37 
     38 namespace android {
     39 
     40 const int64_t USEC = 1000LL;
     41 const int64_t MSEC = USEC * 1000LL;
     42 const int64_t SEC = MSEC * 1000LL;
     43 
     44 const uint32_t EmulatedFakeCamera2::kAvailableFormats[4] = {
     45         HAL_PIXEL_FORMAT_RAW16,
     46         HAL_PIXEL_FORMAT_BLOB,
     47         HAL_PIXEL_FORMAT_RGBA_8888,
     48         //        HAL_PIXEL_FORMAT_YV12,
     49         HAL_PIXEL_FORMAT_YCrCb_420_SP
     50 };
     51 
     52 const uint32_t EmulatedFakeCamera2::kAvailableRawSizes[2] = {
     53     640, 480
     54     //    Sensor::kResolution[0], Sensor::kResolution[1]
     55 };
     56 
     57 const uint64_t EmulatedFakeCamera2::kAvailableRawMinDurations[1] = {
     58     static_cast<uint64_t>(Sensor::kFrameDurationRange[0])
     59 };
     60 
     61 const uint32_t EmulatedFakeCamera2::kAvailableProcessedSizesBack[4] = {
     62     640, 480, 320, 240
     63     //    Sensor::kResolution[0], Sensor::kResolution[1]
     64 };
     65 
     66 const uint32_t EmulatedFakeCamera2::kAvailableProcessedSizesFront[4] = {
     67     320, 240, 160, 120
     68     //    Sensor::kResolution[0], Sensor::kResolution[1]
     69 };
     70 
     71 const uint64_t EmulatedFakeCamera2::kAvailableProcessedMinDurations[1] = {
     72     static_cast<uint64_t>(Sensor::kFrameDurationRange[0])
     73 };
     74 
     75 const uint32_t EmulatedFakeCamera2::kAvailableJpegSizesBack[2] = {
     76     640, 480
     77     //    Sensor::kResolution[0], Sensor::kResolution[1]
     78 };
     79 
     80 const uint32_t EmulatedFakeCamera2::kAvailableJpegSizesFront[2] = {
     81     320, 240
     82     //    Sensor::kResolution[0], Sensor::kResolution[1]
     83 };
     84 
     85 
     86 const uint64_t EmulatedFakeCamera2::kAvailableJpegMinDurations[1] = {
     87     static_cast<uint64_t>(Sensor::kFrameDurationRange[0])
     88 };
     89 
     90 
     91 EmulatedFakeCamera2::EmulatedFakeCamera2(int cameraId,
     92         bool facingBack,
     93         struct hw_module_t* module)
     94         : EmulatedCamera2(cameraId,module),
     95           mFacingBack(facingBack),
     96           mIsConnected(false)
     97 {
     98     ALOGD("Constructing emulated fake camera 2 facing %s",
     99             facingBack ? "back" : "front");
    100 }
    101 
    102 EmulatedFakeCamera2::~EmulatedFakeCamera2() {
    103     if (mCameraInfo != NULL) {
    104         free_camera_metadata(mCameraInfo);
    105     }
    106 }
    107 
    108 /****************************************************************************
    109  * Public API overrides
    110  ***************************************************************************/
    111 
    112 status_t EmulatedFakeCamera2::Initialize() {
    113     status_t res;
    114 
    115     res = constructStaticInfo(&mCameraInfo, true);
    116     if (res != OK) {
    117         ALOGE("%s: Unable to allocate static info: %s (%d)",
    118                 __FUNCTION__, strerror(-res), res);
    119         return res;
    120     }
    121     res = constructStaticInfo(&mCameraInfo, false);
    122     if (res != OK) {
    123         ALOGE("%s: Unable to fill in static info: %s (%d)",
    124                 __FUNCTION__, strerror(-res), res);
    125         return res;
    126     }
    127     if (res != OK) return res;
    128 
    129     mNextStreamId = 1;
    130     mNextReprocessStreamId = 1;
    131     mRawStreamCount = 0;
    132     mProcessedStreamCount = 0;
    133     mJpegStreamCount = 0;
    134     mReprocessStreamCount = 0;
    135 
    136     return NO_ERROR;
    137 }
    138 
    139 /****************************************************************************
    140  * Camera module API overrides
    141  ***************************************************************************/
    142 
    143 status_t EmulatedFakeCamera2::connectCamera(hw_device_t** device) {
    144     status_t res;
    145     ALOGV("%s", __FUNCTION__);
    146 
    147     {
    148         Mutex::Autolock l(mMutex);
    149         if (!mStatusPresent) {
    150             ALOGE("%s: Camera ID %d is unplugged", __FUNCTION__,
    151                   mCameraID);
    152             return -ENODEV;
    153         }
    154     }
    155 
    156     mConfigureThread = new ConfigureThread(this);
    157     mReadoutThread = new ReadoutThread(this);
    158     mControlThread = new ControlThread(this);
    159     mSensor = new Sensor();
    160     mJpegCompressor = new JpegCompressor();
    161 
    162     mNextStreamId = 1;
    163     mNextReprocessStreamId = 1;
    164 
    165     res = mSensor->startUp();
    166     if (res != NO_ERROR) return res;
    167 
    168     res = mConfigureThread->run("EmulatedFakeCamera2::configureThread");
    169     if (res != NO_ERROR) return res;
    170 
    171     res = mReadoutThread->run("EmulatedFakeCamera2::readoutThread");
    172     if (res != NO_ERROR) return res;
    173 
    174     res = mControlThread->run("EmulatedFakeCamera2::controlThread");
    175     if (res != NO_ERROR) return res;
    176 
    177     status_t ret = EmulatedCamera2::connectCamera(device);
    178 
    179     if (ret >= 0) {
    180         mIsConnected = true;
    181     }
    182 
    183     return ret;
    184 }
    185 
    186 status_t EmulatedFakeCamera2::plugCamera() {
    187     {
    188         Mutex::Autolock l(mMutex);
    189 
    190         if (!mStatusPresent) {
    191             ALOGI("%s: Plugged back in", __FUNCTION__);
    192             mStatusPresent = true;
    193         }
    194     }
    195 
    196     return NO_ERROR;
    197 }
    198 
    199 status_t EmulatedFakeCamera2::unplugCamera() {
    200     {
    201         Mutex::Autolock l(mMutex);
    202 
    203         if (mStatusPresent) {
    204             ALOGI("%s: Unplugged camera", __FUNCTION__);
    205             mStatusPresent = false;
    206         }
    207     }
    208 
    209     return closeCamera();
    210 }
    211 
    212 camera_device_status_t EmulatedFakeCamera2::getHotplugStatus() {
    213     Mutex::Autolock l(mMutex);
    214     return mStatusPresent ?
    215         CAMERA_DEVICE_STATUS_PRESENT :
    216         CAMERA_DEVICE_STATUS_NOT_PRESENT;
    217 }
    218 
    219 
    220 
    221 status_t EmulatedFakeCamera2::closeCamera() {
    222     {
    223         Mutex::Autolock l(mMutex);
    224 
    225         status_t res;
    226         ALOGV("%s", __FUNCTION__);
    227 
    228         if (!mIsConnected) {
    229             return NO_ERROR;
    230         }
    231 
    232         res = mSensor->shutDown();
    233         if (res != NO_ERROR) {
    234             ALOGE("%s: Unable to shut down sensor: %d", __FUNCTION__, res);
    235             return res;
    236         }
    237 
    238         mConfigureThread->requestExit();
    239         mReadoutThread->requestExit();
    240         mControlThread->requestExit();
    241         mJpegCompressor->cancel();
    242     }
    243 
    244     // give up the lock since we will now block and the threads
    245     // can call back into this object
    246     mConfigureThread->join();
    247     mReadoutThread->join();
    248     mControlThread->join();
    249 
    250     ALOGV("%s exit", __FUNCTION__);
    251 
    252     {
    253         Mutex::Autolock l(mMutex);
    254         mIsConnected = false;
    255     }
    256 
    257     return NO_ERROR;
    258 }
    259 
    260 status_t EmulatedFakeCamera2::getCameraInfo(struct camera_info *info) {
    261     info->facing = mFacingBack ? CAMERA_FACING_BACK : CAMERA_FACING_FRONT;
    262     info->orientation = gEmulatedCameraFactory.getFakeCameraOrientation();
    263     return EmulatedCamera2::getCameraInfo(info);
    264 }
    265 
    266 /****************************************************************************
    267  * Camera device API overrides
    268  ***************************************************************************/
    269 
    270 /** Request input queue */
    271 
    272 int EmulatedFakeCamera2::requestQueueNotify() {
    273     ALOGV("Request queue notification received");
    274 
    275     ALOG_ASSERT(mRequestQueueSrc != NULL,
    276             "%s: Request queue src not set, but received queue notification!",
    277             __FUNCTION__);
    278     ALOG_ASSERT(mFrameQueueDst != NULL,
    279             "%s: Request queue src not set, but received queue notification!",
    280             __FUNCTION__);
    281     ALOG_ASSERT(mStreams.size() != 0,
    282             "%s: No streams allocated, but received queue notification!",
    283             __FUNCTION__);
    284     return mConfigureThread->newRequestAvailable();
    285 }
    286 
    287 int EmulatedFakeCamera2::getInProgressCount() {
    288     Mutex::Autolock l(mMutex);
    289 
    290     if (!mStatusPresent) {
    291         ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
    292         return ERROR_CAMERA_NOT_PRESENT;
    293     }
    294 
    295     int requestCount = 0;
    296     requestCount += mConfigureThread->getInProgressCount();
    297     requestCount += mReadoutThread->getInProgressCount();
    298     requestCount += mJpegCompressor->isBusy() ? 1 : 0;
    299 
    300     return requestCount;
    301 }
    302 
    303 int EmulatedFakeCamera2::constructDefaultRequest(
    304         int request_template,
    305         camera_metadata_t **request) {
    306 
    307     if (request == NULL) return BAD_VALUE;
    308     if (request_template < 0 || request_template >= CAMERA2_TEMPLATE_COUNT) {
    309         return BAD_VALUE;
    310     }
    311 
    312     {
    313         Mutex::Autolock l(mMutex);
    314         if (!mStatusPresent) {
    315             ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
    316             return ERROR_CAMERA_NOT_PRESENT;
    317         }
    318     }
    319 
    320     status_t res;
    321     // Pass 1, calculate size and allocate
    322     res = constructDefaultRequest(request_template,
    323             request,
    324             true);
    325     if (res != OK) {
    326         return res;
    327     }
    328     // Pass 2, build request
    329     res = constructDefaultRequest(request_template,
    330             request,
    331             false);
    332     if (res != OK) {
    333         ALOGE("Unable to populate new request for template %d",
    334                 request_template);
    335     }
    336 
    337     return res;
    338 }
    339 
    340 int EmulatedFakeCamera2::allocateStream(
    341         uint32_t width,
    342         uint32_t height,
    343         int format,
    344         const camera2_stream_ops_t *stream_ops,
    345         uint32_t *stream_id,
    346         uint32_t *format_actual,
    347         uint32_t *usage,
    348         uint32_t *max_buffers) {
    349     Mutex::Autolock l(mMutex);
    350 
    351     if (!mStatusPresent) {
    352         ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
    353         return ERROR_CAMERA_NOT_PRESENT;
    354     }
    355 
    356     // Temporary shim until FORMAT_ZSL is removed
    357     if (format == CAMERA2_HAL_PIXEL_FORMAT_ZSL) {
    358         format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    359     }
    360 
    361     if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
    362         unsigned int numFormats = sizeof(kAvailableFormats) / sizeof(uint32_t);
    363         unsigned int formatIdx = 0;
    364         unsigned int sizeOffsetIdx = 0;
    365         for (; formatIdx < numFormats; formatIdx++) {
    366             if (format == (int)kAvailableFormats[formatIdx]) break;
    367         }
    368         if (formatIdx == numFormats) {
    369             ALOGE("%s: Format 0x%x is not supported", __FUNCTION__, format);
    370             return BAD_VALUE;
    371         }
    372     }
    373 
    374     const uint32_t *availableSizes;
    375     size_t availableSizeCount;
    376     switch (format) {
    377         case HAL_PIXEL_FORMAT_RAW16:
    378             availableSizes = kAvailableRawSizes;
    379             availableSizeCount = sizeof(kAvailableRawSizes)/sizeof(uint32_t);
    380             break;
    381         case HAL_PIXEL_FORMAT_BLOB:
    382             availableSizes = mFacingBack ?
    383                     kAvailableJpegSizesBack : kAvailableJpegSizesFront;
    384             availableSizeCount = mFacingBack ?
    385                     sizeof(kAvailableJpegSizesBack)/sizeof(uint32_t) :
    386                     sizeof(kAvailableJpegSizesFront)/sizeof(uint32_t);
    387             break;
    388         case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
    389         case HAL_PIXEL_FORMAT_RGBA_8888:
    390         case HAL_PIXEL_FORMAT_YV12:
    391         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
    392             availableSizes = mFacingBack ?
    393                     kAvailableProcessedSizesBack : kAvailableProcessedSizesFront;
    394             availableSizeCount = mFacingBack ?
    395                     sizeof(kAvailableProcessedSizesBack)/sizeof(uint32_t) :
    396                     sizeof(kAvailableProcessedSizesFront)/sizeof(uint32_t);
    397             break;
    398         default:
    399             ALOGE("%s: Unknown format 0x%x", __FUNCTION__, format);
    400             return BAD_VALUE;
    401     }
    402 
    403     unsigned int resIdx = 0;
    404     for (; resIdx < availableSizeCount; resIdx++) {
    405         if (availableSizes[resIdx * 2] == width &&
    406                 availableSizes[resIdx * 2 + 1] == height) break;
    407     }
    408     if (resIdx == availableSizeCount) {
    409         ALOGE("%s: Format 0x%x does not support resolution %d, %d", __FUNCTION__,
    410                 format, width, height);
    411         return BAD_VALUE;
    412     }
    413 
    414     switch (format) {
    415         case HAL_PIXEL_FORMAT_RAW16:
    416             if (mRawStreamCount >= kMaxRawStreamCount) {
    417                 ALOGE("%s: Cannot allocate another raw stream (%d already allocated)",
    418                         __FUNCTION__, mRawStreamCount);
    419                 return INVALID_OPERATION;
    420             }
    421             mRawStreamCount++;
    422             break;
    423         case HAL_PIXEL_FORMAT_BLOB:
    424             if (mJpegStreamCount >= kMaxJpegStreamCount) {
    425                 ALOGE("%s: Cannot allocate another JPEG stream (%d already allocated)",
    426                         __FUNCTION__, mJpegStreamCount);
    427                 return INVALID_OPERATION;
    428             }
    429             mJpegStreamCount++;
    430             break;
    431         default:
    432             if (mProcessedStreamCount >= kMaxProcessedStreamCount) {
    433                 ALOGE("%s: Cannot allocate another processed stream (%d already allocated)",
    434                         __FUNCTION__, mProcessedStreamCount);
    435                 return INVALID_OPERATION;
    436             }
    437             mProcessedStreamCount++;
    438     }
    439 
    440     Stream newStream;
    441     newStream.ops = stream_ops;
    442     newStream.width = width;
    443     newStream.height = height;
    444     newStream.format = format;
    445     // TODO: Query stride from gralloc
    446     newStream.stride = width;
    447 
    448     mStreams.add(mNextStreamId, newStream);
    449 
    450     *stream_id = mNextStreamId;
    451     if (format_actual) *format_actual = format;
    452     *usage = GRALLOC_USAGE_HW_CAMERA_WRITE;
    453     *max_buffers = kMaxBufferCount;
    454 
    455     ALOGV("Stream allocated: %d, %d x %d, 0x%x. U: %x, B: %d",
    456             *stream_id, width, height, format, *usage, *max_buffers);
    457 
    458     mNextStreamId++;
    459     return NO_ERROR;
    460 }
    461 
    462 int EmulatedFakeCamera2::registerStreamBuffers(
    463             uint32_t stream_id,
    464             int num_buffers,
    465             buffer_handle_t *buffers) {
    466     Mutex::Autolock l(mMutex);
    467 
    468     if (!mStatusPresent) {
    469         ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
    470         return ERROR_CAMERA_NOT_PRESENT;
    471     }
    472 
    473     ALOGV("%s: Stream %d registering %d buffers", __FUNCTION__,
    474             stream_id, num_buffers);
    475     // Need to find out what the final concrete pixel format for our stream is
    476     // Assumes that all buffers have the same format.
    477     if (num_buffers < 1) {
    478         ALOGE("%s: Stream %d only has %d buffers!",
    479                 __FUNCTION__, stream_id, num_buffers);
    480         return BAD_VALUE;
    481     }
    482     const cb_handle_t *streamBuffer =
    483             reinterpret_cast<const cb_handle_t*>(buffers[0]);
    484 
    485     int finalFormat = streamBuffer->format;
    486 
    487     if (finalFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
    488         ALOGE("%s: Stream %d: Bad final pixel format "
    489                 "HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; "
    490                 "concrete pixel format required!", __FUNCTION__, stream_id);
    491         return BAD_VALUE;
    492     }
    493 
    494     ssize_t streamIndex = mStreams.indexOfKey(stream_id);
    495     if (streamIndex < 0) {
    496         ALOGE("%s: Unknown stream id %d!", __FUNCTION__, stream_id);
    497         return BAD_VALUE;
    498     }
    499 
    500     Stream &stream = mStreams.editValueAt(streamIndex);
    501 
    502     ALOGV("%s: Stream %d format set to %x, previously %x",
    503             __FUNCTION__, stream_id, finalFormat, stream.format);
    504 
    505     stream.format = finalFormat;
    506 
    507     return NO_ERROR;
    508 }
    509 
    510 int EmulatedFakeCamera2::releaseStream(uint32_t stream_id) {
    511     Mutex::Autolock l(mMutex);
    512 
    513     ssize_t streamIndex = mStreams.indexOfKey(stream_id);
    514     if (streamIndex < 0) {
    515         ALOGE("%s: Unknown stream id %d!", __FUNCTION__, stream_id);
    516         return BAD_VALUE;
    517     }
    518 
    519     if (isStreamInUse(stream_id)) {
    520         ALOGE("%s: Cannot release stream %d; in use!", __FUNCTION__,
    521                 stream_id);
    522         return BAD_VALUE;
    523     }
    524 
    525     switch(mStreams.valueAt(streamIndex).format) {
    526         case HAL_PIXEL_FORMAT_RAW16:
    527             mRawStreamCount--;
    528             break;
    529         case HAL_PIXEL_FORMAT_BLOB:
    530             mJpegStreamCount--;
    531             break;
    532         default:
    533             mProcessedStreamCount--;
    534             break;
    535     }
    536 
    537     mStreams.removeItemsAt(streamIndex);
    538 
    539     return NO_ERROR;
    540 }
    541 
    542 int EmulatedFakeCamera2::allocateReprocessStreamFromStream(
    543         uint32_t output_stream_id,
    544         const camera2_stream_in_ops_t *stream_ops,
    545         uint32_t *stream_id) {
    546     Mutex::Autolock l(mMutex);
    547 
    548     if (!mStatusPresent) {
    549         ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
    550         return ERROR_CAMERA_NOT_PRESENT;
    551     }
    552 
    553     ssize_t baseStreamIndex = mStreams.indexOfKey(output_stream_id);
    554     if (baseStreamIndex < 0) {
    555         ALOGE("%s: Unknown output stream id %d!", __FUNCTION__, output_stream_id);
    556         return BAD_VALUE;
    557     }
    558 
    559     const Stream &baseStream = mStreams[baseStreamIndex];
    560 
    561     // We'll reprocess anything we produced
    562 
    563     if (mReprocessStreamCount >= kMaxReprocessStreamCount) {
    564         ALOGE("%s: Cannot allocate another reprocess stream (%d already allocated)",
    565                 __FUNCTION__, mReprocessStreamCount);
    566         return INVALID_OPERATION;
    567     }
    568     mReprocessStreamCount++;
    569 
    570     ReprocessStream newStream;
    571     newStream.ops = stream_ops;
    572     newStream.width = baseStream.width;
    573     newStream.height = baseStream.height;
    574     newStream.format = baseStream.format;
    575     newStream.stride = baseStream.stride;
    576     newStream.sourceStreamId = output_stream_id;
    577 
    578     *stream_id = mNextReprocessStreamId;
    579     mReprocessStreams.add(mNextReprocessStreamId, newStream);
    580 
    581     ALOGV("Reprocess stream allocated: %d: %d, %d, 0x%x. Parent stream: %d",
    582             *stream_id, newStream.width, newStream.height, newStream.format,
    583             output_stream_id);
    584 
    585     mNextReprocessStreamId++;
    586     return NO_ERROR;
    587 }
    588 
    589 int EmulatedFakeCamera2::releaseReprocessStream(uint32_t stream_id) {
    590     Mutex::Autolock l(mMutex);
    591 
    592     ssize_t streamIndex = mReprocessStreams.indexOfKey(stream_id);
    593     if (streamIndex < 0) {
    594         ALOGE("%s: Unknown reprocess stream id %d!", __FUNCTION__, stream_id);
    595         return BAD_VALUE;
    596     }
    597 
    598     if (isReprocessStreamInUse(stream_id)) {
    599         ALOGE("%s: Cannot release reprocessing stream %d; in use!", __FUNCTION__,
    600                 stream_id);
    601         return BAD_VALUE;
    602     }
    603 
    604     mReprocessStreamCount--;
    605     mReprocessStreams.removeItemsAt(streamIndex);
    606 
    607     return NO_ERROR;
    608 }
    609 
    610 int EmulatedFakeCamera2::triggerAction(uint32_t trigger_id,
    611         int32_t ext1,
    612         int32_t ext2) {
    613     Mutex::Autolock l(mMutex);
    614 
    615     if (trigger_id == CAMERA2_EXT_TRIGGER_TESTING_DISCONNECT) {
    616         ALOGI("%s: Disconnect trigger - camera must be closed", __FUNCTION__);
    617         mStatusPresent = false;
    618 
    619         gEmulatedCameraFactory.onStatusChanged(
    620                 mCameraID,
    621                 CAMERA_DEVICE_STATUS_NOT_PRESENT);
    622     }
    623 
    624     if (!mStatusPresent) {
    625         ALOGW("%s: Camera was physically disconnected", __FUNCTION__);
    626         return ERROR_CAMERA_NOT_PRESENT;
    627     }
    628 
    629     return mControlThread->triggerAction(trigger_id,
    630             ext1, ext2);
    631 }
    632 
    633 /** Shutdown and debug methods */
    634 
    635 int EmulatedFakeCamera2::dump(int fd) {
    636     String8 result;
    637 
    638     result.appendFormat("    Camera HAL device: EmulatedFakeCamera2\n");
    639     result.appendFormat("      Streams:\n");
    640     for (size_t i = 0; i < mStreams.size(); i++) {
    641         int id = mStreams.keyAt(i);
    642         const Stream& s = mStreams.valueAt(i);
    643         result.appendFormat(
    644             "         Stream %d: %d x %d, format 0x%x, stride %d\n",
    645             id, s.width, s.height, s.format, s.stride);
    646     }
    647 
    648     write(fd, result.string(), result.size());
    649 
    650     return NO_ERROR;
    651 }
    652 
    653 void EmulatedFakeCamera2::signalError() {
    654     // TODO: Let parent know so we can shut down cleanly
    655     ALOGE("Worker thread is signaling a serious error");
    656 }
    657 
    658 /** Pipeline control worker thread methods */
    659 
    660 EmulatedFakeCamera2::ConfigureThread::ConfigureThread(EmulatedFakeCamera2 *parent):
    661         Thread(false),
    662         mParent(parent),
    663         mRequestCount(0),
    664         mNextBuffers(NULL) {
    665     mRunning = false;
    666 }
    667 
    668 EmulatedFakeCamera2::ConfigureThread::~ConfigureThread() {
    669 }
    670 
    671 status_t EmulatedFakeCamera2::ConfigureThread::readyToRun() {
    672     Mutex::Autolock lock(mInputMutex);
    673 
    674     ALOGV("Starting up ConfigureThread");
    675     mRequest = NULL;
    676     mActive  = false;
    677     mRunning = true;
    678 
    679     mInputSignal.signal();
    680     return NO_ERROR;
    681 }
    682 
    683 status_t EmulatedFakeCamera2::ConfigureThread::waitUntilRunning() {
    684     Mutex::Autolock lock(mInputMutex);
    685     if (!mRunning) {
    686         ALOGV("Waiting for configure thread to start");
    687         mInputSignal.wait(mInputMutex);
    688     }
    689     return OK;
    690 }
    691 
    692 status_t EmulatedFakeCamera2::ConfigureThread::newRequestAvailable() {
    693     waitUntilRunning();
    694 
    695     Mutex::Autolock lock(mInputMutex);
    696 
    697     mActive = true;
    698     mInputSignal.signal();
    699 
    700     return OK;
    701 }
    702 
    703 bool EmulatedFakeCamera2::ConfigureThread::isStreamInUse(uint32_t id) {
    704     Mutex::Autolock lock(mInternalsMutex);
    705 
    706     if (mNextBuffers == NULL) return false;
    707     for (size_t i=0; i < mNextBuffers->size(); i++) {
    708         if ((*mNextBuffers)[i].streamId == (int)id) return true;
    709     }
    710     return false;
    711 }
    712 
    713 int EmulatedFakeCamera2::ConfigureThread::getInProgressCount() {
    714     Mutex::Autolock lock(mInputMutex);
    715     return mRequestCount;
    716 }
    717 
    718 bool EmulatedFakeCamera2::ConfigureThread::threadLoop() {
    719     status_t res;
    720 
    721     // Check if we're currently processing or just waiting
    722     {
    723         Mutex::Autolock lock(mInputMutex);
    724         if (!mActive) {
    725             // Inactive, keep waiting until we've been signaled
    726             status_t res;
    727             res = mInputSignal.waitRelative(mInputMutex, kWaitPerLoop);
    728             if (res != NO_ERROR && res != TIMED_OUT) {
    729                 ALOGE("%s: Error waiting for input requests: %d",
    730                         __FUNCTION__, res);
    731                 return false;
    732             }
    733             if (!mActive) return true;
    734             ALOGV("New request available");
    735         }
    736         // Active
    737     }
    738 
    739     if (mRequest == NULL) {
    740         Mutex::Autolock il(mInternalsMutex);
    741 
    742         ALOGV("Configure: Getting next request");
    743         res = mParent->mRequestQueueSrc->dequeue_request(
    744             mParent->mRequestQueueSrc,
    745             &mRequest);
    746         if (res != NO_ERROR) {
    747             ALOGE("%s: Error dequeuing next request: %d", __FUNCTION__, res);
    748             mParent->signalError();
    749             return false;
    750         }
    751         if (mRequest == NULL) {
    752             ALOGV("Configure: Request queue empty, going inactive");
    753             // No requests available, go into inactive mode
    754             Mutex::Autolock lock(mInputMutex);
    755             mActive = false;
    756             return true;
    757         } else {
    758             Mutex::Autolock lock(mInputMutex);
    759             mRequestCount++;
    760         }
    761 
    762         camera_metadata_entry_t type;
    763         res = find_camera_metadata_entry(mRequest,
    764                 ANDROID_REQUEST_TYPE,
    765                 &type);
    766         if (res != NO_ERROR) {
    767             ALOGE("%s: error reading request type", __FUNCTION__);
    768             mParent->signalError();
    769             return false;
    770         }
    771         bool success = false;;
    772         switch (type.data.u8[0]) {
    773             case ANDROID_REQUEST_TYPE_CAPTURE:
    774                 success = setupCapture();
    775                 break;
    776             case ANDROID_REQUEST_TYPE_REPROCESS:
    777                 success = setupReprocess();
    778                 break;
    779             default:
    780                 ALOGE("%s: Unexpected request type %d",
    781                         __FUNCTION__, type.data.u8[0]);
    782                 mParent->signalError();
    783                 break;
    784         }
    785         if (!success) return false;
    786 
    787     }
    788 
    789     if (mWaitingForReadout) {
    790         bool readoutDone;
    791         readoutDone = mParent->mReadoutThread->waitForReady(kWaitPerLoop);
    792         if (!readoutDone) return true;
    793 
    794         if (mNextNeedsJpeg) {
    795             ALOGV("Configure: Waiting for JPEG compressor");
    796         } else {
    797             ALOGV("Configure: Waiting for sensor");
    798         }
    799         mWaitingForReadout = false;
    800     }
    801 
    802     if (mNextNeedsJpeg) {
    803         bool jpegDone;
    804         jpegDone = mParent->mJpegCompressor->waitForDone(kWaitPerLoop);
    805         if (!jpegDone) return true;
    806 
    807         ALOGV("Configure: Waiting for sensor");
    808         mNextNeedsJpeg = false;
    809     }
    810 
    811     if (mNextIsCapture) {
    812         return configureNextCapture();
    813     } else {
    814         return configureNextReprocess();
    815     }
    816 }
    817 
    818 bool EmulatedFakeCamera2::ConfigureThread::setupCapture() {
    819     status_t res;
    820 
    821     mNextIsCapture = true;
    822     // Get necessary parameters for sensor config
    823     mParent->mControlThread->processRequest(mRequest);
    824 
    825     camera_metadata_entry_t streams;
    826     res = find_camera_metadata_entry(mRequest,
    827             ANDROID_REQUEST_OUTPUT_STREAMS,
    828             &streams);
    829     if (res != NO_ERROR) {
    830         ALOGE("%s: error reading output stream tag", __FUNCTION__);
    831         mParent->signalError();
    832         return false;
    833     }
    834 
    835     mNextBuffers = new Buffers;
    836     mNextNeedsJpeg = false;
    837     ALOGV("Configure: Setting up buffers for capture");
    838     for (size_t i = 0; i < streams.count; i++) {
    839         int streamId = streams.data.i32[i];
    840         const Stream &s = mParent->getStreamInfo(streamId);
    841         if (s.format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
    842             ALOGE("%s: Stream %d does not have a concrete pixel format, but "
    843                     "is included in a request!", __FUNCTION__, streamId);
    844             mParent->signalError();
    845             return false;
    846         }
    847         StreamBuffer b;
    848         b.streamId = streams.data.u8[i];
    849         b.width  = s.width;
    850         b.height = s.height;
    851         b.format = s.format;
    852         b.stride = s.stride;
    853         mNextBuffers->push_back(b);
    854         ALOGV("Configure:    Buffer %zu: Stream %d, %d x %d, format 0x%x, "
    855                 "stride %d",
    856                 i, b.streamId, b.width, b.height, b.format, b.stride);
    857         if (b.format == HAL_PIXEL_FORMAT_BLOB) {
    858             mNextNeedsJpeg = true;
    859         }
    860     }
    861 
    862     camera_metadata_entry_t e;
    863     res = find_camera_metadata_entry(mRequest,
    864             ANDROID_REQUEST_FRAME_COUNT,
    865             &e);
    866     if (res != NO_ERROR) {
    867         ALOGE("%s: error reading frame count tag: %s (%d)",
    868                 __FUNCTION__, strerror(-res), res);
    869         mParent->signalError();
    870         return false;
    871     }
    872     mNextFrameNumber = *e.data.i32;
    873 
    874     res = find_camera_metadata_entry(mRequest,
    875             ANDROID_SENSOR_EXPOSURE_TIME,
    876             &e);
    877     if (res != NO_ERROR) {
    878         ALOGE("%s: error reading exposure time tag: %s (%d)",
    879                 __FUNCTION__, strerror(-res), res);
    880         mParent->signalError();
    881         return false;
    882     }
    883     mNextExposureTime = *e.data.i64;
    884 
    885     res = find_camera_metadata_entry(mRequest,
    886             ANDROID_SENSOR_FRAME_DURATION,
    887             &e);
    888     if (res != NO_ERROR) {
    889         ALOGE("%s: error reading frame duration tag", __FUNCTION__);
    890         mParent->signalError();
    891         return false;
    892     }
    893     mNextFrameDuration = *e.data.i64;
    894 
    895     if (mNextFrameDuration <
    896             mNextExposureTime + Sensor::kMinVerticalBlank) {
    897         mNextFrameDuration = mNextExposureTime + Sensor::kMinVerticalBlank;
    898     }
    899     res = find_camera_metadata_entry(mRequest,
    900             ANDROID_SENSOR_SENSITIVITY,
    901             &e);
    902     if (res != NO_ERROR) {
    903         ALOGE("%s: error reading sensitivity tag", __FUNCTION__);
    904         mParent->signalError();
    905         return false;
    906     }
    907     mNextSensitivity = *e.data.i32;
    908 
    909     // Start waiting on readout thread
    910     mWaitingForReadout = true;
    911     ALOGV("Configure: Waiting for readout thread");
    912 
    913     return true;
    914 }
    915 
    916 bool EmulatedFakeCamera2::ConfigureThread::configureNextCapture() {
    917     bool vsync = mParent->mSensor->waitForVSync(kWaitPerLoop);
    918     if (!vsync) return true;
    919 
    920     Mutex::Autolock il(mInternalsMutex);
    921     ALOGV("Configure: Configuring sensor for capture %d", mNextFrameNumber);
    922     mParent->mSensor->setExposureTime(mNextExposureTime);
    923     mParent->mSensor->setFrameDuration(mNextFrameDuration);
    924     mParent->mSensor->setSensitivity(mNextSensitivity);
    925 
    926     getBuffers();
    927 
    928     ALOGV("Configure: Done configure for capture %d", mNextFrameNumber);
    929     mParent->mReadoutThread->setNextOperation(true, mRequest, mNextBuffers);
    930     mParent->mSensor->setDestinationBuffers(mNextBuffers);
    931 
    932     mRequest = NULL;
    933     mNextBuffers = NULL;
    934 
    935     Mutex::Autolock lock(mInputMutex);
    936     mRequestCount--;
    937 
    938     return true;
    939 }
    940 
    941 bool EmulatedFakeCamera2::ConfigureThread::setupReprocess() {
    942     status_t res;
    943 
    944     mNextNeedsJpeg = true;
    945     mNextIsCapture = false;
    946 
    947     camera_metadata_entry_t reprocessStreams;
    948     res = find_camera_metadata_entry(mRequest,
    949             ANDROID_REQUEST_INPUT_STREAMS,
    950             &reprocessStreams);
    951     if (res != NO_ERROR) {
    952         ALOGE("%s: error reading output stream tag", __FUNCTION__);
    953         mParent->signalError();
    954         return false;
    955     }
    956 
    957     mNextBuffers = new Buffers;
    958 
    959     ALOGV("Configure: Setting up input buffers for reprocess");
    960     for (size_t i = 0; i < reprocessStreams.count; i++) {
    961         int streamId = reprocessStreams.data.i32[i];
    962         const ReprocessStream &s = mParent->getReprocessStreamInfo(streamId);
    963         if (s.format != HAL_PIXEL_FORMAT_RGB_888) {
    964             ALOGE("%s: Only ZSL reprocessing supported!",
    965                     __FUNCTION__);
    966             mParent->signalError();
    967             return false;
    968         }
    969         StreamBuffer b;
    970         b.streamId = -streamId;
    971         b.width = s.width;
    972         b.height = s.height;
    973         b.format = s.format;
    974         b.stride = s.stride;
    975         mNextBuffers->push_back(b);
    976     }
    977 
    978     camera_metadata_entry_t streams;
    979     res = find_camera_metadata_entry(mRequest,
    980             ANDROID_REQUEST_OUTPUT_STREAMS,
    981             &streams);
    982     if (res != NO_ERROR) {
    983         ALOGE("%s: error reading output stream tag", __FUNCTION__);
    984         mParent->signalError();
    985         return false;
    986     }
    987 
    988     ALOGV("Configure: Setting up output buffers for reprocess");
    989     for (size_t i = 0; i < streams.count; i++) {
    990         int streamId = streams.data.i32[i];
    991         const Stream &s = mParent->getStreamInfo(streamId);
    992         if (s.format != HAL_PIXEL_FORMAT_BLOB) {
    993             // TODO: Support reprocess to YUV
    994             ALOGE("%s: Non-JPEG output stream %d for reprocess not supported",
    995                     __FUNCTION__, streamId);
    996             mParent->signalError();
    997             return false;
    998         }
    999         StreamBuffer b;
   1000         b.streamId = streams.data.u8[i];
   1001         b.width  = s.width;
   1002         b.height = s.height;
   1003         b.format = s.format;
   1004         b.stride = s.stride;
   1005         mNextBuffers->push_back(b);
   1006         ALOGV("Configure:    Buffer %zu: Stream %d, %d x %d, format 0x%x, "
   1007                 "stride %d",
   1008                 i, b.streamId, b.width, b.height, b.format, b.stride);
   1009     }
   1010 
   1011     camera_metadata_entry_t e;
   1012     res = find_camera_metadata_entry(mRequest,
   1013             ANDROID_REQUEST_FRAME_COUNT,
   1014             &e);
   1015     if (res != NO_ERROR) {
   1016         ALOGE("%s: error reading frame count tag: %s (%d)",
   1017                 __FUNCTION__, strerror(-res), res);
   1018         mParent->signalError();
   1019         return false;
   1020     }
   1021     mNextFrameNumber = *e.data.i32;
   1022 
   1023     return true;
   1024 }
   1025 
   1026 bool EmulatedFakeCamera2::ConfigureThread::configureNextReprocess() {
   1027     Mutex::Autolock il(mInternalsMutex);
   1028 
   1029     getBuffers();
   1030 
   1031     ALOGV("Configure: Done configure for reprocess %d", mNextFrameNumber);
   1032     mParent->mReadoutThread->setNextOperation(false, mRequest, mNextBuffers);
   1033 
   1034     mRequest = NULL;
   1035     mNextBuffers = NULL;
   1036 
   1037     Mutex::Autolock lock(mInputMutex);
   1038     mRequestCount--;
   1039 
   1040     return true;
   1041 }
   1042 
   1043 bool EmulatedFakeCamera2::ConfigureThread::getBuffers() {
   1044     status_t res;
   1045     /** Get buffers to fill for this frame */
   1046     for (size_t i = 0; i < mNextBuffers->size(); i++) {
   1047         StreamBuffer &b = mNextBuffers->editItemAt(i);
   1048 
   1049         if (b.streamId > 0) {
   1050             Stream s = mParent->getStreamInfo(b.streamId);
   1051             ALOGV("Configure: Dequeing buffer from stream %d", b.streamId);
   1052             res = s.ops->dequeue_buffer(s.ops, &(b.buffer) );
   1053             if (res != NO_ERROR || b.buffer == NULL) {
   1054                 ALOGE("%s: Unable to dequeue buffer from stream %d: %s (%d)",
   1055                         __FUNCTION__, b.streamId, strerror(-res), res);
   1056                 mParent->signalError();
   1057                 return false;
   1058             }
   1059 
   1060             /* Lock the buffer from the perspective of the graphics mapper */
   1061             const Rect rect(s.width, s.height);
   1062 
   1063             res = GraphicBufferMapper::get().lock(*(b.buffer),
   1064                     GRALLOC_USAGE_HW_CAMERA_WRITE,
   1065                     rect, (void**)&(b.img) );
   1066 
   1067             if (res != NO_ERROR) {
   1068                 ALOGE("%s: grbuffer_mapper.lock failure: %s (%d)",
   1069                         __FUNCTION__, strerror(-res), res);
   1070                 s.ops->cancel_buffer(s.ops,
   1071                         b.buffer);
   1072                 mParent->signalError();
   1073                 return false;
   1074             }
   1075         } else {
   1076             ReprocessStream s = mParent->getReprocessStreamInfo(-b.streamId);
   1077             ALOGV("Configure: Acquiring buffer from reprocess stream %d",
   1078                     -b.streamId);
   1079             res = s.ops->acquire_buffer(s.ops, &(b.buffer) );
   1080             if (res != NO_ERROR || b.buffer == NULL) {
   1081                 ALOGE("%s: Unable to acquire buffer from reprocess stream %d: "
   1082                         "%s (%d)", __FUNCTION__, -b.streamId,
   1083                         strerror(-res), res);
   1084                 mParent->signalError();
   1085                 return false;
   1086             }
   1087 
   1088             /* Lock the buffer from the perspective of the graphics mapper */
   1089             const Rect rect(s.width, s.height);
   1090 
   1091             res = GraphicBufferMapper::get().lock(*(b.buffer),
   1092                     GRALLOC_USAGE_HW_CAMERA_READ,
   1093                     rect, (void**)&(b.img) );
   1094             if (res != NO_ERROR) {
   1095                 ALOGE("%s: grbuffer_mapper.lock failure: %s (%d)",
   1096                         __FUNCTION__, strerror(-res), res);
   1097                 s.ops->release_buffer(s.ops,
   1098                         b.buffer);
   1099                 mParent->signalError();
   1100                 return false;
   1101             }
   1102         }
   1103     }
   1104     return true;
   1105 }
   1106 
   1107 EmulatedFakeCamera2::ReadoutThread::ReadoutThread(EmulatedFakeCamera2 *parent):
   1108         Thread(false),
   1109         mParent(parent),
   1110         mRunning(false),
   1111         mActive(false),
   1112         mRequestCount(0),
   1113         mRequest(NULL),
   1114         mBuffers(NULL) {
   1115     mInFlightQueue = new InFlightQueue[kInFlightQueueSize];
   1116     mInFlightHead = 0;
   1117     mInFlightTail = 0;
   1118 }
   1119 
   1120 EmulatedFakeCamera2::ReadoutThread::~ReadoutThread() {
   1121     delete mInFlightQueue;
   1122 }
   1123 
   1124 status_t EmulatedFakeCamera2::ReadoutThread::readyToRun() {
   1125     Mutex::Autolock lock(mInputMutex);
   1126     ALOGV("Starting up ReadoutThread");
   1127     mRunning = true;
   1128     mInputSignal.signal();
   1129     return NO_ERROR;
   1130 }
   1131 
   1132 status_t EmulatedFakeCamera2::ReadoutThread::waitUntilRunning() {
   1133     Mutex::Autolock lock(mInputMutex);
   1134     if (!mRunning) {
   1135         ALOGV("Waiting for readout thread to start");
   1136         mInputSignal.wait(mInputMutex);
   1137     }
   1138     return OK;
   1139 }
   1140 
   1141 bool EmulatedFakeCamera2::ReadoutThread::waitForReady(nsecs_t timeout) {
   1142     status_t res;
   1143     Mutex::Autolock lock(mInputMutex);
   1144     while (!readyForNextCapture()) {
   1145         res = mReadySignal.waitRelative(mInputMutex, timeout);
   1146         if (res == TIMED_OUT) return false;
   1147         if (res != OK) {
   1148             ALOGE("%s: Error waiting for ready: %s (%d)", __FUNCTION__,
   1149                     strerror(-res), res);
   1150             return false;
   1151         }
   1152     }
   1153     return true;
   1154 }
   1155 
   1156 bool EmulatedFakeCamera2::ReadoutThread::readyForNextCapture() {
   1157     return (mInFlightTail + 1) % kInFlightQueueSize != mInFlightHead;
   1158 }
   1159 
   1160 void EmulatedFakeCamera2::ReadoutThread::setNextOperation(
   1161         bool isCapture,
   1162         camera_metadata_t *request,
   1163         Buffers *buffers) {
   1164     Mutex::Autolock lock(mInputMutex);
   1165     if ( !readyForNextCapture() ) {
   1166         ALOGE("In flight queue full, dropping captures");
   1167         mParent->signalError();
   1168         return;
   1169     }
   1170     mInFlightQueue[mInFlightTail].isCapture = isCapture;
   1171     mInFlightQueue[mInFlightTail].request = request;
   1172     mInFlightQueue[mInFlightTail].buffers = buffers;
   1173     mInFlightTail = (mInFlightTail + 1) % kInFlightQueueSize;
   1174     mRequestCount++;
   1175 
   1176     if (!mActive) {
   1177         mActive = true;
   1178         mInputSignal.signal();
   1179     }
   1180 }
   1181 
   1182 bool EmulatedFakeCamera2::ReadoutThread::isStreamInUse(uint32_t id) {
   1183     // acquire in same order as threadLoop
   1184     Mutex::Autolock iLock(mInternalsMutex);
   1185     Mutex::Autolock lock(mInputMutex);
   1186 
   1187     size_t i = mInFlightHead;
   1188     while (i != mInFlightTail) {
   1189         for (size_t j = 0; j < mInFlightQueue[i].buffers->size(); j++) {
   1190             if ( (*(mInFlightQueue[i].buffers))[j].streamId == (int)id )
   1191                 return true;
   1192         }
   1193         i = (i + 1) % kInFlightQueueSize;
   1194     }
   1195 
   1196 
   1197     if (mBuffers != NULL) {
   1198         for (i = 0; i < mBuffers->size(); i++) {
   1199             if ( (*mBuffers)[i].streamId == (int)id) return true;
   1200         }
   1201     }
   1202 
   1203     return false;
   1204 }
   1205 
   1206 int EmulatedFakeCamera2::ReadoutThread::getInProgressCount() {
   1207     Mutex::Autolock lock(mInputMutex);
   1208 
   1209     return mRequestCount;
   1210 }
   1211 
   1212 bool EmulatedFakeCamera2::ReadoutThread::threadLoop() {
   1213     static const nsecs_t kWaitPerLoop = 10000000L; // 10 ms
   1214     status_t res;
   1215     int32_t frameNumber;
   1216 
   1217     // Check if we're currently processing or just waiting
   1218     {
   1219         Mutex::Autolock lock(mInputMutex);
   1220         if (!mActive) {
   1221             // Inactive, keep waiting until we've been signaled
   1222             res = mInputSignal.waitRelative(mInputMutex, kWaitPerLoop);
   1223             if (res != NO_ERROR && res != TIMED_OUT) {
   1224                 ALOGE("%s: Error waiting for capture requests: %d",
   1225                         __FUNCTION__, res);
   1226                 mParent->signalError();
   1227                 return false;
   1228             }
   1229             if (!mActive) return true;
   1230         }
   1231         // Active, see if we need a new request
   1232         if (mRequest == NULL) {
   1233             if (mInFlightHead == mInFlightTail) {
   1234                 // Go inactive
   1235                 ALOGV("Waiting for sensor data");
   1236                 mActive = false;
   1237                 return true;
   1238             } else {
   1239                 Mutex::Autolock iLock(mInternalsMutex);
   1240                 mReadySignal.signal();
   1241                 mIsCapture = mInFlightQueue[mInFlightHead].isCapture;
   1242                 mRequest = mInFlightQueue[mInFlightHead].request;
   1243                 mBuffers  = mInFlightQueue[mInFlightHead].buffers;
   1244                 mInFlightQueue[mInFlightHead].request = NULL;
   1245                 mInFlightQueue[mInFlightHead].buffers = NULL;
   1246                 mInFlightHead = (mInFlightHead + 1) % kInFlightQueueSize;
   1247                 ALOGV("Ready to read out request %p, %zu buffers",
   1248                         mRequest, mBuffers->size());
   1249             }
   1250         }
   1251     }
   1252 
   1253     // Active with request, wait on sensor to complete
   1254 
   1255     nsecs_t captureTime;
   1256 
   1257     if (mIsCapture) {
   1258         bool gotFrame;
   1259         gotFrame = mParent->mSensor->waitForNewFrame(kWaitPerLoop,
   1260                 &captureTime);
   1261 
   1262         if (!gotFrame) return true;
   1263     }
   1264 
   1265     Mutex::Autolock iLock(mInternalsMutex);
   1266 
   1267     camera_metadata_entry_t entry;
   1268     if (!mIsCapture) {
   1269         res = find_camera_metadata_entry(mRequest,
   1270                 ANDROID_SENSOR_TIMESTAMP,
   1271             &entry);
   1272         if (res != NO_ERROR) {
   1273             ALOGE("%s: error reading reprocessing timestamp: %s (%d)",
   1274                     __FUNCTION__, strerror(-res), res);
   1275             mParent->signalError();
   1276             return false;
   1277         }
   1278         captureTime = entry.data.i64[0];
   1279     }
   1280 
   1281     res = find_camera_metadata_entry(mRequest,
   1282             ANDROID_REQUEST_FRAME_COUNT,
   1283             &entry);
   1284     if (res != NO_ERROR) {
   1285         ALOGE("%s: error reading frame count tag: %s (%d)",
   1286                 __FUNCTION__, strerror(-res), res);
   1287         mParent->signalError();
   1288         return false;
   1289     }
   1290     frameNumber = *entry.data.i32;
   1291 
   1292     res = find_camera_metadata_entry(mRequest,
   1293             ANDROID_REQUEST_METADATA_MODE,
   1294             &entry);
   1295     if (res != NO_ERROR) {
   1296         ALOGE("%s: error reading metadata mode tag: %s (%d)",
   1297                 __FUNCTION__, strerror(-res), res);
   1298         mParent->signalError();
   1299         return false;
   1300     }
   1301 
   1302     // Got sensor data and request, construct frame and send it out
   1303     ALOGV("Readout: Constructing metadata and frames for request %d",
   1304             frameNumber);
   1305 
   1306     if (*entry.data.u8 == ANDROID_REQUEST_METADATA_MODE_FULL) {
   1307         ALOGV("Readout: Metadata requested, constructing");
   1308 
   1309         camera_metadata_t *frame = NULL;
   1310 
   1311         size_t frame_entries = get_camera_metadata_entry_count(mRequest);
   1312         size_t frame_data    = get_camera_metadata_data_count(mRequest);
   1313 
   1314         // TODO: Dynamically calculate based on enabled statistics, etc
   1315         frame_entries += 10;
   1316         frame_data += 100;
   1317 
   1318         res = mParent->mFrameQueueDst->dequeue_frame(mParent->mFrameQueueDst,
   1319                 frame_entries, frame_data, &frame);
   1320 
   1321         if (res != NO_ERROR || frame == NULL) {
   1322             ALOGE("%s: Unable to dequeue frame metadata buffer", __FUNCTION__);
   1323             mParent->signalError();
   1324             return false;
   1325         }
   1326 
   1327         res = append_camera_metadata(frame, mRequest);
   1328         if (res != NO_ERROR) {
   1329             ALOGE("Unable to append request metadata");
   1330         }
   1331 
   1332         if (mIsCapture) {
   1333             add_camera_metadata_entry(frame,
   1334                     ANDROID_SENSOR_TIMESTAMP,
   1335                     &captureTime,
   1336                     1);
   1337 
   1338             collectStatisticsMetadata(frame);
   1339             // TODO: Collect all final values used from sensor in addition to timestamp
   1340         }
   1341 
   1342         ALOGV("Readout: Enqueue frame %d", frameNumber);
   1343         mParent->mFrameQueueDst->enqueue_frame(mParent->mFrameQueueDst,
   1344                 frame);
   1345     }
   1346     ALOGV("Readout: Free request");
   1347     res = mParent->mRequestQueueSrc->free_request(mParent->mRequestQueueSrc, mRequest);
   1348     if (res != NO_ERROR) {
   1349         ALOGE("%s: Unable to return request buffer to queue: %d",
   1350                 __FUNCTION__, res);
   1351         mParent->signalError();
   1352         return false;
   1353     }
   1354     mRequest = NULL;
   1355 
   1356     int compressedBufferIndex = -1;
   1357     ALOGV("Readout: Processing %zu buffers", mBuffers->size());
   1358     for (size_t i = 0; i < mBuffers->size(); i++) {
   1359         const StreamBuffer &b = (*mBuffers)[i];
   1360         ALOGV("Readout:    Buffer %zu: Stream %d, %d x %d, format 0x%x, stride %d",
   1361                 i, b.streamId, b.width, b.height, b.format, b.stride);
   1362         if (b.streamId > 0) {
   1363             if (b.format == HAL_PIXEL_FORMAT_BLOB) {
   1364                 // Assumes only one BLOB buffer type per capture
   1365                 compressedBufferIndex = i;
   1366             } else {
   1367                 ALOGV("Readout:    Sending image buffer %zu (%p) to output stream %d",
   1368                         i, (void*)*(b.buffer), b.streamId);
   1369                 GraphicBufferMapper::get().unlock(*(b.buffer));
   1370                 const Stream &s = mParent->getStreamInfo(b.streamId);
   1371                 res = s.ops->enqueue_buffer(s.ops, captureTime, b.buffer);
   1372                 if (res != OK) {
   1373                     ALOGE("Error enqueuing image buffer %p: %s (%d)", b.buffer,
   1374                             strerror(-res), res);
   1375                     mParent->signalError();
   1376                 }
   1377             }
   1378         }
   1379     }
   1380 
   1381     if (compressedBufferIndex == -1) {
   1382         delete mBuffers;
   1383     } else {
   1384         ALOGV("Readout:  Starting JPEG compression for buffer %d, stream %d",
   1385                 compressedBufferIndex,
   1386                 (*mBuffers)[compressedBufferIndex].streamId);
   1387         mJpegTimestamp = captureTime;
   1388         // Takes ownership of mBuffers
   1389         mParent->mJpegCompressor->start(mBuffers, this);
   1390     }
   1391     mBuffers = NULL;
   1392 
   1393     Mutex::Autolock l(mInputMutex);
   1394     mRequestCount--;
   1395     ALOGV("Readout: Done with request %d", frameNumber);
   1396     return true;
   1397 }
   1398 
   1399 void EmulatedFakeCamera2::ReadoutThread::onJpegDone(
   1400         const StreamBuffer &jpegBuffer, bool success) {
   1401     status_t res;
   1402     if (!success) {
   1403         ALOGE("%s: Error queueing compressed image buffer %p",
   1404                 __FUNCTION__, jpegBuffer.buffer);
   1405         mParent->signalError();
   1406         return;
   1407     }
   1408 
   1409     // Write to JPEG output stream
   1410     ALOGV("%s: Compression complete, pushing to stream %d", __FUNCTION__,
   1411             jpegBuffer.streamId);
   1412 
   1413     GraphicBufferMapper::get().unlock(*(jpegBuffer.buffer));
   1414     const Stream &s = mParent->getStreamInfo(jpegBuffer.streamId);
   1415     res = s.ops->enqueue_buffer(s.ops, mJpegTimestamp, jpegBuffer.buffer);
   1416 }
   1417 
   1418 void EmulatedFakeCamera2::ReadoutThread::onJpegInputDone(
   1419         const StreamBuffer &inputBuffer) {
   1420     status_t res;
   1421     GraphicBufferMapper::get().unlock(*(inputBuffer.buffer));
   1422     const ReprocessStream &s =
   1423             mParent->getReprocessStreamInfo(-inputBuffer.streamId);
   1424     res = s.ops->release_buffer(s.ops, inputBuffer.buffer);
   1425     if (res != OK) {
   1426         ALOGE("Error releasing reprocess buffer %p: %s (%d)",
   1427                 inputBuffer.buffer, strerror(-res), res);
   1428         mParent->signalError();
   1429     }
   1430 }
   1431 
   1432 status_t EmulatedFakeCamera2::ReadoutThread::collectStatisticsMetadata(
   1433         camera_metadata_t *frame) {
   1434     // Completely fake face rectangles, don't correspond to real faces in scene
   1435     ALOGV("Readout:    Collecting statistics metadata");
   1436 
   1437     status_t res;
   1438     camera_metadata_entry_t entry;
   1439     res = find_camera_metadata_entry(frame,
   1440                 ANDROID_STATISTICS_FACE_DETECT_MODE,
   1441                 &entry);
   1442     if (res != OK) {
   1443         ALOGE("%s: Unable to find face detect mode!", __FUNCTION__);
   1444         return BAD_VALUE;
   1445     }
   1446 
   1447     if (entry.data.u8[0] == ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) return OK;
   1448 
   1449     // The coordinate system for the face regions is the raw sensor pixel
   1450     // coordinates. Here, we map from the scene coordinates (0-19 in both axis)
   1451     // to raw pixels, for the scene defined in fake-pipeline2/Scene.cpp. We
   1452     // approximately place two faces on top of the windows of the house. No
   1453     // actual faces exist there, but might one day. Note that this doesn't
   1454     // account for the offsets used to account for aspect ratio differences, so
   1455     // the rectangles don't line up quite right.
   1456     const size_t numFaces = 2;
   1457     int32_t rects[numFaces * 4] = {
   1458         static_cast<int32_t>(Sensor::kResolution[0] * 10 / 20),
   1459         static_cast<int32_t>(Sensor::kResolution[1] * 15 / 20),
   1460         static_cast<int32_t>(Sensor::kResolution[0] * 12 / 20),
   1461         static_cast<int32_t>(Sensor::kResolution[1] * 17 / 20),
   1462 
   1463         static_cast<int32_t>(Sensor::kResolution[0] * 16 / 20),
   1464         static_cast<int32_t>(Sensor::kResolution[1] * 15 / 20),
   1465         static_cast<int32_t>(Sensor::kResolution[0] * 18 / 20),
   1466         static_cast<int32_t>(Sensor::kResolution[1] * 17 / 20)
   1467     };
   1468     // To simulate some kind of real detection going on, we jitter the rectangles on
   1469     // each frame by a few pixels in each dimension.
   1470     for (size_t i = 0; i < numFaces * 4; i++) {
   1471         rects[i] += (int32_t)(((float)rand() / RAND_MAX) * 6 - 3);
   1472     }
   1473     // The confidence scores (0-100) are similarly jittered.
   1474     uint8_t scores[numFaces] = { 85, 95 };
   1475     for (size_t i = 0; i < numFaces; i++) {
   1476         scores[i] += (int32_t)(((float)rand() / RAND_MAX) * 10 - 5);
   1477     }
   1478 
   1479     res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_RECTANGLES,
   1480             rects, numFaces * 4);
   1481     if (res != OK) {
   1482         ALOGE("%s: Unable to add face rectangles!", __FUNCTION__);
   1483         return BAD_VALUE;
   1484     }
   1485 
   1486     res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_SCORES,
   1487             scores, numFaces);
   1488     if (res != OK) {
   1489         ALOGE("%s: Unable to add face scores!", __FUNCTION__);
   1490         return BAD_VALUE;
   1491     }
   1492 
   1493     if (entry.data.u8[0] == ANDROID_STATISTICS_FACE_DETECT_MODE_SIMPLE) return OK;
   1494 
   1495     // Advanced face detection options - add eye/mouth coordinates.  The
   1496     // coordinates in order are (leftEyeX, leftEyeY, rightEyeX, rightEyeY,
   1497     // mouthX, mouthY). The mapping is the same as the face rectangles.
   1498     int32_t features[numFaces * 6] = {
   1499         static_cast<int32_t>(Sensor::kResolution[0] * 10.5 / 20),
   1500         static_cast<int32_t>(Sensor::kResolution[1] * 16 / 20),
   1501         static_cast<int32_t>(Sensor::kResolution[0] * 11.5 / 20),
   1502         static_cast<int32_t>(Sensor::kResolution[1] * 16 / 20),
   1503         static_cast<int32_t>(Sensor::kResolution[0] * 11 / 20),
   1504         static_cast<int32_t>(Sensor::kResolution[1] * 16.5 / 20),
   1505 
   1506         static_cast<int32_t>(Sensor::kResolution[0] * 16.5 / 20),
   1507         static_cast<int32_t>(Sensor::kResolution[1] * 16 / 20),
   1508         static_cast<int32_t>(Sensor::kResolution[0] * 17.5 / 20),
   1509         static_cast<int32_t>(Sensor::kResolution[1] * 16 / 20),
   1510         static_cast<int32_t>(Sensor::kResolution[0] * 17 / 20),
   1511         static_cast<int32_t>(Sensor::kResolution[1] * 16.5 / 20),
   1512     };
   1513     // Jitter these a bit less than the rects
   1514     for (size_t i = 0; i < numFaces * 6; i++) {
   1515         features[i] += (int32_t)(((float)rand() / RAND_MAX) * 4 - 2);
   1516     }
   1517     // These are unique IDs that are used to identify each face while it's
   1518     // visible to the detector (if a face went away and came back, it'd get a
   1519     // new ID).
   1520     int32_t ids[numFaces] = {
   1521         100, 200
   1522     };
   1523 
   1524     res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_LANDMARKS,
   1525             features, numFaces * 6);
   1526     if (res != OK) {
   1527         ALOGE("%s: Unable to add face landmarks!", __FUNCTION__);
   1528         return BAD_VALUE;
   1529     }
   1530 
   1531     res = add_camera_metadata_entry(frame, ANDROID_STATISTICS_FACE_IDS,
   1532             ids, numFaces);
   1533     if (res != OK) {
   1534         ALOGE("%s: Unable to add face scores!", __FUNCTION__);
   1535         return BAD_VALUE;
   1536     }
   1537 
   1538     return OK;
   1539 }
   1540 
   1541 EmulatedFakeCamera2::ControlThread::ControlThread(EmulatedFakeCamera2 *parent):
   1542         Thread(false),
   1543         mParent(parent) {
   1544     mRunning = false;
   1545 }
   1546 
   1547 EmulatedFakeCamera2::ControlThread::~ControlThread() {
   1548 }
   1549 
   1550 status_t EmulatedFakeCamera2::ControlThread::readyToRun() {
   1551     Mutex::Autolock lock(mInputMutex);
   1552 
   1553     ALOGV("Starting up ControlThread");
   1554     mRunning = true;
   1555     mStartAf = false;
   1556     mCancelAf = false;
   1557     mStartPrecapture = false;
   1558 
   1559     mControlMode = ANDROID_CONTROL_MODE_AUTO;
   1560 
   1561     mEffectMode = ANDROID_CONTROL_EFFECT_MODE_OFF;
   1562     mSceneMode = ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY;
   1563 
   1564     mAfMode = ANDROID_CONTROL_AF_MODE_AUTO;
   1565     mAfModeChange = false;
   1566 
   1567     mAeMode = ANDROID_CONTROL_AE_MODE_ON;
   1568     mAwbMode = ANDROID_CONTROL_AWB_MODE_AUTO;
   1569 
   1570     mAfTriggerId = 0;
   1571     mPrecaptureTriggerId = 0;
   1572 
   1573     mAfState = ANDROID_CONTROL_AF_STATE_INACTIVE;
   1574     mAeState = ANDROID_CONTROL_AE_STATE_INACTIVE;
   1575     mAwbState = ANDROID_CONTROL_AWB_STATE_INACTIVE;
   1576 
   1577     mExposureTime = kNormalExposureTime;
   1578 
   1579     mInputSignal.signal();
   1580     return NO_ERROR;
   1581 }
   1582 
   1583 status_t EmulatedFakeCamera2::ControlThread::waitUntilRunning() {
   1584     Mutex::Autolock lock(mInputMutex);
   1585     if (!mRunning) {
   1586         ALOGV("Waiting for control thread to start");
   1587         mInputSignal.wait(mInputMutex);
   1588     }
   1589     return OK;
   1590 }
   1591 
   1592 // Override android.control.* fields with 3A values before sending request to sensor
   1593 status_t EmulatedFakeCamera2::ControlThread::processRequest(camera_metadata_t *request) {
   1594     Mutex::Autolock lock(mInputMutex);
   1595     // TODO: Add handling for all android.control.* fields here
   1596     camera_metadata_entry_t mode;
   1597     status_t res;
   1598 
   1599 #define READ_IF_OK(res, what, def)                                             \
   1600     (((res) == OK) ? (what) : (uint8_t)(def))
   1601 
   1602     res = find_camera_metadata_entry(request,
   1603             ANDROID_CONTROL_MODE,
   1604             &mode);
   1605     mControlMode = READ_IF_OK(res, mode.data.u8[0], ANDROID_CONTROL_MODE_OFF);
   1606 
   1607     // disable all 3A
   1608     if (mControlMode == ANDROID_CONTROL_MODE_OFF) {
   1609         mEffectMode =   ANDROID_CONTROL_EFFECT_MODE_OFF;
   1610         mSceneMode =    ANDROID_CONTROL_SCENE_MODE_DISABLED;
   1611         mAfMode =       ANDROID_CONTROL_AF_MODE_OFF;
   1612         mAeLock =       ANDROID_CONTROL_AE_LOCK_ON;
   1613         mAeMode =       ANDROID_CONTROL_AE_MODE_OFF;
   1614         mAfModeChange = true;
   1615         mStartAf =      false;
   1616         mCancelAf =     true;
   1617         mAeState =      ANDROID_CONTROL_AE_STATE_INACTIVE;
   1618         mAwbMode =      ANDROID_CONTROL_AWB_MODE_OFF;
   1619         return res;
   1620     }
   1621 
   1622     res = find_camera_metadata_entry(request,
   1623             ANDROID_CONTROL_EFFECT_MODE,
   1624             &mode);
   1625     mEffectMode = READ_IF_OK(res, mode.data.u8[0],
   1626                              ANDROID_CONTROL_EFFECT_MODE_OFF);
   1627 
   1628     res = find_camera_metadata_entry(request,
   1629             ANDROID_CONTROL_SCENE_MODE,
   1630             &mode);
   1631     mSceneMode = READ_IF_OK(res, mode.data.u8[0],
   1632                              ANDROID_CONTROL_SCENE_MODE_DISABLED);
   1633 
   1634     res = find_camera_metadata_entry(request,
   1635             ANDROID_CONTROL_AF_MODE,
   1636             &mode);
   1637     if (mAfMode != mode.data.u8[0]) {
   1638         ALOGV("AF new mode: %d, old mode %d", mode.data.u8[0], mAfMode);
   1639         mAfMode = mode.data.u8[0];
   1640         mAfModeChange = true;
   1641         mStartAf = false;
   1642         mCancelAf = false;
   1643     }
   1644 
   1645     res = find_camera_metadata_entry(request,
   1646             ANDROID_CONTROL_AE_MODE,
   1647             &mode);
   1648     mAeMode = READ_IF_OK(res, mode.data.u8[0],
   1649                              ANDROID_CONTROL_AE_MODE_OFF);
   1650 
   1651     res = find_camera_metadata_entry(request,
   1652             ANDROID_CONTROL_AE_LOCK,
   1653             &mode);
   1654     uint8_t aeLockVal = READ_IF_OK(res, mode.data.u8[0],
   1655                                    ANDROID_CONTROL_AE_LOCK_ON);
   1656     bool aeLock = (aeLockVal == ANDROID_CONTROL_AE_LOCK_ON);
   1657     if (mAeLock && !aeLock) {
   1658         mAeState = ANDROID_CONTROL_AE_STATE_INACTIVE;
   1659     }
   1660     mAeLock = aeLock;
   1661 
   1662     res = find_camera_metadata_entry(request,
   1663             ANDROID_CONTROL_AWB_MODE,
   1664             &mode);
   1665     mAwbMode = READ_IF_OK(res, mode.data.u8[0],
   1666                           ANDROID_CONTROL_AWB_MODE_OFF);
   1667 
   1668     // TODO: Override more control fields
   1669 
   1670     if (mAeMode != ANDROID_CONTROL_AE_MODE_OFF) {
   1671         camera_metadata_entry_t exposureTime;
   1672         res = find_camera_metadata_entry(request,
   1673                 ANDROID_SENSOR_EXPOSURE_TIME,
   1674                 &exposureTime);
   1675         if (res == OK) {
   1676             exposureTime.data.i64[0] = mExposureTime;
   1677         }
   1678     }
   1679 
   1680 #undef READ_IF_OK
   1681 
   1682     return OK;
   1683 }
   1684 
   1685 status_t EmulatedFakeCamera2::ControlThread::triggerAction(uint32_t msgType,
   1686         int32_t ext1, int32_t ext2) {
   1687     ALOGV("%s: Triggering %d (%d, %d)", __FUNCTION__, msgType, ext1, ext2);
   1688     Mutex::Autolock lock(mInputMutex);
   1689     switch (msgType) {
   1690         case CAMERA2_TRIGGER_AUTOFOCUS:
   1691             mAfTriggerId = ext1;
   1692             mStartAf = true;
   1693             mCancelAf = false;
   1694             break;
   1695         case CAMERA2_TRIGGER_CANCEL_AUTOFOCUS:
   1696             mAfTriggerId = ext1;
   1697             mStartAf = false;
   1698             mCancelAf = true;
   1699             break;
   1700         case CAMERA2_TRIGGER_PRECAPTURE_METERING:
   1701             mPrecaptureTriggerId = ext1;
   1702             mStartPrecapture = true;
   1703             break;
   1704         default:
   1705             ALOGE("%s: Unknown action triggered: %d (arguments %d %d)",
   1706                     __FUNCTION__, msgType, ext1, ext2);
   1707             return BAD_VALUE;
   1708     }
   1709     return OK;
   1710 }
   1711 
   1712 const nsecs_t EmulatedFakeCamera2::ControlThread::kControlCycleDelay = 100 * MSEC;
   1713 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinAfDuration = 500 * MSEC;
   1714 const nsecs_t EmulatedFakeCamera2::ControlThread::kMaxAfDuration = 900 * MSEC;
   1715 const float EmulatedFakeCamera2::ControlThread::kAfSuccessRate = 0.9;
   1716  // Once every 5 seconds
   1717 const float EmulatedFakeCamera2::ControlThread::kContinuousAfStartRate =
   1718         kControlCycleDelay / 5.0 * SEC;
   1719 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinAeDuration = 500 * MSEC;
   1720 const nsecs_t EmulatedFakeCamera2::ControlThread::kMaxAeDuration = 2 * SEC;
   1721 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinPrecaptureAeDuration = 100 * MSEC;
   1722 const nsecs_t EmulatedFakeCamera2::ControlThread::kMaxPrecaptureAeDuration = 400 * MSEC;
   1723  // Once every 3 seconds
   1724 const float EmulatedFakeCamera2::ControlThread::kAeScanStartRate =
   1725     kControlCycleDelay / 3000000000.0;
   1726 
   1727 const nsecs_t EmulatedFakeCamera2::ControlThread::kNormalExposureTime = 10 * MSEC;
   1728 const nsecs_t EmulatedFakeCamera2::ControlThread::kExposureJump = 2 * MSEC;
   1729 const nsecs_t EmulatedFakeCamera2::ControlThread::kMinExposureTime = 1 * MSEC;
   1730 
   1731 bool EmulatedFakeCamera2::ControlThread::threadLoop() {
   1732     bool afModeChange = false;
   1733     bool afTriggered = false;
   1734     bool afCancelled = false;
   1735     uint8_t afState;
   1736     uint8_t afMode;
   1737     int32_t afTriggerId;
   1738     bool precaptureTriggered = false;
   1739     uint8_t aeState;
   1740     uint8_t aeMode;
   1741     bool    aeLock;
   1742     int32_t precaptureTriggerId;
   1743     nsecs_t nextSleep = kControlCycleDelay;
   1744 
   1745     {
   1746         Mutex::Autolock lock(mInputMutex);
   1747         if (mStartAf) {
   1748             ALOGD("Starting AF trigger processing");
   1749             afTriggered = true;
   1750             mStartAf = false;
   1751         } else if (mCancelAf) {
   1752             ALOGD("Starting cancel AF trigger processing");
   1753             afCancelled = true;
   1754             mCancelAf = false;
   1755         }
   1756         afState = mAfState;
   1757         afMode = mAfMode;
   1758         afModeChange = mAfModeChange;
   1759         mAfModeChange = false;
   1760 
   1761         afTriggerId = mAfTriggerId;
   1762 
   1763         if(mStartPrecapture) {
   1764             ALOGD("Starting precapture trigger processing");
   1765             precaptureTriggered = true;
   1766             mStartPrecapture = false;
   1767         }
   1768         aeState = mAeState;
   1769         aeMode = mAeMode;
   1770         aeLock = mAeLock;
   1771         precaptureTriggerId = mPrecaptureTriggerId;
   1772     }
   1773 
   1774     if (afCancelled || afModeChange) {
   1775         ALOGV("Resetting AF state due to cancel/mode change");
   1776         afState = ANDROID_CONTROL_AF_STATE_INACTIVE;
   1777         updateAfState(afState, afTriggerId);
   1778         mAfScanDuration = 0;
   1779         mLockAfterPassiveScan = false;
   1780     }
   1781 
   1782     uint8_t oldAfState = afState;
   1783 
   1784     if (afTriggered) {
   1785         afState = processAfTrigger(afMode, afState);
   1786     }
   1787 
   1788     afState = maybeStartAfScan(afMode, afState);
   1789     afState = updateAfScan(afMode, afState, &nextSleep);
   1790     updateAfState(afState, afTriggerId);
   1791 
   1792     if (precaptureTriggered) {
   1793         aeState = processPrecaptureTrigger(aeMode, aeState);
   1794     }
   1795 
   1796     aeState = maybeStartAeScan(aeMode, aeLock, aeState);
   1797     aeState = updateAeScan(aeMode, aeLock, aeState, &nextSleep);
   1798     updateAeState(aeState, precaptureTriggerId);
   1799 
   1800     int ret;
   1801     timespec t;
   1802     t.tv_sec = 0;
   1803     t.tv_nsec = nextSleep;
   1804     do {
   1805         ret = nanosleep(&t, &t);
   1806     } while (ret != 0);
   1807 
   1808     if (mAfScanDuration > 0) {
   1809         mAfScanDuration -= nextSleep;
   1810     }
   1811     if (mAeScanDuration > 0) {
   1812         mAeScanDuration -= nextSleep;
   1813     }
   1814 
   1815     return true;
   1816 }
   1817 
   1818 int EmulatedFakeCamera2::ControlThread::processAfTrigger(uint8_t afMode,
   1819         uint8_t afState) {
   1820     switch (afMode) {
   1821         case ANDROID_CONTROL_AF_MODE_OFF:
   1822         case ANDROID_CONTROL_AF_MODE_EDOF:
   1823             // Do nothing
   1824             break;
   1825         case ANDROID_CONTROL_AF_MODE_MACRO:
   1826         case ANDROID_CONTROL_AF_MODE_AUTO:
   1827             switch (afState) {
   1828                 case ANDROID_CONTROL_AF_STATE_INACTIVE:
   1829                 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
   1830                 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
   1831                     // Start new focusing cycle
   1832                     mAfScanDuration =  ((double)rand() / RAND_MAX) *
   1833                         (kMaxAfDuration - kMinAfDuration) + kMinAfDuration;
   1834                     afState = ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN;
   1835                     ALOGV("%s: AF scan start, duration %" PRId64 " ms",
   1836                           __FUNCTION__, mAfScanDuration / 1000000);
   1837                     break;
   1838                 case ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN:
   1839                     // Ignore new request, already scanning
   1840                     break;
   1841                 default:
   1842                     ALOGE("Unexpected AF state in AUTO/MACRO AF mode: %d",
   1843                           afState);
   1844             }
   1845             break;
   1846         case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
   1847             switch (afState) {
   1848                 // Picture mode waits for passive scan to complete
   1849                 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
   1850                     mLockAfterPassiveScan = true;
   1851                     break;
   1852                 case ANDROID_CONTROL_AF_STATE_INACTIVE:
   1853                     afState = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
   1854                     break;
   1855                 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
   1856                     afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
   1857                     break;
   1858                 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
   1859                 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
   1860                     // Must cancel to get out of these states
   1861                     break;
   1862                 default:
   1863                     ALOGE("Unexpected AF state in CONTINUOUS_PICTURE AF mode: %d",
   1864                           afState);
   1865             }
   1866             break;
   1867         case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
   1868             switch (afState) {
   1869                 // Video mode does not wait for passive scan to complete
   1870                 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
   1871                 case ANDROID_CONTROL_AF_STATE_INACTIVE:
   1872                     afState = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
   1873                     break;
   1874                 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
   1875                     afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
   1876                     break;
   1877                 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
   1878                 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
   1879                     // Must cancel to get out of these states
   1880                     break;
   1881                 default:
   1882                     ALOGE("Unexpected AF state in CONTINUOUS_VIDEO AF mode: %d",
   1883                           afState);
   1884             }
   1885             break;
   1886         default:
   1887             break;
   1888     }
   1889     return afState;
   1890 }
   1891 
   1892 int EmulatedFakeCamera2::ControlThread::maybeStartAfScan(uint8_t afMode,
   1893         uint8_t afState) {
   1894     if ((afMode == ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO ||
   1895             afMode == ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE) &&
   1896         (afState == ANDROID_CONTROL_AF_STATE_INACTIVE ||
   1897             afState == ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED)) {
   1898 
   1899         bool startScan = ((double)rand() / RAND_MAX) < kContinuousAfStartRate;
   1900         if (startScan) {
   1901             // Start new passive focusing cycle
   1902             mAfScanDuration =  ((double)rand() / RAND_MAX) *
   1903                 (kMaxAfDuration - kMinAfDuration) + kMinAfDuration;
   1904             afState = ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN;
   1905             ALOGV("%s: AF passive scan start, duration %" PRId64 " ms",
   1906                 __FUNCTION__, mAfScanDuration / 1000000);
   1907         }
   1908     }
   1909     return afState;
   1910 }
   1911 
   1912 int EmulatedFakeCamera2::ControlThread::updateAfScan(uint8_t afMode,
   1913         uint8_t afState, nsecs_t *maxSleep) {
   1914     if (! (afState == ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN ||
   1915             afState == ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN ) ) {
   1916         return afState;
   1917     }
   1918 
   1919     if (mAfScanDuration <= 0) {
   1920         ALOGV("%s: AF scan done", __FUNCTION__);
   1921         switch (afMode) {
   1922             case ANDROID_CONTROL_AF_MODE_MACRO:
   1923             case ANDROID_CONTROL_AF_MODE_AUTO: {
   1924                 bool success = ((double)rand() / RAND_MAX) < kAfSuccessRate;
   1925                 if (success) {
   1926                     afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
   1927                 } else {
   1928                     afState = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
   1929                 }
   1930                 break;
   1931             }
   1932             case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
   1933                 if (mLockAfterPassiveScan) {
   1934                     afState = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
   1935                     mLockAfterPassiveScan = false;
   1936                 } else {
   1937                     afState = ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED;
   1938                 }
   1939                 break;
   1940             case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
   1941                 afState = ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED;
   1942                 break;
   1943             default:
   1944                 ALOGE("Unexpected AF mode in scan state");
   1945         }
   1946     } else {
   1947         if (mAfScanDuration <= *maxSleep) {
   1948             *maxSleep = mAfScanDuration;
   1949         }
   1950     }
   1951     return afState;
   1952 }
   1953 
   1954 void EmulatedFakeCamera2::ControlThread::updateAfState(uint8_t newState,
   1955         int32_t triggerId) {
   1956     Mutex::Autolock lock(mInputMutex);
   1957     if (mAfState != newState) {
   1958         ALOGV("%s: Autofocus state now %d, id %d", __FUNCTION__,
   1959                 newState, triggerId);
   1960         mAfState = newState;
   1961         mParent->sendNotification(CAMERA2_MSG_AUTOFOCUS,
   1962                 newState, triggerId, 0);
   1963     }
   1964 }
   1965 
   1966 int EmulatedFakeCamera2::ControlThread::processPrecaptureTrigger(uint8_t aeMode,
   1967         uint8_t aeState) {
   1968     switch (aeMode) {
   1969         case ANDROID_CONTROL_AE_MODE_OFF:
   1970             // Don't do anything for these
   1971             return aeState;
   1972         case ANDROID_CONTROL_AE_MODE_ON:
   1973         case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH:
   1974         case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH:
   1975         case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE:
   1976             // Trigger a precapture cycle
   1977             aeState = ANDROID_CONTROL_AE_STATE_PRECAPTURE;
   1978             mAeScanDuration = ((double)rand() / RAND_MAX) *
   1979                     (kMaxPrecaptureAeDuration - kMinPrecaptureAeDuration) +
   1980                     kMinPrecaptureAeDuration;
   1981             ALOGD("%s: AE precapture scan start, duration %" PRId64 " ms",
   1982                     __FUNCTION__, mAeScanDuration / 1000000);
   1983 
   1984     }
   1985     return aeState;
   1986 }
   1987 
   1988 int EmulatedFakeCamera2::ControlThread::maybeStartAeScan(uint8_t aeMode,
   1989         bool aeLocked,
   1990         uint8_t aeState) {
   1991     if (aeLocked) return aeState;
   1992     switch (aeMode) {
   1993         case ANDROID_CONTROL_AE_MODE_OFF:
   1994             break;
   1995         case ANDROID_CONTROL_AE_MODE_ON:
   1996         case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH:
   1997         case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH:
   1998         case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE: {
   1999             if (aeState != ANDROID_CONTROL_AE_STATE_INACTIVE &&
   2000                     aeState != ANDROID_CONTROL_AE_STATE_CONVERGED) break;
   2001 
   2002             bool startScan = ((double)rand() / RAND_MAX) < kAeScanStartRate;
   2003             if (startScan) {
   2004                 mAeScanDuration = ((double)rand() / RAND_MAX) *
   2005                 (kMaxAeDuration - kMinAeDuration) + kMinAeDuration;
   2006                 aeState = ANDROID_CONTROL_AE_STATE_SEARCHING;
   2007                 ALOGV("%s: AE scan start, duration %" PRId64 " ms",
   2008                         __FUNCTION__, mAeScanDuration / 1000000);
   2009             }
   2010         }
   2011     }
   2012 
   2013     return aeState;
   2014 }
   2015 
   2016 int EmulatedFakeCamera2::ControlThread::updateAeScan(uint8_t aeMode,
   2017         bool aeLock, uint8_t aeState, nsecs_t *maxSleep) {
   2018     if (aeLock && aeState != ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
   2019         mAeScanDuration = 0;
   2020         aeState = ANDROID_CONTROL_AE_STATE_LOCKED;
   2021     } else if ((aeState == ANDROID_CONTROL_AE_STATE_SEARCHING) ||
   2022             (aeState == ANDROID_CONTROL_AE_STATE_PRECAPTURE ) ) {
   2023         if (mAeScanDuration <= 0) {
   2024             ALOGV("%s: AE scan done", __FUNCTION__);
   2025             aeState = aeLock ?
   2026                     ANDROID_CONTROL_AE_STATE_LOCKED :ANDROID_CONTROL_AE_STATE_CONVERGED;
   2027 
   2028             Mutex::Autolock lock(mInputMutex);
   2029             mExposureTime = kNormalExposureTime;
   2030         } else {
   2031             if (mAeScanDuration <= *maxSleep) {
   2032                 *maxSleep = mAeScanDuration;
   2033             }
   2034 
   2035             int64_t exposureDelta =
   2036                     ((double)rand() / RAND_MAX) * 2 * kExposureJump -
   2037                     kExposureJump;
   2038             Mutex::Autolock lock(mInputMutex);
   2039             mExposureTime = mExposureTime + exposureDelta;
   2040             if (mExposureTime < kMinExposureTime) mExposureTime = kMinExposureTime;
   2041         }
   2042     }
   2043 
   2044     return aeState;
   2045 }
   2046 
   2047 
   2048 void EmulatedFakeCamera2::ControlThread::updateAeState(uint8_t newState,
   2049         int32_t triggerId) {
   2050     Mutex::Autolock lock(mInputMutex);
   2051     if (mAeState != newState) {
   2052         ALOGV("%s: Autoexposure state now %d, id %d", __FUNCTION__,
   2053                 newState, triggerId);
   2054         mAeState = newState;
   2055         mParent->sendNotification(CAMERA2_MSG_AUTOEXPOSURE,
   2056                 newState, triggerId, 0);
   2057     }
   2058 }
   2059 
   2060 /** Private methods */
   2061 
   2062 status_t EmulatedFakeCamera2::constructStaticInfo(
   2063         camera_metadata_t **info,
   2064         bool sizeRequest) const {
   2065 
   2066     size_t entryCount = 0;
   2067     size_t dataCount = 0;
   2068     status_t ret;
   2069 
   2070 #define ADD_OR_SIZE( tag, data, count ) \
   2071     if ( ( ret = addOrSize(*info, sizeRequest, &entryCount, &dataCount, \
   2072             tag, data, count) ) != OK ) return ret
   2073 
   2074     // android.lens
   2075 
   2076     // 5 cm min focus distance for back camera, infinity (fixed focus) for front
   2077     const float minFocusDistance = mFacingBack ? 1.0/0.05 : 0.0;
   2078     ADD_OR_SIZE(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE,
   2079             &minFocusDistance, 1);
   2080     // 5 m hyperfocal distance for back camera, infinity (fixed focus) for front
   2081     const float hyperFocalDistance = mFacingBack ? 1.0/5.0 : 0.0;
   2082     ADD_OR_SIZE(ANDROID_LENS_INFO_HYPERFOCAL_DISTANCE,
   2083             &minFocusDistance, 1);
   2084 
   2085     static const float focalLength = 3.30f; // mm
   2086     ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS,
   2087             &focalLength, 1);
   2088     static const float aperture = 2.8f;
   2089     ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_APERTURES,
   2090             &aperture, 1);
   2091     static const float filterDensity = 0;
   2092     ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
   2093             &filterDensity, 1);
   2094     static const uint8_t availableOpticalStabilization =
   2095             ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
   2096     ADD_OR_SIZE(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
   2097             &availableOpticalStabilization, 1);
   2098 
   2099     static const int32_t lensShadingMapSize[] = {1, 1};
   2100     ADD_OR_SIZE(ANDROID_LENS_INFO_SHADING_MAP_SIZE, lensShadingMapSize,
   2101             sizeof(lensShadingMapSize)/sizeof(int32_t));
   2102 
   2103     int32_t lensFacing = mFacingBack ?
   2104             ANDROID_LENS_FACING_BACK : ANDROID_LENS_FACING_FRONT;
   2105     ADD_OR_SIZE(ANDROID_LENS_FACING, &lensFacing, 1);
   2106 
   2107     // android.sensor
   2108 
   2109     ADD_OR_SIZE(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE,
   2110             Sensor::kExposureTimeRange, 2);
   2111 
   2112     ADD_OR_SIZE(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
   2113             &Sensor::kFrameDurationRange[1], 1);
   2114 
   2115     ADD_OR_SIZE(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE,
   2116             Sensor::kSensitivityRange,
   2117             sizeof(Sensor::kSensitivityRange)
   2118             /sizeof(int32_t));
   2119 
   2120     ADD_OR_SIZE(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT,
   2121             &Sensor::kColorFilterArrangement, 1);
   2122 
   2123     static const float sensorPhysicalSize[2] = {3.20f, 2.40f}; // mm
   2124     ADD_OR_SIZE(ANDROID_SENSOR_INFO_PHYSICAL_SIZE,
   2125             sensorPhysicalSize, 2);
   2126 
   2127     ADD_OR_SIZE(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
   2128             Sensor::kResolution, 2);
   2129 
   2130     ADD_OR_SIZE(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
   2131             Sensor::kResolution, 2);
   2132 
   2133     ADD_OR_SIZE(ANDROID_SENSOR_INFO_WHITE_LEVEL,
   2134             &Sensor::kMaxRawValue, 1);
   2135 
   2136     static const int32_t blackLevelPattern[4] = {
   2137         static_cast<int32_t>(Sensor::kBlackLevel),
   2138         static_cast<int32_t>(Sensor::kBlackLevel),
   2139         static_cast<int32_t>(Sensor::kBlackLevel),
   2140         static_cast<int32_t>(Sensor::kBlackLevel)
   2141     };
   2142     ADD_OR_SIZE(ANDROID_SENSOR_BLACK_LEVEL_PATTERN,
   2143             blackLevelPattern, sizeof(blackLevelPattern)/sizeof(int32_t));
   2144 
   2145     //TODO: sensor color calibration fields
   2146 
   2147     // android.flash
   2148     static const uint8_t flashAvailable = 0;
   2149     ADD_OR_SIZE(ANDROID_FLASH_INFO_AVAILABLE, &flashAvailable, 1);
   2150 
   2151     static const int64_t flashChargeDuration = 0;
   2152     ADD_OR_SIZE(ANDROID_FLASH_INFO_CHARGE_DURATION, &flashChargeDuration, 1);
   2153 
   2154     // android.tonemap
   2155 
   2156     static const int32_t tonemapCurvePoints = 128;
   2157     ADD_OR_SIZE(ANDROID_TONEMAP_MAX_CURVE_POINTS, &tonemapCurvePoints, 1);
   2158 
   2159     // android.scaler
   2160 
   2161     ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_FORMATS,
   2162             kAvailableFormats,
   2163             sizeof(kAvailableFormats)/sizeof(uint32_t));
   2164 
   2165     ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_RAW_SIZES,
   2166             kAvailableRawSizes,
   2167             sizeof(kAvailableRawSizes)/sizeof(uint32_t));
   2168 
   2169     ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_RAW_MIN_DURATIONS,
   2170             kAvailableRawMinDurations,
   2171             sizeof(kAvailableRawMinDurations)/sizeof(uint64_t));
   2172 
   2173     if (mFacingBack) {
   2174         ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
   2175                 kAvailableProcessedSizesBack,
   2176                 sizeof(kAvailableProcessedSizesBack)/sizeof(uint32_t));
   2177     } else {
   2178         ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
   2179                 kAvailableProcessedSizesFront,
   2180                 sizeof(kAvailableProcessedSizesFront)/sizeof(uint32_t));
   2181     }
   2182 
   2183     ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS,
   2184             kAvailableProcessedMinDurations,
   2185             sizeof(kAvailableProcessedMinDurations)/sizeof(uint64_t));
   2186 
   2187     if (mFacingBack) {
   2188         ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
   2189                 kAvailableJpegSizesBack,
   2190                 sizeof(kAvailableJpegSizesBack)/sizeof(uint32_t));
   2191     } else {
   2192         ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
   2193                 kAvailableJpegSizesFront,
   2194                 sizeof(kAvailableJpegSizesFront)/sizeof(uint32_t));
   2195     }
   2196 
   2197     ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS,
   2198             kAvailableJpegMinDurations,
   2199             sizeof(kAvailableJpegMinDurations)/sizeof(uint64_t));
   2200 
   2201     static const float maxZoom = 10;
   2202     ADD_OR_SIZE(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
   2203             &maxZoom, 1);
   2204 
   2205     // android.jpeg
   2206 
   2207     static const int32_t jpegThumbnailSizes[] = {
   2208             0, 0,
   2209             160, 120,
   2210             320, 240
   2211      };
   2212     ADD_OR_SIZE(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
   2213             jpegThumbnailSizes, sizeof(jpegThumbnailSizes)/sizeof(int32_t));
   2214 
   2215     static const int32_t jpegMaxSize = JpegCompressor::kMaxJpegSize;
   2216     ADD_OR_SIZE(ANDROID_JPEG_MAX_SIZE, &jpegMaxSize, 1);
   2217 
   2218     // android.stats
   2219 
   2220     static const uint8_t availableFaceDetectModes[] = {
   2221         ANDROID_STATISTICS_FACE_DETECT_MODE_OFF,
   2222         ANDROID_STATISTICS_FACE_DETECT_MODE_SIMPLE,
   2223         ANDROID_STATISTICS_FACE_DETECT_MODE_FULL
   2224     };
   2225 
   2226     ADD_OR_SIZE(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
   2227             availableFaceDetectModes,
   2228             sizeof(availableFaceDetectModes));
   2229 
   2230     static const int32_t maxFaceCount = 8;
   2231     ADD_OR_SIZE(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
   2232             &maxFaceCount, 1);
   2233 
   2234     static const int32_t histogramSize = 64;
   2235     ADD_OR_SIZE(ANDROID_STATISTICS_INFO_HISTOGRAM_BUCKET_COUNT,
   2236             &histogramSize, 1);
   2237 
   2238     static const int32_t maxHistogramCount = 1000;
   2239     ADD_OR_SIZE(ANDROID_STATISTICS_INFO_MAX_HISTOGRAM_COUNT,
   2240             &maxHistogramCount, 1);
   2241 
   2242     static const int32_t sharpnessMapSize[2] = {64, 64};
   2243     ADD_OR_SIZE(ANDROID_STATISTICS_INFO_SHARPNESS_MAP_SIZE,
   2244             sharpnessMapSize, sizeof(sharpnessMapSize)/sizeof(int32_t));
   2245 
   2246     static const int32_t maxSharpnessMapValue = 1000;
   2247     ADD_OR_SIZE(ANDROID_STATISTICS_INFO_MAX_SHARPNESS_MAP_VALUE,
   2248             &maxSharpnessMapValue, 1);
   2249 
   2250     // android.control
   2251 
   2252     static const uint8_t availableSceneModes[] = {
   2253             ANDROID_CONTROL_SCENE_MODE_DISABLED
   2254     };
   2255     ADD_OR_SIZE(ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
   2256             availableSceneModes, sizeof(availableSceneModes));
   2257 
   2258     static const uint8_t availableEffects[] = {
   2259             ANDROID_CONTROL_EFFECT_MODE_OFF
   2260     };
   2261     ADD_OR_SIZE(ANDROID_CONTROL_AVAILABLE_EFFECTS,
   2262             availableEffects, sizeof(availableEffects));
   2263 
   2264     static const int32_t max3aRegions[] = {/*AE*/ 0,/*AWB*/ 0,/*AF*/ 0};
   2265     ADD_OR_SIZE(ANDROID_CONTROL_MAX_REGIONS,
   2266             max3aRegions, sizeof(max3aRegions)/sizeof(max3aRegions[0]));
   2267 
   2268     static const uint8_t availableAeModes[] = {
   2269             ANDROID_CONTROL_AE_MODE_OFF,
   2270             ANDROID_CONTROL_AE_MODE_ON
   2271     };
   2272     ADD_OR_SIZE(ANDROID_CONTROL_AE_AVAILABLE_MODES,
   2273             availableAeModes, sizeof(availableAeModes));
   2274 
   2275     static const camera_metadata_rational exposureCompensationStep = {
   2276             1, 3
   2277     };
   2278     ADD_OR_SIZE(ANDROID_CONTROL_AE_COMPENSATION_STEP,
   2279             &exposureCompensationStep, 1);
   2280 
   2281     int32_t exposureCompensationRange[] = {-9, 9};
   2282     ADD_OR_SIZE(ANDROID_CONTROL_AE_COMPENSATION_RANGE,
   2283             exposureCompensationRange,
   2284             sizeof(exposureCompensationRange)/sizeof(int32_t));
   2285 
   2286     static const int32_t availableTargetFpsRanges[] = {
   2287             5, 30, 15, 30
   2288     };
   2289     ADD_OR_SIZE(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
   2290             availableTargetFpsRanges,
   2291             sizeof(availableTargetFpsRanges)/sizeof(int32_t));
   2292 
   2293     static const uint8_t availableAntibandingModes[] = {
   2294             ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF,
   2295             ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO
   2296     };
   2297     ADD_OR_SIZE(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
   2298             availableAntibandingModes, sizeof(availableAntibandingModes));
   2299 
   2300     static const uint8_t availableAwbModes[] = {
   2301             ANDROID_CONTROL_AWB_MODE_OFF,
   2302             ANDROID_CONTROL_AWB_MODE_AUTO,
   2303             ANDROID_CONTROL_AWB_MODE_INCANDESCENT,
   2304             ANDROID_CONTROL_AWB_MODE_FLUORESCENT,
   2305             ANDROID_CONTROL_AWB_MODE_DAYLIGHT,
   2306             ANDROID_CONTROL_AWB_MODE_SHADE
   2307     };
   2308     ADD_OR_SIZE(ANDROID_CONTROL_AWB_AVAILABLE_MODES,
   2309             availableAwbModes, sizeof(availableAwbModes));
   2310 
   2311     static const uint8_t availableAfModesBack[] = {
   2312             ANDROID_CONTROL_AF_MODE_OFF,
   2313             ANDROID_CONTROL_AF_MODE_AUTO,
   2314             ANDROID_CONTROL_AF_MODE_MACRO,
   2315             ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO,
   2316             ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE
   2317     };
   2318 
   2319     static const uint8_t availableAfModesFront[] = {
   2320             ANDROID_CONTROL_AF_MODE_OFF
   2321     };
   2322 
   2323     if (mFacingBack) {
   2324         ADD_OR_SIZE(ANDROID_CONTROL_AF_AVAILABLE_MODES,
   2325                     availableAfModesBack, sizeof(availableAfModesBack));
   2326     } else {
   2327         ADD_OR_SIZE(ANDROID_CONTROL_AF_AVAILABLE_MODES,
   2328                     availableAfModesFront, sizeof(availableAfModesFront));
   2329     }
   2330 
   2331     static const uint8_t availableVstabModes[] = {
   2332             ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF
   2333     };
   2334     ADD_OR_SIZE(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
   2335             availableVstabModes, sizeof(availableVstabModes));
   2336 
   2337 #undef ADD_OR_SIZE
   2338     /** Allocate metadata if sizing */
   2339     if (sizeRequest) {
   2340         ALOGV("Allocating %zu entries, %zu extra bytes for "
   2341                 "static camera info",
   2342                 entryCount, dataCount);
   2343         *info = allocate_camera_metadata(entryCount, dataCount);
   2344         if (*info == NULL) {
   2345             ALOGE("Unable to allocate camera static info"
   2346                     "(%zu entries, %zu bytes extra data)",
   2347                     entryCount, dataCount);
   2348             return NO_MEMORY;
   2349         }
   2350     }
   2351     return OK;
   2352 }
   2353 
   2354 status_t EmulatedFakeCamera2::constructDefaultRequest(
   2355         int request_template,
   2356         camera_metadata_t **request,
   2357         bool sizeRequest) const {
   2358 
   2359     size_t entryCount = 0;
   2360     size_t dataCount = 0;
   2361     status_t ret;
   2362 
   2363 #define ADD_OR_SIZE( tag, data, count ) \
   2364     if ( ( ret = addOrSize(*request, sizeRequest, &entryCount, &dataCount, \
   2365             tag, data, count) ) != OK ) return ret
   2366 
   2367     /** android.request */
   2368 
   2369     static const uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
   2370     ADD_OR_SIZE(ANDROID_REQUEST_TYPE, &requestType, 1);
   2371 
   2372     static const uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
   2373     ADD_OR_SIZE(ANDROID_REQUEST_METADATA_MODE, &metadataMode, 1);
   2374 
   2375     static const int32_t id = 0;
   2376     ADD_OR_SIZE(ANDROID_REQUEST_ID, &id, 1);
   2377 
   2378     static const int32_t frameCount = 0;
   2379     ADD_OR_SIZE(ANDROID_REQUEST_FRAME_COUNT, &frameCount, 1);
   2380 
   2381     // OUTPUT_STREAMS set by user
   2382     entryCount += 1;
   2383     dataCount += 5; // TODO: Should be maximum stream number
   2384 
   2385     /** android.lens */
   2386 
   2387     static const float focusDistance = 0;
   2388     ADD_OR_SIZE(ANDROID_LENS_FOCUS_DISTANCE, &focusDistance, 1);
   2389 
   2390     static const float aperture = 2.8f;
   2391     ADD_OR_SIZE(ANDROID_LENS_APERTURE, &aperture, 1);
   2392 
   2393     static const float focalLength = 5.0f;
   2394     ADD_OR_SIZE(ANDROID_LENS_FOCAL_LENGTH, &focalLength, 1);
   2395 
   2396     static const float filterDensity = 0;
   2397     ADD_OR_SIZE(ANDROID_LENS_FILTER_DENSITY, &filterDensity, 1);
   2398 
   2399     static const uint8_t opticalStabilizationMode =
   2400             ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
   2401     ADD_OR_SIZE(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
   2402             &opticalStabilizationMode, 1);
   2403 
   2404     // FOCUS_RANGE set only in frame
   2405 
   2406     /** android.sensor */
   2407 
   2408     static const int64_t exposureTime = 10 * MSEC;
   2409     ADD_OR_SIZE(ANDROID_SENSOR_EXPOSURE_TIME, &exposureTime, 1);
   2410 
   2411     static const int64_t frameDuration = 33333333L; // 1/30 s
   2412     ADD_OR_SIZE(ANDROID_SENSOR_FRAME_DURATION, &frameDuration, 1);
   2413 
   2414     static const int32_t sensitivity = 100;
   2415     ADD_OR_SIZE(ANDROID_SENSOR_SENSITIVITY, &sensitivity, 1);
   2416 
   2417     // TIMESTAMP set only in frame
   2418 
   2419     /** android.flash */
   2420 
   2421     static const uint8_t flashMode = ANDROID_FLASH_MODE_OFF;
   2422     ADD_OR_SIZE(ANDROID_FLASH_MODE, &flashMode, 1);
   2423 
   2424     static const uint8_t flashPower = 10;
   2425     ADD_OR_SIZE(ANDROID_FLASH_FIRING_POWER, &flashPower, 1);
   2426 
   2427     static const int64_t firingTime = 0;
   2428     ADD_OR_SIZE(ANDROID_FLASH_FIRING_TIME, &firingTime, 1);
   2429 
   2430     /** Processing block modes */
   2431     uint8_t hotPixelMode = 0;
   2432     uint8_t demosaicMode = 0;
   2433     uint8_t noiseMode = 0;
   2434     uint8_t shadingMode = 0;
   2435     uint8_t colorMode = 0;
   2436     uint8_t tonemapMode = 0;
   2437     uint8_t edgeMode = 0;
   2438     switch (request_template) {
   2439       case CAMERA2_TEMPLATE_STILL_CAPTURE:
   2440         // fall-through
   2441       case CAMERA2_TEMPLATE_VIDEO_SNAPSHOT:
   2442         // fall-through
   2443       case CAMERA2_TEMPLATE_ZERO_SHUTTER_LAG:
   2444         hotPixelMode = ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY;
   2445         demosaicMode = ANDROID_DEMOSAIC_MODE_HIGH_QUALITY;
   2446         noiseMode = ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY;
   2447         shadingMode = ANDROID_SHADING_MODE_HIGH_QUALITY;
   2448         colorMode = ANDROID_COLOR_CORRECTION_MODE_HIGH_QUALITY;
   2449         tonemapMode = ANDROID_TONEMAP_MODE_HIGH_QUALITY;
   2450         edgeMode = ANDROID_EDGE_MODE_HIGH_QUALITY;
   2451         break;
   2452       case CAMERA2_TEMPLATE_PREVIEW:
   2453         // fall-through
   2454       case CAMERA2_TEMPLATE_VIDEO_RECORD:
   2455         // fall-through
   2456       default:
   2457         hotPixelMode = ANDROID_HOT_PIXEL_MODE_FAST;
   2458         demosaicMode = ANDROID_DEMOSAIC_MODE_FAST;
   2459         noiseMode = ANDROID_NOISE_REDUCTION_MODE_FAST;
   2460         shadingMode = ANDROID_SHADING_MODE_FAST;
   2461         colorMode = ANDROID_COLOR_CORRECTION_MODE_FAST;
   2462         tonemapMode = ANDROID_TONEMAP_MODE_FAST;
   2463         edgeMode = ANDROID_EDGE_MODE_FAST;
   2464         break;
   2465     }
   2466     ADD_OR_SIZE(ANDROID_HOT_PIXEL_MODE, &hotPixelMode, 1);
   2467     ADD_OR_SIZE(ANDROID_DEMOSAIC_MODE, &demosaicMode, 1);
   2468     ADD_OR_SIZE(ANDROID_NOISE_REDUCTION_MODE, &noiseMode, 1);
   2469     ADD_OR_SIZE(ANDROID_SHADING_MODE, &shadingMode, 1);
   2470     ADD_OR_SIZE(ANDROID_COLOR_CORRECTION_MODE, &colorMode, 1);
   2471     ADD_OR_SIZE(ANDROID_TONEMAP_MODE, &tonemapMode, 1);
   2472     ADD_OR_SIZE(ANDROID_EDGE_MODE, &edgeMode, 1);
   2473 
   2474     /** android.noise */
   2475     static const uint8_t noiseStrength = 5;
   2476     ADD_OR_SIZE(ANDROID_NOISE_REDUCTION_STRENGTH, &noiseStrength, 1);
   2477 
   2478     /** android.color */
   2479     static const float colorTransform[9] = {
   2480         1.0f, 0.f, 0.f,
   2481         0.f, 1.f, 0.f,
   2482         0.f, 0.f, 1.f
   2483     };
   2484     ADD_OR_SIZE(ANDROID_COLOR_CORRECTION_TRANSFORM, colorTransform, 9);
   2485 
   2486     /** android.tonemap */
   2487     static const float tonemapCurve[4] = {
   2488         0.f, 0.f,
   2489         1.f, 1.f
   2490     };
   2491     ADD_OR_SIZE(ANDROID_TONEMAP_CURVE_RED, tonemapCurve, 4);
   2492     ADD_OR_SIZE(ANDROID_TONEMAP_CURVE_GREEN, tonemapCurve, 4);
   2493     ADD_OR_SIZE(ANDROID_TONEMAP_CURVE_BLUE, tonemapCurve, 4);
   2494 
   2495     /** android.edge */
   2496     static const uint8_t edgeStrength = 5;
   2497     ADD_OR_SIZE(ANDROID_EDGE_STRENGTH, &edgeStrength, 1);
   2498 
   2499     /** android.scaler */
   2500     static const int32_t cropRegion[3] = {
   2501         0, 0, static_cast<int32_t>(Sensor::kResolution[0])
   2502     };
   2503     ADD_OR_SIZE(ANDROID_SCALER_CROP_REGION, cropRegion, 3);
   2504 
   2505     /** android.jpeg */
   2506     static const int32_t jpegQuality = 80;
   2507     ADD_OR_SIZE(ANDROID_JPEG_QUALITY, &jpegQuality, 1);
   2508 
   2509     static const int32_t thumbnailSize[2] = {
   2510         640, 480
   2511     };
   2512     ADD_OR_SIZE(ANDROID_JPEG_THUMBNAIL_SIZE, thumbnailSize, 2);
   2513 
   2514     static const int32_t thumbnailQuality = 80;
   2515     ADD_OR_SIZE(ANDROID_JPEG_THUMBNAIL_QUALITY, &thumbnailQuality, 1);
   2516 
   2517     static const double gpsCoordinates[2] = {
   2518         0, 0
   2519     };
   2520     ADD_OR_SIZE(ANDROID_JPEG_GPS_COORDINATES, gpsCoordinates, 2);
   2521 
   2522     static const uint8_t gpsProcessingMethod[32] = "None";
   2523     ADD_OR_SIZE(ANDROID_JPEG_GPS_PROCESSING_METHOD, gpsProcessingMethod, 32);
   2524 
   2525     static const int64_t gpsTimestamp = 0;
   2526     ADD_OR_SIZE(ANDROID_JPEG_GPS_TIMESTAMP, &gpsTimestamp, 1);
   2527 
   2528     static const int32_t jpegOrientation = 0;
   2529     ADD_OR_SIZE(ANDROID_JPEG_ORIENTATION, &jpegOrientation, 1);
   2530 
   2531     /** android.stats */
   2532 
   2533     static const uint8_t faceDetectMode =
   2534         ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
   2535     ADD_OR_SIZE(ANDROID_STATISTICS_FACE_DETECT_MODE, &faceDetectMode, 1);
   2536 
   2537     static const uint8_t histogramMode = ANDROID_STATISTICS_HISTOGRAM_MODE_OFF;
   2538     ADD_OR_SIZE(ANDROID_STATISTICS_HISTOGRAM_MODE, &histogramMode, 1);
   2539 
   2540     static const uint8_t sharpnessMapMode =
   2541         ANDROID_STATISTICS_SHARPNESS_MAP_MODE_OFF;
   2542     ADD_OR_SIZE(ANDROID_STATISTICS_SHARPNESS_MAP_MODE, &sharpnessMapMode, 1);
   2543 
   2544     // faceRectangles, faceScores, faceLandmarks, faceIds, histogram,
   2545     // sharpnessMap only in frames
   2546 
   2547     /** android.control */
   2548 
   2549     uint8_t controlIntent = 0;
   2550     switch (request_template) {
   2551       case CAMERA2_TEMPLATE_PREVIEW:
   2552         controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
   2553         break;
   2554       case CAMERA2_TEMPLATE_STILL_CAPTURE:
   2555         controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
   2556         break;
   2557       case CAMERA2_TEMPLATE_VIDEO_RECORD:
   2558         controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
   2559         break;
   2560       case CAMERA2_TEMPLATE_VIDEO_SNAPSHOT:
   2561         controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
   2562         break;
   2563       case CAMERA2_TEMPLATE_ZERO_SHUTTER_LAG:
   2564         controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG;
   2565         break;
   2566       default:
   2567         controlIntent = ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM;
   2568         break;
   2569     }
   2570     ADD_OR_SIZE(ANDROID_CONTROL_CAPTURE_INTENT, &controlIntent, 1);
   2571 
   2572     static const uint8_t controlMode = ANDROID_CONTROL_MODE_AUTO;
   2573     ADD_OR_SIZE(ANDROID_CONTROL_MODE, &controlMode, 1);
   2574 
   2575     static const uint8_t effectMode = ANDROID_CONTROL_EFFECT_MODE_OFF;
   2576     ADD_OR_SIZE(ANDROID_CONTROL_EFFECT_MODE, &effectMode, 1);
   2577 
   2578     static const uint8_t sceneMode = ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY;
   2579     ADD_OR_SIZE(ANDROID_CONTROL_SCENE_MODE, &sceneMode, 1);
   2580 
   2581     static const uint8_t aeMode = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH;
   2582     ADD_OR_SIZE(ANDROID_CONTROL_AE_MODE, &aeMode, 1);
   2583 
   2584     static const uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
   2585     ADD_OR_SIZE(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
   2586 
   2587     static const int32_t controlRegions[5] = {
   2588         0, 0,
   2589         static_cast<int32_t>(Sensor::kResolution[0]),
   2590         static_cast<int32_t>(Sensor::kResolution[1]),
   2591         1000
   2592     };
   2593     ADD_OR_SIZE(ANDROID_CONTROL_AE_REGIONS, controlRegions, 5);
   2594 
   2595     static const int32_t aeExpCompensation = 0;
   2596     ADD_OR_SIZE(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, &aeExpCompensation, 1);
   2597 
   2598     static const int32_t aeTargetFpsRange[2] = {
   2599         10, 30
   2600     };
   2601     ADD_OR_SIZE(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, aeTargetFpsRange, 2);
   2602 
   2603     static const uint8_t aeAntibandingMode =
   2604             ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
   2605     ADD_OR_SIZE(ANDROID_CONTROL_AE_ANTIBANDING_MODE, &aeAntibandingMode, 1);
   2606 
   2607     static const uint8_t awbMode =
   2608             ANDROID_CONTROL_AWB_MODE_AUTO;
   2609     ADD_OR_SIZE(ANDROID_CONTROL_AWB_MODE, &awbMode, 1);
   2610 
   2611     static const uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF;
   2612     ADD_OR_SIZE(ANDROID_CONTROL_AWB_LOCK, &awbLock, 1);
   2613 
   2614     ADD_OR_SIZE(ANDROID_CONTROL_AWB_REGIONS, controlRegions, 5);
   2615 
   2616     uint8_t afMode = 0;
   2617     switch (request_template) {
   2618       case CAMERA2_TEMPLATE_PREVIEW:
   2619         afMode = ANDROID_CONTROL_AF_MODE_AUTO;
   2620         break;
   2621       case CAMERA2_TEMPLATE_STILL_CAPTURE:
   2622         afMode = ANDROID_CONTROL_AF_MODE_AUTO;
   2623         break;
   2624       case CAMERA2_TEMPLATE_VIDEO_RECORD:
   2625         afMode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO;
   2626         break;
   2627       case CAMERA2_TEMPLATE_VIDEO_SNAPSHOT:
   2628         afMode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO;
   2629         break;
   2630       case CAMERA2_TEMPLATE_ZERO_SHUTTER_LAG:
   2631         afMode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE;
   2632         break;
   2633       default:
   2634         afMode = ANDROID_CONTROL_AF_MODE_AUTO;
   2635         break;
   2636     }
   2637     ADD_OR_SIZE(ANDROID_CONTROL_AF_MODE, &afMode, 1);
   2638 
   2639     ADD_OR_SIZE(ANDROID_CONTROL_AF_REGIONS, controlRegions, 5);
   2640 
   2641     static const uint8_t vstabMode =
   2642         ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
   2643     ADD_OR_SIZE(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &vstabMode, 1);
   2644 
   2645     // aeState, awbState, afState only in frame
   2646 
   2647     /** Allocate metadata if sizing */
   2648     if (sizeRequest) {
   2649         ALOGV("Allocating %zu entries, %zu extra bytes for "
   2650                 "request template type %d",
   2651                 entryCount, dataCount, request_template);
   2652         *request = allocate_camera_metadata(entryCount, dataCount);
   2653         if (*request == NULL) {
   2654             ALOGE("Unable to allocate new request template type %d "
   2655                     "(%zu entries, %zu bytes extra data)", request_template,
   2656                     entryCount, dataCount);
   2657             return NO_MEMORY;
   2658         }
   2659     }
   2660     return OK;
   2661 #undef ADD_OR_SIZE
   2662 }
   2663 
   2664 status_t EmulatedFakeCamera2::addOrSize(camera_metadata_t *request,
   2665         bool sizeRequest,
   2666         size_t *entryCount,
   2667         size_t *dataCount,
   2668         uint32_t tag,
   2669         const void *entryData,
   2670         size_t entryDataCount) {
   2671     status_t res;
   2672     if (!sizeRequest) {
   2673         return add_camera_metadata_entry(request, tag, entryData,
   2674                 entryDataCount);
   2675     } else {
   2676         int type = get_camera_metadata_tag_type(tag);
   2677         if (type < 0 ) return BAD_VALUE;
   2678         (*entryCount)++;
   2679         (*dataCount) += calculate_camera_metadata_entry_data_size(type,
   2680                 entryDataCount);
   2681         return OK;
   2682     }
   2683 }
   2684 
   2685 bool EmulatedFakeCamera2::isStreamInUse(uint32_t id) {
   2686     // Assumes mMutex is locked; otherwise new requests could enter
   2687     // configureThread while readoutThread is being checked
   2688 
   2689     // Order of isStreamInUse calls matters
   2690     if (mConfigureThread->isStreamInUse(id) ||
   2691             mReadoutThread->isStreamInUse(id) ||
   2692             mJpegCompressor->isStreamInUse(id) ) {
   2693         ALOGE("%s: Stream %d is in use in active requests!",
   2694                 __FUNCTION__, id);
   2695         return true;
   2696     }
   2697     return false;
   2698 }
   2699 
   2700 bool EmulatedFakeCamera2::isReprocessStreamInUse(uint32_t id) {
   2701     // TODO: implement
   2702     return false;
   2703 }
   2704 
   2705 const Stream& EmulatedFakeCamera2::getStreamInfo(uint32_t streamId) {
   2706     Mutex::Autolock lock(mMutex);
   2707 
   2708     return mStreams.valueFor(streamId);
   2709 }
   2710 
   2711 const ReprocessStream& EmulatedFakeCamera2::getReprocessStreamInfo(uint32_t streamId) {
   2712     Mutex::Autolock lock(mMutex);
   2713 
   2714     return mReprocessStreams.valueFor(streamId);
   2715 }
   2716 
   2717 };  /* namespace android */
   2718