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
33namespace android {
34namespace camera2 {
35
36Parameters::Parameters(int cameraId,
37        int cameraFacing) :
38        cameraId(cameraId),
39        cameraFacing(cameraFacing),
40        info(NULL) {
41}
42
43Parameters::~Parameters() {
44}
45
46status_t Parameters::initialize(const CameraMetadata *info) {
47    status_t res;
48
49    if (info->entryCount() == 0) {
50        ALOGE("%s: No static information provided!", __FUNCTION__);
51        return BAD_VALUE;
52    }
53    Parameters::info = info;
54
55    res = buildFastInfo();
56    if (res != OK) return res;
57
58    res = buildQuirks();
59    if (res != OK) return res;
60
61    camera_metadata_ro_entry_t availableProcessedSizes =
62        staticInfo(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES, 2);
63    if (!availableProcessedSizes.count) return NO_INIT;
64
65    // TODO: Pick more intelligently
66    previewWidth = availableProcessedSizes.data.i32[0];
67    previewHeight = availableProcessedSizes.data.i32[1];
68    videoWidth = previewWidth;
69    videoHeight = previewHeight;
70
71    params.setPreviewSize(previewWidth, previewHeight);
72    params.setVideoSize(videoWidth, videoHeight);
73    params.set(CameraParameters::KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO,
74            String8::format("%dx%d",
75                    previewWidth, previewHeight));
76    {
77        String8 supportedPreviewSizes;
78        for (size_t i=0; i < availableProcessedSizes.count; i += 2) {
79            if (i != 0) supportedPreviewSizes += ",";
80            supportedPreviewSizes += String8::format("%dx%d",
81                    availableProcessedSizes.data.i32[i],
82                    availableProcessedSizes.data.i32[i+1]);
83        }
84        params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES,
85                supportedPreviewSizes);
86        params.set(CameraParameters::KEY_SUPPORTED_VIDEO_SIZES,
87                supportedPreviewSizes);
88    }
89
90    camera_metadata_ro_entry_t availableFpsRanges =
91        staticInfo(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, 2);
92    if (!availableFpsRanges.count) return NO_INIT;
93
94    previewFpsRange[0] = availableFpsRanges.data.i32[0];
95    previewFpsRange[1] = availableFpsRanges.data.i32[1];
96
97    params.set(CameraParameters::KEY_PREVIEW_FPS_RANGE,
98            String8::format("%d,%d",
99                    previewFpsRange[0] * kFpsToApiScale,
100                    previewFpsRange[1] * kFpsToApiScale));
101
102    {
103        String8 supportedPreviewFpsRange;
104        for (size_t i=0; i < availableFpsRanges.count; i += 2) {
105            if (i != 0) supportedPreviewFpsRange += ",";
106            supportedPreviewFpsRange += String8::format("(%d,%d)",
107                    availableFpsRanges.data.i32[i] * kFpsToApiScale,
108                    availableFpsRanges.data.i32[i+1] * kFpsToApiScale);
109        }
110        params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE,
111                supportedPreviewFpsRange);
112    }
113
114    previewFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP;
115    params.set(CameraParameters::KEY_PREVIEW_FORMAT,
116            formatEnumToString(previewFormat)); // NV21
117
118    previewTransform = degToTransform(0,
119            cameraFacing == CAMERA_FACING_FRONT);
120
121    camera_metadata_ro_entry_t availableFormats =
122        staticInfo(ANDROID_SCALER_AVAILABLE_FORMATS);
123
124    {
125        String8 supportedPreviewFormats;
126        bool addComma = false;
127        for (size_t i=0; i < availableFormats.count; i++) {
128            if (addComma) supportedPreviewFormats += ",";
129            addComma = true;
130            switch (availableFormats.data.i32[i]) {
131            case HAL_PIXEL_FORMAT_YCbCr_422_SP:
132                supportedPreviewFormats +=
133                    CameraParameters::PIXEL_FORMAT_YUV422SP;
134                break;
135            case HAL_PIXEL_FORMAT_YCrCb_420_SP:
136                supportedPreviewFormats +=
137                    CameraParameters::PIXEL_FORMAT_YUV420SP;
138                break;
139            case HAL_PIXEL_FORMAT_YCbCr_422_I:
140                supportedPreviewFormats +=
141                    CameraParameters::PIXEL_FORMAT_YUV422I;
142                break;
143            case HAL_PIXEL_FORMAT_YV12:
144                supportedPreviewFormats +=
145                    CameraParameters::PIXEL_FORMAT_YUV420P;
146                break;
147            case HAL_PIXEL_FORMAT_RGB_565:
148                supportedPreviewFormats +=
149                    CameraParameters::PIXEL_FORMAT_RGB565;
150                break;
151            case HAL_PIXEL_FORMAT_RGBA_8888:
152                supportedPreviewFormats +=
153                    CameraParameters::PIXEL_FORMAT_RGBA8888;
154                break;
155            // Not advertizing JPEG, RAW_SENSOR, etc, for preview formats
156            case HAL_PIXEL_FORMAT_RAW_SENSOR:
157            case HAL_PIXEL_FORMAT_BLOB:
158                addComma = false;
159                break;
160
161            default:
162                ALOGW("%s: Camera %d: Unknown preview format: %x",
163                        __FUNCTION__, cameraId, availableFormats.data.i32[i]);
164                addComma = false;
165                break;
166            }
167        }
168        params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS,
169                supportedPreviewFormats);
170    }
171
172    // PREVIEW_FRAME_RATE / SUPPORTED_PREVIEW_FRAME_RATES are deprecated, but
173    // still have to do something sane for them
174
175    // NOTE: Not scaled like FPS range values are.
176    previewFps = fpsFromRange(previewFpsRange[0], previewFpsRange[1]);
177    params.set(CameraParameters::KEY_PREVIEW_FRAME_RATE,
178            previewFps);
179
180    {
181        SortedVector<int32_t> sortedPreviewFrameRates;
182
183        String8 supportedPreviewFrameRates;
184        for (size_t i=0; i < availableFpsRanges.count; i += 2) {
185            // from the [min, max] fps range use the max value
186            int fps = fpsFromRange(availableFpsRanges.data.i32[i],
187                                   availableFpsRanges.data.i32[i+1]);
188
189            // de-dupe frame rates
190            if (sortedPreviewFrameRates.indexOf(fps) == NAME_NOT_FOUND) {
191                sortedPreviewFrameRates.add(fps);
192            }
193            else {
194                continue;
195            }
196
197            if (sortedPreviewFrameRates.size() > 1) {
198                supportedPreviewFrameRates += ",";
199            }
200
201            supportedPreviewFrameRates += String8::format("%d",
202                    fps);
203
204            ALOGV("%s: Supported preview frame rates: %s",
205                    __FUNCTION__, supportedPreviewFrameRates.string());
206        }
207        params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES,
208                supportedPreviewFrameRates);
209    }
210
211    camera_metadata_ro_entry_t availableJpegSizes =
212        staticInfo(ANDROID_SCALER_AVAILABLE_JPEG_SIZES, 2);
213    if (!availableJpegSizes.count) return NO_INIT;
214
215    // TODO: Pick maximum
216    pictureWidth = availableJpegSizes.data.i32[0];
217    pictureHeight = availableJpegSizes.data.i32[1];
218
219    params.setPictureSize(pictureWidth,
220            pictureHeight);
221
222    {
223        String8 supportedPictureSizes;
224        for (size_t i=0; i < availableJpegSizes.count; i += 2) {
225            if (i != 0) supportedPictureSizes += ",";
226            supportedPictureSizes += String8::format("%dx%d",
227                    availableJpegSizes.data.i32[i],
228                    availableJpegSizes.data.i32[i+1]);
229        }
230        params.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES,
231                supportedPictureSizes);
232    }
233
234    params.setPictureFormat(CameraParameters::PIXEL_FORMAT_JPEG);
235    params.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS,
236            CameraParameters::PIXEL_FORMAT_JPEG);
237
238    camera_metadata_ro_entry_t availableJpegThumbnailSizes =
239        staticInfo(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, 4);
240    if (!availableJpegThumbnailSizes.count) return NO_INIT;
241
242    // TODO: Pick default thumbnail size sensibly
243    jpegThumbSize[0] = availableJpegThumbnailSizes.data.i32[0];
244    jpegThumbSize[1] = availableJpegThumbnailSizes.data.i32[1];
245
246    params.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH,
247            jpegThumbSize[0]);
248    params.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT,
249            jpegThumbSize[1]);
250
251    {
252        String8 supportedJpegThumbSizes;
253        for (size_t i=0; i < availableJpegThumbnailSizes.count; i += 2) {
254            if (i != 0) supportedJpegThumbSizes += ",";
255            supportedJpegThumbSizes += String8::format("%dx%d",
256                    availableJpegThumbnailSizes.data.i32[i],
257                    availableJpegThumbnailSizes.data.i32[i+1]);
258        }
259        params.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES,
260                supportedJpegThumbSizes);
261    }
262
263    jpegThumbQuality = 90;
264    params.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY,
265            jpegThumbQuality);
266    jpegQuality = 90;
267    params.set(CameraParameters::KEY_JPEG_QUALITY,
268            jpegQuality);
269    jpegRotation = 0;
270    params.set(CameraParameters::KEY_ROTATION,
271            jpegRotation);
272
273    gpsEnabled = false;
274    gpsCoordinates[0] = 0.0;
275    gpsCoordinates[1] = 0.0;
276    gpsCoordinates[2] = 0.0;
277    gpsTimestamp = 0;
278    gpsProcessingMethod = "unknown";
279    // GPS fields in CameraParameters are not set by implementation
280
281    wbMode = ANDROID_CONTROL_AWB_AUTO;
282    params.set(CameraParameters::KEY_WHITE_BALANCE,
283            CameraParameters::WHITE_BALANCE_AUTO);
284
285    camera_metadata_ro_entry_t availableWhiteBalanceModes =
286        staticInfo(ANDROID_CONTROL_AWB_AVAILABLE_MODES);
287    {
288        String8 supportedWhiteBalance;
289        bool addComma = false;
290        for (size_t i=0; i < availableWhiteBalanceModes.count; i++) {
291            if (addComma) supportedWhiteBalance += ",";
292            addComma = true;
293            switch (availableWhiteBalanceModes.data.u8[i]) {
294            case ANDROID_CONTROL_AWB_AUTO:
295                supportedWhiteBalance +=
296                    CameraParameters::WHITE_BALANCE_AUTO;
297                break;
298            case ANDROID_CONTROL_AWB_INCANDESCENT:
299                supportedWhiteBalance +=
300                    CameraParameters::WHITE_BALANCE_INCANDESCENT;
301                break;
302            case ANDROID_CONTROL_AWB_FLUORESCENT:
303                supportedWhiteBalance +=
304                    CameraParameters::WHITE_BALANCE_FLUORESCENT;
305                break;
306            case ANDROID_CONTROL_AWB_WARM_FLUORESCENT:
307                supportedWhiteBalance +=
308                    CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT;
309                break;
310            case ANDROID_CONTROL_AWB_DAYLIGHT:
311                supportedWhiteBalance +=
312                    CameraParameters::WHITE_BALANCE_DAYLIGHT;
313                break;
314            case ANDROID_CONTROL_AWB_CLOUDY_DAYLIGHT:
315                supportedWhiteBalance +=
316                    CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT;
317                break;
318            case ANDROID_CONTROL_AWB_TWILIGHT:
319                supportedWhiteBalance +=
320                    CameraParameters::WHITE_BALANCE_TWILIGHT;
321                break;
322            case ANDROID_CONTROL_AWB_SHADE:
323                supportedWhiteBalance +=
324                    CameraParameters::WHITE_BALANCE_SHADE;
325                break;
326            // Skipping values not mappable to v1 API
327            case ANDROID_CONTROL_AWB_OFF:
328                addComma = false;
329                break;
330            default:
331                ALOGW("%s: Camera %d: Unknown white balance value: %d",
332                        __FUNCTION__, cameraId,
333                        availableWhiteBalanceModes.data.u8[i]);
334                addComma = false;
335                break;
336            }
337        }
338        params.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE,
339                supportedWhiteBalance);
340    }
341
342    effectMode = ANDROID_CONTROL_EFFECT_OFF;
343    params.set(CameraParameters::KEY_EFFECT,
344            CameraParameters::EFFECT_NONE);
345
346    camera_metadata_ro_entry_t availableEffects =
347        staticInfo(ANDROID_CONTROL_AVAILABLE_EFFECTS);
348    if (!availableEffects.count) return NO_INIT;
349    {
350        String8 supportedEffects;
351        bool addComma = false;
352        for (size_t i=0; i < availableEffects.count; i++) {
353            if (addComma) supportedEffects += ",";
354            addComma = true;
355            switch (availableEffects.data.u8[i]) {
356                case ANDROID_CONTROL_EFFECT_OFF:
357                    supportedEffects +=
358                        CameraParameters::EFFECT_NONE;
359                    break;
360                case ANDROID_CONTROL_EFFECT_MONO:
361                    supportedEffects +=
362                        CameraParameters::EFFECT_MONO;
363                    break;
364                case ANDROID_CONTROL_EFFECT_NEGATIVE:
365                    supportedEffects +=
366                        CameraParameters::EFFECT_NEGATIVE;
367                    break;
368                case ANDROID_CONTROL_EFFECT_SOLARIZE:
369                    supportedEffects +=
370                        CameraParameters::EFFECT_SOLARIZE;
371                    break;
372                case ANDROID_CONTROL_EFFECT_SEPIA:
373                    supportedEffects +=
374                        CameraParameters::EFFECT_SEPIA;
375                    break;
376                case ANDROID_CONTROL_EFFECT_POSTERIZE:
377                    supportedEffects +=
378                        CameraParameters::EFFECT_POSTERIZE;
379                    break;
380                case ANDROID_CONTROL_EFFECT_WHITEBOARD:
381                    supportedEffects +=
382                        CameraParameters::EFFECT_WHITEBOARD;
383                    break;
384                case ANDROID_CONTROL_EFFECT_BLACKBOARD:
385                    supportedEffects +=
386                        CameraParameters::EFFECT_BLACKBOARD;
387                    break;
388                case ANDROID_CONTROL_EFFECT_AQUA:
389                    supportedEffects +=
390                        CameraParameters::EFFECT_AQUA;
391                    break;
392                default:
393                    ALOGW("%s: Camera %d: Unknown effect value: %d",
394                        __FUNCTION__, cameraId, availableEffects.data.u8[i]);
395                    addComma = false;
396                    break;
397            }
398        }
399        params.set(CameraParameters::KEY_SUPPORTED_EFFECTS, supportedEffects);
400    }
401
402    antibandingMode = ANDROID_CONTROL_AE_ANTIBANDING_AUTO;
403    params.set(CameraParameters::KEY_ANTIBANDING,
404            CameraParameters::ANTIBANDING_AUTO);
405
406    camera_metadata_ro_entry_t availableAntibandingModes =
407        staticInfo(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES);
408    if (!availableAntibandingModes.count) return NO_INIT;
409    {
410        String8 supportedAntibanding;
411        bool addComma = false;
412        for (size_t i=0; i < availableAntibandingModes.count; i++) {
413            if (addComma) supportedAntibanding += ",";
414            addComma = true;
415            switch (availableAntibandingModes.data.u8[i]) {
416                case ANDROID_CONTROL_AE_ANTIBANDING_OFF:
417                    supportedAntibanding +=
418                        CameraParameters::ANTIBANDING_OFF;
419                    break;
420                case ANDROID_CONTROL_AE_ANTIBANDING_50HZ:
421                    supportedAntibanding +=
422                        CameraParameters::ANTIBANDING_50HZ;
423                    break;
424                case ANDROID_CONTROL_AE_ANTIBANDING_60HZ:
425                    supportedAntibanding +=
426                        CameraParameters::ANTIBANDING_60HZ;
427                    break;
428                case ANDROID_CONTROL_AE_ANTIBANDING_AUTO:
429                    supportedAntibanding +=
430                        CameraParameters::ANTIBANDING_AUTO;
431                    break;
432                default:
433                    ALOGW("%s: Camera %d: Unknown antibanding value: %d",
434                        __FUNCTION__, cameraId,
435                            availableAntibandingModes.data.u8[i]);
436                    addComma = false;
437                    break;
438            }
439        }
440        params.set(CameraParameters::KEY_SUPPORTED_ANTIBANDING,
441                supportedAntibanding);
442    }
443
444    sceneMode = ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED;
445    params.set(CameraParameters::KEY_SCENE_MODE,
446            CameraParameters::SCENE_MODE_AUTO);
447
448    camera_metadata_ro_entry_t availableSceneModes =
449        staticInfo(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
450    if (!availableSceneModes.count) return NO_INIT;
451    {
452        String8 supportedSceneModes(CameraParameters::SCENE_MODE_AUTO);
453        bool addComma = true;
454        bool noSceneModes = false;
455        for (size_t i=0; i < availableSceneModes.count; i++) {
456            if (addComma) supportedSceneModes += ",";
457            addComma = true;
458            switch (availableSceneModes.data.u8[i]) {
459                case ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED:
460                    noSceneModes = true;
461                    break;
462                case ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY:
463                    // Not in old API
464                    addComma = false;
465                    break;
466                case ANDROID_CONTROL_SCENE_MODE_ACTION:
467                    supportedSceneModes +=
468                        CameraParameters::SCENE_MODE_ACTION;
469                    break;
470                case ANDROID_CONTROL_SCENE_MODE_PORTRAIT:
471                    supportedSceneModes +=
472                        CameraParameters::SCENE_MODE_PORTRAIT;
473                    break;
474                case ANDROID_CONTROL_SCENE_MODE_LANDSCAPE:
475                    supportedSceneModes +=
476                        CameraParameters::SCENE_MODE_LANDSCAPE;
477                    break;
478                case ANDROID_CONTROL_SCENE_MODE_NIGHT:
479                    supportedSceneModes +=
480                        CameraParameters::SCENE_MODE_NIGHT;
481                    break;
482                case ANDROID_CONTROL_SCENE_MODE_NIGHT_PORTRAIT:
483                    supportedSceneModes +=
484                        CameraParameters::SCENE_MODE_NIGHT_PORTRAIT;
485                    break;
486                case ANDROID_CONTROL_SCENE_MODE_THEATRE:
487                    supportedSceneModes +=
488                        CameraParameters::SCENE_MODE_THEATRE;
489                    break;
490                case ANDROID_CONTROL_SCENE_MODE_BEACH:
491                    supportedSceneModes +=
492                        CameraParameters::SCENE_MODE_BEACH;
493                    break;
494                case ANDROID_CONTROL_SCENE_MODE_SNOW:
495                    supportedSceneModes +=
496                        CameraParameters::SCENE_MODE_SNOW;
497                    break;
498                case ANDROID_CONTROL_SCENE_MODE_SUNSET:
499                    supportedSceneModes +=
500                        CameraParameters::SCENE_MODE_SUNSET;
501                    break;
502                case ANDROID_CONTROL_SCENE_MODE_STEADYPHOTO:
503                    supportedSceneModes +=
504                        CameraParameters::SCENE_MODE_STEADYPHOTO;
505                    break;
506                case ANDROID_CONTROL_SCENE_MODE_FIREWORKS:
507                    supportedSceneModes +=
508                        CameraParameters::SCENE_MODE_FIREWORKS;
509                    break;
510                case ANDROID_CONTROL_SCENE_MODE_SPORTS:
511                    supportedSceneModes +=
512                        CameraParameters::SCENE_MODE_SPORTS;
513                    break;
514                case ANDROID_CONTROL_SCENE_MODE_PARTY:
515                    supportedSceneModes +=
516                        CameraParameters::SCENE_MODE_PARTY;
517                    break;
518                case ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT:
519                    supportedSceneModes +=
520                        CameraParameters::SCENE_MODE_CANDLELIGHT;
521                    break;
522                case ANDROID_CONTROL_SCENE_MODE_BARCODE:
523                    supportedSceneModes +=
524                        CameraParameters::SCENE_MODE_BARCODE;
525                    break;
526                default:
527                    ALOGW("%s: Camera %d: Unknown scene mode value: %d",
528                        __FUNCTION__, cameraId,
529                            availableSceneModes.data.u8[i]);
530                    addComma = false;
531                    break;
532            }
533        }
534        if (!noSceneModes) {
535            params.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES,
536                    supportedSceneModes);
537        }
538    }
539
540    camera_metadata_ro_entry_t flashAvailable =
541        staticInfo(ANDROID_FLASH_AVAILABLE, 1, 1);
542    if (!flashAvailable.count) return NO_INIT;
543
544    camera_metadata_ro_entry_t availableAeModes =
545        staticInfo(ANDROID_CONTROL_AE_AVAILABLE_MODES);
546    if (!availableAeModes.count) return NO_INIT;
547
548    if (flashAvailable.data.u8[0]) {
549        flashMode = Parameters::FLASH_MODE_OFF;
550        params.set(CameraParameters::KEY_FLASH_MODE,
551                CameraParameters::FLASH_MODE_OFF);
552
553        String8 supportedFlashModes(CameraParameters::FLASH_MODE_OFF);
554        supportedFlashModes = supportedFlashModes +
555            "," + CameraParameters::FLASH_MODE_AUTO +
556            "," + CameraParameters::FLASH_MODE_ON +
557            "," + CameraParameters::FLASH_MODE_TORCH;
558        for (size_t i=0; i < availableAeModes.count; i++) {
559            if (availableAeModes.data.u8[i] ==
560                    ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE) {
561                supportedFlashModes = supportedFlashModes + "," +
562                    CameraParameters::FLASH_MODE_RED_EYE;
563                break;
564            }
565        }
566        params.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES,
567                supportedFlashModes);
568    } else {
569        flashMode = Parameters::FLASH_MODE_OFF;
570        params.set(CameraParameters::KEY_FLASH_MODE,
571                CameraParameters::FLASH_MODE_OFF);
572        params.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES,
573                CameraParameters::FLASH_MODE_OFF);
574    }
575
576    camera_metadata_ro_entry_t minFocusDistance =
577        staticInfo(ANDROID_LENS_MINIMUM_FOCUS_DISTANCE, 1, 1);
578    if (!minFocusDistance.count) return NO_INIT;
579
580    camera_metadata_ro_entry_t availableAfModes =
581        staticInfo(ANDROID_CONTROL_AF_AVAILABLE_MODES);
582    if (!availableAfModes.count) return NO_INIT;
583
584    if (minFocusDistance.data.f[0] == 0) {
585        // Fixed-focus lens
586        focusMode = Parameters::FOCUS_MODE_FIXED;
587        params.set(CameraParameters::KEY_FOCUS_MODE,
588                CameraParameters::FOCUS_MODE_FIXED);
589        params.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,
590                CameraParameters::FOCUS_MODE_FIXED);
591    } else {
592        focusMode = Parameters::FOCUS_MODE_AUTO;
593        params.set(CameraParameters::KEY_FOCUS_MODE,
594                CameraParameters::FOCUS_MODE_AUTO);
595        String8 supportedFocusModes(CameraParameters::FOCUS_MODE_INFINITY);
596        bool addComma = true;
597
598        for (size_t i=0; i < availableAfModes.count; i++) {
599            if (addComma) supportedFocusModes += ",";
600            addComma = true;
601            switch (availableAfModes.data.u8[i]) {
602                case ANDROID_CONTROL_AF_AUTO:
603                    supportedFocusModes +=
604                        CameraParameters::FOCUS_MODE_AUTO;
605                    break;
606                case ANDROID_CONTROL_AF_MACRO:
607                    supportedFocusModes +=
608                        CameraParameters::FOCUS_MODE_MACRO;
609                    break;
610                case ANDROID_CONTROL_AF_CONTINUOUS_VIDEO:
611                    supportedFocusModes +=
612                        CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO;
613                    break;
614                case ANDROID_CONTROL_AF_CONTINUOUS_PICTURE:
615                    supportedFocusModes +=
616                        CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE;
617                    break;
618                case ANDROID_CONTROL_AF_EDOF:
619                    supportedFocusModes +=
620                        CameraParameters::FOCUS_MODE_EDOF;
621                    break;
622                // Not supported in old API
623                case ANDROID_CONTROL_AF_OFF:
624                    addComma = false;
625                    break;
626                default:
627                    ALOGW("%s: Camera %d: Unknown AF mode value: %d",
628                        __FUNCTION__, cameraId, availableAfModes.data.u8[i]);
629                    addComma = false;
630                    break;
631            }
632        }
633        params.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES,
634                supportedFocusModes);
635    }
636    focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
637    shadowFocusMode = FOCUS_MODE_INVALID;
638
639    camera_metadata_ro_entry_t max3aRegions =
640        staticInfo(ANDROID_CONTROL_MAX_REGIONS, 1, 1);
641    if (!max3aRegions.count) return NO_INIT;
642
643    int32_t maxNumFocusAreas = 0;
644    if (focusMode != Parameters::FOCUS_MODE_FIXED) {
645        maxNumFocusAreas = max3aRegions.data.i32[0];
646    }
647    params.set(CameraParameters::KEY_MAX_NUM_FOCUS_AREAS, maxNumFocusAreas);
648    params.set(CameraParameters::KEY_FOCUS_AREAS,
649            "(0,0,0,0,0)");
650    focusingAreas.clear();
651    focusingAreas.add(Parameters::Area(0,0,0,0,0));
652
653    camera_metadata_ro_entry_t availableFocalLengths =
654        staticInfo(ANDROID_LENS_AVAILABLE_FOCAL_LENGTHS);
655    if (!availableFocalLengths.count) return NO_INIT;
656
657    float minFocalLength = availableFocalLengths.data.f[0];
658    params.setFloat(CameraParameters::KEY_FOCAL_LENGTH, minFocalLength);
659
660    camera_metadata_ro_entry_t sensorSize =
661        staticInfo(ANDROID_SENSOR_PHYSICAL_SIZE, 2, 2);
662    if (!sensorSize.count) return NO_INIT;
663
664    // The fields of view here assume infinity focus, maximum wide angle
665    float horizFov = 180 / M_PI *
666            2 * atanf(sensorSize.data.f[0] / (2 * minFocalLength));
667    float vertFov  = 180 / M_PI *
668            2 * atanf(sensorSize.data.f[1] / (2 * minFocalLength));
669    params.setFloat(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, horizFov);
670    params.setFloat(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, vertFov);
671
672    exposureCompensation = 0;
673    params.set(CameraParameters::KEY_EXPOSURE_COMPENSATION,
674                exposureCompensation);
675
676    camera_metadata_ro_entry_t exposureCompensationRange =
677        staticInfo(ANDROID_CONTROL_AE_EXP_COMPENSATION_RANGE, 2, 2);
678    if (!exposureCompensationRange.count) return NO_INIT;
679
680    params.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION,
681            exposureCompensationRange.data.i32[1]);
682    params.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION,
683            exposureCompensationRange.data.i32[0]);
684
685    camera_metadata_ro_entry_t exposureCompensationStep =
686        staticInfo(ANDROID_CONTROL_AE_EXP_COMPENSATION_STEP, 1, 1);
687    if (!exposureCompensationStep.count) return NO_INIT;
688
689    params.setFloat(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP,
690            (float)exposureCompensationStep.data.r[0].numerator /
691            exposureCompensationStep.data.r[0].denominator);
692
693    autoExposureLock = false;
694    params.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK,
695            CameraParameters::FALSE);
696    params.set(CameraParameters::KEY_AUTO_EXPOSURE_LOCK_SUPPORTED,
697            CameraParameters::TRUE);
698
699    autoWhiteBalanceLock = false;
700    params.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK,
701            CameraParameters::FALSE);
702    params.set(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK_SUPPORTED,
703            CameraParameters::TRUE);
704
705    meteringAreas.add(Parameters::Area(0, 0, 0, 0, 0));
706    params.set(CameraParameters::KEY_MAX_NUM_METERING_AREAS,
707            max3aRegions.data.i32[0]);
708    params.set(CameraParameters::KEY_METERING_AREAS,
709            "(0,0,0,0,0)");
710
711    zoom = 0;
712    params.set(CameraParameters::KEY_ZOOM, zoom);
713    params.set(CameraParameters::KEY_MAX_ZOOM, NUM_ZOOM_STEPS - 1);
714
715    camera_metadata_ro_entry_t maxDigitalZoom =
716        staticInfo(ANDROID_SCALER_AVAILABLE_MAX_ZOOM, /*minCount*/1, /*maxCount*/1);
717    if (!maxDigitalZoom.count) return NO_INIT;
718
719    {
720        String8 zoomRatios;
721        float zoom = 1.f;
722        float zoomIncrement = (maxDigitalZoom.data.f[0] - zoom) /
723                (NUM_ZOOM_STEPS-1);
724        bool addComma = false;
725        for (size_t i=0; i < NUM_ZOOM_STEPS; i++) {
726            if (addComma) zoomRatios += ",";
727            addComma = true;
728            zoomRatios += String8::format("%d", static_cast<int>(zoom * 100));
729            zoom += zoomIncrement;
730        }
731        params.set(CameraParameters::KEY_ZOOM_RATIOS, zoomRatios);
732    }
733
734    params.set(CameraParameters::KEY_ZOOM_SUPPORTED,
735            CameraParameters::TRUE);
736    params.set(CameraParameters::KEY_SMOOTH_ZOOM_SUPPORTED,
737            CameraParameters::FALSE);
738
739    params.set(CameraParameters::KEY_FOCUS_DISTANCES,
740            "Infinity,Infinity,Infinity");
741
742    params.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW,
743            fastInfo.maxFaces);
744    params.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW,
745            0);
746
747    params.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT,
748            CameraParameters::PIXEL_FORMAT_ANDROID_OPAQUE);
749
750    recordingHint = false;
751    params.set(CameraParameters::KEY_RECORDING_HINT,
752            CameraParameters::FALSE);
753
754    params.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED,
755            CameraParameters::TRUE);
756
757    videoStabilization = false;
758    params.set(CameraParameters::KEY_VIDEO_STABILIZATION,
759            CameraParameters::FALSE);
760
761    camera_metadata_ro_entry_t availableVideoStabilizationModes =
762        staticInfo(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES);
763    if (!availableVideoStabilizationModes.count) return NO_INIT;
764
765    if (availableVideoStabilizationModes.count > 1) {
766        params.set(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED,
767                CameraParameters::TRUE);
768    } else {
769        params.set(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED,
770                CameraParameters::FALSE);
771    }
772
773    // Set up initial state for non-Camera.Parameters state variables
774
775    storeMetadataInBuffers = true;
776    playShutterSound = true;
777    enableFaceDetect = false;
778
779    enableFocusMoveMessages = false;
780    afTriggerCounter = 1;
781    currentAfTriggerId = -1;
782    afInMotion = false;
783
784    precaptureTriggerCounter = 1;
785
786    previewCallbackFlags = 0;
787    previewCallbackOneShot = false;
788
789    char value[PROPERTY_VALUE_MAX];
790    property_get("camera.disable_zsl_mode", value, "0");
791    if (!strcmp(value,"1")) {
792        ALOGI("Camera %d: Disabling ZSL mode", cameraId);
793        zslMode = false;
794    } else {
795        zslMode = true;
796    }
797
798    lightFx = LIGHTFX_NONE;
799
800    state = STOPPED;
801
802    paramsFlattened = params.flatten();
803
804    return OK;
805}
806
807String8 Parameters::get() const {
808    return paramsFlattened;
809}
810
811status_t Parameters::buildFastInfo() {
812
813    camera_metadata_ro_entry_t activeArraySize =
814        staticInfo(ANDROID_SENSOR_ACTIVE_ARRAY_SIZE, 2, 2);
815    if (!activeArraySize.count) return NO_INIT;
816    int32_t arrayWidth = activeArraySize.data.i32[0];
817    int32_t arrayHeight = activeArraySize.data.i32[1];
818
819    camera_metadata_ro_entry_t availableFaceDetectModes =
820        staticInfo(ANDROID_STATS_AVAILABLE_FACE_DETECT_MODES);
821    if (!availableFaceDetectModes.count) return NO_INIT;
822
823    uint8_t bestFaceDetectMode =
824        ANDROID_STATS_FACE_DETECTION_OFF;
825    for (size_t i = 0 ; i < availableFaceDetectModes.count; i++) {
826        switch (availableFaceDetectModes.data.u8[i]) {
827            case ANDROID_STATS_FACE_DETECTION_OFF:
828                break;
829            case ANDROID_STATS_FACE_DETECTION_SIMPLE:
830                if (bestFaceDetectMode !=
831                        ANDROID_STATS_FACE_DETECTION_FULL) {
832                    bestFaceDetectMode =
833                        ANDROID_STATS_FACE_DETECTION_SIMPLE;
834                }
835                break;
836            case ANDROID_STATS_FACE_DETECTION_FULL:
837                bestFaceDetectMode =
838                    ANDROID_STATS_FACE_DETECTION_FULL;
839                break;
840            default:
841                ALOGE("%s: Camera %d: Unknown face detect mode %d:",
842                        __FUNCTION__, cameraId,
843                        availableFaceDetectModes.data.u8[i]);
844                return NO_INIT;
845        }
846    }
847
848    camera_metadata_ro_entry_t maxFacesDetected =
849        staticInfo(ANDROID_STATS_MAX_FACE_COUNT, 1, 1);
850    if (!maxFacesDetected.count) return NO_INIT;
851
852    int32_t maxFaces = maxFacesDetected.data.i32[0];
853
854    camera_metadata_ro_entry_t availableSceneModes =
855        staticInfo(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
856    camera_metadata_ro_entry_t sceneModeOverrides =
857        staticInfo(ANDROID_CONTROL_SCENE_MODE_OVERRIDES);
858    camera_metadata_ro_entry_t minFocusDistance =
859        staticInfo(ANDROID_LENS_MINIMUM_FOCUS_DISTANCE);
860    bool fixedLens = (minFocusDistance.data.f[0] == 0);
861
862    if (sceneModeOverrides.count > 0) {
863        // sceneModeOverrides is defined to have 3 entries for each scene mode,
864        // which are AE, AWB, and AF override modes the HAL wants for that scene
865        // mode.
866        const size_t kModesPerSceneMode = 3;
867        if (sceneModeOverrides.count !=
868                availableSceneModes.count * kModesPerSceneMode) {
869            ALOGE("%s: Camera %d: Scene mode override list is an "
870                    "unexpected size: %d (expected %d)", __FUNCTION__,
871                    cameraId, sceneModeOverrides.count,
872                    availableSceneModes.count);
873            return NO_INIT;
874        }
875        for (size_t i = 0; i < availableSceneModes.count; i++) {
876            DeviceInfo::OverrideModes modes;
877            uint8_t aeMode =
878                    sceneModeOverrides.data.u8[i * kModesPerSceneMode + 0];
879            switch(aeMode) {
880                case ANDROID_CONTROL_AE_ON:
881                    modes.flashMode = FLASH_MODE_OFF;
882                    break;
883                case ANDROID_CONTROL_AE_ON_AUTO_FLASH:
884                    modes.flashMode = FLASH_MODE_AUTO;
885                    break;
886                case ANDROID_CONTROL_AE_ON_ALWAYS_FLASH:
887                    modes.flashMode = FLASH_MODE_ON;
888                    break;
889                case ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE:
890                    modes.flashMode = FLASH_MODE_RED_EYE;
891                    break;
892                default:
893                    ALOGE("%s: Unknown override AE mode: %d", __FUNCTION__,
894                            aeMode);
895                    modes.flashMode = FLASH_MODE_INVALID;
896                    break;
897            }
898            modes.wbMode =
899                    sceneModeOverrides.data.u8[i * kModesPerSceneMode + 1];
900            uint8_t afMode =
901                    sceneModeOverrides.data.u8[i * kModesPerSceneMode + 2];
902            switch(afMode) {
903                case ANDROID_CONTROL_AF_OFF:
904                    modes.focusMode = fixedLens ?
905                            FOCUS_MODE_FIXED : FOCUS_MODE_INFINITY;
906                    break;
907                case ANDROID_CONTROL_AF_AUTO:
908                case ANDROID_CONTROL_AF_MACRO:
909                case ANDROID_CONTROL_AF_CONTINUOUS_VIDEO:
910                case ANDROID_CONTROL_AF_CONTINUOUS_PICTURE:
911                case ANDROID_CONTROL_AF_EDOF:
912                    modes.focusMode = static_cast<focusMode_t>(afMode);
913                    break;
914                default:
915                    ALOGE("%s: Unknown override AF mode: %d", __FUNCTION__,
916                            afMode);
917                    modes.focusMode = FOCUS_MODE_INVALID;
918                    break;
919            }
920            fastInfo.sceneModeOverrides.add(availableSceneModes.data.u8[i],
921                    modes);
922        }
923    }
924
925    fastInfo.arrayWidth = arrayWidth;
926    fastInfo.arrayHeight = arrayHeight;
927    fastInfo.bestFaceDetectMode = bestFaceDetectMode;
928    fastInfo.maxFaces = maxFaces;
929    return OK;
930}
931
932status_t Parameters::buildQuirks() {
933    camera_metadata_ro_entry_t entry;
934    entry = info->find(ANDROID_QUIRKS_TRIGGER_AF_WITH_AUTO);
935    quirks.triggerAfWithAuto = (entry.count != 0 && entry.data.u8[0] == 1);
936    ALOGV_IF(quirks.triggerAfWithAuto, "Camera %d: Quirk triggerAfWithAuto enabled",
937            cameraId);
938
939    entry = info->find(ANDROID_QUIRKS_USE_ZSL_FORMAT);
940    quirks.useZslFormat = (entry.count != 0 && entry.data.u8[0] == 1);
941    ALOGV_IF(quirks.useZslFormat, "Camera %d: Quirk useZslFormat enabled",
942            cameraId);
943
944    entry = info->find(ANDROID_QUIRKS_METERING_CROP_REGION);
945    quirks.meteringCropRegion = (entry.count != 0 && entry.data.u8[0] == 1);
946    ALOGV_IF(quirks.meteringCropRegion, "Camera %d: Quirk meteringCropRegion"
947                " enabled", cameraId);
948
949    return OK;
950}
951
952camera_metadata_ro_entry_t Parameters::staticInfo(uint32_t tag,
953        size_t minCount, size_t maxCount) const {
954    status_t res;
955    camera_metadata_ro_entry_t entry = info->find(tag);
956
957    if (CC_UNLIKELY( entry.count == 0 )) {
958        const char* tagSection = get_camera_metadata_section_name(tag);
959        if (tagSection == NULL) tagSection = "<unknown>";
960        const char* tagName = get_camera_metadata_tag_name(tag);
961        if (tagName == NULL) tagName = "<unknown>";
962
963        ALOGE("Error finding static metadata entry '%s.%s' (%x)",
964                tagSection, tagName, tag);
965    } else if (CC_UNLIKELY(
966            (minCount != 0 && entry.count < minCount) ||
967            (maxCount != 0 && entry.count > maxCount) ) ) {
968        const char* tagSection = get_camera_metadata_section_name(tag);
969        if (tagSection == NULL) tagSection = "<unknown>";
970        const char* tagName = get_camera_metadata_tag_name(tag);
971        if (tagName == NULL) tagName = "<unknown>";
972        ALOGE("Malformed static metadata entry '%s.%s' (%x):"
973                "Expected between %d and %d values, but got %d values",
974                tagSection, tagName, tag, minCount, maxCount, entry.count);
975    }
976
977    return entry;
978}
979
980status_t Parameters::set(const String8& paramString) {
981    status_t res;
982
983    CameraParameters newParams(paramString);
984
985    // TODO: Currently ignoring any changes to supposedly read-only parameters
986    // such as supported preview sizes, etc. Should probably produce an error if
987    // they're changed.
988
989    /** Extract and verify new parameters */
990
991    size_t i;
992
993    Parameters validatedParams(*this);
994
995    // PREVIEW_SIZE
996    newParams.getPreviewSize(&validatedParams.previewWidth,
997            &validatedParams.previewHeight);
998
999    if (validatedParams.previewWidth != previewWidth ||
1000            validatedParams.previewHeight != previewHeight) {
1001        if (state >= PREVIEW) {
1002            ALOGE("%s: Preview size cannot be updated when preview "
1003                    "is active! (Currently %d x %d, requested %d x %d",
1004                    __FUNCTION__,
1005                    previewWidth, previewHeight,
1006                    validatedParams.previewWidth, validatedParams.previewHeight);
1007            return BAD_VALUE;
1008        }
1009        camera_metadata_ro_entry_t availablePreviewSizes =
1010            staticInfo(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES);
1011        for (i = 0; i < availablePreviewSizes.count; i += 2 ) {
1012            if ((availablePreviewSizes.data.i32[i] ==
1013                    validatedParams.previewWidth) &&
1014                (availablePreviewSizes.data.i32[i+1] ==
1015                    validatedParams.previewHeight)) break;
1016        }
1017        if (i == availablePreviewSizes.count) {
1018            ALOGE("%s: Requested preview size %d x %d is not supported",
1019                    __FUNCTION__, validatedParams.previewWidth,
1020                    validatedParams.previewHeight);
1021            return BAD_VALUE;
1022        }
1023    }
1024
1025    // RECORDING_HINT (always supported)
1026    validatedParams.recordingHint = boolFromString(
1027        newParams.get(CameraParameters::KEY_RECORDING_HINT) );
1028    bool recordingHintChanged = validatedParams.recordingHint != recordingHint;
1029    ALOGV_IF(recordingHintChanged, "%s: Recording hint changed to %d",
1030            __FUNCTION__, recordingHintChanged);
1031
1032    // PREVIEW_FPS_RANGE
1033    bool fpsRangeChanged = false;
1034    newParams.getPreviewFpsRange(&validatedParams.previewFpsRange[0],
1035            &validatedParams.previewFpsRange[1]);
1036    validatedParams.previewFpsRange[0] /= kFpsToApiScale;
1037    validatedParams.previewFpsRange[1] /= kFpsToApiScale;
1038
1039    if (validatedParams.previewFpsRange[0] != previewFpsRange[0] ||
1040            validatedParams.previewFpsRange[1] != previewFpsRange[1]) {
1041        fpsRangeChanged = true;
1042        camera_metadata_ro_entry_t availablePreviewFpsRanges =
1043            staticInfo(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, 2);
1044        for (i = 0; i < availablePreviewFpsRanges.count; i += 2) {
1045            if ((availablePreviewFpsRanges.data.i32[i] ==
1046                    validatedParams.previewFpsRange[0]) &&
1047                (availablePreviewFpsRanges.data.i32[i+1] ==
1048                    validatedParams.previewFpsRange[1]) ) {
1049                break;
1050            }
1051        }
1052        if (i == availablePreviewFpsRanges.count) {
1053            ALOGE("%s: Requested preview FPS range %d - %d is not supported",
1054                __FUNCTION__, validatedParams.previewFpsRange[0],
1055                    validatedParams.previewFpsRange[1]);
1056            return BAD_VALUE;
1057        }
1058        validatedParams.previewFps =
1059            fpsFromRange(validatedParams.previewFpsRange[0],
1060                         validatedParams.previewFpsRange[1]);
1061        newParams.setPreviewFrameRate(validatedParams.previewFps);
1062    }
1063
1064    // PREVIEW_FORMAT
1065    validatedParams.previewFormat =
1066            formatStringToEnum(newParams.getPreviewFormat());
1067    if (validatedParams.previewFormat != previewFormat) {
1068        if (state >= PREVIEW) {
1069            ALOGE("%s: Preview format cannot be updated when preview "
1070                    "is active!", __FUNCTION__);
1071            return BAD_VALUE;
1072        }
1073        camera_metadata_ro_entry_t availableFormats =
1074            staticInfo(ANDROID_SCALER_AVAILABLE_FORMATS);
1075        for (i = 0; i < availableFormats.count; i++) {
1076            if (availableFormats.data.i32[i] == validatedParams.previewFormat)
1077                break;
1078        }
1079        if (i == availableFormats.count) {
1080            ALOGE("%s: Requested preview format %s (0x%x) is not supported",
1081                    __FUNCTION__, newParams.getPreviewFormat(),
1082                    validatedParams.previewFormat);
1083            return BAD_VALUE;
1084        }
1085    }
1086
1087    // PREVIEW_FRAME_RATE
1088    // Deprecated, only use if the preview fps range is unchanged this time.
1089    // The single-value FPS is the same as the minimum of the range.
1090    if (!fpsRangeChanged) {
1091        validatedParams.previewFps = newParams.getPreviewFrameRate();
1092        if (validatedParams.previewFps != previewFps || recordingHintChanged) {
1093            camera_metadata_ro_entry_t availableFrameRates =
1094                staticInfo(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
1095            /**
1096              * If recording hint is set, find the range that encompasses
1097              * previewFps with the largest min index.
1098              *
1099              * If recording hint is not set, find the range with previewFps
1100              * with the smallest min index.
1101              *
1102              * Either way, in case of multiple ranges, break the tie by
1103              * selecting the smaller range.
1104              */
1105            int targetFps = validatedParams.previewFps;
1106            // all ranges which have targetFps
1107            Vector<Range> candidateRanges;
1108            for (i = 0; i < availableFrameRates.count; i+=2) {
1109                Range r = {
1110                            availableFrameRates.data.i32[i],
1111                            availableFrameRates.data.i32[i+1]
1112                };
1113
1114                if (r.min <= targetFps && targetFps <= r.max) {
1115                    candidateRanges.push(r);
1116                }
1117            }
1118            if (candidateRanges.isEmpty()) {
1119                ALOGE("%s: Requested preview frame rate %d is not supported",
1120                        __FUNCTION__, validatedParams.previewFps);
1121                return BAD_VALUE;
1122            }
1123            // most applicable range with targetFps
1124            Range bestRange = candidateRanges[0];
1125            for (i = 1; i < candidateRanges.size(); ++i) {
1126                Range r = candidateRanges[i];
1127
1128                // Find by largest minIndex in recording mode
1129                if (validatedParams.recordingHint) {
1130                    if (r.min > bestRange.min) {
1131                        bestRange = r;
1132                    }
1133                    else if (r.min == bestRange.min && r.max < bestRange.max) {
1134                        bestRange = r;
1135                    }
1136                }
1137                // Find by smallest minIndex in preview mode
1138                else {
1139                    if (r.min < bestRange.min) {
1140                        bestRange = r;
1141                    }
1142                    else if (r.min == bestRange.min && r.max < bestRange.max) {
1143                        bestRange = r;
1144                    }
1145                }
1146            }
1147
1148            validatedParams.previewFpsRange[0] =
1149                    bestRange.min;
1150            validatedParams.previewFpsRange[1] =
1151                    bestRange.max;
1152
1153            ALOGV("%s: New preview FPS range: %d, %d, recordingHint = %d",
1154                __FUNCTION__,
1155                validatedParams.previewFpsRange[0],
1156                validatedParams.previewFpsRange[1],
1157                validatedParams.recordingHint);
1158        }
1159        newParams.set(CameraParameters::KEY_PREVIEW_FPS_RANGE,
1160                String8::format("%d,%d",
1161                        validatedParams.previewFpsRange[0] * kFpsToApiScale,
1162                        validatedParams.previewFpsRange[1] * kFpsToApiScale));
1163
1164    }
1165
1166    // PICTURE_SIZE
1167    newParams.getPictureSize(&validatedParams.pictureWidth,
1168            &validatedParams.pictureHeight);
1169    if (validatedParams.pictureWidth == pictureWidth ||
1170            validatedParams.pictureHeight == pictureHeight) {
1171        camera_metadata_ro_entry_t availablePictureSizes =
1172            staticInfo(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
1173        for (i = 0; i < availablePictureSizes.count; i+=2) {
1174            if ((availablePictureSizes.data.i32[i] ==
1175                    validatedParams.pictureWidth) &&
1176                (availablePictureSizes.data.i32[i+1] ==
1177                    validatedParams.pictureHeight)) break;
1178        }
1179        if (i == availablePictureSizes.count) {
1180            ALOGE("%s: Requested picture size %d x %d is not supported",
1181                    __FUNCTION__, validatedParams.pictureWidth,
1182                    validatedParams.pictureHeight);
1183            return BAD_VALUE;
1184        }
1185    }
1186
1187    // JPEG_THUMBNAIL_WIDTH/HEIGHT
1188    validatedParams.jpegThumbSize[0] =
1189            newParams.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH);
1190    validatedParams.jpegThumbSize[1] =
1191            newParams.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT);
1192    if (validatedParams.jpegThumbSize[0] != jpegThumbSize[0] ||
1193            validatedParams.jpegThumbSize[1] != jpegThumbSize[1]) {
1194        camera_metadata_ro_entry_t availableJpegThumbSizes =
1195            staticInfo(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES);
1196        for (i = 0; i < availableJpegThumbSizes.count; i+=2) {
1197            if ((availableJpegThumbSizes.data.i32[i] ==
1198                    validatedParams.jpegThumbSize[0]) &&
1199                (availableJpegThumbSizes.data.i32[i+1] ==
1200                    validatedParams.jpegThumbSize[1])) break;
1201        }
1202        if (i == availableJpegThumbSizes.count) {
1203            ALOGE("%s: Requested JPEG thumbnail size %d x %d is not supported",
1204                    __FUNCTION__, validatedParams.jpegThumbSize[0],
1205                    validatedParams.jpegThumbSize[1]);
1206            return BAD_VALUE;
1207        }
1208    }
1209
1210    // JPEG_THUMBNAIL_QUALITY
1211    validatedParams.jpegThumbQuality =
1212            newParams.getInt(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY);
1213    if (validatedParams.jpegThumbQuality < 0 ||
1214            validatedParams.jpegThumbQuality > 100) {
1215        ALOGE("%s: Requested JPEG thumbnail quality %d is not supported",
1216                __FUNCTION__, validatedParams.jpegThumbQuality);
1217        return BAD_VALUE;
1218    }
1219
1220    // JPEG_QUALITY
1221    validatedParams.jpegQuality =
1222            newParams.getInt(CameraParameters::KEY_JPEG_QUALITY);
1223    if (validatedParams.jpegQuality < 0 || validatedParams.jpegQuality > 100) {
1224        ALOGE("%s: Requested JPEG quality %d is not supported",
1225                __FUNCTION__, validatedParams.jpegQuality);
1226        return BAD_VALUE;
1227    }
1228
1229    // ROTATION
1230    validatedParams.jpegRotation =
1231            newParams.getInt(CameraParameters::KEY_ROTATION);
1232    if (validatedParams.jpegRotation != 0 &&
1233            validatedParams.jpegRotation != 90 &&
1234            validatedParams.jpegRotation != 180 &&
1235            validatedParams.jpegRotation != 270) {
1236        ALOGE("%s: Requested picture rotation angle %d is not supported",
1237                __FUNCTION__, validatedParams.jpegRotation);
1238        return BAD_VALUE;
1239    }
1240
1241    // GPS
1242
1243    const char *gpsLatStr =
1244            newParams.get(CameraParameters::KEY_GPS_LATITUDE);
1245    if (gpsLatStr != NULL) {
1246        const char *gpsLongStr =
1247                newParams.get(CameraParameters::KEY_GPS_LONGITUDE);
1248        const char *gpsAltitudeStr =
1249                newParams.get(CameraParameters::KEY_GPS_ALTITUDE);
1250        const char *gpsTimeStr =
1251                newParams.get(CameraParameters::KEY_GPS_TIMESTAMP);
1252        const char *gpsProcMethodStr =
1253                newParams.get(CameraParameters::KEY_GPS_PROCESSING_METHOD);
1254        if (gpsLongStr == NULL ||
1255                gpsAltitudeStr == NULL ||
1256                gpsTimeStr == NULL ||
1257                gpsProcMethodStr == NULL) {
1258            ALOGE("%s: Incomplete set of GPS parameters provided",
1259                    __FUNCTION__);
1260            return BAD_VALUE;
1261        }
1262        char *endPtr;
1263        errno = 0;
1264        validatedParams.gpsCoordinates[0] = strtod(gpsLatStr, &endPtr);
1265        if (errno || endPtr == gpsLatStr) {
1266            ALOGE("%s: Malformed GPS latitude: %s", __FUNCTION__, gpsLatStr);
1267            return BAD_VALUE;
1268        }
1269        errno = 0;
1270        validatedParams.gpsCoordinates[1] = strtod(gpsLongStr, &endPtr);
1271        if (errno || endPtr == gpsLongStr) {
1272            ALOGE("%s: Malformed GPS longitude: %s", __FUNCTION__, gpsLongStr);
1273            return BAD_VALUE;
1274        }
1275        errno = 0;
1276        validatedParams.gpsCoordinates[2] = strtod(gpsAltitudeStr, &endPtr);
1277        if (errno || endPtr == gpsAltitudeStr) {
1278            ALOGE("%s: Malformed GPS altitude: %s", __FUNCTION__,
1279                    gpsAltitudeStr);
1280            return BAD_VALUE;
1281        }
1282        errno = 0;
1283        validatedParams.gpsTimestamp = strtoll(gpsTimeStr, &endPtr, 10);
1284        if (errno || endPtr == gpsTimeStr) {
1285            ALOGE("%s: Malformed GPS timestamp: %s", __FUNCTION__, gpsTimeStr);
1286            return BAD_VALUE;
1287        }
1288        validatedParams.gpsProcessingMethod = gpsProcMethodStr;
1289
1290        validatedParams.gpsEnabled = true;
1291    } else {
1292        validatedParams.gpsEnabled = false;
1293    }
1294
1295    // EFFECT
1296    validatedParams.effectMode = effectModeStringToEnum(
1297        newParams.get(CameraParameters::KEY_EFFECT) );
1298    if (validatedParams.effectMode != effectMode) {
1299        camera_metadata_ro_entry_t availableEffectModes =
1300            staticInfo(ANDROID_CONTROL_AVAILABLE_EFFECTS);
1301        for (i = 0; i < availableEffectModes.count; i++) {
1302            if (validatedParams.effectMode == availableEffectModes.data.u8[i]) break;
1303        }
1304        if (i == availableEffectModes.count) {
1305            ALOGE("%s: Requested effect mode \"%s\" is not supported",
1306                    __FUNCTION__,
1307                    newParams.get(CameraParameters::KEY_EFFECT) );
1308            return BAD_VALUE;
1309        }
1310    }
1311
1312    // ANTIBANDING
1313    validatedParams.antibandingMode = abModeStringToEnum(
1314        newParams.get(CameraParameters::KEY_ANTIBANDING) );
1315    if (validatedParams.antibandingMode != antibandingMode) {
1316        camera_metadata_ro_entry_t availableAbModes =
1317            staticInfo(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES);
1318        for (i = 0; i < availableAbModes.count; i++) {
1319            if (validatedParams.antibandingMode == availableAbModes.data.u8[i])
1320                break;
1321        }
1322        if (i == availableAbModes.count) {
1323            ALOGE("%s: Requested antibanding mode \"%s\" is not supported",
1324                    __FUNCTION__,
1325                    newParams.get(CameraParameters::KEY_ANTIBANDING));
1326            return BAD_VALUE;
1327        }
1328    }
1329
1330    // SCENE_MODE
1331    validatedParams.sceneMode = sceneModeStringToEnum(
1332        newParams.get(CameraParameters::KEY_SCENE_MODE) );
1333    if (validatedParams.sceneMode != sceneMode &&
1334            validatedParams.sceneMode !=
1335            ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED) {
1336        camera_metadata_ro_entry_t availableSceneModes =
1337            staticInfo(ANDROID_CONTROL_AVAILABLE_SCENE_MODES);
1338        for (i = 0; i < availableSceneModes.count; i++) {
1339            if (validatedParams.sceneMode == availableSceneModes.data.u8[i])
1340                break;
1341        }
1342        if (i == availableSceneModes.count) {
1343            ALOGE("%s: Requested scene mode \"%s\" is not supported",
1344                    __FUNCTION__,
1345                    newParams.get(CameraParameters::KEY_SCENE_MODE));
1346            return BAD_VALUE;
1347        }
1348    }
1349    bool sceneModeSet =
1350            validatedParams.sceneMode != ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED;
1351
1352    // FLASH_MODE
1353    if (sceneModeSet) {
1354        validatedParams.flashMode =
1355                fastInfo.sceneModeOverrides.
1356                        valueFor(validatedParams.sceneMode).flashMode;
1357    } else {
1358        validatedParams.flashMode = FLASH_MODE_INVALID;
1359    }
1360    if (validatedParams.flashMode == FLASH_MODE_INVALID) {
1361        validatedParams.flashMode = flashModeStringToEnum(
1362            newParams.get(CameraParameters::KEY_FLASH_MODE) );
1363    }
1364
1365    if (validatedParams.flashMode != flashMode) {
1366        camera_metadata_ro_entry_t flashAvailable =
1367            staticInfo(ANDROID_FLASH_AVAILABLE, 1, 1);
1368        if (!flashAvailable.data.u8[0] &&
1369                validatedParams.flashMode != Parameters::FLASH_MODE_OFF) {
1370            ALOGE("%s: Requested flash mode \"%s\" is not supported: "
1371                    "No flash on device", __FUNCTION__,
1372                    newParams.get(CameraParameters::KEY_FLASH_MODE));
1373            return BAD_VALUE;
1374        } else if (validatedParams.flashMode == Parameters::FLASH_MODE_RED_EYE) {
1375            camera_metadata_ro_entry_t availableAeModes =
1376                staticInfo(ANDROID_CONTROL_AE_AVAILABLE_MODES);
1377            for (i = 0; i < availableAeModes.count; i++) {
1378                if (validatedParams.flashMode == availableAeModes.data.u8[i])
1379                    break;
1380            }
1381            if (i == availableAeModes.count) {
1382                ALOGE("%s: Requested flash mode \"%s\" is not supported",
1383                        __FUNCTION__,
1384                        newParams.get(CameraParameters::KEY_FLASH_MODE));
1385                return BAD_VALUE;
1386            }
1387        } else if (validatedParams.flashMode == -1) {
1388            ALOGE("%s: Requested flash mode \"%s\" is unknown",
1389                    __FUNCTION__,
1390                    newParams.get(CameraParameters::KEY_FLASH_MODE));
1391            return BAD_VALUE;
1392        }
1393        // Update in case of override
1394        newParams.set(CameraParameters::KEY_FLASH_MODE,
1395                flashModeEnumToString(validatedParams.flashMode));
1396    }
1397
1398    // WHITE_BALANCE
1399    if (sceneModeSet) {
1400        validatedParams.wbMode =
1401                fastInfo.sceneModeOverrides.
1402                        valueFor(validatedParams.sceneMode).wbMode;
1403    } else {
1404        validatedParams.wbMode = ANDROID_CONTROL_AWB_OFF;
1405    }
1406    if (validatedParams.wbMode == ANDROID_CONTROL_AWB_OFF) {
1407        validatedParams.wbMode = wbModeStringToEnum(
1408            newParams.get(CameraParameters::KEY_WHITE_BALANCE) );
1409    }
1410    if (validatedParams.wbMode != wbMode) {
1411        camera_metadata_ro_entry_t availableWbModes =
1412            staticInfo(ANDROID_CONTROL_AWB_AVAILABLE_MODES);
1413        for (i = 0; i < availableWbModes.count; i++) {
1414            if (validatedParams.wbMode == availableWbModes.data.u8[i]) break;
1415        }
1416        if (i == availableWbModes.count) {
1417            ALOGE("%s: Requested white balance mode %s is not supported",
1418                    __FUNCTION__,
1419                    newParams.get(CameraParameters::KEY_WHITE_BALANCE));
1420            return BAD_VALUE;
1421        }
1422        // Update in case of override
1423        newParams.set(CameraParameters::KEY_WHITE_BALANCE,
1424                wbModeEnumToString(validatedParams.wbMode));
1425    }
1426
1427    // FOCUS_MODE
1428    if (sceneModeSet) {
1429        validatedParams.focusMode =
1430                fastInfo.sceneModeOverrides.
1431                        valueFor(validatedParams.sceneMode).focusMode;
1432    } else {
1433        validatedParams.focusMode = FOCUS_MODE_INVALID;
1434    }
1435    if (validatedParams.focusMode == FOCUS_MODE_INVALID) {
1436        validatedParams.focusMode = focusModeStringToEnum(
1437                newParams.get(CameraParameters::KEY_FOCUS_MODE) );
1438    }
1439    if (validatedParams.focusMode != focusMode) {
1440        validatedParams.currentAfTriggerId = -1;
1441        if (validatedParams.focusMode != Parameters::FOCUS_MODE_FIXED) {
1442            camera_metadata_ro_entry_t minFocusDistance =
1443                staticInfo(ANDROID_LENS_MINIMUM_FOCUS_DISTANCE);
1444            if (minFocusDistance.data.f[0] == 0) {
1445                ALOGE("%s: Requested focus mode \"%s\" is not available: "
1446                        "fixed focus lens",
1447                        __FUNCTION__,
1448                        newParams.get(CameraParameters::KEY_FOCUS_MODE));
1449                return BAD_VALUE;
1450            } else if (validatedParams.focusMode !=
1451                    Parameters::FOCUS_MODE_INFINITY) {
1452                camera_metadata_ro_entry_t availableFocusModes =
1453                    staticInfo(ANDROID_CONTROL_AF_AVAILABLE_MODES);
1454                for (i = 0; i < availableFocusModes.count; i++) {
1455                    if (validatedParams.focusMode ==
1456                            availableFocusModes.data.u8[i]) break;
1457                }
1458                if (i == availableFocusModes.count) {
1459                    ALOGE("%s: Requested focus mode \"%s\" is not supported",
1460                            __FUNCTION__,
1461                            newParams.get(CameraParameters::KEY_FOCUS_MODE));
1462                    return BAD_VALUE;
1463                }
1464            }
1465        }
1466        validatedParams.focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
1467        // Always reset shadow focus mode to avoid reverting settings
1468        validatedParams.shadowFocusMode = FOCUS_MODE_INVALID;
1469        // Update in case of override
1470        newParams.set(CameraParameters::KEY_FOCUS_MODE,
1471                focusModeEnumToString(validatedParams.focusMode));
1472    } else {
1473        validatedParams.currentAfTriggerId = currentAfTriggerId;
1474    }
1475
1476    // FOCUS_AREAS
1477    res = parseAreas(newParams.get(CameraParameters::KEY_FOCUS_AREAS),
1478            &validatedParams.focusingAreas);
1479    size_t max3aRegions =
1480        (size_t)staticInfo(ANDROID_CONTROL_MAX_REGIONS, 1, 1).data.i32[0];
1481    if (res == OK) res = validateAreas(validatedParams.focusingAreas,
1482            max3aRegions, AREA_KIND_FOCUS);
1483    if (res != OK) {
1484        ALOGE("%s: Requested focus areas are malformed: %s",
1485                __FUNCTION__, newParams.get(CameraParameters::KEY_FOCUS_AREAS));
1486        return BAD_VALUE;
1487    }
1488
1489    // EXPOSURE_COMPENSATION
1490    validatedParams.exposureCompensation =
1491        newParams.getInt(CameraParameters::KEY_EXPOSURE_COMPENSATION);
1492    camera_metadata_ro_entry_t exposureCompensationRange =
1493        staticInfo(ANDROID_CONTROL_AE_EXP_COMPENSATION_RANGE);
1494    if ((validatedParams.exposureCompensation <
1495            exposureCompensationRange.data.i32[0]) ||
1496        (validatedParams.exposureCompensation >
1497            exposureCompensationRange.data.i32[1])) {
1498        ALOGE("%s: Requested exposure compensation index is out of bounds: %d",
1499                __FUNCTION__, validatedParams.exposureCompensation);
1500        return BAD_VALUE;
1501    }
1502
1503    // AUTO_EXPOSURE_LOCK (always supported)
1504    validatedParams.autoExposureLock = boolFromString(
1505        newParams.get(CameraParameters::KEY_AUTO_EXPOSURE_LOCK));
1506
1507    // AUTO_WHITEBALANCE_LOCK (always supported)
1508    validatedParams.autoWhiteBalanceLock = boolFromString(
1509        newParams.get(CameraParameters::KEY_AUTO_WHITEBALANCE_LOCK));
1510
1511    // METERING_AREAS
1512    res = parseAreas(newParams.get(CameraParameters::KEY_METERING_AREAS),
1513            &validatedParams.meteringAreas);
1514    if (res == OK) {
1515        res = validateAreas(validatedParams.meteringAreas, max3aRegions,
1516                            AREA_KIND_METERING);
1517    }
1518    if (res != OK) {
1519        ALOGE("%s: Requested metering areas are malformed: %s",
1520                __FUNCTION__,
1521                newParams.get(CameraParameters::KEY_METERING_AREAS));
1522        return BAD_VALUE;
1523    }
1524
1525    // ZOOM
1526    validatedParams.zoom = newParams.getInt(CameraParameters::KEY_ZOOM);
1527    if (validatedParams.zoom < 0
1528                || validatedParams.zoom >= (int)NUM_ZOOM_STEPS) {
1529        ALOGE("%s: Requested zoom level %d is not supported",
1530                __FUNCTION__, validatedParams.zoom);
1531        return BAD_VALUE;
1532    }
1533
1534    // VIDEO_SIZE
1535    newParams.getVideoSize(&validatedParams.videoWidth,
1536            &validatedParams.videoHeight);
1537    if (validatedParams.videoWidth != videoWidth ||
1538            validatedParams.videoHeight != videoHeight) {
1539        if (state == RECORD) {
1540            ALOGE("%s: Video size cannot be updated when recording is active!",
1541                    __FUNCTION__);
1542            return BAD_VALUE;
1543        }
1544        camera_metadata_ro_entry_t availableVideoSizes =
1545            staticInfo(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES);
1546        for (i = 0; i < availableVideoSizes.count; i += 2 ) {
1547            if ((availableVideoSizes.data.i32[i] ==
1548                    validatedParams.videoWidth) &&
1549                (availableVideoSizes.data.i32[i+1] ==
1550                    validatedParams.videoHeight)) break;
1551        }
1552        if (i == availableVideoSizes.count) {
1553            ALOGE("%s: Requested video size %d x %d is not supported",
1554                    __FUNCTION__, validatedParams.videoWidth,
1555                    validatedParams.videoHeight);
1556            return BAD_VALUE;
1557        }
1558    }
1559
1560    // VIDEO_STABILIZATION
1561    validatedParams.videoStabilization = boolFromString(
1562        newParams.get(CameraParameters::KEY_VIDEO_STABILIZATION) );
1563    camera_metadata_ro_entry_t availableVideoStabilizationModes =
1564        staticInfo(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES);
1565    if (validatedParams.videoStabilization &&
1566            availableVideoStabilizationModes.count == 1) {
1567        ALOGE("%s: Video stabilization not supported", __FUNCTION__);
1568    }
1569
1570    /** Update internal parameters */
1571
1572    *this = validatedParams;
1573
1574    // Need to flatten again in case of overrides
1575    paramsFlattened = newParams.flatten();
1576    params = newParams;
1577
1578    return OK;
1579}
1580
1581status_t Parameters::updateRequest(CameraMetadata *request) const {
1582    ATRACE_CALL();
1583    status_t res;
1584
1585    uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL;
1586    res = request->update(ANDROID_REQUEST_METADATA_MODE,
1587            &metadataMode, 1);
1588    if (res != OK) return res;
1589
1590    res = request->update(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1591            previewFpsRange, 2);
1592    if (res != OK) return res;
1593
1594    uint8_t reqWbLock = autoWhiteBalanceLock ?
1595            ANDROID_CONTROL_AWB_LOCK_ON : ANDROID_CONTROL_AWB_LOCK_OFF;
1596    res = request->update(ANDROID_CONTROL_AWB_LOCK,
1597            &reqWbLock, 1);
1598
1599    res = request->update(ANDROID_CONTROL_EFFECT_MODE,
1600            &effectMode, 1);
1601    if (res != OK) return res;
1602    res = request->update(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
1603            &antibandingMode, 1);
1604    if (res != OK) return res;
1605
1606    // android.hardware.Camera requires that when face detect is enabled, the
1607    // camera is in a face-priority mode. HAL2 splits this into separate parts
1608    // (face detection statistics and face priority scene mode). Map from other
1609    // to the other.
1610    bool sceneModeActive =
1611            sceneMode != (uint8_t)ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED;
1612    uint8_t reqControlMode = ANDROID_CONTROL_AUTO;
1613    if (enableFaceDetect || sceneModeActive) {
1614        reqControlMode = ANDROID_CONTROL_USE_SCENE_MODE;
1615    }
1616    res = request->update(ANDROID_CONTROL_MODE,
1617            &reqControlMode, 1);
1618    if (res != OK) return res;
1619
1620    uint8_t reqSceneMode =
1621            sceneModeActive ? sceneMode :
1622            enableFaceDetect ? (uint8_t)ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY :
1623            (uint8_t)ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED;
1624    res = request->update(ANDROID_CONTROL_SCENE_MODE,
1625            &reqSceneMode, 1);
1626    if (res != OK) return res;
1627
1628    uint8_t reqFlashMode = ANDROID_FLASH_OFF;
1629    uint8_t reqAeMode = ANDROID_CONTROL_AE_OFF;
1630    switch (flashMode) {
1631        case Parameters::FLASH_MODE_OFF:
1632            reqAeMode = ANDROID_CONTROL_AE_ON; break;
1633        case Parameters::FLASH_MODE_AUTO:
1634            reqAeMode = ANDROID_CONTROL_AE_ON_AUTO_FLASH; break;
1635        case Parameters::FLASH_MODE_ON:
1636            reqAeMode = ANDROID_CONTROL_AE_ON_ALWAYS_FLASH; break;
1637        case Parameters::FLASH_MODE_TORCH:
1638            reqAeMode = ANDROID_CONTROL_AE_ON;
1639            reqFlashMode = ANDROID_FLASH_TORCH;
1640            break;
1641        case Parameters::FLASH_MODE_RED_EYE:
1642            reqAeMode = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE; break;
1643        default:
1644            ALOGE("%s: Camera %d: Unknown flash mode %d", __FUNCTION__,
1645                    cameraId, flashMode);
1646                return BAD_VALUE;
1647    }
1648    res = request->update(ANDROID_FLASH_MODE,
1649            &reqFlashMode, 1);
1650    if (res != OK) return res;
1651    res = request->update(ANDROID_CONTROL_AE_MODE,
1652            &reqAeMode, 1);
1653    if (res != OK) return res;
1654
1655    uint8_t reqAeLock = autoExposureLock ?
1656            ANDROID_CONTROL_AE_LOCK_ON : ANDROID_CONTROL_AE_LOCK_OFF;
1657    res = request->update(ANDROID_CONTROL_AE_LOCK,
1658            &reqAeLock, 1);
1659    if (res != OK) return res;
1660
1661    res = request->update(ANDROID_CONTROL_AWB_MODE,
1662            &wbMode, 1);
1663    if (res != OK) return res;
1664
1665    float reqFocusDistance = 0; // infinity focus in diopters
1666    uint8_t reqFocusMode = ANDROID_CONTROL_AF_OFF;
1667    switch (focusMode) {
1668        case Parameters::FOCUS_MODE_AUTO:
1669        case Parameters::FOCUS_MODE_MACRO:
1670        case Parameters::FOCUS_MODE_CONTINUOUS_VIDEO:
1671        case Parameters::FOCUS_MODE_CONTINUOUS_PICTURE:
1672        case Parameters::FOCUS_MODE_EDOF:
1673            reqFocusMode = focusMode;
1674            break;
1675        case Parameters::FOCUS_MODE_INFINITY:
1676        case Parameters::FOCUS_MODE_FIXED:
1677            reqFocusMode = ANDROID_CONTROL_AF_OFF;
1678            break;
1679        default:
1680                ALOGE("%s: Camera %d: Unknown focus mode %d", __FUNCTION__,
1681                        cameraId, focusMode);
1682                return BAD_VALUE;
1683    }
1684    res = request->update(ANDROID_LENS_FOCUS_DISTANCE,
1685            &reqFocusDistance, 1);
1686    if (res != OK) return res;
1687    res = request->update(ANDROID_CONTROL_AF_MODE,
1688            &reqFocusMode, 1);
1689    if (res != OK) return res;
1690
1691    size_t reqFocusingAreasSize = focusingAreas.size() * 5;
1692    int32_t *reqFocusingAreas = new int32_t[reqFocusingAreasSize];
1693    for (size_t i = 0; i < reqFocusingAreasSize; i += 5) {
1694        if (focusingAreas[i].weight != 0) {
1695            reqFocusingAreas[i + 0] =
1696                    normalizedXToArray(focusingAreas[i].left);
1697            reqFocusingAreas[i + 1] =
1698                    normalizedYToArray(focusingAreas[i].top);
1699            reqFocusingAreas[i + 2] =
1700                    normalizedXToArray(focusingAreas[i].right);
1701            reqFocusingAreas[i + 3] =
1702                    normalizedYToArray(focusingAreas[i].bottom);
1703        } else {
1704            reqFocusingAreas[i + 0] = 0;
1705            reqFocusingAreas[i + 1] = 0;
1706            reqFocusingAreas[i + 2] = 0;
1707            reqFocusingAreas[i + 3] = 0;
1708        }
1709        reqFocusingAreas[i + 4] = focusingAreas[i].weight;
1710    }
1711    res = request->update(ANDROID_CONTROL_AF_REGIONS,
1712            reqFocusingAreas, reqFocusingAreasSize);
1713    if (res != OK) return res;
1714    delete[] reqFocusingAreas;
1715
1716    res = request->update(ANDROID_CONTROL_AE_EXP_COMPENSATION,
1717            &exposureCompensation, 1);
1718    if (res != OK) return res;
1719
1720    size_t reqMeteringAreasSize = meteringAreas.size() * 5;
1721    int32_t *reqMeteringAreas = new int32_t[reqMeteringAreasSize];
1722    for (size_t i = 0; i < reqMeteringAreasSize; i += 5) {
1723        if (meteringAreas[i].weight != 0) {
1724            reqMeteringAreas[i + 0] =
1725                normalizedXToArray(meteringAreas[i].left);
1726            reqMeteringAreas[i + 1] =
1727                normalizedYToArray(meteringAreas[i].top);
1728            reqMeteringAreas[i + 2] =
1729                normalizedXToArray(meteringAreas[i].right);
1730            reqMeteringAreas[i + 3] =
1731                normalizedYToArray(meteringAreas[i].bottom);
1732        } else {
1733            reqMeteringAreas[i + 0] = 0;
1734            reqMeteringAreas[i + 1] = 0;
1735            reqMeteringAreas[i + 2] = 0;
1736            reqMeteringAreas[i + 3] = 0;
1737        }
1738        reqMeteringAreas[i + 4] = meteringAreas[i].weight;
1739    }
1740    res = request->update(ANDROID_CONTROL_AE_REGIONS,
1741            reqMeteringAreas, reqMeteringAreasSize);
1742    if (res != OK) return res;
1743
1744    delete[] reqMeteringAreas;
1745
1746    /* don't include jpeg thumbnail size - it's valid for
1747       it to be set to (0,0), meaning 'no thumbnail' */
1748    CropRegion crop = calculateCropRegion( (CropRegion::Outputs)(
1749            CropRegion::OUTPUT_PREVIEW     |
1750            CropRegion::OUTPUT_VIDEO       |
1751            CropRegion::OUTPUT_PICTURE    ));
1752    int32_t reqCropRegion[3] = { crop.left, crop.top, crop.width };
1753    res = request->update(ANDROID_SCALER_CROP_REGION,
1754            reqCropRegion, 3);
1755    if (res != OK) return res;
1756
1757    uint8_t reqVstabMode = videoStabilization ?
1758            ANDROID_CONTROL_VIDEO_STABILIZATION_ON :
1759            ANDROID_CONTROL_VIDEO_STABILIZATION_OFF;
1760    res = request->update(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
1761            &reqVstabMode, 1);
1762    if (res != OK) return res;
1763
1764    uint8_t reqFaceDetectMode = enableFaceDetect ?
1765            fastInfo.bestFaceDetectMode :
1766            (uint8_t)ANDROID_STATS_FACE_DETECTION_OFF;
1767    res = request->update(ANDROID_STATS_FACE_DETECT_MODE,
1768            &reqFaceDetectMode, 1);
1769    if (res != OK) return res;
1770
1771    return OK;
1772}
1773
1774status_t Parameters::updateRequestJpeg(CameraMetadata *request) const {
1775    status_t res;
1776
1777    res = request->update(ANDROID_JPEG_THUMBNAIL_SIZE,
1778            jpegThumbSize, 2);
1779    if (res != OK) return res;
1780    res = request->update(ANDROID_JPEG_THUMBNAIL_QUALITY,
1781            &jpegThumbQuality, 1);
1782    if (res != OK) return res;
1783    res = request->update(ANDROID_JPEG_QUALITY,
1784            &jpegQuality, 1);
1785    if (res != OK) return res;
1786    res = request->update(
1787            ANDROID_JPEG_ORIENTATION,
1788            &jpegRotation, 1);
1789    if (res != OK) return res;
1790
1791    if (gpsEnabled) {
1792        res = request->update(
1793                ANDROID_JPEG_GPS_COORDINATES,
1794                gpsCoordinates, 3);
1795        if (res != OK) return res;
1796        res = request->update(
1797                ANDROID_JPEG_GPS_TIMESTAMP,
1798                &gpsTimestamp, 1);
1799        if (res != OK) return res;
1800        res = request->update(
1801                ANDROID_JPEG_GPS_PROCESSING_METHOD,
1802                gpsProcessingMethod);
1803        if (res != OK) return res;
1804    } else {
1805        res = request->erase(ANDROID_JPEG_GPS_COORDINATES);
1806        if (res != OK) return res;
1807        res = request->erase(ANDROID_JPEG_GPS_TIMESTAMP);
1808        if (res != OK) return res;
1809        res = request->erase(ANDROID_JPEG_GPS_PROCESSING_METHOD);
1810        if (res != OK) return res;
1811    }
1812    return OK;
1813}
1814
1815
1816const char* Parameters::getStateName(State state) {
1817#define CASE_ENUM_TO_CHAR(x) case x: return(#x); break;
1818    switch(state) {
1819        CASE_ENUM_TO_CHAR(DISCONNECTED)
1820        CASE_ENUM_TO_CHAR(STOPPED)
1821        CASE_ENUM_TO_CHAR(WAITING_FOR_PREVIEW_WINDOW)
1822        CASE_ENUM_TO_CHAR(PREVIEW)
1823        CASE_ENUM_TO_CHAR(RECORD)
1824        CASE_ENUM_TO_CHAR(STILL_CAPTURE)
1825        CASE_ENUM_TO_CHAR(VIDEO_SNAPSHOT)
1826        default:
1827            return "Unknown state!";
1828            break;
1829    }
1830#undef CASE_ENUM_TO_CHAR
1831}
1832
1833int Parameters::formatStringToEnum(const char *format) {
1834    return
1835        !format ?
1836            HAL_PIXEL_FORMAT_YCrCb_420_SP :
1837        !strcmp(format, CameraParameters::PIXEL_FORMAT_YUV422SP) ?
1838            HAL_PIXEL_FORMAT_YCbCr_422_SP : // NV16
1839        !strcmp(format, CameraParameters::PIXEL_FORMAT_YUV420SP) ?
1840            HAL_PIXEL_FORMAT_YCrCb_420_SP : // NV21
1841        !strcmp(format, CameraParameters::PIXEL_FORMAT_YUV422I) ?
1842            HAL_PIXEL_FORMAT_YCbCr_422_I :  // YUY2
1843        !strcmp(format, CameraParameters::PIXEL_FORMAT_YUV420P) ?
1844            HAL_PIXEL_FORMAT_YV12 :         // YV12
1845        !strcmp(format, CameraParameters::PIXEL_FORMAT_RGB565) ?
1846            HAL_PIXEL_FORMAT_RGB_565 :      // RGB565
1847        !strcmp(format, CameraParameters::PIXEL_FORMAT_RGBA8888) ?
1848            HAL_PIXEL_FORMAT_RGBA_8888 :    // RGB8888
1849        !strcmp(format, CameraParameters::PIXEL_FORMAT_BAYER_RGGB) ?
1850            HAL_PIXEL_FORMAT_RAW_SENSOR :   // Raw sensor data
1851        -1;
1852}
1853
1854const char* Parameters::formatEnumToString(int format) {
1855    const char *fmt;
1856    switch(format) {
1857        case HAL_PIXEL_FORMAT_YCbCr_422_SP: // NV16
1858            fmt = CameraParameters::PIXEL_FORMAT_YUV422SP;
1859            break;
1860        case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
1861            fmt = CameraParameters::PIXEL_FORMAT_YUV420SP;
1862            break;
1863        case HAL_PIXEL_FORMAT_YCbCr_422_I: // YUY2
1864            fmt = CameraParameters::PIXEL_FORMAT_YUV422I;
1865            break;
1866        case HAL_PIXEL_FORMAT_YV12:        // YV12
1867            fmt = CameraParameters::PIXEL_FORMAT_YUV420P;
1868            break;
1869        case HAL_PIXEL_FORMAT_RGB_565:     // RGB565
1870            fmt = CameraParameters::PIXEL_FORMAT_RGB565;
1871            break;
1872        case HAL_PIXEL_FORMAT_RGBA_8888:   // RGBA8888
1873            fmt = CameraParameters::PIXEL_FORMAT_RGBA8888;
1874            break;
1875        case HAL_PIXEL_FORMAT_RAW_SENSOR:
1876            ALOGW("Raw sensor preview format requested.");
1877            fmt = CameraParameters::PIXEL_FORMAT_BAYER_RGGB;
1878            break;
1879        default:
1880            ALOGE("%s: Unknown preview format: %x",
1881                    __FUNCTION__,  format);
1882            fmt = NULL;
1883            break;
1884    }
1885    return fmt;
1886}
1887
1888int Parameters::wbModeStringToEnum(const char *wbMode) {
1889    return
1890        !wbMode ?
1891            ANDROID_CONTROL_AWB_AUTO :
1892        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_AUTO) ?
1893            ANDROID_CONTROL_AWB_AUTO :
1894        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_INCANDESCENT) ?
1895            ANDROID_CONTROL_AWB_INCANDESCENT :
1896        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_FLUORESCENT) ?
1897            ANDROID_CONTROL_AWB_FLUORESCENT :
1898        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT) ?
1899            ANDROID_CONTROL_AWB_WARM_FLUORESCENT :
1900        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_DAYLIGHT) ?
1901            ANDROID_CONTROL_AWB_DAYLIGHT :
1902        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT) ?
1903            ANDROID_CONTROL_AWB_CLOUDY_DAYLIGHT :
1904        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_TWILIGHT) ?
1905            ANDROID_CONTROL_AWB_TWILIGHT :
1906        !strcmp(wbMode, CameraParameters::WHITE_BALANCE_SHADE) ?
1907            ANDROID_CONTROL_AWB_SHADE :
1908        -1;
1909}
1910
1911const char* Parameters::wbModeEnumToString(uint8_t wbMode) {
1912    switch (wbMode) {
1913        case ANDROID_CONTROL_AWB_AUTO:
1914            return CameraParameters::WHITE_BALANCE_AUTO;
1915        case ANDROID_CONTROL_AWB_INCANDESCENT:
1916            return CameraParameters::WHITE_BALANCE_INCANDESCENT;
1917        case ANDROID_CONTROL_AWB_FLUORESCENT:
1918            return CameraParameters::WHITE_BALANCE_FLUORESCENT;
1919        case ANDROID_CONTROL_AWB_WARM_FLUORESCENT:
1920            return CameraParameters::WHITE_BALANCE_WARM_FLUORESCENT;
1921        case ANDROID_CONTROL_AWB_DAYLIGHT:
1922            return CameraParameters::WHITE_BALANCE_DAYLIGHT;
1923        case ANDROID_CONTROL_AWB_CLOUDY_DAYLIGHT:
1924            return CameraParameters::WHITE_BALANCE_CLOUDY_DAYLIGHT;
1925        case ANDROID_CONTROL_AWB_TWILIGHT:
1926            return CameraParameters::WHITE_BALANCE_TWILIGHT;
1927        case ANDROID_CONTROL_AWB_SHADE:
1928            return CameraParameters::WHITE_BALANCE_SHADE;
1929        default:
1930            ALOGE("%s: Unknown AWB mode enum: %d",
1931                    __FUNCTION__, wbMode);
1932            return "unknown";
1933    }
1934}
1935
1936int Parameters::effectModeStringToEnum(const char *effectMode) {
1937    return
1938        !effectMode ?
1939            ANDROID_CONTROL_EFFECT_OFF :
1940        !strcmp(effectMode, CameraParameters::EFFECT_NONE) ?
1941            ANDROID_CONTROL_EFFECT_OFF :
1942        !strcmp(effectMode, CameraParameters::EFFECT_MONO) ?
1943            ANDROID_CONTROL_EFFECT_MONO :
1944        !strcmp(effectMode, CameraParameters::EFFECT_NEGATIVE) ?
1945            ANDROID_CONTROL_EFFECT_NEGATIVE :
1946        !strcmp(effectMode, CameraParameters::EFFECT_SOLARIZE) ?
1947            ANDROID_CONTROL_EFFECT_SOLARIZE :
1948        !strcmp(effectMode, CameraParameters::EFFECT_SEPIA) ?
1949            ANDROID_CONTROL_EFFECT_SEPIA :
1950        !strcmp(effectMode, CameraParameters::EFFECT_POSTERIZE) ?
1951            ANDROID_CONTROL_EFFECT_POSTERIZE :
1952        !strcmp(effectMode, CameraParameters::EFFECT_WHITEBOARD) ?
1953            ANDROID_CONTROL_EFFECT_WHITEBOARD :
1954        !strcmp(effectMode, CameraParameters::EFFECT_BLACKBOARD) ?
1955            ANDROID_CONTROL_EFFECT_BLACKBOARD :
1956        !strcmp(effectMode, CameraParameters::EFFECT_AQUA) ?
1957            ANDROID_CONTROL_EFFECT_AQUA :
1958        -1;
1959}
1960
1961int Parameters::abModeStringToEnum(const char *abMode) {
1962    return
1963        !abMode ?
1964            ANDROID_CONTROL_AE_ANTIBANDING_AUTO :
1965        !strcmp(abMode, CameraParameters::ANTIBANDING_AUTO) ?
1966            ANDROID_CONTROL_AE_ANTIBANDING_AUTO :
1967        !strcmp(abMode, CameraParameters::ANTIBANDING_OFF) ?
1968            ANDROID_CONTROL_AE_ANTIBANDING_OFF :
1969        !strcmp(abMode, CameraParameters::ANTIBANDING_50HZ) ?
1970            ANDROID_CONTROL_AE_ANTIBANDING_50HZ :
1971        !strcmp(abMode, CameraParameters::ANTIBANDING_60HZ) ?
1972            ANDROID_CONTROL_AE_ANTIBANDING_60HZ :
1973        -1;
1974}
1975
1976int Parameters::sceneModeStringToEnum(const char *sceneMode) {
1977    return
1978        !sceneMode ?
1979            ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED :
1980        !strcmp(sceneMode, CameraParameters::SCENE_MODE_AUTO) ?
1981            ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED :
1982        !strcmp(sceneMode, CameraParameters::SCENE_MODE_ACTION) ?
1983            ANDROID_CONTROL_SCENE_MODE_ACTION :
1984        !strcmp(sceneMode, CameraParameters::SCENE_MODE_PORTRAIT) ?
1985            ANDROID_CONTROL_SCENE_MODE_PORTRAIT :
1986        !strcmp(sceneMode, CameraParameters::SCENE_MODE_LANDSCAPE) ?
1987            ANDROID_CONTROL_SCENE_MODE_LANDSCAPE :
1988        !strcmp(sceneMode, CameraParameters::SCENE_MODE_NIGHT) ?
1989            ANDROID_CONTROL_SCENE_MODE_NIGHT :
1990        !strcmp(sceneMode, CameraParameters::SCENE_MODE_NIGHT_PORTRAIT) ?
1991            ANDROID_CONTROL_SCENE_MODE_NIGHT_PORTRAIT :
1992        !strcmp(sceneMode, CameraParameters::SCENE_MODE_THEATRE) ?
1993            ANDROID_CONTROL_SCENE_MODE_THEATRE :
1994        !strcmp(sceneMode, CameraParameters::SCENE_MODE_BEACH) ?
1995            ANDROID_CONTROL_SCENE_MODE_BEACH :
1996        !strcmp(sceneMode, CameraParameters::SCENE_MODE_SNOW) ?
1997            ANDROID_CONTROL_SCENE_MODE_SNOW :
1998        !strcmp(sceneMode, CameraParameters::SCENE_MODE_SUNSET) ?
1999            ANDROID_CONTROL_SCENE_MODE_SUNSET :
2000        !strcmp(sceneMode, CameraParameters::SCENE_MODE_STEADYPHOTO) ?
2001            ANDROID_CONTROL_SCENE_MODE_STEADYPHOTO :
2002        !strcmp(sceneMode, CameraParameters::SCENE_MODE_FIREWORKS) ?
2003            ANDROID_CONTROL_SCENE_MODE_FIREWORKS :
2004        !strcmp(sceneMode, CameraParameters::SCENE_MODE_SPORTS) ?
2005            ANDROID_CONTROL_SCENE_MODE_SPORTS :
2006        !strcmp(sceneMode, CameraParameters::SCENE_MODE_PARTY) ?
2007            ANDROID_CONTROL_SCENE_MODE_PARTY :
2008        !strcmp(sceneMode, CameraParameters::SCENE_MODE_CANDLELIGHT) ?
2009            ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT :
2010        !strcmp(sceneMode, CameraParameters::SCENE_MODE_BARCODE) ?
2011            ANDROID_CONTROL_SCENE_MODE_BARCODE:
2012        -1;
2013}
2014
2015Parameters::Parameters::flashMode_t Parameters::flashModeStringToEnum(
2016        const char *flashMode) {
2017    return
2018        !flashMode ?
2019            Parameters::FLASH_MODE_INVALID :
2020        !strcmp(flashMode, CameraParameters::FLASH_MODE_OFF) ?
2021            Parameters::FLASH_MODE_OFF :
2022        !strcmp(flashMode, CameraParameters::FLASH_MODE_AUTO) ?
2023            Parameters::FLASH_MODE_AUTO :
2024        !strcmp(flashMode, CameraParameters::FLASH_MODE_ON) ?
2025            Parameters::FLASH_MODE_ON :
2026        !strcmp(flashMode, CameraParameters::FLASH_MODE_RED_EYE) ?
2027            Parameters::FLASH_MODE_RED_EYE :
2028        !strcmp(flashMode, CameraParameters::FLASH_MODE_TORCH) ?
2029            Parameters::FLASH_MODE_TORCH :
2030        Parameters::FLASH_MODE_INVALID;
2031}
2032
2033const char *Parameters::flashModeEnumToString(flashMode_t flashMode) {
2034    switch (flashMode) {
2035        case FLASH_MODE_OFF:
2036            return CameraParameters::FLASH_MODE_OFF;
2037        case FLASH_MODE_AUTO:
2038            return CameraParameters::FLASH_MODE_AUTO;
2039        case FLASH_MODE_ON:
2040            return CameraParameters::FLASH_MODE_ON;
2041        case FLASH_MODE_RED_EYE:
2042            return CameraParameters::FLASH_MODE_RED_EYE;
2043        case FLASH_MODE_TORCH:
2044            return CameraParameters::FLASH_MODE_TORCH;
2045        default:
2046            ALOGE("%s: Unknown flash mode enum %d",
2047                    __FUNCTION__, flashMode);
2048            return "unknown";
2049    }
2050}
2051
2052Parameters::Parameters::focusMode_t Parameters::focusModeStringToEnum(
2053        const char *focusMode) {
2054    return
2055        !focusMode ?
2056            Parameters::FOCUS_MODE_INVALID :
2057        !strcmp(focusMode, CameraParameters::FOCUS_MODE_AUTO) ?
2058            Parameters::FOCUS_MODE_AUTO :
2059        !strcmp(focusMode, CameraParameters::FOCUS_MODE_INFINITY) ?
2060            Parameters::FOCUS_MODE_INFINITY :
2061        !strcmp(focusMode, CameraParameters::FOCUS_MODE_MACRO) ?
2062            Parameters::FOCUS_MODE_MACRO :
2063        !strcmp(focusMode, CameraParameters::FOCUS_MODE_FIXED) ?
2064            Parameters::FOCUS_MODE_FIXED :
2065        !strcmp(focusMode, CameraParameters::FOCUS_MODE_EDOF) ?
2066            Parameters::FOCUS_MODE_EDOF :
2067        !strcmp(focusMode, CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO) ?
2068            Parameters::FOCUS_MODE_CONTINUOUS_VIDEO :
2069        !strcmp(focusMode, CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE) ?
2070            Parameters::FOCUS_MODE_CONTINUOUS_PICTURE :
2071        Parameters::FOCUS_MODE_INVALID;
2072}
2073
2074const char *Parameters::focusModeEnumToString(focusMode_t focusMode) {
2075    switch (focusMode) {
2076        case FOCUS_MODE_AUTO:
2077            return CameraParameters::FOCUS_MODE_AUTO;
2078        case FOCUS_MODE_MACRO:
2079            return CameraParameters::FOCUS_MODE_MACRO;
2080        case FOCUS_MODE_CONTINUOUS_VIDEO:
2081            return CameraParameters::FOCUS_MODE_CONTINUOUS_VIDEO;
2082        case FOCUS_MODE_CONTINUOUS_PICTURE:
2083            return CameraParameters::FOCUS_MODE_CONTINUOUS_PICTURE;
2084        case FOCUS_MODE_EDOF:
2085            return CameraParameters::FOCUS_MODE_EDOF;
2086        case FOCUS_MODE_INFINITY:
2087            return CameraParameters::FOCUS_MODE_INFINITY;
2088        case FOCUS_MODE_FIXED:
2089            return CameraParameters::FOCUS_MODE_FIXED;
2090        default:
2091            ALOGE("%s: Unknown focus mode enum: %d",
2092                    __FUNCTION__, focusMode);
2093            return "unknown";
2094    }
2095}
2096
2097status_t Parameters::parseAreas(const char *areasCStr,
2098        Vector<Parameters::Area> *areas) {
2099    static const size_t NUM_FIELDS = 5;
2100    areas->clear();
2101    if (areasCStr == NULL) {
2102        // If no key exists, use default (0,0,0,0,0)
2103        areas->push();
2104        return OK;
2105    }
2106    String8 areasStr(areasCStr);
2107    ssize_t areaStart = areasStr.find("(", 0) + 1;
2108    while (areaStart != 0) {
2109        const char* area = areasStr.string() + areaStart;
2110        char *numEnd;
2111        int vals[NUM_FIELDS];
2112        for (size_t i = 0; i < NUM_FIELDS; i++) {
2113            errno = 0;
2114            vals[i] = strtol(area, &numEnd, 10);
2115            if (errno || numEnd == area) return BAD_VALUE;
2116            area = numEnd + 1;
2117        }
2118        areas->push(Parameters::Area(
2119            vals[0], vals[1], vals[2], vals[3], vals[4]) );
2120        areaStart = areasStr.find("(", areaStart) + 1;
2121    }
2122    return OK;
2123}
2124
2125status_t Parameters::validateAreas(const Vector<Parameters::Area> &areas,
2126                                      size_t maxRegions,
2127                                      AreaKind areaKind) const {
2128    // Definition of valid area can be found in
2129    // include/camera/CameraParameters.h
2130    if (areas.size() == 0) return BAD_VALUE;
2131    if (areas.size() == 1) {
2132        if (areas[0].left == 0 &&
2133                areas[0].top == 0 &&
2134                areas[0].right == 0 &&
2135                areas[0].bottom == 0 &&
2136                areas[0].weight == 0) {
2137            // Single (0,0,0,0,0) entry is always valid (== driver decides)
2138            return OK;
2139        }
2140    }
2141
2142    // fixed focus can only set (0,0,0,0,0) focus area
2143    if (areaKind == AREA_KIND_FOCUS && focusMode == FOCUS_MODE_FIXED) {
2144        return BAD_VALUE;
2145    }
2146
2147    if (areas.size() > maxRegions) {
2148        ALOGE("%s: Too many areas requested: %d",
2149                __FUNCTION__, areas.size());
2150        return BAD_VALUE;
2151    }
2152
2153    for (Vector<Parameters::Area>::const_iterator a = areas.begin();
2154         a != areas.end(); a++) {
2155        if (a->weight < 1 || a->weight > 1000) return BAD_VALUE;
2156        if (a->left < -1000 || a->left > 1000) return BAD_VALUE;
2157        if (a->top < -1000 || a->top > 1000) return BAD_VALUE;
2158        if (a->right < -1000 || a->right > 1000) return BAD_VALUE;
2159        if (a->bottom < -1000 || a->bottom > 1000) return BAD_VALUE;
2160        if (a->left >= a->right) return BAD_VALUE;
2161        if (a->top >= a->bottom) return BAD_VALUE;
2162    }
2163    return OK;
2164}
2165
2166bool Parameters::boolFromString(const char *boolStr) {
2167    return !boolStr ? false :
2168        !strcmp(boolStr, CameraParameters::TRUE) ? true :
2169        false;
2170}
2171
2172int Parameters::degToTransform(int degrees, bool mirror) {
2173    if (!mirror) {
2174        if (degrees == 0) return 0;
2175        else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
2176        else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
2177        else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
2178    } else {  // Do mirror (horizontal flip)
2179        if (degrees == 0) {           // FLIP_H and ROT_0
2180            return HAL_TRANSFORM_FLIP_H;
2181        } else if (degrees == 90) {   // FLIP_H and ROT_90
2182            return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
2183        } else if (degrees == 180) {  // FLIP_H and ROT_180
2184            return HAL_TRANSFORM_FLIP_V;
2185        } else if (degrees == 270) {  // FLIP_H and ROT_270
2186            return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
2187        }
2188    }
2189    ALOGE("%s: Bad input: %d", __FUNCTION__, degrees);
2190    return -1;
2191}
2192
2193int Parameters::cropXToArray(int x) const {
2194    ALOG_ASSERT(x >= 0, "Crop-relative X coordinate = '%d' is out of bounds"
2195                         "(lower = 0)", x);
2196
2197    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2198    ALOG_ASSERT(x < previewCrop.width, "Crop-relative X coordinate = '%d' "
2199                    "is out of bounds (upper = %d)", x, previewCrop.width);
2200
2201    int ret = x + previewCrop.left;
2202
2203    ALOG_ASSERT( (ret >= 0 && ret < fastInfo.arrayWidth),
2204        "Calculated pixel array value X = '%d' is out of bounds (upper = %d)",
2205        ret, fastInfo.arrayWidth);
2206    return ret;
2207}
2208
2209int Parameters::cropYToArray(int y) const {
2210    ALOG_ASSERT(y >= 0, "Crop-relative Y coordinate = '%d' is out of bounds "
2211        "(lower = 0)", y);
2212
2213    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2214    ALOG_ASSERT(y < previewCrop.height, "Crop-relative Y coordinate = '%d' is "
2215                "out of bounds (upper = %d)", y, previewCrop.height);
2216
2217    int ret = y + previewCrop.top;
2218
2219    ALOG_ASSERT( (ret >= 0 && ret < fastInfo.arrayHeight),
2220        "Calculated pixel array value Y = '%d' is out of bounds (upper = %d)",
2221        ret, fastInfo.arrayHeight);
2222
2223    return ret;
2224
2225}
2226
2227int Parameters::normalizedXToCrop(int x) const {
2228    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2229    return (x + 1000) * (previewCrop.width - 1) / 2000;
2230}
2231
2232int Parameters::normalizedYToCrop(int y) const {
2233    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2234    return (y + 1000) * (previewCrop.height - 1) / 2000;
2235}
2236
2237int Parameters::arrayXToCrop(int x) const {
2238    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2239    return x - previewCrop.left;
2240}
2241
2242int Parameters::arrayYToCrop(int y) const {
2243    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2244    return y - previewCrop.top;
2245}
2246
2247int Parameters::cropXToNormalized(int x) const {
2248    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2249    return x * 2000 / (previewCrop.width - 1) - 1000;
2250}
2251
2252int Parameters::cropYToNormalized(int y) const {
2253    CropRegion previewCrop = calculateCropRegion(CropRegion::OUTPUT_PREVIEW);
2254    return y * 2000 / (previewCrop.height - 1) - 1000;
2255}
2256
2257int Parameters::arrayXToNormalized(int width) const {
2258    int ret = cropXToNormalized(arrayXToCrop(width));
2259
2260    ALOG_ASSERT(ret >= -1000, "Calculated normalized value out of "
2261        "lower bounds %d", ret);
2262    ALOG_ASSERT(ret <= 1000, "Calculated normalized value out of "
2263        "upper bounds %d", ret);
2264
2265    // Work-around for HAL pre-scaling the coordinates themselves
2266    if (quirks.meteringCropRegion) {
2267        return width * 2000 / (fastInfo.arrayWidth - 1) - 1000;
2268    }
2269
2270    return ret;
2271}
2272
2273int Parameters::arrayYToNormalized(int height) const {
2274    int ret = cropYToNormalized(arrayYToCrop(height));
2275
2276    ALOG_ASSERT(ret >= -1000, "Calculated normalized value out of lower bounds"
2277        " %d", ret);
2278    ALOG_ASSERT(ret <= 1000, "Calculated normalized value out of upper bounds"
2279        " %d", ret);
2280
2281    // Work-around for HAL pre-scaling the coordinates themselves
2282    if (quirks.meteringCropRegion) {
2283        return height * 2000 / (fastInfo.arrayHeight - 1) - 1000;
2284    }
2285
2286    return ret;
2287}
2288
2289int Parameters::normalizedXToArray(int x) const {
2290
2291    // Work-around for HAL pre-scaling the coordinates themselves
2292    if (quirks.meteringCropRegion) {
2293        return (x + 1000) * (fastInfo.arrayWidth - 1) / 2000;
2294    }
2295
2296    return cropXToArray(normalizedXToCrop(x));
2297}
2298
2299int Parameters::normalizedYToArray(int y) const {
2300    // Work-around for HAL pre-scaling the coordinates themselves
2301    if (quirks.meteringCropRegion) {
2302        return (y + 1000) * (fastInfo.arrayHeight - 1) / 2000;
2303    }
2304
2305    return cropYToArray(normalizedYToCrop(y));
2306}
2307
2308Parameters::CropRegion Parameters::calculateCropRegion(
2309                            Parameters::CropRegion::Outputs outputs) const {
2310
2311    float zoomLeft, zoomTop, zoomWidth, zoomHeight;
2312
2313    // Need to convert zoom index into a crop rectangle. The rectangle is
2314    // chosen to maximize its area on the sensor
2315
2316    camera_metadata_ro_entry_t maxDigitalZoom =
2317            staticInfo(ANDROID_SCALER_AVAILABLE_MAX_ZOOM);
2318    // For each zoom step by how many pixels more do we change the zoom
2319    float zoomIncrement = (maxDigitalZoom.data.f[0] - 1) /
2320            (NUM_ZOOM_STEPS-1);
2321    // The desired activeAreaWidth/cropAreaWidth ratio (or height if h>w)
2322    // via interpolating zoom step into a zoom ratio
2323    float zoomRatio = 1 + zoomIncrement * zoom;
2324    ALOG_ASSERT( (zoomRatio >= 1.f && zoomRatio <= maxDigitalZoom.data.f[0]),
2325        "Zoom ratio calculated out of bounds. Expected 1 - %f, actual: %f",
2326        maxDigitalZoom.data.f[0], zoomRatio);
2327
2328    ALOGV("Zoom maxDigital=%f, increment=%f, ratio=%f, previewWidth=%d, "
2329          "previewHeight=%d, activeWidth=%d, activeHeight=%d",
2330          maxDigitalZoom.data.f[0], zoomIncrement, zoomRatio, previewWidth,
2331          previewHeight, fastInfo.arrayWidth, fastInfo.arrayHeight);
2332
2333    /*
2334     * Assumption: On the HAL side each stream buffer calculates its crop
2335     * rectangle as follows:
2336     *   cropRect = (zoomLeft, zoomRight,
2337     *               zoomWidth, zoomHeight * zoomWidth / outputWidth);
2338     *
2339     * Note that if zoomWidth > bufferWidth, the new cropHeight > zoomHeight
2340     *      (we can then get into trouble if the cropHeight > arrayHeight).
2341     * By selecting the zoomRatio based on the smallest outputRatio, we
2342     * guarantee this will never happen.
2343     */
2344
2345    // Enumerate all possible output sizes, select the one with the smallest
2346    // aspect ratio
2347    float minOutputWidth, minOutputHeight, minOutputRatio;
2348    {
2349        float outputSizes[][2] = {
2350            { previewWidth,     previewHeight },
2351            { videoWidth,       videoHeight },
2352            { jpegThumbSize[0], jpegThumbSize[1] },
2353            { pictureWidth,     pictureHeight },
2354        };
2355
2356        minOutputWidth = outputSizes[0][0];
2357        minOutputHeight = outputSizes[0][1];
2358        minOutputRatio = minOutputWidth / minOutputHeight;
2359        for (unsigned int i = 0;
2360             i < sizeof(outputSizes) / sizeof(outputSizes[0]);
2361             ++i) {
2362
2363            // skip over outputs we don't want to consider for the crop region
2364            if ( !((1 << i) & outputs) ) {
2365                continue;
2366            }
2367
2368            float outputWidth = outputSizes[i][0];
2369            float outputHeight = outputSizes[i][1];
2370            float outputRatio = outputWidth / outputHeight;
2371
2372            if (minOutputRatio > outputRatio) {
2373                minOutputRatio = outputRatio;
2374                minOutputWidth = outputWidth;
2375                minOutputHeight = outputHeight;
2376            }
2377
2378            // and then use this output ratio instead of preview output ratio
2379            ALOGV("Enumerating output ratio %f = %f / %f, min is %f",
2380                  outputRatio, outputWidth, outputHeight, minOutputRatio);
2381        }
2382    }
2383
2384    /* Ensure that the width/height never go out of bounds
2385     * by scaling across a diffent dimension if an out-of-bounds
2386     * possibility exists.
2387     *
2388     * e.g. if the previewratio < arrayratio and e.g. zoomratio = 1.0, then by
2389     * calculating the zoomWidth from zoomHeight we'll actually get a
2390     * zoomheight > arrayheight
2391     */
2392    float arrayRatio = 1.f * fastInfo.arrayWidth / fastInfo.arrayHeight;
2393    if (minOutputRatio >= arrayRatio) {
2394        // Adjust the height based on the width
2395        zoomWidth =  fastInfo.arrayWidth / zoomRatio;
2396        zoomHeight = zoomWidth *
2397                minOutputHeight / minOutputWidth;
2398
2399    } else {
2400        // Adjust the width based on the height
2401        zoomHeight = fastInfo.arrayHeight / zoomRatio;
2402        zoomWidth = zoomHeight *
2403                minOutputWidth / minOutputHeight;
2404    }
2405    // centering the zoom area within the active area
2406    zoomLeft = (fastInfo.arrayWidth - zoomWidth) / 2;
2407    zoomTop = (fastInfo.arrayHeight - zoomHeight) / 2;
2408
2409    ALOGV("Crop region calculated (x=%d,y=%d,w=%f,h=%f) for zoom=%d",
2410        (int32_t)zoomLeft, (int32_t)zoomTop, zoomWidth, zoomHeight, this->zoom);
2411
2412
2413    CropRegion crop = { zoomLeft, zoomTop, zoomWidth, zoomHeight };
2414    return crop;
2415}
2416
2417int32_t Parameters::fpsFromRange(int32_t min, int32_t max) const {
2418    return max;
2419}
2420
2421}; // namespace camera2
2422}; // namespace android
2423