Home | History | Annotate | Download | only in camera
      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 #include <hardware/camera3.h>
     20 #include "CameraHAL.h"
     21 
     22 //#define LOG_NDEBUG 0
     23 #define LOG_TAG "Camera"
     24 #include <cutils/log.h>
     25 
     26 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
     27 #include <cutils/trace.h>
     28 
     29 #include "Camera.h"
     30 
     31 namespace default_camera_hal {
     32 
     33 extern "C" {
     34 // Shim passed to the framework to close an opened device.
     35 static int close_device(hw_device_t* dev)
     36 {
     37     camera3_device_t* cam_dev = reinterpret_cast<camera3_device_t*>(dev);
     38     Camera* cam = static_cast<Camera*>(cam_dev->priv);
     39     return cam->close();
     40 }
     41 } // extern "C"
     42 
     43 Camera::Camera(int id)
     44   : mId(id),
     45     mBusy(false),
     46     mCallbackOps(NULL)
     47 {
     48     pthread_mutex_init(&mMutex,
     49                        NULL); // No pthread mutex attributes.
     50 
     51     memset(&mDevice, 0, sizeof(mDevice));
     52     mDevice.common.tag    = HARDWARE_DEVICE_TAG;
     53     mDevice.common.close  = close_device;
     54     mDevice.ops           = const_cast<camera3_device_ops_t*>(&sOps);
     55     mDevice.priv          = this;
     56 }
     57 
     58 Camera::~Camera()
     59 {
     60 }
     61 
     62 int Camera::open(const hw_module_t *module, hw_device_t **device)
     63 {
     64     ALOGI("%s:%d: Opening camera device", __func__, mId);
     65     ATRACE_BEGIN(__func__);
     66     pthread_mutex_lock(&mMutex);
     67     if (mBusy) {
     68         pthread_mutex_unlock(&mMutex);
     69         ATRACE_END();
     70         ALOGE("%s:%d: Error! Camera device already opened", __func__, mId);
     71         return -EBUSY;
     72     }
     73 
     74     // TODO: open camera dev nodes, etc
     75     mBusy = true;
     76     mDevice.common.module = const_cast<hw_module_t*>(module);
     77     *device = &mDevice.common;
     78 
     79     pthread_mutex_unlock(&mMutex);
     80     ATRACE_END();
     81     return 0;
     82 }
     83 
     84 int Camera::close()
     85 {
     86     ALOGI("%s:%d: Closing camera device", __func__, mId);
     87     ATRACE_BEGIN(__func__);
     88     pthread_mutex_lock(&mMutex);
     89     if (!mBusy) {
     90         pthread_mutex_unlock(&mMutex);
     91         ATRACE_END();
     92         ALOGE("%s:%d: Error! Camera device not open", __func__, mId);
     93         return -EINVAL;
     94     }
     95 
     96     // TODO: close camera dev nodes, etc
     97     mBusy = false;
     98 
     99     pthread_mutex_unlock(&mMutex);
    100     ATRACE_END();
    101     return 0;
    102 }
    103 
    104 int Camera::initialize(const camera3_callback_ops_t *callback_ops)
    105 {
    106     ALOGV("%s:%d: callback_ops=%p", __func__, mId, callback_ops);
    107     mCallbackOps = callback_ops;
    108     return 0;
    109 }
    110 
    111 int Camera::configureStreams(camera3_stream_configuration_t *stream_list)
    112 {
    113     ALOGV("%s:%d: stream_list=%p", __func__, mId, stream_list);
    114     // TODO: validate input, create internal stream representations
    115     return 0;
    116 }
    117 
    118 int 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 
    125 const 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 
    132 int 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);
    139         ATRACE_END();
    140         return -EINVAL;
    141     }
    142 
    143     // TODO: verify request; submit request to hardware
    144     ATRACE_END();
    145     return 0;
    146 }
    147 
    148 void 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 
    154 void Camera::dump(int fd)
    155 {
    156     ALOGV("%s:%d: Dumping to fd %d", fd);
    157     // TODO: dprintf all relevant state to fd
    158 }
    159 
    160 extern "C" {
    161 // Get handle to camera from device priv data
    162 static Camera *camdev_to_camera(const camera3_device_t *dev)
    163 {
    164     return reinterpret_cast<Camera*>(dev->priv);
    165 }
    166 
    167 static 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 
    173 static 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 
    179 static 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 
    185 static 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 
    191 static 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 
    197 static 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 
    203 static void dump(const camera3_device_t *dev, int fd)
    204 {
    205     camdev_to_camera(dev)->dump(fd);
    206 }
    207 } // extern "C"
    208 
    209 const 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 
    221 } // namespace default_camera_hal
    222