LegacyResultMapper.java revision df6242e374b81e802a38cb891477f05d3e4b3cbc
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.hardware.camera2.legacy;
18
19import android.graphics.Rect;
20import android.hardware.Camera;
21import android.hardware.Camera.Parameters;
22import android.hardware.camera2.CameraCharacteristics;
23import android.hardware.camera2.CameraMetadata;
24import android.hardware.camera2.CaptureRequest;
25import android.hardware.camera2.CaptureResult;
26import android.hardware.camera2.impl.CameraMetadataNative;
27import android.util.Log;
28import android.util.Size;
29
30import static com.android.internal.util.Preconditions.*;
31import static android.hardware.camera2.CaptureResult.*;
32
33/**
34 * Provide legacy-specific implementations of camera2 CaptureResult for legacy devices.
35 */
36public class LegacyResultMapper {
37    private static final String TAG = "LegacyResultMapper";
38    private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
39
40    /**
41     * Generate capture result metadata from the legacy camera request.
42     *
43     * @param legacyRequest a non-{@code null} legacy request containing the latest parameters
44     * @param timestamp the timestamp to use for this result in nanoseconds.
45     *
46     * @return a {@link CameraMetadataNative} object containing result metadata.
47     */
48    public static CameraMetadataNative convertResultMetadata(LegacyRequest legacyRequest,
49                                                      long timestamp) {
50        CameraCharacteristics characteristics = legacyRequest.characteristics;
51        CaptureRequest request = legacyRequest.captureRequest;
52        Size previewSize = legacyRequest.previewSize;
53        Camera.Parameters params = legacyRequest.parameters;
54
55        CameraMetadataNative result = new CameraMetadataNative();
56
57        /*
58         * control
59         */
60        // control.afState
61        if (LegacyMetadataMapper.LIE_ABOUT_AF) {
62            // TODO: Implement autofocus state machine
63            result.set(CaptureResult.CONTROL_AF_MODE, request.get(CaptureRequest.CONTROL_AF_MODE));
64        }
65
66        /*
67         * control.ae*
68         */
69        mapAe(result, /*out*/params);
70
71        // control.awbLock
72        result.set(CaptureResult.CONTROL_AWB_LOCK, params.getAutoWhiteBalanceLock());
73
74        // control.awbState
75        if (LegacyMetadataMapper.LIE_ABOUT_AWB) {
76            // Lie to pass CTS temporarily.
77            // TODO: CTS needs to be updated not to query this value
78            // for LIMITED devices unless its guaranteed to be available.
79            result.set(CaptureResult.CONTROL_AWB_STATE,
80                    CameraMetadata.CONTROL_AWB_STATE_CONVERGED);
81            // TODO: Read the awb mode from parameters instead
82            result.set(CaptureResult.CONTROL_AWB_MODE,
83                    request.get(CaptureRequest.CONTROL_AWB_MODE));
84        }
85
86        /*
87         * lens
88         */
89        // lens.focalLength
90        result.set(CaptureResult.LENS_FOCAL_LENGTH, params.getFocalLength());
91
92        /*
93         * scaler
94         */
95        mapScaler(result, characteristics, request, previewSize, params);
96
97        /*
98         * sensor
99         */
100        // sensor.timestamp
101        result.set(CaptureResult.SENSOR_TIMESTAMP, timestamp);
102
103        // TODO: Remaining result metadata tags conversions.
104        return result;
105    }
106
107    private static void mapAe(CameraMetadataNative m, /*out*/Parameters p) {
108        // control.aeAntiBandingMode
109        {
110            int antiBandingMode = LegacyMetadataMapper.convertAntiBandingModeOrDefault(
111                    p.getAntibanding());
112            m.set(CONTROL_AE_ANTIBANDING_MODE, antiBandingMode);
113        }
114
115        // control.aeMode, flash.mode
116        mapAeAndFlashMode(m, p);
117
118        // control.aeState
119        if (LegacyMetadataMapper.LIE_ABOUT_AE_STATE) {
120            // Lie to pass CTS temporarily.
121            // TODO: Implement precapture trigger, after which we can report CONVERGED ourselves
122            m.set(CONTROL_AE_STATE, CONTROL_AE_STATE_CONVERGED);
123        }
124    }
125
126
127    /** Map results for control.aeMode, flash.mode */
128    private static void mapAeAndFlashMode(CameraMetadataNative m, /*out*/Parameters p) {
129        // Default: AE mode on but flash never fires
130        int flashMode = FLASH_MODE_OFF;
131        int aeMode = CONTROL_AE_MODE_ON;
132
133        switch (p.getFlashMode()) {
134            case Parameters.FLASH_MODE_OFF:
135                break; // ok, using default
136            case Parameters.FLASH_MODE_AUTO:
137                aeMode = CONTROL_AE_MODE_ON_AUTO_FLASH;
138                break;
139            case Parameters.FLASH_MODE_ON:
140                // flashMode = SINGLE + aeMode = ON is indistinguishable from ON_ALWAYS_FLASH
141                aeMode = CONTROL_AE_MODE_ON_ALWAYS_FLASH;
142                break;
143            case Parameters.FLASH_MODE_RED_EYE:
144                aeMode = CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE;
145                break;
146            case Parameters.FLASH_MODE_TORCH:
147                flashMode = FLASH_MODE_TORCH;
148                break;
149            default:
150                Log.w(TAG, "mapAeAndFlashMode - Ignoring unknown flash mode " + p.getFlashMode());
151        }
152
153        // flash.mode
154        m.set(FLASH_MODE, flashMode);
155        // control.aeMode
156        m.set(CONTROL_AE_MODE, aeMode);
157    }
158
159    /** Map results for scaler.* */
160    private static void mapScaler(CameraMetadataNative m,
161            CameraCharacteristics characteristics,
162            CaptureRequest request,
163            Size previewSize,
164            /*out*/Parameters p) {
165        /*
166         * scaler.cropRegion
167         */
168        {
169            Rect activeArraySize = characteristics.get(
170                    CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
171            Rect activeArraySizeOnly = new Rect(
172                    /*left*/0, /*top*/0,
173                    activeArraySize.width(), activeArraySize.height());
174
175            Rect userCropRegion = request.get(CaptureRequest.SCALER_CROP_REGION);
176
177            if (userCropRegion == null) {
178                userCropRegion = activeArraySizeOnly;
179            }
180
181            Rect reportedCropRegion = new Rect();
182            Rect previewCropRegion = new Rect();
183            ParameterUtils.getClosestAvailableZoomCrop(p, activeArraySizeOnly,
184                    previewSize, userCropRegion,
185                    /*out*/reportedCropRegion, /*out*/previewCropRegion);
186
187            m.set(SCALER_CROP_REGION, reportedCropRegion);
188        }
189    }
190}
191