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 | |
| 45 | #include <camera_metadata_hidden.h> |
| 46 | |
| 47 | #include "device3/Camera3OutputUtils.h" |
| 48 | |
| 49 | using namespace android::camera3; |
| 50 | using namespace android::hardware::camera; |
| 51 | |
| 52 | namespace android { |
| 53 | namespace camera3 { |
| 54 | |
| 55 | status_t fixupMonochromeTags( |
| 56 | CaptureOutputStates& states, |
| 57 | const CameraMetadata& deviceInfo, |
| 58 | CameraMetadata& resultMetadata) { |
| 59 | status_t res = OK; |
| 60 | if (!states.needFixupMonoChrome) { |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | // Remove tags that are not applicable to monochrome camera. |
| 65 | int32_t tagsToRemove[] = { |
| 66 | ANDROID_SENSOR_GREEN_SPLIT, |
| 67 | ANDROID_SENSOR_NEUTRAL_COLOR_POINT, |
| 68 | ANDROID_COLOR_CORRECTION_MODE, |
| 69 | ANDROID_COLOR_CORRECTION_TRANSFORM, |
| 70 | ANDROID_COLOR_CORRECTION_GAINS, |
| 71 | }; |
| 72 | for (auto tag : tagsToRemove) { |
| 73 | res = resultMetadata.erase(tag); |
| 74 | if (res != OK) { |
| 75 | ALOGE("%s: Failed to remove tag %d for monochrome camera", __FUNCTION__, tag); |
| 76 | return res; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL |
| 81 | camera_metadata_entry blEntry = resultMetadata.find(ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL); |
| 82 | for (size_t i = 1; i < blEntry.count; i++) { |
| 83 | blEntry.data.f[i] = blEntry.data.f[0]; |
| 84 | } |
| 85 | |
| 86 | // ANDROID_SENSOR_NOISE_PROFILE |
| 87 | camera_metadata_entry npEntry = resultMetadata.find(ANDROID_SENSOR_NOISE_PROFILE); |
| 88 | if (npEntry.count > 0 && npEntry.count % 2 == 0) { |
| 89 | double np[] = {npEntry.data.d[0], npEntry.data.d[1]}; |
| 90 | res = resultMetadata.update(ANDROID_SENSOR_NOISE_PROFILE, np, 2); |
| 91 | if (res != OK) { |
| 92 | ALOGE("%s: Failed to update SENSOR_NOISE_PROFILE: %s (%d)", |
| 93 | __FUNCTION__, strerror(-res), res); |
| 94 | return res; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // ANDROID_STATISTICS_LENS_SHADING_MAP |
| 99 | camera_metadata_ro_entry lsSizeEntry = deviceInfo.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE); |
| 100 | camera_metadata_entry lsEntry = resultMetadata.find(ANDROID_STATISTICS_LENS_SHADING_MAP); |
| 101 | if (lsSizeEntry.count == 2 && lsEntry.count > 0 |
| 102 | && (int32_t)lsEntry.count == 4 * lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]) { |
| 103 | for (int32_t i = 0; i < lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]; i++) { |
| 104 | lsEntry.data.f[4*i+1] = lsEntry.data.f[4*i]; |
| 105 | lsEntry.data.f[4*i+2] = lsEntry.data.f[4*i]; |
| 106 | lsEntry.data.f[4*i+3] = lsEntry.data.f[4*i]; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // ANDROID_TONEMAP_CURVE_BLUE |
| 111 | // ANDROID_TONEMAP_CURVE_GREEN |
| 112 | // ANDROID_TONEMAP_CURVE_RED |
| 113 | camera_metadata_entry tcbEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_BLUE); |
| 114 | camera_metadata_entry tcgEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_GREEN); |
| 115 | camera_metadata_entry tcrEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_RED); |
| 116 | if (tcbEntry.count > 0 |
| 117 | && tcbEntry.count == tcgEntry.count |
| 118 | && tcbEntry.count == tcrEntry.count) { |
| 119 | for (size_t i = 0; i < tcbEntry.count; i++) { |
| 120 | tcbEntry.data.f[i] = tcrEntry.data.f[i]; |
| 121 | tcgEntry.data.f[i] = tcrEntry.data.f[i]; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return res; |
| 126 | } |
| 127 | |
| 128 | void insertResultLocked(CaptureOutputStates& states, CaptureResult *result, uint32_t frameNumber) { |
| 129 | if (result == nullptr) return; |
| 130 | |
| 131 | camera_metadata_t *meta = const_cast<camera_metadata_t *>( |
| 132 | result->mMetadata.getAndLock()); |
| 133 | set_camera_metadata_vendor_id(meta, states.vendorTagId); |
| 134 | result->mMetadata.unlock(meta); |
| 135 | |
| 136 | if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, |
| 137 | (int32_t*)&frameNumber, 1) != OK) { |
| 138 | SET_ERR("Failed to set frame number %d in metadata", frameNumber); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) { |
| 143 | SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | // Update vendor tag id for physical metadata |
| 148 | for (auto& physicalMetadata : result->mPhysicalMetadatas) { |
| 149 | camera_metadata_t *pmeta = const_cast<camera_metadata_t *>( |
| 150 | physicalMetadata.mPhysicalCameraMetadata.getAndLock()); |
| 151 | set_camera_metadata_vendor_id(pmeta, states.vendorTagId); |
| 152 | physicalMetadata.mPhysicalCameraMetadata.unlock(pmeta); |
| 153 | } |
| 154 | |
| 155 | // Valid result, insert into queue |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 156 | std::list<CaptureResult>::iterator queuedResult = |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 157 | states.resultQueue.insert(states.resultQueue.end(), CaptureResult(*result)); |
Jayant Chowdhary | 8a0be29 | 2020-01-08 13:10:38 -0800 | [diff] [blame] | 158 | ALOGV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 159 | ", burstId = %" PRId32, __FUNCTION__, |
| 160 | queuedResult->mResultExtras.requestId, |
| 161 | queuedResult->mResultExtras.frameNumber, |
| 162 | queuedResult->mResultExtras.burstId); |
| 163 | |
| 164 | states.resultSignal.notify_one(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | void sendPartialCaptureResult(CaptureOutputStates& states, |
| 169 | const camera_metadata_t * partialResult, |
| 170 | const CaptureResultExtras &resultExtras, uint32_t frameNumber) { |
| 171 | ATRACE_CALL(); |
| 172 | std::lock_guard<std::mutex> l(states.outputLock); |
| 173 | |
| 174 | CaptureResult captureResult; |
| 175 | captureResult.mResultExtras = resultExtras; |
| 176 | captureResult.mMetadata = partialResult; |
| 177 | |
| 178 | // Fix up result metadata for monochrome camera. |
| 179 | status_t res = fixupMonochromeTags(states, states.deviceInfo, captureResult.mMetadata); |
| 180 | if (res != OK) { |
| 181 | SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res); |
| 182 | return; |
| 183 | } |
| 184 | |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 185 | // Update partial result by removing keys remapped by DistortionCorrection, ZoomRatio, |
| 186 | // and RotationAndCrop mappers. |
| 187 | std::set<uint32_t> keysToRemove; |
| 188 | |
| 189 | auto iter = states.distortionMappers.find(states.cameraId.c_str()); |
| 190 | if (iter != states.distortionMappers.end()) { |
| 191 | const auto& remappedKeys = iter->second.getRemappedKeys(); |
| 192 | keysToRemove.insert(remappedKeys.begin(), remappedKeys.end()); |
| 193 | } |
| 194 | |
| 195 | const auto& remappedKeys = states.zoomRatioMappers[states.cameraId.c_str()].getRemappedKeys(); |
| 196 | keysToRemove.insert(remappedKeys.begin(), remappedKeys.end()); |
| 197 | |
| 198 | auto mapper = states.rotateAndCropMappers.find(states.cameraId.c_str()); |
| 199 | if (mapper != states.rotateAndCropMappers.end()) { |
| 200 | const auto& remappedKeys = iter->second.getRemappedKeys(); |
| 201 | keysToRemove.insert(remappedKeys.begin(), remappedKeys.end()); |
| 202 | } |
| 203 | |
| 204 | for (uint32_t key : keysToRemove) { |
| 205 | captureResult.mMetadata.erase(key); |
| 206 | } |
| 207 | |
| 208 | // Send partial result |
| 209 | if (captureResult.mMetadata.entryCount() > 0) { |
| 210 | insertResultLocked(states, &captureResult, frameNumber); |
| 211 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void sendCaptureResult( |
| 215 | CaptureOutputStates& states, |
| 216 | CameraMetadata &pendingMetadata, |
| 217 | CaptureResultExtras &resultExtras, |
| 218 | CameraMetadata &collectedPartialResult, |
| 219 | uint32_t frameNumber, |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 220 | bool reprocess, bool zslStillCapture, bool rotateAndCropAuto, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 221 | const std::set<std::string>& cameraIdsWithZoom, |
| 222 | const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) { |
| 223 | ATRACE_CALL(); |
| 224 | if (pendingMetadata.isEmpty()) |
| 225 | return; |
| 226 | |
| 227 | std::lock_guard<std::mutex> l(states.outputLock); |
| 228 | |
| 229 | // TODO: need to track errors for tighter bounds on expected frame number |
| 230 | if (reprocess) { |
| 231 | if (frameNumber < states.nextReprocResultFrameNum) { |
| 232 | SET_ERR("Out-of-order reprocess capture result metadata submitted! " |
| 233 | "(got frame number %d, expecting %d)", |
| 234 | frameNumber, states.nextReprocResultFrameNum); |
| 235 | return; |
| 236 | } |
| 237 | states.nextReprocResultFrameNum = frameNumber + 1; |
| 238 | } else if (zslStillCapture) { |
| 239 | if (frameNumber < states.nextZslResultFrameNum) { |
| 240 | SET_ERR("Out-of-order ZSL still capture result metadata submitted! " |
| 241 | "(got frame number %d, expecting %d)", |
| 242 | frameNumber, states.nextZslResultFrameNum); |
| 243 | return; |
| 244 | } |
| 245 | states.nextZslResultFrameNum = frameNumber + 1; |
| 246 | } else { |
| 247 | if (frameNumber < states.nextResultFrameNum) { |
| 248 | SET_ERR("Out-of-order capture result metadata submitted! " |
| 249 | "(got frame number %d, expecting %d)", |
| 250 | frameNumber, states.nextResultFrameNum); |
| 251 | return; |
| 252 | } |
| 253 | states.nextResultFrameNum = frameNumber + 1; |
| 254 | } |
| 255 | |
| 256 | CaptureResult captureResult; |
| 257 | captureResult.mResultExtras = resultExtras; |
| 258 | captureResult.mMetadata = pendingMetadata; |
| 259 | captureResult.mPhysicalMetadatas = physicalMetadatas; |
| 260 | |
| 261 | // Append any previous partials to form a complete result |
| 262 | if (states.usePartialResult && !collectedPartialResult.isEmpty()) { |
| 263 | captureResult.mMetadata.append(collectedPartialResult); |
| 264 | } |
| 265 | |
| 266 | captureResult.mMetadata.sort(); |
| 267 | |
| 268 | // Check that there's a timestamp in the result metadata |
| 269 | camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); |
| 270 | if (timestamp.count == 0) { |
| 271 | SET_ERR("No timestamp provided by HAL for frame %d!", |
| 272 | frameNumber); |
| 273 | return; |
| 274 | } |
Yin-Chia Yeh | c530c59 | 2020-03-09 14:50:36 -0700 | [diff] [blame] | 275 | nsecs_t sensorTimestamp = timestamp.data.i64[0]; |
| 276 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 277 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
| 278 | camera_metadata_entry timestamp = |
| 279 | physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP); |
| 280 | if (timestamp.count == 0) { |
| 281 | SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!", |
| 282 | String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber); |
| 283 | return; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // 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] | 288 | status_t res = OK; |
| 289 | auto iter = states.distortionMappers.find(states.cameraId.c_str()); |
| 290 | if (iter != states.distortionMappers.end()) { |
| 291 | res = iter->second.correctCaptureResult(&captureResult.mMetadata); |
| 292 | if (res != OK) { |
| 293 | SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)", |
| 294 | frameNumber, strerror(-res), res); |
| 295 | return; |
| 296 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // Fix up result metadata to account for zoom ratio availabilities between |
| 300 | // HAL and app. |
| 301 | bool zoomRatioIs1 = cameraIdsWithZoom.find(states.cameraId.c_str()) == cameraIdsWithZoom.end(); |
| 302 | res = states.zoomRatioMappers[states.cameraId.c_str()].updateCaptureResult( |
| 303 | &captureResult.mMetadata, zoomRatioIs1); |
| 304 | if (res != OK) { |
| 305 | SET_ERR("Failed to update capture result zoom ratio metadata for frame %d: %s (%d)", |
| 306 | frameNumber, strerror(-res), res); |
| 307 | return; |
| 308 | } |
| 309 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 310 | // Fix up result metadata to account for rotateAndCrop in AUTO mode |
| 311 | if (rotateAndCropAuto) { |
| 312 | auto mapper = states.rotateAndCropMappers.find(states.cameraId.c_str()); |
| 313 | if (mapper != states.rotateAndCropMappers.end()) { |
| 314 | res = mapper->second.updateCaptureResult( |
| 315 | &captureResult.mMetadata); |
| 316 | if (res != OK) { |
| 317 | SET_ERR("Unable to correct capture result rotate-and-crop for frame %d: %s (%d)", |
| 318 | frameNumber, strerror(-res), res); |
| 319 | return; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 324 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
| 325 | String8 cameraId8(physicalMetadata.mPhysicalCameraId); |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 326 | auto mapper = states.distortionMappers.find(cameraId8.c_str()); |
| 327 | if (mapper != states.distortionMappers.end()) { |
| 328 | res = mapper->second.correctCaptureResult( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 329 | &physicalMetadata.mPhysicalCameraMetadata); |
| 330 | if (res != OK) { |
| 331 | SET_ERR("Unable to correct physical capture result metadata for frame %d: %s (%d)", |
| 332 | frameNumber, strerror(-res), res); |
| 333 | return; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | zoomRatioIs1 = cameraIdsWithZoom.find(cameraId8.c_str()) == cameraIdsWithZoom.end(); |
| 338 | res = states.zoomRatioMappers[cameraId8.c_str()].updateCaptureResult( |
| 339 | &physicalMetadata.mPhysicalCameraMetadata, zoomRatioIs1); |
| 340 | if (res != OK) { |
| 341 | SET_ERR("Failed to update camera %s's physical zoom ratio metadata for " |
| 342 | "frame %d: %s(%d)", cameraId8.c_str(), frameNumber, strerror(-res), res); |
| 343 | return; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Fix up result metadata for monochrome camera. |
| 348 | res = fixupMonochromeTags(states, states.deviceInfo, captureResult.mMetadata); |
| 349 | if (res != OK) { |
| 350 | SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res); |
| 351 | return; |
| 352 | } |
| 353 | for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) { |
| 354 | String8 cameraId8(physicalMetadata.mPhysicalCameraId); |
| 355 | res = fixupMonochromeTags(states, |
| 356 | states.physicalDeviceInfoMap.at(cameraId8.c_str()), |
| 357 | physicalMetadata.mPhysicalCameraMetadata); |
| 358 | if (res != OK) { |
| 359 | SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res); |
| 360 | return; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | std::unordered_map<std::string, CameraMetadata> monitoredPhysicalMetadata; |
| 365 | for (auto& m : physicalMetadatas) { |
| 366 | monitoredPhysicalMetadata.emplace(String8(m.mPhysicalCameraId).string(), |
| 367 | CameraMetadata(m.mPhysicalCameraMetadata)); |
| 368 | } |
| 369 | states.tagMonitor.monitorMetadata(TagMonitor::RESULT, |
Yin-Chia Yeh | c530c59 | 2020-03-09 14:50:36 -0700 | [diff] [blame] | 370 | frameNumber, sensorTimestamp, captureResult.mMetadata, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 371 | monitoredPhysicalMetadata); |
| 372 | |
| 373 | insertResultLocked(states, &captureResult, frameNumber); |
| 374 | } |
| 375 | |
| 376 | // Reading one camera metadata from result argument via fmq or from the result |
| 377 | // Assuming the fmq is protected by a lock already |
| 378 | status_t readOneCameraMetadataLocked( |
| 379 | std::unique_ptr<ResultMetadataQueue>& fmq, |
| 380 | uint64_t fmqResultSize, |
| 381 | hardware::camera::device::V3_2::CameraMetadata& resultMetadata, |
| 382 | const hardware::camera::device::V3_2::CameraMetadata& result) { |
| 383 | if (fmqResultSize > 0) { |
| 384 | resultMetadata.resize(fmqResultSize); |
| 385 | if (fmq == nullptr) { |
| 386 | return NO_MEMORY; // logged in initialize() |
| 387 | } |
| 388 | if (!fmq->read(resultMetadata.data(), fmqResultSize)) { |
| 389 | ALOGE("%s: Cannot read camera metadata from fmq, size = %" PRIu64, |
| 390 | __FUNCTION__, fmqResultSize); |
| 391 | return INVALID_OPERATION; |
| 392 | } |
| 393 | } else { |
| 394 | resultMetadata.setToExternal(const_cast<uint8_t *>(result.data()), |
| 395 | result.size()); |
| 396 | } |
| 397 | |
| 398 | if (resultMetadata.size() != 0) { |
| 399 | status_t res; |
| 400 | const camera_metadata_t* metadata = |
| 401 | reinterpret_cast<const camera_metadata_t*>(resultMetadata.data()); |
| 402 | size_t expected_metadata_size = resultMetadata.size(); |
| 403 | if ((res = validate_camera_metadata_structure(metadata, &expected_metadata_size)) != OK) { |
| 404 | ALOGE("%s: Invalid camera metadata received by camera service from HAL: %s (%d)", |
| 405 | __FUNCTION__, strerror(-res), res); |
| 406 | return INVALID_OPERATION; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return OK; |
| 411 | } |
| 412 | |
| 413 | void removeInFlightMapEntryLocked(CaptureOutputStates& states, int idx) { |
| 414 | ATRACE_CALL(); |
| 415 | InFlightRequestMap& inflightMap = states.inflightMap; |
| 416 | nsecs_t duration = inflightMap.valueAt(idx).maxExpectedDuration; |
| 417 | inflightMap.removeItemsAt(idx, 1); |
| 418 | |
| 419 | states.inflightIntf.onInflightEntryRemovedLocked(duration); |
| 420 | } |
| 421 | |
| 422 | void removeInFlightRequestIfReadyLocked(CaptureOutputStates& states, int idx) { |
| 423 | InFlightRequestMap& inflightMap = states.inflightMap; |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 424 | const InFlightRequest &request = inflightMap.valueAt(idx); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 425 | const uint32_t frameNumber = inflightMap.keyAt(idx); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 426 | SessionStatsBuilder& sessionStatsBuilder = states.sessionStatsBuilder; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 427 | |
| 428 | nsecs_t sensorTimestamp = request.sensorTimestamp; |
| 429 | nsecs_t shutterTimestamp = request.shutterTimestamp; |
| 430 | |
| 431 | // Check if it's okay to remove the request from InFlightMap: |
| 432 | // In the case of a successful request: |
| 433 | // all input and output buffers, all result metadata, shutter callback |
| 434 | // arrived. |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 435 | // In the case of an unsuccessful request: |
| 436 | // 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] | 437 | if (request.numBuffersLeft == 0 && |
| 438 | (request.skipResultMetadata || |
| 439 | (request.haveResultMetadata && shutterTimestamp != 0))) { |
| 440 | if (request.stillCapture) { |
| 441 | ATRACE_ASYNC_END("still capture", frameNumber); |
| 442 | } |
| 443 | |
| 444 | ATRACE_ASYNC_END("frame capture", frameNumber); |
| 445 | |
Ivan Lozano | c0ad82f | 2020-07-30 09:32:57 -0400 | [diff] [blame] | 446 | // Validation check - if sensor timestamp matches shutter timestamp in the |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 447 | // case of request having callback. |
| 448 | if (request.hasCallback && request.requestStatus == OK && |
| 449 | sensorTimestamp != shutterTimestamp) { |
| 450 | SET_ERR("sensor timestamp (%" PRId64 |
| 451 | ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", |
| 452 | sensorTimestamp, frameNumber, shutterTimestamp); |
| 453 | } |
| 454 | |
| 455 | // for an unsuccessful request, it may have pending output buffers to |
| 456 | // return. |
| 457 | assert(request.requestStatus != OK || |
| 458 | request.pendingOutputBuffers.size() == 0); |
| 459 | |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 460 | returnOutputBuffers( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 461 | states.useHalBufManager, states.listener, |
| 462 | request.pendingOutputBuffers.array(), |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 463 | request.pendingOutputBuffers.size(), 0, |
| 464 | /*requested*/true, request.requestTimeNs, states.sessionStatsBuilder, |
| 465 | /*timestampIncreasing*/true, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 466 | request.outputSurfaces, request.resultExtras, |
| 467 | request.errorBufStrategy); |
| 468 | |
| 469 | // Note down the just completed frame number |
| 470 | if (request.hasInputBuffer) { |
| 471 | states.lastCompletedReprocessFrameNumber = frameNumber; |
| 472 | } else if (request.zslCapture) { |
| 473 | states.lastCompletedZslFrameNumber = frameNumber; |
| 474 | } else { |
| 475 | states.lastCompletedRegularFrameNumber = frameNumber; |
| 476 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 477 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 478 | sessionStatsBuilder.incResultCounter(request.skipResultMetadata); |
| 479 | |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 480 | removeInFlightMapEntryLocked(states, idx); |
| 481 | ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | states.inflightIntf.checkInflightMapLengthLocked(); |
| 485 | } |
| 486 | |
| 487 | void processCaptureResult(CaptureOutputStates& states, const camera3_capture_result *result) { |
| 488 | ATRACE_CALL(); |
| 489 | |
| 490 | status_t res; |
| 491 | |
| 492 | uint32_t frameNumber = result->frame_number; |
| 493 | if (result->result == NULL && result->num_output_buffers == 0 && |
| 494 | result->input_buffer == NULL) { |
| 495 | SET_ERR("No result data provided by HAL for frame %d", |
| 496 | frameNumber); |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | if (!states.usePartialResult && |
| 501 | result->result != NULL && |
| 502 | result->partial_result != 1) { |
| 503 | SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" |
| 504 | " if partial result is not supported", |
| 505 | frameNumber, result->partial_result); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | bool isPartialResult = false; |
| 510 | CameraMetadata collectedPartialResult; |
| 511 | bool hasInputBufferInRequest = false; |
| 512 | |
| 513 | // Get shutter timestamp and resultExtras from list of in-flight requests, |
| 514 | // where it was added by the shutter notification for this frame. If the |
| 515 | // shutter timestamp isn't received yet, append the output buffers to the |
| 516 | // in-flight request and they will be returned when the shutter timestamp |
| 517 | // arrives. Update the in-flight status and remove the in-flight entry if |
| 518 | // all result data and shutter timestamp have been received. |
| 519 | nsecs_t shutterTimestamp = 0; |
| 520 | { |
| 521 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 522 | ssize_t idx = states.inflightMap.indexOfKey(frameNumber); |
| 523 | if (idx == NAME_NOT_FOUND) { |
| 524 | SET_ERR("Unknown frame number for capture result: %d", |
| 525 | frameNumber); |
| 526 | return; |
| 527 | } |
| 528 | InFlightRequest &request = states.inflightMap.editValueAt(idx); |
| 529 | ALOGVV("%s: got InFlightRequest requestId = %" PRId32 |
| 530 | ", frameNumber = %" PRId64 ", burstId = %" PRId32 |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 531 | ", partialResultCount = %d/%d, hasCallback = %d, num_output_buffers %d" |
| 532 | ", usePartialResult = %d", |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 533 | __FUNCTION__, request.resultExtras.requestId, |
| 534 | request.resultExtras.frameNumber, request.resultExtras.burstId, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 535 | result->partial_result, states.numPartialResults, |
| 536 | request.hasCallback, result->num_output_buffers, |
| 537 | states.usePartialResult); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 538 | // Always update the partial count to the latest one if it's not 0 |
| 539 | // (buffers only). When framework aggregates adjacent partial results |
| 540 | // into one, the latest partial count will be used. |
| 541 | if (result->partial_result != 0) |
| 542 | request.resultExtras.partialResultCount = result->partial_result; |
| 543 | |
| 544 | // Check if this result carries only partial metadata |
| 545 | if (states.usePartialResult && result->result != NULL) { |
| 546 | if (result->partial_result > states.numPartialResults || result->partial_result < 1) { |
| 547 | SET_ERR("Result is malformed for frame %d: partial_result %u must be in" |
| 548 | " the range of [1, %d] when metadata is included in the result", |
| 549 | frameNumber, result->partial_result, states.numPartialResults); |
| 550 | return; |
| 551 | } |
| 552 | isPartialResult = (result->partial_result < states.numPartialResults); |
| 553 | if (isPartialResult && result->num_physcam_metadata) { |
| 554 | SET_ERR("Result is malformed for frame %d: partial_result not allowed for" |
| 555 | " physical camera result", frameNumber); |
| 556 | return; |
| 557 | } |
| 558 | if (isPartialResult) { |
| 559 | request.collectedPartialResult.append(result->result); |
| 560 | } |
| 561 | |
| 562 | if (isPartialResult && request.hasCallback) { |
| 563 | // Send partial capture result |
| 564 | sendPartialCaptureResult(states, result->result, request.resultExtras, |
| 565 | frameNumber); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | shutterTimestamp = request.shutterTimestamp; |
| 570 | hasInputBufferInRequest = request.hasInputBuffer; |
| 571 | |
| 572 | // Did we get the (final) result metadata for this capture? |
| 573 | if (result->result != NULL && !isPartialResult) { |
| 574 | if (request.physicalCameraIds.size() != result->num_physcam_metadata) { |
| 575 | SET_ERR("Expected physical Camera metadata count %d not equal to actual count %d", |
| 576 | request.physicalCameraIds.size(), result->num_physcam_metadata); |
| 577 | return; |
| 578 | } |
| 579 | if (request.haveResultMetadata) { |
| 580 | SET_ERR("Called multiple times with metadata for frame %d", |
| 581 | frameNumber); |
| 582 | return; |
| 583 | } |
| 584 | for (uint32_t i = 0; i < result->num_physcam_metadata; i++) { |
| 585 | String8 physicalId(result->physcam_ids[i]); |
| 586 | std::set<String8>::iterator cameraIdIter = |
| 587 | request.physicalCameraIds.find(physicalId); |
| 588 | if (cameraIdIter != request.physicalCameraIds.end()) { |
| 589 | request.physicalCameraIds.erase(cameraIdIter); |
| 590 | } else { |
| 591 | SET_ERR("Total result for frame %d has already returned for camera %s", |
| 592 | frameNumber, physicalId.c_str()); |
| 593 | return; |
| 594 | } |
| 595 | } |
| 596 | if (states.usePartialResult && |
| 597 | !request.collectedPartialResult.isEmpty()) { |
| 598 | collectedPartialResult.acquire( |
| 599 | request.collectedPartialResult); |
| 600 | } |
| 601 | request.haveResultMetadata = true; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 602 | request.errorBufStrategy = ERROR_BUF_RETURN_NOTIFY; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | uint32_t numBuffersReturned = result->num_output_buffers; |
| 606 | if (result->input_buffer != NULL) { |
| 607 | if (hasInputBufferInRequest) { |
| 608 | numBuffersReturned += 1; |
| 609 | } else { |
| 610 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 611 | " buffer sent in the request", |
| 612 | __FUNCTION__); |
| 613 | } |
| 614 | } |
| 615 | request.numBuffersLeft -= numBuffersReturned; |
| 616 | if (request.numBuffersLeft < 0) { |
| 617 | SET_ERR("Too many buffers returned for frame %d", |
| 618 | frameNumber); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | camera_metadata_ro_entry_t entry; |
| 623 | res = find_camera_metadata_ro_entry(result->result, |
| 624 | ANDROID_SENSOR_TIMESTAMP, &entry); |
| 625 | if (res == OK && entry.count == 1) { |
| 626 | request.sensorTimestamp = entry.data.i64[0]; |
| 627 | } |
| 628 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 629 | // If shutter event isn't received yet, do not return the pending output |
| 630 | // buffers. |
| 631 | request.pendingOutputBuffers.appendArray(result->output_buffers, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 632 | result->num_output_buffers); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 633 | if (shutterTimestamp != 0) { |
| 634 | returnAndRemovePendingOutputBuffers( |
| 635 | states.useHalBufManager, states.listener, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 636 | request, states.sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | if (result->result != NULL && !isPartialResult) { |
| 640 | for (uint32_t i = 0; i < result->num_physcam_metadata; i++) { |
| 641 | CameraMetadata physicalMetadata; |
| 642 | physicalMetadata.append(result->physcam_metadata[i]); |
| 643 | request.physicalMetadatas.push_back({String16(result->physcam_ids[i]), |
| 644 | physicalMetadata}); |
| 645 | } |
| 646 | if (shutterTimestamp == 0) { |
| 647 | request.pendingMetadata = result->result; |
| 648 | request.collectedPartialResult = collectedPartialResult; |
| 649 | } else if (request.hasCallback) { |
| 650 | CameraMetadata metadata; |
| 651 | metadata = result->result; |
| 652 | sendCaptureResult(states, metadata, request.resultExtras, |
| 653 | collectedPartialResult, frameNumber, |
| 654 | hasInputBufferInRequest, request.zslCapture && request.stillCapture, |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 655 | request.rotateAndCropAuto, request.cameraIdsWithZoom, |
| 656 | request.physicalMetadatas); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | removeInFlightRequestIfReadyLocked(states, idx); |
| 660 | } // scope for states.inFlightLock |
| 661 | |
| 662 | if (result->input_buffer != NULL) { |
| 663 | if (hasInputBufferInRequest) { |
| 664 | Camera3Stream *stream = |
| 665 | Camera3Stream::cast(result->input_buffer->stream); |
| 666 | res = stream->returnInputBuffer(*(result->input_buffer)); |
| 667 | // Note: stream may be deallocated at this point, if this buffer was the |
| 668 | // last reference to it. |
| 669 | if (res != OK) { |
| 670 | ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" |
| 671 | " its stream:%s (%d)", __FUNCTION__, |
| 672 | frameNumber, strerror(-res), res); |
| 673 | } |
| 674 | } else { |
| 675 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 676 | " buffer sent in the request, skipping input buffer return.", |
| 677 | __FUNCTION__); |
| 678 | } |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | void processOneCaptureResultLocked( |
| 683 | CaptureOutputStates& states, |
| 684 | const hardware::camera::device::V3_2::CaptureResult& result, |
| 685 | const hardware::hidl_vec< |
| 686 | hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata) { |
| 687 | using hardware::camera::device::V3_2::StreamBuffer; |
| 688 | using hardware::camera::device::V3_2::BufferStatus; |
| 689 | std::unique_ptr<ResultMetadataQueue>& fmq = states.fmq; |
| 690 | BufferRecordsInterface& bufferRecords = states.bufferRecordsIntf; |
| 691 | camera3_capture_result r; |
| 692 | status_t res; |
| 693 | r.frame_number = result.frameNumber; |
| 694 | |
| 695 | // Read and validate the result metadata. |
| 696 | hardware::camera::device::V3_2::CameraMetadata resultMetadata; |
| 697 | res = readOneCameraMetadataLocked( |
| 698 | fmq, result.fmqResultSize, |
| 699 | resultMetadata, result.result); |
| 700 | if (res != OK) { |
| 701 | ALOGE("%s: Frame %d: Failed to read capture result metadata", |
| 702 | __FUNCTION__, result.frameNumber); |
| 703 | return; |
| 704 | } |
| 705 | r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data()); |
| 706 | |
| 707 | // Read and validate physical camera metadata |
| 708 | size_t physResultCount = physicalCameraMetadata.size(); |
| 709 | std::vector<const char*> physCamIds(physResultCount); |
| 710 | std::vector<const camera_metadata_t *> phyCamMetadatas(physResultCount); |
| 711 | std::vector<hardware::camera::device::V3_2::CameraMetadata> physResultMetadata; |
| 712 | physResultMetadata.resize(physResultCount); |
| 713 | for (size_t i = 0; i < physicalCameraMetadata.size(); i++) { |
| 714 | res = readOneCameraMetadataLocked(fmq, physicalCameraMetadata[i].fmqMetadataSize, |
| 715 | physResultMetadata[i], physicalCameraMetadata[i].metadata); |
| 716 | if (res != OK) { |
| 717 | ALOGE("%s: Frame %d: Failed to read capture result metadata for camera %s", |
| 718 | __FUNCTION__, result.frameNumber, |
| 719 | physicalCameraMetadata[i].physicalCameraId.c_str()); |
| 720 | return; |
| 721 | } |
| 722 | physCamIds[i] = physicalCameraMetadata[i].physicalCameraId.c_str(); |
| 723 | phyCamMetadatas[i] = reinterpret_cast<const camera_metadata_t*>( |
| 724 | physResultMetadata[i].data()); |
| 725 | } |
| 726 | r.num_physcam_metadata = physResultCount; |
| 727 | r.physcam_ids = physCamIds.data(); |
| 728 | r.physcam_metadata = phyCamMetadatas.data(); |
| 729 | |
| 730 | std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size()); |
| 731 | std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size()); |
| 732 | for (size_t i = 0; i < result.outputBuffers.size(); i++) { |
| 733 | auto& bDst = outputBuffers[i]; |
| 734 | const StreamBuffer &bSrc = result.outputBuffers[i]; |
| 735 | |
| 736 | sp<Camera3StreamInterface> stream = states.outputStreams.get(bSrc.streamId); |
| 737 | if (stream == nullptr) { |
| 738 | ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d", |
| 739 | __FUNCTION__, result.frameNumber, i, bSrc.streamId); |
| 740 | return; |
| 741 | } |
| 742 | bDst.stream = stream->asHalStream(); |
| 743 | |
| 744 | bool noBufferReturned = false; |
| 745 | buffer_handle_t *buffer = nullptr; |
| 746 | if (states.useHalBufManager) { |
| 747 | // This is suspicious most of the time but can be correct during flush where HAL |
| 748 | // has to return capture result before a buffer is requested |
| 749 | if (bSrc.bufferId == BUFFER_ID_NO_BUFFER) { |
| 750 | if (bSrc.status == BufferStatus::OK) { |
| 751 | ALOGE("%s: Frame %d: Buffer %zu: No bufferId for stream %d", |
| 752 | __FUNCTION__, result.frameNumber, i, bSrc.streamId); |
| 753 | // Still proceeds so other buffers can be returned |
| 754 | } |
| 755 | noBufferReturned = true; |
| 756 | } |
| 757 | if (noBufferReturned) { |
| 758 | res = OK; |
| 759 | } else { |
| 760 | res = bufferRecords.popInflightRequestBuffer(bSrc.bufferId, &buffer); |
| 761 | } |
| 762 | } else { |
| 763 | res = bufferRecords.popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer); |
| 764 | } |
| 765 | |
| 766 | if (res != OK) { |
| 767 | ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d", |
| 768 | __FUNCTION__, result.frameNumber, i, bSrc.streamId); |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | bDst.buffer = buffer; |
| 773 | bDst.status = mapHidlBufferStatus(bSrc.status); |
| 774 | bDst.acquire_fence = -1; |
| 775 | if (bSrc.releaseFence == nullptr) { |
| 776 | bDst.release_fence = -1; |
| 777 | } else if (bSrc.releaseFence->numFds == 1) { |
| 778 | if (noBufferReturned) { |
| 779 | ALOGE("%s: got releaseFence without output buffer!", __FUNCTION__); |
| 780 | } |
| 781 | bDst.release_fence = dup(bSrc.releaseFence->data[0]); |
| 782 | } else { |
| 783 | ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1", |
| 784 | __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds); |
| 785 | return; |
| 786 | } |
| 787 | } |
| 788 | r.num_output_buffers = outputBuffers.size(); |
| 789 | r.output_buffers = outputBuffers.data(); |
| 790 | |
| 791 | camera3_stream_buffer_t inputBuffer; |
| 792 | if (result.inputBuffer.streamId == -1) { |
| 793 | r.input_buffer = nullptr; |
| 794 | } else { |
| 795 | if (states.inputStream->getId() != result.inputBuffer.streamId) { |
| 796 | ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__, |
| 797 | result.frameNumber, result.inputBuffer.streamId); |
| 798 | return; |
| 799 | } |
| 800 | inputBuffer.stream = states.inputStream->asHalStream(); |
| 801 | buffer_handle_t *buffer; |
| 802 | res = bufferRecords.popInflightBuffer(result.frameNumber, result.inputBuffer.streamId, |
| 803 | &buffer); |
| 804 | if (res != OK) { |
| 805 | ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d", |
| 806 | __FUNCTION__, result.frameNumber, result.inputBuffer.streamId); |
| 807 | return; |
| 808 | } |
| 809 | inputBuffer.buffer = buffer; |
| 810 | inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status); |
| 811 | inputBuffer.acquire_fence = -1; |
| 812 | if (result.inputBuffer.releaseFence == nullptr) { |
| 813 | inputBuffer.release_fence = -1; |
| 814 | } else if (result.inputBuffer.releaseFence->numFds == 1) { |
| 815 | inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]); |
| 816 | } else { |
| 817 | ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1", |
| 818 | __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds); |
| 819 | return; |
| 820 | } |
| 821 | r.input_buffer = &inputBuffer; |
| 822 | } |
| 823 | |
| 824 | r.partial_result = result.partialResult; |
| 825 | |
| 826 | processCaptureResult(states, &r); |
| 827 | } |
| 828 | |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 829 | void returnOutputBuffers( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 830 | bool useHalBufManager, |
| 831 | sp<NotificationListener> listener, |
| 832 | const camera3_stream_buffer_t *outputBuffers, size_t numBuffers, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 833 | nsecs_t timestamp, bool requested, nsecs_t requestTimeNs, |
| 834 | SessionStatsBuilder& sessionStatsBuilder, bool timestampIncreasing, |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 835 | const SurfaceMap& outputSurfaces, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 836 | const CaptureResultExtras &inResultExtras, |
| 837 | ERROR_BUF_STRATEGY errorBufStrategy) { |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 838 | |
| 839 | for (size_t i = 0; i < numBuffers; i++) |
| 840 | { |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 841 | Camera3StreamInterface *stream = Camera3Stream::cast(outputBuffers[i].stream); |
| 842 | int streamId = stream->getId(); |
| 843 | |
| 844 | // Call notify(ERROR_BUFFER) if necessary. |
| 845 | if (outputBuffers[i].status == CAMERA3_BUFFER_STATUS_ERROR && |
| 846 | errorBufStrategy == ERROR_BUF_RETURN_NOTIFY) { |
| 847 | if (listener != nullptr) { |
| 848 | CaptureResultExtras extras = inResultExtras; |
| 849 | extras.errorStreamId = streamId; |
| 850 | listener->notifyError( |
| 851 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER, |
| 852 | extras); |
| 853 | } |
| 854 | } |
| 855 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 856 | if (outputBuffers[i].buffer == nullptr) { |
| 857 | if (!useHalBufManager) { |
| 858 | // With HAL buffer management API, HAL sometimes will have to return buffers that |
| 859 | // has not got a output buffer handle filled yet. This is though illegal if HAL |
| 860 | // buffer management API is not being used. |
| 861 | ALOGE("%s: cannot return a null buffer!", __FUNCTION__); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 862 | } else { |
| 863 | if (requested) { |
| 864 | sessionStatsBuilder.incCounter(streamId, /*dropped*/true, 0); |
| 865 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 866 | } |
| 867 | continue; |
| 868 | } |
| 869 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 870 | const auto& it = outputSurfaces.find(streamId); |
| 871 | status_t res = OK; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 872 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 873 | // Do not return the buffer if the buffer status is error, and the error |
| 874 | // buffer strategy is CACHE. |
| 875 | if (outputBuffers[i].status != CAMERA3_BUFFER_STATUS_ERROR || |
| 876 | errorBufStrategy != ERROR_BUF_CACHE) { |
| 877 | if (it != outputSurfaces.end()) { |
| 878 | res = stream->returnBuffer( |
| 879 | outputBuffers[i], timestamp, timestampIncreasing, it->second, |
| 880 | inResultExtras.frameNumber); |
| 881 | } else { |
| 882 | res = stream->returnBuffer( |
| 883 | outputBuffers[i], timestamp, timestampIncreasing, std::vector<size_t> (), |
| 884 | inResultExtras.frameNumber); |
| 885 | } |
| 886 | } |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 887 | // Note: stream may be deallocated at this point, if this buffer was |
| 888 | // the last reference to it. |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 889 | bool dropped = false; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 890 | if (res == NO_INIT || res == DEAD_OBJECT) { |
| 891 | 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] | 892 | sessionStatsBuilder.stopCounter(streamId); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 893 | } else if (res != OK) { |
| 894 | 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] | 895 | dropped = true; |
| 896 | } else { |
| 897 | if (outputBuffers[i].status == CAMERA3_BUFFER_STATUS_ERROR || timestamp == 0) { |
| 898 | dropped = true; |
| 899 | } |
| 900 | } |
| 901 | if (requested) { |
| 902 | nsecs_t bufferTimeNs = systemTime(); |
| 903 | int32_t captureLatencyMs = ns2ms(bufferTimeNs - requestTimeNs); |
| 904 | sessionStatsBuilder.incCounter(streamId, dropped, captureLatencyMs); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | // Long processing consumers can cause returnBuffer timeout for shared stream |
| 908 | // If that happens, cancel the buffer and send a buffer error to client |
| 909 | if (it != outputSurfaces.end() && res == TIMED_OUT && |
| 910 | outputBuffers[i].status == CAMERA3_BUFFER_STATUS_OK) { |
| 911 | // cancel the buffer |
| 912 | camera3_stream_buffer_t sb = outputBuffers[i]; |
| 913 | sb.status = CAMERA3_BUFFER_STATUS_ERROR; |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 914 | stream->returnBuffer(sb, /*timestamp*/0, |
| 915 | timestampIncreasing, std::vector<size_t> (), |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 916 | inResultExtras.frameNumber); |
| 917 | |
| 918 | if (listener != nullptr) { |
| 919 | CaptureResultExtras extras = inResultExtras; |
| 920 | extras.errorStreamId = streamId; |
| 921 | listener->notifyError( |
| 922 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER, |
| 923 | extras); |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 929 | void returnAndRemovePendingOutputBuffers(bool useHalBufManager, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 930 | sp<NotificationListener> listener, InFlightRequest& request, |
| 931 | SessionStatsBuilder& sessionStatsBuilder) { |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 932 | bool timestampIncreasing = !(request.zslCapture || request.hasInputBuffer); |
| 933 | returnOutputBuffers(useHalBufManager, listener, |
| 934 | request.pendingOutputBuffers.array(), |
| 935 | request.pendingOutputBuffers.size(), |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 936 | request.shutterTimestamp, /*requested*/true, |
| 937 | request.requestTimeNs, sessionStatsBuilder, timestampIncreasing, |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 938 | request.outputSurfaces, request.resultExtras, |
| 939 | request.errorBufStrategy); |
| 940 | |
| 941 | // Remove error buffers that are not cached. |
| 942 | for (auto iter = request.pendingOutputBuffers.begin(); |
| 943 | iter != request.pendingOutputBuffers.end(); ) { |
| 944 | if (request.errorBufStrategy != ERROR_BUF_CACHE || |
| 945 | iter->status != CAMERA3_BUFFER_STATUS_ERROR) { |
| 946 | iter = request.pendingOutputBuffers.erase(iter); |
| 947 | } else { |
| 948 | iter++; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 953 | void notifyShutter(CaptureOutputStates& states, const camera3_shutter_msg_t &msg) { |
| 954 | ATRACE_CALL(); |
| 955 | ssize_t idx; |
| 956 | |
| 957 | // Set timestamp for the request in the in-flight tracking |
| 958 | // and get the request ID to send upstream |
| 959 | { |
| 960 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 961 | InFlightRequestMap& inflightMap = states.inflightMap; |
| 962 | idx = inflightMap.indexOfKey(msg.frame_number); |
| 963 | if (idx >= 0) { |
| 964 | InFlightRequest &r = inflightMap.editValueAt(idx); |
| 965 | |
| 966 | // Verify ordering of shutter notifications |
| 967 | { |
| 968 | std::lock_guard<std::mutex> l(states.outputLock); |
| 969 | // TODO: need to track errors for tighter bounds on expected frame number. |
| 970 | if (r.hasInputBuffer) { |
| 971 | if (msg.frame_number < states.nextReprocShutterFrameNum) { |
| 972 | SET_ERR("Reprocess shutter notification out-of-order. Expected " |
| 973 | "notification for frame %d, got frame %d", |
| 974 | states.nextReprocShutterFrameNum, msg.frame_number); |
| 975 | return; |
| 976 | } |
| 977 | states.nextReprocShutterFrameNum = msg.frame_number + 1; |
| 978 | } else if (r.zslCapture && r.stillCapture) { |
| 979 | if (msg.frame_number < states.nextZslShutterFrameNum) { |
| 980 | SET_ERR("ZSL still capture shutter notification out-of-order. Expected " |
| 981 | "notification for frame %d, got frame %d", |
| 982 | states.nextZslShutterFrameNum, msg.frame_number); |
| 983 | return; |
| 984 | } |
| 985 | states.nextZslShutterFrameNum = msg.frame_number + 1; |
| 986 | } else { |
| 987 | if (msg.frame_number < states.nextShutterFrameNum) { |
| 988 | SET_ERR("Shutter notification out-of-order. Expected " |
| 989 | "notification for frame %d, got frame %d", |
| 990 | states.nextShutterFrameNum, msg.frame_number); |
| 991 | return; |
| 992 | } |
| 993 | states.nextShutterFrameNum = msg.frame_number + 1; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | r.shutterTimestamp = msg.timestamp; |
| 998 | if (r.hasCallback) { |
| 999 | ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64, |
| 1000 | states.cameraId.string(), __FUNCTION__, |
| 1001 | msg.frame_number, r.resultExtras.requestId, msg.timestamp); |
| 1002 | // Call listener, if any |
| 1003 | if (states.listener != nullptr) { |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1004 | r.resultExtras.lastCompletedRegularFrameNumber = |
| 1005 | states.lastCompletedRegularFrameNumber; |
| 1006 | r.resultExtras.lastCompletedReprocessFrameNumber = |
| 1007 | states.lastCompletedReprocessFrameNumber; |
| 1008 | r.resultExtras.lastCompletedZslFrameNumber = |
| 1009 | states.lastCompletedZslFrameNumber; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1010 | states.listener->notifyShutter(r.resultExtras, msg.timestamp); |
| 1011 | } |
| 1012 | // send pending result and buffers |
| 1013 | sendCaptureResult(states, |
| 1014 | r.pendingMetadata, r.resultExtras, |
| 1015 | r.collectedPartialResult, msg.frame_number, |
| 1016 | r.hasInputBuffer, r.zslCapture && r.stillCapture, |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 1017 | r.rotateAndCropAuto, r.cameraIdsWithZoom, r.physicalMetadatas); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1018 | } |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1019 | returnAndRemovePendingOutputBuffers( |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1020 | states.useHalBufManager, states.listener, r, states.sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1021 | |
| 1022 | removeInFlightRequestIfReadyLocked(states, idx); |
| 1023 | } |
| 1024 | } |
| 1025 | if (idx < 0) { |
| 1026 | SET_ERR("Shutter notification for non-existent frame number %d", |
| 1027 | msg.frame_number); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | void notifyError(CaptureOutputStates& states, const camera3_error_msg_t &msg) { |
| 1032 | ATRACE_CALL(); |
| 1033 | // Map camera HAL error codes to ICameraDeviceCallback error codes |
| 1034 | // Index into this with the HAL error code |
| 1035 | static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = { |
| 1036 | // 0 = Unused error code |
| 1037 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, |
| 1038 | // 1 = CAMERA3_MSG_ERROR_DEVICE |
| 1039 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
| 1040 | // 2 = CAMERA3_MSG_ERROR_REQUEST |
| 1041 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 1042 | // 3 = CAMERA3_MSG_ERROR_RESULT |
| 1043 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, |
| 1044 | // 4 = CAMERA3_MSG_ERROR_BUFFER |
| 1045 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER |
| 1046 | }; |
| 1047 | |
| 1048 | int32_t errorCode = |
| 1049 | ((msg.error_code >= 0) && |
| 1050 | (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ? |
| 1051 | halErrorMap[msg.error_code] : |
| 1052 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; |
| 1053 | |
| 1054 | int streamId = 0; |
| 1055 | String16 physicalCameraId; |
| 1056 | if (msg.error_stream != nullptr) { |
| 1057 | Camera3Stream *stream = |
| 1058 | Camera3Stream::cast(msg.error_stream); |
| 1059 | streamId = stream->getId(); |
| 1060 | physicalCameraId = String16(stream->physicalCameraId()); |
| 1061 | } |
| 1062 | ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d", |
| 1063 | states.cameraId.string(), __FUNCTION__, msg.frame_number, |
| 1064 | streamId, msg.error_code); |
| 1065 | |
| 1066 | CaptureResultExtras resultExtras; |
| 1067 | switch (errorCode) { |
| 1068 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: |
| 1069 | // SET_ERR calls into listener to notify application |
| 1070 | SET_ERR("Camera HAL reported serious device error"); |
| 1071 | break; |
| 1072 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: |
| 1073 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1074 | { |
| 1075 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 1076 | ssize_t idx = states.inflightMap.indexOfKey(msg.frame_number); |
| 1077 | if (idx >= 0) { |
| 1078 | InFlightRequest &r = states.inflightMap.editValueAt(idx); |
| 1079 | r.requestStatus = msg.error_code; |
| 1080 | resultExtras = r.resultExtras; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1081 | bool physicalDeviceResultError = false; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1082 | if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == |
| 1083 | errorCode) { |
| 1084 | if (physicalCameraId.size() > 0) { |
| 1085 | String8 cameraId(physicalCameraId); |
| 1086 | auto iter = r.physicalCameraIds.find(cameraId); |
| 1087 | if (iter == r.physicalCameraIds.end()) { |
| 1088 | ALOGE("%s: Reported result failure for physical camera device: %s " |
| 1089 | " which is not part of the respective request!", |
| 1090 | __FUNCTION__, cameraId.string()); |
| 1091 | break; |
| 1092 | } |
| 1093 | r.physicalCameraIds.erase(iter); |
| 1094 | resultExtras.errorPhysicalCameraId = physicalCameraId; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1095 | physicalDeviceResultError = true; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1099 | if (!physicalDeviceResultError) { |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 1100 | r.skipResultMetadata = true; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1101 | if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT |
| 1102 | == errorCode) { |
| 1103 | r.errorBufStrategy = ERROR_BUF_RETURN_NOTIFY; |
| 1104 | } else { |
| 1105 | // errorCode is ERROR_CAMERA_REQUEST |
| 1106 | r.errorBufStrategy = ERROR_BUF_RETURN; |
| 1107 | } |
| 1108 | |
| 1109 | // Check whether the buffers returned. If they returned, |
| 1110 | // remove inflight request. |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1111 | removeInFlightRequestIfReadyLocked(states, idx); |
| 1112 | } |
| 1113 | } else { |
| 1114 | resultExtras.frameNumber = msg.frame_number; |
| 1115 | ALOGE("Camera %s: %s: cannot find in-flight request on " |
| 1116 | "frame %" PRId64 " error", states.cameraId.string(), __FUNCTION__, |
| 1117 | resultExtras.frameNumber); |
| 1118 | } |
| 1119 | } |
| 1120 | resultExtras.errorStreamId = streamId; |
| 1121 | if (states.listener != nullptr) { |
| 1122 | states.listener->notifyError(errorCode, resultExtras); |
| 1123 | } else { |
| 1124 | ALOGE("Camera %s: %s: no listener available", |
| 1125 | states.cameraId.string(), __FUNCTION__); |
| 1126 | } |
| 1127 | break; |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1128 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: |
| 1129 | // Do not depend on HAL ERROR_CAMERA_BUFFER to send buffer error |
| 1130 | // callback to the app. Rather, use STATUS_ERROR of image buffers. |
| 1131 | break; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1132 | default: |
| 1133 | // SET_ERR calls notifyError |
| 1134 | SET_ERR("Unknown error message from HAL: %d", msg.error_code); |
| 1135 | break; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | void notify(CaptureOutputStates& states, const camera3_notify_msg *msg) { |
| 1140 | switch (msg->type) { |
| 1141 | case CAMERA3_MSG_ERROR: { |
| 1142 | notifyError(states, msg->message.error); |
| 1143 | break; |
| 1144 | } |
| 1145 | case CAMERA3_MSG_SHUTTER: { |
| 1146 | notifyShutter(states, msg->message.shutter); |
| 1147 | break; |
| 1148 | } |
| 1149 | default: |
| 1150 | SET_ERR("Unknown notify message from HAL: %d", |
| 1151 | msg->type); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | void notify(CaptureOutputStates& states, |
| 1156 | const hardware::camera::device::V3_2::NotifyMsg& msg) { |
| 1157 | using android::hardware::camera::device::V3_2::MsgType; |
| 1158 | using android::hardware::camera::device::V3_2::ErrorCode; |
| 1159 | |
| 1160 | ATRACE_CALL(); |
| 1161 | camera3_notify_msg m; |
| 1162 | switch (msg.type) { |
| 1163 | case MsgType::ERROR: |
| 1164 | m.type = CAMERA3_MSG_ERROR; |
| 1165 | m.message.error.frame_number = msg.msg.error.frameNumber; |
| 1166 | if (msg.msg.error.errorStreamId >= 0) { |
| 1167 | sp<Camera3StreamInterface> stream = |
| 1168 | states.outputStreams.get(msg.msg.error.errorStreamId); |
| 1169 | if (stream == nullptr) { |
| 1170 | ALOGE("%s: Frame %d: Invalid error stream id %d", __FUNCTION__, |
| 1171 | m.message.error.frame_number, msg.msg.error.errorStreamId); |
| 1172 | return; |
| 1173 | } |
| 1174 | m.message.error.error_stream = stream->asHalStream(); |
| 1175 | } else { |
| 1176 | m.message.error.error_stream = nullptr; |
| 1177 | } |
| 1178 | switch (msg.msg.error.errorCode) { |
| 1179 | case ErrorCode::ERROR_DEVICE: |
| 1180 | m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE; |
| 1181 | break; |
| 1182 | case ErrorCode::ERROR_REQUEST: |
| 1183 | m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; |
| 1184 | break; |
| 1185 | case ErrorCode::ERROR_RESULT: |
| 1186 | m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT; |
| 1187 | break; |
| 1188 | case ErrorCode::ERROR_BUFFER: |
| 1189 | m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER; |
| 1190 | break; |
| 1191 | } |
| 1192 | break; |
| 1193 | case MsgType::SHUTTER: |
| 1194 | m.type = CAMERA3_MSG_SHUTTER; |
| 1195 | m.message.shutter.frame_number = msg.msg.shutter.frameNumber; |
| 1196 | m.message.shutter.timestamp = msg.msg.shutter.timestamp; |
| 1197 | break; |
| 1198 | } |
| 1199 | notify(states, &m); |
| 1200 | } |
| 1201 | |
| 1202 | void requestStreamBuffers(RequestBufferStates& states, |
| 1203 | const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs, |
| 1204 | hardware::camera::device::V3_5::ICameraDeviceCallback::requestStreamBuffers_cb _hidl_cb) { |
| 1205 | using android::hardware::camera::device::V3_2::BufferStatus; |
| 1206 | using android::hardware::camera::device::V3_2::StreamBuffer; |
| 1207 | using android::hardware::camera::device::V3_5::BufferRequestStatus; |
| 1208 | using android::hardware::camera::device::V3_5::StreamBufferRet; |
| 1209 | using android::hardware::camera::device::V3_5::StreamBufferRequestError; |
| 1210 | |
| 1211 | std::lock_guard<std::mutex> lock(states.reqBufferLock); |
| 1212 | |
| 1213 | hardware::hidl_vec<StreamBufferRet> bufRets; |
| 1214 | if (!states.useHalBufManager) { |
| 1215 | ALOGE("%s: Camera %s does not support HAL buffer management", |
| 1216 | __FUNCTION__, states.cameraId.string()); |
| 1217 | _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets); |
| 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | SortedVector<int32_t> streamIds; |
| 1222 | ssize_t sz = streamIds.setCapacity(bufReqs.size()); |
| 1223 | if (sz < 0 || static_cast<size_t>(sz) != bufReqs.size()) { |
| 1224 | ALOGE("%s: failed to allocate memory for %zu buffer requests", |
| 1225 | __FUNCTION__, bufReqs.size()); |
| 1226 | _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets); |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | if (bufReqs.size() > states.outputStreams.size()) { |
| 1231 | ALOGE("%s: too many buffer requests (%zu > # of output streams %zu)", |
| 1232 | __FUNCTION__, bufReqs.size(), states.outputStreams.size()); |
| 1233 | _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets); |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | // Check for repeated streamId |
| 1238 | for (const auto& bufReq : bufReqs) { |
| 1239 | if (streamIds.indexOf(bufReq.streamId) != NAME_NOT_FOUND) { |
| 1240 | ALOGE("%s: Stream %d appear multiple times in buffer requests", |
| 1241 | __FUNCTION__, bufReq.streamId); |
| 1242 | _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets); |
| 1243 | return; |
| 1244 | } |
| 1245 | streamIds.add(bufReq.streamId); |
| 1246 | } |
| 1247 | |
| 1248 | if (!states.reqBufferIntf.startRequestBuffer()) { |
| 1249 | ALOGE("%s: request buffer disallowed while camera service is configuring", |
| 1250 | __FUNCTION__); |
| 1251 | _hidl_cb(BufferRequestStatus::FAILED_CONFIGURING, bufRets); |
| 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | bufRets.resize(bufReqs.size()); |
| 1256 | |
| 1257 | bool allReqsSucceeds = true; |
| 1258 | bool oneReqSucceeds = false; |
| 1259 | for (size_t i = 0; i < bufReqs.size(); i++) { |
| 1260 | const auto& bufReq = bufReqs[i]; |
| 1261 | auto& bufRet = bufRets[i]; |
| 1262 | int32_t streamId = bufReq.streamId; |
| 1263 | sp<Camera3OutputStreamInterface> outputStream = states.outputStreams.get(streamId); |
| 1264 | if (outputStream == nullptr) { |
| 1265 | ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId); |
| 1266 | hardware::hidl_vec<StreamBufferRet> emptyBufRets; |
| 1267 | _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, emptyBufRets); |
| 1268 | states.reqBufferIntf.endRequestBuffer(); |
| 1269 | return; |
| 1270 | } |
| 1271 | |
Yin-Chia Yeh | 2012653 | 2020-07-14 11:35:48 -0700 | [diff] [blame] | 1272 | bufRet.streamId = streamId; |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1273 | if (outputStream->isAbandoned()) { |
| 1274 | bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED); |
| 1275 | allReqsSucceeds = false; |
| 1276 | continue; |
| 1277 | } |
| 1278 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1279 | size_t handOutBufferCount = outputStream->getOutstandingBuffersCount(); |
| 1280 | uint32_t numBuffersRequested = bufReq.numBuffersRequested; |
| 1281 | size_t totalHandout = handOutBufferCount + numBuffersRequested; |
| 1282 | uint32_t maxBuffers = outputStream->asHalStream()->max_buffers; |
| 1283 | if (totalHandout > maxBuffers) { |
| 1284 | // Not able to allocate enough buffer. Exit early for this stream |
| 1285 | ALOGE("%s: request too much buffers for stream %d: at HAL: %zu + requesting: %d" |
| 1286 | " > max: %d", __FUNCTION__, streamId, handOutBufferCount, |
| 1287 | numBuffersRequested, maxBuffers); |
| 1288 | bufRet.val.error(StreamBufferRequestError::MAX_BUFFER_EXCEEDED); |
| 1289 | allReqsSucceeds = false; |
| 1290 | continue; |
| 1291 | } |
| 1292 | |
| 1293 | hardware::hidl_vec<StreamBuffer> tmpRetBuffers(numBuffersRequested); |
| 1294 | bool currentReqSucceeds = true; |
| 1295 | std::vector<camera3_stream_buffer_t> streamBuffers(numBuffersRequested); |
| 1296 | size_t numAllocatedBuffers = 0; |
| 1297 | size_t numPushedInflightBuffers = 0; |
| 1298 | for (size_t b = 0; b < numBuffersRequested; b++) { |
| 1299 | camera3_stream_buffer_t& sb = streamBuffers[b]; |
| 1300 | // Since this method can run concurrently with request thread |
| 1301 | // We need to update the wait duration everytime we call getbuffer |
| 1302 | nsecs_t waitDuration = states.reqBufferIntf.getWaitDuration(); |
| 1303 | status_t res = outputStream->getBuffer(&sb, waitDuration); |
| 1304 | if (res != OK) { |
| 1305 | if (res == NO_INIT || res == DEAD_OBJECT) { |
| 1306 | ALOGV("%s: Can't get output buffer for stream %d: %s (%d)", |
| 1307 | __FUNCTION__, streamId, strerror(-res), res); |
| 1308 | bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1309 | states.sessionStatsBuilder.stopCounter(streamId); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1310 | } else { |
| 1311 | ALOGE("%s: Can't get output buffer for stream %d: %s (%d)", |
| 1312 | __FUNCTION__, streamId, strerror(-res), res); |
| 1313 | if (res == TIMED_OUT || res == NO_MEMORY) { |
| 1314 | bufRet.val.error(StreamBufferRequestError::NO_BUFFER_AVAILABLE); |
| 1315 | } else { |
| 1316 | bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR); |
| 1317 | } |
| 1318 | } |
| 1319 | currentReqSucceeds = false; |
| 1320 | break; |
| 1321 | } |
| 1322 | numAllocatedBuffers++; |
| 1323 | |
| 1324 | buffer_handle_t *buffer = sb.buffer; |
| 1325 | auto pair = states.bufferRecordsIntf.getBufferId(*buffer, streamId); |
| 1326 | bool isNewBuffer = pair.first; |
| 1327 | uint64_t bufferId = pair.second; |
| 1328 | StreamBuffer& hBuf = tmpRetBuffers[b]; |
| 1329 | |
| 1330 | hBuf.streamId = streamId; |
| 1331 | hBuf.bufferId = bufferId; |
| 1332 | hBuf.buffer = (isNewBuffer) ? *buffer : nullptr; |
| 1333 | hBuf.status = BufferStatus::OK; |
| 1334 | hBuf.releaseFence = nullptr; |
| 1335 | |
| 1336 | native_handle_t *acquireFence = nullptr; |
| 1337 | if (sb.acquire_fence != -1) { |
| 1338 | acquireFence = native_handle_create(1,0); |
| 1339 | acquireFence->data[0] = sb.acquire_fence; |
| 1340 | } |
| 1341 | hBuf.acquireFence.setTo(acquireFence, /*shouldOwn*/true); |
| 1342 | hBuf.releaseFence = nullptr; |
| 1343 | |
| 1344 | res = states.bufferRecordsIntf.pushInflightRequestBuffer(bufferId, buffer, streamId); |
| 1345 | if (res != OK) { |
| 1346 | ALOGE("%s: Can't get register request buffers for stream %d: %s (%d)", |
| 1347 | __FUNCTION__, streamId, strerror(-res), res); |
| 1348 | bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR); |
| 1349 | currentReqSucceeds = false; |
| 1350 | break; |
| 1351 | } |
| 1352 | numPushedInflightBuffers++; |
| 1353 | } |
| 1354 | if (currentReqSucceeds) { |
| 1355 | bufRet.val.buffers(std::move(tmpRetBuffers)); |
| 1356 | oneReqSucceeds = true; |
| 1357 | } else { |
| 1358 | allReqsSucceeds = false; |
| 1359 | for (size_t b = 0; b < numPushedInflightBuffers; b++) { |
| 1360 | StreamBuffer& hBuf = tmpRetBuffers[b]; |
| 1361 | buffer_handle_t* buffer; |
| 1362 | status_t res = states.bufferRecordsIntf.popInflightRequestBuffer( |
| 1363 | hBuf.bufferId, &buffer); |
| 1364 | if (res != OK) { |
| 1365 | SET_ERR("%s: popInflightRequestBuffer failed for stream %d: %s (%d)", |
| 1366 | __FUNCTION__, streamId, strerror(-res), res); |
| 1367 | } |
| 1368 | } |
| 1369 | for (size_t b = 0; b < numAllocatedBuffers; b++) { |
| 1370 | camera3_stream_buffer_t& sb = streamBuffers[b]; |
| 1371 | sb.acquire_fence = -1; |
| 1372 | sb.status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1373 | } |
| 1374 | returnOutputBuffers(states.useHalBufManager, /*listener*/nullptr, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1375 | streamBuffers.data(), numAllocatedBuffers, 0, /*requested*/false, |
| 1376 | /*requestTimeNs*/0, states.sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | _hidl_cb(allReqsSucceeds ? BufferRequestStatus::OK : |
| 1381 | oneReqSucceeds ? BufferRequestStatus::FAILED_PARTIAL : |
| 1382 | BufferRequestStatus::FAILED_UNKNOWN, |
| 1383 | bufRets); |
| 1384 | states.reqBufferIntf.endRequestBuffer(); |
| 1385 | } |
| 1386 | |
| 1387 | void returnStreamBuffers(ReturnBufferStates& states, |
| 1388 | const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) { |
| 1389 | if (!states.useHalBufManager) { |
| 1390 | ALOGE("%s: Camera %s does not support HAL buffer managerment", |
| 1391 | __FUNCTION__, states.cameraId.string()); |
| 1392 | return; |
| 1393 | } |
| 1394 | |
| 1395 | for (const auto& buf : buffers) { |
| 1396 | if (buf.bufferId == BUFFER_ID_NO_BUFFER) { |
| 1397 | ALOGE("%s: cannot return a buffer without bufferId", __FUNCTION__); |
| 1398 | continue; |
| 1399 | } |
| 1400 | |
| 1401 | buffer_handle_t* buffer; |
| 1402 | status_t res = states.bufferRecordsIntf.popInflightRequestBuffer(buf.bufferId, &buffer); |
| 1403 | |
| 1404 | if (res != OK) { |
| 1405 | ALOGE("%s: cannot find in-flight buffer %" PRIu64 " for stream %d", |
| 1406 | __FUNCTION__, buf.bufferId, buf.streamId); |
| 1407 | continue; |
| 1408 | } |
| 1409 | |
| 1410 | camera3_stream_buffer_t streamBuffer; |
| 1411 | streamBuffer.buffer = buffer; |
| 1412 | streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1413 | streamBuffer.acquire_fence = -1; |
| 1414 | streamBuffer.release_fence = -1; |
| 1415 | |
| 1416 | if (buf.releaseFence == nullptr) { |
| 1417 | streamBuffer.release_fence = -1; |
| 1418 | } else if (buf.releaseFence->numFds == 1) { |
| 1419 | streamBuffer.release_fence = dup(buf.releaseFence->data[0]); |
| 1420 | } else { |
| 1421 | ALOGE("%s: Invalid release fence, fd count is %d, not 1", |
| 1422 | __FUNCTION__, buf.releaseFence->numFds); |
| 1423 | continue; |
| 1424 | } |
| 1425 | |
| 1426 | sp<Camera3StreamInterface> stream = states.outputStreams.get(buf.streamId); |
| 1427 | if (stream == nullptr) { |
| 1428 | ALOGE("%s: Output stream id %d not found!", __FUNCTION__, buf.streamId); |
| 1429 | continue; |
| 1430 | } |
| 1431 | streamBuffer.stream = stream->asHalStream(); |
| 1432 | returnOutputBuffers(states.useHalBufManager, /*listener*/nullptr, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1433 | &streamBuffer, /*size*/1, /*timestamp*/ 0, /*requested*/false, |
| 1434 | /*requestTimeNs*/0, states.sessionStatsBuilder); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | void flushInflightRequests(FlushInflightReqStates& states) { |
| 1439 | ATRACE_CALL(); |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1440 | { // First return buffers cached in inFlightMap |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1441 | std::lock_guard<std::mutex> l(states.inflightLock); |
| 1442 | for (size_t idx = 0; idx < states.inflightMap.size(); idx++) { |
Greg Kaiser | 51b882c | 2020-06-10 05:41:44 +0000 | [diff] [blame] | 1443 | const InFlightRequest &request = states.inflightMap.valueAt(idx); |
| 1444 | returnOutputBuffers( |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1445 | states.useHalBufManager, states.listener, |
| 1446 | request.pendingOutputBuffers.array(), |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1447 | request.pendingOutputBuffers.size(), 0, /*requested*/true, |
| 1448 | request.requestTimeNs, states.sessionStatsBuilder, /*timestampIncreasing*/true, |
| 1449 | request.outputSurfaces, request.resultExtras, request.errorBufStrategy); |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1450 | ALOGW("%s: Frame %d | Timestamp: %" PRId64 ", metadata" |
| 1451 | " arrived: %s, buffers left: %d.\n", __FUNCTION__, |
| 1452 | states.inflightMap.keyAt(idx), request.shutterTimestamp, |
| 1453 | request.haveResultMetadata ? "true" : "false", |
| 1454 | request.numBuffersLeft); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1455 | } |
Shuzhen Wang | b7b4265 | 2020-05-07 11:59:02 -0700 | [diff] [blame] | 1456 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1457 | states.inflightMap.clear(); |
| 1458 | states.inflightIntf.onInflightMapFlushedLocked(); |
| 1459 | } |
| 1460 | |
| 1461 | // Then return all inflight buffers not returned by HAL |
| 1462 | std::vector<std::pair<int32_t, int32_t>> inflightKeys; |
| 1463 | states.flushBufferIntf.getInflightBufferKeys(&inflightKeys); |
| 1464 | |
| 1465 | // Inflight buffers for HAL buffer manager |
| 1466 | std::vector<uint64_t> inflightRequestBufferKeys; |
| 1467 | states.flushBufferIntf.getInflightRequestBufferKeys(&inflightRequestBufferKeys); |
| 1468 | |
| 1469 | // (streamId, frameNumber, buffer_handle_t*) tuple for all inflight buffers. |
| 1470 | // frameNumber will be -1 for buffers from HAL buffer manager |
| 1471 | std::vector<std::tuple<int32_t, int32_t, buffer_handle_t*>> inflightBuffers; |
| 1472 | inflightBuffers.reserve(inflightKeys.size() + inflightRequestBufferKeys.size()); |
| 1473 | |
| 1474 | for (auto& pair : inflightKeys) { |
| 1475 | int32_t frameNumber = pair.first; |
| 1476 | int32_t streamId = pair.second; |
| 1477 | buffer_handle_t* buffer; |
| 1478 | status_t res = states.bufferRecordsIntf.popInflightBuffer(frameNumber, streamId, &buffer); |
| 1479 | if (res != OK) { |
| 1480 | ALOGE("%s: Frame %d: No in-flight buffer for stream %d", |
| 1481 | __FUNCTION__, frameNumber, streamId); |
| 1482 | continue; |
| 1483 | } |
| 1484 | inflightBuffers.push_back(std::make_tuple(streamId, frameNumber, buffer)); |
| 1485 | } |
| 1486 | |
| 1487 | for (auto& bufferId : inflightRequestBufferKeys) { |
| 1488 | int32_t streamId = -1; |
| 1489 | buffer_handle_t* buffer = nullptr; |
| 1490 | status_t res = states.bufferRecordsIntf.popInflightRequestBuffer( |
| 1491 | bufferId, &buffer, &streamId); |
| 1492 | if (res != OK) { |
| 1493 | ALOGE("%s: cannot find in-flight buffer %" PRIu64, __FUNCTION__, bufferId); |
| 1494 | continue; |
| 1495 | } |
| 1496 | inflightBuffers.push_back(std::make_tuple(streamId, /*frameNumber*/-1, buffer)); |
| 1497 | } |
| 1498 | |
| 1499 | std::vector<sp<Camera3StreamInterface>> streams = states.flushBufferIntf.getAllStreams(); |
| 1500 | |
| 1501 | for (auto& tuple : inflightBuffers) { |
| 1502 | status_t res = OK; |
| 1503 | int32_t streamId = std::get<0>(tuple); |
| 1504 | int32_t frameNumber = std::get<1>(tuple); |
| 1505 | buffer_handle_t* buffer = std::get<2>(tuple); |
| 1506 | |
| 1507 | camera3_stream_buffer_t streamBuffer; |
| 1508 | streamBuffer.buffer = buffer; |
| 1509 | streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1510 | streamBuffer.acquire_fence = -1; |
| 1511 | streamBuffer.release_fence = -1; |
| 1512 | |
| 1513 | for (auto& stream : streams) { |
| 1514 | if (streamId == stream->getId()) { |
| 1515 | // Return buffer to deleted stream |
| 1516 | camera3_stream* halStream = stream->asHalStream(); |
| 1517 | streamBuffer.stream = halStream; |
| 1518 | switch (halStream->stream_type) { |
| 1519 | case CAMERA3_STREAM_OUTPUT: |
| 1520 | res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1521 | /*timestampIncreasing*/true, |
| 1522 | std::vector<size_t> (), frameNumber); |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 1523 | if (res != OK) { |
| 1524 | ALOGE("%s: Can't return output buffer for frame %d to" |
| 1525 | " stream %d: %s (%d)", __FUNCTION__, |
| 1526 | frameNumber, streamId, strerror(-res), res); |
| 1527 | } |
| 1528 | break; |
| 1529 | case CAMERA3_STREAM_INPUT: |
| 1530 | res = stream->returnInputBuffer(streamBuffer); |
| 1531 | if (res != OK) { |
| 1532 | ALOGE("%s: Can't return input buffer for frame %d to" |
| 1533 | " stream %d: %s (%d)", __FUNCTION__, |
| 1534 | frameNumber, streamId, strerror(-res), res); |
| 1535 | } |
| 1536 | break; |
| 1537 | default: // Bi-direcitonal stream is deprecated |
| 1538 | ALOGE("%s: stream %d has unknown stream type %d", |
| 1539 | __FUNCTION__, streamId, halStream->stream_type); |
| 1540 | break; |
| 1541 | } |
| 1542 | break; |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | } // camera3 |
| 1549 | } // namespace android |