blob: d937fe96e3cbc12dd954fefd253d465a224d37b3 [file] [log] [blame]
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -08001/*
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 */
Austin Borgerea931242021-12-13 23:10:41 +000016
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070017#include <cutils/properties.h>
18
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080019#include "SessionConfigurationUtils.h"
Colin Crossb8a9dbb2020-08-27 04:12:26 +000020#include "../api2/DepthCompositeStream.h"
21#include "../api2/HeicCompositeStream.h"
Emilian Peev434248e2022-10-06 14:58:54 -070022#include "aidl/android/hardware/graphics/common/Dataspace.h"
23#include "api2/JpegRCompositeStream.h"
Emilian Peeve75a2852024-05-20 22:35:15 +000024#include "binder/Status.h"
Colin Crossb8a9dbb2020-08-27 04:12:26 +000025#include "common/CameraDeviceBase.h"
Jayant Chowdharya04055f2022-01-03 02:07:49 +000026#include "common/HalConversionsTemplated.h"
Colin Crossb8a9dbb2020-08-27 04:12:26 +000027#include "../CameraService.h"
Jayant Chowdhary35642f22022-01-08 00:39:39 +000028#include "device3/aidl/AidlCamera3Device.h"
Jayant Chowdhary22441f32021-12-26 18:35:41 -080029#include "device3/hidl/HidlCamera3Device.h"
Colin Crossb8a9dbb2020-08-27 04:12:26 +000030#include "device3/Camera3OutputStream.h"
Shuzhen Wang33c84cd2024-01-03 17:45:03 +000031#include "device3/ZoomRatioMapper.h"
Emilian Peev2295df72021-11-12 18:14:10 -080032#include "system/graphics-base-v1.1.h"
Austin Borger1c1bee02023-06-01 16:51:35 -070033#include <camera/StringUtils.h>
Emilian Peev434248e2022-10-06 14:58:54 -070034#include <ui/PublicFormat.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000035
Colin Crossb8a9dbb2020-08-27 04:12:26 +000036using android::camera3::OutputStreamInfo;
37using android::camera3::OutputStreamInfo;
38using android::hardware::camera2::ICameraDeviceUser;
Shuzhen Wang045be6c2023-10-12 10:01:10 -070039using aidl::android::hardware::camera::device::RequestTemplate;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -080040
41namespace android {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080042namespace camera3 {
43
44void StreamConfiguration::getStreamConfigurations(
45 const CameraMetadata &staticInfo, int configuration,
46 std::unordered_map<int, std::vector<StreamConfiguration>> *scm) {
47 if (scm == nullptr) {
48 ALOGE("%s: StreamConfigurationMap nullptr", __FUNCTION__);
49 return;
50 }
51 const int STREAM_FORMAT_OFFSET = 0;
52 const int STREAM_WIDTH_OFFSET = 1;
53 const int STREAM_HEIGHT_OFFSET = 2;
54 const int STREAM_IS_INPUT_OFFSET = 3;
55
56 camera_metadata_ro_entry availableStreamConfigs = staticInfo.find(configuration);
57 for (size_t i = 0; i < availableStreamConfigs.count; i += 4) {
58 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
59 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
60 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
61 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
62 StreamConfiguration sc = {format, width, height, isInput};
63 (*scm)[format].push_back(sc);
64 }
65}
66
67void StreamConfiguration::getStreamConfigurations(
68 const CameraMetadata &staticInfo, bool maxRes,
69 std::unordered_map<int, std::vector<StreamConfiguration>> *scm) {
70 int32_t scalerKey =
71 SessionConfigurationUtils::getAppropriateModeTag(
72 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, maxRes);
73
74 int32_t depthKey =
75 SessionConfigurationUtils::getAppropriateModeTag(
76 ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS, maxRes);
77
78 int32_t dynamicDepthKey =
79 SessionConfigurationUtils::getAppropriateModeTag(
Jayant Chowdhary9492b072023-06-13 18:07:42 -070080 ANDROID_DEPTH_AVAILABLE_DYNAMIC_DEPTH_STREAM_CONFIGURATIONS, maxRes);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080081
82 int32_t heicKey =
83 SessionConfigurationUtils::getAppropriateModeTag(
Jayant Chowdhary9492b072023-06-13 18:07:42 -070084 ANDROID_HEIC_AVAILABLE_HEIC_STREAM_CONFIGURATIONS, maxRes);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080085
86 getStreamConfigurations(staticInfo, scalerKey, scm);
87 getStreamConfigurations(staticInfo, depthKey, scm);
88 getStreamConfigurations(staticInfo, dynamicDepthKey, scm);
89 getStreamConfigurations(staticInfo, heicKey, scm);
90}
91
Austin Borgerea931242021-12-13 23:10:41 +000092namespace SessionConfigurationUtils {
93
94int32_t PERF_CLASS_LEVEL =
95 property_get_int32("ro.odm.build.media_performance_class", 0);
96
Shuzhen Wangf18887c2022-05-31 10:24:02 -070097bool IS_PERF_CLASS = (PERF_CLASS_LEVEL >= SDK_VERSION_S);
Austin Borgerea931242021-12-13 23:10:41 +000098
99camera3::Size getMaxJpegResolution(const CameraMetadata &metadata,
100 bool ultraHighResolution) {
101 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
102 const int STREAM_CONFIGURATION_SIZE = 4;
103 const int STREAM_FORMAT_OFFSET = 0;
104 const int STREAM_WIDTH_OFFSET = 1;
105 const int STREAM_HEIGHT_OFFSET = 2;
106 const int STREAM_IS_INPUT_OFFSET = 3;
107
108 int32_t scalerSizesTag = ultraHighResolution ?
109 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_MAXIMUM_RESOLUTION :
110 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS;
111 camera_metadata_ro_entry_t availableStreamConfigs =
112 metadata.find(scalerSizesTag);
113 if (availableStreamConfigs.count == 0 ||
114 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
115 return camera3::Size(0, 0);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800116 }
Austin Borgerea931242021-12-13 23:10:41 +0000117
118 // Get max jpeg size (area-wise).
119 for (size_t i= 0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
120 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
121 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
122 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
123 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
124 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
125 && format == HAL_PIXEL_FORMAT_BLOB &&
126 (width * height > maxJpegWidth * maxJpegHeight)) {
127 maxJpegWidth = width;
128 maxJpegHeight = height;
129 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800130 }
Austin Borgerea931242021-12-13 23:10:41 +0000131
132 return camera3::Size(maxJpegWidth, maxJpegHeight);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800133}
134
Austin Borgerea931242021-12-13 23:10:41 +0000135size_t getUHRMaxJpegBufferSize(camera3::Size uhrMaxJpegSize,
136 camera3::Size defaultMaxJpegSize, size_t defaultMaxJpegBufferSize) {
Jayant Chowdharyb2d84202022-07-11 21:46:53 +0000137 return ((float)(uhrMaxJpegSize.width * uhrMaxJpegSize.height)) /
Austin Borgerea931242021-12-13 23:10:41 +0000138 (defaultMaxJpegSize.width * defaultMaxJpegSize.height) * defaultMaxJpegBufferSize;
139}
140
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800141StreamConfigurationPair
Austin Borgerea931242021-12-13 23:10:41 +0000142getStreamConfigurationPair(const CameraMetadata &staticInfo) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800143 camera3::StreamConfigurationPair streamConfigurationPair;
144 camera3::StreamConfiguration::getStreamConfigurations(staticInfo, false,
145 &streamConfigurationPair.mDefaultStreamConfigurationMap);
146 camera3::StreamConfiguration::getStreamConfigurations(staticInfo, true,
147 &streamConfigurationPair.mMaximumResolutionStreamConfigurationMap);
148 return streamConfigurationPair;
149}
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800150
Austin Borgerea931242021-12-13 23:10:41 +0000151int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000152 int64_t d0 = x0 - x1;
153 int64_t d1 = y0 - y1;
154 return d0 * d0 + d1 * d1;
155}
156
Austin Borgerea931242021-12-13 23:10:41 +0000157bool roundBufferDimensionNearest(int32_t width, int32_t height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800158 int32_t format, android_dataspace dataSpace,
159 const CameraMetadata& info, bool maxResolution, /*out*/int32_t* outWidth,
160 /*out*/int32_t* outHeight) {
161 const int32_t depthSizesTag =
162 getAppropriateModeTag(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS,
163 maxResolution);
164 const int32_t scalerSizesTag =
165 getAppropriateModeTag(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, maxResolution);
166 const int32_t heicSizesTag =
167 getAppropriateModeTag(ANDROID_HEIC_AVAILABLE_HEIC_STREAM_CONFIGURATIONS, maxResolution);
Emilian Peevad25a072023-04-05 12:24:13 -0700168 const int32_t jpegRSizesTag = getAppropriateModeTag(
169 ANDROID_JPEGR_AVAILABLE_JPEG_R_STREAM_CONFIGURATIONS, maxResolution);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000170
Emilian Peevad25a072023-04-05 12:24:13 -0700171 bool isJpegRDataSpace = (dataSpace == static_cast<android_dataspace_t>(
172 ::aidl::android::hardware::graphics::common::Dataspace::JPEG_R));
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000173 camera_metadata_ro_entry streamConfigs =
Emilian Peevad25a072023-04-05 12:24:13 -0700174 (isJpegRDataSpace) ? info.find(jpegRSizesTag) :
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800175 (dataSpace == HAL_DATASPACE_DEPTH) ? info.find(depthSizesTag) :
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000176 (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_HEIF)) ?
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800177 info.find(heicSizesTag) :
178 info.find(scalerSizesTag);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000179
180 int32_t bestWidth = -1;
181 int32_t bestHeight = -1;
182
183 // Iterate through listed stream configurations and find the one with the smallest euclidean
184 // distance from the given dimensions for the given format.
185 for (size_t i = 0; i < streamConfigs.count; i += 4) {
186 int32_t fmt = streamConfigs.data.i32[i];
187 int32_t w = streamConfigs.data.i32[i + 1];
188 int32_t h = streamConfigs.data.i32[i + 2];
189
190 // Ignore input/output type for now
191 if (fmt == format) {
192 if (w == width && h == height) {
193 bestWidth = width;
194 bestHeight = height;
195 break;
196 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
197 SessionConfigurationUtils::euclidDistSquare(w, h, width, height) <
198 SessionConfigurationUtils::euclidDistSquare(bestWidth, bestHeight, width,
199 height))) {
200 bestWidth = w;
201 bestHeight = h;
202 }
203 }
204 }
205
206 if (bestWidth == -1) {
207 // Return false if no configurations for this format were listed
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800208 ALOGE("%s: No configurations for format %d width %d, height %d, maxResolution ? %s",
209 __FUNCTION__, format, width, height, maxResolution ? "true" : "false");
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000210 return false;
211 }
212
213 // Set the outputs to the closet width/height
214 if (outWidth != NULL) {
215 *outWidth = bestWidth;
216 }
217 if (outHeight != NULL) {
218 *outHeight = bestHeight;
219 }
220
221 // Return true if at least one configuration for this format was listed
222 return true;
223}
224
Emilian Peev2295df72021-11-12 18:14:10 -0800225//check if format is 10-bit compatible
Emilian Peev434248e2022-10-06 14:58:54 -0700226bool is10bitCompatibleFormat(int32_t format, android_dataspace_t dataSpace) {
Emilian Peev2295df72021-11-12 18:14:10 -0800227 switch(format) {
228 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
229 case HAL_PIXEL_FORMAT_YCBCR_P010:
230 return true;
Emilian Peev434248e2022-10-06 14:58:54 -0700231 case HAL_PIXEL_FORMAT_BLOB:
232 if (dataSpace == static_cast<android_dataspace_t>(
233 ::aidl::android::hardware::graphics::common::Dataspace::JPEG_R)) {
234 return true;
235 }
236
237 return false;
Emilian Peev2295df72021-11-12 18:14:10 -0800238 default:
239 return false;
240 }
241}
242
Emilian Peevc81a7592022-02-14 17:38:18 -0800243bool isDynamicRangeProfileSupported(int64_t dynamicRangeProfile, const CameraMetadata& staticInfo) {
Emilian Peev2295df72021-11-12 18:14:10 -0800244 if (dynamicRangeProfile == ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
245 // Supported by default
246 return true;
247 }
248
249 camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
250 bool is10bitDynamicRangeSupported = false;
251 for (size_t i = 0; i < entry.count; ++i) {
252 uint8_t capability = entry.data.u8[i];
253 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT) {
254 is10bitDynamicRangeSupported = true;
255 break;
256 }
257 }
258
259 if (!is10bitDynamicRangeSupported) {
260 return false;
261 }
262
263 switch (dynamicRangeProfile) {
264 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10_PLUS:
265 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10:
266 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HLG10:
267 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_OEM:
268 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_OEM_PO:
269 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_REF:
270 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_REF_PO:
271 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_OEM:
272 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_OEM_PO:
273 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_REF:
274 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_8B_HDR_REF_PO:
275 entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800276 for (size_t i = 0; i < entry.count; i += 3) {
277 if (dynamicRangeProfile == entry.data.i64[i]) {
Emilian Peev2295df72021-11-12 18:14:10 -0800278 return true;
279 }
280 }
281
282 return false;
283 default:
284 return false;
285 }
286
287 return false;
288}
289
290//check if format is 10-bit compatible
Emilian Peevc81a7592022-02-14 17:38:18 -0800291bool is10bitDynamicRangeProfile(int64_t dynamicRangeProfile) {
Emilian Peev2295df72021-11-12 18:14:10 -0800292 switch (dynamicRangeProfile) {
293 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10_PLUS:
294 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HDR10:
295 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_HLG10:
296 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_OEM:
297 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_OEM_PO:
298 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_REF:
299 case ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_DOLBY_VISION_10B_HDR_REF_PO:
300 return true;
301 default:
302 return false;
303 }
304}
305
Austin Borger9e2b27c2022-07-15 11:27:24 -0700306bool deviceReportsColorSpaces(const CameraMetadata& staticInfo) {
307 camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
308 for (size_t i = 0; i < entry.count; ++i) {
309 uint8_t capability = entry.data.u8[i];
310 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_COLOR_SPACE_PROFILES) {
311 return true;
312 }
313 }
314
315 return false;
316}
317
318bool isColorSpaceSupported(int32_t colorSpace, int32_t format, android_dataspace dataSpace,
319 int64_t dynamicRangeProfile, const CameraMetadata& staticInfo) {
320 int64_t colorSpace64 = colorSpace;
321 int64_t format64 = format;
322
323 // Translate HAL format + data space to public format
324 if (format == HAL_PIXEL_FORMAT_BLOB && dataSpace == HAL_DATASPACE_V0_JFIF) {
325 format64 = 0x100; // JPEG
326 } else if (format == HAL_PIXEL_FORMAT_BLOB
327 && dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_HEIF)) {
328 format64 = 0x48454946; // HEIC
329 } else if (format == HAL_PIXEL_FORMAT_BLOB
330 && dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_DYNAMIC_DEPTH)) {
331 format64 = 0x69656963; // DEPTH_JPEG
332 } else if (format == HAL_PIXEL_FORMAT_BLOB && dataSpace == HAL_DATASPACE_DEPTH) {
333 return false; // DEPTH_POINT_CLOUD, not applicable
334 } else if (format == HAL_PIXEL_FORMAT_Y16 && dataSpace == HAL_DATASPACE_DEPTH) {
335 return false; // DEPTH16, not applicable
336 } else if (format == HAL_PIXEL_FORMAT_RAW16 && dataSpace == HAL_DATASPACE_DEPTH) {
337 return false; // RAW_DEPTH, not applicable
338 } else if (format == HAL_PIXEL_FORMAT_RAW10 && dataSpace == HAL_DATASPACE_DEPTH) {
339 return false; // RAW_DEPTH10, not applicable
Emilian Peev434248e2022-10-06 14:58:54 -0700340 } else if (format == HAL_PIXEL_FORMAT_BLOB && dataSpace ==
341 static_cast<android_dataspace>(
342 ::aidl::android::hardware::graphics::common::Dataspace::JPEG_R)) {
343 format64 = static_cast<int64_t>(PublicFormat::JPEG_R);
Austin Borger9e2b27c2022-07-15 11:27:24 -0700344 }
345
346 camera_metadata_ro_entry_t entry =
347 staticInfo.find(ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP);
348 for (size_t i = 0; i < entry.count; i += 3) {
349 bool isFormatCompatible = (format64 == entry.data.i64[i + 1]);
350 bool isDynamicProfileCompatible =
351 (dynamicRangeProfile & entry.data.i64[i + 2]) != 0;
352
353 if (colorSpace64 == entry.data.i64[i]
354 && isFormatCompatible
355 && isDynamicProfileCompatible) {
356 return true;
357 }
358 }
359
360 ALOGE("Color space %d, image format %" PRId64 ", and dynamic range 0x%" PRIx64
361 " combination not found", colorSpace, format64, dynamicRangeProfile);
362 return false;
363}
364
Austin Borgerea931242021-12-13 23:10:41 +0000365bool isPublicFormat(int32_t format)
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000366{
367 switch(format) {
368 case HAL_PIXEL_FORMAT_RGBA_8888:
369 case HAL_PIXEL_FORMAT_RGBX_8888:
370 case HAL_PIXEL_FORMAT_RGB_888:
371 case HAL_PIXEL_FORMAT_RGB_565:
372 case HAL_PIXEL_FORMAT_BGRA_8888:
373 case HAL_PIXEL_FORMAT_YV12:
374 case HAL_PIXEL_FORMAT_Y8:
375 case HAL_PIXEL_FORMAT_Y16:
376 case HAL_PIXEL_FORMAT_RAW16:
377 case HAL_PIXEL_FORMAT_RAW10:
378 case HAL_PIXEL_FORMAT_RAW12:
379 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
380 case HAL_PIXEL_FORMAT_BLOB:
381 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
382 case HAL_PIXEL_FORMAT_YCbCr_420_888:
383 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
384 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
385 case HAL_PIXEL_FORMAT_YCbCr_422_I:
386 return true;
387 default:
388 return false;
389 }
390}
391
Austin Borger9b263b12023-03-27 17:01:33 -0700392bool dataSpaceFromColorSpace(android_dataspace *dataSpace, int32_t colorSpace) {
393 switch (colorSpace) {
394 case ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_SRGB:
395 *dataSpace = HAL_DATASPACE_V0_SRGB;
396 return true;
397 case ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_DISPLAY_P3:
398 *dataSpace = HAL_DATASPACE_DISPLAY_P3;
399 return true;
400 case ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_BT2020_HLG:
401 *(reinterpret_cast<int32_t*>(dataSpace)) = HAL_DATASPACE_BT2020_HLG;
402 return true;
403 default:
404 ALOGE("%s: Unsupported color space %d", __FUNCTION__, colorSpace);
405 return false;
406 }
407}
408
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800409bool isStreamUseCaseSupported(int64_t streamUseCase,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800410 const CameraMetadata &deviceInfo) {
411 camera_metadata_ro_entry_t availableStreamUseCases =
412 deviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES);
413
414 if (availableStreamUseCases.count == 0 &&
415 streamUseCase == ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT) {
416 return true;
417 }
Shuzhen Wangb77131a2022-04-27 15:34:33 -0700418 // Allow vendor stream use case unconditionally.
419 if (streamUseCase >= ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VENDOR_START) {
420 return true;
421 }
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800422
423 for (size_t i = 0; i < availableStreamUseCases.count; i++) {
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800424 if (availableStreamUseCases.data.i64[i] == streamUseCase) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800425 return true;
426 }
427 }
428 return false;
429}
430
Austin Borgerea931242021-12-13 23:10:41 +0000431binder::Status createSurfaceFromGbp(
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000432 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
433 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp,
Austin Borger1c1bee02023-06-01 16:51:35 -0700434 const std::string &logicalCameraId, const CameraMetadata &physicalCameraMetadata,
Emilian Peevc81a7592022-02-14 17:38:18 -0800435 const std::vector<int32_t> &sensorPixelModesUsed, int64_t dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700436 int64_t streamUseCase, int timestampBase, int mirrorMode,
Shuzhen Wang0709c282024-02-12 09:09:32 -0800437 int32_t colorSpace, bool respectSurfaceSize) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000438 // bufferProducer must be non-null
439 if (gbp == nullptr) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700440 std::string msg = fmt::sprintf("Camera %s: Surface is NULL", logicalCameraId.c_str());
441 ALOGW("%s: %s", __FUNCTION__, msg.c_str());
442 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000443 }
444 // HACK b/10949105
445 // Query consumer usage bits to set async operation mode for
446 // GLConsumer using controlledByApp parameter.
447 bool useAsync = false;
448 uint64_t consumerUsage = 0;
449 status_t err;
450 if ((err = gbp->getConsumerUsage(&consumerUsage)) != OK) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700451 std::string msg = fmt::sprintf("Camera %s: Failed to query Surface consumer usage: %s (%d)",
452 logicalCameraId.c_str(), strerror(-err), err);
453 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
454 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000455 }
456 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
457 ALOGW("%s: Camera %s with consumer usage flag: %" PRIu64 ": Forcing asynchronous mode for"
Austin Borger1c1bee02023-06-01 16:51:35 -0700458 "stream", __FUNCTION__, logicalCameraId.c_str(), consumerUsage);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000459 useAsync = true;
460 }
461
462 uint64_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
463 GRALLOC_USAGE_RENDERSCRIPT;
464 uint64_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
465 GraphicBuffer::USAGE_HW_TEXTURE |
466 GraphicBuffer::USAGE_HW_COMPOSER;
467 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
468 (consumerUsage & allowedFlags) != 0;
469
470 surface = new Surface(gbp, useAsync);
471 ANativeWindow *anw = surface.get();
472
473 int width, height, format;
474 android_dataspace dataSpace;
475 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700476 std::string msg = fmt::sprintf("Camera %s: Failed to query Surface width: %s (%d)",
477 logicalCameraId.c_str(), strerror(-err), err);
478 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
479 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000480 }
481 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700482 std::string msg = fmt::sprintf("Camera %s: Failed to query Surface height: %s (%d)",
483 logicalCameraId.c_str(), strerror(-err), err);
484 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
485 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000486 }
487 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700488 std::string msg = fmt::sprintf("Camera %s: Failed to query Surface format: %s (%d)",
489 logicalCameraId.c_str(), strerror(-err), err);
490 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
491 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000492 }
493 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
494 reinterpret_cast<int*>(&dataSpace))) != OK) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700495 std::string msg = fmt::sprintf("Camera %s: Failed to query Surface dataspace: %s (%d)",
496 logicalCameraId.c_str(), strerror(-err), err);
497 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
498 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000499 }
500
Austin Borger9b263b12023-03-27 17:01:33 -0700501 if (colorSpace != ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED &&
502 format != HAL_PIXEL_FORMAT_BLOB) {
503 if (!dataSpaceFromColorSpace(&dataSpace, colorSpace)) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700504 std::string msg = fmt::sprintf("Camera %s: color space %d not supported, failed to "
505 "convert to data space", logicalCameraId.c_str(), colorSpace);
506 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
507 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Austin Borger9b263b12023-03-27 17:01:33 -0700508 }
509 }
510
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000511 // FIXME: remove this override since the default format should be
512 // IMPLEMENTATION_DEFINED. b/9487482 & b/35317944
513 if ((format >= HAL_PIXEL_FORMAT_RGBA_8888 && format <= HAL_PIXEL_FORMAT_BGRA_8888) &&
514 ((consumerUsage & GRALLOC_USAGE_HW_MASK) &&
515 ((consumerUsage & GRALLOC_USAGE_SW_READ_MASK) == 0))) {
516 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
Austin Borger1c1bee02023-06-01 16:51:35 -0700517 __FUNCTION__, logicalCameraId.c_str(), format);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000518 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
519 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800520 std::unordered_set<int32_t> overriddenSensorPixelModes;
521 if (checkAndOverrideSensorPixelModesUsed(sensorPixelModesUsed, format, width, height,
Jayant Chowdharya80ad242023-03-17 00:49:47 +0000522 physicalCameraMetadata, &overriddenSensorPixelModes) != OK) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700523 std::string msg = fmt::sprintf("Camera %s: sensor pixel modes for stream with "
524 "format %#x are not valid",logicalCameraId.c_str(), format);
525 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
526 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800527 }
528 bool foundInMaxRes = false;
529 if (overriddenSensorPixelModes.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
530 overriddenSensorPixelModes.end()) {
531 // we can use the default stream configuration map
532 foundInMaxRes = true;
533 }
Shuzhen Wang0709c282024-02-12 09:09:32 -0800534 // Round dimensions to the nearest dimensions available for this format.
535 // Only do the rounding if the client doesn't ask to respect the surface
536 // size.
537 if (flexibleConsumer && isPublicFormat(format) && !respectSurfaceSize &&
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000538 !SessionConfigurationUtils::roundBufferDimensionNearest(width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800539 format, dataSpace, physicalCameraMetadata, foundInMaxRes, /*out*/&width,
540 /*out*/&height)) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700541 std::string msg = fmt::sprintf("Camera %s: No supported stream configurations with "
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000542 "format %#x defined, failed to create output stream",
Austin Borger1c1bee02023-06-01 16:51:35 -0700543 logicalCameraId.c_str(), format);
544 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
545 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000546 }
Emilian Peev2295df72021-11-12 18:14:10 -0800547 if (!SessionConfigurationUtils::isDynamicRangeProfileSupported(dynamicRangeProfile,
548 physicalCameraMetadata)) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700549 std::string msg = fmt::sprintf("Camera %s: Dynamic range profile 0x%" PRIx64
550 " not supported,failed to create output stream", logicalCameraId.c_str(),
Emilian Peevc81a7592022-02-14 17:38:18 -0800551 dynamicRangeProfile);
Austin Borger1c1bee02023-06-01 16:51:35 -0700552 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
553 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Emilian Peev2295df72021-11-12 18:14:10 -0800554 }
555 if (SessionConfigurationUtils::is10bitDynamicRangeProfile(dynamicRangeProfile) &&
Emilian Peev434248e2022-10-06 14:58:54 -0700556 !SessionConfigurationUtils::is10bitCompatibleFormat(format, dataSpace)) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700557 std::string msg = fmt::sprintf("Camera %s: No 10-bit supported stream configurations with "
Emilian Peevc81a7592022-02-14 17:38:18 -0800558 "format %#x defined and profile %" PRIx64 ", failed to create output stream",
Austin Borger1c1bee02023-06-01 16:51:35 -0700559 logicalCameraId.c_str(), format, dynamicRangeProfile);
560 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
561 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Emilian Peev2295df72021-11-12 18:14:10 -0800562 }
Austin Borger9e2b27c2022-07-15 11:27:24 -0700563 if (colorSpace != ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED &&
564 SessionConfigurationUtils::deviceReportsColorSpaces(physicalCameraMetadata) &&
565 !SessionConfigurationUtils::isColorSpaceSupported(colorSpace, format, dataSpace,
566 dynamicRangeProfile, physicalCameraMetadata)) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700567 std::string msg = fmt::sprintf("Camera %s: Color space %d not supported, failed to "
Austin Borger9e2b27c2022-07-15 11:27:24 -0700568 "create output stream (pixel format %d dynamic range profile %" PRId64 ")",
Austin Borger1c1bee02023-06-01 16:51:35 -0700569 logicalCameraId.c_str(), colorSpace, format, dynamicRangeProfile);
570 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
571 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Austin Borger9e2b27c2022-07-15 11:27:24 -0700572 }
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800573 if (!SessionConfigurationUtils::isStreamUseCaseSupported(streamUseCase,
574 physicalCameraMetadata)) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700575 std::string msg = fmt::sprintf("Camera %s: stream use case %" PRId64 " not supported,"
576 " failed to create output stream", logicalCameraId.c_str(), streamUseCase);
577 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
578 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800579 }
Shuzhen Wange4208922022-02-01 16:52:48 -0800580 if (timestampBase < OutputConfiguration::TIMESTAMP_BASE_DEFAULT ||
Shuzhen Wangffc4c012022-04-20 15:55:46 -0700581 timestampBase > OutputConfiguration::TIMESTAMP_BASE_MAX) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700582 std::string msg = fmt::sprintf("Camera %s: invalid timestamp base %d",
583 logicalCameraId.c_str(), timestampBase);
584 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
585 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Shuzhen Wange4208922022-02-01 16:52:48 -0800586 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800587 if (mirrorMode < OutputConfiguration::MIRROR_MODE_AUTO ||
588 mirrorMode > OutputConfiguration::MIRROR_MODE_V) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700589 std::string msg = fmt::sprintf("Camera %s: invalid mirroring mode %d",
590 logicalCameraId.c_str(), mirrorMode);
591 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
592 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800593 }
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000594
595 if (!isStreamInfoValid) {
596 streamInfo.width = width;
597 streamInfo.height = height;
598 streamInfo.format = format;
599 streamInfo.dataSpace = dataSpace;
600 streamInfo.consumerUsage = consumerUsage;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800601 streamInfo.sensorPixelModesUsed = overriddenSensorPixelModes;
Emilian Peev2295df72021-11-12 18:14:10 -0800602 streamInfo.dynamicRangeProfile = dynamicRangeProfile;
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800603 streamInfo.streamUseCase = streamUseCase;
Shuzhen Wange4208922022-02-01 16:52:48 -0800604 streamInfo.timestampBase = timestampBase;
Austin Borger9e2b27c2022-07-15 11:27:24 -0700605 streamInfo.colorSpace = colorSpace;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000606 return binder::Status::ok();
607 }
608 if (width != streamInfo.width) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700609 std::string msg = fmt::sprintf("Camera %s:Surface width doesn't match: %d vs %d",
610 logicalCameraId.c_str(), width, streamInfo.width);
611 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
612 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000613 }
614 if (height != streamInfo.height) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700615 std::string msg = fmt::sprintf("Camera %s:Surface height doesn't match: %d vs %d",
616 logicalCameraId.c_str(), height, streamInfo.height);
617 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
618 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000619 }
620 if (format != streamInfo.format) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700621 std::string msg = fmt::sprintf("Camera %s:Surface format doesn't match: %d vs %d",
622 logicalCameraId.c_str(), format, streamInfo.format);
623 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
624 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000625 }
626 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
627 if (dataSpace != streamInfo.dataSpace) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700628 std::string msg = fmt::sprintf("Camera %s:Surface dataSpace doesn't match: %d vs %d",
Henri Chataingbcb99452023-11-01 17:40:30 +0000629 logicalCameraId.c_str(), static_cast<int>(dataSpace), static_cast<int>(streamInfo.dataSpace));
Austin Borger1c1bee02023-06-01 16:51:35 -0700630 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
631 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000632 }
633 //At the native side, there isn't a way to check whether 2 surfaces come from the same
634 //surface class type. Use usage flag to approximate the comparison.
635 if (consumerUsage != streamInfo.consumerUsage) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700636 std::string msg = fmt::sprintf(
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000637 "Camera %s:Surface usage flag doesn't match %" PRIu64 " vs %" PRIu64 "",
Austin Borger1c1bee02023-06-01 16:51:35 -0700638 logicalCameraId.c_str(), consumerUsage, streamInfo.consumerUsage);
639 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
640 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000641 }
642 }
643 return binder::Status::ok();
644}
645
Austin Borgerea931242021-12-13 23:10:41 +0000646void mapStreamInfo(const OutputStreamInfo &streamInfo,
Austin Borger1c1bee02023-06-01 16:51:35 -0700647 camera3::camera_stream_rotation_t rotation, const std::string &physicalId,
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000648 int32_t groupId, aidl::android::hardware::camera::device::Stream *stream /*out*/) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000649 if (stream == nullptr) {
650 return;
651 }
652
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000653 stream->streamType = aidl::android::hardware::camera::device::StreamType::OUTPUT;
654 stream->width = streamInfo.width;
655 stream->height = streamInfo.height;
Jayant Chowdhary35642f22022-01-08 00:39:39 +0000656 stream->format = AidlCamera3Device::mapToAidlPixelFormat(streamInfo.format);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000657 auto u = streamInfo.consumerUsage;
658 camera3::Camera3OutputStream::applyZSLUsageQuirk(streamInfo.format, &u);
Jayant Chowdhary35642f22022-01-08 00:39:39 +0000659 stream->usage = AidlCamera3Device::mapToAidlConsumerUsage(u);
660 stream->dataSpace = AidlCamera3Device::mapToAidlDataspace(streamInfo.dataSpace);
Austin Borger9e2b27c2022-07-15 11:27:24 -0700661 stream->colorSpace = streamInfo.colorSpace;
Jayant Chowdhary35642f22022-01-08 00:39:39 +0000662 stream->rotation = AidlCamera3Device::mapToAidlStreamRotation(rotation);
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000663 stream->id = -1; // Invalid stream id
Austin Borger1c1bee02023-06-01 16:51:35 -0700664 stream->physicalCameraId = physicalId;
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000665 stream->bufferSize = 0;
666 stream->groupId = groupId;
667 stream->sensorPixelModesUsed.resize(streamInfo.sensorPixelModesUsed.size());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800668 size_t idx = 0;
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000669 using SensorPixelMode = aidl::android::hardware::camera::metadata::SensorPixelMode;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800670 for (auto mode : streamInfo.sensorPixelModesUsed) {
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000671 stream->sensorPixelModesUsed[idx++] =
672 static_cast<SensorPixelMode>(mode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800673 }
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000674 using DynamicRangeProfile =
675 aidl::android::hardware::camera::metadata::RequestAvailableDynamicRangeProfilesMap;
676 stream->dynamicRangeProfile = static_cast<DynamicRangeProfile>(streamInfo.dynamicRangeProfile);
677 using StreamUseCases =
678 aidl::android::hardware::camera::metadata::ScalerAvailableStreamUseCases;
679 stream->useCase = static_cast<StreamUseCases>(streamInfo.streamUseCase);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000680}
681
Emilian Peeve75a2852024-05-20 22:35:15 +0000682binder::Status mapStream(const OutputStreamInfo& streamInfo, bool isCompositeJpegRDisabled,
683 const CameraMetadata& deviceInfo, camera_stream_rotation_t rotation,
684 size_t* streamIdx/*out*/, const std::string &physicalId, int32_t groupId,
685 const std::string& logicalCameraId,
686 aidl::android::hardware::camera::device::StreamConfiguration &streamConfiguration /*out*/,
687 bool *earlyExit /*out*/) {
688 bool isDepthCompositeStream =
689 camera3::DepthCompositeStream::isDepthCompositeStreamInfo(streamInfo);
690 bool isHeicCompositeStream =
691 camera3::HeicCompositeStream::isHeicCompositeStreamInfo(streamInfo);
692 bool isJpegRCompositeStream =
693 camera3::JpegRCompositeStream::isJpegRCompositeStreamInfo(streamInfo) &&
694 !isCompositeJpegRDisabled;
695 if (isDepthCompositeStream || isHeicCompositeStream || isJpegRCompositeStream) {
696 // We need to take in to account that composite streams can have
697 // additional internal camera streams.
698 std::vector<OutputStreamInfo> compositeStreams;
699 status_t ret;
700 if (isDepthCompositeStream) {
701 // TODO: Take care of composite streams.
702 ret = camera3::DepthCompositeStream::getCompositeStreamInfo(streamInfo,
703 deviceInfo, &compositeStreams);
704 } else if (isHeicCompositeStream) {
705 ret = camera3::HeicCompositeStream::getCompositeStreamInfo(streamInfo,
706 deviceInfo, &compositeStreams);
707 } else {
708 ret = camera3::JpegRCompositeStream::getCompositeStreamInfo(streamInfo,
709 deviceInfo, &compositeStreams);
710 }
711
712 if (ret != OK) {
713 std::string msg = fmt::sprintf(
714 "Camera %s: Failed adding composite streams: %s (%d)",
715 logicalCameraId.c_str(), strerror(-ret), ret);
716 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
717 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
718 }
719
720 if (compositeStreams.size() == 0) {
721 // No internal streams means composite stream not
722 // supported.
723 *earlyExit = true;
724 return binder::Status::ok();
725 } else if (compositeStreams.size() > 1) {
726 size_t streamCount = streamConfiguration.streams.size() + compositeStreams.size() - 1;
727 streamConfiguration.streams.resize(streamCount);
728 }
729
730 for (const auto& compositeStream : compositeStreams) {
731 mapStreamInfo(compositeStream, rotation,
732 physicalId, groupId,
733 &streamConfiguration.streams[(*streamIdx)++]);
734 }
735 } else {
736 mapStreamInfo(streamInfo, rotation,
737 physicalId, groupId, &streamConfiguration.streams[(*streamIdx)++]);
738 }
739
740 return binder::Status::ok();
741}
742
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800743binder::Status
Austin Borgerea931242021-12-13 23:10:41 +0000744convertToHALStreamCombination(
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800745 const SessionConfiguration& sessionConfiguration,
Austin Borger1c1bee02023-06-01 16:51:35 -0700746 const std::string &logicalCameraId, const CameraMetadata &deviceInfo,
Emilian Peev15230142023-04-27 20:22:54 +0000747 bool isCompositeJpegRDisabled,
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800748 metadataGetter getMetadata, const std::vector<std::string> &physicalCameraIds,
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000749 aidl::android::hardware::camera::device::StreamConfiguration &streamConfiguration,
Shuzhen Wang045be6c2023-10-12 10:01:10 -0700750 bool overrideForPerfClass, metadata_vendor_id_t vendorTagId,
Shuzhen Wang33c84cd2024-01-03 17:45:03 +0000751 bool checkSessionParams, const std::vector<int32_t>& additionalKeys,
752 bool *earlyExit) {
Jayant Chowdhary35642f22022-01-08 00:39:39 +0000753 using SensorPixelMode = aidl::android::hardware::camera::metadata::SensorPixelMode;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000754 auto operatingMode = sessionConfiguration.getOperatingMode();
Austin Borger1c1bee02023-06-01 16:51:35 -0700755 binder::Status res = checkOperatingMode(operatingMode, deviceInfo,
756 logicalCameraId);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000757 if (!res.isOk()) {
758 return res;
759 }
760
761 if (earlyExit == nullptr) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700762 std::string msg("earlyExit nullptr");
763 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
764 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000765 }
766 *earlyExit = false;
Jayant Chowdhary35642f22022-01-08 00:39:39 +0000767 auto ret = AidlCamera3Device::mapToAidlStreamConfigurationMode(
Emilian Peevf4816702020-04-03 15:44:51 -0700768 static_cast<camera_stream_configuration_mode_t> (operatingMode),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000769 /*out*/ &streamConfiguration.operationMode);
770 if (ret != OK) {
Austin Borger1c1bee02023-06-01 16:51:35 -0700771 std::string msg = fmt::sprintf(
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000772 "Camera %s: Failed mapping operating mode %d requested: %s (%d)",
Austin Borger1c1bee02023-06-01 16:51:35 -0700773 logicalCameraId.c_str(), operatingMode, strerror(-ret), ret);
774 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000775 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Austin Borger1c1bee02023-06-01 16:51:35 -0700776 msg.c_str());
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000777 }
778
779 bool isInputValid = (sessionConfiguration.getInputWidth() > 0) &&
780 (sessionConfiguration.getInputHeight() > 0) &&
781 (sessionConfiguration.getInputFormat() > 0);
782 auto outputConfigs = sessionConfiguration.getOutputConfigurations();
783 size_t streamCount = outputConfigs.size();
784 streamCount = isInputValid ? streamCount + 1 : streamCount;
785 streamConfiguration.streams.resize(streamCount);
786 size_t streamIdx = 0;
787 if (isInputValid) {
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000788 std::vector<SensorPixelMode> defaultSensorPixelModes;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800789 defaultSensorPixelModes.resize(1);
790 defaultSensorPixelModes[0] =
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000791 static_cast<SensorPixelMode>(ANDROID_SENSOR_PIXEL_MODE_DEFAULT);
792 aidl::android::hardware::camera::device::Stream stream;
793 stream.id = 0;
794 stream.streamType = aidl::android::hardware::camera::device::StreamType::INPUT;
795 stream.width = static_cast<uint32_t> (sessionConfiguration.getInputWidth());
796 stream.height = static_cast<uint32_t> (sessionConfiguration.getInputHeight());
Jayant Chowdhary35642f22022-01-08 00:39:39 +0000797 stream.format =
798 AidlCamera3Device::AidlCamera3Device::mapToAidlPixelFormat(
799 sessionConfiguration.getInputFormat());
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000800 stream.usage = static_cast<aidl::android::hardware::graphics::common::BufferUsage>(0);
801 stream.dataSpace =
802 static_cast<aidl::android::hardware::graphics::common::Dataspace>(
803 HAL_DATASPACE_UNKNOWN);
804 stream.rotation = aidl::android::hardware::camera::device::StreamRotation::ROTATION_0;
805 stream.bufferSize = 0;
806 stream.groupId = -1;
807 stream.sensorPixelModesUsed = defaultSensorPixelModes;
Emilian Peev065fb0f2022-05-16 15:37:16 -0700808 using DynamicRangeProfile =
809 aidl::android::hardware::camera::metadata::RequestAvailableDynamicRangeProfilesMap;
810 stream.dynamicRangeProfile =
811 DynamicRangeProfile::ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000812 streamConfiguration.streams[streamIdx++] = stream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800813 streamConfiguration.multiResolutionInputImage =
814 sessionConfiguration.inputIsMultiResolution();
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000815 }
816
817 for (const auto &it : outputConfigs) {
818 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
819 it.getGraphicBufferProducers();
820 bool deferredConsumer = it.isDeferred();
Shuzhen Wang0709c282024-02-12 09:09:32 -0800821 bool isConfigurationComplete = it.isComplete();
Austin Borger1c1bee02023-06-01 16:51:35 -0700822 const std::string &physicalCameraId = it.getPhysicalCameraId();
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800823
Emilian Peevc81a7592022-02-14 17:38:18 -0800824 int64_t dynamicRangeProfile = it.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700825 int32_t colorSpace = it.getColorSpace();
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800826 std::vector<int32_t> sensorPixelModesUsed = it.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700827 const CameraMetadata &physicalDeviceInfo = getMetadata(physicalCameraId,
828 overrideForPerfClass);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800829 const CameraMetadata &metadataChosen =
830 physicalCameraId.size() > 0 ? physicalDeviceInfo : deviceInfo;
831
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000832 size_t numBufferProducers = bufferProducers.size();
833 bool isStreamInfoValid = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800834 int32_t groupId = it.isMultiResolution() ? it.getSurfaceSetID() : -1;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000835 OutputStreamInfo streamInfo;
836
Shuzhen Wang0709c282024-02-12 09:09:32 -0800837 res = checkSurfaceType(numBufferProducers, deferredConsumer, it.getSurfaceType(),
838 isConfigurationComplete);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000839 if (!res.isOk()) {
840 return res;
841 }
842 res = checkPhysicalCameraId(physicalCameraIds, physicalCameraId,
843 logicalCameraId);
844 if (!res.isOk()) {
845 return res;
846 }
847
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800848 int64_t streamUseCase = it.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800849 int timestampBase = it.getTimestampBase();
Shuzhen Wang0709c282024-02-12 09:09:32 -0800850 // If the configuration is a deferred consumer, or a not yet completed
851 // configuration with no buffer producers attached.
852 if (deferredConsumer || (!isConfigurationComplete && numBufferProducers == 0)) {
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000853 streamInfo.width = it.getWidth();
854 streamInfo.height = it.getHeight();
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000855 auto surfaceType = it.getSurfaceType();
Shuzhen Wang0709c282024-02-12 09:09:32 -0800856 switch (surfaceType) {
857 case OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE:
858 streamInfo.consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
859 streamInfo.format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
860 streamInfo.dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
861 break;
862 case OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW:
863 streamInfo.consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE
864 | GraphicBuffer::USAGE_HW_COMPOSER;
865 streamInfo.format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
866 streamInfo.dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
867 break;
868 case OutputConfiguration::SURFACE_TYPE_MEDIA_RECORDER:
869 case OutputConfiguration::SURFACE_TYPE_MEDIA_CODEC:
870 streamInfo.consumerUsage = GraphicBuffer::USAGE_HW_VIDEO_ENCODER;
871 streamInfo.format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
872 streamInfo.dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
873 break;
874 case OutputConfiguration::SURFACE_TYPE_IMAGE_READER:
875 streamInfo.consumerUsage = it.getUsage();
876 streamInfo.format = it.getFormat();
877 streamInfo.dataSpace = (android_dataspace)it.getDataspace();
878 break;
879 default:
880 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
881 "Invalid surface type.");
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000882 }
Emilian Peev2295df72021-11-12 18:14:10 -0800883 streamInfo.dynamicRangeProfile = it.getDynamicRangeProfile();
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800884 if (checkAndOverrideSensorPixelModesUsed(sensorPixelModesUsed,
885 streamInfo.format, streamInfo.width,
Jayant Chowdharya80ad242023-03-17 00:49:47 +0000886 streamInfo.height, metadataChosen,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800887 &streamInfo.sensorPixelModesUsed) != OK) {
888 ALOGE("%s: Deferred surface sensor pixel modes not valid",
889 __FUNCTION__);
890 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
891 "Deferred surface sensor pixel modes not valid");
892 }
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800893 streamInfo.streamUseCase = streamUseCase;
Emilian Peeve75a2852024-05-20 22:35:15 +0000894 auto status = mapStream(streamInfo, isCompositeJpegRDisabled, deviceInfo,
895 camera3::CAMERA_STREAM_ROTATION_0, &streamIdx, physicalCameraId, groupId,
896 logicalCameraId, streamConfiguration, earlyExit);
897 if (*earlyExit || !status.isOk()) {
898 return status;
899 }
900
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000901 isStreamInfoValid = true;
902
903 if (numBufferProducers == 0) {
904 continue;
905 }
906 }
907
908 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang38df0ca2024-10-09 11:33:44 -0700909 int mirrorMode = it.getMirrorMode(bufferProducer);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000910 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000911 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800912 logicalCameraId, metadataChosen, sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang0709c282024-02-12 09:09:32 -0800913 streamUseCase, timestampBase, mirrorMode, colorSpace,
914 /*respectSurfaceSize*/true);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000915
916 if (!res.isOk())
917 return res;
918
919 if (!isStreamInfoValid) {
Emilian Peeve75a2852024-05-20 22:35:15 +0000920 auto status = mapStream(streamInfo, isCompositeJpegRDisabled, deviceInfo,
921 static_cast<camera_stream_rotation_t> (it.getRotation()), &streamIdx,
922 physicalCameraId, groupId, logicalCameraId, streamConfiguration, earlyExit);
923 if (*earlyExit || !status.isOk()) {
924 return status;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000925 }
926 isStreamInfoValid = true;
927 }
928 }
929 }
Shuzhen Wang045be6c2023-10-12 10:01:10 -0700930
931 if (checkSessionParams) {
932 const CameraMetadata &deviceInfo = getMetadata(logicalCameraId,
933 /*overrideForPerfClass*/false);
934 CameraMetadata filteredParams;
935
936 filterParameters(sessionConfiguration.getSessionParameters(), deviceInfo,
Shuzhen Wang33c84cd2024-01-03 17:45:03 +0000937 additionalKeys, vendorTagId, filteredParams);
Shuzhen Wang045be6c2023-10-12 10:01:10 -0700938
939 camera_metadata_t* metadata = const_cast<camera_metadata_t*>(filteredParams.getAndLock());
940 uint8_t *metadataP = reinterpret_cast<uint8_t*>(metadata);
941 streamConfiguration.sessionParams.metadata.assign(metadataP,
942 metadataP + get_camera_metadata_size(metadata));
943 }
944
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000945 return binder::Status::ok();
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800946}
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000947
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000948binder::Status checkPhysicalCameraId(
Austin Borger1c1bee02023-06-01 16:51:35 -0700949 const std::vector<std::string> &physicalCameraIds, const std::string &physicalCameraId,
950 const std::string &logicalCameraId) {
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000951 if (physicalCameraId.size() == 0) {
952 return binder::Status::ok();
953 }
954 if (std::find(physicalCameraIds.begin(), physicalCameraIds.end(),
Austin Borger1c1bee02023-06-01 16:51:35 -0700955 physicalCameraId) == physicalCameraIds.end()) {
956 std::string msg = fmt::sprintf("Camera %s: Camera doesn't support physicalCameraId %s.",
957 logicalCameraId.c_str(), physicalCameraId.c_str());
958 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
959 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000960 }
961 return binder::Status::ok();
962}
963
964binder::Status checkSurfaceType(size_t numBufferProducers,
Shuzhen Wang0709c282024-02-12 09:09:32 -0800965 bool deferredConsumer, int surfaceType, bool isConfigurationComplete) {
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000966 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
967 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
968 __FUNCTION__, numBufferProducers, MAX_SURFACES_PER_STREAM);
969 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
Shuzhen Wang0709c282024-02-12 09:09:32 -0800970 } else if ((numBufferProducers == 0) && (!deferredConsumer) && isConfigurationComplete) {
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000971 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
972 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "No valid consumers.");
973 }
974
Shuzhen Wang0709c282024-02-12 09:09:32 -0800975 if (deferredConsumer) {
976 bool validSurfaceType = (
977 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
978 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
979 if (!validSurfaceType) {
980 std::string msg = fmt::sprintf("Deferred target surface has invalid "
981 "surfaceType = %d.", surfaceType);
982 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
983 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
984 }
985 } else if (!isConfigurationComplete && numBufferProducers == 0) {
986 bool validSurfaceType = (
987 (surfaceType == OutputConfiguration::SURFACE_TYPE_MEDIA_RECORDER) ||
988 (surfaceType == OutputConfiguration::SURFACE_TYPE_MEDIA_CODEC) ||
989 (surfaceType == OutputConfiguration::SURFACE_TYPE_IMAGE_READER));
990 if (!validSurfaceType) {
991 std::string msg = fmt::sprintf("OutputConfiguration target surface has invalid "
992 "surfaceType = %d.", surfaceType);
993 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
994 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
995 }
Jayant Chowdharya04055f2022-01-03 02:07:49 +0000996 }
997
998 return binder::Status::ok();
999}
1000
1001binder::Status checkOperatingMode(int operatingMode,
Austin Borger1c1bee02023-06-01 16:51:35 -07001002 const CameraMetadata &staticInfo, const std::string &cameraId) {
Jayant Chowdharya04055f2022-01-03 02:07:49 +00001003 if (operatingMode < 0) {
Austin Borger1c1bee02023-06-01 16:51:35 -07001004 std::string msg = fmt::sprintf(
1005 "Camera %s: Invalid operating mode %d requested", cameraId.c_str(), operatingMode);
1006 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
Jayant Chowdharya04055f2022-01-03 02:07:49 +00001007 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Austin Borger1c1bee02023-06-01 16:51:35 -07001008 msg.c_str());
Jayant Chowdharya04055f2022-01-03 02:07:49 +00001009 }
1010
1011 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
1012 if (isConstrainedHighSpeed) {
1013 camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
1014 bool isConstrainedHighSpeedSupported = false;
1015 for(size_t i = 0; i < entry.count; ++i) {
1016 uint8_t capability = entry.data.u8[i];
1017 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
1018 isConstrainedHighSpeedSupported = true;
1019 break;
1020 }
1021 }
1022 if (!isConstrainedHighSpeedSupported) {
Austin Borger1c1bee02023-06-01 16:51:35 -07001023 std::string msg = fmt::sprintf(
Jayant Chowdharya04055f2022-01-03 02:07:49 +00001024 "Camera %s: Try to create a constrained high speed configuration on a device"
Austin Borger1c1bee02023-06-01 16:51:35 -07001025 " that doesn't support it.", cameraId.c_str());
1026 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
Jayant Chowdharya04055f2022-01-03 02:07:49 +00001027 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Austin Borger1c1bee02023-06-01 16:51:35 -07001028 msg.c_str());
Jayant Chowdharya04055f2022-01-03 02:07:49 +00001029 }
1030 }
1031
1032 return binder::Status::ok();
1033}
1034
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001035static bool inStreamConfigurationMap(int format, int width, int height,
1036 const std::unordered_map<int, std::vector<camera3::StreamConfiguration>> &sm) {
1037 auto scs = sm.find(format);
1038 if (scs == sm.end()) {
1039 return false;
1040 }
1041 for (auto &sc : scs->second) {
1042 if (sc.width == width && sc.height == height && sc.isInput == 0) {
1043 return true;
1044 }
1045 }
1046 return false;
1047}
1048
1049static std::unordered_set<int32_t> convertToSet(const std::vector<int32_t> &sensorPixelModesUsed) {
1050 return std::unordered_set<int32_t>(sensorPixelModesUsed.begin(), sensorPixelModesUsed.end());
1051}
1052
Austin Borgerea931242021-12-13 23:10:41 +00001053status_t checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001054 const std::vector<int32_t> &sensorPixelModesUsed, int format, int width, int height,
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001055 const CameraMetadata &staticInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001056 std::unordered_set<int32_t> *overriddenSensorPixelModesUsed) {
Jayant Chowdhary84df28c2021-05-26 22:32:21 -07001057
1058 const std::unordered_set<int32_t> &sensorPixelModesUsedSet =
1059 convertToSet(sensorPixelModesUsed);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08001060 if (!supportsUltraHighResolutionCapture(staticInfo)) {
Jayant Chowdhary84df28c2021-05-26 22:32:21 -07001061 if (sensorPixelModesUsedSet.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1062 sensorPixelModesUsedSet.end()) {
1063 // invalid value for non ultra high res sensors
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001064 ALOGE("%s ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION used on a device which doesn't "
1065 "support ultra high resolution capture", __FUNCTION__);
Jayant Chowdhary84df28c2021-05-26 22:32:21 -07001066 return BAD_VALUE;
1067 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001068 overriddenSensorPixelModesUsed->clear();
1069 overriddenSensorPixelModesUsed->insert(ANDROID_SENSOR_PIXEL_MODE_DEFAULT);
1070 return OK;
1071 }
1072
1073 StreamConfigurationPair streamConfigurationPair = getStreamConfigurationPair(staticInfo);
Jayant Chowdhary84df28c2021-05-26 22:32:21 -07001074
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001075 bool isInDefaultStreamConfigurationMap =
1076 inStreamConfigurationMap(format, width, height,
1077 streamConfigurationPair.mDefaultStreamConfigurationMap);
1078
1079 bool isInMaximumResolutionStreamConfigurationMap =
1080 inStreamConfigurationMap(format, width, height,
1081 streamConfigurationPair.mMaximumResolutionStreamConfigurationMap);
1082
1083 // Case 1: The client has not changed the sensor mode defaults. In this case, we check if the
1084 // size + format of the OutputConfiguration is found exclusively in 1.
1085 // If yes, add that sensorPixelMode to overriddenSensorPixelModes.
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08001086 // If no, add 'DEFAULT' and MAXIMUM_RESOLUTION to overriddenSensorPixelModes.
1087 // This maintains backwards compatibility and also tells the framework the stream
1088 // might be used in either sensor pixel mode.
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001089 if (sensorPixelModesUsedSet.size() == 0) {
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08001090 // Ambiguous case, override to include both cases.
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001091 if (isInDefaultStreamConfigurationMap && isInMaximumResolutionStreamConfigurationMap) {
1092 overriddenSensorPixelModesUsed->insert(ANDROID_SENSOR_PIXEL_MODE_DEFAULT);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08001093 overriddenSensorPixelModesUsed->insert(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001094 return OK;
1095 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001096 if (isInMaximumResolutionStreamConfigurationMap) {
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001097 overriddenSensorPixelModesUsed->insert(
1098 ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION);
1099 } else {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001100 overriddenSensorPixelModesUsed->insert(ANDROID_SENSOR_PIXEL_MODE_DEFAULT);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001101 }
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001102 return OK;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001103 }
1104
1105 // Case2: The app has set sensorPixelModesUsed, we need to verify that they
1106 // are valid / err out.
1107 if (sensorPixelModesUsedSet.find(ANDROID_SENSOR_PIXEL_MODE_DEFAULT) !=
1108 sensorPixelModesUsedSet.end() && !isInDefaultStreamConfigurationMap) {
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001109 ALOGE("%s: ANDROID_SENSOR_PIXEL_MODE_DEFAULT set by client, but stream f: %d size %d x %d"
1110 " isn't present in default stream configuration map", __FUNCTION__, format, width,
1111 height);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001112 return BAD_VALUE;
1113 }
1114
1115 if (sensorPixelModesUsedSet.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1116 sensorPixelModesUsedSet.end() && !isInMaximumResolutionStreamConfigurationMap) {
Jayant Chowdharya80ad242023-03-17 00:49:47 +00001117 ALOGE("%s: ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION set by client, but stream f: "
1118 "%d size %d x %d isn't present in default stream configuration map", __FUNCTION__,
1119 format, width, height);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001120 return BAD_VALUE;
1121 }
1122 *overriddenSensorPixelModesUsed = sensorPixelModesUsedSet;
1123 return OK;
1124}
1125
Austin Borgerea931242021-12-13 23:10:41 +00001126bool targetPerfClassPrimaryCamera(
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001127 const std::set<std::string>& perfClassPrimaryCameraIds, const std::string& cameraId,
1128 int targetSdkVersion) {
1129 bool isPerfClassPrimaryCamera =
1130 perfClassPrimaryCameraIds.find(cameraId) != perfClassPrimaryCameraIds.end();
1131 return targetSdkVersion >= SDK_VERSION_S && isPerfClassPrimaryCamera;
1132}
1133
Shuzhen Wang045be6c2023-10-12 10:01:10 -07001134binder::Status mapRequestTemplateFromClient(const std::string& cameraId, int templateId,
1135 camera_request_template_t* tempId /*out*/) {
1136 binder::Status ret = binder::Status::ok();
1137
1138 if (tempId == nullptr) {
1139 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1140 "Camera %s: Invalid template argument", cameraId.c_str());
1141 return ret;
1142 }
1143 switch(templateId) {
1144 case ICameraDeviceUser::TEMPLATE_PREVIEW:
1145 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
1146 break;
1147 case ICameraDeviceUser::TEMPLATE_RECORD:
1148 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
1149 break;
1150 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
1151 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
1152 break;
1153 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
1154 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
1155 break;
1156 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
1157 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
1158 break;
1159 case ICameraDeviceUser::TEMPLATE_MANUAL:
1160 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
1161 break;
1162 default:
1163 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1164 "Camera %s: Template ID %d is invalid or not supported",
1165 cameraId.c_str(), templateId);
1166 return ret;
1167 }
1168
1169 return ret;
1170}
1171
1172status_t mapRequestTemplateToAidl(camera_request_template_t templateId,
1173 RequestTemplate* id /*out*/) {
1174 switch (templateId) {
1175 case CAMERA_TEMPLATE_PREVIEW:
1176 *id = RequestTemplate::PREVIEW;
1177 break;
1178 case CAMERA_TEMPLATE_STILL_CAPTURE:
1179 *id = RequestTemplate::STILL_CAPTURE;
1180 break;
1181 case CAMERA_TEMPLATE_VIDEO_RECORD:
1182 *id = RequestTemplate::VIDEO_RECORD;
1183 break;
1184 case CAMERA_TEMPLATE_VIDEO_SNAPSHOT:
1185 *id = RequestTemplate::VIDEO_SNAPSHOT;
1186 break;
1187 case CAMERA_TEMPLATE_ZERO_SHUTTER_LAG:
1188 *id = RequestTemplate::ZERO_SHUTTER_LAG;
1189 break;
1190 case CAMERA_TEMPLATE_MANUAL:
1191 *id = RequestTemplate::MANUAL;
1192 break;
1193 default:
1194 // Unknown template ID, or this HAL is too old to support it
1195 return BAD_VALUE;
1196 }
1197 return OK;
1198}
1199
1200void filterParameters(const CameraMetadata& src, const CameraMetadata& deviceInfo,
Shuzhen Wang33c84cd2024-01-03 17:45:03 +00001201 const std::vector<int32_t>& additionalTags, metadata_vendor_id_t vendorTagId,
1202 CameraMetadata& dst) {
Shuzhen Wang045be6c2023-10-12 10:01:10 -07001203 const CameraMetadata params(src);
1204 camera_metadata_ro_entry_t availableSessionKeys = deviceInfo.find(
1205 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
1206 CameraMetadata filteredParams(availableSessionKeys.count);
1207 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1208 filteredParams.getAndLock());
1209 set_camera_metadata_vendor_id(meta, vendorTagId);
1210 filteredParams.unlock(meta);
Shuzhen Wang33c84cd2024-01-03 17:45:03 +00001211
1212 std::unordered_set<int32_t> filteredTags(availableSessionKeys.data.i32,
1213 availableSessionKeys.data.i32 + availableSessionKeys.count);
1214 filteredTags.insert(additionalTags.begin(), additionalTags.end());
1215 for (int32_t tag : filteredTags) {
1216 camera_metadata_ro_entry entry = params.find(tag);
Shuzhen Wang045be6c2023-10-12 10:01:10 -07001217 if (entry.count > 0) {
1218 filteredParams.update(entry);
1219 }
1220 }
1221 dst = std::move(filteredParams);
1222}
1223
Shuzhen Wang33c84cd2024-01-03 17:45:03 +00001224status_t overrideDefaultRequestKeys(CameraMetadata *request) {
1225 // Override the template request with ZoomRatioMapper
1226 status_t res = ZoomRatioMapper::initZoomRatioInTemplate(request);
1227 if (res != OK) {
1228 ALOGE("Failed to update zoom ratio: %s (%d)", strerror(-res), res);
1229 return res;
1230 }
1231
1232 // Fill in JPEG_QUALITY if not available
1233 if (!request->exists(ANDROID_JPEG_QUALITY)) {
1234 static const uint8_t kDefaultJpegQuality = 95;
1235 request->update(ANDROID_JPEG_QUALITY, &kDefaultJpegQuality, 1);
1236 }
1237
1238 // Fill in AUTOFRAMING if not available
1239 if (!request->exists(ANDROID_CONTROL_AUTOFRAMING)) {
1240 static const uint8_t kDefaultAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_OFF;
1241 request->update(ANDROID_CONTROL_AUTOFRAMING, &kDefaultAutoframingMode, 1);
1242 }
1243
1244 return OK;
1245}
1246
Austin Borgerea931242021-12-13 23:10:41 +00001247} // namespace SessionConfigurationUtils
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001248} // namespace camera3
1249} // namespace android