1 /* 2 * Copyright (c) 2019-2021 Rockchip Eletronics Co., Ltd. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _RK_AIQ_ANALYZE_GROUP_MANAGER_ 18 #define _RK_AIQ_ANALYZE_GROUP_MANAGER_ 19 20 #include <stdint.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 24 #include <functional> 25 #include <map> 26 #include <array> 27 28 #include "MessageBus.h" 29 #include "RkAiqCoreConfig.h" 30 #include "RkAiqCore.h" 31 #include "rk_aiq_comm.h" 32 #include "rk_aiq_pool.h" 33 #include "video_buffer.h" 34 #include "xcam_std.h" 35 #include "xcam_thread.h" 36 #include "safe_list_ex.h" 37 38 // using namespace XCam; 39 namespace RkCam { 40 41 class RkAiqAnalyzeGroupManager; 42 class RkAiqCore; 43 class RkAiqAnalyzeGroupMsgHdlThread; 44 45 #define MAX_MESSAGES 5 46 // TODO(Cody): This is just workaround for current implementation 47 //using MessageHandleWrapper = std::function<XCamReturn(const std::list<SmartPtr<XCamMessage>>&)>; 48 typedef std::function<XCamReturn(std::array<RkAiqCoreVdBufMsg, MAX_MESSAGES>&, int, uint32_t, uint64_t)> 49 MessageHandleWrapper; 50 51 class RkAiqAnalyzerGroup { 52 public: 53 struct GroupMessage { 54 std::array<RkAiqCoreVdBufMsg, MAX_MESSAGES> msgList; 55 uint64_t msg_flags; 56 int msg_cnts; 57 }; 58 59 RkAiqAnalyzerGroup(RkAiqCore* aiqCore, enum rk_aiq_core_analyze_type_e type, 60 const uint64_t flag, const RkAiqGrpConditions_t* grpConds, 61 const bool singleThrd); 62 virtual ~RkAiqAnalyzerGroup() = default; 63 setConcreteHandler(const MessageHandleWrapper handler)64 void setConcreteHandler(const MessageHandleWrapper handler) { mHandler = handler; } 65 XCamReturn start(); 66 bool pushMsg(RkAiqCoreVdBufMsg& msg); 67 bool msgHandle(RkAiqCoreVdBufMsg* msg); 68 XCamReturn stop(); 69 getType()70 rk_aiq_core_analyze_type_e getType() const { return mGroupType; } getDepsFlag()71 uint64_t getDepsFlag() const { return mDepsFlag; } setDepsFlag(uint64_t new_deps)72 void setDepsFlag(uint64_t new_deps) { mDepsFlag = new_deps; } 73 void setDepsFlagAndClearMap(uint64_t new_deps); 74 getAiqCore()75 RkAiqCore* getAiqCore() { return mAiqCore; } 76 void setDelayCnts(int8_t delayCnts); 77 void setVicapScaleFlag(bool mode); 78 private: 79 void msgReduction(std::map<uint32_t, GroupMessage>& msgMap); 80 int8_t getMsgDelayCnt(XCamMessageType &msg_id); 81 82 // TODO(Cody): use weak ptr 83 RkAiqCore* mAiqCore; 84 const rk_aiq_core_analyze_type_e mGroupType; 85 uint64_t mDepsFlag; 86 RkAiqGrpConditions_t mGrpConds; 87 SmartPtr<RkAiqAnalyzeGroupMsgHdlThread> mRkAiqGroupMsgHdlTh; 88 std::map<uint32_t, GroupMessage> mGroupMsgMap; 89 MessageHandleWrapper mHandler; 90 int8_t mUserSetDelayCnts; 91 bool mVicapScaleStart{false}; 92 }; 93 94 class RkAiqAnalyzeGroupMsgHdlThread : public Thread { 95 public: RkAiqAnalyzeGroupMsgHdlThread(const std::string name,RkAiqAnalyzerGroup * group)96 RkAiqAnalyzeGroupMsgHdlThread(const std::string name, RkAiqAnalyzerGroup* group) 97 : Thread(name.c_str()) { 98 if (group != nullptr) mHandlerGroups.push_back(group); 99 }; ~RkAiqAnalyzeGroupMsgHdlThread()100 ~RkAiqAnalyzeGroupMsgHdlThread() { mMsgsQueue.clear(); }; 101 add_group(RkAiqAnalyzerGroup * group)102 void add_group(RkAiqAnalyzerGroup* group) { 103 mHandlerGroups.push_back(group); 104 } 105 triger_stop()106 void triger_stop() { mMsgsQueue.pause_pop(); }; 107 triger_start()108 void triger_start() { 109 mMsgsQueue.clear(); 110 mMsgsQueue.resume_pop(); 111 }; 112 push_msg(RkAiqCoreVdBufMsg & buffer)113 bool push_msg(RkAiqCoreVdBufMsg& buffer) { 114 mMsgsQueue.push(buffer); 115 return true; 116 }; 117 118 protected: 119 // virtual bool started (); stopped()120 virtual void stopped() { mMsgsQueue.clear(); }; 121 122 virtual bool loop(); 123 124 private: 125 XCamReturn handleCalibUpdate(RkAiqAnalyzerGroup* grp); 126 127 private: 128 std::vector<RkAiqAnalyzerGroup*> mHandlerGroups; 129 SafeListEx<RkAiqCoreVdBufMsg> mMsgsQueue; 130 }; 131 132 class RkAiqAnalyzeGroupManager { 133 public: 134 RkAiqAnalyzeGroupManager(RkAiqCore* aiqCore, bool single_thread); ~RkAiqAnalyzeGroupManager()135 virtual ~RkAiqAnalyzeGroupManager(){}; 136 137 void parseAlgoGroup(const struct RkAiqAlgoDesCommExt* algoDes); 138 139 uint64_t getGrpDeps(rk_aiq_core_analyze_type_e group); 140 XCamReturn setGrpDeps(rk_aiq_core_analyze_type_e group, uint64_t new_deps); 141 XCamReturn start(); 142 XCamReturn stop(); 143 void setDelayCnts(int delayCnts); 144 145 XCamReturn firstAnalyze(); 146 XCamReturn handleMessage(RkAiqCoreVdBufMsg& msg); getGroupAlgoList(rk_aiq_core_analyze_type_e group)147 std::vector<SmartPtr<RkAiqHandle>>& getGroupAlgoList(rk_aiq_core_analyze_type_e group) { 148 return mGroupAlgoListMap[group]; 149 } 150 getGroupAlgoListMap()151 std::map<uint64_t, std::vector<SmartPtr<RkAiqHandle>>> getGroupAlgoListMap() { 152 return mGroupAlgoListMap; 153 } 154 getGroups()155 std::map<uint64_t, SmartPtr<RkAiqAnalyzerGroup>>& getGroups() { 156 return mGroupMap; 157 } 158 159 protected: 160 XCamReturn groupMessageHandler(std::array<RkAiqCoreVdBufMsg, MAX_MESSAGES>& msgs, int msg_cnts, uint32_t id, 161 uint64_t grpId); 162 #if defined(RKAIQ_HAVE_THUMBNAILS) 163 XCamReturn thumbnailsGroupMessageHandler(std::array<RkAiqCoreVdBufMsg, MAX_MESSAGES>& msgs, int msg_cnts, uint32_t id, 164 uint64_t grpId); 165 #endif 166 private: 167 RkAiqCore* mAiqCore; 168 const bool mSingleThreadMode; 169 std::map<uint64_t, SmartPtr<RkAiqAnalyzerGroup>> mGroupMap; 170 std::map<uint64_t, std::vector<SmartPtr<RkAiqHandle>>> mGroupAlgoListMap; 171 SmartPtr<RkAiqAnalyzeGroupMsgHdlThread> mMsgThrd; 172 }; 173 174 } // namespace RkCam 175 176 #endif // _RK_AIQ_ANALYZE_GROUP_MANAGER_ 177 178