1 /* 2 * smart_analyzer_loader.cpp - smart 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: Zong Wei <wei.zong (at) intel.com> 19 */ 20 21 #include "smart_analyzer_loader.h" 22 #include "analyzer_loader.h" 23 #include "smart_analyzer.h" 24 #include "smart_analysis_handler.h" 25 #include <dirent.h> 26 27 namespace XCam { 28 29 #define MAX_PLUGIN_LIB_COUNT 10 30 31 SmartAnalyzerLoader::SmartAnalyzerLoader (const char *lib_path, const char *name, const char *symbol) 32 : AnalyzerLoader (lib_path, symbol) 33 , _name (NULL) 34 { 35 if (name) 36 _name = strndup (name, XCAM_MAX_STR_SIZE); 37 } 38 39 SmartAnalyzerLoader::~SmartAnalyzerLoader () 40 { 41 if (_name) 42 xcam_free (_name); 43 } 44 45 SmartHandlerList 46 47 SmartAnalyzerLoader::load_smart_handlers (const char *dir_path) 48 { 49 SmartHandlerList ret_handers; 50 AnalyzerLoaderList loaders = create_analyzer_loader (dir_path); 51 for (AnalyzerLoaderList::iterator i_loader = loaders.begin (); 52 i_loader != loaders.end (); ++i_loader) 53 { 54 SmartPtr<SmartAnalysisHandler> handler = (*i_loader)->load_smart_handler(*i_loader); 55 if (!handler.ptr ()) 56 continue; 57 58 SmartHandlerList::iterator i_pos = ret_handers.begin (); 59 for (; i_pos != ret_handers.end (); ++i_pos) 60 { 61 if (handler->get_priority() < (*i_pos)->get_priority ()) 62 break; 63 } 64 ret_handers.insert (i_pos, handler); 65 } 66 return ret_handers; 67 } 68 69 AnalyzerLoaderList 70 SmartAnalyzerLoader::create_analyzer_loader (const char *dir_path) 71 { 72 XCAM_ASSERT (dir_path); 73 74 char lib_path[512]; 75 DIR *lib_dir = NULL; 76 struct dirent *dirent_lib = NULL; 77 SmartPtr<SmartAnalyzerLoader> loader; 78 AnalyzerLoaderList loader_list; 79 uint8_t count = 0; 80 81 lib_dir = opendir (dir_path); 82 if (lib_dir) { 83 while ((count < MAX_PLUGIN_LIB_COUNT) && (dirent_lib = readdir (lib_dir)) != NULL) { 84 if (dirent_lib->d_type != DT_LNK && 85 dirent_lib->d_type != DT_REG) 86 continue; 87 snprintf (lib_path, sizeof(lib_path), "%s/%s", dir_path, dirent_lib->d_name); 88 loader = new SmartAnalyzerLoader (lib_path, dirent_lib->d_name); 89 if (loader.ptr ()) { 90 loader_list.push_back (loader); 91 } 92 } 93 } 94 if (lib_dir) 95 closedir (lib_dir); 96 return loader_list; 97 } 98 99 SmartPtr<SmartAnalysisHandler> 100 SmartAnalyzerLoader::load_smart_handler (SmartPtr<SmartAnalyzerLoader> &self) 101 { 102 XCAM_ASSERT (self.ptr () == this); 103 104 SmartPtr<SmartAnalysisHandler> handler; 105 XCamSmartAnalysisDescription *desc = (XCamSmartAnalysisDescription*)load_library (get_lib_path ()); 106 if (NULL == desc) { 107 XCAM_LOG_WARNING ("load smart handler lib symbol failed"); 108 return NULL; 109 } 110 111 handler = new SmartAnalysisHandler (desc, self, (desc->name ? desc->name : _name)); 112 if (!handler.ptr ()) { 113 XCAM_LOG_WARNING ("create smart handler failed"); 114 close_handle (); 115 return NULL; 116 } 117 118 XCAM_LOG_INFO ("smart handler(%s) created from lib", XCAM_STR (handler->get_name())); 119 return handler; 120 } 121 122 void * 123 SmartAnalyzerLoader::load_symbol (void* handle) 124 { 125 XCamSmartAnalysisDescription *desc = NULL; 126 127 desc = (XCamSmartAnalysisDescription *)AnalyzerLoader::get_symbol (handle); 128 if (!desc) { 129 XCAM_LOG_DEBUG ("get symbol failed from lib"); 130 return NULL; 131 } 132 if (desc->version < xcam_version ()) { 133 XCAM_LOG_WARNING ("get symbol version is:0x%04x, but expect:0x%04x", 134 desc->version, xcam_version ()); 135 } 136 if (desc->size < sizeof (XCamSmartAnalysisDescription)) { 137 XCAM_LOG_DEBUG ("get symbol failed, XCamSmartAnalysisDescription size is:%" PRIu32 ", but expect:%" PRIuS, 138 desc->size, sizeof (XCamSmartAnalysisDescription)); 139 return NULL; 140 } 141 142 if (!desc->create_context || !desc->destroy_context || 143 !desc->update_params || !desc->analyze || 144 !desc->free_results) { 145 XCAM_LOG_DEBUG ("some functions in symbol not set from lib"); 146 return NULL; 147 } 148 return (void*)desc; 149 } 150 151 }; 152