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 | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 21 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 22 | //#define LOG_NDEBUG 0 |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 23 | #define LOG_TAG "Camera" |
| 24 | #include <cutils/log.h> |
| 25 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 26 | #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) |
| 27 | #include <cutils/trace.h> |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame^] | 28 | #include "ScopedTrace.h" |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame] | 29 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 30 | #include "Camera.h" |
| 31 | |
| 32 | namespace default_camera_hal { |
| 33 | |
| 34 | extern "C" { |
| 35 | // Shim passed to the framework to close an opened device. |
| 36 | static int close_device(hw_device_t* dev) |
| 37 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 38 | camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 39 | Camera* cam = static_cast<Camera*>(cam_dev->priv); |
| 40 | return cam->close(); |
| 41 | } |
| 42 | } // extern "C" |
| 43 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 44 | Camera::Camera(int id) |
| 45 | : mId(id), |
| 46 | mBusy(false), |
| 47 | mCallbackOps(NULL) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 48 | { |
| 49 | pthread_mutex_init(&mMutex, |
| 50 | NULL); // No pthread mutex attributes. |
| 51 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 52 | memset(&mDevice, 0, sizeof(mDevice)); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 53 | mDevice.common.tag = HARDWARE_DEVICE_TAG; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 54 | mDevice.common.close = close_device; |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 55 | mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 56 | mDevice.priv = this; |
| 57 | } |
| 58 | |
| 59 | Camera::~Camera() |
| 60 | { |
| 61 | } |
| 62 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 63 | int Camera::open(const hw_module_t *module, hw_device_t **device) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 64 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 65 | ALOGI("%s:%d: Opening camera device", __func__, mId); |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame^] | 66 | CAMTRACE_CALL(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 67 | pthread_mutex_lock(&mMutex); |
| 68 | if (mBusy) { |
| 69 | pthread_mutex_unlock(&mMutex); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 70 | ALOGE("%s:%d: Error! Camera device already opened", __func__, mId); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 71 | return -EBUSY; |
| 72 | } |
| 73 | |
| 74 | // TODO: open camera dev nodes, etc |
| 75 | mBusy = true; |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 76 | mDevice.common.module = const_cast<hw_module_t*>(module); |
| 77 | *device = &mDevice.common; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 78 | |
| 79 | pthread_mutex_unlock(&mMutex); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int Camera::close() |
| 84 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 85 | ALOGI("%s:%d: Closing camera device", __func__, mId); |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame^] | 86 | CAMTRACE_CALL(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 87 | pthread_mutex_lock(&mMutex); |
| 88 | if (!mBusy) { |
| 89 | pthread_mutex_unlock(&mMutex); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 90 | ALOGE("%s:%d: Error! Camera device not open", __func__, mId); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 91 | return -EINVAL; |
| 92 | } |
| 93 | |
| 94 | // TODO: close camera dev nodes, etc |
| 95 | mBusy = false; |
| 96 | |
| 97 | pthread_mutex_unlock(&mMutex); |
| 98 | return 0; |
| 99 | } |
| 100 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 101 | int Camera::initialize(const camera3_callback_ops_t *callback_ops) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 102 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 103 | ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops); |
| 104 | mCallbackOps = callback_ops; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 108 | int Camera::configureStreams(camera3_stream_configuration_t *stream_list) |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 109 | { |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 110 | ALOGV("%s:%d: stream_list=%p", __func__, mId, stream_list); |
| 111 | // TODO: validate input, create internal stream representations |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set) |
| 116 | { |
| 117 | ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set); |
| 118 | // TODO: register buffers with hardware |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | const camera_metadata_t* Camera::constructDefaultRequestSettings(int type) |
| 123 | { |
| 124 | ALOGV("%s:%d: type=%d", __func__, mId, type); |
| 125 | // TODO: return statically built default request |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | int Camera::processCaptureRequest(camera3_capture_request_t *request) |
| 130 | { |
| 131 | ALOGV("%s:%d: request=%p", __func__, mId, request); |
Alex Ray | c16e56d | 2013-04-29 12:24:49 -0700 | [diff] [blame^] | 132 | CAMTRACE_CALL(); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 133 | |
| 134 | if (request == NULL) { |
| 135 | ALOGE("%s:%d: NULL request recieved", __func__, mId); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 136 | return -EINVAL; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 139 | // TODO: verify request; submit request to hardware |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 140 | return 0; |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 143 | void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops) |
| 144 | { |
| 145 | ALOGV("%s:%d: ops=%p", __func__, mId, ops); |
| 146 | // TODO: return vendor tag ops |
| 147 | } |
| 148 | |
| 149 | void Camera::dump(int fd) |
| 150 | { |
Alex Ray | af3a461 | 2013-04-29 14:16:10 -0700 | [diff] [blame] | 151 | ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd); |
Alex Ray | a0ed4be | 2013-02-25 15:02:16 -0800 | [diff] [blame] | 152 | // TODO: dprintf all relevant state to fd |
| 153 | } |
| 154 | |
| 155 | extern "C" { |
| 156 | // Get handle to camera from device priv data |
| 157 | static Camera *camdev_to_camera(const camera3_device_t *dev) |
| 158 | { |
| 159 | return reinterpret_cast<Camera*>(dev->priv); |
| 160 | } |
| 161 | |
| 162 | static int initialize(const camera3_device_t *dev, |
| 163 | const camera3_callback_ops_t *callback_ops) |
| 164 | { |
| 165 | return camdev_to_camera(dev)->initialize(callback_ops); |
| 166 | } |
| 167 | |
| 168 | static int configure_streams(const camera3_device_t *dev, |
| 169 | camera3_stream_configuration_t *stream_list) |
| 170 | { |
| 171 | return camdev_to_camera(dev)->configureStreams(stream_list); |
| 172 | } |
| 173 | |
| 174 | static int register_stream_buffers(const camera3_device_t *dev, |
| 175 | const camera3_stream_buffer_set_t *buffer_set) |
| 176 | { |
| 177 | return camdev_to_camera(dev)->registerStreamBuffers(buffer_set); |
| 178 | } |
| 179 | |
| 180 | static const camera_metadata_t *construct_default_request_settings( |
| 181 | const camera3_device_t *dev, int type) |
| 182 | { |
| 183 | return camdev_to_camera(dev)->constructDefaultRequestSettings(type); |
| 184 | } |
| 185 | |
| 186 | static int process_capture_request(const camera3_device_t *dev, |
| 187 | camera3_capture_request_t *request) |
| 188 | { |
| 189 | return camdev_to_camera(dev)->processCaptureRequest(request); |
| 190 | } |
| 191 | |
| 192 | static void get_metadata_vendor_tag_ops(const camera3_device_t *dev, |
| 193 | vendor_tag_query_ops_t *ops) |
| 194 | { |
| 195 | camdev_to_camera(dev)->getMetadataVendorTagOps(ops); |
| 196 | } |
| 197 | |
| 198 | static void dump(const camera3_device_t *dev, int fd) |
| 199 | { |
| 200 | camdev_to_camera(dev)->dump(fd); |
| 201 | } |
| 202 | } // extern "C" |
| 203 | |
| 204 | const camera3_device_ops_t Camera::sOps = { |
| 205 | .initialize = default_camera_hal::initialize, |
| 206 | .configure_streams = default_camera_hal::configure_streams, |
| 207 | .register_stream_buffers = default_camera_hal::register_stream_buffers, |
| 208 | .construct_default_request_settings = |
| 209 | default_camera_hal::construct_default_request_settings, |
| 210 | .process_capture_request = default_camera_hal::process_capture_request, |
| 211 | .get_metadata_vendor_tag_ops = |
| 212 | default_camera_hal::get_metadata_vendor_tag_ops, |
| 213 | .dump = default_camera_hal::dump |
| 214 | }; |
| 215 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 216 | } // namespace default_camera_hal |