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 | |
| 19 | #include <cstdlib> |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <vector> |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <hardware/camera3.h> |
| 24 | #include <sync/sync.h> |
| 25 | #include <system/camera_metadata.h> |
| 26 | #include <system/graphics.h> |
| 27 | #include <utils/Mutex.h> |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 28 | |
| 29 | #include "metadata/metadata_common.h" |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 30 | #include "stream.h" |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 31 | |
| 32 | //#define LOG_NDEBUG 0 |
| 33 | #define LOG_TAG "Camera" |
| 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), |
| 60 | mStreams(NULL), |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 61 | mNumStreams(0), |
| 62 | mInFlightTracker(new RequestTracker) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 63 | { |
| 64 | memset(&mTemplates, 0, sizeof(mTemplates)); |
| 65 | memset(&mDevice, 0, sizeof(mDevice)); |
| 66 | mDevice.common.tag = HARDWARE_DEVICE_TAG; |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 67 | mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_4; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 68 | mDevice.common.close = close_device; |
| 69 | mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps); |
| 70 | mDevice.priv = this; |
| 71 | } |
| 72 | |
| 73 | Camera::~Camera() |
| 74 | { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 77 | 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] | 78 | { |
| 79 | ALOGI("%s:%d: Opening camera device", __func__, mId); |
| 80 | ATRACE_CALL(); |
| 81 | android::Mutex::Autolock al(mDeviceLock); |
| 82 | |
| 83 | if (mBusy) { |
| 84 | ALOGE("%s:%d: Error! Camera device already opened", __func__, mId); |
| 85 | return -EBUSY; |
| 86 | } |
| 87 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 88 | int connectResult = connect(); |
| 89 | if (connectResult != 0) { |
| 90 | return connectResult; |
| 91 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 92 | mBusy = true; |
| 93 | mDevice.common.module = const_cast<hw_module_t*>(module); |
| 94 | *device = &mDevice.common; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int Camera::getInfo(struct camera_info *info) |
| 99 | { |
| 100 | android::Mutex::Autolock al(mStaticInfoLock); |
| 101 | |
| 102 | info->device_version = mDevice.common.version; |
| 103 | initDeviceInfo(info); |
| 104 | if (mStaticInfo == NULL) { |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 105 | std::unique_ptr<android::CameraMetadata> static_info = |
| 106 | std::make_unique<android::CameraMetadata>(); |
| 107 | if (initStaticInfo(static_info.get())) { |
| 108 | return -ENODEV; |
| 109 | } |
| 110 | mStaticInfo = std::move(static_info); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 111 | } |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 112 | // The "locking" here only causes non-const methods to fail, |
| 113 | // which is not a problem since the CameraMetadata being locked |
| 114 | // is already const. Destructing automatically "unlocks". |
| 115 | info->static_camera_characteristics = mStaticInfo->getAndLock(); |
| 116 | |
| 117 | // Get facing & orientation from the static info. |
| 118 | uint8_t facing = 0; |
| 119 | int res = v4l2_camera_hal::SingleTagValue( |
| 120 | *mStaticInfo, ANDROID_LENS_FACING, &facing); |
| 121 | if (res) { |
| 122 | ALOGE("%s:%d: Failed to get facing from static metadata.", |
| 123 | __func__, mId); |
| 124 | return res; |
| 125 | } |
| 126 | switch (facing) { |
| 127 | case (ANDROID_LENS_FACING_FRONT): |
| 128 | info->facing = CAMERA_FACING_FRONT; |
| 129 | break; |
| 130 | case (ANDROID_LENS_FACING_BACK): |
| 131 | info->facing = CAMERA_FACING_BACK; |
| 132 | break; |
| 133 | case (ANDROID_LENS_FACING_EXTERNAL): |
| 134 | info->facing = CAMERA_FACING_EXTERNAL; |
| 135 | break; |
| 136 | default: |
| 137 | ALOGE("%s:%d: Invalid facing from metadata: %d.", |
| 138 | __func__, mId, facing); |
| 139 | return -ENODEV; |
| 140 | } |
| 141 | int32_t orientation = 0; |
| 142 | res = v4l2_camera_hal::SingleTagValue( |
| 143 | *mStaticInfo, ANDROID_SENSOR_ORIENTATION, &orientation); |
| 144 | if (res) { |
| 145 | ALOGE("%s:%d: Failed to get orientation from static metadata.", |
| 146 | __func__, mId); |
| 147 | return res; |
| 148 | } |
| 149 | info->orientation = static_cast<int>(orientation); |
| 150 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | int Camera::close() |
| 155 | { |
| 156 | ALOGI("%s:%d: Closing camera device", __func__, mId); |
| 157 | ATRACE_CALL(); |
| 158 | android::Mutex::Autolock al(mDeviceLock); |
| 159 | |
| 160 | if (!mBusy) { |
| 161 | ALOGE("%s:%d: Error! Camera device not open", __func__, mId); |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
Ari Hausman-Cohen | 345bd3a | 2016-06-13 15:33:53 -0700 | [diff] [blame] | 165 | disconnect(); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 166 | mBusy = false; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | int Camera::initialize(const camera3_callback_ops_t *callback_ops) |
| 171 | { |
| 172 | int res; |
| 173 | |
| 174 | ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops); |
| 175 | mCallbackOps = callback_ops; |
| 176 | // per-device specific initialization |
| 177 | res = initDevice(); |
| 178 | if (res != 0) { |
| 179 | ALOGE("%s:%d: Failed to initialize device!", __func__, mId); |
| 180 | return res; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | int Camera::configureStreams(camera3_stream_configuration_t *stream_config) |
| 186 | { |
| 187 | camera3_stream_t *astream; |
| 188 | Stream **newStreams = NULL; |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 189 | int res = 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 190 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 191 | // Must provide new settings after configureStreams. |
| 192 | mSettingsSet = false; |
| 193 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 194 | ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config); |
| 195 | ATRACE_CALL(); |
| 196 | android::Mutex::Autolock al(mDeviceLock); |
| 197 | |
| 198 | if (stream_config == NULL) { |
| 199 | ALOGE("%s:%d: NULL stream configuration array", __func__, mId); |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | if (stream_config->num_streams == 0) { |
| 203 | ALOGE("%s:%d: Empty stream configuration array", __func__, mId); |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | // Create new stream array |
| 208 | newStreams = new Stream*[stream_config->num_streams]; |
| 209 | ALOGV("%s:%d: Number of Streams: %d", __func__, mId, |
| 210 | stream_config->num_streams); |
| 211 | |
| 212 | // Mark all current streams unused for now |
| 213 | for (int i = 0; i < mNumStreams; i++) |
| 214 | mStreams[i]->mReuse = false; |
| 215 | // Fill new stream array with reused streams and new streams |
| 216 | for (unsigned int i = 0; i < stream_config->num_streams; i++) { |
| 217 | astream = stream_config->streams[i]; |
| 218 | if (astream->max_buffers > 0) { |
| 219 | ALOGV("%s:%d: Reusing stream %d", __func__, mId, i); |
| 220 | newStreams[i] = reuseStream(astream); |
| 221 | } else { |
| 222 | ALOGV("%s:%d: Creating new stream %d", __func__, mId, i); |
| 223 | newStreams[i] = new Stream(mId, astream); |
| 224 | } |
| 225 | |
| 226 | if (newStreams[i] == NULL) { |
| 227 | ALOGE("%s:%d: Error processing stream %d", __func__, mId, i); |
| 228 | goto err_out; |
| 229 | } |
| 230 | astream->priv = newStreams[i]; |
| 231 | } |
| 232 | |
| 233 | // Verify the set of streams in aggregate |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 234 | if (!isValidStreamSet(newStreams, stream_config->num_streams, |
| 235 | stream_config->operation_mode)) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 236 | ALOGE("%s:%d: Invalid stream set", __func__, mId); |
| 237 | goto err_out; |
| 238 | } |
| 239 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 240 | // Set up all streams (calculate usage/max_buffers for each, |
| 241 | // do any device-specific initialization) |
| 242 | res = setupStreams(newStreams, stream_config->num_streams); |
| 243 | if (res) { |
| 244 | ALOGE("%s:%d: Failed to setup stream set", __func__, mId); |
| 245 | goto err_out; |
| 246 | } |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 247 | |
| 248 | // Destroy all old streams and replace stream array with new one |
| 249 | destroyStreams(mStreams, mNumStreams); |
| 250 | mStreams = newStreams; |
| 251 | mNumStreams = stream_config->num_streams; |
| 252 | |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 253 | // Update the request tracker. |
| 254 | mInFlightTracker->SetStreamConfiguration(*stream_config); |
| 255 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 256 | return 0; |
| 257 | |
| 258 | err_out: |
| 259 | // Clean up temporary streams, preserve existing mStreams/mNumStreams |
| 260 | destroyStreams(newStreams, stream_config->num_streams); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 261 | // Set error if it wasn't specified. |
| 262 | if (!res) { |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 263 | res = -EINVAL; |
| 264 | } else if (res != -EINVAL) { |
| 265 | // Fatal error, clear stream configuration. |
| 266 | mInFlightTracker->ClearStreamConfiguration(); |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 267 | } |
| 268 | return res; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void Camera::destroyStreams(Stream **streams, int count) |
| 272 | { |
| 273 | if (streams == NULL) |
| 274 | return; |
| 275 | for (int i = 0; i < count; i++) { |
| 276 | // Only destroy streams that weren't reused |
| 277 | if (streams[i] != NULL && !streams[i]->mReuse) |
| 278 | delete streams[i]; |
| 279 | } |
| 280 | delete [] streams; |
| 281 | } |
| 282 | |
| 283 | Stream *Camera::reuseStream(camera3_stream_t *astream) |
| 284 | { |
| 285 | Stream *priv = reinterpret_cast<Stream*>(astream->priv); |
| 286 | // Verify the re-used stream's parameters match |
| 287 | if (!priv->isValidReuseStream(mId, astream)) { |
| 288 | ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId); |
| 289 | return NULL; |
| 290 | } |
| 291 | // Mark stream to be reused |
| 292 | priv->mReuse = true; |
| 293 | return priv; |
| 294 | } |
| 295 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 296 | bool Camera::isValidStreamSet(Stream **streams, int count, uint32_t mode) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 297 | { |
| 298 | int inputs = 0; |
| 299 | int outputs = 0; |
| 300 | |
| 301 | if (streams == NULL) { |
| 302 | ALOGE("%s:%d: NULL stream configuration streams", __func__, mId); |
| 303 | return false; |
| 304 | } |
| 305 | if (count == 0) { |
| 306 | ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId); |
| 307 | return false; |
| 308 | } |
| 309 | // Validate there is at most one input stream and at least one output stream |
| 310 | for (int i = 0; i < count; i++) { |
| 311 | // A stream may be both input and output (bidirectional) |
| 312 | if (streams[i]->isInputType()) |
| 313 | inputs++; |
| 314 | if (streams[i]->isOutputType()) |
| 315 | outputs++; |
| 316 | } |
| 317 | ALOGV("%s:%d: Configuring %d output streams and %d input streams", |
| 318 | __func__, mId, outputs, inputs); |
| 319 | if (outputs < 1) { |
| 320 | ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId); |
| 321 | return false; |
| 322 | } |
| 323 | if (inputs > 1) { |
| 324 | ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId); |
| 325 | return false; |
| 326 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 327 | |
| 328 | // check for correct number of Bayer/YUV/JPEG/Encoder streams |
| 329 | return isSupportedStreamSet(streams, count, mode); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 332 | int Camera::setupStreams(Stream **streams, int count) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 333 | { |
| 334 | /* |
| 335 | * This is where the HAL has to decide internally how to handle all of the |
| 336 | * streams, and then produce usage and max_buffer values for each stream. |
| 337 | * Note, the stream array has been checked before this point for ALL invalid |
| 338 | * conditions, so it must find a successful configuration for this stream |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 339 | * array. The only errors should be from individual streams requesting |
| 340 | * unsupported features (such as data_space or rotation). |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 341 | */ |
| 342 | for (int i = 0; i < count; i++) { |
| 343 | uint32_t usage = 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 344 | if (streams[i]->isOutputType()) |
Ari Hausman-Cohen | fbac174 | 2016-09-19 14:03:13 -0700 | [diff] [blame] | 345 | usage |= GRALLOC_USAGE_SW_WRITE_OFTEN; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 346 | if (streams[i]->isInputType()) |
Ari Hausman-Cohen | fbac174 | 2016-09-19 14:03:13 -0700 | [diff] [blame] | 347 | usage |= GRALLOC_USAGE_SW_READ_OFTEN; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 348 | streams[i]->setUsage(usage); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 349 | |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 350 | uint32_t max_buffers; |
| 351 | int res = setupStream(streams[i], &max_buffers); |
| 352 | if (res) { |
| 353 | return res; |
| 354 | } |
| 355 | streams[i]->setMaxBuffers(max_buffers); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 356 | } |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 357 | return 0; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | bool Camera::isValidTemplateType(int type) |
| 361 | { |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 362 | return type > 0 && type < CAMERA3_TEMPLATE_COUNT; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | const camera_metadata_t* Camera::constructDefaultRequestSettings(int type) |
| 366 | { |
| 367 | ALOGV("%s:%d: type=%d", __func__, mId, type); |
| 368 | |
| 369 | if (!isValidTemplateType(type)) { |
| 370 | ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type); |
| 371 | return NULL; |
| 372 | } |
Ari Hausman-Cohen | 4992584 | 2016-06-21 14:07:58 -0700 | [diff] [blame] | 373 | |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 374 | if (!mTemplates[type]) { |
| 375 | // Initialize this template if it hasn't been initialized yet. |
| 376 | std::unique_ptr<android::CameraMetadata> new_template = |
| 377 | std::make_unique<android::CameraMetadata>(); |
| 378 | int res = initTemplate(type, new_template.get()); |
| 379 | if (res || !new_template) { |
| 380 | ALOGE("%s:%d: Failed to generate template of type: %d", |
| 381 | __func__, mId, type); |
| 382 | return NULL; |
| 383 | } |
| 384 | mTemplates[type] = std::move(new_template); |
| 385 | } |
| 386 | |
| 387 | // The "locking" here only causes non-const methods to fail, |
| 388 | // which is not a problem since the CameraMetadata being locked |
| 389 | // is already const. Destructing automatically "unlocks". |
| 390 | return mTemplates[type]->getAndLock(); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 393 | int Camera::processCaptureRequest(camera3_capture_request_t *temp_request) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 394 | { |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 395 | int res; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 396 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 397 | ALOGV("%s:%d: request=%p", __func__, mId, temp_request); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 398 | ATRACE_CALL(); |
| 399 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 400 | if (temp_request == NULL) { |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 401 | ALOGE("%s:%d: NULL request recieved", __func__, mId); |
| 402 | return -EINVAL; |
| 403 | } |
| 404 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 405 | // Make a persistent copy of request, since otherwise it won't live |
| 406 | // past the end of this method. |
| 407 | std::shared_ptr<CaptureRequest> request = std::make_shared<CaptureRequest>(temp_request); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 408 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 409 | ALOGV("%s:%d: Request Frame:%d", __func__, mId, |
| 410 | request->frame_number); |
| 411 | |
| 412 | // Null/Empty indicates use last settings |
| 413 | if (request->settings.isEmpty() && !mSettingsSet) { |
| 414 | ALOGE("%s:%d: NULL settings without previous set Frame:%d", |
| 415 | __func__, mId, request->frame_number); |
Ari Hausman-Cohen | abbf9cc | 2016-08-23 11:59:59 -0700 | [diff] [blame] | 416 | return -EINVAL; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (request->input_buffer != NULL) { |
| 420 | ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId, |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 421 | request->input_buffer.get()); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 422 | } else { |
| 423 | ALOGV("%s:%d: Capturing new frame.", __func__, mId); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 426 | if (!isValidRequest(*request)) { |
| 427 | ALOGE("%s:%d: Invalid request.", __func__, mId); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 428 | return -EINVAL; |
| 429 | } |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 430 | // Valid settings have been provided (mSettingsSet is a misnomer; |
| 431 | // all that matters is that a previous request with valid settings |
| 432 | // has been passed to the device, not that they've been set). |
| 433 | mSettingsSet = true; |
| 434 | |
| 435 | // Pre-process output buffers. |
| 436 | if (request->output_buffers.size() <= 0) { |
| 437 | ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId, |
| 438 | request->output_buffers.size()); |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | for (auto& output_buffer : request->output_buffers) { |
| 442 | res = preprocessCaptureBuffer(&output_buffer); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 443 | if (res) |
| 444 | return -ENODEV; |
| 445 | } |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 446 | |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 447 | // Add the request to tracking. |
| 448 | if (!mInFlightTracker->Add(request)) { |
| 449 | ALOGE("%s:%d: Failed to track request for frame %d.", |
| 450 | __func__, mId, request->frame_number); |
| 451 | return -ENODEV; |
| 452 | } |
| 453 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 454 | // Send the request off to the device for completion. |
| 455 | enqueueRequest(request); |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 456 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 457 | // Request is now in flight. The device will call completeRequest |
| 458 | // asynchronously when it is done filling buffers and metadata. |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 459 | return 0; |
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 | void Camera::completeRequest(std::shared_ptr<CaptureRequest> request, int err) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 463 | { |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 464 | if (!mInFlightTracker->Remove(request)) { |
| 465 | ALOGE("%s:%d: Completed request %p is not being tracked.", |
| 466 | __func__, mId, request.get()); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 467 | return; |
| 468 | } |
| 469 | |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 470 | // Since |request| has been removed from the tracking, this method |
| 471 | // MUST call sendResult (can still return a result in an error state, e.g. |
| 472 | // through completeRequestWithError) so the frame doesn't get lost. |
| 473 | |
| 474 | if (err) { |
| 475 | ALOGE("%s:%d: Error completing request for frame %d.", |
| 476 | __func__, mId, request->frame_number); |
| 477 | completeRequestWithError(request); |
| 478 | return; |
| 479 | } |
| 480 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 481 | // Notify the framework with the shutter time (extracted from the result). |
| 482 | int64_t timestamp = 0; |
| 483 | // TODO(b/31360070): The general metadata methods should be part of the |
| 484 | // default_camera_hal namespace, not the v4l2_camera_hal namespace. |
| 485 | int res = v4l2_camera_hal::SingleTagValue( |
| 486 | request->settings, ANDROID_SENSOR_TIMESTAMP, ×tamp); |
| 487 | if (res) { |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 488 | ALOGE("%s:%d: Request for frame %d is missing required metadata.", |
| 489 | __func__, mId, request->frame_number); |
Ari Hausman-Cohen | fb16111 | 2016-09-29 14:35:00 -0700 | [diff] [blame] | 490 | // TODO(b/31653322): Send RESULT error. |
| 491 | // For now sending REQUEST error instead. |
| 492 | completeRequestWithError(request); |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | notifyShutter(request->frame_number, timestamp); |
| 496 | |
| 497 | // TODO(b/31653322): Check all returned buffers for errors |
| 498 | // (if any, send BUFFER error). |
| 499 | |
Ari Hausman-Cohen | fb16111 | 2016-09-29 14:35:00 -0700 | [diff] [blame] | 500 | sendResult(request); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 503 | int Camera::preprocessCaptureBuffer(camera3_stream_buffer_t *buffer) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 504 | { |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 505 | int res; |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 506 | // TODO(b/29334616): This probably should be non-blocking; part |
| 507 | // of the asynchronous request processing. |
| 508 | if (buffer->acquire_fence != -1) { |
| 509 | res = sync_wait(buffer->acquire_fence, CAMERA_SYNC_TIMEOUT); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 510 | if (res == -ETIME) { |
| 511 | ALOGE("%s:%d: Timeout waiting on buffer acquire fence", |
| 512 | __func__, mId); |
| 513 | return res; |
| 514 | } else if (res) { |
| 515 | ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)", |
| 516 | __func__, mId, strerror(-res), res); |
| 517 | return res; |
| 518 | } |
| 519 | } |
| 520 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 521 | // Acquire fence has been waited upon. |
| 522 | buffer->acquire_fence = -1; |
| 523 | // No release fence waiting unless the device sets it. |
| 524 | buffer->release_fence = -1; |
Ari Hausman-Cohen | 24e541c | 2016-07-21 11:20:30 -0700 | [diff] [blame] | 525 | |
Ari Hausman-Cohen | 2738a9c | 2016-09-21 15:03:49 -0700 | [diff] [blame] | 526 | buffer->status = CAMERA3_BUFFER_STATUS_OK; |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp) |
| 531 | { |
Ari Hausman-Cohen | fb16111 | 2016-09-29 14:35:00 -0700 | [diff] [blame] | 532 | camera3_notify_msg_t message; |
| 533 | memset(&message, 0, sizeof(message)); |
| 534 | message.type = CAMERA3_MSG_SHUTTER; |
| 535 | message.message.shutter.frame_number = frame_number; |
| 536 | message.message.shutter.timestamp = timestamp; |
| 537 | mCallbackOps->notify(mCallbackOps, &message); |
| 538 | } |
| 539 | |
| 540 | void Camera::completeRequestWithError(std::shared_ptr<CaptureRequest> request) |
| 541 | { |
| 542 | // Send an error notification. |
| 543 | camera3_notify_msg_t message; |
| 544 | memset(&message, 0, sizeof(message)); |
| 545 | message.type = CAMERA3_MSG_ERROR; |
| 546 | message.message.error.frame_number = request->frame_number; |
| 547 | message.message.error.error_stream = nullptr; |
| 548 | message.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST; |
| 549 | mCallbackOps->notify(mCallbackOps, &message); |
| 550 | |
| 551 | // TODO(b/31856611): Ensure all the buffers indicate their error status. |
| 552 | |
| 553 | // Send the errored out result. |
| 554 | sendResult(request); |
| 555 | } |
| 556 | |
| 557 | void Camera::sendResult(std::shared_ptr<CaptureRequest> request) { |
| 558 | // Fill in the result struct |
| 559 | // (it only needs to live until the end of the framework callback). |
| 560 | camera3_capture_result_t result { |
| 561 | request->frame_number, |
| 562 | request->settings.getAndLock(), |
| 563 | request->output_buffers.size(), |
| 564 | request->output_buffers.data(), |
| 565 | request->input_buffer.get(), |
| 566 | 1 // Total result; only 1 part. |
| 567 | }; |
| 568 | // Make the framework callback. |
| 569 | mCallbackOps->process_capture_result(mCallbackOps, &result); |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void Camera::dump(int fd) |
| 573 | { |
| 574 | ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd); |
| 575 | ATRACE_CALL(); |
| 576 | android::Mutex::Autolock al(mDeviceLock); |
| 577 | |
| 578 | dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy); |
| 579 | |
| 580 | // TODO: dump all settings |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 581 | |
| 582 | dprintf(fd, "Number of streams: %d\n", mNumStreams); |
| 583 | for (int i = 0; i < mNumStreams; i++) { |
| 584 | dprintf(fd, "Stream %d/%d:\n", i, mNumStreams); |
| 585 | mStreams[i]->dump(fd); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | const char* Camera::templateToString(int type) |
| 590 | { |
| 591 | switch (type) { |
| 592 | case CAMERA3_TEMPLATE_PREVIEW: |
| 593 | return "CAMERA3_TEMPLATE_PREVIEW"; |
| 594 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
| 595 | return "CAMERA3_TEMPLATE_STILL_CAPTURE"; |
| 596 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 597 | return "CAMERA3_TEMPLATE_VIDEO_RECORD"; |
| 598 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 599 | return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT"; |
| 600 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: |
| 601 | return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG"; |
| 602 | } |
| 603 | // TODO: support vendor templates |
| 604 | return "Invalid template type!"; |
| 605 | } |
| 606 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 607 | extern "C" { |
| 608 | // Get handle to camera from device priv data |
| 609 | static Camera *camdev_to_camera(const camera3_device_t *dev) |
| 610 | { |
| 611 | return reinterpret_cast<Camera*>(dev->priv); |
| 612 | } |
| 613 | |
| 614 | static int initialize(const camera3_device_t *dev, |
| 615 | const camera3_callback_ops_t *callback_ops) |
| 616 | { |
| 617 | return camdev_to_camera(dev)->initialize(callback_ops); |
| 618 | } |
| 619 | |
| 620 | static int configure_streams(const camera3_device_t *dev, |
| 621 | camera3_stream_configuration_t *stream_list) |
| 622 | { |
| 623 | return camdev_to_camera(dev)->configureStreams(stream_list); |
| 624 | } |
| 625 | |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 626 | static const camera_metadata_t *construct_default_request_settings( |
| 627 | const camera3_device_t *dev, int type) |
| 628 | { |
| 629 | return camdev_to_camera(dev)->constructDefaultRequestSettings(type); |
| 630 | } |
| 631 | |
| 632 | static int process_capture_request(const camera3_device_t *dev, |
| 633 | camera3_capture_request_t *request) |
| 634 | { |
| 635 | return camdev_to_camera(dev)->processCaptureRequest(request); |
| 636 | } |
| 637 | |
| 638 | static void dump(const camera3_device_t *dev, int fd) |
| 639 | { |
| 640 | camdev_to_camera(dev)->dump(fd); |
| 641 | } |
| 642 | |
| 643 | static int flush(const camera3_device_t*) |
| 644 | { |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 645 | // TODO(b/29937783) |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 646 | ALOGE("%s: unimplemented.", __func__); |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | } // extern "C" |
| 651 | |
| 652 | const camera3_device_ops_t Camera::sOps = { |
| 653 | .initialize = default_camera_hal::initialize, |
| 654 | .configure_streams = default_camera_hal::configure_streams, |
Ari Hausman-Cohen | 900c1e3 | 2016-06-20 16:52:41 -0700 | [diff] [blame] | 655 | .register_stream_buffers = nullptr, |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 656 | .construct_default_request_settings |
| 657 | = default_camera_hal::construct_default_request_settings, |
| 658 | .process_capture_request = default_camera_hal::process_capture_request, |
Ari Hausman-Cohen | 72fddb3 | 2016-06-30 16:53:31 -0700 | [diff] [blame] | 659 | .get_metadata_vendor_tag_ops = nullptr, |
Ari Hausman-Cohen | 7344215 | 2016-06-08 15:50:49 -0700 | [diff] [blame] | 660 | .dump = default_camera_hal::dump, |
| 661 | .flush = default_camera_hal::flush, |
| 662 | .reserved = {0}, |
| 663 | }; |
| 664 | |
Ari Hausman-Cohen | 3841a7f | 2016-07-19 17:27:52 -0700 | [diff] [blame] | 665 | } // namespace default_camera_hal |