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