1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "freeze_controller.h"
17 
18 #include <securec.h>
19 
20 #include "display_manager_service_inner.h"
21 #include "pixel_map.h"
22 #include "surface_draw.h"
23 #include "window_manager_hilog.h"
24 #include "window_option.h"
25 #include "wm_common.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace {
30 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "FreezeController"};
31 }
32 
FreezeDisplay(DisplayId displayId)33 bool FreezeController::FreezeDisplay(DisplayId displayId)
34 {
35     sptr<Window> window = CreateCoverWindow(displayId);
36     if (window == nullptr) {
37         return false;
38     }
39 
40     WMError res = window->Show();
41     if (res != WMError::WM_OK) {
42         WLOGFE("Show window failed");
43         return false;
44     }
45     std::shared_ptr<Media::PixelMap> pixelMap = DisplayManagerServiceInner::GetInstance().GetDisplaySnapshot(displayId);
46     if (pixelMap == nullptr) {
47         WLOGE("freeze display fail, pixel map is null. display %{public}" PRIu64"", displayId);
48         return false;
49     }
50     return SurfaceDraw::DrawImage(window->GetSurfaceNode(), window->GetRect().width_,
51         window->GetRect().height_, pixelMap);
52 }
53 
UnfreezeDisplay(DisplayId displayId)54 bool FreezeController::UnfreezeDisplay(DisplayId displayId)
55 {
56     auto iter = coverWindowMap_.find(displayId);
57     if (iter == coverWindowMap_.end()) {
58         WLOGW("unfreeze fail, no cover window. display %{public}" PRIu64"", displayId);
59         return false;
60     }
61     sptr<Window> window = iter->second;
62     if (window == nullptr) {
63         WLOGW("unfreeze fail, window is null. display %{public}" PRIu64"", displayId);
64         return false;
65     }
66     return WMError::WM_OK == window->Destroy();
67 }
68 
CreateCoverWindow(DisplayId displayId)69 sptr<Window> FreezeController::CreateCoverWindow(DisplayId displayId)
70 {
71     sptr<WindowOption> option = new (std::nothrow) WindowOption();
72     if (option == nullptr) {
73         WLOGFE("window option is null");
74         return nullptr;
75     }
76     option->SetWindowType(WindowType::WINDOW_TYPE_FREEZE_DISPLAY);
77     option->SetFocusable(false);
78     option->SetMainHandlerAvailable(false);
79     option->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_NEED_AVOID);
80     option->SetDisplayId(displayId);
81     sptr<Window> window = Window::Create("freeze" + std::to_string(displayId), option);
82     if (window == nullptr) {
83         WLOGFE("cover window is null");
84         return nullptr;
85     }
86     coverWindowMap_[displayId] = window;
87     return window;
88 }
89 } // Rosen
90 } // OHOS