1625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/* Copyright (c) 2012-2013, The Linux Foundataion. All rights reserved.
2625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*
3625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* Redistribution and use in source and binary forms, with or without
4625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* modification, are permitted provided that the following conditions are
5625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* met:
6625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*     * Redistributions of source code must retain the above copyright
7625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*       notice, this list of conditions and the following disclaimer.
8625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*     * Redistributions in binary form must reproduce the above
9625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*       copyright notice, this list of conditions and the following
10625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*       disclaimer in the documentation and/or other materials provided
11625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*       with the distribution.
12625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*     * Neither the name of The Linux Foundation nor the names of its
13625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*       contributors may be used to endorse or promote products derived
14625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*       from this software without specific prior written permission.
15625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*
16625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*
28625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang*/
29625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
30625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#define LOG_TAG "QCamera3HWI"
3114dfc272241ba78c85a327da2872e71b3208f8c5Shuzhen Wang//#define LOG_NDEBUG 0
32625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
33625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include <cutils/properties.h>
34625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include <hardware/camera3.h>
35625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include <camera/CameraMetadata.h>
36625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include <stdlib.h>
37625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include <utils/Log.h>
38625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include <utils/Errors.h>
39e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang#include <ui/Fence.h>
40625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include <gralloc_priv.h>
41625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include "QCamera3HWI.h"
42625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include "QCamera3Mem.h"
43625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#include "QCamera3Channel.h"
4466cadfdce2ea19f4707bf2a595b58bd36fbfdf3bApurva Rajguru#include "QCamera3PostProc.h"
45625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
46625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangusing namespace android;
47625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
48625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangnamespace qcamera {
4962442157951e325f2e7b9d81e669992e8ba5564aShuzhen Wang
5062442157951e325f2e7b9d81e669992e8ba5564aShuzhen Wang#define MAX(a, b) ((a) > (b) ? (a) : (b))
5162442157951e325f2e7b9d81e669992e8ba5564aShuzhen Wang
52625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang#define DATA_PTR(MEM_OBJ,INDEX) MEM_OBJ->getPtr( INDEX )
53625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangcam_capability_t *gCamCapability[MM_CAMERA_MAX_NUM_SENSORS];
5480e50bfee66172f8a924573247aa99ee72bfe24fShuzhen Wangparm_buffer_t *prevSettings;
55b6c8485f9005cf0f759c49e2256e1a4382e5628cShuzhen Wangconst camera_metadata_t *gStaticMetadata[MM_CAMERA_MAX_NUM_SENSORS];
56625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
57cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wangpthread_mutex_t QCamera3HardwareInterface::mCameraSessionLock =
58cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    PTHREAD_MUTEX_INITIALIZER;
59cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wangunsigned int QCamera3HardwareInterface::mCameraSessionActive = 0;
60cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang
61662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godboleconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::EFFECT_MODES_MAP[] = {
62662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_OFF,       CAM_EFFECT_MODE_OFF },
63662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_MONO,       CAM_EFFECT_MODE_MONO },
64662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_NEGATIVE,   CAM_EFFECT_MODE_NEGATIVE },
65662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_SOLARIZE,   CAM_EFFECT_MODE_SOLARIZE },
66662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_SEPIA,      CAM_EFFECT_MODE_SEPIA },
67662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_POSTERIZE,  CAM_EFFECT_MODE_POSTERIZE },
68662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_WHITEBOARD, CAM_EFFECT_MODE_WHITEBOARD },
69662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_BLACKBOARD, CAM_EFFECT_MODE_BLACKBOARD },
70662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_EFFECT_MODE_AQUA,       CAM_EFFECT_MODE_AQUA }
71662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole};
72662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole
73662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godboleconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::WHITE_BALANCE_MODES_MAP[] = {
740e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_OFF,             CAM_WB_MODE_OFF },
75662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_AUTO,            CAM_WB_MODE_AUTO },
76662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_INCANDESCENT,    CAM_WB_MODE_INCANDESCENT },
77662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_FLUORESCENT,     CAM_WB_MODE_FLUORESCENT },
78662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_WARM_FLUORESCENT,CAM_WB_MODE_WARM_FLUORESCENT},
79662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_DAYLIGHT,        CAM_WB_MODE_DAYLIGHT },
80662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT, CAM_WB_MODE_CLOUDY_DAYLIGHT },
81662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_TWILIGHT,        CAM_WB_MODE_TWILIGHT },
82662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AWB_MODE_SHADE,           CAM_WB_MODE_SHADE }
83662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole};
84662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole
85662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godboleconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::SCENE_MODES_MAP[] = {
8634296c8a11c87ca1c00cfaedddd1f9089344ee80Shuzhen Wang    { ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY,  CAM_SCENE_MODE_OFF },
87662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_ACTION,         CAM_SCENE_MODE_ACTION },
88662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_PORTRAIT,       CAM_SCENE_MODE_PORTRAIT },
89662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_LANDSCAPE,      CAM_SCENE_MODE_LANDSCAPE },
90662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_NIGHT,          CAM_SCENE_MODE_NIGHT },
91662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_NIGHT_PORTRAIT, CAM_SCENE_MODE_NIGHT_PORTRAIT },
92662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_THEATRE,        CAM_SCENE_MODE_THEATRE },
93662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_BEACH,          CAM_SCENE_MODE_BEACH },
94662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_SNOW,           CAM_SCENE_MODE_SNOW },
95662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_SUNSET,         CAM_SCENE_MODE_SUNSET },
96662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_STEADYPHOTO,    CAM_SCENE_MODE_ANTISHAKE },
97662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_FIREWORKS ,     CAM_SCENE_MODE_FIREWORKS },
98662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_SPORTS ,        CAM_SCENE_MODE_SPORTS },
99662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_PARTY,          CAM_SCENE_MODE_PARTY },
100662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT,    CAM_SCENE_MODE_CANDLELIGHT },
10197e8019457b448a6e2141a49a0f471a467b70dc0Tanmaya Godbole    { ANDROID_CONTROL_SCENE_MODE_BARCODE,        CAM_SCENE_MODE_BARCODE}
102662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole};
103662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole
104662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godboleconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::FOCUS_MODES_MAP[] = {
105b66f295ef24aea24873bcf80cee589835338e2a1Shuzhen Wang    { ANDROID_CONTROL_AF_MODE_OFF,                CAM_FOCUS_MODE_FIXED },
106662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AF_MODE_AUTO,               CAM_FOCUS_MODE_AUTO },
107662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AF_MODE_MACRO,              CAM_FOCUS_MODE_MACRO },
108662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AF_MODE_EDOF,               CAM_FOCUS_MODE_EDOF },
109662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE, CAM_FOCUS_MODE_CONTINOUS_PICTURE },
110662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO,   CAM_FOCUS_MODE_CONTINOUS_VIDEO }
111662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole};
112662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole
113662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godboleconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::ANTIBANDING_MODES_MAP[] = {
114662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF,  CAM_ANTIBANDING_MODE_OFF },
115662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ, CAM_ANTIBANDING_MODE_50HZ },
116662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ, CAM_ANTIBANDING_MODE_60HZ },
117662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO, CAM_ANTIBANDING_MODE_AUTO }
118662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole};
119662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole
120e9a0b4995192d264f75543f62efa089845456291Shuzhen Wangconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::AE_FLASH_MODE_MAP[] = {
121e9a0b4995192d264f75543f62efa089845456291Shuzhen Wang    { ANDROID_CONTROL_AE_MODE_OFF,                  CAM_FLASH_MODE_OFF },
122e9a0b4995192d264f75543f62efa089845456291Shuzhen Wang    { ANDROID_CONTROL_AE_MODE_ON,                   CAM_FLASH_MODE_OFF },
123e9a0b4995192d264f75543f62efa089845456291Shuzhen Wang    { ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH,        CAM_FLASH_MODE_AUTO},
124e9a0b4995192d264f75543f62efa089845456291Shuzhen Wang    { ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH,      CAM_FLASH_MODE_ON  },
125e9a0b4995192d264f75543f62efa089845456291Shuzhen Wang    { ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE, CAM_FLASH_MODE_AUTO}
126662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole};
127662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole
128662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godboleconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::FLASH_MODES_MAP[] = {
129662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole    { ANDROID_FLASH_MODE_OFF,    CAM_FLASH_MODE_OFF  },
1306356e2d4fc01ff878ac4664d86892c8d81e330a9Ajay Dudani    { ANDROID_FLASH_MODE_SINGLE, CAM_FLASH_MODE_SINGLE },
1316356e2d4fc01ff878ac4664d86892c8d81e330a9Ajay Dudani    { ANDROID_FLASH_MODE_TORCH,  CAM_FLASH_MODE_TORCH }
132662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole};
133662d6f3c8101a94f45f3b0f892817e062a5c0faaTanmaya Godbole
1346990dc70b744c1fa9eb3d2d7eb27bcc89857746eShuzhen Wangconst QCamera3HardwareInterface::QCameraMap QCamera3HardwareInterface::FACEDETECT_MODES_MAP[] = {
1356990dc70b744c1fa9eb3d2d7eb27bcc89857746eShuzhen Wang    { ANDROID_STATISTICS_FACE_DETECT_MODE_OFF,    CAM_FACE_DETECT_MODE_OFF     },
1366990dc70b744c1fa9eb3d2d7eb27bcc89857746eShuzhen Wang    { ANDROID_STATISTICS_FACE_DETECT_MODE_FULL,   CAM_FACE_DETECT_MODE_FULL    }
1376990dc70b744c1fa9eb3d2d7eb27bcc89857746eShuzhen Wang};
1386990dc70b744c1fa9eb3d2d7eb27bcc89857746eShuzhen Wang
13908ad023e7517b0bb44b3e636abfea3ef3bba0affMansoor Aftabconst int32_t available_thumbnail_sizes[] = {512, 288, 480, 288, 256, 154, 432, 288,
14008ad023e7517b0bb44b3e636abfea3ef3bba0affMansoor Aftab                                             320, 240, 176, 144, 0, 0};
141625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
142625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangcamera3_device_ops_t QCamera3HardwareInterface::mCameraOps = {
143625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    initialize:                         QCamera3HardwareInterface::initialize,
144625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    configure_streams:                  QCamera3HardwareInterface::configure_streams,
145625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    register_stream_buffers:            QCamera3HardwareInterface::register_stream_buffers,
146625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    construct_default_request_settings: QCamera3HardwareInterface::construct_default_request_settings,
147625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    process_capture_request:            QCamera3HardwareInterface::process_capture_request,
148625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    get_metadata_vendor_tag_ops:        QCamera3HardwareInterface::get_metadata_vendor_tag_ops,
149625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    dump:                               QCamera3HardwareInterface::dump,
150c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang    flush:                              QCamera3HardwareInterface::flush,
151c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang    reserved:                           {0},
152625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang};
153625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
154f060ebe7f00ea62a029df01482e32b1d57c3efe6Mansoor Aftabint QCamera3HardwareInterface::kMaxInFlight = 5;
155625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
156625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/*===========================================================================
157625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * FUNCTION   : QCamera3HardwareInterface
158625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
159625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * DESCRIPTION: constructor of QCamera3HardwareInterface
160625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
161625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * PARAMETERS :
162625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *   @cameraId  : camera ID
163625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
164625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * RETURN     : none
165625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *==========================================================================*/
166625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen WangQCamera3HardwareInterface::QCamera3HardwareInterface(int cameraId)
167625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    : mCameraId(cameraId),
168625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang      mCameraHandle(NULL),
169625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang      mCameraOpened(false),
170cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang      mCameraInitialized(false),
17197e8019457b448a6e2141a49a0f471a467b70dc0Tanmaya Godbole      mCallbackOps(NULL),
17233cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang      mInputStream(NULL),
17333cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang      mMetadataChannel(NULL),
1747ee407aa1024789089aad411c372c79423699976Ajay Dudani      mPictureChannel(NULL),
17533cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang      mFirstRequest(false),
17633cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang      mParamHeap(NULL),
17733cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang      mParameters(NULL),
17899119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan      mJpegSettings(NULL),
1798bb5784949bef76a5880ca3c9a795c71b6ac680bAjay Dudani      mIsZslMode(false),
18062442157951e325f2e7b9d81e669992e8ba5564aShuzhen Wang      mMinProcessedFrameDuration(0),
18162442157951e325f2e7b9d81e669992e8ba5564aShuzhen Wang      mMinJpegFrameDuration(0),
18262442157951e325f2e7b9d81e669992e8ba5564aShuzhen Wang      mMinRawFrameDuration(0),
183dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab      m_pPowerModule(NULL),
184dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab      mHdrHint(false)
185625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang{
186625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mCameraDevice.common.tag = HARDWARE_DEVICE_TAG;
187e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    mCameraDevice.common.version = CAMERA_DEVICE_API_VERSION_3_0;
188e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    mCameraDevice.common.close = close_camera_device;
189625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mCameraDevice.ops = &mCameraOps;
190625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mCameraDevice.priv = this;
191625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    gCamCapability[cameraId]->version = CAM_HAL_V3;
1927ee407aa1024789089aad411c372c79423699976Ajay Dudani    // TODO: hardcode for now until mctl add support for min_num_pp_bufs
1937ee407aa1024789089aad411c372c79423699976Ajay Dudani    //TBD - To see if this hardcoding is needed. Check by printing if this is filled by mctl to 3
1947ee407aa1024789089aad411c372c79423699976Ajay Dudani    gCamCapability[cameraId]->min_num_pp_bufs = 3;
1959de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang
1969de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang    pthread_cond_init(&mRequestCond, NULL);
1979de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang    mPendingRequest = 0;
19874f6761b0f475069b59e715fcf8753b1664e0bdbMansoor Aftab    mCurrentRequestId = -1;
199e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    pthread_mutex_init(&mMutex, NULL);
20033cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang
20133cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang    for (size_t i = 0; i < CAMERA3_TEMPLATE_COUNT; i++)
20233cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang        mDefaultMetadata[i] = NULL;
20399119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan
20499119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan#ifdef HAS_MULTIMEDIA_HINTS
20599119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan    if (hw_get_module(POWER_HARDWARE_MODULE_ID, (const hw_module_t **)&m_pPowerModule)) {
20699119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan        ALOGE("%s: %s module not found", __func__, POWER_HARDWARE_MODULE_ID);
20799119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan    }
20899119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan#endif
209625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang}
210625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
211625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/*===========================================================================
212625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * FUNCTION   : ~QCamera3HardwareInterface
213625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
21466cadfdce2ea19f4707bf2a595b58bd36fbfdf3bApurva Rajguru * DESCRIPTION: destructor of QCamera3HardwareInterface
215625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
216625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * PARAMETERS : none
217625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
218625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * RETURN     : none
219625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *==========================================================================*/
220625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen WangQCamera3HardwareInterface::~QCamera3HardwareInterface()
221625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang{
222d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang    ALOGV("%s: E", __func__);
223d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang    /* We need to stop all streams before deleting any stream */
224d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab        /*flush the metadata list*/
225d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab    if (!mStoredMetadataList.empty()) {
226d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab        for (List<MetadataBufferInfo>::iterator m = mStoredMetadataList.begin();
227d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab              m != mStoredMetadataList.end(); m++) {
228d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab            mMetadataChannel->bufDone(m->meta_buf);
229d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab            free(m->meta_buf);
230d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab            m = mStoredMetadataList.erase(m);
231d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab        }
232d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab    }
2331fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang
2341fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang    // NOTE: 'camera3_stream_t *' objects are already freed at
2351fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang    //        this stage by the framework
2361c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole    for (List<stream_info_t *>::iterator it = mStreamInfo.begin();
2371c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole        it != mStreamInfo.end(); it++) {
2381fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang        QCamera3Channel *channel = (*it)->channel;
2391fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang        if (channel) {
2401fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang            channel->stop();
2411fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang        }
242d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang    }
2431fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang
244d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang    for (List<stream_info_t *>::iterator it = mStreamInfo.begin();
245d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang        it != mStreamInfo.end(); it++) {
2461fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang        QCamera3Channel *channel = (*it)->channel;
2470f0f251722ee12e53bab3d4e4918e541ebc82722Shuzhen Wang        if ((*it)->registered && (*it)->buffer_set.buffers) {
2480f0f251722ee12e53bab3d4e4918e541ebc82722Shuzhen Wang             delete[] (buffer_handle_t*)(*it)->buffer_set.buffers;
2490f0f251722ee12e53bab3d4e4918e541ebc82722Shuzhen Wang        }
250dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang        if (channel)
251dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang            delete channel;
2521c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole        free (*it);
2531c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole    }
2541c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole
2557ee407aa1024789089aad411c372c79423699976Ajay Dudani    mPictureChannel = NULL;
2567ee407aa1024789089aad411c372c79423699976Ajay Dudani
25797e8019457b448a6e2141a49a0f471a467b70dc0Tanmaya Godbole    if (mJpegSettings != NULL) {
25897e8019457b448a6e2141a49a0f471a467b70dc0Tanmaya Godbole        free(mJpegSettings);
25997e8019457b448a6e2141a49a0f471a467b70dc0Tanmaya Godbole        mJpegSettings = NULL;
26097e8019457b448a6e2141a49a0f471a467b70dc0Tanmaya Godbole    }
261dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang
262dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang    /* Clean up all channels */
263dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang    if (mCameraInitialized) {
2644e5fef6d28e89ec2635f92ea344d3947a7bd4422Shuzhen Wang        if (mMetadataChannel) {
2654e5fef6d28e89ec2635f92ea344d3947a7bd4422Shuzhen Wang            mMetadataChannel->stop();
2664e5fef6d28e89ec2635f92ea344d3947a7bd4422Shuzhen Wang            delete mMetadataChannel;
2674e5fef6d28e89ec2635f92ea344d3947a7bd4422Shuzhen Wang            mMetadataChannel = NULL;
2684e5fef6d28e89ec2635f92ea344d3947a7bd4422Shuzhen Wang        }
269dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang        deinitParameters();
270dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang    }
271dbeda6fde21ef974c09a2a8a89c6729f1873f660Shuzhen Wang
272cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    if (mCameraOpened)
273cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang        closeCamera();
274e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang
27533cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang    for (size_t i = 0; i < CAMERA3_TEMPLATE_COUNT; i++)
27633cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang        if (mDefaultMetadata[i])
27733cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang            free_camera_metadata(mDefaultMetadata[i]);
27833cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang
279e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    pthread_cond_destroy(&mRequestCond);
280e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang
281e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    pthread_mutex_destroy(&mMutex);
282d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang    ALOGV("%s: X", __func__);
283625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang}
284625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
285625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/*===========================================================================
286625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * FUNCTION   : openCamera
287625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
288625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * DESCRIPTION: open camera
289625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
290625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * PARAMETERS :
291625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *   @hw_device  : double ptr for camera device struct
292625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
293625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * RETURN     : int32_t type of status
294625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *              NO_ERROR  -- success
295625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *              none-zero failure code
296625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *==========================================================================*/
297625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangint QCamera3HardwareInterface::openCamera(struct hw_device_t **hw_device)
298625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang{
299625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    int rc = 0;
300cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    pthread_mutex_lock(&mCameraSessionLock);
301cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    if (mCameraSessionActive) {
302cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang        ALOGE("%s: multiple simultaneous camera instance not supported", __func__);
303cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang        pthread_mutex_unlock(&mCameraSessionLock);
304249d8d801ab218ff4920e57fdd4ccf17e1626057Zhijun He        return -EUSERS;
305cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    }
306cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang
307625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    if (mCameraOpened) {
308625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        *hw_device = NULL;
309625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        return PERMISSION_DENIED;
310625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    }
311625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
312625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    rc = openCamera();
313cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    if (rc == 0) {
314625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        *hw_device = &mCameraDevice.common;
315cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang        mCameraSessionActive = 1;
316cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    } else
317625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        *hw_device = NULL;
318cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang
31999119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan#ifdef HAS_MULTIMEDIA_HINTS
32099119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan    if (rc == 0) {
32199119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan        if (m_pPowerModule) {
32299119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan            if (m_pPowerModule->powerHint) {
32399119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan                m_pPowerModule->powerHint(m_pPowerModule, POWER_HINT_VIDEO_ENCODE,
32499119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan                        (void *)"state=1");
32599119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan            }
32699119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan        }
32799119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan    }
32899119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan#endif
329cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    pthread_mutex_unlock(&mCameraSessionLock);
330625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    return rc;
331625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang}
332625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
333625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/*===========================================================================
334625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * FUNCTION   : openCamera
335625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
336625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * DESCRIPTION: open camera
337625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
338625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * PARAMETERS : none
339625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
340625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * RETURN     : int32_t type of status
341625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *              NO_ERROR  -- success
342625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *              none-zero failure code
343625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *==========================================================================*/
344625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangint QCamera3HardwareInterface::openCamera()
345625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang{
346625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    if (mCameraHandle) {
347625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        ALOGE("Failure: Camera already opened");
348625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        return ALREADY_EXISTS;
349625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    }
350625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mCameraHandle = camera_open(mCameraId);
351625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    if (!mCameraHandle) {
352625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        ALOGE("camera_open failed.");
353625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        return UNKNOWN_ERROR;
354625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    }
355625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
356625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mCameraOpened = true;
357625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
358625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    return NO_ERROR;
359625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang}
360625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
361625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/*===========================================================================
362625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * FUNCTION   : closeCamera
363625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
364625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * DESCRIPTION: close camera
365625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
366625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * PARAMETERS : none
367625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
368625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * RETURN     : int32_t type of status
369625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *              NO_ERROR  -- success
370625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *              none-zero failure code
371625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *==========================================================================*/
372625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangint QCamera3HardwareInterface::closeCamera()
373625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang{
374625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    int rc = NO_ERROR;
375625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
376625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    rc = mCameraHandle->ops->close_camera(mCameraHandle->camera_handle);
377625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mCameraHandle = NULL;
378625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mCameraOpened = false;
379625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
38099119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan#ifdef HAS_MULTIMEDIA_HINTS
38199119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan    if (rc == NO_ERROR) {
38299119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan        if (m_pPowerModule) {
38399119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan            if (m_pPowerModule->powerHint) {
384dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                if(mHdrHint == true) {
385dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                    m_pPowerModule->powerHint(m_pPowerModule, POWER_HINT_VIDEO_ENCODE,
386dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                            (void *)"state=3");
387dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                    mHdrHint = false;
388dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                }
38999119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan                m_pPowerModule->powerHint(m_pPowerModule, POWER_HINT_VIDEO_ENCODE,
39099119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan                        (void *)"state=0");
39199119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan            }
39299119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan        }
39399119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan    }
39499119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan#endif
39599119ea1812fef51559a48bff4be66624cc55b0dMekala Natarajan
396625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    return rc;
397625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang}
398625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
399625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/*===========================================================================
400625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * FUNCTION   : initialize
401625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
402625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * DESCRIPTION: Initialize frameworks callback functions
403625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
404625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * PARAMETERS :
405625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *   @callback_ops : callback function to frameworks
406625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
407625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * RETURN     :
408625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
409625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *==========================================================================*/
410625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangint QCamera3HardwareInterface::initialize(
411625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        const struct camera3_callback_ops *callback_ops)
412625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang{
4139de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang    int rc;
414625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
415e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    pthread_mutex_lock(&mMutex);
416e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang
41733cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang    rc = initParameters();
41833cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang    if (rc < 0) {
41933cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang        ALOGE("%s: initParamters failed %d", __func__, rc);
42033cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang       goto err1;
42133cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang    }
4229de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang    mCallbackOps = callback_ops;
423e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang
424e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    pthread_mutex_unlock(&mMutex);
425cd500de28de7d527e4d6b1fbbfbe25bdc58829edShuzhen Wang    mCameraInitialized = true;
4269de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang    return 0;
4279de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang
4289de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wangerr1:
429e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang    pthread_mutex_unlock(&mMutex);
4309de643761e1282fb5af14a9249618efa9d8ac8feShuzhen Wang    return rc;
431625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang}
432625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
433625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang/*===========================================================================
434625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * FUNCTION   : configureStreams
435625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
436625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * DESCRIPTION: Reset HAL camera device processing pipeline and set up new input
437625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *              and output streams.
438625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
439625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * PARAMETERS :
440625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *   @stream_list : streams to be configured
441625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
442625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang * RETURN     :
443625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *
444625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang *==========================================================================*/
445625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wangint QCamera3HardwareInterface::configureStreams(
446625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        camera3_stream_configuration_t *streamList)
447625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang{
44833cb7276f6c4d2eda72253d11cd9242e1f82bf7dShuzhen Wang    int rc = 0;
449d63e05e886cb66cbbd321c0f5441e17e66ba25daMansoor Aftab    mIsZslMode = false;
45062595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang
451625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    // Sanity check stream_list
452625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    if (streamList == NULL) {
453625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        ALOGE("%s: NULL stream configuration", __func__);
454625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        return BAD_VALUE;
455625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    }
456625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    if (streamList->streams == NULL) {
457625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        ALOGE("%s: NULL stream list", __func__);
458625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        return BAD_VALUE;
459625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    }
460625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
461625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    if (streamList->num_streams < 1) {
462625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        ALOGE("%s: Bad number of streams requested: %d", __func__,
463625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang                streamList->num_streams);
464625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        return BAD_VALUE;
465625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    }
466625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
4670e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole    /* first invalidate all the steams in the mStreamList
4680e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole     * if they appear again, they will be validated */
4691c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole    for (List<stream_info_t*>::iterator it=mStreamInfo.begin();
4701c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole            it != mStreamInfo.end(); it++) {
471d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang        QCamera3Channel *channel = (QCamera3Channel*)(*it)->stream->priv;
472d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang        channel->stop();
4730e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        (*it)->status = INVALID;
4740e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole    }
47562595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    if (mMetadataChannel) {
47662595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        /* If content of mStreamInfo is not 0, there is metadata stream */
47762595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        mMetadataChannel->stop();
47862595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    }
47962595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang
480dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab#ifdef HAS_MULTIMEDIA_HINTS
481dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab    if(mHdrHint == true) {
482dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab        if (m_pPowerModule) {
483dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab            if (m_pPowerModule->powerHint) {
484dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                m_pPowerModule->powerHint(m_pPowerModule, POWER_HINT_VIDEO_ENCODE,
485dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                        (void *)"state=3");
486dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                mHdrHint = false;
487dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab            }
488dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab        }
489dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab    }
490dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab#endif
491dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab
49262595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    pthread_mutex_lock(&mMutex);
49362595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang
49462595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    camera3_stream_t *inputStream = NULL;
49562595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    camera3_stream_t *jpegStream = NULL;
49662595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    cam_stream_size_info_t stream_config_info;
497d5d5a90863708ac1cdbef25c41853b6f6f05d4e2Shuzhen Wang
498625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    for (size_t i = 0; i < streamList->num_streams; i++) {
499625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        camera3_stream_t *newStream = streamList->streams[i];
500c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang        ALOGV("%s: newStream type = %d, stream format = %d stream size : %d x %d",
501c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                __func__, newStream->stream_type, newStream->format,
502c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                 newStream->width, newStream->height);
5030e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        //if the stream is in the mStreamList validate it
5040e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        bool stream_exists = false;
5051c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole        for (List<stream_info_t*>::iterator it=mStreamInfo.begin();
5061c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                it != mStreamInfo.end(); it++) {
5070e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            if ((*it)->stream == newStream) {
5081c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                QCamera3Channel *channel =
5091c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                    (QCamera3Channel*)(*it)->stream->priv;
5100e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole                stream_exists = true;
5110e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole                (*it)->status = RECONFIGURE;
5121c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                /*delete the channel object associated with the stream because
5131c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                  we need to reconfigure*/
5141c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                delete channel;
5151c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                (*it)->stream->priv = NULL;
5161fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang                (*it)->channel = NULL;
5170e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            }
5180e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        }
5190e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        if (!stream_exists) {
5200e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            //new stream
5210e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            stream_info_t* stream_info;
5220e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            stream_info = (stream_info_t* )malloc(sizeof(stream_info_t));
5230e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            stream_info->stream = newStream;
5240e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            stream_info->status = VALID;
5251c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole            stream_info->registered = 0;
5261fca7d2974d2adb729b13ad51600e0b8d8478e48Shuzhen Wang            stream_info->channel = NULL;
5270e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            mStreamInfo.push_back(stream_info);
5280e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        }
529c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang        if (newStream->stream_type == CAMERA3_STREAM_INPUT
530c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                || newStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL ) {
531625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang            if (inputStream != NULL) {
532625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang                ALOGE("%s: Multiple input streams requested!", __func__);
533e6ab32d89cf169705236988f0f74309f914c88b7Shuzhen Wang                pthread_mutex_unlock(&mMutex);
534625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang                return BAD_VALUE;
535625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang            }
536625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang            inputStream = newStream;
537625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        }
538c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang        if (newStream->format == HAL_PIXEL_FORMAT_BLOB) {
539c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang            jpegStream = newStream;
540c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang        }
541625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    }
542625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    mInputStream = inputStream;
543625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
5440e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole    /*clean up invalid streams*/
5451c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole    for (List<stream_info_t*>::iterator it=mStreamInfo.begin();
5461c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole            it != mStreamInfo.end();) {
5470e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        if(((*it)->status) == INVALID){
5481c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole            QCamera3Channel *channel = (QCamera3Channel*)(*it)->stream->priv;
5491c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole            delete channel;
5500e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            delete[] (buffer_handle_t*)(*it)->buffer_set.buffers;
5510e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole            free(*it);
5521c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole            it = mStreamInfo.erase(it);
5531c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole        } else {
5541c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole            it++;
5550e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole        }
5560e03eb09035f20fd3e45ad4b6be5eb84303377deTanmaya Godbole    }
55762595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    if (mMetadataChannel) {
55862595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        delete mMetadataChannel;
55962595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        mMetadataChannel = NULL;
56062595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    }
561625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
56262595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    //Create metadata channel and initialize it
56362595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    mMetadataChannel = new QCamera3MetadataChannel(mCameraHandle->camera_handle,
56462595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang                    mCameraHandle->ops, captureResultCb,
56562595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang                    &gCamCapability[mCameraId]->padding_info, this);
56662595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    if (mMetadataChannel == NULL) {
56762595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        ALOGE("%s: failed to allocate metadata channel", __func__);
56862595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        rc = -ENOMEM;
56962595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        pthread_mutex_unlock(&mMutex);
57062595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        return rc;
57162595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    }
57262595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    rc = mMetadataChannel->initialize();
57362595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    if (rc < 0) {
57462595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        ALOGE("%s: metadata channel initialization failed", __func__);
57562595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        delete mMetadataChannel;
576f49d780c0f2fc137a3463e06537706d8829d6da5Shuzhen Wang        mMetadataChannel = NULL;
57762595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        pthread_mutex_unlock(&mMutex);
57862595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang        return rc;
57962595db7d19b3c5997145f35f1d26b87102aa984Shuzhen Wang    }
580625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
581625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    /* Allocate channel objects for the requested streams */
582625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang    for (size_t i = 0; i < streamList->num_streams; i++) {
583625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        camera3_stream_t *newStream = streamList->streams[i];
584dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang        uint32_t stream_usage = newStream->usage;
585d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang        stream_config_info.stream_sizes[i].width = newStream->width;
586d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang        stream_config_info.stream_sizes[i].height = newStream->height;
587d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang        if (newStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL &&
588d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang            newStream->format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED && jpegStream){
589d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang            //for zsl stream the size is jpeg size
590d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang            stream_config_info.stream_sizes[i].width = jpegStream->width;
591d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang            stream_config_info.stream_sizes[i].height = jpegStream->height;
592dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang            stream_config_info.type[i] = CAM_STREAM_TYPE_SNAPSHOT;
593dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang        } else {
594dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang           //for non zsl streams find out the format
595dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang           switch (newStream->format) {
596dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang           case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED :
597dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              {
598dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang                 if (stream_usage & private_handle_t::PRIV_FLAGS_VIDEO_ENCODER) {
599dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang                    stream_config_info.type[i] = CAM_STREAM_TYPE_VIDEO;
600dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang                 } else {
601dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang                    stream_config_info.type[i] = CAM_STREAM_TYPE_PREVIEW;
602dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang                 }
603dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              }
604dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              break;
605dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang           case HAL_PIXEL_FORMAT_YCbCr_420_888:
606dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              stream_config_info.type[i] = CAM_STREAM_TYPE_CALLBACK;
607dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab#ifdef HAS_MULTIMEDIA_HINTS
608dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab              if (m_pPowerModule) {
609dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                  if (m_pPowerModule->powerHint) {
610dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                      m_pPowerModule->powerHint(m_pPowerModule,
611dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                          POWER_HINT_VIDEO_ENCODE, (void *)"state=2");
612dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                      mHdrHint = true;
613dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab                  }
614dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab              }
615dd60b0cec98a84145dd88747b5cb642da3123802Mansoor Aftab#endif
616dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              break;
617dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang           case HAL_PIXEL_FORMAT_BLOB:
618dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              stream_config_info.type[i] = CAM_STREAM_TYPE_NON_ZSL_SNAPSHOT;
619dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              break;
620dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang           default:
621dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              stream_config_info.type[i] = CAM_STREAM_TYPE_DEFAULT;
622dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang              break;
623dac62b757b64e83a6add04a9fae98a857b1c28f8Shuzhen Wang           }
624d363f94a30706c0f951d037b3601bc2a0b445837Shuzhen Wang        }
625625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang        if (newStream->priv == NULL) {
626625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang            //New stream, construct channel
627625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang            switch (newStream->stream_type) {
628f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab            case CAMERA3_STREAM_INPUT:
629f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                newStream->usage = GRALLOC_USAGE_HW_CAMERA_READ;
630f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                break;
631f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab            case CAMERA3_STREAM_BIDIRECTIONAL:
632f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                newStream->usage = GRALLOC_USAGE_HW_CAMERA_READ |
633f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                    GRALLOC_USAGE_HW_CAMERA_WRITE;
634f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                break;
635f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab            case CAMERA3_STREAM_OUTPUT:
636c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                /* For video encoding stream, set read/write rarely
637c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                 * flag so that they may be set to un-cached */
638c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                if (newStream->usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
639c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                    newStream->usage =
640c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                         (GRALLOC_USAGE_SW_READ_RARELY |
641c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                         GRALLOC_USAGE_SW_WRITE_RARELY |
642c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                         GRALLOC_USAGE_HW_CAMERA_WRITE);
643c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                else
644c232705a3fe88a15722efec36dba335e417b7febShuzhen Wang                    newStream->usage = GRALLOC_USAGE_HW_CAMERA_WRITE;
645f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                break;
646f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab            default:
647f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                ALOGE("%s: Invalid stream_type %d", __func__, newStream->stream_type);
648f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                break;
649625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang            }
650625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang
651625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang            if (newStream->stream_type == CAMERA3_STREAM_OUTPUT ||
6521c6fa865f97afe06b8b9d9520390a37dd86f55e1Tanmaya Godbole                    newStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL) {
653625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang                QCamera3Channel *channel;
654625515beb9c1347216a2d261930ceb0d85ba1c47Shuzhen Wang                switch (newStream->format) {
655f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
6568867984d33ed870071109a4500402dddbc85c048Shuzhen Wang                case HAL_PIXEL_FORMAT_YCbCr_420_888:
657f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                    newStream->max_buffers = QCamera3RegularChannel::kMaxBuffers;
658c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                    if (newStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL &&
659c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                        jpegStream) {
660c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                        uint32_t width = jpegStream->width;
661c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                        uint32_t height = jpegStream->height;
6628bb5784949bef76a5880ca3c9a795c71b6ac680bAjay Dudani                        mIsZslMode = true;
663c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                        channel = new QCamera3RegularChannel(mCameraHandle->camera_handle,
664c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                            mCameraHandle->ops, captureResultCb,
665c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                            &gCamCapability[mCameraId]->padding_info, this, newStream,
666c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                            width, height);
667c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                    } else
668c8d1059ae679132e7654708fdfca6ee221775187Shuzhen Wang                        channel = new QCamera3RegularChannel(mCameraHandle->camera_handle,
669f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                            mCameraHandle->ops, captureResultCb,
670f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                            &gCamCapability[mCameraId]->padding_info, this, newStream);
671f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                    if (channel == NULL) {
672f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                        ALOGE("%s: allocation of channel failed", __func__);
673f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                        pthread_mutex_unlock(&mMutex);
674f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                        return -ENOMEM;
675f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                    }
676f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab
677f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                    newStream->priv = channel;
678f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                    break;
679f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                case HAL_PIXEL_FORMAT_BLOB:
680f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                    newStream->max_buffers = QCamera3PicChannel::kMaxBuffers;
6817ee407aa1024789089aad411c372c79423699976Ajay Dudani                    mPictureChannel = new QCamera3PicChannel(mCameraHandle->camera_handle,
682f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                            mCameraHandle->ops, captureResultCb,
683f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                            &gCamCapability[mCameraId]->padding_info, this, newStream);
6847ee407aa1024789089aad411c372c79423699976Ajay Dudani                    if (mPictureChannel == NULL) {
685f2fc1625456303cb52ec068d25308229e4322d4cMansoor Aftab                        ALOGE("%s: allocation of channel failed", __func__);
686