• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <securec.h>
17 
18 #include <shared_mutex>
19 
20 #include "display.h"
21 #include "display_info.h"
22 #include "display_manager.h"
23 #include "oh_display_capture.h"
24 #include "oh_display_manager.h"
25 #include "oh_display_manager_inner.h"
26 #include "pixelmap_native_impl.h"
27 #include "window_manager_hilog.h"
28 
29 using namespace OHOS;
30 using namespace Rosen;
31 
32 namespace OHOS {
33 namespace {
34 class AvailableAreaChangeListener : public DisplayManager::IAvailableAreaListener {
35 public:
AvailableAreaChangeListener(OH_NativeDisplayManager_AvailableAreaChangeCallback availableAreaChangeFunc)36     explicit AvailableAreaChangeListener(OH_NativeDisplayManager_AvailableAreaChangeCallback availableAreaChangeFunc)
37     {
38         innerAvailableAreaChangeFunc_ = availableAreaChangeFunc;
39     }
40 
OnAvailableAreaChanged(DMRect area)41     void OnAvailableAreaChanged(DMRect area)
42     {
43         if (innerAvailableAreaChangeFunc_ == nullptr) {
44             TLOGE(WmsLogTag::DMS, "[DMNDK] callback is nullptr");
45             return;
46         }
47         DisplayId displayId = DisplayManager::GetInstance().GetDefaultDisplayId();
48         innerAvailableAreaChangeFunc_(displayId);
49     }
50 
GetAvailableAreaChangeInnerFunc()51     OH_NativeDisplayManager_AvailableAreaChangeCallback GetAvailableAreaChangeInnerFunc()
52     {
53         return innerAvailableAreaChangeFunc_;
54     }
55 
56 private:
57     OH_NativeDisplayManager_AvailableAreaChangeCallback innerAvailableAreaChangeFunc_;
58 };
59 
60 class DisplayAddListener : public DisplayManager::IDisplayListener {
61 public:
DisplayAddListener(OH_NativeDisplayManager_DisplayAddCallback displayAddFunc)62     explicit DisplayAddListener(OH_NativeDisplayManager_DisplayAddCallback displayAddFunc)
63     {
64         innerDisplayAddFunc_ = displayAddFunc;
65     }
66 
OnCreate(DisplayId displayId)67     void OnCreate(DisplayId displayId)
68     {
69         if (innerDisplayAddFunc_ == nullptr) {
70             TLOGE(WmsLogTag::DMS, "[DMNDK] AddListener callback is nullptr");
71             return;
72         }
73         TLOGI(WmsLogTag::DMS, "[DMNDK] AddListener callback displayId=%{public}" PRIu64, displayId);
74         innerDisplayAddFunc_(displayId);
75     }
76 
OnDestroy(DisplayId displayId)77     void OnDestroy(DisplayId displayId)
78     {
79         TLOGI(WmsLogTag::DMS, "[DMNDK] AddListener current not support delete callback.");
80     }
81 
OnChange(DisplayId displayId)82     void OnChange(DisplayId displayId)
83     {
84         TLOGI(WmsLogTag::DMS, "[DMNDK] AddListener current not support Change callback.");
85     }
86 
GetDisplayAddInnerFunc()87     OH_NativeDisplayManager_DisplayAddCallback GetDisplayAddInnerFunc()
88     {
89         return innerDisplayAddFunc_;
90     }
91 
92 private:
93     OH_NativeDisplayManager_DisplayAddCallback innerDisplayAddFunc_;
94 };
95 
96 class DisplayRemoveListener : public DisplayManager::IDisplayListener {
97 public:
DisplayRemoveListener(OH_NativeDisplayManager_DisplayRemoveCallback displayRemoveFunc)98     explicit DisplayRemoveListener(OH_NativeDisplayManager_DisplayRemoveCallback displayRemoveFunc)
99     {
100         innerDisplayRemoveFunc_ = displayRemoveFunc;
101     }
102 
OnCreate(DisplayId displayId)103     void OnCreate(DisplayId displayId)
104     {
105         TLOGI(WmsLogTag::DMS, "[DMNDK] RemoveListener current not support create callback.");
106     }
107 
OnDestroy(DisplayId displayId)108     void OnDestroy(DisplayId displayId)
109     {
110         if (innerDisplayRemoveFunc_ == nullptr) {
111             TLOGE(WmsLogTag::DMS, "[DMNDK] RemoveListener callback is nullptr");
112             return;
113         }
114         TLOGI(WmsLogTag::DMS, "[DMNDK] RemoveListener callback displayId=%{public}" PRIu64, displayId);
115         innerDisplayRemoveFunc_(displayId);
116     }
117 
OnChange(DisplayId displayId)118     void OnChange(DisplayId displayId)
119     {
120         TLOGI(WmsLogTag::DMS, "[DMNDK] RemoveListener current not support Change callback.");
121     }
122 
GetDisplayRemoveInnerFunc()123     OH_NativeDisplayManager_DisplayRemoveCallback GetDisplayRemoveInnerFunc()
124     {
125         return innerDisplayRemoveFunc_;
126     }
127 
128 private:
129     OH_NativeDisplayManager_DisplayRemoveCallback innerDisplayRemoveFunc_;
130 };
131 
132 std::shared_mutex availableAreaChangeMutex;
133 std::unordered_map<uint32_t, sptr<AvailableAreaChangeListener>> availableAreaChangeListenerMap;
134 std::shared_mutex displayAddMutex;
135 std::unordered_map<uint32_t, sptr<DisplayAddListener>> displayAddListenerMap;
136 std::shared_mutex displayRemoveMutex;
137 std::unordered_map<uint32_t, sptr<DisplayRemoveListener>> displayRemoveListenerMap;
138 
CheckAvailableAreaChangeHasRegistered(const sptr<AvailableAreaChangeListener> & availableAreaChangeListener)139 bool CheckAvailableAreaChangeHasRegistered(const sptr<AvailableAreaChangeListener>& availableAreaChangeListener)
140 {
141     std::unique_lock<std::shared_mutex> lock(availableAreaChangeMutex);
142     for (const auto& iter : availableAreaChangeListenerMap) {
143         if (iter.second->GetAvailableAreaChangeInnerFunc() ==
144             availableAreaChangeListener->GetAvailableAreaChangeInnerFunc()) {
145             return true;
146         }
147     }
148     return false;
149 }
150 
CheckDisplayAddHasRegistered(const sptr<DisplayAddListener> & displayAddListener)151 bool CheckDisplayAddHasRegistered(const sptr<DisplayAddListener>& displayAddListener)
152 {
153     std::unique_lock<std::shared_mutex> lock(displayAddMutex);
154     for (const auto& iter : displayAddListenerMap) {
155         if (iter.second->GetDisplayAddInnerFunc() == displayAddListener->GetDisplayAddInnerFunc()) {
156             return true;
157         }
158     }
159     return false;
160 }
161 
CheckDisplayRemoveHasRegistered(const sptr<DisplayRemoveListener> & displayRemoveListener)162 bool CheckDisplayRemoveHasRegistered(const sptr<DisplayRemoveListener>& displayRemoveListener)
163 {
164     std::unique_lock<std::shared_mutex> lock(displayRemoveMutex);
165     for (const auto& iter : displayRemoveListenerMap) {
166         if (iter.second->GetDisplayRemoveInnerFunc() == displayRemoveListener->GetDisplayRemoveInnerFunc()) {
167             return true;
168         }
169     }
170     return false;
171 }
172 }
173 }
174 
175 class OH_DisplayModeChangeListener : public DisplayManager::IDisplayModeListener {
176 private:
177     OH_NativeDisplayManager_FoldDisplayModeChangeCallback innerDisplayModeChangeFunc_;
178 public:
OH_DisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeFunc)179     explicit OH_DisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeFunc)
180     {
181         innerDisplayModeChangeFunc_ = displayModeChangeFunc;
182     }
OnDisplayModeChanged(FoldDisplayMode displayMode)183     void OnDisplayModeChanged(FoldDisplayMode displayMode)
184     {
185         if (innerDisplayModeChangeFunc_ == NULL) {
186             TLOGI(WmsLogTag::DMS, "[DMNDK] callback is null");
187             return;
188         }
189         TLOGI(WmsLogTag::DMS, "[DMNDK] displayMode callback displayMode=%{public}d", displayMode);
190         innerDisplayModeChangeFunc_(static_cast<NativeDisplayManager_FoldDisplayMode>(displayMode));
191     }
192 };
193 
194 class OH_DisplayChangeListener : public DisplayManager::IDisplayListener {
195 private:
196     OH_NativeDisplayManager_DisplayChangeCallback innerDisplayChangeFunc_;
197 public:
OH_DisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeFunc)198     explicit OH_DisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeFunc)
199     {
200         innerDisplayChangeFunc_ = displayChangeFunc;
201     }
OnCreate(DisplayId)202     void OnCreate(DisplayId)
203     {
204         TLOGI(WmsLogTag::DMS, "[DMNDK] current not support create callback.");
205     }
OnDestroy(DisplayId)206     void OnDestroy(DisplayId)
207     {
208         TLOGI(WmsLogTag::DMS, "[DMNDK] current not support delete callback.");
209     }
OnChange(DisplayId displayId)210     void OnChange(DisplayId displayId)
211     {
212         if (innerDisplayChangeFunc_ == NULL) {
213             TLOGI(WmsLogTag::DMS, "[DMNDK] callback is null");
214             return;
215         }
216         TLOGI(WmsLogTag::DMS, "[DMNDK] callback displayId=%{public}" PRIu64, displayId);
217         innerDisplayChangeFunc_(static_cast<uint64_t>(displayId));
218         sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplaySync();
219         if (display != nullptr) {
220             TLOGI(WmsLogTag::DMS, "[DMNDK] callback rotation=%{public}d orientation=%{public}d",
221                 display->GetRotation(), display->GetOrientation());
222         }
223     }
224 };
225 
OH_GetDefaultDisplayInfo()226 static sptr<DisplayInfo> OH_GetDefaultDisplayInfo()
227 {
228     sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplaySync();
229     if (defaultDisplay == nullptr) {
230         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display null.");
231         return nullptr;
232     }
233     auto info = defaultDisplay->GetDisplayInfo();
234     if (info == nullptr) {
235         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
236         return nullptr;
237     }
238     return info;
239 }
240 
OH_NativeDisplayManager_GetDefaultDisplayId(uint64_t * displayId)241 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayId(uint64_t *displayId)
242 {
243     if (displayId == nullptr) {
244         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayId null.");
245         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
246     }
247     auto displayInfo = OH_GetDefaultDisplayInfo();
248     if (displayInfo == nullptr) {
249         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
250         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
251     }
252     *displayId = displayInfo->GetDisplayId();
253     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
254 }
255 
OH_NativeDisplayManager_GetDefaultDisplayWidth(int32_t * displayWidth)256 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayWidth(int32_t *displayWidth)
257 {
258     if (displayWidth == nullptr) {
259         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayWidth null.");
260         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
261     }
262     auto displayInfo = OH_GetDefaultDisplayInfo();
263     if (displayInfo == nullptr) {
264         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
265         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
266     }
267     *displayWidth = displayInfo->GetWidth();
268     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
269 }
270 
OH_NativeDisplayManager_GetDefaultDisplayHeight(int32_t * displayHeight)271 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayHeight(int32_t *displayHeight)
272 {
273     if (displayHeight == nullptr) {
274         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayHeight null.");
275         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
276     }
277     auto displayInfo = OH_GetDefaultDisplayInfo();
278     if (displayInfo == nullptr) {
279         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
280         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
281     }
282     *displayHeight = displayInfo->GetHeight();
283     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
284 }
285 
OH_NativeDisplayManager_GetDefaultDisplayRotation(NativeDisplayManager_Rotation * displayRotation)286 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayRotation(
287     NativeDisplayManager_Rotation *displayRotation)
288 {
289     if (displayRotation == nullptr) {
290         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayRotation null.");
291         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
292     }
293     auto displayInfo = OH_GetDefaultDisplayInfo();
294     if (displayInfo == nullptr) {
295         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
296         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
297     }
298     *displayRotation = static_cast<NativeDisplayManager_Rotation>(displayInfo->GetRotation());
299     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
300 }
301 
OH_NativeDisplayManager_GetDefaultDisplayOrientation(NativeDisplayManager_Orientation * displayOrientation)302 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayOrientation(
303     NativeDisplayManager_Orientation *displayOrientation)
304 {
305     if (displayOrientation == nullptr) {
306         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayOrientation null.");
307         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
308     }
309     auto displayInfo = OH_GetDefaultDisplayInfo();
310     if (displayInfo == nullptr) {
311         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
312         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
313     }
314     *displayOrientation = static_cast<NativeDisplayManager_Orientation>(displayInfo->GetDisplayOrientation());
315     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
316 }
317 
OH_NativeDisplayManager_GetDefaultDisplayVirtualPixelRatio(float * virtualPixel)318 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayVirtualPixelRatio(float *virtualPixel)
319 {
320     if (virtualPixel == nullptr) {
321         TLOGE(WmsLogTag::DMS, "[DMNDK] input virtualPixel null.");
322         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
323     }
324     auto displayInfo = OH_GetDefaultDisplayInfo();
325     if (displayInfo == nullptr) {
326         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
327         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
328     }
329     *virtualPixel = displayInfo->GetVirtualPixelRatio();
330     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
331 }
332 
OH_NativeDisplayManager_GetDefaultDisplayRefreshRate(uint32_t * refreshRate)333 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayRefreshRate(uint32_t *refreshRate)
334 {
335     if (refreshRate == nullptr) {
336         TLOGE(WmsLogTag::DMS, "[DMNDK] input refreshRate null.");
337         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
338     }
339     auto displayInfo = OH_GetDefaultDisplayInfo();
340     if (displayInfo == nullptr) {
341         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
342         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
343     }
344     *refreshRate = displayInfo->GetRefreshRate();
345     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
346 }
347 
OH_NativeDisplayManager_GetDefaultDisplayDensityDpi(int32_t * densityDpi)348 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityDpi(int32_t *densityDpi)
349 {
350     if (densityDpi == nullptr) {
351         TLOGE(WmsLogTag::DMS, "[DMNDK] input densityDpi null.");
352         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
353     }
354     auto displayInfo = OH_GetDefaultDisplayInfo();
355     if (displayInfo == nullptr) {
356         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
357         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
358     }
359     *densityDpi = displayInfo->GetVirtualPixelRatio() * DOT_PER_INCH;
360     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
361 }
362 
OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(float * densityPixels)363 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(float *densityPixels)
364 {
365     if (densityPixels == nullptr) {
366         TLOGE(WmsLogTag::DMS, "[DMNDK] input densityPixels null.");
367         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
368     }
369     auto displayInfo = OH_GetDefaultDisplayInfo();
370     if (displayInfo == nullptr) {
371         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
372         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
373     }
374     *densityPixels = displayInfo->GetVirtualPixelRatio();
375     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
376 }
377 
OH_NativeDisplayManager_GetDefaultDisplayScaledDensity(float * scaledDensity)378 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayScaledDensity(float *scaledDensity)
379 {
380     if (scaledDensity == nullptr) {
381         TLOGE(WmsLogTag::DMS, "[DMNDK] input scaledDensity null.");
382         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
383     }
384     auto displayInfo = OH_GetDefaultDisplayInfo();
385     if (displayInfo == nullptr) {
386         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
387         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
388     }
389     *scaledDensity = displayInfo->GetVirtualPixelRatio();
390     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
391 }
392 
OH_NativeDisplayManager_GetDefaultDisplayDensityXdpi(float * xDpi)393 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityXdpi(float *xDpi)
394 {
395     if (xDpi == nullptr) {
396         TLOGE(WmsLogTag::DMS, "[DMNDK] input xDpi null.");
397         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
398     }
399     auto displayInfo = OH_GetDefaultDisplayInfo();
400     if (displayInfo == nullptr) {
401         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
402         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
403     }
404     *xDpi = displayInfo->GetXDpi();
405     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
406 }
407 
OH_NativeDisplayManager_GetDefaultDisplayDensityYdpi(float * yDpi)408 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityYdpi(float *yDpi)
409 {
410     if (yDpi == nullptr) {
411         TLOGE(WmsLogTag::DMS, "[DMNDK] input yDpi null.");
412         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
413     }
414     auto displayInfo = OH_GetDefaultDisplayInfo();
415     if (displayInfo == nullptr) {
416         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
417         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
418     }
419     *yDpi = displayInfo->GetYDpi();
420     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
421 }
422 
OH_SetDisplayRect(const DMRect & dmRect,NativeDisplayManager_Rect * displayRectItem)423 static void OH_SetDisplayRect(const DMRect &dmRect, NativeDisplayManager_Rect *displayRectItem)
424 {
425     displayRectItem->left = dmRect.posX_;
426     displayRectItem->top = dmRect.posY_;
427     displayRectItem->width = dmRect.width_;
428     displayRectItem->height = dmRect.height_;
429 }
430 
OH_CreateBoundingRects(const std::vector<DMRect> & boundingRects)431 static NativeDisplayManager_Rect* OH_CreateBoundingRects(const std::vector<DMRect> &boundingRects)
432 {
433     int32_t boundSize = static_cast<int32_t>(boundingRects.size());
434     NativeDisplayManager_Rect *ohBoundingRects =
435         static_cast<NativeDisplayManager_Rect*>(malloc(sizeof(NativeDisplayManager_Rect) * boundSize));
436     if (ohBoundingRects == NULL) {
437         TLOGE(WmsLogTag::DMS, "[DMNDK] memory failed.");
438         return NULL;
439     }
440     auto retMemset = memset_s(ohBoundingRects, sizeof(NativeDisplayManager_Rect) * boundSize, 0,
441         sizeof(NativeDisplayManager_Rect) * boundSize);
442     if (retMemset != 0) {
443         free(ohBoundingRects);
444         ohBoundingRects = NULL;
445         TLOGE(WmsLogTag::DMS, "[DMNDK] memset failed.");
446         return NULL;
447     }
448     TLOGI(WmsLogTag::DMS, "[DMNDK] bounding size:%{public}d.", boundSize);
449     for (int i = 0; i < boundSize; i++) {
450         OH_SetDisplayRect(boundingRects[i], (ohBoundingRects + i));
451     }
452     return ohBoundingRects;
453 }
454 
OH_SetWaterfallDisplayAreaRects(const WaterfallDisplayAreaRects & waterArea,NativeDisplayManager_CutoutInfo * ohCutoutInfo)455 static void OH_SetWaterfallDisplayAreaRects(const WaterfallDisplayAreaRects &waterArea,
456     NativeDisplayManager_CutoutInfo *ohCutoutInfo)
457 {
458     if (ohCutoutInfo == NULL) {
459         TLOGE(WmsLogTag::DMS, "[DMNDK] ohCutoutInfo is null.");
460         return;
461     }
462     TLOGI(WmsLogTag::DMS, "[DMNDK] set waterfall Area.");
463     OH_SetDisplayRect(waterArea.left, &(ohCutoutInfo->waterfallDisplayAreaRects.left));
464     OH_SetDisplayRect(waterArea.top, &(ohCutoutInfo->waterfallDisplayAreaRects.top));
465     OH_SetDisplayRect(waterArea.right, &(ohCutoutInfo->waterfallDisplayAreaRects.right));
466     OH_SetDisplayRect(waterArea.bottom, &(ohCutoutInfo->waterfallDisplayAreaRects.bottom));
467 }
468 
OH_CreateCutoutInfoObject(const sptr<CutoutInfo> & cutoutInfo)469 static NativeDisplayManager_CutoutInfo* OH_CreateCutoutInfoObject(const sptr<CutoutInfo> &cutoutInfo)
470 {
471     NativeDisplayManager_CutoutInfo *ohCutoutInfo =
472         static_cast<NativeDisplayManager_CutoutInfo*>(malloc(sizeof(NativeDisplayManager_CutoutInfo)));
473     if (ohCutoutInfo == NULL) {
474         TLOGE(WmsLogTag::DMS, "[DMNDK] memory failed.");
475         return NULL;
476     }
477     auto retMemset = memset_s(ohCutoutInfo, sizeof(NativeDisplayManager_CutoutInfo), 0,
478         sizeof(NativeDisplayManager_CutoutInfo));
479     if (retMemset != 0) {
480         free(ohCutoutInfo);
481         ohCutoutInfo = NULL;
482         TLOGE(WmsLogTag::DMS, "[DMNDK] memset failed.");
483         return NULL;
484     }
485     std::vector<DMRect> boundingRects = cutoutInfo->GetBoundingRects();
486     WaterfallDisplayAreaRects waterRects = cutoutInfo->GetWaterfallDisplayAreaRects();
487     ohCutoutInfo->boundingRectsLength = static_cast<int32_t>(boundingRects.size());
488     TLOGI(WmsLogTag::DMS, "[DMNDK] boundingRectsLength=%{public}d.", ohCutoutInfo->boundingRectsLength);
489     ohCutoutInfo->boundingRects = OH_CreateBoundingRects(boundingRects);
490     if (ohCutoutInfo->boundingRects == NULL) {
491         TLOGE(WmsLogTag::DMS, "[DMNDK] create bounding rects failed.");
492         free(ohCutoutInfo);
493         ohCutoutInfo = NULL;
494         return NULL;
495     }
496     OH_SetWaterfallDisplayAreaRects(waterRects, ohCutoutInfo);
497     return ohCutoutInfo;
498 }
499 
OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo ** cutoutInfo)500 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(
501     NativeDisplayManager_CutoutInfo **cutoutInfo)
502 {
503     if (cutoutInfo == NULL) {
504         TLOGE(WmsLogTag::DMS, "[DMNDK] input cutoutInfo null.");
505         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
506     }
507     TLOGI(WmsLogTag::DMS, "[DMNDK] get display cutout info.");
508     sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplaySync();
509     if (defaultDisplay == nullptr) {
510         TLOGE(WmsLogTag::DMS, "[DMNDK] get cutout info (display) null.");
511         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
512     }
513     sptr<CutoutInfo> cutoutInfoInner = defaultDisplay->GetCutoutInfo();
514     if (cutoutInfoInner == nullptr) {
515         TLOGE(WmsLogTag::DMS, "[DMNDK] get cutout info (from display) null.");
516         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
517     }
518     *cutoutInfo = OH_CreateCutoutInfoObject(cutoutInfoInner);
519     if (*cutoutInfo == NULL) {
520         TLOGE(WmsLogTag::DMS, "[DMNDK] convert cutout info null.");
521         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
522     }
523     TLOGI(WmsLogTag::DMS, "[DMNDK] get display cutout info success.");
524     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
525 }
526 
OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo * cutoutInfo)527 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(
528     NativeDisplayManager_CutoutInfo *cutoutInfo)
529 {
530     if (cutoutInfo == NULL) {
531         TLOGE(WmsLogTag::DMS, "[DMNDK] input cutoutInfo null pointer.");
532         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
533     }
534     if (cutoutInfo->boundingRects != NULL) {
535         free(cutoutInfo->boundingRects);
536         cutoutInfo->boundingRects = NULL;
537     }
538     free(cutoutInfo);
539     cutoutInfo = NULL;
540     TLOGI(WmsLogTag::DMS, "[DMNDK] destroy cutoutInfo end.");
541     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
542 }
543 
OH_NativeDisplayManager_IsFoldable()544 bool OH_NativeDisplayManager_IsFoldable()
545 {
546     bool isFoldable = DisplayManager::GetInstance().IsFoldable();
547     TLOGI(WmsLogTag::DMS, "[DMNDK] get display isFoldable=%{public}d.", isFoldable);
548     return isFoldable;
549 }
550 
OH_NativeDisplayManager_GetFoldDisplayMode(NativeDisplayManager_FoldDisplayMode * foldDisplayMode)551 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetFoldDisplayMode(
552     NativeDisplayManager_FoldDisplayMode *foldDisplayMode)
553 {
554     TLOGI(WmsLogTag::DMS, "[DMNDK] get fold display mode.");
555     if (foldDisplayMode == NULL) {
556         TLOGE(WmsLogTag::DMS, "[DMNDK] input foldDisplayMode null.");
557         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
558     }
559     if (!DisplayManager::GetInstance().IsFoldable()) {
560         TLOGE(WmsLogTag::DMS, "[DMNDK] device is not foldable.");
561         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
562     }
563     FoldDisplayMode foldMode = DisplayManager::GetInstance().GetFoldDisplayModeForExternal();
564     switch (foldMode) {
565         case FoldDisplayMode::SUB:
566             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_SUB;
567             break;
568         case FoldDisplayMode::MAIN:
569             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_MAIN;
570             break;
571         case FoldDisplayMode::FULL:
572             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_FULL;
573             break;
574         case FoldDisplayMode::COORDINATION:
575             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_COORDINATION;
576             break;
577         default:
578             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_UNKNOWN;
579             break;
580     }
581     TLOGI(WmsLogTag::DMS, "[DMNDK] current fold display mode: %{public}d.", *foldDisplayMode);
582     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
583 }
584 
585 std::shared_mutex foldChangeMutex;
586 std::map<uint32_t, OH_NativeDisplayManager_FoldDisplayModeChangeCallback> g_foldChangeCallbackMap;
587 std::map<uint32_t, sptr<DisplayManager::IDisplayModeListener>> g_foldDisplayModeChangeListenerMap;
588 
CheckFoldChangeHasRegistered(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback)589 bool CheckFoldChangeHasRegistered(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback)
590 {
591     if (g_foldChangeCallbackMap.empty()) {
592         return false;
593     }
594     for (auto iter : g_foldChangeCallbackMap) {
595         if (iter.second == displayModeChangeCallback) {
596             return true;
597         }
598     }
599     return false;
600 }
601 
OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback,uint32_t * listenerIndex)602 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener(
603     OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback, uint32_t *listenerIndex)
604 {
605     TLOGI(WmsLogTag::DMS, "[DMNDK] register fold display mode change listener.");
606     if (displayModeChangeCallback == NULL || listenerIndex == NULL) {
607         TLOGE(WmsLogTag::DMS, "[DMNDK] input params null.");
608         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
609     }
610     if (!DisplayManager::GetInstance().IsFoldable()) {
611         TLOGE(WmsLogTag::DMS, "[DMNDK] device is not foldable.");
612         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
613     }
614     std::unique_lock<std::shared_mutex> lock(foldChangeMutex);
615     if (CheckFoldChangeHasRegistered(displayModeChangeCallback)) {
616         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
617         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
618     }
619     sptr<DisplayManager::IDisplayModeListener> displayModeListener =
620         sptr<OH_DisplayModeChangeListener>::MakeSptr(displayModeChangeCallback);
621     if (displayModeListener == nullptr) {
622         TLOGE(WmsLogTag::DMS, "[DMNDK] display mode listener MakeSptr fail.");
623         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
624     }
625     static std::atomic<uint32_t> registerCount = 1;
626     DMError ret = DisplayManager::GetInstance().RegisterDisplayModeListener(displayModeListener);
627     if (ret != DMError::DM_OK) {
628         TLOGE(WmsLogTag::DMS, "[DMNDK] display mode listener register failed ret=%{public}d.", ret);
629         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
630     }
631     *listenerIndex = registerCount++;
632     g_foldChangeCallbackMap.emplace(*listenerIndex, displayModeChangeCallback);
633     g_foldDisplayModeChangeListenerMap.emplace(*listenerIndex, displayModeListener);
634     TLOGI(WmsLogTag::DMS, "[DMNDK] register fold change success and listenerIndex= %{public}d.", *listenerIndex);
635     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
636 }
637 
OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(uint32_t listenerIndex)638 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(uint32_t listenerIndex)
639 {
640     TLOGI(WmsLogTag::DMS, "[DMNDK] unregister fold display mode change listener %{public}d.", listenerIndex);
641     if (!DisplayManager::GetInstance().IsFoldable()) {
642         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fail(device is not foldable).");
643         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
644     }
645     std::unique_lock<std::shared_mutex> lock(foldChangeMutex);
646     auto iter = g_foldDisplayModeChangeListenerMap.find(listenerIndex);
647     if (iter == g_foldDisplayModeChangeListenerMap.end()) {
648         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister listener fail(not find register info).");
649         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
650     }
651     DMError ret = DMError::DM_OK;
652     if (iter->second != nullptr) {
653         ret = DisplayManager::GetInstance().UnregisterDisplayModeListener(iter->second);
654         g_foldDisplayModeChangeListenerMap.erase(listenerIndex);
655         g_foldChangeCallbackMap.erase(listenerIndex);
656         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister listener ert=%{public}d", ret);
657     }
658     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
659         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
660 }
661 
662 std::shared_mutex displayChangeMutex;
663 std::map<uint32_t, OH_NativeDisplayManager_DisplayChangeCallback> g_displayChangeCallbackMap;
664 std::map<uint32_t, sptr<DisplayManager::IDisplayListener>> g_displayChangeListenerMap;
665 
CheckDisplayChangeHasRegistered(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback)666 bool CheckDisplayChangeHasRegistered(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback)
667 {
668     if (g_displayChangeCallbackMap.empty()) {
669         return false;
670     }
671     for (auto iter : g_displayChangeCallbackMap) {
672         if (iter.second == displayChangeCallback) {
673             return true;
674         }
675     }
676     return false;
677 }
678 
OH_NativeDisplayManager_RegisterDisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback,uint32_t * listenerIndex)679 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterDisplayChangeListener(
680     OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback, uint32_t *listenerIndex)
681 {
682     TLOGI(WmsLogTag::DMS, "[DMNDK] register display change listener.");
683     if (displayChangeCallback == NULL || listenerIndex == NULL) {
684         TLOGE(WmsLogTag::DMS, "[DMNDK] register fail(input params null).");
685         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
686     }
687     std::unique_lock<std::shared_mutex> lock(displayChangeMutex);
688     if (CheckDisplayChangeHasRegistered(displayChangeCallback)) {
689         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
690         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
691     }
692     sptr<DisplayManager::IDisplayListener> displayListener =
693         sptr<OH_DisplayChangeListener>::MakeSptr(displayChangeCallback);
694     if (displayListener == nullptr) {
695         TLOGE(WmsLogTag::DMS, "[DMNDK] register display change MakeSptr fail.");
696         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
697     }
698     static std::atomic<uint32_t> registerCount = 1;
699     DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(displayListener);
700     if (ret != DMError::DM_OK) {
701         TLOGE(WmsLogTag::DMS, "[DMNDK] register failed ret=%{public}d.", ret);
702         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
703     }
704     *listenerIndex = registerCount++;
705     g_displayChangeCallbackMap.emplace(*listenerIndex, displayChangeCallback);
706     g_displayChangeListenerMap.emplace(*listenerIndex, displayListener);
707     TLOGI(WmsLogTag::DMS, "[DMNDK] register listenerIndex= %{public}d.", *listenerIndex);
708     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
709 }
710 
OH_NativeDisplayManager_UnregisterDisplayChangeListener(uint32_t listenerIndex)711 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterDisplayChangeListener(uint32_t listenerIndex)
712 {
713     TLOGI(WmsLogTag::DMS, "[DMNDK] unregister display change listener %{public}d.", listenerIndex);
714     std::unique_lock<std::shared_mutex> lock(displayChangeMutex);
715     auto iter = g_displayChangeListenerMap.find(listenerIndex);
716     if (iter == g_displayChangeListenerMap.end()) {
717         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fail(not find register info).");
718         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
719     }
720     DMError ret = DMError::DM_OK;
721     if (iter->second != nullptr) {
722         ret = DisplayManager::GetInstance().UnregisterDisplayListener(iter->second);
723         g_displayChangeListenerMap.erase(listenerIndex);
724         g_displayChangeCallbackMap.erase(listenerIndex);
725         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister listener ret=%{public}d", ret);
726     }
727     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
728         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
729 }
730 
NativeDisplayManager_SetColorSpace(NativeDisplayManager_DisplayInfo * displayInfo,sptr<DisplayInfo> info)731 static void NativeDisplayManager_SetColorSpace(NativeDisplayManager_DisplayInfo *displayInfo, sptr<DisplayInfo> info)
732 {
733     std::vector<uint32_t> colorSpaces = info->GetColorSpaces();
734     if (colorSpaces.empty()) {
735         TLOGW(WmsLogTag::DMS, "[DMNDK] colorSpaces is empty displayId=%{public}u", displayInfo->id);
736         return;
737     }
738     displayInfo->colorSpace = (NativeDisplayManager_DisplayColorSpace*)malloc(
739         sizeof(NativeDisplayManager_DisplayColorSpace));
740     if (displayInfo->colorSpace == nullptr) {
741         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc color space failed");
742         return;
743     }
744     auto retMemset = memset_s(displayInfo->colorSpace, sizeof(NativeDisplayManager_DisplayColorSpace), 0,
745         sizeof(NativeDisplayManager_DisplayColorSpace));
746     if (retMemset != EOK) {
747         TLOGE(WmsLogTag::DMS, "[DMNDK] memset color space failed");
748         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
749         return;
750     }
751     displayInfo->colorSpace->colorSpaceLength = static_cast<uint32_t>(colorSpaces.size());
752     displayInfo->colorSpace->colorSpaces = (uint32_t*)malloc(sizeof(uint32_t) * colorSpaces.size());
753     if (displayInfo->colorSpace->colorSpaces == nullptr) {
754         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc color spaces failed");
755         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
756         return;
757     }
758     retMemset = memset_s(displayInfo->colorSpace->colorSpaces, sizeof(uint32_t) * colorSpaces.size(), 0,
759         sizeof(uint32_t) * colorSpaces.size());
760     if (retMemset != EOK) {
761         TLOGE(WmsLogTag::DMS, "[DMNDK] memset color spaces failed");
762         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace->colorSpaces);
763         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
764         return;
765     }
766 
767     uint32_t colorLoop = 0;
768     for (const auto colorSpace : colorSpaces) {
769         DM_GraphicCM_ColorSpaceType colorSpaceValue = static_cast<DM_GraphicCM_ColorSpaceType>(colorSpace);
770         if (DM_NATIVE_TO_NDK_COLOR_SPACE_TYPE_MAP.find(colorSpaceValue) ==
771             DM_NATIVE_TO_NDK_COLOR_SPACE_TYPE_MAP.end()) {
772             TLOGW(WmsLogTag::DMS, "[DMNDK] color spaces[%{public}d] not in map.", colorSpace);
773             continue;
774         }
775         displayInfo->colorSpace->colorSpaces[colorLoop] =
776             static_cast<uint32_t>(DM_NATIVE_TO_NDK_COLOR_SPACE_TYPE_MAP.at(colorSpaceValue));
777         colorLoop++;
778     }
779     TLOGI(WmsLogTag::DMS, "[DMNDK] color spaces count:%{public}u.", colorLoop);
780 }
781 
NativeDisplayManager_SetHdrFormat(NativeDisplayManager_DisplayInfo * displayInfo,sptr<DisplayInfo> info)782 static void NativeDisplayManager_SetHdrFormat(NativeDisplayManager_DisplayInfo *displayInfo, sptr<DisplayInfo> info)
783 {
784     std::vector<uint32_t> hdrFormats = info->GetHdrFormats();
785     if (hdrFormats.empty()) {
786         TLOGW(WmsLogTag::DMS, "[DMNDK] hdrFormats is empty displayId=%{public}u", displayInfo->id);
787         return;
788     }
789     displayInfo->hdrFormat = (NativeDisplayManager_DisplayHdrFormat*)malloc(
790         sizeof(NativeDisplayManager_DisplayHdrFormat));
791     if (displayInfo->hdrFormat == nullptr) {
792         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc hdr format failed");
793         return;
794     }
795     auto retMemset = memset_s(displayInfo->hdrFormat, sizeof(NativeDisplayManager_DisplayHdrFormat), 0,
796         sizeof(NativeDisplayManager_DisplayHdrFormat));
797     if (retMemset != EOK) {
798         TLOGE(WmsLogTag::DMS, "[DMNDK] memset hdr format failed");
799         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
800         return;
801     }
802     displayInfo->hdrFormat->hdrFormatLength = static_cast<uint32_t>(hdrFormats.size());
803     displayInfo->hdrFormat->hdrFormats = (uint32_t*)malloc(sizeof(uint32_t) * hdrFormats.size());
804     if (displayInfo->hdrFormat->hdrFormats == nullptr) {
805         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc hdr format failed");
806         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
807         return;
808     }
809     retMemset = memset_s(displayInfo->hdrFormat->hdrFormats, sizeof(uint32_t) * hdrFormats.size(), 0,
810         sizeof(uint32_t) * hdrFormats.size());
811     if (retMemset != EOK) {
812         TLOGE(WmsLogTag::DMS, "[DMNDK] memset hdr format failed");
813         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat->hdrFormats);
814         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
815         return;
816     }
817 
818     uint32_t hdrLoop = 0;
819     for (const auto hdrFormat : hdrFormats) {
820         DM_ScreenHDRFormat hdrFormatValue = static_cast<DM_ScreenHDRFormat>(hdrFormat);
821         if (DM_NATIVE_TO_NDK_HDR_FORMAT_TYPE_MAP.find(hdrFormatValue) == DM_NATIVE_TO_NDK_HDR_FORMAT_TYPE_MAP.end()) {
822             TLOGW(WmsLogTag::DMS, "[DMNDK] hdr format[%{public}d] not in map.", hdrFormat);
823             continue;
824         }
825         displayInfo->hdrFormat->hdrFormats[hdrLoop] =
826             static_cast<uint32_t>(DM_NATIVE_TO_NDK_HDR_FORMAT_TYPE_MAP.at(hdrFormatValue));
827         hdrLoop++;
828     }
829     TLOGI(WmsLogTag::DMS, "[DMNDK] hdr format count:%{public}u", hdrLoop);
830 }
831 
NativeDisplayManager_SetDisplayInfo(NativeDisplayManager_DisplayInfo * displayInfo,sptr<DisplayInfo> info)832 static void NativeDisplayManager_SetDisplayInfo(NativeDisplayManager_DisplayInfo *displayInfo,
833     sptr<DisplayInfo> info)
834 {
835     displayInfo->id = static_cast<uint32_t>(info->GetDisplayId());
836     displayInfo->width = info->GetWidth();
837     displayInfo->height = info->GetHeight();
838     displayInfo->orientation = static_cast<NativeDisplayManager_Orientation>(info->GetDisplayOrientation());
839     displayInfo->rotation = static_cast<NativeDisplayManager_Rotation>(info->GetRotation());
840     displayInfo->refreshRate = info->GetRefreshRate();
841     displayInfo->availableWidth = info->GetAvailableWidth();
842     displayInfo->availableHeight = info->GetAvailableHeight();
843     displayInfo->physicalWidth = info->GetPhysicalWidth();
844     displayInfo->physicalHeight = info->GetPhysicalHeight();
845     displayInfo->densityDPI = info->GetVirtualPixelRatio() * DOT_PER_INCH;
846     displayInfo->densityPixels = info->GetVirtualPixelRatio();
847     displayInfo->scaledDensity = info->GetVirtualPixelRatio();
848     displayInfo->xDPI = info->GetXDpi();
849     displayInfo->yDPI = info->GetYDpi();
850     displayInfo->isAlive = info->GetAliveStatus();
851     if (DM_NATIVE_TO_NDK_DISPLAY_STATE_MAP.find(info->GetDisplayState()) != DM_NATIVE_TO_NDK_DISPLAY_STATE_MAP.end()) {
852         displayInfo->state = static_cast<NativeDisplayManager_DisplayState>(
853             DM_NATIVE_TO_NDK_DISPLAY_STATE_MAP.at(info->GetDisplayState()));
854     } else {
855         displayInfo->state = static_cast<NativeDisplayManager_DisplayState>(DM_DisplayStateMode::STATE_UNKNOWN);
856     }
857     /* color space */
858     NativeDisplayManager_SetColorSpace(displayInfo, info);
859     /* hdr format */
860     NativeDisplayManager_SetHdrFormat(displayInfo, info);
861     TLOGI(WmsLogTag::DMS, "[DMNDK] set display id[%{public}u] finish.", displayInfo->id);
862 }
863 
NativeDisplayManager_SetDisplaysInfo(const std::vector<sptr<Display>> & displays,NativeDisplayManager_DisplayInfo * displaysInfo)864 static NativeDisplayManager_ErrorCode NativeDisplayManager_SetDisplaysInfo(const std::vector<sptr<Display>>& displays,
865     NativeDisplayManager_DisplayInfo *displaysInfo)
866 {
867     uint32_t i = 0;
868     for (auto& display : displays) {
869         if (display == nullptr) {
870             TLOGE(WmsLogTag::DMS, "[DMNDK] get display null.");
871             continue;
872         }
873         auto info = display->GetDisplayInfo();
874         if (info == nullptr) {
875             TLOGE(WmsLogTag::DMS, "[DMNDK] get display id[%{public}" PRIu64"] info null.", display->GetId());
876             continue;
877         }
878         int ret = memcpy_s(displaysInfo[i].name, OH_DISPLAY_NAME_LENGTH, info->GetName().c_str(),
879             OH_DISPLAY_NAME_LENGTH);
880         if (ret != EOK) {
881             TLOGE(WmsLogTag::DMS, "[DMNDK] failed to memcpy name.");
882             return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
883         }
884         NativeDisplayManager_SetDisplayInfo(displaysInfo + i, info);
885         i++;
886     }
887     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
888 }
889 
NativeDisplayManager_DestroyDisplaysInfoInner(uint32_t displaysLength,NativeDisplayManager_DisplayInfo * displaysInfo)890 static void NativeDisplayManager_DestroyDisplaysInfoInner(uint32_t displaysLength,
891     NativeDisplayManager_DisplayInfo *displaysInfo)
892 {
893     if (displaysLength == 0 || displaysInfo == nullptr) {
894         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
895         return;
896     }
897     for (uint32_t i = 0; i < displaysLength; i++) {
898         NativeDisplayManager_DisplayInfo displayItem = displaysInfo[i];
899         if (displayItem.colorSpace != nullptr) {
900             if (displayItem.colorSpace->colorSpaces != nullptr) {
901                 DISPLAY_MANAGER_FREE_MEMORY(displayItem.colorSpace->colorSpaces);
902             }
903             DISPLAY_MANAGER_FREE_MEMORY(displayItem.colorSpace);
904         }
905         if (displayItem.hdrFormat != nullptr) {
906             if (displayItem.hdrFormat->hdrFormats != nullptr) {
907                 DISPLAY_MANAGER_FREE_MEMORY(displayItem.hdrFormat->hdrFormats);
908             }
909             DISPLAY_MANAGER_FREE_MEMORY(displayItem.hdrFormat);
910         }
911     }
912     DISPLAY_MANAGER_FREE_MEMORY(displaysInfo);
913 }
914 
OH_NativeDisplayManager_CreateAllDisplays(NativeDisplayManager_DisplaysInfo ** allDisplays)915 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateAllDisplays(
916     NativeDisplayManager_DisplaysInfo **allDisplays)
917 {
918     if (allDisplays == nullptr) {
919         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
920         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
921     }
922     /* displays is not null make sure by GetAllDisplays*/
923     std::vector<sptr<Display>> displays = DisplayManager::GetInstance().GetAllDisplays();
924     if (displays.empty()) {
925         TLOGE(WmsLogTag::DMS, "[DMNDK] displays is empty.");
926         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
927     }
928     NativeDisplayManager_DisplaysInfo *displaysInner =
929         (NativeDisplayManager_DisplaysInfo*)malloc(sizeof(NativeDisplayManager_DisplaysInfo));
930     if (displaysInner == nullptr) {
931         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc displays inner failed.");
932         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
933     }
934     int32_t retMemset = memset_s(displaysInner, sizeof(NativeDisplayManager_DisplaysInfo), 0,
935         sizeof(NativeDisplayManager_DisplaysInfo));
936     if (retMemset != EOK) {
937         TLOGE(WmsLogTag::DMS, "[DMNDK] memset displays failed.");
938         DISPLAY_MANAGER_FREE_MEMORY(displaysInner);
939         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
940     }
941     size_t displaySize = displays.size();
942     displaysInner->displaysLength = static_cast<uint32_t>(displaySize);
943     NativeDisplayManager_DisplayInfo *displaysInfo =
944         (NativeDisplayManager_DisplayInfo*)malloc(sizeof(NativeDisplayManager_DisplayInfo) * displaySize);
945     if (displaysInfo == nullptr) {
946         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc displaysInfo failed.");
947         DISPLAY_MANAGER_FREE_MEMORY(displaysInner);
948         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
949     }
950     retMemset = memset_s(displaysInfo, sizeof(NativeDisplayManager_DisplayInfo) * displaySize, 0,
951         sizeof(NativeDisplayManager_DisplayInfo) * displaySize);
952     NativeDisplayManager_ErrorCode setRet = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
953     if (retMemset == EOK) {
954         setRet = NativeDisplayManager_SetDisplaysInfo(displays, displaysInfo);
955     }
956     if (retMemset != EOK || setRet != NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK) {
957         TLOGE(WmsLogTag::DMS, "[DMNDK] memset or set displaysInfo failed setRet=%{public}d.", setRet);
958         NativeDisplayManager_DestroyDisplaysInfoInner(displaysInner->displaysLength, displaysInfo);
959         DISPLAY_MANAGER_FREE_MEMORY(displaysInner);
960         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
961     }
962     displaysInner->displaysInfo = displaysInfo;
963     *allDisplays = displaysInner;
964     return setRet;
965 }
966 
OH_NativeDisplayManager_DestroyAllDisplays(NativeDisplayManager_DisplaysInfo * allDisplays)967 void OH_NativeDisplayManager_DestroyAllDisplays(NativeDisplayManager_DisplaysInfo *allDisplays)
968 {
969     if (allDisplays == nullptr) {
970         TLOGI(WmsLogTag::DMS, "[DMNDK] param is null.");
971         return;
972     }
973     if (allDisplays->displaysInfo == nullptr) {
974         DISPLAY_MANAGER_FREE_MEMORY(allDisplays);
975         return;
976     }
977     NativeDisplayManager_DestroyDisplaysInfoInner(allDisplays->displaysLength, allDisplays->displaysInfo);
978     DISPLAY_MANAGER_FREE_MEMORY(allDisplays);
979 }
980 
NativeDisplayManager_FillDisplayInfo(sptr<Display> display,NativeDisplayManager_ErrorCode * errCode)981 static NativeDisplayManager_DisplayInfo* NativeDisplayManager_FillDisplayInfo(sptr<Display> display,
982     NativeDisplayManager_ErrorCode *errCode)
983 {
984     sptr<DisplayInfo> info = display->GetDisplayInfo();
985     if (info == nullptr) {
986         TLOGE(WmsLogTag::DMS, "[DMNDK] get display info null.");
987         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
988         return nullptr;
989     }
990     NativeDisplayManager_DisplayInfo *displayInner =
991         (NativeDisplayManager_DisplayInfo*)malloc(sizeof(NativeDisplayManager_DisplayInfo));
992     if (displayInner == nullptr) {
993         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc display info null.");
994         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
995         return nullptr;
996     }
997     auto retMemset = memset_s(displayInner, sizeof(NativeDisplayManager_DisplayInfo), 0,
998         sizeof(NativeDisplayManager_DisplayInfo));
999     if (retMemset != EOK) {
1000         TLOGE(WmsLogTag::DMS, "[DMNDK] memset display info null.");
1001         DISPLAY_MANAGER_FREE_MEMORY(displayInner);
1002         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1003         return nullptr;
1004     }
1005     int ret = memcpy_s(displayInner->name, OH_DISPLAY_NAME_LENGTH, info->GetName().c_str(), OH_DISPLAY_NAME_LENGTH);
1006     if (ret != EOK) {
1007         TLOGE(WmsLogTag::DMS, "[DMNDK] memcpy display name failed.");
1008         DISPLAY_MANAGER_FREE_MEMORY(displayInner);
1009         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1010         return nullptr;
1011     }
1012     NativeDisplayManager_SetDisplayInfo(displayInner, info);
1013     *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1014     return displayInner;
1015 }
1016 
OH_NativeDisplayManager_CreateDisplayById(uint32_t id,NativeDisplayManager_DisplayInfo ** displayInfo)1017 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateDisplayById(uint32_t id,
1018     NativeDisplayManager_DisplayInfo **displayInfo)
1019 {
1020     if (displayInfo == nullptr) {
1021         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
1022         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
1023     }
1024     sptr<Display> display = DisplayManager::GetInstance().GetDisplayById(static_cast<DisplayId>(id));
1025     if (display == nullptr) {
1026         TLOGE(WmsLogTag::DMS, "[DMNDK] get display by id null.");
1027         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1028     }
1029     TLOGI(WmsLogTag::DMS, "[DMNDK] get display id[%{public}" PRIu64"] info", display->GetId());
1030     NativeDisplayManager_ErrorCode errorCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1031     *displayInfo = NativeDisplayManager_FillDisplayInfo(display, &errorCode);
1032     return errorCode;
1033 }
1034 
OH_NativeDisplayManager_CreatePrimaryDisplay(NativeDisplayManager_DisplayInfo ** displayInfo)1035 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreatePrimaryDisplay(
1036     NativeDisplayManager_DisplayInfo **displayInfo)
1037 {
1038     if (displayInfo == nullptr) {
1039         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
1040         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
1041     }
1042     sptr<Display> display = DisplayManager::GetInstance().GetPrimaryDisplaySync();
1043     if (display == nullptr) {
1044         TLOGE(WmsLogTag::DMS, "[DMNDK] get primary display is null.");
1045         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1046     }
1047     TLOGI(WmsLogTag::DMS, "[DMNDK] get primary display id[%{public}" PRIu64"].", display->GetId());
1048     NativeDisplayManager_ErrorCode errorCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1049     *displayInfo = NativeDisplayManager_FillDisplayInfo(display, &errorCode);
1050     return errorCode;
1051 }
1052 
OH_NativeDisplayManager_DestroyDisplay(NativeDisplayManager_DisplayInfo * displayInfo)1053 void OH_NativeDisplayManager_DestroyDisplay(NativeDisplayManager_DisplayInfo *displayInfo)
1054 {
1055     if (displayInfo == nullptr) {
1056         TLOGE(WmsLogTag::DMS, "[DMNDK] free display info is null.");
1057         return;
1058     }
1059     if (displayInfo->colorSpace != nullptr) {
1060         if (displayInfo->colorSpace->colorSpaces != nullptr) {
1061             DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace->colorSpaces);
1062         }
1063         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
1064     }
1065     if (displayInfo->hdrFormat != nullptr) {
1066         if (displayInfo->hdrFormat->hdrFormats != nullptr) {
1067             DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat->hdrFormats);
1068         }
1069         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
1070     }
1071     DISPLAY_MANAGER_FREE_MEMORY(displayInfo);
1072 }
1073 
OH_NativeDisplayManager_CaptureScreenPixelmap(uint32_t displayId,OH_PixelmapNative ** pixelMap)1074 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CaptureScreenPixelmap(uint32_t displayId,
1075     OH_PixelmapNative **pixelMap)
1076 {
1077     if (pixelMap == nullptr) {
1078         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap is null.");
1079         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
1080     }
1081     CaptureOption option;
1082     option.displayId_ = static_cast<DisplayId>(displayId);
1083     option.isNeedNotify_ = true;
1084     DmErrorCode errCode = DmErrorCode::DM_OK;
1085     std::shared_ptr<Media::PixelMap> captureImage = DisplayManager::GetInstance().GetScreenCapture(option, &errCode);
1086 
1087     if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM) {
1088         TLOGE(WmsLogTag::DMS, "[DMNDK] param error.");
1089         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
1090     }
1091     if (errCode == DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT) {
1092         TLOGE(WmsLogTag::DMS, "[DMNDK] not support.");
1093         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
1094     }
1095     if (errCode == DmErrorCode::DM_ERROR_NO_PERMISSION) {
1096         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap no permission.");
1097         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_NO_PERMISSION;
1098     }
1099     if (captureImage == nullptr) {
1100         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap is null.");
1101         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1102     }
1103     *pixelMap = new OH_PixelmapNative(captureImage);
1104     if (*pixelMap == nullptr) {
1105         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap convert pixelMapNative null.");
1106         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1107     }
1108     TLOGI(WmsLogTag::DMS, "[DMNDK] get screen capture end.");
1109     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1110 }
1111 
OH_NativeDisplayManager_RegisterAvailableAreaChangeListener(OH_NativeDisplayManager_AvailableAreaChangeCallback availableAreaChangeCallback,uint32_t * listenerIndex)1112 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterAvailableAreaChangeListener(
1113     OH_NativeDisplayManager_AvailableAreaChangeCallback availableAreaChangeCallback, uint32_t* listenerIndex)
1114 {
1115     if (availableAreaChangeCallback == nullptr || listenerIndex == nullptr) {
1116         TLOGE(WmsLogTag::DMS, "[DMNDK] input params null.");
1117         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1118     }
1119     sptr<AvailableAreaChangeListener> availableAreaChangeListener =
1120         sptr<AvailableAreaChangeListener>::MakeSptr(availableAreaChangeCallback);
1121     if (CheckAvailableAreaChangeHasRegistered(availableAreaChangeListener)) {
1122         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
1123         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1124     }
1125     sptr<DisplayManager::IAvailableAreaListener> displayAvailableAreaListener = availableAreaChangeListener;
1126 
1127     static std::atomic<uint32_t> registerCount = 1;
1128     DMError ret = DisplayManager::GetInstance().RegisterAvailableAreaListener(displayAvailableAreaListener);
1129     if (ret != DMError::DM_OK) {
1130         TLOGE(WmsLogTag::DMS, "[DMNDK] register failed ret=%{public}d.", ret);
1131         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1132     }
1133     *listenerIndex = registerCount++;
1134     std::unique_lock<std::shared_mutex> lock(availableAreaChangeMutex);
1135     availableAreaChangeListenerMap.emplace(*listenerIndex, availableAreaChangeListener);
1136     TLOGI(WmsLogTag::DMS, "[DMNDK] register listenerIndex=%{public}d.", *listenerIndex);
1137     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1138 }
1139 
OH_NativeDisplayManager_UnregisterAvailableAreaChangeListener(uint32_t listenerIndex)1140 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterAvailableAreaChangeListener(uint32_t listenerIndex)
1141 {
1142     std::unique_lock<std::shared_mutex> lock(availableAreaChangeMutex);
1143     auto iter = availableAreaChangeListenerMap.find(listenerIndex);
1144     if (iter == availableAreaChangeListenerMap.end()) {
1145         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister listener fail(not find register info).");
1146         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1147     }
1148     DMError ret = DMError::DM_OK;
1149     if (iter->second != nullptr) {
1150         ret = DisplayManager::GetInstance().UnregisterAvailableAreaListener(iter->second);
1151         availableAreaChangeListenerMap.erase(listenerIndex);
1152         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister listener ret=%{public}d", ret);
1153     }
1154     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
1155         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1156 }
1157 
OH_NativeDisplayManager_RegisterDisplayAddListener(OH_NativeDisplayManager_DisplayAddCallback displayAddCallback,uint32_t * listenerIndex)1158 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterDisplayAddListener(
1159     OH_NativeDisplayManager_DisplayAddCallback displayAddCallback, uint32_t* listenerIndex)
1160 {
1161     if (displayAddCallback == nullptr || listenerIndex == nullptr) {
1162         TLOGE(WmsLogTag::DMS, "[DMNDK] register fail(input params null).");
1163         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1164     }
1165     sptr<DisplayAddListener> displayAddListener =
1166         sptr<DisplayAddListener>::MakeSptr(displayAddCallback);
1167     if (CheckDisplayAddHasRegistered(displayAddListener)) {
1168         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
1169         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1170     }
1171     sptr<DisplayManager::IDisplayListener> displayListener = displayAddListener;
1172 
1173     static std::atomic<uint32_t> registerCount = 1;
1174     DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(displayListener);
1175     if (ret != DMError::DM_OK) {
1176         TLOGE(WmsLogTag::DMS, "[DMNDK] register failed ret=%{public}d.", ret);
1177         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1178     }
1179     *listenerIndex = registerCount++;
1180     std::unique_lock<std::shared_mutex> lock(displayAddMutex);
1181     displayAddListenerMap.emplace(*listenerIndex, displayAddListener);
1182     TLOGI(WmsLogTag::DMS, "[DMNDK] register listenerIndex= %{public}d.", *listenerIndex);
1183     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1184 }
1185 
OH_NativeDisplayManager_UnregisterDisplayAddListener(uint32_t listenerIndex)1186 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterDisplayAddListener(uint32_t listenerIndex)
1187 {
1188     std::unique_lock<std::shared_mutex> lock(displayAddMutex);
1189     auto iter = displayAddListenerMap.find(listenerIndex);
1190     if (iter == displayAddListenerMap.end()) {
1191         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fail(not find register info).");
1192         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1193     }
1194     DMError ret = DMError::DM_OK;
1195     if (iter->second != nullptr) {
1196         ret = DisplayManager::GetInstance().UnregisterDisplayListener(iter->second);
1197         displayAddListenerMap.erase(listenerIndex);
1198         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister listener ret=%{public}d", ret);
1199     }
1200     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
1201         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1202 }
1203 
OH_NativeDisplayManager_RegisterDisplayRemoveListener(OH_NativeDisplayManager_DisplayRemoveCallback displayRemoveCallback,uint32_t * listenerIndex)1204 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterDisplayRemoveListener(
1205     OH_NativeDisplayManager_DisplayRemoveCallback displayRemoveCallback, uint32_t* listenerIndex)
1206 {
1207     if (displayRemoveCallback == nullptr || listenerIndex == nullptr) {
1208         TLOGE(WmsLogTag::DMS, "[DMNDK] register fail(input params null).");
1209         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1210     }
1211     sptr<DisplayRemoveListener> displayRemoveListener =
1212         sptr<DisplayRemoveListener>::MakeSptr(displayRemoveCallback);
1213     if (CheckDisplayRemoveHasRegistered(displayRemoveListener)) {
1214         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
1215         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1216     }
1217     sptr<DisplayManager::IDisplayListener> displayListener = displayRemoveListener;
1218 
1219     static std::atomic<uint32_t> registerCount = 1;
1220     DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(displayListener);
1221     if (ret != DMError::DM_OK) {
1222         TLOGE(WmsLogTag::DMS, "[DMNDK] register failed ret=%{public}d.", ret);
1223         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1224     }
1225     *listenerIndex = registerCount++;
1226     std::unique_lock<std::shared_mutex> lock(displayRemoveMutex);
1227     displayRemoveListenerMap.emplace(*listenerIndex, displayRemoveListener);
1228     TLOGI(WmsLogTag::DMS, "[DMNDK] register listenerIndex= %{public}d.", *listenerIndex);
1229     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1230 }
1231 
OH_NativeDisplayManager_UnregisterDisplayRemoveListener(uint32_t listenerIndex)1232 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterDisplayRemoveListener(uint32_t listenerIndex)
1233 {
1234     std::unique_lock<std::shared_mutex> lock(displayRemoveMutex);
1235     auto iter = displayRemoveListenerMap.find(listenerIndex);
1236     if (iter == displayRemoveListenerMap.end()) {
1237         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fail(not find register info).");
1238         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1239     }
1240     DMError ret = DMError::DM_OK;
1241     if (iter->second != nullptr) {
1242         ret = DisplayManager::GetInstance().UnregisterDisplayListener(iter->second);
1243         displayRemoveListenerMap.erase(listenerIndex);
1244         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister listener ret=%{public}d", ret);
1245     }
1246     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
1247         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1248 }
1249 
OH_NativeDisplayManager_CreateAvailableArea(uint64_t displayId,NativeDisplayManager_Rect ** availableArea)1250 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateAvailableArea(
1251     uint64_t displayId, NativeDisplayManager_Rect** availableArea)
1252 {
1253     if (availableArea == nullptr) {
1254         TLOGE(WmsLogTag::DMS, "[DMNDK] input availableArea null.");
1255         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1256     }
1257     int64_t displayCheck = static_cast<int64_t>(displayId);
1258     if (displayCheck < 0) {
1259         TLOGE(WmsLogTag::DMS, "[DMNDK] input display illegal.");
1260         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1261     }
1262     sptr<Display> display = DisplayManager::GetInstance().GetDisplayById(static_cast<DisplayId>(displayId));
1263     if (display == nullptr) {
1264         TLOGE(WmsLogTag::DMS, "[DMNDK] display is  null, id %{public}" PRIu64" ", displayId);
1265         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1266     }
1267     NativeDisplayManager_Rect* availableAreaInfo =
1268         static_cast<NativeDisplayManager_Rect*>(malloc(sizeof(NativeDisplayManager_Rect)));
1269     if (availableAreaInfo == NULL) {
1270         TLOGE(WmsLogTag::DMS, "[DMNDK] memory failed.");
1271         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1272     }
1273 
1274     auto retMemset = memset_s(availableAreaInfo, sizeof(NativeDisplayManager_Rect), 0,
1275         sizeof(NativeDisplayManager_Rect));
1276     if (retMemset != EOK) {
1277         free(availableAreaInfo);
1278         availableAreaInfo = nullptr;
1279         TLOGE(WmsLogTag::DMS, "[DMNDK] memset failed.");
1280         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1281     }
1282 
1283     DMRect displayAvailableArea = DMRect::NONE();
1284     display->GetAvailableArea(displayAvailableArea);
1285     TLOGI(WmsLogTag::DMS, "[DMNDK] posX_=%{public}d posY_=%{public}d width_=%{public}d height_=%{public}d",
1286         displayAvailableArea.posX_, displayAvailableArea.posY_,
1287         displayAvailableArea.width_, displayAvailableArea.height_);
1288     OH_SetDisplayRect(displayAvailableArea, availableAreaInfo);
1289     *availableArea = availableAreaInfo;
1290     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1291 }
1292 
OH_NativeDisplayManager_DestroyAvailableArea(NativeDisplayManager_Rect * availableArea)1293 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_DestroyAvailableArea(NativeDisplayManager_Rect* availableArea)
1294 {
1295     if (availableArea == nullptr) {
1296         TLOGE(WmsLogTag::DMS, "[DMNDK] input availableArea null.");
1297         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1298     }
1299     free(availableArea);
1300     availableArea = nullptr;
1301     TLOGI(WmsLogTag::DMS, "[DMNDK] destroy availableArea end");
1302     return  NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1303 }
1304 
OH_NativeDisplayManager_GetDisplaySourceMode(uint64_t displayId,NativeDisplayManager_SourceMode * sourceMode)1305 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDisplaySourceMode(
1306     uint64_t displayId, NativeDisplayManager_SourceMode* sourceMode)
1307 {
1308     if (sourceMode == nullptr) {
1309         TLOGE(WmsLogTag::DMS, "[DMNDK] input sourceMode null.");
1310         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1311     }
1312     int64_t displayCheck = static_cast<int64_t>(displayId);
1313     if (displayCheck < 0) {
1314         TLOGE(WmsLogTag::DMS, "[DMNDK] input display illegal.");
1315         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1316     }
1317     sptr<Display> display = DisplayManager::GetInstance().GetDisplayById(static_cast<DisplayId>(displayId));
1318     if (display == nullptr) {
1319         TLOGE(WmsLogTag::DMS, "[DMNDK] display is  null, id %{public}" PRIu64" ", displayId);
1320         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1321     }
1322     sptr<DisplayInfo> displayInfo = display->GetDisplayInfo();
1323     if (displayInfo == nullptr) {
1324         TLOGE(WmsLogTag::DMS, "[DMNDK] get displayInfo null.");
1325         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1326     }
1327     DisplaySourceMode getSourceMode = displayInfo->GetDisplaySourceMode();
1328     TLOGI(WmsLogTag::DMS, "[DMNDK] getSourceMode = %{public}d", getSourceMode);
1329     *sourceMode = static_cast<NativeDisplayManager_SourceMode>(getSourceMode);
1330     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1331 }
1332 
OH_NativeDisplayManager_GetDisplayPosition(uint64_t displayId,int32_t * x,int32_t * y)1333 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDisplayPosition(uint64_t displayId, int32_t* x, int32_t* y)
1334 {
1335     if (x == nullptr || y == nullptr) {
1336         TLOGE(WmsLogTag::DMS, "[DMNDK] input x or y is null.");
1337         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1338     }
1339     int64_t displayCheck = static_cast<int64_t>(displayId);
1340     if (displayCheck < 0) {
1341         TLOGE(WmsLogTag::DMS, "[DMNDK] input display illegal.");
1342         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1343     }
1344     sptr<Display> display = DisplayManager::GetInstance().GetDisplayById(static_cast<DisplayId>(displayId));
1345     if (display == nullptr) {
1346         TLOGE(WmsLogTag::DMS, "[DMNDK] display is  null, id %{public}" PRIu64" ", displayId);
1347         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1348     }
1349     sptr<DisplayInfo> displayInfo = display->GetDisplayInfo();
1350     if (displayInfo == nullptr) {
1351         TLOGE(WmsLogTag::DMS, "[DMNDK] get displayInfo null.");
1352         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
1353     }
1354     DisplaySourceMode getSourceMode = displayInfo->GetDisplaySourceMode();
1355     if (getSourceMode == DisplaySourceMode::MAIN || getSourceMode == DisplaySourceMode::EXTEND) {
1356         *x = displayInfo->GetX();
1357         *y = displayInfo->GetY();
1358         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
1359     } else {
1360         TLOGE(WmsLogTag::DMS, "[DMNDK] just main and extend has x, y.");
1361         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM;
1362     }
1363 }