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