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 #ifdef DISPLAY_COMPOSER_SERVICE_HIDUMPER
25     #include "display_dump_service.h"
26 #endif
27 
28 #undef LOG_TAG
29 #define LOG_TAG "COMPOSER_SRV"
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD002515
32 
33 #undef DISPLAY_TRACE
34 #define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:")
35 
36 namespace OHOS {
37 namespace HDI {
38 namespace Display {
39 namespace Composer {
DisplayComposerImplGetInstance(void)40 extern "C" V1_2::IDisplayComposer* DisplayComposerImplGetInstance(void)
41 {
42     return new (std::nothrow) DisplayComposerService();
43 }
44 
DisplayComposerService()45 DisplayComposerService::DisplayComposerService()
46     : libHandle_(nullptr),
47     cacheMgr_(nullptr),
48     currentBacklightLevel_(0),
49     hotPlugCb_(nullptr),
50     vBlankCb_(nullptr),
51     modeCb_(nullptr),
52     seamlessChangeCb_(nullptr),
53     vdiImpl_(nullptr),
54     destroyVdiFunc_(nullptr),
55     cmdResponser_(nullptr),
56     vdiImplV1_1_(nullptr),
57     destroyVdiFuncV1_1_(nullptr),
58     cmdResponserV1_1_(nullptr),
59     refreshCb_(nullptr),
60     VBlankIdleCb_(nullptr)
61 {
62     int32_t ret = LoadVdiSo();
63     if (ret != HDF_SUCCESS) {
64         DISPLAY_LOGE("Load composer VDI failed, lib: %{public}s", DISPLAY_COMPOSER_VDI_LIBRARY);
65         return;
66     }
67 
68     ret = LoadVdiV1_1();
69     if (ret != HDF_SUCCESS) {
70         ret = LoadVdiV1_0();
71     }
72 
73     if (ret != HDF_SUCCESS) {
74         dlclose(libHandle_);
75         libHandle_ = nullptr;
76         DISPLAY_LOGE("Load composer VDI function failed");
77     }
78 
79     HidumperInit();
80 }
81 
~DisplayComposerService()82 DisplayComposerService::~DisplayComposerService()
83 {
84     std::lock_guard<std::mutex> lck(mutex_);
85     cmdResponser_ = nullptr;
86     cmdResponserV1_1_ = nullptr;
87 
88     if ((destroyVdiFunc_ != nullptr) && (vdiImpl_ != nullptr)) {
89         destroyVdiFunc_(vdiImpl_);
90         vdiImpl_ = nullptr;
91         destroyVdiFunc_ = nullptr;
92     }
93 
94     if ((destroyVdiFuncV1_1_ != nullptr) && (vdiImplV1_1_ != nullptr)) {
95         destroyVdiFuncV1_1_(vdiImplV1_1_);
96         vdiImplV1_1_ = nullptr;
97         destroyVdiFuncV1_1_ = nullptr;
98     }
99 
100     if (libHandle_ != nullptr) {
101         dlclose(libHandle_);
102         libHandle_ = nullptr;
103     }
104 }
105 
HidumperInit()106 void DisplayComposerService::HidumperInit()
107 {
108 #ifdef DISPLAY_COMPOSER_SERVICE_HIDUMPER
109     VdiDumper& dumper = VdiDumper::GetInstance();
110     dumper.SetDumpInfoFunc(reinterpret_cast<GetDumpInfoFunc>(dlsym(libHandle_, "GetDumpInfo")));
111     dumper.SetConfigFunc(reinterpret_cast<UpdateConfigFunc>(dlsym(libHandle_, "UpdateConfig")));
112     (void)DevHostRegisterDumpHost(ComposerDumpEvent);
113 #endif
114 }
115 
LoadVdiSo()116 int32_t DisplayComposerService::LoadVdiSo()
117 {
118     const char* errStr = dlerror();
119     if (errStr != nullptr) {
120         DISPLAY_LOGD("composer load vdi, clear earlier dlerror: %{public}s", errStr);
121     }
122 #ifdef COMPOSER_VDI_DEFAULT_LIBRARY_ENABLE
123     libHandle_ = dlopen(DISPLAY_COMPOSER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
124     if (libHandle_ == nullptr) {
125         DISPLAY_LOGE("composer load vendor vdi default library failed: %{public}s",
126             DISPLAY_COMPOSER_VDI_DEFAULT_LIBRARY);
127 #endif // COMPOSER_VDI_DEFAULT_LIBRARY_ENABLE
128         libHandle_ = dlopen(DISPLAY_COMPOSER_VDI_LIBRARY, RTLD_LAZY);
129         DISPLAY_LOGD("composer load vendor vdi library: %{public}s", DISPLAY_COMPOSER_VDI_LIBRARY);
130 #ifdef COMPOSER_VDI_DEFAULT_LIBRARY_ENABLE
131     } else {
132         DISPLAY_LOGD("composer load vendor vdi default library: %{public}s", DISPLAY_COMPOSER_VDI_DEFAULT_LIBRARY);
133     }
134 #endif // COMPOSER_VDI_DEFAULT_LIBRARY_ENABLE
135     CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
136 
137     return HDF_SUCCESS;
138 }
139 
LoadVdiV1_0()140 int32_t DisplayComposerService::LoadVdiV1_0()
141 {
142     CreateComposerVdiFunc createVdiFunc = nullptr;
143     const char* errStr = nullptr;
144 
145     createVdiFunc = reinterpret_cast<CreateComposerVdiFunc>(dlsym(libHandle_, "CreateComposerVdi"));
146     if (createVdiFunc == nullptr) {
147         errStr = dlerror();
148         if (errStr != nullptr) {
149             DISPLAY_LOGE("CreateVdiFuncV1_0 dlsym error: %{public}s", errStr);
150         }
151         return HDF_FAILURE;
152     }
153 
154     destroyVdiFunc_ = reinterpret_cast<DestroyComposerVdiFunc>(dlsym(libHandle_, "DestroyComposerVdi"));
155     if (destroyVdiFunc_ == nullptr) {
156         errStr = dlerror();
157         if (errStr != nullptr) {
158             DISPLAY_LOGE("DestroyVdiFuncV1_0 dlsym error: %{public}s", errStr);
159         }
160         return HDF_FAILURE;
161     }
162 
163     vdiImpl_ = createVdiFunc();
164     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
165     cacheMgr_ = DeviceCacheManager::GetInstance();
166     CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
167     cmdResponser_ = V1_2::HdiDisplayCmdResponser::Create(vdiImpl_, cacheMgr_);
168     CHECK_NULLPOINTER_RETURN_VALUE(cmdResponser_, HDF_FAILURE);
169     return HDF_SUCCESS;
170 }
171 
LoadVdiV1_1()172 int32_t DisplayComposerService::LoadVdiV1_1()
173 {
174     CreateComposerVdiFuncV1_1 createVdiFunc = nullptr;
175     const char* errStr = nullptr;
176 
177     createVdiFunc = reinterpret_cast<CreateComposerVdiFuncV1_1>(dlsym(libHandle_, "CreateComposerVdiV1_1"));
178     if (createVdiFunc == nullptr) {
179         errStr = dlerror();
180         if (errStr != nullptr) {
181             DISPLAY_LOGE("CreateVdiFuncV1_1 dlsym error: %{public}s", errStr);
182         }
183         return HDF_FAILURE;
184     }
185 
186     destroyVdiFuncV1_1_ = reinterpret_cast<DestroyComposerVdiFuncV1_1>(dlsym(libHandle_, "DestroyComposerVdiV1_1"));
187     if (destroyVdiFuncV1_1_ == nullptr) {
188         errStr = dlerror();
189         if (errStr != nullptr) {
190             DISPLAY_LOGE("DestroyVdiFuncV1_1 dlsym error: %{public}s", errStr);
191         }
192         return HDF_FAILURE;
193     }
194 
195     vdiImplV1_1_ = createVdiFunc();
196     CHECK_NULLPOINTER_RETURN_VALUE(vdiImplV1_1_, HDF_FAILURE);
197     vdiImpl_ = dynamic_cast<IDisplayComposerVdi*>(vdiImplV1_1_);
198     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
199     cacheMgr_ = DeviceCacheManager::GetInstance();
200     CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
201     cmdResponserV1_1_ = V1_2::HdiDisplayCmdResponser_1_1::CreateV1_1(vdiImplV1_1_, cacheMgr_);
202     CHECK_NULLPOINTER_RETURN_VALUE(cmdResponserV1_1_, HDF_FAILURE);
203     return HDF_SUCCESS;
204 }
205 
OnHotPlug(uint32_t outputId,bool connected,void * data)206 void DisplayComposerService::OnHotPlug(uint32_t outputId, bool connected, void* data)
207 {
208     if (data == nullptr) {
209         DISPLAY_LOGE("cb data is nullptr");
210         return;
211     }
212 
213     auto cacheMgr = reinterpret_cast<DisplayComposerService*>(data)->cacheMgr_;
214     if (cacheMgr == nullptr) {
215         DISPLAY_LOGE("CacheMgr_ is nullptr");
216         return;
217     }
218     if (connected) {
219         std::lock_guard<std::mutex> lock(cacheMgr->GetCacheMgrMutex());
220         // Add new device cache
221         if (cacheMgr->AddDeviceCache(outputId) != HDF_SUCCESS) {
222             DISPLAY_LOGE("Add device cache failed");
223         }
224     } else {
225         std::lock_guard<std::mutex> lock(cacheMgr->GetCacheMgrMutex());
226         // Del new device cache
227         if (cacheMgr->RemoveDeviceCache(outputId) != HDF_SUCCESS) {
228             DISPLAY_LOGE("Del device cache failed");
229         }
230     }
231 
232     sptr<IHotPlugCallback> remoteCb = reinterpret_cast<DisplayComposerService*>(data)->hotPlugCb_;
233     if (remoteCb == nullptr) {
234         DISPLAY_LOGE("hotPlugCb_ is nullptr");
235         return;
236     }
237     remoteCb->OnHotPlug(outputId, connected);
238 }
239 
OnVBlank(unsigned int sequence,uint64_t ns,void * data)240 void DisplayComposerService::OnVBlank(unsigned int sequence, uint64_t ns, void* data)
241 {
242     if (data == nullptr) {
243         DISPLAY_LOGE("cb data is nullptr");
244         return;
245     }
246 
247     IVBlankCallback* remoteCb = reinterpret_cast<IVBlankCallback*>(data);
248     if (remoteCb == nullptr) {
249         DISPLAY_LOGE("vblankCb_ is nullptr");
250         return;
251     }
252     remoteCb->OnVBlank(sequence, ns);
253 }
254 
RegHotPlugCallback(const sptr<IHotPlugCallback> & cb)255 int32_t DisplayComposerService::RegHotPlugCallback(const sptr<IHotPlugCallback>& cb)
256 {
257     DISPLAY_TRACE;
258 
259     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
260     hotPlugCb_ = cb;
261     int32_t ret = vdiImpl_->RegHotPlugCallback(OnHotPlug, this);
262     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
263     return ret;
264 }
265 
SetClientBufferCacheCount(uint32_t devId,uint32_t count)266 int32_t DisplayComposerService::SetClientBufferCacheCount(uint32_t devId, uint32_t count)
267 {
268     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
269     CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
270     std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
271     DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
272     DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
273 
274     DISPLAY_CHK_RETURN(devCache->SetClientBufferCacheCount(count) != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE("fail"));
275     return HDF_SUCCESS;
276 }
277 
GetDisplayCapability(uint32_t devId,DisplayCapability & info)278 int32_t DisplayComposerService::GetDisplayCapability(uint32_t devId, DisplayCapability& info)
279 {
280     DISPLAY_TRACE;
281 
282     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
283     int32_t ret =  vdiImpl_->GetDisplayCapability(devId, info);
284     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
285     return HDF_SUCCESS;
286 }
287 
GetDisplaySupportedModes(uint32_t devId,std::vector<DisplayModeInfo> & modes)288 int32_t DisplayComposerService::GetDisplaySupportedModes(uint32_t devId, std::vector<DisplayModeInfo>& modes)
289 {
290     DISPLAY_TRACE;
291 
292     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
293     int32_t ret =  vdiImpl_->GetDisplaySupportedModes(devId, modes);
294     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
295     return ret;
296 }
297 
GetDisplayMode(uint32_t devId,uint32_t & modeId)298 int32_t DisplayComposerService::GetDisplayMode(uint32_t devId, uint32_t& modeId)
299 {
300     DISPLAY_TRACE;
301 
302     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
303     int32_t ret =  vdiImpl_->GetDisplayMode(devId, modeId);
304     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
305     return ret;
306 }
307 
SetDisplayMode(uint32_t devId,uint32_t modeId)308 int32_t DisplayComposerService::SetDisplayMode(uint32_t devId, uint32_t modeId)
309 {
310     DISPLAY_TRACE;
311 
312     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
313     int32_t ret =  vdiImpl_->SetDisplayMode(devId, modeId);
314     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
315     return ret;
316 }
317 
GetDisplayPowerStatus(uint32_t devId,V1_0::DispPowerStatus & status)318 int32_t DisplayComposerService::GetDisplayPowerStatus(uint32_t devId, V1_0::DispPowerStatus& status)
319 {
320     DISPLAY_TRACE;
321 
322     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
323     int32_t ret = vdiImpl_->GetDisplayPowerStatus(devId, status);
324     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
325     return ret;
326 }
327 
SetDisplayPowerStatus(uint32_t devId,V1_0::DispPowerStatus status)328 int32_t DisplayComposerService::SetDisplayPowerStatus(uint32_t devId, V1_0::DispPowerStatus status)
329 {
330     DISPLAY_TRACE;
331 
332     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
333     int32_t ret = vdiImpl_->SetDisplayPowerStatus(devId, status);
334     DISPLAY_LOGI("devid: %{public}u, status: %{public}u, vdi return %{public}d", devId, status, ret);
335     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
336     return ret;
337 }
338 
GetDisplayBacklight(uint32_t devId,uint32_t & level)339 int32_t DisplayComposerService::GetDisplayBacklight(uint32_t devId, uint32_t& level)
340 {
341     DISPLAY_TRACE;
342 
343     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
344     int32_t ret = vdiImpl_->GetDisplayBacklight(devId, level);
345     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_SUCCESS, level = currentBacklightLevel_);
346     return ret;
347 }
348 
SetDisplayBacklight(uint32_t devId,uint32_t level)349 int32_t DisplayComposerService::SetDisplayBacklight(uint32_t devId, uint32_t level)
350 {
351     DISPLAY_TRACE;
352 
353     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
354     int32_t ret = vdiImpl_->SetDisplayBacklight(devId, level);
355     DISPLAY_LOGD("devid: %{public}u, level: %{public}u, vdi return %{public}d", devId, level, ret);
356     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
357     currentBacklightLevel_ = level;
358     return ret;
359 }
360 
GetDisplayProperty(uint32_t devId,uint32_t id,uint64_t & value)361 int32_t DisplayComposerService::GetDisplayProperty(uint32_t devId, uint32_t id, uint64_t& value)
362 {
363     DISPLAY_TRACE;
364 
365     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
366     int32_t ret = vdiImpl_->GetDisplayProperty(devId, id, value);
367     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
368     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
369     return ret;
370 }
371 
SetHardwareCursorPosition(uint32_t devId,int32_t x,int32_t y)372 int32_t DisplayComposerService::SetHardwareCursorPosition(uint32_t devId, int32_t x, int32_t y)
373 {
374     DISPLAY_TRACE;
375 
376     CHECK_NULLPOINTER_RETURN_VALUE(vdiImplV1_1_, HDF_ERR_NOT_SUPPORT);
377     int32_t ret = vdiImplV1_1_->SetHardwareCursorPosition(devId, x, y);
378     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
379     return ret;
380 }
381 
EnableHardwareCursorStats(uint32_t devId,bool enable)382 int32_t DisplayComposerService::EnableHardwareCursorStats(uint32_t devId, bool enable)
383 {
384     DISPLAY_TRACE;
385 
386     CHECK_NULLPOINTER_RETURN_VALUE(vdiImplV1_1_, HDF_ERR_NOT_SUPPORT);
387     int32_t ret = vdiImplV1_1_->EnableHardwareCursorStats(devId, enable);
388     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
389     return ret;
390 }
391 
GetHardwareCursorStats(uint32_t devId,uint32_t & frameCount,uint32_t & vsyncCount)392 int32_t DisplayComposerService::GetHardwareCursorStats(uint32_t devId, uint32_t& frameCount, uint32_t& vsyncCount)
393 {
394     DISPLAY_TRACE;
395 
396     CHECK_NULLPOINTER_RETURN_VALUE(vdiImplV1_1_, HDF_ERR_NOT_SUPPORT);
397     int32_t ret = vdiImplV1_1_->GetHardwareCursorStats(devId, frameCount, vsyncCount);
398     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
399     return ret;
400 }
401 
SetDisplayClientCrop(uint32_t devId,const IRect & rect)402 int32_t DisplayComposerService::SetDisplayClientCrop(uint32_t devId, const IRect& rect)
403 {
404     DISPLAY_TRACE;
405 
406     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
407     int32_t ret = vdiImpl_->SetDisplayClientCrop(devId, rect);
408     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
409     return ret;
410 }
411 
SetDisplayVsyncEnabled(uint32_t devId,bool enabled)412 int32_t DisplayComposerService::SetDisplayVsyncEnabled(uint32_t devId, bool enabled)
413 {
414     DISPLAY_TRACE;
415 
416     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
417     int32_t ret = vdiImpl_->SetDisplayVsyncEnabled(devId, enabled);
418     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
419     return ret;
420 }
421 
RegDisplayVBlankCallback(uint32_t devId,const sptr<IVBlankCallback> & cb)422 int32_t DisplayComposerService::RegDisplayVBlankCallback(uint32_t devId, const sptr<IVBlankCallback>& cb)
423 {
424     DISPLAY_TRACE;
425 
426     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
427     int32_t ret = vdiImpl_->RegDisplayVBlankCallback(devId, OnVBlank, cb.GetRefPtr());
428     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
429     vBlankCb_ = cb;
430     return ret;
431 }
432 
GetDisplayReleaseFence(uint32_t devId,std::vector<uint32_t> & layers,std::vector<sptr<HdifdParcelable>> & fences)433 int32_t DisplayComposerService::GetDisplayReleaseFence(
434     uint32_t devId, std::vector<uint32_t>& layers, std::vector<sptr<HdifdParcelable>>& fences)
435 {
436     DISPLAY_TRACE;
437 
438     std::vector<int32_t> outFences;
439     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
440     int32_t ret = vdiImpl_->GetDisplayReleaseFence(devId, layers, outFences);
441     for (uint i = 0; i < outFences.size(); i++) {
442         int32_t dupFd = outFences[i];
443         sptr<HdifdParcelable> hdifd(new HdifdParcelable());
444         CHECK_NULLPOINTER_RETURN_VALUE(hdifd, HDF_FAILURE);
445         hdifd->Init(dupFd);
446         fences.push_back(hdifd);
447     }
448     return ret;
449 }
450 
CreateVirtualDisplay(uint32_t width,uint32_t height,int32_t & format,uint32_t & devId)451 int32_t DisplayComposerService::CreateVirtualDisplay(uint32_t width, uint32_t height, int32_t& format, uint32_t& devId)
452 {
453     DISPLAY_TRACE;
454 
455     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
456     int32_t ret = vdiImpl_->CreateVirtualDisplay(width, height, format, devId);
457     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
458     return ret;
459 }
460 
DestroyVirtualDisplay(uint32_t devId)461 int32_t DisplayComposerService::DestroyVirtualDisplay(uint32_t devId)
462 {
463     DISPLAY_TRACE;
464 
465     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
466     int32_t ret = vdiImpl_->DestroyVirtualDisplay(devId);
467     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
468     return ret;
469 }
470 
SetVirtualDisplayBuffer(uint32_t devId,const sptr<NativeBuffer> & buffer,const sptr<HdifdParcelable> & fence)471 int32_t DisplayComposerService::SetVirtualDisplayBuffer(
472     uint32_t devId, const sptr<NativeBuffer>& buffer, const sptr<HdifdParcelable>& fence)
473 {
474     DISPLAY_TRACE;
475 
476     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
477     CHECK_NULLPOINTER_RETURN_VALUE(fence, HDF_FAILURE);
478     BufferHandle* handle = buffer->GetBufferHandle();
479     int32_t inFence = fence->GetFd();
480     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
481     CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
482     int32_t ret = vdiImpl_->SetVirtualDisplayBuffer(devId, *handle, inFence);
483     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
484     return ret;
485 }
486 
SetDisplayProperty(uint32_t devId,uint32_t id,uint64_t value)487 int32_t DisplayComposerService::SetDisplayProperty(uint32_t devId, uint32_t id, uint64_t value)
488 {
489     DISPLAY_TRACE;
490 
491     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
492     int32_t ret = vdiImpl_->SetDisplayProperty(devId, id, value);
493     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
494     return ret;
495 }
496 
CreateLayer(uint32_t devId,const LayerInfo & layerInfo,uint32_t cacheCount,uint32_t & layerId)497 int32_t DisplayComposerService::CreateLayer(uint32_t devId, const LayerInfo& layerInfo, uint32_t cacheCount,
498     uint32_t& layerId)
499 {
500     DISPLAY_TRACE;
501 
502     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
503     int32_t ret = vdiImpl_->CreateLayer(devId, layerInfo, layerId);
504     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
505 
506     CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
507     std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
508     DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
509     DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
510 
511     return devCache->AddLayerCache(layerId, cacheCount);
512 }
513 
DestroyLayer(uint32_t devId,uint32_t layerId)514 int32_t DisplayComposerService::DestroyLayer(uint32_t devId, uint32_t layerId)
515 {
516     DISPLAY_TRACE;
517     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
518     int32_t ret = vdiImpl_->DestroyLayer(devId, layerId);
519     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
520 
521     CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
522     std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
523     DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
524     DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
525 
526     return devCache->RemoveLayerCache(layerId);
527 }
528 
RegSeamlessChangeCallback(const sptr<ISeamlessChangeCallback> & cb)529 int32_t DisplayComposerService::RegSeamlessChangeCallback(const sptr<ISeamlessChangeCallback>& cb)
530 {
531     DISPLAY_TRACE;
532     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
533     int32_t ret = vdiImplV1_1_->RegSeamlessChangeCallback(OnSeamlessChange, this);
534     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
535     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
536     if (ret == HDF_SUCCESS) {
537         seamlessChangeCb_ = cb;
538     }
539     return ret;
540 }
541 
GetDisplaySupportedModesExt(uint32_t devId,std::vector<DisplayModeInfoExt> & modes)542 int32_t DisplayComposerService::GetDisplaySupportedModesExt(uint32_t devId, std::vector<DisplayModeInfoExt>& modes)
543 {
544     DISPLAY_TRACE;
545     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
546     int32_t ret = vdiImplV1_1_->GetDisplaySupportedModesExt(devId, modes);
547     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
548     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
549     return ret;
550 }
551 
OnMode(uint32_t modeId,uint64_t vBlankPeriod,void * data)552 void DisplayComposerService::OnMode(uint32_t modeId, uint64_t vBlankPeriod, void* data)
553 {
554     if (data == nullptr) {
555         DISPLAY_LOGE("data is nullptr");
556         return;
557     }
558 
559     sptr<IModeCallback> remoteCb = reinterpret_cast<DisplayComposerService*>(data)->modeCb_;
560     if (remoteCb == nullptr) {
561         DISPLAY_LOGE("remoteCb is nullptr");
562         return;
563     }
564     remoteCb->OnMode(modeId, vBlankPeriod);
565 }
566 
SetDisplayModeAsync(uint32_t devId,uint32_t modeId,const sptr<IModeCallback> & cb)567 int32_t DisplayComposerService::SetDisplayModeAsync(uint32_t devId, uint32_t modeId, const sptr<IModeCallback>& cb)
568 {
569     DISPLAY_TRACE;
570     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
571     int32_t ret = vdiImplV1_1_->SetDisplayModeAsync(devId, modeId, OnMode, this);
572     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
573     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
574     if (ret == HDF_SUCCESS) {
575         modeCb_ = cb;
576     }
577     return ret;
578 }
579 
GetDisplayVBlankPeriod(uint32_t devId,uint64_t & period)580 int32_t DisplayComposerService::GetDisplayVBlankPeriod(uint32_t devId, uint64_t& period)
581 {
582     DISPLAY_TRACE;
583     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
584     int32_t ret = vdiImplV1_1_->GetDisplayVBlankPeriod(devId, period);
585     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
586     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
587     return ret;
588 }
589 
OnSeamlessChange(uint32_t devId,void * data)590 void DisplayComposerService::OnSeamlessChange(uint32_t devId, void* data)
591 {
592     if (data == nullptr) {
593         DISPLAY_LOGE("data is nullptr");
594         return;
595     }
596 
597     sptr<ISeamlessChangeCallback> remoteCb = reinterpret_cast<DisplayComposerService*>(data)->seamlessChangeCb_;
598     if (remoteCb == nullptr) {
599         DISPLAY_LOGE("remoteCb is nullptr");
600         return;
601     }
602     remoteCb->OnSeamlessChange(devId);
603 }
604 
InitCmdRequest(const std::shared_ptr<SharedMemQueue<int32_t>> & request)605 int32_t DisplayComposerService::InitCmdRequest(const std::shared_ptr<SharedMemQueue<int32_t>>& request)
606 {
607     CHECK_NULLPOINTER_RETURN_VALUE(request, HDF_FAILURE);
608     int32_t ret = HDF_FAILURE;
609 
610     if (cmdResponserV1_1_ != nullptr) {
611         ret = cmdResponserV1_1_->InitCmdRequest(request);
612     } else if (cmdResponser_ != nullptr) {
613         ret = cmdResponser_->InitCmdRequest(request);
614     }
615     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
616     return ret;
617 }
618 
CmdRequest(uint32_t inEleCnt,const std::vector<HdifdInfo> & inFds,uint32_t & outEleCnt,std::vector<HdifdInfo> & outFds)619 int32_t DisplayComposerService::CmdRequest(
620     uint32_t inEleCnt, const std::vector<HdifdInfo>& inFds, uint32_t& outEleCnt, std::vector<HdifdInfo>& outFds)
621 {
622     int32_t ret = HDF_FAILURE;
623 
624     if (cmdResponserV1_1_ != nullptr) {
625         ret = cmdResponserV1_1_->CmdRequest(inEleCnt, inFds, outEleCnt, outFds);
626     } else if (cmdResponser_ != nullptr) {
627         ret = cmdResponser_->CmdRequest(inEleCnt, inFds, outEleCnt, outFds);
628     }
629     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
630     return ret;
631 }
632 
GetCmdReply(std::shared_ptr<SharedMemQueue<int32_t>> & reply)633 int32_t DisplayComposerService::GetCmdReply(std::shared_ptr<SharedMemQueue<int32_t>>& reply)
634 {
635     int32_t ret = HDF_FAILURE;
636 
637     if (cmdResponserV1_1_ != nullptr) {
638         ret = cmdResponserV1_1_->GetCmdReply(reply);
639     } else if (cmdResponser_ != nullptr) {
640         ret = cmdResponser_->GetCmdReply(reply);
641     }
642     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, HDF_FAILURE, DISPLAY_LOGE(" fail"));
643     return ret;
644 }
645 
SetLayerPerFrameParameter(uint32_t devId,uint32_t layerId,const std::string & key,const std::vector<int8_t> & value)646 int32_t DisplayComposerService::SetLayerPerFrameParameter(uint32_t devId, uint32_t layerId, const std::string& key,
647     const std::vector<int8_t>& value)
648 {
649     DISPLAY_TRACE;
650     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
651     int32_t ret = vdiImplV1_1_->SetLayerPerFrameParameter(devId, layerId, key, value);
652     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
653     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
654     return ret;
655 }
656 
GetSupportedLayerPerFrameParameterKey(std::vector<std::string> & keys)657 int32_t DisplayComposerService::GetSupportedLayerPerFrameParameterKey(std::vector<std::string>& keys)
658 {
659     DISPLAY_TRACE;
660     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
661     int32_t ret = vdiImplV1_1_->GetSupportedLayerPerFrameParameterKey(keys);
662     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
663     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
664     return ret;
665 }
666 
SetDisplayOverlayResolution(uint32_t devId,uint32_t width,uint32_t height)667 int32_t DisplayComposerService::SetDisplayOverlayResolution(uint32_t devId, uint32_t width, uint32_t height)
668 {
669     DISPLAY_TRACE;
670     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
671     int32_t ret = vdiImplV1_1_->SetDisplayOverlayResolution(devId, width, height);
672     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
673     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
674     return ret;
675 }
676 
OnRefresh(uint32_t devId,void * data)677 void DisplayComposerService::OnRefresh(uint32_t devId, void *data)
678 {
679     if (data == nullptr) {
680         DISPLAY_LOGE("cb data is nullptr");
681         return;
682     }
683 
684     sptr<IRefreshCallback> remoteCb = reinterpret_cast<DisplayComposerService*>(data)->refreshCb_;
685     if (remoteCb == nullptr) {
686         DISPLAY_LOGE("remoteCb is nullptr");
687         return;
688     }
689     remoteCb->OnRefresh(devId);
690 }
691 
RegRefreshCallback(const sptr<IRefreshCallback> & cb)692 int32_t DisplayComposerService::RegRefreshCallback(const sptr<IRefreshCallback>& cb)
693 {
694     DISPLAY_TRACE;
695     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
696     int32_t ret = vdiImplV1_1_->RegRefreshCallback(OnRefresh, this);
697     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
698     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
699     if (ret == HDF_SUCCESS) {
700         refreshCb_ = cb;
701     }
702     return ret;
703 }
704 
GetDisplaySupportedColorGamuts(uint32_t devId,std::vector<ColorGamut> & gamuts)705 int32_t DisplayComposerService::GetDisplaySupportedColorGamuts(uint32_t devId, std::vector<ColorGamut>& gamuts)
706 {
707     DISPLAY_TRACE;
708     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
709     int32_t ret = vdiImplV1_1_->GetDisplaySupportedColorGamuts(devId, gamuts);
710     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
711     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
712     return ret;
713 }
714 
GetHDRCapabilityInfos(uint32_t devId,HDRCapability & info)715 int32_t DisplayComposerService::GetHDRCapabilityInfos(uint32_t devId, HDRCapability& info)
716 {
717     DISPLAY_TRACE;
718     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
719     int32_t ret = vdiImplV1_1_->GetHDRCapabilityInfos(devId, info);
720     DISPLAY_CHK_RETURN(ret == DISPLAY_NOT_SUPPORT, HDF_ERR_NOT_SUPPORT);
721     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
722     return ret;
723 }
724 
OnVBlankIdleCallback(uint32_t devId,uint64_t ns,void * data)725 void DisplayComposerService::OnVBlankIdleCallback(uint32_t devId, uint64_t ns, void* data)
726 {
727     if (data == nullptr) {
728         DISPLAY_LOGE("cb data is nullptr");
729         return;
730     }
731 
732     sptr<IVBlankIdleCallback> remoteCb = reinterpret_cast<DisplayComposerService*>(data)->VBlankIdleCb_;
733 
734     if (remoteCb == nullptr) {
735         DISPLAY_LOGE("VBlankIdleCb_ is nullptr");
736         return;
737     }
738     remoteCb->OnVBlankIdleCallback(devId, ns);
739 }
740 
RegDisplayVBlankIdleCallback(const sptr<IVBlankIdleCallback> & cb)741 int32_t DisplayComposerService::RegDisplayVBlankIdleCallback(const sptr<IVBlankIdleCallback>& cb)
742 {
743     DISPLAY_TRACE;
744     DISPLAY_CHK_RETURN(vdiImplV1_1_ == nullptr, HDF_ERR_NOT_SUPPORT);
745     VBlankIdleCb_ = cb;
746     int32_t ret = vdiImplV1_1_->RegDisplayVBlankIdleCallback(OnVBlankIdleCallback, this);
747     DISPLAY_CHK_RETURN(ret != HDF_SUCCESS && ret != HDF_ERR_NOT_SUPPORT, HDF_FAILURE, DISPLAY_LOGE(" fail"));
748     return ret;
749 }
750 
ClearClientBuffer(uint32_t devId)751 int32_t DisplayComposerService::ClearClientBuffer(uint32_t devId)
752 {
753     DISPLAY_LOGI("enter, devId %{public}u", devId);
754     CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
755     std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
756     DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
757     DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
758 
759     return devCache->ClearClientCache();
760 }
761 
ClearLayerBuffer(uint32_t devId,uint32_t layerId)762 int32_t DisplayComposerService::ClearLayerBuffer(uint32_t devId, uint32_t layerId)
763 {
764     DISPLAY_LOGI("enter, devId %{public}u, layerId %{public}u", devId, layerId);
765     CHECK_NULLPOINTER_RETURN_VALUE(cacheMgr_, HDF_FAILURE);
766     std::lock_guard<std::mutex> lock(cacheMgr_->GetCacheMgrMutex());
767     DeviceCache* devCache = cacheMgr_->DeviceCacheInstance(devId);
768     DISPLAY_CHK_RETURN(devCache == nullptr, HDF_FAILURE, DISPLAY_LOGE("fail"));
769 
770     return devCache->ClearLayerBuffer(layerId);
771 }
772 
773 } // namespace Composer
774 } // namespace Display
775 } // namespace HDI
776 } // namespace OHOS
777