Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 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 | */ |
| 20 | |
| 21 | #include "analyzer_loader.h" |
| 22 | #include "handler_interface.h" |
| 23 | #include "x3a_result_factory.h" |
| 24 | #include "x3a_statistics_queue.h" |
| 25 | #include <dlfcn.h> |
| 26 | |
| 27 | namespace XCam { |
| 28 | |
| 29 | class DynamicAeHandler; |
| 30 | class DynamicAwbHandler; |
| 31 | class DynamicAfHandler; |
| 32 | class DynamicCommonHandler; |
| 33 | |
| 34 | class DynamicAnalyzer |
| 35 | : public X3aAnalyzer |
| 36 | { |
| 37 | public: |
| 38 | DynamicAnalyzer (XCam3ADescription *desc); |
| 39 | ~DynamicAnalyzer (); |
| 40 | |
| 41 | public: |
| 42 | XCamReturn analyze_ae (XCamAeParam ¶m); |
| 43 | XCamReturn analyze_awb (XCamAwbParam ¶m); |
| 44 | XCamReturn analyze_af (XCamAfParam ¶m); |
| 45 | |
| 46 | protected: |
| 47 | virtual SmartPtr<AeHandler> create_ae_handler (); |
| 48 | virtual SmartPtr<AwbHandler> create_awb_handler (); |
| 49 | virtual SmartPtr<AfHandler> create_af_handler (); |
| 50 | virtual SmartPtr<CommonHandler> create_common_handler (); |
| 51 | virtual XCamReturn internal_init (uint32_t width, uint32_t height, double framerate); |
| 52 | virtual XCamReturn internal_deinit (); |
| 53 | |
| 54 | virtual XCamReturn configure_3a (); |
| 55 | virtual XCamReturn pre_3a_analyze (SmartPtr<X3aIspStatistics> &stats); |
| 56 | virtual XCamReturn post_3a_analyze (X3aResultList &results); |
| 57 | |
| 58 | XCamReturn create_context (); |
| 59 | void destroy_context (); |
| 60 | |
| 61 | private: |
| 62 | XCamReturn convert_results (XCam3aResultHead *from, uint32_t from_count, X3aResultList &to); |
| 63 | XCAM_DEAD_COPY (DynamicAnalyzer); |
| 64 | |
| 65 | private: |
| 66 | XCam3ADescription *_desc; |
| 67 | XCam3AContext *_context; |
| 68 | SmartPtr<X3aIspStatistics> _cur_stats; |
| 69 | SmartPtr<DynamicCommonHandler> _common_handler; |
| 70 | }; |
| 71 | |
| 72 | class DynamicAeHandler |
| 73 | : public AeHandler |
| 74 | { |
| 75 | public: |
| 76 | explicit DynamicAeHandler (DynamicAnalyzer *analyzer) |
| 77 | : _analyzer (analyzer) |
| 78 | {} |
| 79 | virtual XCamReturn analyze (X3aResultList &output) { |
| 80 | XCAM_UNUSED (output); |
| 81 | AnalyzerHandler::HanlderLock lock(this); |
| 82 | XCamAeParam param = this->get_params_unlock (); |
| 83 | return _analyzer->analyze_ae (param); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | DynamicAnalyzer *_analyzer; |
| 88 | }; |
| 89 | |
| 90 | class DynamicAwbHandler |
| 91 | : public AwbHandler |
| 92 | { |
| 93 | public: |
| 94 | explicit DynamicAwbHandler (DynamicAnalyzer *analyzer) |
| 95 | : _analyzer (analyzer) |
| 96 | {} |
| 97 | virtual XCamReturn analyze (X3aResultList &output) { |
| 98 | XCAM_UNUSED (output); |
| 99 | AnalyzerHandler::HanlderLock lock(this); |
| 100 | XCamAwbParam param = this->get_params_unlock (); |
| 101 | return _analyzer->analyze_awb (param); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | DynamicAnalyzer *_analyzer; |
| 106 | }; |
| 107 | |
| 108 | class DynamicAfHandler |
| 109 | : public AfHandler |
| 110 | { |
| 111 | public: |
| 112 | explicit DynamicAfHandler (DynamicAnalyzer *analyzer) |
| 113 | : _analyzer (analyzer) |
| 114 | {} |
| 115 | virtual XCamReturn analyze (X3aResultList &output) { |
| 116 | XCAM_UNUSED (output); |
| 117 | AnalyzerHandler::HanlderLock lock(this); |
| 118 | XCamAfParam param = this->get_params_unlock (); |
| 119 | return _analyzer->analyze_af (param); |
| 120 | } |
| 121 | |
| 122 | private: |
| 123 | DynamicAnalyzer *_analyzer; |
| 124 | }; |
| 125 | |
| 126 | class DynamicCommonHandler |
| 127 | : public CommonHandler |
| 128 | { |
| 129 | friend class DynamicAnalyzer; |
| 130 | public: |
| 131 | explicit DynamicCommonHandler (DynamicAnalyzer *analyzer) |
| 132 | : _analyzer (analyzer) |
| 133 | {} |
| 134 | virtual XCamReturn analyze (X3aResultList &output) { |
| 135 | XCAM_UNUSED (output); |
| 136 | AnalyzerHandler::HanlderLock lock(this); |
| 137 | return XCAM_RETURN_NO_ERROR; |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | DynamicAnalyzer *_analyzer; |
| 142 | }; |
| 143 | |
| 144 | DynamicAnalyzer::DynamicAnalyzer (XCam3ADescription *desc) |
| 145 | : X3aAnalyzer ("DynamicAnalyzer") |
| 146 | , _desc (desc) |
| 147 | , _context (NULL) |
| 148 | { |
| 149 | } |
| 150 | |
| 151 | DynamicAnalyzer::~DynamicAnalyzer () |
| 152 | { |
| 153 | destroy_context (); |
| 154 | } |
| 155 | |
| 156 | XCamReturn |
| 157 | DynamicAnalyzer::create_context () |
| 158 | { |
| 159 | XCam3AContext *context = NULL; |
| 160 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 161 | XCAM_ASSERT (!_context); |
| 162 | if ((ret = _desc->create_context (&context)) != XCAM_RETURN_NO_ERROR) { |
| 163 | XCAM_LOG_WARNING ("dynamic 3a lib create context failed"); |
| 164 | return ret; |
| 165 | } |
| 166 | _context = context; |
| 167 | return XCAM_RETURN_NO_ERROR; |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | DynamicAnalyzer::destroy_context () |
| 172 | { |
| 173 | if (_context && _desc && _desc->destroy_context) { |
| 174 | _desc->destroy_context (_context); |
| 175 | _context = NULL; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | XCamReturn |
| 180 | DynamicAnalyzer::analyze_ae (XCamAeParam ¶m) |
| 181 | { |
| 182 | XCAM_ASSERT (_context); |
| 183 | return _desc->analyze_ae (_context, ¶m); |
| 184 | } |
| 185 | |
| 186 | XCamReturn |
| 187 | DynamicAnalyzer::analyze_awb (XCamAwbParam ¶m) |
| 188 | { |
| 189 | XCAM_ASSERT (_context); |
| 190 | return _desc->analyze_awb (_context, ¶m); |
| 191 | } |
| 192 | |
| 193 | XCamReturn |
| 194 | DynamicAnalyzer::analyze_af (XCamAfParam ¶m) |
| 195 | { |
| 196 | XCAM_ASSERT (_context); |
| 197 | return _desc->analyze_af (_context, ¶m); |
| 198 | } |
| 199 | |
| 200 | SmartPtr<AeHandler> |
| 201 | DynamicAnalyzer::create_ae_handler () |
| 202 | { |
| 203 | return new DynamicAeHandler (this); |
| 204 | } |
| 205 | |
| 206 | SmartPtr<AwbHandler> |
| 207 | DynamicAnalyzer::create_awb_handler () |
| 208 | { |
| 209 | return new DynamicAwbHandler (this); |
| 210 | } |
| 211 | |
| 212 | SmartPtr<AfHandler> |
| 213 | DynamicAnalyzer::create_af_handler () |
| 214 | { |
| 215 | return new DynamicAfHandler (this); |
| 216 | } |
| 217 | |
| 218 | SmartPtr<CommonHandler> |
| 219 | DynamicAnalyzer::create_common_handler () |
| 220 | { |
| 221 | if (_common_handler.ptr()) |
| 222 | return _common_handler; |
| 223 | |
| 224 | _common_handler = new DynamicCommonHandler (this); |
| 225 | return _common_handler; |
| 226 | } |
| 227 | |
| 228 | XCamReturn |
| 229 | DynamicAnalyzer::internal_init (uint32_t width, uint32_t height, double framerate) |
| 230 | { |
| 231 | XCAM_UNUSED (width); |
| 232 | XCAM_UNUSED (height); |
| 233 | XCAM_UNUSED (framerate); |
| 234 | return create_context (); |
| 235 | } |
| 236 | |
| 237 | XCamReturn |
| 238 | DynamicAnalyzer::internal_deinit () |
| 239 | { |
| 240 | destroy_context (); |
| 241 | return XCAM_RETURN_NO_ERROR; |
| 242 | } |
| 243 | |
| 244 | XCamReturn |
| 245 | DynamicAnalyzer::configure_3a () |
| 246 | { |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 247 | uint32_t width = get_width (); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 248 | uint32_t height = get_height (); |
| 249 | double framerate = get_framerate (); |
| 250 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 251 | |
| 252 | XCAM_ASSERT (_context); |
| 253 | |
| 254 | XCAM_FAIL_RETURN (WARNING, |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 255 | ret = _desc->configure_3a (_context, width, height, framerate), |
| 256 | ret, |
| 257 | "dynamic analyzer configure 3a failed"); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 258 | |
| 259 | return XCAM_RETURN_NO_ERROR; |
| 260 | } |
| 261 | XCamReturn |
| 262 | DynamicAnalyzer::pre_3a_analyze (SmartPtr<X3aIspStatistics> &stats) |
| 263 | { |
| 264 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 265 | XCamCommonParam common_params = _common_handler->get_params_unlock (); |
| 266 | |
| 267 | XCAM_ASSERT (_context); |
| 268 | _cur_stats = stats; |
| 269 | ret = _desc->set_3a_stats (_context, stats->get_3a_stats()); |
| 270 | XCAM_FAIL_RETURN (WARNING, |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 271 | ret == XCAM_RETURN_NO_ERROR, |
| 272 | ret, |
| 273 | "dynamic analyzer set_3a_stats failed"); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 274 | |
| 275 | ret = _desc->update_common_params (_context, &common_params); |
| 276 | XCAM_FAIL_RETURN (WARNING, |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 277 | ret == XCAM_RETURN_NO_ERROR, |
| 278 | ret, |
| 279 | "dynamic analyzer update common params failed"); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 280 | |
| 281 | return XCAM_RETURN_NO_ERROR; |
| 282 | } |
| 283 | |
| 284 | XCamReturn |
| 285 | DynamicAnalyzer::post_3a_analyze (X3aResultList &results) |
| 286 | { |
| 287 | XCamReturn ret = XCAM_RETURN_NO_ERROR; |
| 288 | XCam3aResultHead *res_array = NULL; |
| 289 | uint32_t res_count = 0; |
| 290 | |
| 291 | XCAM_ASSERT (_context); |
| 292 | ret = _desc->combine_analyze_results (_context, &res_array, &res_count); |
| 293 | XCAM_FAIL_RETURN (WARNING, |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 294 | ret == XCAM_RETURN_NO_ERROR, |
| 295 | ret, |
| 296 | "dynamic analyzer combine_analyze_results failed"); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 297 | |
| 298 | _cur_stats.release (); |
| 299 | |
| 300 | if (res_count) { |
| 301 | ret = convert_results (res_array, res_count, results); |
| 302 | XCAM_FAIL_RETURN (WARNING, |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 303 | ret == XCAM_RETURN_NO_ERROR, |
| 304 | ret, |
| 305 | "dynamic analyzer convert_results failed"); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 306 | _desc->free_results (res_array, res_count); |
| 307 | } |
| 308 | |
| 309 | return XCAM_RETURN_NO_ERROR; |
| 310 | } |
| 311 | |
| 312 | XCamReturn |
| 313 | DynamicAnalyzer::convert_results (XCam3aResultHead *from, uint32_t from_count, X3aResultList &to) |
| 314 | { |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 315 | for (uint32_t i = 0; i < from_count; ++i) { |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 316 | SmartPtr<X3aResult> standard_res = |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 317 | X3aResultFactory::instance ()->create_3a_result (&from[i]); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 318 | to.push_back (standard_res); |
| 319 | } |
| 320 | |
| 321 | return XCAM_RETURN_NO_ERROR; |
| 322 | } |
| 323 | |
| 324 | AnalyzerLoader::AnalyzerLoader (const char *lib_path) |
| 325 | : _path (NULL) |
| 326 | , _handle (NULL) |
| 327 | { |
| 328 | XCAM_ASSERT (lib_path); |
| 329 | _path = strdup (lib_path); |
| 330 | } |
| 331 | |
| 332 | AnalyzerLoader::~AnalyzerLoader () |
| 333 | { |
| 334 | close_handle (); |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 335 | if (_path) |
| 336 | xcam_free (_path); |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | SmartPtr<X3aAnalyzer> |
| 340 | AnalyzerLoader::load_analyzer () |
| 341 | { |
| 342 | SmartPtr<X3aAnalyzer> analyzer; |
| 343 | XCam3ADescription *desc = NULL; |
| 344 | const char *symbol = XCAM_3A_LIB_DESCRIPTION; |
| 345 | if (!open_handle ()) { |
| 346 | XCAM_LOG_WARNING ("open dynamic lib:%s failed", XCAM_STR (_path)); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | desc = get_symbol (symbol); |
| 351 | if (!desc) { |
| 352 | XCAM_LOG_WARNING ("get symbol(%s) from lib:%s failed", symbol, XCAM_STR (_path)); |
| 353 | close_handle (); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | XCAM_LOG_DEBUG ("got symbols(%s) from lib(%s)", symbol, XCAM_STR (_path)); |
| 358 | |
| 359 | analyzer = new DynamicAnalyzer (desc); |
| 360 | if (!analyzer.ptr ()) { |
| 361 | XCAM_LOG_WARNING ("get symbol(%s) from lib:%s failed", symbol, XCAM_STR (_path)); |
| 362 | close_handle (); |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | XCAM_LOG_INFO ("analyzer(%s) created from 3a lib(%s)", XCAM_STR (analyzer->get_name()), XCAM_STR (_path)); |
| 367 | return analyzer; |
| 368 | } |
| 369 | |
| 370 | bool |
| 371 | AnalyzerLoader::open_handle () |
| 372 | { |
| 373 | void *handle = NULL; |
| 374 | |
| 375 | if (_handle != NULL) |
| 376 | return true; |
| 377 | |
| 378 | handle = dlopen (_path, RTLD_LAZY); |
| 379 | if (!handle) { |
| 380 | XCAM_LOG_DEBUG ( |
| 381 | "open user-defined 3a lib(%s) failed, reason:%s", |
| 382 | XCAM_STR (_path), dlerror ()); |
| 383 | return false; |
| 384 | } |
| 385 | _handle = handle; |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | XCam3ADescription * |
| 390 | AnalyzerLoader::get_symbol (const char *symbol) |
| 391 | { |
| 392 | XCam3ADescription *desc = NULL; |
| 393 | |
| 394 | XCAM_ASSERT (_handle); |
| 395 | XCAM_ASSERT (symbol); |
| 396 | desc = (XCam3ADescription *)dlsym (_handle, symbol); |
| 397 | if (!desc) { |
| 398 | XCAM_LOG_DEBUG ("get symbol(%s) failed from lib(%s), reason:%s", symbol, XCAM_STR (_path), dlerror ()); |
| 399 | return NULL; |
| 400 | } |
| 401 | if (desc->version < XCAM_VERSION) { |
| 402 | XCAM_LOG_DEBUG ("get symbol(%s) failed. version is:0x%04x, but expect:0x%04x", |
| 403 | symbol, desc->version, XCAM_VERSION); |
| 404 | return NULL; |
| 405 | } |
| 406 | if (desc->size < sizeof (XCam3ADescription)) { |
| 407 | XCAM_LOG_DEBUG ("get symbol(%s) failed, XCam3ADescription size is:%d, but expect:%d", |
| 408 | symbol, desc->size, sizeof (XCam3ADescription)); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | if (!desc->create_context || !desc->destroy_context || |
Wind Yuan | 8a0d296 | 2015-01-27 10:44:22 +0800 | [diff] [blame] | 413 | !desc->configure_3a || !desc->set_3a_stats || |
| 414 | !desc->analyze_awb || !desc->analyze_ae || |
| 415 | !desc->analyze_af || !desc->combine_analyze_results || |
| 416 | !desc->free_results) { |
Wind Yuan | 78ec1f7 | 2015-01-26 16:25:59 +0800 | [diff] [blame] | 417 | XCAM_LOG_DEBUG ("some functions in symbol(%s) not set from lib(%s)", symbol, XCAM_STR (_path)); |
| 418 | return NULL; |
| 419 | } |
| 420 | return desc; |
| 421 | } |
| 422 | |
| 423 | bool |
| 424 | AnalyzerLoader::close_handle () |
| 425 | { |
| 426 | if (!_handle) |
| 427 | return true; |
| 428 | dlclose (_handle); |
| 429 | _handle = NULL; |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | }; |