blob: 6ae0a5d63d5380855ffdbad9ff809fa9bb0de080 [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>
19
Alex Rayed6b8a72012-12-27 10:42:22 -080020//#define LOG_NDEBUG 0
Alex Ray7ee0b7a2012-11-06 00:12:49 -080021#define LOG_TAG "Camera"
22#include <cutils/log.h>
23
Alex Rayed6b8a72012-12-27 10:42:22 -080024#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
25#include <cutils/trace.h>
26
Alex Ray7ee0b7a2012-11-06 00:12:49 -080027#include "Camera.h"
28
29namespace default_camera_hal {
30
31extern "C" {
32// Shim passed to the framework to close an opened device.
33static int close_device(hw_device_t* dev)
34{
35 camera2_device_t* cam_dev = reinterpret_cast<camera2_device_t*>(dev);
36 Camera* cam = static_cast<Camera*>(cam_dev->priv);
37 return cam->close();
38}
39} // extern "C"
40
41Camera::Camera(int id, const hw_module_t* module)
42 : mId(id),
43 mBusy(false),
44 mMetadata(NULL)
45{
46 pthread_mutex_init(&mMutex,
47 NULL); // No pthread mutex attributes.
48
49 // TODO: fill in device operations function pointers
50 mDevice.common.tag = HARDWARE_DEVICE_TAG;
51 mDevice.common.module = const_cast<hw_module_t*>(module);
52 mDevice.common.close = close_device;
53 mDevice.priv = this;
54}
55
56Camera::~Camera()
57{
58}
59
60int Camera::open()
61{
62 ALOGV("%s: camera id %d", __func__, mId);
Alex Rayed6b8a72012-12-27 10:42:22 -080063 ATRACE_BEGIN("open");
Alex Ray7ee0b7a2012-11-06 00:12:49 -080064 pthread_mutex_lock(&mMutex);
65 if (mBusy) {
66 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -080067 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080068 ALOGE("%s:id%d: Error, device already in use.", __func__, mId);
69 return -EBUSY;
70 }
71
72 // TODO: open camera dev nodes, etc
73 mBusy = true;
74
75 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -080076 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080077 return 0;
78}
79
80int Camera::close()
81{
82 ALOGV("%s: camera id %d", __func__, mId);
Alex Rayed6b8a72012-12-27 10:42:22 -080083 ATRACE_BEGIN("close");
Alex Ray7ee0b7a2012-11-06 00:12:49 -080084 pthread_mutex_lock(&mMutex);
85 if (!mBusy) {
86 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -080087 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080088 ALOGE("%s:id%d: Error, close() on not open device.", __func__, mId);
89 return -EINVAL;
90 }
91
92 // TODO: close camera dev nodes, etc
93 mBusy = false;
94
95 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -080096 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -080097 return 0;
98}
99
100int Camera::getCameraInfo(struct camera_info* info)
101{
102 ALOGV("%s: camera id %d: info=%p", __func__, mId, info);
103 init();
104 if (mMetadata == NULL) {
105 ALOGE("%s:id%d: unable to initialize metadata.", __func__, mId);
106 return -ENOENT;
107 }
108
109 // TODO: fill remainder of info struct
110 info->static_camera_characteristics = mMetadata;
111
112 return 0;
113}
114
115void Camera::init()
116{
Alex Rayed6b8a72012-12-27 10:42:22 -0800117 ATRACE_BEGIN("init");
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800118 pthread_mutex_lock(&mMutex);
119 if (mMetadata != NULL) {
120 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -0800121 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800122 return;
123 }
124
125 // TODO: init metadata, dummy value for now
126 mMetadata = allocate_camera_metadata(1,1);
127
128 pthread_mutex_unlock(&mMutex);
Alex Rayed6b8a72012-12-27 10:42:22 -0800129 ATRACE_END();
Alex Ray7ee0b7a2012-11-06 00:12:49 -0800130}
131
132} // namespace default_camera_hal