• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "surface_type.h"
28 #include "vsync_sampler.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 };
39 
40 using OnScreenHotplugFunc = std::function<void(OutputPtr &output, bool connected, void* data)>;
41 using OnPrepareCompleteFunc = std::function<void(sptr<Surface>& surface,
42                                                  const struct PrepareCompleteParam &param, void* data)>;
43 
44 class HdiBackend {
45 public:
46     /* for RS begin */
47     static HdiBackend* GetInstance();
48     RosenError RegScreenHotplug(OnScreenHotplugFunc func, void* data);
49     RosenError RegPrepareComplete(OnPrepareCompleteFunc func, void* data);
50     void Repaint(std::vector<OutputPtr> &outputs);
51     std::map<LayerInfoPtr, sptr<SyncFence>> GetLayersReleaseFence(const OutputPtr& output);
52     /* for RS end */
53 
54     /* only used for mock tests */
55     void SetHdiBackendDevice(Base::HdiDevice* device);
56 private:
57     HdiBackend() = default;
58     virtual ~HdiBackend() = default;
59 
60     HdiBackend(const HdiBackend& rhs) = delete;
61     HdiBackend& operator=(const HdiBackend& rhs) = delete;
62     HdiBackend(HdiBackend&& rhs) = delete;
63     HdiBackend& operator=(HdiBackend&& rhs) = delete;
64 
65     Base::HdiDevice *device_ = nullptr;
66     void* onHotPlugCbData_ = nullptr;
67     void* onPrepareCompleteCbData_ = nullptr;
68     OnScreenHotplugFunc onScreenHotplugCb_ = nullptr;
69     OnPrepareCompleteFunc onPrepareCompleteCb_ = nullptr;
70     std::unordered_map<uint32_t, OutputPtr> outputs_;
71 
72     static void OnHdiBackendHotPlugEvent(uint32_t deviceId, bool connected, void *data);
73     RosenError InitDevice();
74     void OnHdiBackendConnected(uint32_t screenId, bool connected);
75     void CreateHdiOutput(uint32_t screenId);
76     void OnScreenHotplug(uint32_t screenId, bool connected);
77     void ReorderLayerInfo(std::vector<LayerInfoPtr> &newLayerInfos);
78     void OnPrepareComplete(bool needFlush, OutputPtr &output, std::vector<LayerInfoPtr> &newLayerInfos);
79     int32_t FlushScreen(const OutputPtr &output, std::vector<LayerPtr> &compClientLayers, sptr<SurfaceBuffer> &buffer);
80     void ReleaseFramebuffer(const OutputPtr &output, sptr<SyncFence> &presentFence, const sptr<SurfaceBuffer> &buffer);
81     int32_t SetScreenClientInfo(const FrameBufferEntry &fbEntry, const OutputPtr &output);
82     int32_t UpdateLayerCompType(uint32_t screenId, const std::unordered_map<uint32_t, LayerPtr> &layersMap);
83     int32_t PreProcessLayersComp(const OutputPtr &output, const std::unordered_map<uint32_t, LayerPtr> &layersMap,
84                                  bool &needFlush);
85 
86     sptr<VSyncSampler> sampler_ = nullptr;
87     std::unordered_map<int, sptr<SurfaceBuffer>> lastFrameBuffers_;
88     sptr<SyncFence> lastPresentFence_ = SyncFence::INVALID_FENCE;
89 };
90 } // namespace Rosen
91 } // namespace OHOS
92 
93 #endif // HDI_BACKEND_HDI_BACKEND_H