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_BACKEND_HDI_BACKEND_H
17 #define HDI_BACKEND_HDI_BACKEND_H
18 
19 #include <functional>
20 #include <unordered_map>
21 #include <refbase.h>
22 
23 #include "hdi_device.h"
24 #include "hdi_screen.h"
25 #include "hdi_layer.h"
26 #include "hdi_output.h"
27 #include "graphic_error.h"
28 #include "surface_type.h"
29 
30 namespace OHOS {
31 namespace Rosen {
32 
33 using OutputPtr = std::shared_ptr<HdiOutput>;
34 
35 struct PrepareCompleteParam {
36     bool needFlushFramebuffer;
37     std::vector<LayerInfoPtr> layers;
38     uint32_t screenId;
39 };
40 
41 using OnScreenHotplugFunc = std::function<void(OutputPtr &output, bool connected, void* data)>;
42 using OnScreenRefreshFunc = std::function<void(uint32_t deviceId, void* data)>;
43 using OnPrepareCompleteFunc = std::function<void(sptr<Surface>& surface,
44                                                  const struct PrepareCompleteParam &param, void* data)>;
45 
46 class HdiBackend {
47 public:
48     /* for RS begin */
49     static HdiBackend* GetInstance();
50     RosenError RegScreenHotplug(OnScreenHotplugFunc func, void* data);
51     RosenError RegScreenRefresh(OnScreenRefreshFunc func, void* data);
52     RosenError RegPrepareComplete(OnPrepareCompleteFunc func, void* data);
53     RosenError RegHwcDeadListener(OnHwcDeadCallback func, void* data);
54     RosenError RegScreenVBlankIdleCallback(OnVBlankIdleCallback func, void* data);
55     void Repaint(const OutputPtr &output);
56     void ResetDevice();
57     /* for RS end */
58 
59     /* only used for mock tests */
60     RosenError SetHdiBackendDevice(HdiDevice* device);
61     void StartSample(const OutputPtr &output);
62     /* set a temporary period used only for VSyncSampler::GetHardwarePeriod interface */
63     void SetPendingMode(const OutputPtr &output, int64_t period, int64_t timestamp);
64 private:
65     HdiBackend() = default;
66     virtual ~HdiBackend() = default;
67 
68     HdiBackend(const HdiBackend& rhs) = delete;
69     HdiBackend& operator=(const HdiBackend& rhs) = delete;
70     HdiBackend(HdiBackend&& rhs) = delete;
71     HdiBackend& operator=(HdiBackend&& rhs) = delete;
72 
73     HdiDevice *device_ = nullptr;
74     void* onHotPlugCbData_ = nullptr;
75     void* onRefreshCbData_ = nullptr;
76     void* onPrepareCompleteCbData_ = nullptr;
77     OnScreenHotplugFunc onScreenHotplugCb_ = nullptr;
78     OnScreenRefreshFunc onScreenRefreshCb_ = nullptr;
79     OnPrepareCompleteFunc onPrepareCompleteCb_ = nullptr;
80     std::unordered_map<uint32_t, OutputPtr> outputs_;
81 
82     static void OnHdiBackendHotPlugEvent(uint32_t deviceId, bool connected, void *data);
83     static void OnHdiBackendRefreshEvent(uint32_t deviceId, void *data);
84     RosenError InitDevice();
85     void OnHdiBackendConnected(uint32_t screenId, bool connected);
86     void CreateHdiOutput(uint32_t screenId);
87     void OnScreenHotplug(uint32_t screenId, bool connected);
88     void OnScreenRefresh(uint32_t deviceId);
89     void ReorderLayerInfo(std::vector<LayerInfoPtr> &newLayerInfos);
90     void OnPrepareComplete(bool needFlush, const OutputPtr &output, std::vector<LayerInfoPtr> &newLayerInfos);
91     int32_t PrepareCompleteIfNeed(const OutputPtr &output, bool needFlush);
92 };
93 } // namespace Rosen
94 } // namespace OHOS
95 
96 #endif // HDI_BACKEND_HDI_BACKEND_H