1 /*
2  * Copyright (c) 2020-2021 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 "window/window_impl.h"
17 #include "core/render_manager.h"
18 #include "gfx_utils/graphic_log.h"
19 #include "iwindows_manager.h"
20 
21 namespace OHOS {
WindowImpl()22 WindowImpl::WindowImpl() : rootView_(nullptr), iWindow_(nullptr), isShow_(false), gfxAlloc_({}) {}
23 
~WindowImpl()24 WindowImpl::~WindowImpl() {}
25 
Create(const WindowConfig & config)26 bool WindowImpl::Create(const WindowConfig& config)
27 {
28     GRAPHIC_LOGI("Create");
29     if (iWindow_ == nullptr) {
30         config_ = config;
31         LiteWinConfig liteConfig;
32         liteConfig.rect = config.rect;
33         liteConfig.pixelFormat = IMAGE_PIXEL_FORMAT_ARGB8888;
34         liteConfig.opacity = config.opacity;
35         liteConfig.isModal = config.isModal;
36         liteConfig.compositeMode = static_cast<LiteWinConfig::CompositeMode>(config.compositeMode);
37         iWindow_ = IWindowsManager::GetInstance()->CreateWindow(liteConfig);
38         if (iWindow_ == nullptr) {
39             return false;
40         }
41     }
42     return true;
43 }
44 
Destroy()45 void WindowImpl::Destroy()
46 {
47     Flush();
48     if (iWindow_ != nullptr) {
49         IWindowsManager::GetInstance()->RemoveWindow(iWindow_);
50         iWindow_ = nullptr;
51     }
52 }
53 
AddToDisplay()54 void WindowImpl::AddToDisplay()
55 {
56     GRAPHIC_LOGI("AddToDisplay");
57     RenderManager::GetInstance().AddToDisplay(this);
58 }
59 
RemoveFromDisplay()60 void WindowImpl::RemoveFromDisplay()
61 {
62     GRAPHIC_LOGI("RemoveFromDisplay");
63     RenderManager::GetInstance().RemoveFromDisplay(this);
64 }
65 
BindRootView(RootView * rootView)66 void WindowImpl::BindRootView(RootView* rootView)
67 {
68     if (rootView == nullptr) {
69         return;
70     }
71     UnbindRootView();
72     GRAPHIC_LOGI("BindRootView");
73     rootView_ = rootView;
74     rootView_->boundWindow_ = this;
75 }
76 
UnbindRootView()77 void WindowImpl::UnbindRootView()
78 {
79     GRAPHIC_LOGI("UnbindRootView");
80     if (rootView_ != nullptr) {
81         rootView_->boundWindow_ = nullptr;
82         rootView_ = nullptr;
83     }
84 }
85 
GetRootView()86 RootView* WindowImpl::GetRootView()
87 {
88     return rootView_;
89 }
90 
GetRect()91 Rect WindowImpl::GetRect()
92 {
93     return config_.rect;
94 }
95 
Show()96 void WindowImpl::Show()
97 {
98     if (iWindow_ == nullptr) {
99         return;
100     }
101 
102     if (!isShow_) {
103         isShow_ = true;
104         Render();
105         iWindow_->Show();
106     }
107 }
108 
Hide()109 void WindowImpl::Hide()
110 {
111     if (iWindow_ == nullptr) {
112         return;
113     }
114 
115     if (isShow_) {
116         isShow_ = false;
117         iWindow_->Hide();
118     }
119 }
120 
MoveTo(int16_t x,int16_t y)121 void WindowImpl::MoveTo(int16_t x, int16_t y)
122 {
123     GRAPHIC_LOGI("MoveTo");
124     config_.rect.SetPosition(x, y);
125     if (iWindow_ != nullptr) {
126         iWindow_->MoveTo(x, y);
127     }
128 }
129 
Resize(int16_t width,int16_t height)130 void WindowImpl::Resize(int16_t width, int16_t height)
131 {
132     GRAPHIC_LOGI("Resize");
133     if ((width == config_.rect.GetWidth()) && (height == config_.rect.GetHeight())) {
134         return;
135     }
136 
137     config_.rect.Resize(width, height);
138     Flush();
139     if (iWindow_ != nullptr) {
140         iWindow_->Resize(width, height);
141     }
142 
143     if (rootView_ != nullptr) {
144         rootView_->Invalidate();
145     }
146 }
147 
RaiseToTop()148 void WindowImpl::RaiseToTop()
149 {
150     GRAPHIC_LOGI("RaiseToTop");
151     if (iWindow_ != nullptr) {
152         iWindow_->RaiseToTop();
153     }
154 }
155 
LowerToBottom()156 void WindowImpl::LowerToBottom()
157 {
158     GRAPHIC_LOGI("LowerToBottom");
159     if (iWindow_ != nullptr) {
160         iWindow_->LowerToBottom();
161     }
162 }
163 
Render()164 void WindowImpl::Render()
165 {
166     UpdateHalDisplayBuffer();
167     if (gfxAlloc_.virAddr == nullptr) {
168         GRAPHIC_LOGE("window buffer is null, windId=%d", GetWindowId());
169         return;
170     }
171 
172     if (rootView_ != nullptr) {
173         rootView_->Measure();
174         rootView_->Render();
175     }
176 }
177 
Update()178 void WindowImpl::Update()
179 {
180     if (iWindow_ == nullptr) {
181         return;
182     }
183     iWindow_->Update();
184 }
185 
GetWindowId()186 int32_t WindowImpl::GetWindowId()
187 {
188     if (iWindow_ != nullptr) {
189         return iWindow_->GetWindowId();
190     } else {
191         GRAPHIC_LOGE("iwindow is null!");
192         return INVALID_WINDOW_ID;
193     }
194 }
195 
Flush()196 void WindowImpl::Flush()
197 {
198     GRAPHIC_LOGI("Flush");
199     if (iWindow_ == nullptr) {
200         return;
201     }
202     ISurface* surface = iWindow_->GetSurface();
203     if (surface != nullptr) {
204         surface->Unlock();
205         gfxAlloc_ = {};
206     }
207 }
208 
UpdateHalDisplayBuffer()209 void WindowImpl::UpdateHalDisplayBuffer()
210 {
211     if ((gfxAlloc_.virAddr == nullptr) && (iWindow_ != nullptr)) {
212         ISurface* surface = iWindow_->GetSurface();
213         if (surface == nullptr) {
214             return;
215         }
216         surface->Lock(reinterpret_cast<void**>(&gfxAlloc_.virAddr),
217             reinterpret_cast<void**>(&gfxAlloc_.phyAddr), &gfxAlloc_.stride);
218     }
219 }
220 
GetBufferInfo()221 BufferInfo* WindowImpl::GetBufferInfo()
222 {
223     static BufferInfo bufferInfo;
224     bufferInfo.virAddr = gfxAlloc_.virAddr;
225     bufferInfo.phyAddr = gfxAlloc_.phyAddr;
226     bufferInfo.width = config_.rect.GetWidth();
227     bufferInfo.height = config_.rect.GetHeight();
228     bufferInfo.mode = ARGB8888;
229     bufferInfo.stride = gfxAlloc_.stride;
230 
231     bufferInfo.rect = {
232         0,
233         0,
234         static_cast<int16_t>(bufferInfo.width - 1),
235         static_cast<int16_t>(bufferInfo.height - 1)
236     };
237     return &bufferInfo;
238 }
239 } // namespace OHOS
240