Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 1 | /* |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017-2018 The Android Open Source Project |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 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 "CamDevSession@3.4-impl" |
| 18 | #include <android/log.h> |
| 19 | |
| 20 | #include <set> |
| 21 | #include <utils/Trace.h> |
| 22 | #include <hardware/gralloc.h> |
| 23 | #include <hardware/gralloc1.h> |
| 24 | #include "CameraDeviceSession.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace camera { |
| 29 | namespace device { |
| 30 | namespace V3_4 { |
| 31 | namespace implementation { |
| 32 | |
| 33 | CameraDeviceSession::CameraDeviceSession( |
| 34 | camera3_device_t* device, |
| 35 | const camera_metadata_t* deviceInfo, |
| 36 | const sp<V3_2::ICameraDeviceCallback>& callback) : |
Shuzhen Wang | 39cf8fd | 2017-12-29 16:17:09 -0800 | [diff] [blame] | 37 | V3_3::implementation::CameraDeviceSession(device, deviceInfo, callback), |
| 38 | mResultBatcher_3_4(callback) { |
| 39 | |
| 40 | mHasCallback_3_4 = false; |
| 41 | |
| 42 | auto castResult = ICameraDeviceCallback::castFrom(callback); |
| 43 | if (castResult.isOk()) { |
| 44 | sp<ICameraDeviceCallback> callback3_4 = castResult; |
| 45 | if (callback3_4 != nullptr) { |
| 46 | process_capture_result = sProcessCaptureResult_3_4; |
| 47 | notify = sNotify_3_4; |
| 48 | mHasCallback_3_4 = true; |
| 49 | if (!mInitFail) { |
| 50 | mResultBatcher_3_4.setResultMetadataQueue(mResultMetadataQueue); |
| 51 | } |
| 52 | } |
| 53 | } |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | CameraDeviceSession::~CameraDeviceSession() { |
| 57 | } |
| 58 | |
Eino-Ville Talvala | 658d30d | 2018-01-18 12:55:07 -0800 | [diff] [blame] | 59 | Return<void> CameraDeviceSession::constructDefaultRequestSettings_3_4( |
| 60 | RequestTemplate type, ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) { |
| 61 | V3_2::CameraMetadata outMetadata; |
| 62 | Status status = constructDefaultRequestSettingsRaw( (int) type, &outMetadata); |
| 63 | _hidl_cb(status, outMetadata); |
| 64 | return Void(); |
| 65 | } |
| 66 | |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 67 | Return<void> CameraDeviceSession::configureStreams_3_4( |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 68 | const StreamConfiguration& requestedConfiguration, |
| 69 | ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb) { |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 70 | Status status = initStatus(); |
| 71 | HalStreamConfiguration outStreams; |
| 72 | |
Shuzhen Wang | 39cf8fd | 2017-12-29 16:17:09 -0800 | [diff] [blame] | 73 | // If callback is 3.2, make sure no physical stream is configured |
| 74 | if (!mHasCallback_3_4) { |
| 75 | for (size_t i = 0; i < requestedConfiguration.streams.size(); i++) { |
| 76 | if (requestedConfiguration.streams[i].physicalCameraId.size() > 0) { |
| 77 | ALOGE("%s: trying to configureStreams with physical camera id with V3.2 callback", |
| 78 | __FUNCTION__); |
| 79 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 80 | return Void(); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 85 | // hold the inflight lock for entire configureStreams scope since there must not be any |
| 86 | // inflight request/results during stream configuration. |
| 87 | Mutex::Autolock _l(mInflightLock); |
| 88 | if (!mInflightBuffers.empty()) { |
| 89 | ALOGE("%s: trying to configureStreams while there are still %zu inflight buffers!", |
| 90 | __FUNCTION__, mInflightBuffers.size()); |
| 91 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 92 | return Void(); |
| 93 | } |
| 94 | |
| 95 | if (!mInflightAETriggerOverrides.empty()) { |
| 96 | ALOGE("%s: trying to configureStreams while there are still %zu inflight" |
| 97 | " trigger overrides!", __FUNCTION__, |
| 98 | mInflightAETriggerOverrides.size()); |
| 99 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 100 | return Void(); |
| 101 | } |
| 102 | |
| 103 | if (!mInflightRawBoostPresent.empty()) { |
| 104 | ALOGE("%s: trying to configureStreams while there are still %zu inflight" |
| 105 | " boost overrides!", __FUNCTION__, |
| 106 | mInflightRawBoostPresent.size()); |
| 107 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 108 | return Void(); |
| 109 | } |
| 110 | |
| 111 | if (status != Status::OK) { |
| 112 | _hidl_cb(status, outStreams); |
| 113 | return Void(); |
| 114 | } |
| 115 | |
| 116 | const camera_metadata_t *paramBuffer = nullptr; |
| 117 | if (0 < requestedConfiguration.sessionParams.size()) { |
| 118 | ::android::hardware::camera::common::V1_0::helper::CameraMetadata sessionParams; |
| 119 | V3_2::implementation::convertFromHidl(requestedConfiguration.sessionParams, ¶mBuffer); |
| 120 | } |
| 121 | |
| 122 | camera3_stream_configuration_t stream_list{}; |
| 123 | hidl_vec<camera3_stream_t*> streams; |
| 124 | stream_list.session_parameters = paramBuffer; |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 125 | if (!preProcessConfigurationLocked_3_4(requestedConfiguration, &stream_list, &streams)) { |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 126 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 127 | return Void(); |
| 128 | } |
| 129 | |
| 130 | ATRACE_BEGIN("camera3->configure_streams"); |
| 131 | status_t ret = mDevice->ops->configure_streams(mDevice, &stream_list); |
| 132 | ATRACE_END(); |
| 133 | |
| 134 | // In case Hal returns error most likely it was not able to release |
| 135 | // the corresponding resources of the deleted streams. |
| 136 | if (ret == OK) { |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 137 | postProcessConfigurationLocked_3_4(requestedConfiguration); |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (ret == -EINVAL) { |
| 141 | status = Status::ILLEGAL_ARGUMENT; |
| 142 | } else if (ret != OK) { |
| 143 | status = Status::INTERNAL_ERROR; |
| 144 | } else { |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 145 | V3_4::implementation::convertToHidl(stream_list, &outStreams); |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 146 | mFirstRequest = true; |
| 147 | } |
| 148 | |
| 149 | _hidl_cb(status, outStreams); |
| 150 | return Void(); |
| 151 | } |
| 152 | |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 153 | bool CameraDeviceSession::preProcessConfigurationLocked_3_4( |
| 154 | const StreamConfiguration& requestedConfiguration, |
| 155 | camera3_stream_configuration_t *stream_list /*out*/, |
| 156 | hidl_vec<camera3_stream_t*> *streams /*out*/) { |
| 157 | |
| 158 | if ((stream_list == nullptr) || (streams == nullptr)) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | stream_list->operation_mode = (uint32_t) requestedConfiguration.operationMode; |
| 163 | stream_list->num_streams = requestedConfiguration.streams.size(); |
| 164 | streams->resize(stream_list->num_streams); |
| 165 | stream_list->streams = streams->data(); |
| 166 | |
| 167 | for (uint32_t i = 0; i < stream_list->num_streams; i++) { |
| 168 | int id = requestedConfiguration.streams[i].v3_2.id; |
| 169 | |
| 170 | if (mStreamMap.count(id) == 0) { |
| 171 | Camera3Stream stream; |
| 172 | convertFromHidl(requestedConfiguration.streams[i], &stream); |
| 173 | mStreamMap[id] = stream; |
| 174 | mPhysicalCameraIdMap[id] = requestedConfiguration.streams[i].physicalCameraId; |
| 175 | mStreamMap[id].data_space = mapToLegacyDataspace( |
| 176 | mStreamMap[id].data_space); |
| 177 | mStreamMap[id].physical_camera_id = mPhysicalCameraIdMap[id].c_str(); |
| 178 | mCirculatingBuffers.emplace(stream.mId, CirculatingBuffers{}); |
| 179 | } else { |
| 180 | // width/height/format must not change, but usage/rotation might need to change |
| 181 | if (mStreamMap[id].stream_type != |
| 182 | (int) requestedConfiguration.streams[i].v3_2.streamType || |
| 183 | mStreamMap[id].width != requestedConfiguration.streams[i].v3_2.width || |
| 184 | mStreamMap[id].height != requestedConfiguration.streams[i].v3_2.height || |
| 185 | mStreamMap[id].format != (int) requestedConfiguration.streams[i].v3_2.format || |
| 186 | mStreamMap[id].data_space != |
| 187 | mapToLegacyDataspace( static_cast<android_dataspace_t> ( |
| 188 | requestedConfiguration.streams[i].v3_2.dataSpace)) || |
| 189 | mPhysicalCameraIdMap[id] != requestedConfiguration.streams[i].physicalCameraId) { |
| 190 | ALOGE("%s: stream %d configuration changed!", __FUNCTION__, id); |
| 191 | return false; |
| 192 | } |
| 193 | mStreamMap[id].rotation = (int) requestedConfiguration.streams[i].v3_2.rotation; |
| 194 | mStreamMap[id].usage = (uint32_t) requestedConfiguration.streams[i].v3_2.usage; |
| 195 | } |
| 196 | (*streams)[i] = &mStreamMap[id]; |
| 197 | } |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | void CameraDeviceSession::postProcessConfigurationLocked_3_4( |
| 203 | const StreamConfiguration& requestedConfiguration) { |
| 204 | // delete unused streams, note we do this after adding new streams to ensure new stream |
| 205 | // will not have the same address as deleted stream, and HAL has a chance to reference |
| 206 | // the to be deleted stream in configure_streams call |
| 207 | for(auto it = mStreamMap.begin(); it != mStreamMap.end();) { |
| 208 | int id = it->first; |
| 209 | bool found = false; |
| 210 | for (const auto& stream : requestedConfiguration.streams) { |
| 211 | if (id == stream.v3_2.id) { |
| 212 | found = true; |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | if (!found) { |
| 217 | // Unmap all buffers of deleted stream |
| 218 | // in case the configuration call succeeds and HAL |
| 219 | // is able to release the corresponding resources too. |
| 220 | cleanupBuffersLocked(id); |
| 221 | it = mStreamMap.erase(it); |
| 222 | } else { |
| 223 | ++it; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // Track video streams |
| 228 | mVideoStreamIds.clear(); |
| 229 | for (const auto& stream : requestedConfiguration.streams) { |
| 230 | if (stream.v3_2.streamType == StreamType::OUTPUT && |
| 231 | stream.v3_2.usage & |
| 232 | graphics::common::V1_0::BufferUsage::VIDEO_ENCODER) { |
| 233 | mVideoStreamIds.push_back(stream.v3_2.id); |
| 234 | } |
| 235 | } |
Shuzhen Wang | 39cf8fd | 2017-12-29 16:17:09 -0800 | [diff] [blame] | 236 | mResultBatcher_3_4.setBatchedStreams(mVideoStreamIds); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | Return<void> CameraDeviceSession::processCaptureRequest_3_4( |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 240 | const hidl_vec<V3_4::CaptureRequest>& requests, |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 241 | const hidl_vec<V3_2::BufferCache>& cachesToRemove, |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 242 | ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb) { |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 243 | updateBufferCaches(cachesToRemove); |
| 244 | |
| 245 | uint32_t numRequestProcessed = 0; |
| 246 | Status s = Status::OK; |
| 247 | for (size_t i = 0; i < requests.size(); i++, numRequestProcessed++) { |
| 248 | s = processOneCaptureRequest_3_4(requests[i]); |
| 249 | if (s != Status::OK) { |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if (s == Status::OK && requests.size() > 1) { |
Shuzhen Wang | 39cf8fd | 2017-12-29 16:17:09 -0800 | [diff] [blame] | 255 | mResultBatcher_3_4.registerBatch(requests[0].v3_2.frameNumber, requests.size()); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | _hidl_cb(s, numRequestProcessed); |
| 259 | return Void(); |
| 260 | } |
| 261 | |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 262 | Status CameraDeviceSession::processOneCaptureRequest_3_4(const V3_4::CaptureRequest& request) { |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 263 | Status status = initStatus(); |
| 264 | if (status != Status::OK) { |
| 265 | ALOGE("%s: camera init failed or disconnected", __FUNCTION__); |
| 266 | return status; |
| 267 | } |
Shuzhen Wang | 39cf8fd | 2017-12-29 16:17:09 -0800 | [diff] [blame] | 268 | // If callback is 3.2, make sure there are no physical settings. |
| 269 | if (!mHasCallback_3_4) { |
| 270 | if (request.physicalCameraSettings.size() > 0) { |
| 271 | ALOGE("%s: trying to call processCaptureRequest_3_4 with physical camera id " |
| 272 | "and V3.2 callback", __FUNCTION__); |
| 273 | return Status::INTERNAL_ERROR; |
| 274 | } |
| 275 | } |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 276 | |
| 277 | camera3_capture_request_t halRequest; |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 278 | halRequest.frame_number = request.v3_2.frameNumber; |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 279 | |
| 280 | bool converted = true; |
| 281 | V3_2::CameraMetadata settingsFmq; // settings from FMQ |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 282 | if (request.v3_2.fmqSettingsSize > 0) { |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 283 | // non-blocking read; client must write metadata before calling |
| 284 | // processOneCaptureRequest |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 285 | settingsFmq.resize(request.v3_2.fmqSettingsSize); |
| 286 | bool read = mRequestMetadataQueue->read(settingsFmq.data(), request.v3_2.fmqSettingsSize); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 287 | if (read) { |
| 288 | converted = V3_2::implementation::convertFromHidl(settingsFmq, &halRequest.settings); |
| 289 | } else { |
| 290 | ALOGE("%s: capture request settings metadata couldn't be read from fmq!", __FUNCTION__); |
| 291 | converted = false; |
| 292 | } |
| 293 | } else { |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 294 | converted = V3_2::implementation::convertFromHidl(request.v3_2.settings, |
| 295 | &halRequest.settings); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if (!converted) { |
| 299 | ALOGE("%s: capture request settings metadata is corrupt!", __FUNCTION__); |
| 300 | return Status::ILLEGAL_ARGUMENT; |
| 301 | } |
| 302 | |
| 303 | if (mFirstRequest && halRequest.settings == nullptr) { |
| 304 | ALOGE("%s: capture request settings must not be null for first request!", |
| 305 | __FUNCTION__); |
| 306 | return Status::ILLEGAL_ARGUMENT; |
| 307 | } |
| 308 | |
| 309 | hidl_vec<buffer_handle_t*> allBufPtrs; |
| 310 | hidl_vec<int> allFences; |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 311 | bool hasInputBuf = (request.v3_2.inputBuffer.streamId != -1 && |
| 312 | request.v3_2.inputBuffer.bufferId != 0); |
| 313 | size_t numOutputBufs = request.v3_2.outputBuffers.size(); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 314 | size_t numBufs = numOutputBufs + (hasInputBuf ? 1 : 0); |
| 315 | |
| 316 | if (numOutputBufs == 0) { |
| 317 | ALOGE("%s: capture request must have at least one output buffer!", __FUNCTION__); |
| 318 | return Status::ILLEGAL_ARGUMENT; |
| 319 | } |
| 320 | |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 321 | status = importRequest(request.v3_2, allBufPtrs, allFences); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 322 | if (status != Status::OK) { |
| 323 | return status; |
| 324 | } |
| 325 | |
| 326 | hidl_vec<camera3_stream_buffer_t> outHalBufs; |
| 327 | outHalBufs.resize(numOutputBufs); |
| 328 | bool aeCancelTriggerNeeded = false; |
| 329 | ::android::hardware::camera::common::V1_0::helper::CameraMetadata settingsOverride; |
| 330 | { |
| 331 | Mutex::Autolock _l(mInflightLock); |
| 332 | if (hasInputBuf) { |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 333 | auto streamId = request.v3_2.inputBuffer.streamId; |
| 334 | auto key = std::make_pair(request.v3_2.inputBuffer.streamId, request.v3_2.frameNumber); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 335 | auto& bufCache = mInflightBuffers[key] = camera3_stream_buffer_t{}; |
| 336 | convertFromHidl( |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 337 | allBufPtrs[numOutputBufs], request.v3_2.inputBuffer.status, |
| 338 | &mStreamMap[request.v3_2.inputBuffer.streamId], allFences[numOutputBufs], |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 339 | &bufCache); |
| 340 | bufCache.stream->physical_camera_id = mPhysicalCameraIdMap[streamId].c_str(); |
| 341 | halRequest.input_buffer = &bufCache; |
| 342 | } else { |
| 343 | halRequest.input_buffer = nullptr; |
| 344 | } |
| 345 | |
| 346 | halRequest.num_output_buffers = numOutputBufs; |
| 347 | for (size_t i = 0; i < numOutputBufs; i++) { |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 348 | auto streamId = request.v3_2.outputBuffers[i].streamId; |
| 349 | auto key = std::make_pair(streamId, request.v3_2.frameNumber); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 350 | auto& bufCache = mInflightBuffers[key] = camera3_stream_buffer_t{}; |
| 351 | convertFromHidl( |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 352 | allBufPtrs[i], request.v3_2.outputBuffers[i].status, |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 353 | &mStreamMap[streamId], allFences[i], |
| 354 | &bufCache); |
| 355 | bufCache.stream->physical_camera_id = mPhysicalCameraIdMap[streamId].c_str(); |
| 356 | outHalBufs[i] = bufCache; |
| 357 | } |
| 358 | halRequest.output_buffers = outHalBufs.data(); |
| 359 | |
| 360 | AETriggerCancelOverride triggerOverride; |
| 361 | aeCancelTriggerNeeded = handleAePrecaptureCancelRequestLocked( |
| 362 | halRequest, &settingsOverride /*out*/, &triggerOverride/*out*/); |
| 363 | if (aeCancelTriggerNeeded) { |
| 364 | mInflightAETriggerOverrides[halRequest.frame_number] = |
| 365 | triggerOverride; |
| 366 | halRequest.settings = settingsOverride.getAndLock(); |
| 367 | } |
| 368 | } |
| 369 | |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 370 | std::vector<const char *> physicalCameraIds; |
| 371 | std::vector<const camera_metadata_t *> physicalCameraSettings; |
| 372 | std::vector<V3_2::CameraMetadata> physicalFmq; |
| 373 | size_t settingsCount = request.physicalCameraSettings.size(); |
| 374 | if (settingsCount > 0) { |
| 375 | physicalCameraIds.reserve(settingsCount); |
| 376 | physicalCameraSettings.reserve(settingsCount); |
| 377 | physicalFmq.reserve(settingsCount); |
| 378 | |
| 379 | for (size_t i = 0; i < settingsCount; i++) { |
| 380 | uint64_t settingsSize = request.physicalCameraSettings[i].fmqSettingsSize; |
| 381 | const camera_metadata_t *settings; |
| 382 | if (settingsSize > 0) { |
| 383 | physicalFmq.push_back(V3_2::CameraMetadata(settingsSize)); |
| 384 | bool read = mRequestMetadataQueue->read(physicalFmq[i].data(), settingsSize); |
| 385 | if (read) { |
| 386 | converted = V3_2::implementation::convertFromHidl(physicalFmq[i], &settings); |
| 387 | physicalCameraSettings.push_back(settings); |
| 388 | } else { |
| 389 | ALOGE("%s: physical camera settings metadata couldn't be read from fmq!", |
| 390 | __FUNCTION__); |
| 391 | converted = false; |
| 392 | } |
| 393 | } else { |
| 394 | converted = V3_2::implementation::convertFromHidl( |
| 395 | request.physicalCameraSettings[i].settings, &settings); |
| 396 | physicalCameraSettings.push_back(settings); |
| 397 | } |
| 398 | |
| 399 | if (!converted) { |
| 400 | ALOGE("%s: physical camera settings metadata is corrupt!", __FUNCTION__); |
| 401 | return Status::ILLEGAL_ARGUMENT; |
| 402 | } |
| 403 | physicalCameraIds.push_back(request.physicalCameraSettings[i].physicalCameraId.c_str()); |
| 404 | } |
| 405 | } |
| 406 | halRequest.num_physcam_settings = settingsCount; |
| 407 | halRequest.physcam_id = physicalCameraIds.data(); |
| 408 | halRequest.physcam_settings = physicalCameraSettings.data(); |
| 409 | |
| 410 | ATRACE_ASYNC_BEGIN("frame capture", request.v3_2.frameNumber); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 411 | ATRACE_BEGIN("camera3->process_capture_request"); |
| 412 | status_t ret = mDevice->ops->process_capture_request(mDevice, &halRequest); |
| 413 | ATRACE_END(); |
| 414 | if (aeCancelTriggerNeeded) { |
| 415 | settingsOverride.unlock(halRequest.settings); |
| 416 | } |
| 417 | if (ret != OK) { |
| 418 | Mutex::Autolock _l(mInflightLock); |
| 419 | ALOGE("%s: HAL process_capture_request call failed!", __FUNCTION__); |
| 420 | |
| 421 | cleanupInflightFences(allFences, numBufs); |
| 422 | if (hasInputBuf) { |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 423 | auto key = std::make_pair(request.v3_2.inputBuffer.streamId, request.v3_2.frameNumber); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 424 | mInflightBuffers.erase(key); |
| 425 | } |
| 426 | for (size_t i = 0; i < numOutputBufs; i++) { |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 427 | auto key = std::make_pair(request.v3_2.outputBuffers[i].streamId, |
| 428 | request.v3_2.frameNumber); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 429 | mInflightBuffers.erase(key); |
| 430 | } |
| 431 | if (aeCancelTriggerNeeded) { |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 432 | mInflightAETriggerOverrides.erase(request.v3_2.frameNumber); |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 433 | } |
Emilian Peev | b75aa35 | 2018-01-17 11:00:54 +0000 | [diff] [blame] | 434 | |
| 435 | if (ret == BAD_VALUE) { |
| 436 | return Status::ILLEGAL_ARGUMENT; |
| 437 | } else { |
| 438 | return Status::INTERNAL_ERROR; |
| 439 | } |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | mFirstRequest = false; |
| 443 | return Status::OK; |
| 444 | } |
| 445 | |
Shuzhen Wang | 39cf8fd | 2017-12-29 16:17:09 -0800 | [diff] [blame] | 446 | /** |
| 447 | * Static callback forwarding methods from HAL to instance |
| 448 | */ |
| 449 | void CameraDeviceSession::sProcessCaptureResult_3_4( |
| 450 | const camera3_callback_ops *cb, |
| 451 | const camera3_capture_result *hal_result) { |
| 452 | CameraDeviceSession *d = |
| 453 | const_cast<CameraDeviceSession*>(static_cast<const CameraDeviceSession*>(cb)); |
| 454 | |
| 455 | CaptureResult result; |
| 456 | d->constructCaptureResult(result.v3_2, hal_result); |
| 457 | result.physicalCameraMetadata.resize(hal_result->num_physcam_metadata); |
| 458 | for (uint32_t i = 0; i < hal_result->num_physcam_metadata; i++) { |
| 459 | std::string physicalId = hal_result->physcam_ids[i]; |
| 460 | V3_2::CameraMetadata physicalMetadata; |
| 461 | V3_2::implementation::convertToHidl(hal_result->physcam_metadata[i], &physicalMetadata); |
| 462 | PhysicalCameraMetadata physicalCameraMetadata = { |
| 463 | .fmqMetadataSize = 0, |
| 464 | .physicalCameraId = physicalId, |
| 465 | .metadata = physicalMetadata }; |
| 466 | result.physicalCameraMetadata[i] = physicalCameraMetadata; |
| 467 | } |
| 468 | d->mResultBatcher_3_4.processCaptureResult_3_4(result); |
| 469 | } |
| 470 | |
| 471 | void CameraDeviceSession::sNotify_3_4( |
| 472 | const camera3_callback_ops *cb, |
| 473 | const camera3_notify_msg *msg) { |
| 474 | CameraDeviceSession *d = |
| 475 | const_cast<CameraDeviceSession*>(static_cast<const CameraDeviceSession*>(cb)); |
| 476 | V3_2::NotifyMsg hidlMsg; |
| 477 | V3_2::implementation::convertToHidl(msg, &hidlMsg); |
| 478 | |
| 479 | if (hidlMsg.type == (V3_2::MsgType) CAMERA3_MSG_ERROR && |
| 480 | hidlMsg.msg.error.errorStreamId != -1) { |
| 481 | if (d->mStreamMap.count(hidlMsg.msg.error.errorStreamId) != 1) { |
| 482 | ALOGE("%s: unknown stream ID %d reports an error!", |
| 483 | __FUNCTION__, hidlMsg.msg.error.errorStreamId); |
| 484 | return; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (static_cast<camera3_msg_type_t>(hidlMsg.type) == CAMERA3_MSG_ERROR) { |
| 489 | switch (hidlMsg.msg.error.errorCode) { |
| 490 | case V3_2::ErrorCode::ERROR_DEVICE: |
| 491 | case V3_2::ErrorCode::ERROR_REQUEST: |
| 492 | case V3_2::ErrorCode::ERROR_RESULT: { |
| 493 | Mutex::Autolock _l(d->mInflightLock); |
| 494 | auto entry = d->mInflightAETriggerOverrides.find( |
| 495 | hidlMsg.msg.error.frameNumber); |
| 496 | if (d->mInflightAETriggerOverrides.end() != entry) { |
| 497 | d->mInflightAETriggerOverrides.erase( |
| 498 | hidlMsg.msg.error.frameNumber); |
| 499 | } |
| 500 | |
| 501 | auto boostEntry = d->mInflightRawBoostPresent.find( |
| 502 | hidlMsg.msg.error.frameNumber); |
| 503 | if (d->mInflightRawBoostPresent.end() != boostEntry) { |
| 504 | d->mInflightRawBoostPresent.erase( |
| 505 | hidlMsg.msg.error.frameNumber); |
| 506 | } |
| 507 | |
| 508 | } |
| 509 | break; |
| 510 | case V3_2::ErrorCode::ERROR_BUFFER: |
| 511 | default: |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | } |
| 516 | |
| 517 | d->mResultBatcher_3_4.notify(hidlMsg); |
| 518 | } |
| 519 | |
| 520 | CameraDeviceSession::ResultBatcher_3_4::ResultBatcher_3_4( |
| 521 | const sp<V3_2::ICameraDeviceCallback>& callback) : |
| 522 | V3_3::implementation::CameraDeviceSession::ResultBatcher(callback) { |
| 523 | auto castResult = ICameraDeviceCallback::castFrom(callback); |
| 524 | if (castResult.isOk()) { |
| 525 | mCallback_3_4 = castResult; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void CameraDeviceSession::ResultBatcher_3_4::processCaptureResult_3_4(CaptureResult& result) { |
| 530 | auto pair = getBatch(result.v3_2.frameNumber); |
| 531 | int batchIdx = pair.first; |
| 532 | if (batchIdx == NOT_BATCHED) { |
| 533 | processOneCaptureResult_3_4(result); |
| 534 | return; |
| 535 | } |
| 536 | std::shared_ptr<InflightBatch> batch = pair.second; |
| 537 | { |
| 538 | Mutex::Autolock _l(batch->mLock); |
| 539 | // Check if the batch is removed (mostly by notify error) before lock was acquired |
| 540 | if (batch->mRemoved) { |
| 541 | // Fall back to non-batch path |
| 542 | processOneCaptureResult_3_4(result); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | // queue metadata |
| 547 | if (result.v3_2.result.size() != 0) { |
| 548 | // Save a copy of metadata |
| 549 | batch->mResultMds[result.v3_2.partialResult].mMds.push_back( |
| 550 | std::make_pair(result.v3_2.frameNumber, result.v3_2.result)); |
| 551 | } |
| 552 | |
| 553 | // queue buffer |
| 554 | std::vector<int> filledStreams; |
| 555 | std::vector<V3_2::StreamBuffer> nonBatchedBuffers; |
| 556 | for (auto& buffer : result.v3_2.outputBuffers) { |
| 557 | auto it = batch->mBatchBufs.find(buffer.streamId); |
| 558 | if (it != batch->mBatchBufs.end()) { |
| 559 | InflightBatch::BufferBatch& bb = it->second; |
| 560 | pushStreamBuffer(std::move(buffer), bb.mBuffers); |
| 561 | filledStreams.push_back(buffer.streamId); |
| 562 | } else { |
| 563 | pushStreamBuffer(std::move(buffer), nonBatchedBuffers); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | // send non-batched buffers up |
| 568 | if (nonBatchedBuffers.size() > 0 || result.v3_2.inputBuffer.streamId != -1) { |
| 569 | CaptureResult nonBatchedResult; |
| 570 | nonBatchedResult.v3_2.frameNumber = result.v3_2.frameNumber; |
| 571 | nonBatchedResult.v3_2.fmqResultSize = 0; |
| 572 | nonBatchedResult.v3_2.outputBuffers.resize(nonBatchedBuffers.size()); |
| 573 | for (size_t i = 0; i < nonBatchedBuffers.size(); i++) { |
| 574 | moveStreamBuffer( |
| 575 | std::move(nonBatchedBuffers[i]), nonBatchedResult.v3_2.outputBuffers[i]); |
| 576 | } |
| 577 | moveStreamBuffer(std::move(result.v3_2.inputBuffer), nonBatchedResult.v3_2.inputBuffer); |
| 578 | nonBatchedResult.v3_2.partialResult = 0; // 0 for buffer only results |
| 579 | processOneCaptureResult_3_4(nonBatchedResult); |
| 580 | } |
| 581 | |
| 582 | if (result.v3_2.frameNumber == batch->mLastFrame) { |
| 583 | // Send data up |
| 584 | if (result.v3_2.partialResult > 0) { |
| 585 | sendBatchMetadataLocked(batch, result.v3_2.partialResult); |
| 586 | } |
| 587 | // send buffer up |
| 588 | if (filledStreams.size() > 0) { |
| 589 | sendBatchBuffersLocked(batch, filledStreams); |
| 590 | } |
| 591 | } |
| 592 | } // end of batch lock scope |
| 593 | |
| 594 | // see if the batch is complete |
| 595 | if (result.v3_2.frameNumber == batch->mLastFrame) { |
| 596 | checkAndRemoveFirstBatch(); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void CameraDeviceSession::ResultBatcher_3_4::processOneCaptureResult_3_4(CaptureResult& result) { |
| 601 | hidl_vec<CaptureResult> results; |
| 602 | results.resize(1); |
| 603 | results[0] = std::move(result); |
| 604 | invokeProcessCaptureResultCallback_3_4(results, /* tryWriteFmq */true); |
| 605 | freeReleaseFences_3_4(results); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | void CameraDeviceSession::ResultBatcher_3_4::invokeProcessCaptureResultCallback_3_4( |
| 610 | hidl_vec<CaptureResult> &results, bool tryWriteFmq) { |
| 611 | if (mProcessCaptureResultLock.tryLock() != OK) { |
| 612 | ALOGV("%s: previous call is not finished! waiting 1s...", __FUNCTION__); |
| 613 | if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) { |
| 614 | ALOGE("%s: cannot acquire lock in 1s, cannot proceed", |
| 615 | __FUNCTION__); |
| 616 | return; |
| 617 | } |
| 618 | } |
| 619 | if (tryWriteFmq && mResultMetadataQueue->availableToWrite() > 0) { |
| 620 | for (CaptureResult &result : results) { |
| 621 | if (result.v3_2.result.size() > 0) { |
| 622 | if (mResultMetadataQueue->write(result.v3_2.result.data(), |
| 623 | result.v3_2.result.size())) { |
| 624 | result.v3_2.fmqResultSize = result.v3_2.result.size(); |
| 625 | result.v3_2.result.resize(0); |
| 626 | } else { |
| 627 | ALOGW("%s: couldn't utilize fmq, fall back to hwbinder", __FUNCTION__); |
| 628 | result.v3_2.fmqResultSize = 0; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | for (auto& onePhysMetadata : result.physicalCameraMetadata) { |
| 633 | if (mResultMetadataQueue->write(onePhysMetadata.metadata.data(), |
| 634 | onePhysMetadata.metadata.size())) { |
| 635 | onePhysMetadata.fmqMetadataSize = onePhysMetadata.metadata.size(); |
| 636 | onePhysMetadata.metadata.resize(0); |
| 637 | } else { |
| 638 | ALOGW("%s: couldn't utilize fmq, fall back to hwbinder", __FUNCTION__); |
| 639 | onePhysMetadata.fmqMetadataSize = 0; |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | mCallback_3_4->processCaptureResult_3_4(results); |
| 645 | mProcessCaptureResultLock.unlock(); |
| 646 | } |
| 647 | |
| 648 | void CameraDeviceSession::ResultBatcher_3_4::freeReleaseFences_3_4(hidl_vec<CaptureResult>& results) { |
| 649 | for (auto& result : results) { |
| 650 | if (result.v3_2.inputBuffer.releaseFence.getNativeHandle() != nullptr) { |
| 651 | native_handle_t* handle = const_cast<native_handle_t*>( |
| 652 | result.v3_2.inputBuffer.releaseFence.getNativeHandle()); |
| 653 | native_handle_close(handle); |
| 654 | native_handle_delete(handle); |
| 655 | } |
| 656 | for (auto& buf : result.v3_2.outputBuffers) { |
| 657 | if (buf.releaseFence.getNativeHandle() != nullptr) { |
| 658 | native_handle_t* handle = const_cast<native_handle_t*>( |
| 659 | buf.releaseFence.getNativeHandle()); |
| 660 | native_handle_close(handle); |
| 661 | native_handle_delete(handle); |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | return; |
| 666 | } |
| 667 | |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 668 | } // namespace implementation |
| 669 | } // namespace V3_4 |
| 670 | } // namespace device |
| 671 | } // namespace camera |
| 672 | } // namespace hardware |
| 673 | } // namespace android |