blob: 203b77274e0f74af71f654327e7af57c6b464b88 [file] [log] [blame]
Alex Ray7ee0b7a2012-11-06 00:12:49 -08001/*
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 Raya0ed4be2013-02-25 15:02:16 -080019#include <hardware/camera3.h>
20#include "CameraHAL.h"
Alex Ray7ee0b7a2012-11-06 00:12:49 -080021
Alex Rayed6b8a72012-12-27 10:42:22 -080022//#define LOG_NDEBUG 0
Alex Ray7ee0b7a2012-11-06 00:12:49 -080023#define LOG_TAG "Camera"
24#include <cutils/log.h>
25
Alex Rayed6b8a72012-12-27 10:42:22 -080026#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
27#include <cutils/trace.h>
28
Alex Ray7ee0b7a2012-11-06 00:12:49 -080029#include "Camera.h"
30
31namespace default_camera_hal {
32
33extern "C" {
34// Shim passed to the framework to close an opened device.
35static int close_device(hw_device_t* dev)
36{
Alex Raya0ed4be2013-02-25 15:02:16 -080037 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080038 Camera* cam = static_cast<Camera*>(cam_dev->priv);
39 return cam->close();
40}
41} // extern "C"
42
Alex Raya0ed4be2013-02-25 15:02:16 -080043Camera::Camera(int id)
44 : mId(id),
45 mBusy(false),
46 mCallbackOps(NULL)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080047{
48 pthread_mutex_init(&mMutex,
49 NULL); // No pthread mutex attributes.
50
Alex Raya0ed4be2013-02-25 15:02:16 -080051 memset(&mDevice, 0, sizeof(mDevice));
Alex Ray7ee0b7a2012-11-06 00:12:49 -080052 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080053 mDevice.common.close = close_device;
Alex Raya0ed4be2013-02-25 15:02:16 -080054 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080055 mDevice.priv = this;
56}
57
58Camera::~Camera()
59{
60}
61
Alex Raya0ed4be2013-02-25 15:02:16 -080062int Camera::open(const hw_module_t *module, hw_device_t **device)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080063{
Alex Raya0ed4be2013-02-25 15:02:16 -080064 ALOGI("%s:%d: Opening camera device", __func__, mId);
65 ATRACE_BEGIN(__func__);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080066 pthread_mutex_lock(&mMutex);
67 if (mBusy) {
68 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -080069 ATRACE_END();
Alex Raya0ed4be2013-02-25 15:02:16 -080070 ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080071 return -EBUSY;
72 }
73
74 // TODO: open camera dev nodes, etc
75 mBusy = true;
Alex Raya0ed4be2013-02-25 15:02:16 -080076 mDevice.common.module = const_cast<hw_module_t*>(module);
77 *device = &mDevice.common;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080078
79 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -080080 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080081 return 0;
82}
83
84int Camera::close()
85{
Alex Raya0ed4be2013-02-25 15:02:16 -080086 ALOGI("%s:%d: Closing camera device", __func__, mId);
87 ATRACE_BEGIN(__func__);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080088 pthread_mutex_lock(&mMutex);
89 if (!mBusy) {
90 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -080091 ATRACE_END();
Alex Raya0ed4be2013-02-25 15:02:16 -080092 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080093 return -EINVAL;
94 }
95
96 // TODO: close camera dev nodes, etc
97 mBusy = false;
98
99 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -0800100 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800101 return 0;
102}
103
Alex Raya0ed4be2013-02-25 15:02:16 -0800104int Camera::initialize(const camera3_callback_ops_t *callback_ops)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800105{
Alex Raya0ed4be2013-02-25 15:02:16 -0800106 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
107 mCallbackOps = callback_ops;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800108 return 0;
109}
110
Alex Raya0ed4be2013-02-25 15:02:16 -0800111int Camera::configureStreams(camera3_stream_configuration_t *stream_list)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800112{
Alex Raya0ed4be2013-02-25 15:02:16 -0800113 ALOGV("%s:%d: stream_list=%p", __func__, mId, stream_list);
114 // TODO: validate input, create internal stream representations
115 return 0;
116}
117
118int Camera::registerStreamBuffers(const camera3_stream_buffer_set_t *buf_set)
119{
120 ALOGV("%s:%d: buffer_set=%p", __func__, mId, buf_set);
121 // TODO: register buffers with hardware
122 return 0;
123}
124
125const camera_metadata_t* Camera::constructDefaultRequestSettings(int type)
126{
127 ALOGV("%s:%d: type=%d", __func__, mId, type);
128 // TODO: return statically built default request
129 return NULL;
130}
131
132int Camera::processCaptureRequest(camera3_capture_request_t *request)
133{
134 ALOGV("%s:%d: request=%p", __func__, mId, request);
135 ATRACE_BEGIN(__func__);
136
137 if (request == NULL) {
138 ALOGE("%s:%d: NULL request recieved", __func__, mId);
Alex Rayed6b8a72012-12-27 10:42:22 -0800139 ATRACE_END();
Alex Raya0ed4be2013-02-25 15:02:16 -0800140 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800141 }
142
Alex Raya0ed4be2013-02-25 15:02:16 -0800143 // TODO: verify request; submit request to hardware
Alex Rayed6b8a72012-12-27 10:42:22 -0800144 ATRACE_END();
Alex Raya0ed4be2013-02-25 15:02:16 -0800145 return 0;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800146}
147
Alex Raya0ed4be2013-02-25 15:02:16 -0800148void Camera::getMetadataVendorTagOps(vendor_tag_query_ops_t *ops)
149{
150 ALOGV("%s:%d: ops=%p", __func__, mId, ops);
151 // TODO: return vendor tag ops
152}
153
154void Camera::dump(int fd)
155{
156 ALOGV("%s:%d: Dumping to fd %d", fd);
157 // TODO: dprintf all relevant state to fd
158}
159
160extern "C" {
161// Get handle to camera from device priv data
162static Camera *camdev_to_camera(const camera3_device_t *dev)
163{
164 return reinterpret_cast<Camera*>(dev->priv);
165}
166
167static int initialize(const camera3_device_t *dev,
168 const camera3_callback_ops_t *callback_ops)
169{
170 return camdev_to_camera(dev)->initialize(callback_ops);
171}
172
173static int configure_streams(const camera3_device_t *dev,
174 camera3_stream_configuration_t *stream_list)
175{
176 return camdev_to_camera(dev)->configureStreams(stream_list);
177}
178
179static int register_stream_buffers(const camera3_device_t *dev,
180 const camera3_stream_buffer_set_t *buffer_set)
181{
182 return camdev_to_camera(dev)->registerStreamBuffers(buffer_set);
183}
184
185static const camera_metadata_t *construct_default_request_settings(
186 const camera3_device_t *dev, int type)
187{
188 return camdev_to_camera(dev)->constructDefaultRequestSettings(type);
189}
190
191static int process_capture_request(const camera3_device_t *dev,
192 camera3_capture_request_t *request)
193{
194 return camdev_to_camera(dev)->processCaptureRequest(request);
195}
196
197static void get_metadata_vendor_tag_ops(const camera3_device_t *dev,
198 vendor_tag_query_ops_t *ops)
199{
200 camdev_to_camera(dev)->getMetadataVendorTagOps(ops);
201}
202
203static void dump(const camera3_device_t *dev, int fd)
204{
205 camdev_to_camera(dev)->dump(fd);
206}
207} // extern "C"
208
209const camera3_device_ops_t Camera::sOps = {
210 .initialize = default_camera_hal::initialize,
211 .configure_streams = default_camera_hal::configure_streams,
212 .register_stream_buffers = default_camera_hal::register_stream_buffers,
213 .construct_default_request_settings =
214 default_camera_hal::construct_default_request_settings,
215 .process_capture_request = default_camera_hal::process_capture_request,
216 .get_metadata_vendor_tag_ops =
217 default_camera_hal::get_metadata_vendor_tag_ops,
218 .dump = default_camera_hal::dump
219};
220
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800221} // namespace default_camera_hal