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 "screen_client.h"
17
18 #include "dscreen_errcode.h"
19 #include "dscreen_log.h"
20 #include "screen_client_window_adapter.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
24 IMPLEMENT_SINGLE_INSTANCE(ScreenClient);
25
~ScreenClient()26 ScreenClient::~ScreenClient()
27 {
28 DHLOGD("~ScreenClient");
29 {
30 std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
31 for (const auto &item : surfaceMap_) {
32 int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(item.first);
33 if (ret != DH_SUCCESS) {
34 DHLOGE("windowId (ID = %d) remove failed.", item.first);
35 return;
36 }
37 }
38 surfaceMap_.clear();
39 windowId_ = INVALID_WINDOW_ID;
40 }
41 DHLOGD("ScreenClient Destory.");
42 return;
43 }
44
AddWindow(std::shared_ptr<WindowProperty> windowProperty)45 int32_t ScreenClient::AddWindow(std::shared_ptr<WindowProperty> windowProperty)
46 {
47 if (windowProperty == nullptr) {
48 DHLOGE("windowProperty is nullptr.");
49 return ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR;
50 }
51 int32_t windowId = ++windowId_;
52 sptr<Surface> surface = ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty, windowId);
53 if (surface == nullptr) {
54 DHLOGE("Surface is nullptr.");
55 return ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR;
56 }
57 {
58 std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
59 surfaceMap_.emplace(windowId, surface);
60 }
61 DHLOGI("Add window (ID = %d) success.", windowId);
62 return windowId;
63 }
64
ShowWindow(int32_t windowId)65 int32_t ScreenClient::ShowWindow(int32_t windowId)
66 {
67 {
68 std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
69 auto iter = surfaceMap_.find(windowId);
70 if (iter == surfaceMap_.end()) {
71 DHLOGE("windowId (ID = %d) is non-existent.", windowId);
72 return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR;
73 }
74 }
75 int32_t ret = ScreenClientWindowAdapter::GetInstance().ShowWindow(windowId);
76 if (DH_SUCCESS != ret) {
77 DHLOGE("Show window (ID = %d) failed.", windowId);
78 return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR;
79 }
80 DHLOGI("Show window (ID = %d) success.", windowId);
81 return ret;
82 }
83
HideWindow(int32_t windowId)84 int32_t ScreenClient::HideWindow(int32_t windowId)
85 {
86 {
87 std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
88 auto iter = surfaceMap_.find(windowId);
89 if (iter == surfaceMap_.end()) {
90 DHLOGE("windowId (ID = %d) is non-existent.", windowId);
91 return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR;
92 }
93 }
94 int32_t ret = ScreenClientWindowAdapter::GetInstance().HideWindow(windowId);
95 if (DH_SUCCESS != ret) {
96 DHLOGE("Hide window (ID = %d) failed.", windowId);
97 return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR;
98 }
99 DHLOGI("Hide window (ID = %d) success.", windowId);
100 return ret;
101 }
102
MoveWindow(int32_t windowId,int32_t startX,int32_t startY)103 int32_t ScreenClient::MoveWindow(int32_t windowId, int32_t startX, int32_t startY)
104 {
105 {
106 std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
107 auto iter = surfaceMap_.find(windowId);
108 if (iter == surfaceMap_.end()) {
109 DHLOGE("windowId (ID = %d) is non-existent.", windowId);
110 return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR;
111 }
112 }
113 int32_t ret = ScreenClientWindowAdapter::GetInstance().MoveWindow(windowId, startX, startY);
114 if (DH_SUCCESS != ret) {
115 DHLOGE("Move window (ID = %d) failed.", windowId);
116 return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR;
117 }
118 DHLOGD("Move window (ID = %d) success.", windowId);
119 return ret;
120 }
121
GetSurface(int32_t windowId)122 sptr<Surface> ScreenClient::GetSurface(int32_t windowId)
123 {
124 sptr<Surface> surface = nullptr;
125 {
126 std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
127 auto iter = surfaceMap_.find(windowId);
128 if (iter == surfaceMap_.end()) {
129 DHLOGE("windowId (ID = %d) is non-existent.", windowId);
130 return nullptr;
131 }
132 surface = iter->second;
133 }
134 if (surface == nullptr) {
135 DHLOGE("Get surface is null(ID = %d).", windowId);
136 return nullptr;
137 }
138 DHLOGD("Get surface (ID = %d) success.", windowId);
139 return surface;
140 }
141
RemoveWindow(int32_t windowId)142 int32_t ScreenClient::RemoveWindow(int32_t windowId)
143 {
144 {
145 std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
146 auto iter = surfaceMap_.find(windowId);
147 if (iter == surfaceMap_.end()) {
148 DHLOGE("windowId (ID = %d) is non-existent.", windowId);
149 return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR;
150 }
151 surfaceMap_.erase(windowId);
152 }
153 int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
154 if (ret != DH_SUCCESS) {
155 DHLOGE("windowId (ID = %d) remove failed.", windowId);
156 return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR;
157 }
158 DHLOGD("windowId (ID = %d) remove success.", windowId);
159 return DH_SUCCESS;
160 }
161 } // namespace DistributedHardware
162 } // namespace OHOS