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 #include "display_composer_service.h"
17
18 #include <mutex>
19 #include <dlfcn.h>
20 #include <hdf_base.h>
21 #include "display_log.h"
22 #include "hdf_log.h"
23 #include "hdf_trace.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "COMPOSER_SRV"
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN 0xD002500
29
30 #undef DISPLAY_TRACE
31 #define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:")
32
33 namespace OHOS {
34 namespace HDI {
35 namespace Display {
36 namespace Composer {
37 namespace V1_0 {
DisplayComposerImplGetInstance(void)38 extern "C" IDisplayComposer* DisplayComposerImplGetInstance(void)
39 {
40 return new (std::nothrow) DisplayComposerService();
41 }
42
DisplayComposerService()43 DisplayComposerService::DisplayComposerService()
44 : libHandle_(nullptr),
45 cacheMgr_(nullptr),
46 createVdiFunc_(nullptr),
47 destroyVdiFunc_(nullptr),
48 currentBacklightLevel_(0),
49 vdiImpl_(nullptr),
50 cmdResponser_(nullptr),
51 hotPlugCb_(nullptr),
52 vBlankCb_(nullptr)
53 {
54 int32_t ret = LoadVdi();
55 if (ret == HDF_SUCCESS) {
56 vdiImpl_ = createVdiFunc_();
57 CHECK_NULLPOINTER_RETURN(vdiImpl_);
58 cacheMgr_.reset(DeviceCacheManager::GetInstance());
59 CHECK_NULLPOINTER_RETURN(cacheMgr_);
60 cmdResponser_ = HdiDisplayCmdResponser::Create(vdiImpl_, cacheMgr_);
61 CHECK_NULLPOINTER_RETURN(cmdResponser_);
62 } else {
63 DISPLAY_LOGE("Load composer VDI failed, lib: %{public}s", DISPLAY_COMPOSER_VDI_LIBRARY);
64 }
65 }
66
~DisplayComposerService()67 DisplayComposerService::~DisplayComposerService()
68 {
69 cmdResponser_ = nullptr;
70 if ((destroyVdiFunc_ != nullptr) && (vdiImpl_ != nullptr)) {
71 destroyVdiFunc_(vdiImpl_);
72 }
73 if (libHandle_ != nullptr) {
74 dlclose(libHandle_);
75 }
76 }
77
LoadVdi()78 int32_t DisplayComposerService::LoadVdi()
79 {
80 const char* errStr = dlerror();
81 if (errStr != nullptr) {
82 DISPLAY_LOGI("composer loadvid, clear earlier dlerror: %{public}s", errStr);
83 }
84 libHandle_ = dlopen(DISPLAY_COMPOSER_VDI_LIBRARY, RTLD_LAZY);
85 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
86
87 createVdiFunc_ = reinterpret_cast<CreateComposerVdiFunc>(dlsym(libHandle_, "CreateComposerVdi"));
88 if (createVdiFunc_ == nullptr) {
89 errStr = dlerror();
90 if (errStr != nullptr) {
91 DISPLAY_LOGE("composer CreateComposerVdi dlsym error: %{public}s", errStr);
92 }
93 dlclose(libHandle_);
94 return HDF_FAILURE;
95 }
96
97 destroyVdiFunc_ = reinterpret_cast<DestroyComposerVdiFunc>(dlsym(libHandle_, "DestroyComposerVdi"));
98 if (destroyVdiFunc_ == nullptr) {
99 errStr = dlerror();
100 if (errStr != nullptr) {
101 DISPLAY_LOGE("composer DestroyComposerVdi dlsym error: %{public}s", errStr);
102 }
103 dlclose(libHandle_);
104 return HDF_FAILURE;
105 }
106 return HDF_SUCCESS;
107 }
108
OnHotPlug(uint32_t outputId,bool connected,void * data)109 void DisplayComposerService::OnHotPlug(uint32_t outputId, bool connected, void* data)
110 {
111 if (data == nullptr) {
112 DISPLAY_LOGE("cb data is nullptr");
113 return;
114 }
115
116 auto cacheMgr = reinterpret_cast<DisplayComposerService*>(data)->cacheMgr_;
117 if (cacheMgr == nullptr) {
118 DISPLAY_LOGE("CacheMgr_ is nullptr");
119 return;
120 }
121 if (connected) {
122 std::lock_guard<std::mutex> lock(cacheMgr->GetCacheMgrMutex());
123 // Add new device cache
124 if (cacheMgr->AddDeviceCache(outputId) != HDF_SUCCESS) {
125 DISPLAY_LOGE("Add device cache failed");
126 }
127 } else {
128 std::lock_guard<std::mutex> lock(cacheMgr->GetCacheMgrMutex());
129 // Del new device cache
130 if (cacheMgr->RemoveDeviceCache(outputId) != HDF_SUCCESS) {
131 DISPLAY_LOGE("Del device cache failed");
132 }
133 }
134
135 sptr<IHotPlugCallback> remoteCb = reinterpret_cast<DisplayComposerService*>(data)->hotPlugCb_;
136 if (remoteCb == nullptr) {
137 DISPLAY_LOGE("hotPlugCb_ is nullptr");
138 return;
139 }
140 remoteCb->OnHotPlug(outputId, connected);
141 }
142
OnVBlank(unsigned int sequence,uint64_t ns,void * data)143 void DisplayComposerService::OnVBlank(unsigned int sequence, uint64_t ns, void* data)
144 {
145 if (data == nullptr) {
146 DISPLAY_LOGE("cb data is nullptr");
147 return;
148 }
149
150 IVBlankCallback* remoteCb = reinterpret_cast<IVBlankCallback*>(data);
151 if (remoteCb == nullptr) {
152 DISPLAY_LOGE("vblankCb_ is nullptr");
153 return;
154 }
155 remoteCb->OnVBlank(sequence, ns);
156 }
157
RegHotPlugCallback(const sptr<IHotPlugCallback> & cb)158 int32_t DisplayComposerService::RegHotPlugCallback(const sptr<IHotPlugCallback>& cb)
159 {
160 DISPLAY_TRACE;
161
162 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
163 hotPlugCb_ = cb;
164 int32_t ret = vdiImpl_->RegHotPlugCallback(OnHotPlug, this);
165 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
166 return ret;
167 }
168
SetClientBufferCacheCount(uint32_t devId,uint32_t count)169 int32_t DisplayComposerService::SetClientBufferCacheCount(uint32_t devId, uint32_t count)
170 {
171 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
172 CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
173 std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
174 DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
175 DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
176
177 DISPLAY_CHK_RETURN(devCache->SetClientBufferCacheCount(count) != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE("fail"));
178 return HDF_SUCCESS;
179 }
180
GetDisplayCapability(uint32_t devId,DisplayCapability & info)181 int32_t DisplayComposerService::GetDisplayCapability(uint32_t devId, DisplayCapability& info)
182 {
183 DISPLAY_TRACE;
184
185 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
186 int32_t ret = vdiImpl_->GetDisplayCapability(devId, info);
187 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
188 return HDF_SUCCESS;
189 }
190
GetDisplaySupportedModes(uint32_t devId,std::vector<DisplayModeInfo> & modes)191 int32_t DisplayComposerService::GetDisplaySupportedModes(uint32_t devId, std::vector<DisplayModeInfo>& modes)
192 {
193 DISPLAY_TRACE;
194
195 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
196 int32_t ret = vdiImpl_->GetDisplaySupportedModes(devId, modes);
197 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
198 return ret;
199 }
200
GetDisplayMode(uint32_t devId,uint32_t & modeId)201 int32_t DisplayComposerService::GetDisplayMode(uint32_t devId, uint32_t& modeId)
202 {
203 DISPLAY_TRACE;
204
205 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
206 int32_t ret = vdiImpl_->GetDisplayMode(devId, modeId);
207 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
208 return ret;
209 }
210
SetDisplayMode(uint32_t devId,uint32_t modeId)211 int32_t DisplayComposerService::SetDisplayMode(uint32_t devId, uint32_t modeId)
212 {
213 DISPLAY_TRACE;
214
215 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
216 int32_t ret = vdiImpl_->SetDisplayMode(devId, modeId);
217 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
218 return ret;
219 }
220
GetDisplayPowerStatus(uint32_t devId,DispPowerStatus & status)221 int32_t DisplayComposerService::GetDisplayPowerStatus(uint32_t devId, DispPowerStatus& status)
222 {
223 DISPLAY_TRACE;
224
225 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
226 int32_t ret = vdiImpl_->GetDisplayPowerStatus(devId, status);
227 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
228 return ret;
229 }
230
SetDisplayPowerStatus(uint32_t devId,DispPowerStatus status)231 int32_t DisplayComposerService::SetDisplayPowerStatus(uint32_t devId, DispPowerStatus status)
232 {
233 DISPLAY_TRACE;
234
235 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
236 int32_t ret = vdiImpl_->SetDisplayPowerStatus(devId, status);
237 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
238 return ret;
239 }
240
GetDisplayBacklight(uint32_t devId,uint32_t & level)241 int32_t DisplayComposerService::GetDisplayBacklight(uint32_t devId, uint32_t& level)
242 {
243 DISPLAY_TRACE;
244
245 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
246 int32_t ret = vdiImpl_->GetDisplayBacklight(devId, level);
247 DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_SUCCESS, level = currentBacklightLevel_);
248 return ret;
249 }
250
SetDisplayBacklight(uint32_t devId,uint32_t level)251 int32_t DisplayComposerService::SetDisplayBacklight(uint32_t devId, uint32_t level)
252 {
253 DISPLAY_TRACE;
254
255 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
256 int32_t ret = vdiImpl_->SetDisplayBacklight(devId, level);
257 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
258 currentBacklightLevel_ = level;
259 return ret;
260 }
261
GetDisplayProperty(uint32_t devId,uint32_t id,uint64_t & value)262 int32_t DisplayComposerService::GetDisplayProperty(uint32_t devId, uint32_t id, uint64_t& value)
263 {
264 DISPLAY_TRACE;
265
266 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
267 int32_t ret = vdiImpl_->GetDisplayProperty(devId, id, value);
268 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
269 return ret;
270 }
271
SetDisplayClientCrop(uint32_t devId,const IRect & rect)272 int32_t DisplayComposerService::SetDisplayClientCrop(uint32_t devId, const IRect& rect)
273 {
274 DISPLAY_TRACE;
275
276 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
277 int32_t ret = vdiImpl_->SetDisplayClientCrop(devId, rect);
278 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
279 return ret;
280 }
281
SetDisplayVsyncEnabled(uint32_t devId,bool enabled)282 int32_t DisplayComposerService::SetDisplayVsyncEnabled(uint32_t devId, bool enabled)
283 {
284 DISPLAY_TRACE;
285
286 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
287 int32_t ret = vdiImpl_->SetDisplayVsyncEnabled(devId, enabled);
288 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
289 return ret;
290 }
291
RegDisplayVBlankCallback(uint32_t devId,const sptr<IVBlankCallback> & cb)292 int32_t DisplayComposerService::RegDisplayVBlankCallback(uint32_t devId, const sptr<IVBlankCallback>& cb)
293 {
294 DISPLAY_TRACE;
295
296 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
297 int32_t ret = vdiImpl_->RegDisplayVBlankCallback(devId, OnVBlank, cb.GetRefPtr());
298 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
299 vBlankCb_ = cb;
300 return ret;
301 }
302
GetDisplayReleaseFence(uint32_t devId,std::vector<uint32_t> & layers,std::vector<sptr<HdifdParcelable>> & fences)303 int32_t DisplayComposerService::GetDisplayReleaseFence(
304 uint32_t devId, std::vector<uint32_t>& layers, std::vector<sptr<HdifdParcelable>>& fences)
305 {
306 DISPLAY_TRACE;
307
308 std::vector<int32_t> outFences;
309 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
310 int32_t ret = vdiImpl_->GetDisplayReleaseFence(devId, layers, outFences);
311 for (uint i = 0; i < outFences.size(); i++) {
312 int32_t dupFd = outFences[i];
313 sptr<HdifdParcelable> hdifd(new HdifdParcelable());
314 hdifd->Init(dupFd);
315 fences.push_back(hdifd);
316 }
317 return ret;
318 }
319
CreateVirtualDisplay(uint32_t width,uint32_t height,int32_t & format,uint32_t & devId)320 int32_t DisplayComposerService::CreateVirtualDisplay(uint32_t width, uint32_t height, int32_t& format, uint32_t& devId)
321 {
322 DISPLAY_TRACE;
323
324 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
325 int32_t ret = vdiImpl_->CreateVirtualDisplay(width, height, format, devId);
326 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
327 return ret;
328 }
329
DestroyVirtualDisplay(uint32_t devId)330 int32_t DisplayComposerService::DestroyVirtualDisplay(uint32_t devId)
331 {
332 DISPLAY_TRACE;
333
334 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
335 int32_t ret = vdiImpl_->DestroyVirtualDisplay(devId);
336 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
337 return ret;
338 }
339
SetVirtualDisplayBuffer(uint32_t devId,const sptr<NativeBuffer> & buffer,const sptr<HdifdParcelable> & fence)340 int32_t DisplayComposerService::SetVirtualDisplayBuffer(
341 uint32_t devId, const sptr<NativeBuffer>& buffer, const sptr<HdifdParcelable>& fence)
342 {
343 DISPLAY_TRACE;
344
345 CHECK_NULLPOINTER_RETURN_VALUE(fence, HDF_FAILURE);
346 BufferHandle* handle = buffer->GetBufferHandle();
347 int32_t inFence = fence->GetFd();
348 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
349 int32_t ret = vdiImpl_->SetVirtualDisplayBuffer(devId, *handle, inFence);
350 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
351 return ret;
352 }
353
SetDisplayProperty(uint32_t devId,uint32_t id,uint64_t value)354 int32_t DisplayComposerService::SetDisplayProperty(uint32_t devId, uint32_t id, uint64_t value)
355 {
356 DISPLAY_TRACE;
357
358 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
359 int32_t ret = vdiImpl_->SetDisplayProperty(devId, id, value);
360 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
361 return ret;
362 }
363
CreateLayer(uint32_t devId,const LayerInfo & layerInfo,uint32_t cacheCount,uint32_t & layerId)364 int32_t DisplayComposerService::CreateLayer(uint32_t devId, const LayerInfo& layerInfo, uint32_t cacheCount,
365 uint32_t& layerId)
366 {
367 DISPLAY_TRACE;
368
369 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
370 int32_t ret = vdiImpl_->CreateLayer(devId, layerInfo, layerId);
371 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
372
373 CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
374 std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
375 DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
376 DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
377
378 return devCache->AddLayerCache(layerId, cacheCount);
379 }
380
DestroyLayer(uint32_t devId,uint32_t layerId)381 int32_t DisplayComposerService::DestroyLayer(uint32_t devId, uint32_t layerId)
382 {
383 DISPLAY_TRACE;
384
385 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
386 int32_t ret = vdiImpl_->DestroyLayer(devId, layerId);
387 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
388
389 CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
390 std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
391 DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
392 DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
393
394 return devCache->RemoveLayerCache(layerId);
395 }
396
InitCmdRequest(const std::shared_ptr<SharedMemQueue<int32_t>> & request)397 int32_t DisplayComposerService::InitCmdRequest(const std::shared_ptr<SharedMemQueue<int32_t>>& request)
398 {
399 CHECK_NULLPOINTER_RETURN_VALUE(request, HDF_FAILURE);
400 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
401 int32_t ret = cmdResponser_->InitCmdRequest(request);
402 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
403 return ret;
404 }
405
CmdRequest(uint32_t inEleCnt,const std::vector<HdifdInfo> & inFds,uint32_t & outEleCnt,std::vector<HdifdInfo> & outFds)406 int32_t DisplayComposerService::CmdRequest(
407 uint32_t inEleCnt, const std::vector<HdifdInfo>& inFds, uint32_t& outEleCnt, std::vector<HdifdInfo>& outFds)
408 {
409 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
410 int32_t ret = cmdResponser_->CmdRequest(inEleCnt, inFds, outEleCnt, outFds);
411 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
412 return ret;
413 }
414
GetCmdReply(std::shared_ptr<SharedMemQueue<int32_t>> & reply)415 int32_t DisplayComposerService::GetCmdReply(std::shared_ptr<SharedMemQueue<int32_t>>& reply)
416 {
417 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
418 int32_t ret = cmdResponser_->GetCmdReply(reply);
419 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
420 return ret;
421 }
422 } // namespace V1_0
423 } // namespace Composer
424 } // namespace Display
425 } // namespace HDI
426 } // namespace OHOS
427