Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | // Modified from hardware/libhardware/modules/camera/Camera.cpp |
| 18 | |
Sergii Piatakov | 2ad591f | 2018-08-03 11:41:06 +0300 | [diff] [blame^] | 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "Camera" |
| 21 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 22 | #include <cstdlib> |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <vector> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <hardware/camera3.h> |
| 27 | #include <sync/sync.h> |
| 28 | #include <system/camera_metadata.h> |
| 29 | #include <system/graphics.h> |
| 30 | #include <utils/Mutex.h> |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 31 | |
| 32 | #include "metadata/metadata_common.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 33 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 34 | #include <cutils/log.h> |
| 35 | |
| 36 | #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) |
| 37 | #include <utils/Trace.h> |
| 38 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 39 | #include "camera.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 40 | |
| 41 | #define CAMERA_SYNC_TIMEOUT 5000 // in msecs |
| 42 | |
| 43 | namespace default_camera_hal { |
| 44 | |
| 45 | extern "C" { |
| 46 | // Shim passed to the framework to close an opened device. |
| 47 | static int close_device(hw_device_t* dev) |
| 48 | { |
| 49 | camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev); |
| 50 | Camera* cam = static_cast<Camera*>(cam_dev->priv); |
| 51 | return cam->close(); |
| 52 | } |
| 53 | } // extern "C" |
| 54 | |
| 55 | Camera::Camera(int id) |
| 56 | : mId(id), |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 57 | mSettingsSet(false), |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 58 | mBusy(false), |
| 59 | mCallbackOps(NULL), |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame] | 60 | mInFlightTracker(new RequestTracker) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 61 | { |
| 62 | memset(&mTemplates, 0, sizeof(mTemplates)); |
| 63 | memset(&mDevice, 0, sizeof(mDevice)); |
| 64 | mDevice.common.tag = HARDWARE_DEVICE_TAG; |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 65 | mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_4; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 66 | mDevice.common.close = close_device; |
| 67 | mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps); |
| 68 | mDevice.priv = this; |
| 69 | } |
| 70 | |
| 71 | Camera::~Camera() |
| 72 | { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 75 | int Camera::openDevice(const hw_module_t *module, hw_device_t **device) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 76 | { |
| 77 | ALOGI("%s:%d: Opening camera device", __func__, mId); |
| 78 | ATRACE_CALL(); |
| 79 | android::Mutex::Autolock al(mDeviceLock); |
| 80 | |
| 81 | if (mBusy) { |
| 82 | ALOGE("%s:%d: Error! Camera device already opened", __func__, mId); |
| 83 | return -EBUSY; |
| 84 | } |
| 85 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 86 | int connectResult = connect(); |
| 87 | if (connectResult != 0) { |
| 88 | return connectResult; |
| 89 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 90 | mBusy = true; |
| 91 | mDevice.common.module = const_cast<hw_module_t*>(module); |
| 92 | *device = &mDevice.common; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int Camera::getInfo(struct camera_info *info) |
| 97 | { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 98 | info->device_version = mDevice.common.version; |
| 99 | initDeviceInfo(info); |
Ari Hausman-Cohen | e31e1f3 | 2016-11-16 15:02:07 -0800 | [diff] [blame] | 100 | if (!mStaticInfo) { |
| 101 | int res = loadStaticInfo(); |
| 102 | if (res) { |
| 103 | return res; |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 104 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 105 | } |
Ari Hausman-Cohen | e31e1f3 | 2016-11-16 15:02:07 -0800 | [diff] [blame] | 106 | info->static_camera_characteristics = mStaticInfo->raw_metadata(); |
| 107 | info->facing = mStaticInfo->facing(); |
| 108 | info->orientation = mStaticInfo->orientation(); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 109 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
Ari Hausman-Cohen | e31e1f3 | 2016-11-16 15:02:07 -0800 | [diff] [blame] | 113 | int Camera::loadStaticInfo() { |
| 114 | // Using a lock here ensures |mStaticInfo| will only ever be set once, |
| 115 | // even in concurrent situations. |
| 116 | android::Mutex::Autolock al(mStaticInfoLock); |
| 117 | |
| 118 | if (mStaticInfo) { |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | std::unique_ptr<android::CameraMetadata> static_metadata = |
| 123 | std::make_unique<android::CameraMetadata>(); |
| 124 | int res = initStaticInfo(static_metadata.get()); |
| 125 | if (res) { |
| 126 | ALOGE("%s:%d: Failed to get static info from device.", |
| 127 | __func__, mId); |
| 128 | return res; |
| 129 | } |
| 130 | |
| 131 | mStaticInfo.reset(StaticProperties::NewStaticProperties( |
| 132 | std::move(static_metadata))); |
| 133 | if (!mStaticInfo) { |
| 134 | ALOGE("%s:%d: Failed to initialize static properties from device metadata.", |
| 135 | __func__, mId); |
| 136 | return -ENODEV; |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 142 | int Camera::close() |
| 143 | { |
| 144 | ALOGI("%s:%d: Closing camera device", __func__, mId); |
| 145 | ATRACE_CALL(); |
| 146 | android::Mutex::Autolock al(mDeviceLock); |
| 147 | |
| 148 | if (!mBusy) { |
| 149 | ALOGE("%s:%d: Error! Camera device not open", __func__, mId); |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 153 | flush(); |
Ari Hausman-Cohen | 5acb400 | 2016-11-29 18:35:17 -0800 | [diff] [blame] | 154 | disconnect(); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 155 | mBusy = false; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int Camera::initialize(const camera3_callback_ops_t *callback_ops) |
| 160 | { |
| 161 | int res; |
| 162 | |
| 163 | ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops); |
| 164 | mCallbackOps = callback_ops; |
| 165 | // per-device specific initialization |
| 166 | res = initDevice(); |
| 167 | if (res != 0) { |
| 168 | ALOGE("%s:%d: Failed to initialize device!", __func__, mId); |
| 169 | return res; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int Camera::configureStreams(camera3_stream_configuration_t *stream_config) |
| 175 | { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 176 | android::Mutex::Autolock al(mDeviceLock); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 177 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 178 | ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config); |
| 179 | ATRACE_CALL(); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 180 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 181 | // Check that there are no in-flight requests. |
| 182 | if (!mInFlightTracker->Empty()) { |
| 183 | ALOGE("%s:%d: Can't configure streams while frames are in flight.", |
| 184 | __func__, mId); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | } |
| 187 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 188 | // Verify the set of streams in aggregate, and perform configuration if valid. |
| 189 | int res = validateStreamConfiguration(stream_config); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 190 | if (res) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 191 | ALOGE("%s:%d: Failed to validate stream set", __func__, mId); |
| 192 | } else { |
| 193 | // Set up all streams. Since they've been validated, |
| 194 | // this should only result in fatal (-ENODEV) errors. |
| 195 | // This occurs after validation to ensure that if there |
| 196 | // is a non-fatal error, the stream configuration doesn't change states. |
| 197 | res = setupStreams(stream_config); |
| 198 | if (res) { |
| 199 | ALOGE("%s:%d: Failed to setup stream set", __func__, mId); |
| 200 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 201 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 202 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 203 | // Set trackers based on result. |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 204 | if (!res) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 205 | // Success, set up the in-flight trackers for the new streams. |
| 206 | mInFlightTracker->SetStreamConfiguration(*stream_config); |
| 207 | // Must provide new settings for the new configuration. |
| 208 | mSettingsSet = false; |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame] | 209 | } else if (res != -EINVAL) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 210 | // Fatal error, the old configuration is invalid. |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame] | 211 | mInFlightTracker->ClearStreamConfiguration(); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 212 | } |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 213 | // On a non-fatal error the old configuration, if any, remains valid. |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 214 | return res; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 217 | int Camera::validateStreamConfiguration( |
| 218 | const camera3_stream_configuration_t* stream_config) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 219 | { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 220 | // Check that the configuration is well-formed. |
| 221 | if (stream_config == nullptr) { |
| 222 | ALOGE("%s:%d: NULL stream configuration array", __func__, mId); |
| 223 | return -EINVAL; |
| 224 | } else if (stream_config->num_streams == 0) { |
| 225 | ALOGE("%s:%d: Empty stream configuration array", __func__, mId); |
| 226 | return -EINVAL; |
| 227 | } else if (stream_config->streams == nullptr) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 228 | ALOGE("%s:%d: NULL stream configuration streams", __func__, mId); |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 229 | return -EINVAL; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 230 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 231 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 232 | // Check that the configuration is supported. |
| 233 | // Make sure static info has been initialized before trying to use it. |
| 234 | if (!mStaticInfo) { |
| 235 | int res = loadStaticInfo(); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 236 | if (res) { |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 237 | return res; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 238 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 239 | } |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 240 | if (!mStaticInfo->StreamConfigurationSupported(stream_config)) { |
| 241 | ALOGE("%s:%d: Stream configuration does not match static " |
| 242 | "metadata restrictions.", __func__, mId); |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | |
| 246 | // Dataspace support is poorly documented - unclear if the expectation |
| 247 | // is that a device supports ALL dataspaces that could match a given |
| 248 | // format. For now, defer to child class implementation. |
| 249 | // Rotation support isn't described by metadata, so must defer to device. |
| 250 | if (!validateDataspacesAndRotations(stream_config)) { |
| 251 | ALOGE("%s:%d: Device can not handle configuration " |
| 252 | "dataspaces or rotations.", __func__, mId); |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 256 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | bool Camera::isValidTemplateType(int type) |
| 260 | { |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 261 | return type > 0 && type < CAMERA3_TEMPLATE_COUNT; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | const camera_metadata_t* Camera::constructDefaultRequestSettings(int type) |
| 265 | { |
| 266 | ALOGV("%s:%d: type=%d", __func__, mId, type); |
| 267 | |
| 268 | if (!isValidTemplateType(type)) { |
| 269 | ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type); |
| 270 | return NULL; |
| 271 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 272 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 273 | if (!mTemplates[type]) { |
Ari Hausman-Cohen | e31e1f3 | 2016-11-16 15:02:07 -0800 | [diff] [blame] | 274 | // Check if the device has the necessary features |
| 275 | // for the requested template. If not, don't bother. |
| 276 | if (!mStaticInfo->TemplateSupported(type)) { |
| 277 | ALOGW("%s:%d: Camera does not support template type %d", |
| 278 | __func__, mId, type); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 282 | // Initialize this template if it hasn't been initialized yet. |
| 283 | std::unique_ptr<android::CameraMetadata> new_template = |
| 284 | std::make_unique<android::CameraMetadata>(); |
| 285 | int res = initTemplate(type, new_template.get()); |
| 286 | if (res || !new_template) { |
| 287 | ALOGE("%s:%d: Failed to generate template of type: %d", |
| 288 | __func__, mId, type); |
| 289 | return NULL; |
| 290 | } |
| 291 | mTemplates[type] = std::move(new_template); |
| 292 | } |
| 293 | |
| 294 | // The "locking" here only causes non-const methods to fail, |
| 295 | // which is not a problem since the CameraMetadata being locked |
| 296 | // is already const. Destructing automatically "unlocks". |
| 297 | return mTemplates[type]->getAndLock(); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 300 | int Camera::processCaptureRequest(camera3_capture_request_t *temp_request) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 301 | { |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 302 | int res; |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 303 | // TODO(b/32917568): A capture request submitted or ongoing during a flush |
| 304 | // should be returned with an error; for now they are mutually exclusive. |
| 305 | android::Mutex::Autolock al(mFlushLock); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 306 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 307 | ATRACE_CALL(); |
| 308 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 309 | if (temp_request == NULL) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 310 | ALOGE("%s:%d: NULL request recieved", __func__, mId); |
| 311 | return -EINVAL; |
| 312 | } |
| 313 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 314 | // Make a persistent copy of request, since otherwise it won't live |
| 315 | // past the end of this method. |
| 316 | std::shared_ptr<CaptureRequest> request = std::make_shared<CaptureRequest>(temp_request); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 317 | |
Ari Hausman-Cohen | ad6fe2b | 2016-11-16 10:48:07 -0800 | [diff] [blame] | 318 | ALOGV("%s:%d: frame: %d", __func__, mId, request->frame_number); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 319 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 320 | if (!mInFlightTracker->CanAddRequest(*request)) { |
| 321 | // Streams are full or frame number is not unique. |
| 322 | ALOGE("%s:%d: Can not add request.", __func__, mId); |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 326 | // Null/Empty indicates use last settings |
| 327 | if (request->settings.isEmpty() && !mSettingsSet) { |
| 328 | ALOGE("%s:%d: NULL settings without previous set Frame:%d", |
| 329 | __func__, mId, request->frame_number); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 330 | return -EINVAL; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | if (request->input_buffer != NULL) { |
| 334 | ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId, |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 335 | request->input_buffer.get()); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 336 | } else { |
| 337 | ALOGV("%s:%d: Capturing new frame.", __func__, mId); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 340 | if (!isValidRequestSettings(request->settings)) { |
| 341 | ALOGE("%s:%d: Invalid request settings.", __func__, mId); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 342 | return -EINVAL; |
| 343 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 344 | |
| 345 | // Pre-process output buffers. |
| 346 | if (request->output_buffers.size() <= 0) { |
| 347 | ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId, |
| 348 | request->output_buffers.size()); |
| 349 | return -EINVAL; |
| 350 | } |
| 351 | for (auto& output_buffer : request->output_buffers) { |
| 352 | res = preprocessCaptureBuffer(&output_buffer); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 353 | if (res) |
| 354 | return -ENODEV; |
| 355 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 356 | |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame] | 357 | // Add the request to tracking. |
| 358 | if (!mInFlightTracker->Add(request)) { |
| 359 | ALOGE("%s:%d: Failed to track request for frame %d.", |
| 360 | __func__, mId, request->frame_number); |
| 361 | return -ENODEV; |
| 362 | } |
| 363 | |
Ari Hausman-Cohen | ef52310 | 2016-11-21 17:02:01 -0800 | [diff] [blame] | 364 | // Valid settings have been provided (mSettingsSet is a misnomer; |
| 365 | // all that matters is that a previous request with valid settings |
| 366 | // has been passed to the device, not that they've been set). |
| 367 | mSettingsSet = true; |
| 368 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 369 | // Send the request off to the device for completion. |
| 370 | enqueueRequest(request); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 371 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 372 | // Request is now in flight. The device will call completeRequest |
| 373 | // asynchronously when it is done filling buffers and metadata. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 374 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 377 | void Camera::completeRequest(std::shared_ptr<CaptureRequest> request, int err) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 378 | { |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame] | 379 | if (!mInFlightTracker->Remove(request)) { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 380 | ALOGE("%s:%d: Completed request %p is not being tracked. " |
| 381 | "It may have been cleared out during a flush.", |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame] | 382 | __func__, mId, request.get()); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame] | 386 | // Since |request| has been removed from the tracking, this method |
| 387 | // MUST call sendResult (can still return a result in an error state, e.g. |
| 388 | // through completeRequestWithError) so the frame doesn't get lost. |
| 389 | |
| 390 | if (err) { |
| 391 | ALOGE("%s:%d: Error completing request for frame %d.", |
| 392 | __func__, mId, request->frame_number); |
| 393 | completeRequestWithError(request); |
| 394 | return; |
| 395 | } |
| 396 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 397 | // Notify the framework with the shutter time (extracted from the result). |
| 398 | int64_t timestamp = 0; |
| 399 | // TODO(b/31360070): The general metadata methods should be part of the |
| 400 | // default_camera_hal namespace, not the v4l2_camera_hal namespace. |
| 401 | int res = v4l2_camera_hal::SingleTagValue( |
| 402 | request->settings, ANDROID_SENSOR_TIMESTAMP, ×tamp); |
| 403 | if (res) { |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 404 | ALOGE("%s:%d: Request for frame %d is missing required metadata.", |
| 405 | __func__, mId, request->frame_number); |
Ari Hausman-Cohen | fb16111 | 2016-09-29 14:35:00 -0700 | [diff] [blame] | 406 | // TODO(b/31653322): Send RESULT error. |
| 407 | // For now sending REQUEST error instead. |
| 408 | completeRequestWithError(request); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | notifyShutter(request->frame_number, timestamp); |
| 412 | |
| 413 | // TODO(b/31653322): Check all returned buffers for errors |
| 414 | // (if any, send BUFFER error). |
| 415 | |
Ari Hausman-Cohen | fb16111 | 2016-09-29 14:35:00 -0700 | [diff] [blame] | 416 | sendResult(request); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 419 | int Camera::flush() |
| 420 | { |
| 421 | ALOGV("%s:%d: Flushing.", __func__, mId); |
| 422 | // TODO(b/32917568): Synchronization. Behave "appropriately" |
| 423 | // (i.e. according to camera3.h) if process_capture_request() |
| 424 | // is called concurrently with this (in either order). |
| 425 | // Since the callback to completeRequest also may happen on a separate |
| 426 | // thread, this function should behave nicely concurrently with that too. |
| 427 | android::Mutex::Autolock al(mFlushLock); |
| 428 | |
| 429 | std::set<std::shared_ptr<CaptureRequest>> requests; |
| 430 | mInFlightTracker->Clear(&requests); |
| 431 | for (auto& request : requests) { |
| 432 | // TODO(b/31653322): See camera3.h. Should return different error |
| 433 | // depending on status of the request. |
| 434 | completeRequestWithError(request); |
| 435 | } |
| 436 | |
| 437 | ALOGV("%s:%d: Flushed %u requests.", __func__, mId, requests.size()); |
| 438 | |
| 439 | // Call down into the device flushing. |
| 440 | return flushBuffers(); |
| 441 | } |
| 442 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 443 | int Camera::preprocessCaptureBuffer(camera3_stream_buffer_t *buffer) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 444 | { |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 445 | int res; |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 446 | // TODO(b/29334616): This probably should be non-blocking; part |
| 447 | // of the asynchronous request processing. |
| 448 | if (buffer->acquire_fence != -1) { |
| 449 | res = sync_wait(buffer->acquire_fence, CAMERA_SYNC_TIMEOUT); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 450 | if (res == -ETIME) { |
| 451 | ALOGE("%s:%d: Timeout waiting on buffer acquire fence", |
| 452 | __func__, mId); |
| 453 | return res; |
| 454 | } else if (res) { |
| 455 | ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)", |
| 456 | __func__, mId, strerror(-res), res); |
| 457 | return res; |
| 458 | } |
Eric Jeong | eadafff | 2017-08-01 19:40:03 +0900 | [diff] [blame] | 459 | ::close(buffer->acquire_fence); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 462 | // Acquire fence has been waited upon. |
| 463 | buffer->acquire_fence = -1; |
| 464 | // No release fence waiting unless the device sets it. |
| 465 | buffer->release_fence = -1; |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 466 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 467 | buffer->status = CAMERA3_BUFFER_STATUS_OK; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp) |
| 472 | { |
Ari Hausman-Cohen | fb16111 | 2016-09-29 14:35:00 -0700 | [diff] [blame] | 473 | camera3_notify_msg_t message; |
| 474 | memset(&message, 0, sizeof(message)); |
| 475 | message.type = CAMERA3_MSG_SHUTTER; |
| 476 | message.message.shutter.frame_number = frame_number; |
| 477 | message.message.shutter.timestamp = timestamp; |
| 478 | mCallbackOps->notify(mCallbackOps, &message); |
| 479 | } |
| 480 | |
| 481 | void Camera::completeRequestWithError(std::shared_ptr<CaptureRequest> request) |
| 482 | { |
| 483 | // Send an error notification. |
| 484 | camera3_notify_msg_t message; |
| 485 | memset(&message, 0, sizeof(message)); |
| 486 | message.type = CAMERA3_MSG_ERROR; |
| 487 | message.message.error.frame_number = request->frame_number; |
| 488 | message.message.error.error_stream = nullptr; |
| 489 | message.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; |
| 490 | mCallbackOps->notify(mCallbackOps, &message); |
| 491 | |
| 492 | // TODO(b/31856611): Ensure all the buffers indicate their error status. |
| 493 | |
| 494 | // Send the errored out result. |
| 495 | sendResult(request); |
| 496 | } |
| 497 | |
| 498 | void Camera::sendResult(std::shared_ptr<CaptureRequest> request) { |
| 499 | // Fill in the result struct |
| 500 | // (it only needs to live until the end of the framework callback). |
| 501 | camera3_capture_result_t result { |
| 502 | request->frame_number, |
| 503 | request->settings.getAndLock(), |
Dmitry Shmidt | 9df082e | 2018-01-23 16:30:08 -0800 | [diff] [blame] | 504 | static_cast<uint32_t>(request->output_buffers.size()), |
Ari Hausman-Cohen | fb16111 | 2016-09-29 14:35:00 -0700 | [diff] [blame] | 505 | request->output_buffers.data(), |
| 506 | request->input_buffer.get(), |
| 507 | 1 // Total result; only 1 part. |
| 508 | }; |
| 509 | // Make the framework callback. |
| 510 | mCallbackOps->process_capture_result(mCallbackOps, &result); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | void Camera::dump(int fd) |
| 514 | { |
| 515 | ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd); |
| 516 | ATRACE_CALL(); |
| 517 | android::Mutex::Autolock al(mDeviceLock); |
| 518 | |
| 519 | dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy); |
| 520 | |
| 521 | // TODO: dump all settings |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | const char* Camera::templateToString(int type) |
| 525 | { |
| 526 | switch (type) { |
| 527 | case CAMERA3_TEMPLATE_PREVIEW: |
| 528 | return "CAMERA3_TEMPLATE_PREVIEW"; |
| 529 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
| 530 | return "CAMERA3_TEMPLATE_STILL_CAPTURE"; |
| 531 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 532 | return "CAMERA3_TEMPLATE_VIDEO_RECORD"; |
| 533 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 534 | return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT"; |
| 535 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: |
| 536 | return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG"; |
| 537 | } |
| 538 | // TODO: support vendor templates |
| 539 | return "Invalid template type!"; |
| 540 | } |
| 541 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 542 | extern "C" { |
| 543 | // Get handle to camera from device priv data |
| 544 | static Camera *camdev_to_camera(const camera3_device_t *dev) |
| 545 | { |
| 546 | return reinterpret_cast<Camera*>(dev->priv); |
| 547 | } |
| 548 | |
| 549 | static int initialize(const camera3_device_t *dev, |
| 550 | const camera3_callback_ops_t *callback_ops) |
| 551 | { |
| 552 | return camdev_to_camera(dev)->initialize(callback_ops); |
| 553 | } |
| 554 | |
| 555 | static int configure_streams(const camera3_device_t *dev, |
| 556 | camera3_stream_configuration_t *stream_list) |
| 557 | { |
| 558 | return camdev_to_camera(dev)->configureStreams(stream_list); |
| 559 | } |
| 560 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 561 | static const camera_metadata_t *construct_default_request_settings( |
| 562 | const camera3_device_t *dev, int type) |
| 563 | { |
| 564 | return camdev_to_camera(dev)->constructDefaultRequestSettings(type); |
| 565 | } |
| 566 | |
| 567 | static int process_capture_request(const camera3_device_t *dev, |
| 568 | camera3_capture_request_t *request) |
| 569 | { |
| 570 | return camdev_to_camera(dev)->processCaptureRequest(request); |
| 571 | } |
| 572 | |
| 573 | static void dump(const camera3_device_t *dev, int fd) |
| 574 | { |
| 575 | camdev_to_camera(dev)->dump(fd); |
| 576 | } |
| 577 | |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 578 | static int flush(const camera3_device_t *dev) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 579 | { |
Ari Hausman-Cohen | c5a4852 | 2016-11-16 10:53:52 -0800 | [diff] [blame] | 580 | return camdev_to_camera(dev)->flush(); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | } // extern "C" |
| 584 | |
| 585 | const camera3_device_ops_t Camera::sOps = { |
| 586 | .initialize = default_camera_hal::initialize, |
| 587 | .configure_streams = default_camera_hal::configure_streams, |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 588 | .register_stream_buffers = nullptr, |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 589 | .construct_default_request_settings |
| 590 | = default_camera_hal::construct_default_request_settings, |
| 591 | .process_capture_request = default_camera_hal::process_capture_request, |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 592 | .get_metadata_vendor_tag_ops = nullptr, |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 593 | .dump = default_camera_hal::dump, |
| 594 | .flush = default_camera_hal::flush, |
| 595 | .reserved = {0}, |
| 596 | }; |
| 597 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 598 | } // namespace default_camera_hal |