1 /*
2  * Copyright (c) 2021-2023 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 HDI_SESSION_H
17 #define HDI_SESSION_H
18 #include <unordered_map>
19 #include <vector>
20 #include <memory>
21 #include "common/include/display_common.h"
22 #include "display_log.h"
23 #include "hdi_device_interface.h"
24 #include "hdi_display.h"
25 #include "hdi_netlink_monitor.h"
26 #include "v1_0/display_composer_type.h"
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace DISPLAY {
31 class HdiSession {
32 public:
33     void Init();
34     static HdiSession &GetInstance();
35 
36     template<typename... Args>
CallDisplayFunction(uint32_t devId,int32_t (HdiDisplay::* member)(Args...),Args...args)37     int32_t CallDisplayFunction(uint32_t devId, int32_t (HdiDisplay::*member)(Args...), Args... args)
38     {
39         DISPLAY_LOGD("device Id : %{public}d", devId);
40         DISPLAY_CHK_RETURN((devId == INVALIDE_DISPLAY_ID), DISPLAY_FAILURE, DISPLAY_LOGE("invalide device id"));
41         auto iter = mHdiDisplays.find(devId);
42         DISPLAY_CHK_RETURN((iter == mHdiDisplays.end()), DISPLAY_FAILURE,
43             DISPLAY_LOGE("can not find display %{public}d", devId));
44         auto display = iter->second.get();
45         return (display->*member)(std::forward<Args>(args)...);
46     }
47 
48     template<typename... Args>
CallLayerFunction(uint32_t devId,uint32_t layerId,int32_t (HdiLayer::* member)(Args...),Args...args)49     int32_t CallLayerFunction(uint32_t devId, uint32_t layerId, int32_t (HdiLayer::*member)(Args...), Args... args)
50     {
51         DISPLAY_LOGD("device Id : %{public}d", devId);
52         DISPLAY_CHK_RETURN((devId == INVALIDE_DISPLAY_ID), DISPLAY_FAILURE, DISPLAY_LOGE("invalide device id"));
53         auto iter = mHdiDisplays.find(devId);
54         DISPLAY_CHK_RETURN((iter == mHdiDisplays.end()), DISPLAY_FAILURE,
55             DISPLAY_LOGE("can not find display %{public}d", devId));
56         auto display = iter->second.get();
57         auto layer = display->GetHdiLayer(layerId);
58         DISPLAY_CHK_RETURN((layer == nullptr), DISPLAY_FAILURE,
59             DISPLAY_LOGE("can not find the layer %{public}d", layerId));
60         return (layer->*member)(std::forward<Args>(args)...);
61     }
62 
63     int32_t RegHotPlugCallback(HotPlugCallback callback, void *data);
64     void DoHotPlugCallback(uint32_t devId, bool connect);
65     void HandleHotplug(bool plugIn);
66 
67 private:
68     std::shared_ptr<HdiNetLinkMonitor> mNetLinkMonitor;
69     std::unordered_map<uint32_t, std::shared_ptr<HdiDisplay>> mHdiDisplays;
70     std::vector<std::shared_ptr<HdiDeviceInterface>> mHdiDevices;
71     std::unordered_map<HotPlugCallback, void *> mHotPlugCallBacks;
72 };
73 } // namespace OHOS
74 } // namespace HDI
75 } // namespace DISPLAY
76 
77 #endif // HDI_SESSION_H
78