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