blob: d477ef6c4ec60afe71e828c05df5a3d163aabcda [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>
Alex Rayc16e56d2013-04-29 12:24:49 -070028#include "ScopedTrace.h"
Alex Rayed6b8a72012-12-27 10:42:22 -080029
Alex Ray7ee0b7a2012-11-06 00:12:49 -080030#include "Camera.h"
31
32namespace default_camera_hal {
33
34extern "C" {
35// Shim passed to the framework to close an opened device.
36static int close_device(hw_device_t* dev)
37{
Alex Raya0ed4be2013-02-25 15:02:16 -080038 camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080039 Camera* cam = static_cast<Camera*>(cam_dev->priv);
40 return cam->close();
41}
42} // extern "C"
43
Alex Raya0ed4be2013-02-25 15:02:16 -080044Camera::Camera(int id)
45 : mId(id),
46 mBusy(false),
47 mCallbackOps(NULL)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080048{
49 pthread_mutex_init(&mMutex,
50 NULL); // No pthread mutex attributes.
51
Alex Raya0ed4be2013-02-25 15:02:16 -080052 memset(&mDevice, 0, sizeof(mDevice));
Alex Ray7ee0b7a2012-11-06 00:12:49 -080053 mDevice.common.tag = HARDWARE_DEVICE_TAG;
Alex Ray7ee0b7a2012-11-06 00:12:49 -080054 mDevice.common.close = close_device;
Alex Raya0ed4be2013-02-25 15:02:16 -080055 mDevice.ops = const_cast<camera3_device_ops_t*>(&sOps);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080056 mDevice.priv = this;
57}
58
59Camera::~Camera()
60{
61}
62
Alex Raya0ed4be2013-02-25 15:02:16 -080063int Camera::open(const hw_module_t *module, hw_device_t **device)
Alex Ray7ee0b7a2012-11-06 00:12:49 -080064{
Alex Raya0ed4be2013-02-25 15:02:16 -080065 ALOGI("%s:%d: Opening camera device", __func__, mId);
Alex Rayc16e56d2013-04-29 12:24:49 -070066 CAMTRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080067 pthread_mutex_lock(&mMutex);
68 if (mBusy) {
69 pthread_mutex_unlock(&mMutex);
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);
80 return 0;
81}
82
83int Camera::close()
84{
Alex Raya0ed4be2013-02-25 15:02:16 -080085 ALOGI("%s:%d: Closing camera device", __func__, mId);
Alex Rayc16e56d2013-04-29 12:24:49 -070086 CAMTRACE_CALL();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080087 pthread_mutex_lock(&mMutex);
88 if (!mBusy) {
89 pthread_mutex_unlock(&mMutex);
Alex Raya0ed4be2013-02-25 15:02:16 -080090 ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
Alex Ray7ee0b7a2012-11-06 00:12:49 -080091 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 Raya0ed4be2013-02-25 15:02:16 -0800101int Camera::initialize(const camera3_callback_ops_t *callback_ops)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800102{
Alex Raya0ed4be2013-02-25 15:02:16 -0800103 ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
104 mCallbackOps = callback_ops;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800105 return 0;
106}
107
Alex Raya0ed4be2013-02-25 15:02:16 -0800108int Camera::configureStreams(camera3_stream_configuration_t *stream_list)
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800109{
Alex Raya0ed4be2013-02-25 15:02:16 -0800110 ALOGV("%s:%d: stream_list=%p", __func__, mId, stream_list);
111 // TODO: validate input, create internal stream representations
112 return 0;
113}
114
115int 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
122const 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
129int Camera::processCaptureRequest(camera3_capture_request_t *request)
130{
131 ALOGV("%s:%d: request=%p", __func__, mId, request);
Alex Rayc16e56d2013-04-29 12:24:49 -0700132 CAMTRACE_CALL();
Alex Raya0ed4be2013-02-25 15:02:16 -0800133
134 if (request == NULL) {
135 ALOGE("%s:%d: NULL request recieved", __func__, mId);
Alex Raya0ed4be2013-02-25 15:02:16 -0800136 return -EINVAL;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800137 }
138
Alex Raya0ed4be2013-02-25 15:02:16 -0800139 // TODO: verify request; submit request to hardware
Alex Raya0ed4be2013-02-25 15:02:16 -0800140 return 0;
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800141}
142
Alex Raya0ed4be2013-02-25 15:02:16 -0800143void 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
149void Camera::dump(int fd)
150{
Alex Rayaf3a4612013-04-29 14:16:10 -0700151 ALOGV("%s:%d: Dumping to fd %d", __func__, mId, fd);
Alex Raya0ed4be2013-02-25 15:02:16 -0800152 // TODO: dprintf all relevant state to fd
153}
154
155extern "C" {
156// Get handle to camera from device priv data
157static Camera *camdev_to_camera(const camera3_device_t *dev)
158{
159 return reinterpret_cast<Camera*>(dev->priv);
160}
161
162static 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
168static 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
174static 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
180static 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
186static 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
192static 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
198static void dump(const camera3_device_t *dev, int fd)
199{
200 camdev_to_camera(dev)->dump(fd);
201}
202} // extern "C"
203
204const 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 Ray7ee0b7a2012-11-06 00:12:49 -0800216} // namespace default_camera_hal