luca020400

CameraWrapper.cpp

Apr 11th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.68 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2015, The CyanogenMod 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. /**
  18. * @file CameraWrapper.cpp
  19. *
  20. * This file wraps a vendor camera module.
  21. *
  22. */
  23.  
  24. //#define LOG_NDEBUG 0
  25.  
  26. #define LOG_TAG "CameraWrapper"
  27. #include <cutils/log.h>
  28.  
  29. #include <utils/threads.h>
  30. #include <utils/String8.h>
  31. #include <hardware/hardware.h>
  32. #include <hardware/camera.h>
  33. #include <camera/Camera.h>
  34. #include <camera/CameraParameters.h>
  35.  
  36. static android::Mutex gCameraWrapperLock;
  37. static camera_module_t *gVendorModule = 0;
  38.  
  39. static char **fixed_set_params = NULL;
  40.  
  41. static int camera_device_open(const hw_module_t *module, const char *name,
  42.         hw_device_t **device);
  43. static int camera_get_number_of_cameras(void);
  44. static int camera_get_camera_info(int camera_id, struct camera_info *info);
  45.  
  46. static struct hw_module_methods_t camera_module_methods = {
  47.     open: camera_device_open
  48. };
  49.  
  50. camera_module_t HAL_MODULE_INFO_SYM = {
  51.     common: {
  52.          tag: HARDWARE_MODULE_TAG,
  53.          version_major: 1,
  54.          version_minor: 0,
  55.          id: CAMERA_HARDWARE_MODULE_ID,
  56.          name: "Moto G 2014 Camera Wrapper",
  57.          author: "The CyanogenMod Project",
  58.          methods: &camera_module_methods,
  59.          dso: NULL, /* remove compilation warnings */
  60.          reserved: {0}, /* remove compilation warnings */
  61.     },
  62.     get_number_of_cameras: camera_get_number_of_cameras,
  63.     get_camera_info: camera_get_camera_info,
  64.     set_callbacks: NULL, /* remove compilation warnings */
  65.     get_vendor_tag_ops: NULL, /* remove compilation warnings */
  66.     open_legacy: NULL, /* remove compilation warnings */
  67.     reserved: {0}, /* remove compilation warnings */
  68. };
  69.  
  70. typedef struct wrapper_camera_device {
  71.     camera_device_t base;
  72.     int id;
  73.     camera_device_t *vendor;
  74. } wrapper_camera_device_t;
  75.  
  76. #define VENDOR_CALL(device, func, ...) ({ \
  77.     wrapper_camera_device_t *__wrapper_dev = (wrapper_camera_device_t*) device; \
  78.     __wrapper_dev->vendor->ops->func(__wrapper_dev->vendor, ##__VA_ARGS__); \
  79. })
  80.  
  81. #define CAMERA_ID(device) (((wrapper_camera_device_t *)(device))->id)
  82.  
  83. static int check_vendor_module()
  84. {
  85.     int rv = 0;
  86.     ALOGV("%s", __FUNCTION__);
  87.  
  88.     if (gVendorModule)
  89.         return 0;
  90.  
  91.     rv = hw_get_module_by_class("camera", "vendor",
  92.             (const hw_module_t**)&gVendorModule);
  93.     if (rv)
  94.         ALOGE("failed to open vendor camera module");
  95.     return rv;
  96. }
  97.  
  98. static char *camera_fixup_getparams(int id, const char *settings)
  99. {
  100.     android::CameraParameters params;
  101.     params.unflatten(android::String8(settings));
  102.  
  103. #if !LOG_NDEBUG
  104.     ALOGV("%s: original parameters:", __FUNCTION__);
  105.     params.dump();
  106. #endif
  107.  
  108.     // Add 1080p Camera Support
  109.     if (id==0) {
  110.         params.remove(android::CameraParameters::KEY_SUPPORTED_VIDEO_SIZES);
  111.         params.set(android::CameraParameters::KEY_SUPPORTED_VIDEO_SIZES,
  112.             "1920x1080,1280x960,1280x720,960x720,864x480,768x432,720x480,640x640,320x240,176x144");
  113.     }
  114.  
  115. #if !LOG_NDEBUG
  116.     ALOGV("%s: fixed parameters:", __FUNCTION__);
  117.     params.dump();
  118. #endif
  119.  
  120.     android::String8 strParams = params.flatten();
  121.     char *ret = strdup(strParams.string());
  122.  
  123.     return ret;
  124. }
  125.  
  126. static char *camera_fixup_setparams(int id, const char *settings)
  127. {
  128.     android::CameraParameters params;
  129.     params.unflatten(android::String8(settings));
  130.  
  131. #if !LOG_NDEBUG
  132.     ALOGV("%s: original parameters:", __FUNCTION__);
  133.     params.dump();
  134. #endif
  135.  
  136.     params.set(android::CameraParameters::KEY_VIDEO_STABILIZATION, "false");
  137.  
  138. #if !LOG_NDEBUG
  139.     ALOGV("%s: fixed parameters:", __FUNCTION__);
  140.     params.dump();
  141. #endif
  142.  
  143.     android::String8 strParams = params.flatten();
  144.     if (fixed_set_params[id])
  145.         free(fixed_set_params[id]);
  146.     fixed_set_params[id] = strdup(strParams.string());
  147.     char *ret = fixed_set_params[id];
  148.  
  149.     return ret;
  150. }
  151.  
  152. /*******************************************************************
  153.  * implementation of camera_device_ops functions
  154.  *******************************************************************/
  155.  
  156. static int camera_set_preview_window(struct camera_device *device,
  157.         struct preview_stream_ops *window)
  158. {
  159.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  160.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  161.  
  162.     if (!device)
  163.         return -EINVAL;
  164.  
  165.     return VENDOR_CALL(device, set_preview_window, window);
  166. }
  167.  
  168. static void camera_set_callbacks(struct camera_device *device,
  169.         camera_notify_callback notify_cb,
  170.         camera_data_callback data_cb,
  171.         camera_data_timestamp_callback data_cb_timestamp,
  172.         camera_request_memory get_memory,
  173.         void *user)
  174. {
  175.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  176.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  177.  
  178.     if (!device)
  179.         return;
  180.  
  181.     VENDOR_CALL(device, set_callbacks, notify_cb, data_cb, data_cb_timestamp,
  182.             get_memory, user);
  183. }
  184.  
  185. static void camera_enable_msg_type(struct camera_device *device,
  186.         int32_t msg_type)
  187. {
  188.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  189.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  190.  
  191.     if (!device)
  192.         return;
  193.  
  194.     VENDOR_CALL(device, enable_msg_type, msg_type);
  195. }
  196.  
  197. static void camera_disable_msg_type(struct camera_device *device,
  198.         int32_t msg_type)
  199. {
  200.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  201.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  202.  
  203.     if (!device)
  204.         return;
  205.  
  206.     VENDOR_CALL(device, disable_msg_type, msg_type);
  207. }
  208.  
  209. static int camera_msg_type_enabled(struct camera_device *device,
  210.         int32_t msg_type)
  211. {
  212.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  213.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  214.  
  215.     if (!device)
  216.         return 0;
  217.  
  218.     return VENDOR_CALL(device, msg_type_enabled, msg_type);
  219. }
  220.  
  221. static int camera_start_preview(struct camera_device *device)
  222. {
  223.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  224.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  225.  
  226.     if (!device)
  227.         return -EINVAL;
  228.  
  229.     return VENDOR_CALL(device, start_preview);
  230. }
  231.  
  232. static void camera_stop_preview(struct camera_device *device)
  233. {
  234.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  235.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  236.  
  237.     if (!device)
  238.         return;
  239.  
  240.     VENDOR_CALL(device, stop_preview);
  241. }
  242.  
  243. static int camera_preview_enabled(struct camera_device *device)
  244. {
  245.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  246.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  247.  
  248.     if (!device)
  249.         return -EINVAL;
  250.  
  251.     return VENDOR_CALL(device, preview_enabled);
  252. }
  253.  
  254. static int camera_store_meta_data_in_buffers(struct camera_device *device,
  255.         int enable)
  256. {
  257.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  258.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  259.  
  260.     if (!device)
  261.         return -EINVAL;
  262.  
  263.     return VENDOR_CALL(device, store_meta_data_in_buffers, enable);
  264. }
  265.  
  266. static int camera_start_recording(struct camera_device *device)
  267. {
  268.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  269.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  270.  
  271.     if (!device)
  272.         return EINVAL;
  273.  
  274.     return VENDOR_CALL(device, start_recording);
  275. }
  276.  
  277. static void camera_stop_recording(struct camera_device *device)
  278. {
  279.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  280.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  281.  
  282.     if (!device)
  283.         return;
  284.  
  285.     VENDOR_CALL(device, stop_recording);
  286. }
  287.  
  288. static int camera_recording_enabled(struct camera_device *device)
  289. {
  290.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  291.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  292.  
  293.     if (!device)
  294.         return -EINVAL;
  295.  
  296.     return VENDOR_CALL(device, recording_enabled);
  297. }
  298.  
  299. static void camera_release_recording_frame(struct camera_device *device,
  300.         const void *opaque)
  301. {
  302.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  303.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  304.  
  305.     if (!device)
  306.         return;
  307.  
  308.     VENDOR_CALL(device, release_recording_frame, opaque);
  309. }
  310.  
  311. static int camera_auto_focus(struct camera_device *device)
  312. {
  313.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  314.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  315.  
  316.     if (!device)
  317.         return -EINVAL;
  318.  
  319.  
  320.     return VENDOR_CALL(device, auto_focus);
  321. }
  322.  
  323. static int camera_cancel_auto_focus(struct camera_device *device)
  324. {
  325.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  326.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  327.  
  328.     if (!device)
  329.         return -EINVAL;
  330.  
  331.     return VENDOR_CALL(device, cancel_auto_focus);
  332. }
  333.  
  334. static int camera_take_picture(struct camera_device *device)
  335. {
  336.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  337.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  338.  
  339.     if (!device)
  340.         return -EINVAL;
  341.  
  342.     return VENDOR_CALL(device, take_picture);
  343. }
  344.  
  345. static int camera_cancel_picture(struct camera_device *device)
  346. {
  347.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  348.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  349.  
  350.     if (!device)
  351.         return -EINVAL;
  352.  
  353.     return VENDOR_CALL(device, cancel_picture);
  354. }
  355.  
  356. static int camera_set_parameters(struct camera_device *device,
  357.         const char *params)
  358. {
  359.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  360.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  361.  
  362.     if (!device)
  363.         return -EINVAL;
  364.  
  365.     char *tmp = NULL;
  366.     tmp = camera_fixup_setparams(CAMERA_ID(device), params);
  367.  
  368.     int ret = VENDOR_CALL(device, set_parameters, tmp);
  369.     return ret;
  370. }
  371.  
  372. static char *camera_get_parameters(struct camera_device *device)
  373. {
  374.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  375.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  376.  
  377.     if (!device)
  378.         return NULL;
  379.  
  380.     char *params = VENDOR_CALL(device, get_parameters);
  381.  
  382.     char *tmp = camera_fixup_getparams(CAMERA_ID(device), params);
  383.     VENDOR_CALL(device, put_parameters, params);
  384.     params = tmp;
  385.  
  386.     return params;
  387. }
  388.  
  389. static void camera_put_parameters(struct camera_device *device, char *params)
  390. {
  391.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  392.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  393.  
  394.     if (params)
  395.         free(params);
  396. }
  397.  
  398. static int camera_send_command(struct camera_device *device,
  399.         int32_t cmd, int32_t arg1, int32_t arg2)
  400. {
  401.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  402.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  403.  
  404.     if (!device)
  405.         return -EINVAL;
  406.  
  407.     return VENDOR_CALL(device, send_command, cmd, arg1, arg2);
  408. }
  409.  
  410. static void camera_release(struct camera_device *device)
  411. {
  412.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  413.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  414.  
  415.     if (!device)
  416.         return;
  417.  
  418.     VENDOR_CALL(device, release);
  419. }
  420.  
  421. static int camera_dump(struct camera_device *device, int fd)
  422. {
  423.     ALOGV("%s->%08X->%08X", __FUNCTION__, (uintptr_t)device,
  424.             (uintptr_t)(((wrapper_camera_device_t*)device)->vendor));
  425.  
  426.     if (!device)
  427.         return -EINVAL;
  428.  
  429.     return VENDOR_CALL(device, dump, fd);
  430. }
  431.  
  432. extern "C" void heaptracker_free_leaked_memory(void);
  433.  
  434. static int camera_device_close(hw_device_t *device)
  435. {
  436.     int ret = 0;
  437.     wrapper_camera_device_t *wrapper_dev = NULL;
  438.  
  439.     ALOGV("%s", __FUNCTION__);
  440.  
  441.     android::Mutex::Autolock lock(gCameraWrapperLock);
  442.  
  443.     if (!device) {
  444.         ret = -EINVAL;
  445.         goto done;
  446.     }
  447.  
  448.     for (int i = 0; i < camera_get_number_of_cameras(); i++) {
  449.         if (fixed_set_params[i])
  450.             free(fixed_set_params[i]);
  451.     }
  452.  
  453.     wrapper_dev = (wrapper_camera_device_t*) device;
  454.  
  455.     wrapper_dev->vendor->common.close((hw_device_t*)wrapper_dev->vendor);
  456.     if (wrapper_dev->base.ops)
  457.         free(wrapper_dev->base.ops);
  458.     free(wrapper_dev);
  459. done:
  460. #ifdef HEAPTRACKER
  461.     heaptracker_free_leaked_memory();
  462. #endif
  463.     return ret;
  464. }
  465.  
  466. /*******************************************************************
  467.  * implementation of camera_module functions
  468.  *******************************************************************/
  469.  
  470. /* open device handle to one of the cameras
  471.  *
  472.  * assume camera service will keep singleton of each camera
  473.  * so this function will always only be called once per camera instance
  474.  */
  475.  
  476. static int camera_device_open(const hw_module_t *module, const char *name,
  477.         hw_device_t **device)
  478. {
  479.     int rv = 0;
  480.     int num_cameras = 0;
  481.     int cameraid;
  482.     wrapper_camera_device_t *camera_device = NULL;
  483.     camera_device_ops_t *camera_ops = NULL;
  484.  
  485.     android::Mutex::Autolock lock(gCameraWrapperLock);
  486.  
  487.     ALOGV("%s", __FUNCTION__);
  488.  
  489.     if (name != NULL) {
  490.         if (check_vendor_module())
  491.             return -EINVAL;
  492.  
  493.         cameraid = atoi(name);
  494.         num_cameras = gVendorModule->get_number_of_cameras();
  495.  
  496.         fixed_set_params = (char **) malloc(sizeof(char *) * num_cameras);
  497.         if (!fixed_set_params) {
  498.             ALOGE("parameter memory allocation fail");
  499.             rv = -ENOMEM;
  500.             goto fail;
  501.         }
  502.         memset(fixed_set_params, 0, sizeof(char *) * num_cameras);
  503.  
  504.         if (cameraid > num_cameras) {
  505.             ALOGE("camera service provided cameraid out of bounds, "
  506.                     "cameraid = %d, num supported = %d",
  507.                     cameraid, num_cameras);
  508.             rv = -EINVAL;
  509.             goto fail;
  510.         }
  511.  
  512.         camera_device = (wrapper_camera_device_t*)malloc(sizeof(*camera_device));
  513.         if (!camera_device) {
  514.             ALOGE("camera_device allocation fail");
  515.             rv = -ENOMEM;
  516.             goto fail;
  517.         }
  518.         memset(camera_device, 0, sizeof(*camera_device));
  519.         camera_device->id = cameraid;
  520.  
  521.         rv = gVendorModule->common.methods->open(
  522.                 (const hw_module_t*)gVendorModule, name,
  523.                 (hw_device_t**)&(camera_device->vendor));
  524.         if (rv) {
  525.             ALOGE("vendor camera open fail");
  526.             goto fail;
  527.         }
  528.         ALOGV("%s: got vendor camera device 0x%08X",
  529.                 __FUNCTION__, (uintptr_t)(camera_device->vendor));
  530.  
  531.         camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));
  532.         if (!camera_ops) {
  533.             ALOGE("camera_ops allocation fail");
  534.             rv = -ENOMEM;
  535.             goto fail;
  536.         }
  537.  
  538.         memset(camera_ops, 0, sizeof(*camera_ops));
  539.  
  540.         camera_device->base.common.tag = HARDWARE_DEVICE_TAG;
  541.         camera_device->base.common.version = 0;
  542.         camera_device->base.common.module = (hw_module_t *)(module);
  543.         camera_device->base.common.close = camera_device_close;
  544.         camera_device->base.ops = camera_ops;
  545.  
  546.         camera_ops->set_preview_window = camera_set_preview_window;
  547.         camera_ops->set_callbacks = camera_set_callbacks;
  548.         camera_ops->enable_msg_type = camera_enable_msg_type;
  549.         camera_ops->disable_msg_type = camera_disable_msg_type;
  550.         camera_ops->msg_type_enabled = camera_msg_type_enabled;
  551.         camera_ops->start_preview = camera_start_preview;
  552.         camera_ops->stop_preview = camera_stop_preview;
  553.         camera_ops->preview_enabled = camera_preview_enabled;
  554.         camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;
  555.         camera_ops->start_recording = camera_start_recording;
  556.         camera_ops->stop_recording = camera_stop_recording;
  557.         camera_ops->recording_enabled = camera_recording_enabled;
  558.         camera_ops->release_recording_frame = camera_release_recording_frame;
  559.         camera_ops->auto_focus = camera_auto_focus;
  560.         camera_ops->cancel_auto_focus = camera_cancel_auto_focus;
  561.         camera_ops->take_picture = camera_take_picture;
  562.         camera_ops->cancel_picture = camera_cancel_picture;
  563.         camera_ops->set_parameters = camera_set_parameters;
  564.         camera_ops->get_parameters = camera_get_parameters;
  565.         camera_ops->put_parameters = camera_put_parameters;
  566.         camera_ops->send_command = camera_send_command;
  567.         camera_ops->release = camera_release;
  568.         camera_ops->dump = camera_dump;
  569.  
  570.         *device = &camera_device->base.common;
  571.     }
  572.  
  573.     return rv;
  574.  
  575. fail:
  576.     if (camera_device) {
  577.         free(camera_device);
  578.         camera_device = NULL;
  579.     }
  580.     if (camera_ops) {
  581.         free(camera_ops);
  582.         camera_ops = NULL;
  583.     }
  584.     *device = NULL;
  585.     return rv;
  586. }
  587.  
  588. static int camera_get_number_of_cameras(void)
  589. {
  590.     ALOGV("%s", __FUNCTION__);
  591.     if (check_vendor_module())
  592.         return 0;
  593.     return gVendorModule->get_number_of_cameras();
  594. }
  595.  
  596. static int camera_get_camera_info(int camera_id, struct camera_info *info)
  597. {
  598.     ALOGV("%s", __FUNCTION__);
  599.     if (check_vendor_module())
  600.         return 0;
  601.     return gVendorModule->get_camera_info(camera_id, info);
  602. }
Advertisement
Add Comment
Please, Sign In to add comment