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> |
| 19 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 20 | //#define LOG_NDEBUG 0 |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 21 | #define LOG_TAG "Camera" |
| 22 | #include <cutils/log.h> |
| 23 | |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 24 | #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) |
| 25 | #include <cutils/trace.h> |
| 26 | |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 27 | #include "Camera.h" |
| 28 | |
| 29 | namespace default_camera_hal { |
| 30 | |
| 31 | extern "C" { |
| 32 | // Shim passed to the framework to close an opened device. |
| 33 | static 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 | |
| 41 | Camera::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 | |
| 56 | Camera::~Camera() |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | int Camera::open() |
| 61 | { |
| 62 | ALOGV("%s: camera id %d", __func__, mId); |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 63 | ATRACE_BEGIN("open"); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 64 | pthread_mutex_lock(&mMutex); |
| 65 | if (mBusy) { |
| 66 | pthread_mutex_unlock(&mMutex); |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 67 | ATRACE_END(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 68 | 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 Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 76 | ATRACE_END(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int Camera::close() |
| 81 | { |
| 82 | ALOGV("%s: camera id %d", __func__, mId); |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 83 | ATRACE_BEGIN("close"); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 84 | pthread_mutex_lock(&mMutex); |
| 85 | if (!mBusy) { |
| 86 | pthread_mutex_unlock(&mMutex); |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 87 | ATRACE_END(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 88 | 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 Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 96 | ATRACE_END(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int 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 | |
| 115 | void Camera::init() |
| 116 | { |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 117 | ATRACE_BEGIN("init"); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 118 | pthread_mutex_lock(&mMutex); |
| 119 | if (mMetadata != NULL) { |
| 120 | pthread_mutex_unlock(&mMutex); |
Alex Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 121 | ATRACE_END(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 122 | 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 Ray | ed6b8a7 | 2012-12-27 10:42:22 -0800 | [diff] [blame^] | 129 | ATRACE_END(); |
Alex Ray | 7ee0b7a | 2012-11-06 00:12:49 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace default_camera_hal |