1 /*
2  * Copyright (c) 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 OHOS_HDI_DISPLAY_V1_1_DISPLAY_COMPOSER_HDI_IMPL_H
17 #define OHOS_HDI_DISPLAY_V1_1_DISPLAY_COMPOSER_HDI_IMPL_H
18 
19 #include "v1_0/hdi_impl/display_composer_hdi_impl.h"
20 #include "v1_1/display_command/display_cmd_requester.h"
21 #include "v1_1/display_composer_type.h"
22 #include "v1_1/idisplay_composer.h"
23 #include "v1_1/include/idisplay_composer_interface.h"
24 #include <cinttypes>
25 
26 #undef LOG_TAG
27 #define LOG_TAG "DISP_HDI_COMP"
28 #undef LOG_DOMAIN
29 #define LOG_DOMAIN 0xD002515
30 
31 namespace OHOS {
32 namespace HDI {
33 namespace Display {
34 namespace Composer {
35 namespace V1_1 {
36 
37 template <typename Interface, typename CompHdi, typename CmdReq>
38 class DisplayComposerHdiImpl : public V1_0::DisplayComposerHdiImpl<Interface, CompHdi, CmdReq>,
39     public ISeamlessChangeCallback, public IModeCallback, public IRefreshCallback {
40 public:
Create(bool needSMQ)41     static IDisplayComposerInterface* Create(bool needSMQ)
42     {
43         sptr<CompHdi> hdi;
44         std::shared_ptr<CmdReq> req = nullptr;
45         HDF_LOGI("%{public}s: hdi v1_1 start", __func__);
46         while ((hdi = CompHdi::Get()) == nullptr) {
47             // Waiting for display composer service ready
48             usleep(WAIT_TIME_INTERVAL);
49         }
50         HDF_LOGI("%{public}s: hdi v1_1 end", __func__);
51         if (needSMQ) {
52             req = CmdReq::Create(hdi);
53             if (req == nullptr) {
54                 HDF_LOGE("%{public}s: Create DisplayCmdRequester failed %{public}d", __func__, __LINE__);
55                 return nullptr;
56             }
57         }
58         return new DisplayComposerHdiImpl(hdi, req);
59     }
60 
DisplayComposerHdiImpl(sptr<CompHdi> hdi,std::shared_ptr<CmdReq> req)61     DisplayComposerHdiImpl(sptr<CompHdi> hdi, std::shared_ptr<CmdReq> req)
62         : BaseType1_0(hdi, req),
63         hdi_v1_1_(hdi),
64         seamlessChangeCb_(nullptr),
65         refreshCb_(nullptr),
66         seamlessChangeCbData_(nullptr),
67         refreshCbData_(nullptr),
68         modeCb_(nullptr) {}
69 
~DisplayComposerHdiImpl()70     virtual ~DisplayComposerHdiImpl() {}
71 
OnSeamlessChange(uint32_t outputId)72     virtual int32_t OnSeamlessChange(uint32_t outputId) override
73     {
74         DISPLAY_TRACE;
75 
76         HDF_LOGD("OnSeamlessChange(%{public}u)", outputId);
77         int32_t ret = HDF_SUCCESS;
78         if (seamlessChangeCb_) {
79             seamlessChangeCb_(outputId, seamlessChangeCbData_);
80         } else {
81             HDF_LOGE("seamless change callback is nullptr");
82             ret = HDF_FAILURE;
83         }
84         return ret;
85     }
86 
RegSeamlessChangeCallback(SeamlessChangeCallback cb,void * data)87     virtual int32_t RegSeamlessChangeCallback(SeamlessChangeCallback cb, void *data) override
88     {
89         seamlessChangeCb_ = cb;
90         seamlessChangeCbData_ = data;
91         COMPOSER_CHECK_NULLPTR_RETURN(hdi_v1_1_);
92         return ToDispErrCode(hdi_v1_1_->RegSeamlessChangeCallback(this));
93     }
94 
GetDisplaySupportedModesExt(unsigned int devId,std::vector<DisplayModeInfoExt> & modes)95     virtual int32_t GetDisplaySupportedModesExt(unsigned int devId, std::vector<DisplayModeInfoExt> &modes) override
96     {
97         COMPOSER_CHECK_NULLPTR_RETURN(hdi_v1_1_);
98         return ToDispErrCode(hdi_v1_1_->GetDisplaySupportedModesExt(devId, modes));
99     }
100 
OnMode(uint32_t modeId,uint64_t vBlankPeriod)101     virtual int32_t OnMode(uint32_t modeId, uint64_t vBlankPeriod) override
102     {
103         DISPLAY_TRACE;
104 
105         HDF_LOGD("OnMode(%{public}u, %{public}" PRIu64 ")", modeId, vBlankPeriod);
106         int32_t ret = HDF_SUCCESS;
107         if (modeCb_) {
108             modeCb_(modeId, vBlankPeriod, nullptr);
109         } else {
110             HDF_LOGE("mode callback is nullptr");
111             ret = HDF_FAILURE;
112         }
113         return ret;
114     }
115 
SetDisplayModeAsync(uint32_t devId,uint32_t modeId,ModeCallback cb)116     virtual int32_t SetDisplayModeAsync(uint32_t devId, uint32_t modeId, ModeCallback cb) override
117     {
118         modeCb_ = cb;
119         COMPOSER_CHECK_NULLPTR_RETURN(hdi_v1_1_);
120         return ToDispErrCode(hdi_v1_1_->SetDisplayModeAsync(devId, modeId, this));
121     }
122 
GetDisplayVBlankPeriod(uint32_t devId,uint64_t & period)123     virtual int32_t GetDisplayVBlankPeriod(uint32_t devId, uint64_t &period) override
124     {
125         COMPOSER_CHECK_NULLPTR_RETURN(hdi_v1_1_);
126         return ToDispErrCode(hdi_v1_1_->GetDisplayVBlankPeriod(devId, period));
127     }
128 
SetLayerPerFrameParameter(uint32_t devId,uint32_t layerId,const std::string & key,const std::vector<int8_t> & value)129     virtual int32_t SetLayerPerFrameParameter(uint32_t devId, uint32_t layerId, const std::string& key,
130         const std::vector<int8_t>& value) override
131     {
132         return ToDispErrCode(hdi_v1_1_->SetLayerPerFrameParameter(devId, layerId, key, value));
133     }
134 
GetSupportedLayerPerFrameParameterKey(std::vector<std::string> & keys)135     virtual int32_t GetSupportedLayerPerFrameParameterKey(std::vector<std::string>& keys) override
136     {
137         return ToDispErrCode(hdi_v1_1_->GetSupportedLayerPerFrameParameterKey(keys));
138     }
139 
SetDisplayOverlayResolution(uint32_t devId,uint32_t width,uint32_t height)140     virtual int32_t SetDisplayOverlayResolution(uint32_t devId, uint32_t width, uint32_t height) override
141     {
142         return ToDispErrCode(hdi_v1_1_->SetDisplayOverlayResolution(devId, width, height));
143     }
144 
OnRefresh(uint32_t devId)145     virtual int32_t OnRefresh(uint32_t devId) override
146     {
147         DISPLAY_TRACE;
148 
149         HDF_LOGD("OnRefresh(%{public}u)", devId);
150         int32_t ret = HDF_SUCCESS;
151         if (refreshCb_ != nullptr) {
152             refreshCb_(devId, refreshCbData_);
153         } else {
154             HDF_LOGE("error: refresh callback fn is nullptr");
155             ret = HDF_FAILURE;
156         }
157         return ret;
158     }
159 
RegRefreshCallback(RefreshCallback cb,void * data)160     virtual int32_t RegRefreshCallback(RefreshCallback cb, void *data) override
161     {
162         refreshCb_ = cb;
163         refreshCbData_ = data;
164         COMPOSER_CHECK_NULLPTR_RETURN(hdi_v1_1_);
165         return ToDispErrCode(hdi_v1_1_->RegRefreshCallback(this));
166     }
167 
GetDisplaySupportedColorGamuts(uint32_t devId,std::vector<ColorGamut> & gamuts)168     virtual int32_t GetDisplaySupportedColorGamuts(uint32_t devId, std::vector<ColorGamut>& gamuts) override
169     {
170         COMPOSER_CHECK_NULLPTR_RETURN(hdi_v1_1_);
171         return ToDispErrCode(hdi_v1_1_->GetDisplaySupportedColorGamuts(devId, gamuts));
172     }
173 
GetHDRCapabilityInfos(uint32_t devId,HDRCapability & info)174     virtual int32_t GetHDRCapabilityInfos(uint32_t devId, HDRCapability& info) override
175     {
176         COMPOSER_CHECK_NULLPTR_RETURN(hdi_v1_1_);
177         return ToDispErrCode(hdi_v1_1_->GetHDRCapabilityInfos(devId, info));
178     }
179 
180     protected:
181     using BaseType1_0 = V1_0::DisplayComposerHdiImpl<Interface, CompHdi, CmdReq>;
182     using BaseType1_0::WAIT_TIME_INTERVAL;
183     using BaseType1_0::ToDispErrCode;
184     sptr<CompHdi> hdi_v1_1_;
185     private:
186     SeamlessChangeCallback seamlessChangeCb_;
187     RefreshCallback refreshCb_;
188     void *seamlessChangeCbData_;
189     void *refreshCbData_;
190     ModeCallback modeCb_;
191 };
192 using HdiDisplayComposer = DisplayComposerHdiImpl<IDisplayComposerInterface, IDisplayComposer, HdiDisplayCmdRequester>;
193 } // namespace V1_1
194 } // namespace Composer
195 } // namespace Display
196 } // namespace HDI
197 } // namespace OHOS
198 #endif // OHOS_HDI_DISPLAY_V1_1_DISPLAY_COMPOSER_HDI_IMPL_H
199