xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/aiq_core/RkAiqHandle.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * rkisp_aiq_core.h
3  *
4  *  Copyright (c) 2019 Rockchip 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  */
19 
20 #ifndef _RK_AIQ_HANDLE_H_
21 #define _RK_AIQ_HANDLE_H_
22 
23 #include <map>
24 
25 #include "rk_aiq_algo_types.h"
26 #include "rk_aiq_types.h"
27 #include "xcam_mutex.h"
28 #include "rk_aiq_pool.h"
29 
30 namespace RkCam {
31 
32 /*
33  --------------------------------
34 |         :RkAiqHandle           |
35  --------------------------------
36 |  rk_aiq_xxx_attrib_t mCurAtt;  |
37 |  rk_aiq_xxx_attrib_t mNewAtt;  |
38 |        Mutex mCfgMutex;        |
39 |        bool updateAtt;         |
40  --------------------------------
41 |         updateConfig()         |
42  --------------------------------
43 */
44 
45 #define DISABLE_HANDLE_ATTRIB
46 
47 class RkAiqCore;
48 struct RkAiqAlgosGroupShared_s;
49 
50 class RkAiqHandle {
51  public:
52     explicit RkAiqHandle(RkAiqAlgoDesComm* des, RkAiqCore* aiqCore);
53     virtual ~RkAiqHandle();
setEnable(bool enable)54     void setEnable(bool enable) { mEnable = enable; };
setReConfig(bool reconfig)55     void setReConfig(bool reconfig) { mReConfig = reconfig; };
getEnable()56     bool getEnable() { return mEnable; };
57     virtual XCamReturn prepare();
58     virtual XCamReturn preProcess();
59     virtual XCamReturn processing();
60     virtual XCamReturn postProcess();
genIspResult(RkAiqFullParams * params,RkAiqFullParams * cur_params)61     virtual XCamReturn genIspResult(RkAiqFullParams* params, RkAiqFullParams* cur_params) { return XCAM_RETURN_NO_ERROR; };
getAlgoCtx()62     RkAiqAlgoContext* getAlgoCtx() { return mAlgoCtx; }
getAlgoId()63     int getAlgoId() const { return mDes->id; }
getAlgoType()64     int getAlgoType() const { return mDes->type; }
setGroupId(int32_t gId)65     void setGroupId(int32_t gId) {
66         mGroupId = gId;
67     }
getGroupId()68     int32_t getGroupId() {
69        return mGroupId;
70     }
71 
setNextHdl(RkAiqHandle * next)72     void setNextHdl(RkAiqHandle* next) {
73         mNextHdl = next;
74     }
75 
setParentHdl(RkAiqHandle * parent)76     void setParentHdl(RkAiqHandle* parent) {
77         mParentHdl = parent;
78     }
79 
getNextHdl()80     RkAiqHandle* getNextHdl() {
81        return mNextHdl;
82     }
83 
getParent()84     RkAiqHandle* getParent() {
85        return mParentHdl;
86     }
87 
88     // rk algo running with custom algo concunrrently
setMulRun(bool isMulRun)89     void setMulRun(bool isMulRun) {
90         mIsMulRun = isMulRun;
91         if (isMulRun && mDes->id == 0)
92             mPostShared = false;
93         else
94             mPostShared = true;
95     }
96 
setGroupShared(void * grp_shared)97     void setGroupShared(void* grp_shared) {
98         mAlogsGroupSharedParams = grp_shared;
99     }
getGroupShared()100     void* getGroupShared() {
101        return mAlogsGroupSharedParams;
102     }
updateConfig(bool needSync)103     virtual XCamReturn updateConfig(bool needSync) { return XCAM_RETURN_NO_ERROR; };
getPreProcRes()104     virtual RkAiqAlgoResCom* getPreProcRes() {
105         return mPreOutParam;
106     }
getProcProcRes()107     virtual RkAiqAlgoResCom* getProcProcRes() {
108         return mProcOutParam;
109     }
110  protected:
111     virtual void init() = 0;
112     virtual void deInit();
113     void waitSignal(rk_aiq_uapi_mode_sync_e sync = RK_AIQ_UAPI_MODE_DEFAULT);
114     void sendSignal(rk_aiq_uapi_mode_sync_e sync = RK_AIQ_UAPI_MODE_DEFAULT);
115     enum {
116         RKAIQ_CONFIG_COM_PREPARE,
117         RKAIQ_CONFIG_COM_PRE,
118         RKAIQ_CONFIG_COM_PROC,
119         RKAIQ_CONFIG_COM_POST,
120     };
121     virtual XCamReturn configInparamsCom(RkAiqAlgoCom* com, int type);
grpId2GrpMask(uint32_t grpId)122     inline uint64_t grpId2GrpMask(uint32_t grpId) {
123         return grpId == RK_AIQ_CORE_ANALYZE_ALL ? (uint64_t)grpId : (1ULL << grpId);
124     }
125     RkAiqAlgoCom* mConfig;
126     RkAiqAlgoCom* mPreInParam;
127     RkAiqAlgoResCom* mPreOutParam;
128     RkAiqAlgoCom* mProcInParam;
129     RkAiqAlgoResCom* mProcOutParam;
130     RkAiqAlgoCom* mPostInParam;
131     RkAiqAlgoResCom* mPostOutParam;
132     const RkAiqAlgoDesComm* mDes;
133     RkAiqAlgoContext* mAlgoCtx;
134     RkAiqCore* mAiqCore;
135     bool mEnable;
136     bool mReConfig;
137     uint32_t mGroupId;
138     void* mAlogsGroupSharedParams;
139     XCam::Mutex mCfgMutex;
140     mutable std::atomic<bool> updateAtt;
141     XCam::Cond mUpdateCond;
142     RkAiqHandle* mNextHdl;
143     RkAiqHandle* mParentHdl;
144     bool mIsMulRun;
145     bool mPostShared;
146     uint32_t mSyncFlag{(uint32_t)(-1)};
147 };
148 
149 template <typename T>
createT(RkAiqAlgoDesComm * des,RkAiqCore * aiqCore)150 RkAiqHandle* createT(RkAiqAlgoDesComm* des, RkAiqCore* aiqCore) {
151     return new T(des, aiqCore);
152 }
153 
154 struct RkAiqHandleFactory {
155     typedef std::map<std::string, RkAiqHandle* (*)(RkAiqAlgoDesComm* des, RkAiqCore* aiqCore)>
156         map_type;
157 
~RkAiqHandleFactoryRkAiqHandleFactory158     ~RkAiqHandleFactory() {
159         if (map != nullptr) {
160             if (map->empty()) {
161                 delete map;
162             }
163         }
164     }
165 
createInstanceRkAiqHandleFactory166     static RkAiqHandle* createInstance(std::string const& s, RkAiqAlgoDesComm* des,
167                                        RkAiqCore* aiqCore) {
168         map_type::iterator it = getMap()->find(s);
169         if (it == getMap()->end()) return 0;
170         return it->second(des, aiqCore);
171     }
172 
173  protected:
getMapRkAiqHandleFactory174     static map_type* getMap() {
175         // never delete'ed. (exist until program termination)
176         // because we can't guarantee correct destruction order
177         if (!map) {
178             map = new map_type;
179         }
180         return map;
181     }
182 
183  private:
184     static map_type* map;
185 };
186 
187 template <typename T>
188 struct RkAiqHandleRegister : RkAiqHandleFactory {
RkAiqHandleRegisterRkAiqHandleRegister189     RkAiqHandleRegister(std::string const& s) : s_(s) { getMap()->insert(std::make_pair(s, &createT<T>)); }
~RkAiqHandleRegisterRkAiqHandleRegister190     ~RkAiqHandleRegister() { getMap()->erase(s_); }
191 private:
192     const std::string s_;
193 };
194 
195 #define DECLARE_HANDLE_REGISTER_TYPE(NAME) static RkAiqHandleRegister<NAME> reg
196 
197 #define DEFINE_HANDLE_REGISTER_TYPE(NAME) RkAiqHandleRegister<NAME> NAME::reg(#NAME)
198 
199 }  // namespace RkCam
200 
201 #endif
202