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> |
| 18 | #include <pthread.h> |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 19 | #include <hardware/camera3.h> |
| 20 | #include "CameraHAL.h" |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 21 | #include "Stream.h" |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 22 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 23 | //#define LOG_NDEBUG 0 |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 24 | #define LOG_TAG "Camera" |
| 25 | #include <cutils/log.h> |
| 26 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 27 | #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) |
| 28 | #include <cutils/trace.h> |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame] | 29 | #include "ScopedTrace.h" |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 30 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 31 | #include "Camera.h" |
| 32 | |
| 33 | namespace default_camera_hal { |
| 34 | |
| 35 | extern "C" { |
| 36 | // Shim passed to the framework to close an opened device. |
| 37 | static int close_device(hw_device_t* dev) |
| 38 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 39 | camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 40 | Camera* cam = static_cast<Camera*>(cam_dev->priv); |
| 41 | return cam->close(); |
| 42 | } |
| 43 | } // extern "C" |
| 44 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 45 | Camera::Camera(int id) |
| 46 | : mId(id), |
| 47 | mBusy(false), |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 48 | mCallbackOps(NULL), |
| 49 | mStreams(NULL), |
| 50 | mNumStreams(0) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 51 | { |
| 52 | pthread_mutex_init(&mMutex, |
| 53 | NULL); // No pthread mutex attributes. |
| 54 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 55 | memset(&mDevice, 0, sizeof(mDevice)); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 56 | mDevice.common.tag = HARDWARE_DEVICE_TAG; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 57 | mDevice.common.close = close_device; |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 58 | mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 59 | mDevice.priv = this; |
| 60 | } |
| 61 | |
| 62 | Camera::~Camera() |
| 63 | { |
| 64 | } |
| 65 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 66 | int Camera::open(const hw_module_t *module, hw_device_t **device) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 67 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 68 | ALOGI("%s:%d: Opening camera device", __func__, mId); |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame] | 69 | CAMTRACE_CALL(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 70 | pthread_mutex_lock(&mMutex); |
| 71 | if (mBusy) { |
| 72 | pthread_mutex_unlock(&mMutex); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 73 | ALOGE("%s:%d: Error! Camera device already opened", __func__, mId); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 74 | return -EBUSY; |
| 75 | } |
| 76 | |
| 77 | // TODO: open camera dev nodes, etc |
| 78 | mBusy = true; |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 79 | mDevice.common.module = const_cast<hw_module_t*>(module); |
| 80 | *device = &mDevice.common; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 81 | |
| 82 | pthread_mutex_unlock(&mMutex); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int Camera::close() |
| 87 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 88 | ALOGI("%s:%d: Closing camera device", __func__, mId); |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame] | 89 | CAMTRACE_CALL(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 90 | pthread_mutex_lock(&mMutex); |
| 91 | if (!mBusy) { |
| 92 | pthread_mutex_unlock(&mMutex); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 93 | ALOGE("%s:%d: Error! Camera device not open", __func__, mId); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | // TODO: close camera dev nodes, etc |
| 98 | mBusy = false; |
| 99 | |
| 100 | pthread_mutex_unlock(&mMutex); |
| 101 | return 0; |
| 102 | } |
| 103 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 104 | int Camera::initialize(const camera3_callback_ops_t *callback_ops) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 105 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 106 | ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops); |
| 107 | mCallbackOps = callback_ops; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 111 | int Camera::configureStreams(camera3_stream_configuration_t *stream_config) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 112 | { |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 113 | camera3_stream_t *astream; |
| 114 | Stream **newStreams = NULL; |
| 115 | |
| 116 | CAMTRACE_CALL(); |
| 117 | ALOGV("%s:%d: stream_config=%p", __func__, mId, stream_config); |
| 118 | |
| 119 | if (stream_config == NULL) { |
| 120 | ALOGE("%s:%d: NULL stream configuration array", __func__, mId); |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | if (stream_config->num_streams == 0) { |
| 124 | ALOGE("%s:%d: Empty stream configuration array", __func__, mId); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | // Create new stream array |
| 129 | newStreams = new Stream*[stream_config->num_streams]; |
| 130 | ALOGV("%s:%d: Number of Streams: %d", __func__, mId, |
| 131 | stream_config->num_streams); |
| 132 | |
| 133 | pthread_mutex_lock(&mMutex); |
| 134 | |
| 135 | // Mark all current streams unused for now |
| 136 | for (int i = 0; i < mNumStreams; i++) |
| 137 | mStreams[i]->mReuse = false; |
| 138 | // Fill new stream array with reused streams and new streams |
| 139 | for (int i = 0; i < stream_config->num_streams; i++) { |
| 140 | astream = stream_config->streams[i]; |
| 141 | if (astream->max_buffers > 0) |
| 142 | newStreams[i] = reuseStream(astream); |
| 143 | else |
| 144 | newStreams[i] = new Stream(mId, astream); |
| 145 | |
| 146 | if (newStreams[i] == NULL) { |
| 147 | ALOGE("%s:%d: Error processing stream %d", __func__, mId, i); |
| 148 | goto err_out; |
| 149 | } |
| 150 | astream->priv = newStreams[i]; |
| 151 | } |
| 152 | |
| 153 | // Verify the set of streams in aggregate |
| 154 | if (!isValidStreamSet(newStreams, stream_config->num_streams)) { |
| 155 | ALOGE("%s:%d: Invalid stream set", __func__, mId); |
| 156 | goto err_out; |
| 157 | } |
| 158 | |
| 159 | // Set up all streams (calculate usage/max_buffers for each) |
| 160 | setupStreams(newStreams, stream_config->num_streams); |
| 161 | |
| 162 | // Destroy all old streams and replace stream array with new one |
| 163 | destroyStreams(mStreams, mNumStreams); |
| 164 | mStreams = newStreams; |
| 165 | mNumStreams = stream_config->num_streams; |
| 166 | |
| 167 | pthread_mutex_unlock(&mMutex); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 168 | return 0; |
Alex Ray | bcaf788 | 2013-02-28 16:04:35 -0800 | [diff] [blame] | 169 | |
| 170 | err_out: |
| 171 | // Clean up temporary streams, preserve existing mStreams/mNumStreams |
| 172 | destroyStreams(newStreams, stream_config->num_streams); |
| 173 | pthread_mutex_unlock(&mMutex); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | void Camera::destroyStreams(Stream **streams, int count) |
| 178 | { |
| 179 | if (streams == NULL) |
| 180 | return; |
| 181 | for (int i = 0; i < count; i++) { |
| 182 | // Only destroy streams that weren't reused |
| 183 | if (streams[i] != NULL && !streams[i]->mReuse) |
| 184 | delete streams[i]; |
| 185 | } |
| 186 | delete [] streams; |
| 187 | } |
| 188 | |
| 189 | Stream *Camera::reuseStream(camera3_stream_t *astream) |
| 190 | { |
| 191 | Stream *priv = reinterpret_cast<Stream*>(astream->priv); |
| 192 | // Verify the re-used stream's parameters match |
| 193 | if (!priv->isValidReuseStream(mId, astream)) { |
| 194 | ALOGE("%s:%d: Mismatched parameter in reused stream", __func__, mId); |
| 195 | return NULL; |
| 196 | } |
| 197 | // Mark stream to be reused |
| 198 | priv->mReuse = true; |
| 199 | return priv; |
| 200 | } |
| 201 | |
| 202 | bool Camera::isValidStreamSet(Stream **streams, int count) |
| 203 | { |
| 204 | int inputs = 0; |
| 205 | int outputs = 0; |
| 206 | |
| 207 | if (streams == NULL) { |
| 208 | ALOGE("%s:%d: NULL stream configuration streams", __func__, mId); |
| 209 | return false; |
| 210 | } |
| 211 | if (count == 0) { |
| 212 | ALOGE("%s:%d: Zero count stream configuration streams", __func__, mId); |
| 213 | return false; |
| 214 | } |
| 215 | // Validate there is at most one input stream and at least one output stream |
| 216 | for (int i = 0; i < count; i++) { |
| 217 | // A stream may be both input and output (bidirectional) |
| 218 | if (streams[i]->isInputType()) |
| 219 | inputs++; |
| 220 | if (streams[i]->isOutputType()) |
| 221 | outputs++; |
| 222 | } |
| 223 | if (outputs < 1) { |
| 224 | ALOGE("%s:%d: Stream config must have >= 1 output", __func__, mId); |
| 225 | return false; |
| 226 | } |
| 227 | if (inputs > 1) { |
| 228 | ALOGE("%s:%d: Stream config must have <= 1 input", __func__, mId); |
| 229 | return false; |
| 230 | } |
| 231 | // TODO: check for correct number of Bayer/YUV/JPEG/Encoder streams |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | void Camera::setupStreams(Stream **streams, int count) |
| 236 | { |
| 237 | /* |
| 238 | * This is where the HAL has to decide internally how to handle all of the |
| 239 | * streams, and then produce usage and max_buffer values for each stream. |
| 240 | * Note, the stream array has been checked before this point for ALL invalid |
| 241 | * conditions, so it must find a successful configuration for this stream |
| 242 | * array. The HAL may not return an error from this point. |
| 243 | * |
| 244 | * In this demo HAL, we just set all streams to be the same dummy values; |
| 245 | * real implementations will want to avoid USAGE_SW_{READ|WRITE}_OFTEN. |
| 246 | */ |
| 247 | for (int i = 0; i < count; i++) { |
| 248 | uint32_t usage = 0; |
| 249 | |
| 250 | if (streams[i]->isOutputType()) |
| 251 | usage |= GRALLOC_USAGE_SW_WRITE_OFTEN | |
| 252 | GRALLOC_USAGE_HW_CAMERA_WRITE; |
| 253 | if (streams[i]->isInputType()) |
| 254 | usage |= GRALLOC_USAGE_SW_READ_OFTEN | |
| 255 | GRALLOC_USAGE_HW_CAMERA_READ; |
| 256 | |
| 257 | streams[i]->setUsage(usage); |
| 258 | streams[i]->setMaxBuffers(1); |
| 259 | } |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set) |
| 263 | { |
| 264 | ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set); |
Alex Ray | 8a8f86b | 2013-03-01 01:32:21 -0800 | [diff] [blame^] | 265 | if (buf_set == NULL) { |
| 266 | ALOGE("%s:%d: NULL buffer set", __func__, mId); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | if (buf_set->stream == NULL) { |
| 270 | ALOGE("%s:%d: NULL stream handle", __func__, mId); |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | Stream *stream = reinterpret_cast<Stream*>(buf_set->stream->priv); |
| 274 | return stream->registerBuffers(buf_set); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | const camera_metadata_t* Camera::constructDefaultRequestSettings(int type) |
| 278 | { |
| 279 | ALOGV("%s:%d: type=%d", __func__, mId, type); |
| 280 | // TODO: return statically built default request |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | int Camera::processCaptureRequest(camera3_capture_request_t *request) |
| 285 | { |
| 286 | ALOGV("%s:%d: request=%p", __func__, mId, request); |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame] | 287 | CAMTRACE_CALL(); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 288 | |
| 289 | if (request == NULL) { |
| 290 | ALOGE("%s:%d: NULL request recieved", __func__, mId); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 291 | return -EINVAL; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 294 | // TODO: verify request; submit request to hardware |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 295 | return 0; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 298 | void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops) |
| 299 | { |
| 300 | ALOGV("%s:%d: ops=%p", __func__, mId, ops); |
| 301 | // TODO: return vendor tag ops |
| 302 | } |
| 303 | |
| 304 | void Camera::dump(int fd) |
| 305 | { |
Alex Ray | af3a461 | 2013-04-29 14:16:10 -0700 | [diff] [blame] | 306 | ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 307 | // TODO: dprintf all relevant state to fd |
| 308 | } |
| 309 | |
| 310 | extern "C" { |
| 311 | // Get handle to camera from device priv data |
| 312 | static Camera *camdev_to_camera(const camera3_device_t *dev) |
| 313 | { |
| 314 | return reinterpret_cast<Camera*>(dev->priv); |
| 315 | } |
| 316 | |
| 317 | static int initialize(const camera3_device_t *dev, |
| 318 | const camera3_callback_ops_t *callback_ops) |
| 319 | { |
| 320 | return camdev_to_camera(dev)->initialize(callback_ops); |
| 321 | } |
| 322 | |
| 323 | static int configure_streams(const camera3_device_t *dev, |
| 324 | camera3_stream_configuration_t *stream_list) |
| 325 | { |
| 326 | return camdev_to_camera(dev)->configureStreams(stream_list); |
| 327 | } |
| 328 | |
| 329 | static int register_stream_buffers(const camera3_device_t *dev, |
| 330 | const camera3_stream_buffer_set_t *buffer_set) |
| 331 | { |
| 332 | return camdev_to_camera(dev)->registerStreamBuffers(buffer_set); |
| 333 | } |
| 334 | |
| 335 | static const camera_metadata_t *construct_default_request_settings( |
| 336 | const camera3_device_t *dev, int type) |
| 337 | { |
| 338 | return camdev_to_camera(dev)->constructDefaultRequestSettings(type); |
| 339 | } |
| 340 | |
| 341 | static int process_capture_request(const camera3_device_t *dev, |
| 342 | camera3_capture_request_t *request) |
| 343 | { |
| 344 | return camdev_to_camera(dev)->processCaptureRequest(request); |
| 345 | } |
| 346 | |
| 347 | static void get_metadata_vendor_tag_ops(const camera3_device_t *dev, |
| 348 | vendor_tag_query_ops_t *ops) |
| 349 | { |
| 350 | camdev_to_camera(dev)->getMetadataVendorTagOps(ops); |
| 351 | } |
| 352 | |
| 353 | static void dump(const camera3_device_t *dev, int fd) |
| 354 | { |
| 355 | camdev_to_camera(dev)->dump(fd); |
| 356 | } |
| 357 | } // extern "C" |
| 358 | |
| 359 | const camera3_device_ops_t Camera::sOps = { |
| 360 | .initialize = default_camera_hal::initialize, |
| 361 | .configure_streams = default_camera_hal::configure_streams, |
| 362 | .register_stream_buffers = default_camera_hal::register_stream_buffers, |
| 363 | .construct_default_request_settings = |
| 364 | default_camera_hal::construct_default_request_settings, |
| 365 | .process_capture_request = default_camera_hal::process_capture_request, |
| 366 | .get_metadata_vendor_tag_ops = |
| 367 | default_camera_hal::get_metadata_vendor_tag_ops, |
| 368 | .dump = default_camera_hal::dump |
| 369 | }; |
| 370 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 371 | } // namespace default_camera_hal |