| /* |
| * Copyright 2008, The Android Open Source Project |
| * Copyright 2013, Samsung Electronics Co. LTD |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| /*! |
| * \file SecCameraInterface.cpp |
| * \brief source file for Android Camera Ext HAL |
| * \author teahyung kim (tkon.kim@samsung.com) |
| * \date 2013/04/30 |
| * |
| */ |
| |
| #ifndef ANDROID_HARDWARE_SECCAMERAINTERFACE_CPP |
| #define ANDROID_HARDWARE_SECCAMERAINTERFACE_CPP |
| |
| #define LOG_NDEBUG 0 |
| #define LOG_TAG "SecCameraHardware" |
| |
| #include "SecCameraInterface.h" |
| #include "SecCameraHardware.h" |
| |
| namespace android { |
| |
| const static CameraInfo sCameraInfo[] = SEC_CAMERA_INFO; |
| |
| /* Close this device */ |
| |
| static camera_device_t *g_cam_device; |
| |
| static inline SecCameraHardware *obj(struct camera_device *dev) |
| { |
| return reinterpret_cast<SecCameraHardware *>(dev->priv); |
| } |
| |
| static int HAL_camera_device_close(struct hw_device_t *device) |
| { |
| if (!g_cam_device) |
| ALOGI("camera device already closed"); |
| |
| if (device) { |
| camera_device_t *cam_device = (camera_device_t *)device; |
| ALOGI("camera device closed %d", obj(cam_device)->getCameraId()); |
| delete static_cast<SecCameraHardware *>(cam_device->priv); |
| free(cam_device); |
| g_cam_device = 0; |
| } |
| |
| return 0; |
| } |
| |
| /* Set the preview_stream_ops to which preview frames are sent */ |
| static int HAL_camera_device_set_preview_window(struct camera_device *dev, |
| struct preview_stream_ops *buf) |
| { |
| HALDBG("%s", __FUNCTION__); |
| return obj(dev)->setPreviewWindow(buf); |
| } |
| |
| /* Set the notification and data callbacks */ |
| static void HAL_camera_device_set_callbacks(struct camera_device *dev, |
| camera_notify_callback notify_cb, |
| camera_data_callback data_cb, |
| camera_data_timestamp_callback data_cb_timestamp, |
| camera_request_memory get_memory, |
| void *user) |
| { |
| HALDBG("%s", __FUNCTION__); |
| obj(dev)->setCallbacks(notify_cb, data_cb, data_cb_timestamp, |
| get_memory, |
| user); |
| } |
| |
| /* |
| * The following three functions all take a msg_type, which is a bitmask of |
| * the messages defined in include/ui/Camera.h |
| */ |
| |
| /* |
| * Enable a message, or set of messages. |
| */ |
| static void HAL_camera_device_enable_msg_type(struct camera_device *dev, int32_t msg_type) |
| { |
| HALDBG("%s", __FUNCTION__); |
| obj(dev)->enableMsgType(msg_type); |
| } |
| |
| /** |
| * Disable a message, or a set of messages. |
| * |
| * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera |
| * HAL should not rely on its client to call releaseRecordingFrame() to |
| * release video recording frames sent out by the cameral HAL before and |
| * after the disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera HAL |
| * clients must not modify/access any video recording frame after calling |
| * disableMsgType(CAMERA_MSG_VIDEO_FRAME). |
| */ |
| static void HAL_camera_device_disable_msg_type(struct camera_device *dev, int32_t msg_type) |
| { |
| HALDBG("%s", __FUNCTION__); |
| obj(dev)->disableMsgType(msg_type); |
| } |
| |
| /* |
| * Query whether a message, or a set of messages, is enabled. Note that |
| * this is operates as an AND, if any of the messages queried are off, this |
| * will return false. |
| */ |
| static int HAL_camera_device_msg_type_enabled(struct camera_device *dev, int32_t msg_type) |
| { |
| HALDBG("%s", __FUNCTION__); |
| return obj(dev)->msgTypeEnabled(msg_type); |
| } |
| |
| /* |
| * Start preview mode. |
| */ |
| static int HAL_camera_device_start_preview(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->startPreview(); |
| } |
| |
| /* |
| * Stop a previously started preview. |
| */ |
| static void HAL_camera_device_stop_preview(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| obj(dev)->stopPreview(); |
| } |
| |
| /* |
| * Returns true if preview is enabled. |
| */ |
| static int HAL_camera_device_preview_enabled(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->previewEnabled(); |
| } |
| |
| /* |
| * Request the camera HAL to store meta data or real YUV data in the video |
| * buffers sent out via CAMERA_MSG_VIDEO_FRAME for a recording session. If |
| * it is not called, the default camera HAL behavior is to store real YUV |
| * data in the video buffers. |
| * |
| * This method should be called before startRecording() in order to be |
| * effective. |
| * |
| * If meta data is stored in the video buffers, it is up to the receiver of |
| * the video buffers to interpret the contents and to find the actual frame |
| * data with the help of the meta data in the buffer. How this is done is |
| * outside of the scope of this method. |
| * |
| * Some camera HALs may not support storing meta data in the video buffers, |
| * but all camera HALs should support storing real YUV data in the video |
| * buffers. If the camera HAL does not support storing the meta data in the |
| * video buffers when it is requested to do do, INVALID_OPERATION must be |
| * returned. It is very useful for the camera HAL to pass meta data rather |
| * than the actual frame data directly to the video encoder, since the |
| * amount of the uncompressed frame data can be very large if video size is |
| * large. |
| * |
| * @param enable if true to instruct the camera HAL to store |
| * meta data in the video buffers; false to instruct |
| * the camera HAL to store real YUV data in the video |
| * buffers. |
| * |
| * @return OK on success. |
| */ |
| static int HAL_camera_device_store_meta_data_in_buffers(struct camera_device *dev, int enable) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->storeMetaDataInBuffers(enable); |
| } |
| |
| /* |
| * Start record mode. When a record image is available, a |
| * CAMERA_MSG_VIDEO_FRAME message is sent with the corresponding |
| * frame. Every record frame must be released by a camera HAL client via |
| * releaseRecordingFrame() before the client calls |
| * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls |
| * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is the camera HAL's |
| * responsibility to manage the life-cycle of the video recording frames, |
| * and the client must not modify/access any video recording frames. |
| */ |
| static int HAL_camera_device_start_recording(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->startRecording(); |
| } |
| |
| /* |
| * Stop a previously started recording. |
| */ |
| static void HAL_camera_device_stop_recording(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| obj(dev)->stopRecording(); |
| } |
| |
| /* |
| * Returns true if recording is enabled. |
| */ |
| static int HAL_camera_device_recording_enabled(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->recordingEnabled(); |
| } |
| |
| /* |
| * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME. |
| * |
| * It is camera HAL client's responsibility to release video recording |
| * frames sent out by the camera HAL before the camera HAL receives a call |
| * to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives the call to |
| * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is the camera HAL's |
| * responsibility to manage the life-cycle of the video recording frames. |
| */ |
| static void HAL_camera_device_release_recording_frame(struct camera_device *dev, |
| const void *opaque) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| obj(dev)->releaseRecordingFrame(opaque); |
| } |
| |
| /* |
| * Start auto focus, the notification callback routine is called with |
| * CAMERA_MSG_FOCUS once when focusing is complete. autoFocus() will be |
| * called again if another auto focus is needed. |
| */ |
| static int HAL_camera_device_auto_focus(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->autoFocus(); |
| } |
| |
| /* |
| * Cancels auto-focus function. If the auto-focus is still in progress, |
| * this function will cancel it. Whether the auto-focus is in progress or |
| * not, this function will return the focus position to the default. If |
| * the camera does not support auto-focus, this is a no-op. |
| */ |
| static int HAL_camera_device_cancel_auto_focus(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->cancelAutoFocus(); |
| } |
| |
| /* |
| * Take a picture. |
| */ |
| static int HAL_camera_device_take_picture(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->takePicture(); |
| } |
| |
| /* |
| * Cancel a picture that was started with takePicture. Calling this method |
| * when no picture is being taken is a no-op. |
| */ |
| static int HAL_camera_device_cancel_picture(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| return obj(dev)->cancelPicture(); |
| } |
| |
| /* |
| * Set the camera parameters. This returns BAD_VALUE if any parameter is |
| * invalid or not supported. |
| */ |
| static int HAL_camera_device_set_parameters(struct camera_device *dev, |
| const char *parms) |
| { |
| HALDBG("%s", __FUNCTION__); |
| String8 str(parms); |
| CameraParameters p(str); |
| return obj(dev)->setParameters(p); |
| } |
| |
| /* Return the camera parameters. */ |
| char *HAL_camera_device_get_parameters(struct camera_device *dev) |
| { |
| |
| HALDBG("%s", __FUNCTION__); |
| |
| String8 str; |
| CameraParameters parms = obj(dev)->getParameters(); |
| str = parms.flatten(); |
| return strdup(str.string()); |
| } |
| |
| static void HAL_camera_device_put_parameters(struct camera_device *dev, char *parms) |
| { |
| HALDBG("%s", __FUNCTION__); |
| free(parms); |
| } |
| |
| /* |
| * Send command to camera driver. |
| */ |
| static int HAL_camera_device_send_command(struct camera_device *dev, |
| int32_t cmd, int32_t arg1, int32_t arg2) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->sendCommand(cmd, arg1, arg2); |
| } |
| |
| /* |
| * Release the hardware resources owned by this object. Note that this is |
| * *not* done in the destructor. |
| */ |
| static void HAL_camera_device_release(struct camera_device *dev) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| obj(dev)->release(); |
| } |
| |
| /* |
| * Dump state of the camera hardware |
| */ |
| static int HAL_camera_device_dump(struct camera_device *dev, int fd) |
| { |
| HALDBG("%s", __FUNCTION__); |
| |
| return obj(dev)->dump(fd); |
| } |
| |
| |
| static int HAL_getNumberOfCameras(void) |
| { |
| int num = sizeof(sCameraInfo) / sizeof(sCameraInfo[0]); |
| ALOGV("%s: camera num %d", __FUNCTION__, num); |
| return num; |
| } |
| |
| static int HAL_getCameraInfo(int cameraId, struct camera_info *cameraInfo) |
| { |
| if (CC_UNLIKELY(cameraId < 0)) { |
| ALOGE("%s: camera id %d", __FUNCTION__, cameraId); |
| return BAD_VALUE; |
| } |
| |
| memcpy(cameraInfo, &sCameraInfo[cameraId], sizeof(CameraInfo)); |
| return 0; |
| } |
| |
| #define SET_METHOD(m) m : HAL_camera_device_##m |
| |
| static camera_device_ops_t camera_device_ops = { |
| SET_METHOD(set_preview_window), |
| SET_METHOD(set_callbacks), |
| SET_METHOD(enable_msg_type), |
| SET_METHOD(disable_msg_type), |
| SET_METHOD(msg_type_enabled), |
| SET_METHOD(start_preview), |
| SET_METHOD(stop_preview), |
| SET_METHOD(preview_enabled), |
| SET_METHOD(store_meta_data_in_buffers), |
| SET_METHOD(start_recording), |
| SET_METHOD(stop_recording), |
| SET_METHOD(recording_enabled), |
| SET_METHOD(release_recording_frame), |
| SET_METHOD(auto_focus), |
| SET_METHOD(cancel_auto_focus), |
| SET_METHOD(take_picture), |
| SET_METHOD(cancel_picture), |
| SET_METHOD(set_parameters), |
| SET_METHOD(get_parameters), |
| SET_METHOD(put_parameters), |
| SET_METHOD(send_command), |
| SET_METHOD(release), |
| SET_METHOD(dump), |
| }; |
| |
| #undef SET_METHOD |
| |
| static int HAL_ext_camera_device_open(const struct hw_module_t* module, |
| const char *id, |
| struct hw_device_t **device) |
| { |
| ALOGD("camera_device_open: E"); |
| |
| int cameraId = atoi(id); |
| if (cameraId < 0 || cameraId >= HAL_getNumberOfCameras()) { |
| if (cameraId != 99) { |
| ALOGE("camera_device_open: invalid camera ID %s", id); |
| return -EINVAL; |
| } else { |
| cameraId = 2; |
| } |
| } |
| |
| if (g_cam_device) { |
| if (obj(g_cam_device)->getCameraId() == cameraId) { |
| ALOGD("returning existing camera ID %s", id); |
| goto done; |
| } else { |
| ALOGE("camera_device_open: error, cannot open camera %d. camera %d is already running!", |
| cameraId, obj(g_cam_device)->getCameraId()); |
| return -ENOSYS; |
| } |
| } |
| |
| g_cam_device = (camera_device_t *)malloc(sizeof(camera_device_t)); |
| if (!g_cam_device) { |
| ALOGE("camera_device_open: error, fail to get memory"); |
| return -ENOMEM; |
| } |
| |
| memset(g_cam_device, 0, sizeof(camera_device_t)); |
| g_cam_device->common.tag = HARDWARE_DEVICE_TAG; |
| g_cam_device->common.version = 1; |
| g_cam_device->common.module = const_cast<hw_module_t *>(module); |
| g_cam_device->common.close = HAL_camera_device_close; |
| |
| g_cam_device->ops = &camera_device_ops; |
| |
| g_cam_device->priv = new SecCameraHardware(cameraId, g_cam_device); |
| if (!obj(g_cam_device)->mInitialized) { |
| ALOGE("Instance is not created"); |
| if (g_cam_device->priv) { |
| delete static_cast<SecCameraHardware *>(g_cam_device->priv); |
| free(g_cam_device); |
| g_cam_device = 0; |
| } |
| return -ENOSYS; |
| } |
| |
| done: |
| *device = (hw_device_t *)g_cam_device; |
| ALOGI("opened camera %s (%p)", id, *device); |
| return 0; |
| } |
| |
| int HAL_ext_camera_device_open_wrapper(const struct hw_module_t* module, |
| const char *id, |
| struct hw_device_t **device) |
| { |
| return HAL_ext_camera_device_open(module, id, device); |
| } |
| |
| static hw_module_methods_t ext_camera_module_methods = { |
| open : HAL_ext_camera_device_open |
| }; |
| |
| /* merge to libcamera_common */ |
| #if 0 |
| extern "C" { |
| struct camera_module HAL_MODULE_INFO_SYM = { |
| common : { |
| tag : HARDWARE_MODULE_TAG, |
| version_major : 1, |
| version_minor : 0, |
| id : CAMERA_HARDWARE_MODULE_ID, |
| name : "Px camera HAL", |
| author : "Samsung Corporation", |
| methods : &ext_camera_module_methods, |
| dso : NULL, |
| reserved : {0} |
| }, |
| get_number_of_cameras : HAL_getNumberOfCameras, |
| get_camera_info : HAL_getCameraInfo, |
| set_callbacks : NULL, |
| #if (TARGET_ANDROID_VER_MAJ >= 4 && TARGET_ANDROID_VER_MIN >= 4) |
| get_vendor_tag_ops : NULL |
| #endif |
| }; |
| } |
| #endif |
| |
| }; /* namespace android */ |
| |
| #endif /* ANDROID_HARDWARE_SECCAMERAINTERFACE_CPP */ |