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 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 40 | #include <utils/Log.h> |
| 41 | #include <utils/Trace.h> |
| 42 | #include <utils/Timers.h> |
| 43 | #include "Camera3Device.h" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 44 | #include "camera3/Camera3OutputStream.h" |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 45 | #include "camera3/Camera3InputStream.h" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 46 | |
| 47 | using namespace android::camera3; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 48 | |
| 49 | namespace android { |
| 50 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 51 | Camera3Device::Camera3Device(int id): |
| 52 | mId(id), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 53 | mHal3Device(NULL), |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 54 | mStatus(STATUS_UNINITIALIZED), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 55 | mNextResultFrameNumber(0), |
| 56 | mNextShutterFrameNumber(0), |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 57 | mListener(NULL) |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 58 | { |
| 59 | ATRACE_CALL(); |
| 60 | camera3_callback_ops::notify = &sNotify; |
| 61 | camera3_callback_ops::process_capture_result = &sProcessCaptureResult; |
| 62 | ALOGV("%s: Created device for camera %d", __FUNCTION__, id); |
| 63 | } |
| 64 | |
| 65 | Camera3Device::~Camera3Device() |
| 66 | { |
| 67 | ATRACE_CALL(); |
| 68 | ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId); |
| 69 | disconnect(); |
| 70 | } |
| 71 | |
Igor Murashkin | 7138105 | 2013-03-04 14:53:08 -0800 | [diff] [blame] | 72 | int Camera3Device::getId() const { |
| 73 | return mId; |
| 74 | } |
| 75 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 76 | /** |
| 77 | * CameraDeviceBase interface |
| 78 | */ |
| 79 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 80 | status_t Camera3Device::initialize(camera_module_t *module) |
| 81 | { |
| 82 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 83 | Mutex::Autolock l(mLock); |
| 84 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 85 | ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 86 | if (mStatus != STATUS_UNINITIALIZED) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 87 | CLOGE("Already initialized!"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 88 | return INVALID_OPERATION; |
| 89 | } |
| 90 | |
| 91 | /** Open HAL device */ |
| 92 | |
| 93 | status_t res; |
| 94 | String8 deviceName = String8::format("%d", mId); |
| 95 | |
| 96 | camera3_device_t *device; |
| 97 | |
| 98 | res = module->common.methods->open(&module->common, deviceName.string(), |
| 99 | reinterpret_cast<hw_device_t**>(&device)); |
| 100 | |
| 101 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 102 | 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] | 103 | return res; |
| 104 | } |
| 105 | |
| 106 | /** Cross-check device version */ |
| 107 | |
| 108 | if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 109 | SET_ERR_L("Could not open camera: " |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 110 | "Camera device is not version %x, reports %x instead", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 111 | CAMERA_DEVICE_API_VERSION_3_0, |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 112 | device->common.version); |
| 113 | device->common.close(&device->common); |
| 114 | return BAD_VALUE; |
| 115 | } |
| 116 | |
| 117 | camera_info info; |
| 118 | res = module->get_camera_info(mId, &info); |
| 119 | if (res != OK) return res; |
| 120 | |
| 121 | if (info.device_version != device->common.version) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 122 | SET_ERR_L("HAL reporting mismatched camera_info version (%x)" |
| 123 | " and device version (%x).", |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 124 | device->common.version, info.device_version); |
| 125 | device->common.close(&device->common); |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | |
| 129 | /** Initialize device with callback functions */ |
| 130 | |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 131 | ATRACE_BEGIN("camera3->initialize"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 132 | res = device->ops->initialize(device, this); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 133 | ATRACE_END(); |
| 134 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 135 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 136 | SET_ERR_L("Unable to initialize HAL device: %s (%d)", |
| 137 | strerror(-res), res); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 138 | device->common.close(&device->common); |
| 139 | return BAD_VALUE; |
| 140 | } |
| 141 | |
| 142 | /** Get vendor metadata tags */ |
| 143 | |
| 144 | mVendorTagOps.get_camera_vendor_section_name = NULL; |
| 145 | |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 146 | ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 147 | device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 148 | ATRACE_END(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 149 | |
| 150 | if (mVendorTagOps.get_camera_vendor_section_name != NULL) { |
| 151 | res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps); |
| 152 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 153 | SET_ERR_L("Unable to set tag ops: %s (%d)", |
| 154 | strerror(-res), res); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 155 | device->common.close(&device->common); |
| 156 | return res; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** Start up request queue thread */ |
| 161 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 162 | mRequestThread = new RequestThread(this, device); |
| 163 | res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string()); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 164 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 165 | SET_ERR_L("Unable to start request queue thread: %s (%d)", |
| 166 | strerror(-res), res); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 167 | device->common.close(&device->common); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 168 | mRequestThread.clear(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 169 | return res; |
| 170 | } |
| 171 | |
| 172 | /** Everything is good to go */ |
| 173 | |
| 174 | mDeviceInfo = info.static_camera_characteristics; |
| 175 | mHal3Device = device; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 176 | mStatus = STATUS_IDLE; |
| 177 | mNextStreamId = 0; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 178 | mNeedConfig = true; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 179 | |
| 180 | return OK; |
| 181 | } |
| 182 | |
| 183 | status_t Camera3Device::disconnect() { |
| 184 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 185 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 186 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 187 | ALOGV("%s: E", __FUNCTION__); |
| 188 | |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 189 | status_t res = OK; |
| 190 | if (mStatus == STATUS_UNINITIALIZED) return res; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 191 | |
| 192 | if (mStatus == STATUS_ACTIVE || |
| 193 | (mStatus == STATUS_ERROR && mRequestThread != NULL)) { |
| 194 | res = mRequestThread->clearRepeatingRequests(); |
| 195 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 196 | SET_ERR_L("Can't stop streaming"); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 197 | // Continue to close device even in case of error |
| 198 | } else { |
| 199 | res = waitUntilDrainedLocked(); |
| 200 | if (res != OK) { |
| 201 | SET_ERR_L("Timeout waiting for HAL to drain"); |
| 202 | // Continue to close device even in case of error |
| 203 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR); |
| 207 | |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 208 | if (mStatus == STATUS_ERROR) { |
| 209 | CLOGE("Shutting down in an error state"); |
| 210 | } |
| 211 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 212 | if (mRequestThread != NULL) { |
| 213 | mRequestThread->requestExit(); |
| 214 | } |
| 215 | |
| 216 | mOutputStreams.clear(); |
| 217 | mInputStream.clear(); |
| 218 | |
| 219 | if (mRequestThread != NULL) { |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 220 | if (mStatus != STATUS_ERROR) { |
| 221 | // HAL may be in a bad state, so waiting for request thread |
| 222 | // (which may be stuck in the HAL processCaptureRequest call) |
| 223 | // could be dangerous. |
| 224 | mRequestThread->join(); |
| 225 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 226 | mRequestThread.clear(); |
| 227 | } |
| 228 | |
| 229 | if (mHal3Device != NULL) { |
| 230 | mHal3Device->common.close(&mHal3Device->common); |
| 231 | mHal3Device = NULL; |
| 232 | } |
| 233 | |
| 234 | mStatus = STATUS_UNINITIALIZED; |
| 235 | |
| 236 | ALOGV("%s: X", __FUNCTION__); |
Eino-Ville Talvala | 214a17f | 2013-06-13 12:20:02 -0700 | [diff] [blame] | 237 | return res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | status_t Camera3Device::dump(int fd, const Vector<String16> &args) { |
| 241 | ATRACE_CALL(); |
| 242 | (void)args; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 243 | String8 lines; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 244 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 245 | const char *status = |
| 246 | mStatus == STATUS_ERROR ? "ERROR" : |
| 247 | mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" : |
| 248 | mStatus == STATUS_IDLE ? "IDLE" : |
| 249 | mStatus == STATUS_ACTIVE ? "ACTIVE" : |
| 250 | "Unknown"; |
| 251 | lines.appendFormat(" Device status: %s\n", status); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 252 | if (mStatus == STATUS_ERROR) { |
| 253 | lines.appendFormat(" Error cause: %s\n", mErrorCause.string()); |
| 254 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 255 | lines.appendFormat(" Stream configuration:\n"); |
| 256 | |
| 257 | if (mInputStream != NULL) { |
| 258 | write(fd, lines.string(), lines.size()); |
| 259 | mInputStream->dump(fd, args); |
| 260 | } else { |
| 261 | lines.appendFormat(" No input stream.\n"); |
| 262 | write(fd, lines.string(), lines.size()); |
| 263 | } |
| 264 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
| 265 | mOutputStreams[i]->dump(fd,args); |
| 266 | } |
| 267 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 268 | lines = String8(" In-flight requests:\n"); |
| 269 | if (mInFlightMap.size() == 0) { |
| 270 | lines.append(" None\n"); |
| 271 | } else { |
| 272 | for (size_t i = 0; i < mInFlightMap.size(); i++) { |
| 273 | InFlightRequest r = mInFlightMap.valueAt(i); |
| 274 | lines.appendFormat(" Frame %d | Timestamp: %lld, metadata" |
| 275 | " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i), |
| 276 | r.captureTimestamp, r.haveResultMetadata ? "true" : "false", |
| 277 | r.numBuffersLeft); |
| 278 | } |
| 279 | } |
| 280 | write(fd, lines.string(), lines.size()); |
| 281 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 282 | if (mHal3Device != NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 283 | lines = String8(" HAL device dump:\n"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 284 | write(fd, lines.string(), lines.size()); |
| 285 | mHal3Device->ops->dump(mHal3Device, fd); |
| 286 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 287 | |
| 288 | return OK; |
| 289 | } |
| 290 | |
| 291 | const CameraMetadata& Camera3Device::info() const { |
| 292 | ALOGVV("%s: E", __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 293 | if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED || |
| 294 | mStatus == STATUS_ERROR)) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 295 | ALOGW("%s: Access to static info %s!", __FUNCTION__, |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 296 | mStatus == STATUS_ERROR ? |
| 297 | "when in error state" : "before init"); |
| 298 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 299 | return mDeviceInfo; |
| 300 | } |
| 301 | |
| 302 | status_t Camera3Device::capture(CameraMetadata &request) { |
| 303 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 304 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 305 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 306 | // TODO: take ownership of the request |
| 307 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 308 | switch (mStatus) { |
| 309 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 310 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 311 | return INVALID_OPERATION; |
| 312 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 313 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 314 | return INVALID_OPERATION; |
| 315 | case STATUS_IDLE: |
| 316 | case STATUS_ACTIVE: |
| 317 | // OK |
| 318 | break; |
| 319 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 320 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 321 | return INVALID_OPERATION; |
| 322 | } |
| 323 | |
| 324 | sp<CaptureRequest> newRequest = setUpRequestLocked(request); |
| 325 | if (newRequest == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 326 | CLOGE("Can't create capture request"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 327 | return BAD_VALUE; |
| 328 | } |
| 329 | |
| 330 | return mRequestThread->queueRequest(newRequest); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | |
| 334 | status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) { |
| 335 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 336 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 337 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 338 | switch (mStatus) { |
| 339 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 340 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 341 | return INVALID_OPERATION; |
| 342 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 343 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 344 | return INVALID_OPERATION; |
| 345 | case STATUS_IDLE: |
| 346 | case STATUS_ACTIVE: |
| 347 | // OK |
| 348 | break; |
| 349 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 350 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 351 | return INVALID_OPERATION; |
| 352 | } |
| 353 | |
| 354 | sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request); |
| 355 | if (newRepeatingRequest == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 356 | CLOGE("Can't create repeating request"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 357 | return BAD_VALUE; |
| 358 | } |
| 359 | |
| 360 | RequestList newRepeatingRequests; |
| 361 | newRepeatingRequests.push_back(newRepeatingRequest); |
| 362 | |
| 363 | return mRequestThread->setRepeatingRequests(newRepeatingRequests); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked( |
| 368 | const CameraMetadata &request) { |
| 369 | status_t res; |
| 370 | |
| 371 | if (mStatus == STATUS_IDLE) { |
| 372 | res = configureStreamsLocked(); |
| 373 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 374 | 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] | 375 | return NULL; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | sp<CaptureRequest> newRequest = createCaptureRequest(request); |
| 380 | return newRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | status_t Camera3Device::clearStreamingRequest() { |
| 384 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 385 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 386 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 387 | switch (mStatus) { |
| 388 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 389 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 390 | return INVALID_OPERATION; |
| 391 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 392 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 393 | return INVALID_OPERATION; |
| 394 | case STATUS_IDLE: |
| 395 | case STATUS_ACTIVE: |
| 396 | // OK |
| 397 | break; |
| 398 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 399 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 400 | return INVALID_OPERATION; |
| 401 | } |
| 402 | |
| 403 | return mRequestThread->clearRepeatingRequests(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) { |
| 407 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 408 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 409 | return mRequestThread->waitUntilRequestProcessed(requestId, timeout); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 412 | status_t Camera3Device::createInputStream( |
| 413 | uint32_t width, uint32_t height, int format, int *id) { |
| 414 | ATRACE_CALL(); |
| 415 | Mutex::Autolock l(mLock); |
| 416 | |
| 417 | status_t res; |
| 418 | bool wasActive = false; |
| 419 | |
| 420 | switch (mStatus) { |
| 421 | case STATUS_ERROR: |
| 422 | ALOGE("%s: Device has encountered a serious error", __FUNCTION__); |
| 423 | return INVALID_OPERATION; |
| 424 | case STATUS_UNINITIALIZED: |
| 425 | ALOGE("%s: Device not initialized", __FUNCTION__); |
| 426 | return INVALID_OPERATION; |
| 427 | case STATUS_IDLE: |
| 428 | // OK |
| 429 | break; |
| 430 | case STATUS_ACTIVE: |
| 431 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
| 432 | mRequestThread->setPaused(true); |
| 433 | res = waitUntilDrainedLocked(); |
| 434 | if (res != OK) { |
| 435 | ALOGE("%s: Can't pause captures to reconfigure streams!", |
| 436 | __FUNCTION__); |
| 437 | mStatus = STATUS_ERROR; |
| 438 | return res; |
| 439 | } |
| 440 | wasActive = true; |
| 441 | break; |
| 442 | default: |
| 443 | ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus); |
| 444 | return INVALID_OPERATION; |
| 445 | } |
| 446 | assert(mStatus == STATUS_IDLE); |
| 447 | |
| 448 | if (mInputStream != 0) { |
| 449 | ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); |
| 450 | return INVALID_OPERATION; |
| 451 | } |
| 452 | |
| 453 | sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId, |
| 454 | width, height, format); |
| 455 | |
| 456 | mInputStream = newStream; |
| 457 | |
| 458 | *id = mNextStreamId++; |
| 459 | |
| 460 | // Continue captures if active at start |
| 461 | if (wasActive) { |
| 462 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
| 463 | res = configureStreamsLocked(); |
| 464 | if (res != OK) { |
| 465 | ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", |
| 466 | __FUNCTION__, mNextStreamId, strerror(-res), res); |
| 467 | return res; |
| 468 | } |
| 469 | mRequestThread->setPaused(false); |
| 470 | } |
| 471 | |
| 472 | return OK; |
| 473 | } |
| 474 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 475 | |
| 476 | status_t Camera3Device::createZslStream( |
| 477 | uint32_t width, uint32_t height, |
| 478 | int depth, |
| 479 | /*out*/ |
| 480 | int *id, |
| 481 | sp<Camera3ZslStream>* zslStream) { |
| 482 | ATRACE_CALL(); |
| 483 | Mutex::Autolock l(mLock); |
| 484 | |
| 485 | status_t res; |
| 486 | bool wasActive = false; |
| 487 | |
| 488 | switch (mStatus) { |
| 489 | case STATUS_ERROR: |
| 490 | ALOGE("%s: Device has encountered a serious error", __FUNCTION__); |
| 491 | return INVALID_OPERATION; |
| 492 | case STATUS_UNINITIALIZED: |
| 493 | ALOGE("%s: Device not initialized", __FUNCTION__); |
| 494 | return INVALID_OPERATION; |
| 495 | case STATUS_IDLE: |
| 496 | // OK |
| 497 | break; |
| 498 | case STATUS_ACTIVE: |
| 499 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
| 500 | mRequestThread->setPaused(true); |
| 501 | res = waitUntilDrainedLocked(); |
| 502 | if (res != OK) { |
| 503 | ALOGE("%s: Can't pause captures to reconfigure streams!", |
| 504 | __FUNCTION__); |
| 505 | mStatus = STATUS_ERROR; |
| 506 | return res; |
| 507 | } |
| 508 | wasActive = true; |
| 509 | break; |
| 510 | default: |
| 511 | ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus); |
| 512 | return INVALID_OPERATION; |
| 513 | } |
| 514 | assert(mStatus == STATUS_IDLE); |
| 515 | |
| 516 | if (mInputStream != 0) { |
| 517 | ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__); |
| 518 | return INVALID_OPERATION; |
| 519 | } |
| 520 | |
| 521 | sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId, |
| 522 | width, height, depth); |
| 523 | |
| 524 | res = mOutputStreams.add(mNextStreamId, newStream); |
| 525 | if (res < 0) { |
| 526 | ALOGE("%s: Can't add new stream to set: %s (%d)", |
| 527 | __FUNCTION__, strerror(-res), res); |
| 528 | return res; |
| 529 | } |
| 530 | mInputStream = newStream; |
| 531 | |
| 532 | *id = mNextStreamId++; |
| 533 | *zslStream = newStream; |
| 534 | |
| 535 | // Continue captures if active at start |
| 536 | if (wasActive) { |
| 537 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
| 538 | res = configureStreamsLocked(); |
| 539 | if (res != OK) { |
| 540 | ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)", |
| 541 | __FUNCTION__, mNextStreamId, strerror(-res), res); |
| 542 | return res; |
| 543 | } |
| 544 | mRequestThread->setPaused(false); |
| 545 | } |
| 546 | |
| 547 | return OK; |
| 548 | } |
| 549 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 550 | status_t Camera3Device::createStream(sp<ANativeWindow> consumer, |
| 551 | uint32_t width, uint32_t height, int format, size_t size, int *id) { |
| 552 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 553 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 554 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 555 | status_t res; |
| 556 | bool wasActive = false; |
| 557 | |
| 558 | switch (mStatus) { |
| 559 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 560 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 561 | return INVALID_OPERATION; |
| 562 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 563 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 564 | return INVALID_OPERATION; |
| 565 | case STATUS_IDLE: |
| 566 | // OK |
| 567 | break; |
| 568 | case STATUS_ACTIVE: |
| 569 | ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__); |
| 570 | mRequestThread->setPaused(true); |
| 571 | res = waitUntilDrainedLocked(); |
| 572 | if (res != OK) { |
| 573 | ALOGE("%s: Can't pause captures to reconfigure streams!", |
| 574 | __FUNCTION__); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 575 | return res; |
| 576 | } |
| 577 | wasActive = true; |
| 578 | break; |
| 579 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 580 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 581 | return INVALID_OPERATION; |
| 582 | } |
| 583 | assert(mStatus == STATUS_IDLE); |
| 584 | |
| 585 | sp<Camera3OutputStream> newStream; |
| 586 | if (format == HAL_PIXEL_FORMAT_BLOB) { |
| 587 | newStream = new Camera3OutputStream(mNextStreamId, consumer, |
| 588 | width, height, size, format); |
| 589 | } else { |
| 590 | newStream = new Camera3OutputStream(mNextStreamId, consumer, |
| 591 | width, height, format); |
| 592 | } |
| 593 | |
| 594 | res = mOutputStreams.add(mNextStreamId, newStream); |
| 595 | if (res < 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 596 | 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] | 597 | return res; |
| 598 | } |
| 599 | |
| 600 | *id = mNextStreamId++; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 601 | mNeedConfig = true; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 602 | |
| 603 | // Continue captures if active at start |
| 604 | if (wasActive) { |
| 605 | ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__); |
| 606 | res = configureStreamsLocked(); |
| 607 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 608 | CLOGE("Can't reconfigure device for new stream %d: %s (%d)", |
| 609 | mNextStreamId, strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 610 | return res; |
| 611 | } |
| 612 | mRequestThread->setPaused(false); |
| 613 | } |
| 614 | |
| 615 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) { |
| 619 | ATRACE_CALL(); |
| 620 | (void)outputId; (void)id; |
| 621 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 622 | CLOGE("Unimplemented"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 623 | return INVALID_OPERATION; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | status_t Camera3Device::getStreamInfo(int id, |
| 628 | uint32_t *width, uint32_t *height, uint32_t *format) { |
| 629 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 630 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 631 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 632 | switch (mStatus) { |
| 633 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 634 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 635 | return INVALID_OPERATION; |
| 636 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 637 | CLOGE("Device not initialized!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 638 | return INVALID_OPERATION; |
| 639 | case STATUS_IDLE: |
| 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 | } |
| 647 | |
| 648 | ssize_t idx = mOutputStreams.indexOfKey(id); |
| 649 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 650 | CLOGE("Stream %d is unknown", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 651 | return idx; |
| 652 | } |
| 653 | |
| 654 | if (width) *width = mOutputStreams[idx]->getWidth(); |
| 655 | if (height) *height = mOutputStreams[idx]->getHeight(); |
| 656 | if (format) *format = mOutputStreams[idx]->getFormat(); |
| 657 | |
| 658 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | status_t Camera3Device::setStreamTransform(int id, |
| 662 | int transform) { |
| 663 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 664 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 665 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 666 | switch (mStatus) { |
| 667 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 668 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 669 | return INVALID_OPERATION; |
| 670 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 671 | CLOGE("Device not initialized"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 672 | return INVALID_OPERATION; |
| 673 | case STATUS_IDLE: |
| 674 | case STATUS_ACTIVE: |
| 675 | // OK |
| 676 | break; |
| 677 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 678 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 679 | return INVALID_OPERATION; |
| 680 | } |
| 681 | |
| 682 | ssize_t idx = mOutputStreams.indexOfKey(id); |
| 683 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 684 | CLOGE("Stream %d does not exist", |
| 685 | id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 686 | return BAD_VALUE; |
| 687 | } |
| 688 | |
| 689 | return mOutputStreams.editValueAt(idx)->setTransform(transform); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | status_t Camera3Device::deleteStream(int id) { |
| 693 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 694 | Mutex::Autolock l(mLock); |
| 695 | status_t res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 696 | |
Igor Murashkin | e2172be | 2013-05-28 15:31:39 -0700 | [diff] [blame] | 697 | ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id); |
| 698 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 699 | // CameraDevice semantics require device to already be idle before |
| 700 | // deleteStream is called, unlike for createStream. |
| 701 | if (mStatus != STATUS_IDLE) { |
Igor Murashkin | 5282713 | 2013-05-13 14:53:44 -0700 | [diff] [blame] | 702 | ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId); |
| 703 | return -EBUSY; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 706 | sp<Camera3StreamInterface> deletedStream; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 707 | if (mInputStream != NULL && id == mInputStream->getId()) { |
| 708 | deletedStream = mInputStream; |
| 709 | mInputStream.clear(); |
| 710 | } else { |
| 711 | ssize_t idx = mOutputStreams.indexOfKey(id); |
| 712 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 713 | CLOGE("Stream %d does not exist", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 714 | return BAD_VALUE; |
| 715 | } |
| 716 | deletedStream = mOutputStreams.editValueAt(idx); |
| 717 | mOutputStreams.removeItem(id); |
| 718 | } |
| 719 | |
| 720 | // Free up the stream endpoint so that it can be used by some other stream |
| 721 | res = deletedStream->disconnect(); |
| 722 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 723 | SET_ERR_L("Can't disconnect deleted stream %d", id); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 724 | // fall through since we want to still list the stream as deleted. |
| 725 | } |
| 726 | mDeletedStreams.add(deletedStream); |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 727 | mNeedConfig = true; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 728 | |
| 729 | return res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | status_t Camera3Device::deleteReprocessStream(int id) { |
| 733 | ATRACE_CALL(); |
| 734 | (void)id; |
| 735 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 736 | CLOGE("Unimplemented"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 737 | return INVALID_OPERATION; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | status_t Camera3Device::createDefaultRequest(int templateId, |
| 742 | CameraMetadata *request) { |
| 743 | ATRACE_CALL(); |
Alex Ray | fe7e0c6 | 2013-05-30 00:12:13 -0700 | [diff] [blame] | 744 | ALOGV("%s: for template %d", __FUNCTION__, templateId); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 745 | Mutex::Autolock l(mLock); |
| 746 | |
| 747 | switch (mStatus) { |
| 748 | case STATUS_ERROR: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 749 | CLOGE("Device has encountered a serious error"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 750 | return INVALID_OPERATION; |
| 751 | case STATUS_UNINITIALIZED: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 752 | CLOGE("Device is not initialized!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 753 | return INVALID_OPERATION; |
| 754 | case STATUS_IDLE: |
| 755 | case STATUS_ACTIVE: |
| 756 | // OK |
| 757 | break; |
| 758 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 759 | SET_ERR_L("Unexpected status: %d", mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 760 | return INVALID_OPERATION; |
| 761 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 762 | |
| 763 | const camera_metadata_t *rawRequest; |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 764 | ATRACE_BEGIN("camera3->construct_default_request_settings"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 765 | rawRequest = mHal3Device->ops->construct_default_request_settings( |
| 766 | mHal3Device, templateId); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 767 | ATRACE_END(); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 768 | if (rawRequest == NULL) { |
| 769 | SET_ERR_L("HAL is unable to construct default settings for template %d", |
| 770 | templateId); |
| 771 | return DEAD_OBJECT; |
| 772 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 773 | *request = rawRequest; |
| 774 | |
| 775 | return OK; |
| 776 | } |
| 777 | |
| 778 | status_t Camera3Device::waitUntilDrained() { |
| 779 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 780 | Mutex::Autolock l(mLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 781 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 782 | return waitUntilDrainedLocked(); |
| 783 | } |
| 784 | |
| 785 | status_t Camera3Device::waitUntilDrainedLocked() { |
| 786 | ATRACE_CALL(); |
| 787 | status_t res; |
| 788 | |
| 789 | switch (mStatus) { |
| 790 | case STATUS_UNINITIALIZED: |
| 791 | case STATUS_IDLE: |
| 792 | ALOGV("%s: Already idle", __FUNCTION__); |
| 793 | return OK; |
| 794 | case STATUS_ERROR: |
| 795 | case STATUS_ACTIVE: |
| 796 | // Need to shut down |
| 797 | break; |
| 798 | default: |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 799 | SET_ERR_L("Unexpected status: %d",mStatus); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 800 | return INVALID_OPERATION; |
| 801 | } |
| 802 | |
| 803 | if (mRequestThread != NULL) { |
| 804 | res = mRequestThread->waitUntilPaused(kShutdownTimeout); |
| 805 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 806 | SET_ERR_L("Can't stop request thread in %f seconds!", |
| 807 | kShutdownTimeout/1e9); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 808 | return res; |
| 809 | } |
| 810 | } |
| 811 | if (mInputStream != NULL) { |
| 812 | res = mInputStream->waitUntilIdle(kShutdownTimeout); |
| 813 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 814 | SET_ERR_L("Can't idle input stream %d in %f seconds!", |
| 815 | mInputStream->getId(), kShutdownTimeout/1e9); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 816 | return res; |
| 817 | } |
| 818 | } |
| 819 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
| 820 | res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout); |
| 821 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 822 | SET_ERR_L("Can't idle output stream %d in %f seconds!", |
| 823 | mOutputStreams.keyAt(i), kShutdownTimeout/1e9); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 824 | return res; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (mStatus != STATUS_ERROR) { |
| 829 | mStatus = STATUS_IDLE; |
| 830 | } |
| 831 | |
| 832 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | status_t Camera3Device::setNotifyCallback(NotificationListener *listener) { |
| 836 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 837 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 838 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 839 | if (listener != NULL && mListener != NULL) { |
| 840 | ALOGW("%s: Replacing old callback listener", __FUNCTION__); |
| 841 | } |
| 842 | mListener = listener; |
| 843 | |
| 844 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | status_t Camera3Device::waitForNextFrame(nsecs_t timeout) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 848 | ATRACE_CALL(); |
| 849 | status_t res; |
| 850 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 851 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 852 | while (mResultQueue.empty()) { |
| 853 | res = mResultSignal.waitRelative(mOutputLock, timeout); |
| 854 | if (res == TIMED_OUT) { |
| 855 | return res; |
| 856 | } else if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 857 | ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)", |
| 858 | __FUNCTION__, mId, timeout, strerror(-res), res); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 859 | return res; |
| 860 | } |
| 861 | } |
| 862 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | status_t Camera3Device::getNextFrame(CameraMetadata *frame) { |
| 866 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 867 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 868 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 869 | if (mResultQueue.empty()) { |
| 870 | return NOT_ENOUGH_DATA; |
| 871 | } |
| 872 | |
| 873 | CameraMetadata &result = *(mResultQueue.begin()); |
| 874 | frame->acquire(result); |
| 875 | mResultQueue.erase(mResultQueue.begin()); |
| 876 | |
| 877 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | status_t Camera3Device::triggerAutofocus(uint32_t id) { |
| 881 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 882 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 883 | ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id); |
| 884 | // Mix-in this trigger into the next request and only the next request. |
| 885 | RequestTrigger trigger[] = { |
| 886 | { |
| 887 | ANDROID_CONTROL_AF_TRIGGER, |
| 888 | ANDROID_CONTROL_AF_TRIGGER_START |
| 889 | }, |
| 890 | { |
| 891 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 892 | static_cast<int32_t>(id) |
| 893 | }, |
| 894 | }; |
| 895 | |
| 896 | return mRequestThread->queueTrigger(trigger, |
| 897 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | status_t Camera3Device::triggerCancelAutofocus(uint32_t id) { |
| 901 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 902 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 903 | ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id); |
| 904 | // Mix-in this trigger into the next request and only the next request. |
| 905 | RequestTrigger trigger[] = { |
| 906 | { |
| 907 | ANDROID_CONTROL_AF_TRIGGER, |
| 908 | ANDROID_CONTROL_AF_TRIGGER_CANCEL |
| 909 | }, |
| 910 | { |
| 911 | ANDROID_CONTROL_AF_TRIGGER_ID, |
| 912 | static_cast<int32_t>(id) |
| 913 | }, |
| 914 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 915 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 916 | return mRequestThread->queueTrigger(trigger, |
| 917 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) { |
| 921 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 922 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 923 | ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id); |
| 924 | // Mix-in this trigger into the next request and only the next request. |
| 925 | RequestTrigger trigger[] = { |
| 926 | { |
| 927 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, |
| 928 | ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START |
| 929 | }, |
| 930 | { |
| 931 | ANDROID_CONTROL_AE_PRECAPTURE_ID, |
| 932 | static_cast<int32_t>(id) |
| 933 | }, |
| 934 | }; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 935 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 936 | return mRequestThread->queueTrigger(trigger, |
| 937 | sizeof(trigger)/sizeof(trigger[0])); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId, |
| 941 | buffer_handle_t *buffer, wp<BufferReleasedListener> listener) { |
| 942 | ATRACE_CALL(); |
| 943 | (void)reprocessStreamId; (void)buffer; (void)listener; |
| 944 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 945 | CLOGE("Unimplemented"); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 946 | return INVALID_OPERATION; |
| 947 | } |
| 948 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 949 | /** |
| 950 | * Camera3Device private methods |
| 951 | */ |
| 952 | |
| 953 | sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest( |
| 954 | const CameraMetadata &request) { |
| 955 | ATRACE_CALL(); |
| 956 | status_t res; |
| 957 | |
| 958 | sp<CaptureRequest> newRequest = new CaptureRequest; |
| 959 | newRequest->mSettings = request; |
| 960 | |
| 961 | camera_metadata_entry_t inputStreams = |
| 962 | newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS); |
| 963 | if (inputStreams.count > 0) { |
| 964 | if (mInputStream == NULL || |
| 965 | mInputStream->getId() != inputStreams.data.u8[0]) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 966 | CLOGE("Request references unknown input stream %d", |
| 967 | inputStreams.data.u8[0]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 968 | return NULL; |
| 969 | } |
| 970 | // Lazy completion of stream configuration (allocation/registration) |
| 971 | // on first use |
| 972 | if (mInputStream->isConfiguring()) { |
| 973 | res = mInputStream->finishConfiguration(mHal3Device); |
| 974 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 975 | SET_ERR_L("Unable to finish configuring input stream %d:" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 976 | " %s (%d)", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 977 | mInputStream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 978 | return NULL; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | newRequest->mInputStream = mInputStream; |
| 983 | newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS); |
| 984 | } |
| 985 | |
| 986 | camera_metadata_entry_t streams = |
| 987 | newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 988 | if (streams.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 989 | CLOGE("Zero output streams specified!"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 990 | return NULL; |
| 991 | } |
| 992 | |
| 993 | for (size_t i = 0; i < streams.count; i++) { |
| 994 | int idx = mOutputStreams.indexOfKey(streams.data.u8[i]); |
| 995 | if (idx == NAME_NOT_FOUND) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 996 | CLOGE("Request references unknown stream %d", |
| 997 | streams.data.u8[i]); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 998 | return NULL; |
| 999 | } |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1000 | sp<Camera3OutputStreamInterface> stream = |
| 1001 | mOutputStreams.editValueAt(idx); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1002 | |
| 1003 | // Lazy completion of stream configuration (allocation/registration) |
| 1004 | // on first use |
| 1005 | if (stream->isConfiguring()) { |
| 1006 | res = stream->finishConfiguration(mHal3Device); |
| 1007 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1008 | SET_ERR_L("Unable to finish configuring stream %d: %s (%d)", |
| 1009 | stream->getId(), strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1010 | return NULL; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | newRequest->mOutputStreams.push(stream); |
| 1015 | } |
| 1016 | newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS); |
| 1017 | |
| 1018 | return newRequest; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1021 | status_t Camera3Device::configureStreamsLocked() { |
| 1022 | ATRACE_CALL(); |
| 1023 | status_t res; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1024 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1025 | if (mStatus != STATUS_IDLE) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1026 | CLOGE("Not idle"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1027 | return INVALID_OPERATION; |
| 1028 | } |
| 1029 | |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1030 | if (!mNeedConfig) { |
| 1031 | ALOGV("%s: Skipping config, no stream changes", __FUNCTION__); |
Eino-Ville Talvala | 31fdb29 | 2013-06-12 17:06:41 -0700 | [diff] [blame] | 1032 | mStatus = STATUS_ACTIVE; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1033 | return OK; |
| 1034 | } |
| 1035 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1036 | // Start configuring the streams |
| 1037 | |
| 1038 | camera3_stream_configuration config; |
| 1039 | |
| 1040 | config.num_streams = (mInputStream != NULL) + mOutputStreams.size(); |
| 1041 | |
| 1042 | Vector<camera3_stream_t*> streams; |
| 1043 | streams.setCapacity(config.num_streams); |
| 1044 | |
| 1045 | if (mInputStream != NULL) { |
| 1046 | camera3_stream_t *inputStream; |
| 1047 | inputStream = mInputStream->startConfiguration(); |
| 1048 | if (inputStream == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1049 | SET_ERR_L("Can't start input stream configuration"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1050 | return INVALID_OPERATION; |
| 1051 | } |
| 1052 | streams.add(inputStream); |
| 1053 | } |
| 1054 | |
| 1055 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 2fba584 | 2013-04-22 14:03:54 -0700 | [diff] [blame] | 1056 | |
| 1057 | // Don't configure bidi streams twice, nor add them twice to the list |
| 1058 | if (mOutputStreams[i].get() == |
| 1059 | static_cast<Camera3StreamInterface*>(mInputStream.get())) { |
| 1060 | |
| 1061 | config.num_streams--; |
| 1062 | continue; |
| 1063 | } |
| 1064 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1065 | camera3_stream_t *outputStream; |
| 1066 | outputStream = mOutputStreams.editValueAt(i)->startConfiguration(); |
| 1067 | if (outputStream == NULL) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1068 | SET_ERR_L("Can't start output stream configuration"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1069 | return INVALID_OPERATION; |
| 1070 | } |
| 1071 | streams.add(outputStream); |
| 1072 | } |
| 1073 | |
| 1074 | config.streams = streams.editArray(); |
| 1075 | |
| 1076 | // Do the HAL configuration; will potentially touch stream |
| 1077 | // max_buffers, usage, priv fields. |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1078 | ATRACE_BEGIN("camera3->configure_streams"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1079 | res = mHal3Device->ops->configure_streams(mHal3Device, &config); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1080 | ATRACE_END(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1081 | |
| 1082 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1083 | SET_ERR_L("Unable to configure streams with HAL: %s (%d)", |
| 1084 | strerror(-res), res); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1085 | return res; |
| 1086 | } |
| 1087 | |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1088 | // Finish all stream configuration immediately. |
| 1089 | // TODO: Try to relax this later back to lazy completion, which should be |
| 1090 | // faster |
| 1091 | |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1092 | if (mInputStream != NULL && mInputStream->isConfiguring()) { |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1093 | res = mInputStream->finishConfiguration(mHal3Device); |
| 1094 | if (res != OK) { |
| 1095 | SET_ERR_L("Can't finish configuring input stream %d: %s (%d)", |
| 1096 | mInputStream->getId(), strerror(-res), res); |
| 1097 | return res; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | for (size_t i = 0; i < mOutputStreams.size(); i++) { |
Igor Murashkin | 073f857 | 2013-05-02 14:59:28 -0700 | [diff] [blame] | 1102 | sp<Camera3OutputStreamInterface> outputStream = |
| 1103 | mOutputStreams.editValueAt(i); |
| 1104 | if (outputStream->isConfiguring()) { |
| 1105 | res = outputStream->finishConfiguration(mHal3Device); |
| 1106 | if (res != OK) { |
| 1107 | SET_ERR_L("Can't finish configuring output stream %d: %s (%d)", |
| 1108 | outputStream->getId(), strerror(-res), res); |
| 1109 | return res; |
| 1110 | } |
Eino-Ville Talvala | 4c95676 | 2013-04-19 17:26:13 -0700 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1114 | // Request thread needs to know to avoid using repeat-last-settings protocol |
| 1115 | // across configure_streams() calls |
| 1116 | mRequestThread->configurationComplete(); |
| 1117 | |
| 1118 | // Finish configuring the streams lazily on first reference |
| 1119 | |
| 1120 | mStatus = STATUS_ACTIVE; |
Eino-Ville Talvala | ea26c77 | 2013-06-11 16:04:06 -0700 | [diff] [blame] | 1121 | mNeedConfig = false; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1122 | |
| 1123 | return OK; |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1126 | void Camera3Device::setErrorState(const char *fmt, ...) { |
| 1127 | Mutex::Autolock l(mLock); |
| 1128 | va_list args; |
| 1129 | va_start(args, fmt); |
| 1130 | |
| 1131 | setErrorStateLockedV(fmt, args); |
| 1132 | |
| 1133 | va_end(args); |
| 1134 | } |
| 1135 | |
| 1136 | void Camera3Device::setErrorStateV(const char *fmt, va_list args) { |
| 1137 | Mutex::Autolock l(mLock); |
| 1138 | setErrorStateLockedV(fmt, args); |
| 1139 | } |
| 1140 | |
| 1141 | void Camera3Device::setErrorStateLocked(const char *fmt, ...) { |
| 1142 | va_list args; |
| 1143 | va_start(args, fmt); |
| 1144 | |
| 1145 | setErrorStateLockedV(fmt, args); |
| 1146 | |
| 1147 | va_end(args); |
| 1148 | } |
| 1149 | |
| 1150 | void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1151 | // Print out all error messages to log |
| 1152 | String8 errorCause = String8::formatV(fmt, args); |
| 1153 | ALOGE("Camera %d: %s", mId, errorCause.string()); |
| 1154 | |
| 1155 | // But only do error state transition steps for the first error |
Zhijun He | b05eeae | 2013-06-06 13:51:22 -0700 | [diff] [blame] | 1156 | if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return; |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1157 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1158 | mErrorCause = errorCause; |
| 1159 | |
| 1160 | mRequestThread->setPaused(true); |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1161 | mStatus = STATUS_ERROR; |
| 1162 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1163 | |
| 1164 | /** |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1165 | * In-flight request management |
| 1166 | */ |
| 1167 | |
| 1168 | status_t Camera3Device::registerInFlight(int32_t frameNumber, |
| 1169 | int32_t numBuffers) { |
| 1170 | ATRACE_CALL(); |
| 1171 | Mutex::Autolock l(mInFlightLock); |
| 1172 | |
| 1173 | ssize_t res; |
| 1174 | res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers)); |
| 1175 | if (res < 0) return res; |
| 1176 | |
| 1177 | return OK; |
| 1178 | } |
| 1179 | |
| 1180 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1181 | * Camera HAL device callback methods |
| 1182 | */ |
| 1183 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1184 | void Camera3Device::processCaptureResult(const camera3_capture_result *result) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1185 | ATRACE_CALL(); |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1186 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1187 | status_t res; |
| 1188 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1189 | uint32_t frameNumber = result->frame_number; |
| 1190 | if (result->result == NULL && result->num_output_buffers == 0) { |
| 1191 | SET_ERR("No result data provided by HAL for frame %d", |
| 1192 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1193 | return; |
| 1194 | } |
| 1195 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1196 | // Get capture timestamp from list of in-flight requests, where it was added |
| 1197 | // by the shutter notification for this frame. Then update the in-flight |
| 1198 | // status and remove the in-flight entry if all result data has been |
| 1199 | // received. |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1200 | nsecs_t timestamp = 0; |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1201 | { |
| 1202 | Mutex::Autolock l(mInFlightLock); |
| 1203 | ssize_t idx = mInFlightMap.indexOfKey(frameNumber); |
| 1204 | if (idx == NAME_NOT_FOUND) { |
| 1205 | SET_ERR("Unknown frame number for capture result: %d", |
| 1206 | frameNumber); |
| 1207 | return; |
| 1208 | } |
| 1209 | InFlightRequest &request = mInFlightMap.editValueAt(idx); |
| 1210 | timestamp = request.captureTimestamp; |
| 1211 | if (timestamp == 0) { |
| 1212 | SET_ERR("Called before shutter notify for frame %d", |
| 1213 | frameNumber); |
| 1214 | return; |
| 1215 | } |
| 1216 | |
| 1217 | if (result->result != NULL) { |
| 1218 | if (request.haveResultMetadata) { |
| 1219 | SET_ERR("Called multiple times with metadata for frame %d", |
| 1220 | frameNumber); |
| 1221 | return; |
| 1222 | } |
| 1223 | request.haveResultMetadata = true; |
| 1224 | } |
| 1225 | |
| 1226 | request.numBuffersLeft -= result->num_output_buffers; |
| 1227 | |
| 1228 | if (request.numBuffersLeft < 0) { |
| 1229 | SET_ERR("Too many buffers returned for frame %d", |
| 1230 | frameNumber); |
| 1231 | return; |
| 1232 | } |
| 1233 | |
| 1234 | if (request.haveResultMetadata && request.numBuffersLeft == 0) { |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1235 | ATRACE_ASYNC_END("frame capture", frameNumber); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1236 | mInFlightMap.removeItemsAt(idx, 1); |
| 1237 | } |
| 1238 | |
| 1239 | // Sanity check - if we have too many in-flight frames, something has |
| 1240 | // likely gone wrong |
| 1241 | if (mInFlightMap.size() > kInFlightWarnLimit) { |
| 1242 | CLOGE("In-flight list too large: %d", mInFlightMap.size()); |
| 1243 | } |
| 1244 | |
| 1245 | } |
| 1246 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1247 | AlgState cur3aState; |
| 1248 | AlgState new3aState; |
| 1249 | int32_t aeTriggerId = 0; |
| 1250 | int32_t afTriggerId = 0; |
| 1251 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1252 | NotificationListener *listener = NULL; |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1253 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1254 | // Process the result metadata, if provided |
| 1255 | if (result->result != NULL) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1256 | Mutex::Autolock l(mOutputLock); |
| 1257 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1258 | if (frameNumber != mNextResultFrameNumber) { |
| 1259 | SET_ERR("Out-of-order capture result metadata submitted! " |
| 1260 | "(got frame number %d, expecting %d)", |
| 1261 | frameNumber, mNextResultFrameNumber); |
| 1262 | return; |
| 1263 | } |
| 1264 | mNextResultFrameNumber++; |
| 1265 | |
| 1266 | CameraMetadata &captureResult = |
| 1267 | *mResultQueue.insert(mResultQueue.end(), CameraMetadata()); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1268 | |
| 1269 | captureResult = result->result; |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 1270 | if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT, |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1271 | (int32_t*)&frameNumber, 1) != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1272 | SET_ERR("Failed to set frame# in metadata (%d)", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1273 | frameNumber); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 1274 | } else { |
| 1275 | ALOGVV("%s: Camera %d: Set frame# in metadata (%d)", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1276 | __FUNCTION__, mId, frameNumber); |
Igor Murashkin | d2c9069 | 2013-04-02 12:32:32 -0700 | [diff] [blame] | 1277 | } |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1278 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1279 | // Check that there's a timestamp in the result metadata |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1280 | |
| 1281 | camera_metadata_entry entry = |
| 1282 | captureResult.find(ANDROID_SENSOR_TIMESTAMP); |
| 1283 | if (entry.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1284 | SET_ERR("No timestamp provided by HAL for frame %d!", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1285 | frameNumber); |
Alex Ray | fe7e0c6 | 2013-05-30 00:12:13 -0700 | [diff] [blame] | 1286 | } else if (timestamp != entry.data.i64[0]) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1287 | SET_ERR("Timestamp mismatch between shutter notify and result" |
| 1288 | " metadata for frame %d (%lld vs %lld respectively)", |
| 1289 | frameNumber, timestamp, entry.data.i64[0]); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | // Get 3A states from result metadata |
| 1293 | |
| 1294 | entry = captureResult.find(ANDROID_CONTROL_AE_STATE); |
| 1295 | if (entry.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1296 | CLOGE("No AE state provided by HAL for frame %d!", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1297 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1298 | } else { |
| 1299 | new3aState.aeState = |
| 1300 | static_cast<camera_metadata_enum_android_control_ae_state>( |
| 1301 | entry.data.u8[0]); |
| 1302 | } |
| 1303 | |
| 1304 | entry = captureResult.find(ANDROID_CONTROL_AF_STATE); |
| 1305 | if (entry.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1306 | CLOGE("No AF state provided by HAL for frame %d!", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1307 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1308 | } else { |
| 1309 | new3aState.afState = |
| 1310 | static_cast<camera_metadata_enum_android_control_af_state>( |
| 1311 | entry.data.u8[0]); |
| 1312 | } |
| 1313 | |
| 1314 | entry = captureResult.find(ANDROID_CONTROL_AWB_STATE); |
| 1315 | if (entry.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1316 | CLOGE("No AWB state provided by HAL for frame %d!", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1317 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1318 | } else { |
| 1319 | new3aState.awbState = |
| 1320 | static_cast<camera_metadata_enum_android_control_awb_state>( |
| 1321 | entry.data.u8[0]); |
| 1322 | } |
| 1323 | |
| 1324 | entry = captureResult.find(ANDROID_CONTROL_AF_TRIGGER_ID); |
| 1325 | if (entry.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1326 | CLOGE("No AF trigger ID provided by HAL for frame %d!", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1327 | frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1328 | } else { |
| 1329 | afTriggerId = entry.data.i32[0]; |
| 1330 | } |
| 1331 | |
| 1332 | entry = captureResult.find(ANDROID_CONTROL_AE_PRECAPTURE_ID); |
| 1333 | if (entry.count == 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1334 | CLOGE("No AE precapture trigger ID provided by HAL" |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1335 | " for frame %d!", frameNumber); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1336 | } else { |
| 1337 | aeTriggerId = entry.data.i32[0]; |
| 1338 | } |
| 1339 | |
| 1340 | listener = mListener; |
| 1341 | cur3aState = m3AState; |
| 1342 | |
| 1343 | m3AState = new3aState; |
| 1344 | } // scope for mOutputLock |
| 1345 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1346 | // Return completed buffers to their streams with the timestamp |
| 1347 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1348 | for (size_t i = 0; i < result->num_output_buffers; i++) { |
| 1349 | Camera3Stream *stream = |
| 1350 | Camera3Stream::cast(result->output_buffers[i].stream); |
| 1351 | res = stream->returnBuffer(result->output_buffers[i], timestamp); |
| 1352 | // Note: stream may be deallocated at this point, if this buffer was the |
| 1353 | // last reference to it. |
| 1354 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1355 | SET_ERR("Can't return buffer %d for frame %d to its stream: " |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1356 | " %s (%d)", i, frameNumber, strerror(-res), res); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1360 | // Finally, dispatch any 3A change events to listeners if we got metadata |
| 1361 | |
Igor Murashkin | 4345d5b | 2013-05-17 14:39:53 -0700 | [diff] [blame] | 1362 | if (result->result != NULL) { |
| 1363 | mResultSignal.signal(); |
| 1364 | } |
| 1365 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1366 | if (result->result != NULL && listener != NULL) { |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1367 | if (new3aState.aeState != cur3aState.aeState) { |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1368 | ALOGVV("%s: AE state changed from 0x%x to 0x%x", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1369 | __FUNCTION__, cur3aState.aeState, new3aState.aeState); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1370 | listener->notifyAutoExposure(new3aState.aeState, aeTriggerId); |
| 1371 | } |
| 1372 | if (new3aState.afState != cur3aState.afState) { |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1373 | ALOGVV("%s: AF state changed from 0x%x to 0x%x", |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1374 | __FUNCTION__, cur3aState.afState, new3aState.afState); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1375 | listener->notifyAutoFocus(new3aState.afState, afTriggerId); |
| 1376 | } |
| 1377 | if (new3aState.awbState != cur3aState.awbState) { |
| 1378 | listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId); |
| 1379 | } |
| 1380 | } |
| 1381 | |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | void Camera3Device::notify(const camera3_notify_msg *msg) { |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1385 | ATRACE_CALL(); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1386 | NotificationListener *listener; |
| 1387 | { |
| 1388 | Mutex::Autolock l(mOutputLock); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1389 | listener = mListener; |
| 1390 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1391 | |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1392 | if (msg == NULL) { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1393 | SET_ERR("HAL sent NULL notify message!"); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | switch (msg->type) { |
| 1398 | case CAMERA3_MSG_ERROR: { |
| 1399 | int streamId = 0; |
| 1400 | if (msg->message.error.error_stream != NULL) { |
| 1401 | Camera3Stream *stream = |
| 1402 | Camera3Stream::cast( |
| 1403 | msg->message.error.error_stream); |
| 1404 | streamId = stream->getId(); |
| 1405 | } |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1406 | ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d", |
| 1407 | mId, __FUNCTION__, msg->message.error.frame_number, |
| 1408 | streamId, msg->message.error.error_code); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1409 | if (listener != NULL) { |
| 1410 | listener->notifyError(msg->message.error.error_code, |
| 1411 | msg->message.error.frame_number, streamId); |
| 1412 | } |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1413 | break; |
| 1414 | } |
| 1415 | case CAMERA3_MSG_SHUTTER: { |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1416 | ssize_t idx; |
| 1417 | uint32_t frameNumber = msg->message.shutter.frame_number; |
| 1418 | nsecs_t timestamp = msg->message.shutter.timestamp; |
| 1419 | // Verify ordering of shutter notifications |
| 1420 | { |
| 1421 | Mutex::Autolock l(mOutputLock); |
| 1422 | if (frameNumber != mNextShutterFrameNumber) { |
| 1423 | SET_ERR("Shutter notification out-of-order. Expected " |
| 1424 | "notification for frame %d, got frame %d", |
| 1425 | mNextShutterFrameNumber, frameNumber); |
| 1426 | break; |
| 1427 | } |
| 1428 | mNextShutterFrameNumber++; |
| 1429 | } |
| 1430 | |
| 1431 | // Set timestamp for the request in the in-flight tracking |
| 1432 | { |
| 1433 | Mutex::Autolock l(mInFlightLock); |
| 1434 | idx = mInFlightMap.indexOfKey(frameNumber); |
| 1435 | if (idx >= 0) { |
| 1436 | mInFlightMap.editValueAt(idx).captureTimestamp = timestamp; |
| 1437 | } |
| 1438 | } |
| 1439 | if (idx < 0) { |
| 1440 | SET_ERR("Shutter notification for non-existent frame number %d", |
| 1441 | frameNumber); |
| 1442 | break; |
| 1443 | } |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1444 | ALOGVV("Camera %d: %s: Shutter fired for frame %d at %lld", |
| 1445 | mId, __FUNCTION__, frameNumber, timestamp); |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1446 | // Call listener, if any |
| 1447 | if (listener != NULL) { |
| 1448 | listener->notifyShutter(frameNumber, timestamp); |
| 1449 | } |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1450 | break; |
| 1451 | } |
| 1452 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1453 | SET_ERR("Unknown notify message from HAL: %d", |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1454 | msg->type); |
Eino-Ville Talvala | 7d346fa | 2013-03-11 14:13:50 -0700 | [diff] [blame] | 1455 | } |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /** |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1459 | * RequestThread inner class methods |
| 1460 | */ |
| 1461 | |
| 1462 | Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent, |
| 1463 | camera3_device_t *hal3Device) : |
| 1464 | Thread(false), |
| 1465 | mParent(parent), |
| 1466 | mHal3Device(hal3Device), |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1467 | mId(getId(parent)), |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1468 | mReconfigured(false), |
| 1469 | mDoPause(false), |
| 1470 | mPaused(true), |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1471 | mFrameNumber(0), |
| 1472 | mLatestRequestId(NAME_NOT_FOUND) { |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | void Camera3Device::RequestThread::configurationComplete() { |
| 1476 | Mutex::Autolock l(mRequestLock); |
| 1477 | mReconfigured = true; |
| 1478 | } |
| 1479 | |
| 1480 | status_t Camera3Device::RequestThread::queueRequest( |
| 1481 | sp<CaptureRequest> request) { |
| 1482 | Mutex::Autolock l(mRequestLock); |
| 1483 | mRequestQueue.push_back(request); |
| 1484 | |
| 1485 | return OK; |
| 1486 | } |
| 1487 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1488 | |
| 1489 | status_t Camera3Device::RequestThread::queueTrigger( |
| 1490 | RequestTrigger trigger[], |
| 1491 | size_t count) { |
| 1492 | |
| 1493 | Mutex::Autolock l(mTriggerMutex); |
| 1494 | status_t ret; |
| 1495 | |
| 1496 | for (size_t i = 0; i < count; ++i) { |
| 1497 | ret = queueTriggerLocked(trigger[i]); |
| 1498 | |
| 1499 | if (ret != OK) { |
| 1500 | return ret; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | return OK; |
| 1505 | } |
| 1506 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1507 | int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) { |
| 1508 | sp<Camera3Device> d = device.promote(); |
| 1509 | if (d != NULL) return d->mId; |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1513 | status_t Camera3Device::RequestThread::queueTriggerLocked( |
| 1514 | RequestTrigger trigger) { |
| 1515 | |
| 1516 | uint32_t tag = trigger.metadataTag; |
| 1517 | ssize_t index = mTriggerMap.indexOfKey(tag); |
| 1518 | |
| 1519 | switch (trigger.getTagType()) { |
| 1520 | case TYPE_BYTE: |
| 1521 | // fall-through |
| 1522 | case TYPE_INT32: |
| 1523 | break; |
| 1524 | default: |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1525 | ALOGE("%s: Type not supported: 0x%x", __FUNCTION__, |
| 1526 | trigger.getTagType()); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1527 | return INVALID_OPERATION; |
| 1528 | } |
| 1529 | |
| 1530 | /** |
| 1531 | * Collect only the latest trigger, since we only have 1 field |
| 1532 | * in the request settings per trigger tag, and can't send more than 1 |
| 1533 | * trigger per request. |
| 1534 | */ |
| 1535 | if (index != NAME_NOT_FOUND) { |
| 1536 | mTriggerMap.editValueAt(index) = trigger; |
| 1537 | } else { |
| 1538 | mTriggerMap.add(tag, trigger); |
| 1539 | } |
| 1540 | |
| 1541 | return OK; |
| 1542 | } |
| 1543 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1544 | status_t Camera3Device::RequestThread::setRepeatingRequests( |
| 1545 | const RequestList &requests) { |
| 1546 | Mutex::Autolock l(mRequestLock); |
| 1547 | mRepeatingRequests.clear(); |
| 1548 | mRepeatingRequests.insert(mRepeatingRequests.begin(), |
| 1549 | requests.begin(), requests.end()); |
| 1550 | return OK; |
| 1551 | } |
| 1552 | |
| 1553 | status_t Camera3Device::RequestThread::clearRepeatingRequests() { |
| 1554 | Mutex::Autolock l(mRequestLock); |
| 1555 | mRepeatingRequests.clear(); |
| 1556 | return OK; |
| 1557 | } |
| 1558 | |
| 1559 | void Camera3Device::RequestThread::setPaused(bool paused) { |
| 1560 | Mutex::Autolock l(mPauseLock); |
| 1561 | mDoPause = paused; |
| 1562 | mDoPauseSignal.signal(); |
| 1563 | } |
| 1564 | |
| 1565 | status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) { |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1566 | ATRACE_CALL(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1567 | status_t res; |
| 1568 | Mutex::Autolock l(mPauseLock); |
| 1569 | while (!mPaused) { |
| 1570 | res = mPausedSignal.waitRelative(mPauseLock, timeout); |
| 1571 | if (res == TIMED_OUT) { |
| 1572 | return res; |
| 1573 | } |
| 1574 | } |
| 1575 | return OK; |
| 1576 | } |
| 1577 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1578 | status_t Camera3Device::RequestThread::waitUntilRequestProcessed( |
| 1579 | int32_t requestId, nsecs_t timeout) { |
| 1580 | Mutex::Autolock l(mLatestRequestMutex); |
| 1581 | status_t res; |
| 1582 | while (mLatestRequestId != requestId) { |
| 1583 | nsecs_t startTime = systemTime(); |
| 1584 | |
| 1585 | res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout); |
| 1586 | if (res != OK) return res; |
| 1587 | |
| 1588 | timeout -= (systemTime() - startTime); |
| 1589 | } |
| 1590 | |
| 1591 | return OK; |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1596 | bool Camera3Device::RequestThread::threadLoop() { |
| 1597 | |
| 1598 | status_t res; |
| 1599 | |
| 1600 | // Handle paused state. |
| 1601 | if (waitIfPaused()) { |
| 1602 | return true; |
| 1603 | } |
| 1604 | |
| 1605 | // Get work to do |
| 1606 | |
| 1607 | sp<CaptureRequest> nextRequest = waitForNextRequest(); |
| 1608 | if (nextRequest == NULL) { |
| 1609 | return true; |
| 1610 | } |
| 1611 | |
| 1612 | // Create request to HAL |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1613 | camera3_capture_request_t request = camera3_capture_request_t(); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1614 | Vector<camera3_stream_buffer_t> outputBuffers; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1615 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1616 | // Insert any queued triggers (before metadata is locked) |
| 1617 | int32_t triggerCount; |
| 1618 | res = insertTriggers(nextRequest); |
| 1619 | if (res < 0) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1620 | SET_ERR("RequestThread: Unable to insert triggers " |
| 1621 | "(capture request %d, HAL device: %s (%d)", |
| 1622 | (mFrameNumber+1), strerror(-res), res); |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1623 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 1624 | return false; |
| 1625 | } |
| 1626 | triggerCount = res; |
| 1627 | |
| 1628 | bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0); |
| 1629 | |
| 1630 | // If the request is the same as last, or we had triggers last time |
| 1631 | if (mPrevRequest != nextRequest || triggersMixedIn) { |
| 1632 | /** |
| 1633 | * The request should be presorted so accesses in HAL |
| 1634 | * are O(logn). Sidenote, sorting a sorted metadata is nop. |
| 1635 | */ |
| 1636 | nextRequest->mSettings.sort(); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1637 | request.settings = nextRequest->mSettings.getAndLock(); |
| 1638 | mPrevRequest = nextRequest; |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1639 | ALOGVV("%s: Request settings are NEW", __FUNCTION__); |
| 1640 | |
| 1641 | IF_ALOGV() { |
| 1642 | camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t(); |
| 1643 | find_camera_metadata_ro_entry( |
| 1644 | request.settings, |
| 1645 | ANDROID_CONTROL_AF_TRIGGER, |
| 1646 | &e |
| 1647 | ); |
| 1648 | if (e.count > 0) { |
| 1649 | ALOGV("%s: Request (frame num %d) had AF trigger 0x%x", |
| 1650 | __FUNCTION__, |
| 1651 | mFrameNumber+1, |
| 1652 | e.data.u8[0]); |
| 1653 | } |
| 1654 | } |
| 1655 | } else { |
| 1656 | // leave request.settings NULL to indicate 'reuse latest given' |
| 1657 | ALOGVV("%s: Request settings are REUSED", |
| 1658 | __FUNCTION__); |
| 1659 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1660 | |
| 1661 | camera3_stream_buffer_t inputBuffer; |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1662 | |
| 1663 | // Fill in buffers |
| 1664 | |
| 1665 | if (nextRequest->mInputStream != NULL) { |
| 1666 | request.input_buffer = &inputBuffer; |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1667 | res = nextRequest->mInputStream->getInputBuffer(&inputBuffer); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1668 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1669 | SET_ERR("RequestThread: Can't get input buffer, skipping request:" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1670 | " %s (%d)", strerror(-res), res); |
| 1671 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 1672 | return true; |
| 1673 | } |
| 1674 | } else { |
| 1675 | request.input_buffer = NULL; |
| 1676 | } |
| 1677 | |
| 1678 | outputBuffers.insertAt(camera3_stream_buffer_t(), 0, |
| 1679 | nextRequest->mOutputStreams.size()); |
| 1680 | request.output_buffers = outputBuffers.array(); |
| 1681 | for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) { |
| 1682 | res = nextRequest->mOutputStreams.editItemAt(i)-> |
| 1683 | getBuffer(&outputBuffers.editItemAt(i)); |
| 1684 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1685 | SET_ERR("RequestThread: Can't get output buffer, skipping request:" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1686 | "%s (%d)", strerror(-res), res); |
| 1687 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 1688 | return true; |
| 1689 | } |
| 1690 | request.num_output_buffers++; |
| 1691 | } |
| 1692 | |
| 1693 | request.frame_number = mFrameNumber++; |
| 1694 | |
Eino-Ville Talvala | 42368d9 | 2013-04-09 14:13:50 -0700 | [diff] [blame] | 1695 | // Log request in the in-flight queue |
| 1696 | sp<Camera3Device> parent = mParent.promote(); |
| 1697 | if (parent == NULL) { |
| 1698 | CLOGE("RequestThread: Parent is gone"); |
| 1699 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 1700 | return false; |
| 1701 | } |
| 1702 | |
| 1703 | res = parent->registerInFlight(request.frame_number, |
| 1704 | request.num_output_buffers); |
| 1705 | if (res != OK) { |
| 1706 | SET_ERR("RequestThread: Unable to register new in-flight request:" |
| 1707 | " %s (%d)", strerror(-res), res); |
| 1708 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 1709 | return false; |
| 1710 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1711 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1712 | // Submit request and block until ready for next one |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1713 | ATRACE_ASYNC_BEGIN("frame capture", request.frame_number); |
| 1714 | ATRACE_BEGIN("camera3->process_capture_request"); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1715 | res = mHal3Device->ops->process_capture_request(mHal3Device, &request); |
Eino-Ville Talvala | 17a61ad | 2013-06-03 16:53:32 -0700 | [diff] [blame] | 1716 | ATRACE_END(); |
| 1717 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1718 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1719 | SET_ERR("RequestThread: Unable to submit capture request %d to HAL" |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1720 | " device: %s (%d)", request.frame_number, strerror(-res), res); |
| 1721 | cleanUpFailedRequest(request, nextRequest, outputBuffers); |
| 1722 | return false; |
| 1723 | } |
| 1724 | |
| 1725 | if (request.settings != NULL) { |
| 1726 | nextRequest->mSettings.unlock(request.settings); |
| 1727 | } |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1728 | |
| 1729 | // Remove any previously queued triggers (after unlock) |
| 1730 | res = removeTriggers(mPrevRequest); |
| 1731 | if (res != OK) { |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1732 | SET_ERR("RequestThread: Unable to remove triggers " |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1733 | "(capture request %d, HAL device: %s (%d)", |
| 1734 | request.frame_number, strerror(-res), res); |
| 1735 | return false; |
| 1736 | } |
| 1737 | mPrevTriggers = triggerCount; |
| 1738 | |
| 1739 | // Read android.request.id from the request settings metadata |
| 1740 | // - inform waitUntilRequestProcessed thread of a new request ID |
| 1741 | { |
| 1742 | Mutex::Autolock al(mLatestRequestMutex); |
| 1743 | |
| 1744 | camera_metadata_entry_t requestIdEntry = |
| 1745 | nextRequest->mSettings.find(ANDROID_REQUEST_ID); |
| 1746 | if (requestIdEntry.count > 0) { |
| 1747 | mLatestRequestId = requestIdEntry.data.i32[0]; |
| 1748 | } else { |
| 1749 | ALOGW("%s: Did not have android.request.id set in the request", |
| 1750 | __FUNCTION__); |
| 1751 | mLatestRequestId = NAME_NOT_FOUND; |
| 1752 | } |
| 1753 | |
| 1754 | mLatestRequestSignal.signal(); |
| 1755 | } |
| 1756 | |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1757 | // Return input buffer back to framework |
| 1758 | if (request.input_buffer != NULL) { |
| 1759 | Camera3Stream *stream = |
| 1760 | Camera3Stream::cast(request.input_buffer->stream); |
| 1761 | res = stream->returnInputBuffer(*(request.input_buffer)); |
| 1762 | // Note: stream may be deallocated at this point, if this buffer was the |
| 1763 | // last reference to it. |
| 1764 | if (res != OK) { |
| 1765 | ALOGE("%s: RequestThread: Can't return input buffer for frame %d to" |
| 1766 | " its stream:%s (%d)", __FUNCTION__, |
| 1767 | request.frame_number, strerror(-res), res); |
| 1768 | // TODO: Report error upstream |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | |
| 1773 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1774 | return true; |
| 1775 | } |
| 1776 | |
| 1777 | void Camera3Device::RequestThread::cleanUpFailedRequest( |
| 1778 | camera3_capture_request_t &request, |
| 1779 | sp<CaptureRequest> &nextRequest, |
| 1780 | Vector<camera3_stream_buffer_t> &outputBuffers) { |
| 1781 | |
| 1782 | if (request.settings != NULL) { |
| 1783 | nextRequest->mSettings.unlock(request.settings); |
| 1784 | } |
| 1785 | if (request.input_buffer != NULL) { |
| 1786 | request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR; |
Igor Murashkin | 5a269fa | 2013-04-15 14:59:22 -0700 | [diff] [blame] | 1787 | nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer)); |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1788 | } |
| 1789 | for (size_t i = 0; i < request.num_output_buffers; i++) { |
| 1790 | outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR; |
| 1791 | nextRequest->mOutputStreams.editItemAt(i)->returnBuffer( |
| 1792 | outputBuffers[i], 0); |
| 1793 | } |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | sp<Camera3Device::CaptureRequest> |
| 1797 | Camera3Device::RequestThread::waitForNextRequest() { |
| 1798 | status_t res; |
| 1799 | sp<CaptureRequest> nextRequest; |
| 1800 | |
| 1801 | // Optimized a bit for the simple steady-state case (single repeating |
| 1802 | // request), to avoid putting that request in the queue temporarily. |
| 1803 | Mutex::Autolock l(mRequestLock); |
| 1804 | |
| 1805 | while (mRequestQueue.empty()) { |
| 1806 | if (!mRepeatingRequests.empty()) { |
| 1807 | // Always atomically enqueue all requests in a repeating request |
| 1808 | // list. Guarantees a complete in-sequence set of captures to |
| 1809 | // application. |
| 1810 | const RequestList &requests = mRepeatingRequests; |
| 1811 | RequestList::const_iterator firstRequest = |
| 1812 | requests.begin(); |
| 1813 | nextRequest = *firstRequest; |
| 1814 | mRequestQueue.insert(mRequestQueue.end(), |
| 1815 | ++firstRequest, |
| 1816 | requests.end()); |
| 1817 | // No need to wait any longer |
| 1818 | break; |
| 1819 | } |
| 1820 | |
| 1821 | res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout); |
| 1822 | |
| 1823 | if (res == TIMED_OUT) { |
| 1824 | // Signal that we're paused by starvation |
| 1825 | Mutex::Autolock pl(mPauseLock); |
| 1826 | if (mPaused == false) { |
| 1827 | mPaused = true; |
| 1828 | mPausedSignal.signal(); |
| 1829 | } |
| 1830 | // Stop waiting for now and let thread management happen |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | if (nextRequest == NULL) { |
| 1836 | // Don't have a repeating request already in hand, so queue |
| 1837 | // must have an entry now. |
| 1838 | RequestList::iterator firstRequest = |
| 1839 | mRequestQueue.begin(); |
| 1840 | nextRequest = *firstRequest; |
| 1841 | mRequestQueue.erase(firstRequest); |
| 1842 | } |
| 1843 | |
| 1844 | // Not paused |
| 1845 | Mutex::Autolock pl(mPauseLock); |
| 1846 | mPaused = false; |
| 1847 | |
| 1848 | // Check if we've reconfigured since last time, and reset the preview |
| 1849 | // request if so. Can't use 'NULL request == repeat' across configure calls. |
| 1850 | if (mReconfigured) { |
| 1851 | mPrevRequest.clear(); |
| 1852 | mReconfigured = false; |
| 1853 | } |
| 1854 | |
| 1855 | return nextRequest; |
| 1856 | } |
| 1857 | |
| 1858 | bool Camera3Device::RequestThread::waitIfPaused() { |
| 1859 | status_t res; |
| 1860 | Mutex::Autolock l(mPauseLock); |
| 1861 | while (mDoPause) { |
| 1862 | // Signal that we're paused by request |
| 1863 | if (mPaused == false) { |
| 1864 | mPaused = true; |
| 1865 | mPausedSignal.signal(); |
| 1866 | } |
| 1867 | res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout); |
| 1868 | if (res == TIMED_OUT) { |
| 1869 | return true; |
| 1870 | } |
| 1871 | } |
| 1872 | // We don't set mPaused to false here, because waitForNextRequest needs |
| 1873 | // to further manage the paused state in case of starvation. |
| 1874 | return false; |
| 1875 | } |
| 1876 | |
Eino-Ville Talvala | b2058d1 | 2013-04-09 13:49:56 -0700 | [diff] [blame] | 1877 | void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) { |
| 1878 | sp<Camera3Device> parent = mParent.promote(); |
| 1879 | if (parent != NULL) { |
| 1880 | va_list args; |
| 1881 | va_start(args, fmt); |
| 1882 | |
| 1883 | parent->setErrorStateV(fmt, args); |
| 1884 | |
| 1885 | va_end(args); |
| 1886 | } |
| 1887 | } |
| 1888 | |
Igor Murashkin | 4d2f2e8 | 2013-04-01 17:29:07 -0700 | [diff] [blame] | 1889 | status_t Camera3Device::RequestThread::insertTriggers( |
| 1890 | const sp<CaptureRequest> &request) { |
| 1891 | |
| 1892 | Mutex::Autolock al(mTriggerMutex); |
| 1893 | |
| 1894 | CameraMetadata &metadata = request->mSettings; |
| 1895 | size_t count = mTriggerMap.size(); |
| 1896 | |
| 1897 | for (size_t i = 0; i < count; ++i) { |
| 1898 | RequestTrigger trigger = mTriggerMap.valueAt(i); |
| 1899 | |
| 1900 | uint32_t tag = trigger.metadataTag; |
| 1901 | camera_metadata_entry entry = metadata.find(tag); |
| 1902 | |
| 1903 | if (entry.count > 0) { |
| 1904 | /** |
| 1905 | * Already has an entry for this trigger in the request. |
| 1906 | * Rewrite it with our requested trigger value. |
| 1907 | */ |
| 1908 | RequestTrigger oldTrigger = trigger; |
| 1909 | |
| 1910 | oldTrigger.entryValue = entry.data.u8[0]; |
| 1911 | |
| 1912 | mTriggerReplacedMap.add(tag, oldTrigger); |
| 1913 | } else { |
| 1914 | /** |
| 1915 | * More typical, no trigger entry, so we just add it |
| 1916 | */ |
| 1917 | mTriggerRemovedMap.add(tag, trigger); |
| 1918 | } |
| 1919 | |
| 1920 | status_t res; |
| 1921 | |
| 1922 | switch (trigger.getTagType()) { |
| 1923 | case TYPE_BYTE: { |
| 1924 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 1925 | res = metadata.update(tag, |
| 1926 | &entryValue, |
| 1927 | /*count*/1); |
| 1928 | break; |
| 1929 | } |
| 1930 | case TYPE_INT32: |
| 1931 | res = metadata.update(tag, |
| 1932 | &trigger.entryValue, |
| 1933 | /*count*/1); |
| 1934 | break; |
| 1935 | default: |
| 1936 | ALOGE("%s: Type not supported: 0x%x", |
| 1937 | __FUNCTION__, |
| 1938 | trigger.getTagType()); |
| 1939 | return INVALID_OPERATION; |
| 1940 | } |
| 1941 | |
| 1942 | if (res != OK) { |
| 1943 | ALOGE("%s: Failed to update request metadata with trigger tag %s" |
| 1944 | ", value %d", __FUNCTION__, trigger.getTagName(), |
| 1945 | trigger.entryValue); |
| 1946 | return res; |
| 1947 | } |
| 1948 | |
| 1949 | ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__, |
| 1950 | trigger.getTagName(), |
| 1951 | trigger.entryValue); |
| 1952 | } |
| 1953 | |
| 1954 | mTriggerMap.clear(); |
| 1955 | |
| 1956 | return count; |
| 1957 | } |
| 1958 | |
| 1959 | status_t Camera3Device::RequestThread::removeTriggers( |
| 1960 | const sp<CaptureRequest> &request) { |
| 1961 | Mutex::Autolock al(mTriggerMutex); |
| 1962 | |
| 1963 | CameraMetadata &metadata = request->mSettings; |
| 1964 | |
| 1965 | /** |
| 1966 | * Replace all old entries with their old values. |
| 1967 | */ |
| 1968 | for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) { |
| 1969 | RequestTrigger trigger = mTriggerReplacedMap.valueAt(i); |
| 1970 | |
| 1971 | status_t res; |
| 1972 | |
| 1973 | uint32_t tag = trigger.metadataTag; |
| 1974 | switch (trigger.getTagType()) { |
| 1975 | case TYPE_BYTE: { |
| 1976 | uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue); |
| 1977 | res = metadata.update(tag, |
| 1978 | &entryValue, |
| 1979 | /*count*/1); |
| 1980 | break; |
| 1981 | } |
| 1982 | case TYPE_INT32: |
| 1983 | res = metadata.update(tag, |
| 1984 | &trigger.entryValue, |
| 1985 | /*count*/1); |
| 1986 | break; |
| 1987 | default: |
| 1988 | ALOGE("%s: Type not supported: 0x%x", |
| 1989 | __FUNCTION__, |
| 1990 | trigger.getTagType()); |
| 1991 | return INVALID_OPERATION; |
| 1992 | } |
| 1993 | |
| 1994 | if (res != OK) { |
| 1995 | ALOGE("%s: Failed to restore request metadata with trigger tag %s" |
| 1996 | ", trigger value %d", __FUNCTION__, |
| 1997 | trigger.getTagName(), trigger.entryValue); |
| 1998 | return res; |
| 1999 | } |
| 2000 | } |
| 2001 | mTriggerReplacedMap.clear(); |
| 2002 | |
| 2003 | /** |
| 2004 | * Remove all new entries. |
| 2005 | */ |
| 2006 | for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) { |
| 2007 | RequestTrigger trigger = mTriggerRemovedMap.valueAt(i); |
| 2008 | status_t res = metadata.erase(trigger.metadataTag); |
| 2009 | |
| 2010 | if (res != OK) { |
| 2011 | ALOGE("%s: Failed to erase metadata with trigger tag %s" |
| 2012 | ", trigger value %d", __FUNCTION__, |
| 2013 | trigger.getTagName(), trigger.entryValue); |
| 2014 | return res; |
| 2015 | } |
| 2016 | } |
| 2017 | mTriggerRemovedMap.clear(); |
| 2018 | |
| 2019 | return OK; |
| 2020 | } |
| 2021 | |
| 2022 | |
| 2023 | |
Eino-Ville Talvala | f76e027 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 2024 | /** |
Eino-Ville Talvala | 7fa43f3 | 2013-02-06 17:20:07 -0800 | [diff] [blame] | 2025 | * Static callback forwarding methods from HAL to instance |
| 2026 | */ |
| 2027 | |
| 2028 | void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb, |
| 2029 | const camera3_capture_result *result) { |
| 2030 | Camera3Device *d = |
| 2031 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
| 2032 | d->processCaptureResult(result); |
| 2033 | } |
| 2034 | |
| 2035 | void Camera3Device::sNotify(const camera3_callback_ops *cb, |
| 2036 | const camera3_notify_msg *msg) { |
| 2037 | Camera3Device *d = |
| 2038 | const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb)); |
| 2039 | d->notify(msg); |
| 2040 | } |
| 2041 | |
| 2042 | }; // namespace android |