1 /*
2  * Copyright (C) 2015 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 package com.android.camera.one.config;
18 
19 import android.annotation.TargetApi;
20 import android.hardware.camera2.CameraCharacteristics;
21 import android.os.Build;
22 
23 import com.google.common.base.Function;
24 import com.google.common.base.Optional;
25 
26 import com.android.camera.one.OneCamera;
27 
28 /**
29  * Contains the logic for which Camera API and features should be enabled on the
30  * current device.
31  */
32 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
33 public class OneCameraFeatureConfig {
34 
35     /** The camera API 2 support levels for capture module. */
36     public static enum CaptureSupportLevel {
37         /**
38          * Our app maintains a YUV ringbuffer on FULL devices that support it.
39          * App-level JPEG compression. (Option 1).
40          */
41         ZSL,
42         /** This mode is required on LEGACY devices. (Option 2). */
43         LEGACY_JPEG,
44         /** Requests JPEG on LIMITED or FULL devices. (Option 3). */
45         LIMITED_JPEG,
46         /**
47          * Requests YUV images on LIMITED or FULL with app-level JPEG
48          * compression. (Option 4).
49          */
50         LIMITED_YUV;
51 
52         /** Given the GServices override flag, returns the support level. */
fromFlag(int flag)53         public static Optional<CaptureSupportLevel> fromFlag(int flag) {
54             switch (flag) {
55                 case 1:
56                     return Optional.of(ZSL);
57                 case 2:
58                     return Optional.of(LEGACY_JPEG);
59                 case 3:
60                     return Optional.of(LIMITED_JPEG);
61                 case 4:
62                     return Optional.of(LIMITED_YUV);
63                 default:
64                     return Optional.absent();
65             }
66         }
67     }
68 
69     /** The HDR+ support levels. */
70     public static enum HdrPlusSupportLevel {
71         /** No HDR+ supported. */
72         NONE,
73         /** Nexus 5 on KitKat using Camera shim. */
74         LEGACY,
75         /** Full API 2 HDR+ support. */
76         FULL
77     }
78 
79     /** Whether the capture module should be used (instead of PhotoModule). */
80     private final boolean mUseCaptureModule;
81     /** Determines the mode for regular capture on this device. */
82     private final Function<CameraCharacteristics, CaptureSupportLevel> mCaptureModeDetector;
83     /** The level of HDR+ support. */
84     private final HdrPlusSupportLevel mHdrPlusSupportLevel;
85     /**
86      * The maximum amount of memory can be consumed by all opened cameras
87      * during capture and processing, in megabytes.
88      */
89     private final int mMaxMemoryMB;
90 
91     /**
92      * The maximum number of images the camera should allocate in the image reader.
93      */
94     private final int mMaxAllowedImageReaderCount;
95 
OneCameraFeatureConfig(boolean useCaptureModule, Function<CameraCharacteristics, CaptureSupportLevel> captureModeDetector, HdrPlusSupportLevel hdrPlusSupportLevel, int maxMemoryMB, int maxAllowedImageReaderCount)96     OneCameraFeatureConfig(boolean useCaptureModule,
97             Function<CameraCharacteristics, CaptureSupportLevel> captureModeDetector,
98             HdrPlusSupportLevel hdrPlusSupportLevel,
99             int maxMemoryMB,
100             int maxAllowedImageReaderCount) {
101         mUseCaptureModule = useCaptureModule;
102         mCaptureModeDetector = captureModeDetector;
103         mHdrPlusSupportLevel = hdrPlusSupportLevel;
104         mMaxMemoryMB = maxMemoryMB;
105         mMaxAllowedImageReaderCount = maxAllowedImageReaderCount;
106     }
107 
108     /**
109      * @return Whether CaptureModule should be used for photo capture.
110      */
isUsingCaptureModule()111     public boolean isUsingCaptureModule() {
112         return mUseCaptureModule;
113     }
114 
115     /**
116      * @param characteristics the characteristics of the camera.
117      * @return Whether the camera with the given characteristics supports
118      *         app-level ZSL.
119      */
getCaptureSupportLevel(CameraCharacteristics characteristics)120     public CaptureSupportLevel getCaptureSupportLevel(CameraCharacteristics characteristics) {
121         return mCaptureModeDetector.apply(characteristics);
122     }
123 
124     /**
125      * @return The general support level for HDR+ on this device.
126      */
getHdrPlusSupportLevel(OneCamera.Facing cameraFacing)127     public HdrPlusSupportLevel getHdrPlusSupportLevel(OneCamera.Facing cameraFacing) {
128         if (cameraFacing == OneCamera.Facing.FRONT) {
129             return HdrPlusSupportLevel.NONE;
130         }
131         return mHdrPlusSupportLevel;
132     }
133 
134     /**
135      * @return The maximum amount of memory can be consumed by all opened
136      *         cameras during capture and processing, in megabytes.
137      */
getMaxMemoryMB()138     public int getMaxMemoryMB() {
139         return mMaxMemoryMB;
140     }
141 
142     /**
143      * @return The maximum number of images the camera should allocate in the
144      *         image reader.
145      */
getMaxAllowedImageReaderCount()146     public int getMaxAllowedImageReaderCount() {
147         return mMaxAllowedImageReaderCount;
148     }
149 }
150