1 /*
2 * Copyright (c) 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 "transaction/rs_render_service_client.h"
17
18 #include "backend/rs_surface_ohos_gl.h"
19 #include "backend/rs_surface_ohos_raster.h"
20 #include "command/rs_command.h"
21 #include "ipc_callbacks/screen_change_callback_stub.h"
22 #include "ipc_callbacks/surface_capture_callback_stub.h"
23 #include "ipc_callbacks/buffer_available_callback_stub.h"
24 #include "platform/common/rs_log.h"
25 #include "rs_render_service_connect_hub.h"
26 #include "rs_surface_ohos.h"
27
28 namespace OHOS {
29 namespace Rosen {
CreateRenderServiceClient()30 std::shared_ptr<RSIRenderClient> RSIRenderClient::CreateRenderServiceClient()
31 {
32 static std::shared_ptr<RSIRenderClient> client = std::make_shared<RSRenderServiceClient>();
33 return client;
34 }
35
CommitTransaction(std::unique_ptr<RSTransactionData> & transactionData)36 void RSRenderServiceClient::CommitTransaction(std::unique_ptr<RSTransactionData>& transactionData)
37 {
38 auto renderService = RSRenderServiceConnectHub::GetRenderService();
39 if (renderService != nullptr) {
40 renderService->CommitTransaction(transactionData);
41 }
42 }
43
ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask> & task)44 void RSRenderServiceClient::ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task)
45 {
46 auto renderService = RSRenderServiceConnectHub::GetRenderService();
47 if (renderService != nullptr) {
48 renderService->ExecuteSynchronousTask(task);
49 }
50 }
51
CreateNodeAndSurface(const RSSurfaceRenderNodeConfig & config)52 std::shared_ptr<RSSurface> RSRenderServiceClient::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config)
53 {
54 auto renderService = RSRenderServiceConnectHub::GetRenderService();
55 if (renderService == nullptr) {
56 return nullptr;
57 }
58 sptr<Surface> surface = renderService->CreateNodeAndSurface(config);
59
60 #ifdef ACE_ENABLE_GL
61 // GPU render
62 std::shared_ptr<RSSurface> producer = std::make_shared<RSSurfaceOhosGl>(surface);
63 #else
64 // CPU render
65 std::shared_ptr<RSSurface> producer = std::make_shared<RSSurfaceOhosRaster>(surface);
66 #endif
67 return producer;
68 }
69
CreateVSyncReceiver(const std::string & name,const std::shared_ptr<OHOS::AppExecFwk::EventHandler> & looper)70 std::shared_ptr<VSyncReceiver> RSRenderServiceClient::CreateVSyncReceiver(
71 const std::string& name,
72 const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &looper)
73 {
74 auto renderService = RSRenderServiceConnectHub::GetRenderService();
75 if (renderService == nullptr) {
76 return nullptr;
77 }
78 sptr<IVSyncConnection> conn = renderService->CreateVSyncConnection(name);
79 return std::make_shared<VSyncReceiver>(conn, looper, name);
80 }
81
TriggerSurfaceCaptureCallback(NodeId id,Media::PixelMap * pixelmap)82 void RSRenderServiceClient::TriggerSurfaceCaptureCallback(NodeId id, Media::PixelMap* pixelmap)
83 {
84 ROSEN_LOGI("RSRenderServiceClient::Into TriggerSurfaceCaptureCallback nodeId:[%llu]", id);
85 std::shared_ptr<Media::PixelMap> surfaceCapture(pixelmap);
86 std::shared_ptr<SurfaceCaptureCallback> callback = nullptr;
87 {
88 std::lock_guard<std::mutex> lock(mutex_);
89 auto iter = surfaceCaptureCbMap_.find(id);
90 if (iter != surfaceCaptureCbMap_.end()) {
91 callback = iter->second;
92 surfaceCaptureCbMap_.erase(iter);
93 }
94 }
95 if (callback == nullptr) {
96 ROSEN_LOGE("RSRenderServiceClient::TriggerSurfaceCaptureCallback: callback is nullptr!");
97 return;
98 }
99 callback->OnSurfaceCapture(surfaceCapture);
100 }
101
102 class SurfaceCaptureCallbackDirector : public RSSurfaceCaptureCallbackStub
103 {
104 public:
SurfaceCaptureCallbackDirector(RSRenderServiceClient * client)105 explicit SurfaceCaptureCallbackDirector(RSRenderServiceClient* client) : client_(client) {}
~SurfaceCaptureCallbackDirector()106 ~SurfaceCaptureCallbackDirector() override {};
OnSurfaceCapture(NodeId id,Media::PixelMap * pixelmap)107 void OnSurfaceCapture(NodeId id, Media::PixelMap* pixelmap) override
108 {
109 client_->TriggerSurfaceCaptureCallback(id, pixelmap);
110 };
111
112 private:
113 RSRenderServiceClient* client_;
114 };
115
TakeSurfaceCapture(NodeId id,std::shared_ptr<SurfaceCaptureCallback> callback,float scaleX,float scaleY)116 bool RSRenderServiceClient::TakeSurfaceCapture(NodeId id, std::shared_ptr<SurfaceCaptureCallback> callback,
117 float scaleX, float scaleY)
118 {
119 auto renderService = RSRenderServiceConnectHub::GetRenderService();
120 if (renderService == nullptr) {
121 ROSEN_LOGE("RSRenderServiceClient::TakeSurfaceCapture renderService == nullptr!");
122 return false;
123 }
124 if (callback == nullptr) {
125 ROSEN_LOGE("RSRenderServiceClient::TakeSurfaceCapture callback == nullptr!");
126 return false;
127 }
128 {
129 std::lock_guard<std::mutex> lock(mutex_);
130 if (surfaceCaptureCbMap_.count(id) != 0) {
131 ROSEN_LOGW("RSRenderServiceClient::TakeSurfaceCapture surfaceCaptureCbMap_.count(id) != 0");
132 return false;
133 }
134 surfaceCaptureCbMap_.emplace(id, callback);
135 }
136
137 if (surfaceCaptureCbDirector_ == nullptr) {
138 surfaceCaptureCbDirector_ = new SurfaceCaptureCallbackDirector(this);
139 }
140 renderService->TakeSurfaceCapture(id, surfaceCaptureCbDirector_, scaleX, scaleY);
141 return true;
142 }
143
GetDefaultScreenId()144 ScreenId RSRenderServiceClient::GetDefaultScreenId()
145 {
146 auto renderService = RSRenderServiceConnectHub::GetRenderService();
147 if (renderService == nullptr) {
148 return INVALID_SCREEN_ID;
149 }
150
151 return renderService->GetDefaultScreenId();
152 }
153
CreateVirtualScreen(const std::string & name,uint32_t width,uint32_t height,sptr<Surface> surface,ScreenId mirrorId,int32_t flags)154 ScreenId RSRenderServiceClient::CreateVirtualScreen(
155 const std::string &name,
156 uint32_t width,
157 uint32_t height,
158 sptr<Surface> surface,
159 ScreenId mirrorId,
160 int32_t flags)
161 {
162 auto renderService = RSRenderServiceConnectHub::GetRenderService();
163 if (renderService == nullptr) {
164 return INVALID_SCREEN_ID;
165 }
166
167 return renderService->CreateVirtualScreen(name, width, height, surface, mirrorId, flags);
168 }
169
SetVirtualScreenSurface(ScreenId id,sptr<Surface> surface)170 int32_t RSRenderServiceClient::SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface)
171 {
172 auto renderService = RSRenderServiceConnectHub::GetRenderService();
173 if (renderService == nullptr) {
174 return RENDER_SERVICE_NULL;
175 }
176
177 return renderService->SetVirtualScreenSurface(id, surface);
178 }
179
RemoveVirtualScreen(ScreenId id)180 void RSRenderServiceClient::RemoveVirtualScreen(ScreenId id)
181 {
182 auto renderService = RSRenderServiceConnectHub::GetRenderService();
183 if (renderService == nullptr) {
184 return;
185 }
186
187 renderService->RemoveVirtualScreen(id);
188 }
189
190 class CustomScreenChangeCallback : public RSScreenChangeCallbackStub
191 {
192 public:
CustomScreenChangeCallback(const ScreenChangeCallback & callback)193 explicit CustomScreenChangeCallback(const ScreenChangeCallback &callback) : cb_(callback) {}
~CustomScreenChangeCallback()194 ~CustomScreenChangeCallback() override {};
195
OnScreenChanged(ScreenId id,ScreenEvent event)196 void OnScreenChanged(ScreenId id, ScreenEvent event) override
197 {
198 if (cb_ != nullptr) {
199 cb_(id, event);
200 }
201 }
202
203 private:
204 ScreenChangeCallback cb_;
205 };
206
SetScreenChangeCallback(const ScreenChangeCallback & callback)207 int32_t RSRenderServiceClient::SetScreenChangeCallback(const ScreenChangeCallback &callback)
208 {
209 auto renderService = RSRenderServiceConnectHub::GetRenderService();
210 if (renderService == nullptr) {
211 return RENDER_SERVICE_NULL;
212 }
213
214 screenChangeCb_ = new CustomScreenChangeCallback(callback);
215 return renderService->SetScreenChangeCallback(screenChangeCb_);
216 }
217
SetScreenActiveMode(ScreenId id,uint32_t modeId)218 void RSRenderServiceClient::SetScreenActiveMode(ScreenId id, uint32_t modeId)
219 {
220 auto renderService = RSRenderServiceConnectHub::GetRenderService();
221 if (renderService == nullptr) {
222 return;
223 }
224
225 renderService->SetScreenActiveMode(id, modeId);
226 }
227
SetScreenPowerStatus(ScreenId id,ScreenPowerStatus status)228 void RSRenderServiceClient::SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status)
229 {
230 auto renderService = RSRenderServiceConnectHub::GetRenderService();
231 if (renderService == nullptr) {
232 return;
233 }
234
235 renderService->SetScreenPowerStatus(id, status);
236 }
237
GetScreenActiveMode(ScreenId id)238 RSScreenModeInfo RSRenderServiceClient::GetScreenActiveMode(ScreenId id)
239 {
240 auto renderService = RSRenderServiceConnectHub::GetRenderService();
241 if (renderService == nullptr) {
242 return RSScreenModeInfo {}; // return empty RSScreenModeInfo.
243 }
244
245 return renderService->GetScreenActiveMode(id);
246 }
247
GetScreenSupportedModes(ScreenId id)248 std::vector<RSScreenModeInfo> RSRenderServiceClient::GetScreenSupportedModes(ScreenId id)
249 {
250 auto renderService = RSRenderServiceConnectHub::GetRenderService();
251 if (renderService == nullptr) {
252 return {};
253 }
254
255 return renderService->GetScreenSupportedModes(id);
256 }
257
GetScreenCapability(ScreenId id)258 RSScreenCapability RSRenderServiceClient::GetScreenCapability(ScreenId id)
259 {
260 auto renderService = RSRenderServiceConnectHub::GetRenderService();
261 if (renderService == nullptr) {
262 return RSScreenCapability {};
263 }
264
265 return renderService->GetScreenCapability(id);
266 }
267
GetScreenPowerStatus(ScreenId id)268 ScreenPowerStatus RSRenderServiceClient::GetScreenPowerStatus(ScreenId id)
269 {
270 auto renderService = RSRenderServiceConnectHub::GetRenderService();
271 if (renderService == nullptr) {
272 return ScreenPowerStatus::INVALID_POWER_STATUS;
273 }
274
275 return renderService->GetScreenPowerStatus(id);
276 }
277
GetScreenData(ScreenId id)278 RSScreenData RSRenderServiceClient::GetScreenData(ScreenId id)
279 {
280 auto renderService = RSRenderServiceConnectHub::GetRenderService();
281 if (renderService == nullptr) {
282 return RSScreenData {};
283 }
284
285 return renderService->GetScreenData(id);
286 }
287
GetScreenBacklight(ScreenId id)288 int32_t RSRenderServiceClient::GetScreenBacklight(ScreenId id)
289 {
290 auto renderService = RSRenderServiceConnectHub::GetRenderService();
291 if (renderService == nullptr) {
292 return INVALID_BACKLIGHT_VALUE;
293 }
294
295 return renderService->GetScreenBacklight(id);
296 }
297
SetScreenBacklight(ScreenId id,uint32_t level)298 void RSRenderServiceClient::SetScreenBacklight(ScreenId id, uint32_t level)
299 {
300 auto renderService = RSRenderServiceConnectHub::GetRenderService();
301 if (renderService == nullptr) {
302 return;
303 }
304
305 renderService->SetScreenBacklight(id, level);
306 }
307
308 class CustomBufferAvailableCallback : public RSBufferAvailableCallbackStub
309 {
310 public:
CustomBufferAvailableCallback(const BufferAvailableCallback & callback)311 explicit CustomBufferAvailableCallback(const BufferAvailableCallback &callback) : cb_(callback) {}
~CustomBufferAvailableCallback()312 ~CustomBufferAvailableCallback() override {};
313
OnBufferAvailable(bool isBufferAvailable)314 void OnBufferAvailable(bool isBufferAvailable) override
315 {
316 if (cb_ != nullptr) {
317 cb_(isBufferAvailable);
318 }
319 }
320
321 private:
322 BufferAvailableCallback cb_;
323 };
324
RegisterBufferAvailableListener(NodeId id,const BufferAvailableCallback & callback)325 bool RSRenderServiceClient::RegisterBufferAvailableListener(NodeId id, const BufferAvailableCallback &callback)
326 {
327 auto renderService = RSRenderServiceConnectHub::GetRenderService();
328 if (renderService == nullptr) {
329 return false;
330 }
331 auto iter = bufferAvailableCbMap_.find(id);
332 if (iter != bufferAvailableCbMap_.end()) {
333 return true;
334 }
335 sptr<RSIBufferAvailableCallback> bufferAvailableCb = new CustomBufferAvailableCallback(callback);
336 renderService->RegisterBufferAvailableListener(id, bufferAvailableCb);
337 bufferAvailableCbMap_.emplace(id, bufferAvailableCb);
338 return true;
339 }
340
GetScreenSupportedColorGamuts(ScreenId id,std::vector<ScreenColorGamut> & mode)341 int32_t RSRenderServiceClient::GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode)
342 {
343 auto renderService = RSRenderServiceConnectHub::GetRenderService();
344 if (renderService == nullptr) {
345 return RENDER_SERVICE_NULL;
346 }
347 return renderService->GetScreenSupportedColorGamuts(id, mode);
348 }
349
GetScreenColorGamut(ScreenId id,ScreenColorGamut & mode)350 int32_t RSRenderServiceClient::GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode)
351 {
352 auto renderService = RSRenderServiceConnectHub::GetRenderService();
353 if (renderService == nullptr) {
354 return RENDER_SERVICE_NULL;
355 }
356 return renderService->GetScreenColorGamut(id, mode);
357 }
358
SetScreenColorGamut(ScreenId id,int32_t modeIdx)359 int32_t RSRenderServiceClient::SetScreenColorGamut(ScreenId id, int32_t modeIdx)
360 {
361 auto renderService = RSRenderServiceConnectHub::GetRenderService();
362 if (renderService == nullptr) {
363 return RENDER_SERVICE_NULL;
364 }
365 return renderService->SetScreenColorGamut(id, modeIdx);
366 }
367
SetScreenGamutMap(ScreenId id,ScreenGamutMap mode)368 int32_t RSRenderServiceClient::SetScreenGamutMap(ScreenId id, ScreenGamutMap mode)
369 {
370 auto renderService = RSRenderServiceConnectHub::GetRenderService();
371 if (renderService == nullptr) {
372 return RENDER_SERVICE_NULL;
373 }
374 return renderService->SetScreenGamutMap(id, mode);
375 }
376
GetScreenGamutMap(ScreenId id,ScreenGamutMap & mode)377 int32_t RSRenderServiceClient::GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode)
378 {
379 auto renderService = RSRenderServiceConnectHub::GetRenderService();
380 if (renderService == nullptr) {
381 return RENDER_SERVICE_NULL;
382 }
383 return renderService->GetScreenGamutMap(id, mode);
384 }
385
RequestRotation(ScreenId id,ScreenRotation rotation)386 bool RSRenderServiceClient::RequestRotation(ScreenId id, ScreenRotation rotation)
387 {
388 auto renderService = RSRenderServiceConnectHub::GetRenderService();
389 if (renderService == nullptr) {
390 ROSEN_LOGE("RSRenderServiceClient::RequestRotation renderService == nullptr!");
391 return false;
392 }
393 return renderService->RequestRotation(id, rotation);
394 }
395
GetRotation(ScreenId id)396 ScreenRotation RSRenderServiceClient::GetRotation(ScreenId id)
397 {
398 auto renderService = RSRenderServiceConnectHub::GetRenderService();
399 if (renderService == nullptr) {
400 ROSEN_LOGE("RSRenderServiceClient::GetRotation renderService == nullptr!");
401 return ScreenRotation::INVALID_SCREEN_ROTATION;
402 }
403 return renderService->GetRotation(id);
404 }
405 } // namespace Rosen
406 } // namespace OHOS
407