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