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