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