1 /*
2  * Copyright (C) 2018 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 #ifndef ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_EXTCAMERADEVICE_H
18 #define ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_EXTCAMERADEVICE_H
19 
20 #include "utils/Mutex.h"
21 #include "CameraMetadata.h"
22 
23 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
24 #include <hidl/Status.h>
25 #include <hidl/MQDescriptor.h>
26 #include "ExternalCameraDeviceSession.h"
27 
28 #include <vector>
29 
30 namespace android {
31 namespace hardware {
32 namespace camera {
33 namespace device {
34 namespace V3_4 {
35 namespace implementation {
36 
37 using namespace ::android::hardware::camera::device;
38 using ::android::hardware::camera::device::V3_2::ICameraDevice;
39 using ::android::hardware::camera::device::V3_2::ICameraDeviceCallback;
40 using ::android::hardware::camera::common::V1_0::CameraResourceCost;
41 using ::android::hardware::camera::common::V1_0::TorchMode;
42 using ::android::hardware::camera::common::V1_0::Status;
43 using ::android::hardware::camera::external::common::ExternalCameraConfig;
44 using ::android::hardware::camera::external::common::Size;
45 using ::android::hardware::Return;
46 using ::android::hardware::Void;
47 using ::android::hardware::hidl_vec;
48 using ::android::hardware::hidl_string;
49 using ::android::sp;
50 
51 /*
52  * The camera device HAL implementation is opened lazily (via the open call)
53  */
54 struct ExternalCameraDevice : public virtual RefBase {
55 
56     // Called by external camera provider HAL.
57     // Provider HAL must ensure the uniqueness of CameraDevice object per cameraId, or there could
58     // be multiple CameraDevice trying to access the same physical camera.  Also, provider will have
59     // to keep track of all CameraDevice objects in order to notify CameraDevice when the underlying
60     // camera is detached.
61     ExternalCameraDevice(const std::string& cameraId, const ExternalCameraConfig& cfg);
62     virtual ~ExternalCameraDevice();
63 
64     // Retrieve the HIDL interface, split into its own class to avoid inheritance issues when
65     // dealing with minor version revs and simultaneous implementation and interface inheritance
getInterfaceExternalCameraDevice66     virtual sp<ICameraDevice> getInterface() {
67         return new TrampolineDeviceInterface_3_4(this);
68     }
69 
70     // Caller must use this method to check if CameraDevice ctor failed
71     bool isInitFailed();
72     bool isInitFailedLocked();
73 
74     /* Methods from ::android::hardware::camera::device::V3_2::ICameraDevice follow. */
75     // The following method can be called without opening the actual camera device
76     Return<void> getResourceCost(ICameraDevice::getResourceCost_cb _hidl_cb);
77 
78     Return<void> getCameraCharacteristics(
79             ICameraDevice::getCameraCharacteristics_cb _hidl_cb);
80 
81     Return<Status> setTorchMode(TorchMode);
82 
83     // Open the device HAL and also return a default capture session
84     Return<void> open(const sp<ICameraDeviceCallback>&, ICameraDevice::open_cb);
85 
86     // Forward the dump call to the opened session, or do nothing
87     Return<void> dumpState(const ::android::hardware::hidl_handle&);
88     /* End of Methods from ::android::hardware::camera::device::V3_2::ICameraDevice */
89 
90 protected:
91     // Overridden by child implementations for returning different versions of
92     // ExternalCameraDeviceSession
93     virtual sp<ExternalCameraDeviceSession> createSession(
94             const sp<ICameraDeviceCallback>&,
95             const ExternalCameraConfig& cfg,
96             const std::vector<SupportedV4L2Format>& sortedFormats,
97             const CroppingType& croppingType,
98             const common::V1_0::helper::CameraMetadata& chars,
99             const std::string& cameraId,
100             unique_fd v4l2Fd);
101 
102     // Init supported w/h/format/fps in mSupportedFormats. Caller still owns fd
103     void initSupportedFormatsLocked(int fd);
104 
105     // Calls into virtual member function. Do not use it in constructor
106     status_t initCameraCharacteristics();
107     // Init available capabilities keys
108     status_t initAvailableCapabilities(
109             ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
110     // Init non-device dependent keys
111     virtual status_t initDefaultCharsKeys(
112             ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
113     // Init camera control chars keys. Caller still owns fd
114     status_t initCameraControlsCharsKeys(int fd,
115             ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
116     // Init camera output configuration related keys.  Caller still owns fd
117     status_t initOutputCharsKeys(int fd,
118             ::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
119 
120     // Helper function for initOutputCharskeys
121     template <size_t SIZE>
122     status_t initOutputCharskeysByFormat(
123             ::android::hardware::camera::common::V1_0::helper::CameraMetadata*,
124             uint32_t fourcc, const std::array<int, SIZE>& formats,
125             int scaler_stream_config_tag,
126             int stream_configuration, int min_frame_duration, int stall_duration);
127 
128     bool calculateMinFps(::android::hardware::camera::common::V1_0::helper::CameraMetadata*);
129 
130     static void getFrameRateList(int fd, double fpsUpperBound, SupportedV4L2Format* format);
131 
132     static void updateFpsBounds(int fd, CroppingType cropType,
133             const std::vector<ExternalCameraConfig::FpsLimitation>& fpsLimits,
134             SupportedV4L2Format format,
135             std::vector<SupportedV4L2Format>& outFmts);
136 
137     // Get candidate supported formats list of input cropping type.
138     static std::vector<SupportedV4L2Format> getCandidateSupportedFormatsLocked(
139             int fd, CroppingType cropType,
140             const std::vector<ExternalCameraConfig::FpsLimitation>& fpsLimits,
141             const std::vector<ExternalCameraConfig::FpsLimitation>& depthFpsLimits,
142             const Size& minStreamSize,
143             bool depthEnabled);
144     // Trim supported format list by the cropping type. Also sort output formats by width/height
145     static void trimSupportedFormats(CroppingType cropType,
146             /*inout*/std::vector<SupportedV4L2Format>* pFmts);
147 
148     Mutex mLock;
149     bool mInitialized = false;
150     bool mInitFailed = false;
151     std::string mCameraId;
152     const ExternalCameraConfig& mCfg;
153     std::vector<SupportedV4L2Format> mSupportedFormats;
154     CroppingType mCroppingType;
155 
156     wp<ExternalCameraDeviceSession> mSession = nullptr;
157 
158     ::android::hardware::camera::common::V1_0::helper::CameraMetadata mCameraCharacteristics;
159 
160     const std::vector<int32_t> AVAILABLE_CHARACTERISTICS_KEYS_3_4 = {
161         ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
162         ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
163         ANDROID_CONTROL_AE_AVAILABLE_MODES,
164         ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
165         ANDROID_CONTROL_AE_COMPENSATION_RANGE,
166         ANDROID_CONTROL_AE_COMPENSATION_STEP,
167         ANDROID_CONTROL_AE_LOCK_AVAILABLE,
168         ANDROID_CONTROL_AF_AVAILABLE_MODES,
169         ANDROID_CONTROL_AVAILABLE_EFFECTS,
170         ANDROID_CONTROL_AVAILABLE_MODES,
171         ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
172         ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
173         ANDROID_CONTROL_AWB_AVAILABLE_MODES,
174         ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
175         ANDROID_CONTROL_MAX_REGIONS,
176         ANDROID_FLASH_INFO_AVAILABLE,
177         ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
178         ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
179         ANDROID_LENS_FACING,
180         ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
181         ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
182         ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
183         ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
184         ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
185         ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
186         ANDROID_REQUEST_PARTIAL_RESULT_COUNT,
187         ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
188         ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
189         ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
190         ANDROID_SCALER_CROPPING_TYPE,
191         ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
192         ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
193         ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
194         ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE,
195         ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
196         ANDROID_SENSOR_ORIENTATION,
197         ANDROID_SHADING_AVAILABLE_MODES,
198         ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
199         ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
200         ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
201         ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
202         ANDROID_SYNC_MAX_LATENCY};
203 
204 private:
205 
206     struct TrampolineDeviceInterface_3_4 : public ICameraDevice {
TrampolineDeviceInterface_3_4ExternalCameraDevice::TrampolineDeviceInterface_3_4207         TrampolineDeviceInterface_3_4(sp<ExternalCameraDevice> parent) :
208             mParent(parent) {}
209 
getResourceCostExternalCameraDevice::TrampolineDeviceInterface_3_4210         virtual Return<void> getResourceCost(V3_2::ICameraDevice::getResourceCost_cb _hidl_cb)
211                 override {
212             return mParent->getResourceCost(_hidl_cb);
213         }
214 
getCameraCharacteristicsExternalCameraDevice::TrampolineDeviceInterface_3_4215         virtual Return<void> getCameraCharacteristics(
216                 V3_2::ICameraDevice::getCameraCharacteristics_cb _hidl_cb) override {
217             return mParent->getCameraCharacteristics(_hidl_cb);
218         }
219 
setTorchModeExternalCameraDevice::TrampolineDeviceInterface_3_4220         virtual Return<Status> setTorchMode(TorchMode mode) override {
221             return mParent->setTorchMode(mode);
222         }
223 
openExternalCameraDevice::TrampolineDeviceInterface_3_4224         virtual Return<void> open(const sp<V3_2::ICameraDeviceCallback>& callback,
225                 V3_2::ICameraDevice::open_cb _hidl_cb) override {
226             return mParent->open(callback, _hidl_cb);
227         }
228 
dumpStateExternalCameraDevice::TrampolineDeviceInterface_3_4229         virtual Return<void> dumpState(const hidl_handle& fd) override {
230             return mParent->dumpState(fd);
231         }
232 
233     private:
234         sp<ExternalCameraDevice> mParent;
235     };
236 
237 };
238 
239 }  // namespace implementation
240 }  // namespace V3_4
241 }  // namespace device
242 }  // namespace camera
243 }  // namespace hardware
244 }  // namespace android
245 
246 #endif  // ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_EXTCAMERADEVICE_H
247