1/*
2 * analyzer_loader.cpp - analyzer loader
3 *
4 *  Copyright (c) 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 *         Jia Meng <jia.meng@intel.com>
20 */
21
22#include "dynamic_analyzer.h"
23
24namespace XCam {
25
26DynamicAnalyzer::DynamicAnalyzer (XCam3ADescription *desc, SmartPtr<AnalyzerLoader> &loader, const char *name)
27    : X3aAnalyzer (name)
28    , _desc (desc)
29    , _context (NULL)
30    , _loader (loader)
31{
32}
33
34DynamicAnalyzer::~DynamicAnalyzer ()
35{
36    destroy_context ();
37}
38
39XCamReturn
40DynamicAnalyzer::create_context ()
41{
42    XCam3AContext *context = NULL;
43    XCamReturn ret = XCAM_RETURN_NO_ERROR;
44    XCAM_ASSERT (!_context);
45    if ((ret = _desc->create_context (&context)) != XCAM_RETURN_NO_ERROR) {
46        XCAM_LOG_WARNING ("dynamic 3a lib create context failed");
47        return ret;
48    }
49    _context = context;
50    return XCAM_RETURN_NO_ERROR;
51}
52
53void
54DynamicAnalyzer::destroy_context ()
55{
56    if (_context && _desc && _desc->destroy_context) {
57        _desc->destroy_context (_context);
58        _context = NULL;
59    }
60}
61
62XCamReturn
63DynamicAnalyzer::analyze_ae (XCamAeParam &param)
64{
65    XCAM_ASSERT (_context);
66    return _desc->analyze_ae (_context, &param);
67}
68
69XCamReturn
70DynamicAnalyzer::analyze_awb (XCamAwbParam &param)
71{
72    XCAM_ASSERT (_context);
73    return _desc->analyze_awb (_context, &param);
74}
75
76XCamReturn
77DynamicAnalyzer::analyze_af (XCamAfParam &param)
78{
79    XCAM_ASSERT (_context);
80    return _desc->analyze_af (_context, &param);
81}
82
83SmartPtr<AeHandler>
84DynamicAnalyzer::create_ae_handler ()
85{
86    return new DynamicAeHandler (this);
87}
88
89SmartPtr<AwbHandler>
90DynamicAnalyzer::create_awb_handler ()
91{
92    return new DynamicAwbHandler (this);
93}
94
95SmartPtr<AfHandler>
96DynamicAnalyzer::create_af_handler ()
97{
98    return new DynamicAfHandler (this);
99}
100
101SmartPtr<CommonHandler>
102DynamicAnalyzer::create_common_handler ()
103{
104    if (_common_handler.ptr())
105        return _common_handler;
106
107    _common_handler = new DynamicCommonHandler (this);
108    return _common_handler;
109}
110
111XCamReturn
112DynamicAnalyzer::internal_init (uint32_t width, uint32_t height, double framerate)
113{
114    XCAM_UNUSED (width);
115    XCAM_UNUSED (height);
116    XCAM_UNUSED (framerate);
117    return create_context ();
118}
119
120XCamReturn
121DynamicAnalyzer::internal_deinit ()
122{
123    destroy_context ();
124    return XCAM_RETURN_NO_ERROR;
125}
126
127XCamReturn
128DynamicAnalyzer::configure_3a ()
129{
130    uint32_t width = get_width ();
131    uint32_t height = get_height ();
132    double framerate = get_framerate ();
133    XCamReturn ret = XCAM_RETURN_NO_ERROR;
134
135    XCAM_ASSERT (_context);
136
137    ret = _desc->configure_3a (_context, width, height, framerate);
138    XCAM_FAIL_RETURN (WARNING,
139                      ret == XCAM_RETURN_NO_ERROR,
140                      ret,
141                      "dynamic analyzer configure 3a failed");
142    set_manual_brightness(_brightness_level_param);
143
144    return XCAM_RETURN_NO_ERROR;
145}
146XCamReturn
147DynamicAnalyzer::pre_3a_analyze (SmartPtr<X3aStats> &stats)
148{
149    XCamReturn ret = XCAM_RETURN_NO_ERROR;
150    XCamCommonParam common_params = _common_handler->get_params_unlock ();
151
152    XCAM_ASSERT (_context);
153    _cur_stats = stats;
154    ret = _desc->set_3a_stats (_context, stats->get_stats (), stats->get_timestamp ());
155    XCAM_FAIL_RETURN (WARNING,
156                      ret == XCAM_RETURN_NO_ERROR,
157                      ret,
158                      "dynamic analyzer set_3a_stats failed");
159
160    ret = _desc->update_common_params (_context, &common_params);
161    XCAM_FAIL_RETURN (WARNING,
162                      ret == XCAM_RETURN_NO_ERROR,
163                      ret,
164                      "dynamic analyzer update common params failed");
165
166    return XCAM_RETURN_NO_ERROR;
167}
168
169XCamReturn
170DynamicAnalyzer::post_3a_analyze (X3aResultList &results)
171{
172    XCamReturn ret = XCAM_RETURN_NO_ERROR;
173    XCam3aResultHead *res_array[XCAM_3A_MAX_RESULT_COUNT];
174    uint32_t res_count = XCAM_3A_MAX_RESULT_COUNT;
175
176    xcam_mem_clear (res_array);
177    XCAM_ASSERT (_context);
178    ret = _desc->combine_analyze_results (_context, res_array, &res_count);
179    XCAM_FAIL_RETURN (WARNING,
180                      ret == XCAM_RETURN_NO_ERROR,
181                      ret,
182                      "dynamic analyzer combine_analyze_results failed");
183
184    _cur_stats.release ();
185
186    if (res_count) {
187        ret = convert_results (res_array, res_count, results);
188        XCAM_FAIL_RETURN (WARNING,
189                          ret == XCAM_RETURN_NO_ERROR,
190                          ret,
191                          "dynamic analyzer convert_results failed");
192        _desc->free_results (res_array, res_count);
193    }
194
195    return XCAM_RETURN_NO_ERROR;
196}
197
198const XCamCommonParam
199DynamicAnalyzer::get_common_params ()
200{
201    return _common_handler->get_params_unlock ();
202}
203
204XCamReturn
205DynamicAnalyzer::convert_results (XCam3aResultHead *from[], uint32_t from_count, X3aResultList &to)
206{
207    for (uint32_t i = 0; i < from_count; ++i) {
208        SmartPtr<X3aResult> standard_res =
209            X3aResultFactory::instance ()->create_3a_result (from[i]);
210        to.push_back (standard_res);
211    }
212
213    return XCAM_RETURN_NO_ERROR;
214}
215}
216