1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef IC_PLUGIN_H
17 #define IC_PLUGIN_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <vector>
24 
25 #include "ai_datatype.h"
26 #include "engine_adapter.h"
27 #include "plugin_helper.h"
28 #include "plugin/i_plugin.h"
29 
30 namespace OHOS {
31 namespace AI {
32 struct ICPluginConfig : PluginConfig {
33     size_t maxOutputSize;
34 };
35 
36 class ICPlugin : public IPlugin {
37 public:
38     ICPlugin();
39     virtual ~ICPlugin();
40 
41     const long long GetVersion() const override;
42     const char* GetName() const override;
43     const char* GetInferMode() const override;
44     int32_t Prepare(long long transactionId, const DataInfo &inputInfo, DataInfo &outputInfo) override;
45     int32_t SyncProcess(IRequest *request, IResponse *&response) override;
46     int32_t AsyncProcess(IRequest *request, IPluginCallback *callback) override;
47     int32_t Release(bool isFullUnload, long long transactionId, const DataInfo &inputInfo) override;
48     int32_t SetOption(int32_t optionType, const DataInfo &inputInfo) override;
49     int32_t GetOption(int32_t optionType, const DataInfo &inputInfo, DataInfo &outputInfo) override;
50 
51 private:
52     int32_t BuildConfig(intptr_t handle, ICPluginConfig &config);
53     int32_t DoProcess(intptr_t handle, const ICPluginConfig &config, IRequest *request, IResponse *&response);
54     int32_t MakeInference(intptr_t handle, const ICPluginConfig &config, DataInfo &outputInfo);
55     void ReleaseAllHandles();
56 
57 private:
58     std::mutex mutex_;
59     std::shared_ptr<EngineAdapter> adapter_;
60     std::map<intptr_t, ICPluginConfig> handles_;
61 };
62 } // namespace AI
63 } // namespace OHOS
64 #endif // IC_PLUGIN_H