2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera.one.config;
19 import android.content.ContentResolver;
20 import android.hardware.camera2.CameraCharacteristics;
22 import com.android.camera.app.MemoryManager;
23 import com.android.camera.debug.Log;
24 import com.android.camera.one.config.OneCameraFeatureConfig.CaptureSupportLevel;
25 import com.android.camera.one.config.OneCameraFeatureConfig.HdrPlusSupportLevel;
26 import com.android.camera.util.ApiHelper;
27 import com.android.camera.util.GcamHelper;
28 import com.android.camera.util.GservicesHelper;
29 import com.google.common.base.Function;
30 import com.google.common.base.Optional;
33 * Creates the OneCamera feature configurations for the GoogleCamera app.
35 public class OneCameraFeatureConfigCreator {
36 private static final Log.Tag TAG = new Log.Tag("OneCamFtrCnfgCrtr");
39 * Create the default camera feature config.
41 public static OneCameraFeatureConfig createDefault(ContentResolver contentResolver,
42 MemoryManager memoryManager) {
43 // Enable CaptureModule on all L devices unless the device is
45 boolean useCaptureModule = ApiHelper.HAS_CAMERA_2_API
46 && !GservicesHelper.isCaptureModuleDisabled(contentResolver);
47 Log.i(TAG, "CaptureModule? " + useCaptureModule);
49 // HDR+ has multiple levels of support.
50 HdrPlusSupportLevel hdrPlusSupportLevel =
51 GcamHelper.determineHdrPlusSupportLevel(contentResolver, useCaptureModule);
52 return new OneCameraFeatureConfig(useCaptureModule,
53 buildCaptureModuleDetector(contentResolver),
55 memoryManager.getMaxAllowedNativeMemoryAllocation(),
56 GservicesHelper.getMaxAllowedImageReaderCount(contentResolver));
59 private static Function<CameraCharacteristics, CaptureSupportLevel> buildCaptureModuleDetector(
60 final ContentResolver contentResolver) {
61 return new Function<CameraCharacteristics, CaptureSupportLevel>() {
63 public CaptureSupportLevel apply(CameraCharacteristics characteristics) {
64 // If a capture support level override exists, use it. Otherwise
65 // dynamically check the capabilities of the current device.
66 Optional<CaptureSupportLevel> override =
67 getCaptureSupportLevelOverride(characteristics, contentResolver);
68 if (override.isPresent()) {
69 Log.i(TAG, "Camera support level override: " + override.get().name());
70 return override.get();
73 Integer supportedLevel = characteristics
74 .get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
76 // A hardware level should always be supported, so we should
77 // never have to return here. If no hardware level is supported
78 // on a LEGACY device, the LIMITED_JPEG fallback will not work.
79 if (supportedLevel == null) {
80 Log.e(TAG, "Device does not report supported hardware level.");
81 return CaptureSupportLevel.LIMITED_JPEG;
84 // LEGACY_JPEG is the ONLY mode that is supported on LEGACY
86 if (supportedLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
87 return CaptureSupportLevel.LEGACY_JPEG;
90 // No matter if L or L MR1, the N5 does not currently support
91 // ZSL due to HAL bugs. The latest one causes random preview
92 // freezes even on MR1, see b/19565931.
93 if (ApiHelper.IS_NEXUS_5) {
94 return CaptureSupportLevel.LIMITED_JPEG;
97 if (ApiHelper.IS_NEXUS_6) {
98 if (ApiHelper.isLMr1OrHigher()) {
99 // Although front-facing cameras on the N6 (and N5) are not advertised as
100 // FULL, they can do ZSL. We might want to change the check for ZSL
101 // according to b/19625916.
102 return CaptureSupportLevel.ZSL;
104 // On a non-LEGACY N6 (or N5) prior to Lollipop MR1 we fall back to
105 // LIMITED_JPEG due to HAL bugs.
106 return CaptureSupportLevel.LIMITED_JPEG;
110 // On FULL devices starting with L-MR1 we can run ZSL.
111 if (supportedLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL) {
112 return CaptureSupportLevel.ZSL;
115 // On LIMITED devices starting with L-MR1 we run a simple YUV
117 if (supportedLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED) {
118 return CaptureSupportLevel.LIMITED_YUV;
121 // We should never get here. If we do, let's fall back to a mode
122 // that should work on all non-LEGACY devices.
123 Log.e(TAG, "Unknown support level: " + supportedLevel);
124 return CaptureSupportLevel.LIMITED_JPEG;
130 * @return If an override exits, this returns the capture support hardware
131 * level that should be used on this device.
133 private static Optional<CaptureSupportLevel> getCaptureSupportLevelOverride(
134 CameraCharacteristics cameraCharacteristics, ContentResolver contentResolver) {
135 Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
136 if (facing == null) {
137 Log.e(TAG, "Camera not facing anywhere.");
138 return Optional.absent();
142 case CameraCharacteristics.LENS_FACING_BACK: {
143 int override = GservicesHelper.getCaptureSupportLevelOverrideBack(contentResolver);
144 return CaptureSupportLevel.fromFlag(override);
146 case CameraCharacteristics.LENS_FACING_FRONT: {
147 int override = GservicesHelper.getCaptureSupportLevelOverrideFront(contentResolver);
148 return CaptureSupportLevel.fromFlag(override);
151 Log.e(TAG, "Not sure where camera is facing to.");
152 return Optional.absent();