PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/libcamera/ExynosCameraHWInterface.cpp

https://gitlab.com/SerenityS/android_hardware_samsung_slsi-cm_exynos5410
C++ | 804 lines | 498 code | 106 blank | 200 comment | 75 complexity | 31ab4775c474f75c49e9d99ad15fbd51 MD5 | raw file
  1. /*
  2. **
  3. ** Copyright 2012, Samsung Electronics Co. LTD
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. /*!
  18. * \file ExynosCameraHWInterface.h
  19. * \brief source file for Android Camera HAL
  20. * \author thun.hwang(thun.hwang@samsung.com)
  21. * \date 2010/06/03
  22. *
  23. * <b>Revision History: </b>
  24. * - 2011/12/31 : thun.hwang(thun.hwang@samsung.com) \n
  25. * Initial version
  26. *
  27. * - 2012/02/01 : Sangwoo, Park(sw5771.park@samsung.com) \n
  28. * Adjust Android Standard features
  29. *
  30. * - 2012/03/14 : sangwoo.park(sw5771.park@samsung.com) \n
  31. * Change file, class name to ExynosXXX.
  32. *
  33. */
  34. //#define LOG_NDEBUG 0
  35. #define LOG_TAG "ExynosCameraHWInterface"
  36. #include <cutils/log.h>
  37. //#include "ExynosCameraHWInterface.h"
  38. #include "ExynosCameraHWImpl.h"
  39. #include "exynos_format.h"
  40. /*
  41. #define CHECK_PREVIEW_TIME 0
  42. #if ((CHECK_PREVIEW_TIME))
  43. struct timeval oldtime, curtime;
  44. #endif
  45. #define VIDEO_COMMENT_MARKER_H (0xFFBE)
  46. #define VIDEO_COMMENT_MARKER_L (0xFFBF)
  47. #define VIDEO_COMMENT_MARKER_LENGTH (4)
  48. #define JPEG_EOI_MARKER (0xFFD9)
  49. #define HIBYTE(x) (((x) >> 8) & 0xFF)
  50. #define LOBYTE(x) ((x) & 0xFF)
  51. //TODO: This values will be changed
  52. #define BACK_CAMERA_AUTO_FOCUS_DISTANCES_STR "0.10,1.20,Infinity"
  53. #define FRONT_CAMERA_FOCUS_DISTANCES_STR "0.20,0.25,Infinity"
  54. #define BACK_CAMERA_MACRO_FOCUS_DISTANCES_STR "0.10,0.20,Infinity"
  55. #define BACK_CAMERA_INFINITY_FOCUS_DISTANCES_STR "0.10,1.20,Infinity"
  56. #define BACK_CAMERA_FOCUS_DISTANCE_INFINITY "Infinity"
  57. #define FRONT_CAMERA_FOCUS_DISTANCE_INFINITY "Infinity"
  58. #define PREVIEW_GSC_NODE_NUM (1)
  59. #define PICTURE_GSC_NODE_NUM (2)
  60. */
  61. // This hack does two things:
  62. // -- it sets preview to NV21 (YUV420SP)
  63. // -- it sets gralloc to YV12
  64. //
  65. // The reason being: the samsung encoder understands only yuv420sp, and gralloc
  66. // does yv12 and rgb565. So what we do is we break up the interleaved UV in
  67. // separate V and U planes, which makes preview look good, and enabled the
  68. // encoder as well.
  69. //
  70. // FIXME: Samsung needs to enable support for proper yv12 coming out of the
  71. // camera, and to fix their video encoder to work with yv12.
  72. // FIXME: It also seems like either Samsung's YUV420SP (NV21) or img's YV12 has
  73. // the color planes switched. We need to figure which side is doing it
  74. // wrong and have the respective party fix it.
  75. #define MAX_NUM_OF_CAMERA 2
  76. namespace android {
  77. static CameraInfo sCameraInfo[] = {
  78. {
  79. CAMERA_FACING_BACK,
  80. BACK_ROTATION, /* orientation */
  81. },
  82. {
  83. CAMERA_FACING_FRONT,
  84. FRONT_ROTATION, /* orientation */
  85. }
  86. };
  87. /** Close this device */
  88. static camera_device_t *g_cam_device[MAX_NUM_OF_CAMERA];
  89. static Mutex g_cam_openLock[MAX_NUM_OF_CAMERA];
  90. static Mutex g_cam_previewLock[MAX_NUM_OF_CAMERA];
  91. static Mutex g_cam_recordingLock[MAX_NUM_OF_CAMERA];
  92. enum CAMERA_STATE {
  93. CAMERA_NONE,
  94. CAMERA_OPENED,
  95. CAMERA_RELEASED,
  96. CAMERA_CLOSED,
  97. CAMERA_PREVIEW,
  98. CAMERA_PREVIEWSTOPPED,
  99. CAMERA_RECORDING,
  100. CAMERA_RECORDINGSTOPPED,
  101. };
  102. static char *camera_state_enum2str[40] = {
  103. "NONE",
  104. "OPENED",
  105. "RELEASED",
  106. "CLOSED",
  107. "PREVIEW_RUNNING",
  108. "PREVIEW_STOPPED",
  109. "RECORDING_RUNNING",
  110. "RECORDING_STOPPED"
  111. };
  112. static Mutex cam_stateLock[3];
  113. static CAMERA_STATE cam_state[3];
  114. static inline ExynosCameraHWImpl *obj(struct camera_device *dev)
  115. {
  116. return reinterpret_cast<ExynosCameraHWImpl *>(dev->priv);
  117. }
  118. static int check_camera_state(CAMERA_STATE state, int cameraId)
  119. {
  120. bool ret = false;
  121. cam_stateLock[cameraId].lock();
  122. ALOGD("DEBUG(%s):camera(%d) state(%d) checking...", __func__, cameraId, state);
  123. switch (state) {
  124. case CAMERA_NONE:
  125. ret = true;
  126. break;
  127. case CAMERA_OPENED:
  128. if (cam_state[cameraId] == CAMERA_NONE
  129. || cam_state[cameraId] == CAMERA_CLOSED)
  130. ret = true;
  131. break;
  132. case CAMERA_RELEASED:
  133. if (cam_state[cameraId] == state
  134. || cam_state[cameraId] == CAMERA_OPENED
  135. || cam_state[cameraId] == CAMERA_PREVIEWSTOPPED)
  136. ret = true;
  137. break;
  138. case CAMERA_CLOSED:
  139. if (cam_state[cameraId] == state
  140. || cam_state[cameraId] == CAMERA_OPENED
  141. || cam_state[cameraId] == CAMERA_PREVIEWSTOPPED
  142. || cam_state[cameraId] == CAMERA_RELEASED)
  143. ret = true;
  144. break;
  145. case CAMERA_PREVIEW:
  146. if (cam_state[cameraId] == CAMERA_OPENED
  147. || cam_state[cameraId] == CAMERA_PREVIEWSTOPPED)
  148. ret = true;
  149. break;
  150. case CAMERA_PREVIEWSTOPPED:
  151. if (cam_state[cameraId] == state
  152. || cam_state[cameraId] == CAMERA_OPENED
  153. || cam_state[cameraId] == CAMERA_PREVIEW
  154. || cam_state[cameraId] == CAMERA_RECORDINGSTOPPED)
  155. ret = true;
  156. if (cam_state[cameraId] == CAMERA_OPENED)
  157. usleep(100*1000); /* for Preview Thread running */
  158. break;
  159. case CAMERA_RECORDING:
  160. if (cam_state[cameraId] == CAMERA_PREVIEW
  161. || cam_state[cameraId] == CAMERA_RECORDINGSTOPPED)
  162. ret = true;
  163. break;
  164. case CAMERA_RECORDINGSTOPPED:
  165. if (cam_state[cameraId] == state
  166. || cam_state[cameraId] == CAMERA_RECORDING)
  167. ret = true;
  168. break;
  169. default:
  170. ALOGE("ERR(%s):camera(%d) state(%s) is unknown value",
  171. __func__, cameraId, camera_state_enum2str[state]);
  172. ret = false;
  173. break;
  174. }
  175. if (ret == true)
  176. ALOGD("DEBUG(%s):camera(%d) state(%d:%s->%d:%s) is valid",
  177. __func__, cameraId,
  178. cam_state[cameraId], camera_state_enum2str[cam_state[cameraId]],
  179. state, camera_state_enum2str[state]);
  180. else
  181. ALOGE("ERR(%s):camera(%d) state(%d:%s->%d:%s) is INVALID",
  182. __func__, cameraId,
  183. cam_state[cameraId], camera_state_enum2str[cam_state[cameraId]],
  184. state, camera_state_enum2str[state]);
  185. cam_stateLock[cameraId].unlock();
  186. return ret;
  187. }
  188. static int HAL_camera_device_close(struct hw_device_t* device)
  189. {
  190. int cameraId;
  191. enum CAMERA_STATE state;
  192. ALOGI("[INFO] (%s:%d): in", __func__, __LINE__);
  193. if (device) {
  194. camera_device_t *cam_device = (camera_device_t *)device;
  195. cameraId = obj(cam_device)->getCameraId();
  196. ALOGI("[INFO] (%s:%d):camera(%d)", __func__, __LINE__, cameraId);
  197. state = CAMERA_CLOSED;
  198. if (check_camera_state(state, cameraId) == false) {
  199. ALOGE("ERR(%s):camera(%d) state(%d) is INVALID", __func__, cameraId, state);
  200. return -1;
  201. }
  202. g_cam_openLock[cameraId].lock();
  203. ALOGI("[INFO] (%s:%d):camera(%d) locked..", __func__, __LINE__, cameraId);
  204. g_cam_device[cameraId] = NULL;
  205. g_cam_openLock[cameraId].unlock();
  206. ALOGI("[INFO] (%s:%d):camera(%d) unlocked..", __func__, __LINE__, cameraId);
  207. delete static_cast<ExynosCameraHWImpl *>(cam_device->priv);
  208. free(cam_device);
  209. cam_stateLock[cameraId].lock();
  210. cam_state[cameraId] = state;
  211. cam_stateLock[cameraId].unlock();
  212. ALOGI("[INFO] (%s:%d):camera(%d)", __func__, __LINE__, cameraId);
  213. }
  214. ALOGI("[INFO] (%s:%d): out", __func__, __LINE__);
  215. return 0;
  216. }
  217. /** Set the preview_stream_ops to which preview frames are sent */
  218. static int HAL_camera_device_set_preview_window(struct camera_device *dev,
  219. struct preview_stream_ops *buf)
  220. {
  221. static int ret;
  222. int cameraId = obj(dev)->getCameraId();
  223. ALOGI("[INFO] (%s:%d):camera(%d) in", __func__, __LINE__, cameraId);
  224. ret = obj(dev)->setPreviewWindowLocked(buf);
  225. ALOGI("[INFO] (%s:%d):camera(%d) out", __func__, __LINE__, cameraId);
  226. return ret;
  227. }
  228. /** Set the notification and data callbacks */
  229. static void HAL_camera_device_set_callbacks(struct camera_device *dev,
  230. camera_notify_callback notify_cb,
  231. camera_data_callback data_cb,
  232. camera_data_timestamp_callback data_cb_timestamp,
  233. camera_request_memory get_memory,
  234. void* user)
  235. {
  236. ALOGV("DEBUG(%s):", __func__);
  237. obj(dev)->setCallbacks(notify_cb, data_cb, data_cb_timestamp,
  238. get_memory,
  239. user);
  240. }
  241. /**
  242. * The following three functions all take a msg_type, which is a bitmask of
  243. * the messages defined in include/ui/Camera.h
  244. */
  245. /**
  246. * Enable a message, or set of messages.
  247. */
  248. static void HAL_camera_device_enable_msg_type(struct camera_device *dev, int32_t msg_type)
  249. {
  250. ALOGV("DEBUG(%s):", __func__);
  251. obj(dev)->enableMsgType(msg_type);
  252. }
  253. /**
  254. * Disable a message, or a set of messages.
  255. *
  256. * Once received a call to disableMsgType(CAMERA_MSG_VIDEO_FRAME), camera
  257. * HAL should not rely on its client to call releaseRecordingFrame() to
  258. * release video recording frames sent out by the cameral HAL before and
  259. * after the disableMsgType(CAMERA_MSG_VIDEO_FRAME) call. Camera HAL
  260. * clients must not modify/access any video recording frame after calling
  261. * disableMsgType(CAMERA_MSG_VIDEO_FRAME).
  262. */
  263. static void HAL_camera_device_disable_msg_type(struct camera_device *dev, int32_t msg_type)
  264. {
  265. ALOGV("DEBUG(%s):", __func__);
  266. obj(dev)->disableMsgType(msg_type);
  267. }
  268. /**
  269. * Query whether a message, or a set of messages, is enabled. Note that
  270. * this is operates as an AND, if any of the messages queried are off, this
  271. * will return false.
  272. */
  273. static int HAL_camera_device_msg_type_enabled(struct camera_device *dev, int32_t msg_type)
  274. {
  275. ALOGV("DEBUG(%s):", __func__);
  276. return obj(dev)->msgTypeEnabled(msg_type);
  277. }
  278. /**
  279. * Start preview mode.
  280. */
  281. static int HAL_camera_device_start_preview(struct camera_device *dev)
  282. {
  283. static int ret;
  284. int cameraId = obj(dev)->getCameraId();
  285. enum CAMERA_STATE state;
  286. ALOGI("[INFO] (%s:%d):camera(%d) in", __func__, __LINE__, cameraId);
  287. state = CAMERA_PREVIEW;
  288. if (check_camera_state(state, cameraId) == false) {
  289. ALOGE("ERR(%s):camera(%d) state(%d) is INVALID", __func__, cameraId, state);
  290. return -1;
  291. }
  292. g_cam_previewLock[cameraId].lock();
  293. ret = obj(dev)->startPreviewLocked();
  294. ALOGI("[INFO] (%s:%d):camera(%d) out from startPreviewLocked()", __func__, __LINE__, cameraId);
  295. g_cam_previewLock[cameraId].unlock();
  296. ALOGI("[INFO] (%s:%d):camera(%d) unlocked..", __func__, __LINE__, cameraId);
  297. if (ret == OK) {
  298. cam_stateLock[cameraId].lock();
  299. cam_state[cameraId] = state;
  300. cam_stateLock[cameraId].unlock();
  301. ALOGI("[INFO] (%s:%d):camera(%d) out (startPreview succeeded)", __func__, __LINE__, cameraId);
  302. } else {
  303. ALOGI("[INFO] (%s:%d):camera(%d) out (startPreview FAILED)", __func__, __LINE__, cameraId);
  304. }
  305. return ret;
  306. }
  307. /**
  308. * Stop a previously started preview.
  309. */
  310. static void HAL_camera_device_stop_recording(struct camera_device *dev);
  311. static void HAL_camera_device_stop_preview(struct camera_device *dev)
  312. {
  313. int cameraId = obj(dev)->getCameraId();
  314. enum CAMERA_STATE state;
  315. ALOGI("[INFO] (%s:%d):camera(%d) in", __func__, __LINE__, cameraId);
  316. /* HACK : If camera in recording state, */
  317. /* CameraService have to call the stop_recording before the stop_preview */
  318. #if 1
  319. if (cam_state[cameraId] == CAMERA_RECORDING) {
  320. ALOGE("(%s:%d) camera(%d) in RECORDING RUNNING state ---- INVALID ----", __func__, __LINE__, cameraId);
  321. ALOGE("(%s:%d) camera(%d) The stop_recording must be called "
  322. "before the stop_preview ---- INVALID ----",
  323. __func__, __LINE__, cameraId);
  324. HAL_camera_device_stop_recording(dev);
  325. ALOGE("(%s:%d) cameraId=%d out from stop_recording ---- INVALID ----",
  326. __func__, __LINE__, cameraId);
  327. for (int i=0; i<30; i++) {
  328. ALOGE("(%s:%d) camera(%d) The stop_recording must be called "
  329. "before the stop_preview ---- INVALID ----",
  330. __func__, __LINE__, cameraId);
  331. }
  332. ALOGE("(%s:%d) camera(%d) sleep 500ms for ---- INVALID ---- state", __func__, __LINE__, cameraId);
  333. usleep(500000); /* to notify, sleep 500ms */
  334. }
  335. #endif
  336. state = CAMERA_PREVIEWSTOPPED;
  337. if (check_camera_state(state, cameraId) == false) {
  338. ALOGE("ERR(%s):camera(%d) state(%d) is INVALID", __func__, cameraId, state);
  339. return;
  340. }
  341. g_cam_previewLock[cameraId].lock();
  342. obj(dev)->stopPreviewLocked();
  343. ALOGI("[INFO] (%s:%d):camera(%d) out from stopPreviewLocked()", __func__, __LINE__, cameraId);
  344. g_cam_previewLock[cameraId].unlock();
  345. ALOGI("[INFO] (%s:%d):camera(%d) unlocked..", __func__, __LINE__, cameraId);
  346. cam_stateLock[cameraId].lock();
  347. cam_state[cameraId] = state;
  348. cam_stateLock[cameraId].unlock();
  349. ALOGI("[INFO] (%s:%d):camera(%d) out", __func__, __LINE__, cameraId);
  350. }
  351. /**
  352. * Returns true if preview is enabled.
  353. */
  354. static int HAL_camera_device_preview_enabled(struct camera_device *dev)
  355. {
  356. ALOGV("DEBUG(%s):", __func__);
  357. return obj(dev)->previewEnabled();
  358. }
  359. /**
  360. * Request the camera HAL to store meta data or real YUV data in the video
  361. * buffers sent out via CAMERA_MSG_VIDEO_FRAME for a recording session. If
  362. * it is not called, the default camera HAL behavior is to store real YUV
  363. * data in the video buffers.
  364. *
  365. * This method should be called before startRecording() in order to be
  366. * effective.
  367. *
  368. * If meta data is stored in the video buffers, it is up to the receiver of
  369. * the video buffers to interpret the contents and to find the actual frame
  370. * data with the help of the meta data in the buffer. How this is done is
  371. * outside of the scope of this method.
  372. *
  373. * Some camera HALs may not support storing meta data in the video buffers,
  374. * but all camera HALs should support storing real YUV data in the video
  375. * buffers. If the camera HAL does not support storing the meta data in the
  376. * video buffers when it is requested to do do, INVALID_OPERATION must be
  377. * returned. It is very useful for the camera HAL to pass meta data rather
  378. * than the actual frame data directly to the video encoder, since the
  379. * amount of the uncompressed frame data can be very large if video size is
  380. * large.
  381. *
  382. * @param enable if true to instruct the camera HAL to store
  383. * meta data in the video buffers; false to instruct
  384. * the camera HAL to store real YUV data in the video
  385. * buffers.
  386. *
  387. * @return OK on success.
  388. */
  389. static int HAL_camera_device_store_meta_data_in_buffers(struct camera_device *dev, int enable)
  390. {
  391. ALOGV("DEBUG(%s):", __func__);
  392. return obj(dev)->storeMetaDataInBuffers(enable);
  393. }
  394. /**
  395. * Start record mode. When a record image is available, a
  396. * CAMERA_MSG_VIDEO_FRAME message is sent with the corresponding
  397. * frame. Every record frame must be released by a camera HAL client via
  398. * releaseRecordingFrame() before the client calls
  399. * disableMsgType(CAMERA_MSG_VIDEO_FRAME). After the client calls
  400. * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is the camera HAL's
  401. * responsibility to manage the life-cycle of the video recording frames,
  402. * and the client must not modify/access any video recording frames.
  403. */
  404. static int HAL_camera_device_start_recording(struct camera_device *dev)
  405. {
  406. static int ret;
  407. int cameraId = obj(dev)->getCameraId();
  408. enum CAMERA_STATE state;
  409. ALOGI("[INFO] (%s:%d):camera(%d) in", __func__, __LINE__, cameraId);
  410. state = CAMERA_RECORDING;
  411. if (check_camera_state(state, cameraId) == false) {
  412. ALOGE("ERR(%s):camera(%d) state(%d) is INVALID", __func__, cameraId, state);
  413. return -1;
  414. }
  415. g_cam_recordingLock[cameraId].lock();
  416. ret = obj(dev)->startRecording();
  417. ALOGI("[INFO] (%s:%d):camera(%d) out from startRecording()", __func__, __LINE__, cameraId);
  418. g_cam_recordingLock[cameraId].unlock();
  419. ALOGI("[INFO] (%s:%d):camera(%d) unlocked..", __func__, __LINE__, cameraId);
  420. if (ret == OK) {
  421. cam_stateLock[cameraId].lock();
  422. cam_state[cameraId] = state;
  423. cam_stateLock[cameraId].unlock();
  424. ALOGI("[INFO] (%s:%d):camera(%d) out (startRecording succeeded)", __func__, __LINE__, cameraId);
  425. } else {
  426. ALOGI("[INFO] (%s:%d):camera(%d) out (startRecording FAILED)", __func__, __LINE__, cameraId);
  427. }
  428. return ret;
  429. }
  430. /**
  431. * Stop a previously started recording.
  432. */
  433. static void HAL_camera_device_stop_recording(struct camera_device *dev)
  434. {
  435. int cameraId = obj(dev)->getCameraId();
  436. enum CAMERA_STATE state;
  437. ALOGI("[INFO] (%s:%d):camera(%d) in", __func__, __LINE__, cameraId);
  438. state = CAMERA_RECORDINGSTOPPED;
  439. if (check_camera_state(state, cameraId) == false) {
  440. ALOGE("ERR(%s):camera(%d) state(%d) is INVALID", __func__, cameraId, state);
  441. return;
  442. }
  443. g_cam_recordingLock[cameraId].lock();
  444. obj(dev)->stopRecording();
  445. ALOGI("[INFO] (%s:%d):camera(%d) out from stopRecording()", __func__, __LINE__, cameraId);
  446. g_cam_recordingLock[cameraId].unlock();
  447. ALOGI("[INFO] (%s:%d):camera(%d) unlocked..", __func__, __LINE__, cameraId);
  448. cam_stateLock[cameraId].lock();
  449. cam_state[cameraId] = state;
  450. cam_stateLock[cameraId].unlock();
  451. ALOGI("[INFO] (%s:%d):camera(%d) out", __func__, __LINE__, cameraId);
  452. }
  453. /**
  454. * Returns true if recording is enabled.
  455. */
  456. static int HAL_camera_device_recording_enabled(struct camera_device *dev)
  457. {
  458. ALOGV("DEBUG(%s):", __func__);
  459. return obj(dev)->recordingEnabled();
  460. }
  461. /**
  462. * Release a record frame previously returned by CAMERA_MSG_VIDEO_FRAME.
  463. *
  464. * It is camera HAL client's responsibility to release video recording
  465. * frames sent out by the camera HAL before the camera HAL receives a call
  466. * to disableMsgType(CAMERA_MSG_VIDEO_FRAME). After it receives the call to
  467. * disableMsgType(CAMERA_MSG_VIDEO_FRAME), it is the camera HAL's
  468. * responsibility to manage the life-cycle of the video recording frames.
  469. */
  470. static void HAL_camera_device_release_recording_frame(struct camera_device *dev,
  471. const void *opaque)
  472. {
  473. ALOGV("DEBUG(%s):", __func__);
  474. obj(dev)->releaseRecordingFrame(opaque);
  475. }
  476. /**
  477. * Start auto focus, the notification callback routine is called with
  478. * CAMERA_MSG_FOCUS once when focusing is complete. autoFocus() will be
  479. * called again if another auto focus is needed.
  480. */
  481. static int HAL_camera_device_auto_focus(struct camera_device *dev)
  482. {
  483. ALOGV("DEBUG(%s):", __func__);
  484. return obj(dev)->autoFocus();
  485. }
  486. /**
  487. * Cancels auto-focus function. If the auto-focus is still in progress,
  488. * this function will cancel it. Whether the auto-focus is in progress or
  489. * not, this function will return the focus position to the default. If
  490. * the camera does not support auto-focus, this is a no-op.
  491. */
  492. static int HAL_camera_device_cancel_auto_focus(struct camera_device *dev)
  493. {
  494. ALOGV("DEBUG(%s):", __func__);
  495. return obj(dev)->cancelAutoFocus();
  496. }
  497. /**
  498. * Take a picture.
  499. */
  500. static int HAL_camera_device_take_picture(struct camera_device *dev)
  501. {
  502. ALOGV("DEBUG(%s):", __func__);
  503. return obj(dev)->takePicture();
  504. }
  505. /**
  506. * Cancel a picture that was started with takePicture. Calling this method
  507. * when no picture is being taken is a no-op.
  508. */
  509. static int HAL_camera_device_cancel_picture(struct camera_device *dev)
  510. {
  511. ALOGV("DEBUG(%s):", __func__);
  512. return obj(dev)->cancelPicture();
  513. }
  514. /**
  515. * Set the camera parameters. This returns BAD_VALUE if any parameter is
  516. * invalid or not supported.
  517. */
  518. static int HAL_camera_device_set_parameters(struct camera_device *dev,
  519. const char *parms)
  520. {
  521. ALOGV("DEBUG(%s):", __func__);
  522. String8 str(parms);
  523. CameraParameters p(str);
  524. return obj(dev)->setParametersLocked(p);
  525. }
  526. /** Return the camera parameters. */
  527. char *HAL_camera_device_get_parameters(struct camera_device *dev)
  528. {
  529. ALOGV("DEBUG(%s):", __func__);
  530. String8 str;
  531. CameraParameters parms = obj(dev)->getParameters();
  532. str = parms.flatten();
  533. return strdup(str.string());
  534. }
  535. static void HAL_camera_device_put_parameters(struct camera_device *dev, char *parms)
  536. {
  537. ALOGV("DEBUG(%s):", __func__);
  538. free(parms);
  539. }
  540. /**
  541. * Send command to camera driver.
  542. */
  543. static int HAL_camera_device_send_command(struct camera_device *dev,
  544. int32_t cmd, int32_t arg1, int32_t arg2)
  545. {
  546. ALOGV("DEBUG(%s):", __func__);
  547. return obj(dev)->sendCommand(cmd, arg1, arg2);
  548. }
  549. /**
  550. * Release the hardware resources owned by this object. Note that this is
  551. * *not* done in the destructor.
  552. */
  553. static void HAL_camera_device_release(struct camera_device *dev)
  554. {
  555. int cameraId = obj(dev)->getCameraId();
  556. enum CAMERA_STATE state;
  557. ALOGI("[INFO] (%s:%d):camera(%d) in", __func__, __LINE__, cameraId);
  558. state = CAMERA_RELEASED;
  559. if (check_camera_state(state, cameraId) == false) {
  560. ALOGE("ERR(%s):camera(%d) state(%d) is INVALID", __func__, cameraId, state);
  561. return;
  562. }
  563. g_cam_openLock[cameraId].lock();
  564. obj(dev)->release();
  565. ALOGI("[INFO] (%s:%d):camera(%d) out from release()", __func__, __LINE__, cameraId);
  566. g_cam_openLock[cameraId].unlock();
  567. ALOGI("[INFO] (%s:%d):camera(%d) unlocked..", __func__, __LINE__, cameraId);
  568. cam_stateLock[cameraId].lock();
  569. cam_state[cameraId] = state;
  570. cam_stateLock[cameraId].unlock();
  571. ALOGI("[INFO] (%s:%d):camera(%d) out", __func__, __LINE__, cameraId);
  572. }
  573. /**
  574. * Dump state of the camera hardware
  575. */
  576. static int HAL_camera_device_dump(struct camera_device *dev, int fd)
  577. {
  578. ALOGV("DEBUG(%s):", __func__);
  579. return obj(dev)->dump(fd);
  580. }
  581. static int HAL_getNumberOfCameras()
  582. {
  583. ALOGV("DEBUG(%s):", __func__);
  584. return sizeof(sCameraInfo) / sizeof(sCameraInfo[0]);
  585. }
  586. static int HAL_getCameraInfo(int cameraId, struct camera_info *info)
  587. {
  588. ALOGV("DEBUG(%s):", __func__);
  589. if (cameraId < 0 || cameraId >= HAL_getNumberOfCameras()) {
  590. ALOGE("ERR(%s):Invalid camera ID %d", __func__, cameraId);
  591. return -EINVAL;
  592. }
  593. memcpy(info, &sCameraInfo[cameraId], sizeof(CameraInfo));
  594. info->device_version = HARDWARE_DEVICE_API_VERSION(1, 0);
  595. return NO_ERROR;
  596. }
  597. #define SET_METHOD(m) m : HAL_camera_device_##m
  598. static camera_device_ops_t camera_device_ops = {
  599. SET_METHOD(set_preview_window),
  600. SET_METHOD(set_callbacks),
  601. SET_METHOD(enable_msg_type),
  602. SET_METHOD(disable_msg_type),
  603. SET_METHOD(msg_type_enabled),
  604. SET_METHOD(start_preview),
  605. SET_METHOD(stop_preview),
  606. SET_METHOD(preview_enabled),
  607. SET_METHOD(store_meta_data_in_buffers),
  608. SET_METHOD(start_recording),
  609. SET_METHOD(stop_recording),
  610. SET_METHOD(recording_enabled),
  611. SET_METHOD(release_recording_frame),
  612. SET_METHOD(auto_focus),
  613. SET_METHOD(cancel_auto_focus),
  614. SET_METHOD(take_picture),
  615. SET_METHOD(cancel_picture),
  616. SET_METHOD(set_parameters),
  617. SET_METHOD(get_parameters),
  618. SET_METHOD(put_parameters),
  619. SET_METHOD(send_command),
  620. SET_METHOD(release),
  621. SET_METHOD(dump),
  622. };
  623. #undef SET_METHOD
  624. static int HAL_camera_device_open(const struct hw_module_t* module,
  625. const char *id,
  626. struct hw_device_t** device)
  627. {
  628. int cameraId = atoi(id);
  629. enum CAMERA_STATE state;
  630. ALOGI("[INFO] (%s:%d):camera(%d) in", __func__, __LINE__, cameraId);
  631. if (cameraId < 0 || cameraId >= HAL_getNumberOfCameras()) {
  632. ALOGE("ERR(%s):Invalid camera ID %s", __func__, id);
  633. return -EINVAL;
  634. }
  635. state = CAMERA_OPENED;
  636. if (check_camera_state(state, cameraId) == false) {
  637. ALOGE("ERR(%s):camera(%d) state(%d) is INVALID", __func__, cameraId, state);
  638. return -1;
  639. }
  640. if (cameraId < (sizeof(sCameraInfo) / sizeof(sCameraInfo[0]))) {
  641. if (g_cam_device[cameraId]) {
  642. ALOGE("DEBUG(%s):returning existing camera ID %s", __func__, id);
  643. *device = (hw_device_t *)g_cam_device[cameraId];
  644. goto done;
  645. }
  646. g_cam_device[cameraId] = (camera_device_t *)malloc(sizeof(camera_device_t));
  647. if (!g_cam_device[cameraId])
  648. return -ENOMEM;
  649. g_cam_openLock[cameraId].lock();
  650. g_cam_device[cameraId]->common.tag = HARDWARE_DEVICE_TAG;
  651. g_cam_device[cameraId]->common.version = 1;
  652. g_cam_device[cameraId]->common.module = const_cast<hw_module_t *>(module);
  653. g_cam_device[cameraId]->common.close = HAL_camera_device_close;
  654. g_cam_device[cameraId]->ops = &camera_device_ops;
  655. ALOGE("DEBUG(%s):open camera %s", __func__, id);
  656. g_cam_device[cameraId]->priv = new ExynosCameraHWImpl(cameraId, g_cam_device[cameraId]);
  657. *device = (hw_device_t *)g_cam_device[cameraId];
  658. ALOGI("[INFO] (%s:%d):camera(%d) out from new g_cam_device[%d]->priv()", __func__, __LINE__, cameraId, cameraId);
  659. g_cam_openLock[cameraId].unlock();
  660. ALOGI("[INFO] (%s:%d):camera(%d) unlocked..", __func__, __LINE__, cameraId);
  661. } else {
  662. ALOGE("DEBUG(%s):camera(%s) open fail - must front camera open first", __func__, id);
  663. return -EINVAL;
  664. }
  665. done:
  666. cam_stateLock[cameraId].lock();
  667. cam_state[cameraId] = state;
  668. cam_stateLock[cameraId].unlock();
  669. ALOGI("[INFO] (%s:%d):camera(%d) out", __func__, __LINE__, cameraId);
  670. return 0;
  671. }
  672. static hw_module_methods_t camera_module_methods = {
  673. open : HAL_camera_device_open
  674. };
  675. extern "C" {
  676. struct camera_module HAL_MODULE_INFO_SYM = {
  677. common : {
  678. tag : HARDWARE_MODULE_TAG,
  679. module_api_version : CAMERA_MODULE_API_VERSION_1_0,
  680. hal_api_version : HARDWARE_HAL_API_VERSION,
  681. id : CAMERA_HARDWARE_MODULE_ID,
  682. name : "Exynos Camera HAL1",
  683. author : "Samsung Corporation",
  684. methods : &camera_module_methods,
  685. dso : NULL,
  686. reserved : {0},
  687. },
  688. get_number_of_cameras : HAL_getNumberOfCameras,
  689. get_camera_info : HAL_getCameraInfo
  690. };
  691. }
  692. }; // namespace android