Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #include <cstdlib> |
Alex Ray | 69f1f91 | 2013-10-21 00:46:44 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 19 | #include <hardware/camera3.h> |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 20 | #include <sync/sync.h> |
Alex Ray | bfcbd95 | 2013-03-20 13:20:02 -0700 | [diff] [blame] | 21 | #include <system/camera_metadata.h> |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 22 | #include <system/graphics.h> |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 23 | #include <utils/Mutex.h> |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 24 | #include "CameraHAL.h" |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 25 | #include "Metadata.h" |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 26 | #include "Stream.h" |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 27 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 28 | //#define LOG_NDEBUG 0 |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 29 | #define LOG_TAG "Camera" |
| 30 | #include <cutils/log.h> |
| 31 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 32 | #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) |
Alex Ray | ea80382 | 2013-10-14 15:56:43 -0700 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 34 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 35 | #include "Camera.h" |
| 36 | |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 37 | #define CAMERA_SYNC_TIMEOUT 5000 // in msecs |
| 38 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 39 | namespace default_camera_hal { |
| 40 | |
| 41 | extern "C" { |
| 42 | // Shim passed to the framework to close an opened device. |
| 43 | static int close_device(hw_device_t* dev) |
| 44 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 45 | camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 46 | Camera* cam = static_cast<Camera*>(cam_dev->priv); |
| 47 | return cam->close(); |
| 48 | } |
| 49 | } // extern "C" |
| 50 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 51 | Camera::Camera(int id) |
| 52 | : mId(id), |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 53 | mStaticInfo(NULL), |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 54 | mBusy(false), |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 55 | mCallbackOps(NULL), |
| 56 | mStreams(NULL), |
Alex Ray | bfcbd95 | 2013-03-20 13:20:02 -0700 | [diff] [blame] | 57 | mNumStreams(0), |
| 58 | mSettings(NULL) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 59 | { |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 60 | memset(&mTemplates, 0, sizeof(mTemplates)); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 61 | memset(&mDevice, 0, sizeof(mDevice)); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 62 | mDevice.common.tag = HARDWARE_DEVICE_TAG; |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 63 | mDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 64 | mDevice.common.close = close_device; |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 65 | mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 66 | mDevice.priv = this; |
| 67 | } |
| 68 | |
| 69 | Camera::~Camera() |
| 70 | { |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 71 | if (mStaticInfo != NULL) { |
| 72 | free_camera_metadata(mStaticInfo); |
| 73 | } |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 76 | int Camera::open(const hw_module_t *module, hw_device_t **device) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 77 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 78 | ALOGI("%s:%d: Opening camera device", __func__, mId); |
Alex Ray | ea80382 | 2013-10-14 15:56:43 -0700 | [diff] [blame] | 79 | ATRACE_CALL(); |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 80 | android::Mutex::Autolock al(mDeviceLock); |
| 81 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 82 | if (mBusy) { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 83 | ALOGE("%s:%d: Error! Camera device already opened", __func__, mId); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 84 | return -EBUSY; |
| 85 | } |
| 86 | |
| 87 | // TODO: open camera dev nodes, etc |
| 88 | mBusy = true; |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 89 | mDevice.common.module = const_cast<hw_module_t*>(module); |
| 90 | *device = &mDevice.common; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 94 | int Camera::getInfo(struct camera_info *info) |
| 95 | { |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 96 | android::Mutex::Autolock al(mStaticInfoLock); |
| 97 | |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 98 | info->facing = CAMERA_FACING_FRONT; |
| 99 | info->orientation = 0; |
| 100 | info->device_version = mDevice.common.version; |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 101 | if (mStaticInfo == NULL) { |
Alex Ray | 0f82f5a | 2013-07-17 14:23:04 -0700 | [diff] [blame] | 102 | mStaticInfo = initStaticInfo(); |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 103 | } |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 104 | info->static_camera_characteristics = mStaticInfo; |
Alex Ray | 0f82f5a | 2013-07-17 14:23:04 -0700 | [diff] [blame] | 105 | return 0; |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 108 | int Camera::close() |
| 109 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 110 | ALOGI("%s:%d: Closing camera device", __func__, mId); |
Alex Ray | ea80382 | 2013-10-14 15:56:43 -0700 | [diff] [blame] | 111 | ATRACE_CALL(); |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 112 | android::Mutex::Autolock al(mDeviceLock); |
| 113 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 114 | if (!mBusy) { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 115 | ALOGE("%s:%d: Error! Camera device not open", __func__, mId); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 116 | return -EINVAL; |
| 117 | } |
| 118 | |
| 119 | // TODO: close camera dev nodes, etc |
| 120 | mBusy = false; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 124 | int Camera::initialize(const camera3_callback_ops_t *callback_ops) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 125 | { |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 126 | int res; |
| 127 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 128 | ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops); |
| 129 | mCallbackOps = callback_ops; |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 130 | // per-device specific initialization |
| 131 | res = initDevice(); |
| 132 | if (res != 0) { |
| 133 | ALOGE("%s:%d: Failed to initialize device!", __func__, mId); |
| 134 | return res; |
Alex Ray | 89a8266 | 2013-05-28 20:32:48 -0700 | [diff] [blame] | 135 | } |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 139 | int Camera::configureStreams(camera3_stream_configuration_t *stream_config) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 140 | { |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 141 | camera3_stream_t *astream; |
| 142 | Stream **newStreams = NULL; |
| 143 | |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 144 | ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config); |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 145 | ATRACE_CALL(); |
| 146 | android::Mutex::Autolock al(mDeviceLock); |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 147 | |
| 148 | if (stream_config == NULL) { |
| 149 | ALOGE("%s:%d: NULL stream configuration array", __func__, mId); |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | if (stream_config->num_streams == 0) { |
| 153 | ALOGE("%s:%d: Empty stream configuration array", __func__, mId); |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
| 157 | // Create new stream array |
| 158 | newStreams = new Stream*[stream_config->num_streams]; |
| 159 | ALOGV("%s:%d: Number of Streams: %d", __func__, mId, |
| 160 | stream_config->num_streams); |
| 161 | |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 162 | // Mark all current streams unused for now |
| 163 | for (int i = 0; i < mNumStreams; i++) |
| 164 | mStreams[i]->mReuse = false; |
| 165 | // Fill new stream array with reused streams and new streams |
Alex Ray | c6bf2f2 | 2013-05-28 15:52:04 -0700 | [diff] [blame] | 166 | for (unsigned int i = 0; i < stream_config->num_streams; i++) { |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 167 | astream = stream_config->streams[i]; |
Alex Ray | 2b286da | 2013-05-29 15:08:29 -0700 | [diff] [blame] | 168 | if (astream->max_buffers > 0) { |
| 169 | ALOGV("%s:%d: Reusing stream %d", __func__, mId, i); |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 170 | newStreams[i] = reuseStream(astream); |
Alex Ray | 2b286da | 2013-05-29 15:08:29 -0700 | [diff] [blame] | 171 | } else { |
| 172 | ALOGV("%s:%d: Creating new stream %d", __func__, mId, i); |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 173 | newStreams[i] = new Stream(mId, astream); |
Alex Ray | 2b286da | 2013-05-29 15:08:29 -0700 | [diff] [blame] | 174 | } |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 175 | |
| 176 | if (newStreams[i] == NULL) { |
| 177 | ALOGE("%s:%d: Error processing stream %d", __func__, mId, i); |
| 178 | goto err_out; |
| 179 | } |
| 180 | astream->priv = newStreams[i]; |
| 181 | } |
| 182 | |
| 183 | // Verify the set of streams in aggregate |
| 184 | if (!isValidStreamSet(newStreams, stream_config->num_streams)) { |
| 185 | ALOGE("%s:%d: Invalid stream set", __func__, mId); |
| 186 | goto err_out; |
| 187 | } |
| 188 | |
| 189 | // Set up all streams (calculate usage/max_buffers for each) |
| 190 | setupStreams(newStreams, stream_config->num_streams); |
| 191 | |
| 192 | // Destroy all old streams and replace stream array with new one |
| 193 | destroyStreams(mStreams, mNumStreams); |
| 194 | mStreams = newStreams; |
| 195 | mNumStreams = stream_config->num_streams; |
| 196 | |
Alex Ray | bfcbd95 | 2013-03-20 13:20:02 -0700 | [diff] [blame] | 197 | // Clear out last seen settings metadata |
| 198 | setSettings(NULL); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 199 | return 0; |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 200 | |
| 201 | err_out: |
| 202 | // Clean up temporary streams, preserve existing mStreams/mNumStreams |
| 203 | destroyStreams(newStreams, stream_config->num_streams); |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | void Camera::destroyStreams(Stream **streams, int count) |
| 208 | { |
| 209 | if (streams == NULL) |
| 210 | return; |
| 211 | for (int i = 0; i < count; i++) { |
| 212 | // Only destroy streams that weren't reused |
| 213 | if (streams[i] != NULL && !streams[i]->mReuse) |
| 214 | delete streams[i]; |
| 215 | } |
| 216 | delete [] streams; |
| 217 | } |
| 218 | |
| 219 | Stream *Camera::reuseStream(camera3_stream_t *astream) |
| 220 | { |
| 221 | Stream *priv = reinterpret_cast<Stream*>(astream->priv); |
| 222 | // Verify the re-used stream's parameters match |
| 223 | if (!priv->isValidReuseStream(mId, astream)) { |
| 224 | ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId); |
| 225 | return NULL; |
| 226 | } |
| 227 | // Mark stream to be reused |
| 228 | priv->mReuse = true; |
| 229 | return priv; |
| 230 | } |
| 231 | |
| 232 | bool Camera::isValidStreamSet(Stream **streams, int count) |
| 233 | { |
| 234 | int inputs = 0; |
| 235 | int outputs = 0; |
| 236 | |
| 237 | if (streams == NULL) { |
| 238 | ALOGE("%s:%d: NULL stream configuration streams", __func__, mId); |
| 239 | return false; |
| 240 | } |
| 241 | if (count == 0) { |
| 242 | ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId); |
| 243 | return false; |
| 244 | } |
| 245 | // Validate there is at most one input stream and at least one output stream |
| 246 | for (int i = 0; i < count; i++) { |
| 247 | // A stream may be both input and output (bidirectional) |
| 248 | if (streams[i]->isInputType()) |
| 249 | inputs++; |
| 250 | if (streams[i]->isOutputType()) |
| 251 | outputs++; |
| 252 | } |
Alex Ray | 768216e | 2013-07-02 16:56:14 -0700 | [diff] [blame] | 253 | ALOGV("%s:%d: Configuring %d output streams and %d input streams", |
| 254 | __func__, mId, outputs, inputs); |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 255 | if (outputs < 1) { |
| 256 | ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId); |
| 257 | return false; |
| 258 | } |
| 259 | if (inputs > 1) { |
| 260 | ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId); |
| 261 | return false; |
| 262 | } |
| 263 | // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | void Camera::setupStreams(Stream **streams, int count) |
| 268 | { |
| 269 | /* |
| 270 | * This is where the HAL has to decide internally how to handle all of the |
| 271 | * streams, and then produce usage and max_buffer values for each stream. |
| 272 | * Note, the stream array has been checked before this point for ALL invalid |
| 273 | * conditions, so it must find a successful configuration for this stream |
| 274 | * array. The HAL may not return an error from this point. |
| 275 | * |
| 276 | * In this demo HAL, we just set all streams to be the same dummy values; |
| 277 | * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN. |
| 278 | */ |
| 279 | for (int i = 0; i < count; i++) { |
| 280 | uint32_t usage = 0; |
| 281 | |
| 282 | if (streams[i]->isOutputType()) |
| 283 | usage |= GRALLOC_USAGE_SW_WRITE_OFTEN | |
| 284 | GRALLOC_USAGE_HW_CAMERA_WRITE; |
| 285 | if (streams[i]->isInputType()) |
| 286 | usage |= GRALLOC_USAGE_SW_READ_OFTEN | |
| 287 | GRALLOC_USAGE_HW_CAMERA_READ; |
| 288 | |
| 289 | streams[i]->setUsage(usage); |
| 290 | streams[i]->setMaxBuffers(1); |
| 291 | } |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set) |
| 295 | { |
| 296 | ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set); |
Alex Ray | 8a8f86b | 2013-03-01 01:32:21 -0800 | [diff] [blame] | 297 | if (buf_set == NULL) { |
| 298 | ALOGE("%s:%d: NULL buffer set", __func__, mId); |
| 299 | return -EINVAL; |
| 300 | } |
| 301 | if (buf_set->stream == NULL) { |
| 302 | ALOGE("%s:%d: NULL stream handle", __func__, mId); |
| 303 | return -EINVAL; |
| 304 | } |
| 305 | Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv); |
| 306 | return stream->registerBuffers(buf_set); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 309 | bool Camera::isValidTemplateType(int type) |
| 310 | { |
| 311 | return type < 1 || type >= CAMERA3_TEMPLATE_COUNT; |
| 312 | } |
| 313 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 314 | const camera_metadata_t* Camera::constructDefaultRequestSettings(int type) |
| 315 | { |
| 316 | ALOGV("%s:%d: type=%d", __func__, mId, type); |
Alex Ray | 89a8266 | 2013-05-28 20:32:48 -0700 | [diff] [blame] | 317 | |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 318 | if (!isValidTemplateType(type)) { |
Alex Ray | 89a8266 | 2013-05-28 20:32:48 -0700 | [diff] [blame] | 319 | ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type); |
| 320 | return NULL; |
| 321 | } |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 322 | return mTemplates[type]; |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | int Camera::processCaptureRequest(camera3_capture_request_t *request) |
| 326 | { |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 327 | camera3_capture_result result; |
| 328 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 329 | ALOGV("%s:%d: request=%p", __func__, mId, request); |
Alex Ray | ea80382 | 2013-10-14 15:56:43 -0700 | [diff] [blame] | 330 | ATRACE_CALL(); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 331 | |
| 332 | if (request == NULL) { |
| 333 | ALOGE("%s:%d: NULL request recieved", __func__, mId); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 334 | return -EINVAL; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Alex Ray | bfcbd95 | 2013-03-20 13:20:02 -0700 | [diff] [blame] | 337 | ALOGV("%s:%d: Request Frame:%d Settings:%p", __func__, mId, |
| 338 | request->frame_number, request->settings); |
| 339 | |
| 340 | // NULL indicates use last settings |
| 341 | if (request->settings == NULL) { |
| 342 | if (mSettings == NULL) { |
| 343 | ALOGE("%s:%d: NULL settings without previous set Frame:%d Req:%p", |
| 344 | __func__, mId, request->frame_number, request); |
| 345 | return -EINVAL; |
| 346 | } |
| 347 | } else { |
| 348 | setSettings(request->settings); |
| 349 | } |
| 350 | |
Alex Ray | 11bbeef | 2013-04-26 14:47:08 -0700 | [diff] [blame] | 351 | if (request->input_buffer != NULL) { |
| 352 | ALOGV("%s:%d: Reprocessing input buffer %p", __func__, mId, |
| 353 | request->input_buffer); |
| 354 | |
| 355 | if (!isValidReprocessSettings(request->settings)) { |
| 356 | ALOGE("%s:%d: Invalid settings for reprocess request: %p", |
| 357 | __func__, mId, request->settings); |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | } else { |
| 361 | ALOGV("%s:%d: Capturing new frame.", __func__, mId); |
| 362 | |
| 363 | if (!isValidCaptureSettings(request->settings)) { |
| 364 | ALOGE("%s:%d: Invalid settings for capture request: %p", |
| 365 | __func__, mId, request->settings); |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | } |
| 369 | |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 370 | if (request->num_output_buffers <= 0) { |
| 371 | ALOGE("%s:%d: Invalid number of output buffers: %d", __func__, mId, |
| 372 | request->num_output_buffers); |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | result.num_output_buffers = request->num_output_buffers; |
| 376 | result.output_buffers = new camera3_stream_buffer_t[result.num_output_buffers]; |
| 377 | for (unsigned int i = 0; i < request->num_output_buffers; i++) { |
| 378 | int res = processCaptureBuffer(&request->output_buffers[i], |
| 379 | const_cast<camera3_stream_buffer_t*>(&result.output_buffers[i])); |
| 380 | if (res) |
| 381 | goto err_out; |
| 382 | } |
| 383 | |
| 384 | result.frame_number = request->frame_number; |
| 385 | // TODO: return actual captured/reprocessed settings |
| 386 | result.result = request->settings; |
| 387 | // TODO: asynchronously return results |
Alex Ray | 764e442 | 2013-06-04 12:38:07 -0700 | [diff] [blame] | 388 | notifyShutter(request->frame_number, 0); |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 389 | mCallbackOps->process_capture_result(mCallbackOps, &result); |
| 390 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 391 | return 0; |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 392 | |
| 393 | err_out: |
| 394 | delete [] result.output_buffers; |
| 395 | // TODO: this should probably be a total device failure; transient for now |
| 396 | return -EINVAL; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Alex Ray | bfcbd95 | 2013-03-20 13:20:02 -0700 | [diff] [blame] | 399 | void Camera::setSettings(const camera_metadata_t *new_settings) |
| 400 | { |
| 401 | if (mSettings != NULL) { |
| 402 | free_camera_metadata(mSettings); |
| 403 | mSettings = NULL; |
| 404 | } |
| 405 | |
| 406 | if (new_settings != NULL) |
| 407 | mSettings = clone_camera_metadata(new_settings); |
| 408 | } |
| 409 | |
Alex Ray | c6bf2f2 | 2013-05-28 15:52:04 -0700 | [diff] [blame] | 410 | bool Camera::isValidReprocessSettings(const camera_metadata_t* /*settings*/) |
Alex Ray | 11bbeef | 2013-04-26 14:47:08 -0700 | [diff] [blame] | 411 | { |
| 412 | // TODO: reject settings that cannot be reprocessed |
| 413 | // input buffers unimplemented, use this to reject reprocessing requests |
| 414 | ALOGE("%s:%d: Input buffer reprocessing not implemented", __func__, mId); |
| 415 | return false; |
| 416 | } |
| 417 | |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 418 | int Camera::processCaptureBuffer(const camera3_stream_buffer_t *in, |
| 419 | camera3_stream_buffer_t *out) |
| 420 | { |
Alex Ray | 77ecfd7 | 2013-05-30 00:19:04 -0700 | [diff] [blame] | 421 | if (in->acquire_fence != -1) { |
| 422 | int res = sync_wait(in->acquire_fence, CAMERA_SYNC_TIMEOUT); |
| 423 | if (res == -ETIME) { |
| 424 | ALOGE("%s:%d: Timeout waiting on buffer acquire fence", |
| 425 | __func__, mId); |
| 426 | return res; |
| 427 | } else if (res) { |
| 428 | ALOGE("%s:%d: Error waiting on buffer acquire fence: %s(%d)", |
| 429 | __func__, mId, strerror(-res), res); |
| 430 | return res; |
| 431 | } |
Alex Ray | 083315c | 2013-04-26 19:32:29 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | out->stream = in->stream; |
| 435 | out->buffer = in->buffer; |
| 436 | out->status = CAMERA3_BUFFER_STATUS_OK; |
| 437 | // TODO: use driver-backed release fences |
| 438 | out->acquire_fence = -1; |
| 439 | out->release_fence = -1; |
| 440 | |
| 441 | // TODO: lock and software-paint buffer |
| 442 | return 0; |
| 443 | } |
| 444 | |
Alex Ray | 764e442 | 2013-06-04 12:38:07 -0700 | [diff] [blame] | 445 | void Camera::notifyShutter(uint32_t frame_number, uint64_t timestamp) |
| 446 | { |
| 447 | int res; |
| 448 | struct timespec ts; |
| 449 | |
| 450 | // If timestamp is 0, get timestamp from right now instead |
| 451 | if (timestamp == 0) { |
| 452 | ALOGW("%s:%d: No timestamp provided, using CLOCK_BOOTTIME", |
| 453 | __func__, mId); |
| 454 | res = clock_gettime(CLOCK_BOOTTIME, &ts); |
| 455 | if (res == 0) { |
| 456 | timestamp = ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 457 | } else { |
| 458 | ALOGE("%s:%d: No timestamp and failed to get CLOCK_BOOTTIME %s(%d)", |
| 459 | __func__, mId, strerror(errno), errno); |
| 460 | } |
| 461 | } |
| 462 | camera3_notify_msg_t m; |
| 463 | memset(&m, 0, sizeof(m)); |
| 464 | m.type = CAMERA3_MSG_SHUTTER; |
| 465 | m.message.shutter.frame_number = frame_number; |
| 466 | m.message.shutter.timestamp = timestamp; |
| 467 | mCallbackOps->notify(mCallbackOps, &m); |
| 468 | } |
| 469 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 470 | void Camera::dump(int fd) |
| 471 | { |
Alex Ray | af3a461 | 2013-04-29 14:16:10 -0700 | [diff] [blame] | 472 | ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd); |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 473 | ATRACE_CALL(); |
| 474 | android::Mutex::Autolock al(mDeviceLock); |
Alex Ray | 69f1f91 | 2013-10-21 00:46:44 -0700 | [diff] [blame] | 475 | |
Elliott Hughes | 0d1c2a4 | 2014-05-22 11:12:12 -0700 | [diff] [blame] | 476 | dprintf(fd, "Camera ID: %d (Busy: %d)\n", mId, mBusy); |
Alex Ray | 69f1f91 | 2013-10-21 00:46:44 -0700 | [diff] [blame] | 477 | |
| 478 | // TODO: dump all settings |
Elliott Hughes | 0d1c2a4 | 2014-05-22 11:12:12 -0700 | [diff] [blame] | 479 | dprintf(fd, "Most Recent Settings: (%p)\n", mSettings); |
Alex Ray | 69f1f91 | 2013-10-21 00:46:44 -0700 | [diff] [blame] | 480 | |
Elliott Hughes | 0d1c2a4 | 2014-05-22 11:12:12 -0700 | [diff] [blame] | 481 | dprintf(fd, "Number of streams: %d\n", mNumStreams); |
Alex Ray | 69f1f91 | 2013-10-21 00:46:44 -0700 | [diff] [blame] | 482 | for (int i = 0; i < mNumStreams; i++) { |
Elliott Hughes | 0d1c2a4 | 2014-05-22 11:12:12 -0700 | [diff] [blame] | 483 | dprintf(fd, "Stream %d/%d:\n", i, mNumStreams); |
Alex Ray | 69f1f91 | 2013-10-21 00:46:44 -0700 | [diff] [blame] | 484 | mStreams[i]->dump(fd); |
| 485 | } |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 488 | const char* Camera::templateToString(int type) |
| 489 | { |
| 490 | switch (type) { |
| 491 | case CAMERA3_TEMPLATE_PREVIEW: |
| 492 | return "CAMERA3_TEMPLATE_PREVIEW"; |
| 493 | case CAMERA3_TEMPLATE_STILL_CAPTURE: |
| 494 | return "CAMERA3_TEMPLATE_STILL_CAPTURE"; |
| 495 | case CAMERA3_TEMPLATE_VIDEO_RECORD: |
| 496 | return "CAMERA3_TEMPLATE_VIDEO_RECORD"; |
| 497 | case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT: |
| 498 | return "CAMERA3_TEMPLATE_VIDEO_SNAPSHOT"; |
| 499 | case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG: |
| 500 | return "CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG"; |
| 501 | } |
| 502 | // TODO: support vendor templates |
| 503 | return "Invalid template type!"; |
| 504 | } |
| 505 | |
| 506 | int Camera::setTemplate(int type, camera_metadata_t *settings) |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 507 | { |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 508 | android::Mutex::Autolock al(mDeviceLock); |
| 509 | |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 510 | if (!isValidTemplateType(type)) { |
| 511 | ALOGE("%s:%d: Invalid template request type: %d", __func__, mId, type); |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 512 | return -EINVAL; |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 513 | } |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 514 | |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 515 | if (mTemplates[type] != NULL) { |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 516 | ALOGE("%s:%d: Setting already constructed template type %s(%d)", |
| 517 | __func__, mId, templateToString(type), type); |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 518 | return -EINVAL; |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 519 | } |
Alex Ray | 5556764 | 2013-11-12 12:58:39 -0800 | [diff] [blame] | 520 | |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 521 | // Make a durable copy of the underlying metadata |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 522 | mTemplates[type] = clone_camera_metadata(settings); |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 523 | if (mTemplates[type] == NULL) { |
| 524 | ALOGE("%s:%d: Failed to clone metadata %p for template type %s(%d)", |
| 525 | __func__, mId, settings, templateToString(type), type); |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 526 | return -EINVAL; |
| 527 | } |
Alex Ray | 6273508 | 2013-10-21 12:55:24 -0700 | [diff] [blame] | 528 | return 0; |
Alex Ray | 61f7a0c | 2013-07-03 17:54:19 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 531 | extern "C" { |
| 532 | // Get handle to camera from device priv data |
| 533 | static Camera *camdev_to_camera(const camera3_device_t *dev) |
| 534 | { |
| 535 | return reinterpret_cast<Camera*>(dev->priv); |
| 536 | } |
| 537 | |
| 538 | static int initialize(const camera3_device_t *dev, |
| 539 | const camera3_callback_ops_t *callback_ops) |
| 540 | { |
| 541 | return camdev_to_camera(dev)->initialize(callback_ops); |
| 542 | } |
| 543 | |
| 544 | static int configure_streams(const camera3_device_t *dev, |
| 545 | camera3_stream_configuration_t *stream_list) |
| 546 | { |
| 547 | return camdev_to_camera(dev)->configureStreams(stream_list); |
| 548 | } |
| 549 | |
| 550 | static int register_stream_buffers(const camera3_device_t *dev, |
| 551 | const camera3_stream_buffer_set_t *buffer_set) |
| 552 | { |
| 553 | return camdev_to_camera(dev)->registerStreamBuffers(buffer_set); |
| 554 | } |
| 555 | |
| 556 | static const camera_metadata_t *construct_default_request_settings( |
| 557 | const camera3_device_t *dev, int type) |
| 558 | { |
| 559 | return camdev_to_camera(dev)->constructDefaultRequestSettings(type); |
| 560 | } |
| 561 | |
| 562 | static int process_capture_request(const camera3_device_t *dev, |
| 563 | camera3_capture_request_t *request) |
| 564 | { |
| 565 | return camdev_to_camera(dev)->processCaptureRequest(request); |
| 566 | } |
| 567 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 568 | static void dump(const camera3_device_t *dev, int fd) |
| 569 | { |
| 570 | camdev_to_camera(dev)->dump(fd); |
| 571 | } |
Sasha Levitskiy | a82f456 | 2014-04-21 17:20:17 -0700 | [diff] [blame] | 572 | |
| 573 | static int flush(const camera3_device_t*) |
| 574 | { |
| 575 | ALOGE("%s: unimplemented.", __func__); |
| 576 | return -1; |
| 577 | } |
| 578 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 579 | } // extern "C" |
| 580 | |
| 581 | const camera3_device_ops_t Camera::sOps = { |
Sasha Levitskiy | a82f456 | 2014-04-21 17:20:17 -0700 | [diff] [blame] | 582 | .initialize = default_camera_hal::initialize, |
| 583 | .configure_streams = default_camera_hal::configure_streams, |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 584 | .register_stream_buffers = default_camera_hal::register_stream_buffers, |
Sasha Levitskiy | a82f456 | 2014-04-21 17:20:17 -0700 | [diff] [blame] | 585 | .construct_default_request_settings |
| 586 | = default_camera_hal::construct_default_request_settings, |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 587 | .process_capture_request = default_camera_hal::process_capture_request, |
Ruben Brunk | 61cf9eb | 2014-01-14 15:27:58 -0800 | [diff] [blame] | 588 | .get_metadata_vendor_tag_ops = NULL, |
Sasha Levitskiy | a82f456 | 2014-04-21 17:20:17 -0700 | [diff] [blame] | 589 | .dump = default_camera_hal::dump, |
| 590 | .flush = default_camera_hal::flush, |
| 591 | .reserved = {0}, |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 592 | }; |
| 593 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 594 | } // namespace default_camera_hal |