Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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-Device" |
| 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 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 28 | // Convenience macro for transient errors |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 29 | #define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \ |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 30 | ##__VA_ARGS__) |
| 31 | |
| 32 | // Convenience macros for transitioning to the error state |
| 33 | #define SET_ERR(fmt, ...) setErrorState( \ |
| 34 | "%s: " fmt, __FUNCTION__, \ |
| 35 | ##__VA_ARGS__) |
| 36 | #define SET_ERR_L(fmt, ...) setErrorStateLocked( \ |
| 37 | "%s: " fmt, __FUNCTION__, \ |
| 38 | ##__VA_ARGS__) |
| 39 | |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 40 | #include <inttypes.h> |
| 41 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 42 | #include <utils/Log.h> |
| 43 | #include <utils/Trace.h> |
| 44 | #include <utils/Timers.h> |
Zhijun He | 90f7c37 | 2016-08-16 16:19:43 -0700 | [diff] [blame] | 45 | #include <cutils/properties.h> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 46 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 47 | #include <android/hardware/camera2/ICameraDeviceUser.h> |
| 48 | |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 49 | #include "utils/CameraTraces.h" |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 50 | #include "mediautils/SchedulingPolicyService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 51 | #include "device3/Camera3Device.h" |
| 52 | #include "device3/Camera3OutputStream.h" |
| 53 | #include "device3/Camera3InputStream.h" |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 54 | #include "device3/Camera3DummyStream.h" |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 55 | #include "device3/Camera3SharedOutputStream.h" |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 56 | #include "CameraService.h" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 57 | |
| 58 | using namespace android::camera3; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 59 | using namespace android::hardware::camera; |
| 60 | using namespace android::hardware::camera::device::V3_2; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 61 | |
| 62 | namespace android { |
| 63 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 64 | Camera3Device::Camera3Device(const String8 &id): |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 65 | mId(id), |
Eino-Ville Talvala | bbbbe84 | 2017-02-28 17:50:56 -0800 | [diff] [blame] | 66 | mOperatingMode(NO_MODE), |
Eino-Ville Talvala | 9a17941 | 2015-06-09 13:15:16 -0700 | [diff] [blame] | 67 | mIsConstrainedHighSpeedConfiguration(false), |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 68 | mStatus(STATUS_UNINITIALIZED), |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 69 | mStatusWaiters(0), |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 70 | mUsePartialResult(false), |
| 71 | mNumPartialResults(1), |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 72 | mTimestampOffset(0), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 73 | mNextResultFrameNumber(0), |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 74 | mNextReprocessResultFrameNumber(0), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 75 | mNextShutterFrameNumber(0), |
Chien-Yu Chen | 3df11ce | 2015-09-30 14:13:30 -0700 | [diff] [blame] | 76 | mNextReprocessShutterFrameNumber(0), |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 77 | mListener(NULL), |
| 78 | mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID) |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 79 | { |
| 80 | ATRACE_CALL(); |
| 81 | camera3_callback_ops::notify = &sNotify; |
| 82 | camera3_callback_ops::process_capture_result = &sProcessCaptureResult; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 83 | ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | Camera3Device::~Camera3Device() |
| 87 | { |
| 88 | ATRACE_CALL(); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 89 | ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 90 | disconnect(); |
| 91 | } |
| 92 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 93 | const String8& Camera3Device::getId() const { |
Igor Murashkin | 7138105 | 2013-03-04 14:53:08 -0800 | [diff] [blame] | 94 | return mId; |
| 95 | } |
| 96 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 97 | status_t Camera3Device::initialize(sp<CameraProviderManager> manager) { |
| 98 | ATRACE_CALL(); |
| 99 | Mutex::Autolock il(mInterfaceLock); |
| 100 | Mutex::Autolock l(mLock); |
| 101 | |
| 102 | ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string()); |
| 103 | if (mStatus != STATUS_UNINITIALIZED) { |
| 104 | CLOGE("Already initialized!"); |
| 105 | return INVALID_OPERATION; |
| 106 | } |
| 107 | if (manager == nullptr) return INVALID_OPERATION; |
| 108 | |
| 109 | sp<ICameraDeviceSession> session; |
| 110 | ATRACE_BEGIN("CameraHal::openSession"); |
Steven Moreland | 5ff9c91 | 2017-03-09 23:13:00 -0800 | [diff] [blame] | 111 | status_t res = manager->openSession(mId.string(), this, |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 112 | /*out*/ &session); |
| 113 | ATRACE_END(); |
| 114 | if (res != OK) { |
| 115 | SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res); |
| 116 | return res; |
| 117 | } |
| 118 | |
Steven Moreland | 5ff9c91 | 2017-03-09 23:13:00 -0800 | [diff] [blame] | 119 | res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 120 | if (res != OK) { |
| 121 | SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res); |
| 122 | session->close(); |
| 123 | return res; |
| 124 | } |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 125 | |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 126 | std::shared_ptr<RequestMetadataQueue> queue; |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 127 | auto requestQueueRet = session->getCaptureRequestMetadataQueue( |
| 128 | [&queue](const auto& descriptor) { |
| 129 | queue = std::make_shared<RequestMetadataQueue>(descriptor); |
| 130 | if (!queue->isValid() || queue->availableToWrite() <= 0) { |
| 131 | ALOGE("HAL returns empty request metadata fmq, not use it"); |
| 132 | queue = nullptr; |
| 133 | // don't use the queue onwards. |
| 134 | } |
| 135 | }); |
| 136 | if (!requestQueueRet.isOk()) { |
| 137 | ALOGE("Transaction error when getting request metadata fmq: %s, not use it", |
| 138 | requestQueueRet.description().c_str()); |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 139 | return DEAD_OBJECT; |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 140 | } |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 141 | auto resultQueueRet = session->getCaptureResultMetadataQueue( |
| 142 | [&queue = mResultMetadataQueue](const auto& descriptor) { |
| 143 | queue = std::make_unique<ResultMetadataQueue>(descriptor); |
| 144 | if (!queue->isValid() || queue->availableToWrite() <= 0) { |
| 145 | ALOGE("HAL returns empty result metadata fmq, not use it"); |
| 146 | queue = nullptr; |
| 147 | // Don't use the queue onwards. |
| 148 | } |
| 149 | }); |
| 150 | if (!resultQueueRet.isOk()) { |
| 151 | ALOGE("Transaction error when getting result metadata queue from camera session: %s", |
| 152 | resultQueueRet.description().c_str()); |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 153 | return DEAD_OBJECT; |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 154 | } |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 155 | |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 156 | mInterface = std::make_unique<HalInterface>(session, queue); |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 157 | std::string providerType; |
| 158 | mVendorTagId = manager->getProviderTagIdLocked(mId.string()); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 159 | |
| 160 | return initializeCommonLocked(); |
| 161 | } |
| 162 | |
| 163 | status_t Camera3Device::initializeCommonLocked() { |
| 164 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 165 | /** Start up status tracker thread */ |
| 166 | mStatusTracker = new StatusTracker(this); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 167 | status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string()); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 168 | if (res != OK) { |
| 169 | SET_ERR_L("Unable to start status tracking thread: %s (%d)", |
| 170 | strerror(-res), res); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 171 | mInterface->close(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 172 | mStatusTracker.clear(); |
| 173 | return res; |
| 174 | } |
| 175 | |
Eino-Ville Talvala | 24b366e | 2016-07-21 12:53:07 -0700 | [diff] [blame] | 176 | /** Register in-flight map to the status tracker */ |
| 177 | mInFlightStatusId = mStatusTracker->addComponent(); |
| 178 | |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 179 | /** Create buffer manager */ |
| 180 | mBufferManager = new Camera3BufferManager(); |
| 181 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 182 | mTagMonitor.initialize(mVendorTagId); |
| 183 | |
Chien-Yu Chen | ab5135b | 2015-06-30 11:20:58 -0700 | [diff] [blame] | 184 | /** Start up request queue thread */ |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 185 | mRequestThread = new RequestThread(this, mStatusTracker, mInterface.get()); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 186 | res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string()); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 187 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 188 | SET_ERR_L("Unable to start request queue thread: %s (%d)", |
| 189 | strerror(-res), res); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 190 | mInterface->close(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 191 | mRequestThread.clear(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 192 | return res; |
| 193 | } |
| 194 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 195 | mPreparerThread = new PreparerThread(); |
| 196 | |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 197 | internalUpdateStatusLocked(STATUS_UNCONFIGURED); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 198 | mNextStreamId = 0; |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 199 | mDummyStreamId = NO_STREAM; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 200 | mNeedConfig = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 201 | mPauseStateNotify = false; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 202 | |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 203 | // Measure the clock domain offset between camera and video/hw_composer |
| 204 | camera_metadata_entry timestampSource = |
| 205 | mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE); |
| 206 | if (timestampSource.count > 0 && timestampSource.data.u8[0] == |
| 207 | ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) { |
| 208 | mTimestampOffset = getMonoToBoottimeOffset(); |
| 209 | } |
| 210 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 211 | // Will the HAL be sending in early partial result metadata? |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 212 | camera_metadata_entry partialResultsCount = |
| 213 | mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT); |
| 214 | if (partialResultsCount.count > 0) { |
| 215 | mNumPartialResults = partialResultsCount.data.i32[0]; |
| 216 | mUsePartialResult = (mNumPartialResults > 1); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 219 | camera_metadata_entry configs = |
| 220 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); |
| 221 | for (uint32_t i = 0; i < configs.count; i += 4) { |
| 222 | if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && |
| 223 | configs.data.i32[i + 3] == |
| 224 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) { |
| 225 | mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1], |
| 226 | configs.data.i32[i + 2])); |
| 227 | } |
| 228 | } |
| 229 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 230 | return OK; |
| 231 | } |
| 232 | |
| 233 | status_t Camera3Device::disconnect() { |
| 234 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 235 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 236 | |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 237 | ALOGI("%s: E", __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 238 | |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 239 | status_t res = OK; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 240 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 241 | { |
| 242 | Mutex::Autolock l(mLock); |
| 243 | if (mStatus == STATUS_UNINITIALIZED) return res; |
| 244 | |
| 245 | if (mStatus == STATUS_ACTIVE || |
| 246 | (mStatus == STATUS_ERROR && mRequestThread != NULL)) { |
| 247 | res = mRequestThread->clearRepeatingRequests(); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 248 | if (res != OK) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 249 | SET_ERR_L("Can't stop streaming"); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 250 | // Continue to close device even in case of error |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 251 | } else { |
| 252 | res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
| 253 | if (res != OK) { |
| 254 | SET_ERR_L("Timeout waiting for HAL to drain"); |
| 255 | // Continue to close device even in case of error |
| 256 | } |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 257 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 258 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 259 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 260 | if (mStatus == STATUS_ERROR) { |
| 261 | CLOGE("Shutting down in an error state"); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 262 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 263 | |
| 264 | if (mStatusTracker != NULL) { |
| 265 | mStatusTracker->requestExit(); |
| 266 | } |
| 267 | |
| 268 | if (mRequestThread != NULL) { |
| 269 | mRequestThread->requestExit(); |
| 270 | } |
| 271 | |
| 272 | mOutputStreams.clear(); |
| 273 | mInputStream.clear(); |
| 274 | } |
| 275 | |
| 276 | // Joining done without holding mLock, otherwise deadlocks may ensue |
| 277 | // as the threads try to access parent state |
| 278 | if (mRequestThread != NULL && mStatus != STATUS_ERROR) { |
| 279 | // HAL may be in a bad state, so waiting for request thread |
| 280 | // (which may be stuck in the HAL processCaptureRequest call) |
| 281 | // could be dangerous. |
| 282 | mRequestThread->join(); |
| 283 | } |
| 284 | |
| 285 | if (mStatusTracker != NULL) { |
| 286 | mStatusTracker->join(); |
| 287 | } |
| 288 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 289 | HalInterface* interface; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 290 | { |
| 291 | Mutex::Autolock l(mLock); |
| 292 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 293 | mRequestThread.clear(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 294 | mStatusTracker.clear(); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 295 | mBufferManager.clear(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 296 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 297 | interface = mInterface.get(); |
Eino-Ville Talvala | efff1c4 | 2015-08-28 16:27:27 -0700 | [diff] [blame] | 298 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 299 | |
Eino-Ville Talvala | efff1c4 | 2015-08-28 16:27:27 -0700 | [diff] [blame] | 300 | // Call close without internal mutex held, as the HAL close may need to |
| 301 | // wait on assorted callbacks,etc, to complete before it can return. |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 302 | interface->close(); |
Eino-Ville Talvala | efff1c4 | 2015-08-28 16:27:27 -0700 | [diff] [blame] | 303 | |
| 304 | { |
| 305 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 306 | mInterface->clear(); |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 307 | internalUpdateStatusLocked(STATUS_UNINITIALIZED); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 308 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 309 | |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 310 | ALOGI("%s: X", __FUNCTION__); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 311 | return res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 314 | // For dumping/debugging only - |
| 315 | // try to acquire a lock a few times, eventually give up to proceed with |
| 316 | // debug/dump operations |
| 317 | bool Camera3Device::tryLockSpinRightRound(Mutex& lock) { |
| 318 | bool gotLock = false; |
| 319 | for (size_t i = 0; i < kDumpLockAttempts; ++i) { |
| 320 | if (lock.tryLock() == NO_ERROR) { |
| 321 | gotLock = true; |
| 322 | break; |
| 323 | } else { |
| 324 | usleep(kDumpSleepDuration); |
| 325 | } |
| 326 | } |
| 327 | return gotLock; |
| 328 | } |
| 329 | |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 330 | Camera3Device::Size Camera3Device::getMaxJpegResolution() const { |
| 331 | int32_t maxJpegWidth = 0, maxJpegHeight = 0; |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 332 | const int STREAM_CONFIGURATION_SIZE = 4; |
| 333 | const int STREAM_FORMAT_OFFSET = 0; |
| 334 | const int STREAM_WIDTH_OFFSET = 1; |
| 335 | const int STREAM_HEIGHT_OFFSET = 2; |
| 336 | const int STREAM_IS_INPUT_OFFSET = 3; |
| 337 | camera_metadata_ro_entry_t availableStreamConfigs = |
| 338 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); |
| 339 | if (availableStreamConfigs.count == 0 || |
| 340 | availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) { |
| 341 | return Size(0, 0); |
| 342 | } |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 343 | |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 344 | // Get max jpeg size (area-wise). |
| 345 | for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) { |
| 346 | int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET]; |
| 347 | int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET]; |
| 348 | int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET]; |
| 349 | int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET]; |
| 350 | if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT |
| 351 | && format == HAL_PIXEL_FORMAT_BLOB && |
| 352 | (width * height > maxJpegWidth * maxJpegHeight)) { |
| 353 | maxJpegWidth = width; |
| 354 | maxJpegHeight = height; |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 357 | |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 358 | return Size(maxJpegWidth, maxJpegHeight); |
| 359 | } |
| 360 | |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 361 | nsecs_t Camera3Device::getMonoToBoottimeOffset() { |
| 362 | // try three times to get the clock offset, choose the one |
| 363 | // with the minimum gap in measurements. |
| 364 | const int tries = 3; |
| 365 | nsecs_t bestGap, measured; |
| 366 | for (int i = 0; i < tries; ++i) { |
| 367 | const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC); |
| 368 | const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME); |
| 369 | const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC); |
| 370 | const nsecs_t gap = tmono2 - tmono; |
| 371 | if (i == 0 || gap < bestGap) { |
| 372 | bestGap = gap; |
| 373 | measured = tbase - ((tmono + tmono2) >> 1); |
| 374 | } |
| 375 | } |
| 376 | return measured; |
| 377 | } |
| 378 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 379 | hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat( |
| 380 | int frameworkFormat) { |
| 381 | return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat; |
| 382 | } |
| 383 | |
| 384 | DataspaceFlags Camera3Device::mapToHidlDataspace( |
| 385 | android_dataspace dataSpace) { |
| 386 | return dataSpace; |
| 387 | } |
| 388 | |
Chia-I Wu | 67a0c0e | 2017-04-06 13:37:01 -0700 | [diff] [blame] | 389 | BufferUsageFlags Camera3Device::mapToConsumerUsage( |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 390 | uint32_t usage) { |
| 391 | return usage; |
| 392 | } |
| 393 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 394 | StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) { |
| 395 | switch (rotation) { |
| 396 | case CAMERA3_STREAM_ROTATION_0: |
| 397 | return StreamRotation::ROTATION_0; |
| 398 | case CAMERA3_STREAM_ROTATION_90: |
| 399 | return StreamRotation::ROTATION_90; |
| 400 | case CAMERA3_STREAM_ROTATION_180: |
| 401 | return StreamRotation::ROTATION_180; |
| 402 | case CAMERA3_STREAM_ROTATION_270: |
| 403 | return StreamRotation::ROTATION_270; |
| 404 | } |
| 405 | ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation); |
| 406 | return StreamRotation::ROTATION_0; |
| 407 | } |
| 408 | |
Eino-Ville Talvala | bbbbe84 | 2017-02-28 17:50:56 -0800 | [diff] [blame] | 409 | status_t Camera3Device::mapToStreamConfigurationMode( |
| 410 | camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) { |
| 411 | if (mode == nullptr) return BAD_VALUE; |
| 412 | if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) { |
| 413 | switch(operationMode) { |
| 414 | case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE: |
| 415 | *mode = StreamConfigurationMode::NORMAL_MODE; |
| 416 | break; |
| 417 | case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE: |
| 418 | *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE; |
| 419 | break; |
| 420 | default: |
| 421 | ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode); |
| 422 | return BAD_VALUE; |
| 423 | } |
| 424 | } else { |
| 425 | *mode = static_cast<StreamConfigurationMode>(operationMode); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 426 | } |
Eino-Ville Talvala | bbbbe84 | 2017-02-28 17:50:56 -0800 | [diff] [blame] | 427 | return OK; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) { |
| 431 | switch (status) { |
| 432 | case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK; |
| 433 | case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR; |
| 434 | } |
| 435 | return CAMERA3_BUFFER_STATUS_ERROR; |
| 436 | } |
| 437 | |
| 438 | int Camera3Device::mapToFrameworkFormat( |
| 439 | hardware::graphics::common::V1_0::PixelFormat pixelFormat) { |
| 440 | return static_cast<uint32_t>(pixelFormat); |
| 441 | } |
| 442 | |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 443 | uint32_t Camera3Device::mapConsumerToFrameworkUsage( |
Chia-I Wu | 67a0c0e | 2017-04-06 13:37:01 -0700 | [diff] [blame] | 444 | BufferUsageFlags usage) { |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 445 | return usage; |
| 446 | } |
| 447 | |
| 448 | uint32_t Camera3Device::mapProducerToFrameworkUsage( |
Chia-I Wu | 67a0c0e | 2017-04-06 13:37:01 -0700 | [diff] [blame] | 449 | BufferUsageFlags usage) { |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 450 | return usage; |
| 451 | } |
| 452 | |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 453 | ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const { |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 454 | // Get max jpeg size (area-wise). |
| 455 | Size maxJpegResolution = getMaxJpegResolution(); |
| 456 | if (maxJpegResolution.width == 0) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 457 | ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!", |
| 458 | __FUNCTION__, mId.string()); |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 459 | return BAD_VALUE; |
| 460 | } |
| 461 | |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 462 | // Get max jpeg buffer size |
| 463 | ssize_t maxJpegBufferSize = 0; |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 464 | camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE); |
| 465 | if (jpegBufMaxSize.count == 0) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 466 | ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__, |
| 467 | mId.string()); |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 468 | return BAD_VALUE; |
| 469 | } |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 470 | maxJpegBufferSize = jpegBufMaxSize.data.i32[0]; |
Yin-Chia Yeh | 0c4e56d | 2015-01-09 15:21:27 -0800 | [diff] [blame] | 471 | assert(kMinJpegBufferSize < maxJpegBufferSize); |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 472 | |
| 473 | // Calculate final jpeg buffer size for the given resolution. |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 474 | float scaleFactor = ((float) (width * height)) / |
| 475 | (maxJpegResolution.width * maxJpegResolution.height); |
Yin-Chia Yeh | 0c4e56d | 2015-01-09 15:21:27 -0800 | [diff] [blame] | 476 | ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) + |
| 477 | kMinJpegBufferSize; |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 478 | if (jpegBufferSize > maxJpegBufferSize) { |
| 479 | jpegBufferSize = maxJpegBufferSize; |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return jpegBufferSize; |
| 483 | } |
| 484 | |
Eino-Ville Talvala | 95a1d0f | 2015-08-11 15:08:53 -0700 | [diff] [blame] | 485 | ssize_t Camera3Device::getPointCloudBufferSize() const { |
| 486 | const int FLOATS_PER_POINT=4; |
| 487 | camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES); |
| 488 | if (maxPointCount.count == 0) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 489 | ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!", |
| 490 | __FUNCTION__, mId.string()); |
Eino-Ville Talvala | 95a1d0f | 2015-08-11 15:08:53 -0700 | [diff] [blame] | 491 | return BAD_VALUE; |
| 492 | } |
| 493 | ssize_t maxBytesForPointCloud = sizeof(android_depth_points) + |
| 494 | maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT; |
| 495 | return maxBytesForPointCloud; |
| 496 | } |
| 497 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 498 | ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const { |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 499 | const int PER_CONFIGURATION_SIZE = 3; |
| 500 | const int WIDTH_OFFSET = 0; |
| 501 | const int HEIGHT_OFFSET = 1; |
| 502 | const int SIZE_OFFSET = 2; |
| 503 | camera_metadata_ro_entry rawOpaqueSizes = |
| 504 | mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE); |
Aurimas Liutikas | bc57b12 | 2016-02-16 09:59:16 -0800 | [diff] [blame] | 505 | size_t count = rawOpaqueSizes.count; |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 506 | if (count == 0 || (count % PER_CONFIGURATION_SIZE)) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 507 | ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!", |
| 508 | __FUNCTION__, mId.string(), count); |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 509 | return BAD_VALUE; |
| 510 | } |
Eino-Ville Talvala | 95a1d0f | 2015-08-11 15:08:53 -0700 | [diff] [blame] | 511 | |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 512 | for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) { |
| 513 | if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] && |
| 514 | height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) { |
| 515 | return rawOpaqueSizes.data.i32[i + SIZE_OFFSET]; |
| 516 | } |
| 517 | } |
| 518 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 519 | ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!", |
| 520 | __FUNCTION__, mId.string(), width, height); |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 521 | return BAD_VALUE; |
| 522 | } |
Eino-Ville Talvala | 95a1d0f | 2015-08-11 15:08:53 -0700 | [diff] [blame] | 523 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 524 | status_t Camera3Device::dump(int fd, const Vector<String16> &args) { |
| 525 | ATRACE_CALL(); |
| 526 | (void)args; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 527 | |
| 528 | // Try to lock, but continue in case of failure (to avoid blocking in |
| 529 | // deadlocks) |
| 530 | bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock); |
| 531 | bool gotLock = tryLockSpinRightRound(mLock); |
| 532 | |
| 533 | ALOGW_IF(!gotInterfaceLock, |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 534 | "Camera %s: %s: Unable to lock interface lock, proceeding anyway", |
| 535 | mId.string(), __FUNCTION__); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 536 | ALOGW_IF(!gotLock, |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 537 | "Camera %s: %s: Unable to lock main lock, proceeding anyway", |
| 538 | mId.string(), __FUNCTION__); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 539 | |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 540 | bool dumpTemplates = false; |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 541 | |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 542 | String16 templatesOption("-t"); |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 543 | String16 monitorOption("-m"); |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 544 | int n = args.size(); |
| 545 | for (int i = 0; i < n; i++) { |
| 546 | if (args[i] == templatesOption) { |
| 547 | dumpTemplates = true; |
| 548 | } |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 549 | if (args[i] == monitorOption) { |
| 550 | if (i + 1 < n) { |
| 551 | String8 monitorTags = String8(args[i + 1]); |
| 552 | if (monitorTags == "off") { |
| 553 | mTagMonitor.disableMonitoring(); |
| 554 | } else { |
| 555 | mTagMonitor.parseTagsToMonitor(monitorTags); |
| 556 | } |
| 557 | } else { |
| 558 | mTagMonitor.disableMonitoring(); |
| 559 | } |
| 560 | } |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 563 | String8 lines; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 564 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 565 | const char *status = |
| 566 | mStatus == STATUS_ERROR ? "ERROR" : |
| 567 | mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 568 | mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" : |
| 569 | mStatus == STATUS_CONFIGURED ? "CONFIGURED" : |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 570 | mStatus == STATUS_ACTIVE ? "ACTIVE" : |
| 571 | "Unknown"; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 572 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 573 | lines.appendFormat(" Device status: %s\n", status); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 574 | if (mStatus == STATUS_ERROR) { |
| 575 | lines.appendFormat(" Error cause: %s\n", mErrorCause.string()); |
| 576 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 577 | lines.appendFormat(" Stream configuration:\n"); |
Eino-Ville Talvala | bbbbe84 | 2017-02-28 17:50:56 -0800 | [diff] [blame] | 578 | const char *mode = |
| 579 | mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" : |
| 580 | mOperatingMode == static_cast<int>( |
| 581 | StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" : |
| 582 | "CUSTOM"; |
| 583 | lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 584 | |
| 585 | if (mInputStream != NULL) { |
| 586 | write(fd, lines.string(), lines.size()); |
| 587 | mInputStream->dump(fd, args); |
| 588 | } else { |
| 589 | lines.appendFormat(" No input stream.\n"); |
| 590 | write(fd, lines.string(), lines.size()); |
| 591 | } |
| 592 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
| 593 | mOutputStreams[i]->dump(fd,args); |
| 594 | } |
| 595 | |
Zhijun He | 431503c | 2016-03-07 17:30:16 -0800 | [diff] [blame] | 596 | if (mBufferManager != NULL) { |
| 597 | lines = String8(" Camera3 Buffer Manager:\n"); |
| 598 | write(fd, lines.string(), lines.size()); |
| 599 | mBufferManager->dump(fd, args); |
| 600 | } |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 601 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 602 | lines = String8(" In-flight requests:\n"); |
| 603 | if (mInFlightMap.size() == 0) { |
| 604 | lines.append(" None\n"); |
| 605 | } else { |
| 606 | for (size_t i = 0; i < mInFlightMap.size(); i++) { |
| 607 | InFlightRequest r = mInFlightMap.valueAt(i); |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 608 | lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata" |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 609 | " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 610 | r.shutterTimestamp, r.haveResultMetadata ? "true" : "false", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 611 | r.numBuffersLeft); |
| 612 | } |
| 613 | } |
| 614 | write(fd, lines.string(), lines.size()); |
| 615 | |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 616 | if (mRequestThread != NULL) { |
| 617 | mRequestThread->dumpCaptureRequestLatency(fd, |
| 618 | " ProcessCaptureRequest latency histogram:"); |
| 619 | } |
| 620 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 621 | { |
| 622 | lines = String8(" Last request sent:\n"); |
| 623 | write(fd, lines.string(), lines.size()); |
| 624 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 625 | CameraMetadata lastRequest = getLatestRequestLocked(); |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 626 | lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6); |
| 627 | } |
| 628 | |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 629 | if (dumpTemplates) { |
| 630 | const char *templateNames[] = { |
| 631 | "TEMPLATE_PREVIEW", |
| 632 | "TEMPLATE_STILL_CAPTURE", |
| 633 | "TEMPLATE_VIDEO_RECORD", |
| 634 | "TEMPLATE_VIDEO_SNAPSHOT", |
| 635 | "TEMPLATE_ZERO_SHUTTER_LAG", |
| 636 | "TEMPLATE_MANUAL" |
| 637 | }; |
| 638 | |
| 639 | for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 640 | camera_metadata_t *templateRequest = nullptr; |
| 641 | mInterface->constructDefaultRequestSettings( |
| 642 | (camera3_request_template_t) i, &templateRequest); |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 643 | lines = String8::format(" HAL Request %s:\n", templateNames[i-1]); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 644 | if (templateRequest == nullptr) { |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 645 | lines.append(" Not supported\n"); |
| 646 | write(fd, lines.string(), lines.size()); |
| 647 | } else { |
| 648 | write(fd, lines.string(), lines.size()); |
| 649 | dump_indented_camera_metadata(templateRequest, |
| 650 | fd, /*verbosity*/2, /*indentation*/8); |
| 651 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 652 | free_camera_metadata(templateRequest); |
Eino-Ville Talvala | 7e7a62d | 2015-11-04 14:49:43 -0800 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 656 | mTagMonitor.dumpMonitoredMetadata(fd); |
| 657 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 658 | if (mInterface->valid()) { |
Eino-Ville Talvala | d00111e | 2017-01-31 11:59:12 -0800 | [diff] [blame] | 659 | lines = String8(" HAL device dump:\n"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 660 | write(fd, lines.string(), lines.size()); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 661 | mInterface->dump(fd); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 662 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 663 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 664 | if (gotLock) mLock.unlock(); |
| 665 | if (gotInterfaceLock) mInterfaceLock.unlock(); |
| 666 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 667 | return OK; |
| 668 | } |
| 669 | |
| 670 | const CameraMetadata& Camera3Device::info() const { |
| 671 | ALOGVV("%s: E", __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 672 | if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || |
| 673 | mStatus == STATUS_ERROR)) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 674 | ALOGW("%s: Access to static info %s!", __FUNCTION__, |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 675 | mStatus == STATUS_ERROR ? |
| 676 | "when in error state" : "before init"); |
| 677 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 678 | return mDeviceInfo; |
| 679 | } |
| 680 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 681 | status_t Camera3Device::checkStatusOkToCaptureLocked() { |
| 682 | switch (mStatus) { |
| 683 | case STATUS_ERROR: |
| 684 | CLOGE("Device has encountered a serious error"); |
| 685 | return INVALID_OPERATION; |
| 686 | case STATUS_UNINITIALIZED: |
| 687 | CLOGE("Device not initialized"); |
| 688 | return INVALID_OPERATION; |
| 689 | case STATUS_UNCONFIGURED: |
| 690 | case STATUS_CONFIGURED: |
| 691 | case STATUS_ACTIVE: |
| 692 | // OK |
| 693 | break; |
| 694 | default: |
| 695 | SET_ERR_L("Unexpected status: %d", mStatus); |
| 696 | return INVALID_OPERATION; |
| 697 | } |
| 698 | return OK; |
| 699 | } |
| 700 | |
| 701 | status_t Camera3Device::convertMetadataListToRequestListLocked( |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 702 | const List<const CameraMetadata> &metadataList, |
| 703 | const std::list<const SurfaceMap> &surfaceMaps, |
| 704 | bool repeating, |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 705 | RequestList *requestList) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 706 | if (requestList == NULL) { |
| 707 | CLOGE("requestList cannot be NULL."); |
| 708 | return BAD_VALUE; |
| 709 | } |
| 710 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 711 | int32_t burstId = 0; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 712 | List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin(); |
| 713 | std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin(); |
| 714 | for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end(); |
| 715 | ++metadataIt, ++surfaceMapIt) { |
| 716 | sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 717 | if (newRequest == 0) { |
| 718 | CLOGE("Can't create capture request"); |
| 719 | return BAD_VALUE; |
| 720 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 721 | |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 722 | newRequest->mRepeating = repeating; |
| 723 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 724 | // Setup burst Id and request Id |
| 725 | newRequest->mResultExtras.burstId = burstId++; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 726 | if (metadataIt->exists(ANDROID_REQUEST_ID)) { |
| 727 | if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 728 | CLOGE("RequestID entry exists; but must not be empty in metadata"); |
| 729 | return BAD_VALUE; |
| 730 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 731 | newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0]; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 732 | } else { |
| 733 | CLOGE("RequestID does not exist in metadata"); |
| 734 | return BAD_VALUE; |
| 735 | } |
| 736 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 737 | requestList->push_back(newRequest); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 738 | |
| 739 | ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 740 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 741 | if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) { |
| 742 | ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__); |
| 743 | return BAD_VALUE; |
| 744 | } |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 745 | |
| 746 | // Setup batch size if this is a high speed video recording request. |
| 747 | if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) { |
| 748 | auto firstRequest = requestList->begin(); |
| 749 | for (auto& outputStream : (*firstRequest)->mOutputStreams) { |
| 750 | if (outputStream->isVideoStream()) { |
| 751 | (*firstRequest)->mBatchSize = requestList->size(); |
| 752 | break; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 757 | return OK; |
| 758 | } |
| 759 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 760 | status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 761 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 762 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 763 | List<const CameraMetadata> requests; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 764 | std::list<const SurfaceMap> surfaceMaps; |
| 765 | convertToRequestList(requests, surfaceMaps, request); |
| 766 | |
| 767 | return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL); |
| 768 | } |
| 769 | |
| 770 | void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests, |
| 771 | std::list<const SurfaceMap>& surfaceMaps, |
| 772 | const CameraMetadata& request) { |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 773 | requests.push_back(request); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 774 | |
| 775 | SurfaceMap surfaceMap; |
| 776 | camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 777 | // With no surface list passed in, stream and surface will have 1-to-1 |
| 778 | // mapping. So the surface index is 0 for each stream in the surfaceMap. |
| 779 | for (size_t i = 0; i < streams.count; i++) { |
| 780 | surfaceMap[streams.data.i32[i]].push_back(0); |
| 781 | } |
| 782 | surfaceMaps.push_back(surfaceMap); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 785 | status_t Camera3Device::submitRequestsHelper( |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 786 | const List<const CameraMetadata> &requests, |
| 787 | const std::list<const SurfaceMap> &surfaceMaps, |
| 788 | bool repeating, |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 789 | /*out*/ |
| 790 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 791 | ATRACE_CALL(); |
| 792 | Mutex::Autolock il(mInterfaceLock); |
| 793 | Mutex::Autolock l(mLock); |
| 794 | |
| 795 | status_t res = checkStatusOkToCaptureLocked(); |
| 796 | if (res != OK) { |
| 797 | // error logged by previous call |
| 798 | return res; |
| 799 | } |
| 800 | |
| 801 | RequestList requestList; |
| 802 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 803 | res = convertMetadataListToRequestListLocked(requests, surfaceMaps, |
| 804 | repeating, /*out*/&requestList); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 805 | if (res != OK) { |
| 806 | // error logged by previous call |
| 807 | return res; |
| 808 | } |
| 809 | |
| 810 | if (repeating) { |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 811 | res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 812 | } else { |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 813 | res = mRequestThread->queueRequestList(requestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | if (res == OK) { |
| 817 | waitUntilStateThenRelock(/*active*/true, kActiveTimeout); |
| 818 | if (res != OK) { |
| 819 | SET_ERR_L("Can't transition to active in %f seconds!", |
| 820 | kActiveTimeout/1e9); |
| 821 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 822 | ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(), |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 823 | (*(requestList.begin()))->mResultExtras.requestId); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 824 | } else { |
| 825 | CLOGE("Cannot queue request. Impossible."); |
| 826 | return BAD_VALUE; |
| 827 | } |
| 828 | |
| 829 | return res; |
| 830 | } |
| 831 | |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 832 | // Only one processCaptureResult should be called at a time, so |
| 833 | // the locks won't block. The locks are present here simply to enforce this. |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 834 | hardware::Return<void> Camera3Device::processCaptureResult( |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 835 | const hardware::hidl_vec< |
| 836 | hardware::camera::device::V3_2::CaptureResult>& results) { |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 837 | |
| 838 | if (mProcessCaptureResultLock.tryLock() != OK) { |
| 839 | // This should never happen; it indicates a wrong client implementation |
| 840 | // that doesn't follow the contract. But, we can be tolerant here. |
| 841 | ALOGE("%s: callback overlapped! waiting 1s...", |
| 842 | __FUNCTION__); |
| 843 | if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) { |
| 844 | ALOGE("%s: cannot acquire lock in 1s, dropping results", |
| 845 | __FUNCTION__); |
| 846 | // really don't know what to do, so bail out. |
| 847 | return hardware::Void(); |
| 848 | } |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 849 | } |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 850 | for (const auto& result : results) { |
| 851 | processOneCaptureResultLocked(result); |
| 852 | } |
| 853 | mProcessCaptureResultLock.unlock(); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 854 | return hardware::Void(); |
| 855 | } |
| 856 | |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 857 | void Camera3Device::processOneCaptureResultLocked( |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 858 | const hardware::camera::device::V3_2::CaptureResult& result) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 859 | camera3_capture_result r; |
| 860 | status_t res; |
| 861 | r.frame_number = result.frameNumber; |
Yifan Hong | a640c5a | 2017-04-12 16:30:31 -0700 | [diff] [blame] | 862 | |
| 863 | hardware::camera::device::V3_2::CameraMetadata resultMetadata; |
| 864 | if (result.fmqResultSize > 0) { |
| 865 | resultMetadata.resize(result.fmqResultSize); |
| 866 | if (mResultMetadataQueue == nullptr) { |
| 867 | return; // logged in initialize() |
| 868 | } |
| 869 | if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) { |
| 870 | ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64, |
| 871 | __FUNCTION__, result.frameNumber, result.fmqResultSize); |
| 872 | return; |
| 873 | } |
| 874 | } else { |
| 875 | resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()), |
| 876 | result.result.size()); |
| 877 | } |
| 878 | |
| 879 | if (resultMetadata.size() != 0) { |
| 880 | r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data()); |
| 881 | size_t expected_metadata_size = resultMetadata.size(); |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 882 | if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) { |
| 883 | ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)", |
| 884 | __FUNCTION__, result.frameNumber, strerror(-res), res); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 885 | return; |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 886 | } |
| 887 | } else { |
| 888 | r.result = nullptr; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size()); |
| 892 | std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size()); |
| 893 | for (size_t i = 0; i < result.outputBuffers.size(); i++) { |
| 894 | auto& bDst = outputBuffers[i]; |
| 895 | const StreamBuffer &bSrc = result.outputBuffers[i]; |
| 896 | |
| 897 | ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId); |
Emilian Peev | be3d40c | 2017-03-27 13:03:10 +0100 | [diff] [blame] | 898 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 899 | ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d", |
| 900 | __FUNCTION__, result.frameNumber, i, bSrc.streamId); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 901 | return; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 902 | } |
| 903 | bDst.stream = mOutputStreams.valueAt(idx)->asHalStream(); |
| 904 | |
| 905 | buffer_handle_t *buffer; |
Yin-Chia Yeh | f465060 | 2017-01-10 13:13:39 -0800 | [diff] [blame] | 906 | res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 907 | if (res != OK) { |
| 908 | ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d", |
| 909 | __FUNCTION__, result.frameNumber, i, bSrc.streamId); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 910 | return; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 911 | } |
| 912 | bDst.buffer = buffer; |
| 913 | bDst.status = mapHidlBufferStatus(bSrc.status); |
| 914 | bDst.acquire_fence = -1; |
| 915 | if (bSrc.releaseFence == nullptr) { |
| 916 | bDst.release_fence = -1; |
| 917 | } else if (bSrc.releaseFence->numFds == 1) { |
| 918 | bDst.release_fence = dup(bSrc.releaseFence->data[0]); |
| 919 | } else { |
| 920 | ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1", |
| 921 | __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 922 | return; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | r.num_output_buffers = outputBuffers.size(); |
| 926 | r.output_buffers = outputBuffers.data(); |
| 927 | |
| 928 | camera3_stream_buffer_t inputBuffer; |
Yin-Chia Yeh | 52778d4 | 2016-12-22 18:20:43 -0800 | [diff] [blame] | 929 | if (result.inputBuffer.streamId == -1) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 930 | r.input_buffer = nullptr; |
| 931 | } else { |
| 932 | if (mInputStream->getId() != result.inputBuffer.streamId) { |
| 933 | ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__, |
| 934 | result.frameNumber, result.inputBuffer.streamId); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 935 | return; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 936 | } |
| 937 | inputBuffer.stream = mInputStream->asHalStream(); |
| 938 | buffer_handle_t *buffer; |
| 939 | res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId, |
| 940 | &buffer); |
| 941 | if (res != OK) { |
| 942 | ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d", |
| 943 | __FUNCTION__, result.frameNumber, result.inputBuffer.streamId); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 944 | return; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 945 | } |
| 946 | inputBuffer.buffer = buffer; |
| 947 | inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status); |
| 948 | inputBuffer.acquire_fence = -1; |
| 949 | if (result.inputBuffer.releaseFence == nullptr) { |
| 950 | inputBuffer.release_fence = -1; |
| 951 | } else if (result.inputBuffer.releaseFence->numFds == 1) { |
| 952 | inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]); |
| 953 | } else { |
| 954 | ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1", |
| 955 | __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 956 | return; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 957 | } |
| 958 | r.input_buffer = &inputBuffer; |
| 959 | } |
| 960 | |
| 961 | r.partial_result = result.partialResult; |
| 962 | |
| 963 | processCaptureResult(&r); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | hardware::Return<void> Camera3Device::notify( |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 967 | const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) { |
| 968 | for (const auto& msg : msgs) { |
| 969 | notify(msg); |
| 970 | } |
| 971 | return hardware::Void(); |
| 972 | } |
| 973 | |
| 974 | void Camera3Device::notify( |
| 975 | const hardware::camera::device::V3_2::NotifyMsg& msg) { |
| 976 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 977 | camera3_notify_msg m; |
| 978 | switch (msg.type) { |
| 979 | case MsgType::ERROR: |
| 980 | m.type = CAMERA3_MSG_ERROR; |
| 981 | m.message.error.frame_number = msg.msg.error.frameNumber; |
| 982 | if (msg.msg.error.errorStreamId >= 0) { |
| 983 | ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId); |
Emilian Peev | be3d40c | 2017-03-27 13:03:10 +0100 | [diff] [blame] | 984 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 985 | ALOGE("%s: Frame %d: Invalid error stream id %d", |
| 986 | __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 987 | return; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 988 | } |
| 989 | m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream(); |
| 990 | } else { |
| 991 | m.message.error.error_stream = nullptr; |
| 992 | } |
| 993 | switch (msg.msg.error.errorCode) { |
| 994 | case ErrorCode::ERROR_DEVICE: |
| 995 | m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE; |
| 996 | break; |
| 997 | case ErrorCode::ERROR_REQUEST: |
| 998 | m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; |
| 999 | break; |
| 1000 | case ErrorCode::ERROR_RESULT: |
| 1001 | m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT; |
| 1002 | break; |
| 1003 | case ErrorCode::ERROR_BUFFER: |
| 1004 | m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER; |
| 1005 | break; |
| 1006 | } |
| 1007 | break; |
| 1008 | case MsgType::SHUTTER: |
| 1009 | m.type = CAMERA3_MSG_SHUTTER; |
| 1010 | m.message.shutter.frame_number = msg.msg.shutter.frameNumber; |
| 1011 | m.message.shutter.timestamp = msg.msg.shutter.timestamp; |
| 1012 | break; |
| 1013 | } |
| 1014 | notify(&m); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1017 | status_t Camera3Device::captureList(const List<const CameraMetadata> &requests, |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1018 | const std::list<const SurfaceMap> &surfaceMaps, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1019 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1020 | ATRACE_CALL(); |
| 1021 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1022 | return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1023 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1024 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1025 | status_t Camera3Device::setStreamingRequest(const CameraMetadata &request, |
| 1026 | int64_t* /*lastFrameNumber*/) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1027 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1028 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 1029 | List<const CameraMetadata> requests; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1030 | std::list<const SurfaceMap> surfaceMaps; |
| 1031 | convertToRequestList(requests, surfaceMaps, request); |
| 1032 | |
| 1033 | return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps, |
| 1034 | /*lastFrameNumber*/NULL); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1035 | } |
| 1036 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1037 | status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests, |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1038 | const std::list<const SurfaceMap> &surfaceMaps, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1039 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1040 | ATRACE_CALL(); |
| 1041 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1042 | return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1043 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1044 | |
| 1045 | sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1046 | const CameraMetadata &request, const SurfaceMap &surfaceMap) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1047 | status_t res; |
| 1048 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1049 | if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) { |
Eino-Ville Talvala | e7091aa | 2017-03-07 15:23:06 -0800 | [diff] [blame] | 1050 | // This point should only be reached via API1 (API2 must explicitly call configureStreams) |
| 1051 | // so unilaterally select normal operating mode. |
| 1052 | res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE); |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 1053 | // Stream configuration failed. Client might try other configuraitons. |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1054 | if (res != OK) { |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 1055 | CLOGE("Can't set up streams: %s (%d)", strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1056 | return NULL; |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 1057 | } else if (mStatus == STATUS_UNCONFIGURED) { |
| 1058 | // Stream configuration successfully configure to empty stream configuration. |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1059 | CLOGE("No streams configured"); |
| 1060 | return NULL; |
| 1061 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1064 | sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1065 | return newRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1068 | status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1069 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1070 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1071 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1072 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1073 | switch (mStatus) { |
| 1074 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1075 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1076 | return INVALID_OPERATION; |
| 1077 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1078 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1079 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1080 | case STATUS_UNCONFIGURED: |
| 1081 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1082 | case STATUS_ACTIVE: |
| 1083 | // OK |
| 1084 | break; |
| 1085 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1086 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1087 | return INVALID_OPERATION; |
| 1088 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1089 | ALOGV("Camera %s: Clearing repeating request", mId.string()); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1090 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 1091 | return mRequestThread->clearRepeatingRequests(lastFrameNumber); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { |
| 1095 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1096 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1097 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1098 | return mRequestThread->waitUntilRequestProcessed(requestId, timeout); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1101 | status_t Camera3Device::createInputStream( |
| 1102 | uint32_t width, uint32_t height, int format, int *id) { |
| 1103 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1104 | Mutex::Autolock il(mInterfaceLock); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1105 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1106 | ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d", |
| 1107 | mId.string(), mNextStreamId, width, height, format); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1108 | |
| 1109 | status_t res; |
| 1110 | bool wasActive = false; |
| 1111 | |
| 1112 | switch (mStatus) { |
| 1113 | case STATUS_ERROR: |
| 1114 | ALOGE("%s: Device has encountered a serious error", __FUNCTION__); |
| 1115 | return INVALID_OPERATION; |
| 1116 | case STATUS_UNINITIALIZED: |
| 1117 | ALOGE("%s: Device not initialized", __FUNCTION__); |
| 1118 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1119 | case STATUS_UNCONFIGURED: |
| 1120 | case STATUS_CONFIGURED: |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1121 | // OK |
| 1122 | break; |
| 1123 | case STATUS_ACTIVE: |
| 1124 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1125 | res = internalPauseAndWaitLocked(); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1126 | if (res != OK) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1127 | SET_ERR_L("Can't pause captures to reconfigure streams!"); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1128 | return res; |
| 1129 | } |
| 1130 | wasActive = true; |
| 1131 | break; |
| 1132 | default: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1133 | SET_ERR_L("%s: Unexpected status: %d", mStatus); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1134 | return INVALID_OPERATION; |
| 1135 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1136 | assert(mStatus != STATUS_ACTIVE); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (mInputStream != 0) { |
| 1139 | ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); |
| 1140 | return INVALID_OPERATION; |
| 1141 | } |
| 1142 | |
| 1143 | sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, |
| 1144 | width, height, format); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1145 | newStream->setStatusTracker(mStatusTracker); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1146 | |
| 1147 | mInputStream = newStream; |
| 1148 | |
| 1149 | *id = mNextStreamId++; |
| 1150 | |
| 1151 | // Continue captures if active at start |
| 1152 | if (wasActive) { |
| 1153 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
Eino-Ville Talvala | e7091aa | 2017-03-07 15:23:06 -0800 | [diff] [blame] | 1154 | // Reuse current operating mode for new stream config |
| 1155 | res = configureStreamsLocked(mOperatingMode); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1156 | if (res != OK) { |
| 1157 | ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", |
| 1158 | __FUNCTION__, mNextStreamId, strerror(-res), res); |
| 1159 | return res; |
| 1160 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1161 | internalResumeLocked(); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1164 | ALOGV("Camera %s: Created input stream", mId.string()); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1165 | return OK; |
| 1166 | } |
| 1167 | |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 1168 | status_t Camera3Device::createStream(sp<Surface> consumer, |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1169 | uint32_t width, uint32_t height, int format, |
| 1170 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id, |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1171 | int streamSetId, bool isShared, uint32_t consumerUsage) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1172 | ATRACE_CALL(); |
| 1173 | |
| 1174 | if (consumer == nullptr) { |
| 1175 | ALOGE("%s: consumer must not be null", __FUNCTION__); |
| 1176 | return BAD_VALUE; |
| 1177 | } |
| 1178 | |
| 1179 | std::vector<sp<Surface>> consumers; |
| 1180 | consumers.push_back(consumer); |
| 1181 | |
| 1182 | return createStream(consumers, /*hasDeferredConsumer*/ false, width, height, |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1183 | format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers, |
| 1187 | bool hasDeferredConsumer, uint32_t width, uint32_t height, int format, |
| 1188 | android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id, |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1189 | int streamSetId, bool isShared, uint32_t consumerUsage) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1190 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1191 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1192 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1193 | ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d" |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1194 | " consumer usage 0x%x, isShared %d", mId.string(), mNextStreamId, width, height, format, |
| 1195 | dataSpace, rotation, consumerUsage, isShared); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1196 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1197 | status_t res; |
| 1198 | bool wasActive = false; |
| 1199 | |
| 1200 | switch (mStatus) { |
| 1201 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1202 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1203 | return INVALID_OPERATION; |
| 1204 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1205 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1206 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1207 | case STATUS_UNCONFIGURED: |
| 1208 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1209 | // OK |
| 1210 | break; |
| 1211 | case STATUS_ACTIVE: |
| 1212 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1213 | res = internalPauseAndWaitLocked(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1214 | if (res != OK) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1215 | SET_ERR_L("Can't pause captures to reconfigure streams!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1216 | return res; |
| 1217 | } |
| 1218 | wasActive = true; |
| 1219 | break; |
| 1220 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1221 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1222 | return INVALID_OPERATION; |
| 1223 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1224 | assert(mStatus != STATUS_ACTIVE); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1225 | |
| 1226 | sp<Camera3OutputStream> newStream; |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1227 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1228 | if (consumers.size() == 0 && !hasDeferredConsumer) { |
| 1229 | ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__); |
| 1230 | return BAD_VALUE; |
| 1231 | } |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1232 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1233 | if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1234 | ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format"); |
| 1235 | return BAD_VALUE; |
| 1236 | } |
| 1237 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1238 | if (format == HAL_PIXEL_FORMAT_BLOB) { |
Eino-Ville Talvala | 95a1d0f | 2015-08-11 15:08:53 -0700 | [diff] [blame] | 1239 | ssize_t blobBufferSize; |
| 1240 | if (dataSpace != HAL_DATASPACE_DEPTH) { |
| 1241 | blobBufferSize = getJpegBufferSize(width, height); |
| 1242 | if (blobBufferSize <= 0) { |
| 1243 | SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize); |
| 1244 | return BAD_VALUE; |
| 1245 | } |
| 1246 | } else { |
| 1247 | blobBufferSize = getPointCloudBufferSize(); |
| 1248 | if (blobBufferSize <= 0) { |
| 1249 | SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize); |
| 1250 | return BAD_VALUE; |
| 1251 | } |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 1252 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1253 | newStream = new Camera3OutputStream(mNextStreamId, consumers[0], |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 1254 | width, height, blobBufferSize, format, dataSpace, rotation, |
| 1255 | mTimestampOffset, streamSetId); |
Yin-Chia Yeh | e9154ce | 2015-12-07 14:38:04 -0800 | [diff] [blame] | 1256 | } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) { |
| 1257 | ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height); |
| 1258 | if (rawOpaqueBufferSize <= 0) { |
| 1259 | SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize); |
| 1260 | return BAD_VALUE; |
| 1261 | } |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1262 | newStream = new Camera3OutputStream(mNextStreamId, consumers[0], |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 1263 | width, height, rawOpaqueBufferSize, format, dataSpace, rotation, |
| 1264 | mTimestampOffset, streamSetId); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1265 | } else if (isShared) { |
| 1266 | newStream = new Camera3SharedOutputStream(mNextStreamId, consumers, |
| 1267 | width, height, format, consumerUsage, dataSpace, rotation, |
| 1268 | mTimestampOffset, streamSetId); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1269 | } else if (consumers.size() == 0 && hasDeferredConsumer) { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1270 | newStream = new Camera3OutputStream(mNextStreamId, |
| 1271 | width, height, format, consumerUsage, dataSpace, rotation, |
| 1272 | mTimestampOffset, streamSetId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1273 | } else { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1274 | newStream = new Camera3OutputStream(mNextStreamId, consumers[0], |
Shuzhen Wang | c28dccc | 2016-02-11 23:48:46 -0800 | [diff] [blame] | 1275 | width, height, format, dataSpace, rotation, |
| 1276 | mTimestampOffset, streamSetId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1277 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1278 | newStream->setStatusTracker(mStatusTracker); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1279 | |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 1280 | newStream->setBufferManager(mBufferManager); |
Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame] | 1281 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1282 | res = mOutputStreams.add(mNextStreamId, newStream); |
| 1283 | if (res < 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1284 | SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1285 | return res; |
| 1286 | } |
| 1287 | |
| 1288 | *id = mNextStreamId++; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1289 | mNeedConfig = true; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1290 | |
| 1291 | // Continue captures if active at start |
| 1292 | if (wasActive) { |
| 1293 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
Eino-Ville Talvala | e7091aa | 2017-03-07 15:23:06 -0800 | [diff] [blame] | 1294 | // Reuse current operating mode for new stream config |
| 1295 | res = configureStreamsLocked(mOperatingMode); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1296 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1297 | CLOGE("Can't reconfigure device for new stream %d: %s (%d)", |
| 1298 | mNextStreamId, strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1299 | return res; |
| 1300 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1301 | internalResumeLocked(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1302 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1303 | ALOGV("Camera %s: Created new stream", mId.string()); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1304 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1307 | status_t Camera3Device::getStreamInfo(int id, |
Eino-Ville Talvala | d46a6b9 | 2015-05-14 17:26:24 -0700 | [diff] [blame] | 1308 | uint32_t *width, uint32_t *height, |
| 1309 | uint32_t *format, android_dataspace *dataSpace) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1310 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1311 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1312 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1313 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1314 | switch (mStatus) { |
| 1315 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1316 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1317 | return INVALID_OPERATION; |
| 1318 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1319 | CLOGE("Device not initialized!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1320 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1321 | case STATUS_UNCONFIGURED: |
| 1322 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1323 | case STATUS_ACTIVE: |
| 1324 | // OK |
| 1325 | break; |
| 1326 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1327 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1328 | return INVALID_OPERATION; |
| 1329 | } |
| 1330 | |
| 1331 | ssize_t idx = mOutputStreams.indexOfKey(id); |
| 1332 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1333 | CLOGE("Stream %d is unknown", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1334 | return idx; |
| 1335 | } |
| 1336 | |
| 1337 | if (width) *width = mOutputStreams[idx]->getWidth(); |
| 1338 | if (height) *height = mOutputStreams[idx]->getHeight(); |
| 1339 | if (format) *format = mOutputStreams[idx]->getFormat(); |
Eino-Ville Talvala | d46a6b9 | 2015-05-14 17:26:24 -0700 | [diff] [blame] | 1340 | if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1341 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | status_t Camera3Device::setStreamTransform(int id, |
| 1345 | int transform) { |
| 1346 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1347 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1348 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1349 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1350 | switch (mStatus) { |
| 1351 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1352 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1353 | return INVALID_OPERATION; |
| 1354 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1355 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1356 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1357 | case STATUS_UNCONFIGURED: |
| 1358 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1359 | case STATUS_ACTIVE: |
| 1360 | // OK |
| 1361 | break; |
| 1362 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1363 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1364 | return INVALID_OPERATION; |
| 1365 | } |
| 1366 | |
| 1367 | ssize_t idx = mOutputStreams.indexOfKey(id); |
| 1368 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1369 | CLOGE("Stream %d does not exist", |
| 1370 | id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1371 | return BAD_VALUE; |
| 1372 | } |
| 1373 | |
| 1374 | return mOutputStreams.editValueAt(idx)->setTransform(transform); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | status_t Camera3Device::deleteStream(int id) { |
| 1378 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1379 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1380 | Mutex::Autolock l(mLock); |
| 1381 | status_t res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1382 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1383 | ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id); |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 1384 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1385 | // CameraDevice semantics require device to already be idle before |
| 1386 | // deleteStream is called, unlike for createStream. |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1387 | if (mStatus == STATUS_ACTIVE) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1388 | ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string()); |
Igor Murashkin | 5282713 | 2013-05-13 14:53:44 -0700 | [diff] [blame] | 1389 | return -EBUSY; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1392 | sp<Camera3StreamInterface> deletedStream; |
Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 1393 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1394 | if (mInputStream != NULL && id == mInputStream->getId()) { |
| 1395 | deletedStream = mInputStream; |
| 1396 | mInputStream.clear(); |
| 1397 | } else { |
Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 1398 | if (outputStreamIdx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1399 | CLOGE("Stream %d does not exist", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1400 | return BAD_VALUE; |
| 1401 | } |
Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | // Delete output stream or the output part of a bi-directional stream. |
| 1405 | if (outputStreamIdx != NAME_NOT_FOUND) { |
| 1406 | deletedStream = mOutputStreams.editValueAt(outputStreamIdx); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1407 | mOutputStreams.removeItem(id); |
| 1408 | } |
| 1409 | |
| 1410 | // Free up the stream endpoint so that it can be used by some other stream |
| 1411 | res = deletedStream->disconnect(); |
| 1412 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1413 | SET_ERR_L("Can't disconnect deleted stream %d", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1414 | // fall through since we want to still list the stream as deleted. |
| 1415 | } |
| 1416 | mDeletedStreams.add(deletedStream); |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1417 | mNeedConfig = true; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1418 | |
| 1419 | return res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Eino-Ville Talvala | bbbbe84 | 2017-02-28 17:50:56 -0800 | [diff] [blame] | 1422 | status_t Camera3Device::configureStreams(int operatingMode) { |
Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 1423 | ATRACE_CALL(); |
| 1424 | ALOGV("%s: E", __FUNCTION__); |
| 1425 | |
| 1426 | Mutex::Autolock il(mInterfaceLock); |
| 1427 | Mutex::Autolock l(mLock); |
Chien-Yu Chen | 17338fc | 2015-06-18 16:30:12 -0700 | [diff] [blame] | 1428 | |
Eino-Ville Talvala | e7091aa | 2017-03-07 15:23:06 -0800 | [diff] [blame] | 1429 | return configureStreamsLocked(operatingMode); |
Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 1430 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1431 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1432 | status_t Camera3Device::getInputBufferProducer( |
| 1433 | sp<IGraphicBufferProducer> *producer) { |
| 1434 | Mutex::Autolock il(mInterfaceLock); |
| 1435 | Mutex::Autolock l(mLock); |
| 1436 | |
| 1437 | if (producer == NULL) { |
| 1438 | return BAD_VALUE; |
| 1439 | } else if (mInputStream == NULL) { |
| 1440 | return INVALID_OPERATION; |
| 1441 | } |
| 1442 | |
| 1443 | return mInputStream->getInputBufferProducer(producer); |
| 1444 | } |
| 1445 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1446 | status_t Camera3Device::createDefaultRequest(int templateId, |
| 1447 | CameraMetadata *request) { |
| 1448 | ATRACE_CALL(); |
Alex Ray | fe7e0c6 | 2013-05-30 00:12:13 -0700 | [diff] [blame] | 1449 | ALOGV("%s: for template %d", __FUNCTION__, templateId); |
Chien-Yu Chen | 9cd1402 | 2016-03-09 12:21:01 -0800 | [diff] [blame] | 1450 | |
| 1451 | if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) { |
| 1452 | android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110", |
| 1453 | IPCThreadState::self()->getCallingUid(), nullptr, 0); |
| 1454 | return BAD_VALUE; |
| 1455 | } |
| 1456 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1457 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1458 | Mutex::Autolock l(mLock); |
| 1459 | |
| 1460 | switch (mStatus) { |
| 1461 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1462 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1463 | return INVALID_OPERATION; |
| 1464 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1465 | CLOGE("Device is not initialized!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1466 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1467 | case STATUS_UNCONFIGURED: |
| 1468 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1469 | case STATUS_ACTIVE: |
| 1470 | // OK |
| 1471 | break; |
| 1472 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1473 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1474 | return INVALID_OPERATION; |
| 1475 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1476 | |
Zhijun He | a1530f1 | 2014-09-14 12:44:20 -0700 | [diff] [blame] | 1477 | if (!mRequestTemplateCache[templateId].isEmpty()) { |
| 1478 | *request = mRequestTemplateCache[templateId]; |
| 1479 | return OK; |
| 1480 | } |
| 1481 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1482 | camera_metadata_t *rawRequest; |
| 1483 | status_t res = mInterface->constructDefaultRequestSettings( |
| 1484 | (camera3_request_template_t) templateId, &rawRequest); |
| 1485 | if (res == BAD_VALUE) { |
Yin-Chia Yeh | 0336d36 | 2015-04-14 12:34:22 -0700 | [diff] [blame] | 1486 | ALOGI("%s: template %d is not supported on this camera device", |
| 1487 | __FUNCTION__, templateId); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1488 | return res; |
| 1489 | } else if (res != OK) { |
| 1490 | CLOGE("Unable to construct request template %d: %s (%d)", |
| 1491 | templateId, strerror(-res), res); |
| 1492 | return res; |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1493 | } |
Yin-Chia Yeh | 4c06099 | 2016-04-11 17:40:12 -0700 | [diff] [blame] | 1494 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 1495 | set_camera_metadata_vendor_id(rawRequest, mVendorTagId); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1496 | mRequestTemplateCache[templateId].acquire(rawRequest); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1497 | |
Yin-Chia Yeh | 4c06099 | 2016-04-11 17:40:12 -0700 | [diff] [blame] | 1498 | *request = mRequestTemplateCache[templateId]; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1499 | return OK; |
| 1500 | } |
| 1501 | |
| 1502 | status_t Camera3Device::waitUntilDrained() { |
| 1503 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1504 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1505 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1506 | |
Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 1507 | return waitUntilDrainedLocked(); |
| 1508 | } |
| 1509 | |
| 1510 | status_t Camera3Device::waitUntilDrainedLocked() { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1511 | switch (mStatus) { |
| 1512 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1513 | case STATUS_UNCONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1514 | ALOGV("%s: Already idle", __FUNCTION__); |
| 1515 | return OK; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1516 | case STATUS_CONFIGURED: |
| 1517 | // To avoid race conditions, check with tracker to be sure |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1518 | case STATUS_ERROR: |
| 1519 | case STATUS_ACTIVE: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1520 | // Need to verify shut down |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1521 | break; |
| 1522 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1523 | SET_ERR_L("Unexpected status: %d",mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1524 | return INVALID_OPERATION; |
| 1525 | } |
| 1526 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1527 | ALOGV("%s: Camera %s: Waiting until idle", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1528 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
Eino-Ville Talvala | 9c8a091 | 2014-09-14 14:52:19 -0700 | [diff] [blame] | 1529 | if (res != OK) { |
| 1530 | SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res), |
| 1531 | res); |
| 1532 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1533 | return res; |
| 1534 | } |
| 1535 | |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1536 | |
| 1537 | void Camera3Device::internalUpdateStatusLocked(Status status) { |
| 1538 | mStatus = status; |
| 1539 | mRecentStatusUpdates.add(mStatus); |
| 1540 | mStatusChanged.broadcast(); |
| 1541 | } |
| 1542 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1543 | // Pause to reconfigure |
| 1544 | status_t Camera3Device::internalPauseAndWaitLocked() { |
| 1545 | mRequestThread->setPaused(true); |
| 1546 | mPauseStateNotify = true; |
| 1547 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1548 | ALOGV("%s: Camera %s: Internal wait until idle", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1549 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
| 1550 | if (res != OK) { |
| 1551 | SET_ERR_L("Can't idle device in %f seconds!", |
| 1552 | kShutdownTimeout/1e9); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1553 | } |
| 1554 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1555 | return res; |
| 1556 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1557 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1558 | // Resume after internalPauseAndWaitLocked |
| 1559 | status_t Camera3Device::internalResumeLocked() { |
| 1560 | status_t res; |
| 1561 | |
| 1562 | mRequestThread->setPaused(false); |
| 1563 | |
| 1564 | res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); |
| 1565 | if (res != OK) { |
| 1566 | SET_ERR_L("Can't transition to active in %f seconds!", |
| 1567 | kActiveTimeout/1e9); |
| 1568 | } |
| 1569 | mPauseStateNotify = false; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1570 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1573 | status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1574 | status_t res = OK; |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1575 | |
| 1576 | size_t startIndex = 0; |
| 1577 | if (mStatusWaiters == 0) { |
| 1578 | // Clear the list of recent statuses if there are no existing threads waiting on updates to |
| 1579 | // this status list |
| 1580 | mRecentStatusUpdates.clear(); |
| 1581 | } else { |
| 1582 | // If other threads are waiting on updates to this status list, set the position of the |
| 1583 | // first element that this list will check rather than clearing the list. |
| 1584 | startIndex = mRecentStatusUpdates.size(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1587 | mStatusWaiters++; |
| 1588 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1589 | bool stateSeen = false; |
| 1590 | do { |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1591 | if (active == (mStatus == STATUS_ACTIVE)) { |
| 1592 | // Desired state is current |
| 1593 | break; |
| 1594 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1595 | |
| 1596 | res = mStatusChanged.waitRelative(mLock, timeout); |
| 1597 | if (res != OK) break; |
| 1598 | |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1599 | // This is impossible, but if not, could result in subtle deadlocks and invalid state |
| 1600 | // transitions. |
| 1601 | LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(), |
| 1602 | "%s: Skipping status updates in Camera3Device, may result in deadlock.", |
| 1603 | __FUNCTION__); |
| 1604 | |
| 1605 | // Encountered desired state since we began waiting |
| 1606 | for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1607 | if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) { |
| 1608 | stateSeen = true; |
| 1609 | break; |
| 1610 | } |
| 1611 | } |
| 1612 | } while (!stateSeen); |
| 1613 | |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1614 | mStatusWaiters--; |
| 1615 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1616 | return res; |
| 1617 | } |
| 1618 | |
| 1619 | |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 1620 | status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1621 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1622 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1623 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1624 | if (listener != NULL && mListener != NULL) { |
| 1625 | ALOGW("%s: Replacing old callback listener", __FUNCTION__); |
| 1626 | } |
| 1627 | mListener = listener; |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1628 | mRequestThread->setNotificationListener(listener); |
| 1629 | mPreparerThread->setNotificationListener(listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1630 | |
| 1631 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1632 | } |
| 1633 | |
Eino-Ville Talvala | 46910bd | 2013-07-18 19:15:17 -0700 | [diff] [blame] | 1634 | bool Camera3Device::willNotify3A() { |
| 1635 | return false; |
| 1636 | } |
| 1637 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1638 | status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1639 | status_t res; |
| 1640 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1641 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1642 | while (mResultQueue.empty()) { |
| 1643 | res = mResultSignal.waitRelative(mOutputLock, timeout); |
| 1644 | if (res == TIMED_OUT) { |
| 1645 | return res; |
| 1646 | } else if (res != OK) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1647 | ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)", |
| 1648 | __FUNCTION__, mId.string(), timeout, strerror(-res), res); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1649 | return res; |
| 1650 | } |
| 1651 | } |
| 1652 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1653 | } |
| 1654 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1655 | status_t Camera3Device::getNextResult(CaptureResult *frame) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1656 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1657 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1658 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1659 | if (mResultQueue.empty()) { |
| 1660 | return NOT_ENOUGH_DATA; |
| 1661 | } |
| 1662 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1663 | if (frame == NULL) { |
| 1664 | ALOGE("%s: argument cannot be NULL", __FUNCTION__); |
| 1665 | return BAD_VALUE; |
| 1666 | } |
| 1667 | |
| 1668 | CaptureResult &result = *(mResultQueue.begin()); |
| 1669 | frame->mResultExtras = result.mResultExtras; |
| 1670 | frame->mMetadata.acquire(result.mMetadata); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1671 | mResultQueue.erase(mResultQueue.begin()); |
| 1672 | |
| 1673 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | status_t Camera3Device::triggerAutofocus(uint32_t id) { |
| 1677 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1678 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1679 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1680 | ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); |
| 1681 | // Mix-in this trigger into the next request and only the next request. |
| 1682 | RequestTrigger trigger[] = { |
| 1683 | { |
| 1684 | ANDROID_CONTROL_AF_TRIGGER, |
| 1685 | ANDROID_CONTROL_AF_TRIGGER_START |
| 1686 | }, |
| 1687 | { |
| 1688 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 1689 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1690 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1691 | }; |
| 1692 | |
| 1693 | return mRequestThread->queueTrigger(trigger, |
| 1694 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { |
| 1698 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1699 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1700 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1701 | ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); |
| 1702 | // Mix-in this trigger into the next request and only the next request. |
| 1703 | RequestTrigger trigger[] = { |
| 1704 | { |
| 1705 | ANDROID_CONTROL_AF_TRIGGER, |
| 1706 | ANDROID_CONTROL_AF_TRIGGER_CANCEL |
| 1707 | }, |
| 1708 | { |
| 1709 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 1710 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1711 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1712 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1713 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1714 | return mRequestThread->queueTrigger(trigger, |
| 1715 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { |
| 1719 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1720 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1721 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1722 | ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); |
| 1723 | // Mix-in this trigger into the next request and only the next request. |
| 1724 | RequestTrigger trigger[] = { |
| 1725 | { |
| 1726 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 1727 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START |
| 1728 | }, |
| 1729 | { |
| 1730 | ANDROID_CONTROL_AE_PRECAPTURE_ID, |
| 1731 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1732 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1733 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1734 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1735 | return mRequestThread->queueTrigger(trigger, |
| 1736 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1739 | status_t Camera3Device::flush(int64_t *frameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1740 | ATRACE_CALL(); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1741 | ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1742 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1743 | |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1744 | { |
| 1745 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 1746 | mRequestThread->clear(/*out*/frameNumber); |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 1749 | return mRequestThread->flush(); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1752 | status_t Camera3Device::prepare(int streamId) { |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 1753 | return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId); |
| 1754 | } |
| 1755 | |
| 1756 | status_t Camera3Device::prepare(int maxCount, int streamId) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1757 | ATRACE_CALL(); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1758 | ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId); |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1759 | Mutex::Autolock il(mInterfaceLock); |
| 1760 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1761 | |
| 1762 | sp<Camera3StreamInterface> stream; |
| 1763 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); |
| 1764 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 1765 | CLOGE("Stream %d does not exist", streamId); |
| 1766 | return BAD_VALUE; |
| 1767 | } |
| 1768 | |
| 1769 | stream = mOutputStreams.editValueAt(outputStreamIdx); |
| 1770 | |
| 1771 | if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) { |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1772 | CLOGE("Stream %d has already been a request target", streamId); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1773 | return BAD_VALUE; |
| 1774 | } |
| 1775 | |
| 1776 | if (mRequestThread->isStreamPending(stream)) { |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1777 | CLOGE("Stream %d is already a target in a pending request", streamId); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1778 | return BAD_VALUE; |
| 1779 | } |
| 1780 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 1781 | return mPreparerThread->prepare(maxCount, stream); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 1784 | status_t Camera3Device::tearDown(int streamId) { |
| 1785 | ATRACE_CALL(); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1786 | ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId); |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 1787 | Mutex::Autolock il(mInterfaceLock); |
| 1788 | Mutex::Autolock l(mLock); |
| 1789 | |
Eino-Ville Talvala | b25e3c8 | 2015-07-15 16:04:27 -0700 | [diff] [blame] | 1790 | sp<Camera3StreamInterface> stream; |
| 1791 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); |
| 1792 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 1793 | CLOGE("Stream %d does not exist", streamId); |
| 1794 | return BAD_VALUE; |
| 1795 | } |
| 1796 | |
| 1797 | stream = mOutputStreams.editValueAt(outputStreamIdx); |
| 1798 | |
| 1799 | if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) { |
| 1800 | CLOGE("Stream %d is a target of a in-progress request", streamId); |
| 1801 | return BAD_VALUE; |
| 1802 | } |
| 1803 | |
| 1804 | return stream->tearDown(); |
| 1805 | } |
| 1806 | |
Shuzhen Wang | b0fdc1e | 2016-03-20 23:21:39 -0700 | [diff] [blame] | 1807 | status_t Camera3Device::addBufferListenerForStream(int streamId, |
| 1808 | wp<Camera3StreamBufferListener> listener) { |
| 1809 | ATRACE_CALL(); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1810 | ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId); |
Shuzhen Wang | b0fdc1e | 2016-03-20 23:21:39 -0700 | [diff] [blame] | 1811 | Mutex::Autolock il(mInterfaceLock); |
| 1812 | Mutex::Autolock l(mLock); |
| 1813 | |
| 1814 | sp<Camera3StreamInterface> stream; |
| 1815 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); |
| 1816 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 1817 | CLOGE("Stream %d does not exist", streamId); |
| 1818 | return BAD_VALUE; |
| 1819 | } |
| 1820 | |
| 1821 | stream = mOutputStreams.editValueAt(outputStreamIdx); |
| 1822 | stream->addBufferListener(listener); |
| 1823 | |
| 1824 | return OK; |
| 1825 | } |
| 1826 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1827 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1828 | * Methods called by subclasses |
| 1829 | */ |
| 1830 | |
| 1831 | void Camera3Device::notifyStatus(bool idle) { |
| 1832 | { |
| 1833 | // Need mLock to safely update state and synchronize to current |
| 1834 | // state of methods in flight. |
| 1835 | Mutex::Autolock l(mLock); |
| 1836 | // We can get various system-idle notices from the status tracker |
| 1837 | // while starting up. Only care about them if we've actually sent |
| 1838 | // in some requests recently. |
| 1839 | if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) { |
| 1840 | return; |
| 1841 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1842 | ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(), |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1843 | idle ? "idle" : "active"); |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 1844 | internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1845 | |
| 1846 | // Skip notifying listener if we're doing some user-transparent |
| 1847 | // state changes |
| 1848 | if (mPauseStateNotify) return; |
| 1849 | } |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 1850 | |
| 1851 | sp<NotificationListener> listener; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1852 | { |
| 1853 | Mutex::Autolock l(mOutputLock); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 1854 | listener = mListener.promote(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1855 | } |
| 1856 | if (idle && listener != NULL) { |
| 1857 | listener->notifyIdle(); |
| 1858 | } |
| 1859 | } |
| 1860 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1861 | status_t Camera3Device::setConsumerSurfaces(int streamId, |
| 1862 | const std::vector<sp<Surface>>& consumers) { |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1863 | ATRACE_CALL(); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1864 | ALOGV("%s: Camera %s: set consumer surface for stream %d", |
| 1865 | __FUNCTION__, mId.string(), streamId); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1866 | Mutex::Autolock il(mInterfaceLock); |
| 1867 | Mutex::Autolock l(mLock); |
| 1868 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1869 | if (consumers.size() == 0) { |
| 1870 | CLOGE("No consumer is passed!"); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1871 | return BAD_VALUE; |
| 1872 | } |
| 1873 | |
| 1874 | ssize_t idx = mOutputStreams.indexOfKey(streamId); |
| 1875 | if (idx == NAME_NOT_FOUND) { |
| 1876 | CLOGE("Stream %d is unknown", streamId); |
| 1877 | return idx; |
| 1878 | } |
| 1879 | sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx]; |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame] | 1880 | status_t res = stream->setConsumers(consumers); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1881 | if (res != OK) { |
| 1882 | CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res)); |
| 1883 | return res; |
| 1884 | } |
| 1885 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1886 | if (stream->isConsumerConfigurationDeferred()) { |
| 1887 | if (!stream->isConfiguring()) { |
| 1888 | CLOGE("Stream %d was already fully configured.", streamId); |
| 1889 | return INVALID_OPERATION; |
| 1890 | } |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1891 | |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1892 | res = stream->finishConfiguration(); |
| 1893 | if (res != OK) { |
| 1894 | SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", |
| 1895 | stream->getId(), strerror(-res), res); |
| 1896 | return res; |
| 1897 | } |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | return OK; |
| 1901 | } |
| 1902 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1903 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1904 | * Camera3Device private methods |
| 1905 | */ |
| 1906 | |
| 1907 | sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1908 | const CameraMetadata &request, const SurfaceMap &surfaceMap) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1909 | ATRACE_CALL(); |
| 1910 | status_t res; |
| 1911 | |
| 1912 | sp<CaptureRequest> newRequest = new CaptureRequest; |
| 1913 | newRequest->mSettings = request; |
| 1914 | |
| 1915 | camera_metadata_entry_t inputStreams = |
| 1916 | newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); |
| 1917 | if (inputStreams.count > 0) { |
| 1918 | if (mInputStream == NULL || |
Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1919 | mInputStream->getId() != inputStreams.data.i32[0]) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1920 | CLOGE("Request references unknown input stream %d", |
| 1921 | inputStreams.data.u8[0]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1922 | return NULL; |
| 1923 | } |
| 1924 | // Lazy completion of stream configuration (allocation/registration) |
| 1925 | // on first use |
| 1926 | if (mInputStream->isConfiguring()) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1927 | res = mInputStream->finishConfiguration(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1928 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1929 | SET_ERR_L("Unable to finish configuring input stream %d:" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1930 | " %s (%d)", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1931 | mInputStream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1932 | return NULL; |
| 1933 | } |
| 1934 | } |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1935 | // Check if stream is being prepared |
| 1936 | if (mInputStream->isPreparing()) { |
| 1937 | CLOGE("Request references an input stream that's being prepared!"); |
| 1938 | return NULL; |
| 1939 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1940 | |
| 1941 | newRequest->mInputStream = mInputStream; |
| 1942 | newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); |
| 1943 | } |
| 1944 | |
| 1945 | camera_metadata_entry_t streams = |
| 1946 | newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 1947 | if (streams.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1948 | CLOGE("Zero output streams specified!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1949 | return NULL; |
| 1950 | } |
| 1951 | |
| 1952 | for (size_t i = 0; i < streams.count; i++) { |
Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1953 | int idx = mOutputStreams.indexOfKey(streams.data.i32[i]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1954 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1955 | CLOGE("Request references unknown stream %d", |
| 1956 | streams.data.u8[i]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1957 | return NULL; |
| 1958 | } |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1959 | sp<Camera3OutputStreamInterface> stream = |
| 1960 | mOutputStreams.editValueAt(idx); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1961 | |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1962 | // It is illegal to include a deferred consumer output stream into a request |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1963 | auto iter = surfaceMap.find(streams.data.i32[i]); |
| 1964 | if (iter != surfaceMap.end()) { |
| 1965 | const std::vector<size_t>& surfaces = iter->second; |
| 1966 | for (const auto& surface : surfaces) { |
| 1967 | if (stream->isConsumerConfigurationDeferred(surface)) { |
| 1968 | CLOGE("Stream %d surface %zu hasn't finished configuration yet " |
| 1969 | "due to deferred consumer", stream->getId(), surface); |
| 1970 | return NULL; |
| 1971 | } |
| 1972 | } |
| 1973 | newRequest->mOutputSurfaces[i] = surfaces; |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1976 | // Lazy completion of stream configuration (allocation/registration) |
| 1977 | // on first use |
| 1978 | if (stream->isConfiguring()) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 1979 | res = stream->finishConfiguration(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1980 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1981 | SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", |
| 1982 | stream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1983 | return NULL; |
| 1984 | } |
| 1985 | } |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1986 | // Check if stream is being prepared |
| 1987 | if (stream->isPreparing()) { |
| 1988 | CLOGE("Request references an output stream that's being prepared!"); |
| 1989 | return NULL; |
| 1990 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1991 | |
| 1992 | newRequest->mOutputStreams.push(stream); |
| 1993 | } |
| 1994 | newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 1995 | newRequest->mBatchSize = 1; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1996 | |
| 1997 | return newRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1998 | } |
| 1999 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2000 | bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) { |
| 2001 | for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) { |
| 2002 | Size size = mSupportedOpaqueInputSizes[i]; |
| 2003 | if (size.width == width && size.height == height) { |
| 2004 | return true; |
| 2005 | } |
| 2006 | } |
| 2007 | |
| 2008 | return false; |
| 2009 | } |
| 2010 | |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2011 | void Camera3Device::cancelStreamsConfigurationLocked() { |
| 2012 | int res = OK; |
| 2013 | if (mInputStream != NULL && mInputStream->isConfiguring()) { |
| 2014 | res = mInputStream->cancelConfiguration(); |
| 2015 | if (res != OK) { |
| 2016 | CLOGE("Can't cancel configuring input stream %d: %s (%d)", |
| 2017 | mInputStream->getId(), strerror(-res), res); |
| 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
| 2022 | sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i); |
| 2023 | if (outputStream->isConfiguring()) { |
| 2024 | res = outputStream->cancelConfiguration(); |
| 2025 | if (res != OK) { |
| 2026 | CLOGE("Can't cancel configuring output stream %d: %s (%d)", |
| 2027 | outputStream->getId(), strerror(-res), res); |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | // Return state to that at start of call, so that future configures |
| 2033 | // properly clean things up |
| 2034 | internalUpdateStatusLocked(STATUS_UNCONFIGURED); |
| 2035 | mNeedConfig = true; |
| 2036 | } |
| 2037 | |
Eino-Ville Talvala | e7091aa | 2017-03-07 15:23:06 -0800 | [diff] [blame] | 2038 | status_t Camera3Device::configureStreamsLocked(int operatingMode) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2039 | ATRACE_CALL(); |
| 2040 | status_t res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2041 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2042 | if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2043 | CLOGE("Not idle"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2044 | return INVALID_OPERATION; |
| 2045 | } |
| 2046 | |
Eino-Ville Talvala | e7091aa | 2017-03-07 15:23:06 -0800 | [diff] [blame] | 2047 | if (operatingMode < 0) { |
| 2048 | CLOGE("Invalid operating mode: %d", operatingMode); |
| 2049 | return BAD_VALUE; |
| 2050 | } |
| 2051 | |
| 2052 | bool isConstrainedHighSpeed = |
| 2053 | static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) == |
| 2054 | operatingMode; |
| 2055 | |
| 2056 | if (mOperatingMode != operatingMode) { |
| 2057 | mNeedConfig = true; |
| 2058 | mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed; |
| 2059 | mOperatingMode = operatingMode; |
| 2060 | } |
| 2061 | |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 2062 | if (!mNeedConfig) { |
| 2063 | ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); |
| 2064 | return OK; |
| 2065 | } |
| 2066 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 2067 | // Workaround for device HALv3.2 or older spec bug - zero streams requires |
| 2068 | // adding a dummy stream instead. |
| 2069 | // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround. |
| 2070 | if (mOutputStreams.size() == 0) { |
| 2071 | addDummyStreamLocked(); |
| 2072 | } else { |
| 2073 | tryRemoveDummyStreamLocked(); |
| 2074 | } |
| 2075 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2076 | // Start configuring the streams |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2077 | ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2078 | |
| 2079 | camera3_stream_configuration config; |
Eino-Ville Talvala | bbbbe84 | 2017-02-28 17:50:56 -0800 | [diff] [blame] | 2080 | config.operation_mode = mOperatingMode; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2081 | config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); |
| 2082 | |
| 2083 | Vector<camera3_stream_t*> streams; |
| 2084 | streams.setCapacity(config.num_streams); |
| 2085 | |
| 2086 | if (mInputStream != NULL) { |
| 2087 | camera3_stream_t *inputStream; |
| 2088 | inputStream = mInputStream->startConfiguration(); |
| 2089 | if (inputStream == NULL) { |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2090 | CLOGE("Can't start input stream configuration"); |
| 2091 | cancelStreamsConfigurationLocked(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2092 | return INVALID_OPERATION; |
| 2093 | } |
| 2094 | streams.add(inputStream); |
| 2095 | } |
| 2096 | |
| 2097 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 2098 | |
| 2099 | // Don't configure bidi streams twice, nor add them twice to the list |
| 2100 | if (mOutputStreams[i].get() == |
| 2101 | static_cast<Camera3StreamInterface*>(mInputStream.get())) { |
| 2102 | |
| 2103 | config.num_streams--; |
| 2104 | continue; |
| 2105 | } |
| 2106 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2107 | camera3_stream_t *outputStream; |
| 2108 | outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); |
| 2109 | if (outputStream == NULL) { |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2110 | CLOGE("Can't start output stream configuration"); |
| 2111 | cancelStreamsConfigurationLocked(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2112 | return INVALID_OPERATION; |
| 2113 | } |
| 2114 | streams.add(outputStream); |
| 2115 | } |
| 2116 | |
| 2117 | config.streams = streams.editArray(); |
| 2118 | |
| 2119 | // Do the HAL configuration; will potentially touch stream |
| 2120 | // max_buffers, usage, priv fields. |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2121 | |
| 2122 | res = mInterface->configureStreams(&config); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2123 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2124 | if (res == BAD_VALUE) { |
| 2125 | // HAL rejected this set of streams as unsupported, clean up config |
| 2126 | // attempt and return to unconfigured state |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2127 | CLOGE("Set of requested inputs/outputs not supported by HAL"); |
| 2128 | cancelStreamsConfigurationLocked(); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2129 | return BAD_VALUE; |
| 2130 | } else if (res != OK) { |
| 2131 | // Some other kind of error from configure_streams - this is not |
| 2132 | // expected |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2133 | SET_ERR_L("Unable to configure streams with HAL: %s (%d)", |
| 2134 | strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2135 | return res; |
| 2136 | } |
| 2137 | |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 2138 | // Finish all stream configuration immediately. |
| 2139 | // TODO: Try to relax this later back to lazy completion, which should be |
| 2140 | // faster |
| 2141 | |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 2142 | if (mInputStream != NULL && mInputStream->isConfiguring()) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2143 | res = mInputStream->finishConfiguration(); |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 2144 | if (res != OK) { |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2145 | CLOGE("Can't finish configuring input stream %d: %s (%d)", |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 2146 | mInputStream->getId(), strerror(-res), res); |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2147 | cancelStreamsConfigurationLocked(); |
| 2148 | return BAD_VALUE; |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 2153 | sp<Camera3OutputStreamInterface> outputStream = |
| 2154 | mOutputStreams.editValueAt(i); |
Zhijun He | 5d677d1 | 2016-05-29 16:52:39 -0700 | [diff] [blame] | 2155 | if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2156 | res = outputStream->finishConfiguration(); |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 2157 | if (res != OK) { |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2158 | CLOGE("Can't finish configuring output stream %d: %s (%d)", |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 2159 | outputStream->getId(), strerror(-res), res); |
Chien-Yu Chen | 9b5860b | 2016-06-10 13:39:09 -0700 | [diff] [blame] | 2160 | cancelStreamsConfigurationLocked(); |
| 2161 | return BAD_VALUE; |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 2162 | } |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2166 | // Request thread needs to know to avoid using repeat-last-settings protocol |
| 2167 | // across configure_streams() calls |
Chien-Yu Chen | c66969b | 2016-05-19 16:37:51 -0700 | [diff] [blame] | 2168 | mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2169 | |
Zhijun He | 90f7c37 | 2016-08-16 16:19:43 -0700 | [diff] [blame] | 2170 | char value[PROPERTY_VALUE_MAX]; |
| 2171 | property_get("camera.fifo.disable", value, "0"); |
| 2172 | int32_t disableFifo = atoi(value); |
| 2173 | if (disableFifo != 1) { |
| 2174 | // Boost priority of request thread to SCHED_FIFO. |
| 2175 | pid_t requestThreadTid = mRequestThread->getTid(); |
| 2176 | res = requestPriority(getpid(), requestThreadTid, |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 2177 | kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false); |
Zhijun He | 90f7c37 | 2016-08-16 16:19:43 -0700 | [diff] [blame] | 2178 | if (res != OK) { |
| 2179 | ALOGW("Can't set realtime priority for request processing thread: %s (%d)", |
| 2180 | strerror(-res), res); |
| 2181 | } else { |
| 2182 | ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid); |
| 2183 | } |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2186 | // Update device state |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2187 | |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 2188 | mNeedConfig = false; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2189 | |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 2190 | internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ? |
| 2191 | STATUS_CONFIGURED : STATUS_UNCONFIGURED); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2192 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2193 | ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2194 | |
Zhijun He | 0a21051 | 2014-07-24 13:45:15 -0700 | [diff] [blame] | 2195 | // tear down the deleted streams after configure streams. |
| 2196 | mDeletedStreams.clear(); |
| 2197 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2198 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2199 | } |
| 2200 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 2201 | status_t Camera3Device::addDummyStreamLocked() { |
| 2202 | ATRACE_CALL(); |
| 2203 | status_t res; |
| 2204 | |
| 2205 | if (mDummyStreamId != NO_STREAM) { |
| 2206 | // Should never be adding a second dummy stream when one is already |
| 2207 | // active |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2208 | SET_ERR_L("%s: Camera %s: A dummy stream already exists!", |
| 2209 | __FUNCTION__, mId.string()); |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 2210 | return INVALID_OPERATION; |
| 2211 | } |
| 2212 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2213 | ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 2214 | |
| 2215 | sp<Camera3OutputStreamInterface> dummyStream = |
| 2216 | new Camera3DummyStream(mNextStreamId); |
| 2217 | |
| 2218 | res = mOutputStreams.add(mNextStreamId, dummyStream); |
| 2219 | if (res < 0) { |
| 2220 | SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res); |
| 2221 | return res; |
| 2222 | } |
| 2223 | |
| 2224 | mDummyStreamId = mNextStreamId; |
| 2225 | mNextStreamId++; |
| 2226 | |
| 2227 | return OK; |
| 2228 | } |
| 2229 | |
| 2230 | status_t Camera3Device::tryRemoveDummyStreamLocked() { |
| 2231 | ATRACE_CALL(); |
| 2232 | status_t res; |
| 2233 | |
| 2234 | if (mDummyStreamId == NO_STREAM) return OK; |
| 2235 | if (mOutputStreams.size() == 1) return OK; |
| 2236 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2237 | ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string()); |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 2238 | |
| 2239 | // Ok, have a dummy stream and there's at least one other output stream, |
| 2240 | // so remove the dummy |
| 2241 | |
| 2242 | sp<Camera3StreamInterface> deletedStream; |
| 2243 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId); |
| 2244 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 2245 | SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId); |
| 2246 | return INVALID_OPERATION; |
| 2247 | } |
| 2248 | |
| 2249 | deletedStream = mOutputStreams.editValueAt(outputStreamIdx); |
| 2250 | mOutputStreams.removeItemsAt(outputStreamIdx); |
| 2251 | |
| 2252 | // Free up the stream endpoint so that it can be used by some other stream |
| 2253 | res = deletedStream->disconnect(); |
| 2254 | if (res != OK) { |
| 2255 | SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId); |
| 2256 | // fall through since we want to still list the stream as deleted. |
| 2257 | } |
| 2258 | mDeletedStreams.add(deletedStream); |
| 2259 | mDummyStreamId = NO_STREAM; |
| 2260 | |
| 2261 | return res; |
| 2262 | } |
| 2263 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2264 | void Camera3Device::setErrorState(const char *fmt, ...) { |
| 2265 | Mutex::Autolock l(mLock); |
| 2266 | va_list args; |
| 2267 | va_start(args, fmt); |
| 2268 | |
| 2269 | setErrorStateLockedV(fmt, args); |
| 2270 | |
| 2271 | va_end(args); |
| 2272 | } |
| 2273 | |
| 2274 | void Camera3Device::setErrorStateV(const char *fmt, va_list args) { |
| 2275 | Mutex::Autolock l(mLock); |
| 2276 | setErrorStateLockedV(fmt, args); |
| 2277 | } |
| 2278 | |
| 2279 | void Camera3Device::setErrorStateLocked(const char *fmt, ...) { |
| 2280 | va_list args; |
| 2281 | va_start(args, fmt); |
| 2282 | |
| 2283 | setErrorStateLockedV(fmt, args); |
| 2284 | |
| 2285 | va_end(args); |
| 2286 | } |
| 2287 | |
| 2288 | void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2289 | // Print out all error messages to log |
| 2290 | String8 errorCause = String8::formatV(fmt, args); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2291 | ALOGE("Camera %s: %s", mId.string(), errorCause.string()); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2292 | |
| 2293 | // But only do error state transition steps for the first error |
Zhijun He | b05eeae | 2013-06-06 13:51:22 -0700 | [diff] [blame] | 2294 | if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2295 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2296 | mErrorCause = errorCause; |
| 2297 | |
| 2298 | mRequestThread->setPaused(true); |
Ruben Brunk | 183f056 | 2015-08-12 12:55:02 -0700 | [diff] [blame] | 2299 | internalUpdateStatusLocked(STATUS_ERROR); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2300 | |
| 2301 | // Notify upstream about a device error |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 2302 | sp<NotificationListener> listener = mListener.promote(); |
| 2303 | if (listener != NULL) { |
| 2304 | listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2305 | CaptureResultExtras()); |
| 2306 | } |
| 2307 | |
| 2308 | // Save stack trace. View by dumping it later. |
| 2309 | CameraTraces::saveTrace(); |
| 2310 | // TODO: consider adding errorCause and client pid/procname |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2311 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2312 | |
| 2313 | /** |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2314 | * In-flight request management |
| 2315 | */ |
| 2316 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2317 | status_t Camera3Device::registerInFlight(uint32_t frameNumber, |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2318 | int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput, |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2319 | bool hasAppCallback) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2320 | ATRACE_CALL(); |
| 2321 | Mutex::Autolock l(mInFlightLock); |
| 2322 | |
| 2323 | ssize_t res; |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2324 | res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput, |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2325 | hasAppCallback)); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2326 | if (res < 0) return res; |
| 2327 | |
Eino-Ville Talvala | 24b366e | 2016-07-21 12:53:07 -0700 | [diff] [blame] | 2328 | if (mInFlightMap.size() == 1) { |
| 2329 | mStatusTracker->markComponentActive(mInFlightStatusId); |
| 2330 | } |
| 2331 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2332 | return OK; |
| 2333 | } |
| 2334 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2335 | void Camera3Device::returnOutputBuffers( |
| 2336 | const camera3_stream_buffer_t *outputBuffers, size_t numBuffers, |
| 2337 | nsecs_t timestamp) { |
| 2338 | for (size_t i = 0; i < numBuffers; i++) |
| 2339 | { |
| 2340 | Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream); |
| 2341 | status_t res = stream->returnBuffer(outputBuffers[i], timestamp); |
| 2342 | // Note: stream may be deallocated at this point, if this buffer was |
| 2343 | // the last reference to it. |
| 2344 | if (res != OK) { |
| 2345 | ALOGE("Can't return buffer to its stream: %s (%d)", |
| 2346 | strerror(-res), res); |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | |
Shuzhen Wang | cadb330 | 2016-11-04 14:17:56 -0700 | [diff] [blame] | 2351 | void Camera3Device::removeInFlightMapEntryLocked(int idx) { |
| 2352 | mInFlightMap.removeItemsAt(idx, 1); |
| 2353 | |
| 2354 | // Indicate idle inFlightMap to the status tracker |
| 2355 | if (mInFlightMap.size() == 0) { |
| 2356 | mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE); |
| 2357 | } |
| 2358 | } |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2359 | |
| 2360 | void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) { |
| 2361 | |
| 2362 | const InFlightRequest &request = mInFlightMap.valueAt(idx); |
| 2363 | const uint32_t frameNumber = mInFlightMap.keyAt(idx); |
| 2364 | |
| 2365 | nsecs_t sensorTimestamp = request.sensorTimestamp; |
| 2366 | nsecs_t shutterTimestamp = request.shutterTimestamp; |
| 2367 | |
| 2368 | // Check if it's okay to remove the request from InFlightMap: |
| 2369 | // In the case of a successful request: |
| 2370 | // all input and output buffers, all result metadata, shutter callback |
| 2371 | // arrived. |
| 2372 | // In the case of a unsuccessful request: |
| 2373 | // all input and output buffers arrived. |
| 2374 | if (request.numBuffersLeft == 0 && |
| 2375 | (request.requestStatus != OK || |
| 2376 | (request.haveResultMetadata && shutterTimestamp != 0))) { |
| 2377 | ATRACE_ASYNC_END("frame capture", frameNumber); |
| 2378 | |
Shuzhen Wang | 403044a | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2379 | // Sanity check - if sensor timestamp matches shutter timestamp in the |
| 2380 | // case of request having callback. |
| 2381 | if (request.hasCallback && request.requestStatus == OK && |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2382 | sensorTimestamp != shutterTimestamp) { |
| 2383 | SET_ERR("sensor timestamp (%" PRId64 |
| 2384 | ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", |
| 2385 | sensorTimestamp, frameNumber, shutterTimestamp); |
| 2386 | } |
| 2387 | |
| 2388 | // for an unsuccessful request, it may have pending output buffers to |
| 2389 | // return. |
| 2390 | assert(request.requestStatus != OK || |
| 2391 | request.pendingOutputBuffers.size() == 0); |
| 2392 | returnOutputBuffers(request.pendingOutputBuffers.array(), |
| 2393 | request.pendingOutputBuffers.size(), 0); |
| 2394 | |
Shuzhen Wang | cadb330 | 2016-11-04 14:17:56 -0700 | [diff] [blame] | 2395 | removeInFlightMapEntryLocked(idx); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2396 | ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); |
| 2397 | } |
| 2398 | |
| 2399 | // Sanity check - if we have too many in-flight frames, something has |
| 2400 | // likely gone wrong |
Chien-Yu Chen | c96ac8d | 2015-08-12 16:46:24 -0700 | [diff] [blame] | 2401 | if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2402 | CLOGE("In-flight list too large: %zu", mInFlightMap.size()); |
Chien-Yu Chen | c96ac8d | 2015-08-12 16:46:24 -0700 | [diff] [blame] | 2403 | } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > |
| 2404 | kInFlightWarnLimitHighSpeed) { |
| 2405 | CLOGE("In-flight list too large for high speed configuration: %zu", |
| 2406 | mInFlightMap.size()); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2407 | } |
| 2408 | } |
| 2409 | |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2410 | void Camera3Device::insertResultLocked(CaptureResult *result, |
| 2411 | uint32_t frameNumber) { |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2412 | if (result == nullptr) return; |
| 2413 | |
Emilian Peev | 71c73a2 | 2017-03-21 16:35:51 +0000 | [diff] [blame] | 2414 | camera_metadata_t *meta = const_cast<camera_metadata_t *>( |
| 2415 | result->mMetadata.getAndLock()); |
| 2416 | set_camera_metadata_vendor_id(meta, mVendorTagId); |
| 2417 | result->mMetadata.unlock(meta); |
| 2418 | |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2419 | if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, |
| 2420 | (int32_t*)&frameNumber, 1) != OK) { |
| 2421 | SET_ERR("Failed to set frame number %d in metadata", frameNumber); |
| 2422 | return; |
| 2423 | } |
| 2424 | |
| 2425 | if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) { |
| 2426 | SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber); |
| 2427 | return; |
| 2428 | } |
| 2429 | |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2430 | // Valid result, insert into queue |
| 2431 | List<CaptureResult>::iterator queuedResult = |
| 2432 | mResultQueue.insert(mResultQueue.end(), CaptureResult(*result)); |
| 2433 | ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 |
| 2434 | ", burstId = %" PRId32, __FUNCTION__, |
| 2435 | queuedResult->mResultExtras.requestId, |
| 2436 | queuedResult->mResultExtras.frameNumber, |
| 2437 | queuedResult->mResultExtras.burstId); |
| 2438 | |
| 2439 | mResultSignal.signal(); |
| 2440 | } |
| 2441 | |
| 2442 | |
| 2443 | void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult, |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2444 | const CaptureResultExtras &resultExtras, uint32_t frameNumber) { |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2445 | Mutex::Autolock l(mOutputLock); |
| 2446 | |
| 2447 | CaptureResult captureResult; |
| 2448 | captureResult.mResultExtras = resultExtras; |
| 2449 | captureResult.mMetadata = partialResult; |
| 2450 | |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2451 | insertResultLocked(&captureResult, frameNumber); |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2452 | } |
| 2453 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2454 | |
| 2455 | void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata, |
| 2456 | CaptureResultExtras &resultExtras, |
| 2457 | CameraMetadata &collectedPartialResult, |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2458 | uint32_t frameNumber, |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2459 | bool reprocess) { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2460 | if (pendingMetadata.isEmpty()) |
| 2461 | return; |
| 2462 | |
| 2463 | Mutex::Autolock l(mOutputLock); |
| 2464 | |
| 2465 | // TODO: need to track errors for tighter bounds on expected frame number |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2466 | if (reprocess) { |
| 2467 | if (frameNumber < mNextReprocessResultFrameNumber) { |
| 2468 | SET_ERR("Out-of-order reprocess capture result metadata submitted! " |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2469 | "(got frame number %d, expecting %d)", |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2470 | frameNumber, mNextReprocessResultFrameNumber); |
| 2471 | return; |
| 2472 | } |
| 2473 | mNextReprocessResultFrameNumber = frameNumber + 1; |
| 2474 | } else { |
| 2475 | if (frameNumber < mNextResultFrameNumber) { |
| 2476 | SET_ERR("Out-of-order capture result metadata submitted! " |
| 2477 | "(got frame number %d, expecting %d)", |
| 2478 | frameNumber, mNextResultFrameNumber); |
| 2479 | return; |
| 2480 | } |
| 2481 | mNextResultFrameNumber = frameNumber + 1; |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2482 | } |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2483 | |
| 2484 | CaptureResult captureResult; |
| 2485 | captureResult.mResultExtras = resultExtras; |
| 2486 | captureResult.mMetadata = pendingMetadata; |
| 2487 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2488 | // Append any previous partials to form a complete result |
| 2489 | if (mUsePartialResult && !collectedPartialResult.isEmpty()) { |
| 2490 | captureResult.mMetadata.append(collectedPartialResult); |
| 2491 | } |
| 2492 | |
| 2493 | captureResult.mMetadata.sort(); |
| 2494 | |
| 2495 | // Check that there's a timestamp in the result metadata |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 2496 | camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); |
| 2497 | if (timestamp.count == 0) { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2498 | SET_ERR("No timestamp provided by HAL for frame %d!", |
| 2499 | frameNumber); |
| 2500 | return; |
| 2501 | } |
| 2502 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 2503 | mTagMonitor.monitorMetadata(TagMonitor::RESULT, |
| 2504 | frameNumber, timestamp.data.i64[0], captureResult.mMetadata); |
| 2505 | |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2506 | insertResultLocked(&captureResult, frameNumber); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2507 | } |
| 2508 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2509 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2510 | * Camera HAL device callback methods |
| 2511 | */ |
| 2512 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2513 | void Camera3Device::processCaptureResult(const camera3_capture_result *result) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2514 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2515 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2516 | status_t res; |
| 2517 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2518 | uint32_t frameNumber = result->frame_number; |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2519 | if (result->result == NULL && result->num_output_buffers == 0 && |
| 2520 | result->input_buffer == NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2521 | SET_ERR("No result data provided by HAL for frame %d", |
| 2522 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2523 | return; |
| 2524 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2525 | |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2526 | if (!mUsePartialResult && |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2527 | result->result != NULL && |
| 2528 | result->partial_result != 1) { |
| 2529 | SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" |
| 2530 | " if partial result is not supported", |
| 2531 | frameNumber, result->partial_result); |
| 2532 | return; |
| 2533 | } |
| 2534 | |
| 2535 | bool isPartialResult = false; |
| 2536 | CameraMetadata collectedPartialResult; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2537 | CaptureResultExtras resultExtras; |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2538 | bool hasInputBufferInRequest = false; |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2539 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2540 | // Get shutter timestamp and resultExtras from list of in-flight requests, |
| 2541 | // where it was added by the shutter notification for this frame. If the |
| 2542 | // shutter timestamp isn't received yet, append the output buffers to the |
| 2543 | // in-flight request and they will be returned when the shutter timestamp |
| 2544 | // arrives. Update the in-flight status and remove the in-flight entry if |
| 2545 | // all result data and shutter timestamp have been received. |
| 2546 | nsecs_t shutterTimestamp = 0; |
| 2547 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2548 | { |
| 2549 | Mutex::Autolock l(mInFlightLock); |
| 2550 | ssize_t idx = mInFlightMap.indexOfKey(frameNumber); |
| 2551 | if (idx == NAME_NOT_FOUND) { |
| 2552 | SET_ERR("Unknown frame number for capture result: %d", |
| 2553 | frameNumber); |
| 2554 | return; |
| 2555 | } |
| 2556 | InFlightRequest &request = mInFlightMap.editValueAt(idx); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2557 | ALOGVV("%s: got InFlightRequest requestId = %" PRId32 |
| 2558 | ", frameNumber = %" PRId64 ", burstId = %" PRId32 |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2559 | ", partialResultCount = %d, hasCallback = %d", |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2560 | __FUNCTION__, request.resultExtras.requestId, |
| 2561 | request.resultExtras.frameNumber, request.resultExtras.burstId, |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2562 | result->partial_result, request.hasCallback); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2563 | // Always update the partial count to the latest one if it's not 0 |
| 2564 | // (buffers only). When framework aggregates adjacent partial results |
| 2565 | // into one, the latest partial count will be used. |
| 2566 | if (result->partial_result != 0) |
| 2567 | request.resultExtras.partialResultCount = result->partial_result; |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2568 | |
| 2569 | // Check if this result carries only partial metadata |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2570 | if (mUsePartialResult && result->result != NULL) { |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 2571 | if (result->partial_result > mNumPartialResults || result->partial_result < 1) { |
| 2572 | SET_ERR("Result is malformed for frame %d: partial_result %u must be in" |
| 2573 | " the range of [1, %d] when metadata is included in the result", |
| 2574 | frameNumber, result->partial_result, mNumPartialResults); |
| 2575 | return; |
| 2576 | } |
| 2577 | isPartialResult = (result->partial_result < mNumPartialResults); |
| 2578 | if (isPartialResult) { |
| 2579 | request.collectedPartialResult.append(result->result); |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2582 | if (isPartialResult && request.hasCallback) { |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2583 | // Send partial capture result |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2584 | sendPartialCaptureResult(result->result, request.resultExtras, |
| 2585 | frameNumber); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2589 | shutterTimestamp = request.shutterTimestamp; |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2590 | hasInputBufferInRequest = request.hasInputBuffer; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2591 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2592 | // Did we get the (final) result metadata for this capture? |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2593 | if (result->result != NULL && !isPartialResult) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2594 | if (request.haveResultMetadata) { |
| 2595 | SET_ERR("Called multiple times with metadata for frame %d", |
| 2596 | frameNumber); |
| 2597 | return; |
| 2598 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2599 | if (mUsePartialResult && |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2600 | !request.collectedPartialResult.isEmpty()) { |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2601 | collectedPartialResult.acquire( |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2602 | request.collectedPartialResult); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2603 | } |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2604 | request.haveResultMetadata = true; |
| 2605 | } |
| 2606 | |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2607 | uint32_t numBuffersReturned = result->num_output_buffers; |
| 2608 | if (result->input_buffer != NULL) { |
| 2609 | if (hasInputBufferInRequest) { |
| 2610 | numBuffersReturned += 1; |
| 2611 | } else { |
| 2612 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 2613 | " buffer sent in the request", |
| 2614 | __FUNCTION__); |
| 2615 | } |
| 2616 | } |
| 2617 | request.numBuffersLeft -= numBuffersReturned; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2618 | if (request.numBuffersLeft < 0) { |
| 2619 | SET_ERR("Too many buffers returned for frame %d", |
| 2620 | frameNumber); |
| 2621 | return; |
| 2622 | } |
| 2623 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2624 | camera_metadata_ro_entry_t entry; |
| 2625 | res = find_camera_metadata_ro_entry(result->result, |
| 2626 | ANDROID_SENSOR_TIMESTAMP, &entry); |
| 2627 | if (res == OK && entry.count == 1) { |
| 2628 | request.sensorTimestamp = entry.data.i64[0]; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2629 | } |
| 2630 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2631 | // If shutter event isn't received yet, append the output buffers to |
| 2632 | // the in-flight request. Otherwise, return the output buffers to |
| 2633 | // streams. |
| 2634 | if (shutterTimestamp == 0) { |
| 2635 | request.pendingOutputBuffers.appendArray(result->output_buffers, |
| 2636 | result->num_output_buffers); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2637 | } else { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2638 | returnOutputBuffers(result->output_buffers, |
| 2639 | result->num_output_buffers, shutterTimestamp); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2640 | } |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2641 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2642 | if (result->result != NULL && !isPartialResult) { |
| 2643 | if (shutterTimestamp == 0) { |
| 2644 | request.pendingMetadata = result->result; |
Chien-Yu Chen | 5cd8d64 | 2016-03-08 14:46:58 -0800 | [diff] [blame] | 2645 | request.collectedPartialResult = collectedPartialResult; |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2646 | } else if (request.hasCallback) { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2647 | CameraMetadata metadata; |
| 2648 | metadata = result->result; |
| 2649 | sendCaptureResult(metadata, request.resultExtras, |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2650 | collectedPartialResult, frameNumber, |
| 2651 | hasInputBufferInRequest); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2652 | } |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2653 | } |
| 2654 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2655 | removeInFlightRequestIfReadyLocked(idx); |
| 2656 | } // scope for mInFlightLock |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2657 | |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2658 | if (result->input_buffer != NULL) { |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2659 | if (hasInputBufferInRequest) { |
| 2660 | Camera3Stream *stream = |
| 2661 | Camera3Stream::cast(result->input_buffer->stream); |
| 2662 | res = stream->returnInputBuffer(*(result->input_buffer)); |
| 2663 | // Note: stream may be deallocated at this point, if this buffer was the |
| 2664 | // last reference to it. |
| 2665 | if (res != OK) { |
| 2666 | ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" |
| 2667 | " its stream:%s (%d)", __FUNCTION__, |
| 2668 | frameNumber, strerror(-res), res); |
Zhijun He | 0ea8fa4 | 2014-07-07 17:05:38 -0700 | [diff] [blame] | 2669 | } |
| 2670 | } else { |
| 2671 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 2672 | " buffer sent in the request, skipping input buffer return.", |
| 2673 | __FUNCTION__); |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2674 | } |
| 2675 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | void Camera3Device::notify(const camera3_notify_msg *msg) { |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2679 | ATRACE_CALL(); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 2680 | sp<NotificationListener> listener; |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2681 | { |
| 2682 | Mutex::Autolock l(mOutputLock); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 2683 | listener = mListener.promote(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2684 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2685 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2686 | if (msg == NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2687 | SET_ERR("HAL sent NULL notify message!"); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2688 | return; |
| 2689 | } |
| 2690 | |
| 2691 | switch (msg->type) { |
| 2692 | case CAMERA3_MSG_ERROR: { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2693 | notifyError(msg->message.error, listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2694 | break; |
| 2695 | } |
| 2696 | case CAMERA3_MSG_SHUTTER: { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2697 | notifyShutter(msg->message.shutter, listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2698 | break; |
| 2699 | } |
| 2700 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2701 | SET_ERR("Unknown notify message from HAL: %d", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2702 | msg->type); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2703 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2704 | } |
| 2705 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2706 | void Camera3Device::notifyError(const camera3_error_msg_t &msg, |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 2707 | sp<NotificationListener> listener) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2708 | |
| 2709 | // Map camera HAL error codes to ICameraDeviceCallback error codes |
| 2710 | // Index into this with the HAL error code |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2711 | static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2712 | // 0 = Unused error code |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2713 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2714 | // 1 = CAMERA3_MSG_ERROR_DEVICE |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2715 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2716 | // 2 = CAMERA3_MSG_ERROR_REQUEST |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2717 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2718 | // 3 = CAMERA3_MSG_ERROR_RESULT |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2719 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2720 | // 4 = CAMERA3_MSG_ERROR_BUFFER |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2721 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2722 | }; |
| 2723 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2724 | int32_t errorCode = |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2725 | ((msg.error_code >= 0) && |
| 2726 | (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ? |
| 2727 | halErrorMap[msg.error_code] : |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2728 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2729 | |
| 2730 | int streamId = 0; |
| 2731 | if (msg.error_stream != NULL) { |
| 2732 | Camera3Stream *stream = |
| 2733 | Camera3Stream::cast(msg.error_stream); |
| 2734 | streamId = stream->getId(); |
| 2735 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2736 | ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d", |
| 2737 | mId.string(), __FUNCTION__, msg.frame_number, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2738 | streamId, msg.error_code); |
| 2739 | |
| 2740 | CaptureResultExtras resultExtras; |
| 2741 | switch (errorCode) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2742 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2743 | // SET_ERR calls notifyError |
| 2744 | SET_ERR("Camera HAL reported serious device error"); |
| 2745 | break; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 2746 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: |
| 2747 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: |
| 2748 | case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2749 | { |
| 2750 | Mutex::Autolock l(mInFlightLock); |
| 2751 | ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number); |
| 2752 | if (idx >= 0) { |
| 2753 | InFlightRequest &r = mInFlightMap.editValueAt(idx); |
| 2754 | r.requestStatus = msg.error_code; |
| 2755 | resultExtras = r.resultExtras; |
Emilian Peev | ba0fac3 | 2017-03-30 09:05:34 +0100 | [diff] [blame] | 2756 | if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == |
| 2757 | errorCode) { |
| 2758 | // In case of missing result check whether the buffers |
| 2759 | // returned. If they returned, then remove inflight |
| 2760 | // request. |
| 2761 | removeInFlightRequestIfReadyLocked(idx); |
| 2762 | } |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2763 | } else { |
| 2764 | resultExtras.frameNumber = msg.frame_number; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2765 | ALOGE("Camera %s: %s: cannot find in-flight request on " |
| 2766 | "frame %" PRId64 " error", mId.string(), __FUNCTION__, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2767 | resultExtras.frameNumber); |
| 2768 | } |
| 2769 | } |
Eino-Ville Talvala | e95bb63 | 2016-03-06 19:55:44 -0800 | [diff] [blame] | 2770 | resultExtras.errorStreamId = streamId; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2771 | if (listener != NULL) { |
| 2772 | listener->notifyError(errorCode, resultExtras); |
| 2773 | } else { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2774 | ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2775 | } |
| 2776 | break; |
| 2777 | default: |
| 2778 | // SET_ERR calls notifyError |
| 2779 | SET_ERR("Unknown error message from HAL: %d", msg.error_code); |
| 2780 | break; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg, |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 2785 | sp<NotificationListener> listener) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2786 | ssize_t idx; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2787 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2788 | // Set timestamp for the request in the in-flight tracking |
| 2789 | // and get the request ID to send upstream |
| 2790 | { |
| 2791 | Mutex::Autolock l(mInFlightLock); |
| 2792 | idx = mInFlightMap.indexOfKey(msg.frame_number); |
| 2793 | if (idx >= 0) { |
| 2794 | InFlightRequest &r = mInFlightMap.editValueAt(idx); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2795 | |
Chien-Yu Chen | 3df11ce | 2015-09-30 14:13:30 -0700 | [diff] [blame] | 2796 | // Verify ordering of shutter notifications |
| 2797 | { |
| 2798 | Mutex::Autolock l(mOutputLock); |
| 2799 | // TODO: need to track errors for tighter bounds on expected frame number. |
| 2800 | if (r.hasInputBuffer) { |
| 2801 | if (msg.frame_number < mNextReprocessShutterFrameNumber) { |
| 2802 | SET_ERR("Shutter notification out-of-order. Expected " |
| 2803 | "notification for frame %d, got frame %d", |
| 2804 | mNextReprocessShutterFrameNumber, msg.frame_number); |
| 2805 | return; |
| 2806 | } |
| 2807 | mNextReprocessShutterFrameNumber = msg.frame_number + 1; |
| 2808 | } else { |
| 2809 | if (msg.frame_number < mNextShutterFrameNumber) { |
| 2810 | SET_ERR("Shutter notification out-of-order. Expected " |
| 2811 | "notification for frame %d, got frame %d", |
| 2812 | mNextShutterFrameNumber, msg.frame_number); |
| 2813 | return; |
| 2814 | } |
| 2815 | mNextShutterFrameNumber = msg.frame_number + 1; |
| 2816 | } |
| 2817 | } |
| 2818 | |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2819 | r.shutterTimestamp = msg.timestamp; |
| 2820 | if (r.hasCallback) { |
| 2821 | ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64, |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2822 | mId.string(), __FUNCTION__, |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2823 | msg.frame_number, r.resultExtras.requestId, msg.timestamp); |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 2824 | // Call listener, if any |
| 2825 | if (listener != NULL) { |
| 2826 | listener->notifyShutter(r.resultExtras, msg.timestamp); |
| 2827 | } |
| 2828 | // send pending result and buffers |
| 2829 | sendCaptureResult(r.pendingMetadata, r.resultExtras, |
| 2830 | r.collectedPartialResult, msg.frame_number, |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 2831 | r.hasInputBuffer); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2832 | } |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2833 | returnOutputBuffers(r.pendingOutputBuffers.array(), |
| 2834 | r.pendingOutputBuffers.size(), r.shutterTimestamp); |
| 2835 | r.pendingOutputBuffers.clear(); |
| 2836 | |
| 2837 | removeInFlightRequestIfReadyLocked(idx); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2838 | } |
| 2839 | } |
| 2840 | if (idx < 0) { |
| 2841 | SET_ERR("Shutter notification for non-existent frame number %d", |
| 2842 | msg.frame_number); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2847 | CameraMetadata Camera3Device::getLatestRequestLocked() { |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2848 | ALOGV("%s", __FUNCTION__); |
| 2849 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2850 | CameraMetadata retVal; |
| 2851 | |
| 2852 | if (mRequestThread != NULL) { |
| 2853 | retVal = mRequestThread->getLatestRequest(); |
| 2854 | } |
| 2855 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2856 | return retVal; |
| 2857 | } |
| 2858 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2859 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 2860 | void Camera3Device::monitorMetadata(TagMonitor::eventSource source, |
| 2861 | int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) { |
| 2862 | mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata); |
| 2863 | } |
| 2864 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2865 | /** |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2866 | * HalInterface inner class methods |
| 2867 | */ |
| 2868 | |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 2869 | Camera3Device::HalInterface::HalInterface( |
| 2870 | sp<ICameraDeviceSession> &session, |
| 2871 | std::shared_ptr<RequestMetadataQueue> queue) : |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2872 | mHal3Device(nullptr), |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 2873 | mHidlSession(session), |
| 2874 | mRequestMetadataQueue(queue) {} |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2875 | |
| 2876 | Camera3Device::HalInterface::HalInterface() : |
| 2877 | mHal3Device(nullptr) {} |
| 2878 | |
| 2879 | Camera3Device::HalInterface::HalInterface(const HalInterface& other) : |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 2880 | mHal3Device(other.mHal3Device), |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 2881 | mHidlSession(other.mHidlSession), |
| 2882 | mRequestMetadataQueue(other.mRequestMetadataQueue) {} |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2883 | |
| 2884 | bool Camera3Device::HalInterface::valid() { |
| 2885 | return (mHal3Device != nullptr) || (mHidlSession != nullptr); |
| 2886 | } |
| 2887 | |
| 2888 | void Camera3Device::HalInterface::clear() { |
| 2889 | mHal3Device = nullptr; |
| 2890 | mHidlSession.clear(); |
| 2891 | } |
| 2892 | |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 2893 | bool Camera3Device::HalInterface::supportBatchRequest() { |
| 2894 | return mHidlSession != nullptr; |
| 2895 | } |
| 2896 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2897 | status_t Camera3Device::HalInterface::constructDefaultRequestSettings( |
| 2898 | camera3_request_template_t templateId, |
| 2899 | /*out*/ camera_metadata_t **requestTemplate) { |
| 2900 | ATRACE_NAME("CameraHal::constructDefaultRequestSettings"); |
| 2901 | if (!valid()) return INVALID_OPERATION; |
| 2902 | status_t res = OK; |
| 2903 | |
| 2904 | if (mHal3Device != nullptr) { |
| 2905 | const camera_metadata *r; |
| 2906 | r = mHal3Device->ops->construct_default_request_settings( |
| 2907 | mHal3Device, templateId); |
| 2908 | if (r == nullptr) return BAD_VALUE; |
| 2909 | *requestTemplate = clone_camera_metadata(r); |
| 2910 | if (requestTemplate == nullptr) { |
| 2911 | ALOGE("%s: Unable to clone camera metadata received from HAL", |
| 2912 | __FUNCTION__); |
| 2913 | return INVALID_OPERATION; |
| 2914 | } |
| 2915 | } else { |
| 2916 | common::V1_0::Status status; |
| 2917 | RequestTemplate id; |
| 2918 | switch (templateId) { |
| 2919 | case CAMERA3_TEMPLATE_PREVIEW: |
| 2920 | id = RequestTemplate::PREVIEW; |
| 2921 | break; |
| 2922 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
| 2923 | id = RequestTemplate::STILL_CAPTURE; |
| 2924 | break; |
| 2925 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 2926 | id = RequestTemplate::VIDEO_RECORD; |
| 2927 | break; |
| 2928 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 2929 | id = RequestTemplate::VIDEO_SNAPSHOT; |
| 2930 | break; |
| 2931 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: |
| 2932 | id = RequestTemplate::ZERO_SHUTTER_LAG; |
| 2933 | break; |
| 2934 | case CAMERA3_TEMPLATE_MANUAL: |
| 2935 | id = RequestTemplate::MANUAL; |
| 2936 | break; |
| 2937 | default: |
| 2938 | // Unknown template ID |
| 2939 | return BAD_VALUE; |
| 2940 | } |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 2941 | auto err = mHidlSession->constructDefaultRequestSettings(id, |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2942 | [&status, &requestTemplate] |
| 2943 | (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) { |
| 2944 | status = s; |
| 2945 | if (status == common::V1_0::Status::OK) { |
| 2946 | const camera_metadata *r = |
| 2947 | reinterpret_cast<const camera_metadata_t*>(request.data()); |
| 2948 | size_t expectedSize = request.size(); |
| 2949 | int ret = validate_camera_metadata_structure(r, &expectedSize); |
Yin-Chia Yeh | 238ef5f | 2017-04-18 15:01:15 -0700 | [diff] [blame] | 2950 | if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2951 | *requestTemplate = clone_camera_metadata(r); |
| 2952 | if (*requestTemplate == nullptr) { |
| 2953 | ALOGE("%s: Unable to clone camera metadata received from HAL", |
| 2954 | __FUNCTION__); |
| 2955 | status = common::V1_0::Status::INTERNAL_ERROR; |
| 2956 | } |
| 2957 | } else { |
| 2958 | ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__); |
| 2959 | status = common::V1_0::Status::INTERNAL_ERROR; |
| 2960 | } |
| 2961 | } |
| 2962 | }); |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 2963 | if (!err.isOk()) { |
| 2964 | ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); |
| 2965 | res = DEAD_OBJECT; |
| 2966 | } else { |
| 2967 | res = CameraProviderManager::mapToStatusT(status); |
| 2968 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2969 | } |
| 2970 | return res; |
| 2971 | } |
| 2972 | |
| 2973 | status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) { |
| 2974 | ATRACE_NAME("CameraHal::configureStreams"); |
| 2975 | if (!valid()) return INVALID_OPERATION; |
| 2976 | status_t res = OK; |
| 2977 | |
| 2978 | if (mHal3Device != nullptr) { |
| 2979 | res = mHal3Device->ops->configure_streams(mHal3Device, config); |
| 2980 | } else { |
| 2981 | // Convert stream config to HIDL |
Yin-Chia Yeh | 7732705 | 2017-01-09 18:23:07 -0800 | [diff] [blame] | 2982 | std::set<int> activeStreams; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2983 | StreamConfiguration requestedConfiguration; |
| 2984 | requestedConfiguration.streams.resize(config->num_streams); |
| 2985 | for (size_t i = 0; i < config->num_streams; i++) { |
| 2986 | Stream &dst = requestedConfiguration.streams[i]; |
| 2987 | camera3_stream_t *src = config->streams[i]; |
| 2988 | |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 2989 | Camera3Stream* cam3stream = Camera3Stream::cast(src); |
| 2990 | cam3stream->setBufferFreedListener(this); |
| 2991 | int streamId = cam3stream->getId(); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 2992 | StreamType streamType; |
| 2993 | switch (src->stream_type) { |
| 2994 | case CAMERA3_STREAM_OUTPUT: |
| 2995 | streamType = StreamType::OUTPUT; |
| 2996 | break; |
| 2997 | case CAMERA3_STREAM_INPUT: |
| 2998 | streamType = StreamType::INPUT; |
| 2999 | break; |
| 3000 | default: |
| 3001 | ALOGE("%s: Stream %d: Unsupported stream type %d", |
| 3002 | __FUNCTION__, streamId, config->streams[i]->stream_type); |
| 3003 | return BAD_VALUE; |
| 3004 | } |
| 3005 | dst.id = streamId; |
| 3006 | dst.streamType = streamType; |
| 3007 | dst.width = src->width; |
| 3008 | dst.height = src->height; |
| 3009 | dst.format = mapToPixelFormat(src->format); |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 3010 | dst.usage = mapToConsumerUsage(src->usage); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3011 | dst.dataSpace = mapToHidlDataspace(src->data_space); |
| 3012 | dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation); |
Yin-Chia Yeh | 7732705 | 2017-01-09 18:23:07 -0800 | [diff] [blame] | 3013 | |
| 3014 | activeStreams.insert(streamId); |
| 3015 | // Create Buffer ID map if necessary |
| 3016 | if (mBufferIdMaps.count(streamId) == 0) { |
| 3017 | mBufferIdMaps.emplace(streamId, BufferIdMap{}); |
| 3018 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3019 | } |
Yin-Chia Yeh | 7732705 | 2017-01-09 18:23:07 -0800 | [diff] [blame] | 3020 | // remove BufferIdMap for deleted streams |
| 3021 | for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) { |
| 3022 | int streamId = it->first; |
| 3023 | bool active = activeStreams.count(streamId) > 0; |
| 3024 | if (!active) { |
| 3025 | it = mBufferIdMaps.erase(it); |
| 3026 | } else { |
| 3027 | ++it; |
| 3028 | } |
| 3029 | } |
| 3030 | |
Eino-Ville Talvala | bbbbe84 | 2017-02-28 17:50:56 -0800 | [diff] [blame] | 3031 | res = mapToStreamConfigurationMode( |
| 3032 | (camera3_stream_configuration_mode_t) config->operation_mode, |
| 3033 | /*out*/ &requestedConfiguration.operationMode); |
| 3034 | if (res != OK) { |
| 3035 | return res; |
| 3036 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3037 | |
| 3038 | // Invoke configureStreams |
| 3039 | |
| 3040 | HalStreamConfiguration finalConfiguration; |
| 3041 | common::V1_0::Status status; |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 3042 | auto err = mHidlSession->configureStreams(requestedConfiguration, |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3043 | [&status, &finalConfiguration] |
| 3044 | (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) { |
| 3045 | finalConfiguration = halConfiguration; |
| 3046 | status = s; |
| 3047 | }); |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 3048 | if (!err.isOk()) { |
| 3049 | ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); |
| 3050 | return DEAD_OBJECT; |
| 3051 | } |
| 3052 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3053 | if (status != common::V1_0::Status::OK ) { |
| 3054 | return CameraProviderManager::mapToStatusT(status); |
| 3055 | } |
| 3056 | |
| 3057 | // And convert output stream configuration from HIDL |
| 3058 | |
| 3059 | for (size_t i = 0; i < config->num_streams; i++) { |
| 3060 | camera3_stream_t *dst = config->streams[i]; |
| 3061 | int streamId = Camera3Stream::cast(dst)->getId(); |
| 3062 | |
| 3063 | // Start scan at i, with the assumption that the stream order matches |
| 3064 | size_t realIdx = i; |
| 3065 | bool found = false; |
| 3066 | for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) { |
| 3067 | if (finalConfiguration.streams[realIdx].id == streamId) { |
| 3068 | found = true; |
| 3069 | break; |
| 3070 | } |
| 3071 | realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1; |
| 3072 | } |
| 3073 | if (!found) { |
| 3074 | ALOGE("%s: Stream %d not found in stream configuration response from HAL", |
| 3075 | __FUNCTION__, streamId); |
| 3076 | return INVALID_OPERATION; |
| 3077 | } |
| 3078 | HalStream &src = finalConfiguration.streams[realIdx]; |
| 3079 | |
| 3080 | int overrideFormat = mapToFrameworkFormat(src.overrideFormat); |
| 3081 | if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 3082 | if (dst->format != overrideFormat) { |
| 3083 | ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__, |
| 3084 | streamId, dst->format); |
| 3085 | } |
| 3086 | } else { |
| 3087 | // Override allowed with IMPLEMENTATION_DEFINED |
| 3088 | dst->format = overrideFormat; |
| 3089 | } |
| 3090 | |
| 3091 | if (dst->stream_type == CAMERA3_STREAM_INPUT) { |
| 3092 | if (src.producerUsage != 0) { |
| 3093 | ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage", |
| 3094 | __FUNCTION__, streamId); |
| 3095 | return INVALID_OPERATION; |
| 3096 | } |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 3097 | dst->usage = mapConsumerToFrameworkUsage(src.consumerUsage); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3098 | } else { |
| 3099 | // OUTPUT |
| 3100 | if (src.consumerUsage != 0) { |
| 3101 | ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage", |
| 3102 | __FUNCTION__, streamId); |
| 3103 | return INVALID_OPERATION; |
| 3104 | } |
Yin-Chia Yeh | 47cf8e6 | 2017-04-04 13:00:03 -0700 | [diff] [blame] | 3105 | dst->usage = mapProducerToFrameworkUsage(src.producerUsage); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3106 | } |
| 3107 | dst->max_buffers = src.maxBuffers; |
| 3108 | } |
| 3109 | } |
| 3110 | return res; |
| 3111 | } |
| 3112 | |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3113 | void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request, |
| 3114 | /*out*/device::V3_2::CaptureRequest* captureRequest, |
| 3115 | /*out*/std::vector<native_handle_t*>* handlesCreated) { |
| 3116 | |
| 3117 | if (captureRequest == nullptr || handlesCreated == nullptr) { |
| 3118 | ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null", |
| 3119 | __FUNCTION__, captureRequest, handlesCreated); |
| 3120 | return; |
| 3121 | } |
| 3122 | |
| 3123 | captureRequest->frameNumber = request->frame_number; |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 3124 | |
| 3125 | captureRequest->fmqSettingsSize = 0; |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3126 | |
| 3127 | { |
| 3128 | std::lock_guard<std::mutex> lock(mInflightLock); |
| 3129 | if (request->input_buffer != nullptr) { |
| 3130 | int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId(); |
| 3131 | buffer_handle_t buf = *(request->input_buffer->buffer); |
| 3132 | auto pair = getBufferId(buf, streamId); |
| 3133 | bool isNewBuffer = pair.first; |
| 3134 | uint64_t bufferId = pair.second; |
| 3135 | captureRequest->inputBuffer.streamId = streamId; |
| 3136 | captureRequest->inputBuffer.bufferId = bufferId; |
| 3137 | captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr; |
| 3138 | captureRequest->inputBuffer.status = BufferStatus::OK; |
| 3139 | native_handle_t *acquireFence = nullptr; |
| 3140 | if (request->input_buffer->acquire_fence != -1) { |
| 3141 | acquireFence = native_handle_create(1,0); |
| 3142 | acquireFence->data[0] = request->input_buffer->acquire_fence; |
| 3143 | handlesCreated->push_back(acquireFence); |
| 3144 | } |
| 3145 | captureRequest->inputBuffer.acquireFence = acquireFence; |
| 3146 | captureRequest->inputBuffer.releaseFence = nullptr; |
| 3147 | |
| 3148 | pushInflightBufferLocked(captureRequest->frameNumber, streamId, |
| 3149 | request->input_buffer->buffer, |
| 3150 | request->input_buffer->acquire_fence); |
| 3151 | } else { |
| 3152 | captureRequest->inputBuffer.streamId = -1; |
| 3153 | captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER; |
| 3154 | } |
| 3155 | |
| 3156 | captureRequest->outputBuffers.resize(request->num_output_buffers); |
| 3157 | for (size_t i = 0; i < request->num_output_buffers; i++) { |
| 3158 | const camera3_stream_buffer_t *src = request->output_buffers + i; |
| 3159 | StreamBuffer &dst = captureRequest->outputBuffers[i]; |
| 3160 | int32_t streamId = Camera3Stream::cast(src->stream)->getId(); |
| 3161 | buffer_handle_t buf = *(src->buffer); |
| 3162 | auto pair = getBufferId(buf, streamId); |
| 3163 | bool isNewBuffer = pair.first; |
| 3164 | dst.streamId = streamId; |
| 3165 | dst.bufferId = pair.second; |
| 3166 | dst.buffer = isNewBuffer ? buf : nullptr; |
| 3167 | dst.status = BufferStatus::OK; |
| 3168 | native_handle_t *acquireFence = nullptr; |
| 3169 | if (src->acquire_fence != -1) { |
| 3170 | acquireFence = native_handle_create(1,0); |
| 3171 | acquireFence->data[0] = src->acquire_fence; |
| 3172 | handlesCreated->push_back(acquireFence); |
| 3173 | } |
| 3174 | dst.acquireFence = acquireFence; |
| 3175 | dst.releaseFence = nullptr; |
| 3176 | |
| 3177 | pushInflightBufferLocked(captureRequest->frameNumber, streamId, |
| 3178 | src->buffer, src->acquire_fence); |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | status_t Camera3Device::HalInterface::processBatchCaptureRequests( |
| 3184 | std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) { |
| 3185 | ATRACE_NAME("CameraHal::processBatchCaptureRequests"); |
| 3186 | if (!valid()) return INVALID_OPERATION; |
| 3187 | |
| 3188 | hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests; |
| 3189 | size_t batchSize = requests.size(); |
| 3190 | captureRequests.resize(batchSize); |
| 3191 | std::vector<native_handle_t*> handlesCreated; |
| 3192 | |
| 3193 | for (size_t i = 0; i < batchSize; i++) { |
| 3194 | wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated); |
| 3195 | } |
| 3196 | |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 3197 | std::vector<device::V3_2::BufferCache> cachesToRemove; |
| 3198 | { |
| 3199 | std::lock_guard<std::mutex> lock(mBufferIdMapLock); |
| 3200 | for (auto& pair : mFreedBuffers) { |
| 3201 | // The stream might have been removed since onBufferFreed |
| 3202 | if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) { |
| 3203 | cachesToRemove.push_back({pair.first, pair.second}); |
| 3204 | } |
| 3205 | } |
| 3206 | mFreedBuffers.clear(); |
| 3207 | } |
| 3208 | |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3209 | common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR; |
| 3210 | *numRequestProcessed = 0; |
Yifan Hong | f79b554 | 2017-04-11 14:44:25 -0700 | [diff] [blame] | 3211 | |
| 3212 | // Write metadata to FMQ. |
| 3213 | for (size_t i = 0; i < batchSize; i++) { |
| 3214 | camera3_capture_request_t* request = requests[i]; |
| 3215 | device::V3_2::CaptureRequest* captureRequest = &captureRequests[i]; |
| 3216 | |
| 3217 | if (request->settings != nullptr) { |
| 3218 | size_t settingsSize = get_camera_metadata_size(request->settings); |
| 3219 | if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write( |
| 3220 | reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) { |
| 3221 | captureRequest->settings.resize(0); |
| 3222 | captureRequest->fmqSettingsSize = settingsSize; |
| 3223 | } else { |
| 3224 | if (mRequestMetadataQueue != nullptr) { |
| 3225 | ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__); |
| 3226 | } |
| 3227 | captureRequest->settings.setToExternal( |
| 3228 | reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)), |
| 3229 | get_camera_metadata_size(request->settings)); |
| 3230 | captureRequest->fmqSettingsSize = 0u; |
| 3231 | } |
| 3232 | } else { |
| 3233 | // A null request settings maps to a size-0 CameraMetadata |
| 3234 | captureRequest->settings.resize(0); |
| 3235 | captureRequest->fmqSettingsSize = 0u; |
| 3236 | } |
| 3237 | } |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 3238 | auto err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove, |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3239 | [&status, &numRequestProcessed] (auto s, uint32_t n) { |
| 3240 | status = s; |
| 3241 | *numRequestProcessed = n; |
| 3242 | }); |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 3243 | if (!err.isOk()) { |
| 3244 | ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); |
| 3245 | return DEAD_OBJECT; |
| 3246 | } |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3247 | if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) { |
| 3248 | ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests", |
| 3249 | __FUNCTION__, *numRequestProcessed, batchSize); |
| 3250 | status = common::V1_0::Status::INTERNAL_ERROR; |
| 3251 | } |
| 3252 | |
| 3253 | for (auto& handle : handlesCreated) { |
| 3254 | native_handle_delete(handle); |
| 3255 | } |
| 3256 | return CameraProviderManager::mapToStatusT(status); |
| 3257 | } |
| 3258 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3259 | status_t Camera3Device::HalInterface::processCaptureRequest( |
| 3260 | camera3_capture_request_t *request) { |
| 3261 | ATRACE_NAME("CameraHal::processCaptureRequest"); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3262 | if (!valid()) return INVALID_OPERATION; |
| 3263 | status_t res = OK; |
| 3264 | |
| 3265 | if (mHal3Device != nullptr) { |
| 3266 | res = mHal3Device->ops->process_capture_request(mHal3Device, request); |
| 3267 | } else { |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3268 | uint32_t numRequestProcessed = 0; |
| 3269 | std::vector<camera3_capture_request_t*> requests(1); |
| 3270 | requests[0] = request; |
| 3271 | res = processBatchCaptureRequests(requests, &numRequestProcessed); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3272 | } |
| 3273 | return res; |
| 3274 | } |
| 3275 | |
| 3276 | status_t Camera3Device::HalInterface::flush() { |
| 3277 | ATRACE_NAME("CameraHal::flush"); |
| 3278 | if (!valid()) return INVALID_OPERATION; |
| 3279 | status_t res = OK; |
| 3280 | |
| 3281 | if (mHal3Device != nullptr) { |
| 3282 | res = mHal3Device->ops->flush(mHal3Device); |
| 3283 | } else { |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 3284 | auto err = mHidlSession->flush(); |
| 3285 | if (!err.isOk()) { |
| 3286 | ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str()); |
| 3287 | res = DEAD_OBJECT; |
| 3288 | } else { |
| 3289 | res = CameraProviderManager::mapToStatusT(err); |
| 3290 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3291 | } |
| 3292 | return res; |
| 3293 | } |
| 3294 | |
| 3295 | status_t Camera3Device::HalInterface::dump(int fd) { |
| 3296 | ATRACE_NAME("CameraHal::dump"); |
| 3297 | if (!valid()) return INVALID_OPERATION; |
| 3298 | status_t res = OK; |
| 3299 | |
| 3300 | if (mHal3Device != nullptr) { |
| 3301 | mHal3Device->ops->dump(mHal3Device, fd); |
| 3302 | } else { |
| 3303 | // Handled by CameraProviderManager::dump |
| 3304 | } |
| 3305 | return res; |
| 3306 | } |
| 3307 | |
| 3308 | status_t Camera3Device::HalInterface::close() { |
| 3309 | ATRACE_NAME("CameraHal::close()"); |
| 3310 | if (!valid()) return INVALID_OPERATION; |
| 3311 | status_t res = OK; |
| 3312 | |
| 3313 | if (mHal3Device != nullptr) { |
| 3314 | mHal3Device->common.close(&mHal3Device->common); |
| 3315 | } else { |
Eino-Ville Talvala | c5cbb87 | 2017-04-27 12:48:33 -0700 | [diff] [blame] | 3316 | auto err = mHidlSession->close(); |
| 3317 | // Interface will be dead shortly anyway, so don't log errors |
| 3318 | if (!err.isOk()) { |
| 3319 | res = DEAD_OBJECT; |
| 3320 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3321 | } |
| 3322 | return res; |
| 3323 | } |
| 3324 | |
| 3325 | status_t Camera3Device::HalInterface::pushInflightBufferLocked( |
Yin-Chia Yeh | f465060 | 2017-01-10 13:13:39 -0800 | [diff] [blame] | 3326 | int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3327 | uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId); |
Yin-Chia Yeh | f465060 | 2017-01-10 13:13:39 -0800 | [diff] [blame] | 3328 | auto pair = std::make_pair(buffer, acquireFence); |
| 3329 | mInflightBufferMap[key] = pair; |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3330 | return OK; |
| 3331 | } |
| 3332 | |
| 3333 | status_t Camera3Device::HalInterface::popInflightBuffer( |
Yin-Chia Yeh | f465060 | 2017-01-10 13:13:39 -0800 | [diff] [blame] | 3334 | int32_t frameNumber, int32_t streamId, |
| 3335 | /*out*/ buffer_handle_t **buffer) { |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3336 | std::lock_guard<std::mutex> lock(mInflightLock); |
| 3337 | |
| 3338 | uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId); |
| 3339 | auto it = mInflightBufferMap.find(key); |
| 3340 | if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND; |
Yin-Chia Yeh | f465060 | 2017-01-10 13:13:39 -0800 | [diff] [blame] | 3341 | auto pair = it->second; |
| 3342 | *buffer = pair.first; |
| 3343 | int acquireFence = pair.second; |
| 3344 | if (acquireFence > 0) { |
| 3345 | ::close(acquireFence); |
| 3346 | } |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3347 | mInflightBufferMap.erase(it); |
| 3348 | return OK; |
| 3349 | } |
| 3350 | |
Yin-Chia Yeh | 7732705 | 2017-01-09 18:23:07 -0800 | [diff] [blame] | 3351 | std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId( |
| 3352 | const buffer_handle_t& buf, int streamId) { |
| 3353 | std::lock_guard<std::mutex> lock(mBufferIdMapLock); |
| 3354 | |
| 3355 | BufferIdMap& bIdMap = mBufferIdMaps.at(streamId); |
| 3356 | auto it = bIdMap.find(buf); |
| 3357 | if (it == bIdMap.end()) { |
| 3358 | bIdMap[buf] = mNextBufferId++; |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 3359 | ALOGV("stream %d now have %zu buffer caches, buf %p", |
| 3360 | streamId, bIdMap.size(), buf); |
Yin-Chia Yeh | 7732705 | 2017-01-09 18:23:07 -0800 | [diff] [blame] | 3361 | return std::make_pair(true, mNextBufferId - 1); |
| 3362 | } else { |
| 3363 | return std::make_pair(false, it->second); |
| 3364 | } |
| 3365 | } |
| 3366 | |
Yin-Chia Yeh | be83fa7 | 2017-03-30 13:35:36 -0700 | [diff] [blame] | 3367 | void Camera3Device::HalInterface::onBufferFreed( |
| 3368 | int streamId, const native_handle_t* handle) { |
| 3369 | std::lock_guard<std::mutex> lock(mBufferIdMapLock); |
| 3370 | uint64_t bufferId = BUFFER_ID_NO_BUFFER; |
| 3371 | auto mapIt = mBufferIdMaps.find(streamId); |
| 3372 | if (mapIt == mBufferIdMaps.end()) { |
| 3373 | // streamId might be from a deleted stream here |
| 3374 | ALOGI("%s: stream %d has been removed", |
| 3375 | __FUNCTION__, streamId); |
| 3376 | return; |
| 3377 | } |
| 3378 | BufferIdMap& bIdMap = mapIt->second; |
| 3379 | auto it = bIdMap.find(handle); |
| 3380 | if (it == bIdMap.end()) { |
| 3381 | ALOGW("%s: cannot find buffer %p in stream %d", |
| 3382 | __FUNCTION__, handle, streamId); |
| 3383 | return; |
| 3384 | } else { |
| 3385 | bufferId = it->second; |
| 3386 | bIdMap.erase(it); |
| 3387 | ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p", |
| 3388 | __FUNCTION__, streamId, bIdMap.size(), handle); |
| 3389 | } |
| 3390 | mFreedBuffers.push_back(std::make_pair(streamId, bufferId)); |
| 3391 | } |
| 3392 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3393 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3394 | * RequestThread inner class methods |
| 3395 | */ |
| 3396 | |
| 3397 | Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3398 | sp<StatusTracker> statusTracker, |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 3399 | HalInterface* interface) : |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 3400 | Thread(/*canCallJava*/false), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3401 | mParent(parent), |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3402 | mStatusTracker(statusTracker), |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3403 | mInterface(interface), |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 3404 | mListener(nullptr), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 3405 | mId(getId(parent)), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3406 | mReconfigured(false), |
| 3407 | mDoPause(false), |
| 3408 | mPaused(true), |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3409 | mFrameNumber(0), |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3410 | mLatestRequestId(NAME_NOT_FOUND), |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 3411 | mCurrentAfTriggerId(0), |
| 3412 | mCurrentPreCaptureTriggerId(0), |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3413 | mRepeatingLastFrameNumber( |
| 3414 | hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES), |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 3415 | mPrepareVideoStream(false), |
| 3416 | mRequestLatency(kRequestLatencyBinSize) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3417 | mStatusId = statusTracker->addComponent(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3418 | } |
| 3419 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3420 | Camera3Device::RequestThread::~RequestThread() {} |
| 3421 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 3422 | void Camera3Device::RequestThread::setNotificationListener( |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 3423 | wp<NotificationListener> listener) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3424 | Mutex::Autolock l(mRequestLock); |
| 3425 | mListener = listener; |
| 3426 | } |
| 3427 | |
Chien-Yu Chen | c66969b | 2016-05-19 16:37:51 -0700 | [diff] [blame] | 3428 | void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3429 | Mutex::Autolock l(mRequestLock); |
| 3430 | mReconfigured = true; |
Chien-Yu Chen | c66969b | 2016-05-19 16:37:51 -0700 | [diff] [blame] | 3431 | // Prepare video stream for high speed recording. |
| 3432 | mPrepareVideoStream = isConstrainedHighSpeed; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3433 | } |
| 3434 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3435 | status_t Camera3Device::RequestThread::queueRequestList( |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3436 | List<sp<CaptureRequest> > &requests, |
| 3437 | /*out*/ |
| 3438 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3439 | Mutex::Autolock l(mRequestLock); |
| 3440 | for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); |
| 3441 | ++it) { |
| 3442 | mRequestQueue.push_back(*it); |
| 3443 | } |
| 3444 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3445 | if (lastFrameNumber != NULL) { |
| 3446 | *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1; |
| 3447 | ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".", |
| 3448 | __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber, |
| 3449 | *lastFrameNumber); |
| 3450 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3451 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3452 | unpauseForNewRequests(); |
| 3453 | |
| 3454 | return OK; |
| 3455 | } |
| 3456 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3457 | |
| 3458 | status_t Camera3Device::RequestThread::queueTrigger( |
| 3459 | RequestTrigger trigger[], |
| 3460 | size_t count) { |
| 3461 | |
| 3462 | Mutex::Autolock l(mTriggerMutex); |
| 3463 | status_t ret; |
| 3464 | |
| 3465 | for (size_t i = 0; i < count; ++i) { |
| 3466 | ret = queueTriggerLocked(trigger[i]); |
| 3467 | |
| 3468 | if (ret != OK) { |
| 3469 | return ret; |
| 3470 | } |
| 3471 | } |
| 3472 | |
| 3473 | return OK; |
| 3474 | } |
| 3475 | |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3476 | const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { |
| 3477 | static String8 deadId("<DeadDevice>"); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 3478 | sp<Camera3Device> d = device.promote(); |
Eino-Ville Talvala | 0b1cb14 | 2016-12-19 16:29:17 -0800 | [diff] [blame] | 3479 | if (d != nullptr) return d->mId; |
| 3480 | return deadId; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3483 | status_t Camera3Device::RequestThread::queueTriggerLocked( |
| 3484 | RequestTrigger trigger) { |
| 3485 | |
| 3486 | uint32_t tag = trigger.metadataTag; |
| 3487 | ssize_t index = mTriggerMap.indexOfKey(tag); |
| 3488 | |
| 3489 | switch (trigger.getTagType()) { |
| 3490 | case TYPE_BYTE: |
| 3491 | // fall-through |
| 3492 | case TYPE_INT32: |
| 3493 | break; |
| 3494 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 3495 | ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, |
| 3496 | trigger.getTagType()); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3497 | return INVALID_OPERATION; |
| 3498 | } |
| 3499 | |
| 3500 | /** |
| 3501 | * Collect only the latest trigger, since we only have 1 field |
| 3502 | * in the request settings per trigger tag, and can't send more than 1 |
| 3503 | * trigger per request. |
| 3504 | */ |
| 3505 | if (index != NAME_NOT_FOUND) { |
| 3506 | mTriggerMap.editValueAt(index) = trigger; |
| 3507 | } else { |
| 3508 | mTriggerMap.add(tag, trigger); |
| 3509 | } |
| 3510 | |
| 3511 | return OK; |
| 3512 | } |
| 3513 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3514 | status_t Camera3Device::RequestThread::setRepeatingRequests( |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3515 | const RequestList &requests, |
| 3516 | /*out*/ |
| 3517 | int64_t *lastFrameNumber) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3518 | Mutex::Autolock l(mRequestLock); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3519 | if (lastFrameNumber != NULL) { |
| 3520 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 3521 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3522 | mRepeatingRequests.clear(); |
| 3523 | mRepeatingRequests.insert(mRepeatingRequests.begin(), |
| 3524 | requests.begin(), requests.end()); |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3525 | |
| 3526 | unpauseForNewRequests(); |
| 3527 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3528 | mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3529 | return OK; |
| 3530 | } |
| 3531 | |
Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 3532 | bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) { |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 3533 | if (mRepeatingRequests.empty()) { |
| 3534 | return false; |
| 3535 | } |
| 3536 | int32_t requestId = requestIn->mResultExtras.requestId; |
| 3537 | const RequestList &repeatRequests = mRepeatingRequests; |
| 3538 | // All repeating requests are guaranteed to have same id so only check first quest |
| 3539 | const sp<CaptureRequest> firstRequest = *repeatRequests.begin(); |
| 3540 | return (firstRequest->mResultExtras.requestId == requestId); |
| 3541 | } |
| 3542 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3543 | status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3544 | Mutex::Autolock l(mRequestLock); |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 3545 | return clearRepeatingRequestsLocked(lastFrameNumber); |
| 3546 | |
| 3547 | } |
| 3548 | |
| 3549 | status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3550 | mRepeatingRequests.clear(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3551 | if (lastFrameNumber != NULL) { |
| 3552 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 3553 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3554 | mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3555 | return OK; |
| 3556 | } |
| 3557 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3558 | status_t Camera3Device::RequestThread::clear( |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3559 | /*out*/int64_t *lastFrameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 3560 | Mutex::Autolock l(mRequestLock); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3561 | ALOGV("RequestThread::%s:", __FUNCTION__); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3562 | |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 3563 | mRepeatingRequests.clear(); |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 3564 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3565 | // Send errors for all requests pending in the request queue, including |
| 3566 | // pending repeating requests |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 3567 | sp<NotificationListener> listener = mListener.promote(); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3568 | if (listener != NULL) { |
| 3569 | for (RequestList::iterator it = mRequestQueue.begin(); |
| 3570 | it != mRequestQueue.end(); ++it) { |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 3571 | // Abort the input buffers for reprocess requests. |
| 3572 | if ((*it)->mInputStream != NULL) { |
| 3573 | camera3_stream_buffer_t inputBuffer; |
| 3574 | status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer); |
| 3575 | if (res != OK) { |
| 3576 | ALOGW("%s: %d: couldn't get input buffer while clearing the request " |
| 3577 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); |
| 3578 | } else { |
| 3579 | res = (*it)->mInputStream->returnInputBuffer(inputBuffer); |
| 3580 | if (res != OK) { |
| 3581 | ALOGE("%s: %d: couldn't return input buffer while clearing the request " |
| 3582 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); |
| 3583 | } |
| 3584 | } |
| 3585 | } |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3586 | // Set the frame number this request would have had, if it |
| 3587 | // had been submitted; this frame number will not be reused. |
| 3588 | // The requestId and burstId fields were set when the request was |
| 3589 | // submitted originally (in convertMetadataListToRequestListLocked) |
| 3590 | (*it)->mResultExtras.frameNumber = mFrameNumber++; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3591 | listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 3592 | (*it)->mResultExtras); |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 3593 | } |
| 3594 | } |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 3595 | mRequestQueue.clear(); |
Jinguang Dong | b26e7a0 | 2016-11-14 16:04:02 +0800 | [diff] [blame] | 3596 | |
| 3597 | Mutex::Autolock al(mTriggerMutex); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 3598 | mTriggerMap.clear(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3599 | if (lastFrameNumber != NULL) { |
| 3600 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 3601 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 3602 | mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 3603 | return OK; |
| 3604 | } |
| 3605 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3606 | status_t Camera3Device::RequestThread::flush() { |
| 3607 | ATRACE_CALL(); |
| 3608 | Mutex::Autolock l(mFlushLock); |
| 3609 | |
Emilian Peev | 08dd245 | 2017-04-06 16:55:14 +0100 | [diff] [blame] | 3610 | return mInterface->flush(); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3611 | } |
| 3612 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3613 | void Camera3Device::RequestThread::setPaused(bool paused) { |
| 3614 | Mutex::Autolock l(mPauseLock); |
| 3615 | mDoPause = paused; |
| 3616 | mDoPauseSignal.signal(); |
| 3617 | } |
| 3618 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3619 | status_t Camera3Device::RequestThread::waitUntilRequestProcessed( |
| 3620 | int32_t requestId, nsecs_t timeout) { |
| 3621 | Mutex::Autolock l(mLatestRequestMutex); |
| 3622 | status_t res; |
| 3623 | while (mLatestRequestId != requestId) { |
| 3624 | nsecs_t startTime = systemTime(); |
| 3625 | |
| 3626 | res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); |
| 3627 | if (res != OK) return res; |
| 3628 | |
| 3629 | timeout -= (systemTime() - startTime); |
| 3630 | } |
| 3631 | |
| 3632 | return OK; |
| 3633 | } |
| 3634 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3635 | void Camera3Device::RequestThread::requestExit() { |
| 3636 | // Call parent to set up shutdown |
| 3637 | Thread::requestExit(); |
| 3638 | // The exit from any possible waits |
| 3639 | mDoPauseSignal.signal(); |
| 3640 | mRequestSignal.signal(); |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 3641 | |
| 3642 | mRequestLatency.log("ProcessCaptureRequest latency histogram"); |
| 3643 | mRequestLatency.reset(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3644 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3645 | |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 3646 | void Camera3Device::RequestThread::checkAndStopRepeatingRequest() { |
Yin-Chia Yeh | 473fad9 | 2016-05-23 15:54:41 -0700 | [diff] [blame] | 3647 | bool surfaceAbandoned = false; |
| 3648 | int64_t lastFrameNumber = 0; |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 3649 | sp<NotificationListener> listener; |
Yin-Chia Yeh | 473fad9 | 2016-05-23 15:54:41 -0700 | [diff] [blame] | 3650 | { |
| 3651 | Mutex::Autolock l(mRequestLock); |
| 3652 | // Check all streams needed by repeating requests are still valid. Otherwise, stop |
| 3653 | // repeating requests. |
| 3654 | for (const auto& request : mRepeatingRequests) { |
| 3655 | for (const auto& s : request->mOutputStreams) { |
| 3656 | if (s->isAbandoned()) { |
| 3657 | surfaceAbandoned = true; |
| 3658 | clearRepeatingRequestsLocked(&lastFrameNumber); |
| 3659 | break; |
| 3660 | } |
| 3661 | } |
| 3662 | if (surfaceAbandoned) { |
| 3663 | break; |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 3664 | } |
| 3665 | } |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 3666 | listener = mListener.promote(); |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 3667 | } |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 3668 | |
| 3669 | if (listener != NULL && surfaceAbandoned) { |
| 3670 | listener->notifyRepeatingRequestError(lastFrameNumber); |
Yin-Chia Yeh | 473fad9 | 2016-05-23 15:54:41 -0700 | [diff] [blame] | 3671 | } |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 3672 | } |
| 3673 | |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3674 | bool Camera3Device::RequestThread::sendRequestsBatch() { |
| 3675 | status_t res; |
| 3676 | size_t batchSize = mNextRequests.size(); |
| 3677 | std::vector<camera3_capture_request_t*> requests(batchSize); |
| 3678 | uint32_t numRequestProcessed = 0; |
| 3679 | for (size_t i = 0; i < batchSize; i++) { |
| 3680 | requests[i] = &mNextRequests.editItemAt(i).halRequest; |
| 3681 | } |
| 3682 | |
| 3683 | ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number); |
| 3684 | res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed); |
| 3685 | |
| 3686 | bool triggerRemoveFailed = false; |
| 3687 | NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0); |
| 3688 | for (size_t i = 0; i < numRequestProcessed; i++) { |
| 3689 | NextRequest& nextRequest = mNextRequests.editItemAt(i); |
| 3690 | nextRequest.submitted = true; |
| 3691 | |
| 3692 | |
| 3693 | // Update the latest request sent to HAL |
| 3694 | if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged |
| 3695 | Mutex::Autolock al(mLatestRequestMutex); |
| 3696 | |
| 3697 | camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings); |
| 3698 | mLatestRequest.acquire(cloned); |
| 3699 | |
| 3700 | sp<Camera3Device> parent = mParent.promote(); |
| 3701 | if (parent != NULL) { |
| 3702 | parent->monitorMetadata(TagMonitor::REQUEST, |
| 3703 | nextRequest.halRequest.frame_number, |
| 3704 | 0, mLatestRequest); |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | if (nextRequest.halRequest.settings != NULL) { |
| 3709 | nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings); |
| 3710 | } |
| 3711 | |
| 3712 | if (!triggerRemoveFailed) { |
| 3713 | // Remove any previously queued triggers (after unlock) |
| 3714 | status_t removeTriggerRes = removeTriggers(mPrevRequest); |
| 3715 | if (removeTriggerRes != OK) { |
| 3716 | triggerRemoveFailed = true; |
| 3717 | triggerFailedRequest = nextRequest; |
| 3718 | } |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | if (triggerRemoveFailed) { |
| 3723 | SET_ERR("RequestThread: Unable to remove triggers " |
| 3724 | "(capture request %d, HAL device: %s (%d)", |
| 3725 | triggerFailedRequest.halRequest.frame_number, strerror(-res), res); |
| 3726 | cleanUpFailedRequests(/*sendRequestError*/ false); |
| 3727 | return false; |
| 3728 | } |
| 3729 | |
| 3730 | if (res != OK) { |
| 3731 | // Should only get a failure here for malformed requests or device-level |
| 3732 | // errors, so consider all errors fatal. Bad metadata failures should |
| 3733 | // come through notify. |
| 3734 | SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)", |
| 3735 | mNextRequests[numRequestProcessed].halRequest.frame_number, |
| 3736 | strerror(-res), res); |
| 3737 | cleanUpFailedRequests(/*sendRequestError*/ false); |
| 3738 | return false; |
| 3739 | } |
| 3740 | return true; |
| 3741 | } |
| 3742 | |
| 3743 | bool Camera3Device::RequestThread::sendRequestsOneByOne() { |
| 3744 | status_t res; |
| 3745 | |
| 3746 | for (auto& nextRequest : mNextRequests) { |
| 3747 | // Submit request and block until ready for next one |
| 3748 | ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number); |
| 3749 | res = mInterface->processCaptureRequest(&nextRequest.halRequest); |
| 3750 | |
| 3751 | if (res != OK) { |
| 3752 | // Should only get a failure here for malformed requests or device-level |
| 3753 | // errors, so consider all errors fatal. Bad metadata failures should |
| 3754 | // come through notify. |
| 3755 | SET_ERR("RequestThread: Unable to submit capture request %d to HAL" |
| 3756 | " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res), |
| 3757 | res); |
| 3758 | cleanUpFailedRequests(/*sendRequestError*/ false); |
| 3759 | return false; |
| 3760 | } |
| 3761 | |
| 3762 | // Mark that the request has be submitted successfully. |
| 3763 | nextRequest.submitted = true; |
| 3764 | |
| 3765 | // Update the latest request sent to HAL |
| 3766 | if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged |
| 3767 | Mutex::Autolock al(mLatestRequestMutex); |
| 3768 | |
| 3769 | camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings); |
| 3770 | mLatestRequest.acquire(cloned); |
| 3771 | |
| 3772 | sp<Camera3Device> parent = mParent.promote(); |
| 3773 | if (parent != NULL) { |
| 3774 | parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number, |
| 3775 | 0, mLatestRequest); |
| 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | if (nextRequest.halRequest.settings != NULL) { |
| 3780 | nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings); |
| 3781 | } |
| 3782 | |
| 3783 | // Remove any previously queued triggers (after unlock) |
| 3784 | res = removeTriggers(mPrevRequest); |
| 3785 | if (res != OK) { |
| 3786 | SET_ERR("RequestThread: Unable to remove triggers " |
| 3787 | "(capture request %d, HAL device: %s (%d)", |
| 3788 | nextRequest.halRequest.frame_number, strerror(-res), res); |
| 3789 | cleanUpFailedRequests(/*sendRequestError*/ false); |
| 3790 | return false; |
| 3791 | } |
| 3792 | } |
| 3793 | return true; |
| 3794 | } |
| 3795 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3796 | bool Camera3Device::RequestThread::threadLoop() { |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3797 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3798 | status_t res; |
| 3799 | |
| 3800 | // Handle paused state. |
| 3801 | if (waitIfPaused()) { |
| 3802 | return true; |
| 3803 | } |
| 3804 | |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3805 | // Wait for the next batch of requests. |
| 3806 | waitForNextRequestBatch(); |
| 3807 | if (mNextRequests.size() == 0) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3808 | return true; |
| 3809 | } |
| 3810 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3811 | // Get the latest request ID, if any |
| 3812 | int latestRequestId; |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3813 | camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1]. |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3814 | captureRequest->mSettings.find(ANDROID_REQUEST_ID); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3815 | if (requestIdEntry.count > 0) { |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3816 | latestRequestId = requestIdEntry.data.i32[0]; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3817 | } else { |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3818 | ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__); |
| 3819 | latestRequestId = NAME_NOT_FOUND; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3820 | } |
| 3821 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3822 | // Prepare a batch of HAL requests and output buffers. |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3823 | res = prepareHalRequests(); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3824 | if (res == TIMED_OUT) { |
| 3825 | // Not a fatal error if getting output buffers time out. |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3826 | cleanUpFailedRequests(/*sendRequestError*/ true); |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 3827 | // Check if any stream is abandoned. |
| 3828 | checkAndStopRepeatingRequest(); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3829 | return true; |
| 3830 | } else if (res != OK) { |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3831 | cleanUpFailedRequests(/*sendRequestError*/ false); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 3832 | return false; |
| 3833 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3834 | |
Zhijun He | cc27e11 | 2013-10-03 16:12:43 -0700 | [diff] [blame] | 3835 | // Inform waitUntilRequestProcessed thread of a new request ID |
| 3836 | { |
| 3837 | Mutex::Autolock al(mLatestRequestMutex); |
| 3838 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3839 | mLatestRequestId = latestRequestId; |
Zhijun He | cc27e11 | 2013-10-03 16:12:43 -0700 | [diff] [blame] | 3840 | mLatestRequestSignal.signal(); |
| 3841 | } |
| 3842 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3843 | // Submit a batch of requests to HAL. |
| 3844 | // Use flush lock only when submitting multilple requests in a batch. |
| 3845 | // TODO: The problem with flush lock is flush() will be blocked by process_capture_request() |
| 3846 | // which may take a long time to finish so synchronizing flush() and |
| 3847 | // process_capture_request() defeats the purpose of cancelling requests ASAP with flush(). |
| 3848 | // For now, only synchronize for high speed recording and we should figure something out for |
| 3849 | // removing the synchronization. |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3850 | bool useFlushLock = mNextRequests.size() > 1; |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 3851 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3852 | if (useFlushLock) { |
| 3853 | mFlushLock.lock(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3854 | } |
| 3855 | |
Zhijun He | f0645c1 | 2016-08-02 00:58:11 -0700 | [diff] [blame] | 3856 | ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__, |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3857 | mNextRequests.size()); |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 3858 | |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3859 | bool submitRequestSuccess = false; |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 3860 | nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC); |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3861 | if (mInterface->supportBatchRequest()) { |
| 3862 | submitRequestSuccess = sendRequestsBatch(); |
| 3863 | } else { |
| 3864 | submitRequestSuccess = sendRequestsOneByOne(); |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 3865 | } |
Shuzhen Wang | 686f644 | 2017-06-20 16:16:04 -0700 | [diff] [blame] | 3866 | nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC); |
| 3867 | mRequestLatency.add(tRequestStart, tRequestEnd); |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 3868 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3869 | if (useFlushLock) { |
| 3870 | mFlushLock.unlock(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3871 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3872 | |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 3873 | // Unset as current request |
| 3874 | { |
| 3875 | Mutex::Autolock l(mRequestLock); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3876 | mNextRequests.clear(); |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 3877 | } |
| 3878 | |
Yin-Chia Yeh | 94c68e0 | 2017-03-06 14:09:44 -0800 | [diff] [blame] | 3879 | return submitRequestSuccess; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3880 | } |
| 3881 | |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 3882 | status_t Camera3Device::RequestThread::prepareHalRequests() { |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3883 | ATRACE_CALL(); |
| 3884 | |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 3885 | for (size_t i = 0; i < mNextRequests.size(); i++) { |
| 3886 | auto& nextRequest = mNextRequests.editItemAt(i); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3887 | sp<CaptureRequest> captureRequest = nextRequest.captureRequest; |
| 3888 | camera3_capture_request_t* halRequest = &nextRequest.halRequest; |
| 3889 | Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers; |
| 3890 | |
| 3891 | // Prepare a request to HAL |
| 3892 | halRequest->frame_number = captureRequest->mResultExtras.frameNumber; |
| 3893 | |
| 3894 | // Insert any queued triggers (before metadata is locked) |
| 3895 | status_t res = insertTriggers(captureRequest); |
| 3896 | |
| 3897 | if (res < 0) { |
| 3898 | SET_ERR("RequestThread: Unable to insert triggers " |
| 3899 | "(capture request %d, HAL device: %s (%d)", |
| 3900 | halRequest->frame_number, strerror(-res), res); |
| 3901 | return INVALID_OPERATION; |
| 3902 | } |
| 3903 | int triggerCount = res; |
| 3904 | bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); |
| 3905 | mPrevTriggers = triggerCount; |
| 3906 | |
| 3907 | // If the request is the same as last, or we had triggers last time |
| 3908 | if (mPrevRequest != captureRequest || triggersMixedIn) { |
| 3909 | /** |
| 3910 | * HAL workaround: |
| 3911 | * Insert a dummy trigger ID if a trigger is set but no trigger ID is |
| 3912 | */ |
| 3913 | res = addDummyTriggerIds(captureRequest); |
| 3914 | if (res != OK) { |
| 3915 | SET_ERR("RequestThread: Unable to insert dummy trigger IDs " |
| 3916 | "(capture request %d, HAL device: %s (%d)", |
| 3917 | halRequest->frame_number, strerror(-res), res); |
| 3918 | return INVALID_OPERATION; |
| 3919 | } |
| 3920 | |
| 3921 | /** |
| 3922 | * The request should be presorted so accesses in HAL |
| 3923 | * are O(logn). Sidenote, sorting a sorted metadata is nop. |
| 3924 | */ |
| 3925 | captureRequest->mSettings.sort(); |
| 3926 | halRequest->settings = captureRequest->mSettings.getAndLock(); |
| 3927 | mPrevRequest = captureRequest; |
| 3928 | ALOGVV("%s: Request settings are NEW", __FUNCTION__); |
| 3929 | |
| 3930 | IF_ALOGV() { |
| 3931 | camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); |
| 3932 | find_camera_metadata_ro_entry( |
| 3933 | halRequest->settings, |
| 3934 | ANDROID_CONTROL_AF_TRIGGER, |
| 3935 | &e |
| 3936 | ); |
| 3937 | if (e.count > 0) { |
| 3938 | ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", |
| 3939 | __FUNCTION__, |
| 3940 | halRequest->frame_number, |
| 3941 | e.data.u8[0]); |
| 3942 | } |
| 3943 | } |
| 3944 | } else { |
| 3945 | // leave request.settings NULL to indicate 'reuse latest given' |
| 3946 | ALOGVV("%s: Request settings are REUSED", |
| 3947 | __FUNCTION__); |
| 3948 | } |
| 3949 | |
| 3950 | uint32_t totalNumBuffers = 0; |
| 3951 | |
| 3952 | // Fill in buffers |
| 3953 | if (captureRequest->mInputStream != NULL) { |
| 3954 | halRequest->input_buffer = &captureRequest->mInputBuffer; |
| 3955 | totalNumBuffers += 1; |
| 3956 | } else { |
| 3957 | halRequest->input_buffer = NULL; |
| 3958 | } |
| 3959 | |
| 3960 | outputBuffers->insertAt(camera3_stream_buffer_t(), 0, |
| 3961 | captureRequest->mOutputStreams.size()); |
| 3962 | halRequest->output_buffers = outputBuffers->array(); |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 3963 | for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) { |
| 3964 | sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j); |
Chien-Yu Chen | c66969b | 2016-05-19 16:37:51 -0700 | [diff] [blame] | 3965 | |
| 3966 | // Prepare video buffers for high speed recording on the first video request. |
| 3967 | if (mPrepareVideoStream && outputStream->isVideoStream()) { |
| 3968 | // Only try to prepare video stream on the first video request. |
| 3969 | mPrepareVideoStream = false; |
| 3970 | |
| 3971 | res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX); |
| 3972 | while (res == NOT_ENOUGH_DATA) { |
| 3973 | res = outputStream->prepareNextBuffer(); |
| 3974 | } |
| 3975 | if (res != OK) { |
| 3976 | ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)", |
| 3977 | __FUNCTION__, strerror(-res), res); |
| 3978 | outputStream->cancelPrepare(); |
| 3979 | } |
| 3980 | } |
| 3981 | |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 3982 | res = outputStream->getBuffer(&outputBuffers->editItemAt(j), |
| 3983 | captureRequest->mOutputSurfaces[j]); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3984 | if (res != OK) { |
| 3985 | // Can't get output buffer from gralloc queue - this could be due to |
| 3986 | // abandoned queue or other consumer misbehavior, so not a fatal |
| 3987 | // error |
| 3988 | ALOGE("RequestThread: Can't get output buffer, skipping request:" |
| 3989 | " %s (%d)", strerror(-res), res); |
| 3990 | |
| 3991 | return TIMED_OUT; |
| 3992 | } |
| 3993 | halRequest->num_output_buffers++; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 3994 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 3995 | } |
| 3996 | totalNumBuffers += halRequest->num_output_buffers; |
| 3997 | |
| 3998 | // Log request in the in-flight queue |
| 3999 | sp<Camera3Device> parent = mParent.promote(); |
| 4000 | if (parent == NULL) { |
| 4001 | // Should not happen, and nowhere to send errors to, so just log it |
| 4002 | CLOGE("RequestThread: Parent is gone"); |
| 4003 | return INVALID_OPERATION; |
| 4004 | } |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 4005 | |
| 4006 | // If this request list is for constrained high speed recording (not |
| 4007 | // preview), and the current request is not the last one in the batch, |
| 4008 | // do not send callback to the app. |
| 4009 | bool hasCallback = true; |
| 4010 | if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) { |
| 4011 | hasCallback = false; |
| 4012 | } |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4013 | res = parent->registerInFlight(halRequest->frame_number, |
| 4014 | totalNumBuffers, captureRequest->mResultExtras, |
| 4015 | /*hasInput*/halRequest->input_buffer != NULL, |
Shuzhen Wang | 4a47266 | 2017-02-26 23:29:04 -0800 | [diff] [blame] | 4016 | hasCallback); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4017 | ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64 |
| 4018 | ", burstId = %" PRId32 ".", |
| 4019 | __FUNCTION__, |
| 4020 | captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber, |
| 4021 | captureRequest->mResultExtras.burstId); |
| 4022 | if (res != OK) { |
| 4023 | SET_ERR("RequestThread: Unable to register new in-flight request:" |
| 4024 | " %s (%d)", strerror(-res), res); |
| 4025 | return INVALID_OPERATION; |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | return OK; |
| 4030 | } |
| 4031 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 4032 | CameraMetadata Camera3Device::RequestThread::getLatestRequest() const { |
| 4033 | Mutex::Autolock al(mLatestRequestMutex); |
| 4034 | |
| 4035 | ALOGV("RequestThread::%s", __FUNCTION__); |
| 4036 | |
| 4037 | return mLatestRequest; |
| 4038 | } |
| 4039 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4040 | bool Camera3Device::RequestThread::isStreamPending( |
| 4041 | sp<Camera3StreamInterface>& stream) { |
| 4042 | Mutex::Autolock l(mRequestLock); |
| 4043 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4044 | for (const auto& nextRequest : mNextRequests) { |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4045 | if (!nextRequest.submitted) { |
| 4046 | for (const auto& s : nextRequest.captureRequest->mOutputStreams) { |
| 4047 | if (stream == s) return true; |
| 4048 | } |
| 4049 | if (stream == nextRequest.captureRequest->mInputStream) return true; |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 4050 | } |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4053 | for (const auto& request : mRequestQueue) { |
| 4054 | for (const auto& s : request->mOutputStreams) { |
| 4055 | if (stream == s) return true; |
| 4056 | } |
| 4057 | if (stream == request->mInputStream) return true; |
| 4058 | } |
| 4059 | |
| 4060 | for (const auto& request : mRepeatingRequests) { |
| 4061 | for (const auto& s : request->mOutputStreams) { |
| 4062 | if (stream == s) return true; |
| 4063 | } |
| 4064 | if (stream == request->mInputStream) return true; |
| 4065 | } |
| 4066 | |
| 4067 | return false; |
| 4068 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 4069 | |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4070 | void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) { |
| 4071 | if (mNextRequests.empty()) { |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4072 | return; |
| 4073 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4074 | |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4075 | for (auto& nextRequest : mNextRequests) { |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4076 | // Skip the ones that have been submitted successfully. |
| 4077 | if (nextRequest.submitted) { |
| 4078 | continue; |
| 4079 | } |
| 4080 | |
| 4081 | sp<CaptureRequest> captureRequest = nextRequest.captureRequest; |
| 4082 | camera3_capture_request_t* halRequest = &nextRequest.halRequest; |
| 4083 | Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers; |
| 4084 | |
| 4085 | if (halRequest->settings != NULL) { |
| 4086 | captureRequest->mSettings.unlock(halRequest->settings); |
| 4087 | } |
| 4088 | |
| 4089 | if (captureRequest->mInputStream != NULL) { |
| 4090 | captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; |
| 4091 | captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer); |
| 4092 | } |
| 4093 | |
| 4094 | for (size_t i = 0; i < halRequest->num_output_buffers; i++) { |
Emilian Peev | c58cf4c | 2017-05-11 17:23:41 +0100 | [diff] [blame] | 4095 | //Buffers that failed processing could still have |
| 4096 | //valid acquire fence. |
| 4097 | int acquireFence = (*outputBuffers)[i].acquire_fence; |
| 4098 | if (0 <= acquireFence) { |
| 4099 | close(acquireFence); |
| 4100 | outputBuffers->editItemAt(i).acquire_fence = -1; |
| 4101 | } |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4102 | outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 4103 | captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0); |
| 4104 | } |
| 4105 | |
| 4106 | if (sendRequestError) { |
| 4107 | Mutex::Autolock l(mRequestLock); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4108 | sp<NotificationListener> listener = mListener.promote(); |
| 4109 | if (listener != NULL) { |
| 4110 | listener->notifyError( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4111 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4112 | captureRequest->mResultExtras); |
| 4113 | } |
| 4114 | } |
Shuzhen Wang | cadb330 | 2016-11-04 14:17:56 -0700 | [diff] [blame] | 4115 | |
| 4116 | // Remove yet-to-be submitted inflight request from inflightMap |
| 4117 | { |
| 4118 | sp<Camera3Device> parent = mParent.promote(); |
| 4119 | if (parent != NULL) { |
| 4120 | Mutex::Autolock l(parent->mInFlightLock); |
| 4121 | ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber); |
| 4122 | if (idx >= 0) { |
| 4123 | ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64, |
| 4124 | __FUNCTION__, captureRequest->mResultExtras.frameNumber); |
| 4125 | parent->removeInFlightMapEntryLocked(idx); |
| 4126 | } |
| 4127 | } |
| 4128 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4129 | } |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 4130 | |
| 4131 | Mutex::Autolock l(mRequestLock); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4132 | mNextRequests.clear(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4133 | } |
| 4134 | |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4135 | void Camera3Device::RequestThread::waitForNextRequestBatch() { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4136 | // Optimized a bit for the simple steady-state case (single repeating |
| 4137 | // request), to avoid putting that request in the queue temporarily. |
| 4138 | Mutex::Autolock l(mRequestLock); |
| 4139 | |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4140 | assert(mNextRequests.empty()); |
| 4141 | |
| 4142 | NextRequest nextRequest; |
| 4143 | nextRequest.captureRequest = waitForNextRequestLocked(); |
| 4144 | if (nextRequest.captureRequest == nullptr) { |
| 4145 | return; |
| 4146 | } |
| 4147 | |
| 4148 | nextRequest.halRequest = camera3_capture_request_t(); |
| 4149 | nextRequest.submitted = false; |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4150 | mNextRequests.add(nextRequest); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4151 | |
| 4152 | // Wait for additional requests |
| 4153 | const size_t batchSize = nextRequest.captureRequest->mBatchSize; |
| 4154 | |
| 4155 | for (size_t i = 1; i < batchSize; i++) { |
| 4156 | NextRequest additionalRequest; |
| 4157 | additionalRequest.captureRequest = waitForNextRequestLocked(); |
| 4158 | if (additionalRequest.captureRequest == nullptr) { |
| 4159 | break; |
| 4160 | } |
| 4161 | |
| 4162 | additionalRequest.halRequest = camera3_capture_request_t(); |
| 4163 | additionalRequest.submitted = false; |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4164 | mNextRequests.add(additionalRequest); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4165 | } |
| 4166 | |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4167 | if (mNextRequests.size() < batchSize) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 4168 | ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.", |
Chien-Yu Chen | 57ea292 | 2015-09-04 12:58:56 -0700 | [diff] [blame] | 4169 | mNextRequests.size(), batchSize); |
| 4170 | cleanUpFailedRequests(/*sendRequestError*/true); |
Chien-Yu Chen | 85a6455 | 2015-08-28 15:46:12 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | return; |
| 4174 | } |
| 4175 | |
| 4176 | sp<Camera3Device::CaptureRequest> |
| 4177 | Camera3Device::RequestThread::waitForNextRequestLocked() { |
| 4178 | status_t res; |
| 4179 | sp<CaptureRequest> nextRequest; |
| 4180 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4181 | while (mRequestQueue.empty()) { |
| 4182 | if (!mRepeatingRequests.empty()) { |
| 4183 | // Always atomically enqueue all requests in a repeating request |
| 4184 | // list. Guarantees a complete in-sequence set of captures to |
| 4185 | // application. |
| 4186 | const RequestList &requests = mRepeatingRequests; |
| 4187 | RequestList::const_iterator firstRequest = |
| 4188 | requests.begin(); |
| 4189 | nextRequest = *firstRequest; |
| 4190 | mRequestQueue.insert(mRequestQueue.end(), |
| 4191 | ++firstRequest, |
| 4192 | requests.end()); |
| 4193 | // No need to wait any longer |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 4194 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 4195 | mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 4196 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4197 | break; |
| 4198 | } |
| 4199 | |
| 4200 | res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); |
| 4201 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4202 | if ((mRequestQueue.empty() && mRepeatingRequests.empty()) || |
| 4203 | exitPending()) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4204 | Mutex::Autolock pl(mPauseLock); |
| 4205 | if (mPaused == false) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4206 | ALOGV("%s: RequestThread: Going idle", __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4207 | mPaused = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4208 | // Let the tracker know |
| 4209 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 4210 | if (statusTracker != 0) { |
| 4211 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); |
| 4212 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4213 | } |
| 4214 | // Stop waiting for now and let thread management happen |
| 4215 | return NULL; |
| 4216 | } |
| 4217 | } |
| 4218 | |
| 4219 | if (nextRequest == NULL) { |
| 4220 | // Don't have a repeating request already in hand, so queue |
| 4221 | // must have an entry now. |
| 4222 | RequestList::iterator firstRequest = |
| 4223 | mRequestQueue.begin(); |
| 4224 | nextRequest = *firstRequest; |
| 4225 | mRequestQueue.erase(firstRequest); |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 4226 | if (mRequestQueue.empty() && !nextRequest->mRepeating) { |
| 4227 | sp<NotificationListener> listener = mListener.promote(); |
| 4228 | if (listener != NULL) { |
| 4229 | listener->notifyRequestQueueEmpty(); |
| 4230 | } |
| 4231 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4232 | } |
| 4233 | |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 4234 | // In case we've been unpaused by setPaused clearing mDoPause, need to |
| 4235 | // update internal pause state (capture/setRepeatingRequest unpause |
| 4236 | // directly). |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4237 | Mutex::Autolock pl(mPauseLock); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4238 | if (mPaused) { |
| 4239 | ALOGV("%s: RequestThread: Unpaused", __FUNCTION__); |
| 4240 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 4241 | if (statusTracker != 0) { |
| 4242 | statusTracker->markComponentActive(mStatusId); |
| 4243 | } |
| 4244 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4245 | mPaused = false; |
| 4246 | |
| 4247 | // Check if we've reconfigured since last time, and reset the preview |
| 4248 | // request if so. Can't use 'NULL request == repeat' across configure calls. |
| 4249 | if (mReconfigured) { |
| 4250 | mPrevRequest.clear(); |
| 4251 | mReconfigured = false; |
| 4252 | } |
| 4253 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 4254 | if (nextRequest != NULL) { |
| 4255 | nextRequest->mResultExtras.frameNumber = mFrameNumber++; |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 4256 | nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId; |
| 4257 | nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId; |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 4258 | |
| 4259 | // Since RequestThread::clear() removes buffers from the input stream, |
| 4260 | // get the right buffer here before unlocking mRequestLock |
| 4261 | if (nextRequest->mInputStream != NULL) { |
| 4262 | res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer); |
| 4263 | if (res != OK) { |
| 4264 | // Can't get input buffer from gralloc queue - this could be due to |
| 4265 | // disconnected queue or other producer misbehavior, so not a fatal |
| 4266 | // error |
| 4267 | ALOGE("%s: Can't get input buffer, skipping request:" |
| 4268 | " %s (%d)", __FUNCTION__, strerror(-res), res); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4269 | |
| 4270 | sp<NotificationListener> listener = mListener.promote(); |
| 4271 | if (listener != NULL) { |
| 4272 | listener->notifyError( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 4273 | hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 4274 | nextRequest->mResultExtras); |
| 4275 | } |
| 4276 | return NULL; |
| 4277 | } |
| 4278 | } |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 4279 | } |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 4280 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4281 | return nextRequest; |
| 4282 | } |
| 4283 | |
| 4284 | bool Camera3Device::RequestThread::waitIfPaused() { |
| 4285 | status_t res; |
| 4286 | Mutex::Autolock l(mPauseLock); |
| 4287 | while (mDoPause) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4288 | if (mPaused == false) { |
| 4289 | mPaused = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4290 | ALOGV("%s: RequestThread: Paused", __FUNCTION__); |
| 4291 | // Let the tracker know |
| 4292 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 4293 | if (statusTracker != 0) { |
| 4294 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); |
| 4295 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4296 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4297 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4298 | res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4299 | if (res == TIMED_OUT || exitPending()) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4300 | return true; |
| 4301 | } |
| 4302 | } |
| 4303 | // We don't set mPaused to false here, because waitForNextRequest needs |
| 4304 | // to further manage the paused state in case of starvation. |
| 4305 | return false; |
| 4306 | } |
| 4307 | |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 4308 | void Camera3Device::RequestThread::unpauseForNewRequests() { |
| 4309 | // With work to do, mark thread as unpaused. |
| 4310 | // If paused by request (setPaused), don't resume, to avoid |
| 4311 | // extra signaling/waiting overhead to waitUntilPaused |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4312 | mRequestSignal.signal(); |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 4313 | Mutex::Autolock p(mPauseLock); |
| 4314 | if (!mDoPause) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 4315 | ALOGV("%s: RequestThread: Going active", __FUNCTION__); |
| 4316 | if (mPaused) { |
| 4317 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 4318 | if (statusTracker != 0) { |
| 4319 | statusTracker->markComponentActive(mStatusId); |
| 4320 | } |
| 4321 | } |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 4322 | mPaused = false; |
| 4323 | } |
| 4324 | } |
| 4325 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 4326 | void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { |
| 4327 | sp<Camera3Device> parent = mParent.promote(); |
| 4328 | if (parent != NULL) { |
| 4329 | va_list args; |
| 4330 | va_start(args, fmt); |
| 4331 | |
| 4332 | parent->setErrorStateV(fmt, args); |
| 4333 | |
| 4334 | va_end(args); |
| 4335 | } |
| 4336 | } |
| 4337 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 4338 | status_t Camera3Device::RequestThread::insertTriggers( |
| 4339 | const sp<CaptureRequest> &request) { |
| 4340 | |
| 4341 | Mutex::Autolock al(mTriggerMutex); |
| 4342 | |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 4343 | sp<Camera3Device> parent = mParent.promote(); |
| 4344 | if (parent == NULL) { |
| 4345 | CLOGE("RequestThread: Parent is gone"); |
| 4346 | return DEAD_OBJECT; |
| 4347 | } |
| 4348 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 4349 | CameraMetadata &metadata = request->mSettings; |
| 4350 | size_t count = mTriggerMap.size(); |
| 4351 | |
| 4352 | for (size_t i = 0; i < count; ++i) { |
| 4353 | RequestTrigger trigger = mTriggerMap.valueAt(i); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 4354 | uint32_t tag = trigger.metadataTag; |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 4355 | |
| 4356 | if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) { |
| 4357 | bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID); |
| 4358 | uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue); |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 4359 | if (isAeTrigger) { |
| 4360 | request->mResultExtras.precaptureTriggerId = triggerId; |
| 4361 | mCurrentPreCaptureTriggerId = triggerId; |
| 4362 | } else { |
| 4363 | request->mResultExtras.afTriggerId = triggerId; |
| 4364 | mCurrentAfTriggerId = triggerId; |
| 4365 | } |
Emilian Peev | 7e25e5e | 2017-04-07 15:48:49 +0100 | [diff] [blame] | 4366 | continue; |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 4367 | } |
| 4368 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 4369 | camera_metadata_entry entry = metadata.find(tag); |
| 4370 | |
| 4371 | if (entry.count > 0) { |
| 4372 | /** |
| 4373 | * Already has an entry for this trigger in the request. |
| 4374 | * Rewrite it with our requested trigger value. |
| 4375 | */ |
| 4376 | RequestTrigger oldTrigger = trigger; |
| 4377 | |
| 4378 | oldTrigger.entryValue = entry.data.u8[0]; |
| 4379 | |
| 4380 | mTriggerReplacedMap.add(tag, oldTrigger); |
| 4381 | } else { |
| 4382 | /** |
| 4383 | * More typical, no trigger entry, so we just add it |
| 4384 | */ |
| 4385 | mTriggerRemovedMap.add(tag, trigger); |
| 4386 | } |
| 4387 | |
| 4388 | status_t res; |
| 4389 | |
| 4390 | switch (trigger.getTagType()) { |
| 4391 | case TYPE_BYTE: { |
| 4392 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 4393 | res = metadata.update(tag, |
| 4394 | &entryValue, |
| 4395 | /*count*/1); |
| 4396 | break; |
| 4397 | } |
| 4398 | case TYPE_INT32: |
| 4399 | res = metadata.update(tag, |
| 4400 | &trigger.entryValue, |
| 4401 | /*count*/1); |
| 4402 | break; |
| 4403 | default: |
| 4404 | ALOGE("%s: Type not supported: 0x%x", |
| 4405 | __FUNCTION__, |
| 4406 | trigger.getTagType()); |
| 4407 | return INVALID_OPERATION; |
| 4408 | } |
| 4409 | |
| 4410 | if (res != OK) { |
| 4411 | ALOGE("%s: Failed to update request metadata with trigger tag %s" |
| 4412 | ", value %d", __FUNCTION__, trigger.getTagName(), |
| 4413 | trigger.entryValue); |
| 4414 | return res; |
| 4415 | } |
| 4416 | |
| 4417 | ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, |
| 4418 | trigger.getTagName(), |
| 4419 | trigger.entryValue); |
| 4420 | } |
| 4421 | |
| 4422 | mTriggerMap.clear(); |
| 4423 | |
| 4424 | return count; |
| 4425 | } |
| 4426 | |
| 4427 | status_t Camera3Device::RequestThread::removeTriggers( |
| 4428 | const sp<CaptureRequest> &request) { |
| 4429 | Mutex::Autolock al(mTriggerMutex); |
| 4430 | |
| 4431 | CameraMetadata &metadata = request->mSettings; |
| 4432 | |
| 4433 | /** |
| 4434 | * Replace all old entries with their old values. |
| 4435 | */ |
| 4436 | for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { |
| 4437 | RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); |
| 4438 | |
| 4439 | status_t res; |
| 4440 | |
| 4441 | uint32_t tag = trigger.metadataTag; |
| 4442 | switch (trigger.getTagType()) { |
| 4443 | case TYPE_BYTE: { |
| 4444 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 4445 | res = metadata.update(tag, |
| 4446 | &entryValue, |
| 4447 | /*count*/1); |
| 4448 | break; |
| 4449 | } |
| 4450 | case TYPE_INT32: |
| 4451 | res = metadata.update(tag, |
| 4452 | &trigger.entryValue, |
| 4453 | /*count*/1); |
| 4454 | break; |
| 4455 | default: |
| 4456 | ALOGE("%s: Type not supported: 0x%x", |
| 4457 | __FUNCTION__, |
| 4458 | trigger.getTagType()); |
| 4459 | return INVALID_OPERATION; |
| 4460 | } |
| 4461 | |
| 4462 | if (res != OK) { |
| 4463 | ALOGE("%s: Failed to restore request metadata with trigger tag %s" |
| 4464 | ", trigger value %d", __FUNCTION__, |
| 4465 | trigger.getTagName(), trigger.entryValue); |
| 4466 | return res; |
| 4467 | } |
| 4468 | } |
| 4469 | mTriggerReplacedMap.clear(); |
| 4470 | |
| 4471 | /** |
| 4472 | * Remove all new entries. |
| 4473 | */ |
| 4474 | for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { |
| 4475 | RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); |
| 4476 | status_t res = metadata.erase(trigger.metadataTag); |
| 4477 | |
| 4478 | if (res != OK) { |
| 4479 | ALOGE("%s: Failed to erase metadata with trigger tag %s" |
| 4480 | ", trigger value %d", __FUNCTION__, |
| 4481 | trigger.getTagName(), trigger.entryValue); |
| 4482 | return res; |
| 4483 | } |
| 4484 | } |
| 4485 | mTriggerRemovedMap.clear(); |
| 4486 | |
| 4487 | return OK; |
| 4488 | } |
| 4489 | |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 4490 | status_t Camera3Device::RequestThread::addDummyTriggerIds( |
| 4491 | const sp<CaptureRequest> &request) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 4492 | // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 4493 | static const int32_t dummyTriggerId = 1; |
| 4494 | status_t res; |
| 4495 | |
| 4496 | CameraMetadata &metadata = request->mSettings; |
| 4497 | |
| 4498 | // If AF trigger is active, insert a dummy AF trigger ID if none already |
| 4499 | // exists |
| 4500 | camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER); |
| 4501 | camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID); |
| 4502 | if (afTrigger.count > 0 && |
| 4503 | afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE && |
| 4504 | afId.count == 0) { |
| 4505 | res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1); |
| 4506 | if (res != OK) return res; |
| 4507 | } |
| 4508 | |
| 4509 | // If AE precapture trigger is active, insert a dummy precapture trigger ID |
| 4510 | // if none already exists |
| 4511 | camera_metadata_entry pcTrigger = |
| 4512 | metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); |
| 4513 | camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); |
| 4514 | if (pcTrigger.count > 0 && |
| 4515 | pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE && |
| 4516 | pcId.count == 0) { |
| 4517 | res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID, |
| 4518 | &dummyTriggerId, 1); |
| 4519 | if (res != OK) return res; |
| 4520 | } |
| 4521 | |
| 4522 | return OK; |
| 4523 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 4524 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4525 | /** |
| 4526 | * PreparerThread inner class methods |
| 4527 | */ |
| 4528 | |
| 4529 | Camera3Device::PreparerThread::PreparerThread() : |
Eino-Ville Talvala | 77c1a35 | 2016-06-13 12:32:43 -0700 | [diff] [blame] | 4530 | Thread(/*canCallJava*/false), mListener(nullptr), |
| 4531 | mActive(false), mCancelNow(false) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | Camera3Device::PreparerThread::~PreparerThread() { |
| 4535 | Thread::requestExitAndWait(); |
| 4536 | if (mCurrentStream != nullptr) { |
| 4537 | mCurrentStream->cancelPrepare(); |
| 4538 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 4539 | mCurrentStream.clear(); |
| 4540 | } |
| 4541 | clear(); |
| 4542 | } |
| 4543 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 4544 | status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4545 | status_t res; |
| 4546 | |
| 4547 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4548 | sp<NotificationListener> listener = mListener.promote(); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4549 | |
Ruben Brunk | c78ac26 | 2015-08-13 17:58:46 -0700 | [diff] [blame] | 4550 | res = stream->startPrepare(maxCount); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4551 | if (res == OK) { |
| 4552 | // No preparation needed, fire listener right off |
| 4553 | ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId()); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4554 | if (listener != NULL) { |
| 4555 | listener->notifyPrepared(stream->getId()); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4556 | } |
| 4557 | return OK; |
| 4558 | } else if (res != NOT_ENOUGH_DATA) { |
| 4559 | return res; |
| 4560 | } |
| 4561 | |
| 4562 | // Need to prepare, start up thread if necessary |
| 4563 | if (!mActive) { |
| 4564 | // mRunning will change to false before the thread fully shuts down, so wait to be sure it |
| 4565 | // isn't running |
| 4566 | Thread::requestExitAndWait(); |
| 4567 | res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND); |
| 4568 | if (res != OK) { |
| 4569 | ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res)); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4570 | if (listener != NULL) { |
| 4571 | listener->notifyPrepared(stream->getId()); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4572 | } |
| 4573 | return res; |
| 4574 | } |
| 4575 | mCancelNow = false; |
| 4576 | mActive = true; |
| 4577 | ALOGV("%s: Preparer stream started", __FUNCTION__); |
| 4578 | } |
| 4579 | |
| 4580 | // queue up the work |
| 4581 | mPendingStreams.push_back(stream); |
| 4582 | ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId()); |
| 4583 | |
| 4584 | return OK; |
| 4585 | } |
| 4586 | |
| 4587 | status_t Camera3Device::PreparerThread::clear() { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4588 | Mutex::Autolock l(mLock); |
| 4589 | |
| 4590 | for (const auto& stream : mPendingStreams) { |
| 4591 | stream->cancelPrepare(); |
| 4592 | } |
| 4593 | mPendingStreams.clear(); |
| 4594 | mCancelNow = true; |
| 4595 | |
| 4596 | return OK; |
| 4597 | } |
| 4598 | |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4599 | void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4600 | Mutex::Autolock l(mLock); |
| 4601 | mListener = listener; |
| 4602 | } |
| 4603 | |
| 4604 | bool Camera3Device::PreparerThread::threadLoop() { |
| 4605 | status_t res; |
| 4606 | { |
| 4607 | Mutex::Autolock l(mLock); |
| 4608 | if (mCurrentStream == nullptr) { |
| 4609 | // End thread if done with work |
| 4610 | if (mPendingStreams.empty()) { |
| 4611 | ALOGV("%s: Preparer stream out of work", __FUNCTION__); |
| 4612 | // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would |
| 4613 | // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive. |
| 4614 | mActive = false; |
| 4615 | return false; |
| 4616 | } |
| 4617 | |
| 4618 | // Get next stream to prepare |
| 4619 | auto it = mPendingStreams.begin(); |
| 4620 | mCurrentStream = *it; |
| 4621 | mPendingStreams.erase(it); |
| 4622 | ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId()); |
| 4623 | ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId()); |
| 4624 | } else if (mCancelNow) { |
| 4625 | mCurrentStream->cancelPrepare(); |
| 4626 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 4627 | ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId()); |
| 4628 | mCurrentStream.clear(); |
| 4629 | mCancelNow = false; |
| 4630 | return true; |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | res = mCurrentStream->prepareNextBuffer(); |
| 4635 | if (res == NOT_ENOUGH_DATA) return true; |
| 4636 | if (res != OK) { |
| 4637 | // Something bad happened; try to recover by cancelling prepare and |
| 4638 | // signalling listener anyway |
| 4639 | ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__, |
| 4640 | mCurrentStream->getId(), res, strerror(-res)); |
| 4641 | mCurrentStream->cancelPrepare(); |
| 4642 | } |
| 4643 | |
| 4644 | // This stream has finished, notify listener |
| 4645 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4646 | sp<NotificationListener> listener = mListener.promote(); |
| 4647 | if (listener != NULL) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4648 | ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__, |
| 4649 | mCurrentStream->getId()); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 4650 | listener->notifyPrepared(mCurrentStream->getId()); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 4654 | mCurrentStream.clear(); |
| 4655 | |
| 4656 | return true; |
| 4657 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 4658 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 4659 | /** |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 4660 | * Static callback forwarding methods from HAL to instance |
| 4661 | */ |
| 4662 | |
| 4663 | void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, |
| 4664 | const camera3_capture_result *result) { |
| 4665 | Camera3Device *d = |
| 4666 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 4667 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 4668 | d->processCaptureResult(result); |
| 4669 | } |
| 4670 | |
| 4671 | void Camera3Device::sNotify(const camera3_callback_ops *cb, |
| 4672 | const camera3_notify_msg *msg) { |
| 4673 | Camera3Device *d = |
| 4674 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
| 4675 | d->notify(msg); |
| 4676 | } |
| 4677 | |
| 4678 | }; // namespace android |