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