• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "display_composer_service.h"
17 #include <dlfcn.h>
18 #include <hdf_base.h>
19 
20 #include "display_log.h"
21 #include "hdf_log.h"
22 
23 namespace OHOS {
24 namespace HDI {
25 namespace Display {
26 namespace Composer {
27 namespace V1_0 {
DisplayComposerImplGetInstance(void)28 extern "C" IDisplayComposer *DisplayComposerImplGetInstance(void)
29 {
30     return new (std::nothrow) DisplayComposerService();
31 }
32 
DisplayComposerService()33 DisplayComposerService::DisplayComposerService()
34     : libHandle_(nullptr),
35     createHwiFunc_(nullptr),
36     destroyHwiFunc_(nullptr),
37     hwiImpl_(nullptr),
38     cmdResponser_(nullptr),
39     hotPlugCb_(nullptr),
40     vBlankCb_(nullptr)
41 {
42     int32_t ret = LoadHwi();
43     if (ret == HDF_SUCCESS) {
44         hwiImpl_.reset(createHwiFunc_());
45         CHECK_NULLPOINTER_RETURN(hwiImpl_);
46         cmdResponser_ = HdiDisplayCmdResponser::Create(hwiImpl_);
47         CHECK_NULLPOINTER_RETURN(cmdResponser_);
48     } else {
49         HDF_LOGE("error: LoadHwi failure, lib name:%{public}s", DISPLAY_COMPOSER_HWI_LIBRARY_NAME);
50     }
51 }
52 
~DisplayComposerService()53 DisplayComposerService::~DisplayComposerService()
54 {
55     if (destroyHwiFunc_ != nullptr && hwiImpl_ != nullptr) {
56         destroyHwiFunc_(hwiImpl_.get());
57         hwiImpl_.reset();
58     }
59     if (libHandle_ != nullptr) {
60         dlclose(libHandle_);
61     }
62 }
63 
LoadHwi()64 int32_t DisplayComposerService::LoadHwi()
65 {
66     const char *errStr = dlerror();
67     if (errStr) {
68         HDF_LOGI("warning, existing dlerror: %{public}s", errStr);
69     }
70     libHandle_ = dlopen(DISPLAY_COMPOSER_HWI_LIBRARY_NAME, RTLD_LAZY);
71     CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
72 
73     createHwiFunc_ = reinterpret_cast<CreateComposerHwiFunc_t *>(dlsym(libHandle_, "CreateComposerHwi"));
74     errStr = dlerror();
75     if (errStr) {
76         HDF_LOGE("error: %{public}s", errStr);
77         return HDF_FAILURE;
78     }
79 
80     destroyHwiFunc_ = reinterpret_cast<DestroyComposerHwiFunc_t *>(dlsym(libHandle_, "DestroyComposerHwi"));
81     errStr = dlerror();
82     if (errStr) {
83         HDF_LOGE("error: %{public}s", errStr);
84         return HDF_FAILURE;
85     }
86 
87     return HDF_SUCCESS;
88 }
89 
OnHotPlug(uint32_t outputId,bool connected,void * data)90 void DisplayComposerService::OnHotPlug(uint32_t outputId, bool connected, void *data)
91 {
92     if (data != nullptr) {
93         sptr<IHotPlugCallback> remoteCb = reinterpret_cast<DisplayComposerService *>(data)->hotPlugCb_;
94         if (remoteCb != nullptr) {
95             remoteCb->OnHotPlug(outputId, connected);
96         } else {
97             HDF_LOGE("error: OnHotPlug hotPlugCb_ nullptr");
98         }
99     } else {
100         HDF_LOGE("error: OnHotPlug cb data nullptr");
101     }
102     return;
103 }
104 
OnVBlank(unsigned int sequence,uint64_t ns,void * data)105 void DisplayComposerService::OnVBlank(unsigned int sequence, uint64_t ns, void *data)
106 {
107     if (data != nullptr) {
108         IVBlankCallback *remoteCb = reinterpret_cast<IVBlankCallback *>(data);
109         if (remoteCb != nullptr) {
110             remoteCb->OnVBlank(sequence, ns);
111         } else {
112             HDF_LOGE("error: OnVBlank hotPlugCb_ nullptr");
113         }
114     } else {
115         HDF_LOGE("error: OnVBlank cb data nullptr");
116     }
117     return;
118 }
119 
RegHotPlugCallback(const sptr<IHotPlugCallback> & cb)120 int32_t DisplayComposerService::RegHotPlugCallback(const sptr<IHotPlugCallback> &cb)
121 {
122     hotPlugCb_ = cb;
123     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
124     return hwiImpl_->RegHotPlugCallback(OnHotPlug, this);
125 }
126 
GetDisplayCapability(uint32_t devId,DisplayCapability & info)127 int32_t DisplayComposerService::GetDisplayCapability(uint32_t devId, DisplayCapability &info)
128 {
129     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
130     return hwiImpl_->GetDisplayCapability(devId, info);
131 }
132 
GetDisplaySupportedModes(uint32_t devId,std::vector<DisplayModeInfo> & modes)133 int32_t DisplayComposerService::GetDisplaySupportedModes(uint32_t devId, std::vector<DisplayModeInfo> &modes)
134 {
135     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
136     return hwiImpl_->GetDisplaySupportedModes(devId, modes);
137 }
138 
GetDisplayMode(uint32_t devId,uint32_t & modeId)139 int32_t DisplayComposerService::GetDisplayMode(uint32_t devId, uint32_t &modeId)
140 {
141     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
142     return hwiImpl_->GetDisplayMode(devId, modeId);
143 }
144 
SetDisplayMode(uint32_t devId,uint32_t modeId)145 int32_t DisplayComposerService::SetDisplayMode(uint32_t devId, uint32_t modeId)
146 {
147     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
148     return hwiImpl_->SetDisplayMode(devId, modeId);
149 }
150 
GetDisplayPowerStatus(uint32_t devId,DispPowerStatus & status)151 int32_t DisplayComposerService::GetDisplayPowerStatus(uint32_t devId, DispPowerStatus &status)
152 {
153     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
154     return hwiImpl_->GetDisplayPowerStatus(devId, status);
155 }
156 
SetDisplayPowerStatus(uint32_t devId,DispPowerStatus status)157 int32_t DisplayComposerService::SetDisplayPowerStatus(uint32_t devId, DispPowerStatus status)
158 {
159     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
160     return hwiImpl_->SetDisplayPowerStatus(devId, status);
161 }
162 
GetDisplayBacklight(uint32_t devId,uint32_t & level)163 int32_t DisplayComposerService::GetDisplayBacklight(uint32_t devId, uint32_t &level)
164 {
165     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
166     return hwiImpl_->GetDisplayBacklight(devId, level);
167 }
168 
SetDisplayBacklight(uint32_t devId,uint32_t level)169 int32_t DisplayComposerService::SetDisplayBacklight(uint32_t devId, uint32_t level)
170 {
171     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
172     return hwiImpl_->SetDisplayBacklight(devId, level);
173 }
174 
GetDisplayProperty(uint32_t devId,uint32_t id,uint64_t & value)175 int32_t DisplayComposerService::GetDisplayProperty(uint32_t devId, uint32_t id, uint64_t &value)
176 {
177     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
178     return hwiImpl_->GetDisplayProperty(devId, id, value);
179 }
180 
GetDisplayCompChange(uint32_t devId,std::vector<uint32_t> & layers,std::vector<int32_t> & type)181 int32_t DisplayComposerService::GetDisplayCompChange(
182     uint32_t devId, std::vector<uint32_t> &layers, std::vector<int32_t> &type)
183 {
184     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
185     return hwiImpl_->GetDisplayCompChange(devId, layers, type);
186 }
187 
SetDisplayClientCrop(uint32_t devId,const IRect & rect)188 int32_t DisplayComposerService::SetDisplayClientCrop(uint32_t devId, const IRect &rect)
189 {
190     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
191     return hwiImpl_->SetDisplayClientCrop(devId, rect);
192 }
193 
SetDisplayClientDestRect(uint32_t devId,const IRect & rect)194 int32_t DisplayComposerService::SetDisplayClientDestRect(uint32_t devId, const IRect &rect)
195 {
196     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
197     return hwiImpl_->SetDisplayClientDestRect(devId, rect);
198 }
199 
SetDisplayVsyncEnabled(uint32_t devId,bool enabled)200 int32_t DisplayComposerService::SetDisplayVsyncEnabled(uint32_t devId, bool enabled)
201 {
202     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
203     return hwiImpl_->SetDisplayVsyncEnabled(devId, enabled);
204 }
205 
RegDisplayVBlankCallback(uint32_t devId,const sptr<IVBlankCallback> & cb)206 int32_t DisplayComposerService::RegDisplayVBlankCallback(uint32_t devId, const sptr<IVBlankCallback> &cb)
207 {
208     vBlankCb_ = cb;
209     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
210     return hwiImpl_->RegDisplayVBlankCallback(devId, OnVBlank, vBlankCb_.GetRefPtr());
211 }
212 
GetDisplayReleaseFence(uint32_t devId,std::vector<uint32_t> & layers,std::vector<sptr<HdifdParcelable>> & fences)213 int32_t DisplayComposerService::GetDisplayReleaseFence(
214     uint32_t devId, std::vector<uint32_t> &layers, std::vector<sptr<HdifdParcelable>> &fences)
215 {
216     std::vector<int32_t> outFences;
217     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
218     int32_t ec = hwiImpl_->GetDisplayReleaseFence(devId, layers, outFences);
219     for (int i = 0; i < outFences.size(); i++) {
220         int32_t dupFd = outFences[i];
221         sptr<HdifdParcelable> hdifd(new HdifdParcelable());
222         hdifd->Init(dupFd);
223         fences.push_back(hdifd);
224     }
225     return ec;
226 }
227 
CreateVirtualDisplay(uint32_t width,uint32_t height,int32_t & format,uint32_t & devId)228 int32_t DisplayComposerService::CreateVirtualDisplay(uint32_t width, uint32_t height, int32_t &format, uint32_t &devId)
229 {
230     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
231     return hwiImpl_->CreateVirtualDisplay(width, height, format, devId);
232 }
233 
DestroyVirtualDisplay(uint32_t devId)234 int32_t DisplayComposerService::DestroyVirtualDisplay(uint32_t devId)
235 {
236     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
237     return hwiImpl_->DestroyVirtualDisplay(devId);
238 }
239 
SetVirtualDisplayBuffer(uint32_t devId,const sptr<BufferHandleParcelable> & buffer,const sptr<HdifdParcelable> & fence)240 int32_t DisplayComposerService::SetVirtualDisplayBuffer(
241     uint32_t devId, const sptr<BufferHandleParcelable> &buffer, const sptr<HdifdParcelable> &fence)
242 {
243     BufferHandle *handle = buffer->GetBufferHandle();
244     int32_t inFence = fence->GetFd();
245     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
246     return hwiImpl_->SetVirtualDisplayBuffer(devId, *handle, inFence);
247 }
248 
SetDisplayProperty(uint32_t devId,uint32_t id,uint64_t value)249 int32_t DisplayComposerService::SetDisplayProperty(uint32_t devId, uint32_t id, uint64_t value)
250 {
251     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
252     return hwiImpl_->SetDisplayProperty(devId, id, value);
253 }
254 
CreateLayer(uint32_t devId,const LayerInfo & layerInfo,uint32_t & layerId)255 int32_t DisplayComposerService::CreateLayer(uint32_t devId, const LayerInfo &layerInfo, uint32_t &layerId)
256 {
257     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
258     return hwiImpl_->CreateLayer(devId, layerInfo, layerId);
259 }
260 
DestroyLayer(uint32_t devId,uint32_t layerId)261 int32_t DisplayComposerService::DestroyLayer(uint32_t devId, uint32_t layerId)
262 {
263     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
264     return hwiImpl_->DestroyLayer(devId, layerId);
265 }
266 
InitCmdRequest(const std::shared_ptr<SharedMemQueue<int32_t>> & request)267 int32_t DisplayComposerService::InitCmdRequest(const std::shared_ptr<SharedMemQueue<int32_t>> &request)
268 {
269     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
270     return cmdResponser_->InitCmdRequest(request);
271 }
272 
CmdRequest(uint32_t inEleCnt,const std::vector<HdifdInfo> & inFds,uint32_t & outEleCnt,std::vector<HdifdInfo> & outFds)273 int32_t DisplayComposerService::CmdRequest(
274     uint32_t inEleCnt, const std::vector<HdifdInfo> &inFds, uint32_t &outEleCnt, std::vector<HdifdInfo> &outFds)
275 {
276     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
277     return cmdResponser_->CmdRequest(inEleCnt, inFds, outEleCnt, outFds);
278 }
279 
GetCmdReply(std::shared_ptr<SharedMemQueue<int32_t>> & reply)280 int32_t DisplayComposerService::GetCmdReply(std::shared_ptr<SharedMemQueue<int32_t>> &reply)
281 {
282     CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
283     return cmdResponser_->GetCmdReply(reply);
284 }
285 } // namespace V1_0
286 } // namespace Composer
287 } // namespace Display
288 } // namespace HDI
289 } // namespace OHOS
290