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