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); |
Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame] | 1036 | mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed; |
Igor Murashkin | e2d167e | 2014-08-19 16:19:59 -0700 | [diff] [blame] | 1037 | |
| 1038 | return configureStreamsLocked(); |
| 1039 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1040 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1041 | status_t Camera3Device::getInputBufferProducer( |
| 1042 | sp<IGraphicBufferProducer> *producer) { |
| 1043 | Mutex::Autolock il(mInterfaceLock); |
| 1044 | Mutex::Autolock l(mLock); |
| 1045 | |
| 1046 | if (producer == NULL) { |
| 1047 | return BAD_VALUE; |
| 1048 | } else if (mInputStream == NULL) { |
| 1049 | return INVALID_OPERATION; |
| 1050 | } |
| 1051 | |
| 1052 | return mInputStream->getInputBufferProducer(producer); |
| 1053 | } |
| 1054 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1055 | status_t Camera3Device::createDefaultRequest(int templateId, |
| 1056 | CameraMetadata *request) { |
| 1057 | ATRACE_CALL(); |
Alex Ray | fe7e0c6 | 2013-05-30 00:12:13 -0700 | [diff] [blame] | 1058 | ALOGV("%s: for template %d", __FUNCTION__, templateId); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1059 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1060 | Mutex::Autolock l(mLock); |
| 1061 | |
| 1062 | switch (mStatus) { |
| 1063 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1064 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1065 | return INVALID_OPERATION; |
| 1066 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1067 | CLOGE("Device is not initialized!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1068 | return INVALID_OPERATION; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1069 | case STATUS_UNCONFIGURED: |
| 1070 | case STATUS_CONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1071 | case STATUS_ACTIVE: |
| 1072 | // OK |
| 1073 | break; |
| 1074 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1075 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1076 | return INVALID_OPERATION; |
| 1077 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1078 | |
Zhijun He | a1530f1 | 2014-09-14 12:44:20 -0700 | [diff] [blame] | 1079 | if (!mRequestTemplateCache[templateId].isEmpty()) { |
| 1080 | *request = mRequestTemplateCache[templateId]; |
| 1081 | return OK; |
| 1082 | } |
| 1083 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1084 | const camera_metadata_t *rawRequest; |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1085 | ATRACE_BEGIN("camera3->construct_default_request_settings"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1086 | rawRequest = mHal3Device->ops->construct_default_request_settings( |
| 1087 | mHal3Device, templateId); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1088 | ATRACE_END(); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1089 | if (rawRequest == NULL) { |
Yin-Chia Yeh | 0336d36 | 2015-04-14 12:34:22 -0700 | [diff] [blame] | 1090 | ALOGI("%s: template %d is not supported on this camera device", |
| 1091 | __FUNCTION__, templateId); |
| 1092 | return BAD_VALUE; |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1093 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1094 | *request = rawRequest; |
Zhijun He | a1530f1 | 2014-09-14 12:44:20 -0700 | [diff] [blame] | 1095 | mRequestTemplateCache[templateId] = rawRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1096 | |
| 1097 | return OK; |
| 1098 | } |
| 1099 | |
| 1100 | status_t Camera3Device::waitUntilDrained() { |
| 1101 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1102 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1103 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1104 | |
Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 1105 | return waitUntilDrainedLocked(); |
| 1106 | } |
| 1107 | |
| 1108 | status_t Camera3Device::waitUntilDrainedLocked() { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1109 | switch (mStatus) { |
| 1110 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1111 | case STATUS_UNCONFIGURED: |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1112 | ALOGV("%s: Already idle", __FUNCTION__); |
| 1113 | return OK; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1114 | case STATUS_CONFIGURED: |
| 1115 | // To avoid race conditions, check with tracker to be sure |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1116 | case STATUS_ERROR: |
| 1117 | case STATUS_ACTIVE: |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1118 | // Need to verify shut down |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1119 | break; |
| 1120 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1121 | SET_ERR_L("Unexpected status: %d",mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1122 | return INVALID_OPERATION; |
| 1123 | } |
| 1124 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1125 | ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId); |
| 1126 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
Eino-Ville Talvala | 9c8a091 | 2014-09-14 14:52:19 -0700 | [diff] [blame] | 1127 | if (res != OK) { |
| 1128 | SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res), |
| 1129 | res); |
| 1130 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1131 | return res; |
| 1132 | } |
| 1133 | |
| 1134 | // Pause to reconfigure |
| 1135 | status_t Camera3Device::internalPauseAndWaitLocked() { |
| 1136 | mRequestThread->setPaused(true); |
| 1137 | mPauseStateNotify = true; |
| 1138 | |
| 1139 | ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId); |
| 1140 | status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout); |
| 1141 | if (res != OK) { |
| 1142 | SET_ERR_L("Can't idle device in %f seconds!", |
| 1143 | kShutdownTimeout/1e9); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1146 | return res; |
| 1147 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1148 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1149 | // Resume after internalPauseAndWaitLocked |
| 1150 | status_t Camera3Device::internalResumeLocked() { |
| 1151 | status_t res; |
| 1152 | |
| 1153 | mRequestThread->setPaused(false); |
| 1154 | |
| 1155 | res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout); |
| 1156 | if (res != OK) { |
| 1157 | SET_ERR_L("Can't transition to active in %f seconds!", |
| 1158 | kActiveTimeout/1e9); |
| 1159 | } |
| 1160 | mPauseStateNotify = false; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1161 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1164 | status_t Camera3Device::waitUntilStateThenRelock(bool active, |
| 1165 | nsecs_t timeout) { |
| 1166 | status_t res = OK; |
| 1167 | if (active == (mStatus == STATUS_ACTIVE)) { |
| 1168 | // Desired state already reached |
| 1169 | return res; |
| 1170 | } |
| 1171 | |
| 1172 | bool stateSeen = false; |
| 1173 | do { |
| 1174 | mRecentStatusUpdates.clear(); |
| 1175 | |
| 1176 | res = mStatusChanged.waitRelative(mLock, timeout); |
| 1177 | if (res != OK) break; |
| 1178 | |
| 1179 | // Check state change history during wait |
| 1180 | for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) { |
| 1181 | if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) { |
| 1182 | stateSeen = true; |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | } while (!stateSeen); |
| 1187 | |
| 1188 | return res; |
| 1189 | } |
| 1190 | |
| 1191 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1192 | status_t Camera3Device::setNotifyCallback(NotificationListener *listener) { |
| 1193 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1194 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1195 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1196 | if (listener != NULL && mListener != NULL) { |
| 1197 | ALOGW("%s: Replacing old callback listener", __FUNCTION__); |
| 1198 | } |
| 1199 | mListener = listener; |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1200 | mRequestThread->setNotificationListener(listener); |
| 1201 | mPreparerThread->setNotificationListener(listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1202 | |
| 1203 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Eino-Ville Talvala | 46910bd | 2013-07-18 19:15:17 -0700 | [diff] [blame] | 1206 | bool Camera3Device::willNotify3A() { |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1210 | status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1211 | status_t res; |
| 1212 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1213 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1214 | while (mResultQueue.empty()) { |
| 1215 | res = mResultSignal.waitRelative(mOutputLock, timeout); |
| 1216 | if (res == TIMED_OUT) { |
| 1217 | return res; |
| 1218 | } else if (res != OK) { |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 1219 | ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1220 | __FUNCTION__, mId, timeout, strerror(-res), res); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1221 | return res; |
| 1222 | } |
| 1223 | } |
| 1224 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1227 | status_t Camera3Device::getNextResult(CaptureResult *frame) { |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1228 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1229 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1230 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1231 | if (mResultQueue.empty()) { |
| 1232 | return NOT_ENOUGH_DATA; |
| 1233 | } |
| 1234 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1235 | if (frame == NULL) { |
| 1236 | ALOGE("%s: argument cannot be NULL", __FUNCTION__); |
| 1237 | return BAD_VALUE; |
| 1238 | } |
| 1239 | |
| 1240 | CaptureResult &result = *(mResultQueue.begin()); |
| 1241 | frame->mResultExtras = result.mResultExtras; |
| 1242 | frame->mMetadata.acquire(result.mMetadata); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1243 | mResultQueue.erase(mResultQueue.begin()); |
| 1244 | |
| 1245 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | status_t Camera3Device::triggerAutofocus(uint32_t id) { |
| 1249 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1250 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1251 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1252 | ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); |
| 1253 | // Mix-in this trigger into the next request and only the next request. |
| 1254 | RequestTrigger trigger[] = { |
| 1255 | { |
| 1256 | ANDROID_CONTROL_AF_TRIGGER, |
| 1257 | ANDROID_CONTROL_AF_TRIGGER_START |
| 1258 | }, |
| 1259 | { |
| 1260 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 1261 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1262 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1263 | }; |
| 1264 | |
| 1265 | return mRequestThread->queueTrigger(trigger, |
| 1266 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { |
| 1270 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1271 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1272 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1273 | ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); |
| 1274 | // Mix-in this trigger into the next request and only the next request. |
| 1275 | RequestTrigger trigger[] = { |
| 1276 | { |
| 1277 | ANDROID_CONTROL_AF_TRIGGER, |
| 1278 | ANDROID_CONTROL_AF_TRIGGER_CANCEL |
| 1279 | }, |
| 1280 | { |
| 1281 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 1282 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1283 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1284 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1285 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1286 | return mRequestThread->queueTrigger(trigger, |
| 1287 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { |
| 1291 | ATRACE_CALL(); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1292 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1293 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1294 | ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); |
| 1295 | // Mix-in this trigger into the next request and only the next request. |
| 1296 | RequestTrigger trigger[] = { |
| 1297 | { |
| 1298 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 1299 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START |
| 1300 | }, |
| 1301 | { |
| 1302 | ANDROID_CONTROL_AE_PRECAPTURE_ID, |
| 1303 | static_cast<int32_t>(id) |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1304 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1305 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1306 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1307 | return mRequestThread->queueTrigger(trigger, |
| 1308 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId, |
| 1312 | buffer_handle_t *buffer, wp<BufferReleasedListener> listener) { |
| 1313 | ATRACE_CALL(); |
| 1314 | (void)reprocessStreamId; (void)buffer; (void)listener; |
| 1315 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1316 | CLOGE("Unimplemented"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1317 | return INVALID_OPERATION; |
| 1318 | } |
| 1319 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1320 | status_t Camera3Device::flush(int64_t *frameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1321 | ATRACE_CALL(); |
| 1322 | ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1323 | Mutex::Autolock il(mInterfaceLock); |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1324 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1325 | NotificationListener* listener; |
| 1326 | { |
| 1327 | Mutex::Autolock l(mOutputLock); |
| 1328 | listener = mListener; |
| 1329 | } |
| 1330 | |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1331 | { |
| 1332 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1333 | mRequestThread->clear(listener, /*out*/frameNumber); |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Zhijun He | 491e341 | 2013-12-27 10:57:44 -0800 | [diff] [blame] | 1336 | status_t res; |
| 1337 | if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) { |
| 1338 | res = mHal3Device->ops->flush(mHal3Device); |
| 1339 | } else { |
Zhijun He | 7ef2039 | 2014-04-21 16:04:17 -0700 | [diff] [blame] | 1340 | Mutex::Autolock l(mLock); |
Zhijun He | 69a3748 | 2014-03-23 18:44:49 -0700 | [diff] [blame] | 1341 | res = waitUntilDrainedLocked(); |
Zhijun He | 491e341 | 2013-12-27 10:57:44 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | return res; |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1347 | status_t Camera3Device::prepare(int streamId) { |
| 1348 | ATRACE_CALL(); |
| 1349 | ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId); |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1350 | Mutex::Autolock il(mInterfaceLock); |
| 1351 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1352 | |
| 1353 | sp<Camera3StreamInterface> stream; |
| 1354 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId); |
| 1355 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 1356 | CLOGE("Stream %d does not exist", streamId); |
| 1357 | return BAD_VALUE; |
| 1358 | } |
| 1359 | |
| 1360 | stream = mOutputStreams.editValueAt(outputStreamIdx); |
| 1361 | |
| 1362 | if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) { |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1363 | CLOGE("Stream %d has already been a request target", streamId); |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1364 | return BAD_VALUE; |
| 1365 | } |
| 1366 | |
| 1367 | if (mRequestThread->isStreamPending(stream)) { |
Eino-Ville Talvala | 261394e | 2015-05-13 14:28:38 -0700 | [diff] [blame] | 1368 | 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] | 1369 | return BAD_VALUE; |
| 1370 | } |
| 1371 | |
| 1372 | return mPreparerThread->prepare(stream); |
| 1373 | } |
| 1374 | |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1375 | uint32_t Camera3Device::getDeviceVersion() { |
| 1376 | ATRACE_CALL(); |
| 1377 | Mutex::Autolock il(mInterfaceLock); |
| 1378 | return mDeviceVersion; |
| 1379 | } |
| 1380 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1381 | /** |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1382 | * Methods called by subclasses |
| 1383 | */ |
| 1384 | |
| 1385 | void Camera3Device::notifyStatus(bool idle) { |
| 1386 | { |
| 1387 | // Need mLock to safely update state and synchronize to current |
| 1388 | // state of methods in flight. |
| 1389 | Mutex::Autolock l(mLock); |
| 1390 | // We can get various system-idle notices from the status tracker |
| 1391 | // while starting up. Only care about them if we've actually sent |
| 1392 | // in some requests recently. |
| 1393 | if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) { |
| 1394 | return; |
| 1395 | } |
| 1396 | ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId, |
| 1397 | idle ? "idle" : "active"); |
| 1398 | mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE; |
| 1399 | mRecentStatusUpdates.add(mStatus); |
| 1400 | mStatusChanged.signal(); |
| 1401 | |
| 1402 | // Skip notifying listener if we're doing some user-transparent |
| 1403 | // state changes |
| 1404 | if (mPauseStateNotify) return; |
| 1405 | } |
| 1406 | NotificationListener *listener; |
| 1407 | { |
| 1408 | Mutex::Autolock l(mOutputLock); |
| 1409 | listener = mListener; |
| 1410 | } |
| 1411 | if (idle && listener != NULL) { |
| 1412 | listener->notifyIdle(); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1417 | * Camera3Device private methods |
| 1418 | */ |
| 1419 | |
| 1420 | sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( |
| 1421 | const CameraMetadata &request) { |
| 1422 | ATRACE_CALL(); |
| 1423 | status_t res; |
| 1424 | |
| 1425 | sp<CaptureRequest> newRequest = new CaptureRequest; |
| 1426 | newRequest->mSettings = request; |
| 1427 | |
| 1428 | camera_metadata_entry_t inputStreams = |
| 1429 | newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); |
| 1430 | if (inputStreams.count > 0) { |
| 1431 | if (mInputStream == NULL || |
Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1432 | mInputStream->getId() != inputStreams.data.i32[0]) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1433 | CLOGE("Request references unknown input stream %d", |
| 1434 | inputStreams.data.u8[0]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1435 | return NULL; |
| 1436 | } |
| 1437 | // Lazy completion of stream configuration (allocation/registration) |
| 1438 | // on first use |
| 1439 | if (mInputStream->isConfiguring()) { |
| 1440 | res = mInputStream->finishConfiguration(mHal3Device); |
| 1441 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1442 | SET_ERR_L("Unable to finish configuring input stream %d:" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1443 | " %s (%d)", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1444 | mInputStream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1445 | return NULL; |
| 1446 | } |
| 1447 | } |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1448 | // Check if stream is being prepared |
| 1449 | if (mInputStream->isPreparing()) { |
| 1450 | CLOGE("Request references an input stream that's being prepared!"); |
| 1451 | return NULL; |
| 1452 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1453 | |
| 1454 | newRequest->mInputStream = mInputStream; |
| 1455 | newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); |
| 1456 | } |
| 1457 | |
| 1458 | camera_metadata_entry_t streams = |
| 1459 | newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 1460 | if (streams.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1461 | CLOGE("Zero output streams specified!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1462 | return NULL; |
| 1463 | } |
| 1464 | |
| 1465 | for (size_t i = 0; i < streams.count; i++) { |
Zhijun He | d1d6467 | 2013-09-06 15:00:01 -0700 | [diff] [blame] | 1466 | int idx = mOutputStreams.indexOfKey(streams.data.i32[i]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1467 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1468 | CLOGE("Request references unknown stream %d", |
| 1469 | streams.data.u8[i]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1470 | return NULL; |
| 1471 | } |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1472 | sp<Camera3OutputStreamInterface> stream = |
| 1473 | mOutputStreams.editValueAt(idx); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1474 | |
| 1475 | // Lazy completion of stream configuration (allocation/registration) |
| 1476 | // on first use |
| 1477 | if (stream->isConfiguring()) { |
| 1478 | res = stream->finishConfiguration(mHal3Device); |
| 1479 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1480 | SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", |
| 1481 | stream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1482 | return NULL; |
| 1483 | } |
| 1484 | } |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 1485 | // Check if stream is being prepared |
| 1486 | if (stream->isPreparing()) { |
| 1487 | CLOGE("Request references an output stream that's being prepared!"); |
| 1488 | return NULL; |
| 1489 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1490 | |
| 1491 | newRequest->mOutputStreams.push(stream); |
| 1492 | } |
| 1493 | newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 1494 | |
| 1495 | return newRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1496 | } |
| 1497 | |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 1498 | bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) { |
| 1499 | for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) { |
| 1500 | Size size = mSupportedOpaqueInputSizes[i]; |
| 1501 | if (size.width == width && size.height == height) { |
| 1502 | return true; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | return false; |
| 1507 | } |
| 1508 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1509 | status_t Camera3Device::configureStreamsLocked() { |
| 1510 | ATRACE_CALL(); |
| 1511 | status_t res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1512 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1513 | if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1514 | CLOGE("Not idle"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1515 | return INVALID_OPERATION; |
| 1516 | } |
| 1517 | |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1518 | if (!mNeedConfig) { |
| 1519 | ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); |
| 1520 | return OK; |
| 1521 | } |
| 1522 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1523 | // Workaround for device HALv3.2 or older spec bug - zero streams requires |
| 1524 | // adding a dummy stream instead. |
| 1525 | // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround. |
| 1526 | if (mOutputStreams.size() == 0) { |
| 1527 | addDummyStreamLocked(); |
| 1528 | } else { |
| 1529 | tryRemoveDummyStreamLocked(); |
| 1530 | } |
| 1531 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1532 | // Start configuring the streams |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1533 | ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1534 | |
| 1535 | camera3_stream_configuration config; |
Zhijun He | 1fa8999 | 2015-06-01 15:44:31 -0700 | [diff] [blame] | 1536 | config.operation_mode = mIsConstrainedHighSpeedConfiguration ? |
| 1537 | CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE : |
| 1538 | CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1539 | config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); |
| 1540 | |
| 1541 | Vector<camera3_stream_t*> streams; |
| 1542 | streams.setCapacity(config.num_streams); |
| 1543 | |
| 1544 | if (mInputStream != NULL) { |
| 1545 | camera3_stream_t *inputStream; |
| 1546 | inputStream = mInputStream->startConfiguration(); |
| 1547 | if (inputStream == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1548 | SET_ERR_L("Can't start input stream configuration"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1549 | return INVALID_OPERATION; |
| 1550 | } |
| 1551 | streams.add(inputStream); |
| 1552 | } |
| 1553 | |
| 1554 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1555 | |
| 1556 | // Don't configure bidi streams twice, nor add them twice to the list |
| 1557 | if (mOutputStreams[i].get() == |
| 1558 | static_cast<Camera3StreamInterface*>(mInputStream.get())) { |
| 1559 | |
| 1560 | config.num_streams--; |
| 1561 | continue; |
| 1562 | } |
| 1563 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1564 | camera3_stream_t *outputStream; |
| 1565 | outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); |
| 1566 | if (outputStream == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1567 | SET_ERR_L("Can't start output stream configuration"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1568 | return INVALID_OPERATION; |
| 1569 | } |
| 1570 | streams.add(outputStream); |
| 1571 | } |
| 1572 | |
| 1573 | config.streams = streams.editArray(); |
| 1574 | |
| 1575 | // Do the HAL configuration; will potentially touch stream |
| 1576 | // max_buffers, usage, priv fields. |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1577 | ATRACE_BEGIN("camera3->configure_streams"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1578 | res = mHal3Device->ops->configure_streams(mHal3Device, &config); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1579 | ATRACE_END(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1580 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1581 | if (res == BAD_VALUE) { |
| 1582 | // HAL rejected this set of streams as unsupported, clean up config |
| 1583 | // attempt and return to unconfigured state |
| 1584 | if (mInputStream != NULL && mInputStream->isConfiguring()) { |
| 1585 | res = mInputStream->cancelConfiguration(); |
| 1586 | if (res != OK) { |
| 1587 | SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)", |
| 1588 | mInputStream->getId(), strerror(-res), res); |
| 1589 | return res; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
| 1594 | sp<Camera3OutputStreamInterface> outputStream = |
| 1595 | mOutputStreams.editValueAt(i); |
| 1596 | if (outputStream->isConfiguring()) { |
| 1597 | res = outputStream->cancelConfiguration(); |
| 1598 | if (res != OK) { |
| 1599 | SET_ERR_L( |
| 1600 | "Can't cancel configuring output stream %d: %s (%d)", |
| 1601 | outputStream->getId(), strerror(-res), res); |
| 1602 | return res; |
| 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | // Return state to that at start of call, so that future configures |
| 1608 | // properly clean things up |
| 1609 | mStatus = STATUS_UNCONFIGURED; |
| 1610 | mNeedConfig = true; |
| 1611 | |
| 1612 | ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId); |
| 1613 | return BAD_VALUE; |
| 1614 | } else if (res != OK) { |
| 1615 | // Some other kind of error from configure_streams - this is not |
| 1616 | // expected |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1617 | SET_ERR_L("Unable to configure streams with HAL: %s (%d)", |
| 1618 | strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1619 | return res; |
| 1620 | } |
| 1621 | |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1622 | // Finish all stream configuration immediately. |
| 1623 | // TODO: Try to relax this later back to lazy completion, which should be |
| 1624 | // faster |
| 1625 | |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1626 | if (mInputStream != NULL && mInputStream->isConfiguring()) { |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1627 | res = mInputStream->finishConfiguration(mHal3Device); |
| 1628 | if (res != OK) { |
| 1629 | SET_ERR_L("Can't finish configuring input stream %d: %s (%d)", |
| 1630 | mInputStream->getId(), strerror(-res), res); |
| 1631 | return res; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1636 | sp<Camera3OutputStreamInterface> outputStream = |
| 1637 | mOutputStreams.editValueAt(i); |
| 1638 | if (outputStream->isConfiguring()) { |
| 1639 | res = outputStream->finishConfiguration(mHal3Device); |
| 1640 | if (res != OK) { |
| 1641 | SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", |
| 1642 | outputStream->getId(), strerror(-res), res); |
| 1643 | return res; |
| 1644 | } |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1648 | // Request thread needs to know to avoid using repeat-last-settings protocol |
| 1649 | // across configure_streams() calls |
| 1650 | mRequestThread->configurationComplete(); |
| 1651 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1652 | // Update device state |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1653 | |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1654 | mNeedConfig = false; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1655 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1656 | if (mDummyStreamId == NO_STREAM) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1657 | mStatus = STATUS_CONFIGURED; |
| 1658 | } else { |
| 1659 | mStatus = STATUS_UNCONFIGURED; |
| 1660 | } |
| 1661 | |
| 1662 | ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId); |
| 1663 | |
Zhijun He | 0a21051 | 2014-07-24 13:45:15 -0700 | [diff] [blame] | 1664 | // tear down the deleted streams after configure streams. |
| 1665 | mDeletedStreams.clear(); |
| 1666 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1667 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
Eino-Ville Talvala | 16a2ada | 2014-08-27 14:41:33 -0700 | [diff] [blame] | 1670 | status_t Camera3Device::addDummyStreamLocked() { |
| 1671 | ATRACE_CALL(); |
| 1672 | status_t res; |
| 1673 | |
| 1674 | if (mDummyStreamId != NO_STREAM) { |
| 1675 | // Should never be adding a second dummy stream when one is already |
| 1676 | // active |
| 1677 | SET_ERR_L("%s: Camera %d: A dummy stream already exists!", |
| 1678 | __FUNCTION__, mId); |
| 1679 | return INVALID_OPERATION; |
| 1680 | } |
| 1681 | |
| 1682 | ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId); |
| 1683 | |
| 1684 | sp<Camera3OutputStreamInterface> dummyStream = |
| 1685 | new Camera3DummyStream(mNextStreamId); |
| 1686 | |
| 1687 | res = mOutputStreams.add(mNextStreamId, dummyStream); |
| 1688 | if (res < 0) { |
| 1689 | SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res); |
| 1690 | return res; |
| 1691 | } |
| 1692 | |
| 1693 | mDummyStreamId = mNextStreamId; |
| 1694 | mNextStreamId++; |
| 1695 | |
| 1696 | return OK; |
| 1697 | } |
| 1698 | |
| 1699 | status_t Camera3Device::tryRemoveDummyStreamLocked() { |
| 1700 | ATRACE_CALL(); |
| 1701 | status_t res; |
| 1702 | |
| 1703 | if (mDummyStreamId == NO_STREAM) return OK; |
| 1704 | if (mOutputStreams.size() == 1) return OK; |
| 1705 | |
| 1706 | ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId); |
| 1707 | |
| 1708 | // Ok, have a dummy stream and there's at least one other output stream, |
| 1709 | // so remove the dummy |
| 1710 | |
| 1711 | sp<Camera3StreamInterface> deletedStream; |
| 1712 | ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId); |
| 1713 | if (outputStreamIdx == NAME_NOT_FOUND) { |
| 1714 | SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId); |
| 1715 | return INVALID_OPERATION; |
| 1716 | } |
| 1717 | |
| 1718 | deletedStream = mOutputStreams.editValueAt(outputStreamIdx); |
| 1719 | mOutputStreams.removeItemsAt(outputStreamIdx); |
| 1720 | |
| 1721 | // Free up the stream endpoint so that it can be used by some other stream |
| 1722 | res = deletedStream->disconnect(); |
| 1723 | if (res != OK) { |
| 1724 | SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId); |
| 1725 | // fall through since we want to still list the stream as deleted. |
| 1726 | } |
| 1727 | mDeletedStreams.add(deletedStream); |
| 1728 | mDummyStreamId = NO_STREAM; |
| 1729 | |
| 1730 | return res; |
| 1731 | } |
| 1732 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1733 | void Camera3Device::setErrorState(const char *fmt, ...) { |
| 1734 | Mutex::Autolock l(mLock); |
| 1735 | va_list args; |
| 1736 | va_start(args, fmt); |
| 1737 | |
| 1738 | setErrorStateLockedV(fmt, args); |
| 1739 | |
| 1740 | va_end(args); |
| 1741 | } |
| 1742 | |
| 1743 | void Camera3Device::setErrorStateV(const char *fmt, va_list args) { |
| 1744 | Mutex::Autolock l(mLock); |
| 1745 | setErrorStateLockedV(fmt, args); |
| 1746 | } |
| 1747 | |
| 1748 | void Camera3Device::setErrorStateLocked(const char *fmt, ...) { |
| 1749 | va_list args; |
| 1750 | va_start(args, fmt); |
| 1751 | |
| 1752 | setErrorStateLockedV(fmt, args); |
| 1753 | |
| 1754 | va_end(args); |
| 1755 | } |
| 1756 | |
| 1757 | void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1758 | // Print out all error messages to log |
| 1759 | String8 errorCause = String8::formatV(fmt, args); |
| 1760 | ALOGE("Camera %d: %s", mId, errorCause.string()); |
| 1761 | |
| 1762 | // But only do error state transition steps for the first error |
Zhijun He | b05eeae | 2013-06-06 13:51:22 -0700 | [diff] [blame] | 1763 | if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1764 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1765 | mErrorCause = errorCause; |
| 1766 | |
| 1767 | mRequestThread->setPaused(true); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1768 | mStatus = STATUS_ERROR; |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 1769 | |
| 1770 | // Notify upstream about a device error |
| 1771 | if (mListener != NULL) { |
| 1772 | mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
| 1773 | CaptureResultExtras()); |
| 1774 | } |
| 1775 | |
| 1776 | // Save stack trace. View by dumping it later. |
| 1777 | CameraTraces::saveTrace(); |
| 1778 | // TODO: consider adding errorCause and client pid/procname |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1779 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1780 | |
| 1781 | /** |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1782 | * In-flight request management |
| 1783 | */ |
| 1784 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1785 | status_t Camera3Device::registerInFlight(uint32_t frameNumber, |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 1786 | int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1787 | ATRACE_CALL(); |
| 1788 | Mutex::Autolock l(mInFlightLock); |
| 1789 | |
| 1790 | ssize_t res; |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 1791 | res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput)); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1792 | if (res < 0) return res; |
| 1793 | |
| 1794 | return OK; |
| 1795 | } |
| 1796 | |
| 1797 | /** |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1798 | * Check if all 3A fields are ready, and send off a partial 3A-only result |
| 1799 | * to the output frame queue |
| 1800 | */ |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1801 | bool Camera3Device::processPartial3AResult( |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1802 | uint32_t frameNumber, |
| 1803 | const CameraMetadata& partial, const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1804 | |
| 1805 | // Check if all 3A states are present |
| 1806 | // The full list of fields is |
| 1807 | // android.control.afMode |
| 1808 | // android.control.awbMode |
| 1809 | // android.control.aeState |
| 1810 | // android.control.awbState |
| 1811 | // android.control.afState |
| 1812 | // android.control.afTriggerID |
| 1813 | // android.control.aePrecaptureID |
| 1814 | // TODO: Add android.control.aeMode |
| 1815 | |
| 1816 | bool gotAllStates = true; |
| 1817 | |
| 1818 | uint8_t afMode; |
| 1819 | uint8_t awbMode; |
| 1820 | uint8_t aeState; |
| 1821 | uint8_t afState; |
| 1822 | uint8_t awbState; |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1823 | |
| 1824 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE, |
| 1825 | &afMode, frameNumber); |
| 1826 | |
| 1827 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE, |
| 1828 | &awbMode, frameNumber); |
| 1829 | |
| 1830 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE, |
| 1831 | &aeState, frameNumber); |
| 1832 | |
| 1833 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE, |
| 1834 | &afState, frameNumber); |
| 1835 | |
| 1836 | gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE, |
| 1837 | &awbState, frameNumber); |
| 1838 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1839 | if (!gotAllStates) return false; |
| 1840 | |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1841 | 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] | 1842 | "AF state %d, AE state %d, AWB state %d, " |
| 1843 | "AF trigger %d, AE precapture trigger %d", |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 1844 | __FUNCTION__, mId, frameNumber, resultExtras.requestId, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1845 | afMode, awbMode, |
| 1846 | afState, aeState, awbState, |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1847 | resultExtras.afTriggerId, resultExtras.precaptureTriggerId); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1848 | |
| 1849 | // Got all states, so construct a minimal result to send |
| 1850 | // In addition to the above fields, this means adding in |
| 1851 | // android.request.frameCount |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1852 | // android.request.requestId |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1853 | // android.quirks.partialResult (for HAL version below HAL3.2) |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1854 | |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1855 | const size_t kMinimal3AResultEntries = 10; |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1856 | |
| 1857 | Mutex::Autolock l(mOutputLock); |
| 1858 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1859 | CaptureResult captureResult; |
| 1860 | captureResult.mResultExtras = resultExtras; |
| 1861 | captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0); |
| 1862 | // TODO: change this to sp<CaptureResult>. This will need other changes, including, |
| 1863 | // but not limited to CameraDeviceBase::getNextResult |
| 1864 | CaptureResult& min3AResult = |
| 1865 | *mResultQueue.insert(mResultQueue.end(), captureResult); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1866 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1867 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT, |
| 1868 | // TODO: This is problematic casting. Need to fix CameraMetadata. |
| 1869 | reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1870 | return false; |
| 1871 | } |
| 1872 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1873 | int32_t requestId = resultExtras.requestId; |
| 1874 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID, |
Eino-Ville Talvala | 184dfe4 | 2013-11-07 15:13:16 -0800 | [diff] [blame] | 1875 | &requestId, frameNumber)) { |
| 1876 | return false; |
| 1877 | } |
| 1878 | |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1879 | if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) { |
| 1880 | static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL; |
| 1881 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT, |
| 1882 | &partialResult, frameNumber)) { |
| 1883 | return false; |
| 1884 | } |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1885 | } |
| 1886 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1887 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1888 | &afMode, frameNumber)) { |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1892 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1893 | &awbMode, frameNumber)) { |
| 1894 | return false; |
| 1895 | } |
| 1896 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1897 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1898 | &aeState, frameNumber)) { |
| 1899 | return false; |
| 1900 | } |
| 1901 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1902 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1903 | &afState, frameNumber)) { |
| 1904 | return false; |
| 1905 | } |
| 1906 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1907 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE, |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1908 | &awbState, frameNumber)) { |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1912 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID, |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1913 | &resultExtras.afTriggerId, frameNumber)) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1914 | return false; |
| 1915 | } |
| 1916 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1917 | if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID, |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 1918 | &resultExtras.precaptureTriggerId, frameNumber)) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1919 | return false; |
| 1920 | } |
| 1921 | |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 1922 | // We only send the aggregated partial when all 3A related metadata are available |
| 1923 | // For both API1 and API2. |
| 1924 | // TODO: we probably should pass through all partials to API2 unconditionally. |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1925 | mResultSignal.signal(); |
| 1926 | |
| 1927 | return true; |
| 1928 | } |
| 1929 | |
| 1930 | template<typename T> |
| 1931 | bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1932 | T* value, uint32_t frameNumber) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1933 | (void) frameNumber; |
| 1934 | |
| 1935 | camera_metadata_ro_entry_t entry; |
| 1936 | |
| 1937 | entry = result.find(tag); |
| 1938 | if (entry.count == 0) { |
| 1939 | ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__, |
| 1940 | mId, frameNumber, get_camera_metadata_tag_name(tag)); |
| 1941 | return false; |
| 1942 | } |
| 1943 | |
| 1944 | if (sizeof(T) == sizeof(uint8_t)) { |
| 1945 | *value = entry.data.u8[0]; |
| 1946 | } else if (sizeof(T) == sizeof(int32_t)) { |
| 1947 | *value = entry.data.i32[0]; |
| 1948 | } else { |
| 1949 | ALOGE("%s: Unexpected type", __FUNCTION__); |
| 1950 | return false; |
| 1951 | } |
| 1952 | return true; |
| 1953 | } |
| 1954 | |
| 1955 | template<typename T> |
| 1956 | bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1957 | const T* value, uint32_t frameNumber) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 1958 | if (result.update(tag, value, 1) != NO_ERROR) { |
| 1959 | mResultQueue.erase(--mResultQueue.end(), mResultQueue.end()); |
| 1960 | SET_ERR("Frame %d: Failed to set %s in partial metadata", |
| 1961 | frameNumber, get_camera_metadata_tag_name(tag)); |
| 1962 | return false; |
| 1963 | } |
| 1964 | return true; |
| 1965 | } |
| 1966 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 1967 | void Camera3Device::returnOutputBuffers( |
| 1968 | const camera3_stream_buffer_t *outputBuffers, size_t numBuffers, |
| 1969 | nsecs_t timestamp) { |
| 1970 | for (size_t i = 0; i < numBuffers; i++) |
| 1971 | { |
| 1972 | Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream); |
| 1973 | status_t res = stream->returnBuffer(outputBuffers[i], timestamp); |
| 1974 | // Note: stream may be deallocated at this point, if this buffer was |
| 1975 | // the last reference to it. |
| 1976 | if (res != OK) { |
| 1977 | ALOGE("Can't return buffer to its stream: %s (%d)", |
| 1978 | strerror(-res), res); |
| 1979 | } |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | |
| 1984 | void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) { |
| 1985 | |
| 1986 | const InFlightRequest &request = mInFlightMap.valueAt(idx); |
| 1987 | const uint32_t frameNumber = mInFlightMap.keyAt(idx); |
| 1988 | |
| 1989 | nsecs_t sensorTimestamp = request.sensorTimestamp; |
| 1990 | nsecs_t shutterTimestamp = request.shutterTimestamp; |
| 1991 | |
| 1992 | // Check if it's okay to remove the request from InFlightMap: |
| 1993 | // In the case of a successful request: |
| 1994 | // all input and output buffers, all result metadata, shutter callback |
| 1995 | // arrived. |
| 1996 | // In the case of a unsuccessful request: |
| 1997 | // all input and output buffers arrived. |
| 1998 | if (request.numBuffersLeft == 0 && |
| 1999 | (request.requestStatus != OK || |
| 2000 | (request.haveResultMetadata && shutterTimestamp != 0))) { |
| 2001 | ATRACE_ASYNC_END("frame capture", frameNumber); |
| 2002 | |
| 2003 | // Sanity check - if sensor timestamp matches shutter timestamp |
| 2004 | if (request.requestStatus == OK && |
| 2005 | sensorTimestamp != shutterTimestamp) { |
| 2006 | SET_ERR("sensor timestamp (%" PRId64 |
| 2007 | ") for frame %d doesn't match shutter timestamp (%" PRId64 ")", |
| 2008 | sensorTimestamp, frameNumber, shutterTimestamp); |
| 2009 | } |
| 2010 | |
| 2011 | // for an unsuccessful request, it may have pending output buffers to |
| 2012 | // return. |
| 2013 | assert(request.requestStatus != OK || |
| 2014 | request.pendingOutputBuffers.size() == 0); |
| 2015 | returnOutputBuffers(request.pendingOutputBuffers.array(), |
| 2016 | request.pendingOutputBuffers.size(), 0); |
| 2017 | |
| 2018 | mInFlightMap.removeItemsAt(idx, 1); |
| 2019 | |
| 2020 | ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber); |
| 2021 | } |
| 2022 | |
| 2023 | // Sanity check - if we have too many in-flight frames, something has |
| 2024 | // likely gone wrong |
| 2025 | if (mInFlightMap.size() > kInFlightWarnLimit) { |
| 2026 | CLOGE("In-flight list too large: %zu", mInFlightMap.size()); |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata, |
| 2032 | CaptureResultExtras &resultExtras, |
| 2033 | CameraMetadata &collectedPartialResult, |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2034 | uint32_t frameNumber, |
| 2035 | bool reprocess) { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2036 | if (pendingMetadata.isEmpty()) |
| 2037 | return; |
| 2038 | |
| 2039 | Mutex::Autolock l(mOutputLock); |
| 2040 | |
| 2041 | // 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] | 2042 | if (reprocess) { |
| 2043 | if (frameNumber < mNextReprocessResultFrameNumber) { |
| 2044 | SET_ERR("Out-of-order reprocess capture result metadata submitted! " |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2045 | "(got frame number %d, expecting %d)", |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2046 | frameNumber, mNextReprocessResultFrameNumber); |
| 2047 | return; |
| 2048 | } |
| 2049 | mNextReprocessResultFrameNumber = frameNumber + 1; |
| 2050 | } else { |
| 2051 | if (frameNumber < mNextResultFrameNumber) { |
| 2052 | SET_ERR("Out-of-order capture result metadata submitted! " |
| 2053 | "(got frame number %d, expecting %d)", |
| 2054 | frameNumber, mNextResultFrameNumber); |
| 2055 | return; |
| 2056 | } |
| 2057 | mNextResultFrameNumber = frameNumber + 1; |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2058 | } |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2059 | |
| 2060 | CaptureResult captureResult; |
| 2061 | captureResult.mResultExtras = resultExtras; |
| 2062 | captureResult.mMetadata = pendingMetadata; |
| 2063 | |
| 2064 | if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT, |
| 2065 | (int32_t*)&frameNumber, 1) != OK) { |
| 2066 | SET_ERR("Failed to set frame# in metadata (%d)", |
| 2067 | frameNumber); |
| 2068 | return; |
| 2069 | } else { |
| 2070 | ALOGVV("%s: Camera %d: Set frame# in metadata (%d)", |
| 2071 | __FUNCTION__, mId, frameNumber); |
| 2072 | } |
| 2073 | |
| 2074 | // Append any previous partials to form a complete result |
| 2075 | if (mUsePartialResult && !collectedPartialResult.isEmpty()) { |
| 2076 | captureResult.mMetadata.append(collectedPartialResult); |
| 2077 | } |
| 2078 | |
| 2079 | captureResult.mMetadata.sort(); |
| 2080 | |
| 2081 | // Check that there's a timestamp in the result metadata |
| 2082 | camera_metadata_entry entry = |
| 2083 | captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP); |
| 2084 | if (entry.count == 0) { |
| 2085 | SET_ERR("No timestamp provided by HAL for frame %d!", |
| 2086 | frameNumber); |
| 2087 | return; |
| 2088 | } |
| 2089 | |
| 2090 | // Valid result, insert into queue |
| 2091 | List<CaptureResult>::iterator queuedResult = |
| 2092 | mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult)); |
| 2093 | ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64 |
| 2094 | ", burstId = %" PRId32, __FUNCTION__, |
| 2095 | queuedResult->mResultExtras.requestId, |
| 2096 | queuedResult->mResultExtras.frameNumber, |
| 2097 | queuedResult->mResultExtras.burstId); |
| 2098 | |
| 2099 | mResultSignal.signal(); |
| 2100 | } |
| 2101 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2102 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2103 | * Camera HAL device callback methods |
| 2104 | */ |
| 2105 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2106 | void Camera3Device::processCaptureResult(const camera3_capture_result *result) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2107 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2108 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2109 | status_t res; |
| 2110 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2111 | uint32_t frameNumber = result->frame_number; |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2112 | if (result->result == NULL && result->num_output_buffers == 0 && |
| 2113 | result->input_buffer == NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2114 | SET_ERR("No result data provided by HAL for frame %d", |
| 2115 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2116 | return; |
| 2117 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2118 | |
| 2119 | // For HAL3.2 or above, If HAL doesn't support partial, it must always set |
| 2120 | // partial_result to 1 when metadata is included in this result. |
| 2121 | if (!mUsePartialResult && |
| 2122 | mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 && |
| 2123 | result->result != NULL && |
| 2124 | result->partial_result != 1) { |
| 2125 | SET_ERR("Result is malformed for frame %d: partial_result %u must be 1" |
| 2126 | " if partial result is not supported", |
| 2127 | frameNumber, result->partial_result); |
| 2128 | return; |
| 2129 | } |
| 2130 | |
| 2131 | bool isPartialResult = false; |
| 2132 | CameraMetadata collectedPartialResult; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2133 | CaptureResultExtras resultExtras; |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2134 | bool hasInputBufferInRequest = false; |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2135 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2136 | // Get shutter timestamp and resultExtras from list of in-flight requests, |
| 2137 | // where it was added by the shutter notification for this frame. If the |
| 2138 | // shutter timestamp isn't received yet, append the output buffers to the |
| 2139 | // in-flight request and they will be returned when the shutter timestamp |
| 2140 | // arrives. Update the in-flight status and remove the in-flight entry if |
| 2141 | // all result data and shutter timestamp have been received. |
| 2142 | nsecs_t shutterTimestamp = 0; |
| 2143 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2144 | { |
| 2145 | Mutex::Autolock l(mInFlightLock); |
| 2146 | ssize_t idx = mInFlightMap.indexOfKey(frameNumber); |
| 2147 | if (idx == NAME_NOT_FOUND) { |
| 2148 | SET_ERR("Unknown frame number for capture result: %d", |
| 2149 | frameNumber); |
| 2150 | return; |
| 2151 | } |
| 2152 | InFlightRequest &request = mInFlightMap.editValueAt(idx); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2153 | ALOGVV("%s: got InFlightRequest requestId = %" PRId32 |
| 2154 | ", frameNumber = %" PRId64 ", burstId = %" PRId32 |
| 2155 | ", partialResultCount = %d", |
| 2156 | __FUNCTION__, request.resultExtras.requestId, |
| 2157 | request.resultExtras.frameNumber, request.resultExtras.burstId, |
| 2158 | result->partial_result); |
| 2159 | // Always update the partial count to the latest one if it's not 0 |
| 2160 | // (buffers only). When framework aggregates adjacent partial results |
| 2161 | // into one, the latest partial count will be used. |
| 2162 | if (result->partial_result != 0) |
| 2163 | request.resultExtras.partialResultCount = result->partial_result; |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2164 | |
| 2165 | // Check if this result carries only partial metadata |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2166 | if (mUsePartialResult && result->result != NULL) { |
| 2167 | if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 2168 | if (result->partial_result > mNumPartialResults || result->partial_result < 1) { |
| 2169 | SET_ERR("Result is malformed for frame %d: partial_result %u must be in" |
| 2170 | " the range of [1, %d] when metadata is included in the result", |
| 2171 | frameNumber, result->partial_result, mNumPartialResults); |
| 2172 | return; |
| 2173 | } |
| 2174 | isPartialResult = (result->partial_result < mNumPartialResults); |
Zhijun He | 5d76e1a | 2014-07-22 16:08:13 -0700 | [diff] [blame] | 2175 | if (isPartialResult) { |
| 2176 | request.partialResult.collectedResult.append(result->result); |
| 2177 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2178 | } else { |
| 2179 | camera_metadata_ro_entry_t partialResultEntry; |
| 2180 | res = find_camera_metadata_ro_entry(result->result, |
| 2181 | ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry); |
| 2182 | if (res != NAME_NOT_FOUND && |
| 2183 | partialResultEntry.count > 0 && |
| 2184 | partialResultEntry.data.u8[0] == |
| 2185 | ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) { |
| 2186 | // A partial result. Flag this as such, and collect this |
| 2187 | // set of metadata into the in-flight entry. |
| 2188 | isPartialResult = true; |
| 2189 | request.partialResult.collectedResult.append( |
| 2190 | result->result); |
| 2191 | request.partialResult.collectedResult.erase( |
| 2192 | ANDROID_QUIRKS_PARTIAL_RESULT); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | if (isPartialResult) { |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2197 | // Fire off a 3A-only result if possible |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2198 | if (!request.partialResult.haveSent3A) { |
| 2199 | request.partialResult.haveSent3A = |
| 2200 | processPartial3AResult(frameNumber, |
| 2201 | request.partialResult.collectedResult, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2202 | request.resultExtras); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2207 | shutterTimestamp = request.shutterTimestamp; |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2208 | hasInputBufferInRequest = request.hasInputBuffer; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2209 | |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2210 | // Did we get the (final) result metadata for this capture? |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2211 | if (result->result != NULL && !isPartialResult) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2212 | if (request.haveResultMetadata) { |
| 2213 | SET_ERR("Called multiple times with metadata for frame %d", |
| 2214 | frameNumber); |
| 2215 | return; |
| 2216 | } |
Zhijun He | 204e329 | 2014-07-14 17:09:23 -0700 | [diff] [blame] | 2217 | if (mUsePartialResult && |
| 2218 | !request.partialResult.collectedResult.isEmpty()) { |
| 2219 | collectedPartialResult.acquire( |
| 2220 | request.partialResult.collectedResult); |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2221 | } |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2222 | request.haveResultMetadata = true; |
| 2223 | } |
| 2224 | |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2225 | uint32_t numBuffersReturned = result->num_output_buffers; |
| 2226 | if (result->input_buffer != NULL) { |
| 2227 | if (hasInputBufferInRequest) { |
| 2228 | numBuffersReturned += 1; |
| 2229 | } else { |
| 2230 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 2231 | " buffer sent in the request", |
| 2232 | __FUNCTION__); |
| 2233 | } |
| 2234 | } |
| 2235 | request.numBuffersLeft -= numBuffersReturned; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2236 | if (request.numBuffersLeft < 0) { |
| 2237 | SET_ERR("Too many buffers returned for frame %d", |
| 2238 | frameNumber); |
| 2239 | return; |
| 2240 | } |
| 2241 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2242 | camera_metadata_ro_entry_t entry; |
| 2243 | res = find_camera_metadata_ro_entry(result->result, |
| 2244 | ANDROID_SENSOR_TIMESTAMP, &entry); |
| 2245 | if (res == OK && entry.count == 1) { |
| 2246 | request.sensorTimestamp = entry.data.i64[0]; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2247 | } |
| 2248 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2249 | // If shutter event isn't received yet, append the output buffers to |
| 2250 | // the in-flight request. Otherwise, return the output buffers to |
| 2251 | // streams. |
| 2252 | if (shutterTimestamp == 0) { |
| 2253 | request.pendingOutputBuffers.appendArray(result->output_buffers, |
| 2254 | result->num_output_buffers); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2255 | } else { |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2256 | returnOutputBuffers(result->output_buffers, |
| 2257 | result->num_output_buffers, shutterTimestamp); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 2258 | } |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2259 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2260 | if (result->result != NULL && !isPartialResult) { |
| 2261 | if (shutterTimestamp == 0) { |
| 2262 | request.pendingMetadata = result->result; |
| 2263 | request.partialResult.collectedResult = collectedPartialResult; |
| 2264 | } else { |
| 2265 | CameraMetadata metadata; |
| 2266 | metadata = result->result; |
| 2267 | sendCaptureResult(metadata, request.resultExtras, |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2268 | collectedPartialResult, frameNumber, hasInputBufferInRequest); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2269 | } |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2272 | removeInFlightRequestIfReadyLocked(idx); |
| 2273 | } // scope for mInFlightLock |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2274 | |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2275 | if (result->input_buffer != NULL) { |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2276 | if (hasInputBufferInRequest) { |
| 2277 | Camera3Stream *stream = |
| 2278 | Camera3Stream::cast(result->input_buffer->stream); |
| 2279 | res = stream->returnInputBuffer(*(result->input_buffer)); |
| 2280 | // Note: stream may be deallocated at this point, if this buffer was the |
| 2281 | // last reference to it. |
| 2282 | if (res != OK) { |
| 2283 | ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" |
| 2284 | " its stream:%s (%d)", __FUNCTION__, |
| 2285 | frameNumber, strerror(-res), res); |
Zhijun He | 0ea8fa4 | 2014-07-07 17:05:38 -0700 | [diff] [blame] | 2286 | } |
| 2287 | } else { |
| 2288 | ALOGW("%s: Input buffer should be NULL if there is no input" |
| 2289 | " buffer sent in the request, skipping input buffer return.", |
| 2290 | __FUNCTION__); |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2291 | } |
| 2292 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | void Camera3Device::notify(const camera3_notify_msg *msg) { |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2296 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2297 | NotificationListener *listener; |
| 2298 | { |
| 2299 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2300 | listener = mListener; |
| 2301 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2302 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2303 | if (msg == NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2304 | SET_ERR("HAL sent NULL notify message!"); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2305 | return; |
| 2306 | } |
| 2307 | |
| 2308 | switch (msg->type) { |
| 2309 | case CAMERA3_MSG_ERROR: { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2310 | notifyError(msg->message.error, listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2311 | break; |
| 2312 | } |
| 2313 | case CAMERA3_MSG_SHUTTER: { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2314 | notifyShutter(msg->message.shutter, listener); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2315 | break; |
| 2316 | } |
| 2317 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2318 | SET_ERR("Unknown notify message from HAL: %d", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2319 | msg->type); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 2320 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2321 | } |
| 2322 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2323 | void Camera3Device::notifyError(const camera3_error_msg_t &msg, |
| 2324 | NotificationListener *listener) { |
| 2325 | |
| 2326 | // Map camera HAL error codes to ICameraDeviceCallback error codes |
| 2327 | // Index into this with the HAL error code |
| 2328 | static const ICameraDeviceCallbacks::CameraErrorCode |
| 2329 | halErrorMap[CAMERA3_MSG_NUM_ERRORS] = { |
| 2330 | // 0 = Unused error code |
| 2331 | ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR, |
| 2332 | // 1 = CAMERA3_MSG_ERROR_DEVICE |
| 2333 | ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
| 2334 | // 2 = CAMERA3_MSG_ERROR_REQUEST |
| 2335 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 2336 | // 3 = CAMERA3_MSG_ERROR_RESULT |
| 2337 | ICameraDeviceCallbacks::ERROR_CAMERA_RESULT, |
| 2338 | // 4 = CAMERA3_MSG_ERROR_BUFFER |
| 2339 | ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER |
| 2340 | }; |
| 2341 | |
| 2342 | ICameraDeviceCallbacks::CameraErrorCode errorCode = |
| 2343 | ((msg.error_code >= 0) && |
| 2344 | (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ? |
| 2345 | halErrorMap[msg.error_code] : |
| 2346 | ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR; |
| 2347 | |
| 2348 | int streamId = 0; |
| 2349 | if (msg.error_stream != NULL) { |
| 2350 | Camera3Stream *stream = |
| 2351 | Camera3Stream::cast(msg.error_stream); |
| 2352 | streamId = stream->getId(); |
| 2353 | } |
| 2354 | ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d", |
| 2355 | mId, __FUNCTION__, msg.frame_number, |
| 2356 | streamId, msg.error_code); |
| 2357 | |
| 2358 | CaptureResultExtras resultExtras; |
| 2359 | switch (errorCode) { |
| 2360 | case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE: |
| 2361 | // SET_ERR calls notifyError |
| 2362 | SET_ERR("Camera HAL reported serious device error"); |
| 2363 | break; |
| 2364 | case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: |
| 2365 | case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: |
| 2366 | case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: |
| 2367 | { |
| 2368 | Mutex::Autolock l(mInFlightLock); |
| 2369 | ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number); |
| 2370 | if (idx >= 0) { |
| 2371 | InFlightRequest &r = mInFlightMap.editValueAt(idx); |
| 2372 | r.requestStatus = msg.error_code; |
| 2373 | resultExtras = r.resultExtras; |
| 2374 | } else { |
| 2375 | resultExtras.frameNumber = msg.frame_number; |
| 2376 | ALOGE("Camera %d: %s: cannot find in-flight request on " |
| 2377 | "frame %" PRId64 " error", mId, __FUNCTION__, |
| 2378 | resultExtras.frameNumber); |
| 2379 | } |
| 2380 | } |
| 2381 | if (listener != NULL) { |
| 2382 | listener->notifyError(errorCode, resultExtras); |
| 2383 | } else { |
| 2384 | ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__); |
| 2385 | } |
| 2386 | break; |
| 2387 | default: |
| 2388 | // SET_ERR calls notifyError |
| 2389 | SET_ERR("Unknown error message from HAL: %d", msg.error_code); |
| 2390 | break; |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg, |
| 2395 | NotificationListener *listener) { |
| 2396 | ssize_t idx; |
| 2397 | // Verify ordering of shutter notifications |
| 2398 | { |
| 2399 | Mutex::Autolock l(mOutputLock); |
| 2400 | // TODO: need to track errors for tighter bounds on expected frame number. |
| 2401 | if (msg.frame_number < mNextShutterFrameNumber) { |
| 2402 | SET_ERR("Shutter notification out-of-order. Expected " |
| 2403 | "notification for frame %d, got frame %d", |
| 2404 | mNextShutterFrameNumber, msg.frame_number); |
| 2405 | return; |
| 2406 | } |
| 2407 | mNextShutterFrameNumber = msg.frame_number + 1; |
| 2408 | } |
| 2409 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2410 | // Set timestamp for the request in the in-flight tracking |
| 2411 | // and get the request ID to send upstream |
| 2412 | { |
| 2413 | Mutex::Autolock l(mInFlightLock); |
| 2414 | idx = mInFlightMap.indexOfKey(msg.frame_number); |
| 2415 | if (idx >= 0) { |
| 2416 | InFlightRequest &r = mInFlightMap.editValueAt(idx); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2417 | |
| 2418 | ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64, |
| 2419 | mId, __FUNCTION__, |
| 2420 | msg.frame_number, r.resultExtras.requestId, msg.timestamp); |
| 2421 | // Call listener, if any |
| 2422 | if (listener != NULL) { |
| 2423 | listener->notifyShutter(r.resultExtras, msg.timestamp); |
| 2424 | } |
| 2425 | |
| 2426 | r.shutterTimestamp = msg.timestamp; |
| 2427 | |
| 2428 | // send pending result and buffers |
| 2429 | sendCaptureResult(r.pendingMetadata, r.resultExtras, |
Chien-Yu Chen | 618ff8a | 2015-03-13 11:27:17 -0700 | [diff] [blame] | 2430 | r.partialResult.collectedResult, msg.frame_number, |
| 2431 | r.hasInputBuffer); |
Chien-Yu Chen | 43e69a6 | 2014-11-25 16:38:33 -0800 | [diff] [blame] | 2432 | returnOutputBuffers(r.pendingOutputBuffers.array(), |
| 2433 | r.pendingOutputBuffers.size(), r.shutterTimestamp); |
| 2434 | r.pendingOutputBuffers.clear(); |
| 2435 | |
| 2436 | removeInFlightRequestIfReadyLocked(idx); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | if (idx < 0) { |
| 2440 | SET_ERR("Shutter notification for non-existent frame number %d", |
| 2441 | msg.frame_number); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2442 | } |
| 2443 | } |
| 2444 | |
| 2445 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2446 | CameraMetadata Camera3Device::getLatestRequestLocked() { |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2447 | ALOGV("%s", __FUNCTION__); |
| 2448 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2449 | CameraMetadata retVal; |
| 2450 | |
| 2451 | if (mRequestThread != NULL) { |
| 2452 | retVal = mRequestThread->getLatestRequest(); |
| 2453 | } |
| 2454 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2455 | return retVal; |
| 2456 | } |
| 2457 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2458 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2459 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2460 | * RequestThread inner class methods |
| 2461 | */ |
| 2462 | |
| 2463 | Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2464 | sp<StatusTracker> statusTracker, |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2465 | camera3_device_t *hal3Device) : |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2466 | Thread(/*canCallJava*/false), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2467 | mParent(parent), |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2468 | mStatusTracker(statusTracker), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2469 | mHal3Device(hal3Device), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2470 | mId(getId(parent)), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2471 | mReconfigured(false), |
| 2472 | mDoPause(false), |
| 2473 | mPaused(true), |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2474 | mFrameNumber(0), |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2475 | mLatestRequestId(NAME_NOT_FOUND), |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 2476 | mCurrentAfTriggerId(0), |
| 2477 | mCurrentPreCaptureTriggerId(0), |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2478 | mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2479 | mStatusId = statusTracker->addComponent(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2480 | } |
| 2481 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2482 | void Camera3Device::RequestThread::setNotificationListener( |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2483 | NotificationListener *listener) { |
| 2484 | Mutex::Autolock l(mRequestLock); |
| 2485 | mListener = listener; |
| 2486 | } |
| 2487 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2488 | void Camera3Device::RequestThread::configurationComplete() { |
| 2489 | Mutex::Autolock l(mRequestLock); |
| 2490 | mReconfigured = true; |
| 2491 | } |
| 2492 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2493 | status_t Camera3Device::RequestThread::queueRequestList( |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2494 | List<sp<CaptureRequest> > &requests, |
| 2495 | /*out*/ |
| 2496 | int64_t *lastFrameNumber) { |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2497 | Mutex::Autolock l(mRequestLock); |
| 2498 | for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); |
| 2499 | ++it) { |
| 2500 | mRequestQueue.push_back(*it); |
| 2501 | } |
| 2502 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2503 | if (lastFrameNumber != NULL) { |
| 2504 | *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1; |
| 2505 | ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".", |
| 2506 | __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber, |
| 2507 | *lastFrameNumber); |
| 2508 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2509 | |
Jianing Wei | 90e59c9 | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2510 | unpauseForNewRequests(); |
| 2511 | |
| 2512 | return OK; |
| 2513 | } |
| 2514 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2515 | |
| 2516 | status_t Camera3Device::RequestThread::queueTrigger( |
| 2517 | RequestTrigger trigger[], |
| 2518 | size_t count) { |
| 2519 | |
| 2520 | Mutex::Autolock l(mTriggerMutex); |
| 2521 | status_t ret; |
| 2522 | |
| 2523 | for (size_t i = 0; i < count; ++i) { |
| 2524 | ret = queueTriggerLocked(trigger[i]); |
| 2525 | |
| 2526 | if (ret != OK) { |
| 2527 | return ret; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | return OK; |
| 2532 | } |
| 2533 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2534 | int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { |
| 2535 | sp<Camera3Device> d = device.promote(); |
| 2536 | if (d != NULL) return d->mId; |
| 2537 | return 0; |
| 2538 | } |
| 2539 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2540 | status_t Camera3Device::RequestThread::queueTriggerLocked( |
| 2541 | RequestTrigger trigger) { |
| 2542 | |
| 2543 | uint32_t tag = trigger.metadataTag; |
| 2544 | ssize_t index = mTriggerMap.indexOfKey(tag); |
| 2545 | |
| 2546 | switch (trigger.getTagType()) { |
| 2547 | case TYPE_BYTE: |
| 2548 | // fall-through |
| 2549 | case TYPE_INT32: |
| 2550 | break; |
| 2551 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2552 | ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, |
| 2553 | trigger.getTagType()); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2554 | return INVALID_OPERATION; |
| 2555 | } |
| 2556 | |
| 2557 | /** |
| 2558 | * Collect only the latest trigger, since we only have 1 field |
| 2559 | * in the request settings per trigger tag, and can't send more than 1 |
| 2560 | * trigger per request. |
| 2561 | */ |
| 2562 | if (index != NAME_NOT_FOUND) { |
| 2563 | mTriggerMap.editValueAt(index) = trigger; |
| 2564 | } else { |
| 2565 | mTriggerMap.add(tag, trigger); |
| 2566 | } |
| 2567 | |
| 2568 | return OK; |
| 2569 | } |
| 2570 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2571 | status_t Camera3Device::RequestThread::setRepeatingRequests( |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2572 | const RequestList &requests, |
| 2573 | /*out*/ |
| 2574 | int64_t *lastFrameNumber) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2575 | Mutex::Autolock l(mRequestLock); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2576 | if (lastFrameNumber != NULL) { |
| 2577 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 2578 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2579 | mRepeatingRequests.clear(); |
| 2580 | mRepeatingRequests.insert(mRepeatingRequests.begin(), |
| 2581 | requests.begin(), requests.end()); |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 2582 | |
| 2583 | unpauseForNewRequests(); |
| 2584 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2585 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2586 | return OK; |
| 2587 | } |
| 2588 | |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2589 | bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) { |
| 2590 | if (mRepeatingRequests.empty()) { |
| 2591 | return false; |
| 2592 | } |
| 2593 | int32_t requestId = requestIn->mResultExtras.requestId; |
| 2594 | const RequestList &repeatRequests = mRepeatingRequests; |
| 2595 | // All repeating requests are guaranteed to have same id so only check first quest |
| 2596 | const sp<CaptureRequest> firstRequest = *repeatRequests.begin(); |
| 2597 | return (firstRequest->mResultExtras.requestId == requestId); |
| 2598 | } |
| 2599 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2600 | status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2601 | Mutex::Autolock l(mRequestLock); |
| 2602 | mRepeatingRequests.clear(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2603 | if (lastFrameNumber != NULL) { |
| 2604 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 2605 | } |
| 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 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2610 | status_t Camera3Device::RequestThread::clear( |
| 2611 | NotificationListener *listener, |
| 2612 | /*out*/int64_t *lastFrameNumber) { |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2613 | Mutex::Autolock l(mRequestLock); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2614 | ALOGV("RequestThread::%s:", __FUNCTION__); |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2615 | |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2616 | mRepeatingRequests.clear(); |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2617 | |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2618 | // Send errors for all requests pending in the request queue, including |
| 2619 | // pending repeating requests |
| 2620 | if (listener != NULL) { |
| 2621 | for (RequestList::iterator it = mRequestQueue.begin(); |
| 2622 | it != mRequestQueue.end(); ++it) { |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2623 | // Abort the input buffers for reprocess requests. |
| 2624 | if ((*it)->mInputStream != NULL) { |
| 2625 | camera3_stream_buffer_t inputBuffer; |
| 2626 | status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer); |
| 2627 | if (res != OK) { |
| 2628 | ALOGW("%s: %d: couldn't get input buffer while clearing the request " |
| 2629 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); |
| 2630 | } else { |
| 2631 | res = (*it)->mInputStream->returnInputBuffer(inputBuffer); |
| 2632 | if (res != OK) { |
| 2633 | ALOGE("%s: %d: couldn't return input buffer while clearing the request " |
| 2634 | "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res); |
| 2635 | } |
| 2636 | } |
| 2637 | } |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2638 | // Set the frame number this request would have had, if it |
| 2639 | // had been submitted; this frame number will not be reused. |
| 2640 | // The requestId and burstId fields were set when the request was |
| 2641 | // submitted originally (in convertMetadataListToRequestListLocked) |
| 2642 | (*it)->mResultExtras.frameNumber = mFrameNumber++; |
| 2643 | listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 2644 | (*it)->mResultExtras); |
Yin-Chia Yeh | 8684b7f | 2014-06-13 14:53:05 -0700 | [diff] [blame] | 2645 | } |
| 2646 | } |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2647 | mRequestQueue.clear(); |
| 2648 | mTriggerMap.clear(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2649 | if (lastFrameNumber != NULL) { |
| 2650 | *lastFrameNumber = mRepeatingLastFrameNumber; |
| 2651 | } |
| 2652 | mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES; |
Eino-Ville Talvala | abaa51d | 2013-08-14 11:37:00 -0700 | [diff] [blame] | 2653 | return OK; |
| 2654 | } |
| 2655 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2656 | void Camera3Device::RequestThread::setPaused(bool paused) { |
| 2657 | Mutex::Autolock l(mPauseLock); |
| 2658 | mDoPause = paused; |
| 2659 | mDoPauseSignal.signal(); |
| 2660 | } |
| 2661 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2662 | status_t Camera3Device::RequestThread::waitUntilRequestProcessed( |
| 2663 | int32_t requestId, nsecs_t timeout) { |
| 2664 | Mutex::Autolock l(mLatestRequestMutex); |
| 2665 | status_t res; |
| 2666 | while (mLatestRequestId != requestId) { |
| 2667 | nsecs_t startTime = systemTime(); |
| 2668 | |
| 2669 | res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); |
| 2670 | if (res != OK) return res; |
| 2671 | |
| 2672 | timeout -= (systemTime() - startTime); |
| 2673 | } |
| 2674 | |
| 2675 | return OK; |
| 2676 | } |
| 2677 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2678 | void Camera3Device::RequestThread::requestExit() { |
| 2679 | // Call parent to set up shutdown |
| 2680 | Thread::requestExit(); |
| 2681 | // The exit from any possible waits |
| 2682 | mDoPauseSignal.signal(); |
| 2683 | mRequestSignal.signal(); |
| 2684 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2685 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2686 | bool Camera3Device::RequestThread::threadLoop() { |
| 2687 | |
| 2688 | status_t res; |
| 2689 | |
| 2690 | // Handle paused state. |
| 2691 | if (waitIfPaused()) { |
| 2692 | return true; |
| 2693 | } |
| 2694 | |
| 2695 | // Get work to do |
| 2696 | |
| 2697 | sp<CaptureRequest> nextRequest = waitForNextRequest(); |
| 2698 | if (nextRequest == NULL) { |
| 2699 | return true; |
| 2700 | } |
| 2701 | |
| 2702 | // Create request to HAL |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2703 | camera3_capture_request_t request = camera3_capture_request_t(); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2704 | request.frame_number = nextRequest->mResultExtras.frameNumber; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2705 | Vector<camera3_stream_buffer_t> outputBuffers; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2706 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2707 | // Get the request ID, if any |
| 2708 | int requestId; |
| 2709 | camera_metadata_entry_t requestIdEntry = |
| 2710 | nextRequest->mSettings.find(ANDROID_REQUEST_ID); |
| 2711 | if (requestIdEntry.count > 0) { |
| 2712 | requestId = requestIdEntry.data.i32[0]; |
| 2713 | } else { |
| 2714 | ALOGW("%s: Did not have android.request.id set in the request", |
| 2715 | __FUNCTION__); |
| 2716 | requestId = NAME_NOT_FOUND; |
| 2717 | } |
| 2718 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2719 | // Insert any queued triggers (before metadata is locked) |
| 2720 | int32_t triggerCount; |
| 2721 | res = insertTriggers(nextRequest); |
| 2722 | if (res < 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2723 | SET_ERR("RequestThread: Unable to insert triggers " |
| 2724 | "(capture request %d, HAL device: %s (%d)", |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2725 | request.frame_number, strerror(-res), res); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2726 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2727 | return false; |
| 2728 | } |
| 2729 | triggerCount = res; |
| 2730 | |
| 2731 | bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); |
| 2732 | |
| 2733 | // If the request is the same as last, or we had triggers last time |
| 2734 | if (mPrevRequest != nextRequest || triggersMixedIn) { |
| 2735 | /** |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 2736 | * HAL workaround: |
| 2737 | * Insert a dummy trigger ID if a trigger is set but no trigger ID is |
| 2738 | */ |
| 2739 | res = addDummyTriggerIds(nextRequest); |
| 2740 | if (res != OK) { |
| 2741 | SET_ERR("RequestThread: Unable to insert dummy trigger IDs " |
| 2742 | "(capture request %d, HAL device: %s (%d)", |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2743 | request.frame_number, strerror(-res), res); |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 2744 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2745 | return false; |
| 2746 | } |
| 2747 | |
| 2748 | /** |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2749 | * The request should be presorted so accesses in HAL |
| 2750 | * are O(logn). Sidenote, sorting a sorted metadata is nop. |
| 2751 | */ |
| 2752 | nextRequest->mSettings.sort(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2753 | request.settings = nextRequest->mSettings.getAndLock(); |
| 2754 | mPrevRequest = nextRequest; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2755 | ALOGVV("%s: Request settings are NEW", __FUNCTION__); |
| 2756 | |
| 2757 | IF_ALOGV() { |
| 2758 | camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); |
| 2759 | find_camera_metadata_ro_entry( |
| 2760 | request.settings, |
| 2761 | ANDROID_CONTROL_AF_TRIGGER, |
| 2762 | &e |
| 2763 | ); |
| 2764 | if (e.count > 0) { |
| 2765 | ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", |
| 2766 | __FUNCTION__, |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2767 | request.frame_number, |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2768 | e.data.u8[0]); |
| 2769 | } |
| 2770 | } |
| 2771 | } else { |
| 2772 | // leave request.settings NULL to indicate 'reuse latest given' |
| 2773 | ALOGVV("%s: Request settings are REUSED", |
| 2774 | __FUNCTION__); |
| 2775 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2776 | |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2777 | uint32_t totalNumBuffers = 0; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2778 | |
| 2779 | // Fill in buffers |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2780 | if (nextRequest->mInputStream != NULL) { |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2781 | request.input_buffer = &nextRequest->mInputBuffer; |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2782 | totalNumBuffers += 1; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2783 | } else { |
| 2784 | request.input_buffer = NULL; |
| 2785 | } |
| 2786 | |
| 2787 | outputBuffers.insertAt(camera3_stream_buffer_t(), 0, |
| 2788 | nextRequest->mOutputStreams.size()); |
| 2789 | request.output_buffers = outputBuffers.array(); |
| 2790 | for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) { |
| 2791 | res = nextRequest->mOutputStreams.editItemAt(i)-> |
| 2792 | getBuffer(&outputBuffers.editItemAt(i)); |
| 2793 | if (res != OK) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2794 | // Can't get output buffer from gralloc queue - this could be due to |
| 2795 | // abandoned queue or other consumer misbehavior, so not a fatal |
| 2796 | // error |
Eino-Ville Talvala | 07d2169 | 2013-09-24 18:04:19 -0700 | [diff] [blame] | 2797 | ALOGE("RequestThread: Can't get output buffer, skipping request:" |
| 2798 | " %s (%d)", strerror(-res), res); |
Eino-Ville Talvala | 0ec23d3 | 2015-05-28 15:15:54 -0700 | [diff] [blame] | 2799 | { |
| 2800 | Mutex::Autolock l(mRequestLock); |
| 2801 | if (mListener != NULL) { |
| 2802 | mListener->notifyError( |
| 2803 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 2804 | nextRequest->mResultExtras); |
| 2805 | } |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2806 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2807 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2808 | return true; |
| 2809 | } |
| 2810 | request.num_output_buffers++; |
| 2811 | } |
Zhijun He | f0d962a | 2014-06-30 10:24:11 -0700 | [diff] [blame] | 2812 | totalNumBuffers += request.num_output_buffers; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2813 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2814 | // Log request in the in-flight queue |
| 2815 | sp<Camera3Device> parent = mParent.promote(); |
| 2816 | if (parent == NULL) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2817 | // 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] | 2818 | CLOGE("RequestThread: Parent is gone"); |
| 2819 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2820 | return false; |
| 2821 | } |
| 2822 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2823 | res = parent->registerInFlight(request.frame_number, |
Zhijun He | c98bd8d | 2014-07-07 12:44:10 -0700 | [diff] [blame] | 2824 | totalNumBuffers, nextRequest->mResultExtras, |
| 2825 | /*hasInput*/request.input_buffer != NULL); |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2826 | ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64 |
| 2827 | ", burstId = %" PRId32 ".", |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2828 | __FUNCTION__, |
| 2829 | nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber, |
| 2830 | nextRequest->mResultExtras.burstId); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 2831 | if (res != OK) { |
| 2832 | SET_ERR("RequestThread: Unable to register new in-flight request:" |
| 2833 | " %s (%d)", strerror(-res), res); |
| 2834 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2835 | return false; |
| 2836 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2837 | |
Zhijun He | cc27e11 | 2013-10-03 16:12:43 -0700 | [diff] [blame] | 2838 | // Inform waitUntilRequestProcessed thread of a new request ID |
| 2839 | { |
| 2840 | Mutex::Autolock al(mLatestRequestMutex); |
| 2841 | |
| 2842 | mLatestRequestId = requestId; |
| 2843 | mLatestRequestSignal.signal(); |
| 2844 | } |
| 2845 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2846 | // Submit request and block until ready for next one |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2847 | ATRACE_ASYNC_BEGIN("frame capture", request.frame_number); |
| 2848 | ATRACE_BEGIN("camera3->process_capture_request"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2849 | res = mHal3Device->ops->process_capture_request(mHal3Device, &request); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 2850 | ATRACE_END(); |
| 2851 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2852 | if (res != OK) { |
Eino-Ville Talvala | 1754351 | 2014-08-06 14:32:02 -0700 | [diff] [blame] | 2853 | // Should only get a failure here for malformed requests or device-level |
| 2854 | // errors, so consider all errors fatal. Bad metadata failures should |
| 2855 | // come through notify. |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2856 | SET_ERR("RequestThread: Unable to submit capture request %d to HAL" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2857 | " device: %s (%d)", request.frame_number, strerror(-res), res); |
| 2858 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 2859 | return false; |
| 2860 | } |
| 2861 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2862 | // Update the latest request sent to HAL |
| 2863 | if (request.settings != NULL) { // Don't update them if they were unchanged |
| 2864 | Mutex::Autolock al(mLatestRequestMutex); |
| 2865 | |
| 2866 | camera_metadata_t* cloned = clone_camera_metadata(request.settings); |
| 2867 | mLatestRequest.acquire(cloned); |
| 2868 | } |
| 2869 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2870 | if (request.settings != NULL) { |
| 2871 | nextRequest->mSettings.unlock(request.settings); |
| 2872 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2873 | |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2874 | // Unset as current request |
| 2875 | { |
| 2876 | Mutex::Autolock l(mRequestLock); |
| 2877 | mNextRequest.clear(); |
| 2878 | } |
| 2879 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2880 | // Remove any previously queued triggers (after unlock) |
| 2881 | res = removeTriggers(mPrevRequest); |
| 2882 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 2883 | SET_ERR("RequestThread: Unable to remove triggers " |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 2884 | "(capture request %d, HAL device: %s (%d)", |
| 2885 | request.frame_number, strerror(-res), res); |
| 2886 | return false; |
| 2887 | } |
| 2888 | mPrevTriggers = triggerCount; |
| 2889 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2890 | return true; |
| 2891 | } |
| 2892 | |
Igor Murashkin | 1e479c0 | 2013-09-06 16:55:14 -0700 | [diff] [blame] | 2893 | CameraMetadata Camera3Device::RequestThread::getLatestRequest() const { |
| 2894 | Mutex::Autolock al(mLatestRequestMutex); |
| 2895 | |
| 2896 | ALOGV("RequestThread::%s", __FUNCTION__); |
| 2897 | |
| 2898 | return mLatestRequest; |
| 2899 | } |
| 2900 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2901 | bool Camera3Device::RequestThread::isStreamPending( |
| 2902 | sp<Camera3StreamInterface>& stream) { |
| 2903 | Mutex::Autolock l(mRequestLock); |
| 2904 | |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2905 | if (mNextRequest != nullptr) { |
| 2906 | for (const auto& s : mNextRequest->mOutputStreams) { |
| 2907 | if (stream == s) return true; |
| 2908 | } |
| 2909 | if (stream == mNextRequest->mInputStream) return true; |
| 2910 | } |
| 2911 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 2912 | for (const auto& request : mRequestQueue) { |
| 2913 | for (const auto& s : request->mOutputStreams) { |
| 2914 | if (stream == s) return true; |
| 2915 | } |
| 2916 | if (stream == request->mInputStream) return true; |
| 2917 | } |
| 2918 | |
| 2919 | for (const auto& request : mRepeatingRequests) { |
| 2920 | for (const auto& s : request->mOutputStreams) { |
| 2921 | if (stream == s) return true; |
| 2922 | } |
| 2923 | if (stream == request->mInputStream) return true; |
| 2924 | } |
| 2925 | |
| 2926 | return false; |
| 2927 | } |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2928 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2929 | void Camera3Device::RequestThread::cleanUpFailedRequest( |
| 2930 | camera3_capture_request_t &request, |
| 2931 | sp<CaptureRequest> &nextRequest, |
| 2932 | Vector<camera3_stream_buffer_t> &outputBuffers) { |
| 2933 | |
| 2934 | if (request.settings != NULL) { |
| 2935 | nextRequest->mSettings.unlock(request.settings); |
| 2936 | } |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 2937 | if (nextRequest->mInputStream != NULL) { |
| 2938 | nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR; |
| 2939 | nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2940 | } |
| 2941 | for (size_t i = 0; i < request.num_output_buffers; i++) { |
| 2942 | outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 2943 | nextRequest->mOutputStreams.editItemAt(i)->returnBuffer( |
| 2944 | outputBuffers[i], 0); |
| 2945 | } |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 2946 | |
| 2947 | Mutex::Autolock l(mRequestLock); |
| 2948 | mNextRequest.clear(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | sp<Camera3Device::CaptureRequest> |
| 2952 | Camera3Device::RequestThread::waitForNextRequest() { |
| 2953 | status_t res; |
| 2954 | sp<CaptureRequest> nextRequest; |
| 2955 | |
| 2956 | // Optimized a bit for the simple steady-state case (single repeating |
| 2957 | // request), to avoid putting that request in the queue temporarily. |
| 2958 | Mutex::Autolock l(mRequestLock); |
| 2959 | |
| 2960 | while (mRequestQueue.empty()) { |
| 2961 | if (!mRepeatingRequests.empty()) { |
| 2962 | // Always atomically enqueue all requests in a repeating request |
| 2963 | // list. Guarantees a complete in-sequence set of captures to |
| 2964 | // application. |
| 2965 | const RequestList &requests = mRepeatingRequests; |
| 2966 | RequestList::const_iterator firstRequest = |
| 2967 | requests.begin(); |
| 2968 | nextRequest = *firstRequest; |
| 2969 | mRequestQueue.insert(mRequestQueue.end(), |
| 2970 | ++firstRequest, |
| 2971 | requests.end()); |
| 2972 | // No need to wait any longer |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2973 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 2974 | mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1; |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 2975 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2976 | break; |
| 2977 | } |
| 2978 | |
| 2979 | res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); |
| 2980 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2981 | if ((mRequestQueue.empty() && mRepeatingRequests.empty()) || |
| 2982 | exitPending()) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2983 | Mutex::Autolock pl(mPauseLock); |
| 2984 | if (mPaused == false) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2985 | ALOGV("%s: RequestThread: Going idle", __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2986 | mPaused = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 2987 | // Let the tracker know |
| 2988 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 2989 | if (statusTracker != 0) { |
| 2990 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); |
| 2991 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2992 | } |
| 2993 | // Stop waiting for now and let thread management happen |
| 2994 | return NULL; |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | if (nextRequest == NULL) { |
| 2999 | // Don't have a repeating request already in hand, so queue |
| 3000 | // must have an entry now. |
| 3001 | RequestList::iterator firstRequest = |
| 3002 | mRequestQueue.begin(); |
| 3003 | nextRequest = *firstRequest; |
| 3004 | mRequestQueue.erase(firstRequest); |
| 3005 | } |
| 3006 | |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3007 | // In case we've been unpaused by setPaused clearing mDoPause, need to |
| 3008 | // update internal pause state (capture/setRepeatingRequest unpause |
| 3009 | // directly). |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3010 | Mutex::Autolock pl(mPauseLock); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3011 | if (mPaused) { |
| 3012 | ALOGV("%s: RequestThread: Unpaused", __FUNCTION__); |
| 3013 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 3014 | if (statusTracker != 0) { |
| 3015 | statusTracker->markComponentActive(mStatusId); |
| 3016 | } |
| 3017 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3018 | mPaused = false; |
| 3019 | |
| 3020 | // Check if we've reconfigured since last time, and reset the preview |
| 3021 | // request if so. Can't use 'NULL request == repeat' across configure calls. |
| 3022 | if (mReconfigured) { |
| 3023 | mPrevRequest.clear(); |
| 3024 | mReconfigured = false; |
| 3025 | } |
| 3026 | |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3027 | if (nextRequest != NULL) { |
| 3028 | nextRequest->mResultExtras.frameNumber = mFrameNumber++; |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 3029 | nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId; |
| 3030 | nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId; |
Chien-Yu Chen | c2adf48 | 2015-05-27 14:27:49 -0700 | [diff] [blame] | 3031 | |
| 3032 | // Since RequestThread::clear() removes buffers from the input stream, |
| 3033 | // get the right buffer here before unlocking mRequestLock |
| 3034 | if (nextRequest->mInputStream != NULL) { |
| 3035 | res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer); |
| 3036 | if (res != OK) { |
| 3037 | // Can't get input buffer from gralloc queue - this could be due to |
| 3038 | // disconnected queue or other producer misbehavior, so not a fatal |
| 3039 | // error |
| 3040 | ALOGE("%s: Can't get input buffer, skipping request:" |
| 3041 | " %s (%d)", __FUNCTION__, strerror(-res), res); |
| 3042 | if (mListener != NULL) { |
| 3043 | mListener->notifyError( |
| 3044 | ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST, |
| 3045 | nextRequest->mResultExtras); |
| 3046 | } |
| 3047 | return NULL; |
| 3048 | } |
| 3049 | } |
Jianing Wei | 2d6bb3f | 2014-04-11 10:00:31 -0700 | [diff] [blame] | 3050 | } |
Eino-Ville Talvala | e74c228 | 2015-05-27 14:46:23 -0700 | [diff] [blame] | 3051 | mNextRequest = nextRequest; |
| 3052 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3053 | return nextRequest; |
| 3054 | } |
| 3055 | |
| 3056 | bool Camera3Device::RequestThread::waitIfPaused() { |
| 3057 | status_t res; |
| 3058 | Mutex::Autolock l(mPauseLock); |
| 3059 | while (mDoPause) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3060 | if (mPaused == false) { |
| 3061 | mPaused = true; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3062 | ALOGV("%s: RequestThread: Paused", __FUNCTION__); |
| 3063 | // Let the tracker know |
| 3064 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 3065 | if (statusTracker != 0) { |
| 3066 | statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE); |
| 3067 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3068 | } |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3069 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3070 | res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3071 | if (res == TIMED_OUT || exitPending()) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3072 | return true; |
| 3073 | } |
| 3074 | } |
| 3075 | // We don't set mPaused to false here, because waitForNextRequest needs |
| 3076 | // to further manage the paused state in case of starvation. |
| 3077 | return false; |
| 3078 | } |
| 3079 | |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3080 | void Camera3Device::RequestThread::unpauseForNewRequests() { |
| 3081 | // With work to do, mark thread as unpaused. |
| 3082 | // If paused by request (setPaused), don't resume, to avoid |
| 3083 | // extra signaling/waiting overhead to waitUntilPaused |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3084 | mRequestSignal.signal(); |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3085 | Mutex::Autolock p(mPauseLock); |
| 3086 | if (!mDoPause) { |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 3087 | ALOGV("%s: RequestThread: Going active", __FUNCTION__); |
| 3088 | if (mPaused) { |
| 3089 | sp<StatusTracker> statusTracker = mStatusTracker.promote(); |
| 3090 | if (statusTracker != 0) { |
| 3091 | statusTracker->markComponentActive(mStatusId); |
| 3092 | } |
| 3093 | } |
Eino-Ville Talvala | 26fe6c7 | 2013-08-29 12:46:18 -0700 | [diff] [blame] | 3094 | mPaused = false; |
| 3095 | } |
| 3096 | } |
| 3097 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 3098 | void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { |
| 3099 | sp<Camera3Device> parent = mParent.promote(); |
| 3100 | if (parent != NULL) { |
| 3101 | va_list args; |
| 3102 | va_start(args, fmt); |
| 3103 | |
| 3104 | parent->setErrorStateV(fmt, args); |
| 3105 | |
| 3106 | va_end(args); |
| 3107 | } |
| 3108 | } |
| 3109 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3110 | status_t Camera3Device::RequestThread::insertTriggers( |
| 3111 | const sp<CaptureRequest> &request) { |
| 3112 | |
| 3113 | Mutex::Autolock al(mTriggerMutex); |
| 3114 | |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3115 | sp<Camera3Device> parent = mParent.promote(); |
| 3116 | if (parent == NULL) { |
| 3117 | CLOGE("RequestThread: Parent is gone"); |
| 3118 | return DEAD_OBJECT; |
| 3119 | } |
| 3120 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3121 | CameraMetadata &metadata = request->mSettings; |
| 3122 | size_t count = mTriggerMap.size(); |
| 3123 | |
| 3124 | for (size_t i = 0; i < count; ++i) { |
| 3125 | RequestTrigger trigger = mTriggerMap.valueAt(i); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3126 | uint32_t tag = trigger.metadataTag; |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3127 | |
| 3128 | if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) { |
| 3129 | bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID); |
| 3130 | uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue); |
Yin-Chia Yeh | c00a25c | 2014-08-21 14:27:44 -0700 | [diff] [blame] | 3131 | if (isAeTrigger) { |
| 3132 | request->mResultExtras.precaptureTriggerId = triggerId; |
| 3133 | mCurrentPreCaptureTriggerId = triggerId; |
| 3134 | } else { |
| 3135 | request->mResultExtras.afTriggerId = triggerId; |
| 3136 | mCurrentAfTriggerId = triggerId; |
| 3137 | } |
Yin-Chia Yeh | 741ace8 | 2014-06-23 14:07:56 -0700 | [diff] [blame] | 3138 | if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) { |
| 3139 | continue; // Trigger ID tag is deprecated since device HAL 3.2 |
| 3140 | } |
| 3141 | } |
| 3142 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3143 | camera_metadata_entry entry = metadata.find(tag); |
| 3144 | |
| 3145 | if (entry.count > 0) { |
| 3146 | /** |
| 3147 | * Already has an entry for this trigger in the request. |
| 3148 | * Rewrite it with our requested trigger value. |
| 3149 | */ |
| 3150 | RequestTrigger oldTrigger = trigger; |
| 3151 | |
| 3152 | oldTrigger.entryValue = entry.data.u8[0]; |
| 3153 | |
| 3154 | mTriggerReplacedMap.add(tag, oldTrigger); |
| 3155 | } else { |
| 3156 | /** |
| 3157 | * More typical, no trigger entry, so we just add it |
| 3158 | */ |
| 3159 | mTriggerRemovedMap.add(tag, trigger); |
| 3160 | } |
| 3161 | |
| 3162 | status_t res; |
| 3163 | |
| 3164 | switch (trigger.getTagType()) { |
| 3165 | case TYPE_BYTE: { |
| 3166 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 3167 | res = metadata.update(tag, |
| 3168 | &entryValue, |
| 3169 | /*count*/1); |
| 3170 | break; |
| 3171 | } |
| 3172 | case TYPE_INT32: |
| 3173 | res = metadata.update(tag, |
| 3174 | &trigger.entryValue, |
| 3175 | /*count*/1); |
| 3176 | break; |
| 3177 | default: |
| 3178 | ALOGE("%s: Type not supported: 0x%x", |
| 3179 | __FUNCTION__, |
| 3180 | trigger.getTagType()); |
| 3181 | return INVALID_OPERATION; |
| 3182 | } |
| 3183 | |
| 3184 | if (res != OK) { |
| 3185 | ALOGE("%s: Failed to update request metadata with trigger tag %s" |
| 3186 | ", value %d", __FUNCTION__, trigger.getTagName(), |
| 3187 | trigger.entryValue); |
| 3188 | return res; |
| 3189 | } |
| 3190 | |
| 3191 | ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, |
| 3192 | trigger.getTagName(), |
| 3193 | trigger.entryValue); |
| 3194 | } |
| 3195 | |
| 3196 | mTriggerMap.clear(); |
| 3197 | |
| 3198 | return count; |
| 3199 | } |
| 3200 | |
| 3201 | status_t Camera3Device::RequestThread::removeTriggers( |
| 3202 | const sp<CaptureRequest> &request) { |
| 3203 | Mutex::Autolock al(mTriggerMutex); |
| 3204 | |
| 3205 | CameraMetadata &metadata = request->mSettings; |
| 3206 | |
| 3207 | /** |
| 3208 | * Replace all old entries with their old values. |
| 3209 | */ |
| 3210 | for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { |
| 3211 | RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); |
| 3212 | |
| 3213 | status_t res; |
| 3214 | |
| 3215 | uint32_t tag = trigger.metadataTag; |
| 3216 | switch (trigger.getTagType()) { |
| 3217 | case TYPE_BYTE: { |
| 3218 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 3219 | res = metadata.update(tag, |
| 3220 | &entryValue, |
| 3221 | /*count*/1); |
| 3222 | break; |
| 3223 | } |
| 3224 | case TYPE_INT32: |
| 3225 | res = metadata.update(tag, |
| 3226 | &trigger.entryValue, |
| 3227 | /*count*/1); |
| 3228 | break; |
| 3229 | default: |
| 3230 | ALOGE("%s: Type not supported: 0x%x", |
| 3231 | __FUNCTION__, |
| 3232 | trigger.getTagType()); |
| 3233 | return INVALID_OPERATION; |
| 3234 | } |
| 3235 | |
| 3236 | if (res != OK) { |
| 3237 | ALOGE("%s: Failed to restore request metadata with trigger tag %s" |
| 3238 | ", trigger value %d", __FUNCTION__, |
| 3239 | trigger.getTagName(), trigger.entryValue); |
| 3240 | return res; |
| 3241 | } |
| 3242 | } |
| 3243 | mTriggerReplacedMap.clear(); |
| 3244 | |
| 3245 | /** |
| 3246 | * Remove all new entries. |
| 3247 | */ |
| 3248 | for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { |
| 3249 | RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); |
| 3250 | status_t res = metadata.erase(trigger.metadataTag); |
| 3251 | |
| 3252 | if (res != OK) { |
| 3253 | ALOGE("%s: Failed to erase metadata with trigger tag %s" |
| 3254 | ", trigger value %d", __FUNCTION__, |
| 3255 | trigger.getTagName(), trigger.entryValue); |
| 3256 | return res; |
| 3257 | } |
| 3258 | } |
| 3259 | mTriggerRemovedMap.clear(); |
| 3260 | |
| 3261 | return OK; |
| 3262 | } |
| 3263 | |
Eino-Ville Talvala | 2f876f9 | 2013-09-13 11:39:24 -0700 | [diff] [blame] | 3264 | status_t Camera3Device::RequestThread::addDummyTriggerIds( |
| 3265 | const sp<CaptureRequest> &request) { |
| 3266 | // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here |
| 3267 | static const int32_t dummyTriggerId = 1; |
| 3268 | status_t res; |
| 3269 | |
| 3270 | CameraMetadata &metadata = request->mSettings; |
| 3271 | |
| 3272 | // If AF trigger is active, insert a dummy AF trigger ID if none already |
| 3273 | // exists |
| 3274 | camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER); |
| 3275 | camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID); |
| 3276 | if (afTrigger.count > 0 && |
| 3277 | afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE && |
| 3278 | afId.count == 0) { |
| 3279 | res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1); |
| 3280 | if (res != OK) return res; |
| 3281 | } |
| 3282 | |
| 3283 | // If AE precapture trigger is active, insert a dummy precapture trigger ID |
| 3284 | // if none already exists |
| 3285 | camera_metadata_entry pcTrigger = |
| 3286 | metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER); |
| 3287 | camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); |
| 3288 | if (pcTrigger.count > 0 && |
| 3289 | pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE && |
| 3290 | pcId.count == 0) { |
| 3291 | res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID, |
| 3292 | &dummyTriggerId, 1); |
| 3293 | if (res != OK) return res; |
| 3294 | } |
| 3295 | |
| 3296 | return OK; |
| 3297 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3298 | |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 3299 | /** |
| 3300 | * PreparerThread inner class methods |
| 3301 | */ |
| 3302 | |
| 3303 | Camera3Device::PreparerThread::PreparerThread() : |
| 3304 | Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) { |
| 3305 | } |
| 3306 | |
| 3307 | Camera3Device::PreparerThread::~PreparerThread() { |
| 3308 | Thread::requestExitAndWait(); |
| 3309 | if (mCurrentStream != nullptr) { |
| 3310 | mCurrentStream->cancelPrepare(); |
| 3311 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 3312 | mCurrentStream.clear(); |
| 3313 | } |
| 3314 | clear(); |
| 3315 | } |
| 3316 | |
| 3317 | status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) { |
| 3318 | status_t res; |
| 3319 | |
| 3320 | Mutex::Autolock l(mLock); |
| 3321 | |
| 3322 | res = stream->startPrepare(); |
| 3323 | if (res == OK) { |
| 3324 | // No preparation needed, fire listener right off |
| 3325 | ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId()); |
| 3326 | if (mListener) { |
| 3327 | mListener->notifyPrepared(stream->getId()); |
| 3328 | } |
| 3329 | return OK; |
| 3330 | } else if (res != NOT_ENOUGH_DATA) { |
| 3331 | return res; |
| 3332 | } |
| 3333 | |
| 3334 | // Need to prepare, start up thread if necessary |
| 3335 | if (!mActive) { |
| 3336 | // mRunning will change to false before the thread fully shuts down, so wait to be sure it |
| 3337 | // isn't running |
| 3338 | Thread::requestExitAndWait(); |
| 3339 | res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND); |
| 3340 | if (res != OK) { |
| 3341 | ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res)); |
| 3342 | if (mListener) { |
| 3343 | mListener->notifyPrepared(stream->getId()); |
| 3344 | } |
| 3345 | return res; |
| 3346 | } |
| 3347 | mCancelNow = false; |
| 3348 | mActive = true; |
| 3349 | ALOGV("%s: Preparer stream started", __FUNCTION__); |
| 3350 | } |
| 3351 | |
| 3352 | // queue up the work |
| 3353 | mPendingStreams.push_back(stream); |
| 3354 | ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId()); |
| 3355 | |
| 3356 | return OK; |
| 3357 | } |
| 3358 | |
| 3359 | status_t Camera3Device::PreparerThread::clear() { |
| 3360 | status_t res; |
| 3361 | |
| 3362 | Mutex::Autolock l(mLock); |
| 3363 | |
| 3364 | for (const auto& stream : mPendingStreams) { |
| 3365 | stream->cancelPrepare(); |
| 3366 | } |
| 3367 | mPendingStreams.clear(); |
| 3368 | mCancelNow = true; |
| 3369 | |
| 3370 | return OK; |
| 3371 | } |
| 3372 | |
| 3373 | void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) { |
| 3374 | Mutex::Autolock l(mLock); |
| 3375 | mListener = listener; |
| 3376 | } |
| 3377 | |
| 3378 | bool Camera3Device::PreparerThread::threadLoop() { |
| 3379 | status_t res; |
| 3380 | { |
| 3381 | Mutex::Autolock l(mLock); |
| 3382 | if (mCurrentStream == nullptr) { |
| 3383 | // End thread if done with work |
| 3384 | if (mPendingStreams.empty()) { |
| 3385 | ALOGV("%s: Preparer stream out of work", __FUNCTION__); |
| 3386 | // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would |
| 3387 | // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive. |
| 3388 | mActive = false; |
| 3389 | return false; |
| 3390 | } |
| 3391 | |
| 3392 | // Get next stream to prepare |
| 3393 | auto it = mPendingStreams.begin(); |
| 3394 | mCurrentStream = *it; |
| 3395 | mPendingStreams.erase(it); |
| 3396 | ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId()); |
| 3397 | ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId()); |
| 3398 | } else if (mCancelNow) { |
| 3399 | mCurrentStream->cancelPrepare(); |
| 3400 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 3401 | ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId()); |
| 3402 | mCurrentStream.clear(); |
| 3403 | mCancelNow = false; |
| 3404 | return true; |
| 3405 | } |
| 3406 | } |
| 3407 | |
| 3408 | res = mCurrentStream->prepareNextBuffer(); |
| 3409 | if (res == NOT_ENOUGH_DATA) return true; |
| 3410 | if (res != OK) { |
| 3411 | // Something bad happened; try to recover by cancelling prepare and |
| 3412 | // signalling listener anyway |
| 3413 | ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__, |
| 3414 | mCurrentStream->getId(), res, strerror(-res)); |
| 3415 | mCurrentStream->cancelPrepare(); |
| 3416 | } |
| 3417 | |
| 3418 | // This stream has finished, notify listener |
| 3419 | Mutex::Autolock l(mLock); |
| 3420 | if (mListener) { |
| 3421 | ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__, |
| 3422 | mCurrentStream->getId()); |
| 3423 | mListener->notifyPrepared(mCurrentStream->getId()); |
| 3424 | } |
| 3425 | |
| 3426 | ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId()); |
| 3427 | mCurrentStream.clear(); |
| 3428 | |
| 3429 | return true; |
| 3430 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 3431 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 3432 | /** |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 3433 | * Static callback forwarding methods from HAL to instance |
| 3434 | */ |
| 3435 | |
| 3436 | void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, |
| 3437 | const camera3_capture_result *result) { |
| 3438 | Camera3Device *d = |
| 3439 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
| 3440 | d->processCaptureResult(result); |
| 3441 | } |
| 3442 | |
| 3443 | void Camera3Device::sNotify(const camera3_callback_ops *cb, |
| 3444 | const camera3_notify_msg *msg) { |
| 3445 | Camera3Device *d = |
| 3446 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
| 3447 | d->notify(msg); |
| 3448 | } |
| 3449 | |
| 3450 | }; // namespace android |