Home | History | Annotate | Download | only in client2
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "Camera2-Parameters"
     18 #define ATRACE_TAG ATRACE_TAG_CAMERA
     19 // #define LOG_NDEBUG 0
     20 
     21 #include <utils/Log.h>
     22 #include <utils/Trace.h>
     23 #include <utils/Vector.h>
     24 #include <utils/SortedVector.h>
     25 
     26 #include <math.h>
     27 #include <stdlib.h>
     28 #include <cutils/properties.h>
     29 
     30 #include "Parameters.h"
     31 #include "system/camera.h"
     32 #include "hardware/camera_common.h"
     33 #include <android/hardware/ICamera.h>
     34 #include <media/MediaProfiles.h>
     35 #include <media/mediarecorder.h>
     36 
     37 namespace android {
     38 namespace camera2 {
     39 
     40 Parameters::Parameters(int cameraId,
     41         int cameraFacing) :
     42         cameraId(cameraId),
     43         cameraFacing(cameraFacing),
     44         info(NULL) {
     45 }
     46 
     47 Parameters::~Parameters() {
     48 }
     49 
     50 status_t Parameters::initialize(const CameraMetadata *info, int deviceVersion) {
     51     status_t res;
     52 
     53     if (info->entryCount() == 0) {
     54         ALOGE("%s: No static information provided!", __FUNCTION__);
     55         return BAD_VALUE;
     56     }
     57     Parameters::info = info;
     58     mDeviceVersion = deviceVersion;
     59 
     60     res = buildFastInfo();
     61     if (res != OK) return res;
     62 
     63     res = buildQuirks();
     64     if (res != OK) return res;
     65 
     66     const Size MAX_PREVIEW_SIZE = { MAX_PREVIEW_WIDTH, MAX_PREVIEW_HEIGHT };
     67     // Treat the H.264 max size as the max supported video size.
     68     MediaProfiles *videoEncoderProfiles = MediaProfiles::getInstance();
     69     Vector<video_encoder> encoders = videoEncoderProfiles->getVideoEncoders();
     70     int32_t maxVideoWidth = 0;
     71     int32_t maxVideoHeight = 0;
     72     for (size_t i = 0; i < encoders.size(); i++) {
     73         int width = videoEncoderProfiles->getVideoEncoderParamByName(
     74                 "enc.vid.width.max", encoders[i]);
     75         int height = videoEncoderProfiles->getVideoEncoderParamByName(
     76                 "enc.vid.height.max", encoders[i]);
     77         // Treat width/height separately here to handle the case where different
     78         // profile might report max size of different aspect ratio
     79         if (width > maxVideoWidth) {
     80             maxVideoWidth = width;
     81         }
     82         if (height > maxVideoHeight) {
     83             maxVideoHeight = height;
     84         }
     85     }
     86     // This is just an upper bound and may not be an actually valid video size
     87     const Size VIDEO_SIZE_UPPER_BOUND = {maxVideoWidth, maxVideoHeight};
     88 
     89     res = getFilteredSizes(MAX_PREVIEW_SIZE, &availablePreviewSizes);
     90     if (res != OK) return res;
     91     res = getFilteredSizes(VIDEO_SIZE_UPPER_BOUND, &availableVideoSizes);
     92     if (res != OK) return res;
     93 
     94     // Select initial preview and video size that's under the initial bound and
     95     // on the list of both preview and recording sizes
     96     previewWidth = 0;
     97     previewHeight = 0;
     98     for (size_t i = 0 ; i < availablePreviewSizes.size(); i++) {
     99         int newWidth = availablePreviewSizes[i].width;
    100         int newHeight = availablePreviewSizes[i].height;
    101         if (newWidth >= previewWidth && newHeight >= previewHeight &&
    102                 newWidth <= MAX_INITIAL_PREVIEW_WIDTH &&
    103                 newHeight <= MAX_INITIAL_PREVIEW_HEIGHT) {
    104             for (size_t j = 0; j < availableVideoSizes.size(); j++) {
    105                 if (availableVideoSizes[j].width == newWidth &&
    106                         availableVideoSizes[j].height == newHeight) {
    107                     previewWidth = newWidth;
    108                     previewHeight = newHeight;
    109                 }
    110             }
    111         }
    112     }
    113     if (previewWidth == 0) {
    114         ALOGE("%s: No initial preview size can be found!", __FUNCTION__);
    115         return BAD_VALUE;
    116     }
    117     videoWidth = previewWidth;
    118     videoHeight = previewHeight;
    119 
    120     params.setPreviewSize(previewWidth, previewHeight);
    121     params.setVideoSize(videoWidth, videoHeight);
    122     params.set(CameraParameters::KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO,
    123             String8::format("%dx%d",
    124                     previewWidth, previewHeight));
    125     {
    126         String8 supportedPreviewSizes;
    127         for (size_t i = 0; i < availablePreviewSizes.size(); i++) {
    128             if (i != 0) supportedPreviewSizes += ",";
    129             supportedPreviewSizes += String8::format("%dx%d",
    130                     availablePreviewSizes[i].width,
    131                     availablePreviewSizes[i].height);
    132         }
    133         ALOGV("Supported preview sizes are: %s", supportedPreviewSizes.string());
    134         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES,
    135                 supportedPreviewSizes);
    136 
    137         String8 supportedVideoSizes;
    138         for (size_t i = 0; i < availableVideoSizes.size(); i++) {
    139             if (i != 0) supportedVideoSizes += ",";
    140             supportedVideoSizes += String8::format("%dx%d",
    141                     availableVideoSizes[i].width,
    142                     availableVideoSizes[i].height);
    143         }
    144         ALOGV("Supported video sizes are: %s", supportedVideoSizes.string());
    145         params.set(CameraParameters::KEY_SUPPORTED_VIDEO_SIZES,
    146                 supportedVideoSizes);
    147     }
    148 
    149     camera_metadata_ro_entry_t availableFpsRanges =
    150         staticInfo(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, 2);
    151     if (!availableFpsRanges.count) return NO_INIT;
    152 
    153     previewFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP;
    154     params.set(CameraParameters::KEY_PREVIEW_FORMAT,
    155             formatEnumToString(previewFormat)); // NV21
    156 
    157     previewTransform = degToTransform(0,
    158             cameraFacing == CAMERA_FACING_FRONT);
    159 
    160     {
    161         String8 supportedPreviewFormats;
    162         SortedVector<int32_t> outputFormats = getAvailableOutputFormats();
    163         bool addComma = false;
    164         for (size_t i=0; i < outputFormats.size(); i++) {
    165             if (addComma) supportedPreviewFormats += ",";
    166             addComma = true;
    167             switch (outputFormats[i]) {
    168             case HAL_PIXEL_FORMAT_YCbCr_422_SP:
    169                 supportedPreviewFormats +=
    170                     CameraParameters::PIXEL_FORMAT_YUV422SP;
    171                 break;
    172             case HAL_PIXEL_FORMAT_YCrCb_420_SP:
    173                 supportedPreviewFormats +=
    174                     CameraParameters::PIXEL_FORMAT_YUV420SP;
    175                 break;
    176             case HAL_PIXEL_FORMAT_YCbCr_422_I:
    177                 supportedPreviewFormats +=
    178                     CameraParameters::PIXEL_FORMAT_YUV422I;
    179                 break;
    180             case HAL_PIXEL_FORMAT_YV12:
    181                 supportedPreviewFormats +=
    182                     CameraParameters::PIXEL_FORMAT_YUV420P;
    183                 break;
    184             case HAL_PIXEL_FORMAT_RGB_565:
    185                 supportedPreviewFormats +=
    186                     CameraParameters::PIXEL_FORMAT_RGB565;
    187                 break;
    188             case HAL_PIXEL_FORMAT_RGBA_8888:
    189                 supportedPreviewFormats +=
    190                     CameraParameters::PIXEL_FORMAT_RGBA8888;
    191                 break;
    192             case HAL_PIXEL_FORMAT_YCbCr_420_888:
    193                 // Flexible YUV allows both YV12 and NV21
    194                 supportedPreviewFormats +=
    195                     CameraParameters::PIXEL_FORMAT_YUV420P;
    196                 supportedPreviewFormats += ",";
    197                 supportedPreviewFormats +=
    198                     CameraParameters::PIXEL_FORMAT_YUV420SP;
    199                 break;
    200             // Not advertizing JPEG, RAW16, etc, for preview formats
    201             case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
    202             case HAL_PIXEL_FORMAT_RAW16:
    203             case HAL_PIXEL_FORMAT_BLOB:
    204                 addComma = false;
    205                 break;
    206 
    207             default:
    208                 ALOGW("%s: Camera %d: Unknown preview format: %x",
    209                         __FUNCTION__, cameraId, outputFormats[i]);
    210                 addComma = false;
    211                 break;
    212             }
    213         }
    214         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS,
    215                 supportedPreviewFormats);
    216     }
    217 
    218     previewFpsRange[0] = fastInfo.bestStillCaptureFpsRange[0];
    219     previewFpsRange[1] = fastInfo.bestStillCaptureFpsRange[1];
    220 
    221     // PREVIEW_FRAME_RATE / SUPPORTED_PREVIEW_FRAME_RATES are deprecated, but
    222     // still have to do something sane for them
    223 
    224     // NOTE: Not scaled like FPS range values are.
    225     int previewFps = fpsFromRange(previewFpsRange[0], previewFpsRange[1]);
    226     params.set(CameraParameters::KEY_PREVIEW_FRAME_RATE,
    227             previewFps);
    228 
    229     // PREVIEW_FPS_RANGE
    230     // -- Order matters. Set range after single value to so that a roundtrip
    231     //    of setParameters(getParameters()) would keep the FPS range in higher
    232     //    order.
    233     params.set(CameraParameters::KEY_PREVIEW_FPS_RANGE,
    234             String8::format("%d,%d",
    235                     previewFpsRange[0] * kFpsToApiScale,
    236                     previewFpsRange[1] * kFpsToApiScale));
    237 
    238     {
    239         String8 supportedPreviewFpsRange;
    240         for (size_t i=0; i < availableFpsRanges.count; i += 2) {
    241             if (!isFpsSupported(availablePreviewSizes,
    242                 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, availableFpsRanges.data.i32[i+1])) {
    243                 continue;
    244             }
    245             if (i != 0) supportedPreviewFpsRange += ",";
    246             supportedPreviewFpsRange += String8::format("(%d,%d)",
    247                     availableFpsRanges.data.i32[i] * kFpsToApiScale,
    248                     availableFpsRanges.data.i32[i+1] * kFpsToApiScale);
    249         }
    250         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE,
    251                 supportedPreviewFpsRange);
    252     }
    253 
    254     {
    255         SortedVector<int32_t> sortedPreviewFrameRates;
    256 
    257         String8 supportedPreviewFrameRates;
    258         for (size_t i=0; i < availableFpsRanges.count; i += 2) {
    259             // from the [min, max] fps range use the max value
    260             int fps = fpsFromRange(availableFpsRanges.data.i32[i],
    261                                    availableFpsRanges.data.i32[i+1]);
    262             if (!isFpsSupported(availablePreviewSizes,
    263                     HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED, fps)) {
    264                 continue;
    265             }
    266             // de-dupe frame rates
    267             if (sortedPreviewFrameRates.indexOf(fps) == NAME_NOT_FOUND) {
    268                 sortedPreviewFrameRates.add(fps);
    269             }
    270             else {
    271                 continue;
    272             }
    273 
    274             if (sortedPreviewFrameRates.size() > 1) {
    275                 supportedPreviewFrameRates += ",";
    276             }
    277 
    278             supportedPreviewFrameRates += String8::format("%d",
    279                     fps);
    280 
    281             ALOGV("%s: Supported preview frame rates: %s",
    282                     __FUNCTION__, supportedPreviewFrameRates.string());
    283         }
    284         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES,
    285                 supportedPreviewFrameRates);
    286     }
    287 
    288     Vector<Size> availableJpegSizes = getAvailableJpegSizes();
    289     if (!availableJpegSizes.size()) return NO_INIT;
    290 
    291     // TODO: Pick maximum
    292     pictureWidth = availableJpegSizes[0].width;
    293     pictureHeight = availableJpegSizes[0].height;
    294     pictureWidthLastSet = pictureWidth;
    295     pictureHeightLastSet = pictureHeight;
    296     pictureSizeOverriden = false;
    297 
    298     params.setPictureSize(pictureWidth,
    299             pictureHeight);
    300 
    301     {
    302         String8 supportedPictureSizes;
    303         for (size_t i=0; i < availableJpegSizes.size(); i++) {
    304             if (i != 0) supportedPictureSizes += ",";
    305             supportedPictureSizes += String8::format("%dx%d",
    306                     availableJpegSizes[i].width,
    307                     availableJpegSizes[i].height);
    308         }
    309         params.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES,
    310                 supportedPictureSizes);
    311     }
    312 
    313     params.setPictureFormat(CameraParameters::PIXEL_FORMAT_JPEG);
    314     params.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS,
    315             CameraParameters::PIXEL_FORMAT_JPEG);
    316 
    317     camera_metadata_ro_entry_t availableJpegThumbnailSizes =
    318         staticInfo(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, 4);
    319     if (!availableJpegThumbnailSizes.count) return NO_INIT;
    320 
    321     // Pick the largest thumbnail size that matches still image aspect ratio.
    322     ALOG_ASSERT(pictureWidth > 0 && pictureHeight > 0,
    323             "Invalid picture size, %d x %d", pictureWidth, pictureHeight);
    324     float picAspectRatio = static_cast<float>(pictureWidth) / pictureHeight;
    325     Size thumbnailSize =
    326             getMaxSizeForRatio(
    327                     picAspectRatio,
    328                     &availableJpegThumbnailSizes.data.i32[0],
    329                     availableJpegThumbnailSizes.count);
    330     jpegThumbSize[0] = thumbnailSize.width;
    331     jpegThumbSize[1] = thumbnailSize.height;
    332 
    333     params.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH,
    334             jpegThumbSize[0]);
    335     params.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT,
    336             jpegThumbSize[1]);
    337 
    338     {
    339         String8 supportedJpegThumbSizes;
    340         for (size_t i=0; i < availableJpegThumbnailSizes.count; i += 2) {
    341             if (i != 0) supportedJpegThumbSizes += ",";
    342             supportedJpegThumbSizes += String8::format("%dx%d",
    343                     availableJpegThumbnailSizes.data.i32[i],
    344                     availableJpegThumbnailSizes.data.i32[i+1]);
    345         }
    346         params.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES,
    347                 supportedJpegThumbSizes);
    348     }
    349 
    350     jpegThumbQuality = 90;
    351     params.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY,
    352             jpegThumbQuality);
    353     jpegQuality = 90;
    354     params.set(CameraParameters::KEY_JPEG_QUALITY,
    355             jpegQuality);
    356     jpegRotation = 0;
    357     params.set(CameraParameters::KEY_ROTATION,
    358             jpegRotation);
    359 
    360     gpsEnabled = false;
    361     gpsCoordinates[0] = 0.0;
    362     gpsCoordinates[1] = 0.0;
    363     gpsCoordinates[2] = 0.0;
    364     gpsTimestamp = 0;
    365     gpsProcessingMethod = "unknown";
    366     // GPS fields in CameraParameters are not set by implementation
    367 
    368     wbMode = ANDROID_CONTROL_AWB_MODE_AUTO;
    369     params.set(CameraParameters::KEY_WHITE_BALANCE,
    370             CameraParameters::WHITE_BALANCE_AUTO);
    371 
    372     camera_metadata_ro_entry_t availableWhiteBalanceModes =
    373         staticInfo(ANDROID_CONTROL_AWB_AVAILABLE_MODES, 0, 0, false);
    374     if (!availableWhiteBalanceModes.count) {
    375         params.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE,
    376                 CameraParameters::WHITE_BALANCE_AUTO);
    377     } else {
    378         String8 supportedWhiteBalance;
    379         bool addComma = false;
    380         for (size_t i=0; i < availableWhiteBalanceModes.count; i++) {
    381             if (addComma) supportedWhiteBalance += ",";
    382             addComma = true;
    383             switch (availableWhiteBalanceModes.data.u8[i]) {
    384             case ANDROID_CONTROL_AWB_MODE_AUTO:
    385                 supportedWhiteBalance +=
    386                     CameraParameters::WHITE_BALANCE_AUTO;
    387                 break;
    388             case ANDROID_CONTROL_AWB_MODE_INCANDESCENT:
    389                 supportedWhiteBalance +=
    390                     CameraParameters::WHITE_BALANCE_INCANDESCENT;
    391                 break;
    392             case ANDROID_CONTROL_AWB_MODE_FLUORESCENT:
    393                 supportedWhiteBalance +=
    394                     CameraParameters::WHITE_BALANCE_FLUORESCENT;
    395                 break;
    396             case ANDROID_CONTROL_AWB_MODE_WARM_FLUORESCENT:
    397                 supportedWhiteBalance +=
    398                     CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT;
    399                 break;
    400             case ANDROID_CONTROL_AWB_MODE_DAYLIGHT:
    401                 supportedWhiteBalance +=
    402                     CameraParameters::WHITE_BALANCE_DAYLIGHT;
    403                 break;
    404             case ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT:
    405                 supportedWhiteBalance +=
    406                     CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT;
    407                 break;
    408             case ANDROID_CONTROL_AWB_MODE_TWILIGHT:
    409                 supportedWhiteBalance +=
    410                     CameraParameters::WHITE_BALANCE_TWILIGHT;
    411                 break;
    412             case ANDROID_CONTROL_AWB_MODE_SHADE:
    413                 supportedWhiteBalance +=
    414                     CameraParameters::WHITE_BALANCE_SHADE;
    415                 break;
    416             // Skipping values not mappable to v1 API
    417             case ANDROID_CONTROL_AWB_MODE_OFF:
    418                 addComma = false;
    419                 break;
    420             default:
    421                 ALOGW("%s: Camera %d: Unknown white balance value: %d",
    422                         __FUNCTION__, cameraId,
    423                         availableWhiteBalanceModes.data.u8[i]);
    424                 addComma = false;
    425                 break;
    426             }
    427         }
    428         params.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE,
    429                 supportedWhiteBalance);
    430     }
    431 
    432     effectMode = ANDROID_CONTROL_EFFECT_MODE_OFF;
    433     params.set(CameraParameters::KEY_EFFECT,
    434             CameraParameters::EFFECT_NONE);
    435 
    436     camera_metadata_ro_entry_t availableEffects =
    437         staticInfo(ANDROID_CONTROL_AVAILABLE_EFFECTS, 0, 0, false);
    438     if (!availableEffects.count) {
    439         params.set(CameraParameters::KEY_SUPPORTED_EFFECTS,
    440                 CameraParameters::EFFECT_NONE);
    441     } else {
    442         String8 supportedEffects;
    443         bool addComma = false;
    444         for (size_t i=0; i < availableEffects.count; i++) {
    445             if (addComma) supportedEffects += ",";
    446             addComma = true;
    447             switch (availableEffects.data.u8[i]) {
    448                 case ANDROID_CONTROL_EFFECT_MODE_OFF:
    449                     supportedEffects +=
    450                         CameraParameters::EFFECT_NONE;
    451                     break;
    452                 case ANDROID_CONTROL_EFFECT_MODE_MONO:
    453                     supportedEffects +=
    454                         CameraParameters::EFFECT_MONO;
    455                     break;
    456                 case ANDROID_CONTROL_EFFECT_MODE_NEGATIVE:
    457                     supportedEffects +=
    458                         CameraParameters::EFFECT_NEGATIVE;
    459                     break;
    460                 case ANDROID_CONTROL_EFFECT_MODE_SOLARIZE:
    461                     supportedEffects +=
    462                         CameraParameters::EFFECT_SOLARIZE;
    463                     break;
    464                 case ANDROID_CONTROL_EFFECT_MODE_SEPIA:
    465                     supportedEffects +=
    466                         CameraParameters::EFFECT_SEPIA;
    467                     break;
    468                 case ANDROID_CONTROL_EFFECT_MODE_POSTERIZE:
    469                     supportedEffects +=
    470                         CameraParameters::EFFECT_POSTERIZE;
    471                     break;
    472                 case ANDROID_CONTROL_EFFECT_MODE_WHITEBOARD:
    473                     supportedEffects +=
    474                         CameraParameters::EFFECT_WHITEBOARD;
    475                     break;
    476                 case ANDROID_CONTROL_EFFECT_MODE_BLACKBOARD:
    477                     supportedEffects +=
    478                         CameraParameters::EFFECT_BLACKBOARD;
    479                     break;
    480                 case ANDROID_CONTROL_EFFECT_MODE_AQUA:
    481                     supportedEffects +=
    482                         CameraParameters::EFFECT_AQUA;
    483                     break;
    484                 default:
    485                     ALOGW("%s: Camera %d: Unknown effect value: %d",
    486                         __FUNCTION__, cameraId, availableEffects.data.u8[i]);
    487                     addComma = false;
    488                     break;
    489             }
    490         }
    491         params.set(CameraParameters::KEY_SUPPORTED_EFFECTS, supportedEffects);
    492     }
    493 
    494     antibandingMode = ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
    495     params.set(CameraParameters::KEY_ANTIBANDING,
    496             CameraParameters::ANTIBANDING_AUTO);
    497 
    498     camera_metadata_ro_entry_t availableAntibandingModes =
    499         staticInfo(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES, 0, 0, false);
    500     if (!availableAntibandingModes.count) {
    501         params.set(CameraParameters::KEY_SUPPORTED_ANTIBANDING,
    502                 CameraParameters::ANTIBANDING_OFF);
    503     } else {
    504         String8 supportedAntibanding;
    505         bool addComma = false;
    506         for (size_t i=0; i < availableAntibandingModes.count; i++) {
    507             if (addComma) supportedAntibanding += ",";
    508             addComma = true;
    509             switch (availableAntibandingModes.data.u8[i]) {
    510                 case ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF:
    511                     supportedAntibanding +=
    512                         CameraParameters::ANTIBANDING_OFF;
    513                     break;
    514                 case ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ:
    515                     supportedAntibanding +=
    516                         CameraParameters::ANTIBANDING_50HZ;
    517                     break;
    518                 case ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ:
    519                     supportedAntibanding +=
    520                         CameraParameters::ANTIBANDING_60HZ;
    521                     break;
    522                 case ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO:
    523                     supportedAntibanding +=
    524                         CameraParameters::ANTIBANDING_AUTO;
    525                     break;
    526                 default:
    527                     ALOGW("%s: Camera %d: Unknown antibanding value: %d",
    528                         __FUNCTION__, cameraId,
    529                             availableAntibandingModes.data.u8[i]);
    530                     addComma = false;
    531                     break;
    532             }
    533         }
    534         params.set(CameraParameters::KEY_SUPPORTED_ANTIBANDING,
    535                 supportedAntibanding);
    536     }
    537 
    538     sceneMode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
    539     params.set(CameraParameters::KEY_SCENE_MODE,
    540             CameraParameters::SCENE_MODE_AUTO);
    541 
    542     camera_metadata_ro_entry_t availableSceneModes =
    543         staticInfo(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, 0, 0, false);
    544     if (!availableSceneModes.count) {
    545         params.remove(CameraParameters::KEY_SCENE_MODE);
    546     } else {
    547         String8 supportedSceneModes(CameraParameters::SCENE_MODE_AUTO);
    548         bool addComma = true;
    549         bool noSceneModes = false;
    550         for (size_t i=0; i < availableSceneModes.count; i++) {
    551             if (addComma) supportedSceneModes += ",";
    552             addComma = true;
    553             switch (availableSceneModes.data.u8[i]) {
    554                 case ANDROID_CONTROL_SCENE_MODE_DISABLED:
    555                     noSceneModes = true;
    556                     break;
    557                 case ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY:
    558                     // Not in old API
    559                     addComma = false;
    560                     break;
    561                 case ANDROID_CONTROL_SCENE_MODE_ACTION:
    562                     supportedSceneModes +=
    563                         CameraParameters::SCENE_MODE_ACTION;
    564                     break;
    565                 case ANDROID_CONTROL_SCENE_MODE_PORTRAIT:
    566                     supportedSceneModes +=
    567                         CameraParameters::SCENE_MODE_PORTRAIT;
    568                     break;
    569                 case ANDROID_CONTROL_SCENE_MODE_LANDSCAPE:
    570                     supportedSceneModes +=
    571                         CameraParameters::SCENE_MODE_LANDSCAPE;
    572                     break;
    573                 case ANDROID_CONTROL_SCENE_MODE_NIGHT:
    574                     supportedSceneModes +=
    575                         CameraParameters::SCENE_MODE_NIGHT;
    576                     break;
    577                 case ANDROID_CONTROL_SCENE_MODE_NIGHT_PORTRAIT:
    578                     supportedSceneModes +=
    579                         CameraParameters::SCENE_MODE_NIGHT_PORTRAIT;
    580                     break;
    581                 case ANDROID_CONTROL_SCENE_MODE_THEATRE:
    582                     supportedSceneModes +=
    583                         CameraParameters::SCENE_MODE_THEATRE;
    584                     break;
    585                 case ANDROID_CONTROL_SCENE_MODE_BEACH:
    586                     supportedSceneModes +=
    587                         CameraParameters::SCENE_MODE_BEACH;
    588                     break;
    589                 case ANDROID_CONTROL_SCENE_MODE_SNOW:
    590                     supportedSceneModes +=
    591                         CameraParameters::SCENE_MODE_SNOW;
    592                     break;
    593                 case ANDROID_CONTROL_SCENE_MODE_SUNSET:
    594                     supportedSceneModes +=
    595                         CameraParameters::SCENE_MODE_SUNSET;
    596                     break;
    597                 case ANDROID_CONTROL_SCENE_MODE_STEADYPHOTO:
    598                     supportedSceneModes +=
    599                         CameraParameters::SCENE_MODE_STEADYPHOTO;
    600                     break;
    601                 case ANDROID_CONTROL_SCENE_MODE_FIREWORKS:
    602                     supportedSceneModes +=
    603                         CameraParameters::SCENE_MODE_FIREWORKS;
    604                     break;
    605                 case ANDROID_CONTROL_SCENE_MODE_SPORTS:
    606                     supportedSceneModes +=
    607                         CameraParameters::SCENE_MODE_SPORTS;
    608                     break;
    609                 case ANDROID_CONTROL_SCENE_MODE_PARTY:
    610                     supportedSceneModes +=
    611                         CameraParameters::SCENE_MODE_PARTY;
    612                     break;
    613                 case ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT:
    614                     supportedSceneModes +=
    615                         CameraParameters::SCENE_MODE_CANDLELIGHT;
    616                     break;
    617                 case ANDROID_CONTROL_SCENE_MODE_BARCODE:
    618                     supportedSceneModes +=
    619                         CameraParameters::SCENE_MODE_BARCODE;
    620                     break;
    621                 case ANDROID_CONTROL_SCENE_MODE_HDR:
    622                     supportedSceneModes +=
    623                         CameraParameters::SCENE_MODE_HDR;
    624                     break;
    625                 default:
    626                     ALOGW("%s: Camera %d: Unknown scene mode value: %d",
    627                         __FUNCTION__, cameraId,
    628                             availableSceneModes.data.u8[i]);
    629                     addComma = false;
    630                     break;
    631             }
    632         }
    633         if (!noSceneModes) {
    634             params.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES,
    635                     supportedSceneModes);
    636         } else {
    637             params.remove(CameraParameters::KEY_SCENE_MODE);
    638         }
    639     }
    640 
    641     bool isFlashAvailable = false;
    642     camera_metadata_ro_entry_t flashAvailable =
    643         staticInfo(ANDROID_FLASH_INFO_AVAILABLE, 0, 1, false);
    644     if (flashAvailable.count) {
    645         isFlashAvailable = flashAvailable.data.u8[0];
    646     }
    647 
    648     camera_metadata_ro_entry_t availableAeModes =
    649         staticInfo(ANDROID_CONTROL_AE_AVAILABLE_MODES, 0, 0, false);
    650 
    651     flashMode = Parameters::FLASH_MODE_OFF;
    652     if (isFlashAvailable) {
    653         params.set(CameraParameters::KEY_FLASH_MODE,
    654                 CameraParameters::FLASH_MODE_OFF);
    655 
    656         String8 supportedFlashModes(CameraParameters::FLASH_MODE_OFF);
    657         supportedFlashModes = supportedFlashModes +
    658             "," + CameraParameters::FLASH_MODE_AUTO +
    659             "," + CameraParameters::FLASH_MODE_ON +
    660             "," + CameraParameters::FLASH_MODE_TORCH;
    661         for (size_t i=0; i < availableAeModes.count; i++) {
    662             if (availableAeModes.data.u8[i] ==
    663                     ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE) {
    664                 supportedFlashModes = supportedFlashModes + "," +
    665                     CameraParameters::FLASH_MODE_RED_EYE;
    666                 break;
    667             }
    668         }
    669         params.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES,
    670                 supportedFlashModes);
    671     } else {
    672         // No flash means null flash mode and supported flash modes keys, so
    673         // remove them just to be safe
    674         params.remove(CameraParameters::KEY_FLASH_MODE);
    675         params.remove(CameraParameters::KEY_SUPPORTED_FLASH_MODES);
    676     }
    677 
    678     camera_metadata_ro_entry_t minFocusDistance =
    679         staticInfo(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, 0, 1, false);
    680 
    681     camera_metadata_ro_entry_t availableAfModes =
    682         staticInfo(ANDROID_CONTROL_AF_AVAILABLE_MODES, 0, 0, false);
    683 
    684     if (!minFocusDistance.count || minFocusDistance.data.f[0] == 0) {
    685         // Fixed-focus lens
    686         focusMode = Parameters::FOCUS_MODE_FIXED;
    687         params.set(CameraParameters::KEY_FOCUS_MODE,
    688                 CameraParameters::FOCUS_MODE_FIXED);
    689         params.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,
    690                 CameraParameters::FOCUS_MODE_FIXED);
    691     } else {
    692         focusMode = Parameters::FOCUS_MODE_AUTO;
    693         params.set(CameraParameters::KEY_FOCUS_MODE,
    694                 CameraParameters::FOCUS_MODE_AUTO);
    695         String8 supportedFocusModes;
    696         bool addComma = false;
    697         camera_metadata_ro_entry_t focusDistanceCalibration =
    698             staticInfo(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION, 0, 0, false);
    699 
    700         if (focusDistanceCalibration.count &&
    701                 focusDistanceCalibration.data.u8[0] !=
    702                 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED) {
    703             supportedFocusModes += CameraParameters::FOCUS_MODE_INFINITY;
    704             addComma = true;
    705         }
    706 
    707         for (size_t i=0; i < availableAfModes.count; i++) {
    708             if (addComma) supportedFocusModes += ",";
    709             addComma = true;
    710             switch (availableAfModes.data.u8[i]) {
    711                 case ANDROID_CONTROL_AF_MODE_AUTO:
    712                     supportedFocusModes +=
    713                         CameraParameters::FOCUS_MODE_AUTO;
    714                     break;
    715                 case ANDROID_CONTROL_AF_MODE_MACRO:
    716                     supportedFocusModes +=
    717                         CameraParameters::FOCUS_MODE_MACRO;
    718                     break;
    719                 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
    720                     supportedFocusModes +=
    721                         CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO;
    722                     break;
    723                 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
    724                     supportedFocusModes +=
    725                         CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE;
    726                     break;
    727                 case ANDROID_CONTROL_AF_MODE_EDOF:
    728                     supportedFocusModes +=
    729                         CameraParameters::FOCUS_MODE_EDOF;
    730                     break;
    731                 // Not supported in old API
    732                 case ANDROID_CONTROL_AF_MODE_OFF:
    733                     addComma = false;
    734                     break;
    735                 default:
    736                     ALOGW("%s: Camera %d: Unknown AF mode value: %d",
    737                         __FUNCTION__, cameraId, availableAfModes.data.u8[i]);
    738                     addComma = false;
    739                     break;
    740             }
    741         }
    742         params.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,
    743                 supportedFocusModes);
    744     }
    745     focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
    746     shadowFocusMode = FOCUS_MODE_INVALID;
    747 
    748     camera_metadata_ro_entry_t max3aRegions = staticInfo(ANDROID_CONTROL_MAX_REGIONS,
    749             Parameters::NUM_REGION, Parameters::NUM_REGION);
    750     if (max3aRegions.count != Parameters::NUM_REGION) return NO_INIT;
    751 
    752     int32_t maxNumFocusAreas = 0;
    753     if (focusMode != Parameters::FOCUS_MODE_FIXED) {
    754         maxNumFocusAreas = max3aRegions.data.i32[Parameters::REGION_AF];
    755     }
    756     params.set(CameraParameters::KEY_MAX_NUM_FOCUS_AREAS, maxNumFocusAreas);
    757     params.set(CameraParameters::KEY_FOCUS_AREAS,
    758             "(0,0,0,0,0)");
    759     focusingAreas.clear();
    760     focusingAreas.add(Parameters::Area(0,0,0,0,0));
    761 
    762     camera_metadata_ro_entry_t availableFocalLengths =
    763         staticInfo(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, 0, 0, false);
    764     if (!availableFocalLengths.count) return NO_INIT;
    765 
    766     float minFocalLength = availableFocalLengths.data.f[0];
    767     params.setFloat(CameraParameters::KEY_FOCAL_LENGTH, minFocalLength);
    768 
    769     float horizFov, vertFov;
    770     res = calculatePictureFovs(&horizFov, &vertFov);
    771     if (res != OK) {
    772         ALOGE("%s: Can't calculate field of views!", __FUNCTION__);
    773         return res;
    774     }
    775 
    776     params.setFloat(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, horizFov);
    777     params.setFloat(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, vertFov);
    778 
    779     exposureCompensation = 0;
    780     params.set(CameraParameters::KEY_EXPOSURE_COMPENSATION,
    781                 exposureCompensation);
    782 
    783     camera_metadata_ro_entry_t exposureCompensationRange =
    784         staticInfo(ANDROID_CONTROL_AE_COMPENSATION_RANGE, 2, 2);
    785     if (!exposureCompensationRange.count) return NO_INIT;
    786 
    787     params.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION,
    788             exposureCompensationRange.data.i32[1]);
    789     params.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION,
    790             exposureCompensationRange.data.i32[0]);
    791 
    792     camera_metadata_ro_entry_t exposureCompensationStep =
    793         staticInfo(ANDROID_CONTROL_AE_COMPENSATION_STEP, 1, 1);
    794     if (!exposureCompensationStep.count) return NO_INIT;
    795 
    796     params.setFloat(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP,
    797             (float)exposureCompensationStep.data.r[0].numerator /
    798             exposureCompensationStep.data.r[0].denominator);
    799 
    800     autoExposureLock = false;
    801     params.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK,
    802             CameraParameters::FALSE);
    803     params.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK_SUPPORTED,
    804             CameraParameters::TRUE);
    805 
    806     autoWhiteBalanceLock = false;
    807     params.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK,
    808             CameraParameters::FALSE);
    809     params.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED,
    810             CameraParameters::TRUE);
    811 
    812     meteringAreas.add(Parameters::Area(0, 0, 0, 0, 0));
    813     params.set(CameraParameters::KEY_MAX_NUM_METERING_AREAS,
    814             max3aRegions.data.i32[Parameters::REGION_AE]);
    815     params.set(CameraParameters::KEY_METERING_AREAS,
    816             "(0,0,0,0,0)");
    817 
    818     zoom = 0;
    819     params.set(CameraParameters::KEY_ZOOM, zoom);
    820     params.set(CameraParameters::KEY_MAX_ZOOM, NUM_ZOOM_STEPS - 1);
    821 
    822     camera_metadata_ro_entry_t maxDigitalZoom =
    823         staticInfo(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM, /*minCount*/1, /*maxCount*/1);
    824     if (!maxDigitalZoom.count) return NO_INIT;
    825 
    826     {
    827         String8 zoomRatios;
    828         float zoom = 1.f;
    829         float zoomIncrement = (maxDigitalZoom.data.f[0] - zoom) /
    830                 (NUM_ZOOM_STEPS-1);
    831         bool addComma = false;
    832         for (size_t i=0; i < NUM_ZOOM_STEPS; i++) {
    833             if (addComma) zoomRatios += ",";
    834             addComma = true;
    835             zoomRatios += String8::format("%d", static_cast<int>(zoom * 100));
    836             zoom += zoomIncrement;
    837         }
    838         params.set(CameraParameters::KEY_ZOOM_RATIOS, zoomRatios);
    839     }
    840 
    841     params.set(CameraParameters::KEY_ZOOM_SUPPORTED,
    842             CameraParameters::TRUE);
    843     params.set(CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED,
    844             CameraParameters::FALSE);
    845 
    846     params.set(CameraParameters::KEY_FOCUS_DISTANCES,
    847             "Infinity,Infinity,Infinity");
    848 
    849     params.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW,
    850             fastInfo.maxFaces);
    851     params.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW,
    852             0);
    853 
    854     params.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT,
    855             CameraParameters::PIXEL_FORMAT_ANDROID_OPAQUE);
    856 
    857     recordingHint = false;
    858     params.set(CameraParameters::KEY_RECORDING_HINT,
    859             CameraParameters::FALSE);
    860 
    861     params.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED,
    862             CameraParameters::TRUE);
    863 
    864     videoStabilization = false;
    865     params.set(CameraParameters::KEY_VIDEO_STABILIZATION,
    866             CameraParameters::FALSE);
    867 
    868     camera_metadata_ro_entry_t availableVideoStabilizationModes =
    869         staticInfo(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, 0, 0,
    870                 false);
    871 
    872     if (availableVideoStabilizationModes.count > 1) {
    873         params.set(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED,
    874                 CameraParameters::TRUE);
    875     } else {
    876         params.set(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED,
    877                 CameraParameters::FALSE);
    878     }
    879 
    880     // Set up initial state for non-Camera.Parameters state variables
    881     videoFormat = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    882     videoDataSpace = HAL_DATASPACE_V0_BT709;
    883     videoBufferMode = hardware::ICamera::VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV;
    884     playShutterSound = true;
    885     enableFaceDetect = false;
    886 
    887     enableFocusMoveMessages = false;
    888     afTriggerCounter = 1;
    889     afStateCounter = 0;
    890     currentAfTriggerId = -1;
    891     afInMotion = false;
    892 
    893     precaptureTriggerCounter = 1;
    894 
    895     takePictureCounter = 0;
    896 
    897     previewCallbackFlags = 0;
    898     previewCallbackOneShot = false;
    899     previewCallbackSurface = false;
    900 
    901     Size maxJpegSize = getMaxSize(getAvailableJpegSizes());
    902     int64_t minFrameDurationNs = getJpegStreamMinFrameDurationNs(maxJpegSize);
    903 
    904     slowJpegMode = false;
    905     if (minFrameDurationNs > kSlowJpegModeThreshold) {
    906         slowJpegMode = true;
    907         // Slow jpeg devices does not support video snapshot without
    908         // slowing down preview.
    909         // TODO: support video size video snapshot only?
    910         params.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED,
    911             CameraParameters::FALSE);
    912     }
    913 
    914     char value[PROPERTY_VALUE_MAX];
    915     property_get("camera.disable_zsl_mode", value, "0");
    916     if (!strcmp(value,"1") || slowJpegMode) {
    917         ALOGI("Camera %d: Disabling ZSL mode", cameraId);
    918         allowZslMode = false;
    919     } else {
    920         allowZslMode = true;
    921     }
    922 
    923     ALOGI("%s: allowZslMode: %d slowJpegMode %d", __FUNCTION__, allowZslMode, slowJpegMode);
    924 
    925     state = STOPPED;
    926 
    927     paramsFlattened = params.flatten();
    928 
    929     return OK;
    930 }
    931 
    932 String8 Parameters::get() const {
    933     return paramsFlattened;
    934 }
    935 
    936 status_t Parameters::buildFastInfo() {
    937 
    938     camera_metadata_ro_entry_t activeArraySize =
    939         staticInfo(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, 2, 4);
    940     if (!activeArraySize.count) return NO_INIT;
    941     int32_t arrayWidth;
    942     int32_t arrayHeight;
    943     if (activeArraySize.count == 2) {
    944         ALOGW("%s: Camera %d: activeArraySize is missing xmin/ymin!",
    945                 __FUNCTION__, cameraId);
    946         arrayWidth = activeArraySize.data.i32[0];
    947         arrayHeight = activeArraySize.data.i32[1];
    948     } else if (activeArraySize.count == 4) {
    949         arrayWidth = activeArraySize.data.i32[2];
    950         arrayHeight = activeArraySize.data.i32[3];
    951     } else return NO_INIT;
    952 
    953     // We'll set the target FPS range for still captures to be as wide
    954     // as possible to give the HAL maximum latitude for exposure selection
    955     camera_metadata_ro_entry_t availableFpsRanges =
    956         staticInfo(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, 2);
    957     if (availableFpsRanges.count < 2 || availableFpsRanges.count % 2 != 0) {
    958         return NO_INIT;
    959     }
    960 
    961     int32_t bestStillCaptureFpsRange[2] = {
    962         availableFpsRanges.data.i32[0], availableFpsRanges.data.i32[1]
    963     };
    964     int32_t curRange =
    965             bestStillCaptureFpsRange[1] - bestStillCaptureFpsRange[0];
    966     for (size_t i = 2; i < availableFpsRanges.count; i += 2) {
    967         int32_t nextRange =
    968                 availableFpsRanges.data.i32[i + 1] -
    969                 availableFpsRanges.data.i32[i];
    970         if ( (nextRange > curRange) ||       // Maximize size of FPS range first
    971                 (nextRange == curRange &&    // Then minimize low-end FPS
    972                  bestStillCaptureFpsRange[0] > availableFpsRanges.data.i32[i])) {
    973 
    974             bestStillCaptureFpsRange[0] = availableFpsRanges.data.i32[i];
    975             bestStillCaptureFpsRange[1] = availableFpsRanges.data.i32[i + 1];
    976             curRange = nextRange;
    977         }
    978     }
    979 
    980     camera_metadata_ro_entry_t availableFaceDetectModes =
    981         staticInfo(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, 0, 0,
    982                 false);
    983 
    984     uint8_t bestFaceDetectMode =
    985         ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
    986     for (size_t i = 0 ; i < availableFaceDetectModes.count; i++) {
    987         switch (availableFaceDetectModes.data.u8[i]) {
    988             case ANDROID_STATISTICS_FACE_DETECT_MODE_OFF:
    989                 break;
    990             case ANDROID_STATISTICS_FACE_DETECT_MODE_SIMPLE:
    991                 if (bestFaceDetectMode !=
    992                         ANDROID_STATISTICS_FACE_DETECT_MODE_FULL) {
    993                     bestFaceDetectMode =
    994                         ANDROID_STATISTICS_FACE_DETECT_MODE_SIMPLE;
    995                 }
    996                 break;
    997             case ANDROID_STATISTICS_FACE_DETECT_MODE_FULL:
    998                 bestFaceDetectMode =
    999                     ANDROID_STATISTICS_FACE_DETECT_MODE_FULL;
   1000                 break;
   1001             default:
   1002                 ALOGE("%s: Camera %d: Unknown face detect mode %d:",
   1003                         __FUNCTION__, cameraId,
   1004                         availableFaceDetectModes.data.u8[i]);
   1005                 return NO_INIT;
   1006         }
   1007     }
   1008 
   1009     int32_t maxFaces = 0;
   1010     camera_metadata_ro_entry_t maxFacesDetected =
   1011         staticInfo(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, 0, 1, false);
   1012     if (maxFacesDetected.count) {
   1013         maxFaces = maxFacesDetected.data.i32[0];
   1014     }
   1015 
   1016     camera_metadata_ro_entry_t availableSceneModes =
   1017         staticInfo(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, 0, 0, false);
   1018     camera_metadata_ro_entry_t sceneModeOverrides =
   1019         staticInfo(ANDROID_CONTROL_SCENE_MODE_OVERRIDES, 0, 0, false);
   1020     camera_metadata_ro_entry_t minFocusDistance =
   1021         staticInfo(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, 0, 0, false);
   1022     bool fixedLens = minFocusDistance.count == 0 ||
   1023         minFocusDistance.data.f[0] == 0;
   1024 
   1025     camera_metadata_ro_entry_t focusDistanceCalibration =
   1026             staticInfo(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION, 0, 0,
   1027                     false);
   1028     bool canFocusInfinity = (focusDistanceCalibration.count &&
   1029             focusDistanceCalibration.data.u8[0] !=
   1030             ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED);
   1031 
   1032     camera_metadata_ro_entry_t availableFocalLengths =
   1033         staticInfo(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS);
   1034     if (!availableFocalLengths.count) return NO_INIT;
   1035 
   1036     SortedVector<int32_t> availableFormats = getAvailableOutputFormats();
   1037     if (!availableFormats.size()) return NO_INIT;
   1038 
   1039 
   1040     if (sceneModeOverrides.count > 0) {
   1041         // sceneModeOverrides is defined to have 3 entries for each scene mode,
   1042         // which are AE, AWB, and AF override modes the HAL wants for that scene
   1043         // mode.
   1044         const size_t kModesPerSceneMode = 3;
   1045         if (sceneModeOverrides.count !=
   1046                 availableSceneModes.count * kModesPerSceneMode) {
   1047             ALOGE("%s: Camera %d: Scene mode override list is an "
   1048                     "unexpected size: %zu (expected %zu)", __FUNCTION__,
   1049                     cameraId, sceneModeOverrides.count,
   1050                     availableSceneModes.count * kModesPerSceneMode);
   1051             return NO_INIT;
   1052         }
   1053         for (size_t i = 0; i < availableSceneModes.count; i++) {
   1054             DeviceInfo::OverrideModes modes;
   1055             uint8_t aeMode =
   1056                     sceneModeOverrides.data.u8[i * kModesPerSceneMode + 0];
   1057             switch(aeMode) {
   1058                 case ANDROID_CONTROL_AE_MODE_ON:
   1059                     modes.flashMode = FLASH_MODE_OFF;
   1060                     break;
   1061                 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH:
   1062                     modes.flashMode = FLASH_MODE_AUTO;
   1063                     break;
   1064                 case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH:
   1065                     modes.flashMode = FLASH_MODE_ON;
   1066                     break;
   1067                 case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE:
   1068                     modes.flashMode = FLASH_MODE_RED_EYE;
   1069                     break;
   1070                 default:
   1071                     ALOGE("%s: Unknown override AE mode: %d", __FUNCTION__,
   1072                             aeMode);
   1073                     modes.flashMode = FLASH_MODE_INVALID;
   1074                     break;
   1075             }
   1076             modes.wbMode =
   1077                     sceneModeOverrides.data.u8[i * kModesPerSceneMode + 1];
   1078             uint8_t afMode =
   1079                     sceneModeOverrides.data.u8[i * kModesPerSceneMode + 2];
   1080             switch(afMode) {
   1081                 case ANDROID_CONTROL_AF_MODE_OFF:
   1082                     if (!fixedLens && !canFocusInfinity) {
   1083                         ALOGE("%s: Camera %d: Scene mode override lists asks for"
   1084                                 " fixed focus on a device with focuser but not"
   1085                                 " calibrated for infinity focus", __FUNCTION__,
   1086                                 cameraId);
   1087                         return NO_INIT;
   1088                     }
   1089                     modes.focusMode = fixedLens ?
   1090                             FOCUS_MODE_FIXED : FOCUS_MODE_INFINITY;
   1091                     break;
   1092                 case ANDROID_CONTROL_AF_MODE_AUTO:
   1093                 case ANDROID_CONTROL_AF_MODE_MACRO:
   1094                 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
   1095                 case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
   1096                 case ANDROID_CONTROL_AF_MODE_EDOF:
   1097                     modes.focusMode = static_cast<focusMode_t>(afMode);
   1098                     break;
   1099                 default:
   1100                     ALOGE("%s: Unknown override AF mode: %d", __FUNCTION__,
   1101                             afMode);
   1102                     modes.focusMode = FOCUS_MODE_INVALID;
   1103                     break;
   1104             }
   1105             fastInfo.sceneModeOverrides.add(availableSceneModes.data.u8[i],
   1106                     modes);
   1107         }
   1108     }
   1109 
   1110     fastInfo.arrayWidth = arrayWidth;
   1111     fastInfo.arrayHeight = arrayHeight;
   1112     fastInfo.bestStillCaptureFpsRange[0] = bestStillCaptureFpsRange[0];
   1113     fastInfo.bestStillCaptureFpsRange[1] = bestStillCaptureFpsRange[1];
   1114     fastInfo.bestFaceDetectMode = bestFaceDetectMode;
   1115     fastInfo.maxFaces = maxFaces;
   1116 
   1117     // Find smallest (widest-angle) focal length to use as basis of still
   1118     // picture FOV reporting.
   1119     fastInfo.minFocalLength = availableFocalLengths.data.f[0];
   1120     for (size_t i = 1; i < availableFocalLengths.count; i++) {
   1121         if (fastInfo.minFocalLength > availableFocalLengths.data.f[i]) {
   1122             fastInfo.minFocalLength = availableFocalLengths.data.f[i];
   1123         }
   1124     }
   1125 
   1126     // Check if the HAL supports HAL_PIXEL_FORMAT_YCbCr_420_888
   1127     fastInfo.useFlexibleYuv = false;
   1128     for (size_t i = 0; i < availableFormats.size(); i++) {
   1129         if (availableFormats[i] == HAL_PIXEL_FORMAT_YCbCr_420_888) {
   1130             fastInfo.useFlexibleYuv = true;
   1131             break;
   1132         }
   1133     }
   1134     ALOGV("Camera %d: Flexible YUV %s supported",
   1135             cameraId, fastInfo.useFlexibleYuv ? "is" : "is not");
   1136 
   1137     fastInfo.maxJpegSize = getMaxSize(getAvailableJpegSizes());
   1138 
   1139     return OK;
   1140 }
   1141 
   1142 status_t Parameters::buildQuirks() {
   1143     camera_metadata_ro_entry_t entry;
   1144     entry = info->find(ANDROID_QUIRKS_TRIGGER_AF_WITH_AUTO);
   1145     quirks.triggerAfWithAuto = (entry.count != 0 && entry.data.u8[0] == 1);
   1146     ALOGV_IF(quirks.triggerAfWithAuto, "Camera %d: Quirk triggerAfWithAuto enabled",
   1147             cameraId);
   1148 
   1149     entry = info->find(ANDROID_QUIRKS_USE_ZSL_FORMAT);
   1150     quirks.useZslFormat = (entry.count != 0 && entry.data.u8[0] == 1);
   1151     ALOGV_IF(quirks.useZslFormat, "Camera %d: Quirk useZslFormat enabled",
   1152             cameraId);
   1153 
   1154     entry = info->find(ANDROID_QUIRKS_METERING_CROP_REGION);
   1155     quirks.meteringCropRegion = (entry.count != 0 && entry.data.u8[0] == 1);
   1156     ALOGV_IF(quirks.meteringCropRegion, "Camera %d: Quirk meteringCropRegion"
   1157                 " enabled", cameraId);
   1158 
   1159     entry = info->find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
   1160     quirks.partialResults = (entry.count != 0 && entry.data.u8[0] == 1);
   1161     ALOGV_IF(quirks.partialResults, "Camera %d: Quirk usePartialResult"
   1162                 " enabled", cameraId);
   1163 
   1164     return OK;
   1165 }
   1166 
   1167 camera_metadata_ro_entry_t Parameters::staticInfo(uint32_t tag,
   1168         size_t minCount, size_t maxCount, bool required) const {
   1169     camera_metadata_ro_entry_t entry = info->find(tag);
   1170 
   1171     if (CC_UNLIKELY( entry.count == 0 ) && required) {
   1172         const char* tagSection = get_camera_metadata_section_name(tag);
   1173         if (tagSection == NULL) tagSection = "<unknown>";
   1174         const char* tagName = get_camera_metadata_tag_name(tag);
   1175         if (tagName == NULL) tagName = "<unknown>";
   1176 
   1177         ALOGE("Error finding static metadata entry '%s.%s' (%x)",
   1178                 tagSection, tagName, tag);
   1179     } else if (CC_UNLIKELY(
   1180             (minCount != 0 && entry.count < minCount) ||
   1181             (maxCount != 0 && entry.count > maxCount) ) ) {
   1182         const char* tagSection = get_camera_metadata_section_name(tag);
   1183         if (tagSection == NULL) tagSection = "<unknown>";
   1184         const char* tagName = get_camera_metadata_tag_name(tag);
   1185         if (tagName == NULL) tagName = "<unknown>";
   1186         ALOGE("Malformed static metadata entry '%s.%s' (%x):"
   1187                 "Expected between %zu and %zu values, but got %zu values",
   1188                 tagSection, tagName, tag, minCount, maxCount, entry.count);
   1189     }
   1190 
   1191     return entry;
   1192 }
   1193 
   1194 status_t Parameters::set(const String8& paramString) {
   1195     status_t res;
   1196 
   1197     CameraParameters2 newParams(paramString);
   1198 
   1199     // TODO: Currently ignoring any changes to supposedly read-only parameters
   1200     // such as supported preview sizes, etc. Should probably produce an error if
   1201     // they're changed.
   1202 
   1203     /** Extract and verify new parameters */
   1204 
   1205     size_t i;
   1206 
   1207     Parameters validatedParams(*this);
   1208 
   1209     // PREVIEW_SIZE
   1210     newParams.getPreviewSize(&validatedParams.previewWidth,
   1211             &validatedParams.previewHeight);
   1212 
   1213     if (validatedParams.previewWidth != previewWidth ||
   1214             validatedParams.previewHeight != previewHeight) {
   1215         if (state >= PREVIEW) {
   1216             ALOGE("%s: Preview size cannot be updated when preview "
   1217                     "is active! (Currently %d x %d, requested %d x %d",
   1218                     __FUNCTION__,
   1219                     previewWidth, previewHeight,
   1220                     validatedParams.previewWidth, validatedParams.previewHeight);
   1221             return BAD_VALUE;
   1222         }
   1223         for (i = 0; i < availablePreviewSizes.size(); i++) {
   1224             if ((availablePreviewSizes[i].width ==
   1225                     validatedParams.previewWidth) &&
   1226                 (availablePreviewSizes[i].height ==
   1227                     validatedParams.previewHeight)) break;
   1228         }
   1229         if (i == availablePreviewSizes.size()) {
   1230             ALOGE("%s: Requested preview size %d x %d is not supported",
   1231                     __FUNCTION__, validatedParams.previewWidth,
   1232                     validatedParams.previewHeight);
   1233             return BAD_VALUE;
   1234         }
   1235     }
   1236 
   1237     // RECORDING_HINT (always supported)
   1238     validatedParams.recordingHint = boolFromString(
   1239         newParams.get(CameraParameters::KEY_RECORDING_HINT) );
   1240     IF_ALOGV() { // Avoid unused variable warning
   1241         bool recordingHintChanged =
   1242                 validatedParams.recordingHint != recordingHint;
   1243         if (recordingHintChanged) {
   1244             ALOGV("%s: Recording hint changed to %d",
   1245                   __FUNCTION__, validatedParams.recordingHint);
   1246         }
   1247     }
   1248 
   1249     // PREVIEW_FPS_RANGE
   1250 
   1251     /**
   1252      * Use the single FPS value if it was set later than the range.
   1253      * Otherwise, use the range value.
   1254      */
   1255     bool fpsUseSingleValue;
   1256     {
   1257         const char *fpsRange, *fpsSingle;
   1258 
   1259         fpsRange = newParams.get(CameraParameters::KEY_PREVIEW_FRAME_RATE);
   1260         fpsSingle = newParams.get(CameraParameters::KEY_PREVIEW_FPS_RANGE);
   1261 
   1262         /**
   1263          * Pick either the range or the single key if only one was set.
   1264          *
   1265          * If both are set, pick the one that has greater set order.
   1266          */
   1267         if (fpsRange == NULL && fpsSingle == NULL) {
   1268             ALOGE("%s: FPS was not set. One of %s or %s must be set.",
   1269                   __FUNCTION__, CameraParameters::KEY_PREVIEW_FRAME_RATE,
   1270                   CameraParameters::KEY_PREVIEW_FPS_RANGE);
   1271             return BAD_VALUE;
   1272         } else if (fpsRange == NULL) {
   1273             fpsUseSingleValue = true;
   1274             ALOGV("%s: FPS range not set, using FPS single value",
   1275                   __FUNCTION__);
   1276         } else if (fpsSingle == NULL) {
   1277             fpsUseSingleValue = false;
   1278             ALOGV("%s: FPS single not set, using FPS range value",
   1279                   __FUNCTION__);
   1280         } else {
   1281             int fpsKeyOrder;
   1282             res = newParams.compareSetOrder(
   1283                     CameraParameters::KEY_PREVIEW_FRAME_RATE,
   1284                     CameraParameters::KEY_PREVIEW_FPS_RANGE,
   1285                     &fpsKeyOrder);
   1286             LOG_ALWAYS_FATAL_IF(res != OK, "Impossibly bad FPS keys");
   1287 
   1288             fpsUseSingleValue = (fpsKeyOrder > 0);
   1289 
   1290         }
   1291 
   1292         ALOGV("%s: Preview FPS value is used from '%s'",
   1293               __FUNCTION__, fpsUseSingleValue ? "single" : "range");
   1294     }
   1295     newParams.getPreviewFpsRange(&validatedParams.previewFpsRange[0],
   1296             &validatedParams.previewFpsRange[1]);
   1297 
   1298     validatedParams.previewFpsRange[0] /= kFpsToApiScale;
   1299     validatedParams.previewFpsRange[1] /= kFpsToApiScale;
   1300 
   1301     // Ignore the FPS range if the FPS single has higher precedence
   1302     if (!fpsUseSingleValue) {
   1303         ALOGV("%s: Preview FPS range (%d, %d)", __FUNCTION__,
   1304                 validatedParams.previewFpsRange[0],
   1305                 validatedParams.previewFpsRange[1]);
   1306 
   1307         camera_metadata_ro_entry_t availablePreviewFpsRanges =
   1308             staticInfo(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, 2);
   1309         for (i = 0; i < availablePreviewFpsRanges.count; i += 2) {
   1310             if ((availablePreviewFpsRanges.data.i32[i] ==
   1311                     validatedParams.previewFpsRange[0]) &&
   1312                 (availablePreviewFpsRanges.data.i32[i+1] ==
   1313                     validatedParams.previewFpsRange[1]) ) {
   1314                 break;
   1315             }
   1316         }
   1317         if (i == availablePreviewFpsRanges.count) {
   1318             ALOGE("%s: Requested preview FPS range %d - %d is not supported",
   1319                 __FUNCTION__, validatedParams.previewFpsRange[0],
   1320                     validatedParams.previewFpsRange[1]);
   1321             return BAD_VALUE;
   1322         }
   1323     }
   1324 
   1325     // PREVIEW_FORMAT
   1326     validatedParams.previewFormat =
   1327             formatStringToEnum(newParams.getPreviewFormat());
   1328     if (validatedParams.previewFormat != previewFormat) {
   1329         if (state >= PREVIEW) {
   1330             ALOGE("%s: Preview format cannot be updated when preview "
   1331                     "is active!", __FUNCTION__);
   1332             return BAD_VALUE;
   1333         }
   1334         SortedVector<int32_t> availableFormats = getAvailableOutputFormats();
   1335         // If using flexible YUV, always support NV21/YV12. Otherwise, check
   1336         // HAL's list.
   1337         if (! (fastInfo.useFlexibleYuv &&
   1338                 (validatedParams.previewFormat ==
   1339                         HAL_PIXEL_FORMAT_YCrCb_420_SP ||
   1340                  validatedParams.previewFormat ==
   1341                         HAL_PIXEL_FORMAT_YV12) ) ) {
   1342             // Not using flexible YUV format, so check explicitly
   1343             for (i = 0; i < availableFormats.size(); i++) {
   1344                 if (availableFormats[i] == validatedParams.previewFormat) break;
   1345             }
   1346             if (i == availableFormats.size()) {
   1347                 ALOGE("%s: Requested preview format %s (0x%x) is not supported",
   1348                         __FUNCTION__, newParams.getPreviewFormat(),
   1349                         validatedParams.previewFormat);
   1350                 return BAD_VALUE;
   1351             }
   1352         }
   1353     }
   1354 
   1355     // PREVIEW_FRAME_RATE Deprecated
   1356     // - Use only if the single FPS value was set later than the FPS range
   1357     if (fpsUseSingleValue) {
   1358         int previewFps = newParams.getPreviewFrameRate();
   1359         ALOGV("%s: Preview FPS single value requested: %d",
   1360               __FUNCTION__, previewFps);
   1361         {
   1362             camera_metadata_ro_entry_t availableFrameRates =
   1363                 staticInfo(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
   1364             /**
   1365               * If recording hint is set, find the range that encompasses
   1366               * previewFps with the largest min index.
   1367               *
   1368               * If recording hint is not set, find the range with previewFps
   1369               * with the smallest min index.
   1370               *
   1371               * Either way, in case of multiple ranges, break the tie by
   1372               * selecting the smaller range.
   1373               */
   1374 
   1375             // all ranges which have previewFps
   1376             Vector<Range> candidateRanges;
   1377             for (i = 0; i < availableFrameRates.count; i+=2) {
   1378                 Range r = {
   1379                             availableFrameRates.data.i32[i],
   1380                             availableFrameRates.data.i32[i+1]
   1381                 };
   1382 
   1383                 if (r.min <= previewFps && previewFps <= r.max) {
   1384                     candidateRanges.push(r);
   1385                 }
   1386             }
   1387             if (candidateRanges.isEmpty()) {
   1388                 ALOGE("%s: Requested preview frame rate %d is not supported",
   1389                         __FUNCTION__, previewFps);
   1390                 return BAD_VALUE;
   1391             }
   1392             // most applicable range with targetFps
   1393             Range bestRange = candidateRanges[0];
   1394             for (i = 1; i < candidateRanges.size(); ++i) {
   1395                 Range r = candidateRanges[i];
   1396 
   1397                 // Find by largest minIndex in recording mode
   1398                 if (validatedParams.recordingHint) {
   1399                     if (r.min > bestRange.min) {
   1400                         bestRange = r;
   1401                     }
   1402                     else if (r.min == bestRange.min && r.max < bestRange.max) {
   1403                         bestRange = r;
   1404                     }
   1405                 }
   1406                 // Find by smallest minIndex in preview mode
   1407                 else {
   1408                     if (r.min < bestRange.min) {
   1409                         bestRange = r;
   1410                     }
   1411                     else if (r.min == bestRange.min && r.max < bestRange.max) {
   1412                         bestRange = r;
   1413                     }
   1414                 }
   1415             }
   1416 
   1417             validatedParams.previewFpsRange[0] =
   1418                     bestRange.min;
   1419             validatedParams.previewFpsRange[1] =
   1420                     bestRange.max;
   1421 
   1422             ALOGV("%s: New preview FPS range: %d, %d, recordingHint = %d",
   1423                 __FUNCTION__,
   1424                 validatedParams.previewFpsRange[0],
   1425                 validatedParams.previewFpsRange[1],
   1426                 validatedParams.recordingHint);
   1427         }
   1428     }
   1429 
   1430     /**
   1431      * Update Preview FPS and Preview FPS ranges based on
   1432      * what we actually set.
   1433      *
   1434      * This updates the API-visible (Camera.Parameters#getParameters) values of
   1435      * the FPS fields, not only the internal versions.
   1436      *
   1437      * Order matters: The value that was set last takes precedence.
   1438      * - If the client does a setParameters(getParameters()) we retain
   1439      *   the same order for preview FPS.
   1440      */
   1441     if (!fpsUseSingleValue) {
   1442         // Set fps single, then fps range (range wins)
   1443         newParams.setPreviewFrameRate(
   1444                 fpsFromRange(/*min*/validatedParams.previewFpsRange[0],
   1445                              /*max*/validatedParams.previewFpsRange[1]));
   1446         newParams.setPreviewFpsRange(
   1447                 validatedParams.previewFpsRange[0] * kFpsToApiScale,
   1448                 validatedParams.previewFpsRange[1] * kFpsToApiScale);
   1449     } else {
   1450         // Set fps range, then fps single (single wins)
   1451         newParams.setPreviewFpsRange(
   1452                 validatedParams.previewFpsRange[0] * kFpsToApiScale,
   1453                 validatedParams.previewFpsRange[1] * kFpsToApiScale);
   1454         // Set this to the same value, but with higher priority
   1455         newParams.setPreviewFrameRate(
   1456                 newParams.getPreviewFrameRate());
   1457     }
   1458 
   1459     // PICTURE_SIZE
   1460     newParams.getPictureSize(&validatedParams.pictureWidth,
   1461             &validatedParams.pictureHeight);
   1462     if (validatedParams.pictureWidth != pictureWidth ||
   1463             validatedParams.pictureHeight != pictureHeight) {
   1464         Vector<Size> availablePictureSizes = getAvailableJpegSizes();
   1465         for (i = 0; i < availablePictureSizes.size(); i++) {
   1466             if ((availablePictureSizes[i].width ==
   1467                     validatedParams.pictureWidth) &&
   1468                 (availablePictureSizes[i].height ==
   1469                     validatedParams.pictureHeight)) break;
   1470         }
   1471         if (i == availablePictureSizes.size()) {
   1472             ALOGE("%s: Requested picture size %d x %d is not supported",
   1473                     __FUNCTION__, validatedParams.pictureWidth,
   1474                     validatedParams.pictureHeight);
   1475             return BAD_VALUE;
   1476         }
   1477     }
   1478 
   1479     // JPEG_THUMBNAIL_WIDTH/HEIGHT
   1480     validatedParams.jpegThumbSize[0] =
   1481             newParams.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH);
   1482     validatedParams.jpegThumbSize[1] =
   1483             newParams.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT);
   1484     if (validatedParams.jpegThumbSize[0] != jpegThumbSize[0] ||
   1485             validatedParams.jpegThumbSize[1] != jpegThumbSize[1]) {
   1486         camera_metadata_ro_entry_t availableJpegThumbSizes =
   1487             staticInfo(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES);
   1488         for (i = 0; i < availableJpegThumbSizes.count; i+=2) {
   1489             if ((availableJpegThumbSizes.data.i32[i] ==
   1490                     validatedParams.jpegThumbSize[0]) &&
   1491                 (availableJpegThumbSizes.data.i32[i+1] ==
   1492                     validatedParams.jpegThumbSize[1])) break;
   1493         }
   1494         if (i == availableJpegThumbSizes.count) {
   1495             ALOGE("%s: Requested JPEG thumbnail size %d x %d is not supported",
   1496                     __FUNCTION__, validatedParams.jpegThumbSize[0],
   1497                     validatedParams.jpegThumbSize[1]);
   1498             return BAD_VALUE;
   1499         }
   1500     }
   1501 
   1502     // JPEG_THUMBNAIL_QUALITY
   1503     int quality = newParams.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY);
   1504     // also makes sure quality fits in uint8_t
   1505     if (quality < 0 || quality > 100) {
   1506         ALOGE("%s: Requested JPEG thumbnail quality %d is not supported",
   1507                 __FUNCTION__, quality);
   1508         return BAD_VALUE;
   1509     }
   1510     validatedParams.jpegThumbQuality = quality;
   1511 
   1512     // JPEG_QUALITY
   1513     quality = newParams.getInt(CameraParameters::KEY_JPEG_QUALITY);
   1514     // also makes sure quality fits in uint8_t
   1515     if (quality < 0 || quality > 100) {
   1516         ALOGE("%s: Requested JPEG quality %d is not supported",
   1517                 __FUNCTION__, quality);
   1518         return BAD_VALUE;
   1519     }
   1520     validatedParams.jpegQuality = quality;
   1521 
   1522     // ROTATION
   1523     validatedParams.jpegRotation =
   1524             newParams.getInt(CameraParameters::KEY_ROTATION);
   1525     if (validatedParams.jpegRotation != 0 &&
   1526             validatedParams.jpegRotation != 90 &&
   1527             validatedParams.jpegRotation != 180 &&
   1528             validatedParams.jpegRotation != 270) {
   1529         ALOGE("%s: Requested picture rotation angle %d is not supported",
   1530                 __FUNCTION__, validatedParams.jpegRotation);
   1531         return BAD_VALUE;
   1532     }
   1533 
   1534     // GPS
   1535 
   1536     const char *gpsLatStr =
   1537             newParams.get(CameraParameters::KEY_GPS_LATITUDE);
   1538     if (gpsLatStr != NULL) {
   1539         const char *gpsLongStr =
   1540                 newParams.get(CameraParameters::KEY_GPS_LONGITUDE);
   1541         const char *gpsAltitudeStr =
   1542                 newParams.get(CameraParameters::KEY_GPS_ALTITUDE);
   1543         const char *gpsTimeStr =
   1544                 newParams.get(CameraParameters::KEY_GPS_TIMESTAMP);
   1545         const char *gpsProcMethodStr =
   1546                 newParams.get(CameraParameters::KEY_GPS_PROCESSING_METHOD);
   1547         if (gpsLongStr == NULL ||
   1548                 gpsAltitudeStr == NULL ||
   1549                 gpsTimeStr == NULL ||
   1550                 gpsProcMethodStr == NULL) {
   1551             ALOGE("%s: Incomplete set of GPS parameters provided",
   1552                     __FUNCTION__);
   1553             return BAD_VALUE;
   1554         }
   1555         char *endPtr;
   1556         errno = 0;
   1557         validatedParams.gpsCoordinates[0] = strtod(gpsLatStr, &endPtr);
   1558         if (errno || endPtr == gpsLatStr) {
   1559             ALOGE("%s: Malformed GPS latitude: %s", __FUNCTION__, gpsLatStr);
   1560             return BAD_VALUE;
   1561         }
   1562         errno = 0;
   1563         validatedParams.gpsCoordinates[1] = strtod(gpsLongStr, &endPtr);
   1564         if (errno || endPtr == gpsLongStr) {
   1565             ALOGE("%s: Malformed GPS longitude: %s", __FUNCTION__, gpsLongStr);
   1566             return BAD_VALUE;
   1567         }
   1568         errno = 0;
   1569         validatedParams.gpsCoordinates[2] = strtod(gpsAltitudeStr, &endPtr);
   1570         if (errno || endPtr == gpsAltitudeStr) {
   1571             ALOGE("%s: Malformed GPS altitude: %s", __FUNCTION__,
   1572                     gpsAltitudeStr);
   1573             return BAD_VALUE;
   1574         }
   1575         errno = 0;
   1576         validatedParams.gpsTimestamp = strtoll(gpsTimeStr, &endPtr, 10);
   1577         if (errno || endPtr == gpsTimeStr) {
   1578             ALOGE("%s: Malformed GPS timestamp: %s", __FUNCTION__, gpsTimeStr);
   1579             return BAD_VALUE;
   1580         }
   1581         validatedParams.gpsProcessingMethod = gpsProcMethodStr;
   1582 
   1583         validatedParams.gpsEnabled = true;
   1584     } else {
   1585         validatedParams.gpsEnabled = false;
   1586     }
   1587 
   1588     // EFFECT
   1589     validatedParams.effectMode = effectModeStringToEnum(
   1590         newParams.get(CameraParameters::KEY_EFFECT) );
   1591     if (validatedParams.effectMode != effectMode) {
   1592         camera_metadata_ro_entry_t availableEffectModes =
   1593             staticInfo(ANDROID_CONTROL_AVAILABLE_EFFECTS);
   1594         for (i = 0; i < availableEffectModes.count; i++) {
   1595             if (validatedParams.effectMode == availableEffectModes.data.u8[i]) break;
   1596         }
   1597         if (i == availableEffectModes.count) {
   1598             ALOGE("%s: Requested effect mode \"%s\" is not supported",
   1599                     __FUNCTION__,
   1600                     newParams.get(CameraParameters::KEY_EFFECT) );
   1601             return BAD_VALUE;
   1602         }
   1603     }
   1604 
   1605     // ANTIBANDING
   1606     validatedParams.antibandingMode = abModeStringToEnum(
   1607         newParams.get(CameraParameters::KEY_ANTIBANDING) );
   1608     if (validatedParams.antibandingMode != antibandingMode) {
   1609         camera_metadata_ro_entry_t availableAbModes =
   1610             staticInfo(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES);
   1611         for (i = 0; i < availableAbModes.count; i++) {
   1612             if (validatedParams.antibandingMode == availableAbModes.data.u8[i])
   1613                 break;
   1614         }
   1615         if (i == availableAbModes.count) {
   1616             ALOGE("%s: Requested antibanding mode \"%s\" is not supported",
   1617                     __FUNCTION__,
   1618                     newParams.get(CameraParameters::KEY_ANTIBANDING));
   1619             return BAD_VALUE;
   1620         }
   1621     }
   1622 
   1623     // SCENE_MODE
   1624     validatedParams.sceneMode = sceneModeStringToEnum(
   1625         newParams.get(CameraParameters::KEY_SCENE_MODE) );
   1626     if (validatedParams.sceneMode != sceneMode &&
   1627             validatedParams.sceneMode !=
   1628             ANDROID_CONTROL_SCENE_MODE_DISABLED) {
   1629         camera_metadata_ro_entry_t availableSceneModes =
   1630             staticInfo(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
   1631         for (i = 0; i < availableSceneModes.count; i++) {
   1632             if (validatedParams.sceneMode == availableSceneModes.data.u8[i])
   1633                 break;
   1634         }
   1635         if (i == availableSceneModes.count) {
   1636             ALOGE("%s: Requested scene mode \"%s\" is not supported",
   1637                     __FUNCTION__,
   1638                     newParams.get(CameraParameters::KEY_SCENE_MODE));
   1639             return BAD_VALUE;
   1640         }
   1641     }
   1642     bool sceneModeSet =
   1643             validatedParams.sceneMode != ANDROID_CONTROL_SCENE_MODE_DISABLED;
   1644 
   1645     // FLASH_MODE
   1646     if (sceneModeSet) {
   1647         validatedParams.flashMode =
   1648                 fastInfo.sceneModeOverrides.
   1649                         valueFor(validatedParams.sceneMode).flashMode;
   1650     } else {
   1651         validatedParams.flashMode = FLASH_MODE_INVALID;
   1652     }
   1653     if (validatedParams.flashMode == FLASH_MODE_INVALID) {
   1654         validatedParams.flashMode = flashModeStringToEnum(
   1655             newParams.get(CameraParameters::KEY_FLASH_MODE) );
   1656     }
   1657 
   1658     if (validatedParams.flashMode != flashMode) {
   1659         camera_metadata_ro_entry_t flashAvailable =
   1660             staticInfo(ANDROID_FLASH_INFO_AVAILABLE, 1, 1);
   1661         bool isFlashAvailable =
   1662                 flashAvailable.data.u8[0] == ANDROID_FLASH_INFO_AVAILABLE_TRUE;
   1663         if (!isFlashAvailable &&
   1664                 validatedParams.flashMode != Parameters::FLASH_MODE_OFF) {
   1665             ALOGE("%s: Requested flash mode \"%s\" is not supported: "
   1666                     "No flash on device", __FUNCTION__,
   1667                     newParams.get(CameraParameters::KEY_FLASH_MODE));
   1668             return BAD_VALUE;
   1669         } else if (validatedParams.flashMode == Parameters::FLASH_MODE_RED_EYE) {
   1670             camera_metadata_ro_entry_t availableAeModes =
   1671                 staticInfo(ANDROID_CONTROL_AE_AVAILABLE_MODES);
   1672             for (i = 0; i < availableAeModes.count; i++) {
   1673                 if (validatedParams.flashMode == availableAeModes.data.u8[i])
   1674                     break;
   1675             }
   1676             if (i == availableAeModes.count) {
   1677                 ALOGE("%s: Requested flash mode \"%s\" is not supported",
   1678                         __FUNCTION__,
   1679                         newParams.get(CameraParameters::KEY_FLASH_MODE));
   1680                 return BAD_VALUE;
   1681             }
   1682         } else if (validatedParams.flashMode == -1) {
   1683             ALOGE("%s: Requested flash mode \"%s\" is unknown",
   1684                     __FUNCTION__,
   1685                     newParams.get(CameraParameters::KEY_FLASH_MODE));
   1686             return BAD_VALUE;
   1687         }
   1688         // Update in case of override, but only if flash is supported
   1689         if (isFlashAvailable) {
   1690             newParams.set(CameraParameters::KEY_FLASH_MODE,
   1691                     flashModeEnumToString(validatedParams.flashMode));
   1692         }
   1693     }
   1694 
   1695     // WHITE_BALANCE
   1696     if (sceneModeSet) {
   1697         validatedParams.wbMode =
   1698                 fastInfo.sceneModeOverrides.
   1699                         valueFor(validatedParams.sceneMode).wbMode;
   1700     } else {
   1701         validatedParams.wbMode = ANDROID_CONTROL_AWB_MODE_OFF;
   1702     }
   1703     if (validatedParams.wbMode == ANDROID_CONTROL_AWB_MODE_OFF) {
   1704         validatedParams.wbMode = wbModeStringToEnum(
   1705             newParams.get(CameraParameters::KEY_WHITE_BALANCE) );
   1706     }
   1707     if (validatedParams.wbMode != wbMode) {
   1708         camera_metadata_ro_entry_t availableWbModes =
   1709             staticInfo(ANDROID_CONTROL_AWB_AVAILABLE_MODES, 0, 0, false);
   1710         for (i = 0; i < availableWbModes.count; i++) {
   1711             if (validatedParams.wbMode == availableWbModes.data.u8[i]) break;
   1712         }
   1713         if (i == availableWbModes.count) {
   1714             ALOGE("%s: Requested white balance mode %s is not supported",
   1715                     __FUNCTION__,
   1716                     newParams.get(CameraParameters::KEY_WHITE_BALANCE));
   1717             return BAD_VALUE;
   1718         }
   1719         // Update in case of override
   1720         newParams.set(CameraParameters::KEY_WHITE_BALANCE,
   1721                 wbModeEnumToString(validatedParams.wbMode));
   1722     }
   1723 
   1724     // FOCUS_MODE
   1725     if (sceneModeSet) {
   1726         validatedParams.focusMode =
   1727                 fastInfo.sceneModeOverrides.
   1728                         valueFor(validatedParams.sceneMode).focusMode;
   1729     } else {
   1730         validatedParams.focusMode = FOCUS_MODE_INVALID;
   1731     }
   1732     if (validatedParams.focusMode == FOCUS_MODE_INVALID) {
   1733         validatedParams.focusMode = focusModeStringToEnum(
   1734                 newParams.get(CameraParameters::KEY_FOCUS_MODE) );
   1735     }
   1736     if (validatedParams.focusMode != focusMode) {
   1737         validatedParams.currentAfTriggerId = -1;
   1738         if (validatedParams.focusMode != Parameters::FOCUS_MODE_FIXED) {
   1739             camera_metadata_ro_entry_t minFocusDistance =
   1740                 staticInfo(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, 0, 0,
   1741                         false);
   1742             if (minFocusDistance.count && minFocusDistance.data.f[0] == 0) {
   1743                 ALOGE("%s: Requested focus mode \"%s\" is not available: "
   1744                         "fixed focus lens",
   1745                         __FUNCTION__,
   1746                         newParams.get(CameraParameters::KEY_FOCUS_MODE));
   1747                 return BAD_VALUE;
   1748             } else if (validatedParams.focusMode !=
   1749                     Parameters::FOCUS_MODE_INFINITY) {
   1750                 camera_metadata_ro_entry_t availableFocusModes =
   1751                     staticInfo(ANDROID_CONTROL_AF_AVAILABLE_MODES);
   1752                 for (i = 0; i < availableFocusModes.count; i++) {
   1753                     if (validatedParams.focusMode ==
   1754                             availableFocusModes.data.u8[i]) break;
   1755                 }
   1756                 if (i == availableFocusModes.count) {
   1757                     ALOGE("%s: Requested focus mode \"%s\" is not supported",
   1758                             __FUNCTION__,
   1759                             newParams.get(CameraParameters::KEY_FOCUS_MODE));
   1760                     return BAD_VALUE;
   1761                 }
   1762             }
   1763         }
   1764         validatedParams.focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
   1765         // Always reset shadow focus mode to avoid reverting settings
   1766         validatedParams.shadowFocusMode = FOCUS_MODE_INVALID;
   1767         // Update in case of override
   1768         newParams.set(CameraParameters::KEY_FOCUS_MODE,
   1769                 focusModeEnumToString(validatedParams.focusMode));
   1770     } else {
   1771         validatedParams.currentAfTriggerId = currentAfTriggerId;
   1772     }
   1773 
   1774     // FOCUS_AREAS
   1775     res = parseAreas(newParams.get(CameraParameters::KEY_FOCUS_AREAS),
   1776             &validatedParams.focusingAreas);
   1777     size_t maxAfRegions = (size_t)staticInfo(ANDROID_CONTROL_MAX_REGIONS,
   1778               Parameters::NUM_REGION, Parameters::NUM_REGION).
   1779               data.i32[Parameters::REGION_AF];
   1780     if (res == OK) res = validateAreas(validatedParams.focusingAreas,
   1781             maxAfRegions, AREA_KIND_FOCUS);
   1782     if (res != OK) {
   1783         ALOGE("%s: Requested focus areas are malformed: %s",
   1784                 __FUNCTION__, newParams.get(CameraParameters::KEY_FOCUS_AREAS));
   1785         return BAD_VALUE;
   1786     }
   1787 
   1788     // EXPOSURE_COMPENSATION
   1789     validatedParams.exposureCompensation =
   1790         newParams.getInt(CameraParameters::KEY_EXPOSURE_COMPENSATION);
   1791     camera_metadata_ro_entry_t exposureCompensationRange =
   1792         staticInfo(ANDROID_CONTROL_AE_COMPENSATION_RANGE);
   1793     if ((validatedParams.exposureCompensation <
   1794             exposureCompensationRange.data.i32[0]) ||
   1795         (validatedParams.exposureCompensation >
   1796             exposureCompensationRange.data.i32[1])) {
   1797         ALOGE("%s: Requested exposure compensation index is out of bounds: %d",
   1798                 __FUNCTION__, validatedParams.exposureCompensation);
   1799         return BAD_VALUE;
   1800     }
   1801 
   1802     // AUTO_EXPOSURE_LOCK (always supported)
   1803     validatedParams.autoExposureLock = boolFromString(
   1804         newParams.get(CameraParameters::KEY_AUTO_EXPOSURE_LOCK));
   1805 
   1806     // AUTO_WHITEBALANCE_LOCK (always supported)
   1807     validatedParams.autoWhiteBalanceLock = boolFromString(
   1808         newParams.get(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK));
   1809 
   1810     // METERING_AREAS
   1811     size_t maxAeRegions = (size_t)staticInfo(ANDROID_CONTROL_MAX_REGIONS,
   1812             Parameters::NUM_REGION, Parameters::NUM_REGION).
   1813             data.i32[Parameters::REGION_AE];
   1814     res = parseAreas(newParams.get(CameraParameters::KEY_METERING_AREAS),
   1815             &validatedParams.meteringAreas);
   1816     if (res == OK) {
   1817         res = validateAreas(validatedParams.meteringAreas, maxAeRegions,
   1818                             AREA_KIND_METERING);
   1819     }
   1820     if (res != OK) {
   1821         ALOGE("%s: Requested metering areas are malformed: %s",
   1822                 __FUNCTION__,
   1823                 newParams.get(CameraParameters::KEY_METERING_AREAS));
   1824         return BAD_VALUE;
   1825     }
   1826 
   1827     // ZOOM
   1828     validatedParams.zoom = newParams.getInt(CameraParameters::KEY_ZOOM);
   1829     if (validatedParams.zoom < 0
   1830                 || validatedParams.zoom >= (int)NUM_ZOOM_STEPS) {
   1831         ALOGE("%s: Requested zoom level %d is not supported",
   1832                 __FUNCTION__, validatedParams.zoom);
   1833         return BAD_VALUE;
   1834     }
   1835 
   1836     // VIDEO_SIZE
   1837     newParams.getVideoSize(&validatedParams.videoWidth,
   1838             &validatedParams.videoHeight);
   1839     if (validatedParams.videoWidth != videoWidth ||
   1840             validatedParams.videoHeight != videoHeight) {
   1841         if (state == RECORD) {
   1842             ALOGW("%s: Video size cannot be updated (from %d x %d to %d x %d)"
   1843                     " when recording is active! Ignore the size update!",
   1844                     __FUNCTION__, videoWidth, videoHeight, validatedParams.videoWidth,
   1845                     validatedParams.videoHeight);
   1846             validatedParams.videoWidth = videoWidth;
   1847             validatedParams.videoHeight = videoHeight;
   1848             newParams.setVideoSize(videoWidth, videoHeight);
   1849         } else {
   1850             for (i = 0; i < availableVideoSizes.size(); i++) {
   1851                 if ((availableVideoSizes[i].width ==
   1852                         validatedParams.videoWidth) &&
   1853                     (availableVideoSizes[i].height ==
   1854                         validatedParams.videoHeight)) break;
   1855             }
   1856             if (i == availableVideoSizes.size()) {
   1857                 ALOGE("%s: Requested video size %d x %d is not supported",
   1858                         __FUNCTION__, validatedParams.videoWidth,
   1859                         validatedParams.videoHeight);
   1860                 return BAD_VALUE;
   1861             }
   1862         }
   1863     }
   1864 
   1865     // VIDEO_STABILIZATION
   1866     validatedParams.videoStabilization = boolFromString(
   1867         newParams.get(CameraParameters::KEY_VIDEO_STABILIZATION) );
   1868     camera_metadata_ro_entry_t availableVideoStabilizationModes =
   1869         staticInfo(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, 0, 0,
   1870                 false);
   1871     if (validatedParams.videoStabilization &&
   1872             availableVideoStabilizationModes.count == 1) {
   1873         ALOGE("%s: Video stabilization not supported", __FUNCTION__);
   1874     }
   1875 
   1876     /** Update internal parameters */
   1877 
   1878     *this = validatedParams;
   1879     updateOverriddenJpegSize();
   1880 
   1881     /** Update external parameters calculated from the internal ones */
   1882 
   1883     // HORIZONTAL/VERTICAL FIELD OF VIEW
   1884     float horizFov, vertFov;
   1885     res = calculatePictureFovs(&horizFov, &vertFov);
   1886     if (res != OK) {
   1887         ALOGE("%s: Can't calculate FOVs", __FUNCTION__);
   1888         // continue so parameters are at least consistent
   1889     }
   1890     newParams.setFloat(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE,
   1891             horizFov);
   1892     newParams.setFloat(CameraParameters::KEY_VERTICAL_VIEW_ANGLE,
   1893             vertFov);
   1894     ALOGV("Current still picture FOV: %f x %f deg", horizFov, vertFov);
   1895 
   1896     // Need to flatten again in case of overrides
   1897     paramsFlattened = newParams.flatten();
   1898     params = newParams;
   1899 
   1900     return OK;
   1901 }
   1902 
   1903 status_t Parameters::updateRequest(CameraMetadata *request) const {
   1904     ATRACE_CALL();
   1905     status_t res;
   1906 
   1907     /**
   1908      * Mixin default important security values
   1909      * - android.led.transmit = defaulted ON
   1910      */
   1911     camera_metadata_ro_entry_t entry = staticInfo(ANDROID_LED_AVAILABLE_LEDS,
   1912                                                   /*minimumCount*/0,
   1913                                                   /*maximumCount*/0,
   1914                                                   /*required*/false);
   1915     for(size_t i = 0; i < entry.count; ++i) {
   1916         uint8_t led = entry.data.u8[i];
   1917 
   1918         switch(led) {
   1919             // Transmit LED is unconditionally on when using
   1920             // the android.hardware.Camera API
   1921             case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
   1922                 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
   1923                 res = request->update(ANDROID_LED_TRANSMIT,
   1924                                       &transmitDefault, 1);
   1925                 if (res != OK) return res;
   1926                 break;
   1927             }
   1928         }
   1929     }
   1930 
   1931     /**
   1932      * Construct metadata from parameters
   1933      */
   1934 
   1935     uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
   1936     res = request->update(ANDROID_REQUEST_METADATA_MODE,
   1937             &metadataMode, 1);
   1938     if (res != OK) return res;
   1939 
   1940     camera_metadata_entry_t intent =
   1941             request->find(ANDROID_CONTROL_CAPTURE_INTENT);
   1942 
   1943     if (intent.count == 0) return BAD_VALUE;
   1944 
   1945     if (intent.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE) {
   1946         res = request->update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
   1947                 fastInfo.bestStillCaptureFpsRange, 2);
   1948     } else {
   1949         res = request->update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
   1950                 previewFpsRange, 2);
   1951     }
   1952     if (res != OK) return res;
   1953 
   1954     uint8_t reqWbLock = autoWhiteBalanceLock ?
   1955             ANDROID_CONTROL_AWB_LOCK_ON : ANDROID_CONTROL_AWB_LOCK_OFF;
   1956     res = request->update(ANDROID_CONTROL_AWB_LOCK,
   1957             &reqWbLock, 1);
   1958 
   1959     res = request->update(ANDROID_CONTROL_EFFECT_MODE,
   1960             &effectMode, 1);
   1961     if (res != OK) return res;
   1962     res = request->update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
   1963             &antibandingMode, 1);
   1964     if (res != OK) return res;
   1965 
   1966     // android.hardware.Camera requires that when face detect is enabled, the
   1967     // camera is in a face-priority mode. HAL3.x splits this into separate parts
   1968     // (face detection statistics and face priority scene mode). Map from other
   1969     // to the other.
   1970     bool sceneModeActive =
   1971             sceneMode != (uint8_t)ANDROID_CONTROL_SCENE_MODE_DISABLED;
   1972     uint8_t reqControlMode = ANDROID_CONTROL_MODE_AUTO;
   1973     if (enableFaceDetect || sceneModeActive) {
   1974         reqControlMode = ANDROID_CONTROL_MODE_USE_SCENE_MODE;
   1975     }
   1976     res = request->update(ANDROID_CONTROL_MODE,
   1977             &reqControlMode, 1);
   1978     if (res != OK) return res;
   1979 
   1980     uint8_t reqSceneMode =
   1981             sceneModeActive ? sceneMode :
   1982             enableFaceDetect ? (uint8_t)ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY :
   1983             (uint8_t)ANDROID_CONTROL_SCENE_MODE_DISABLED;
   1984     res = request->update(ANDROID_CONTROL_SCENE_MODE,
   1985             &reqSceneMode, 1);
   1986     if (res != OK) return res;
   1987 
   1988     uint8_t reqFlashMode = ANDROID_FLASH_MODE_OFF;
   1989     uint8_t reqAeMode = ANDROID_CONTROL_AE_MODE_OFF;
   1990     switch (flashMode) {
   1991         case Parameters::FLASH_MODE_OFF:
   1992             reqAeMode = ANDROID_CONTROL_AE_MODE_ON; break;
   1993         case Parameters::FLASH_MODE_AUTO:
   1994             reqAeMode = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH; break;
   1995         case Parameters::FLASH_MODE_ON:
   1996             reqAeMode = ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH; break;
   1997         case Parameters::FLASH_MODE_TORCH:
   1998             reqAeMode = ANDROID_CONTROL_AE_MODE_ON;
   1999             reqFlashMode = ANDROID_FLASH_MODE_TORCH;
   2000             break;
   2001         case Parameters::FLASH_MODE_RED_EYE:
   2002             reqAeMode = ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE; break;
   2003         default:
   2004             ALOGE("%s: Camera %d: Unknown flash mode %d", __FUNCTION__,
   2005                     cameraId, flashMode);
   2006                 return BAD_VALUE;
   2007     }
   2008     res = request->update(ANDROID_FLASH_MODE,
   2009             &reqFlashMode, 1);
   2010     if (res != OK) return res;
   2011     res = request->update(ANDROID_CONTROL_AE_MODE,
   2012             &reqAeMode, 1);
   2013     if (res != OK) return res;
   2014 
   2015     uint8_t reqAeLock = autoExposureLock ?
   2016             ANDROID_CONTROL_AE_LOCK_ON : ANDROID_CONTROL_AE_LOCK_OFF;
   2017     res = request->update(ANDROID_CONTROL_AE_LOCK,
   2018             &reqAeLock, 1);
   2019     if (res != OK) return res;
   2020 
   2021     res = request->update(ANDROID_CONTROL_AWB_MODE,
   2022             &wbMode, 1);
   2023     if (res != OK) return res;
   2024 
   2025     float reqFocusDistance = 0; // infinity focus in diopters
   2026     uint8_t reqFocusMode = ANDROID_CONTROL_AF_MODE_OFF;
   2027     switch (focusMode) {
   2028         case Parameters::FOCUS_MODE_AUTO:
   2029         case Parameters::FOCUS_MODE_MACRO:
   2030         case Parameters::FOCUS_MODE_CONTINUOUS_VIDEO:
   2031         case Parameters::FOCUS_MODE_CONTINUOUS_PICTURE:
   2032         case Parameters::FOCUS_MODE_EDOF:
   2033             reqFocusMode = focusMode;
   2034             break;
   2035         case Parameters::FOCUS_MODE_INFINITY:
   2036         case Parameters::FOCUS_MODE_FIXED:
   2037             reqFocusMode = ANDROID_CONTROL_AF_MODE_OFF;
   2038             break;
   2039         default:
   2040                 ALOGE("%s: Camera %d: Unknown focus mode %d", __FUNCTION__,
   2041                         cameraId, focusMode);
   2042                 return BAD_VALUE;
   2043     }
   2044     res = request->update(ANDROID_LENS_FOCUS_DISTANCE,
   2045             &reqFocusDistance, 1);
   2046     if (res != OK) return res;
   2047     res = request->update(ANDROID_CONTROL_AF_MODE,
   2048             &reqFocusMode, 1);
   2049     if (res != OK) return res;
   2050 
   2051     size_t reqFocusingAreasSize = focusingAreas.size() * 5;
   2052     int32_t *reqFocusingAreas = new int32_t[reqFocusingAreasSize];
   2053     for (size_t i = 0, j = 0; i < reqFocusingAreasSize; i += 5, j++) {
   2054         if (focusingAreas[j].weight != 0) {
   2055             reqFocusingAreas[i + 0] =
   2056                     normalizedXToArray(focusingAreas[j].left);
   2057             reqFocusingAreas[i + 1] =
   2058                     normalizedYToArray(focusingAreas[j].top);
   2059             reqFocusingAreas[i + 2] =
   2060                     normalizedXToArray(focusingAreas[j].right);
   2061             reqFocusingAreas[i + 3] =
   2062                     normalizedYToArray(focusingAreas[j].bottom);
   2063         } else {
   2064             reqFocusingAreas[i + 0] = 0;
   2065             reqFocusingAreas[i + 1] = 0;
   2066             reqFocusingAreas[i + 2] = 0;
   2067             reqFocusingAreas[i + 3] = 0;
   2068         }
   2069         reqFocusingAreas[i + 4] = focusingAreas[j].weight;
   2070     }
   2071     res = request->update(ANDROID_CONTROL_AF_REGIONS,
   2072             reqFocusingAreas, reqFocusingAreasSize);
   2073     if (res != OK) return res;
   2074     delete[] reqFocusingAreas;
   2075 
   2076     res = request->update(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
   2077             &exposureCompensation, 1);
   2078     if (res != OK) return res;
   2079 
   2080     size_t reqMeteringAreasSize = meteringAreas.size() * 5;
   2081     int32_t *reqMeteringAreas = new int32_t[reqMeteringAreasSize];
   2082     for (size_t i = 0, j = 0; i < reqMeteringAreasSize; i += 5, j++) {
   2083         if (meteringAreas[j].weight != 0) {
   2084             reqMeteringAreas[i + 0] =
   2085                 normalizedXToArray(meteringAreas[j].left);
   2086             reqMeteringAreas[i + 1] =
   2087                 normalizedYToArray(meteringAreas[j].top);
   2088             reqMeteringAreas[i + 2] =
   2089                 normalizedXToArray(meteringAreas[j].right);
   2090             reqMeteringAreas[i + 3] =
   2091                 normalizedYToArray(meteringAreas[j].bottom);
   2092         } else {
   2093             reqMeteringAreas[i + 0] = 0;
   2094             reqMeteringAreas[i + 1] = 0;
   2095             reqMeteringAreas[i + 2] = 0;
   2096             reqMeteringAreas[i + 3] = 0;
   2097         }
   2098         reqMeteringAreas[i + 4] = meteringAreas[j].weight;
   2099     }
   2100     res = request->update(ANDROID_CONTROL_AE_REGIONS,
   2101             reqMeteringAreas, reqMeteringAreasSize);
   2102     if (res != OK) return res;
   2103 
   2104     // Set awb regions to be the same as the metering regions if allowed
   2105     size_t maxAwbRegions = (size_t)staticInfo(ANDROID_CONTROL_MAX_REGIONS,
   2106             Parameters::NUM_REGION, Parameters::NUM_REGION).
   2107             data.i32[Parameters::REGION_AWB];
   2108     if (maxAwbRegions > 0) {
   2109         if (maxAwbRegions >= meteringAreas.size()) {
   2110             res = request->update(ANDROID_CONTROL_AWB_REGIONS,
   2111                     reqMeteringAreas, reqMeteringAreasSize);
   2112         } else {
   2113             // Ensure the awb regions are zeroed if the region count is too high.
   2114             int32_t zeroedAwbAreas[5] = {0, 0, 0, 0, 0};
   2115             res = request->update(ANDROID_CONTROL_AWB_REGIONS,
   2116                     zeroedAwbAreas, sizeof(zeroedAwbAreas)/sizeof(int32_t));
   2117         }
   2118         if (res != OK) return res;
   2119     }
   2120 
   2121     delete[] reqMeteringAreas;
   2122 
   2123     CropRegion crop = calculateCropRegion(/*previewOnly*/ false);
   2124     int32_t reqCropRegion[4] = {
   2125         static_cast<int32_t>(crop.left),
   2126         static_cast<int32_t>(crop.top),
   2127         static_cast<int32_t>(crop.width),
   2128         static_cast<int32_t>(crop.height)
   2129     };
   2130     res = request->update(ANDROID_SCALER_CROP_REGION,
   2131             reqCropRegion, 4);
   2132     if (res != OK) return res;
   2133 
   2134     uint8_t reqVstabMode = videoStabilization ?
   2135             ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_ON :
   2136             ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
   2137     res = request->update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
   2138             &reqVstabMode, 1);
   2139     if (res != OK) return res;
   2140 
   2141     uint8_t reqFaceDetectMode = enableFaceDetect ?
   2142             fastInfo.bestFaceDetectMode :
   2143             (uint8_t)ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
   2144     res = request->update(ANDROID_STATISTICS_FACE_DETECT_MODE,
   2145             &reqFaceDetectMode, 1);
   2146     if (res != OK) return res;
   2147 
   2148     return OK;
   2149 }
   2150 
   2151 status_t Parameters::updateRequestJpeg(CameraMetadata *request) const {
   2152     status_t res;
   2153 
   2154     res = request->update(ANDROID_JPEG_THUMBNAIL_SIZE,
   2155             jpegThumbSize, 2);
   2156     if (res != OK) return res;
   2157     res = request->update(ANDROID_JPEG_THUMBNAIL_QUALITY,
   2158             &jpegThumbQuality, 1);
   2159     if (res != OK) return res;
   2160     res = request->update(ANDROID_JPEG_QUALITY,
   2161             &jpegQuality, 1);
   2162     if (res != OK) return res;
   2163     res = request->update(
   2164             ANDROID_JPEG_ORIENTATION,
   2165             &jpegRotation, 1);
   2166     if (res != OK) return res;
   2167 
   2168     if (gpsEnabled) {
   2169         res = request->update(
   2170                 ANDROID_JPEG_GPS_COORDINATES,
   2171                 gpsCoordinates, 3);
   2172         if (res != OK) return res;
   2173         res = request->update(
   2174                 ANDROID_JPEG_GPS_TIMESTAMP,
   2175                 &gpsTimestamp, 1);
   2176         if (res != OK) return res;
   2177         res = request->update(
   2178                 ANDROID_JPEG_GPS_PROCESSING_METHOD,
   2179                 gpsProcessingMethod);
   2180         if (res != OK) return res;
   2181     } else {
   2182         res = request->erase(ANDROID_JPEG_GPS_COORDINATES);
   2183         if (res != OK) return res;
   2184         res = request->erase(ANDROID_JPEG_GPS_TIMESTAMP);
   2185         if (res != OK) return res;
   2186         res = request->erase(ANDROID_JPEG_GPS_PROCESSING_METHOD);
   2187         if (res != OK) return res;
   2188     }
   2189     return OK;
   2190 }
   2191 
   2192 status_t Parameters::overrideJpegSizeByVideoSize() {
   2193     if (pictureSizeOverriden) {
   2194         ALOGV("Picture size has been overridden. Skip overriding");
   2195         return OK;
   2196     }
   2197 
   2198     pictureSizeOverriden = true;
   2199     pictureWidthLastSet = pictureWidth;
   2200     pictureHeightLastSet = pictureHeight;
   2201     pictureWidth = videoWidth;
   2202     pictureHeight = videoHeight;
   2203     // This change of picture size is invisible to app layer.
   2204     // Do not update app visible params
   2205     return OK;
   2206 }
   2207 
   2208 status_t Parameters::updateOverriddenJpegSize() {
   2209     if (!pictureSizeOverriden) {
   2210         ALOGV("Picture size has not been overridden. Skip checking");
   2211         return OK;
   2212     }
   2213 
   2214     pictureWidthLastSet = pictureWidth;
   2215     pictureHeightLastSet = pictureHeight;
   2216 
   2217     if (pictureWidth <= videoWidth && pictureHeight <= videoHeight) {
   2218         // Picture size is now smaller than video size. No need to override anymore
   2219         return recoverOverriddenJpegSize();
   2220     }
   2221 
   2222     pictureWidth = videoWidth;
   2223     pictureHeight = videoHeight;
   2224 
   2225     return OK;
   2226 }
   2227 
   2228 status_t Parameters::recoverOverriddenJpegSize() {
   2229     if (!pictureSizeOverriden) {
   2230         ALOGV("Picture size has not been overridden. Skip recovering");
   2231         return OK;
   2232     }
   2233     pictureSizeOverriden = false;
   2234     pictureWidth = pictureWidthLastSet;
   2235     pictureHeight = pictureHeightLastSet;
   2236     return OK;
   2237 }
   2238 
   2239 bool Parameters::isJpegSizeOverridden() {
   2240     return pictureSizeOverriden;
   2241 }
   2242 
   2243 bool Parameters::useZeroShutterLag() const {
   2244     // If ZSL mode is disabled, don't use it
   2245     if (!allowZslMode) return false;
   2246     // If recording hint is enabled, don't do ZSL
   2247     if (recordingHint) return false;
   2248     // If still capture size is no bigger than preview or video size,
   2249     // don't do ZSL
   2250     if (pictureWidth <= previewWidth || pictureHeight <= previewHeight ||
   2251             pictureWidth <= videoWidth || pictureHeight <= videoHeight) {
   2252         return false;
   2253     }
   2254     // If still capture size is less than quarter of max, don't do ZSL
   2255     if ((pictureWidth * pictureHeight) <
   2256             (fastInfo.maxJpegSize.width * fastInfo.maxJpegSize.height / 4) ) {
   2257         return false;
   2258     }
   2259     return true;
   2260 }
   2261 
   2262 const char* Parameters::getStateName(State state) {
   2263 #define CASE_ENUM_TO_CHAR(x) case x: return(#x); break;
   2264     switch(state) {
   2265         CASE_ENUM_TO_CHAR(DISCONNECTED)
   2266         CASE_ENUM_TO_CHAR(STOPPED)
   2267         CASE_ENUM_TO_CHAR(WAITING_FOR_PREVIEW_WINDOW)
   2268         CASE_ENUM_TO_CHAR(PREVIEW)
   2269         CASE_ENUM_TO_CHAR(RECORD)
   2270         CASE_ENUM_TO_CHAR(STILL_CAPTURE)
   2271         CASE_ENUM_TO_CHAR(VIDEO_SNAPSHOT)
   2272         default:
   2273             return "Unknown state!";
   2274             break;
   2275     }
   2276 #undef CASE_ENUM_TO_CHAR
   2277 }
   2278 
   2279 int Parameters::formatStringToEnum(const char *format) {
   2280     return CameraParameters::previewFormatToEnum(format);
   2281 }
   2282 
   2283 const char* Parameters::formatEnumToString(int format) {
   2284     const char *fmt;
   2285     switch(format) {
   2286         case HAL_PIXEL_FORMAT_YCbCr_422_SP: // NV16
   2287             fmt = CameraParameters::PIXEL_FORMAT_YUV422SP;
   2288             break;
   2289         case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
   2290             fmt = CameraParameters::PIXEL_FORMAT_YUV420SP;
   2291             break;
   2292         case HAL_PIXEL_FORMAT_YCbCr_422_I: // YUY2
   2293             fmt = CameraParameters::PIXEL_FORMAT_YUV422I;
   2294             break;
   2295         case HAL_PIXEL_FORMAT_YV12:        // YV12
   2296             fmt = CameraParameters::PIXEL_FORMAT_YUV420P;
   2297             break;
   2298         case HAL_PIXEL_FORMAT_RGB_565:     // RGB565
   2299             fmt = CameraParameters::PIXEL_FORMAT_RGB565;
   2300             break;
   2301         case HAL_PIXEL_FORMAT_RGBA_8888:   // RGBA8888
   2302             fmt = CameraParameters::PIXEL_FORMAT_RGBA8888;
   2303             break;
   2304         case HAL_PIXEL_FORMAT_RAW16:
   2305             ALOGW("Raw sensor preview format requested.");
   2306             fmt = CameraParameters::PIXEL_FORMAT_BAYER_RGGB;
   2307             break;
   2308         default:
   2309             ALOGE("%s: Unknown preview format: %x",
   2310                     __FUNCTION__,  format);
   2311             fmt = NULL;
   2312             break;
   2313     }
   2314     return fmt;
   2315 }
   2316 
   2317 int Parameters::wbModeStringToEnum(const char *wbMode) {
   2318     return
   2319         !wbMode ?
   2320             ANDROID_CONTROL_AWB_MODE_AUTO :
   2321         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_AUTO) ?
   2322             ANDROID_CONTROL_AWB_MODE_AUTO :
   2323         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_INCANDESCENT) ?
   2324             ANDROID_CONTROL_AWB_MODE_INCANDESCENT :
   2325         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_FLUORESCENT) ?
   2326             ANDROID_CONTROL_AWB_MODE_FLUORESCENT :
   2327         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT) ?
   2328             ANDROID_CONTROL_AWB_MODE_WARM_FLUORESCENT :
   2329         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_DAYLIGHT) ?
   2330             ANDROID_CONTROL_AWB_MODE_DAYLIGHT :
   2331         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT) ?
   2332             ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT :
   2333         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_TWILIGHT) ?
   2334             ANDROID_CONTROL_AWB_MODE_TWILIGHT :
   2335         !strcmp(wbMode, CameraParameters::WHITE_BALANCE_SHADE) ?
   2336             ANDROID_CONTROL_AWB_MODE_SHADE :
   2337         -1;
   2338 }
   2339 
   2340 const char* Parameters::wbModeEnumToString(uint8_t wbMode) {
   2341     switch (wbMode) {
   2342         case ANDROID_CONTROL_AWB_MODE_AUTO:
   2343             return CameraParameters::WHITE_BALANCE_AUTO;
   2344         case ANDROID_CONTROL_AWB_MODE_INCANDESCENT:
   2345             return CameraParameters::WHITE_BALANCE_INCANDESCENT;
   2346         case ANDROID_CONTROL_AWB_MODE_FLUORESCENT:
   2347             return CameraParameters::WHITE_BALANCE_FLUORESCENT;
   2348         case ANDROID_CONTROL_AWB_MODE_WARM_FLUORESCENT:
   2349             return CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT;
   2350         case ANDROID_CONTROL_AWB_MODE_DAYLIGHT:
   2351             return CameraParameters::WHITE_BALANCE_DAYLIGHT;
   2352         case ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT:
   2353             return CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT;
   2354         case ANDROID_CONTROL_AWB_MODE_TWILIGHT:
   2355             return CameraParameters::WHITE_BALANCE_TWILIGHT;
   2356         case ANDROID_CONTROL_AWB_MODE_SHADE:
   2357             return CameraParameters::WHITE_BALANCE_SHADE;
   2358         default:
   2359             ALOGE("%s: Unknown AWB mode enum: %d",
   2360                     __FUNCTION__, wbMode);
   2361             return "unknown";
   2362     }
   2363 }
   2364 
   2365 int Parameters::effectModeStringToEnum(const char *effectMode) {
   2366     return
   2367         !effectMode ?
   2368             ANDROID_CONTROL_EFFECT_MODE_OFF :
   2369         !strcmp(effectMode, CameraParameters::EFFECT_NONE) ?
   2370             ANDROID_CONTROL_EFFECT_MODE_OFF :
   2371         !strcmp(effectMode, CameraParameters::EFFECT_MONO) ?
   2372             ANDROID_CONTROL_EFFECT_MODE_MONO :
   2373         !strcmp(effectMode, CameraParameters::EFFECT_NEGATIVE) ?
   2374             ANDROID_CONTROL_EFFECT_MODE_NEGATIVE :
   2375         !strcmp(effectMode, CameraParameters::EFFECT_SOLARIZE) ?
   2376             ANDROID_CONTROL_EFFECT_MODE_SOLARIZE :
   2377         !strcmp(effectMode, CameraParameters::EFFECT_SEPIA) ?
   2378             ANDROID_CONTROL_EFFECT_MODE_SEPIA :
   2379         !strcmp(effectMode, CameraParameters::EFFECT_POSTERIZE) ?
   2380             ANDROID_CONTROL_EFFECT_MODE_POSTERIZE :
   2381         !strcmp(effectMode, CameraParameters::EFFECT_WHITEBOARD) ?
   2382             ANDROID_CONTROL_EFFECT_MODE_WHITEBOARD :
   2383         !strcmp(effectMode, CameraParameters::EFFECT_BLACKBOARD) ?
   2384             ANDROID_CONTROL_EFFECT_MODE_BLACKBOARD :
   2385         !strcmp(effectMode, CameraParameters::EFFECT_AQUA) ?
   2386             ANDROID_CONTROL_EFFECT_MODE_AQUA :
   2387         -1;
   2388 }
   2389 
   2390 int Parameters::abModeStringToEnum(const char *abMode) {
   2391     return
   2392         !abMode ?
   2393             ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO :
   2394         !strcmp(abMode, CameraParameters::ANTIBANDING_AUTO) ?
   2395             ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO :
   2396         !strcmp(abMode, CameraParameters::ANTIBANDING_OFF) ?
   2397             ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF :
   2398         !strcmp(abMode, CameraParameters::ANTIBANDING_50HZ) ?
   2399             ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ :
   2400         !strcmp(abMode, CameraParameters::ANTIBANDING_60HZ) ?
   2401             ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ :
   2402         -1;
   2403 }
   2404 
   2405 int Parameters::sceneModeStringToEnum(const char *sceneMode) {
   2406     return
   2407         !sceneMode ?
   2408             ANDROID_CONTROL_SCENE_MODE_DISABLED :
   2409         !strcmp(sceneMode, CameraParameters::SCENE_MODE_AUTO) ?
   2410             ANDROID_CONTROL_SCENE_MODE_DISABLED :
   2411         !strcmp(sceneMode, CameraParameters::SCENE_MODE_ACTION) ?
   2412             ANDROID_CONTROL_SCENE_MODE_ACTION :
   2413         !strcmp(sceneMode, CameraParameters::SCENE_MODE_PORTRAIT) ?
   2414             ANDROID_CONTROL_SCENE_MODE_PORTRAIT :
   2415         !strcmp(sceneMode, CameraParameters::SCENE_MODE_LANDSCAPE) ?
   2416             ANDROID_CONTROL_SCENE_MODE_LANDSCAPE :
   2417         !strcmp(sceneMode, CameraParameters::SCENE_MODE_NIGHT) ?
   2418             ANDROID_CONTROL_SCENE_MODE_NIGHT :
   2419         !strcmp(sceneMode, CameraParameters::SCENE_MODE_NIGHT_PORTRAIT) ?
   2420             ANDROID_CONTROL_SCENE_MODE_NIGHT_PORTRAIT :
   2421         !strcmp(sceneMode, CameraParameters::SCENE_MODE_THEATRE) ?
   2422             ANDROID_CONTROL_SCENE_MODE_THEATRE :
   2423         !strcmp(sceneMode, CameraParameters::SCENE_MODE_BEACH) ?
   2424             ANDROID_CONTROL_SCENE_MODE_BEACH :
   2425         !strcmp(sceneMode, CameraParameters::SCENE_MODE_SNOW) ?
   2426             ANDROID_CONTROL_SCENE_MODE_SNOW :
   2427         !strcmp(sceneMode, CameraParameters::SCENE_MODE_SUNSET) ?
   2428             ANDROID_CONTROL_SCENE_MODE_SUNSET :
   2429         !strcmp(sceneMode, CameraParameters::SCENE_MODE_STEADYPHOTO) ?
   2430             ANDROID_CONTROL_SCENE_MODE_STEADYPHOTO :
   2431         !strcmp(sceneMode, CameraParameters::SCENE_MODE_FIREWORKS) ?
   2432             ANDROID_CONTROL_SCENE_MODE_FIREWORKS :
   2433         !strcmp(sceneMode, CameraParameters::SCENE_MODE_SPORTS) ?
   2434             ANDROID_CONTROL_SCENE_MODE_SPORTS :
   2435         !strcmp(sceneMode, CameraParameters::SCENE_MODE_PARTY) ?
   2436             ANDROID_CONTROL_SCENE_MODE_PARTY :
   2437         !strcmp(sceneMode, CameraParameters::SCENE_MODE_CANDLELIGHT) ?
   2438             ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT :
   2439         !strcmp(sceneMode, CameraParameters::SCENE_MODE_BARCODE) ?
   2440             ANDROID_CONTROL_SCENE_MODE_BARCODE:
   2441         !strcmp(sceneMode, CameraParameters::SCENE_MODE_HDR) ?
   2442             ANDROID_CONTROL_SCENE_MODE_HDR:
   2443         -1;
   2444 }
   2445 
   2446 Parameters::Parameters::flashMode_t Parameters::flashModeStringToEnum(
   2447         const char *flashMode) {
   2448     return
   2449         !flashMode ?
   2450             Parameters::FLASH_MODE_OFF :
   2451         !strcmp(flashMode, CameraParameters::FLASH_MODE_OFF) ?
   2452             Parameters::FLASH_MODE_OFF :
   2453         !strcmp(flashMode, CameraParameters::FLASH_MODE_AUTO) ?
   2454             Parameters::FLASH_MODE_AUTO :
   2455         !strcmp(flashMode, CameraParameters::FLASH_MODE_ON) ?
   2456             Parameters::FLASH_MODE_ON :
   2457         !strcmp(flashMode, CameraParameters::FLASH_MODE_RED_EYE) ?
   2458             Parameters::FLASH_MODE_RED_EYE :
   2459         !strcmp(flashMode, CameraParameters::FLASH_MODE_TORCH) ?
   2460             Parameters::FLASH_MODE_TORCH :
   2461         Parameters::FLASH_MODE_INVALID;
   2462 }
   2463 
   2464 const char *Parameters::flashModeEnumToString(flashMode_t flashMode) {
   2465     switch (flashMode) {
   2466         case FLASH_MODE_OFF:
   2467             return CameraParameters::FLASH_MODE_OFF;
   2468         case FLASH_MODE_AUTO:
   2469             return CameraParameters::FLASH_MODE_AUTO;
   2470         case FLASH_MODE_ON:
   2471             return CameraParameters::FLASH_MODE_ON;
   2472         case FLASH_MODE_RED_EYE:
   2473             return CameraParameters::FLASH_MODE_RED_EYE;
   2474         case FLASH_MODE_TORCH:
   2475             return CameraParameters::FLASH_MODE_TORCH;
   2476         default:
   2477             ALOGE("%s: Unknown flash mode enum %d",
   2478                     __FUNCTION__, flashMode);
   2479             return "unknown";
   2480     }
   2481 }
   2482 
   2483 Parameters::Parameters::focusMode_t Parameters::focusModeStringToEnum(
   2484         const char *focusMode) {
   2485     return
   2486         !focusMode ?
   2487             Parameters::FOCUS_MODE_INVALID :
   2488         !strcmp(focusMode, CameraParameters::FOCUS_MODE_AUTO) ?
   2489             Parameters::FOCUS_MODE_AUTO :
   2490         !strcmp(focusMode, CameraParameters::FOCUS_MODE_INFINITY) ?
   2491             Parameters::FOCUS_MODE_INFINITY :
   2492         !strcmp(focusMode, CameraParameters::FOCUS_MODE_MACRO) ?
   2493             Parameters::FOCUS_MODE_MACRO :
   2494         !strcmp(focusMode, CameraParameters::FOCUS_MODE_FIXED) ?
   2495             Parameters::FOCUS_MODE_FIXED :
   2496         !strcmp(focusMode, CameraParameters::FOCUS_MODE_EDOF) ?
   2497             Parameters::FOCUS_MODE_EDOF :
   2498         !strcmp(focusMode, CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO) ?
   2499             Parameters::FOCUS_MODE_CONTINUOUS_VIDEO :
   2500         !strcmp(focusMode, CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE) ?
   2501             Parameters::FOCUS_MODE_CONTINUOUS_PICTURE :
   2502         Parameters::FOCUS_MODE_INVALID;
   2503 }
   2504 
   2505 const char *Parameters::focusModeEnumToString(focusMode_t focusMode) {
   2506     switch (focusMode) {
   2507         case FOCUS_MODE_AUTO:
   2508             return CameraParameters::FOCUS_MODE_AUTO;
   2509         case FOCUS_MODE_MACRO:
   2510             return CameraParameters::FOCUS_MODE_MACRO;
   2511         case FOCUS_MODE_CONTINUOUS_VIDEO:
   2512             return CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO;
   2513         case FOCUS_MODE_CONTINUOUS_PICTURE:
   2514             return CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE;
   2515         case FOCUS_MODE_EDOF:
   2516             return CameraParameters::FOCUS_MODE_EDOF;
   2517         case FOCUS_MODE_INFINITY:
   2518             return CameraParameters::FOCUS_MODE_INFINITY;
   2519         case FOCUS_MODE_FIXED:
   2520             return CameraParameters::FOCUS_MODE_FIXED;
   2521         default:
   2522             ALOGE("%s: Unknown focus mode enum: %d",
   2523                     __FUNCTION__, focusMode);
   2524             return "unknown";
   2525     }
   2526 }
   2527 
   2528 status_t Parameters::parseAreas(const char *areasCStr,
   2529         Vector<Parameters::Area> *areas) {
   2530     static const size_t NUM_FIELDS = 5;
   2531     areas->clear();
   2532     if (areasCStr == NULL) {
   2533         // If no key exists, use default (0,0,0,0,0)
   2534         areas->push();
   2535         return OK;
   2536     }
   2537     String8 areasStr(areasCStr);
   2538     ssize_t areaStart = areasStr.find("(", 0) + 1;
   2539     while (areaStart != 0) {
   2540         const char* area = areasStr.string() + areaStart;
   2541         char *numEnd;
   2542         int vals[NUM_FIELDS];
   2543         for (size_t i = 0; i < NUM_FIELDS; i++) {
   2544             errno = 0;
   2545             vals[i] = strtol(area, &numEnd, 10);
   2546             if (errno || numEnd == area) return BAD_VALUE;
   2547             area = numEnd + 1;
   2548         }
   2549         areas->push(Parameters::Area(
   2550             vals[0], vals[1], vals[2], vals[3], vals[4]) );
   2551         areaStart = areasStr.find("(", areaStart) + 1;
   2552     }
   2553     return OK;
   2554 }
   2555 
   2556 status_t Parameters::validateAreas(const Vector<Parameters::Area> &areas,
   2557                                       size_t maxRegions,
   2558                                       AreaKind areaKind) const {
   2559     // Definition of valid area can be found in
   2560     // include/camera/CameraParameters.h
   2561     if (areas.size() == 0) return BAD_VALUE;
   2562     if (areas.size() == 1) {
   2563         if (areas[0].left == 0 &&
   2564                 areas[0].top == 0 &&
   2565                 areas[0].right == 0 &&
   2566                 areas[0].bottom == 0 &&
   2567                 areas[0].weight == 0) {
   2568             // Single (0,0,0,0,0) entry is always valid (== driver decides)
   2569             return OK;
   2570         }
   2571     }
   2572 
   2573     // fixed focus can only set (0,0,0,0,0) focus area
   2574     if (areaKind == AREA_KIND_FOCUS && focusMode == FOCUS_MODE_FIXED) {
   2575         return BAD_VALUE;
   2576     }
   2577 
   2578     if (areas.size() > maxRegions) {
   2579         ALOGE("%s: Too many areas requested: %zu",
   2580                 __FUNCTION__, areas.size());
   2581         return BAD_VALUE;
   2582     }
   2583 
   2584     for (Vector<Parameters::Area>::const_iterator a = areas.begin();
   2585          a != areas.end(); a++) {
   2586         if (a->weight < 1 || a->weight > 1000) return BAD_VALUE;
   2587         if (a->left < -1000 || a->left > 1000) return BAD_VALUE;
   2588         if (a->top < -1000 || a->top > 1000) return BAD_VALUE;
   2589         if (a->right < -1000 || a->right > 1000) return BAD_VALUE;
   2590         if (a->bottom < -1000 || a->bottom > 1000) return BAD_VALUE;
   2591         if (a->left >= a->right) return BAD_VALUE;
   2592         if (a->top >= a->bottom) return BAD_VALUE;
   2593     }
   2594     return OK;
   2595 }
   2596 
   2597 bool Parameters::boolFromString(const char *boolStr) {
   2598     return !boolStr ? false :
   2599         !strcmp(boolStr, CameraParameters::TRUE) ? true :
   2600         false;
   2601 }
   2602 
   2603 int Parameters::degToTransform(int degrees, bool mirror) {
   2604     if (!mirror) {
   2605         if (degrees == 0) return 0;
   2606         else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
   2607         else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
   2608         else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
   2609     } else {  // Do mirror (horizontal flip)
   2610         if (degrees == 0) {           // FLIP_H and ROT_0
   2611             return HAL_TRANSFORM_FLIP_H;
   2612         } else if (degrees == 90) {   // FLIP_H and ROT_90
   2613             return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
   2614         } else if (degrees == 180) {  // FLIP_H and ROT_180
   2615             return HAL_TRANSFORM_FLIP_V;
   2616         } else if (degrees == 270) {  // FLIP_H and ROT_270
   2617             return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
   2618         }
   2619     }
   2620     ALOGE("%s: Bad input: %d", __FUNCTION__, degrees);
   2621     return -1;
   2622 }
   2623 
   2624 int Parameters::cropXToArray(int x) const {
   2625     ALOG_ASSERT(x >= 0, "Crop-relative X coordinate = '%d' is out of bounds"
   2626                          "(lower = 0)", x);
   2627 
   2628     CropRegion previewCrop = calculateCropRegion(/*previewOnly*/ true);
   2629     ALOG_ASSERT(x < previewCrop.width, "Crop-relative X coordinate = '%d' "
   2630                     "is out of bounds (upper = %f)", x, previewCrop.width);
   2631 
   2632     int ret = x + previewCrop.left;
   2633 
   2634     ALOG_ASSERT( (ret >= 0 && ret < fastInfo.arrayWidth),
   2635         "Calculated pixel array value X = '%d' is out of bounds (upper = %d)",
   2636         ret, fastInfo.arrayWidth);
   2637     return ret;
   2638 }
   2639 
   2640 int Parameters::cropYToArray(int y) const {
   2641     ALOG_ASSERT(y >= 0, "Crop-relative Y coordinate = '%d' is out of bounds "
   2642         "(lower = 0)", y);
   2643 
   2644     CropRegion previewCrop = calculateCropRegion(/*previewOnly*/ true);
   2645     ALOG_ASSERT(y < previewCrop.height, "Crop-relative Y coordinate = '%d' is "
   2646                 "out of bounds (upper = %f)", y, previewCrop.height);
   2647 
   2648     int ret = y + previewCrop.top;
   2649 
   2650     ALOG_ASSERT( (ret >= 0 && ret < fastInfo.arrayHeight),
   2651         "Calculated pixel array value Y = '%d' is out of bounds (upper = %d)",
   2652         ret, fastInfo.arrayHeight);
   2653 
   2654     return ret;
   2655 
   2656 }
   2657 
   2658 int Parameters::normalizedXToCrop(int x) const {
   2659     CropRegion previewCrop = calculateCropRegion(/*previewOnly*/ true);
   2660     return (x + 1000) * (previewCrop.width - 1) / 2000;
   2661 }
   2662 
   2663 int Parameters::normalizedYToCrop(int y) const {
   2664     CropRegion previewCrop = calculateCropRegion(/*previewOnly*/ true);
   2665     return (y + 1000) * (previewCrop.height - 1) / 2000;
   2666 }
   2667 
   2668 int Parameters::normalizedXToArray(int x) const {
   2669 
   2670     // Work-around for HAL pre-scaling the coordinates themselves
   2671     if (quirks.meteringCropRegion) {
   2672         return (x + 1000) * (fastInfo.arrayWidth - 1) / 2000;
   2673     }
   2674 
   2675     return cropXToArray(normalizedXToCrop(x));
   2676 }
   2677 
   2678 int Parameters::normalizedYToArray(int y) const {
   2679     // Work-around for HAL pre-scaling the coordinates themselves
   2680     if (quirks.meteringCropRegion) {
   2681         return (y + 1000) * (fastInfo.arrayHeight - 1) / 2000;
   2682     }
   2683 
   2684     return cropYToArray(normalizedYToCrop(y));
   2685 }
   2686 
   2687 
   2688 Parameters::CropRegion Parameters::calculatePreviewCrop(
   2689         const CropRegion &scalerCrop) const {
   2690     float left, top, width, height;
   2691     float previewAspect = static_cast<float>(previewWidth) / previewHeight;
   2692     float cropAspect = scalerCrop.width / scalerCrop.height;
   2693 
   2694     if (previewAspect > cropAspect) {
   2695         width = scalerCrop.width;
   2696         height = cropAspect * scalerCrop.height / previewAspect;
   2697 
   2698         left = scalerCrop.left;
   2699         top = scalerCrop.top + (scalerCrop.height - height) / 2;
   2700     } else {
   2701         width = previewAspect * scalerCrop.width / cropAspect;
   2702         height = scalerCrop.height;
   2703 
   2704         left = scalerCrop.left + (scalerCrop.width - width) / 2;
   2705         top = scalerCrop.top;
   2706     }
   2707 
   2708     CropRegion previewCrop = {left, top, width, height};
   2709 
   2710     return previewCrop;
   2711 }
   2712 
   2713 int Parameters::arrayXToNormalizedWithCrop(int x,
   2714         const CropRegion &scalerCrop) const {
   2715     // Work-around for HAL pre-scaling the coordinates themselves
   2716     if (quirks.meteringCropRegion) {
   2717         return x * 2000 / (fastInfo.arrayWidth - 1) - 1000;
   2718     } else {
   2719         CropRegion previewCrop = calculatePreviewCrop(scalerCrop);
   2720         return (x - previewCrop.left) * 2000 / (previewCrop.width - 1) - 1000;
   2721     }
   2722 }
   2723 
   2724 int Parameters::arrayYToNormalizedWithCrop(int y,
   2725         const CropRegion &scalerCrop) const {
   2726     // Work-around for HAL pre-scaling the coordinates themselves
   2727     if (quirks.meteringCropRegion) {
   2728         return y * 2000 / (fastInfo.arrayHeight - 1) - 1000;
   2729     } else {
   2730         CropRegion previewCrop = calculatePreviewCrop(scalerCrop);
   2731         return (y - previewCrop.top) * 2000 / (previewCrop.height - 1) - 1000;
   2732     }
   2733 }
   2734 
   2735 status_t Parameters::getFilteredSizes(Size limit, Vector<Size> *sizes) {
   2736     if (info == NULL) {
   2737         ALOGE("%s: Static metadata is not initialized", __FUNCTION__);
   2738         return NO_INIT;
   2739     }
   2740     if (sizes == NULL) {
   2741         ALOGE("%s: Input size is null", __FUNCTION__);
   2742         return BAD_VALUE;
   2743     }
   2744     sizes->clear();
   2745 
   2746     if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
   2747         Vector<StreamConfiguration> scs = getStreamConfigurations();
   2748         for (size_t i=0; i < scs.size(); i++) {
   2749             const StreamConfiguration &sc = scs[i];
   2750             if (sc.isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
   2751                     sc.format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
   2752                     sc.width <= limit.width && sc.height <= limit.height) {
   2753                 Size sz = {sc.width, sc.height};
   2754                 sizes->push(sz);
   2755             }
   2756         }
   2757     } else {
   2758         const size_t SIZE_COUNT = sizeof(Size) / sizeof(int);
   2759         camera_metadata_ro_entry_t availableProcessedSizes =
   2760             staticInfo(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES, SIZE_COUNT);
   2761         if (availableProcessedSizes.count < SIZE_COUNT) return BAD_VALUE;
   2762 
   2763         Size filteredSize;
   2764         for (size_t i = 0; i < availableProcessedSizes.count; i += SIZE_COUNT) {
   2765             filteredSize.width = availableProcessedSizes.data.i32[i];
   2766             filteredSize.height = availableProcessedSizes.data.i32[i+1];
   2767                 // Need skip the preview sizes that are too large.
   2768                 if (filteredSize.width <= limit.width &&
   2769                         filteredSize.height <= limit.height) {
   2770                     sizes->push(filteredSize);
   2771                 }
   2772         }
   2773     }
   2774 
   2775     if (sizes->isEmpty()) {
   2776         ALOGE("generated preview size list is empty!!");
   2777         return BAD_VALUE;
   2778     }
   2779     return OK;
   2780 }
   2781 
   2782 Parameters::Size Parameters::getMaxSizeForRatio(
   2783         float ratio, const int32_t* sizeArray, size_t count) {
   2784     ALOG_ASSERT(sizeArray != NULL, "size array shouldn't be NULL");
   2785     ALOG_ASSERT(count >= 2 && count % 2 == 0, "count must be a positive even number");
   2786 
   2787     Size maxSize = {0, 0};
   2788     for (size_t i = 0; i < count; i += 2) {
   2789         if (sizeArray[i] > 0 && sizeArray[i+1] > 0) {
   2790             float curRatio = static_cast<float>(sizeArray[i]) / sizeArray[i+1];
   2791             if (fabs(curRatio - ratio) < ASPECT_RATIO_TOLERANCE && maxSize.width < sizeArray[i]) {
   2792                 maxSize.width = sizeArray[i];
   2793                 maxSize.height = sizeArray[i+1];
   2794             }
   2795         }
   2796     }
   2797 
   2798     if (maxSize.width == 0 || maxSize.height == 0) {
   2799         maxSize.width = sizeArray[0];
   2800         maxSize.height = sizeArray[1];
   2801         ALOGW("Unable to find the size to match the given aspect ratio %f."
   2802                 "Fall back to %d x %d", ratio, maxSize.width, maxSize.height);
   2803     }
   2804 
   2805     return maxSize;
   2806 }
   2807 
   2808 Parameters::Size Parameters::getMaxSize(const Vector<Parameters::Size> &sizes) {
   2809     Size maxSize = {-1, -1};
   2810     for (size_t i = 0; i < sizes.size(); i++) {
   2811         if (sizes[i].width > maxSize.width ||
   2812                 (sizes[i].width == maxSize.width && sizes[i].height > maxSize.height )) {
   2813             maxSize = sizes[i];
   2814         }
   2815     }
   2816     return maxSize;
   2817 }
   2818 
   2819 Vector<Parameters::StreamConfiguration> Parameters::getStreamConfigurations() {
   2820     const int STREAM_CONFIGURATION_SIZE = 4;
   2821     const int STREAM_FORMAT_OFFSET = 0;
   2822     const int STREAM_WIDTH_OFFSET = 1;
   2823     const int STREAM_HEIGHT_OFFSET = 2;
   2824     const int STREAM_IS_INPUT_OFFSET = 3;
   2825     Vector<StreamConfiguration> scs;
   2826     if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
   2827         ALOGE("StreamConfiguration is only valid after device HAL 3.2!");
   2828         return scs;
   2829     }
   2830 
   2831     camera_metadata_ro_entry_t availableStreamConfigs =
   2832                 staticInfo(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
   2833     for (size_t i = 0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
   2834         int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
   2835         int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
   2836         int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
   2837         int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
   2838         StreamConfiguration sc = {format, width, height, isInput};
   2839         scs.add(sc);
   2840     }
   2841     return scs;
   2842 }
   2843 
   2844 int64_t Parameters::getJpegStreamMinFrameDurationNs(Parameters::Size size) {
   2845     if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
   2846         return getMinFrameDurationNs(size, HAL_PIXEL_FORMAT_BLOB);
   2847     } else {
   2848         Vector<Size> availableJpegSizes = getAvailableJpegSizes();
   2849         size_t streamIdx = availableJpegSizes.size();
   2850         for (size_t i = 0; i < availableJpegSizes.size(); i++) {
   2851             if (availableJpegSizes[i].width == size.width &&
   2852                     availableJpegSizes[i].height == size.height) {
   2853                 streamIdx = i;
   2854                 break;
   2855             }
   2856         }
   2857         if (streamIdx != availableJpegSizes.size()) {
   2858             camera_metadata_ro_entry_t jpegMinDurations =
   2859                     staticInfo(ANDROID_SCALER_AVAILABLE_JPEG_MIN_DURATIONS);
   2860             if (streamIdx < jpegMinDurations.count) {
   2861                 return jpegMinDurations.data.i64[streamIdx];
   2862             }
   2863         }
   2864     }
   2865     ALOGE("%s: cannot find min frame duration for jpeg size %dx%d",
   2866             __FUNCTION__, size.width, size.height);
   2867     return -1;
   2868 }
   2869 
   2870 int64_t Parameters::getMinFrameDurationNs(Parameters::Size size, int fmt) {
   2871     if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
   2872         ALOGE("Min frame duration for HAL 3.1 or lower is not supported");
   2873         return -1;
   2874     }
   2875 
   2876     const int STREAM_DURATION_SIZE = 4;
   2877     const int STREAM_FORMAT_OFFSET = 0;
   2878     const int STREAM_WIDTH_OFFSET = 1;
   2879     const int STREAM_HEIGHT_OFFSET = 2;
   2880     const int STREAM_DURATION_OFFSET = 3;
   2881     camera_metadata_ro_entry_t availableStreamMinDurations =
   2882                 staticInfo(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
   2883     for (size_t i = 0; i < availableStreamMinDurations.count; i+= STREAM_DURATION_SIZE) {
   2884         int64_t format = availableStreamMinDurations.data.i64[i + STREAM_FORMAT_OFFSET];
   2885         int64_t width = availableStreamMinDurations.data.i64[i + STREAM_WIDTH_OFFSET];
   2886         int64_t height = availableStreamMinDurations.data.i64[i + STREAM_HEIGHT_OFFSET];
   2887         int64_t duration = availableStreamMinDurations.data.i64[i + STREAM_DURATION_OFFSET];
   2888         if (format == fmt && width == size.width && height == size.height) {
   2889             return duration;
   2890         }
   2891     }
   2892 
   2893     return -1;
   2894 }
   2895 
   2896 bool Parameters::isFpsSupported(const Vector<Size> &sizes, int format, int32_t fps) {
   2897     // Skip the check for older HAL version, as the min duration is not supported.
   2898     if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
   2899         return true;
   2900     }
   2901 
   2902     // Get min frame duration for each size and check if the given fps range can be supported.
   2903     const int32_t FPS_MARGIN = 1;
   2904     for (size_t i = 0 ; i < sizes.size(); i++) {
   2905         int64_t minFrameDuration = getMinFrameDurationNs(sizes[i], format);
   2906         if (minFrameDuration <= 0) {
   2907             ALOGE("Min frame duration (%" PRId64") for size (%dx%d) and format 0x%x is wrong!",
   2908                 minFrameDuration, sizes[i].width, sizes[i].height, format);
   2909             return false;
   2910         }
   2911         int32_t maxSupportedFps = 1e9 / minFrameDuration;
   2912         // Add some margin here for the case where the hal supports 29.xxxfps.
   2913         maxSupportedFps += FPS_MARGIN;
   2914         if (fps > maxSupportedFps) {
   2915             return false;
   2916         }
   2917     }
   2918     return true;
   2919 }
   2920 
   2921 SortedVector<int32_t> Parameters::getAvailableOutputFormats() {
   2922     SortedVector<int32_t> outputFormats; // Non-duplicated output formats
   2923     if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
   2924         Vector<StreamConfiguration> scs = getStreamConfigurations();
   2925         for (size_t i = 0; i < scs.size(); i++) {
   2926             const StreamConfiguration &sc = scs[i];
   2927             if (sc.isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT) {
   2928                 outputFormats.add(sc.format);
   2929             }
   2930         }
   2931     } else {
   2932         camera_metadata_ro_entry_t availableFormats = staticInfo(ANDROID_SCALER_AVAILABLE_FORMATS);
   2933         for (size_t i = 0; i < availableFormats.count; i++) {
   2934             outputFormats.add(availableFormats.data.i32[i]);
   2935         }
   2936     }
   2937     return outputFormats;
   2938 }
   2939 
   2940 Vector<Parameters::Size> Parameters::getAvailableJpegSizes() {
   2941     Vector<Parameters::Size> jpegSizes;
   2942     if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
   2943         Vector<StreamConfiguration> scs = getStreamConfigurations();
   2944         for (size_t i = 0; i < scs.size(); i++) {
   2945             const StreamConfiguration &sc = scs[i];
   2946             if (sc.isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT &&
   2947                     sc.format == HAL_PIXEL_FORMAT_BLOB) {
   2948                 Size sz = {sc.width, sc.height};
   2949                 jpegSizes.add(sz);
   2950             }
   2951         }
   2952     } else {
   2953         const int JPEG_SIZE_ENTRY_COUNT = 2;
   2954         const int WIDTH_OFFSET = 0;
   2955         const int HEIGHT_OFFSET = 1;
   2956         camera_metadata_ro_entry_t availableJpegSizes =
   2957             staticInfo(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
   2958         for (size_t i = 0; i < availableJpegSizes.count; i+= JPEG_SIZE_ENTRY_COUNT) {
   2959             int width = availableJpegSizes.data.i32[i + WIDTH_OFFSET];
   2960             int height = availableJpegSizes.data.i32[i + HEIGHT_OFFSET];
   2961             Size sz = {width, height};
   2962             jpegSizes.add(sz);
   2963         }
   2964     }
   2965     return jpegSizes;
   2966 }
   2967 
   2968 Parameters::CropRegion Parameters::calculateCropRegion(bool previewOnly) const {
   2969 
   2970     float zoomLeft, zoomTop, zoomWidth, zoomHeight;
   2971 
   2972     // Need to convert zoom index into a crop rectangle. The rectangle is
   2973     // chosen to maximize its area on the sensor
   2974 
   2975     camera_metadata_ro_entry_t maxDigitalZoom =
   2976             staticInfo(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM);
   2977     // For each zoom step by how many pixels more do we change the zoom
   2978     float zoomIncrement = (maxDigitalZoom.data.f[0] - 1) /
   2979             (NUM_ZOOM_STEPS-1);
   2980     // The desired activeAreaWidth/cropAreaWidth ratio (or height if h>w)
   2981     // via interpolating zoom step into a zoom ratio
   2982     float zoomRatio = 1 + zoomIncrement * zoom;
   2983     ALOG_ASSERT( (zoomRatio >= 1.f && zoomRatio <= maxDigitalZoom.data.f[0]),
   2984         "Zoom ratio calculated out of bounds. Expected 1 - %f, actual: %f",
   2985         maxDigitalZoom.data.f[0], zoomRatio);
   2986 
   2987     ALOGV("Zoom maxDigital=%f, increment=%f, ratio=%f, previewWidth=%d, "
   2988           "previewHeight=%d, activeWidth=%d, activeHeight=%d",
   2989           maxDigitalZoom.data.f[0], zoomIncrement, zoomRatio, previewWidth,
   2990           previewHeight, fastInfo.arrayWidth, fastInfo.arrayHeight);
   2991 
   2992     if (previewOnly) {
   2993         // Calculate a tight crop region for the preview stream only
   2994         float previewRatio = static_cast<float>(previewWidth) / previewHeight;
   2995 
   2996         /* Ensure that the width/height never go out of bounds
   2997          * by scaling across a diffent dimension if an out-of-bounds
   2998          * possibility exists.
   2999          *
   3000          * e.g. if the previewratio < arrayratio and e.g. zoomratio = 1.0, then by
   3001          * calculating the zoomWidth from zoomHeight we'll actually get a
   3002          * zoomheight > arrayheight
   3003          */
   3004         float arrayRatio = 1.f * fastInfo.arrayWidth / fastInfo.arrayHeight;
   3005         if (previewRatio >= arrayRatio) {
   3006             // Adjust the height based on the width
   3007             zoomWidth =  fastInfo.arrayWidth / zoomRatio;
   3008             zoomHeight = zoomWidth *
   3009                     previewHeight / previewWidth;
   3010 
   3011         } else {
   3012             // Adjust the width based on the height
   3013             zoomHeight = fastInfo.arrayHeight / zoomRatio;
   3014             zoomWidth = zoomHeight *
   3015                     previewWidth / previewHeight;
   3016         }
   3017     } else {
   3018         // Calculate the global crop region with a shape matching the active
   3019         // array.
   3020         zoomWidth = fastInfo.arrayWidth / zoomRatio;
   3021         zoomHeight = fastInfo.arrayHeight / zoomRatio;
   3022     }
   3023 
   3024     // center the zoom area within the active area
   3025     zoomLeft = (fastInfo.arrayWidth - zoomWidth) / 2;
   3026     zoomTop = (fastInfo.arrayHeight - zoomHeight) / 2;
   3027 
   3028     ALOGV("Crop region calculated (x=%d,y=%d,w=%f,h=%f) for zoom=%d",
   3029         (int32_t)zoomLeft, (int32_t)zoomTop, zoomWidth, zoomHeight, this->zoom);
   3030 
   3031     CropRegion crop = { zoomLeft, zoomTop, zoomWidth, zoomHeight };
   3032     return crop;
   3033 }
   3034 
   3035 status_t Parameters::calculatePictureFovs(float *horizFov, float *vertFov)
   3036         const {
   3037     camera_metadata_ro_entry_t sensorSize =
   3038             staticInfo(ANDROID_SENSOR_INFO_PHYSICAL_SIZE, 2, 2);
   3039     if (!sensorSize.count) return NO_INIT;
   3040 
   3041     camera_metadata_ro_entry_t pixelArraySize =
   3042             staticInfo(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, 2, 2);
   3043     if (!pixelArraySize.count) return NO_INIT;
   3044 
   3045     float arrayAspect = static_cast<float>(fastInfo.arrayWidth) /
   3046             fastInfo.arrayHeight;
   3047     float stillAspect = static_cast<float>(pictureWidth) / pictureHeight;
   3048     ALOGV("Array aspect: %f, still aspect: %f", arrayAspect, stillAspect);
   3049 
   3050     // The crop factors from the full sensor array to the still picture crop
   3051     // region
   3052     float horizCropFactor = 1.f;
   3053     float vertCropFactor = 1.f;
   3054 
   3055     /**
   3056      * Need to calculate the still image field of view based on the total pixel
   3057      * array field of view, and the relative aspect ratios of the pixel array
   3058      * and output streams.
   3059      *
   3060      * Special treatment for quirky definition of crop region and relative
   3061      * stream cropping.
   3062      */
   3063     if (quirks.meteringCropRegion) {
   3064         // Use max of preview and video as first crop
   3065         float previewAspect = static_cast<float>(previewWidth) / previewHeight;
   3066         float videoAspect = static_cast<float>(videoWidth) / videoHeight;
   3067         if (videoAspect > previewAspect) {
   3068             previewAspect = videoAspect;
   3069         }
   3070         // First crop sensor to preview aspect ratio
   3071         if (arrayAspect < previewAspect) {
   3072             vertCropFactor = arrayAspect / previewAspect;
   3073         } else {
   3074             horizCropFactor = previewAspect / arrayAspect;
   3075         }
   3076         // Second crop to still aspect ratio
   3077         if (stillAspect < previewAspect) {
   3078             horizCropFactor *= stillAspect / previewAspect;
   3079         } else {
   3080             vertCropFactor *= previewAspect / stillAspect;
   3081         }
   3082     } else {
   3083         /**
   3084          * Crop are just a function of just the still/array relative aspect
   3085          * ratios. Since each stream will maximize its area within the crop
   3086          * region, and for FOV we assume a full-sensor crop region, we only ever
   3087          * crop the FOV either vertically or horizontally, never both.
   3088          */
   3089         horizCropFactor = (arrayAspect > stillAspect) ?
   3090                 (stillAspect / arrayAspect) : 1.f;
   3091         vertCropFactor = (arrayAspect < stillAspect) ?
   3092                 (arrayAspect / stillAspect) : 1.f;
   3093     }
   3094 
   3095     /**
   3096      * Convert the crop factors w.r.t the active array size to the crop factors
   3097      * w.r.t the pixel array size.
   3098      */
   3099     horizCropFactor *= (static_cast<float>(fastInfo.arrayWidth) /
   3100                             pixelArraySize.data.i32[0]);
   3101     vertCropFactor *= (static_cast<float>(fastInfo.arrayHeight) /
   3102                             pixelArraySize.data.i32[1]);
   3103 
   3104     ALOGV("Horiz crop factor: %f, vert crop fact: %f",
   3105             horizCropFactor, vertCropFactor);
   3106     /**
   3107      * Basic field of view formula is:
   3108      *   angle of view = 2 * arctangent ( d / 2f )
   3109      * where d is the physical sensor dimension of interest, and f is
   3110      * the focal length. This only applies to rectilinear sensors, for focusing
   3111      * at distances >> f, etc.
   3112      */
   3113     if (horizFov != NULL) {
   3114         *horizFov = 180 / M_PI * 2 *
   3115                 atanf(horizCropFactor * sensorSize.data.f[0] /
   3116                         (2 * fastInfo.minFocalLength));
   3117     }
   3118     if (vertFov != NULL) {
   3119         *vertFov = 180 / M_PI * 2 *
   3120                 atanf(vertCropFactor * sensorSize.data.f[1] /
   3121                         (2 * fastInfo.minFocalLength));
   3122     }
   3123     return OK;
   3124 }
   3125 
   3126 int32_t Parameters::fpsFromRange(int32_t /*min*/, int32_t max) const {
   3127     return max;
   3128 }
   3129 
   3130 }; // namespace camera2
   3131 }; // namespace android
   3132