LegacyResultMapper.java revision d25388c1556ea22821cfb05ab783efaad6652840
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        // control.aeRegions
126
127        /*
128         * TODO: Use the *resulting* crop region to calculate intersection with
129         * metering region
130         *
131         * Report the sensor-relative metering region in the result even
132         * if that's not actually the real thing (similar to how we do it
133         * for zooming)
134         */
135    }
136
137
138    /** Map results for control.aeMode, flash.mode */
139    private static void mapAeAndFlashMode(CameraMetadataNative m, /*out*/Parameters p) {
140        // Default: AE mode on but flash never fires
141        int flashMode = FLASH_MODE_OFF;
142        int aeMode = CONTROL_AE_MODE_ON;
143
144        switch (p.getFlashMode()) {
145            case Parameters.FLASH_MODE_OFF:
146                break; // ok, using default
147            case Parameters.FLASH_MODE_AUTO:
148                aeMode = CONTROL_AE_MODE_ON_AUTO_FLASH;
149                break;
150            case Parameters.FLASH_MODE_ON:
151                // flashMode = SINGLE + aeMode = ON is indistinguishable from ON_ALWAYS_FLASH
152                aeMode = CONTROL_AE_MODE_ON_ALWAYS_FLASH;
153                break;
154            case Parameters.FLASH_MODE_RED_EYE:
155                aeMode = CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE;
156                break;
157            case Parameters.FLASH_MODE_TORCH:
158                flashMode = FLASH_MODE_TORCH;
159                break;
160            default:
161                Log.w(TAG, "mapAeAndFlashMode - Ignoring unknown flash mode " + p.getFlashMode());
162        }
163
164        // flash.mode
165        m.set(FLASH_MODE, flashMode);
166        // control.aeMode
167        m.set(CONTROL_AE_MODE, aeMode);
168    }
169
170    /** Map results for scaler.* */
171    private static void mapScaler(CameraMetadataNative m,
172            CameraCharacteristics characteristics,
173            CaptureRequest request,
174            Size previewSize,
175            /*out*/Parameters p) {
176        /*
177         * scaler.cropRegion
178         */
179        {
180            Rect activeArraySize = characteristics.get(
181                    CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
182            Rect activeArraySizeOnly = new Rect(
183                    /*left*/0, /*top*/0,
184                    activeArraySize.width(), activeArraySize.height());
185
186            Rect userCropRegion = request.get(CaptureRequest.SCALER_CROP_REGION);
187
188            if (userCropRegion == null) {
189                userCropRegion = activeArraySizeOnly;
190            }
191
192            Rect reportedCropRegion = new Rect();
193            Rect previewCropRegion = new Rect();
194            ParameterUtils.getClosestAvailableZoomCrop(p, activeArraySizeOnly,
195                    previewSize, userCropRegion,
196                    /*out*/reportedCropRegion, /*out*/previewCropRegion);
197
198            m.set(SCALER_CROP_REGION, reportedCropRegion);
199        }
200    }
201}
202