1 /*
2  * isp_poll_thread.cpp - isp poll thread for event and buffer
3  *
4  *  Copyright (c) 2014-2015 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Wind Yuan <feng.yuan@intel.com>
19  * Author: Yinhang Liu <yinhangx.liu@intel.com>
20  */
21 
22 #include "isp_poll_thread.h"
23 #include "x3a_statistics_queue.h"
24 #include <unistd.h>
25 
26 namespace XCam {
27 
28 class IspPollThread;
29 
IspPollThread()30 IspPollThread::IspPollThread ()
31 {
32     XCAM_LOG_DEBUG ("IspPollThread constructed");
33 }
34 
~IspPollThread()35 IspPollThread::~IspPollThread ()
36 {
37     stop();
38 
39     XCAM_LOG_DEBUG ("~IspPollThread destructed");
40 }
41 
42 bool
set_isp_controller(SmartPtr<IspController> & isp)43 IspPollThread::set_isp_controller (SmartPtr<IspController>  &isp)
44 {
45     XCAM_ASSERT (!_isp_controller.ptr());
46     _isp_controller = isp;
47     return true;
48 }
49 
50 XCamReturn
start()51 IspPollThread::start ()
52 {
53     _3a_stats_pool = new X3aStatisticsQueue;
54 
55     return PollThread::start ();
56 }
57 
58 XCamReturn
stop()59 IspPollThread::stop ()
60 {
61     if (_3a_stats_pool.ptr ())
62         _3a_stats_pool->stop ();
63 
64     return PollThread::stop ();
65 }
66 
67 XCamReturn
init_3a_stats_pool()68 IspPollThread::init_3a_stats_pool ()
69 {
70     XCamReturn ret = XCAM_RETURN_NO_ERROR;
71     struct atomisp_parm parameters;
72 
73     xcam_mem_clear (parameters);
74     ret = _isp_controller->get_isp_parameter (parameters);
75     if (ret != XCAM_RETURN_NO_ERROR ) {
76         XCAM_LOG_WARNING ("get isp parameters failed");
77         return ret;
78     }
79     if (!parameters.info.width || !parameters.info.height) {
80         XCAM_LOG_WARNING ("get isp parameters width or height wrong");
81         return XCAM_RETURN_ERROR_ISP;
82     }
83     _3a_stats_pool.dynamic_cast_ptr<X3aStatisticsQueue>()->set_grid_info (parameters.info);
84     if (!_3a_stats_pool->reserve (6)) {
85         XCAM_LOG_WARNING ("init_3a_stats_pool failed to reserve stats buffer.");
86         return XCAM_RETURN_ERROR_MEM;
87     }
88     return XCAM_RETURN_NO_ERROR;
89 }
90 
91 XCamReturn
capture_3a_stats(SmartPtr<X3aStats> & stats)92 IspPollThread::capture_3a_stats (SmartPtr<X3aStats> &stats)
93 {
94     XCamReturn ret = XCAM_RETURN_NO_ERROR;
95     SmartPtr<X3aIspStatistics> new_stats =
96         _3a_stats_pool->get_buffer (_3a_stats_pool).dynamic_cast_ptr<X3aIspStatistics> ();
97 
98     if (!new_stats.ptr()) {
99         XCAM_LOG_WARNING ("request stats buffer failed.");
100         return XCAM_RETURN_ERROR_MEM;
101     }
102 
103     ret = _isp_controller->get_3a_statistics (new_stats);
104     if (ret != XCAM_RETURN_NO_ERROR) {
105         XCAM_LOG_WARNING ("get 3a stats from ISP failed");
106         return ret;
107     }
108 
109     if (!new_stats->fill_standard_stats ()) {
110         XCAM_LOG_WARNING ("isp 3a stats failed to fill standard stats but continued");
111     }
112 
113     stats = new_stats;
114     return ret;
115 }
116 
117 
118 XCamReturn
handle_events(struct v4l2_event & event)119 IspPollThread::handle_events (struct v4l2_event &event)
120 {
121     XCamReturn ret = XCAM_RETURN_NO_ERROR;
122     switch (event.type) {
123     case V4L2_EVENT_ATOMISP_3A_STATS_READY:
124         ret = handle_3a_stats_event (event);
125         break;
126     case V4L2_EVENT_FRAME_SYNC:
127         break;
128     default:
129         ret = XCAM_RETURN_ERROR_UNKNOWN;
130         break;
131     }
132 
133     return ret;
134 }
135 
136 };
137