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 |
| 29 | #define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \ |
| 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> |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 45 | |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 46 | #include "utils/CameraTraces.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 47 | #include "device3/Camera3Device.h" |
| 48 | #include "device3/Camera3OutputStream.h" |
| 49 | #include "device3/Camera3InputStream.h" |
| 50 | #include "device3/Camera3ZslStream.h" |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 51 | #include "device3/Camera3DummyStream.h" |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 52 | #include "CameraService.h" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 53 | |
| 54 | using namespace android::camera3; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 55 | |
| 56 | namespace android { |
| 57 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 58 | Camera3Device::Camera3Device(int id): |
| 59 | mId(id), |
Eino-Ville Talvala | 9a17941 | 2015-06-09 13:15:16 -0700 | [diff] [blame] | 60 | mIsConstrainedHighSpeedConfiguration(false), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 61 | mHal3Device(NULL), |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 62 | mStatus(STATUS_UNINITIALIZED), |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 63 | mUsePartialResult(false), |
| 64 | mNumPartialResults(1), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 65 | mNextResultFrameNumber(0), |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 66 | mNextReprocessResultFrameNumber(0), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 67 | mNextShutterFrameNumber(0), |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 68 | mListener(NULL) |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 69 | { |
| 70 | ATRACE_CALL(); |
| 71 | camera3_callback_ops::notify = &sNotify; |
| 72 | camera3_callback_ops::process_capture_result = &sProcessCaptureResult; |
| 73 | ALOGV("%s: Created device for camera %d", __FUNCTION__, id); |
| 74 | } |
| 75 | |
| 76 | Camera3Device::~Camera3Device() |
| 77 | { |
| 78 | ATRACE_CALL(); |
| 79 | ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId); |
| 80 | disconnect(); |
| 81 | } |
| 82 | |
Igor Murashkin | 7138105 | 2013-03-04 14:53:08 -0800 | [diff] [blame] | 83 | int Camera3Device::getId() const { |
| 84 | return mId; |
| 85 | } |
| 86 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 87 | /** |
| 88 | * CameraDeviceBase interface |
| 89 | */ |
| 90 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 91 | status_t Camera3Device::initialize(CameraModule *module) |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 92 | { |
| 93 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 94 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 95 | Mutex::Autolock l(mLock); |
| 96 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 97 | ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 98 | if (mStatus != STATUS_UNINITIALIZED) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 99 | CLOGE("Already initialized!"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 100 | return INVALID_OPERATION; |
| 101 | } |
| 102 | |
| 103 | /** Open HAL device */ |
| 104 | |
| 105 | status_t res; |
| 106 | String8 deviceName = String8::format("%d", mId); |
| 107 | |
| 108 | camera3_device_t *device; |
| 109 | |
Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 110 | ATRACE_BEGIN("camera3->open"); |
Chien-Yu Chen | d231fd6 | 2015-02-25 16:04:22 -0800 | [diff] [blame] | 111 | res = module->open(deviceName.string(), |
| 112 | reinterpret_cast<hw_device_t**>(&device)); |
Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 113 | ATRACE_END(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 114 | |
| 115 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 116 | 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] | 117 | return res; |
| 118 | } |
| 119 | |
| 120 | /** Cross-check device version */ |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 121 | if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 122 | SET_ERR_L("Could not open camera: " |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 123 | "Camera device should be at least %x, reports %x instead", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 124 | CAMERA_DEVICE_API_VERSION_3_0, |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 125 | device->common.version); |
| 126 | device->common.close(&device->common); |
| 127 | return BAD_VALUE; |
| 128 | } |
| 129 | |
| 130 | camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 131 | res = CameraService::filterGetInfoErrorCode(module->getCameraInfo( |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 132 | mId, &info)); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 133 | if (res != OK) return res; |
| 134 | |
| 135 | if (info.device_version != device->common.version) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 136 | SET_ERR_L("HAL reporting mismatched camera_info version (%x)" |
| 137 | " and device version (%x).", |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 138 | info.device_version, device->common.version); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 139 | device->common.close(&device->common); |
| 140 | return BAD_VALUE; |
| 141 | } |
| 142 | |
| 143 | /** Initialize device with callback functions */ |
| 144 | |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 145 | ATRACE_BEGIN("camera3->initialize"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 146 | res = device->ops->initialize(device, this); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 147 | ATRACE_END(); |
| 148 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 149 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 150 | SET_ERR_L("Unable to initialize HAL device: %s (%d)", |
| 151 | strerror(-res), res); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 152 | device->common.close(&device->common); |
| 153 | return BAD_VALUE; |
| 154 | } |
| 155 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 156 | /** Start up status tracker thread */ |
| 157 | mStatusTracker = new StatusTracker(this); |
| 158 | res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string()); |
| 159 | if (res != OK) { |
| 160 | SET_ERR_L("Unable to start status tracking thread: %s (%d)", |
| 161 | strerror(-res), res); |
| 162 | device->common.close(&device->common); |
| 163 | mStatusTracker.clear(); |
| 164 | return res; |
| 165 | } |
| 166 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 167 | /** Start up request queue thread */ |
| 168 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 169 | mRequestThread = new RequestThread(this, mStatusTracker, device); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 170 | res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string()); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 171 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 172 | SET_ERR_L("Unable to start request queue thread: %s (%d)", |
| 173 | strerror(-res), res); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 174 | device->common.close(&device->common); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 175 | mRequestThread.clear(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 176 | return res; |
| 177 | } |
| 178 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 179 | mPreparerThread = new PreparerThread(); |
| 180 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 181 | /** Everything is good to go */ |
| 182 | |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 183 | mDeviceVersion = device->common.version; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 184 | mDeviceInfo = info.static_camera_characteristics; |
| 185 | mHal3Device = device; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 186 | mStatus = STATUS_UNCONFIGURED; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 187 | mNextStreamId = 0; |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 188 | mDummyStreamId = NO_STREAM; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 189 | mNeedConfig = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 190 | mPauseStateNotify = false; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 191 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 192 | // Will the HAL be sending in early partial result metadata? |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 193 | if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 194 | camera_metadata_entry partialResultsCount = |
| 195 | mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT); |
| 196 | if (partialResultsCount.count > 0) { |
| 197 | mNumPartialResults = partialResultsCount.data.i32[0]; |
| 198 | mUsePartialResult = (mNumPartialResults > 1); |
| 199 | } |
| 200 | } else { |
| 201 | camera_metadata_entry partialResultsQuirk = |
| 202 | mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT); |
| 203 | if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) { |
| 204 | mUsePartialResult = true; |
| 205 | } |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 208 | camera_metadata_entry configs = |
| 209 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); |
| 210 | for (uint32_t i = 0; i < configs.count; i += 4) { |
| 211 | if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && |
| 212 | configs.data.i32[i + 3] == |
| 213 | ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) { |
| 214 | mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1], |
| 215 | configs.data.i32[i + 2])); |
| 216 | } |
| 217 | } |
| 218 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 219 | return OK; |
| 220 | } |
| 221 | |
| 222 | status_t Camera3Device::disconnect() { |
| 223 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 224 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 225 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 226 | ALOGV("%s: E", __FUNCTION__); |
| 227 | |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 228 | status_t res = OK; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 229 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 230 | { |
| 231 | Mutex::Autolock l(mLock); |
| 232 | if (mStatus == STATUS_UNINITIALIZED) return res; |
| 233 | |
| 234 | if (mStatus == STATUS_ACTIVE || |
| 235 | (mStatus == STATUS_ERROR && mRequestThread != NULL)) { |
| 236 | res = mRequestThread->clearRepeatingRequests(); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 237 | if (res != OK) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 238 | SET_ERR_L("Can't stop streaming"); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 239 | // Continue to close device even in case of error |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 240 | } else { |
| 241 | res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
| 242 | if (res != OK) { |
| 243 | SET_ERR_L("Timeout waiting for HAL to drain"); |
| 244 | // Continue to close device even in case of error |
| 245 | } |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 246 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 247 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 248 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 249 | if (mStatus == STATUS_ERROR) { |
| 250 | CLOGE("Shutting down in an error state"); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 251 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 252 | |
| 253 | if (mStatusTracker != NULL) { |
| 254 | mStatusTracker->requestExit(); |
| 255 | } |
| 256 | |
| 257 | if (mRequestThread != NULL) { |
| 258 | mRequestThread->requestExit(); |
| 259 | } |
| 260 | |
| 261 | mOutputStreams.clear(); |
| 262 | mInputStream.clear(); |
| 263 | } |
| 264 | |
| 265 | // Joining done without holding mLock, otherwise deadlocks may ensue |
| 266 | // as the threads try to access parent state |
| 267 | if (mRequestThread != NULL && mStatus != STATUS_ERROR) { |
| 268 | // HAL may be in a bad state, so waiting for request thread |
| 269 | // (which may be stuck in the HAL processCaptureRequest call) |
| 270 | // could be dangerous. |
| 271 | mRequestThread->join(); |
| 272 | } |
| 273 | |
| 274 | if (mStatusTracker != NULL) { |
| 275 | mStatusTracker->join(); |
| 276 | } |
| 277 | |
| 278 | { |
| 279 | Mutex::Autolock l(mLock); |
| 280 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 281 | mRequestThread.clear(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 282 | mStatusTracker.clear(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 283 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 284 | if (mHal3Device != NULL) { |
Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 285 | ATRACE_BEGIN("camera3->close"); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 286 | mHal3Device->common.close(&mHal3Device->common); |
Zhijun He | 213ce79 | 2013-11-19 08:45:15 -0800 | [diff] [blame] | 287 | ATRACE_END(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 288 | mHal3Device = NULL; |
| 289 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 290 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 291 | mStatus = STATUS_UNINITIALIZED; |
| 292 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 293 | |
| 294 | ALOGV("%s: X", __FUNCTION__); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 295 | return res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 298 | // For dumping/debugging only - |
| 299 | // try to acquire a lock a few times, eventually give up to proceed with |
| 300 | // debug/dump operations |
| 301 | bool Camera3Device::tryLockSpinRightRound(Mutex& lock) { |
| 302 | bool gotLock = false; |
| 303 | for (size_t i = 0; i < kDumpLockAttempts; ++i) { |
| 304 | if (lock.tryLock() == NO_ERROR) { |
| 305 | gotLock = true; |
| 306 | break; |
| 307 | } else { |
| 308 | usleep(kDumpSleepDuration); |
| 309 | } |
| 310 | } |
| 311 | return gotLock; |
| 312 | } |
| 313 | |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 314 | Camera3Device::Size Camera3Device::getMaxJpegResolution() const { |
| 315 | int32_t maxJpegWidth = 0, maxJpegHeight = 0; |
| 316 | if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 317 | const int STREAM_CONFIGURATION_SIZE = 4; |
| 318 | const int STREAM_FORMAT_OFFSET = 0; |
| 319 | const int STREAM_WIDTH_OFFSET = 1; |
| 320 | const int STREAM_HEIGHT_OFFSET = 2; |
| 321 | const int STREAM_IS_INPUT_OFFSET = 3; |
| 322 | camera_metadata_ro_entry_t availableStreamConfigs = |
| 323 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); |
| 324 | if (availableStreamConfigs.count == 0 || |
| 325 | availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) { |
| 326 | return Size(0, 0); |
| 327 | } |
| 328 | |
| 329 | // Get max jpeg size (area-wise). |
| 330 | for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) { |
| 331 | int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET]; |
| 332 | int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET]; |
| 333 | int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET]; |
| 334 | int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET]; |
| 335 | if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT |
| 336 | && format == HAL_PIXEL_FORMAT_BLOB && |
| 337 | (width * height > maxJpegWidth * maxJpegHeight)) { |
| 338 | maxJpegWidth = width; |
| 339 | maxJpegHeight = height; |
| 340 | } |
| 341 | } |
| 342 | } else { |
| 343 | camera_metadata_ro_entry availableJpegSizes = |
| 344 | mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES); |
| 345 | if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) { |
| 346 | return Size(0, 0); |
| 347 | } |
| 348 | |
| 349 | // Get max jpeg size (area-wise). |
| 350 | for (size_t i = 0; i < availableJpegSizes.count; i += 2) { |
| 351 | if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1]) |
| 352 | > (maxJpegWidth * maxJpegHeight)) { |
| 353 | maxJpegWidth = availableJpegSizes.data.i32[i]; |
| 354 | maxJpegHeight = availableJpegSizes.data.i32[i + 1]; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | return Size(maxJpegWidth, maxJpegHeight); |
| 359 | } |
| 360 | |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 361 | ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const { |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 362 | // Get max jpeg size (area-wise). |
| 363 | Size maxJpegResolution = getMaxJpegResolution(); |
| 364 | if (maxJpegResolution.width == 0) { |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 365 | ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!", |
| 366 | __FUNCTION__, mId); |
| 367 | return BAD_VALUE; |
| 368 | } |
| 369 | |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 370 | // Get max jpeg buffer size |
| 371 | ssize_t maxJpegBufferSize = 0; |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 372 | camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE); |
| 373 | if (jpegBufMaxSize.count == 0) { |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 374 | ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId); |
| 375 | return BAD_VALUE; |
| 376 | } |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 377 | maxJpegBufferSize = jpegBufMaxSize.data.i32[0]; |
Yin-Chia Yeh | 0c4e56d | 2015-01-09 15:21:27 -0800 | [diff] [blame] | 378 | assert(kMinJpegBufferSize < maxJpegBufferSize); |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 379 | |
| 380 | // Calculate final jpeg buffer size for the given resolution. |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 381 | float scaleFactor = ((float) (width * height)) / |
| 382 | (maxJpegResolution.width * maxJpegResolution.height); |
Yin-Chia Yeh | 0c4e56d | 2015-01-09 15:21:27 -0800 | [diff] [blame] | 383 | ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) + |
| 384 | kMinJpegBufferSize; |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 385 | if (jpegBufferSize > maxJpegBufferSize) { |
| 386 | jpegBufferSize = maxJpegBufferSize; |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | return jpegBufferSize; |
| 390 | } |
| 391 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 392 | status_t Camera3Device::dump(int fd, const Vector<String16> &args) { |
| 393 | ATRACE_CALL(); |
| 394 | (void)args; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 395 | |
| 396 | // Try to lock, but continue in case of failure (to avoid blocking in |
| 397 | // deadlocks) |
| 398 | bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock); |
| 399 | bool gotLock = tryLockSpinRightRound(mLock); |
| 400 | |
| 401 | ALOGW_IF(!gotInterfaceLock, |
| 402 | "Camera %d: %s: Unable to lock interface lock, proceeding anyway", |
| 403 | mId, __FUNCTION__); |
| 404 | ALOGW_IF(!gotLock, |
| 405 | "Camera %d: %s: Unable to lock main lock, proceeding anyway", |
| 406 | mId, __FUNCTION__); |
| 407 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 408 | String8 lines; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 409 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 410 | const char *status = |
| 411 | mStatus == STATUS_ERROR ? "ERROR" : |
| 412 | mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 413 | mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" : |
| 414 | mStatus == STATUS_CONFIGURED ? "CONFIGURED" : |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 415 | mStatus == STATUS_ACTIVE ? "ACTIVE" : |
| 416 | "Unknown"; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 417 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 418 | lines.appendFormat(" Device status: %s\n", status); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 419 | if (mStatus == STATUS_ERROR) { |
| 420 | lines.appendFormat(" Error cause: %s\n", mErrorCause.string()); |
| 421 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 422 | lines.appendFormat(" Stream configuration:\n"); |
Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame] | 423 | lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ? |
Eino-Ville Talvala | 9a17941 | 2015-06-09 13:15:16 -0700 | [diff] [blame] | 424 | "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 425 | |
| 426 | if (mInputStream != NULL) { |
| 427 | write(fd, lines.string(), lines.size()); |
| 428 | mInputStream->dump(fd, args); |
| 429 | } else { |
| 430 | lines.appendFormat(" No input stream.\n"); |
| 431 | write(fd, lines.string(), lines.size()); |
| 432 | } |
| 433 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
| 434 | mOutputStreams[i]->dump(fd,args); |
| 435 | } |
| 436 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 437 | lines = String8(" In-flight requests:\n"); |
| 438 | if (mInFlightMap.size() == 0) { |
| 439 | lines.append(" None\n"); |
| 440 | } else { |
| 441 | for (size_t i = 0; i < mInFlightMap.size(); i++) { |
| 442 | InFlightRequest r = mInFlightMap.valueAt(i); |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 443 | lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata" |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 444 | " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 445 | r.shutterTimestamp, r.haveResultMetadata ? "true" : "false", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 446 | r.numBuffersLeft); |
| 447 | } |
| 448 | } |
| 449 | write(fd, lines.string(), lines.size()); |
| 450 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 451 | { |
| 452 | lines = String8(" Last request sent:\n"); |
| 453 | write(fd, lines.string(), lines.size()); |
| 454 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 455 | CameraMetadata lastRequest = getLatestRequestLocked(); |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 456 | lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6); |
| 457 | } |
| 458 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 459 | if (mHal3Device != NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 460 | lines = String8(" HAL device dump:\n"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 461 | write(fd, lines.string(), lines.size()); |
| 462 | mHal3Device->ops->dump(mHal3Device, fd); |
| 463 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 464 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 465 | if (gotLock) mLock.unlock(); |
| 466 | if (gotInterfaceLock) mInterfaceLock.unlock(); |
| 467 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 468 | return OK; |
| 469 | } |
| 470 | |
| 471 | const CameraMetadata& Camera3Device::info() const { |
| 472 | ALOGVV("%s: E", __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 473 | if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || |
| 474 | mStatus == STATUS_ERROR)) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 475 | ALOGW("%s: Access to static info %s!", __FUNCTION__, |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 476 | mStatus == STATUS_ERROR ? |
| 477 | "when in error state" : "before init"); |
| 478 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 479 | return mDeviceInfo; |
| 480 | } |
| 481 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 482 | status_t Camera3Device::checkStatusOkToCaptureLocked() { |
| 483 | switch (mStatus) { |
| 484 | case STATUS_ERROR: |
| 485 | CLOGE("Device has encountered a serious error"); |
| 486 | return INVALID_OPERATION; |
| 487 | case STATUS_UNINITIALIZED: |
| 488 | CLOGE("Device not initialized"); |
| 489 | return INVALID_OPERATION; |
| 490 | case STATUS_UNCONFIGURED: |
| 491 | case STATUS_CONFIGURED: |
| 492 | case STATUS_ACTIVE: |
| 493 | // OK |
| 494 | break; |
| 495 | default: |
| 496 | SET_ERR_L("Unexpected status: %d", mStatus); |
| 497 | return INVALID_OPERATION; |
| 498 | } |
| 499 | return OK; |
| 500 | } |
| 501 | |
| 502 | status_t Camera3Device::convertMetadataListToRequestListLocked( |
| 503 | const List<const CameraMetadata> &metadataList, RequestList *requestList) { |
| 504 | if (requestList == NULL) { |
| 505 | CLOGE("requestList cannot be NULL."); |
| 506 | return BAD_VALUE; |
| 507 | } |
| 508 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 509 | int32_t burstId = 0; |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 510 | for (List<const CameraMetadata>::const_iterator it = metadataList.begin(); |
| 511 | it != metadataList.end(); ++it) { |
| 512 | sp<CaptureRequest> newRequest = setUpRequestLocked(*it); |
| 513 | if (newRequest == 0) { |
| 514 | CLOGE("Can't create capture request"); |
| 515 | return BAD_VALUE; |
| 516 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 517 | |
| 518 | // Setup burst Id and request Id |
| 519 | newRequest->mResultExtras.burstId = burstId++; |
| 520 | if (it->exists(ANDROID_REQUEST_ID)) { |
| 521 | if (it->find(ANDROID_REQUEST_ID).count == 0) { |
| 522 | CLOGE("RequestID entry exists; but must not be empty in metadata"); |
| 523 | return BAD_VALUE; |
| 524 | } |
| 525 | newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0]; |
| 526 | } else { |
| 527 | CLOGE("RequestID does not exist in metadata"); |
| 528 | return BAD_VALUE; |
| 529 | } |
| 530 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 531 | requestList->push_back(newRequest); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 532 | |
| 533 | ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 534 | } |
| 535 | return OK; |
| 536 | } |
| 537 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 538 | status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 539 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 540 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 541 | List<const CameraMetadata> requests; |
| 542 | requests.push_back(request); |
| 543 | return captureList(requests, /*lastFrameNumber*/NULL); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 546 | status_t Camera3Device::submitRequestsHelper( |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 547 | const List<const CameraMetadata> &requests, bool repeating, |
| 548 | /*out*/ |
| 549 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 550 | ATRACE_CALL(); |
| 551 | Mutex::Autolock il(mInterfaceLock); |
| 552 | Mutex::Autolock l(mLock); |
| 553 | |
| 554 | status_t res = checkStatusOkToCaptureLocked(); |
| 555 | if (res != OK) { |
| 556 | // error logged by previous call |
| 557 | return res; |
| 558 | } |
| 559 | |
| 560 | RequestList requestList; |
| 561 | |
| 562 | res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList); |
| 563 | if (res != OK) { |
| 564 | // error logged by previous call |
| 565 | return res; |
| 566 | } |
| 567 | |
| 568 | if (repeating) { |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 569 | res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 570 | } else { |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 571 | res = mRequestThread->queueRequestList(requestList, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (res == OK) { |
| 575 | waitUntilStateThenRelock(/*active*/true, kActiveTimeout); |
| 576 | if (res != OK) { |
| 577 | SET_ERR_L("Can't transition to active in %f seconds!", |
| 578 | kActiveTimeout/1e9); |
| 579 | } |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 580 | ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId, |
| 581 | (*(requestList.begin()))->mResultExtras.requestId); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 582 | } else { |
| 583 | CLOGE("Cannot queue request. Impossible."); |
| 584 | return BAD_VALUE; |
| 585 | } |
| 586 | |
| 587 | return res; |
| 588 | } |
| 589 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 590 | status_t Camera3Device::captureList(const List<const CameraMetadata> &requests, |
| 591 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 592 | ATRACE_CALL(); |
| 593 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 594 | return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 595 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 596 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 597 | status_t Camera3Device::setStreamingRequest(const CameraMetadata &request, |
| 598 | int64_t* /*lastFrameNumber*/) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 599 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 600 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 601 | List<const CameraMetadata> requests; |
| 602 | requests.push_back(request); |
| 603 | return setStreamingRequestList(requests, /*lastFrameNumber*/NULL); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 606 | status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests, |
| 607 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 608 | ATRACE_CALL(); |
| 609 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 610 | return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber); |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 611 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 612 | |
| 613 | sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( |
| 614 | const CameraMetadata &request) { |
| 615 | status_t res; |
| 616 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 617 | if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 618 | res = configureStreamsLocked(); |
Yin-Chia Yeh | 3ea3fcd | 2014-09-05 14:14:44 -0700 | [diff] [blame] | 619 | // Stream configuration failed due to unsupported configuration. |
| 620 | // Device back to unconfigured state. Client might try other configuraitons |
| 621 | if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) { |
| 622 | CLOGE("No streams configured"); |
| 623 | return NULL; |
| 624 | } |
| 625 | // Stream configuration failed for other reason. Fatal. |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 626 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 627 | SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 628 | return NULL; |
| 629 | } |
Yin-Chia Yeh | 3ea3fcd | 2014-09-05 14:14:44 -0700 | [diff] [blame] | 630 | // Stream configuration successfully configure to empty stream configuration. |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 631 | if (mStatus == STATUS_UNCONFIGURED) { |
| 632 | CLOGE("No streams configured"); |
| 633 | return NULL; |
| 634 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | sp<CaptureRequest> newRequest = createCaptureRequest(request); |
| 638 | return newRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 641 | status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 642 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 643 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 644 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 645 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 646 | switch (mStatus) { |
| 647 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 648 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 649 | return INVALID_OPERATION; |
| 650 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 651 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 652 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 653 | case STATUS_UNCONFIGURED: |
| 654 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 655 | case STATUS_ACTIVE: |
| 656 | // OK |
| 657 | break; |
| 658 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 659 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 660 | return INVALID_OPERATION; |
| 661 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 662 | ALOGV("Camera %d: Clearing repeating request", mId); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 663 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 664 | return mRequestThread->clearRepeatingRequests(lastFrameNumber); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { |
| 668 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 669 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 670 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 671 | return mRequestThread->waitUntilRequestProcessed(requestId, timeout); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 674 | status_t Camera3Device::createInputStream( |
| 675 | uint32_t width, uint32_t height, int format, int *id) { |
| 676 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 677 | Mutex::Autolock il(mInterfaceLock); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 678 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 679 | ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d", |
| 680 | mId, mNextStreamId, width, height, format); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 681 | |
| 682 | status_t res; |
| 683 | bool wasActive = false; |
| 684 | |
| 685 | switch (mStatus) { |
| 686 | case STATUS_ERROR: |
| 687 | ALOGE("%s: Device has encountered a serious error", __FUNCTION__); |
| 688 | return INVALID_OPERATION; |
| 689 | case STATUS_UNINITIALIZED: |
| 690 | ALOGE("%s: Device not initialized", __FUNCTION__); |
| 691 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 692 | case STATUS_UNCONFIGURED: |
| 693 | case STATUS_CONFIGURED: |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 694 | // OK |
| 695 | break; |
| 696 | case STATUS_ACTIVE: |
| 697 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 698 | res = internalPauseAndWaitLocked(); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 699 | if (res != OK) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 700 | SET_ERR_L("Can't pause captures to reconfigure streams!"); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 701 | return res; |
| 702 | } |
| 703 | wasActive = true; |
| 704 | break; |
| 705 | default: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 706 | SET_ERR_L("%s: Unexpected status: %d", mStatus); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 707 | return INVALID_OPERATION; |
| 708 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 709 | assert(mStatus != STATUS_ACTIVE); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 710 | |
| 711 | if (mInputStream != 0) { |
| 712 | ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); |
| 713 | return INVALID_OPERATION; |
| 714 | } |
| 715 | |
| 716 | sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, |
| 717 | width, height, format); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 718 | newStream->setStatusTracker(mStatusTracker); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 719 | |
| 720 | mInputStream = newStream; |
| 721 | |
| 722 | *id = mNextStreamId++; |
| 723 | |
| 724 | // Continue captures if active at start |
| 725 | if (wasActive) { |
| 726 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
| 727 | res = configureStreamsLocked(); |
| 728 | if (res != OK) { |
| 729 | ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", |
| 730 | __FUNCTION__, mNextStreamId, strerror(-res), res); |
| 731 | return res; |
| 732 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 733 | internalResumeLocked(); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 736 | ALOGV("Camera %d: Created input stream", mId); |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 737 | return OK; |
| 738 | } |
| 739 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 740 | |
| 741 | status_t Camera3Device::createZslStream( |
| 742 | uint32_t width, uint32_t height, |
| 743 | int depth, |
| 744 | /*out*/ |
| 745 | int *id, |
| 746 | sp<Camera3ZslStream>* zslStream) { |
| 747 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 748 | Mutex::Autolock il(mInterfaceLock); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 749 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 750 | ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d", |
| 751 | mId, mNextStreamId, width, height, depth); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 752 | |
| 753 | status_t res; |
| 754 | bool wasActive = false; |
| 755 | |
| 756 | switch (mStatus) { |
| 757 | case STATUS_ERROR: |
| 758 | ALOGE("%s: Device has encountered a serious error", __FUNCTION__); |
| 759 | return INVALID_OPERATION; |
| 760 | case STATUS_UNINITIALIZED: |
| 761 | ALOGE("%s: Device not initialized", __FUNCTION__); |
| 762 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 763 | case STATUS_UNCONFIGURED: |
| 764 | case STATUS_CONFIGURED: |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 765 | // OK |
| 766 | break; |
| 767 | case STATUS_ACTIVE: |
| 768 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 769 | res = internalPauseAndWaitLocked(); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 770 | if (res != OK) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 771 | SET_ERR_L("Can't pause captures to reconfigure streams!"); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 772 | return res; |
| 773 | } |
| 774 | wasActive = true; |
| 775 | break; |
| 776 | default: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 777 | SET_ERR_L("Unexpected status: %d", mStatus); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 778 | return INVALID_OPERATION; |
| 779 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 780 | assert(mStatus != STATUS_ACTIVE); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 781 | |
| 782 | if (mInputStream != 0) { |
| 783 | ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); |
| 784 | return INVALID_OPERATION; |
| 785 | } |
| 786 | |
| 787 | sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId, |
| 788 | width, height, depth); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 789 | newStream->setStatusTracker(mStatusTracker); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 790 | |
| 791 | res = mOutputStreams.add(mNextStreamId, newStream); |
| 792 | if (res < 0) { |
| 793 | ALOGE("%s: Can't add new stream to set: %s (%d)", |
| 794 | __FUNCTION__, strerror(-res), res); |
| 795 | return res; |
| 796 | } |
| 797 | mInputStream = newStream; |
| 798 | |
Yuvraj Pasi | e5e3d08 | 2014-04-15 18:37:45 +0530 | [diff] [blame] | 799 | mNeedConfig = true; |
| 800 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 801 | *id = mNextStreamId++; |
| 802 | *zslStream = newStream; |
| 803 | |
| 804 | // Continue captures if active at start |
| 805 | if (wasActive) { |
| 806 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
| 807 | res = configureStreamsLocked(); |
| 808 | if (res != OK) { |
| 809 | ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", |
| 810 | __FUNCTION__, mNextStreamId, strerror(-res), res); |
| 811 | return res; |
| 812 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 813 | internalResumeLocked(); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 816 | ALOGV("Camera %d: Created ZSL stream", mId); |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 817 | return OK; |
| 818 | } |
| 819 | |
Eino-Ville Talvala | 727d172 | 2015-06-09 13:44:19 -0700 | [diff] [blame] | 820 | status_t Camera3Device::createStream(sp<Surface> consumer, |
Eino-Ville Talvala | 3d82c0d | 2015-02-23 15:19:19 -0800 | [diff] [blame] | 821 | uint32_t width, uint32_t height, int format, android_dataspace dataSpace, |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 822 | camera3_stream_rotation_t rotation, int *id) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 823 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 824 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 825 | Mutex::Autolock l(mLock); |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 826 | ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d", |
| 827 | mId, mNextStreamId, width, height, format, dataSpace, rotation); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 828 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 829 | status_t res; |
| 830 | bool wasActive = false; |
| 831 | |
| 832 | switch (mStatus) { |
| 833 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 834 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 835 | return INVALID_OPERATION; |
| 836 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 837 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 838 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 839 | case STATUS_UNCONFIGURED: |
| 840 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 841 | // OK |
| 842 | break; |
| 843 | case STATUS_ACTIVE: |
| 844 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 845 | res = internalPauseAndWaitLocked(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 846 | if (res != OK) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 847 | SET_ERR_L("Can't pause captures to reconfigure streams!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 848 | return res; |
| 849 | } |
| 850 | wasActive = true; |
| 851 | break; |
| 852 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 853 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 854 | return INVALID_OPERATION; |
| 855 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 856 | assert(mStatus != STATUS_ACTIVE); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 857 | |
| 858 | sp<Camera3OutputStream> newStream; |
| 859 | if (format == HAL_PIXEL_FORMAT_BLOB) { |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 860 | ssize_t jpegBufferSize = getJpegBufferSize(width, height); |
Zhijun He | 28c9b6f | 2014-08-08 12:00:47 -0700 | [diff] [blame] | 861 | if (jpegBufferSize <= 0) { |
Zhijun He | f7da096 | 2014-04-24 13:27:56 -0700 | [diff] [blame] | 862 | SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize); |
| 863 | return BAD_VALUE; |
| 864 | } |
| 865 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 866 | newStream = new Camera3OutputStream(mNextStreamId, consumer, |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 867 | width, height, jpegBufferSize, format, dataSpace, rotation); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 868 | } else { |
| 869 | newStream = new Camera3OutputStream(mNextStreamId, consumer, |
Yin-Chia Yeh | b97babb | 2015-03-12 13:42:44 -0700 | [diff] [blame] | 870 | width, height, format, dataSpace, rotation); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 871 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 872 | newStream->setStatusTracker(mStatusTracker); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 873 | |
| 874 | res = mOutputStreams.add(mNextStreamId, newStream); |
| 875 | if (res < 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 876 | 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] | 877 | return res; |
| 878 | } |
| 879 | |
| 880 | *id = mNextStreamId++; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 881 | mNeedConfig = true; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 882 | |
| 883 | // Continue captures if active at start |
| 884 | if (wasActive) { |
| 885 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
| 886 | res = configureStreamsLocked(); |
| 887 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 888 | CLOGE("Can't reconfigure device for new stream %d: %s (%d)", |
| 889 | mNextStreamId, strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 890 | return res; |
| 891 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 892 | internalResumeLocked(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 893 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 894 | ALOGV("Camera %d: Created new stream", mId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 895 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) { |
| 899 | ATRACE_CALL(); |
| 900 | (void)outputId; (void)id; |
| 901 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 902 | CLOGE("Unimplemented"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 903 | return INVALID_OPERATION; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | status_t Camera3Device::getStreamInfo(int id, |
Eino-Ville Talvala | d46a6b9 | 2015-05-14 17:26:24 -0700 | [diff] [blame] | 908 | uint32_t *width, uint32_t *height, |
| 909 | uint32_t *format, android_dataspace *dataSpace) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 910 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 911 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 912 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 913 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 914 | switch (mStatus) { |
| 915 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 916 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 917 | return INVALID_OPERATION; |
| 918 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 919 | CLOGE("Device not initialized!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 920 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 921 | case STATUS_UNCONFIGURED: |
| 922 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 923 | case STATUS_ACTIVE: |
| 924 | // OK |
| 925 | break; |
| 926 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 927 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 928 | return INVALID_OPERATION; |
| 929 | } |
| 930 | |
| 931 | ssize_t idx = mOutputStreams.indexOfKey(id); |
| 932 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 933 | CLOGE("Stream %d is unknown", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 934 | return idx; |
| 935 | } |
| 936 | |
| 937 | if (width) *width = mOutputStreams[idx]->getWidth(); |
| 938 | if (height) *height = mOutputStreams[idx]->getHeight(); |
| 939 | if (format) *format = mOutputStreams[idx]->getFormat(); |
Eino-Ville Talvala | d46a6b9 | 2015-05-14 17:26:24 -0700 | [diff] [blame] | 940 | if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 941 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | status_t Camera3Device::setStreamTransform(int id, |
| 945 | int transform) { |
| 946 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 947 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 948 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 949 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 950 | switch (mStatus) { |
| 951 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 952 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 953 | return INVALID_OPERATION; |
| 954 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 955 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 956 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 957 | case STATUS_UNCONFIGURED: |
| 958 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 959 | case STATUS_ACTIVE: |
| 960 | // OK |
| 961 | break; |
| 962 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 963 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 964 | return INVALID_OPERATION; |
| 965 | } |
| 966 | |
| 967 | ssize_t idx = mOutputStreams.indexOfKey(id); |
| 968 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 969 | CLOGE("Stream %d does not exist", |
| 970 | id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 971 | return BAD_VALUE; |
| 972 | } |
| 973 | |
| 974 | return mOutputStreams.editValueAt(idx)->setTransform(transform); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | status_t Camera3Device::deleteStream(int id) { |
| 978 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 979 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 980 | Mutex::Autolock l(mLock); |
| 981 | status_t res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 982 | |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 983 | ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id); |
| 984 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 985 | // CameraDevice semantics require device to already be idle before |
| 986 | // deleteStream is called, unlike for createStream. |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 987 | if (mStatus == STATUS_ACTIVE) { |
Igor Murashkin | 5282713 | 2013-05-13 14:53:44 -0700 | [diff] [blame] | 988 | ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId); |
| 989 | return -EBUSY; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 992 | sp<Camera3StreamInterface> deletedStream; |
Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 993 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 994 | if (mInputStream != NULL && id == mInputStream->getId()) { |
| 995 | deletedStream = mInputStream; |
| 996 | mInputStream.clear(); |
| 997 | } else { |
Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 998 | if (outputStreamIdx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 999 | CLOGE("Stream %d does not exist", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1000 | return BAD_VALUE; |
| 1001 | } |
Zhijun He | 5f44635 | 2014-01-22 09:49:33 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | // Delete output stream or the output part of a bi-directional stream. |
| 1005 | if (outputStreamIdx != NAME_NOT_FOUND) { |
| 1006 | deletedStream = mOutputStreams.editValueAt(outputStreamIdx); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1007 | mOutputStreams.removeItem(id); |
| 1008 | } |
| 1009 | |
| 1010 | // Free up the stream endpoint so that it can be used by some other stream |
| 1011 | res = deletedStream->disconnect(); |
| 1012 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1013 | SET_ERR_L("Can't disconnect deleted stream %d", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1014 | // fall through since we want to still list the stream as deleted. |
| 1015 | } |
| 1016 | mDeletedStreams.add(deletedStream); |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1017 | mNeedConfig = true; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1018 | |
| 1019 | return res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | status_t Camera3Device::deleteReprocessStream(int id) { |
| 1023 | ATRACE_CALL(); |
| 1024 | (void)id; |
| 1025 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1026 | CLOGE("Unimplemented"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1027 | return INVALID_OPERATION; |
| 1028 | } |
| 1029 | |
Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame] | 1030 | status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) { |
Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 1031 | ATRACE_CALL(); |
| 1032 | ALOGV("%s: E", __FUNCTION__); |
| 1033 | |
| 1034 | Mutex::Autolock il(mInterfaceLock); |
| 1035 | Mutex::Autolock l(mLock); |
Chien-Yu Chen | 17338fc | 2015-06-18 16:30:12 -0700 | [diff] [blame] | 1036 | |
| 1037 | if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) { |
| 1038 | mNeedConfig = true; |
| 1039 | mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed; |
| 1040 | } |
Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 1041 | |
| 1042 | return configureStreamsLocked(); |
| 1043 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1044 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1045 | status_t Camera3Device::getInputBufferProducer( |
| 1046 | sp<IGraphicBufferProducer> *producer) { |
| 1047 | Mutex::Autolock il(mInterfaceLock); |
| 1048 | Mutex::Autolock l(mLock); |
| 1049 | |
| 1050 | if (producer == NULL) { |
| 1051 | return BAD_VALUE; |
| 1052 | } else if (mInputStream == NULL) { |
| 1053 | return INVALID_OPERATION; |
| 1054 | } |
| 1055 | |
| 1056 | return mInputStream->getInputBufferProducer(producer); |
| 1057 | } |
| 1058 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1059 | status_t Camera3Device::createDefaultRequest(int templateId, |
| 1060 | CameraMetadata *request) { |
| 1061 | ATRACE_CALL(); |
Alex Ray | fe7e0c6 | 2013-05-30 00:12:13 -0700 | [diff] [blame] | 1062 | ALOGV("%s: for template %d", __FUNCTION__, templateId); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1063 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1064 | Mutex::Autolock l(mLock); |
| 1065 | |
| 1066 | switch (mStatus) { |
| 1067 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1068 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1069 | return INVALID_OPERATION; |
| 1070 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1071 | CLOGE("Device is not initialized!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1072 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1073 | case STATUS_UNCONFIGURED: |
| 1074 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1075 | case STATUS_ACTIVE: |
| 1076 | // OK |
| 1077 | break; |
| 1078 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1079 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1080 | return INVALID_OPERATION; |
| 1081 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1082 | |
Zhijun He | a1530f1 | 2014-09-14 12:44:20 -0700 | [diff] [blame] | 1083 | if (!mRequestTemplateCache[templateId].isEmpty()) { |
| 1084 | *request = mRequestTemplateCache[templateId]; |
| 1085 | return OK; |
| 1086 | } |
| 1087 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1088 | const camera_metadata_t *rawRequest; |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1089 | ATRACE_BEGIN("camera3->construct_default_request_settings"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1090 | rawRequest = mHal3Device->ops->construct_default_request_settings( |
| 1091 | mHal3Device, templateId); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1092 | ATRACE_END(); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1093 | if (rawRequest == NULL) { |
Yin-Chia Yeh | 0336d36 | 2015-04-14 12:34:22 -0700 | [diff] [blame] | 1094 | ALOGI("%s: template %d is not supported on this camera device", |
| 1095 | __FUNCTION__, templateId); |
| 1096 | return BAD_VALUE; |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1097 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1098 | *request = rawRequest; |
Zhijun He | a1530f1 | 2014-09-14 12:44:20 -0700 | [diff] [blame] | 1099 | mRequestTemplateCache[templateId] = rawRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1100 | |
| 1101 | return OK; |
| 1102 | } |
| 1103 | |
| 1104 | status_t Camera3Device::waitUntilDrained() { |
| 1105 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1106 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1107 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1108 | |
Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 1109 | return waitUntilDrainedLocked(); |
| 1110 | } |
| 1111 | |
| 1112 | status_t Camera3Device::waitUntilDrainedLocked() { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1113 | switch (mStatus) { |
| 1114 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1115 | case STATUS_UNCONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1116 | ALOGV("%s: Already idle", __FUNCTION__); |
| 1117 | return OK; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1118 | case STATUS_CONFIGURED: |
| 1119 | // To avoid race conditions, check with tracker to be sure |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1120 | case STATUS_ERROR: |
| 1121 | case STATUS_ACTIVE: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1122 | // Need to verify shut down |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1123 | break; |
| 1124 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1125 | SET_ERR_L("Unexpected status: %d",mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1126 | return INVALID_OPERATION; |
| 1127 | } |
| 1128 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1129 | ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId); |
| 1130 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
Eino-Ville Talvala | 9c8a091 | 2014-09-14 14:52:19 -0700 | [diff] [blame] | 1131 | if (res != OK) { |
| 1132 | SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res), |
| 1133 | res); |
| 1134 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1135 | return res; |
| 1136 | } |
| 1137 | |
| 1138 | // Pause to reconfigure |
| 1139 | status_t Camera3Device::internalPauseAndWaitLocked() { |
| 1140 | mRequestThread->setPaused(true); |
| 1141 | mPauseStateNotify = true; |
| 1142 | |
| 1143 | ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId); |
| 1144 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
| 1145 | if (res != OK) { |
| 1146 | SET_ERR_L("Can't idle device in %f seconds!", |
| 1147 | kShutdownTimeout/1e9); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1150 | return res; |
| 1151 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1152 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1153 | // Resume after internalPauseAndWaitLocked |
| 1154 | status_t Camera3Device::internalResumeLocked() { |
| 1155 | status_t res; |
| 1156 | |
| 1157 | mRequestThread->setPaused(false); |
| 1158 | |
| 1159 | res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); |
| 1160 | if (res != OK) { |
| 1161 | SET_ERR_L("Can't transition to active in %f seconds!", |
| 1162 | kActiveTimeout/1e9); |
| 1163 | } |
| 1164 | mPauseStateNotify = false; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1165 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1166 | } |
| 1167 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1168 | status_t Camera3Device::waitUntilStateThenRelock(bool active, |
| 1169 | nsecs_t timeout) { |
| 1170 | status_t res = OK; |
| 1171 | if (active == (mStatus == STATUS_ACTIVE)) { |
| 1172 | // Desired state already reached |
| 1173 | return res; |
| 1174 | } |
| 1175 | |
| 1176 | bool stateSeen = false; |
| 1177 | do { |
| 1178 | mRecentStatusUpdates.clear(); |
| 1179 | |
| 1180 | res = mStatusChanged.waitRelative(mLock, timeout); |
| 1181 | if (res != OK) break; |
| 1182 | |
| 1183 | // Check state change history during wait |
| 1184 | for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) { |
| 1185 | if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) { |
| 1186 | stateSeen = true; |
| 1187 | break; |
| 1188 | } |
| 1189 | } |
| 1190 | } while (!stateSeen); |
| 1191 | |
| 1192 | return res; |
| 1193 | } |
| 1194 | |
| 1195 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1196 | status_t Camera3Device::setNotifyCallback(NotificationListener *listener) { |
| 1197 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1198 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1199 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1200 | if (listener != NULL && mListener != NULL) { |
| 1201 | ALOGW("%s: Replacing old callback listener", __FUNCTION__); |
| 1202 | } |
| 1203 | mListener = listener; |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1204 | mRequestThread->setNotificationListener(listener); |
| 1205 | mPreparerThread->setNotificationListener(listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1206 | |
| 1207 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Eino-Ville Talvala | 46910bd | 2013-07-18 19:15:17 -0700 | [diff] [blame] | 1210 | bool Camera3Device::willNotify3A() { |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1214 | status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1215 | status_t res; |
| 1216 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1217 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1218 | while (mResultQueue.empty()) { |
| 1219 | res = mResultSignal.waitRelative(mOutputLock, timeout); |
| 1220 | if (res == TIMED_OUT) { |
| 1221 | return res; |
| 1222 | } else if (res != OK) { |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 1223 | ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1224 | __FUNCTION__, mId, timeout, strerror(-res), res); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1225 | return res; |
| 1226 | } |
| 1227 | } |
| 1228 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1231 | status_t Camera3Device::getNextResult(CaptureResult *frame) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1232 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1233 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1234 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1235 | if (mResultQueue.empty()) { |
| 1236 | return NOT_ENOUGH_DATA; |
| 1237 | } |
| 1238 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1239 | if (frame == NULL) { |
| 1240 | ALOGE("%s: argument cannot be NULL", __FUNCTION__); |
| 1241 | return BAD_VALUE; |
| 1242 | } |
| 1243 | |
| 1244 | CaptureResult &result = *(mResultQueue.begin()); |
| 1245 | frame->mResultExtras = result.mResultExtras; |
| 1246 | frame->mMetadata.acquire(result.mMetadata); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1247 | mResultQueue.erase(mResultQueue.begin()); |
| 1248 | |
| 1249 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | status_t Camera3Device::triggerAutofocus(uint32_t id) { |
| 1253 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1254 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1255 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1256 | ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); |
| 1257 | // Mix-in this trigger into the next request and only the next request. |
| 1258 | RequestTrigger trigger[] = { |
| 1259 | { |
| 1260 | ANDROID_CONTROL_AF_TRIGGER, |
| 1261 | ANDROID_CONTROL_AF_TRIGGER_START |
| 1262 | }, |
| 1263 | { |
| 1264 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 1265 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1266 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1267 | }; |
| 1268 | |
| 1269 | return mRequestThread->queueTrigger(trigger, |
| 1270 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { |
| 1274 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1275 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1276 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1277 | ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); |
| 1278 | // Mix-in this trigger into the next request and only the next request. |
| 1279 | RequestTrigger trigger[] = { |
| 1280 | { |
| 1281 | ANDROID_CONTROL_AF_TRIGGER, |
| 1282 | ANDROID_CONTROL_AF_TRIGGER_CANCEL |
| 1283 | }, |
| 1284 | { |
| 1285 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 1286 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1287 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1288 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1289 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1290 | return mRequestThread->queueTrigger(trigger, |
| 1291 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { |
| 1295 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1296 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1297 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1298 | ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); |
| 1299 | // Mix-in this trigger into the next request and only the next request. |
| 1300 | RequestTrigger trigger[] = { |
| 1301 | { |
| 1302 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 1303 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START |
| 1304 | }, |
| 1305 | { |
| 1306 | ANDROID_CONTROL_AE_PRECAPTURE_ID, |
| 1307 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1308 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1309 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1310 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1311 | return mRequestThread->queueTrigger(trigger, |
| 1312 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId, |
| 1316 | buffer_handle_t *buffer, wp<BufferReleasedListener> listener) { |
| 1317 | ATRACE_CALL(); |
| 1318 | (void)reprocessStreamId; (void)buffer; (void)listener; |
| 1319 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1320 | CLOGE("Unimplemented"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1321 | return INVALID_OPERATION; |
| 1322 | } |
| 1323 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1324 | status_t Camera3Device::flush(int64_t *frameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1325 | ATRACE_CALL(); |
| 1326 | ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1327 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1328 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1329 | NotificationListener* listener; |
| 1330 | { |
| 1331 | Mutex::Autolock l(mOutputLock); |
| 1332 | listener = mListener; |
| 1333 | } |
| 1334 | |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1335 | { |
| 1336 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1337 | mRequestThread->clear(listener, /*out*/frameNumber); |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
Zhijun He | 491e341 | 2013-12-27 10:57:44 -0800 | [diff] [blame] | 1340 | status_t res; |
| 1341 | if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) { |
| 1342 | res = mHal3Device->ops->flush(mHal3Device); |
| 1343 | } else { |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1344 | Mutex::Autolock l(mLock); |
Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 1345 | res = waitUntilDrainedLocked(); |
Zhijun He | 491e341 | 2013-12-27 10:57:44 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | return res; |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1351 | status_t Camera3Device::prepare(int streamId) { |
| 1352 | ATRACE_CALL(); |
| 1353 | ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId); |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1354 | Mutex::Autolock il(mInterfaceLock); |
| 1355 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1356 | |
| 1357 | sp<Camera3StreamInterface> stream; |
| 1358 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); |
| 1359 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 1360 | CLOGE("Stream %d does not exist", streamId); |
| 1361 | return BAD_VALUE; |
| 1362 | } |
| 1363 | |
| 1364 | stream = mOutputStreams.editValueAt(outputStreamIdx); |
| 1365 | |
| 1366 | if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) { |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1367 | CLOGE("Stream %d has already been a request target", streamId); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1368 | return BAD_VALUE; |
| 1369 | } |
| 1370 | |
| 1371 | if (mRequestThread->isStreamPending(stream)) { |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1372 | 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] | 1373 | return BAD_VALUE; |
| 1374 | } |
| 1375 | |
| 1376 | return mPreparerThread->prepare(stream); |
| 1377 | } |
| 1378 | |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1379 | uint32_t Camera3Device::getDeviceVersion() { |
| 1380 | ATRACE_CALL(); |
| 1381 | Mutex::Autolock il(mInterfaceLock); |
| 1382 | return mDeviceVersion; |
| 1383 | } |
| 1384 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1385 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1386 | * Methods called by subclasses |
| 1387 | */ |
| 1388 | |
| 1389 | void Camera3Device::notifyStatus(bool idle) { |
| 1390 | { |
| 1391 | // Need mLock to safely update state and synchronize to current |
| 1392 | // state of methods in flight. |
| 1393 | Mutex::Autolock l(mLock); |
| 1394 | // We can get various system-idle notices from the status tracker |
| 1395 | // while starting up. Only care about them if we've actually sent |
| 1396 | // in some requests recently. |
| 1397 | if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) { |
| 1398 | return; |
| 1399 | } |
| 1400 | ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId, |
| 1401 | idle ? "idle" : "active"); |
| 1402 | mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE; |
| 1403 | mRecentStatusUpdates.add(mStatus); |
| 1404 | mStatusChanged.signal(); |
| 1405 | |
| 1406 | // Skip notifying listener if we're doing some user-transparent |
| 1407 | // state changes |
| 1408 | if (mPauseStateNotify) return; |
| 1409 | } |
| 1410 | NotificationListener *listener; |
| 1411 | { |
| 1412 | Mutex::Autolock l(mOutputLock); |
| 1413 | listener = mListener; |
| 1414 | } |
| 1415 | if (idle && listener != NULL) { |
| 1416 | listener->notifyIdle(); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1421 | * Camera3Device private methods |
| 1422 | */ |
| 1423 | |
| 1424 | sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( |
| 1425 | const CameraMetadata &request) { |
| 1426 | ATRACE_CALL(); |
| 1427 | status_t res; |
| 1428 | |
| 1429 | sp<CaptureRequest> newRequest = new CaptureRequest; |
| 1430 | newRequest->mSettings = request; |
| 1431 | |
| 1432 | camera_metadata_entry_t inputStreams = |
| 1433 | newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); |
| 1434 | if (inputStreams.count > 0) { |
| 1435 | if (mInputStream == NULL || |
Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1436 | mInputStream->getId() != inputStreams.data.i32[0]) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1437 | CLOGE("Request references unknown input stream %d", |
| 1438 | inputStreams.data.u8[0]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1439 | return NULL; |
| 1440 | } |
| 1441 | // Lazy completion of stream configuration (allocation/registration) |
| 1442 | // on first use |
| 1443 | if (mInputStream->isConfiguring()) { |
| 1444 | res = mInputStream->finishConfiguration(mHal3Device); |
| 1445 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1446 | SET_ERR_L("Unable to finish configuring input stream %d:" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1447 | " %s (%d)", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1448 | mInputStream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1449 | return NULL; |
| 1450 | } |
| 1451 | } |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1452 | // Check if stream is being prepared |
| 1453 | if (mInputStream->isPreparing()) { |
| 1454 | CLOGE("Request references an input stream that's being prepared!"); |
| 1455 | return NULL; |
| 1456 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1457 | |
| 1458 | newRequest->mInputStream = mInputStream; |
| 1459 | newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); |
| 1460 | } |
| 1461 | |
| 1462 | camera_metadata_entry_t streams = |
| 1463 | newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 1464 | if (streams.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1465 | CLOGE("Zero output streams specified!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1466 | return NULL; |
| 1467 | } |
| 1468 | |
| 1469 | for (size_t i = 0; i < streams.count; i++) { |
Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1470 | int idx = mOutputStreams.indexOfKey(streams.data.i32[i]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1471 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1472 | CLOGE("Request references unknown stream %d", |
| 1473 | streams.data.u8[i]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1474 | return NULL; |
| 1475 | } |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1476 | sp<Camera3OutputStreamInterface> stream = |
| 1477 | mOutputStreams.editValueAt(idx); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1478 | |
| 1479 | // Lazy completion of stream configuration (allocation/registration) |
| 1480 | // on first use |
| 1481 | if (stream->isConfiguring()) { |
| 1482 | res = stream->finishConfiguration(mHal3Device); |
| 1483 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1484 | SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", |
| 1485 | stream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1486 | return NULL; |
| 1487 | } |
| 1488 | } |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1489 | // Check if stream is being prepared |
| 1490 | if (stream->isPreparing()) { |
| 1491 | CLOGE("Request references an output stream that's being prepared!"); |
| 1492 | return NULL; |
| 1493 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1494 | |
| 1495 | newRequest->mOutputStreams.push(stream); |
| 1496 | } |
| 1497 | newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 1498 | |
| 1499 | return newRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1500 | } |
| 1501 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1502 | bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) { |
| 1503 | for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) { |
| 1504 | Size size = mSupportedOpaqueInputSizes[i]; |
| 1505 | if (size.width == width && size.height == height) { |
| 1506 | return true; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | return false; |
| 1511 | } |
| 1512 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1513 | status_t Camera3Device::configureStreamsLocked() { |
| 1514 | ATRACE_CALL(); |
| 1515 | status_t res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1516 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1517 | if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1518 | CLOGE("Not idle"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1519 | return INVALID_OPERATION; |
| 1520 | } |
| 1521 | |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1522 | if (!mNeedConfig) { |
| 1523 | ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); |
| 1524 | return OK; |
| 1525 | } |
| 1526 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1527 | // Workaround for device HALv3.2 or older spec bug - zero streams requires |
| 1528 | // adding a dummy stream instead. |
| 1529 | // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround. |
| 1530 | if (mOutputStreams.size() == 0) { |
| 1531 | addDummyStreamLocked(); |
| 1532 | } else { |
| 1533 | tryRemoveDummyStreamLocked(); |
| 1534 | } |
| 1535 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1536 | // Start configuring the streams |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1537 | ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1538 | |
| 1539 | camera3_stream_configuration config; |
Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame] | 1540 | config.operation_mode = mIsConstrainedHighSpeedConfiguration ? |
| 1541 | CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE : |
| 1542 | CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1543 | config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); |
| 1544 | |
| 1545 | Vector<camera3_stream_t*> streams; |
| 1546 | streams.setCapacity(config.num_streams); |
| 1547 | |
| 1548 | if (mInputStream != NULL) { |
| 1549 | camera3_stream_t *inputStream; |
| 1550 | inputStream = mInputStream->startConfiguration(); |
| 1551 | if (inputStream == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1552 | SET_ERR_L("Can't start input stream configuration"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1553 | return INVALID_OPERATION; |
| 1554 | } |
| 1555 | streams.add(inputStream); |
| 1556 | } |
| 1557 | |
| 1558 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1559 | |
| 1560 | // Don't configure bidi streams twice, nor add them twice to the list |
| 1561 | if (mOutputStreams[i].get() == |
| 1562 | static_cast<Camera3StreamInterface*>(mInputStream.get())) { |
| 1563 | |
| 1564 | config.num_streams--; |
| 1565 | continue; |
| 1566 | } |
| 1567 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1568 | camera3_stream_t *outputStream; |
| 1569 | outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); |
| 1570 | if (outputStream == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1571 | SET_ERR_L("Can't start output stream configuration"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1572 | return INVALID_OPERATION; |
| 1573 | } |
| 1574 | streams.add(outputStream); |
| 1575 | } |
| 1576 | |
| 1577 | config.streams = streams.editArray(); |
| 1578 | |
| 1579 | // Do the HAL configuration; will potentially touch stream |
| 1580 | // max_buffers, usage, priv fields. |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1581 | ATRACE_BEGIN("camera3->configure_streams"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1582 | res = mHal3Device->ops->configure_streams(mHal3Device, &config); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1583 | ATRACE_END(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1584 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1585 | if (res == BAD_VALUE) { |
| 1586 | // HAL rejected this set of streams as unsupported, clean up config |
| 1587 | // attempt and return to unconfigured state |
| 1588 | if (mInputStream != NULL && mInputStream->isConfiguring()) { |
| 1589 | res = mInputStream->cancelConfiguration(); |
| 1590 | if (res != OK) { |
| 1591 | SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)", |
| 1592 | mInputStream->getId(), strerror(-res), res); |
| 1593 | return res; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
| 1598 | sp<Camera3OutputStreamInterface> outputStream = |
| 1599 | mOutputStreams.editValueAt(i); |
| 1600 | if (outputStream->isConfiguring()) { |
| 1601 | res = outputStream->cancelConfiguration(); |
| 1602 | if (res != OK) { |
| 1603 | SET_ERR_L( |
| 1604 | "Can't cancel configuring output stream %d: %s (%d)", |
| 1605 | outputStream->getId(), strerror(-res), res); |
| 1606 | return res; |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | // Return state to that at start of call, so that future configures |
| 1612 | // properly clean things up |
| 1613 | mStatus = STATUS_UNCONFIGURED; |
| 1614 | mNeedConfig = true; |
| 1615 | |
| 1616 | ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId); |
| 1617 | return BAD_VALUE; |
| 1618 | } else if (res != OK) { |
| 1619 | // Some other kind of error from configure_streams - this is not |
| 1620 | // expected |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1621 | SET_ERR_L("Unable to configure streams with HAL: %s (%d)", |
| 1622 | strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1623 | return res; |
| 1624 | } |
| 1625 | |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1626 | // Finish all stream configuration immediately. |
| 1627 | // TODO: Try to relax this later back to lazy completion, which should be |
| 1628 | // faster |
| 1629 | |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1630 | if (mInputStream != NULL && mInputStream->isConfiguring()) { |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1631 | res = mInputStream->finishConfiguration(mHal3Device); |
| 1632 | if (res != OK) { |
| 1633 | SET_ERR_L("Can't finish configuring input stream %d: %s (%d)", |
| 1634 | mInputStream->getId(), strerror(-res), res); |
| 1635 | return res; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1640 | sp<Camera3OutputStreamInterface> outputStream = |
| 1641 | mOutputStreams.editValueAt(i); |
| 1642 | if (outputStream->isConfiguring()) { |
| 1643 | res = outputStream->finishConfiguration(mHal3Device); |
| 1644 | if (res != OK) { |
| 1645 | SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", |
| 1646 | outputStream->getId(), strerror(-res), res); |
| 1647 | return res; |
| 1648 | } |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1652 | // Request thread needs to know to avoid using repeat-last-settings protocol |
| 1653 | // across configure_streams() calls |
| 1654 | mRequestThread->configurationComplete(); |
| 1655 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1656 | // Update device state |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1657 | |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1658 | mNeedConfig = false; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1659 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1660 | if (mDummyStreamId == NO_STREAM) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1661 | mStatus = STATUS_CONFIGURED; |
| 1662 | } else { |
| 1663 | mStatus = STATUS_UNCONFIGURED; |
| 1664 | } |
| 1665 | |
| 1666 | ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId); |
| 1667 | |
Zhijun He | 0a21051 | 2014-07-24 13:45:15 -0700 | [diff] [blame] | 1668 | // tear down the deleted streams after configure streams. |
| 1669 | mDeletedStreams.clear(); |
| 1670 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1671 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1672 | } |
| 1673 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1674 | status_t Camera3Device::addDummyStreamLocked() { |
| 1675 | ATRACE_CALL(); |
| 1676 | status_t res; |
| 1677 | |
| 1678 | if (mDummyStreamId != NO_STREAM) { |
| 1679 | // Should never be adding a second dummy stream when one is already |
| 1680 | // active |
| 1681 | SET_ERR_L("%s: Camera %d: A dummy stream already exists!", |
| 1682 | __FUNCTION__, mId); |
| 1683 | return INVALID_OPERATION; |
| 1684 | } |
| 1685 | |
| 1686 | ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId); |
| 1687 | |
| 1688 | sp<Camera3OutputStreamInterface> dummyStream = |
| 1689 | new Camera3DummyStream(mNextStreamId); |
| 1690 | |
| 1691 | res = mOutputStreams.add(mNextStreamId, dummyStream); |
| 1692 | if (res < 0) { |
| 1693 | SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res); |
| 1694 | return res; |
| 1695 | } |
| 1696 | |
| 1697 | mDummyStreamId = mNextStreamId; |
| 1698 | mNextStreamId++; |
| 1699 | |
| 1700 | return OK; |
| 1701 | } |
| 1702 | |
| 1703 | status_t Camera3Device::tryRemoveDummyStreamLocked() { |
| 1704 | ATRACE_CALL(); |
| 1705 | status_t res; |
| 1706 | |
| 1707 | if (mDummyStreamId == NO_STREAM) return OK; |
| 1708 | if (mOutputStreams.size() == 1) return OK; |
| 1709 | |
| 1710 | ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId); |
| 1711 | |
| 1712 | // Ok, have a dummy stream and there's at least one other output stream, |
| 1713 | // so remove the dummy |
| 1714 | |
| 1715 | sp<Camera3StreamInterface> deletedStream; |
| 1716 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId); |
| 1717 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 1718 | SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId); |
| 1719 | return INVALID_OPERATION; |
| 1720 | } |
| 1721 | |
| 1722 | deletedStream = mOutputStreams.editValueAt(outputStreamIdx); |
| 1723 | mOutputStreams.removeItemsAt(outputStreamIdx); |
| 1724 | |
| 1725 | // Free up the stream endpoint so that it can be used by some other stream |
| 1726 | res = deletedStream->disconnect(); |
| 1727 | if (res != OK) { |
| 1728 | SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId); |
| 1729 | // fall through since we want to still list the stream as deleted. |
| 1730 | } |
| 1731 | mDeletedStreams.add(deletedStream); |
| 1732 | mDummyStreamId = NO_STREAM; |
| 1733 | |
| 1734 | return res; |
| 1735 | } |
| 1736 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1737 | void Camera3Device::setErrorState(const char *fmt, ...) { |
| 1738 | Mutex::Autolock l(mLock); |
| 1739 | va_list args; |
| 1740 | va_start(args, fmt); |
| 1741 | |
| 1742 | setErrorStateLockedV(fmt, args); |
| 1743 | |
| 1744 | va_end(args); |
| 1745 | } |
| 1746 | |
| 1747 | void Camera3Device::setErrorStateV(const char *fmt, va_list args) { |
| 1748 | Mutex::Autolock l(mLock); |
| 1749 | setErrorStateLockedV(fmt, args); |
| 1750 | } |
| 1751 | |
| 1752 | void Camera3Device::setErrorStateLocked(const char *fmt, ...) { |
| 1753 | va_list args; |
| 1754 | va_start(args, fmt); |
| 1755 | |
| 1756 | setErrorStateLockedV(fmt, args); |
| 1757 | |
| 1758 | va_end(args); |
| 1759 | } |
| 1760 | |
| 1761 | void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1762 | // Print out all error messages to log |
| 1763 | String8 errorCause = String8::formatV(fmt, args); |
| 1764 | ALOGE("Camera %d: %s", mId, errorCause.string()); |
| 1765 | |
| 1766 | // But only do error state transition steps for the first error |
Zhijun He | b05eeae | 2013-06-06 13:51:22 -0700 | [diff] [blame] | 1767 | if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1768 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1769 | mErrorCause = errorCause; |
| 1770 | |
| 1771 | mRequestThread->setPaused(true); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1772 | mStatus = STATUS_ERROR; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1773 | |
| 1774 | // Notify upstream about a device error |
| 1775 | if (mListener != NULL) { |
| 1776 | mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
| 1777 | CaptureResultExtras()); |
| 1778 | } |
| 1779 | |
| 1780 | // Save stack trace. View by dumping it later. |
| 1781 | CameraTraces::saveTrace(); |
| 1782 | // TODO: consider adding errorCause and client pid/procname |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1783 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1784 | |
| 1785 | /** |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1786 | * In-flight request management |
| 1787 | */ |
| 1788 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1789 | status_t Camera3Device::registerInFlight(uint32_t frameNumber, |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 1790 | int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput, |
| 1791 | const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1792 | ATRACE_CALL(); |
| 1793 | Mutex::Autolock l(mInFlightLock); |
| 1794 | |
| 1795 | ssize_t res; |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 1796 | res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput, |
| 1797 | aeTriggerCancelOverride)); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1798 | if (res < 0) return res; |
| 1799 | |
| 1800 | return OK; |
| 1801 | } |
| 1802 | |
| 1803 | /** |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1804 | * Check if all 3A fields are ready, and send off a partial 3A-only result |
| 1805 | * to the output frame queue |
| 1806 | */ |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1807 | bool Camera3Device::processPartial3AResult( |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1808 | uint32_t frameNumber, |
| 1809 | const CameraMetadata& partial, const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1810 | |
| 1811 | // Check if all 3A states are present |
| 1812 | // The full list of fields is |
| 1813 | // android.control.afMode |
| 1814 | // android.control.awbMode |
| 1815 | // android.control.aeState |
| 1816 | // android.control.awbState |
| 1817 | // android.control.afState |
| 1818 | // android.control.afTriggerID |
| 1819 | // android.control.aePrecaptureID |
| 1820 | // TODO: Add android.control.aeMode |
| 1821 | |
| 1822 | bool gotAllStates = true; |
| 1823 | |
| 1824 | uint8_t afMode; |
| 1825 | uint8_t awbMode; |
| 1826 | uint8_t aeState; |
| 1827 | uint8_t afState; |
| 1828 | uint8_t awbState; |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1829 | |
| 1830 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE, |
| 1831 | &afMode, frameNumber); |
| 1832 | |
| 1833 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE, |
| 1834 | &awbMode, frameNumber); |
| 1835 | |
| 1836 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE, |
| 1837 | &aeState, frameNumber); |
| 1838 | |
| 1839 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE, |
| 1840 | &afState, frameNumber); |
| 1841 | |
| 1842 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE, |
| 1843 | &awbState, frameNumber); |
| 1844 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1845 | if (!gotAllStates) return false; |
| 1846 | |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1847 | ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, " |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1848 | "AF state %d, AE state %d, AWB state %d, " |
| 1849 | "AF trigger %d, AE precapture trigger %d", |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 1850 | __FUNCTION__, mId, frameNumber, resultExtras.requestId, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1851 | afMode, awbMode, |
| 1852 | afState, aeState, awbState, |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1853 | resultExtras.afTriggerId, resultExtras.precaptureTriggerId); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1854 | |
| 1855 | // Got all states, so construct a minimal result to send |
| 1856 | // In addition to the above fields, this means adding in |
| 1857 | // android.request.frameCount |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1858 | // android.request.requestId |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1859 | // android.quirks.partialResult (for HAL version below HAL3.2) |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1860 | |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1861 | const size_t kMinimal3AResultEntries = 10; |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1862 | |
| 1863 | Mutex::Autolock l(mOutputLock); |
| 1864 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1865 | CaptureResult captureResult; |
| 1866 | captureResult.mResultExtras = resultExtras; |
| 1867 | captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0); |
| 1868 | // TODO: change this to sp<CaptureResult>. This will need other changes, including, |
| 1869 | // but not limited to CameraDeviceBase::getNextResult |
| 1870 | CaptureResult& min3AResult = |
| 1871 | *mResultQueue.insert(mResultQueue.end(), captureResult); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1872 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1873 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT, |
| 1874 | // TODO: This is problematic casting. Need to fix CameraMetadata. |
| 1875 | reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1876 | return false; |
| 1877 | } |
| 1878 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1879 | int32_t requestId = resultExtras.requestId; |
| 1880 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID, |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1881 | &requestId, frameNumber)) { |
| 1882 | return false; |
| 1883 | } |
| 1884 | |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1885 | if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) { |
| 1886 | static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL; |
| 1887 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT, |
| 1888 | &partialResult, frameNumber)) { |
| 1889 | return false; |
| 1890 | } |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1893 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1894 | &afMode, frameNumber)) { |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1898 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1899 | &awbMode, frameNumber)) { |
| 1900 | return false; |
| 1901 | } |
| 1902 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1903 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1904 | &aeState, frameNumber)) { |
| 1905 | return false; |
| 1906 | } |
| 1907 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1908 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1909 | &afState, frameNumber)) { |
| 1910 | return false; |
| 1911 | } |
| 1912 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1913 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1914 | &awbState, frameNumber)) { |
| 1915 | return false; |
| 1916 | } |
| 1917 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1918 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID, |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1919 | &resultExtras.afTriggerId, frameNumber)) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1920 | return false; |
| 1921 | } |
| 1922 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1923 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID, |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1924 | &resultExtras.precaptureTriggerId, frameNumber)) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1925 | return false; |
| 1926 | } |
| 1927 | |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1928 | // We only send the aggregated partial when all 3A related metadata are available |
| 1929 | // For both API1 and API2. |
| 1930 | // TODO: we probably should pass through all partials to API2 unconditionally. |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1931 | mResultSignal.signal(); |
| 1932 | |
| 1933 | return true; |
| 1934 | } |
| 1935 | |
| 1936 | template<typename T> |
| 1937 | bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1938 | T* value, uint32_t frameNumber) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1939 | (void) frameNumber; |
| 1940 | |
| 1941 | camera_metadata_ro_entry_t entry; |
| 1942 | |
| 1943 | entry = result.find(tag); |
| 1944 | if (entry.count == 0) { |
| 1945 | ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__, |
| 1946 | mId, frameNumber, get_camera_metadata_tag_name(tag)); |
| 1947 | return false; |
| 1948 | } |
| 1949 | |
| 1950 | if (sizeof(T) == sizeof(uint8_t)) { |
| 1951 | *value = entry.data.u8[0]; |
| 1952 | } else if (sizeof(T) == sizeof(int32_t)) { |
| 1953 | *value = entry.data.i32[0]; |
| 1954 | } else { |
| 1955 | ALOGE("%s: Unexpected type", __FUNCTION__); |
| 1956 | return false; |
| 1957 | } |
| 1958 | return true; |
| 1959 | } |
| 1960 | |
| 1961 | template<typename T> |
| 1962 | bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1963 | const T* value, uint32_t frameNumber) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1964 | if (result.update(tag, value, 1) != NO_ERROR) { |
| 1965 | mResultQueue.erase(--mResultQueue.end(), mResultQueue.end()); |
| 1966 | SET_ERR("Frame %d: Failed to set %s in partial metadata", |
| 1967 | frameNumber, get_camera_metadata_tag_name(tag)); |
| 1968 | return false; |
| 1969 | } |
| 1970 | return true; |
| 1971 | } |
| 1972 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 1973 | void Camera3Device::returnOutputBuffers( |
| 1974 | const camera3_stream_buffer_t *outputBuffers, size_t numBuffers, |
| 1975 | nsecs_t timestamp) { |
| 1976 | for (size_t i = 0; i < numBuffers; i++) |
| 1977 | { |
| 1978 | Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream); |
| 1979 | status_t res = stream->returnBuffer(outputBuffers[i], timestamp); |
| 1980 | // Note: stream may be deallocated at this point, if this buffer was |
| 1981 | // the last reference to it. |
| 1982 | if (res != OK) { |
| 1983 | ALOGE("Can't return buffer to its stream: %s (%d)", |
| 1984 | strerror(-res), res); |
| 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) { |
| 1991 | |
| 1992 | const InFlightRequest &request = mInFlightMap.valueAt(idx); |
| 1993 | const uint32_t frameNumber = mInFlightMap.keyAt(idx); |
| 1994 | |
| 1995 | nsecs_t sensorTimestamp = request.sensorTimestamp; |
| 1996 | nsecs_t shutterTimestamp = request.shutterTimestamp; |
| 1997 | |
| 1998 | // Check if it's okay to remove the request from InFlightMap: |
| 1999 | // In the case of a successful request: |
| 2000 | // all input and output buffers, all result metadata, shutter callback |
| 2001 | // arrived. |
| 2002 | // In the case of a unsuccessful request: |
| 2003 | // all input and output buffers arrived. |
| 2004 | if (request.numBuffersLeft == 0 && |
| 2005 | (request.requestStatus != OK || |
| 2006 | (request.haveResultMetadata && shutterTimestamp != 0))) { |
| 2007 | ATRACE_ASYNC_END("frame capture", frameNumber); |
| 2008 | |
| 2009 | // Sanity check - if sensor timestamp matches shutter timestamp |
| 2010 | if (request.requestStatus == OK && |
| 2011 | sensorTimestamp != shutterTimestamp) { |
| 2012 | SET_ERR("sensor timestamp (%" PRId64 |
| 2013 | ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", |
| 2014 | sensorTimestamp, frameNumber, shutterTimestamp); |
| 2015 | } |
| 2016 | |
| 2017 | // for an unsuccessful request, it may have pending output buffers to |
| 2018 | // return. |
| 2019 | assert(request.requestStatus != OK || |
| 2020 | request.pendingOutputBuffers.size() == 0); |
| 2021 | returnOutputBuffers(request.pendingOutputBuffers.array(), |
| 2022 | request.pendingOutputBuffers.size(), 0); |
| 2023 | |
| 2024 | mInFlightMap.removeItemsAt(idx, 1); |
| 2025 | |
| 2026 | ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); |
| 2027 | } |
| 2028 | |
| 2029 | // Sanity check - if we have too many in-flight frames, something has |
| 2030 | // likely gone wrong |
| 2031 | if (mInFlightMap.size() > kInFlightWarnLimit) { |
| 2032 | CLOGE("In-flight list too large: %zu", mInFlightMap.size()); |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | |
| 2037 | void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata, |
| 2038 | CaptureResultExtras &resultExtras, |
| 2039 | CameraMetadata &collectedPartialResult, |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2040 | uint32_t frameNumber, |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2041 | bool reprocess, |
| 2042 | const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2043 | if (pendingMetadata.isEmpty()) |
| 2044 | return; |
| 2045 | |
| 2046 | Mutex::Autolock l(mOutputLock); |
| 2047 | |
| 2048 | // 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] | 2049 | if (reprocess) { |
| 2050 | if (frameNumber < mNextReprocessResultFrameNumber) { |
| 2051 | SET_ERR("Out-of-order reprocess capture result metadata submitted! " |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2052 | "(got frame number %d, expecting %d)", |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2053 | frameNumber, mNextReprocessResultFrameNumber); |
| 2054 | return; |
| 2055 | } |
| 2056 | mNextReprocessResultFrameNumber = frameNumber + 1; |
| 2057 | } else { |
| 2058 | if (frameNumber < mNextResultFrameNumber) { |
| 2059 | SET_ERR("Out-of-order capture result metadata submitted! " |
| 2060 | "(got frame number %d, expecting %d)", |
| 2061 | frameNumber, mNextResultFrameNumber); |
| 2062 | return; |
| 2063 | } |
| 2064 | mNextResultFrameNumber = frameNumber + 1; |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2065 | } |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2066 | |
| 2067 | CaptureResult captureResult; |
| 2068 | captureResult.mResultExtras = resultExtras; |
| 2069 | captureResult.mMetadata = pendingMetadata; |
| 2070 | |
| 2071 | if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, |
| 2072 | (int32_t*)&frameNumber, 1) != OK) { |
| 2073 | SET_ERR("Failed to set frame# in metadata (%d)", |
| 2074 | frameNumber); |
| 2075 | return; |
| 2076 | } else { |
| 2077 | ALOGVV("%s: Camera %d: Set frame# in metadata (%d)", |
| 2078 | __FUNCTION__, mId, frameNumber); |
| 2079 | } |
| 2080 | |
| 2081 | // Append any previous partials to form a complete result |
| 2082 | if (mUsePartialResult && !collectedPartialResult.isEmpty()) { |
| 2083 | captureResult.mMetadata.append(collectedPartialResult); |
| 2084 | } |
| 2085 | |
| 2086 | captureResult.mMetadata.sort(); |
| 2087 | |
| 2088 | // Check that there's a timestamp in the result metadata |
| 2089 | camera_metadata_entry entry = |
| 2090 | captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); |
| 2091 | if (entry.count == 0) { |
| 2092 | SET_ERR("No timestamp provided by HAL for frame %d!", |
| 2093 | frameNumber); |
| 2094 | return; |
| 2095 | } |
| 2096 | |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2097 | overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride); |
| 2098 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2099 | // Valid result, insert into queue |
| 2100 | List<CaptureResult>::iterator queuedResult = |
| 2101 | mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult)); |
| 2102 | ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 |
| 2103 | ", burstId = %" PRId32, __FUNCTION__, |
| 2104 | queuedResult->mResultExtras.requestId, |
| 2105 | queuedResult->mResultExtras.frameNumber, |
| 2106 | queuedResult->mResultExtras.burstId); |
| 2107 | |
| 2108 | mResultSignal.signal(); |
| 2109 | } |
| 2110 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2111 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2112 | * Camera HAL device callback methods |
| 2113 | */ |
| 2114 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2115 | void Camera3Device::processCaptureResult(const camera3_capture_result *result) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2116 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2117 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2118 | status_t res; |
| 2119 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2120 | uint32_t frameNumber = result->frame_number; |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2121 | if (result->result == NULL && result->num_output_buffers == 0 && |
| 2122 | result->input_buffer == NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2123 | SET_ERR("No result data provided by HAL for frame %d", |
| 2124 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2125 | return; |
| 2126 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2127 | |
| 2128 | // For HAL3.2 or above, If HAL doesn't support partial, it must always set |
| 2129 | // partial_result to 1 when metadata is included in this result. |
| 2130 | if (!mUsePartialResult && |
| 2131 | mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 && |
| 2132 | result->result != NULL && |
| 2133 | result->partial_result != 1) { |
| 2134 | SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" |
| 2135 | " if partial result is not supported", |
| 2136 | frameNumber, result->partial_result); |
| 2137 | return; |
| 2138 | } |
| 2139 | |
| 2140 | bool isPartialResult = false; |
| 2141 | CameraMetadata collectedPartialResult; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2142 | CaptureResultExtras resultExtras; |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2143 | bool hasInputBufferInRequest = false; |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2144 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2145 | // Get shutter timestamp and resultExtras from list of in-flight requests, |
| 2146 | // where it was added by the shutter notification for this frame. If the |
| 2147 | // shutter timestamp isn't received yet, append the output buffers to the |
| 2148 | // in-flight request and they will be returned when the shutter timestamp |
| 2149 | // arrives. Update the in-flight status and remove the in-flight entry if |
| 2150 | // all result data and shutter timestamp have been received. |
| 2151 | nsecs_t shutterTimestamp = 0; |
| 2152 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2153 | { |
| 2154 | Mutex::Autolock l(mInFlightLock); |
| 2155 | ssize_t idx = mInFlightMap.indexOfKey(frameNumber); |
| 2156 | if (idx == NAME_NOT_FOUND) { |
| 2157 | SET_ERR("Unknown frame number for capture result: %d", |
| 2158 | frameNumber); |
| 2159 | return; |
| 2160 | } |
| 2161 | InFlightRequest &request = mInFlightMap.editValueAt(idx); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2162 | ALOGVV("%s: got InFlightRequest requestId = %" PRId32 |
| 2163 | ", frameNumber = %" PRId64 ", burstId = %" PRId32 |
| 2164 | ", partialResultCount = %d", |
| 2165 | __FUNCTION__, request.resultExtras.requestId, |
| 2166 | request.resultExtras.frameNumber, request.resultExtras.burstId, |
| 2167 | result->partial_result); |
| 2168 | // Always update the partial count to the latest one if it's not 0 |
| 2169 | // (buffers only). When framework aggregates adjacent partial results |
| 2170 | // into one, the latest partial count will be used. |
| 2171 | if (result->partial_result != 0) |
| 2172 | request.resultExtras.partialResultCount = result->partial_result; |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2173 | |
| 2174 | // Check if this result carries only partial metadata |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2175 | if (mUsePartialResult && result->result != NULL) { |
| 2176 | if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 2177 | if (result->partial_result > mNumPartialResults || result->partial_result < 1) { |
| 2178 | SET_ERR("Result is malformed for frame %d: partial_result %u must be in" |
| 2179 | " the range of [1, %d] when metadata is included in the result", |
| 2180 | frameNumber, result->partial_result, mNumPartialResults); |
| 2181 | return; |
| 2182 | } |
| 2183 | isPartialResult = (result->partial_result < mNumPartialResults); |
Zhijun He | 5d76e1a | 2014-07-22 16:08:13 -0700 | [diff] [blame] | 2184 | if (isPartialResult) { |
| 2185 | request.partialResult.collectedResult.append(result->result); |
| 2186 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2187 | } else { |
| 2188 | camera_metadata_ro_entry_t partialResultEntry; |
| 2189 | res = find_camera_metadata_ro_entry(result->result, |
| 2190 | ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry); |
| 2191 | if (res != NAME_NOT_FOUND && |
| 2192 | partialResultEntry.count > 0 && |
| 2193 | partialResultEntry.data.u8[0] == |
| 2194 | ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) { |
| 2195 | // A partial result. Flag this as such, and collect this |
| 2196 | // set of metadata into the in-flight entry. |
| 2197 | isPartialResult = true; |
| 2198 | request.partialResult.collectedResult.append( |
| 2199 | result->result); |
| 2200 | request.partialResult.collectedResult.erase( |
| 2201 | ANDROID_QUIRKS_PARTIAL_RESULT); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | if (isPartialResult) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2206 | // Fire off a 3A-only result if possible |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2207 | if (!request.partialResult.haveSent3A) { |
| 2208 | request.partialResult.haveSent3A = |
| 2209 | processPartial3AResult(frameNumber, |
| 2210 | request.partialResult.collectedResult, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2211 | request.resultExtras); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2216 | shutterTimestamp = request.shutterTimestamp; |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2217 | hasInputBufferInRequest = request.hasInputBuffer; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2218 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2219 | // Did we get the (final) result metadata for this capture? |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2220 | if (result->result != NULL && !isPartialResult) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2221 | if (request.haveResultMetadata) { |
| 2222 | SET_ERR("Called multiple times with metadata for frame %d", |
| 2223 | frameNumber); |
| 2224 | return; |
| 2225 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2226 | if (mUsePartialResult && |
| 2227 | !request.partialResult.collectedResult.isEmpty()) { |
| 2228 | collectedPartialResult.acquire( |
| 2229 | request.partialResult.collectedResult); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2230 | } |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2231 | request.haveResultMetadata = true; |
| 2232 | } |
| 2233 | |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2234 | uint32_t numBuffersReturned = result->num_output_buffers; |
| 2235 | if (result->input_buffer != NULL) { |
| 2236 | if (hasInputBufferInRequest) { |
| 2237 | numBuffersReturned += 1; |
| 2238 | } else { |
| 2239 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 2240 | " buffer sent in the request", |
| 2241 | __FUNCTION__); |
| 2242 | } |
| 2243 | } |
| 2244 | request.numBuffersLeft -= numBuffersReturned; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2245 | if (request.numBuffersLeft < 0) { |
| 2246 | SET_ERR("Too many buffers returned for frame %d", |
| 2247 | frameNumber); |
| 2248 | return; |
| 2249 | } |
| 2250 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2251 | camera_metadata_ro_entry_t entry; |
| 2252 | res = find_camera_metadata_ro_entry(result->result, |
| 2253 | ANDROID_SENSOR_TIMESTAMP, &entry); |
| 2254 | if (res == OK && entry.count == 1) { |
| 2255 | request.sensorTimestamp = entry.data.i64[0]; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2258 | // If shutter event isn't received yet, append the output buffers to |
| 2259 | // the in-flight request. Otherwise, return the output buffers to |
| 2260 | // streams. |
| 2261 | if (shutterTimestamp == 0) { |
| 2262 | request.pendingOutputBuffers.appendArray(result->output_buffers, |
| 2263 | result->num_output_buffers); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2264 | } else { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2265 | returnOutputBuffers(result->output_buffers, |
| 2266 | result->num_output_buffers, shutterTimestamp); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2267 | } |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2268 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2269 | if (result->result != NULL && !isPartialResult) { |
| 2270 | if (shutterTimestamp == 0) { |
| 2271 | request.pendingMetadata = result->result; |
| 2272 | request.partialResult.collectedResult = collectedPartialResult; |
| 2273 | } else { |
| 2274 | CameraMetadata metadata; |
| 2275 | metadata = result->result; |
| 2276 | sendCaptureResult(metadata, request.resultExtras, |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2277 | collectedPartialResult, frameNumber, hasInputBufferInRequest, |
| 2278 | request.aeTriggerCancelOverride); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2279 | } |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2282 | removeInFlightRequestIfReadyLocked(idx); |
| 2283 | } // scope for mInFlightLock |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2284 | |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2285 | if (result->input_buffer != NULL) { |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2286 | if (hasInputBufferInRequest) { |
| 2287 | Camera3Stream *stream = |
| 2288 | Camera3Stream::cast(result->input_buffer->stream); |
| 2289 | res = stream->returnInputBuffer(*(result->input_buffer)); |
| 2290 | // Note: stream may be deallocated at this point, if this buffer was the |
| 2291 | // last reference to it. |
| 2292 | if (res != OK) { |
| 2293 | ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" |
| 2294 | " its stream:%s (%d)", __FUNCTION__, |
| 2295 | frameNumber, strerror(-res), res); |
Zhijun He | 0ea8fa4 | 2014-07-07 17:05:38 -0700 | [diff] [blame] | 2296 | } |
| 2297 | } else { |
| 2298 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 2299 | " buffer sent in the request, skipping input buffer return.", |
| 2300 | __FUNCTION__); |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2301 | } |
| 2302 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | void Camera3Device::notify(const camera3_notify_msg *msg) { |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2306 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2307 | NotificationListener *listener; |
| 2308 | { |
| 2309 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2310 | listener = mListener; |
| 2311 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2312 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2313 | if (msg == NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2314 | SET_ERR("HAL sent NULL notify message!"); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2315 | return; |
| 2316 | } |
| 2317 | |
| 2318 | switch (msg->type) { |
| 2319 | case CAMERA3_MSG_ERROR: { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2320 | notifyError(msg->message.error, listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2321 | break; |
| 2322 | } |
| 2323 | case CAMERA3_MSG_SHUTTER: { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2324 | notifyShutter(msg->message.shutter, listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2325 | break; |
| 2326 | } |
| 2327 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2328 | SET_ERR("Unknown notify message from HAL: %d", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2329 | msg->type); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2330 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2331 | } |
| 2332 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2333 | void Camera3Device::notifyError(const camera3_error_msg_t &msg, |
| 2334 | NotificationListener *listener) { |
| 2335 | |
| 2336 | // Map camera HAL error codes to ICameraDeviceCallback error codes |
| 2337 | // Index into this with the HAL error code |
| 2338 | static const ICameraDeviceCallbacks::CameraErrorCode |
| 2339 | halErrorMap[CAMERA3_MSG_NUM_ERRORS] = { |
| 2340 | // 0 = Unused error code |
| 2341 | ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, |
| 2342 | // 1 = CAMERA3_MSG_ERROR_DEVICE |
| 2343 | ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
| 2344 | // 2 = CAMERA3_MSG_ERROR_REQUEST |
| 2345 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 2346 | // 3 = CAMERA3_MSG_ERROR_RESULT |
| 2347 | ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, |
| 2348 | // 4 = CAMERA3_MSG_ERROR_BUFFER |
| 2349 | ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER |
| 2350 | }; |
| 2351 | |
| 2352 | ICameraDeviceCallbacks::CameraErrorCode errorCode = |
| 2353 | ((msg.error_code >= 0) && |
| 2354 | (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ? |
| 2355 | halErrorMap[msg.error_code] : |
| 2356 | ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; |
| 2357 | |
| 2358 | int streamId = 0; |
| 2359 | if (msg.error_stream != NULL) { |
| 2360 | Camera3Stream *stream = |
| 2361 | Camera3Stream::cast(msg.error_stream); |
| 2362 | streamId = stream->getId(); |
| 2363 | } |
| 2364 | ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d", |
| 2365 | mId, __FUNCTION__, msg.frame_number, |
| 2366 | streamId, msg.error_code); |
| 2367 | |
| 2368 | CaptureResultExtras resultExtras; |
| 2369 | switch (errorCode) { |
| 2370 | case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: |
| 2371 | // SET_ERR calls notifyError |
| 2372 | SET_ERR("Camera HAL reported serious device error"); |
| 2373 | break; |
| 2374 | case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: |
| 2375 | case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: |
| 2376 | case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: |
| 2377 | { |
| 2378 | Mutex::Autolock l(mInFlightLock); |
| 2379 | ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number); |
| 2380 | if (idx >= 0) { |
| 2381 | InFlightRequest &r = mInFlightMap.editValueAt(idx); |
| 2382 | r.requestStatus = msg.error_code; |
| 2383 | resultExtras = r.resultExtras; |
| 2384 | } else { |
| 2385 | resultExtras.frameNumber = msg.frame_number; |
| 2386 | ALOGE("Camera %d: %s: cannot find in-flight request on " |
| 2387 | "frame %" PRId64 " error", mId, __FUNCTION__, |
| 2388 | resultExtras.frameNumber); |
| 2389 | } |
| 2390 | } |
| 2391 | if (listener != NULL) { |
| 2392 | listener->notifyError(errorCode, resultExtras); |
| 2393 | } else { |
| 2394 | ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__); |
| 2395 | } |
| 2396 | break; |
| 2397 | default: |
| 2398 | // SET_ERR calls notifyError |
| 2399 | SET_ERR("Unknown error message from HAL: %d", msg.error_code); |
| 2400 | break; |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg, |
| 2405 | NotificationListener *listener) { |
| 2406 | ssize_t idx; |
| 2407 | // Verify ordering of shutter notifications |
| 2408 | { |
| 2409 | Mutex::Autolock l(mOutputLock); |
| 2410 | // TODO: need to track errors for tighter bounds on expected frame number. |
| 2411 | if (msg.frame_number < mNextShutterFrameNumber) { |
| 2412 | SET_ERR("Shutter notification out-of-order. Expected " |
| 2413 | "notification for frame %d, got frame %d", |
| 2414 | mNextShutterFrameNumber, msg.frame_number); |
| 2415 | return; |
| 2416 | } |
| 2417 | mNextShutterFrameNumber = msg.frame_number + 1; |
| 2418 | } |
| 2419 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2420 | // Set timestamp for the request in the in-flight tracking |
| 2421 | // and get the request ID to send upstream |
| 2422 | { |
| 2423 | Mutex::Autolock l(mInFlightLock); |
| 2424 | idx = mInFlightMap.indexOfKey(msg.frame_number); |
| 2425 | if (idx >= 0) { |
| 2426 | InFlightRequest &r = mInFlightMap.editValueAt(idx); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2427 | |
| 2428 | ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64, |
| 2429 | mId, __FUNCTION__, |
| 2430 | msg.frame_number, r.resultExtras.requestId, msg.timestamp); |
| 2431 | // Call listener, if any |
| 2432 | if (listener != NULL) { |
| 2433 | listener->notifyShutter(r.resultExtras, msg.timestamp); |
| 2434 | } |
| 2435 | |
| 2436 | r.shutterTimestamp = msg.timestamp; |
| 2437 | |
| 2438 | // send pending result and buffers |
| 2439 | sendCaptureResult(r.pendingMetadata, r.resultExtras, |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2440 | r.partialResult.collectedResult, msg.frame_number, |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2441 | r.hasInputBuffer, r.aeTriggerCancelOverride); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2442 | returnOutputBuffers(r.pendingOutputBuffers.array(), |
| 2443 | r.pendingOutputBuffers.size(), r.shutterTimestamp); |
| 2444 | r.pendingOutputBuffers.clear(); |
| 2445 | |
| 2446 | removeInFlightRequestIfReadyLocked(idx); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | if (idx < 0) { |
| 2450 | SET_ERR("Shutter notification for non-existent frame number %d", |
| 2451 | msg.frame_number); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2456 | CameraMetadata Camera3Device::getLatestRequestLocked() { |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2457 | ALOGV("%s", __FUNCTION__); |
| 2458 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2459 | CameraMetadata retVal; |
| 2460 | |
| 2461 | if (mRequestThread != NULL) { |
| 2462 | retVal = mRequestThread->getLatestRequest(); |
| 2463 | } |
| 2464 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2465 | return retVal; |
| 2466 | } |
| 2467 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2468 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2469 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2470 | * RequestThread inner class methods |
| 2471 | */ |
| 2472 | |
| 2473 | Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2474 | sp<StatusTracker> statusTracker, |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2475 | camera3_device_t *hal3Device) : |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2476 | Thread(/*canCallJava*/false), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2477 | mParent(parent), |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2478 | mStatusTracker(statusTracker), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2479 | mHal3Device(hal3Device), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2480 | mId(getId(parent)), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2481 | mReconfigured(false), |
| 2482 | mDoPause(false), |
| 2483 | mPaused(true), |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2484 | mFrameNumber(0), |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2485 | mLatestRequestId(NAME_NOT_FOUND), |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 2486 | mCurrentAfTriggerId(0), |
| 2487 | mCurrentPreCaptureTriggerId(0), |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2488 | mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2489 | mStatusId = statusTracker->addComponent(); |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2490 | |
| 2491 | mAeLockAvailable = false; |
| 2492 | sp<Camera3Device> p = parent.promote(); |
| 2493 | if (p != NULL) { |
| 2494 | camera_metadata_ro_entry aeLockAvailable = |
| 2495 | p->info().find(ANDROID_CONTROL_AE_LOCK_AVAILABLE); |
| 2496 | if (aeLockAvailable.count > 0) { |
| 2497 | mAeLockAvailable = (aeLockAvailable.data.u8[0] == |
| 2498 | ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE); |
| 2499 | } |
| 2500 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2501 | } |
| 2502 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2503 | void Camera3Device::RequestThread::setNotificationListener( |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2504 | NotificationListener *listener) { |
| 2505 | Mutex::Autolock l(mRequestLock); |
| 2506 | mListener = listener; |
| 2507 | } |
| 2508 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2509 | void Camera3Device::RequestThread::configurationComplete() { |
| 2510 | Mutex::Autolock l(mRequestLock); |
| 2511 | mReconfigured = true; |
| 2512 | } |
| 2513 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2514 | status_t Camera3Device::RequestThread::queueRequestList( |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2515 | List<sp<CaptureRequest> > &requests, |
| 2516 | /*out*/ |
| 2517 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2518 | Mutex::Autolock l(mRequestLock); |
| 2519 | for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); |
| 2520 | ++it) { |
| 2521 | mRequestQueue.push_back(*it); |
| 2522 | } |
| 2523 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2524 | if (lastFrameNumber != NULL) { |
| 2525 | *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1; |
| 2526 | ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".", |
| 2527 | __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber, |
| 2528 | *lastFrameNumber); |
| 2529 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2530 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2531 | unpauseForNewRequests(); |
| 2532 | |
| 2533 | return OK; |
| 2534 | } |
| 2535 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2536 | |
| 2537 | status_t Camera3Device::RequestThread::queueTrigger( |
| 2538 | RequestTrigger trigger[], |
| 2539 | size_t count) { |
| 2540 | |
| 2541 | Mutex::Autolock l(mTriggerMutex); |
| 2542 | status_t ret; |
| 2543 | |
| 2544 | for (size_t i = 0; i < count; ++i) { |
| 2545 | ret = queueTriggerLocked(trigger[i]); |
| 2546 | |
| 2547 | if (ret != OK) { |
| 2548 | return ret; |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | return OK; |
| 2553 | } |
| 2554 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2555 | int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { |
| 2556 | sp<Camera3Device> d = device.promote(); |
| 2557 | if (d != NULL) return d->mId; |
| 2558 | return 0; |
| 2559 | } |
| 2560 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2561 | status_t Camera3Device::RequestThread::queueTriggerLocked( |
| 2562 | RequestTrigger trigger) { |
| 2563 | |
| 2564 | uint32_t tag = trigger.metadataTag; |
| 2565 | ssize_t index = mTriggerMap.indexOfKey(tag); |
| 2566 | |
| 2567 | switch (trigger.getTagType()) { |
| 2568 | case TYPE_BYTE: |
| 2569 | // fall-through |
| 2570 | case TYPE_INT32: |
| 2571 | break; |
| 2572 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2573 | ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, |
| 2574 | trigger.getTagType()); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2575 | return INVALID_OPERATION; |
| 2576 | } |
| 2577 | |
| 2578 | /** |
| 2579 | * Collect only the latest trigger, since we only have 1 field |
| 2580 | * in the request settings per trigger tag, and can't send more than 1 |
| 2581 | * trigger per request. |
| 2582 | */ |
| 2583 | if (index != NAME_NOT_FOUND) { |
| 2584 | mTriggerMap.editValueAt(index) = trigger; |
| 2585 | } else { |
| 2586 | mTriggerMap.add(tag, trigger); |
| 2587 | } |
| 2588 | |
| 2589 | return OK; |
| 2590 | } |
| 2591 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2592 | status_t Camera3Device::RequestThread::setRepeatingRequests( |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2593 | const RequestList &requests, |
| 2594 | /*out*/ |
| 2595 | int64_t *lastFrameNumber) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2596 | Mutex::Autolock l(mRequestLock); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2597 | if (lastFrameNumber != NULL) { |
| 2598 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 2599 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2600 | mRepeatingRequests.clear(); |
| 2601 | mRepeatingRequests.insert(mRepeatingRequests.begin(), |
| 2602 | requests.begin(), requests.end()); |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 2603 | |
| 2604 | unpauseForNewRequests(); |
| 2605 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2606 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2607 | return OK; |
| 2608 | } |
| 2609 | |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2610 | bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) { |
| 2611 | if (mRepeatingRequests.empty()) { |
| 2612 | return false; |
| 2613 | } |
| 2614 | int32_t requestId = requestIn->mResultExtras.requestId; |
| 2615 | const RequestList &repeatRequests = mRepeatingRequests; |
| 2616 | // All repeating requests are guaranteed to have same id so only check first quest |
| 2617 | const sp<CaptureRequest> firstRequest = *repeatRequests.begin(); |
| 2618 | return (firstRequest->mResultExtras.requestId == requestId); |
| 2619 | } |
| 2620 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2621 | status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2622 | Mutex::Autolock l(mRequestLock); |
| 2623 | mRepeatingRequests.clear(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2624 | if (lastFrameNumber != NULL) { |
| 2625 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 2626 | } |
| 2627 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2628 | return OK; |
| 2629 | } |
| 2630 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2631 | status_t Camera3Device::RequestThread::clear( |
| 2632 | NotificationListener *listener, |
| 2633 | /*out*/int64_t *lastFrameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2634 | Mutex::Autolock l(mRequestLock); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2635 | ALOGV("RequestThread::%s:", __FUNCTION__); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2636 | |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2637 | mRepeatingRequests.clear(); |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2638 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2639 | // Send errors for all requests pending in the request queue, including |
| 2640 | // pending repeating requests |
| 2641 | if (listener != NULL) { |
| 2642 | for (RequestList::iterator it = mRequestQueue.begin(); |
| 2643 | it != mRequestQueue.end(); ++it) { |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2644 | // Abort the input buffers for reprocess requests. |
| 2645 | if ((*it)->mInputStream != NULL) { |
| 2646 | camera3_stream_buffer_t inputBuffer; |
| 2647 | status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer); |
| 2648 | if (res != OK) { |
| 2649 | ALOGW("%s: %d: couldn't get input buffer while clearing the request " |
| 2650 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); |
| 2651 | } else { |
| 2652 | res = (*it)->mInputStream->returnInputBuffer(inputBuffer); |
| 2653 | if (res != OK) { |
| 2654 | ALOGE("%s: %d: couldn't return input buffer while clearing the request " |
| 2655 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); |
| 2656 | } |
| 2657 | } |
| 2658 | } |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2659 | // Set the frame number this request would have had, if it |
| 2660 | // had been submitted; this frame number will not be reused. |
| 2661 | // The requestId and burstId fields were set when the request was |
| 2662 | // submitted originally (in convertMetadataListToRequestListLocked) |
| 2663 | (*it)->mResultExtras.frameNumber = mFrameNumber++; |
| 2664 | listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 2665 | (*it)->mResultExtras); |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2666 | } |
| 2667 | } |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2668 | mRequestQueue.clear(); |
| 2669 | mTriggerMap.clear(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2670 | if (lastFrameNumber != NULL) { |
| 2671 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 2672 | } |
| 2673 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2674 | return OK; |
| 2675 | } |
| 2676 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2677 | void Camera3Device::RequestThread::setPaused(bool paused) { |
| 2678 | Mutex::Autolock l(mPauseLock); |
| 2679 | mDoPause = paused; |
| 2680 | mDoPauseSignal.signal(); |
| 2681 | } |
| 2682 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2683 | status_t Camera3Device::RequestThread::waitUntilRequestProcessed( |
| 2684 | int32_t requestId, nsecs_t timeout) { |
| 2685 | Mutex::Autolock l(mLatestRequestMutex); |
| 2686 | status_t res; |
| 2687 | while (mLatestRequestId != requestId) { |
| 2688 | nsecs_t startTime = systemTime(); |
| 2689 | |
| 2690 | res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); |
| 2691 | if (res != OK) return res; |
| 2692 | |
| 2693 | timeout -= (systemTime() - startTime); |
| 2694 | } |
| 2695 | |
| 2696 | return OK; |
| 2697 | } |
| 2698 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2699 | void Camera3Device::RequestThread::requestExit() { |
| 2700 | // Call parent to set up shutdown |
| 2701 | Thread::requestExit(); |
| 2702 | // The exit from any possible waits |
| 2703 | mDoPauseSignal.signal(); |
| 2704 | mRequestSignal.signal(); |
| 2705 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2706 | |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2707 | |
| 2708 | /** |
| 2709 | * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so |
| 2710 | * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF |
| 2711 | * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides |
| 2712 | * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the |
| 2713 | * request. |
| 2714 | */ |
| 2715 | void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) { |
| 2716 | request->mAeTriggerCancelOverride.applyAeLock = false; |
| 2717 | request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false; |
| 2718 | |
| 2719 | if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) { |
| 2720 | return; |
| 2721 | } |
| 2722 | |
| 2723 | camera_metadata_entry_t aePrecaptureTrigger = |
| 2724 | request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); |
| 2725 | if (aePrecaptureTrigger.count > 0 && |
| 2726 | aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) { |
| 2727 | // Always override CANCEL to IDLE |
| 2728 | uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE; |
| 2729 | request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1); |
| 2730 | request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true; |
| 2731 | request->mAeTriggerCancelOverride.aePrecaptureTrigger = |
| 2732 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL; |
| 2733 | |
| 2734 | if (mAeLockAvailable == true) { |
| 2735 | camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK); |
| 2736 | if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) { |
| 2737 | uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON; |
| 2738 | request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1); |
| 2739 | request->mAeTriggerCancelOverride.applyAeLock = true; |
| 2740 | request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF; |
| 2741 | } |
| 2742 | } |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | /** |
| 2747 | * Override result metadata for cancelling AE precapture trigger applied in |
| 2748 | * handleAePrecaptureCancelRequest(). |
| 2749 | */ |
| 2750 | void Camera3Device::overrideResultForPrecaptureCancel( |
| 2751 | CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) { |
| 2752 | if (aeTriggerCancelOverride.applyAeLock) { |
| 2753 | // Only devices <= v3.2 should have this override |
| 2754 | assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2); |
| 2755 | result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1); |
| 2756 | } |
| 2757 | |
| 2758 | if (aeTriggerCancelOverride.applyAePrecaptureTrigger) { |
| 2759 | // Only devices <= v3.2 should have this override |
| 2760 | assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2); |
| 2761 | result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 2762 | &aeTriggerCancelOverride.aePrecaptureTrigger, 1); |
| 2763 | } |
| 2764 | } |
| 2765 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2766 | bool Camera3Device::RequestThread::threadLoop() { |
| 2767 | |
| 2768 | status_t res; |
| 2769 | |
| 2770 | // Handle paused state. |
| 2771 | if (waitIfPaused()) { |
| 2772 | return true; |
| 2773 | } |
| 2774 | |
| 2775 | // Get work to do |
| 2776 | |
| 2777 | sp<CaptureRequest> nextRequest = waitForNextRequest(); |
| 2778 | if (nextRequest == NULL) { |
| 2779 | return true; |
| 2780 | } |
| 2781 | |
| 2782 | // Create request to HAL |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2783 | camera3_capture_request_t request = camera3_capture_request_t(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2784 | request.frame_number = nextRequest->mResultExtras.frameNumber; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2785 | Vector<camera3_stream_buffer_t> outputBuffers; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2786 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2787 | // Get the request ID, if any |
| 2788 | int requestId; |
| 2789 | camera_metadata_entry_t requestIdEntry = |
| 2790 | nextRequest->mSettings.find(ANDROID_REQUEST_ID); |
| 2791 | if (requestIdEntry.count > 0) { |
| 2792 | requestId = requestIdEntry.data.i32[0]; |
| 2793 | } else { |
| 2794 | ALOGW("%s: Did not have android.request.id set in the request", |
| 2795 | __FUNCTION__); |
| 2796 | requestId = NAME_NOT_FOUND; |
| 2797 | } |
| 2798 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2799 | // Insert any queued triggers (before metadata is locked) |
| 2800 | int32_t triggerCount; |
| 2801 | res = insertTriggers(nextRequest); |
| 2802 | if (res < 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2803 | SET_ERR("RequestThread: Unable to insert triggers " |
| 2804 | "(capture request %d, HAL device: %s (%d)", |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2805 | request.frame_number, strerror(-res), res); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2806 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2807 | return false; |
| 2808 | } |
| 2809 | triggerCount = res; |
| 2810 | |
| 2811 | bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); |
| 2812 | |
| 2813 | // If the request is the same as last, or we had triggers last time |
| 2814 | if (mPrevRequest != nextRequest || triggersMixedIn) { |
| 2815 | /** |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 2816 | * HAL workaround: |
| 2817 | * Insert a dummy trigger ID if a trigger is set but no trigger ID is |
| 2818 | */ |
| 2819 | res = addDummyTriggerIds(nextRequest); |
| 2820 | if (res != OK) { |
| 2821 | SET_ERR("RequestThread: Unable to insert dummy trigger IDs " |
| 2822 | "(capture request %d, HAL device: %s (%d)", |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2823 | request.frame_number, strerror(-res), res); |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 2824 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2825 | return false; |
| 2826 | } |
| 2827 | |
| 2828 | /** |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2829 | * The request should be presorted so accesses in HAL |
| 2830 | * are O(logn). Sidenote, sorting a sorted metadata is nop. |
| 2831 | */ |
| 2832 | nextRequest->mSettings.sort(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2833 | request.settings = nextRequest->mSettings.getAndLock(); |
| 2834 | mPrevRequest = nextRequest; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2835 | ALOGVV("%s: Request settings are NEW", __FUNCTION__); |
| 2836 | |
| 2837 | IF_ALOGV() { |
| 2838 | camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); |
| 2839 | find_camera_metadata_ro_entry( |
| 2840 | request.settings, |
| 2841 | ANDROID_CONTROL_AF_TRIGGER, |
| 2842 | &e |
| 2843 | ); |
| 2844 | if (e.count > 0) { |
| 2845 | ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", |
| 2846 | __FUNCTION__, |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2847 | request.frame_number, |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2848 | e.data.u8[0]); |
| 2849 | } |
| 2850 | } |
| 2851 | } else { |
| 2852 | // leave request.settings NULL to indicate 'reuse latest given' |
| 2853 | ALOGVV("%s: Request settings are REUSED", |
| 2854 | __FUNCTION__); |
| 2855 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2856 | |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2857 | uint32_t totalNumBuffers = 0; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2858 | |
| 2859 | // Fill in buffers |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2860 | if (nextRequest->mInputStream != NULL) { |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2861 | request.input_buffer = &nextRequest->mInputBuffer; |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2862 | totalNumBuffers += 1; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2863 | } else { |
| 2864 | request.input_buffer = NULL; |
| 2865 | } |
| 2866 | |
| 2867 | outputBuffers.insertAt(camera3_stream_buffer_t(), 0, |
| 2868 | nextRequest->mOutputStreams.size()); |
| 2869 | request.output_buffers = outputBuffers.array(); |
| 2870 | for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) { |
| 2871 | res = nextRequest->mOutputStreams.editItemAt(i)-> |
| 2872 | getBuffer(&outputBuffers.editItemAt(i)); |
| 2873 | if (res != OK) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2874 | // Can't get output buffer from gralloc queue - this could be due to |
| 2875 | // abandoned queue or other consumer misbehavior, so not a fatal |
| 2876 | // error |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 2877 | ALOGE("RequestThread: Can't get output buffer, skipping request:" |
| 2878 | " %s (%d)", strerror(-res), res); |
Eino-Ville Talvala | 0ec23d3 | 2015-05-28 15:15:54 -0700 | [diff] [blame] | 2879 | { |
| 2880 | Mutex::Autolock l(mRequestLock); |
| 2881 | if (mListener != NULL) { |
| 2882 | mListener->notifyError( |
| 2883 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 2884 | nextRequest->mResultExtras); |
| 2885 | } |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2886 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2887 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2888 | return true; |
| 2889 | } |
| 2890 | request.num_output_buffers++; |
| 2891 | } |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2892 | totalNumBuffers += request.num_output_buffers; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2893 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2894 | // Log request in the in-flight queue |
| 2895 | sp<Camera3Device> parent = mParent.promote(); |
| 2896 | if (parent == NULL) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2897 | // Should not happen, and nowhere to send errors to, so just log it |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2898 | CLOGE("RequestThread: Parent is gone"); |
| 2899 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2900 | return false; |
| 2901 | } |
| 2902 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2903 | res = parent->registerInFlight(request.frame_number, |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2904 | totalNumBuffers, nextRequest->mResultExtras, |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 2905 | /*hasInput*/request.input_buffer != NULL, |
| 2906 | nextRequest->mAeTriggerCancelOverride); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2907 | ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64 |
| 2908 | ", burstId = %" PRId32 ".", |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2909 | __FUNCTION__, |
| 2910 | nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber, |
| 2911 | nextRequest->mResultExtras.burstId); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2912 | if (res != OK) { |
| 2913 | SET_ERR("RequestThread: Unable to register new in-flight request:" |
| 2914 | " %s (%d)", strerror(-res), res); |
| 2915 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2916 | return false; |
| 2917 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2918 | |
Zhijun He | cc27e11 | 2013-10-03 16:12:43 -0700 | [diff] [blame] | 2919 | // Inform waitUntilRequestProcessed thread of a new request ID |
| 2920 | { |
| 2921 | Mutex::Autolock al(mLatestRequestMutex); |
| 2922 | |
| 2923 | mLatestRequestId = requestId; |
| 2924 | mLatestRequestSignal.signal(); |
| 2925 | } |
| 2926 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2927 | // Submit request and block until ready for next one |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2928 | ATRACE_ASYNC_BEGIN("frame capture", request.frame_number); |
| 2929 | ATRACE_BEGIN("camera3->process_capture_request"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2930 | res = mHal3Device->ops->process_capture_request(mHal3Device, &request); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2931 | ATRACE_END(); |
| 2932 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2933 | if (res != OK) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2934 | // Should only get a failure here for malformed requests or device-level |
| 2935 | // errors, so consider all errors fatal. Bad metadata failures should |
| 2936 | // come through notify. |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2937 | SET_ERR("RequestThread: Unable to submit capture request %d to HAL" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2938 | " device: %s (%d)", request.frame_number, strerror(-res), res); |
| 2939 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2940 | return false; |
| 2941 | } |
| 2942 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2943 | // Update the latest request sent to HAL |
| 2944 | if (request.settings != NULL) { // Don't update them if they were unchanged |
| 2945 | Mutex::Autolock al(mLatestRequestMutex); |
| 2946 | |
| 2947 | camera_metadata_t* cloned = clone_camera_metadata(request.settings); |
| 2948 | mLatestRequest.acquire(cloned); |
| 2949 | } |
| 2950 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2951 | if (request.settings != NULL) { |
| 2952 | nextRequest->mSettings.unlock(request.settings); |
| 2953 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2954 | |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2955 | // Unset as current request |
| 2956 | { |
| 2957 | Mutex::Autolock l(mRequestLock); |
| 2958 | mNextRequest.clear(); |
| 2959 | } |
| 2960 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2961 | // Remove any previously queued triggers (after unlock) |
| 2962 | res = removeTriggers(mPrevRequest); |
| 2963 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2964 | SET_ERR("RequestThread: Unable to remove triggers " |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2965 | "(capture request %d, HAL device: %s (%d)", |
| 2966 | request.frame_number, strerror(-res), res); |
| 2967 | return false; |
| 2968 | } |
| 2969 | mPrevTriggers = triggerCount; |
| 2970 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2971 | return true; |
| 2972 | } |
| 2973 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2974 | CameraMetadata Camera3Device::RequestThread::getLatestRequest() const { |
| 2975 | Mutex::Autolock al(mLatestRequestMutex); |
| 2976 | |
| 2977 | ALOGV("RequestThread::%s", __FUNCTION__); |
| 2978 | |
| 2979 | return mLatestRequest; |
| 2980 | } |
| 2981 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2982 | bool Camera3Device::RequestThread::isStreamPending( |
| 2983 | sp<Camera3StreamInterface>& stream) { |
| 2984 | Mutex::Autolock l(mRequestLock); |
| 2985 | |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2986 | if (mNextRequest != nullptr) { |
| 2987 | for (const auto& s : mNextRequest->mOutputStreams) { |
| 2988 | if (stream == s) return true; |
| 2989 | } |
| 2990 | if (stream == mNextRequest->mInputStream) return true; |
| 2991 | } |
| 2992 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2993 | for (const auto& request : mRequestQueue) { |
| 2994 | for (const auto& s : request->mOutputStreams) { |
| 2995 | if (stream == s) return true; |
| 2996 | } |
| 2997 | if (stream == request->mInputStream) return true; |
| 2998 | } |
| 2999 | |
| 3000 | for (const auto& request : mRepeatingRequests) { |
| 3001 | for (const auto& s : request->mOutputStreams) { |
| 3002 | if (stream == s) return true; |
| 3003 | } |
| 3004 | if (stream == request->mInputStream) return true; |
| 3005 | } |
| 3006 | |
| 3007 | return false; |
| 3008 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3009 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3010 | void Camera3Device::RequestThread::cleanUpFailedRequest( |
| 3011 | camera3_capture_request_t &request, |
| 3012 | sp<CaptureRequest> &nextRequest, |
| 3013 | Vector<camera3_stream_buffer_t> &outputBuffers) { |
| 3014 | |
| 3015 | if (request.settings != NULL) { |
| 3016 | nextRequest->mSettings.unlock(request.settings); |
| 3017 | } |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 3018 | if (nextRequest->mInputStream != NULL) { |
| 3019 | nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; |
| 3020 | nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3021 | } |
| 3022 | for (size_t i = 0; i < request.num_output_buffers; i++) { |
| 3023 | outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 3024 | nextRequest->mOutputStreams.editItemAt(i)->returnBuffer( |
| 3025 | outputBuffers[i], 0); |
| 3026 | } |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 3027 | |
| 3028 | Mutex::Autolock l(mRequestLock); |
| 3029 | mNextRequest.clear(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | sp<Camera3Device::CaptureRequest> |
| 3033 | Camera3Device::RequestThread::waitForNextRequest() { |
| 3034 | status_t res; |
| 3035 | sp<CaptureRequest> nextRequest; |
| 3036 | |
| 3037 | // Optimized a bit for the simple steady-state case (single repeating |
| 3038 | // request), to avoid putting that request in the queue temporarily. |
| 3039 | Mutex::Autolock l(mRequestLock); |
| 3040 | |
| 3041 | while (mRequestQueue.empty()) { |
| 3042 | if (!mRepeatingRequests.empty()) { |
| 3043 | // Always atomically enqueue all requests in a repeating request |
| 3044 | // list. Guarantees a complete in-sequence set of captures to |
| 3045 | // application. |
| 3046 | const RequestList &requests = mRepeatingRequests; |
| 3047 | RequestList::const_iterator firstRequest = |
| 3048 | requests.begin(); |
| 3049 | nextRequest = *firstRequest; |
| 3050 | mRequestQueue.insert(mRequestQueue.end(), |
| 3051 | ++firstRequest, |
| 3052 | requests.end()); |
| 3053 | // No need to wait any longer |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3054 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3055 | mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 3056 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3057 | break; |
| 3058 | } |
| 3059 | |
| 3060 | res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); |
| 3061 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3062 | if ((mRequestQueue.empty() && mRepeatingRequests.empty()) || |
| 3063 | exitPending()) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3064 | Mutex::Autolock pl(mPauseLock); |
| 3065 | if (mPaused == false) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3066 | ALOGV("%s: RequestThread: Going idle", __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3067 | mPaused = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3068 | // Let the tracker know |
| 3069 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 3070 | if (statusTracker != 0) { |
| 3071 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); |
| 3072 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3073 | } |
| 3074 | // Stop waiting for now and let thread management happen |
| 3075 | return NULL; |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | if (nextRequest == NULL) { |
| 3080 | // Don't have a repeating request already in hand, so queue |
| 3081 | // must have an entry now. |
| 3082 | RequestList::iterator firstRequest = |
| 3083 | mRequestQueue.begin(); |
| 3084 | nextRequest = *firstRequest; |
| 3085 | mRequestQueue.erase(firstRequest); |
| 3086 | } |
| 3087 | |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3088 | // In case we've been unpaused by setPaused clearing mDoPause, need to |
| 3089 | // update internal pause state (capture/setRepeatingRequest unpause |
| 3090 | // directly). |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3091 | Mutex::Autolock pl(mPauseLock); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3092 | if (mPaused) { |
| 3093 | ALOGV("%s: RequestThread: Unpaused", __FUNCTION__); |
| 3094 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 3095 | if (statusTracker != 0) { |
| 3096 | statusTracker->markComponentActive(mStatusId); |
| 3097 | } |
| 3098 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3099 | mPaused = false; |
| 3100 | |
| 3101 | // Check if we've reconfigured since last time, and reset the preview |
| 3102 | // request if so. Can't use 'NULL request == repeat' across configure calls. |
| 3103 | if (mReconfigured) { |
| 3104 | mPrevRequest.clear(); |
| 3105 | mReconfigured = false; |
| 3106 | } |
| 3107 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3108 | if (nextRequest != NULL) { |
| 3109 | nextRequest->mResultExtras.frameNumber = mFrameNumber++; |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 3110 | nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId; |
| 3111 | nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId; |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 3112 | |
| 3113 | // Since RequestThread::clear() removes buffers from the input stream, |
| 3114 | // get the right buffer here before unlocking mRequestLock |
| 3115 | if (nextRequest->mInputStream != NULL) { |
| 3116 | res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer); |
| 3117 | if (res != OK) { |
| 3118 | // Can't get input buffer from gralloc queue - this could be due to |
| 3119 | // disconnected queue or other producer misbehavior, so not a fatal |
| 3120 | // error |
| 3121 | ALOGE("%s: Can't get input buffer, skipping request:" |
| 3122 | " %s (%d)", __FUNCTION__, strerror(-res), res); |
| 3123 | if (mListener != NULL) { |
| 3124 | mListener->notifyError( |
| 3125 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 3126 | nextRequest->mResultExtras); |
| 3127 | } |
| 3128 | return NULL; |
| 3129 | } |
| 3130 | } |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3131 | } |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 3132 | |
| 3133 | handleAePrecaptureCancelRequest(nextRequest); |
| 3134 | |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 3135 | mNextRequest = nextRequest; |
| 3136 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3137 | return nextRequest; |
| 3138 | } |
| 3139 | |
| 3140 | bool Camera3Device::RequestThread::waitIfPaused() { |
| 3141 | status_t res; |
| 3142 | Mutex::Autolock l(mPauseLock); |
| 3143 | while (mDoPause) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3144 | if (mPaused == false) { |
| 3145 | mPaused = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3146 | ALOGV("%s: RequestThread: Paused", __FUNCTION__); |
| 3147 | // Let the tracker know |
| 3148 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 3149 | if (statusTracker != 0) { |
| 3150 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); |
| 3151 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3152 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3153 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3154 | res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3155 | if (res == TIMED_OUT || exitPending()) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3156 | return true; |
| 3157 | } |
| 3158 | } |
| 3159 | // We don't set mPaused to false here, because waitForNextRequest needs |
| 3160 | // to further manage the paused state in case of starvation. |
| 3161 | return false; |
| 3162 | } |
| 3163 | |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3164 | void Camera3Device::RequestThread::unpauseForNewRequests() { |
| 3165 | // With work to do, mark thread as unpaused. |
| 3166 | // If paused by request (setPaused), don't resume, to avoid |
| 3167 | // extra signaling/waiting overhead to waitUntilPaused |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3168 | mRequestSignal.signal(); |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3169 | Mutex::Autolock p(mPauseLock); |
| 3170 | if (!mDoPause) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3171 | ALOGV("%s: RequestThread: Going active", __FUNCTION__); |
| 3172 | if (mPaused) { |
| 3173 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 3174 | if (statusTracker != 0) { |
| 3175 | statusTracker->markComponentActive(mStatusId); |
| 3176 | } |
| 3177 | } |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3178 | mPaused = false; |
| 3179 | } |
| 3180 | } |
| 3181 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 3182 | void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { |
| 3183 | sp<Camera3Device> parent = mParent.promote(); |
| 3184 | if (parent != NULL) { |
| 3185 | va_list args; |
| 3186 | va_start(args, fmt); |
| 3187 | |
| 3188 | parent->setErrorStateV(fmt, args); |
| 3189 | |
| 3190 | va_end(args); |
| 3191 | } |
| 3192 | } |
| 3193 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3194 | status_t Camera3Device::RequestThread::insertTriggers( |
| 3195 | const sp<CaptureRequest> &request) { |
| 3196 | |
| 3197 | Mutex::Autolock al(mTriggerMutex); |
| 3198 | |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3199 | sp<Camera3Device> parent = mParent.promote(); |
| 3200 | if (parent == NULL) { |
| 3201 | CLOGE("RequestThread: Parent is gone"); |
| 3202 | return DEAD_OBJECT; |
| 3203 | } |
| 3204 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3205 | CameraMetadata &metadata = request->mSettings; |
| 3206 | size_t count = mTriggerMap.size(); |
| 3207 | |
| 3208 | for (size_t i = 0; i < count; ++i) { |
| 3209 | RequestTrigger trigger = mTriggerMap.valueAt(i); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3210 | uint32_t tag = trigger.metadataTag; |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3211 | |
| 3212 | if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) { |
| 3213 | bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID); |
| 3214 | uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue); |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 3215 | if (isAeTrigger) { |
| 3216 | request->mResultExtras.precaptureTriggerId = triggerId; |
| 3217 | mCurrentPreCaptureTriggerId = triggerId; |
| 3218 | } else { |
| 3219 | request->mResultExtras.afTriggerId = triggerId; |
| 3220 | mCurrentAfTriggerId = triggerId; |
| 3221 | } |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3222 | if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 3223 | continue; // Trigger ID tag is deprecated since device HAL 3.2 |
| 3224 | } |
| 3225 | } |
| 3226 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3227 | camera_metadata_entry entry = metadata.find(tag); |
| 3228 | |
| 3229 | if (entry.count > 0) { |
| 3230 | /** |
| 3231 | * Already has an entry for this trigger in the request. |
| 3232 | * Rewrite it with our requested trigger value. |
| 3233 | */ |
| 3234 | RequestTrigger oldTrigger = trigger; |
| 3235 | |
| 3236 | oldTrigger.entryValue = entry.data.u8[0]; |
| 3237 | |
| 3238 | mTriggerReplacedMap.add(tag, oldTrigger); |
| 3239 | } else { |
| 3240 | /** |
| 3241 | * More typical, no trigger entry, so we just add it |
| 3242 | */ |
| 3243 | mTriggerRemovedMap.add(tag, trigger); |
| 3244 | } |
| 3245 | |
| 3246 | status_t res; |
| 3247 | |
| 3248 | switch (trigger.getTagType()) { |
| 3249 | case TYPE_BYTE: { |
| 3250 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 3251 | res = metadata.update(tag, |
| 3252 | &entryValue, |
| 3253 | /*count*/1); |
| 3254 | break; |
| 3255 | } |
| 3256 | case TYPE_INT32: |
| 3257 | res = metadata.update(tag, |
| 3258 | &trigger.entryValue, |
| 3259 | /*count*/1); |
| 3260 | break; |
| 3261 | default: |
| 3262 | ALOGE("%s: Type not supported: 0x%x", |
| 3263 | __FUNCTION__, |
| 3264 | trigger.getTagType()); |
| 3265 | return INVALID_OPERATION; |
| 3266 | } |
| 3267 | |
| 3268 | if (res != OK) { |
| 3269 | ALOGE("%s: Failed to update request metadata with trigger tag %s" |
| 3270 | ", value %d", __FUNCTION__, trigger.getTagName(), |
| 3271 | trigger.entryValue); |
| 3272 | return res; |
| 3273 | } |
| 3274 | |
| 3275 | ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, |
| 3276 | trigger.getTagName(), |
| 3277 | trigger.entryValue); |
| 3278 | } |
| 3279 | |
| 3280 | mTriggerMap.clear(); |
| 3281 | |
| 3282 | return count; |
| 3283 | } |
| 3284 | |
| 3285 | status_t Camera3Device::RequestThread::removeTriggers( |
| 3286 | const sp<CaptureRequest> &request) { |
| 3287 | Mutex::Autolock al(mTriggerMutex); |
| 3288 | |
| 3289 | CameraMetadata &metadata = request->mSettings; |
| 3290 | |
| 3291 | /** |
| 3292 | * Replace all old entries with their old values. |
| 3293 | */ |
| 3294 | for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { |
| 3295 | RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); |
| 3296 | |
| 3297 | status_t res; |
| 3298 | |
| 3299 | uint32_t tag = trigger.metadataTag; |
| 3300 | switch (trigger.getTagType()) { |
| 3301 | case TYPE_BYTE: { |
| 3302 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 3303 | res = metadata.update(tag, |
| 3304 | &entryValue, |
| 3305 | /*count*/1); |
| 3306 | break; |
| 3307 | } |
| 3308 | case TYPE_INT32: |
| 3309 | res = metadata.update(tag, |
| 3310 | &trigger.entryValue, |
| 3311 | /*count*/1); |
| 3312 | break; |
| 3313 | default: |
| 3314 | ALOGE("%s: Type not supported: 0x%x", |
| 3315 | __FUNCTION__, |
| 3316 | trigger.getTagType()); |
| 3317 | return INVALID_OPERATION; |
| 3318 | } |
| 3319 | |
| 3320 | if (res != OK) { |
| 3321 | ALOGE("%s: Failed to restore request metadata with trigger tag %s" |
| 3322 | ", trigger value %d", __FUNCTION__, |
| 3323 | trigger.getTagName(), trigger.entryValue); |
| 3324 | return res; |
| 3325 | } |
| 3326 | } |
| 3327 | mTriggerReplacedMap.clear(); |
| 3328 | |
| 3329 | /** |
| 3330 | * Remove all new entries. |
| 3331 | */ |
| 3332 | for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { |
| 3333 | RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); |
| 3334 | status_t res = metadata.erase(trigger.metadataTag); |
| 3335 | |
| 3336 | if (res != OK) { |
| 3337 | ALOGE("%s: Failed to erase metadata with trigger tag %s" |
| 3338 | ", trigger value %d", __FUNCTION__, |
| 3339 | trigger.getTagName(), trigger.entryValue); |
| 3340 | return res; |
| 3341 | } |
| 3342 | } |
| 3343 | mTriggerRemovedMap.clear(); |
| 3344 | |
| 3345 | return OK; |
| 3346 | } |
| 3347 | |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 3348 | status_t Camera3Device::RequestThread::addDummyTriggerIds( |
| 3349 | const sp<CaptureRequest> &request) { |
| 3350 | // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here |
| 3351 | static const int32_t dummyTriggerId = 1; |
| 3352 | status_t res; |
| 3353 | |
| 3354 | CameraMetadata &metadata = request->mSettings; |
| 3355 | |
| 3356 | // If AF trigger is active, insert a dummy AF trigger ID if none already |
| 3357 | // exists |
| 3358 | camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER); |
| 3359 | camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID); |
| 3360 | if (afTrigger.count > 0 && |
| 3361 | afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE && |
| 3362 | afId.count == 0) { |
| 3363 | res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1); |
| 3364 | if (res != OK) return res; |
| 3365 | } |
| 3366 | |
| 3367 | // If AE precapture trigger is active, insert a dummy precapture trigger ID |
| 3368 | // if none already exists |
| 3369 | camera_metadata_entry pcTrigger = |
| 3370 | metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); |
| 3371 | camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); |
| 3372 | if (pcTrigger.count > 0 && |
| 3373 | pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE && |
| 3374 | pcId.count == 0) { |
| 3375 | res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID, |
| 3376 | &dummyTriggerId, 1); |
| 3377 | if (res != OK) return res; |
| 3378 | } |
| 3379 | |
| 3380 | return OK; |
| 3381 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3382 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 3383 | /** |
| 3384 | * PreparerThread inner class methods |
| 3385 | */ |
| 3386 | |
| 3387 | Camera3Device::PreparerThread::PreparerThread() : |
| 3388 | Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) { |
| 3389 | } |
| 3390 | |
| 3391 | Camera3Device::PreparerThread::~PreparerThread() { |
| 3392 | Thread::requestExitAndWait(); |
| 3393 | if (mCurrentStream != nullptr) { |
| 3394 | mCurrentStream->cancelPrepare(); |
| 3395 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 3396 | mCurrentStream.clear(); |
| 3397 | } |
| 3398 | clear(); |
| 3399 | } |
| 3400 | |
| 3401 | status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) { |
| 3402 | status_t res; |
| 3403 | |
| 3404 | Mutex::Autolock l(mLock); |
| 3405 | |
| 3406 | res = stream->startPrepare(); |
| 3407 | if (res == OK) { |
| 3408 | // No preparation needed, fire listener right off |
| 3409 | ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId()); |
| 3410 | if (mListener) { |
| 3411 | mListener->notifyPrepared(stream->getId()); |
| 3412 | } |
| 3413 | return OK; |
| 3414 | } else if (res != NOT_ENOUGH_DATA) { |
| 3415 | return res; |
| 3416 | } |
| 3417 | |
| 3418 | // Need to prepare, start up thread if necessary |
| 3419 | if (!mActive) { |
| 3420 | // mRunning will change to false before the thread fully shuts down, so wait to be sure it |
| 3421 | // isn't running |
| 3422 | Thread::requestExitAndWait(); |
| 3423 | res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND); |
| 3424 | if (res != OK) { |
| 3425 | ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res)); |
| 3426 | if (mListener) { |
| 3427 | mListener->notifyPrepared(stream->getId()); |
| 3428 | } |
| 3429 | return res; |
| 3430 | } |
| 3431 | mCancelNow = false; |
| 3432 | mActive = true; |
| 3433 | ALOGV("%s: Preparer stream started", __FUNCTION__); |
| 3434 | } |
| 3435 | |
| 3436 | // queue up the work |
| 3437 | mPendingStreams.push_back(stream); |
| 3438 | ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId()); |
| 3439 | |
| 3440 | return OK; |
| 3441 | } |
| 3442 | |
| 3443 | status_t Camera3Device::PreparerThread::clear() { |
| 3444 | status_t res; |
| 3445 | |
| 3446 | Mutex::Autolock l(mLock); |
| 3447 | |
| 3448 | for (const auto& stream : mPendingStreams) { |
| 3449 | stream->cancelPrepare(); |
| 3450 | } |
| 3451 | mPendingStreams.clear(); |
| 3452 | mCancelNow = true; |
| 3453 | |
| 3454 | return OK; |
| 3455 | } |
| 3456 | |
| 3457 | void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) { |
| 3458 | Mutex::Autolock l(mLock); |
| 3459 | mListener = listener; |
| 3460 | } |
| 3461 | |
| 3462 | bool Camera3Device::PreparerThread::threadLoop() { |
| 3463 | status_t res; |
| 3464 | { |
| 3465 | Mutex::Autolock l(mLock); |
| 3466 | if (mCurrentStream == nullptr) { |
| 3467 | // End thread if done with work |
| 3468 | if (mPendingStreams.empty()) { |
| 3469 | ALOGV("%s: Preparer stream out of work", __FUNCTION__); |
| 3470 | // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would |
| 3471 | // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive. |
| 3472 | mActive = false; |
| 3473 | return false; |
| 3474 | } |
| 3475 | |
| 3476 | // Get next stream to prepare |
| 3477 | auto it = mPendingStreams.begin(); |
| 3478 | mCurrentStream = *it; |
| 3479 | mPendingStreams.erase(it); |
| 3480 | ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId()); |
| 3481 | ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId()); |
| 3482 | } else if (mCancelNow) { |
| 3483 | mCurrentStream->cancelPrepare(); |
| 3484 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 3485 | ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId()); |
| 3486 | mCurrentStream.clear(); |
| 3487 | mCancelNow = false; |
| 3488 | return true; |
| 3489 | } |
| 3490 | } |
| 3491 | |
| 3492 | res = mCurrentStream->prepareNextBuffer(); |
| 3493 | if (res == NOT_ENOUGH_DATA) return true; |
| 3494 | if (res != OK) { |
| 3495 | // Something bad happened; try to recover by cancelling prepare and |
| 3496 | // signalling listener anyway |
| 3497 | ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__, |
| 3498 | mCurrentStream->getId(), res, strerror(-res)); |
| 3499 | mCurrentStream->cancelPrepare(); |
| 3500 | } |
| 3501 | |
| 3502 | // This stream has finished, notify listener |
| 3503 | Mutex::Autolock l(mLock); |
| 3504 | if (mListener) { |
| 3505 | ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__, |
| 3506 | mCurrentStream->getId()); |
| 3507 | mListener->notifyPrepared(mCurrentStream->getId()); |
| 3508 | } |
| 3509 | |
| 3510 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 3511 | mCurrentStream.clear(); |
| 3512 | |
| 3513 | return true; |
| 3514 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3515 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3516 | /** |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 3517 | * Static callback forwarding methods from HAL to instance |
| 3518 | */ |
| 3519 | |
| 3520 | void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, |
| 3521 | const camera3_capture_result *result) { |
| 3522 | Camera3Device *d = |
| 3523 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
Chien-Yu Chen | d196d61 | 2015-06-22 19:49:01 -0700 | [diff] [blame] | 3524 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 3525 | d->processCaptureResult(result); |
| 3526 | } |
| 3527 | |
| 3528 | void Camera3Device::sNotify(const camera3_callback_ops *cb, |
| 3529 | const camera3_notify_msg *msg) { |
| 3530 | Camera3Device *d = |
| 3531 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
| 3532 | d->notify(msg); |
| 3533 | } |
| 3534 | |
| 3535 | }; // namespace android |