1 /*
2 * Copyright (C) 2020 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 #ifndef ANDROID_SERVERS_CAMERA_SESSION_CONFIGURATION_UTILS_H
17 #define ANDROID_SERVERS_CAMERA_SESSION_CONFIGURATION_UTILS_H
18
19 #include <android/hardware/camera2/BnCameraDeviceUser.h>
20 #include <android/hardware/camera2/ICameraDeviceCallbacks.h>
21 #include <camera/camera2/OutputConfiguration.h>
22 #include <camera/camera2/SessionConfiguration.h>
23 #include <camera/camera2/SubmitInfo.h>
24 #include <camera/StringUtils.h>
25 #include <aidl/android/hardware/camera/device/ICameraDevice.h>
26 #include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
27 #include <android/hardware/camera/device/3.7/ICameraDeviceSession.h>
28
29 #include <device3/Camera3StreamInterface.h>
30 #include <utils/IPCTransport.h>
31
32 #include <set>
33 #include <stdint.h>
34
35 #include "SessionConfigurationUtilsHost.h"
36
37 // Convenience methods for constructing binder::Status objects for error returns
38
39 #define STATUS_ERROR(errorCode, errorString) \
40 binder::Status::fromServiceSpecificError(errorCode, \
41 fmt::sprintf("%s:%d: %s", __FUNCTION__, __LINE__, errorString).c_str())
42
43 #define STATUS_ERROR_FMT(errorCode, errorString, ...) \
44 binder::Status::fromServiceSpecificError(errorCode, \
45 fmt::sprintf("%s:%d: " errorString, __FUNCTION__, __LINE__, \
46 __VA_ARGS__).c_str())
47
48 namespace android {
49 namespace camera3 {
50
51 typedef enum camera_request_template {
52 CAMERA_TEMPLATE_PREVIEW = 1,
53 CAMERA_TEMPLATE_STILL_CAPTURE = 2,
54 CAMERA_TEMPLATE_VIDEO_RECORD = 3,
55 CAMERA_TEMPLATE_VIDEO_SNAPSHOT = 4,
56 CAMERA_TEMPLATE_ZERO_SHUTTER_LAG = 5,
57 CAMERA_TEMPLATE_MANUAL = 6,
58 CAMERA_TEMPLATE_COUNT,
59 CAMERA_VENDOR_TEMPLATE_START = 0x40000000
60 } camera_request_template_t;
61
62 typedef std::function<CameraMetadata (const std::string &, bool overrideForPerfClass)>
63 metadataGetter;
64
65 class StreamConfiguration {
66 public:
67 int32_t format;
68 int32_t width;
69 int32_t height;
70 int32_t isInput;
71 static void getStreamConfigurations(
72 const CameraMetadata &static_info, bool maxRes,
73 std::unordered_map<int, std::vector<StreamConfiguration>> *scm);
74 static void getStreamConfigurations(
75 const CameraMetadata &static_info, int configuration,
76 std::unordered_map<int, std::vector<StreamConfiguration>> *scm);
77 };
78
79 // Holds the default StreamConfigurationMap and Maximum resolution
80 // StreamConfigurationMap for a camera device.
81 struct StreamConfigurationPair {
82 std::unordered_map<int, std::vector<camera3::StreamConfiguration>>
83 mDefaultStreamConfigurationMap;
84 std::unordered_map<int, std::vector<camera3::StreamConfiguration>>
85 mMaximumResolutionStreamConfigurationMap;
86 };
87
88 namespace SessionConfigurationUtils {
89
90 camera3::Size getMaxJpegResolution(const CameraMetadata &metadata,
91 bool ultraHighResolution);
92
93 size_t getUHRMaxJpegBufferSize(camera3::Size uhrMaxJpegSize,
94 camera3::Size defaultMaxJpegSize, size_t defaultMaxJpegBufferSize);
95
96 int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
97
98 // Find the closest dimensions for a given format in available stream configurations with
99 // a width <= ROUNDING_WIDTH_CAP
100 bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
101 android_dataspace dataSpace, const CameraMetadata& info, bool maxResolution,
102 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
103
104 // check if format is not custom format
105 bool isPublicFormat(int32_t format);
106
107 // Create a Surface from an IGraphicBufferProducer. Returns error if
108 // IGraphicBufferProducer's property doesn't match with streamInfo
109 binder::Status createSurfaceFromGbp(
110 camera3::OutputStreamInfo& streamInfo, bool isStreamInfoValid,
111 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp,
112 const std::string &logicalCameraId, const CameraMetadata &physicalCameraMetadata,
113 const std::vector<int32_t> &sensorPixelModesUsed, int64_t dynamicRangeProfile,
114 int64_t streamUseCase, int timestampBase, int mirrorMode,
115 int32_t colorSpace, bool respectSurfaceSize);
116
117 //check if format is 10-bit output compatible
118 bool is10bitCompatibleFormat(int32_t format, android_dataspace_t dataSpace);
119
120 // check if the dynamic range requires 10-bit output
121 bool is10bitDynamicRangeProfile(int64_t dynamicRangeProfile);
122
123 // Check if the device supports a given dynamicRangeProfile
124 bool isDynamicRangeProfileSupported(int64_t dynamicRangeProfile, const CameraMetadata& staticMeta);
125
126 bool deviceReportsColorSpaces(const CameraMetadata& staticMeta);
127
128 bool isColorSpaceSupported(int32_t colorSpace, int32_t format, android_dataspace dataSpace,
129 int64_t dynamicRangeProfile, const CameraMetadata& staticMeta);
130
131 bool dataSpaceFromColorSpace(android_dataspace *dataSpace, int32_t colorSpace);
132
133 bool isStreamUseCaseSupported(int64_t streamUseCase, const CameraMetadata &deviceInfo);
134
135 void mapStreamInfo(const OutputStreamInfo &streamInfo,
136 camera3::camera_stream_rotation_t rotation, const std::string &physicalId,
137 int32_t groupId, aidl::android::hardware::camera::device::Stream *stream /*out*/);
138
139 // Check that the physicalCameraId passed in is spported by the camera
140 // device.
141 binder::Status checkPhysicalCameraId(
142 const std::vector<std::string> &physicalCameraIds, const std::string &physicalCameraId,
143 const std::string &logicalCameraId);
144
145 binder::Status checkSurfaceType(size_t numBufferProducers,
146 bool deferredConsumer, int surfaceType, bool isConfigurationComplete);
147
148 binder::Status checkOperatingMode(int operatingMode,
149 const CameraMetadata &staticInfo, const std::string &cameraId);
150
151 binder::Status
152 convertToHALStreamCombination(
153 const SessionConfiguration& sessionConfiguration,
154 const std::string &logicalCameraId, const CameraMetadata &deviceInfo,
155 bool isCompositeJpegRDisabled, metadataGetter getMetadata,
156 const std::vector<std::string> &physicalCameraIds,
157 aidl::android::hardware::camera::device::StreamConfiguration &streamConfiguration,
158 bool overrideForPerfClass, metadata_vendor_id_t vendorTagId,
159 bool checkSessionParams, const std::vector<int32_t>& additionalKeys,
160 bool *earlyExit);
161
162 StreamConfigurationPair getStreamConfigurationPair(const CameraMetadata &metadata);
163
164 status_t checkAndOverrideSensorPixelModesUsed(
165 const std::vector<int32_t> &sensorPixelModesUsed, int format, int width, int height,
166 const CameraMetadata &staticInfo,
167 std::unordered_set<int32_t> *overriddenSensorPixelModesUsed);
168
169 bool targetPerfClassPrimaryCamera(
170 const std::set<std::string>& perfClassPrimaryCameraIds, const std::string& cameraId,
171 int32_t targetSdkVersion);
172
173 // Utility method that maps AIDL request templates.
174 binder::Status mapRequestTemplateFromClient(const std::string& cameraId, int templateId,
175 camera_request_template_t* tempId /*out*/);
176
177 status_t mapRequestTemplateToAidl(camera_request_template_t templateId,
178 aidl::android::hardware::camera::device::RequestTemplate* tempId /*out*/);
179
180 void filterParameters(const CameraMetadata& src, const CameraMetadata& deviceInfo,
181 const std::vector<int32_t>& additionalKeys, metadata_vendor_id_t vendorTagId,
182 CameraMetadata& dst);
183
184 status_t overrideDefaultRequestKeys(CameraMetadata *request);
185
contains(std::set<T> container,T value)186 template <typename T> bool contains(std::set<T> container, T value) {
187 return container.find(value) != container.end();
188 }
189
190 constexpr int32_t MAX_SURFACES_PER_STREAM = 4;
191
192 constexpr int32_t ROUNDING_WIDTH_CAP = 1920;
193
194 constexpr int32_t SDK_VERSION_S = 31;
195 extern int32_t PERF_CLASS_LEVEL;
196 extern bool IS_PERF_CLASS;
197 constexpr int32_t PERF_CLASS_JPEG_THRESH_W = 1920;
198 constexpr int32_t PERF_CLASS_JPEG_THRESH_H = 1080;
199
200 } // SessionConfigurationUtils
201 } // camera3
202 } // android
203
204 #endif
205