ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.android.camera.one.config;
 
import android.content.ContentResolver;
import android.hardware.camera2.CameraCharacteristics;
 
import com.android.camera.app.MemoryManager;
import com.android.camera.debug.Log;
import com.android.camera.one.config.OneCameraFeatureConfig.CaptureSupportLevel;
import com.android.camera.one.config.OneCameraFeatureConfig.HdrPlusSupportLevel;
import com.android.camera.util.ApiHelper;
import com.android.camera.util.GcamHelper;
import com.android.camera.util.GservicesHelper;
import com.google.common.base.Function;
import com.google.common.base.Optional;
 
/**
 * Creates the OneCamera feature configurations for the GoogleCamera app.
 */
public class OneCameraFeatureConfigCreator {
    private static final Log.Tag TAG = new Log.Tag("OneCamFtrCnfgCrtr");
 
    /**
     * Create the default camera feature config.
     */
    public static OneCameraFeatureConfig createDefault(ContentResolver contentResolver,
            MemoryManager memoryManager) {
        // Enable CaptureModule on all M devices.
        boolean useCaptureModule = true;
        Log.i(TAG, "CaptureModule? " + useCaptureModule);
 
        // HDR+ has multiple levels of support.
        HdrPlusSupportLevel hdrPlusSupportLevel =
                GcamHelper.determineHdrPlusSupportLevel(contentResolver, useCaptureModule);
        return new OneCameraFeatureConfig(useCaptureModule,
                buildCaptureModuleDetector(contentResolver),
                hdrPlusSupportLevel,
                memoryManager.getMaxAllowedNativeMemoryAllocation(),
                GservicesHelper.getMaxAllowedImageReaderCount(contentResolver));
    }
 
    private static Function<CameraCharacteristics, CaptureSupportLevel> buildCaptureModuleDetector(
            final ContentResolver contentResolver) {
        return new Function<CameraCharacteristics, CaptureSupportLevel>() {
            @Override
            public CaptureSupportLevel apply(CameraCharacteristics characteristics) {
                // If a capture support level override exists, use it. Otherwise
                // dynamically check the capabilities of the current device.
                Optional<CaptureSupportLevel> override =
                        getCaptureSupportLevelOverride(characteristics, contentResolver);
                if (override.isPresent()) {
                    Log.i(TAG, "Camera support level override: " + override.get().name());
                    return override.get();
                }
 
                Integer supportedLevel = characteristics
                        .get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
 
                // A hardware level should always be supported, so we should
                // never have to return here. If no hardware level is supported
                // on a LEGACY device, the LIMITED_JPEG fallback will not work.
                if (supportedLevel == null) {
                    Log.e(TAG, "Device does not report supported hardware level.");
                    return CaptureSupportLevel.LIMITED_JPEG;
                }
 
                // LEGACY_JPEG is the ONLY mode that is supported on LEGACY
                // devices.
                if (supportedLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
                    return CaptureSupportLevel.LEGACY_JPEG;
                }
 
                // No matter if L or L MR1, the N5 does not currently support
                // ZSL due to HAL bugs. The latest one causes random preview
                // freezes even on MR1, see b/19565931.
                if (ApiHelper.IS_NEXUS_5) {
                    return CaptureSupportLevel.LIMITED_JPEG;
                }
 
                if (ApiHelper.IS_NEXUS_6) {
                    if (ApiHelper.isLMr1OrHigher()) {
                        // Although front-facing cameras on the N6 (and N5) are not advertised as
                        // FULL, they can do ZSL. We might want to change the check for ZSL
                        // according to b/19625916.
                        return CaptureSupportLevel.ZSL;
                    } else {
                        // On a non-LEGACY N6 (or N5) prior to Lollipop MR1 we fall back to
                        // LIMITED_JPEG due to HAL bugs.
                        return CaptureSupportLevel.LIMITED_JPEG;
                    }
                }
 
                // On FULL devices starting with L-MR1 we can run ZSL if private reprocessing
                // or YUV reprocessing is supported.
                if (supportedLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL ||
                        supportedLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_3) {
                    if (supportsReprocessing(characteristics)) {
                        return CaptureSupportLevel.ZSL;
                    } else {
                        return CaptureSupportLevel.LIMITED_YUV;
                    }
                }
 
                // On LIMITED devices starting with L-MR1 we run a simple YUV
                // capture mode.
                if (supportedLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED) {
                    return CaptureSupportLevel.LIMITED_JPEG;
                }
 
                // We should never get here. If we do, let's fall back to a mode
                // that should work on all non-LEGACY devices.
                Log.e(TAG, "Unknown support level: " + supportedLevel);
                return CaptureSupportLevel.LIMITED_JPEG;
            }
        };
    }
 
    private static boolean supportsReprocessing(CameraCharacteristics characteristics) {
        Integer maxNumInputStreams = characteristics.get(
                CameraCharacteristics.REQUEST_MAX_NUM_INPUT_STREAMS);
        if (maxNumInputStreams == null) {
            Log.e(TAG, "Camera does not have maximum number of input streams.");
            return false;
        }
        if (maxNumInputStreams == 0) {
            return false;
        }
 
        int[] capabilities = characteristics.get(
                CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);
        for (int cap : capabilities) {
            if (cap == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING ||
                    cap == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_YUV_REPROCESSING) {
                return true;
            }
        }
        return false;
    }
 
    /**
     * @return If an override exits, this returns the capture support hardware
     *         level that should be used on this device.
     */
    private static Optional<CaptureSupportLevel> getCaptureSupportLevelOverride(
            CameraCharacteristics cameraCharacteristics, ContentResolver contentResolver) {
        Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
        if (facing == null) {
            Log.e(TAG, "Camera not facing anywhere.");
            return Optional.absent();
        }
 
        switch (facing) {
            case CameraCharacteristics.LENS_FACING_BACK: {
                int override = GservicesHelper.getCaptureSupportLevelOverrideBack(contentResolver);
                return CaptureSupportLevel.fromFlag(override);
            }
            case CameraCharacteristics.LENS_FACING_FRONT: {
                int override = GservicesHelper.getCaptureSupportLevelOverrideFront(contentResolver);
                return CaptureSupportLevel.fromFlag(override);
            }
            case CameraCharacteristics.LENS_FACING_EXTERNAL: {
                int override = GservicesHelper.getCaptureSupportLevelOverrideFront(contentResolver);
                return CaptureSupportLevel.fromFlag(override);
            }
            default:
                Log.e(TAG, "Not sure where camera is facing to.");
                return Optional.absent();
        }
    }
}