1 /*
2 * Copyright (c) 2021-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 "display_manager_adapter.h"
17
18 #include <iremote_broker.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21
22 #include "window_manager_hilog.h"
23
24 namespace OHOS::Rosen {
25 namespace {
26 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayManagerAdapter"};
27 }
28 WM_IMPLEMENT_SINGLE_INSTANCE(DisplayManagerAdapter)
WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapter)29 WM_IMPLEMENT_SINGLE_INSTANCE(ScreenManagerAdapter)
30
31 #define INIT_PROXY_CHECK_RETURN(ret) \
32 do { \
33 if (!InitDMSProxy()) { \
34 WLOGFE("InitDMSProxy failed!"); \
35 return ret; \
36 } \
37 } while (false)
38
39 DisplayId DisplayManagerAdapter::GetDefaultDisplayId()
40 {
41 INIT_PROXY_CHECK_RETURN(DISPLAY_ID_INVALID);
42
43 return displayManagerServiceProxy_->GetDefaultDisplayId();
44 }
45
GetDisplayInfoByScreenId(ScreenId screenId)46 sptr<DisplayInfo> DisplayManagerAdapter::GetDisplayInfoByScreenId(ScreenId screenId)
47 {
48 INIT_PROXY_CHECK_RETURN(nullptr);
49
50 return displayManagerServiceProxy_->GetDisplayInfoByScreen(screenId);
51 }
52
GetDisplaySnapshot(DisplayId displayId)53 std::shared_ptr<Media::PixelMap> DisplayManagerAdapter::GetDisplaySnapshot(DisplayId displayId)
54 {
55 INIT_PROXY_CHECK_RETURN(nullptr);
56
57 return displayManagerServiceProxy_->GetDisplaySnapshot(displayId);
58 }
59
GetScreenSupportedColorGamuts(ScreenId screenId,std::vector<ScreenColorGamut> & colorGamuts)60 DMError ScreenManagerAdapter::GetScreenSupportedColorGamuts(ScreenId screenId,
61 std::vector<ScreenColorGamut>& colorGamuts)
62 {
63 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
64
65 return displayManagerServiceProxy_->GetScreenSupportedColorGamuts(screenId, colorGamuts);
66 }
67
GetScreenColorGamut(ScreenId screenId,ScreenColorGamut & colorGamut)68 DMError ScreenManagerAdapter::GetScreenColorGamut(ScreenId screenId, ScreenColorGamut& colorGamut)
69 {
70 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
71
72 return displayManagerServiceProxy_->GetScreenColorGamut(screenId, colorGamut);
73 }
74
SetScreenColorGamut(ScreenId screenId,int32_t colorGamutIdx)75 DMError ScreenManagerAdapter::SetScreenColorGamut(ScreenId screenId, int32_t colorGamutIdx)
76 {
77 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
78
79 return displayManagerServiceProxy_->SetScreenColorGamut(screenId, colorGamutIdx);
80 }
81
GetScreenGamutMap(ScreenId screenId,ScreenGamutMap & gamutMap)82 DMError ScreenManagerAdapter::GetScreenGamutMap(ScreenId screenId, ScreenGamutMap& gamutMap)
83 {
84 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
85
86 return displayManagerServiceProxy_->GetScreenGamutMap(screenId, gamutMap);
87 }
88
SetScreenGamutMap(ScreenId screenId,ScreenGamutMap gamutMap)89 DMError ScreenManagerAdapter::SetScreenGamutMap(ScreenId screenId, ScreenGamutMap gamutMap)
90 {
91 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
92
93 return displayManagerServiceProxy_->SetScreenGamutMap(screenId, gamutMap);
94 }
95
SetScreenColorTransform(ScreenId screenId)96 DMError ScreenManagerAdapter::SetScreenColorTransform(ScreenId screenId)
97 {
98 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
99
100 return displayManagerServiceProxy_->SetScreenColorTransform(screenId);
101 }
102
CreateVirtualScreen(VirtualScreenOption option)103 ScreenId ScreenManagerAdapter::CreateVirtualScreen(VirtualScreenOption option)
104 {
105 INIT_PROXY_CHECK_RETURN(SCREEN_ID_INVALID);
106
107 WLOGFI("DisplayManagerAdapter::CreateVirtualScreen");
108 return displayManagerServiceProxy_->CreateVirtualScreen(option);
109 }
110
DestroyVirtualScreen(ScreenId screenId)111 DMError ScreenManagerAdapter::DestroyVirtualScreen(ScreenId screenId)
112 {
113 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
114
115 WLOGFI("DisplayManagerAdapter::DestroyVirtualScreen");
116 return displayManagerServiceProxy_->DestroyVirtualScreen(screenId);
117 }
118
SetVirtualScreenSurface(ScreenId screenId,sptr<Surface> surface)119 DMError ScreenManagerAdapter::SetVirtualScreenSurface(ScreenId screenId, sptr<Surface> surface)
120 {
121 INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED);
122
123 WLOGFI("DisplayManagerAdapter::SetVirtualScreenSurface");
124 return displayManagerServiceProxy_->SetVirtualScreenSurface(screenId, surface);
125 }
126
SetScreenPowerForAll(ScreenPowerState state,PowerStateChangeReason reason)127 bool ScreenManagerAdapter::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason)
128 {
129 INIT_PROXY_CHECK_RETURN(false);
130 return displayManagerServiceProxy_->SetScreenPowerForAll(state, reason);
131 }
132
GetScreenPower(ScreenId dmsScreenId)133 ScreenPowerState ScreenManagerAdapter::GetScreenPower(ScreenId dmsScreenId)
134 {
135 INIT_PROXY_CHECK_RETURN(ScreenPowerState::INVALID_STATE);
136 return displayManagerServiceProxy_->GetScreenPower(dmsScreenId);
137 }
138
SetOrientation(ScreenId screenId,Orientation orientation)139 bool ScreenManagerAdapter::SetOrientation(ScreenId screenId, Orientation orientation)
140 {
141 INIT_PROXY_CHECK_RETURN(false);
142
143 return displayManagerServiceProxy_->SetOrientation(screenId, orientation);
144 }
145
RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)146 bool BaseAdapter::RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
147 DisplayManagerAgentType type)
148 {
149 INIT_PROXY_CHECK_RETURN(false);
150
151 return displayManagerServiceProxy_->RegisterDisplayManagerAgent(displayManagerAgent, type);
152 }
153
UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent> & displayManagerAgent,DisplayManagerAgentType type)154 bool BaseAdapter::UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
155 DisplayManagerAgentType type)
156 {
157 INIT_PROXY_CHECK_RETURN(false);
158
159 return displayManagerServiceProxy_->UnregisterDisplayManagerAgent(displayManagerAgent, type);
160 }
161
WakeUpBegin(PowerStateChangeReason reason)162 bool DisplayManagerAdapter::WakeUpBegin(PowerStateChangeReason reason)
163 {
164 INIT_PROXY_CHECK_RETURN(false);
165
166 return displayManagerServiceProxy_->WakeUpBegin(reason);
167 }
168
WakeUpEnd()169 bool DisplayManagerAdapter::WakeUpEnd()
170 {
171 INIT_PROXY_CHECK_RETURN(false);
172
173 return displayManagerServiceProxy_->WakeUpEnd();
174 }
175
SuspendBegin(PowerStateChangeReason reason)176 bool DisplayManagerAdapter::SuspendBegin(PowerStateChangeReason reason)
177 {
178 INIT_PROXY_CHECK_RETURN(false);
179
180 return displayManagerServiceProxy_->SuspendBegin(reason);
181 }
182
SuspendEnd()183 bool DisplayManagerAdapter::SuspendEnd()
184 {
185 INIT_PROXY_CHECK_RETURN(false);
186
187 return displayManagerServiceProxy_->SuspendEnd();
188 }
189
SetDisplayState(DisplayState state)190 bool DisplayManagerAdapter::SetDisplayState(DisplayState state)
191 {
192 INIT_PROXY_CHECK_RETURN(false);
193
194 return displayManagerServiceProxy_->SetDisplayState(state);
195 }
196
GetDisplayState(DisplayId displayId)197 DisplayState DisplayManagerAdapter::GetDisplayState(DisplayId displayId)
198 {
199 INIT_PROXY_CHECK_RETURN(DisplayState::UNKNOWN);
200
201 return displayManagerServiceProxy_->GetDisplayState(displayId);
202 }
203
NotifyDisplayEvent(DisplayEvent event)204 void DisplayManagerAdapter::NotifyDisplayEvent(DisplayEvent event)
205 {
206 INIT_PROXY_CHECK_RETURN();
207
208 displayManagerServiceProxy_->NotifyDisplayEvent(event);
209 }
210
SetFreeze(std::vector<DisplayId> displayIds,bool isFreeze)211 bool DisplayManagerAdapter::SetFreeze(std::vector<DisplayId> displayIds, bool isFreeze)
212 {
213 INIT_PROXY_CHECK_RETURN(false);
214 return displayManagerServiceProxy_->SetFreeze(displayIds, isFreeze);
215 }
216
InitDMSProxy()217 bool BaseAdapter::InitDMSProxy()
218 {
219 std::lock_guard<std::recursive_mutex> lock(mutex_);
220 if (!isProxyValid_) {
221 sptr<ISystemAbilityManager> systemAbilityManager =
222 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
223 if (!systemAbilityManager) {
224 WLOGFE("Failed to get system ability mgr.");
225 return false;
226 }
227
228 sptr<IRemoteObject> remoteObject
229 = systemAbilityManager->GetSystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID);
230 if (!remoteObject) {
231 WLOGFE("Failed to get display manager service.");
232 return false;
233 }
234
235 displayManagerServiceProxy_ = iface_cast<IDisplayManager>(remoteObject);
236 if ((!displayManagerServiceProxy_) || (!displayManagerServiceProxy_->AsObject())) {
237 WLOGFE("Failed to get system display manager services");
238 return false;
239 }
240
241 dmsDeath_ = new DMSDeathRecipient(*this);
242 if (!dmsDeath_) {
243 WLOGFE("Failed to create death Recipient ptr DMSDeathRecipient");
244 return false;
245 }
246 if (remoteObject->IsProxyObject() && !remoteObject->AddDeathRecipient(dmsDeath_)) {
247 WLOGFE("Failed to add death recipient");
248 return false;
249 }
250 isProxyValid_ = true;
251 }
252 return true;
253 }
254
DMSDeathRecipient(BaseAdapter & adapter)255 DMSDeathRecipient::DMSDeathRecipient(BaseAdapter& adapter) : adapter_(adapter)
256 {
257 }
258
OnRemoteDied(const wptr<IRemoteObject> & wptrDeath)259 void DMSDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& wptrDeath)
260 {
261 if (wptrDeath == nullptr) {
262 WLOGFE("wptrDeath is null");
263 return;
264 }
265
266 sptr<IRemoteObject> object = wptrDeath.promote();
267 if (!object) {
268 WLOGFE("object is null");
269 return;
270 }
271 adapter_.Clear();
272 return;
273 }
274
Clear()275 void BaseAdapter::Clear()
276 {
277 if ((displayManagerServiceProxy_ != nullptr) && (displayManagerServiceProxy_->AsObject() != nullptr)) {
278 displayManagerServiceProxy_->AsObject()->RemoveDeathRecipient(dmsDeath_);
279 }
280 std::lock_guard<std::recursive_mutex> lock(mutex_);
281 isProxyValid_ = false;
282 }
283
MakeMirror(ScreenId mainScreenId,std::vector<ScreenId> mirrorScreenId)284 ScreenId ScreenManagerAdapter::MakeMirror(ScreenId mainScreenId, std::vector<ScreenId> mirrorScreenId)
285 {
286 INIT_PROXY_CHECK_RETURN(SCREEN_ID_INVALID);
287
288 return displayManagerServiceProxy_->MakeMirror(mainScreenId, mirrorScreenId);
289 }
290
GetScreenInfo(ScreenId screenId)291 sptr<ScreenInfo> ScreenManagerAdapter::GetScreenInfo(ScreenId screenId)
292 {
293 if (screenId == SCREEN_ID_INVALID) {
294 WLOGFE("screen id is invalid");
295 return nullptr;
296 }
297 INIT_PROXY_CHECK_RETURN(nullptr);
298
299 sptr<ScreenInfo> screenInfo = displayManagerServiceProxy_->GetScreenInfoById(screenId);
300 return screenInfo;
301 }
302
GetAllDisplayIds()303 std::vector<DisplayId> DisplayManagerAdapter::GetAllDisplayIds()
304 {
305 INIT_PROXY_CHECK_RETURN(std::vector<DisplayId>());
306
307 return displayManagerServiceProxy_->GetAllDisplayIds();
308 }
309
GetDisplayInfo(DisplayId displayId)310 sptr<DisplayInfo> DisplayManagerAdapter::GetDisplayInfo(DisplayId displayId)
311 {
312 if (displayId == DISPLAY_ID_INVALID) {
313 WLOGFE("screen id is invalid");
314 return nullptr;
315 }
316 INIT_PROXY_CHECK_RETURN(nullptr);
317
318 return displayManagerServiceProxy_->GetDisplayInfoById(displayId);
319 }
320
GetScreenGroupInfoById(ScreenId screenId)321 sptr<ScreenGroupInfo> ScreenManagerAdapter::GetScreenGroupInfoById(ScreenId screenId)
322 {
323 if (screenId == SCREEN_ID_INVALID) {
324 WLOGFE("screenGroup id is invalid");
325 return nullptr;
326 }
327 INIT_PROXY_CHECK_RETURN(nullptr);
328
329 return displayManagerServiceProxy_->GetScreenGroupInfoById(screenId);
330 }
331
GetAllScreenInfos()332 std::vector<sptr<ScreenInfo>> ScreenManagerAdapter::GetAllScreenInfos()
333 {
334 INIT_PROXY_CHECK_RETURN(std::vector<sptr<ScreenInfo>>());
335
336 return displayManagerServiceProxy_->GetAllScreenInfos();
337 }
338
MakeExpand(std::vector<ScreenId> screenId,std::vector<Point> startPoint)339 ScreenId ScreenManagerAdapter::MakeExpand(std::vector<ScreenId> screenId, std::vector<Point> startPoint)
340 {
341 INIT_PROXY_CHECK_RETURN(SCREEN_ID_INVALID);
342
343 return displayManagerServiceProxy_->MakeExpand(screenId, startPoint);
344 }
345
RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)346 void ScreenManagerAdapter::RemoveVirtualScreenFromGroup(std::vector<ScreenId> screens)
347 {
348 INIT_PROXY_CHECK_RETURN();
349
350 displayManagerServiceProxy_->RemoveVirtualScreenFromGroup(screens);
351 }
352
SetScreenActiveMode(ScreenId screenId,uint32_t modeId)353 bool ScreenManagerAdapter::SetScreenActiveMode(ScreenId screenId, uint32_t modeId)
354 {
355 INIT_PROXY_CHECK_RETURN(false);
356
357 return displayManagerServiceProxy_->SetScreenActiveMode(screenId, modeId);
358 }
359 } // namespace OHOS::Rosen