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