Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #define LOG_TAG "Camera3-OutputUtils" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | //#define LOG_NNDEBUG 0 // Per-frame verbose logging |
| 21 | |
| 22 | #ifdef LOG_NNDEBUG |
| 23 | #define ALOGVV(...) ALOGV(__VA_ARGS__) |
| 24 | #else |
| 25 | #define ALOGVV(...) ((void)0) |
| 26 | #endif |
| 27 | |
| 28 | // Convenience macros for transitioning to the error state |
| 29 | #define SET_ERR(fmt, ...) states.setErrIntf.setErrorState( \ |
| 30 | "%s: " fmt, __FUNCTION__, \ |
| 31 | ##__VA_ARGS__) |
| 32 | |
| 33 | #include <inttypes.h> |
| 34 | |
| 35 | #include <utils/Log.h> |
| 36 | #include <utils/SortedVector.h> |
| 37 | #include <utils/Trace.h> |
| 38 | |
| 39 | #include <android/hardware/camera2/ICameraDeviceCallbacks.h> |
| 40 | |
| 41 | #include <android/hardware/camera/device/3.4/ICameraDeviceCallback.h> |
| 42 | #include <android/hardware/camera/device/3.5/ICameraDeviceCallback.h> |
| 43 | #include <android/hardware/camera/device/3.5/ICameraDeviceSession.h> |
| 44 | |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 45 | #include <camera/CameraUtils.h> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 46 | #include <camera/StringUtils.h> |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 47 | #include <camera_metadata_hidden.h> |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 48 | #include <com_android_internal_camera_flags.h> |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 49 | |
| 50 | #include "device3/Camera3OutputUtils.h" |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 51 | #include "utils/SessionConfigurationUtils.h" |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 52 | |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 53 | #include "system/camera_metadata.h" |
| 54 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 55 | using namespace android::camera3; |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 56 | using namespace android::camera3::SessionConfigurationUtils; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 57 | using namespace android::hardware::camera; |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 58 | namespace flags = com::android::internal::camera::flags; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 59 | |
| 60 | namespace android { |
| 61 | namespace camera3 { |
| 62 | |
| 63 | status_t fixupMonochromeTags( |
| 64 | CaptureOutputStates& states, |
| 65 | const CameraMetadata& deviceInfo, |
| 66 | CameraMetadata& resultMetadata) { |
| 67 | status_t res = OK; |
| 68 | if (!states.needFixupMonoChrome) { |
| 69 | return res; |
| 70 | } |
| 71 | |
| 72 | // Remove tags that are not applicable to monochrome camera. |
| 73 | int32_t tagsToRemove[] = { |
| 74 | ANDROID_SENSOR_GREEN_SPLIT, |
| 75 | ANDROID_SENSOR_NEUTRAL_COLOR_POINT, |
| 76 | ANDROID_COLOR_CORRECTION_MODE, |
| 77 | ANDROID_COLOR_CORRECTION_TRANSFORM, |
| 78 | ANDROID_COLOR_CORRECTION_GAINS, |
| 79 | }; |
| 80 | for (auto tag : tagsToRemove) { |
| 81 | res = resultMetadata.erase(tag); |
| 82 | if (res != OK) { |
| 83 | ALOGE("%s: Failed to remove tag %d for monochrome camera", __FUNCTION__, tag); |
| 84 | return res; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL |
| 89 | camera_metadata_entry blEntry = resultMetadata.find(ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL); |
| 90 | for (size_t i = 1; i < blEntry.count; i++) { |
| 91 | blEntry.data.f[i] = blEntry.data.f[0]; |
| 92 | } |
| 93 | |
| 94 | // ANDROID_SENSOR_NOISE_PROFILE |
| 95 | camera_metadata_entry npEntry = resultMetadata.find(ANDROID_SENSOR_NOISE_PROFILE); |
| 96 | if (npEntry.count > 0 && npEntry.count % 2 == 0) { |
| 97 | double np[] = {npEntry.data.d[0], npEntry.data.d[1]}; |
| 98 | res = resultMetadata.update(ANDROID_SENSOR_NOISE_PROFILE, np, 2); |
| 99 | if (res != OK) { |
| 100 | ALOGE("%s: Failed to update SENSOR_NOISE_PROFILE: %s (%d)", |
| 101 | __FUNCTION__, strerror(-res), res); |
| 102 | return res; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // ANDROID_STATISTICS_LENS_SHADING_MAP |
| 107 | camera_metadata_ro_entry lsSizeEntry = deviceInfo.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE); |
| 108 | camera_metadata_entry lsEntry = resultMetadata.find(ANDROID_STATISTICS_LENS_SHADING_MAP); |
| 109 | if (lsSizeEntry.count == 2 && lsEntry.count > 0 |
| 110 | && (int32_t)lsEntry.count == 4 * lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]) { |
| 111 | for (int32_t i = 0; i < lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]; i++) { |
| 112 | lsEntry.data.f[4*i+1] = lsEntry.data.f[4*i]; |
| 113 | lsEntry.data.f[4*i+2] = lsEntry.data.f[4*i]; |
| 114 | lsEntry.data.f[4*i+3] = lsEntry.data.f[4*i]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // ANDROID_TONEMAP_CURVE_BLUE |
| 119 | // ANDROID_TONEMAP_CURVE_GREEN |
| 120 | // ANDROID_TONEMAP_CURVE_RED |
| 121 | camera_metadata_entry tcbEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_BLUE); |
| 122 | camera_metadata_entry tcgEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_GREEN); |
| 123 | camera_metadata_entry tcrEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_RED); |
| 124 | if (tcbEntry.count > 0 |
| 125 | && tcbEntry.count == tcgEntry.count |
| 126 | && tcbEntry.count == tcrEntry.count) { |
| 127 | for (size_t i = 0; i < tcbEntry.count; i++) { |
| 128 | tcbEntry.data.f[i] = tcrEntry.data.f[i]; |
| 129 | tcgEntry.data.f[i] = tcrEntry.data.f[i]; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return res; |
| 134 | } |
| 135 | |
Bharatt Kukreja | 0e13db3 | 2022-12-07 21:38:45 +0000 | [diff] [blame] | 136 | status_t fixupAutoframingTags(CameraMetadata& resultMetadata) { |
| 137 | status_t res = OK; |
| 138 | camera_metadata_entry autoframingEntry = |
| 139 | resultMetadata.find(ANDROID_CONTROL_AUTOFRAMING); |
| 140 | if (autoframingEntry.count == 0) { |
| 141 | const uint8_t defaultAutoframingEntry = ANDROID_CONTROL_AUTOFRAMING_OFF; |
| 142 | res = resultMetadata.update(ANDROID_CONTROL_AUTOFRAMING, &defaultAutoframingEntry, 1); |
| 143 | if (res != OK) { |
| 144 | ALOGE("%s: Failed to update ANDROID_CONTROL_AUTOFRAMING: %s (%d)", |
| 145 | __FUNCTION__, strerror(-res), res); |
| 146 | return res; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | camera_metadata_entry autoframingStateEntry = |
| 151 | resultMetadata.find(ANDROID_CONTROL_AUTOFRAMING_STATE); |
| 152 | if (autoframingStateEntry.count == 0) { |
| 153 | const uint8_t defaultAutoframingStateEntry = ANDROID_CONTROL_AUTOFRAMING_STATE_INACTIVE; |
| 154 | res = resultMetadata.update(ANDROID_CONTROL_AUTOFRAMING_STATE, |
| 155 | &defaultAutoframingStateEntry, 1); |
| 156 | if (res != OK) { |
| 157 | ALOGE("%s: Failed to update ANDROID_CONTROL_AUTOFRAMING_STATE: %s (%d)", |
| 158 | __FUNCTION__, strerror(-res), res); |
| 159 | return res; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return res; |
| 164 | } |
| 165 | |
Rucha Katakwar | 47c2e7c | 2023-11-09 12:08:35 -0800 | [diff] [blame] | 166 | status_t fixupManualFlashStrengthControlTags(CameraMetadata& resultMetadata) { |
| 167 | status_t res = OK; |
| 168 | camera_metadata_entry strengthLevelEntry = |
| 169 | resultMetadata.find(ANDROID_FLASH_STRENGTH_LEVEL); |
| 170 | if (strengthLevelEntry.count == 0) { |
| 171 | const int32_t defaultStrengthLevelEntry = ANDROID_FLASH_STRENGTH_LEVEL; |
| 172 | res = resultMetadata.update(ANDROID_FLASH_STRENGTH_LEVEL, &defaultStrengthLevelEntry, 1); |
| 173 | if (res != OK) { |
| 174 | ALOGE("%s: Failed to update ANDROID_FLASH_STRENGTH_LEVEL: %s (%d)", |
| 175 | __FUNCTION__, strerror(-res), res); |
| 176 | return res; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return res; |
| 181 | } |
| 182 | |
Shuzhen Wang | cd8811c | 2022-08-29 17:23:52 +0000 | [diff] [blame] | 183 | void correctMeteringRegions(camera_metadata_t *meta) { |
| 184 | if (meta == nullptr) return; |
| 185 | |
| 186 | uint32_t meteringRegionKeys[] = { |
| 187 | ANDROID_CONTROL_AE_REGIONS, |
| 188 | ANDROID_CONTROL_AWB_REGIONS, |
| 189 | ANDROID_CONTROL_AF_REGIONS }; |
| 190 | |
| 191 | for (uint32_t key : meteringRegionKeys) { |
| 192 | camera_metadata_entry_t entry; |
| 193 | int res = find_camera_metadata_entry(meta, key, &entry); |
| 194 | if (res != OK) continue; |
| 195 | |
| 196 | for (size_t i = 0; i < entry.count; i += 5) { |
| 197 | if (entry.data.i32[0] > entry.data.i32[2]) { |
| 198 | ALOGW("%s: Invalid metering region (%d): left: %d, right: %d", |
| 199 | __FUNCTION__, key, entry.data.i32[0], entry.data.i32[2]); |
| 200 | entry.data.i32[2] = entry.data.i32[0]; |
| 201 | } |
| 202 | if (entry.data.i32[1] > entry.data.i32[3]) { |
| 203 | ALOGW("%s: Invalid metering region (%d): top: %d, bottom: %d", |
| 204 | __FUNCTION__, key, entry.data.i32[1], entry.data.i32[3]); |
| 205 | entry.data.i32[3] = entry.data.i32[1]; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 211 | void insertResultLocked(CaptureOutputStates& states, CaptureResult *result, uint32_t frameNumber) { |
| 212 | if (result == nullptr) return; |
| 213 | |
| 214 | camera_metadata_t *meta = const_cast<camera_metadata_t *>( |
| 215 | result->mMetadata.getAndLock()); |
| 216 | set_camera_metadata_vendor_id(meta, states.vendorTagId); |
Shuzhen Wang | cd8811c | 2022-08-29 17:23:52 +0000 | [diff] [blame] | 217 | correctMeteringRegions(meta); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 218 | result->mMetadata.unlock(meta); |
| 219 | |
| 220 | if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, |
| 221 | (int32_t*)&frameNumber, 1) != OK) { |
| 222 | SET_ERR("Failed to set frame number %d in metadata", frameNumber); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) { |
| 227 | SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | // Update vendor tag id for physical metadata |
| 232 | for (auto& physicalMetadata : result->mPhysicalMetadatas) { |
| 233 | camera_metadata_t *pmeta = const_cast<camera_metadata_t *>( |
| 234 | physicalMetadata.mPhysicalCameraMetadata.getAndLock()); |
| 235 | set_camera_metadata_vendor_id(pmeta, states.vendorTagId); |
Shuzhen Wang | cd8811c | 2022-08-29 17:23:52 +0000 | [diff] [blame] | 236 | correctMeteringRegions(pmeta); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 237 | physicalMetadata.mPhysicalCameraMetadata.unlock(pmeta); |
| 238 | } |
| 239 | |
| 240 | // Valid result, insert into queue |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 241 | std::list<CaptureResult>::iterator queuedResult = |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 242 | states.resultQueue.insert(states.resultQueue.end(), CaptureResult(*result)); |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 243 | ALOGV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 244 | ", burstId = %" PRId32, __FUNCTION__, |
| 245 | queuedResult->mResultExtras.requestId, |
| 246 | queuedResult->mResultExtras.frameNumber, |
| 247 | queuedResult->mResultExtras.burstId); |
| 248 | |
| 249 | states.resultSignal.notify_one(); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | void sendPartialCaptureResult(CaptureOutputStates& states, |
| 254 | const camera_metadata_t * partialResult, |
| 255 | const CaptureResultExtras &resultExtras, uint32_t frameNumber) { |
| 256 | ATRACE_CALL(); |
| 257 | std::lock_guard<std::mutex> l(states.outputLock); |
| 258 | |
| 259 | CaptureResult captureResult; |
| 260 | captureResult.mResultExtras = resultExtras; |
| 261 | captureResult.mMetadata = partialResult; |
| 262 | |
| 263 | // Fix up result metadata for monochrome camera. |
| 264 | status_t res = fixupMonochromeTags(states, states.deviceInfo, captureResult.mMetadata); |
| 265 | if (res != OK) { |
| 266 | SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res); |
| 267 | return; |
| 268 | } |
| 269 | |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 270 | // Update partial result by removing keys remapped by DistortionCorrection, ZoomRatio, |
| 271 | // and RotationAndCrop mappers. |
| 272 | std::set<uint32_t> keysToRemove; |
| 273 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 274 | auto iter = states.distortionMappers.find(states.cameraId); |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 275 | if (iter != states.distortionMappers.end()) { |
| 276 | const auto& remappedKeys = iter->second.getRemappedKeys(); |
| 277 | keysToRemove.insert(remappedKeys.begin(), remappedKeys.end()); |
| 278 | } |
| 279 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 280 | const auto& remappedKeys = states.zoomRatioMappers[states.cameraId].getRemappedKeys(); |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 281 | keysToRemove.insert(remappedKeys.begin(), remappedKeys.end()); |
| 282 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 283 | auto mapper = states.rotateAndCropMappers.find(states.cameraId); |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 284 | if (mapper != states.rotateAndCropMappers.end()) { |
Ravneet | 5848590 | 2023-04-07 04:34:48 +0000 | [diff] [blame] | 285 | const auto& remappedKeys = mapper->second.getRemappedKeys(); |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 286 | keysToRemove.insert(remappedKeys.begin(), remappedKeys.end()); |
| 287 | } |
| 288 | |
| 289 | for (uint32_t key : keysToRemove) { |
| 290 | captureResult.mMetadata.erase(key); |
| 291 | } |
| 292 | |
| 293 | // Send partial result |
| 294 | if (captureResult.mMetadata.entryCount() > 0) { |
| 295 | insertResultLocked(states, &captureResult, frameNumber); |
| 296 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void sendCaptureResult( |
| 300 | CaptureOutputStates& states, |
| 301 | CameraMetadata &pendingMetadata, |
| 302 | CaptureResultExtras &resultExtras, |
| 303 | CameraMetadata &collectedPartialResult, |
| 304 | uint32_t frameNumber, |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 305 | bool reprocess, bool zslStillCapture, bool rotateAndCropAuto, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 306 | const std::set<std::string>& cameraIdsWithZoom, |
| 307 | const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) { |
| 308 | ATRACE_CALL(); |
| 309 | if (pendingMetadata.isEmpty()) |
| 310 | return; |
| 311 | |
| 312 | std::lock_guard<std::mutex> l(states.outputLock); |
| 313 | |
| 314 | // TODO: need to track errors for tighter bounds on expected frame number |
| 315 | if (reprocess) { |
| 316 | if (frameNumber < states.nextReprocResultFrameNum) { |
| 317 | SET_ERR("Out-of-order reprocess capture result metadata submitted! " |
| 318 | "(got frame number %d, expecting %d)", |
| 319 | frameNumber, states.nextReprocResultFrameNum); |
| 320 | return; |
| 321 | } |
| 322 | states.nextReprocResultFrameNum = frameNumber + 1; |
| 323 | } else if (zslStillCapture) { |
| 324 | if (frameNumber < states.nextZslResultFrameNum) { |
| 325 | SET_ERR("Out-of-order ZSL still capture result metadata submitted! " |
| 326 | "(got frame number %d, expecting %d)", |
| 327 | frameNumber, states.nextZslResultFrameNum); |
| 328 | return; |
| 329 | } |
| 330 | states.nextZslResultFrameNum = frameNumber + 1; |
| 331 | } else { |
| 332 | if (frameNumber < states.nextResultFrameNum) { |
| 333 | SET_ERR("Out-of-order capture result metadata submitted! " |
| 334 | "(got frame number %d, expecting %d)", |
| 335 | frameNumber, states.nextResultFrameNum); |
| 336 | return; |
| 337 | } |
| 338 | states.nextResultFrameNum = frameNumber + 1; |
| 339 | } |
| 340 | |
| 341 | CaptureResult captureResult; |
| 342 | captureResult.mResultExtras = resultExtras; |
| 343 | captureResult.mMetadata = pendingMetadata; |
| 344 | captureResult.mPhysicalMetadatas = physicalMetadatas; |
| 345 | |
| 346 | // Append any previous partials to form a complete result |
| 347 | if (states.usePartialResult && !collectedPartialResult.isEmpty()) { |
| 348 | captureResult.mMetadata.append(collectedPartialResult); |
| 349 | } |
| 350 | |
| 351 | captureResult.mMetadata.sort(); |
| 352 | |
| 353 | // Check that there's a timestamp in the result metadata |
| 354 | camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); |
| 355 | if (timestamp.count == 0) { |
| 356 | SET_ERR("No timestamp provided by HAL for frame %d!", |
| 357 | frameNumber); |
| 358 | return; |
| 359 | } |
Yin-Chia Yeh | c530c59 | 2020-03-09 14:50:36 -0700 | [diff] [blame] | 360 | nsecs_t sensorTimestamp = timestamp.data.i64[0]; |
| 361 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 362 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
| 363 | camera_metadata_entry timestamp = |
| 364 | physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP); |
| 365 | if (timestamp.count == 0) { |
| 366 | SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 367 | physicalMetadata.mPhysicalCameraId.c_str(), frameNumber); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Fix up some result metadata to account for HAL-level distortion correction |
Yin-Chia Yeh | 7ffbd98 | 2020-04-13 10:04:23 -0700 | [diff] [blame] | 373 | status_t res = OK; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 374 | auto iter = states.distortionMappers.find(states.cameraId); |
Yin-Chia Yeh | 7ffbd98 | 2020-04-13 10:04:23 -0700 | [diff] [blame] | 375 | if (iter != states.distortionMappers.end()) { |
| 376 | res = iter->second.correctCaptureResult(&captureResult.mMetadata); |
| 377 | if (res != OK) { |
| 378 | SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)", |
| 379 | frameNumber, strerror(-res), res); |
| 380 | return; |
| 381 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // Fix up result metadata to account for zoom ratio availabilities between |
| 385 | // HAL and app. |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 386 | bool zoomRatioIs1 = cameraIdsWithZoom.find(states.cameraId) == cameraIdsWithZoom.end(); |
| 387 | res = states.zoomRatioMappers[states.cameraId].updateCaptureResult( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 388 | &captureResult.mMetadata, zoomRatioIs1); |
| 389 | if (res != OK) { |
| 390 | SET_ERR("Failed to update capture result zoom ratio metadata for frame %d: %s (%d)", |
| 391 | frameNumber, strerror(-res), res); |
| 392 | return; |
| 393 | } |
| 394 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 395 | // Fix up result metadata to account for rotateAndCrop in AUTO mode |
| 396 | if (rotateAndCropAuto) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 397 | auto mapper = states.rotateAndCropMappers.find(states.cameraId); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 398 | if (mapper != states.rotateAndCropMappers.end()) { |
| 399 | res = mapper->second.updateCaptureResult( |
| 400 | &captureResult.mMetadata); |
| 401 | if (res != OK) { |
| 402 | SET_ERR("Unable to correct capture result rotate-and-crop for frame %d: %s (%d)", |
| 403 | frameNumber, strerror(-res), res); |
| 404 | return; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
Rucha Katakwar | 47c2e7c | 2023-11-09 12:08:35 -0800 | [diff] [blame] | 409 | // Fix up manual flash strength control metadata |
| 410 | res = fixupManualFlashStrengthControlTags(captureResult.mMetadata); |
| 411 | if (res != OK) { |
| 412 | SET_ERR("Failed to set flash strength level defaults in result metadata: %s (%d)", |
| 413 | strerror(-res), res); |
| 414 | return; |
| 415 | } |
| 416 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
| 417 | res = fixupManualFlashStrengthControlTags(physicalMetadata.mPhysicalCameraMetadata); |
| 418 | if (res != OK) { |
| 419 | SET_ERR("Failed to set flash strength level defaults in physical result" |
| 420 | " metadata: %s (%d)", strerror(-res), res); |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | |
Bharatt Kukreja | d33fe9f | 2022-11-23 21:52:52 +0000 | [diff] [blame] | 425 | // Fix up autoframing metadata |
Bharatt Kukreja | 0e13db3 | 2022-12-07 21:38:45 +0000 | [diff] [blame] | 426 | res = fixupAutoframingTags(captureResult.mMetadata); |
| 427 | if (res != OK) { |
| 428 | SET_ERR("Failed to set autoframing defaults in result metadata: %s (%d)", |
| 429 | strerror(-res), res); |
| 430 | return; |
Bharatt Kukreja | d33fe9f | 2022-11-23 21:52:52 +0000 | [diff] [blame] | 431 | } |
Bharatt Kukreja | 0e13db3 | 2022-12-07 21:38:45 +0000 | [diff] [blame] | 432 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
| 433 | res = fixupAutoframingTags(physicalMetadata.mPhysicalCameraMetadata); |
| 434 | if (res != OK) { |
| 435 | SET_ERR("Failed to set autoframing defaults in physical result metadata: %s (%d)", |
| 436 | strerror(-res), res); |
Bharatt Kukreja | d33fe9f | 2022-11-23 21:52:52 +0000 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | } |
| 440 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 441 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 442 | const std::string cameraId = physicalMetadata.mPhysicalCameraId; |
| 443 | auto mapper = states.distortionMappers.find(cameraId); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 444 | if (mapper != states.distortionMappers.end()) { |
| 445 | res = mapper->second.correctCaptureResult( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 446 | &physicalMetadata.mPhysicalCameraMetadata); |
| 447 | if (res != OK) { |
| 448 | SET_ERR("Unable to correct physical capture result metadata for frame %d: %s (%d)", |
| 449 | frameNumber, strerror(-res), res); |
| 450 | return; |
| 451 | } |
| 452 | } |
| 453 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 454 | zoomRatioIs1 = cameraIdsWithZoom.find(cameraId) == cameraIdsWithZoom.end(); |
| 455 | res = states.zoomRatioMappers[cameraId].updateCaptureResult( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 456 | &physicalMetadata.mPhysicalCameraMetadata, zoomRatioIs1); |
| 457 | if (res != OK) { |
| 458 | SET_ERR("Failed to update camera %s's physical zoom ratio metadata for " |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 459 | "frame %d: %s(%d)", cameraId.c_str(), frameNumber, strerror(-res), res); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Fix up result metadata for monochrome camera. |
| 465 | res = fixupMonochromeTags(states, states.deviceInfo, captureResult.mMetadata); |
| 466 | if (res != OK) { |
| 467 | SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res); |
| 468 | return; |
| 469 | } |
| 470 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 471 | const std::string &cameraId = physicalMetadata.mPhysicalCameraId; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 472 | res = fixupMonochromeTags(states, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 473 | states.physicalDeviceInfoMap.at(cameraId), |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 474 | physicalMetadata.mPhysicalCameraMetadata); |
| 475 | if (res != OK) { |
| 476 | SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res); |
| 477 | return; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | std::unordered_map<std::string, CameraMetadata> monitoredPhysicalMetadata; |
| 482 | for (auto& m : physicalMetadatas) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 483 | monitoredPhysicalMetadata.emplace(m.mPhysicalCameraId, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 484 | CameraMetadata(m.mPhysicalCameraMetadata)); |
| 485 | } |
| 486 | states.tagMonitor.monitorMetadata(TagMonitor::RESULT, |
Yin-Chia Yeh | c530c59 | 2020-03-09 14:50:36 -0700 | [diff] [blame] | 487 | frameNumber, sensorTimestamp, captureResult.mMetadata, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 488 | monitoredPhysicalMetadata); |
| 489 | |
| 490 | insertResultLocked(states, &captureResult, frameNumber); |
| 491 | } |
| 492 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 493 | void removeInFlightMapEntryLocked(CaptureOutputStates& states, int idx) { |
| 494 | ATRACE_CALL(); |
| 495 | InFlightRequestMap& inflightMap = states.inflightMap; |
| 496 | nsecs_t duration = inflightMap.valueAt(idx).maxExpectedDuration; |
| 497 | inflightMap.removeItemsAt(idx, 1); |
| 498 | |
| 499 | states.inflightIntf.onInflightEntryRemovedLocked(duration); |
| 500 | } |
| 501 | |
| 502 | void removeInFlightRequestIfReadyLocked(CaptureOutputStates& states, int idx) { |
| 503 | InFlightRequestMap& inflightMap = states.inflightMap; |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 504 | const InFlightRequest &request = inflightMap.valueAt(idx); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 505 | const uint32_t frameNumber = inflightMap.keyAt(idx); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 506 | SessionStatsBuilder& sessionStatsBuilder = states.sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 507 | |
| 508 | nsecs_t sensorTimestamp = request.sensorTimestamp; |
| 509 | nsecs_t shutterTimestamp = request.shutterTimestamp; |
| 510 | |
| 511 | // Check if it's okay to remove the request from InFlightMap: |
| 512 | // In the case of a successful request: |
| 513 | // all input and output buffers, all result metadata, shutter callback |
| 514 | // arrived. |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 515 | // In the case of an unsuccessful request: |
| 516 | // all input and output buffers, as well as request/result error notifications, arrived. |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 517 | if (request.numBuffersLeft == 0 && |
| 518 | (request.skipResultMetadata || |
| 519 | (request.haveResultMetadata && shutterTimestamp != 0))) { |
| 520 | if (request.stillCapture) { |
| 521 | ATRACE_ASYNC_END("still capture", frameNumber); |
| 522 | } |
| 523 | |
| 524 | ATRACE_ASYNC_END("frame capture", frameNumber); |
| 525 | |
Ivan Lozano | c0ad82f | 2020-07-30 09:32:57 -0400 | [diff] [blame] | 526 | // Validation check - if sensor timestamp matches shutter timestamp in the |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 527 | // case of request having callback. |
| 528 | if (request.hasCallback && request.requestStatus == OK && |
| 529 | sensorTimestamp != shutterTimestamp) { |
| 530 | SET_ERR("sensor timestamp (%" PRId64 |
| 531 | ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", |
| 532 | sensorTimestamp, frameNumber, shutterTimestamp); |
| 533 | } |
| 534 | |
| 535 | // for an unsuccessful request, it may have pending output buffers to |
| 536 | // return. |
| 537 | assert(request.requestStatus != OK || |
| 538 | request.pendingOutputBuffers.size() == 0); |
| 539 | |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 540 | returnOutputBuffers( |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 541 | states.useHalBufManager, states.halBufManagedStreamIds, |
| 542 | states.listener, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 543 | request.pendingOutputBuffers.array(), |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 544 | request.pendingOutputBuffers.size(), /*timestamp*/0, /*readoutTimestamp*/0, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 545 | /*requested*/true, request.requestTimeNs, states.sessionStatsBuilder, |
| 546 | /*timestampIncreasing*/true, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 547 | request.outputSurfaces, request.resultExtras, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 548 | request.errorBufStrategy, request.transform); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 549 | |
| 550 | // Note down the just completed frame number |
| 551 | if (request.hasInputBuffer) { |
| 552 | states.lastCompletedReprocessFrameNumber = frameNumber; |
Shuzhen Wang | 828ade3 | 2022-01-10 17:19:02 -0800 | [diff] [blame] | 553 | } else if (request.zslCapture && request.stillCapture) { |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 554 | states.lastCompletedZslFrameNumber = frameNumber; |
| 555 | } else { |
| 556 | states.lastCompletedRegularFrameNumber = frameNumber; |
| 557 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 558 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 559 | sessionStatsBuilder.incResultCounter(request.skipResultMetadata); |
| 560 | |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 561 | removeInFlightMapEntryLocked(states, idx); |
| 562 | ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | states.inflightIntf.checkInflightMapLengthLocked(); |
| 566 | } |
| 567 | |
Shuzhen Wang | 9908050 | 2021-03-07 21:08:20 -0800 | [diff] [blame] | 568 | // Erase the subset of physicalCameraIds that contains id |
| 569 | bool erasePhysicalCameraIdSet( |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 570 | std::set<std::set<std::string>>& physicalCameraIds, const std::string& id) { |
Shuzhen Wang | 9908050 | 2021-03-07 21:08:20 -0800 | [diff] [blame] | 571 | bool found = false; |
| 572 | for (auto iter = physicalCameraIds.begin(); iter != physicalCameraIds.end(); iter++) { |
| 573 | if (iter->count(id) == 1) { |
| 574 | physicalCameraIds.erase(iter); |
| 575 | found = true; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | return found; |
| 580 | } |
| 581 | |
Shuzhen Wang | 3c00b09 | 2022-11-08 22:53:07 +0000 | [diff] [blame] | 582 | const std::set<std::string>& getCameraIdsWithZoomLocked( |
| 583 | const InFlightRequestMap& inflightMap, const CameraMetadata& metadata, |
| 584 | const std::set<std::string>& cameraIdsWithZoom) { |
| 585 | camera_metadata_ro_entry overrideEntry = |
| 586 | metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE); |
| 587 | camera_metadata_ro_entry frameNumberEntry = |
| 588 | metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDING_FRAME_NUMBER); |
| 589 | if (overrideEntry.count != 1 |
| 590 | || overrideEntry.data.i32[0] != ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM |
| 591 | || frameNumberEntry.count != 1) { |
| 592 | // No valid overriding frame number, skip |
| 593 | return cameraIdsWithZoom; |
| 594 | } |
| 595 | |
| 596 | uint32_t overridingFrameNumber = frameNumberEntry.data.i32[0]; |
| 597 | ssize_t idx = inflightMap.indexOfKey(overridingFrameNumber); |
| 598 | if (idx < 0) { |
| 599 | ALOGE("%s: Failed to find pending request #%d in inflight map", |
| 600 | __FUNCTION__, overridingFrameNumber); |
| 601 | return cameraIdsWithZoom; |
| 602 | } |
| 603 | |
| 604 | const InFlightRequest &r = inflightMap.valueFor(overridingFrameNumber); |
| 605 | return r.cameraIdsWithZoom; |
| 606 | } |
| 607 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 608 | void processCaptureResult(CaptureOutputStates& states, const camera_capture_result *result) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 609 | ATRACE_CALL(); |
| 610 | |
| 611 | status_t res; |
| 612 | |
| 613 | uint32_t frameNumber = result->frame_number; |
| 614 | if (result->result == NULL && result->num_output_buffers == 0 && |
| 615 | result->input_buffer == NULL) { |
| 616 | SET_ERR("No result data provided by HAL for frame %d", |
| 617 | frameNumber); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | if (!states.usePartialResult && |
| 622 | result->result != NULL && |
| 623 | result->partial_result != 1) { |
| 624 | SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" |
| 625 | " if partial result is not supported", |
| 626 | frameNumber, result->partial_result); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | bool isPartialResult = false; |
| 631 | CameraMetadata collectedPartialResult; |
| 632 | bool hasInputBufferInRequest = false; |
| 633 | |
| 634 | // Get shutter timestamp and resultExtras from list of in-flight requests, |
| 635 | // where it was added by the shutter notification for this frame. If the |
| 636 | // shutter timestamp isn't received yet, append the output buffers to the |
| 637 | // in-flight request and they will be returned when the shutter timestamp |
| 638 | // arrives. Update the in-flight status and remove the in-flight entry if |
| 639 | // all result data and shutter timestamp have been received. |
| 640 | nsecs_t shutterTimestamp = 0; |
| 641 | { |
| 642 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 643 | ssize_t idx = states.inflightMap.indexOfKey(frameNumber); |
| 644 | if (idx == NAME_NOT_FOUND) { |
| 645 | SET_ERR("Unknown frame number for capture result: %d", |
| 646 | frameNumber); |
| 647 | return; |
| 648 | } |
| 649 | InFlightRequest &request = states.inflightMap.editValueAt(idx); |
| 650 | ALOGVV("%s: got InFlightRequest requestId = %" PRId32 |
| 651 | ", frameNumber = %" PRId64 ", burstId = %" PRId32 |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 652 | ", partialResultCount = %d/%d, hasCallback = %d, num_output_buffers %d" |
| 653 | ", usePartialResult = %d", |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 654 | __FUNCTION__, request.resultExtras.requestId, |
| 655 | request.resultExtras.frameNumber, request.resultExtras.burstId, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 656 | result->partial_result, states.numPartialResults, |
| 657 | request.hasCallback, result->num_output_buffers, |
| 658 | states.usePartialResult); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 659 | // Always update the partial count to the latest one if it's not 0 |
| 660 | // (buffers only). When framework aggregates adjacent partial results |
| 661 | // into one, the latest partial count will be used. |
| 662 | if (result->partial_result != 0) |
| 663 | request.resultExtras.partialResultCount = result->partial_result; |
| 664 | |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 665 | if (result->result != nullptr) { |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 666 | camera_metadata_ro_entry entry; |
| 667 | auto ret = find_camera_metadata_ro_entry(result->result, |
| 668 | ANDROID_LOGICAL_MULTI_CAMERA_ACTIVE_PHYSICAL_ID, &entry); |
| 669 | if ((ret == OK) && (entry.count > 0)) { |
| 670 | std::string physicalId(reinterpret_cast<const char *>(entry.data.u8)); |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 671 | if (!states.activePhysicalId.empty() && physicalId != states.activePhysicalId) { |
| 672 | states.listener->notifyPhysicalCameraChange(physicalId); |
| 673 | } |
| 674 | states.activePhysicalId = physicalId; |
| 675 | |
| 676 | if (!states.legacyClient && !states.overrideToPortrait) { |
| 677 | auto deviceInfo = states.physicalDeviceInfoMap.find(physicalId); |
| 678 | if (deviceInfo != states.physicalDeviceInfoMap.end()) { |
| 679 | auto orientation = deviceInfo->second.find(ANDROID_SENSOR_ORIENTATION); |
| 680 | if (orientation.count > 0) { |
Emilian Peev | 747be7e | 2023-03-31 11:10:08 -0700 | [diff] [blame] | 681 | int32_t transform; |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 682 | ret = CameraUtils::getRotationTransform(deviceInfo->second, |
Emilian Peev | 747be7e | 2023-03-31 11:10:08 -0700 | [diff] [blame] | 683 | OutputConfiguration::MIRROR_MODE_AUTO, &transform); |
| 684 | if (ret == OK) { |
| 685 | // It is possible for camera providers to return the capture |
| 686 | // results after the processed frames. In such scenario, we will |
| 687 | // not be able to set the output transformation before the frames |
| 688 | // return back to the consumer for the current capture request |
| 689 | // but we could still try and configure it for any future requests |
| 690 | // that are still in flight. The assumption is that the physical |
| 691 | // device id remains the same for the duration of the pending queue. |
| 692 | for (size_t i = 0; i < states.inflightMap.size(); i++) { |
| 693 | auto &r = states.inflightMap.editValueAt(i); |
| 694 | if (r.requestTimeNs >= request.requestTimeNs) { |
| 695 | r.transform = transform; |
| 696 | } |
| 697 | } |
| 698 | } else { |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 699 | ALOGE("%s: Failed to calculate current stream transformation: %s " |
| 700 | "(%d)", __FUNCTION__, strerror(-ret), ret); |
| 701 | } |
| 702 | } else { |
| 703 | ALOGE("%s: Physical device orientation absent!", __FUNCTION__); |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 704 | } |
| 705 | } else { |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 706 | ALOGE("%s: Physical device not found in device info map found!", |
| 707 | __FUNCTION__); |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 708 | } |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 713 | // Check if this result carries only partial metadata |
| 714 | if (states.usePartialResult && result->result != NULL) { |
| 715 | if (result->partial_result > states.numPartialResults || result->partial_result < 1) { |
| 716 | SET_ERR("Result is malformed for frame %d: partial_result %u must be in" |
| 717 | " the range of [1, %d] when metadata is included in the result", |
| 718 | frameNumber, result->partial_result, states.numPartialResults); |
| 719 | return; |
| 720 | } |
| 721 | isPartialResult = (result->partial_result < states.numPartialResults); |
| 722 | if (isPartialResult && result->num_physcam_metadata) { |
| 723 | SET_ERR("Result is malformed for frame %d: partial_result not allowed for" |
| 724 | " physical camera result", frameNumber); |
| 725 | return; |
| 726 | } |
| 727 | if (isPartialResult) { |
| 728 | request.collectedPartialResult.append(result->result); |
| 729 | } |
| 730 | |
| 731 | if (isPartialResult && request.hasCallback) { |
| 732 | // Send partial capture result |
| 733 | sendPartialCaptureResult(states, result->result, request.resultExtras, |
| 734 | frameNumber); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | shutterTimestamp = request.shutterTimestamp; |
| 739 | hasInputBufferInRequest = request.hasInputBuffer; |
| 740 | |
| 741 | // Did we get the (final) result metadata for this capture? |
| 742 | if (result->result != NULL && !isPartialResult) { |
| 743 | if (request.physicalCameraIds.size() != result->num_physcam_metadata) { |
| 744 | SET_ERR("Expected physical Camera metadata count %d not equal to actual count %d", |
| 745 | request.physicalCameraIds.size(), result->num_physcam_metadata); |
| 746 | return; |
| 747 | } |
| 748 | if (request.haveResultMetadata) { |
| 749 | SET_ERR("Called multiple times with metadata for frame %d", |
| 750 | frameNumber); |
| 751 | return; |
| 752 | } |
| 753 | for (uint32_t i = 0; i < result->num_physcam_metadata; i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 754 | const std::string physicalId = result->physcam_ids[i]; |
Shuzhen Wang | 9908050 | 2021-03-07 21:08:20 -0800 | [diff] [blame] | 755 | bool validPhysicalCameraMetadata = |
| 756 | erasePhysicalCameraIdSet(request.physicalCameraIds, physicalId); |
| 757 | if (!validPhysicalCameraMetadata) { |
| 758 | SET_ERR("Unexpected total result for frame %d camera %s", |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 759 | frameNumber, physicalId.c_str()); |
| 760 | return; |
| 761 | } |
| 762 | } |
| 763 | if (states.usePartialResult && |
| 764 | !request.collectedPartialResult.isEmpty()) { |
| 765 | collectedPartialResult.acquire( |
| 766 | request.collectedPartialResult); |
| 767 | } |
| 768 | request.haveResultMetadata = true; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 769 | request.errorBufStrategy = ERROR_BUF_RETURN_NOTIFY; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | uint32_t numBuffersReturned = result->num_output_buffers; |
| 773 | if (result->input_buffer != NULL) { |
| 774 | if (hasInputBufferInRequest) { |
| 775 | numBuffersReturned += 1; |
| 776 | } else { |
| 777 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 778 | " buffer sent in the request", |
| 779 | __FUNCTION__); |
| 780 | } |
| 781 | } |
| 782 | request.numBuffersLeft -= numBuffersReturned; |
| 783 | if (request.numBuffersLeft < 0) { |
| 784 | SET_ERR("Too many buffers returned for frame %d", |
| 785 | frameNumber); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | camera_metadata_ro_entry_t entry; |
| 790 | res = find_camera_metadata_ro_entry(result->result, |
| 791 | ANDROID_SENSOR_TIMESTAMP, &entry); |
| 792 | if (res == OK && entry.count == 1) { |
| 793 | request.sensorTimestamp = entry.data.i64[0]; |
| 794 | } |
| 795 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 796 | // If shutter event isn't received yet, do not return the pending output |
| 797 | // buffers. |
| 798 | request.pendingOutputBuffers.appendArray(result->output_buffers, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 799 | result->num_output_buffers); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 800 | if (shutterTimestamp != 0) { |
| 801 | returnAndRemovePendingOutputBuffers( |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 802 | states.useHalBufManager, states.halBufManagedStreamIds, |
| 803 | states.listener, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 804 | request, states.sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | if (result->result != NULL && !isPartialResult) { |
| 808 | for (uint32_t i = 0; i < result->num_physcam_metadata; i++) { |
| 809 | CameraMetadata physicalMetadata; |
| 810 | physicalMetadata.append(result->physcam_metadata[i]); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 811 | request.physicalMetadatas.push_back({result->physcam_ids[i], |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 812 | physicalMetadata}); |
| 813 | } |
| 814 | if (shutterTimestamp == 0) { |
| 815 | request.pendingMetadata = result->result; |
| 816 | request.collectedPartialResult = collectedPartialResult; |
| 817 | } else if (request.hasCallback) { |
| 818 | CameraMetadata metadata; |
| 819 | metadata = result->result; |
Shuzhen Wang | 3c00b09 | 2022-11-08 22:53:07 +0000 | [diff] [blame] | 820 | auto cameraIdsWithZoom = getCameraIdsWithZoomLocked( |
| 821 | states.inflightMap, metadata, request.cameraIdsWithZoom); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 822 | sendCaptureResult(states, metadata, request.resultExtras, |
| 823 | collectedPartialResult, frameNumber, |
| 824 | hasInputBufferInRequest, request.zslCapture && request.stillCapture, |
Shuzhen Wang | 3c00b09 | 2022-11-08 22:53:07 +0000 | [diff] [blame] | 825 | request.rotateAndCropAuto, cameraIdsWithZoom, |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 826 | request.physicalMetadatas); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | removeInFlightRequestIfReadyLocked(states, idx); |
| 830 | } // scope for states.inFlightLock |
| 831 | |
| 832 | if (result->input_buffer != NULL) { |
| 833 | if (hasInputBufferInRequest) { |
| 834 | Camera3Stream *stream = |
| 835 | Camera3Stream::cast(result->input_buffer->stream); |
| 836 | res = stream->returnInputBuffer(*(result->input_buffer)); |
| 837 | // Note: stream may be deallocated at this point, if this buffer was the |
| 838 | // last reference to it. |
| 839 | if (res != OK) { |
| 840 | ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" |
| 841 | " its stream:%s (%d)", __FUNCTION__, |
| 842 | frameNumber, strerror(-res), res); |
| 843 | } |
| 844 | } else { |
| 845 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 846 | " buffer sent in the request, skipping input buffer return.", |
| 847 | __FUNCTION__); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 852 | void returnOutputBuffers( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 853 | bool useHalBufManager, |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 854 | const std::set<int32_t> &halBufferManagedStreams, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 855 | sp<NotificationListener> listener, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 856 | const camera_stream_buffer_t *outputBuffers, size_t numBuffers, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 857 | nsecs_t timestamp, nsecs_t readoutTimestamp, bool requested, |
| 858 | nsecs_t requestTimeNs, SessionStatsBuilder& sessionStatsBuilder, |
| 859 | bool timestampIncreasing, const SurfaceMap& outputSurfaces, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 860 | const CaptureResultExtras &inResultExtras, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 861 | ERROR_BUF_STRATEGY errorBufStrategy, int32_t transform) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 862 | |
| 863 | for (size_t i = 0; i < numBuffers; i++) |
| 864 | { |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 865 | Camera3StreamInterface *stream = Camera3Stream::cast(outputBuffers[i].stream); |
| 866 | int streamId = stream->getId(); |
| 867 | |
| 868 | // Call notify(ERROR_BUFFER) if necessary. |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 869 | if (outputBuffers[i].status == CAMERA_BUFFER_STATUS_ERROR && |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 870 | errorBufStrategy == ERROR_BUF_RETURN_NOTIFY) { |
| 871 | if (listener != nullptr) { |
| 872 | CaptureResultExtras extras = inResultExtras; |
| 873 | extras.errorStreamId = streamId; |
| 874 | listener->notifyError( |
| 875 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER, |
| 876 | extras); |
| 877 | } |
| 878 | } |
| 879 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 880 | if (outputBuffers[i].buffer == nullptr) { |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 881 | if (!useHalBufManager && |
| 882 | !(flags::session_hal_buf_manager() && |
| 883 | contains(halBufferManagedStreams, streamId))) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 884 | // With HAL buffer management API, HAL sometimes will have to return buffers that |
| 885 | // has not got a output buffer handle filled yet. This is though illegal if HAL |
| 886 | // buffer management API is not being used. |
| 887 | ALOGE("%s: cannot return a null buffer!", __FUNCTION__); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 888 | } else { |
| 889 | if (requested) { |
| 890 | sessionStatsBuilder.incCounter(streamId, /*dropped*/true, 0); |
| 891 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 892 | } |
| 893 | continue; |
| 894 | } |
| 895 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 896 | const auto& it = outputSurfaces.find(streamId); |
| 897 | status_t res = OK; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 898 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 899 | // Do not return the buffer if the buffer status is error, and the error |
| 900 | // buffer strategy is CACHE. |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 901 | if (outputBuffers[i].status != CAMERA_BUFFER_STATUS_ERROR || |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 902 | errorBufStrategy != ERROR_BUF_CACHE) { |
| 903 | if (it != outputSurfaces.end()) { |
| 904 | res = stream->returnBuffer( |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 905 | outputBuffers[i], timestamp, readoutTimestamp, timestampIncreasing, |
| 906 | it->second, inResultExtras.frameNumber, transform); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 907 | } else { |
| 908 | res = stream->returnBuffer( |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 909 | outputBuffers[i], timestamp, readoutTimestamp, timestampIncreasing, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 910 | std::vector<size_t> (), inResultExtras.frameNumber, transform); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 913 | // Note: stream may be deallocated at this point, if this buffer was |
| 914 | // the last reference to it. |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 915 | bool dropped = false; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 916 | if (res == NO_INIT || res == DEAD_OBJECT) { |
| 917 | ALOGV("Can't return buffer to its stream: %s (%d)", strerror(-res), res); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 918 | sessionStatsBuilder.stopCounter(streamId); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 919 | } else if (res != OK) { |
| 920 | ALOGE("Can't return buffer to its stream: %s (%d)", strerror(-res), res); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 921 | dropped = true; |
| 922 | } else { |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 923 | if (outputBuffers[i].status == CAMERA_BUFFER_STATUS_ERROR || timestamp == 0) { |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 924 | dropped = true; |
| 925 | } |
| 926 | } |
| 927 | if (requested) { |
| 928 | nsecs_t bufferTimeNs = systemTime(); |
| 929 | int32_t captureLatencyMs = ns2ms(bufferTimeNs - requestTimeNs); |
| 930 | sessionStatsBuilder.incCounter(streamId, dropped, captureLatencyMs); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | // Long processing consumers can cause returnBuffer timeout for shared stream |
| 934 | // If that happens, cancel the buffer and send a buffer error to client |
| 935 | if (it != outputSurfaces.end() && res == TIMED_OUT && |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 936 | outputBuffers[i].status == CAMERA_BUFFER_STATUS_OK) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 937 | // cancel the buffer |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 938 | camera_stream_buffer_t sb = outputBuffers[i]; |
| 939 | sb.status = CAMERA_BUFFER_STATUS_ERROR; |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 940 | stream->returnBuffer(sb, /*timestamp*/0, /*readoutTimestamp*/0, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 941 | timestampIncreasing, std::vector<size_t> (), |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 942 | inResultExtras.frameNumber, transform); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 943 | |
| 944 | if (listener != nullptr) { |
| 945 | CaptureResultExtras extras = inResultExtras; |
| 946 | extras.errorStreamId = streamId; |
| 947 | listener->notifyError( |
| 948 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER, |
| 949 | extras); |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 955 | void returnAndRemovePendingOutputBuffers(bool useHalBufManager, |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 956 | const std::set<int32_t> &halBufferManagedStreams, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 957 | sp<NotificationListener> listener, InFlightRequest& request, |
| 958 | SessionStatsBuilder& sessionStatsBuilder) { |
Shuzhen Wang | 828ade3 | 2022-01-10 17:19:02 -0800 | [diff] [blame] | 959 | bool timestampIncreasing = |
| 960 | !((request.zslCapture && request.stillCapture) || request.hasInputBuffer); |
Shuzhen Wang | ffc4c01 | 2022-04-20 15:55:46 -0700 | [diff] [blame] | 961 | nsecs_t readoutTimestamp = request.resultExtras.hasReadoutTimestamp ? |
| 962 | request.resultExtras.readoutTimestamp : 0; |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 963 | returnOutputBuffers(useHalBufManager, halBufferManagedStreams, listener, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 964 | request.pendingOutputBuffers.array(), |
| 965 | request.pendingOutputBuffers.size(), |
Shuzhen Wang | ffc4c01 | 2022-04-20 15:55:46 -0700 | [diff] [blame] | 966 | request.shutterTimestamp, readoutTimestamp, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 967 | /*requested*/true, request.requestTimeNs, sessionStatsBuilder, timestampIncreasing, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 968 | request.outputSurfaces, request.resultExtras, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 969 | request.errorBufStrategy, request.transform); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 970 | |
| 971 | // Remove error buffers that are not cached. |
| 972 | for (auto iter = request.pendingOutputBuffers.begin(); |
| 973 | iter != request.pendingOutputBuffers.end(); ) { |
| 974 | if (request.errorBufStrategy != ERROR_BUF_CACHE || |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 975 | iter->status != CAMERA_BUFFER_STATUS_ERROR) { |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 976 | iter = request.pendingOutputBuffers.erase(iter); |
| 977 | } else { |
| 978 | iter++; |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 983 | void notifyShutter(CaptureOutputStates& states, const camera_shutter_msg_t &msg) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 984 | ATRACE_CALL(); |
| 985 | ssize_t idx; |
| 986 | |
| 987 | // Set timestamp for the request in the in-flight tracking |
| 988 | // and get the request ID to send upstream |
| 989 | { |
| 990 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 991 | InFlightRequestMap& inflightMap = states.inflightMap; |
| 992 | idx = inflightMap.indexOfKey(msg.frame_number); |
| 993 | if (idx >= 0) { |
| 994 | InFlightRequest &r = inflightMap.editValueAt(idx); |
| 995 | |
| 996 | // Verify ordering of shutter notifications |
| 997 | { |
| 998 | std::lock_guard<std::mutex> l(states.outputLock); |
| 999 | // TODO: need to track errors for tighter bounds on expected frame number. |
| 1000 | if (r.hasInputBuffer) { |
| 1001 | if (msg.frame_number < states.nextReprocShutterFrameNum) { |
| 1002 | SET_ERR("Reprocess shutter notification out-of-order. Expected " |
| 1003 | "notification for frame %d, got frame %d", |
| 1004 | states.nextReprocShutterFrameNum, msg.frame_number); |
| 1005 | return; |
| 1006 | } |
| 1007 | states.nextReprocShutterFrameNum = msg.frame_number + 1; |
| 1008 | } else if (r.zslCapture && r.stillCapture) { |
| 1009 | if (msg.frame_number < states.nextZslShutterFrameNum) { |
| 1010 | SET_ERR("ZSL still capture shutter notification out-of-order. Expected " |
| 1011 | "notification for frame %d, got frame %d", |
| 1012 | states.nextZslShutterFrameNum, msg.frame_number); |
| 1013 | return; |
| 1014 | } |
| 1015 | states.nextZslShutterFrameNum = msg.frame_number + 1; |
| 1016 | } else { |
| 1017 | if (msg.frame_number < states.nextShutterFrameNum) { |
| 1018 | SET_ERR("Shutter notification out-of-order. Expected " |
| 1019 | "notification for frame %d, got frame %d", |
| 1020 | states.nextShutterFrameNum, msg.frame_number); |
| 1021 | return; |
| 1022 | } |
| 1023 | states.nextShutterFrameNum = msg.frame_number + 1; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | r.shutterTimestamp = msg.timestamp; |
Shuzhen Wang | ffc4c01 | 2022-04-20 15:55:46 -0700 | [diff] [blame] | 1028 | if (msg.readout_timestamp_valid) { |
| 1029 | r.resultExtras.hasReadoutTimestamp = true; |
| 1030 | r.resultExtras.readoutTimestamp = msg.readout_timestamp; |
| 1031 | } |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1032 | if (r.minExpectedDuration != states.minFrameDuration || |
| 1033 | r.isFixedFps != states.isFixedFps) { |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1034 | for (size_t i = 0; i < states.outputStreams.size(); i++) { |
| 1035 | auto outputStream = states.outputStreams[i]; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1036 | outputStream->onMinDurationChanged(r.minExpectedDuration, r.isFixedFps); |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1037 | } |
| 1038 | states.minFrameDuration = r.minExpectedDuration; |
Shuzhen Wang | 696e4da | 2022-09-08 14:31:13 -0700 | [diff] [blame] | 1039 | states.isFixedFps = r.isFixedFps; |
Shuzhen Wang | 00abbeb | 2022-02-25 17:14:42 -0800 | [diff] [blame] | 1040 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1041 | if (r.hasCallback) { |
| 1042 | ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1043 | states.cameraId.c_str(), __FUNCTION__, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1044 | msg.frame_number, r.resultExtras.requestId, msg.timestamp); |
| 1045 | // Call listener, if any |
| 1046 | if (states.listener != nullptr) { |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1047 | r.resultExtras.lastCompletedRegularFrameNumber = |
| 1048 | states.lastCompletedRegularFrameNumber; |
| 1049 | r.resultExtras.lastCompletedReprocessFrameNumber = |
| 1050 | states.lastCompletedReprocessFrameNumber; |
| 1051 | r.resultExtras.lastCompletedZslFrameNumber = |
| 1052 | states.lastCompletedZslFrameNumber; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1053 | states.listener->notifyShutter(r.resultExtras, msg.timestamp); |
| 1054 | } |
| 1055 | // send pending result and buffers |
Shuzhen Wang | 3c00b09 | 2022-11-08 22:53:07 +0000 | [diff] [blame] | 1056 | const auto& cameraIdsWithZoom = getCameraIdsWithZoomLocked( |
| 1057 | inflightMap, r.pendingMetadata, r.cameraIdsWithZoom); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1058 | sendCaptureResult(states, |
| 1059 | r.pendingMetadata, r.resultExtras, |
| 1060 | r.collectedPartialResult, msg.frame_number, |
| 1061 | r.hasInputBuffer, r.zslCapture && r.stillCapture, |
Shuzhen Wang | 3c00b09 | 2022-11-08 22:53:07 +0000 | [diff] [blame] | 1062 | r.rotateAndCropAuto, cameraIdsWithZoom, r.physicalMetadatas); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1063 | } |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1064 | returnAndRemovePendingOutputBuffers( |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 1065 | states.useHalBufManager, states.halBufManagedStreamIds, |
| 1066 | states.listener, r, states.sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1067 | |
| 1068 | removeInFlightRequestIfReadyLocked(states, idx); |
| 1069 | } |
| 1070 | } |
| 1071 | if (idx < 0) { |
| 1072 | SET_ERR("Shutter notification for non-existent frame number %d", |
| 1073 | msg.frame_number); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1077 | void notifyError(CaptureOutputStates& states, const camera_error_msg_t &msg) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1078 | ATRACE_CALL(); |
| 1079 | // Map camera HAL error codes to ICameraDeviceCallback error codes |
| 1080 | // Index into this with the HAL error code |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1081 | static const int32_t halErrorMap[CAMERA_MSG_NUM_ERRORS] = { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1082 | // 0 = Unused error code |
| 1083 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1084 | // 1 = CAMERA_MSG_ERROR_DEVICE |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1085 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1086 | // 2 = CAMERA_MSG_ERROR_REQUEST |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1087 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1088 | // 3 = CAMERA_MSG_ERROR_RESULT |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1089 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1090 | // 4 = CAMERA_MSG_ERROR_BUFFER |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1091 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER |
| 1092 | }; |
| 1093 | |
| 1094 | int32_t errorCode = |
| 1095 | ((msg.error_code >= 0) && |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1096 | (msg.error_code < CAMERA_MSG_NUM_ERRORS)) ? |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1097 | halErrorMap[msg.error_code] : |
| 1098 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; |
| 1099 | |
| 1100 | int streamId = 0; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1101 | std::string physicalCameraId; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1102 | if (msg.error_stream != nullptr) { |
| 1103 | Camera3Stream *stream = |
| 1104 | Camera3Stream::cast(msg.error_stream); |
| 1105 | streamId = stream->getId(); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1106 | physicalCameraId = stream->physicalCameraId(); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1107 | } |
| 1108 | ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1109 | states.cameraId.c_str(), __FUNCTION__, msg.frame_number, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1110 | streamId, msg.error_code); |
| 1111 | |
| 1112 | CaptureResultExtras resultExtras; |
| 1113 | switch (errorCode) { |
| 1114 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: |
| 1115 | // SET_ERR calls into listener to notify application |
| 1116 | SET_ERR("Camera HAL reported serious device error"); |
| 1117 | break; |
| 1118 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: |
| 1119 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1120 | { |
| 1121 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 1122 | ssize_t idx = states.inflightMap.indexOfKey(msg.frame_number); |
| 1123 | if (idx >= 0) { |
| 1124 | InFlightRequest &r = states.inflightMap.editValueAt(idx); |
| 1125 | r.requestStatus = msg.error_code; |
| 1126 | resultExtras = r.resultExtras; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1127 | bool physicalDeviceResultError = false; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1128 | if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == |
| 1129 | errorCode) { |
| 1130 | if (physicalCameraId.size() > 0) { |
Shuzhen Wang | 9908050 | 2021-03-07 21:08:20 -0800 | [diff] [blame] | 1131 | bool validPhysicalCameraId = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1132 | erasePhysicalCameraIdSet(r.physicalCameraIds, physicalCameraId); |
Shuzhen Wang | 9908050 | 2021-03-07 21:08:20 -0800 | [diff] [blame] | 1133 | if (!validPhysicalCameraId) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1134 | ALOGE("%s: Reported result failure for physical camera device: %s " |
| 1135 | " which is not part of the respective request!", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1136 | __FUNCTION__, physicalCameraId.c_str()); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1137 | break; |
| 1138 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1139 | resultExtras.errorPhysicalCameraId = physicalCameraId; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1140 | physicalDeviceResultError = true; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1144 | if (!physicalDeviceResultError) { |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 1145 | r.skipResultMetadata = true; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1146 | if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT |
| 1147 | == errorCode) { |
| 1148 | r.errorBufStrategy = ERROR_BUF_RETURN_NOTIFY; |
| 1149 | } else { |
| 1150 | // errorCode is ERROR_CAMERA_REQUEST |
| 1151 | r.errorBufStrategy = ERROR_BUF_RETURN; |
| 1152 | } |
| 1153 | |
| 1154 | // Check whether the buffers returned. If they returned, |
| 1155 | // remove inflight request. |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1156 | removeInFlightRequestIfReadyLocked(states, idx); |
| 1157 | } |
| 1158 | } else { |
| 1159 | resultExtras.frameNumber = msg.frame_number; |
| 1160 | ALOGE("Camera %s: %s: cannot find in-flight request on " |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1161 | "frame %" PRId64 " error", states.cameraId.c_str(), __FUNCTION__, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1162 | resultExtras.frameNumber); |
| 1163 | } |
| 1164 | } |
| 1165 | resultExtras.errorStreamId = streamId; |
| 1166 | if (states.listener != nullptr) { |
| 1167 | states.listener->notifyError(errorCode, resultExtras); |
| 1168 | } else { |
| 1169 | ALOGE("Camera %s: %s: no listener available", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 1170 | states.cameraId.c_str(), __FUNCTION__); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1171 | } |
| 1172 | break; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1173 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: |
| 1174 | // Do not depend on HAL ERROR_CAMERA_BUFFER to send buffer error |
| 1175 | // callback to the app. Rather, use STATUS_ERROR of image buffers. |
| 1176 | break; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1177 | default: |
| 1178 | // SET_ERR calls notifyError |
| 1179 | SET_ERR("Unknown error message from HAL: %d", msg.error_code); |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1184 | void notify(CaptureOutputStates& states, const camera_notify_msg *msg) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1185 | switch (msg->type) { |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1186 | case CAMERA_MSG_ERROR: { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1187 | notifyError(states, msg->message.error); |
| 1188 | break; |
| 1189 | } |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1190 | case CAMERA_MSG_SHUTTER: { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1191 | notifyShutter(states, msg->message.shutter); |
| 1192 | break; |
| 1193 | } |
| 1194 | default: |
| 1195 | SET_ERR("Unknown notify message from HAL: %d", |
| 1196 | msg->type); |
| 1197 | } |
| 1198 | } |
| 1199 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1200 | void flushInflightRequests(FlushInflightReqStates& states) { |
| 1201 | ATRACE_CALL(); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1202 | { // First return buffers cached in inFlightMap |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1203 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 1204 | for (size_t idx = 0; idx < states.inflightMap.size(); idx++) { |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 1205 | const InFlightRequest &request = states.inflightMap.valueAt(idx); |
| 1206 | returnOutputBuffers( |
Jayant Chowdhary | 025e97f | 2023-12-12 22:15:57 +0000 | [diff] [blame^] | 1207 | states.useHalBufManager, states.halBufManagedStreamIds, |
| 1208 | states.listener, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1209 | request.pendingOutputBuffers.array(), |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 1210 | request.pendingOutputBuffers.size(), /*timestamp*/0, /*readoutTimestamp*/0, |
| 1211 | /*requested*/true, request.requestTimeNs, states.sessionStatsBuilder, |
| 1212 | /*timestampIncreasing*/true, request.outputSurfaces, request.resultExtras, |
| 1213 | request.errorBufStrategy); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1214 | ALOGW("%s: Frame %d | Timestamp: %" PRId64 ", metadata" |
| 1215 | " arrived: %s, buffers left: %d.\n", __FUNCTION__, |
| 1216 | states.inflightMap.keyAt(idx), request.shutterTimestamp, |
| 1217 | request.haveResultMetadata ? "true" : "false", |
| 1218 | request.numBuffersLeft); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1219 | } |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1220 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1221 | states.inflightMap.clear(); |
| 1222 | states.inflightIntf.onInflightMapFlushedLocked(); |
| 1223 | } |
| 1224 | |
| 1225 | // Then return all inflight buffers not returned by HAL |
| 1226 | std::vector<std::pair<int32_t, int32_t>> inflightKeys; |
| 1227 | states.flushBufferIntf.getInflightBufferKeys(&inflightKeys); |
| 1228 | |
| 1229 | // Inflight buffers for HAL buffer manager |
| 1230 | std::vector<uint64_t> inflightRequestBufferKeys; |
| 1231 | states.flushBufferIntf.getInflightRequestBufferKeys(&inflightRequestBufferKeys); |
| 1232 | |
| 1233 | // (streamId, frameNumber, buffer_handle_t*) tuple for all inflight buffers. |
| 1234 | // frameNumber will be -1 for buffers from HAL buffer manager |
| 1235 | std::vector<std::tuple<int32_t, int32_t, buffer_handle_t*>> inflightBuffers; |
| 1236 | inflightBuffers.reserve(inflightKeys.size() + inflightRequestBufferKeys.size()); |
| 1237 | |
| 1238 | for (auto& pair : inflightKeys) { |
| 1239 | int32_t frameNumber = pair.first; |
| 1240 | int32_t streamId = pair.second; |
| 1241 | buffer_handle_t* buffer; |
| 1242 | status_t res = states.bufferRecordsIntf.popInflightBuffer(frameNumber, streamId, &buffer); |
| 1243 | if (res != OK) { |
| 1244 | ALOGE("%s: Frame %d: No in-flight buffer for stream %d", |
| 1245 | __FUNCTION__, frameNumber, streamId); |
| 1246 | continue; |
| 1247 | } |
| 1248 | inflightBuffers.push_back(std::make_tuple(streamId, frameNumber, buffer)); |
| 1249 | } |
| 1250 | |
| 1251 | for (auto& bufferId : inflightRequestBufferKeys) { |
| 1252 | int32_t streamId = -1; |
| 1253 | buffer_handle_t* buffer = nullptr; |
| 1254 | status_t res = states.bufferRecordsIntf.popInflightRequestBuffer( |
| 1255 | bufferId, &buffer, &streamId); |
| 1256 | if (res != OK) { |
| 1257 | ALOGE("%s: cannot find in-flight buffer %" PRIu64, __FUNCTION__, bufferId); |
| 1258 | continue; |
| 1259 | } |
| 1260 | inflightBuffers.push_back(std::make_tuple(streamId, /*frameNumber*/-1, buffer)); |
| 1261 | } |
| 1262 | |
| 1263 | std::vector<sp<Camera3StreamInterface>> streams = states.flushBufferIntf.getAllStreams(); |
| 1264 | |
| 1265 | for (auto& tuple : inflightBuffers) { |
| 1266 | status_t res = OK; |
| 1267 | int32_t streamId = std::get<0>(tuple); |
| 1268 | int32_t frameNumber = std::get<1>(tuple); |
| 1269 | buffer_handle_t* buffer = std::get<2>(tuple); |
| 1270 | |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1271 | camera_stream_buffer_t streamBuffer; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1272 | streamBuffer.buffer = buffer; |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1273 | streamBuffer.status = CAMERA_BUFFER_STATUS_ERROR; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1274 | streamBuffer.acquire_fence = -1; |
| 1275 | streamBuffer.release_fence = -1; |
| 1276 | |
| 1277 | for (auto& stream : streams) { |
| 1278 | if (streamId == stream->getId()) { |
| 1279 | // Return buffer to deleted stream |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1280 | camera_stream* halStream = stream->asHalStream(); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1281 | streamBuffer.stream = halStream; |
| 1282 | switch (halStream->stream_type) { |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1283 | case CAMERA_STREAM_OUTPUT: |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1284 | res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0, |
Shuzhen Wang | 90708ea | 2021-11-04 11:40:49 -0700 | [diff] [blame] | 1285 | /*readoutTimestamp*/0, /*timestampIncreasing*/true, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1286 | std::vector<size_t> (), frameNumber); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1287 | if (res != OK) { |
| 1288 | ALOGE("%s: Can't return output buffer for frame %d to" |
| 1289 | " stream %d: %s (%d)", __FUNCTION__, |
| 1290 | frameNumber, streamId, strerror(-res), res); |
| 1291 | } |
| 1292 | break; |
Emilian Peev | f481670 | 2020-04-03 15:44:51 -0700 | [diff] [blame] | 1293 | case CAMERA_STREAM_INPUT: |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1294 | res = stream->returnInputBuffer(streamBuffer); |
| 1295 | if (res != OK) { |
| 1296 | ALOGE("%s: Can't return input buffer for frame %d to" |
| 1297 | " stream %d: %s (%d)", __FUNCTION__, |
| 1298 | frameNumber, streamId, strerror(-res), res); |
| 1299 | } |
| 1300 | break; |
| 1301 | default: // Bi-direcitonal stream is deprecated |
| 1302 | ALOGE("%s: stream %d has unknown stream type %d", |
| 1303 | __FUNCTION__, streamId, halStream->stream_type); |
| 1304 | break; |
| 1305 | } |
| 1306 | break; |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | } // camera3 |
| 1313 | } // namespace android |