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) : |
| 37 | V3_3::implementation::CameraDeviceSession(device, deviceInfo, callback) { |
| 38 | } |
| 39 | |
| 40 | CameraDeviceSession::~CameraDeviceSession() { |
| 41 | } |
| 42 | |
| 43 | Return<void> CameraDeviceSession::configureStreams_3_4( |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame^] | 44 | const StreamConfiguration& requestedConfiguration, |
| 45 | ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb) { |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 46 | Status status = initStatus(); |
| 47 | HalStreamConfiguration outStreams; |
| 48 | |
| 49 | // hold the inflight lock for entire configureStreams scope since there must not be any |
| 50 | // inflight request/results during stream configuration. |
| 51 | Mutex::Autolock _l(mInflightLock); |
| 52 | if (!mInflightBuffers.empty()) { |
| 53 | ALOGE("%s: trying to configureStreams while there are still %zu inflight buffers!", |
| 54 | __FUNCTION__, mInflightBuffers.size()); |
| 55 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 56 | return Void(); |
| 57 | } |
| 58 | |
| 59 | if (!mInflightAETriggerOverrides.empty()) { |
| 60 | ALOGE("%s: trying to configureStreams while there are still %zu inflight" |
| 61 | " trigger overrides!", __FUNCTION__, |
| 62 | mInflightAETriggerOverrides.size()); |
| 63 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 64 | return Void(); |
| 65 | } |
| 66 | |
| 67 | if (!mInflightRawBoostPresent.empty()) { |
| 68 | ALOGE("%s: trying to configureStreams while there are still %zu inflight" |
| 69 | " boost overrides!", __FUNCTION__, |
| 70 | mInflightRawBoostPresent.size()); |
| 71 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 72 | return Void(); |
| 73 | } |
| 74 | |
| 75 | if (status != Status::OK) { |
| 76 | _hidl_cb(status, outStreams); |
| 77 | return Void(); |
| 78 | } |
| 79 | |
| 80 | const camera_metadata_t *paramBuffer = nullptr; |
| 81 | if (0 < requestedConfiguration.sessionParams.size()) { |
| 82 | ::android::hardware::camera::common::V1_0::helper::CameraMetadata sessionParams; |
| 83 | V3_2::implementation::convertFromHidl(requestedConfiguration.sessionParams, ¶mBuffer); |
| 84 | } |
| 85 | |
| 86 | camera3_stream_configuration_t stream_list{}; |
| 87 | hidl_vec<camera3_stream_t*> streams; |
| 88 | stream_list.session_parameters = paramBuffer; |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame^] | 89 | if (!preProcessConfigurationLocked_3_4(requestedConfiguration, &stream_list, &streams)) { |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 90 | _hidl_cb(Status::INTERNAL_ERROR, outStreams); |
| 91 | return Void(); |
| 92 | } |
| 93 | |
| 94 | ATRACE_BEGIN("camera3->configure_streams"); |
| 95 | status_t ret = mDevice->ops->configure_streams(mDevice, &stream_list); |
| 96 | ATRACE_END(); |
| 97 | |
| 98 | // In case Hal returns error most likely it was not able to release |
| 99 | // the corresponding resources of the deleted streams. |
| 100 | if (ret == OK) { |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame^] | 101 | postProcessConfigurationLocked_3_4(requestedConfiguration); |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | if (ret == -EINVAL) { |
| 105 | status = Status::ILLEGAL_ARGUMENT; |
| 106 | } else if (ret != OK) { |
| 107 | status = Status::INTERNAL_ERROR; |
| 108 | } else { |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame^] | 109 | V3_4::implementation::convertToHidl(stream_list, &outStreams); |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 110 | mFirstRequest = true; |
| 111 | } |
| 112 | |
| 113 | _hidl_cb(status, outStreams); |
| 114 | return Void(); |
| 115 | } |
| 116 | |
Shuzhen Wang | 82e36b3 | 2017-11-28 17:00:43 -0800 | [diff] [blame^] | 117 | bool CameraDeviceSession::preProcessConfigurationLocked_3_4( |
| 118 | const StreamConfiguration& requestedConfiguration, |
| 119 | camera3_stream_configuration_t *stream_list /*out*/, |
| 120 | hidl_vec<camera3_stream_t*> *streams /*out*/) { |
| 121 | |
| 122 | if ((stream_list == nullptr) || (streams == nullptr)) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | stream_list->operation_mode = (uint32_t) requestedConfiguration.operationMode; |
| 127 | stream_list->num_streams = requestedConfiguration.streams.size(); |
| 128 | streams->resize(stream_list->num_streams); |
| 129 | stream_list->streams = streams->data(); |
| 130 | |
| 131 | for (uint32_t i = 0; i < stream_list->num_streams; i++) { |
| 132 | int id = requestedConfiguration.streams[i].v3_2.id; |
| 133 | |
| 134 | if (mStreamMap.count(id) == 0) { |
| 135 | Camera3Stream stream; |
| 136 | convertFromHidl(requestedConfiguration.streams[i], &stream); |
| 137 | mStreamMap[id] = stream; |
| 138 | mPhysicalCameraIdMap[id] = requestedConfiguration.streams[i].physicalCameraId; |
| 139 | mStreamMap[id].data_space = mapToLegacyDataspace( |
| 140 | mStreamMap[id].data_space); |
| 141 | mStreamMap[id].physical_camera_id = mPhysicalCameraIdMap[id].c_str(); |
| 142 | mCirculatingBuffers.emplace(stream.mId, CirculatingBuffers{}); |
| 143 | } else { |
| 144 | // width/height/format must not change, but usage/rotation might need to change |
| 145 | if (mStreamMap[id].stream_type != |
| 146 | (int) requestedConfiguration.streams[i].v3_2.streamType || |
| 147 | mStreamMap[id].width != requestedConfiguration.streams[i].v3_2.width || |
| 148 | mStreamMap[id].height != requestedConfiguration.streams[i].v3_2.height || |
| 149 | mStreamMap[id].format != (int) requestedConfiguration.streams[i].v3_2.format || |
| 150 | mStreamMap[id].data_space != |
| 151 | mapToLegacyDataspace( static_cast<android_dataspace_t> ( |
| 152 | requestedConfiguration.streams[i].v3_2.dataSpace)) || |
| 153 | mPhysicalCameraIdMap[id] != requestedConfiguration.streams[i].physicalCameraId) { |
| 154 | ALOGE("%s: stream %d configuration changed!", __FUNCTION__, id); |
| 155 | return false; |
| 156 | } |
| 157 | mStreamMap[id].rotation = (int) requestedConfiguration.streams[i].v3_2.rotation; |
| 158 | mStreamMap[id].usage = (uint32_t) requestedConfiguration.streams[i].v3_2.usage; |
| 159 | } |
| 160 | (*streams)[i] = &mStreamMap[id]; |
| 161 | } |
| 162 | |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | void CameraDeviceSession::postProcessConfigurationLocked_3_4( |
| 167 | const StreamConfiguration& requestedConfiguration) { |
| 168 | // delete unused streams, note we do this after adding new streams to ensure new stream |
| 169 | // will not have the same address as deleted stream, and HAL has a chance to reference |
| 170 | // the to be deleted stream in configure_streams call |
| 171 | for(auto it = mStreamMap.begin(); it != mStreamMap.end();) { |
| 172 | int id = it->first; |
| 173 | bool found = false; |
| 174 | for (const auto& stream : requestedConfiguration.streams) { |
| 175 | if (id == stream.v3_2.id) { |
| 176 | found = true; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | if (!found) { |
| 181 | // Unmap all buffers of deleted stream |
| 182 | // in case the configuration call succeeds and HAL |
| 183 | // is able to release the corresponding resources too. |
| 184 | cleanupBuffersLocked(id); |
| 185 | it = mStreamMap.erase(it); |
| 186 | } else { |
| 187 | ++it; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Track video streams |
| 192 | mVideoStreamIds.clear(); |
| 193 | for (const auto& stream : requestedConfiguration.streams) { |
| 194 | if (stream.v3_2.streamType == StreamType::OUTPUT && |
| 195 | stream.v3_2.usage & |
| 196 | graphics::common::V1_0::BufferUsage::VIDEO_ENCODER) { |
| 197 | mVideoStreamIds.push_back(stream.v3_2.id); |
| 198 | } |
| 199 | } |
| 200 | mResultBatcher.setBatchedStreams(mVideoStreamIds); |
| 201 | } |
| 202 | |
| 203 | Return<void> CameraDeviceSession::processCaptureRequest_3_4( |
| 204 | const hidl_vec<CaptureRequest>& requests, |
| 205 | const hidl_vec<V3_2::BufferCache>& cachesToRemove, |
| 206 | ICameraDeviceSession::processCaptureRequest_cb _hidl_cb) { |
| 207 | updateBufferCaches(cachesToRemove); |
| 208 | |
| 209 | uint32_t numRequestProcessed = 0; |
| 210 | Status s = Status::OK; |
| 211 | for (size_t i = 0; i < requests.size(); i++, numRequestProcessed++) { |
| 212 | s = processOneCaptureRequest_3_4(requests[i]); |
| 213 | if (s != Status::OK) { |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (s == Status::OK && requests.size() > 1) { |
| 219 | mResultBatcher.registerBatch(requests); |
| 220 | } |
| 221 | |
| 222 | _hidl_cb(s, numRequestProcessed); |
| 223 | return Void(); |
| 224 | } |
| 225 | |
| 226 | Status CameraDeviceSession::processOneCaptureRequest_3_4(const CaptureRequest& request) { |
| 227 | Status status = initStatus(); |
| 228 | if (status != Status::OK) { |
| 229 | ALOGE("%s: camera init failed or disconnected", __FUNCTION__); |
| 230 | return status; |
| 231 | } |
| 232 | |
| 233 | camera3_capture_request_t halRequest; |
| 234 | halRequest.frame_number = request.frameNumber; |
| 235 | |
| 236 | bool converted = true; |
| 237 | V3_2::CameraMetadata settingsFmq; // settings from FMQ |
| 238 | if (request.fmqSettingsSize > 0) { |
| 239 | // non-blocking read; client must write metadata before calling |
| 240 | // processOneCaptureRequest |
| 241 | settingsFmq.resize(request.fmqSettingsSize); |
| 242 | bool read = mRequestMetadataQueue->read(settingsFmq.data(), request.fmqSettingsSize); |
| 243 | if (read) { |
| 244 | converted = V3_2::implementation::convertFromHidl(settingsFmq, &halRequest.settings); |
| 245 | } else { |
| 246 | ALOGE("%s: capture request settings metadata couldn't be read from fmq!", __FUNCTION__); |
| 247 | converted = false; |
| 248 | } |
| 249 | } else { |
| 250 | converted = V3_2::implementation::convertFromHidl(request.settings, &halRequest.settings); |
| 251 | } |
| 252 | |
| 253 | if (!converted) { |
| 254 | ALOGE("%s: capture request settings metadata is corrupt!", __FUNCTION__); |
| 255 | return Status::ILLEGAL_ARGUMENT; |
| 256 | } |
| 257 | |
| 258 | if (mFirstRequest && halRequest.settings == nullptr) { |
| 259 | ALOGE("%s: capture request settings must not be null for first request!", |
| 260 | __FUNCTION__); |
| 261 | return Status::ILLEGAL_ARGUMENT; |
| 262 | } |
| 263 | |
| 264 | hidl_vec<buffer_handle_t*> allBufPtrs; |
| 265 | hidl_vec<int> allFences; |
| 266 | bool hasInputBuf = (request.inputBuffer.streamId != -1 && |
| 267 | request.inputBuffer.bufferId != 0); |
| 268 | size_t numOutputBufs = request.outputBuffers.size(); |
| 269 | size_t numBufs = numOutputBufs + (hasInputBuf ? 1 : 0); |
| 270 | |
| 271 | if (numOutputBufs == 0) { |
| 272 | ALOGE("%s: capture request must have at least one output buffer!", __FUNCTION__); |
| 273 | return Status::ILLEGAL_ARGUMENT; |
| 274 | } |
| 275 | |
| 276 | status = importRequest(request, allBufPtrs, allFences); |
| 277 | if (status != Status::OK) { |
| 278 | return status; |
| 279 | } |
| 280 | |
| 281 | hidl_vec<camera3_stream_buffer_t> outHalBufs; |
| 282 | outHalBufs.resize(numOutputBufs); |
| 283 | bool aeCancelTriggerNeeded = false; |
| 284 | ::android::hardware::camera::common::V1_0::helper::CameraMetadata settingsOverride; |
| 285 | { |
| 286 | Mutex::Autolock _l(mInflightLock); |
| 287 | if (hasInputBuf) { |
| 288 | auto streamId = request.inputBuffer.streamId; |
| 289 | auto key = std::make_pair(request.inputBuffer.streamId, request.frameNumber); |
| 290 | auto& bufCache = mInflightBuffers[key] = camera3_stream_buffer_t{}; |
| 291 | convertFromHidl( |
| 292 | allBufPtrs[numOutputBufs], request.inputBuffer.status, |
| 293 | &mStreamMap[request.inputBuffer.streamId], allFences[numOutputBufs], |
| 294 | &bufCache); |
| 295 | bufCache.stream->physical_camera_id = mPhysicalCameraIdMap[streamId].c_str(); |
| 296 | halRequest.input_buffer = &bufCache; |
| 297 | } else { |
| 298 | halRequest.input_buffer = nullptr; |
| 299 | } |
| 300 | |
| 301 | halRequest.num_output_buffers = numOutputBufs; |
| 302 | for (size_t i = 0; i < numOutputBufs; i++) { |
| 303 | auto streamId = request.outputBuffers[i].streamId; |
| 304 | auto key = std::make_pair(streamId, request.frameNumber); |
| 305 | auto& bufCache = mInflightBuffers[key] = camera3_stream_buffer_t{}; |
| 306 | convertFromHidl( |
| 307 | allBufPtrs[i], request.outputBuffers[i].status, |
| 308 | &mStreamMap[streamId], allFences[i], |
| 309 | &bufCache); |
| 310 | bufCache.stream->physical_camera_id = mPhysicalCameraIdMap[streamId].c_str(); |
| 311 | outHalBufs[i] = bufCache; |
| 312 | } |
| 313 | halRequest.output_buffers = outHalBufs.data(); |
| 314 | |
| 315 | AETriggerCancelOverride triggerOverride; |
| 316 | aeCancelTriggerNeeded = handleAePrecaptureCancelRequestLocked( |
| 317 | halRequest, &settingsOverride /*out*/, &triggerOverride/*out*/); |
| 318 | if (aeCancelTriggerNeeded) { |
| 319 | mInflightAETriggerOverrides[halRequest.frame_number] = |
| 320 | triggerOverride; |
| 321 | halRequest.settings = settingsOverride.getAndLock(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | ATRACE_ASYNC_BEGIN("frame capture", request.frameNumber); |
| 326 | ATRACE_BEGIN("camera3->process_capture_request"); |
| 327 | status_t ret = mDevice->ops->process_capture_request(mDevice, &halRequest); |
| 328 | ATRACE_END(); |
| 329 | if (aeCancelTriggerNeeded) { |
| 330 | settingsOverride.unlock(halRequest.settings); |
| 331 | } |
| 332 | if (ret != OK) { |
| 333 | Mutex::Autolock _l(mInflightLock); |
| 334 | ALOGE("%s: HAL process_capture_request call failed!", __FUNCTION__); |
| 335 | |
| 336 | cleanupInflightFences(allFences, numBufs); |
| 337 | if (hasInputBuf) { |
| 338 | auto key = std::make_pair(request.inputBuffer.streamId, request.frameNumber); |
| 339 | mInflightBuffers.erase(key); |
| 340 | } |
| 341 | for (size_t i = 0; i < numOutputBufs; i++) { |
| 342 | auto key = std::make_pair(request.outputBuffers[i].streamId, request.frameNumber); |
| 343 | mInflightBuffers.erase(key); |
| 344 | } |
| 345 | if (aeCancelTriggerNeeded) { |
| 346 | mInflightAETriggerOverrides.erase(request.frameNumber); |
| 347 | } |
| 348 | return Status::INTERNAL_ERROR; |
| 349 | } |
| 350 | |
| 351 | mFirstRequest = false; |
| 352 | return Status::OK; |
| 353 | } |
| 354 | |
Emilian Peev | e18057b | 2017-11-13 16:03:44 +0000 | [diff] [blame] | 355 | } // namespace implementation |
| 356 | } // namespace V3_4 |
| 357 | } // namespace device |
| 358 | } // namespace camera |
| 359 | } // namespace hardware |
| 360 | } // namespace android |