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