Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "Camera3DeviceInjectionMethods" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/Trace.h> |
| 23 | |
| 24 | #include "common/CameraProviderManager.h" |
| 25 | #include "device3/Camera3Device.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | using hardware::camera::device::V3_2::ICameraDeviceSession; |
| 30 | |
| 31 | Camera3Device::Camera3DeviceInjectionMethods::Camera3DeviceInjectionMethods( |
| 32 | wp<Camera3Device> parent) |
| 33 | : mParent(parent) { |
| 34 | ALOGV("%s: Created injection camera methods", __FUNCTION__); |
| 35 | } |
| 36 | |
| 37 | Camera3Device::Camera3DeviceInjectionMethods::~Camera3DeviceInjectionMethods() { |
| 38 | ALOGV("%s: Removed injection camera methods", __FUNCTION__); |
| 39 | injectionDisconnectImpl(); |
| 40 | } |
| 41 | |
| 42 | status_t Camera3Device::Camera3DeviceInjectionMethods::injectionInitialize( |
| 43 | const String8& injectedCamId, sp<CameraProviderManager> manager, |
| 44 | const sp<android::hardware::camera::device::V3_2::ICameraDeviceCallback>& |
| 45 | callback) { |
| 46 | ATRACE_CALL(); |
| 47 | Mutex::Autolock lock(mInjectionLock); |
| 48 | |
| 49 | if (manager == nullptr) { |
| 50 | ALOGE("%s: manager does not exist!", __FUNCTION__); |
| 51 | return INVALID_OPERATION; |
| 52 | } |
| 53 | |
| 54 | sp<Camera3Device> parent = mParent.promote(); |
| 55 | if (parent == nullptr) { |
| 56 | ALOGE("%s: parent does not exist!", __FUNCTION__); |
| 57 | return INVALID_OPERATION; |
| 58 | } |
| 59 | |
| 60 | mInjectedCamId = injectedCamId; |
| 61 | sp<ICameraDeviceSession> session; |
| 62 | ATRACE_BEGIN("Injection CameraHal::openSession"); |
| 63 | status_t res = manager->openSession(injectedCamId.string(), callback, |
| 64 | /*out*/ &session); |
| 65 | ATRACE_END(); |
| 66 | if (res != OK) { |
| 67 | ALOGE("Injection camera could not open camera session: %s (%d)", |
| 68 | strerror(-res), res); |
| 69 | return res; |
| 70 | } |
| 71 | |
| 72 | std::shared_ptr<RequestMetadataQueue> queue; |
| 73 | auto requestQueueRet = |
| 74 | session->getCaptureRequestMetadataQueue([&queue](const auto& descriptor) { |
| 75 | queue = std::make_shared<RequestMetadataQueue>(descriptor); |
| 76 | if (!queue->isValid() || queue->availableToWrite() <= 0) { |
| 77 | ALOGE("Injection camera HAL returns empty request metadata fmq, not " |
| 78 | "use it"); |
| 79 | queue = nullptr; |
| 80 | // don't use the queue onwards. |
| 81 | } |
| 82 | }); |
| 83 | if (!requestQueueRet.isOk()) { |
| 84 | ALOGE("Injection camera transaction error when getting request metadata fmq: " |
| 85 | "%s, not use it", requestQueueRet.description().c_str()); |
| 86 | return DEAD_OBJECT; |
| 87 | } |
| 88 | |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 89 | std::unique_ptr<ResultMetadataQueue>& resQueue = mInjectionResultMetadataQueue; |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 90 | auto resultQueueRet = session->getCaptureResultMetadataQueue( |
| 91 | [&resQueue](const auto& descriptor) { |
| 92 | resQueue = std::make_unique<ResultMetadataQueue>(descriptor); |
| 93 | if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) { |
| 94 | ALOGE("Injection camera HAL returns empty result metadata fmq, not use " |
| 95 | "it"); |
| 96 | resQueue = nullptr; |
| 97 | // Don't use the resQueue onwards. |
| 98 | } |
| 99 | }); |
| 100 | if (!resultQueueRet.isOk()) { |
| 101 | ALOGE("Injection camera transaction error when getting result metadata queue " |
| 102 | "from camera session: %s", resultQueueRet.description().c_str()); |
| 103 | return DEAD_OBJECT; |
| 104 | } |
| 105 | IF_ALOGV() { |
| 106 | session->interfaceChain( |
| 107 | [](::android::hardware::hidl_vec<::android::hardware::hidl_string> |
| 108 | interfaceChain) { |
| 109 | ALOGV("Injection camera session interface chain:"); |
| 110 | for (const auto& iface : interfaceChain) { |
| 111 | ALOGV(" %s", iface.c_str()); |
| 112 | } |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | ALOGV("%s: Injection camera interface = new HalInterface()", __FUNCTION__); |
| 117 | mInjectedCamHalInterface = |
| 118 | new HalInterface(session, queue, parent->mUseHalBufManager, |
| 119 | parent->mSupportOfflineProcessing); |
| 120 | if (mInjectedCamHalInterface == nullptr) { |
| 121 | ALOGE("%s: mInjectedCamHalInterface does not exist!", __FUNCTION__); |
| 122 | return DEAD_OBJECT; |
| 123 | } |
| 124 | |
| 125 | return OK; |
| 126 | } |
| 127 | |
| 128 | status_t Camera3Device::Camera3DeviceInjectionMethods::injectCamera( |
| 129 | camera3::camera_stream_configuration& injectionConfig, |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 130 | const std::vector<uint32_t>& injectionBufferSizes) { |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 131 | status_t res = NO_ERROR; |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 132 | |
| 133 | if (mInjectedCamHalInterface == nullptr) { |
| 134 | ALOGE("%s: mInjectedCamHalInterface does not exist!", __FUNCTION__); |
| 135 | return DEAD_OBJECT; |
| 136 | } |
| 137 | |
| 138 | sp<Camera3Device> parent = mParent.promote(); |
| 139 | if (parent == nullptr) { |
| 140 | ALOGE("%s: parent does not exist!", __FUNCTION__); |
| 141 | return INVALID_OPERATION; |
| 142 | } |
| 143 | |
| 144 | nsecs_t maxExpectedDuration = parent->getExpectedInFlightDuration(); |
| 145 | bool wasActive = false; |
| 146 | if (parent->mStatus == STATUS_ACTIVE) { |
| 147 | ALOGV("%s: Let the device be IDLE and the request thread is paused", |
| 148 | __FUNCTION__); |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 149 | res = parent->internalPauseAndWaitLocked(maxExpectedDuration); |
| 150 | if (res != OK) { |
| 151 | ALOGE("%s: Can't pause captures to inject camera!", __FUNCTION__); |
| 152 | return res; |
| 153 | } |
| 154 | wasActive = true; |
| 155 | } |
| 156 | |
| 157 | ALOGV("%s: Injection camera: replaceHalInterface", __FUNCTION__); |
| 158 | res = replaceHalInterface(mInjectedCamHalInterface, true); |
| 159 | if (res != OK) { |
| 160 | ALOGE("%s: Failed to replace the new HalInterface!", __FUNCTION__); |
| 161 | injectionDisconnectImpl(); |
| 162 | return res; |
| 163 | } |
| 164 | |
| 165 | res = parent->mRequestThread->setHalInterface(mInjectedCamHalInterface); |
| 166 | if (res != OK) { |
| 167 | ALOGE("%s: Failed to set new HalInterface in RequestThread!", __FUNCTION__); |
| 168 | replaceHalInterface(mBackupHalInterface, false); |
| 169 | injectionDisconnectImpl(); |
| 170 | return res; |
| 171 | } |
| 172 | |
| 173 | parent->mNeedConfig = true; |
| 174 | res = injectionConfigureStreams(injectionConfig, injectionBufferSizes); |
| 175 | parent->mNeedConfig = false; |
| 176 | if (res != OK) { |
| 177 | ALOGE("Can't injectionConfigureStreams device for streams: %d: %s " |
| 178 | "(%d)", parent->mNextStreamId, strerror(-res), res); |
| 179 | replaceHalInterface(mBackupHalInterface, false); |
| 180 | injectionDisconnectImpl(); |
| 181 | return res; |
| 182 | } |
| 183 | |
| 184 | if (wasActive) { |
| 185 | ALOGV("%s: Restarting activity to inject camera", __FUNCTION__); |
| 186 | // Reuse current operating mode and session parameters for new stream |
| 187 | // config. |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 188 | parent->internalResumeLocked(); |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return OK; |
| 192 | } |
| 193 | |
| 194 | status_t Camera3Device::Camera3DeviceInjectionMethods::stopInjection() { |
| 195 | status_t res = NO_ERROR; |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 196 | mIsStreamConfigCompleteButNotInjected = false; |
| 197 | if (mInjectionConfig.streams != nullptr) { |
| 198 | delete [] mInjectionConfig.streams; |
| 199 | mInjectionConfig.streams = nullptr; |
| 200 | } |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 201 | |
| 202 | sp<Camera3Device> parent = mParent.promote(); |
| 203 | if (parent == nullptr) { |
| 204 | ALOGE("%s: parent does not exist!", __FUNCTION__); |
| 205 | return DEAD_OBJECT; |
| 206 | } |
| 207 | |
| 208 | nsecs_t maxExpectedDuration = parent->getExpectedInFlightDuration(); |
| 209 | bool wasActive = false; |
| 210 | if (parent->mStatus == STATUS_ACTIVE) { |
| 211 | ALOGV("%s: Let the device be IDLE and the request thread is paused", |
| 212 | __FUNCTION__); |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 213 | res = parent->internalPauseAndWaitLocked(maxExpectedDuration); |
| 214 | if (res != OK) { |
| 215 | ALOGE("%s: Can't pause captures to stop injection!", __FUNCTION__); |
| 216 | return res; |
| 217 | } |
| 218 | wasActive = true; |
| 219 | } |
| 220 | |
| 221 | res = replaceHalInterface(mBackupHalInterface, false); |
| 222 | if (res != OK) { |
| 223 | ALOGE("%s: Failed to restore the backup HalInterface!", __FUNCTION__); |
| 224 | injectionDisconnectImpl(); |
| 225 | return res; |
| 226 | } |
| 227 | injectionDisconnectImpl(); |
| 228 | |
| 229 | if (wasActive) { |
| 230 | ALOGV("%s: Restarting activity to stop injection", __FUNCTION__); |
| 231 | // Reuse current operating mode and session parameters for new stream |
| 232 | // config. |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 233 | parent->internalResumeLocked(); |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return OK; |
| 237 | } |
| 238 | |
| 239 | bool Camera3Device::Camera3DeviceInjectionMethods::isInjecting() { |
| 240 | if (mInjectedCamHalInterface == nullptr) { |
| 241 | return false; |
| 242 | } else { |
| 243 | return true; |
| 244 | } |
| 245 | } |
| 246 | |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 247 | bool Camera3Device::Camera3DeviceInjectionMethods::isStreamConfigCompleteButNotInjected() { |
| 248 | return mIsStreamConfigCompleteButNotInjected; |
| 249 | } |
| 250 | |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 251 | const String8& Camera3Device::Camera3DeviceInjectionMethods::getInjectedCamId() |
| 252 | const { |
| 253 | return mInjectedCamId; |
| 254 | } |
| 255 | |
| 256 | void Camera3Device::Camera3DeviceInjectionMethods::getInjectionConfig( |
| 257 | /*out*/ camera3::camera_stream_configuration* injectionConfig, |
| 258 | /*out*/ std::vector<uint32_t>* injectionBufferSizes) { |
| 259 | if (injectionConfig == nullptr || injectionBufferSizes == nullptr) { |
| 260 | ALOGE("%s: Injection configuration arguments must not be null!", __FUNCTION__); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | *injectionConfig = mInjectionConfig; |
| 265 | *injectionBufferSizes = mInjectionBufferSizes; |
| 266 | } |
| 267 | |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 268 | void Camera3Device::Camera3DeviceInjectionMethods::storeInjectionConfig( |
| 269 | const camera3::camera_stream_configuration& injectionConfig, |
| 270 | const std::vector<uint32_t>& injectionBufferSizes) { |
| 271 | mIsStreamConfigCompleteButNotInjected = true; |
| 272 | if (mInjectionConfig.streams != nullptr) { |
| 273 | delete [] mInjectionConfig.streams; |
| 274 | mInjectionConfig.streams = nullptr; |
| 275 | } |
| 276 | mInjectionConfig = injectionConfig; |
| 277 | mInjectionConfig.streams = |
| 278 | (android::camera3::camera_stream_t **) new camera_stream_t*[injectionConfig.num_streams]; |
| 279 | for (size_t i = 0; i < injectionConfig.num_streams; i++) { |
| 280 | mInjectionConfig.streams[i] = injectionConfig.streams[i]; |
| 281 | } |
| 282 | mInjectionBufferSizes = injectionBufferSizes; |
| 283 | } |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 284 | |
| 285 | status_t Camera3Device::Camera3DeviceInjectionMethods::injectionConfigureStreams( |
| 286 | camera3::camera_stream_configuration& injectionConfig, |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 287 | const std::vector<uint32_t>& injectionBufferSizes) { |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 288 | ATRACE_CALL(); |
| 289 | status_t res = NO_ERROR; |
| 290 | |
| 291 | sp<Camera3Device> parent = mParent.promote(); |
| 292 | if (parent == nullptr) { |
| 293 | ALOGE("%s: parent does not exist!", __FUNCTION__); |
| 294 | return INVALID_OPERATION; |
| 295 | } |
| 296 | |
| 297 | if (parent->mOperatingMode < 0) { |
| 298 | ALOGE("Invalid operating mode: %d", parent->mOperatingMode); |
| 299 | return BAD_VALUE; |
| 300 | } |
| 301 | |
| 302 | // Start configuring the streams |
| 303 | ALOGV("%s: Injection camera %s: Starting stream configuration", __FUNCTION__, |
| 304 | mInjectedCamId.string()); |
| 305 | |
| 306 | parent->mPreparerThread->pause(); |
| 307 | |
| 308 | // Do the HAL configuration; will potentially touch stream |
| 309 | // max_buffers, usage, and priv fields, as well as data_space and format |
| 310 | // fields for IMPLEMENTATION_DEFINED formats. |
| 311 | |
| 312 | const camera_metadata_t* sessionBuffer = parent->mSessionParams.getAndLock(); |
| 313 | res = mInjectedCamHalInterface->configureInjectedStreams( |
| 314 | sessionBuffer, &injectionConfig, injectionBufferSizes, |
| 315 | parent->mDeviceInfo); |
| 316 | parent->mSessionParams.unlock(sessionBuffer); |
| 317 | |
| 318 | if (res == BAD_VALUE) { |
| 319 | // HAL rejected this set of streams as unsupported, clean up config |
| 320 | // attempt and return to unconfigured state |
| 321 | ALOGE("Set of requested outputs not supported by HAL"); |
| 322 | parent->cancelStreamsConfigurationLocked(); |
| 323 | return BAD_VALUE; |
| 324 | } else if (res != OK) { |
| 325 | // Some other kind of error from configure_streams - this is not |
| 326 | // expected |
| 327 | ALOGE("Unable to configure streams with HAL: %s (%d)", strerror(-res), |
| 328 | res); |
| 329 | return res; |
| 330 | } |
| 331 | |
| 332 | for (size_t i = 0; i < parent->mOutputStreams.size(); i++) { |
| 333 | sp<camera3::Camera3OutputStreamInterface> outputStream = |
| 334 | parent->mOutputStreams[i]; |
| 335 | mInjectedCamHalInterface->onStreamReConfigured(outputStream->getId()); |
| 336 | } |
| 337 | |
| 338 | // Request thread needs to know to avoid using repeat-last-settings protocol |
| 339 | // across configure_streams() calls |
| 340 | parent->mRequestThread->configurationComplete( |
| 341 | parent->mIsConstrainedHighSpeedConfiguration, parent->mSessionParams, |
| 342 | parent->mGroupIdPhysicalCameraMap); |
| 343 | |
| 344 | parent->internalUpdateStatusLocked(STATUS_CONFIGURED); |
| 345 | |
| 346 | ALOGV("%s: Injection camera %s: Stream configuration complete", __FUNCTION__, |
| 347 | mInjectedCamId.string()); |
| 348 | |
| 349 | auto rc = parent->mPreparerThread->resume(); |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 350 | if (rc != OK) { |
| 351 | ALOGE("%s: Injection camera %s: Preparer thread failed to resume!", |
| 352 | __FUNCTION__, mInjectedCamId.string()); |
| 353 | return rc; |
| 354 | } |
| 355 | |
| 356 | return OK; |
| 357 | } |
| 358 | |
| 359 | void Camera3Device::Camera3DeviceInjectionMethods::injectionDisconnectImpl() { |
| 360 | ATRACE_CALL(); |
| 361 | ALOGI("%s: Injection camera disconnect", __FUNCTION__); |
| 362 | |
| 363 | mBackupHalInterface = nullptr; |
| 364 | HalInterface* interface = nullptr; |
| 365 | { |
| 366 | Mutex::Autolock lock(mInjectionLock); |
| 367 | if (mInjectedCamHalInterface != nullptr) { |
| 368 | interface = mInjectedCamHalInterface.get(); |
| 369 | // Call close without internal mutex held, as the HAL close may need |
| 370 | // to wait on assorted callbacks,etc, to complete before it can |
| 371 | // return. |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (interface != nullptr) { |
| 376 | interface->close(); |
| 377 | } |
| 378 | |
| 379 | { |
| 380 | Mutex::Autolock lock(mInjectionLock); |
| 381 | if (mInjectedCamHalInterface != nullptr) { |
| 382 | mInjectedCamHalInterface->clear(); |
| 383 | mInjectedCamHalInterface = nullptr; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | status_t Camera3Device::Camera3DeviceInjectionMethods::replaceHalInterface( |
| 389 | sp<HalInterface> newHalInterface, bool keepBackup) { |
| 390 | Mutex::Autolock lock(mInjectionLock); |
| 391 | if (newHalInterface.get() == nullptr) { |
| 392 | ALOGE("%s: The newHalInterface does not exist, to stop replacing.", |
| 393 | __FUNCTION__); |
| 394 | return DEAD_OBJECT; |
| 395 | } |
| 396 | |
| 397 | sp<Camera3Device> parent = mParent.promote(); |
| 398 | if (parent == nullptr) { |
| 399 | ALOGE("%s: parent does not exist!", __FUNCTION__); |
| 400 | return INVALID_OPERATION; |
| 401 | } |
| 402 | |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 403 | if (keepBackup) { |
| 404 | if (mBackupHalInterface == nullptr) { |
| 405 | mBackupHalInterface = parent->mInterface; |
| 406 | } |
| 407 | if (mBackupResultMetadataQueue == nullptr) { |
| 408 | mBackupResultMetadataQueue = std::move(parent->mResultMetadataQueue); |
| 409 | parent->mResultMetadataQueue = std::move(mInjectionResultMetadataQueue); |
| 410 | } |
| 411 | } else { |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 412 | mBackupHalInterface = nullptr; |
Cliff Wu | 3b26818 | 2021-07-06 15:44:43 +0800 | [diff] [blame] | 413 | parent->mResultMetadataQueue = std::move(mBackupResultMetadataQueue); |
| 414 | mBackupResultMetadataQueue = nullptr; |
Cliff Wu | c2ad9c8 | 2021-04-21 00:58:58 +0800 | [diff] [blame] | 415 | } |
| 416 | parent->mInterface = newHalInterface; |
| 417 | |
| 418 | return OK; |
| 419 | } |
| 420 | |
| 421 | }; // namespace android |