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