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 class OH_DisplayModeChangeListener : public DisplayManager::IDisplayModeListener {
33 private:
34     OH_NativeDisplayManager_FoldDisplayModeChangeCallback innerDisplayModeChangeFunc_;
35 public:
OH_DisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeFunc)36     explicit OH_DisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeFunc)
37     {
38         innerDisplayModeChangeFunc_ = displayModeChangeFunc;
39     }
OnDisplayModeChanged(FoldDisplayMode displayMode)40     void OnDisplayModeChanged(FoldDisplayMode displayMode)
41     {
42         if (innerDisplayModeChangeFunc_ == NULL) {
43             TLOGI(WmsLogTag::DMS, "[DMNDK] callback is null");
44             return;
45         }
46         TLOGI(WmsLogTag::DMS, "[DMNDK] displayMode callback displayMode=%{public}d", displayMode);
47         innerDisplayModeChangeFunc_(static_cast<NativeDisplayManager_FoldDisplayMode>(displayMode));
48     }
49 };
50 
51 class OH_DisplayChangeListener : public DisplayManager::IDisplayListener {
52 private:
53     OH_NativeDisplayManager_DisplayChangeCallback innerDisplayChangeFunc_;
54 public:
OH_DisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeFunc)55     explicit OH_DisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeFunc)
56     {
57         innerDisplayChangeFunc_ = displayChangeFunc;
58     }
OnCreate(DisplayId)59     void OnCreate(DisplayId)
60     {
61         TLOGI(WmsLogTag::DMS, "[DMNDK] current not support create callback.");
62     }
OnDestroy(DisplayId)63     void OnDestroy(DisplayId)
64     {
65         TLOGI(WmsLogTag::DMS, "[DMNDK] current not support delete callback.");
66     }
OnChange(DisplayId displayId)67     void OnChange(DisplayId displayId)
68     {
69         if (innerDisplayChangeFunc_ == NULL) {
70             TLOGI(WmsLogTag::DMS, "[DMNDK] callback is null");
71             return;
72         }
73         TLOGI(WmsLogTag::DMS, "[DMNDK] callback displayId=%{public}" PRIu64, displayId);
74         innerDisplayChangeFunc_(static_cast<uint64_t>(displayId));
75         sptr<Display> display = DisplayManager::GetInstance().GetDefaultDisplaySync();
76         if (display != nullptr) {
77             TLOGI(WmsLogTag::DMS, "[DMNDK] callback rotation=%{public}d orientation=%{public}d",
78                 display->GetRotation(), display->GetOrientation());
79         }
80     }
81 };
82 
OH_GetDefaultDisplayInfo()83 static sptr<DisplayInfo> OH_GetDefaultDisplayInfo()
84 {
85     sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplaySync();
86     if (defaultDisplay == nullptr) {
87         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display null.");
88         return nullptr;
89     }
90     auto info = defaultDisplay->GetDisplayInfo();
91     if (info == nullptr) {
92         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
93         return nullptr;
94     }
95     return info;
96 }
97 
OH_NativeDisplayManager_GetDefaultDisplayId(uint64_t * displayId)98 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayId(uint64_t *displayId)
99 {
100     if (displayId == nullptr) {
101         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayId null.");
102         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
103     }
104     auto displayInfo = OH_GetDefaultDisplayInfo();
105     if (displayInfo == nullptr) {
106         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
107         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
108     }
109     *displayId = displayInfo->GetDisplayId();
110     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
111 }
112 
OH_NativeDisplayManager_GetDefaultDisplayWidth(int32_t * displayWidth)113 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayWidth(int32_t *displayWidth)
114 {
115     if (displayWidth == nullptr) {
116         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayWidth null.");
117         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
118     }
119     auto displayInfo = OH_GetDefaultDisplayInfo();
120     if (displayInfo == nullptr) {
121         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
122         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
123     }
124     *displayWidth = displayInfo->GetWidth();
125     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
126 }
127 
OH_NativeDisplayManager_GetDefaultDisplayHeight(int32_t * displayHeight)128 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayHeight(int32_t *displayHeight)
129 {
130     if (displayHeight == nullptr) {
131         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayHeight null.");
132         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
133     }
134     auto displayInfo = OH_GetDefaultDisplayInfo();
135     if (displayInfo == nullptr) {
136         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
137         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
138     }
139     *displayHeight = displayInfo->GetHeight();
140     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
141 }
142 
OH_NativeDisplayManager_GetDefaultDisplayRotation(NativeDisplayManager_Rotation * displayRotation)143 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayRotation(
144     NativeDisplayManager_Rotation *displayRotation)
145 {
146     if (displayRotation == nullptr) {
147         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayRotation null.");
148         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
149     }
150     auto displayInfo = OH_GetDefaultDisplayInfo();
151     if (displayInfo == nullptr) {
152         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
153         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
154     }
155     *displayRotation = static_cast<NativeDisplayManager_Rotation>(displayInfo->GetRotation());
156     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
157 }
158 
OH_NativeDisplayManager_GetDefaultDisplayOrientation(NativeDisplayManager_Orientation * displayOrientation)159 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayOrientation(
160     NativeDisplayManager_Orientation *displayOrientation)
161 {
162     if (displayOrientation == nullptr) {
163         TLOGE(WmsLogTag::DMS, "[DMNDK] input displayOrientation null.");
164         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
165     }
166     auto displayInfo = OH_GetDefaultDisplayInfo();
167     if (displayInfo == nullptr) {
168         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
169         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
170     }
171     *displayOrientation = static_cast<NativeDisplayManager_Orientation>(displayInfo->GetDisplayOrientation());
172     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
173 }
174 
OH_NativeDisplayManager_GetDefaultDisplayVirtualPixelRatio(float * virtualPixel)175 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayVirtualPixelRatio(float *virtualPixel)
176 {
177     if (virtualPixel == nullptr) {
178         TLOGE(WmsLogTag::DMS, "[DMNDK] input virtualPixel null.");
179         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
180     }
181     auto displayInfo = OH_GetDefaultDisplayInfo();
182     if (displayInfo == nullptr) {
183         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
184         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
185     }
186     *virtualPixel = displayInfo->GetVirtualPixelRatio();
187     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
188 }
189 
OH_NativeDisplayManager_GetDefaultDisplayRefreshRate(uint32_t * refreshRate)190 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayRefreshRate(uint32_t *refreshRate)
191 {
192     if (refreshRate == nullptr) {
193         TLOGE(WmsLogTag::DMS, "[DMNDK] input refreshRate null.");
194         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
195     }
196     auto displayInfo = OH_GetDefaultDisplayInfo();
197     if (displayInfo == nullptr) {
198         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
199         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
200     }
201     *refreshRate = displayInfo->GetRefreshRate();
202     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
203 }
204 
OH_NativeDisplayManager_GetDefaultDisplayDensityDpi(int32_t * densityDpi)205 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityDpi(int32_t *densityDpi)
206 {
207     if (densityDpi == nullptr) {
208         TLOGE(WmsLogTag::DMS, "[DMNDK] input densityDpi null.");
209         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
210     }
211     auto displayInfo = OH_GetDefaultDisplayInfo();
212     if (displayInfo == nullptr) {
213         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
214         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
215     }
216     *densityDpi = displayInfo->GetVirtualPixelRatio() * DOT_PER_INCH;
217     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
218 }
219 
OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(float * densityPixels)220 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityPixels(float *densityPixels)
221 {
222     if (densityPixels == nullptr) {
223         TLOGE(WmsLogTag::DMS, "[DMNDK] input densityPixels null.");
224         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
225     }
226     auto displayInfo = OH_GetDefaultDisplayInfo();
227     if (displayInfo == nullptr) {
228         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
229         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
230     }
231     *densityPixels = displayInfo->GetVirtualPixelRatio();
232     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
233 }
234 
OH_NativeDisplayManager_GetDefaultDisplayScaledDensity(float * scaledDensity)235 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayScaledDensity(float *scaledDensity)
236 {
237     if (scaledDensity == nullptr) {
238         TLOGE(WmsLogTag::DMS, "[DMNDK] input scaledDensity null.");
239         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
240     }
241     auto displayInfo = OH_GetDefaultDisplayInfo();
242     if (displayInfo == nullptr) {
243         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
244         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
245     }
246     *scaledDensity = displayInfo->GetVirtualPixelRatio();
247     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
248 }
249 
OH_NativeDisplayManager_GetDefaultDisplayDensityXdpi(float * xDpi)250 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityXdpi(float *xDpi)
251 {
252     if (xDpi == nullptr) {
253         TLOGE(WmsLogTag::DMS, "[DMNDK] input xDpi null.");
254         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
255     }
256     auto displayInfo = OH_GetDefaultDisplayInfo();
257     if (displayInfo == nullptr) {
258         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
259         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
260     }
261     *xDpi = displayInfo->GetXDpi();
262     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
263 }
264 
OH_NativeDisplayManager_GetDefaultDisplayDensityYdpi(float * yDpi)265 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetDefaultDisplayDensityYdpi(float *yDpi)
266 {
267     if (yDpi == nullptr) {
268         TLOGE(WmsLogTag::DMS, "[DMNDK] input yDpi null.");
269         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
270     }
271     auto displayInfo = OH_GetDefaultDisplayInfo();
272     if (displayInfo == nullptr) {
273         TLOGE(WmsLogTag::DMS, "[DMNDK] get default display info null.");
274         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
275     }
276     *yDpi = displayInfo->GetYDpi();
277     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
278 }
279 
OH_SetDisplayRect(const DMRect & dmRect,NativeDisplayManager_Rect * displayRectItem)280 static void OH_SetDisplayRect(const DMRect &dmRect, NativeDisplayManager_Rect *displayRectItem)
281 {
282     displayRectItem->left = dmRect.posX_;
283     displayRectItem->top = dmRect.posY_;
284     displayRectItem->width = dmRect.width_;
285     displayRectItem->height = dmRect.height_;
286 }
287 
OH_CreateBoundingRects(const std::vector<DMRect> & boundingRects)288 static NativeDisplayManager_Rect* OH_CreateBoundingRects(const std::vector<DMRect> &boundingRects)
289 {
290     int32_t boundSize = static_cast<int32_t>(boundingRects.size());
291     NativeDisplayManager_Rect *ohBoundingRects =
292         static_cast<NativeDisplayManager_Rect*>(malloc(sizeof(NativeDisplayManager_Rect) * boundSize));
293     if (ohBoundingRects == NULL) {
294         TLOGE(WmsLogTag::DMS, "[DMNDK] memory failed.");
295         return NULL;
296     }
297     auto retMemset = memset_s(ohBoundingRects, sizeof(NativeDisplayManager_Rect) * boundSize, 0,
298         sizeof(NativeDisplayManager_Rect) * boundSize);
299     if (retMemset != 0) {
300         free(ohBoundingRects);
301         ohBoundingRects = NULL;
302         TLOGE(WmsLogTag::DMS, "[DMNDK] memset failed.");
303         return NULL;
304     }
305     TLOGI(WmsLogTag::DMS, "[DMNDK] bounding size:%{public}d.", boundSize);
306     for (int i = 0; i < boundSize; i++) {
307         OH_SetDisplayRect(boundingRects[i], (ohBoundingRects + i));
308     }
309     return ohBoundingRects;
310 }
311 
OH_SetWaterfallDisplayAreaRects(const WaterfallDisplayAreaRects & waterArea,NativeDisplayManager_CutoutInfo * ohCutoutInfo)312 static void OH_SetWaterfallDisplayAreaRects(const WaterfallDisplayAreaRects &waterArea,
313     NativeDisplayManager_CutoutInfo *ohCutoutInfo)
314 {
315     if (ohCutoutInfo == NULL) {
316         TLOGE(WmsLogTag::DMS, "[DMNDK] ohCutoutInfo is null.");
317         return;
318     }
319     TLOGI(WmsLogTag::DMS, "[DMNDK] set waterfall Area.");
320     OH_SetDisplayRect(waterArea.left, &(ohCutoutInfo->waterfallDisplayAreaRects.left));
321     OH_SetDisplayRect(waterArea.top, &(ohCutoutInfo->waterfallDisplayAreaRects.top));
322     OH_SetDisplayRect(waterArea.right, &(ohCutoutInfo->waterfallDisplayAreaRects.right));
323     OH_SetDisplayRect(waterArea.bottom, &(ohCutoutInfo->waterfallDisplayAreaRects.bottom));
324 }
325 
OH_CreateCutoutInfoObject(const sptr<CutoutInfo> & cutoutInfo)326 static NativeDisplayManager_CutoutInfo* OH_CreateCutoutInfoObject(const sptr<CutoutInfo> &cutoutInfo)
327 {
328     NativeDisplayManager_CutoutInfo *ohCutoutInfo =
329         static_cast<NativeDisplayManager_CutoutInfo*>(malloc(sizeof(NativeDisplayManager_CutoutInfo)));
330     if (ohCutoutInfo == NULL) {
331         TLOGE(WmsLogTag::DMS, "[DMNDK] memory failed.");
332         return NULL;
333     }
334     auto retMemset = memset_s(ohCutoutInfo, sizeof(NativeDisplayManager_CutoutInfo), 0,
335         sizeof(NativeDisplayManager_CutoutInfo));
336     if (retMemset != 0) {
337         free(ohCutoutInfo);
338         ohCutoutInfo = NULL;
339         TLOGE(WmsLogTag::DMS, "[DMNDK] memset failed.");
340         return NULL;
341     }
342     std::vector<DMRect> boundingRects = cutoutInfo->GetBoundingRects();
343     WaterfallDisplayAreaRects waterRects = cutoutInfo->GetWaterfallDisplayAreaRects();
344     ohCutoutInfo->boundingRectsLength = static_cast<int32_t>(boundingRects.size());
345     TLOGI(WmsLogTag::DMS, "[DMNDK] boundingRectsLength=%{public}d.", ohCutoutInfo->boundingRectsLength);
346     ohCutoutInfo->boundingRects = OH_CreateBoundingRects(boundingRects);
347     if (ohCutoutInfo->boundingRects == NULL) {
348         TLOGE(WmsLogTag::DMS, "[DMNDK] create bounding rects failed.");
349         free(ohCutoutInfo);
350         ohCutoutInfo = NULL;
351         return NULL;
352     }
353     OH_SetWaterfallDisplayAreaRects(waterRects, ohCutoutInfo);
354     return ohCutoutInfo;
355 }
356 
OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo ** cutoutInfo)357 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateDefaultDisplayCutoutInfo(
358     NativeDisplayManager_CutoutInfo **cutoutInfo)
359 {
360     if (cutoutInfo == NULL) {
361         TLOGE(WmsLogTag::DMS, "[DMNDK] input cutoutInfo null.");
362         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
363     }
364     TLOGI(WmsLogTag::DMS, "[DMNDK] get display cutout info.");
365     sptr<Display> defaultDisplay = DisplayManager::GetInstance().GetDefaultDisplaySync();
366     if (defaultDisplay == nullptr) {
367         TLOGE(WmsLogTag::DMS, "[DMNDK] get cutout info (display) null.");
368         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
369     }
370     sptr<CutoutInfo> cutoutInfoInner = defaultDisplay->GetCutoutInfo();
371     if (cutoutInfoInner == nullptr) {
372         TLOGE(WmsLogTag::DMS, "[DMNDK] get cutout info (from display) null.");
373         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
374     }
375     *cutoutInfo = OH_CreateCutoutInfoObject(cutoutInfoInner);
376     if (*cutoutInfo == NULL) {
377         TLOGE(WmsLogTag::DMS, "[DMNDK] convert cutout info null.");
378         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
379     }
380     TLOGI(WmsLogTag::DMS, "[DMNDK] get display cutout info success.");
381     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
382 }
383 
OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(NativeDisplayManager_CutoutInfo * cutoutInfo)384 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_DestroyDefaultDisplayCutoutInfo(
385     NativeDisplayManager_CutoutInfo *cutoutInfo)
386 {
387     if (cutoutInfo == NULL) {
388         TLOGE(WmsLogTag::DMS, "[DMNDK] input cutoutInfo null pointer.");
389         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
390     }
391     if (cutoutInfo->boundingRects != NULL) {
392         free(cutoutInfo->boundingRects);
393         cutoutInfo->boundingRects = NULL;
394     }
395     free(cutoutInfo);
396     cutoutInfo = NULL;
397     TLOGI(WmsLogTag::DMS, "[DMNDK] destroy cutoutInfo end.");
398     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
399 }
400 
OH_NativeDisplayManager_IsFoldable()401 bool OH_NativeDisplayManager_IsFoldable()
402 {
403     bool isFoldable = DisplayManager::GetInstance().IsFoldable();
404     TLOGI(WmsLogTag::DMS, "[DMNDK] get display isFoldable=%{public}d.", isFoldable);
405     return isFoldable;
406 }
407 
OH_NativeDisplayManager_GetFoldDisplayMode(NativeDisplayManager_FoldDisplayMode * foldDisplayMode)408 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_GetFoldDisplayMode(
409     NativeDisplayManager_FoldDisplayMode *foldDisplayMode)
410 {
411     TLOGI(WmsLogTag::DMS, "[DMNDK] get fold display mode.");
412     if (foldDisplayMode == NULL) {
413         TLOGE(WmsLogTag::DMS, "[DMNDK] input foldDisplayMode null.");
414         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
415     }
416     if (!DisplayManager::GetInstance().IsFoldable()) {
417         TLOGE(WmsLogTag::DMS, "[DMNDK] device is not foldable.");
418         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
419     }
420     FoldDisplayMode foldMode = DisplayManager::GetInstance().GetFoldDisplayMode();
421     switch (foldMode) {
422         case FoldDisplayMode::SUB:
423             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_SUB;
424             break;
425         case FoldDisplayMode::MAIN:
426             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_MAIN;
427             break;
428         case FoldDisplayMode::FULL:
429             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_FULL;
430             break;
431         case FoldDisplayMode::COORDINATION:
432             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_COORDINATION;
433             break;
434         default:
435             *foldDisplayMode = NativeDisplayManager_FoldDisplayMode::DISPLAY_MANAGER_FOLD_DISPLAY_MODE_UNKNOWN;
436             break;
437     }
438     TLOGI(WmsLogTag::DMS, "[DMNDK] current fold display mode: %{public}d.", *foldDisplayMode);
439     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
440 }
441 
442 std::shared_mutex foldChangeMutex;
443 std::map<uint32_t, OH_NativeDisplayManager_FoldDisplayModeChangeCallback> g_foldChangeCallbackMap;
444 std::map<uint32_t, sptr<DisplayManager::IDisplayModeListener>> g_foldDisplayModeChangeListenerMap;
445 
CheckFoldChangeHasRegistered(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback)446 bool CheckFoldChangeHasRegistered(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback)
447 {
448     if (g_foldChangeCallbackMap.empty()) {
449         return false;
450     }
451     for (auto iter : g_foldChangeCallbackMap) {
452         if (iter.second == displayModeChangeCallback) {
453             return true;
454         }
455     }
456     return false;
457 }
458 
OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener(OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback,uint32_t * listenerIndex)459 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterFoldDisplayModeChangeListener(
460     OH_NativeDisplayManager_FoldDisplayModeChangeCallback displayModeChangeCallback, uint32_t *listenerIndex)
461 {
462     TLOGI(WmsLogTag::DMS, "[DMNDK] register fold display mode change listener.");
463     if (displayModeChangeCallback == NULL || listenerIndex == NULL) {
464         TLOGE(WmsLogTag::DMS, "[DMNDK] input params null.");
465         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
466     }
467     if (!DisplayManager::GetInstance().IsFoldable()) {
468         TLOGE(WmsLogTag::DMS, "[DMNDK] device is not foldable.");
469         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
470     }
471     std::unique_lock<std::shared_mutex> lock(foldChangeMutex);
472     if (CheckFoldChangeHasRegistered(displayModeChangeCallback)) {
473         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
474         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
475     }
476     sptr<DisplayManager::IDisplayModeListener> displayModeListener =
477         sptr<OH_DisplayModeChangeListener>::MakeSptr(displayModeChangeCallback);
478     if (displayModeListener == nullptr) {
479         TLOGE(WmsLogTag::DMS, "[DMNDK] display mode listener MakeSptr fail.");
480         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
481     }
482     static std::atomic<uint32_t> registerCount = 1;
483     DMError ret = DisplayManager::GetInstance().RegisterDisplayModeListener(displayModeListener);
484     if (ret != DMError::DM_OK) {
485         TLOGE(WmsLogTag::DMS, "[DMNDK] display mode listener register failed ret=%{public}d.", ret);
486         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
487     }
488     *listenerIndex = registerCount++;
489     g_foldChangeCallbackMap.emplace(*listenerIndex, displayModeChangeCallback);
490     g_foldDisplayModeChangeListenerMap.emplace(*listenerIndex, displayModeListener);
491     TLOGI(WmsLogTag::DMS, "[DMNDK] register fold change success and listenerIndex= %{public}d.", *listenerIndex);
492     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
493 }
494 
OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(uint32_t listenerIndex)495 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterFoldDisplayModeChangeListener(uint32_t listenerIndex)
496 {
497     TLOGI(WmsLogTag::DMS, "[DMNDK] unregister fold display mode change listener %{public}d.", listenerIndex);
498     if (!DisplayManager::GetInstance().IsFoldable()) {
499         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fail(device is not foldable).");
500         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
501     }
502     std::unique_lock<std::shared_mutex> lock(foldChangeMutex);
503     auto iter = g_foldDisplayModeChangeListenerMap.find(listenerIndex);
504     if (iter == g_foldDisplayModeChangeListenerMap.end()) {
505         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister listener fail(not find register info).");
506         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
507     }
508     DMError ret = DMError::DM_OK;
509     if (iter->second != nullptr) {
510         ret = DisplayManager::GetInstance().UnregisterDisplayModeListener(iter->second);
511         g_foldDisplayModeChangeListenerMap.erase(listenerIndex);
512         g_foldChangeCallbackMap.erase(listenerIndex);
513         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister listener ert=%{public}d", ret);
514     }
515     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
516         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
517 }
518 
519 std::shared_mutex displayChangeMutex;
520 std::map<uint32_t, OH_NativeDisplayManager_DisplayChangeCallback> g_displayChangeCallbackMap;
521 std::map<uint32_t, sptr<DisplayManager::IDisplayListener>> g_displayChangeListenerMap;
522 
CheckDisplayChangeHasRegistered(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback)523 bool CheckDisplayChangeHasRegistered(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback)
524 {
525     if (g_displayChangeCallbackMap.empty()) {
526         return false;
527     }
528     for (auto iter : g_displayChangeCallbackMap) {
529         if (iter.second == displayChangeCallback) {
530             return true;
531         }
532     }
533     return false;
534 }
535 
OH_NativeDisplayManager_RegisterDisplayChangeListener(OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback,uint32_t * listenerIndex)536 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_RegisterDisplayChangeListener(
537     OH_NativeDisplayManager_DisplayChangeCallback displayChangeCallback, uint32_t *listenerIndex)
538 {
539     TLOGI(WmsLogTag::DMS, "[DMNDK] register display change listener.");
540     if (displayChangeCallback == NULL || listenerIndex == NULL) {
541         TLOGE(WmsLogTag::DMS, "[DMNDK] register fail(input params null).");
542         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
543     }
544     std::unique_lock<std::shared_mutex> lock(displayChangeMutex);
545     if (CheckDisplayChangeHasRegistered(displayChangeCallback)) {
546         TLOGE(WmsLogTag::DMS, "[DMNDK] input params error (has registered).");
547         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
548     }
549     sptr<DisplayManager::IDisplayListener> displayListener =
550         sptr<OH_DisplayChangeListener>::MakeSptr(displayChangeCallback);
551     if (displayListener == nullptr) {
552         TLOGE(WmsLogTag::DMS, "[DMNDK] register display change MakeSptr fail.");
553         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
554     }
555     static std::atomic<uint32_t> registerCount = 1;
556     DMError ret = DisplayManager::GetInstance().RegisterDisplayListener(displayListener);
557     if (ret != DMError::DM_OK) {
558         TLOGE(WmsLogTag::DMS, "[DMNDK] register failed ret=%{public}d.", ret);
559         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
560     }
561     *listenerIndex = registerCount++;
562     g_displayChangeCallbackMap.emplace(*listenerIndex, displayChangeCallback);
563     g_displayChangeListenerMap.emplace(*listenerIndex, displayListener);
564     TLOGI(WmsLogTag::DMS, "[DMNDK] register listenerIndex= %{public}d.", *listenerIndex);
565     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
566 }
567 
OH_NativeDisplayManager_UnregisterDisplayChangeListener(uint32_t listenerIndex)568 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_UnregisterDisplayChangeListener(uint32_t listenerIndex)
569 {
570     TLOGI(WmsLogTag::DMS, "[DMNDK] unregister display change listener %{public}d.", listenerIndex);
571     std::unique_lock<std::shared_mutex> lock(displayChangeMutex);
572     auto iter = g_displayChangeListenerMap.find(listenerIndex);
573     if (iter == g_displayChangeListenerMap.end()) {
574         TLOGE(WmsLogTag::DMS, "[DMNDK] unregister fail(not find register info).");
575         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
576     }
577     DMError ret = DMError::DM_OK;
578     if (iter->second != nullptr) {
579         ret = DisplayManager::GetInstance().UnregisterDisplayListener(iter->second);
580         g_displayChangeListenerMap.erase(listenerIndex);
581         g_displayChangeCallbackMap.erase(listenerIndex);
582         TLOGI(WmsLogTag::DMS, "[DMNDK] unregister listener ret=%{public}d", ret);
583     }
584     return ret == DMError::DM_OK ? NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK :
585         NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
586 }
587 
NativeDisplayManager_SetColorSpace(NativeDisplayManager_DisplayInfo * displayInfo,sptr<DisplayInfo> info)588 static void NativeDisplayManager_SetColorSpace(NativeDisplayManager_DisplayInfo *displayInfo, sptr<DisplayInfo> info)
589 {
590     std::vector<uint32_t> colorSpaces = info->GetColorSpaces();
591     if (colorSpaces.empty()) {
592         TLOGW(WmsLogTag::DMS, "[DMNDK] colorSpaces is empty displayId=%{public}u", displayInfo->id);
593         return;
594     }
595     displayInfo->colorSpace = (NativeDisplayManager_DisplayColorSpace*)malloc(
596         sizeof(NativeDisplayManager_DisplayColorSpace));
597     if (displayInfo->colorSpace == nullptr) {
598         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc color space failed");
599         return;
600     }
601     auto retMemset = memset_s(displayInfo->colorSpace, sizeof(NativeDisplayManager_DisplayColorSpace), 0,
602         sizeof(NativeDisplayManager_DisplayColorSpace));
603     if (retMemset != EOK) {
604         TLOGE(WmsLogTag::DMS, "[DMNDK] memset color space failed");
605         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
606         return;
607     }
608     displayInfo->colorSpace->colorSpaceLength = static_cast<uint32_t>(colorSpaces.size());
609     displayInfo->colorSpace->colorSpaces = (uint32_t*)malloc(sizeof(uint32_t) * colorSpaces.size());
610     if (displayInfo->colorSpace->colorSpaces == nullptr) {
611         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc color spaces failed");
612         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
613         return;
614     }
615     retMemset = memset_s(displayInfo->colorSpace->colorSpaces, sizeof(uint32_t) * colorSpaces.size(), 0,
616         sizeof(uint32_t) * colorSpaces.size());
617     if (retMemset != EOK) {
618         TLOGE(WmsLogTag::DMS, "[DMNDK] memset color spaces failed");
619         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace->colorSpaces);
620         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
621         return;
622     }
623 
624     uint32_t colorLoop = 0;
625     for (const auto colorSpace : colorSpaces) {
626         DM_GraphicCM_ColorSpaceType colorSpaceValue = static_cast<DM_GraphicCM_ColorSpaceType>(colorSpace);
627         if (DM_NATIVE_TO_NDK_COLOR_SPACE_TYPE_MAP.find(colorSpaceValue) ==
628             DM_NATIVE_TO_NDK_COLOR_SPACE_TYPE_MAP.end()) {
629             TLOGW(WmsLogTag::DMS, "[DMNDK] color spaces[%{public}d] not in map.", colorSpace);
630             continue;
631         }
632         displayInfo->colorSpace->colorSpaces[colorLoop] =
633             static_cast<uint32_t>(DM_NATIVE_TO_NDK_COLOR_SPACE_TYPE_MAP.at(colorSpaceValue));
634         colorLoop++;
635     }
636     TLOGI(WmsLogTag::DMS, "[DMNDK] color spaces count:%{public}u.", colorLoop);
637 }
638 
NativeDisplayManager_SetHdrFormat(NativeDisplayManager_DisplayInfo * displayInfo,sptr<DisplayInfo> info)639 static void NativeDisplayManager_SetHdrFormat(NativeDisplayManager_DisplayInfo *displayInfo, sptr<DisplayInfo> info)
640 {
641     std::vector<uint32_t> hdrFormats = info->GetHdrFormats();
642     if (hdrFormats.empty()) {
643         TLOGW(WmsLogTag::DMS, "[DMNDK] hdrFormats is empty displayId=%{public}u", displayInfo->id);
644         return;
645     }
646     displayInfo->hdrFormat = (NativeDisplayManager_DisplayHdrFormat*)malloc(
647         sizeof(NativeDisplayManager_DisplayHdrFormat));
648     if (displayInfo->hdrFormat == nullptr) {
649         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc hdr format failed");
650         return;
651     }
652     auto retMemset = memset_s(displayInfo->hdrFormat, sizeof(NativeDisplayManager_DisplayHdrFormat), 0,
653         sizeof(NativeDisplayManager_DisplayHdrFormat));
654     if (retMemset != EOK) {
655         TLOGE(WmsLogTag::DMS, "[DMNDK] memset hdr format failed");
656         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
657         return;
658     }
659     displayInfo->hdrFormat->hdrFormatLength = static_cast<uint32_t>(hdrFormats.size());
660     displayInfo->hdrFormat->hdrFormats = (uint32_t*)malloc(sizeof(uint32_t) * hdrFormats.size());
661     if (displayInfo->hdrFormat->hdrFormats == nullptr) {
662         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc hdr format failed");
663         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
664         return;
665     }
666     retMemset = memset_s(displayInfo->hdrFormat->hdrFormats, sizeof(uint32_t) * hdrFormats.size(), 0,
667         sizeof(uint32_t) * hdrFormats.size());
668     if (retMemset != EOK) {
669         TLOGE(WmsLogTag::DMS, "[DMNDK] memset hdr format failed");
670         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat->hdrFormats);
671         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
672         return;
673     }
674 
675     uint32_t hdrLoop = 0;
676     for (const auto hdrFormat : hdrFormats) {
677         DM_ScreenHDRFormat hdrFormatValue = static_cast<DM_ScreenHDRFormat>(hdrFormat);
678         if (DM_NATIVE_TO_NDK_HDR_FORMAT_TYPE_MAP.find(hdrFormatValue) == DM_NATIVE_TO_NDK_HDR_FORMAT_TYPE_MAP.end()) {
679             TLOGW(WmsLogTag::DMS, "[DMNDK] hdr format[%{public}d] not in map.", hdrFormat);
680             continue;
681         }
682         displayInfo->hdrFormat->hdrFormats[hdrLoop] =
683             static_cast<uint32_t>(DM_NATIVE_TO_NDK_HDR_FORMAT_TYPE_MAP.at(hdrFormatValue));
684         hdrLoop++;
685     }
686     TLOGI(WmsLogTag::DMS, "[DMNDK] hdr format count:%{public}u", hdrLoop);
687 }
688 
NativeDisplayManager_SetDisplayInfo(NativeDisplayManager_DisplayInfo * displayInfo,sptr<DisplayInfo> info)689 static void NativeDisplayManager_SetDisplayInfo(NativeDisplayManager_DisplayInfo *displayInfo,
690     sptr<DisplayInfo> info)
691 {
692     displayInfo->id = static_cast<uint32_t>(info->GetDisplayId());
693     displayInfo->width = info->GetWidth();
694     displayInfo->height = info->GetHeight();
695     displayInfo->orientation = static_cast<NativeDisplayManager_Orientation>(info->GetDisplayOrientation());
696     displayInfo->rotation = static_cast<NativeDisplayManager_Rotation>(info->GetRotation());
697     displayInfo->refreshRate = info->GetRefreshRate();
698     displayInfo->availableWidth = info->GetAvailableWidth();
699     displayInfo->availableHeight = info->GetAvailableHeight();
700     displayInfo->physicalWidth = info->GetPhysicalWidth();
701     displayInfo->physicalHeight = info->GetPhysicalHeight();
702     displayInfo->densityDPI = info->GetVirtualPixelRatio() * DOT_PER_INCH;
703     displayInfo->densityPixels = info->GetVirtualPixelRatio();
704     displayInfo->scaledDensity = info->GetVirtualPixelRatio();
705     displayInfo->xDPI = info->GetXDpi();
706     displayInfo->yDPI = info->GetYDpi();
707     displayInfo->isAlive = info->GetAliveStatus();
708     if (DM_NATIVE_TO_NDK_DISPLAY_STATE_MAP.find(info->GetDisplayState()) != DM_NATIVE_TO_NDK_DISPLAY_STATE_MAP.end()) {
709         displayInfo->state = static_cast<NativeDisplayManager_DisplayState>(
710             DM_NATIVE_TO_NDK_DISPLAY_STATE_MAP.at(info->GetDisplayState()));
711     } else {
712         displayInfo->state = static_cast<NativeDisplayManager_DisplayState>(DM_DisplayStateMode::STATE_UNKNOWN);
713     }
714     /* color space */
715     NativeDisplayManager_SetColorSpace(displayInfo, info);
716     /* hdr format */
717     NativeDisplayManager_SetHdrFormat(displayInfo, info);
718     TLOGI(WmsLogTag::DMS, "[DMNDK] set display id[%{public}u] finish.", displayInfo->id);
719 }
720 
NativeDisplayManager_SetDisplaysInfo(const std::vector<sptr<Display>> & displays,NativeDisplayManager_DisplayInfo * displaysInfo)721 static NativeDisplayManager_ErrorCode NativeDisplayManager_SetDisplaysInfo(const std::vector<sptr<Display>>& displays,
722     NativeDisplayManager_DisplayInfo *displaysInfo)
723 {
724     uint32_t i = 0;
725     for (auto& display : displays) {
726         if (display == nullptr) {
727             TLOGE(WmsLogTag::DMS, "[DMNDK] get display null.");
728             continue;
729         }
730         auto info = display->GetDisplayInfo();
731         if (info == nullptr) {
732             TLOGE(WmsLogTag::DMS, "[DMNDK] get display id[%{public}" PRIu64"] info null.", display->GetId());
733             continue;
734         }
735         int ret = memcpy_s(displaysInfo[i].name, OH_DISPLAY_NAME_LENGTH, info->GetName().c_str(),
736             OH_DISPLAY_NAME_LENGTH);
737         if (ret != EOK) {
738             TLOGE(WmsLogTag::DMS, "[DMNDK] failed to memcpy name.");
739             return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
740         }
741         NativeDisplayManager_SetDisplayInfo(displaysInfo + i, info);
742         i++;
743     }
744     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
745 }
746 
NativeDisplayManager_DestroyDisplaysInfoInner(uint32_t displaysLength,NativeDisplayManager_DisplayInfo * displaysInfo)747 static void NativeDisplayManager_DestroyDisplaysInfoInner(uint32_t displaysLength,
748     NativeDisplayManager_DisplayInfo *displaysInfo)
749 {
750     if (displaysLength == 0 || displaysInfo == nullptr) {
751         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
752         return;
753     }
754     for (uint32_t i = 0; i < displaysLength; i++) {
755         NativeDisplayManager_DisplayInfo displayItem = displaysInfo[i];
756         if (displayItem.colorSpace != nullptr) {
757             if (displayItem.colorSpace->colorSpaces != nullptr) {
758                 DISPLAY_MANAGER_FREE_MEMORY(displayItem.colorSpace->colorSpaces);
759             }
760             DISPLAY_MANAGER_FREE_MEMORY(displayItem.colorSpace);
761         }
762         if (displayItem.hdrFormat != nullptr) {
763             if (displayItem.hdrFormat->hdrFormats != nullptr) {
764                 DISPLAY_MANAGER_FREE_MEMORY(displayItem.hdrFormat->hdrFormats);
765             }
766             DISPLAY_MANAGER_FREE_MEMORY(displayItem.hdrFormat);
767         }
768     }
769     DISPLAY_MANAGER_FREE_MEMORY(displaysInfo);
770 }
771 
OH_NativeDisplayManager_CreateAllDisplays(NativeDisplayManager_DisplaysInfo ** allDisplays)772 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateAllDisplays(
773     NativeDisplayManager_DisplaysInfo **allDisplays)
774 {
775     if (allDisplays == nullptr) {
776         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
777         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
778     }
779     /* displays is not null make sure by GetAllDisplays*/
780     std::vector<sptr<Display>> displays = DisplayManager::GetInstance().GetAllDisplays();
781     if (displays.empty()) {
782         TLOGE(WmsLogTag::DMS, "[DMNDK] displays is empty.");
783         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
784     }
785     NativeDisplayManager_DisplaysInfo *displaysInner =
786         (NativeDisplayManager_DisplaysInfo*)malloc(sizeof(NativeDisplayManager_DisplaysInfo));
787     if (displaysInner == nullptr) {
788         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc displays inner failed.");
789         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
790     }
791     int32_t retMemset = memset_s(displaysInner, sizeof(NativeDisplayManager_DisplaysInfo), 0,
792         sizeof(NativeDisplayManager_DisplaysInfo));
793     if (retMemset != EOK) {
794         TLOGE(WmsLogTag::DMS, "[DMNDK] memset displays failed.");
795         DISPLAY_MANAGER_FREE_MEMORY(displaysInner);
796         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
797     }
798     size_t displaySize = displays.size();
799     displaysInner->displaysLength = static_cast<uint32_t>(displaySize);
800     NativeDisplayManager_DisplayInfo *displaysInfo =
801         (NativeDisplayManager_DisplayInfo*)malloc(sizeof(NativeDisplayManager_DisplayInfo) * displaySize);
802     if (displaysInfo == nullptr) {
803         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc displaysInfo failed.");
804         DISPLAY_MANAGER_FREE_MEMORY(displaysInner);
805         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
806     }
807     retMemset = memset_s(displaysInfo, sizeof(NativeDisplayManager_DisplayInfo) * displaySize, 0,
808         sizeof(NativeDisplayManager_DisplayInfo) * displaySize);
809     NativeDisplayManager_ErrorCode setRet = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
810     if (retMemset == EOK) {
811         setRet = NativeDisplayManager_SetDisplaysInfo(displays, displaysInfo);
812     }
813     if (retMemset != EOK || setRet != NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK) {
814         TLOGE(WmsLogTag::DMS, "[DMNDK] memset or set displaysInfo failed setRet=%{public}d.", setRet);
815         NativeDisplayManager_DestroyDisplaysInfoInner(displaysInner->displaysLength, displaysInfo);
816         DISPLAY_MANAGER_FREE_MEMORY(displaysInner);
817         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
818     }
819     displaysInner->displaysInfo = displaysInfo;
820     *allDisplays = displaysInner;
821     return setRet;
822 }
823 
OH_NativeDisplayManager_DestroyAllDisplays(NativeDisplayManager_DisplaysInfo * allDisplays)824 void OH_NativeDisplayManager_DestroyAllDisplays(NativeDisplayManager_DisplaysInfo *allDisplays)
825 {
826     if (allDisplays == nullptr) {
827         TLOGI(WmsLogTag::DMS, "[DMNDK] param is null.");
828         return;
829     }
830     if (allDisplays->displaysInfo == nullptr) {
831         DISPLAY_MANAGER_FREE_MEMORY(allDisplays);
832         return;
833     }
834     NativeDisplayManager_DestroyDisplaysInfoInner(allDisplays->displaysLength, allDisplays->displaysInfo);
835     DISPLAY_MANAGER_FREE_MEMORY(allDisplays);
836 }
837 
NativeDisplayManager_FillDisplayInfo(sptr<Display> display,NativeDisplayManager_ErrorCode * errCode)838 static NativeDisplayManager_DisplayInfo* NativeDisplayManager_FillDisplayInfo(sptr<Display> display,
839     NativeDisplayManager_ErrorCode *errCode)
840 {
841     sptr<DisplayInfo> info = display->GetDisplayInfo();
842     if (info == nullptr) {
843         TLOGE(WmsLogTag::DMS, "[DMNDK] get display info null.");
844         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
845         return nullptr;
846     }
847     NativeDisplayManager_DisplayInfo *displayInner =
848         (NativeDisplayManager_DisplayInfo*)malloc(sizeof(NativeDisplayManager_DisplayInfo));
849     if (displayInner == nullptr) {
850         TLOGE(WmsLogTag::DMS, "[DMNDK] malloc display info null.");
851         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
852         return nullptr;
853     }
854     auto retMemset = memset_s(displayInner, sizeof(NativeDisplayManager_DisplayInfo), 0,
855         sizeof(NativeDisplayManager_DisplayInfo));
856     if (retMemset != EOK) {
857         TLOGE(WmsLogTag::DMS, "[DMNDK] memset display info null.");
858         DISPLAY_MANAGER_FREE_MEMORY(displayInner);
859         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
860         return nullptr;
861     }
862     int ret = memcpy_s(displayInner->name, OH_DISPLAY_NAME_LENGTH, info->GetName().c_str(), OH_DISPLAY_NAME_LENGTH);
863     if (ret != EOK) {
864         TLOGE(WmsLogTag::DMS, "[DMNDK] memcpy display name failed.");
865         DISPLAY_MANAGER_FREE_MEMORY(displayInner);
866         *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
867         return nullptr;
868     }
869     NativeDisplayManager_SetDisplayInfo(displayInner, info);
870     *errCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
871     return displayInner;
872 }
873 
OH_NativeDisplayManager_CreateDisplayById(uint32_t id,NativeDisplayManager_DisplayInfo ** displayInfo)874 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreateDisplayById(uint32_t id,
875     NativeDisplayManager_DisplayInfo **displayInfo)
876 {
877     if (displayInfo == nullptr) {
878         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
879         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
880     }
881     sptr<Display> display = DisplayManager::GetInstance().GetDisplayById(static_cast<DisplayId>(id));
882     if (display == nullptr) {
883         TLOGE(WmsLogTag::DMS, "[DMNDK] get display by id null.");
884         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
885     }
886     TLOGI(WmsLogTag::DMS, "[DMNDK] get display id[%{public}" PRIu64"] info", display->GetId());
887     NativeDisplayManager_ErrorCode errorCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
888     *displayInfo = NativeDisplayManager_FillDisplayInfo(display, &errorCode);
889     return errorCode;
890 }
891 
OH_NativeDisplayManager_CreatePrimaryDisplay(NativeDisplayManager_DisplayInfo ** displayInfo)892 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CreatePrimaryDisplay(
893     NativeDisplayManager_DisplayInfo **displayInfo)
894 {
895     if (displayInfo == nullptr) {
896         TLOGE(WmsLogTag::DMS, "[DMNDK] param is null.");
897         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
898     }
899     sptr<Display> display = DisplayManager::GetInstance().GetPrimaryDisplaySync();
900     if (display == nullptr) {
901         TLOGE(WmsLogTag::DMS, "[DMNDK] get primary display is null.");
902         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
903     }
904     TLOGI(WmsLogTag::DMS, "[DMNDK] get primary display id[%{public}" PRIu64"].", display->GetId());
905     NativeDisplayManager_ErrorCode errorCode = NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
906     *displayInfo = NativeDisplayManager_FillDisplayInfo(display, &errorCode);
907     return errorCode;
908 }
909 
OH_NativeDisplayManager_DestroyDisplay(NativeDisplayManager_DisplayInfo * displayInfo)910 void OH_NativeDisplayManager_DestroyDisplay(NativeDisplayManager_DisplayInfo *displayInfo)
911 {
912     if (displayInfo == nullptr) {
913         TLOGE(WmsLogTag::DMS, "[DMNDK] free display info is null.");
914         return;
915     }
916     if (displayInfo->colorSpace != nullptr) {
917         if (displayInfo->colorSpace->colorSpaces != nullptr) {
918             DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace->colorSpaces);
919         }
920         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->colorSpace);
921     }
922     if (displayInfo->hdrFormat != nullptr) {
923         if (displayInfo->hdrFormat->hdrFormats != nullptr) {
924             DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat->hdrFormats);
925         }
926         DISPLAY_MANAGER_FREE_MEMORY(displayInfo->hdrFormat);
927     }
928     DISPLAY_MANAGER_FREE_MEMORY(displayInfo);
929 }
930 
OH_NativeDisplayManager_CaptureScreenPixelmap(uint32_t displayId,OH_PixelmapNative ** pixelMap)931 NativeDisplayManager_ErrorCode OH_NativeDisplayManager_CaptureScreenPixelmap(uint32_t displayId,
932     OH_PixelmapNative **pixelMap)
933 {
934     if (pixelMap == nullptr) {
935         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap is null.");
936         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
937     }
938     CaptureOption option;
939     option.displayId_ = static_cast<DisplayId>(displayId);
940     option.isNeedNotify_ = true;
941     DmErrorCode errCode = DmErrorCode::DM_OK;
942     std::shared_ptr<Media::PixelMap> captureImage = DisplayManager::GetInstance().GetScreenCapture(option, &errCode);
943 
944     if (errCode == DmErrorCode::DM_ERROR_INVALID_PARAM) {
945         TLOGE(WmsLogTag::DMS, "[DMNDK] param error.");
946         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_INVALID_PARAM;
947     }
948     if (errCode == DmErrorCode::DM_ERROR_DEVICE_NOT_SUPPORT) {
949         TLOGE(WmsLogTag::DMS, "[DMNDK] not support.");
950         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_DEVICE_NOT_SUPPORTED;
951     }
952     if (errCode == DmErrorCode::DM_ERROR_NO_PERMISSION) {
953         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap no permission.");
954         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_NO_PERMISSION;
955     }
956     if (captureImage == nullptr) {
957         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap is null.");
958         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
959     }
960     *pixelMap = new OH_PixelmapNative(captureImage);
961     if (*pixelMap == nullptr) {
962         TLOGE(WmsLogTag::DMS, "[DMNDK] pixelMap convert pixelMapNative null.");
963         return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_ERROR_SYSTEM_ABNORMAL;
964     }
965     TLOGI(WmsLogTag::DMS, "[DMNDK] get screen capture end.");
966     return NativeDisplayManager_ErrorCode::DISPLAY_MANAGER_OK;
967 }