1 /*
2 * pipe_manager.cpp - pipe manager
3 *
4 * Copyright (c) 2016 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 "pipe_manager.h"
23
24 #define XCAM_FAILED_STOP(exp, msg, ...) \
25 if ((exp) != XCAM_RETURN_NO_ERROR) { \
26 XCAM_LOG_ERROR (msg, ## __VA_ARGS__); \
27 stop (); \
28 return ret; \
29 }
30
31 namespace XCam {
32
PipeManager()33 PipeManager::PipeManager ()
34 : _is_running (false)
35 {
36 _processor_center = new X3aImageProcessCenter;
37 XCAM_LOG_DEBUG ("PipeManager construction");
38 }
39
~PipeManager()40 PipeManager::~PipeManager ()
41 {
42 XCAM_LOG_DEBUG ("PipeManager destruction");
43 }
44
45 bool
set_smart_analyzer(SmartPtr<SmartAnalyzer> analyzer)46 PipeManager::set_smart_analyzer (SmartPtr<SmartAnalyzer> analyzer)
47 {
48 if (is_running ())
49 return false;
50
51 XCAM_ASSERT (analyzer.ptr () && !_smart_analyzer.ptr ());
52 _smart_analyzer = analyzer;
53
54 return true;
55 }
56
57 bool
add_image_processor(SmartPtr<ImageProcessor> processor)58 PipeManager::add_image_processor (SmartPtr<ImageProcessor> processor)
59 {
60 if (is_running ())
61 return false;
62
63 XCAM_ASSERT (processor.ptr ());
64 return _processor_center->insert_processor (processor);
65 }
66
67 XCamReturn
start()68 PipeManager::start ()
69 {
70 XCamReturn ret = XCAM_RETURN_NO_ERROR;
71
72 if (_smart_analyzer.ptr ()) {
73 if (_smart_analyzer->prepare_handlers () != XCAM_RETURN_NO_ERROR) {
74 XCAM_LOG_INFO ("prepare smart analyzer handler failed");
75 }
76
77 _smart_analyzer->set_results_callback (this);
78 if (_smart_analyzer->init (1920, 1080, 25) != XCAM_RETURN_NO_ERROR) {
79 XCAM_LOG_INFO ("initialize smart analyzer failed");
80 }
81 if (_smart_analyzer->start () != XCAM_RETURN_NO_ERROR) {
82 XCAM_LOG_INFO ("start smart analyzer failed");
83 }
84 }
85
86 if (!_processor_center->has_processors ()) {
87 XCAM_LOG_ERROR ("image processors empty");
88 }
89 _processor_center->set_image_callback (this);
90 XCAM_FAILED_STOP (ret = _processor_center->start (), "3A process center start failed");
91
92 _is_running = true;
93
94 XCAM_LOG_DEBUG ("pipe manager started");
95 return XCAM_RETURN_NO_ERROR;
96 }
97
98 XCamReturn
stop()99 PipeManager::stop ()
100 {
101 _is_running = false;
102
103 if (_smart_analyzer.ptr ()) {
104 _smart_analyzer->stop ();
105 _smart_analyzer->deinit ();
106 }
107
108 if (_processor_center.ptr ())
109 _processor_center->stop ();
110
111 XCAM_LOG_DEBUG ("pipe manager stopped");
112 return XCAM_RETURN_NO_ERROR;
113 }
114
115 XCamReturn
push_buffer(SmartPtr<VideoBuffer> & buf)116 PipeManager::push_buffer (SmartPtr<VideoBuffer> &buf)
117 {
118 // need to add sync mode later
119
120 if (_processor_center->put_buffer (buf) == false) {
121 XCAM_LOG_WARNING ("push buffer failed");
122 return XCAM_RETURN_ERROR_UNKNOWN;
123 }
124
125 return XCAM_RETURN_NO_ERROR;
126 }
127
128 XCamReturn
scaled_image_ready(const SmartPtr<VideoBuffer> & buffer)129 PipeManager::scaled_image_ready (const SmartPtr<VideoBuffer> &buffer)
130 {
131 XCamReturn ret = XCAM_RETURN_NO_ERROR;
132 if (!_smart_analyzer.ptr ()) {
133 return XCAM_RETURN_NO_ERROR;
134 }
135
136 ret = _smart_analyzer->push_buffer (buffer);
137 XCAM_FAIL_RETURN (
138 ERROR, ret == XCAM_RETURN_NO_ERROR,
139 ret, "push scaled buffer failed");
140
141 return XCAM_RETURN_NO_ERROR;
142 }
143
144 void
x3a_calculation_done(XAnalyzer * analyzer,X3aResultList & results)145 PipeManager::x3a_calculation_done (XAnalyzer *analyzer, X3aResultList &results)
146 {
147 XCamReturn ret = _processor_center->put_3a_results (results);
148 if (ret != XCAM_RETURN_NO_ERROR && ret != XCAM_RETURN_BYPASS) {
149 XCAM_LOG_WARNING ("apply 3a results failed");
150 return;
151 }
152 AnalyzerCallback::x3a_calculation_done (analyzer, results);
153 }
154
155 void
x3a_calculation_failed(XAnalyzer * analyzer,int64_t timestamp,const char * msg)156 PipeManager::x3a_calculation_failed (XAnalyzer *analyzer, int64_t timestamp, const char *msg)
157 {
158 AnalyzerCallback::x3a_calculation_failed (analyzer, timestamp, msg);
159 }
160
161 void
process_buffer_done(ImageProcessor * processor,const SmartPtr<VideoBuffer> & buf)162 PipeManager::process_buffer_done (ImageProcessor *processor, const SmartPtr<VideoBuffer> &buf)
163 {
164 ImageProcessCallback::process_buffer_done (processor, buf);
165 post_buffer (buf);
166 }
167
168 void
process_buffer_failed(ImageProcessor * processor,const SmartPtr<VideoBuffer> & buf)169 PipeManager::process_buffer_failed (ImageProcessor *processor, const SmartPtr<VideoBuffer> &buf)
170 {
171 ImageProcessCallback::process_buffer_failed (processor, buf);
172 }
173
174 void
process_image_result_done(ImageProcessor * processor,const SmartPtr<X3aResult> & result)175 PipeManager::process_image_result_done (ImageProcessor *processor, const SmartPtr<X3aResult> &result)
176 {
177 ImageProcessCallback::process_image_result_done (processor, result);
178 }
179
180 };
181