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