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