• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "rs_render_service_connection.h"
17 #include <string>
18 
19 #include "frame_report.h"
20 #include "hgm_command.h"
21 #include "hgm_core.h"
22 #include "hgm_frame_rate_manager.h"
23 #include "luminance/rs_luminance_control.h"
24 #include "offscreen_render/rs_offscreen_render_thread.h"
25 #include "rs_main_thread.h"
26 #include "rs_trace.h"
27 #include "system/rs_system_parameters.h"
28 
29 #include "command/rs_display_node_command.h"
30 #include "command/rs_surface_node_command.h"
31 #include "common/rs_background_thread.h"
32 #include "drawable/rs_canvas_drawing_render_node_drawable.h"
33 #include "include/gpu/GrDirectContext.h"
34 #include "pipeline/parallel_render/rs_sub_thread_manager.h"
35 #include "pipeline/rs_canvas_drawing_render_node.h"
36 #include "pipeline/rs_realtime_refresh_rate_manager.h"
37 #include "pipeline/rs_render_frame_rate_linker_map.h"
38 #include "pipeline/rs_render_node_gc.h"
39 #include "pipeline/rs_render_node_map.h"
40 #include "pipeline/rs_render_service_listener.h"
41 #include "pipeline/rs_surface_buffer_callback_manager.h"
42 #include "pipeline/rs_surface_capture_task.h"
43 #include "pipeline/rs_surface_capture_task_parallel.h"
44 #include "pipeline/rs_ui_capture_task_parallel.h"
45 #include "pipeline/rs_surface_render_node.h"
46 #include "pipeline/rs_task_dispatcher.h"
47 #include "pipeline/rs_uifirst_manager.h"
48 #include "pipeline/rs_uni_render_judgement.h"
49 #include "pipeline/rs_uni_ui_capture.h"
50 #include "pipeline/rs_unmarshal_thread.h"
51 #include "pixel_map_from_surface.h"
52 #include "platform/common/rs_log.h"
53 #include "platform/common/rs_system_properties.h"
54 #include "platform/ohos/rs_jank_stats.h"
55 #include "render/rs_typeface_cache.h"
56 
57 #ifdef TP_FEATURE_ENABLE
58 #include "touch_screen/touch_screen.h"
59 #endif
60 
61 #ifdef RS_ENABLE_VK
62 #include "platform/ohos/backend/rs_vulkan_context.h"
63 #endif
64 
65 namespace OHOS {
66 namespace Rosen {
67 namespace {
68 constexpr int SLEEP_TIME_US = 1000;
69 const std::string REGISTER_NODE = "RegisterNode";
70 }
71 // we guarantee that when constructing this object,
72 // all these pointers are valid, so will not check them.
RSRenderServiceConnection(pid_t remotePid,wptr<RSRenderService> renderService,RSMainThread * mainThread,sptr<RSScreenManager> screenManager,sptr<IRemoteObject> token,sptr<VSyncDistributor> distributor)73 RSRenderServiceConnection::RSRenderServiceConnection(
74     pid_t remotePid,
75     wptr<RSRenderService> renderService,
76     RSMainThread* mainThread,
77     sptr<RSScreenManager> screenManager,
78     sptr<IRemoteObject> token,
79     sptr<VSyncDistributor> distributor)
80     : remotePid_(remotePid),
81       renderService_(renderService),
82       mainThread_(mainThread),
83       renderThread_(RSUniRenderThread::Instance()),
84       screenManager_(screenManager),
85       token_(token),
86       connDeathRecipient_(new RSConnectionDeathRecipient(this)),
87       ApplicationDeathRecipient_(new RSApplicationRenderThreadDeathRecipient(this)),
88       appVSyncDistributor_(distributor)
89 {
90     if (!token_->AddDeathRecipient(connDeathRecipient_)) {
91         RS_LOGW("RSRenderServiceConnection: Failed to set death recipient.");
92     }
93 }
94 
~RSRenderServiceConnection()95 RSRenderServiceConnection::~RSRenderServiceConnection() noexcept
96 {
97     if (token_ && connDeathRecipient_) {
98         token_->RemoveDeathRecipient(connDeathRecipient_);
99     }
100     CleanAll();
101 }
102 
CleanVirtualScreens()103 void RSRenderServiceConnection::CleanVirtualScreens() noexcept
104 {
105     std::lock_guard<std::mutex> lock(mutex_);
106 
107     for (const auto id : virtualScreenIds_) {
108         screenManager_->RemoveVirtualScreen(id);
109     }
110     virtualScreenIds_.clear();
111 
112     if (screenChangeCallback_ != nullptr) {
113         screenManager_->RemoveScreenChangeCallback(screenChangeCallback_);
114         screenChangeCallback_ = nullptr;
115     }
116 }
117 
CleanRenderNodes()118 void RSRenderServiceConnection::CleanRenderNodes() noexcept
119 {
120     auto& context = mainThread_->GetContext();
121     auto& nodeMap = context.GetMutableNodeMap();
122 
123     nodeMap.FilterNodeByPid(remotePid_);
124 }
125 
CleanFrameRateLinkers()126 void RSRenderServiceConnection::CleanFrameRateLinkers() noexcept
127 {
128     auto& context = mainThread_->GetContext();
129     auto& frameRateLinkerMap = context.GetMutableFrameRateLinkerMap();
130 
131     frameRateLinkerMap.FilterFrameRateLinkerByPid(remotePid_);
132 }
133 
CleanAll(bool toDelete)134 void RSRenderServiceConnection::CleanAll(bool toDelete) noexcept
135 {
136     {
137         std::lock_guard<std::mutex> lock(mutex_);
138         if (cleanDone_) {
139             return;
140         }
141     }
142     if (!mainThread_) {
143         return;
144     }
145     RS_LOGD("RSRenderServiceConnection::CleanAll() start.");
146     mainThread_->ScheduleTask(
147         [weakThis = wptr<RSRenderServiceConnection>(this)]() {
148             sptr<RSRenderServiceConnection> connection = weakThis.promote();
149             if (!connection) {
150                 return;
151             }
152             RS_TRACE_NAME_FMT("CleanVirtualScreens %d", connection->remotePid_);
153             connection->CleanVirtualScreens();
154         }).wait();
155     mainThread_->ScheduleTask(
156         [weakThis = wptr<RSRenderServiceConnection>(this)]() {
157             sptr<RSRenderServiceConnection> connection = weakThis.promote();
158             if (!connection) {
159                 return;
160             }
161             RS_TRACE_NAME_FMT("CleanRenderNodes %d", connection->remotePid_);
162             connection->CleanRenderNodes();
163             connection->CleanFrameRateLinkers();
164         }).wait();
165     mainThread_->ScheduleTask(
166         [weakThis = wptr<RSRenderServiceConnection>(this)]() {
167             sptr<RSRenderServiceConnection> connection = weakThis.promote();
168             if (!connection) {
169                 return;
170             }
171             RS_TRACE_NAME_FMT("ClearTransactionDataPidInfo %d", connection->remotePid_);
172             connection->mainThread_->ClearTransactionDataPidInfo(connection->remotePid_);
173         }).wait();
174     mainThread_->ScheduleTask(
175         [weakThis = wptr<RSRenderServiceConnection>(this)]() {
176             sptr<RSRenderServiceConnection> connection = weakThis.promote();
177             if (!connection) {
178                 return;
179             }
180             RS_TRACE_NAME_FMT("CleanHgmEvent %d", connection->remotePid_);
181             if (connection->mainThread_->GetFrameRateMgr() != nullptr) {
182                 connection->mainThread_->GetFrameRateMgr()->CleanVote(connection->remotePid_);
183             }
184         }).wait();
185     mainThread_->ScheduleTask(
186         [weakThis = wptr<RSRenderServiceConnection>(this)]() {
187             sptr<RSRenderServiceConnection> connection = weakThis.promote();
188             if (!connection) {
189                 return;
190             }
191             RS_TRACE_NAME_FMT("UnRegisterCallback %d", connection->remotePid_);
192             HgmConfigCallbackManager::GetInstance()->UnRegisterHgmConfigChangeCallback(connection->remotePid_);
193             connection->mainThread_->UnRegisterOcclusionChangeCallback(connection->remotePid_);
194             connection->mainThread_->ClearSurfaceOcclusionChangeCallback(connection->remotePid_);
195             connection->mainThread_->UnRegisterUIExtensionCallback(connection->remotePid_);
196         }).wait();
197     RSSurfaceBufferCallbackManager::Instance().UnregisterSurfaceBufferCallback(remotePid_);
198     RSTypefaceCache::Instance().RemoveDrawingTypefacesByPid(remotePid_);
199     {
200         std::lock_guard<std::mutex> lock(mutex_);
201         cleanDone_ = true;
202     }
203 
204     if (toDelete) {
205         auto renderService = renderService_.promote();
206         if (renderService == nullptr) {
207             RS_LOGW("RSRenderServiceConnection::CleanAll() RenderService is dead.");
208         } else {
209             renderService->RemoveConnection(GetToken());
210         }
211     }
212 
213     RS_LOGD("RSRenderServiceConnection::CleanAll() end.");
214 }
215 
RSConnectionDeathRecipient(wptr<RSRenderServiceConnection> conn)216 RSRenderServiceConnection::RSConnectionDeathRecipient::RSConnectionDeathRecipient(
217     wptr<RSRenderServiceConnection> conn) : conn_(conn)
218 {
219 }
220 
OnRemoteDied(const wptr<IRemoteObject> & token)221 void RSRenderServiceConnection::RSConnectionDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& token)
222 {
223     auto tokenSptr = token.promote();
224     if (tokenSptr == nullptr) {
225         RS_LOGW("RSConnectionDeathRecipient::OnRemoteDied: can't promote remote object.");
226         return;
227     }
228 
229     auto rsConn = conn_.promote();
230     if (rsConn == nullptr) {
231         RS_LOGW("RSConnectionDeathRecipient::OnRemoteDied: RSRenderServiceConnection was dead, do nothing.");
232         return;
233     }
234 
235     if (rsConn->GetToken() != tokenSptr) {
236         RS_LOGI("RSConnectionDeathRecipient::OnRemoteDied: token doesn't match, ignore it.");
237         return;
238     }
239 
240     rsConn->CleanAll(true);
241 }
242 
RSApplicationRenderThreadDeathRecipient(wptr<RSRenderServiceConnection> conn)243 RSRenderServiceConnection::RSApplicationRenderThreadDeathRecipient::RSApplicationRenderThreadDeathRecipient(
244     wptr<RSRenderServiceConnection> conn) : conn_(conn)
245 {}
246 
OnRemoteDied(const wptr<IRemoteObject> & token)247 void RSRenderServiceConnection::RSApplicationRenderThreadDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& token)
248 {
249     auto tokenSptr = token.promote();
250     if (tokenSptr == nullptr) {
251         RS_LOGW("RSApplicationRenderThreadDeathRecipient::OnRemoteDied: can't promote remote object.");
252         return;
253     }
254 
255     auto rsConn = conn_.promote();
256     if (rsConn == nullptr) {
257         RS_LOGW("RSApplicationRenderThreadDeathRecipient::OnRemoteDied: "
258             "RSRenderServiceConnection was dead, do nothing.");
259         return;
260     }
261 
262     RS_LOGD("RSApplicationRenderThreadDeathRecipient::OnRemoteDied: Unregister.");
263     auto app = iface_cast<IApplicationAgent>(tokenSptr);
264     rsConn->UnRegisterApplicationAgent(app);
265 }
266 
CommitTransaction(std::unique_ptr<RSTransactionData> & transactionData)267 void RSRenderServiceConnection::CommitTransaction(std::unique_ptr<RSTransactionData>& transactionData)
268 {
269     if (!mainThread_) {
270         return;
271     }
272     pid_t callingPid = GetCallingPid();
273     bool isTokenTypeValid = true;
274     bool isNonSystemAppCalling = false;
275     RSInterfaceCodeAccessVerifierBase::GetAccessType(isTokenTypeValid, isNonSystemAppCalling);
276     bool shouldDrop = RSUnmarshalThread::Instance().ReportTransactionDataStatistics(
277         callingPid, transactionData.get(), isNonSystemAppCalling);
278     if (shouldDrop) {
279         RS_LOGW("RSRenderServiceConnection::CommitTransaction data droped");
280         return;
281     }
282     bool isProcessBySingleFrame = mainThread_->IsNeedProcessBySingleFrameComposer(transactionData);
283     if (isProcessBySingleFrame) {
284         mainThread_->ProcessDataBySingleFrameComposer(transactionData);
285     } else {
286         mainThread_->RecvRSTransactionData(transactionData);
287     }
288 }
289 
ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask> & task)290 void RSRenderServiceConnection::ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task)
291 {
292     if (task == nullptr || mainThread_ == nullptr) {
293         RS_LOGW("RSRenderServiceConnection::ExecuteSynchronousTask, task or main thread is null!");
294         return;
295     }
296     // After a synchronous task times out, it will no longer be executed.
297     auto isTimeout = std::make_shared<bool>(0);
298     std::weak_ptr<bool> isTimeoutWeak = isTimeout;
299     std::chrono::nanoseconds span(task->GetTimeout());
300     mainThread_->ScheduleTask([task, mainThread = mainThread_, isTimeoutWeak] {
301         if (task == nullptr || mainThread == nullptr || isTimeoutWeak.expired()) {
302             return;
303         }
304         task->Process(mainThread->GetContext());
305     }).wait_for(span);
306     isTimeout.reset();
307 }
308 
GetUniRenderEnabled()309 bool RSRenderServiceConnection::GetUniRenderEnabled()
310 {
311     return RSUniRenderJudgement::IsUniRender();
312 }
313 
CreateNode(const RSDisplayNodeConfig & displayNodeConfig,NodeId nodeId)314 bool RSRenderServiceConnection::CreateNode(const RSDisplayNodeConfig& displayNodeConfig, NodeId nodeId)
315 {
316     if (!mainThread_) {
317         return false;
318     }
319     std::shared_ptr<RSDisplayRenderNode> node =
320         DisplayNodeCommandHelper::CreateWithConfigInRS(mainThread_->GetContext(), nodeId,
321             displayNodeConfig);
322     if (node == nullptr) {
323         RS_LOGE("RSRenderService::CreateNode Failed.");
324         return false;
325     }
326     std::function<void()> registerNode = [node, weakThis = wptr<RSRenderServiceConnection>(this),
327         mirrorNodeId = displayNodeConfig.mirrorNodeId, isMirrored = displayNodeConfig.isMirrored]() -> void {
328         sptr<RSRenderServiceConnection> connection = weakThis.promote();
329         if (!connection) {
330             return;
331         }
332         auto& context = connection->mainThread_->GetContext();
333         context.GetMutableNodeMap().RegisterDisplayRenderNode(node);
334         context.GetGlobalRootRenderNode()->AddChild(node);
335         if (isMirrored) {
336             auto mirrorSourceNode = context.GetNodeMap()
337                 .GetRenderNode<RSDisplayRenderNode>(mirrorNodeId);
338             if (!mirrorSourceNode) {
339                 return;
340             }
341             node->SetMirrorSource(mirrorSourceNode);
342         }
343     };
344     mainThread_->PostSyncTask(registerNode);
345     return true;
346 }
347 
CreateNode(const RSSurfaceRenderNodeConfig & config)348 bool RSRenderServiceConnection::CreateNode(const RSSurfaceRenderNodeConfig& config)
349 {
350     if (!mainThread_) {
351         return false;
352     }
353     std::shared_ptr<RSSurfaceRenderNode> node =
354         SurfaceNodeCommandHelper::CreateWithConfigInRS(config, mainThread_->GetContext());
355     if (node == nullptr) {
356         RS_LOGE("RSRenderService::CreateNode fail");
357         return false;
358     }
359     std::function<void()> registerNode = [node, weakThis = wptr<RSRenderServiceConnection>(this)]() -> void {
360         sptr<RSRenderServiceConnection> connection = weakThis.promote();
361         if (!connection) {
362             return;
363         }
364         connection->mainThread_->GetContext().GetMutableNodeMap().RegisterRenderNode(node);
365     };
366     mainThread_->PostTask(registerNode);
367     return true;
368 }
369 
CreateNodeAndSurface(const RSSurfaceRenderNodeConfig & config)370 sptr<Surface> RSRenderServiceConnection::CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config)
371 {
372     if (!mainThread_) {
373         return nullptr;
374     }
375     std::shared_ptr<RSSurfaceRenderNode> node =
376         SurfaceNodeCommandHelper::CreateWithConfigInRS(config, mainThread_->GetContext());
377     if (node == nullptr) {
378         RS_LOGE("RSRenderService::CreateNodeAndSurface CreateNode fail");
379         return nullptr;
380     }
381     sptr<IConsumerSurface> surface = IConsumerSurface::Create(config.name);
382     if (surface == nullptr) {
383         RS_LOGE("RSRenderService::CreateNodeAndSurface get consumer surface fail");
384         return nullptr;
385     }
386     const std::string& surfaceName = surface->GetName();
387     RS_LOGI("RsDebug RSRenderService::CreateNodeAndSurface node" \
388         "id:%{public}" PRIu64 " name:%{public}s bundleName:%{public}s surface id:%{public}" PRIu64 " name:%{public}s",
389         node->GetId(), node->GetName().c_str(), node->GetBundleName().c_str(),
390         surface->GetUniqueId(), surfaceName.c_str());
391     auto defaultUsage = surface->GetDefaultUsage();
392     surface->SetDefaultUsage(defaultUsage | BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_HW_COMPOSER);
393     node->GetRSSurfaceHandler()->SetConsumer(surface);
394     RSMainThread* mainThread = mainThread_;
395     std::function<void()> registerNode = [node, mainThread]() -> void {
396         if (auto preNode = mainThread->GetContext().GetNodeMap().GetRenderNode(node->GetId())) {
397             RS_LOGE("CreateNodeAndSurface same id node:%{public}" PRIu64 ", type:%d",
398                 node->GetId(), preNode->GetType());
399             usleep(SLEEP_TIME_US);
400         }
401         mainThread->GetContext().GetMutableNodeMap().RegisterRenderNode(node);
402     };
403     if (config.isSync) {
404         mainThread_->PostSyncTask(registerNode);
405     } else {
406         mainThread_->PostTask(registerNode, REGISTER_NODE, 0, AppExecFwk::EventQueue::Priority::VIP);
407     }
408     std::weak_ptr<RSSurfaceRenderNode> surfaceRenderNode(node);
409     sptr<IBufferConsumerListener> listener = new RSRenderServiceListener(surfaceRenderNode);
410     SurfaceError ret = surface->RegisterConsumerListener(listener);
411     if (ret != SURFACE_ERROR_OK) {
412         RS_LOGE("RSRenderService::CreateNodeAndSurface Register Consumer Listener fail");
413         return nullptr;
414     }
415     sptr<IBufferProducer> producer = surface->GetProducer();
416     sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(producer);
417     return pSurface;
418 }
419 
420 
CreateVSyncConnection(const std::string & name,const sptr<VSyncIConnectionToken> & token,uint64_t id,NodeId windowNodeId)421 sptr<IVSyncConnection> RSRenderServiceConnection::CreateVSyncConnection(const std::string& name,
422                                                                         const sptr<VSyncIConnectionToken>& token,
423                                                                         uint64_t id,
424                                                                         NodeId windowNodeId)
425 {
426     if (!mainThread_) {
427         return nullptr;
428     }
429     sptr<VSyncConnection> conn = new VSyncConnection(appVSyncDistributor_, name, token->AsObject(), 0, windowNodeId);
430     if (ExtractPid(id) == remotePid_) {
431         mainThread_->ScheduleTask([weakThis = wptr<RSRenderServiceConnection>(this), id]() {
432             sptr<RSRenderServiceConnection> connection = weakThis.promote();
433             if (!connection) {
434                 return;
435             }
436             auto linker = std::make_shared<RSRenderFrameRateLinker>(id);
437             auto& context = connection->mainThread_->GetContext();
438             auto& frameRateLinkerMap = context.GetMutableFrameRateLinkerMap();
439             frameRateLinkerMap.RegisterFrameRateLinker(linker);
440         }).wait();
441         conn->id_ = id;
442     }
443     auto ret = appVSyncDistributor_->AddConnection(conn, windowNodeId);
444     if (ret != VSYNC_ERROR_OK) {
445         return nullptr;
446     }
447     return conn;
448 }
449 
CreatePixelMapFromSurface(sptr<Surface> surface,const Rect & srcRect)450 std::shared_ptr<Media::PixelMap> RSRenderServiceConnection::CreatePixelMapFromSurface(sptr<Surface> surface,
451     const Rect &srcRect)
452 {
453     OHOS::Media::Rect rect = {
454         .left = srcRect.x,
455         .top = srcRect.y,
456         .width = srcRect.w,
457         .height = srcRect.h,
458     };
459     std::shared_ptr<Media::PixelMap> pixelmap = nullptr;
460     RSBackgroundThread::Instance().PostSyncTask([surface, rect, &pixelmap]() {
461         pixelmap = OHOS::Rosen::CreatePixelMapFromSurface(surface, rect);
462     });
463     return pixelmap;
464 }
465 
SetFocusAppInfo(int32_t pid,int32_t uid,const std::string & bundleName,const std::string & abilityName,uint64_t focusNodeId)466 int32_t RSRenderServiceConnection::SetFocusAppInfo(
467     int32_t pid, int32_t uid, const std::string &bundleName, const std::string &abilityName, uint64_t focusNodeId)
468 {
469     if (mainThread_ == nullptr) {
470         return INVALID_ARGUMENTS;
471     }
472     mainThread_->ScheduleTask(
473         [pid, uid, bundleName, abilityName, focusNodeId, mainThread = mainThread_]() {
474             // don't use 'this' to get mainThread poninter
475             mainThread->SetFocusAppInfo(pid, uid, bundleName, abilityName, focusNodeId);
476         }
477     );
478     return SUCCESS;
479 }
480 
GetDefaultScreenId()481 ScreenId RSRenderServiceConnection::GetDefaultScreenId()
482 {
483     if (!screenManager_) {
484         return StatusCode::SCREEN_NOT_FOUND;
485     }
486     std::lock_guard<std::mutex> lock(mutex_);
487     return screenManager_->GetDefaultScreenId();
488 }
489 
GetActiveScreenId()490 ScreenId RSRenderServiceConnection::GetActiveScreenId()
491 {
492     if (!screenManager_) {
493         return StatusCode::SCREEN_NOT_FOUND;
494     }
495     std::lock_guard<std::mutex> lock(mutex_);
496     return screenManager_->GetActiveScreenId();
497 }
498 
GetAllScreenIds()499 std::vector<ScreenId> RSRenderServiceConnection::GetAllScreenIds()
500 {
501     std::vector<ScreenId> ids;
502     if (!screenManager_) {
503         return ids;
504     }
505     std::lock_guard<std::mutex> lock(mutex_);
506     return screenManager_->GetAllScreenIds();
507 }
508 
CreateVirtualScreen(const std::string & name,uint32_t width,uint32_t height,sptr<Surface> surface,ScreenId mirrorId,int32_t flags,std::vector<NodeId> whiteList)509 ScreenId RSRenderServiceConnection::CreateVirtualScreen(
510     const std::string &name,
511     uint32_t width,
512     uint32_t height,
513     sptr<Surface> surface,
514     ScreenId mirrorId,
515     int32_t flags,
516     std::vector<NodeId> whiteList)
517 {
518     if (!screenManager_) {
519         return StatusCode::SCREEN_NOT_FOUND;
520     }
521     std::lock_guard<std::mutex> lock(mutex_);
522     auto newVirtualScreenId = screenManager_->CreateVirtualScreen(
523         name, width, height, surface, mirrorId, flags, whiteList);
524     virtualScreenIds_.insert(newVirtualScreenId);
525     if (surface != nullptr) {
526         EventInfo event = { "VOTER_VIRTUALDISPLAY", ADD_VOTE, OLED_60_HZ, OLED_60_HZ, name };
527         NotifyRefreshRateEvent(event);
528     }
529     return newVirtualScreenId;
530 }
531 
SetVirtualScreenBlackList(ScreenId id,std::vector<NodeId> & blackListVector)532 int32_t RSRenderServiceConnection::SetVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector)
533 {
534     if (!screenManager_) {
535         return StatusCode::SCREEN_NOT_FOUND;
536     }
537     std::lock_guard<std::mutex> lock(mutex_);
538     if (blackListVector.empty()) {
539         RS_LOGW("SetVirtualScreenBlackList blackList is empty.");
540     }
541     return screenManager_->SetVirtualScreenBlackList(id, blackListVector);
542 }
543 
AddVirtualScreenBlackList(ScreenId id,std::vector<NodeId> & blackListVector)544 int32_t RSRenderServiceConnection::AddVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector)
545 {
546     if (blackListVector.empty()) {
547         RS_LOGW("AddVirtualScreenBlackList blackList is empty.");
548         return StatusCode::BLACKLIST_IS_EMPTY;
549     }
550     std::lock_guard<std::mutex> lock(mutex_);
551     if (!screenManager_) {
552         return StatusCode::SCREEN_NOT_FOUND;
553     }
554     return screenManager_->AddVirtualScreenBlackList(id, blackListVector);
555 }
556 
RemoveVirtualScreenBlackList(ScreenId id,std::vector<NodeId> & blackListVector)557 int32_t RSRenderServiceConnection::RemoveVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector)
558 {
559     if (blackListVector.empty()) {
560         RS_LOGW("RemoveVirtualScreenBlackList blackList is empty.");
561         return StatusCode::BLACKLIST_IS_EMPTY;
562     }
563     std::lock_guard<std::mutex> lock(mutex_);
564     if (!screenManager_) {
565         return StatusCode::SCREEN_NOT_FOUND;
566     }
567     return screenManager_->RemoveVirtualScreenBlackList(id, blackListVector);
568 }
569 
SetVirtualScreenSecurityExemptionList(ScreenId id,const std::vector<NodeId> & securityExemptionList)570 int32_t RSRenderServiceConnection::SetVirtualScreenSecurityExemptionList(
571     ScreenId id,
572     const std::vector<NodeId>& securityExemptionList)
573 {
574     if (!screenManager_) {
575         return StatusCode::SCREEN_NOT_FOUND;
576     }
577     return screenManager_->SetVirtualScreenSecurityExemptionList(id, securityExemptionList);
578 }
579 
SetCastScreenEnableSkipWindow(ScreenId id,bool enable)580 int32_t RSRenderServiceConnection::SetCastScreenEnableSkipWindow(ScreenId id, bool enable)
581 {
582     if (!screenManager_) {
583         return StatusCode::SCREEN_NOT_FOUND;
584     }
585     std::lock_guard<std::mutex> lock(mutex_);
586     return screenManager_->SetCastScreenEnableSkipWindow(id, enable);
587 }
588 
SetVirtualScreenSurface(ScreenId id,sptr<Surface> surface)589 int32_t RSRenderServiceConnection::SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface)
590 {
591     if (!screenManager_) {
592         return StatusCode::SCREEN_NOT_FOUND;
593     }
594     std::lock_guard<std::mutex> lock(mutex_);
595     return screenManager_->SetVirtualScreenSurface(id, surface);
596 }
597 
RemoveVirtualScreen(ScreenId id)598 void RSRenderServiceConnection::RemoveVirtualScreen(ScreenId id)
599 {
600     if (!screenManager_) {
601         return;
602     }
603     std::lock_guard<std::mutex> lock(mutex_);
604     screenManager_->RemoveVirtualScreen(id);
605     virtualScreenIds_.erase(id);
606     EventInfo event = { "VOTER_VIRTUALDISPLAY", REMOVE_VOTE };
607     NotifyRefreshRateEvent(event);
608 }
609 
SetScreenChangeCallback(sptr<RSIScreenChangeCallback> callback)610 int32_t RSRenderServiceConnection::SetScreenChangeCallback(sptr<RSIScreenChangeCallback> callback)
611 {
612     if (!callback) {
613         return INVALID_ARGUMENTS;
614     }
615     std::unique_lock<std::mutex> lock(mutex_);
616     if (screenChangeCallback_ == callback) {
617         return INVALID_ARGUMENTS;
618     }
619 
620     if (screenChangeCallback_ != nullptr) {
621         // remove the old callback
622         screenManager_->RemoveScreenChangeCallback(screenChangeCallback_);
623     }
624 
625     // update
626     int32_t status = screenManager_->AddScreenChangeCallback(callback);
627     auto tmp = screenChangeCallback_;
628     screenChangeCallback_ = callback;
629     lock.unlock();
630     return status;
631 }
632 
SetScreenActiveMode(ScreenId id,uint32_t modeId)633 void RSRenderServiceConnection::SetScreenActiveMode(ScreenId id, uint32_t modeId)
634 {
635     if (!screenManager_) {
636         return;
637     }
638     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
639     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
640         return RSHardwareThread::Instance().ScheduleTask(
641             [=]() { screenManager_->SetScreenActiveMode(id, modeId); }).wait();
642     } else {
643         return mainThread_->ScheduleTask(
644             [=]() { screenManager_->SetScreenActiveMode(id, modeId); }).wait();
645     }
646 }
647 
SetScreenRefreshRate(ScreenId id,int32_t sceneId,int32_t rate)648 void RSRenderServiceConnection::SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate)
649 {
650     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "RSRenderService::SetScreenRefreshRate");
651     auto &hgmCore = OHOS::Rosen::HgmCore::Instance();
652     int32_t setResult = hgmCore.SetScreenRefreshRate(id, sceneId, rate);
653     if (setResult != 0) {
654         RS_LOGW("SetScreenRefreshRate request of screen %{public}" PRIu64 " of rate %{public}d is refused",
655             id, rate);
656         return;
657     }
658     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
659 }
660 
SetRefreshRateMode(int32_t refreshRateMode)661 void RSRenderServiceConnection::SetRefreshRateMode(int32_t refreshRateMode)
662 {
663     if (!mainThread_) {
664         return;
665     }
666     ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "RSRenderService::SetRefreshRateMode");
667     auto &hgmCore = OHOS::Rosen::HgmCore::Instance();
668     int32_t setResult = hgmCore.SetRefreshRateMode(refreshRateMode);
669     RSSystemProperties::SetHgmRefreshRateModesEnabled(std::to_string(refreshRateMode));
670     if (setResult != 0) {
671         RS_LOGW("SetRefreshRateMode mode %{public}d is not supported", refreshRateMode);
672     }
673     ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
674 }
675 
SyncFrameRateRange(FrameRateLinkerId id,const FrameRateRange & range,int32_t animatorExpectedFrameRate)676 void RSRenderServiceConnection::SyncFrameRateRange(FrameRateLinkerId id,
677     const FrameRateRange& range, int32_t animatorExpectedFrameRate)
678 {
679     if (!mainThread_) {
680         return;
681     }
682     mainThread_->ScheduleTask(
683         [weakThis = wptr<RSRenderServiceConnection>(this), id, &range, animatorExpectedFrameRate]() {
684             sptr<RSRenderServiceConnection> connection = weakThis.promote();
685             if (!connection) {
686                 return;
687             }
688             auto& context = connection->mainThread_->GetContext();
689             auto& linkerMap = context.GetMutableFrameRateLinkerMap();
690             auto linker = linkerMap.GetFrameRateLinker(id);
691             if (linker == nullptr) {
692                 RS_LOGW("SyncFrameRateRange there is no frameRateLinker for id %{public}" PRIu64, id);
693                 return;
694             }
695             linker->SetExpectedRange(range);
696             linker->SetAnimatorExpectedFrameRate(animatorExpectedFrameRate);
697         }).wait();
698 }
699 
UnregisterFrameRateLinker(FrameRateLinkerId id)700 void RSRenderServiceConnection::UnregisterFrameRateLinker(FrameRateLinkerId id)
701 {
702     if (!mainThread_) {
703         return;
704     }
705     mainThread_->ScheduleTask(
706         [weakThis = wptr<RSRenderServiceConnection>(this), id]() {
707             sptr<RSRenderServiceConnection> connection = weakThis.promote();
708             if (!connection) {
709                 return;
710             }
711             auto& context = connection->mainThread_->GetContext();
712             auto& linkerMap = context.GetMutableFrameRateLinkerMap();
713             auto linker = linkerMap.GetFrameRateLinker(id);
714             if (linker == nullptr) {
715                 RS_LOGE("UnregisterFrameRateLinker there is no frameRateLinker for id %{public}" PRIu64, id);
716                 return;
717             }
718             linkerMap.UnregisterFrameRateLinker(id);
719         }).wait();
720 }
721 
GetScreenCurrentRefreshRate(ScreenId id)722 uint32_t RSRenderServiceConnection::GetScreenCurrentRefreshRate(ScreenId id)
723 {
724     auto &hgmCore = OHOS::Rosen::HgmCore::Instance();
725     uint32_t rate = hgmCore.GetScreenCurrentRefreshRate(id);
726     if (rate == 0) {
727         RS_LOGW("GetScreenCurrentRefreshRate failed to get current refreshrate of"
728             " screen : %{public}" PRIu64 "", id);
729     }
730     return rate;
731 }
732 
GetScreenSupportedRefreshRates(ScreenId id)733 std::vector<int32_t> RSRenderServiceConnection::GetScreenSupportedRefreshRates(ScreenId id)
734 {
735     auto &hgmCore = OHOS::Rosen::HgmCore::Instance();
736     std::vector<int32_t> rates = hgmCore.GetScreenComponentRefreshRates(id);
737     return rates;
738 }
739 
GetShowRefreshRateEnabled()740 bool RSRenderServiceConnection::GetShowRefreshRateEnabled()
741 {
742     return RSRealtimeRefreshRateManager::Instance().GetShowRefreshRateEnabled();
743 }
744 
SetShowRefreshRateEnabled(bool enable)745 void RSRenderServiceConnection::SetShowRefreshRateEnabled(bool enable)
746 {
747     return RSRealtimeRefreshRateManager::Instance().SetShowRefreshRateEnabled(enable);
748 }
749 
GetRefreshInfo(pid_t pid)750 std::string RSRenderServiceConnection::GetRefreshInfo(pid_t pid)
751 {
752     if (!mainThread_) {
753         return "";
754     }
755     auto& context = mainThread_->GetContext();
756     auto& nodeMap = context.GetMutableNodeMap();
757     std::string surfaceName = nodeMap.GetSelfDrawSurfaceNameByPid(pid);
758     if (surfaceName.empty()) {
759         return "";
760     }
761     std::string dumpString;
762     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
763     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
764         RSHardwareThread::Instance().ScheduleTask(
765             [this, &dumpString, &surfaceName]() { return screenManager_->FpsDump(dumpString, surfaceName); }).wait();
766     } else {
767         mainThread_->ScheduleTask(
768             [this, &dumpString, &surfaceName]() { return screenManager_->FpsDump(dumpString, surfaceName); }).wait();
769     }
770     return dumpString;
771 }
772 
GetCurrentRefreshRateMode()773 int32_t RSRenderServiceConnection::GetCurrentRefreshRateMode()
774 {
775     auto &hgmCore = OHOS::Rosen::HgmCore::Instance();
776     int32_t refreshRateMode = hgmCore.GetCurrentRefreshRateMode();
777     return refreshRateMode;
778 }
779 
SetVirtualScreenResolution(ScreenId id,uint32_t width,uint32_t height)780 int32_t RSRenderServiceConnection::SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height)
781 {
782     if (!screenManager_) {
783         return StatusCode::SCREEN_NOT_FOUND;
784     }
785     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
786     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
787         return RSHardwareThread::Instance().ScheduleTask(
788             [=]() { return screenManager_->SetVirtualScreenResolution(id, width, height); }).get();
789     } else {
790         return mainThread_->ScheduleTask(
791             [=]() { return screenManager_->SetVirtualScreenResolution(id, width, height); }).get();
792     }
793 }
794 
MarkPowerOffNeedProcessOneFrame()795 void RSRenderServiceConnection::MarkPowerOffNeedProcessOneFrame()
796 {
797     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
798     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
799         renderThread_.PostTask(
800             [weakThis = wptr<RSRenderServiceConnection>(this)]() {
801                 sptr<RSRenderServiceConnection> connection = weakThis.promote();
802                 if (!connection) {
803                     return;
804                 }
805                 connection->screenManager_->MarkPowerOffNeedProcessOneFrame();
806             }
807         );
808     }
809 }
810 
DisablePowerOffRenderControl(ScreenId id)811 void RSRenderServiceConnection::DisablePowerOffRenderControl(ScreenId id)
812 {
813     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
814     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
815         renderThread_.PostTask(
816             [weakThis = wptr<RSRenderServiceConnection>(this), id]() {
817                 sptr<RSRenderServiceConnection> connection = weakThis.promote();
818                 if (!connection) {
819                     return;
820                 }
821                 connection->screenManager_->DisablePowerOffRenderControl(id);
822             }
823         );
824     }
825 }
826 
SetScreenPowerStatus(ScreenId id,ScreenPowerStatus status)827 void RSRenderServiceConnection::SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status)
828 {
829     if (!screenManager_) {
830         return;
831     }
832     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
833     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
834         RSHardwareThread::Instance().ScheduleTask(
835             [=]() { screenManager_->SetScreenPowerStatus(id, status); }).wait();
836         mainThread_->SetDiscardJankFrames(true);
837         renderThread_.SetDiscardJankFrames(true);
838         OHOS::Rosen::HgmCore::Instance().NotifyScreenPowerStatus(id, status);
839     } else {
840         mainThread_->ScheduleTask(
841             [=]() { screenManager_->SetScreenPowerStatus(id, status); }).wait();
842     }
843 }
844 
845 namespace {
TakeSurfaceCaptureForUiParallel(NodeId id,sptr<RSISurfaceCaptureCallback> callback,const RSSurfaceCaptureConfig & captureConfig)846 void TakeSurfaceCaptureForUiParallel(
847     NodeId id, sptr<RSISurfaceCaptureCallback> callback, const RSSurfaceCaptureConfig& captureConfig)
848 {
849     RS_LOGI("TakeSurfaceCaptureForUiParallel nodeId:[%{public}" PRIu64 "], issync:%{public}s", id,
850         captureConfig.isSync ? "true" : "false");
851     std::function<void()> captureTask = [id, callback, captureConfig]() {
852         RSUiCaptureTaskParallel::Capture(id, callback, captureConfig);
853     };
854 
855     if (captureConfig.isSync) {
856         RSMainThread::Instance()->AddUiCaptureTask(id, captureTask);
857         return;
858     }
859 
860     auto node = RSMainThread::Instance()->GetContext().GetNodeMap().GetRenderNode<RSRenderNode>(id);
861     if (!node) {
862         RS_LOGE("RSRenderServiceConnection::TakeSurfaceCaptureForUiParallel node is nullptr");
863         callback->OnSurfaceCapture(id, nullptr);
864         return;
865     }
866 
867     if (node->IsOnTheTree() && !node->IsDirty() && !node->IsSubTreeDirty()) {
868         RSMainThread::Instance()->PostTask(captureTask);
869     } else {
870         RSMainThread::Instance()->AddUiCaptureTask(id, captureTask);
871     }
872 }
873 
TakeSurfaceCaptureForUIWithUni(NodeId id,sptr<RSISurfaceCaptureCallback> callback,const RSSurfaceCaptureConfig & captureConfig)874 void TakeSurfaceCaptureForUIWithUni(NodeId id, sptr<RSISurfaceCaptureCallback> callback,
875     const RSSurfaceCaptureConfig& captureConfig)
876 {
877     std::function<void()> offscreenRenderTask = [id, callback, captureConfig]() -> void {
878         RS_LOGD("RSRenderService::TakeSurfaceCaptureForUIWithUni callback->OnOffscreenRender"
879             " nodeId:[%{public}" PRIu64 "]", id);
880         ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "RSRenderService::TakeSurfaceCaptureForUIWithUni");
881         std::shared_ptr<RSUniUICapture> rsUniUICapture = std::make_shared<RSUniUICapture>(id, captureConfig);
882         std::shared_ptr<Media::PixelMap> pixelmap = rsUniUICapture->TakeLocalCapture();
883         callback->OnSurfaceCapture(id, pixelmap.get());
884         ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
885     };
886     if (!captureConfig.isSync) {
887         RSOffscreenRenderThread::Instance().PostTask(offscreenRenderTask);
888     } else {
889         auto node = RSMainThread::Instance()->GetContext().GetNodeMap().GetRenderNode<RSRenderNode>(id);
890         if (node == nullptr || !node->GetCommandExecuted()) {
891             RSOffscreenRenderThread::Instance().InSertCaptureTask(id, offscreenRenderTask);
892             return;
893         }
894         RSOffscreenRenderThread::Instance().PostTask(offscreenRenderTask);
895         node->SetCommandExecuted(false);
896     }
897 }
898 }
899 
TakeSurfaceCapture(NodeId id,sptr<RSISurfaceCaptureCallback> callback,const RSSurfaceCaptureConfig & captureConfig,RSSurfaceCapturePermissions permissions)900 void RSRenderServiceConnection::TakeSurfaceCapture(NodeId id, sptr<RSISurfaceCaptureCallback> callback,
901     const RSSurfaceCaptureConfig& captureConfig, RSSurfaceCapturePermissions permissions)
902 {
903     if (!mainThread_) {
904         return;
905     }
906     std::function<void()> captureTask = [id, callback, captureConfig,
907         screenCapturePermission = permissions.screenCapturePermission,
908         isSystemCalling = permissions.isSystemCalling,
909         selfCapture = permissions.selfCapture]() -> void {
910         if (captureConfig.captureType == SurfaceCaptureType::UICAPTURE) {
911             // When the isSync flag in captureConfig is true, UI capture processes commands before capture.
912             // When the isSync flag in captureConfig is false, UI capture will check null node independently.
913             // Therefore, a null node is valid for UI capture.
914             auto uiCaptureHasPermission = selfCapture || isSystemCalling;
915             if (!uiCaptureHasPermission) {
916                 RS_LOGE("RSRenderServiceConnection::TakeSurfaceCapture uicapture failed, nodeId:[%{public}" PRIu64
917                         "], isSystemCalling: %{public}u, selfCapture: %{public}u",
918                     id, isSystemCalling, selfCapture);
919                 callback->OnSurfaceCapture(id, nullptr);
920                 return;
921             }
922             if (RSUniRenderJudgement::IsUniRender()) {
923                 TakeSurfaceCaptureForUiParallel(id, callback, captureConfig);
924             } else {
925                 TakeSurfaceCaptureForUIWithUni(id, callback, captureConfig);
926             }
927             return;
928         }
929         auto node = RSMainThread::Instance()->GetContext().GetNodeMap().GetRenderNode(id);
930         if (node == nullptr) {
931             RS_LOGE("RSRenderServiceConnection::TakeSurfaceCapture failed, node is nullptr");
932             callback->OnSurfaceCapture(id, nullptr);
933             return;
934         }
935         auto displayCaptureHasPermission = screenCapturePermission && isSystemCalling;
936         auto surfaceCaptureHasPermission = selfCapture || isSystemCalling;
937         if ((node->GetType() == RSRenderNodeType::DISPLAY_NODE && !displayCaptureHasPermission) ||
938             (node->GetType() == RSRenderNodeType::SURFACE_NODE && !surfaceCaptureHasPermission)) {
939             RS_LOGE("RSRenderServiceConnection::TakeSurfaceCapture failed, node type: %{public}u, "
940                 "screenCapturePermission: %{public}u, isSystemCalling: %{public}u, selfCapture: %{public}u",
941                 node->GetType(), screenCapturePermission, isSystemCalling, selfCapture);
942             callback->OnSurfaceCapture(id, nullptr);
943             return;
944         }
945         auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
946         if (RSSystemParameters::GetRsSurfaceCaptureType() ==
947             RsSurfaceCaptureType::RS_SURFACE_CAPTURE_TYPE_MAIN_THREAD ||
948             renderType == UniRenderEnabledType::UNI_RENDER_DISABLED) {
949             auto isProcOnBgThread = (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) ?
950                 !node->IsOnTheTree() : false;
951             std::function<void()> captureTaskInner = [id, callback, captureConfig, isProcOnBgThread]() -> void {
952                 RS_LOGD("RSRenderService::TakeSurfaceCapture captureTaskInner nodeId:[%{public}" PRIu64 "]", id);
953                 ROSEN_TRACE_BEGIN(HITRACE_TAG_GRAPHIC_AGP, "RSRenderService::TakeSurfaceCapture");
954                 RSSurfaceCaptureTask task(id, captureConfig, isProcOnBgThread);
955                 if (!task.Run(callback)) {
956                     callback->OnSurfaceCapture(id, nullptr);
957                 }
958                 ROSEN_TRACE_END(HITRACE_TAG_GRAPHIC_AGP);
959             };
960             if (isProcOnBgThread) {
961                 RSBackgroundThread::Instance().PostTask(captureTaskInner);
962             } else {
963                 captureTaskInner();
964             }
965         } else {
966             RSSurfaceCaptureTaskParallel::CheckModifiers(id, captureConfig.useCurWindow);
967             RSSurfaceCaptureTaskParallel::Capture(id, callback, captureConfig, isSystemCalling);
968         }
969     };
970     mainThread_->PostTask(captureTask);
971 }
972 
RegisterApplicationAgent(uint32_t pid,sptr<IApplicationAgent> app)973 void RSRenderServiceConnection::RegisterApplicationAgent(uint32_t pid, sptr<IApplicationAgent> app)
974 {
975     if (!mainThread_) {
976         return;
977     }
978     auto captureTask = [weakThis = wptr<RSRenderServiceConnection>(this), pid, app]() -> void {
979         sptr<RSRenderServiceConnection> connection = weakThis.promote();
980         if (!connection) {
981             return;
982         }
983         connection->mainThread_->RegisterApplicationAgent(pid, app);
984     };
985     mainThread_->PostTask(captureTask);
986 
987     app->AsObject()->AddDeathRecipient(ApplicationDeathRecipient_);
988 }
989 
UnRegisterApplicationAgent(sptr<IApplicationAgent> app)990 void RSRenderServiceConnection::UnRegisterApplicationAgent(sptr<IApplicationAgent> app)
991 {
992     auto captureTask = [=]() -> void {
993         RSMainThread::Instance()->UnRegisterApplicationAgent(app);
994     };
995     RSMainThread::Instance()->ScheduleTask(captureTask).wait();
996 }
997 
GetVirtualScreenResolution(ScreenId id)998 RSVirtualScreenResolution RSRenderServiceConnection::GetVirtualScreenResolution(ScreenId id)
999 {
1000     RSVirtualScreenResolution virtualScreenResolution;
1001     if (!screenManager_) {
1002         return virtualScreenResolution;
1003     }
1004     screenManager_->GetVirtualScreenResolution(id, virtualScreenResolution);
1005     return virtualScreenResolution;
1006 }
1007 
GetScreenActiveMode(ScreenId id)1008 RSScreenModeInfo RSRenderServiceConnection::GetScreenActiveMode(ScreenId id)
1009 {
1010     RSScreenModeInfo screenModeInfo;
1011     if (!screenManager_) {
1012         return screenModeInfo;
1013     }
1014     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1015     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1016         RSHardwareThread::Instance().ScheduleTask(
1017             [=, &screenModeInfo]() { return screenManager_->GetScreenActiveMode(id, screenModeInfo); }).wait();
1018     } else {
1019         mainThread_->ScheduleTask(
1020             [=, &screenModeInfo]() { return screenManager_->GetScreenActiveMode(id, screenModeInfo); }).wait();
1021     }
1022     return screenModeInfo;
1023 }
1024 
GetTotalAppMemSize(float & cpuMemSize,float & gpuMemSize)1025 bool RSRenderServiceConnection::GetTotalAppMemSize(float& cpuMemSize, float& gpuMemSize)
1026 {
1027     RSMainThread::Instance()->GetAppMemoryInMB(cpuMemSize, gpuMemSize);
1028     gpuMemSize += RSSubThreadManager::Instance()->GetAppGpuMemoryInMB();
1029     return true;
1030 }
1031 
GetMemoryGraphic(int pid)1032 MemoryGraphic RSRenderServiceConnection::GetMemoryGraphic(int pid)
1033 {
1034     MemoryGraphic memoryGraphic;
1035     if (!mainThread_) {
1036         return memoryGraphic;
1037     }
1038     if (GetUniRenderEnabled()) {
1039         RSMainThread* mainThread = mainThread_;
1040         mainThread_->ScheduleTask([mainThread, &pid, &memoryGraphic]() {
1041             if (RSMainThread::Instance()->GetContext().GetNodeMap().ContainPid(pid)) {
1042                 mainThread->CountMem(pid, memoryGraphic);
1043             }
1044         }).wait();
1045         return memoryGraphic;
1046     } else {
1047         return memoryGraphic;
1048     }
1049 }
1050 
GetMemoryGraphics()1051 std::vector<MemoryGraphic> RSRenderServiceConnection::GetMemoryGraphics()
1052 {
1053     std::vector<MemoryGraphic> memoryGraphics;
1054     if (!mainThread_) {
1055         return memoryGraphics;
1056     }
1057     if (GetUniRenderEnabled()) {
1058         mainThread_->ScheduleTask(
1059             [weakThis = wptr<RSRenderServiceConnection>(this), &memoryGraphics]() {
1060                 sptr<RSRenderServiceConnection> connection = weakThis.promote();
1061                 if (!connection) {
1062                     return;
1063                 }
1064                 return connection->mainThread_->CountMem(memoryGraphics);
1065             }).wait();
1066         return memoryGraphics;
1067     } else {
1068         return memoryGraphics;
1069     }
1070 }
1071 
GetScreenSupportedModes(ScreenId id)1072 std::vector<RSScreenModeInfo> RSRenderServiceConnection::GetScreenSupportedModes(ScreenId id)
1073 {
1074     std::vector<RSScreenModeInfo> screenSupportedModes;
1075     if (!screenManager_) {
1076         return screenSupportedModes;
1077     }
1078     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1079     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1080         return RSHardwareThread::Instance().ScheduleTask(
1081             [=]() { return screenManager_->GetScreenSupportedModes(id); }).get();
1082     } else {
1083         return mainThread_->ScheduleTask(
1084             [=]() { return screenManager_->GetScreenSupportedModes(id); }).get();
1085     }
1086 }
1087 
GetScreenCapability(ScreenId id)1088 RSScreenCapability RSRenderServiceConnection::GetScreenCapability(ScreenId id)
1089 {
1090     RSScreenCapability screenCapability;
1091     if (!screenManager_) {
1092         return screenCapability;
1093     }
1094     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1095     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1096         return RSHardwareThread::Instance().ScheduleTask(
1097             [=]() { return screenManager_->GetScreenCapability(id); }).get();
1098     } else {
1099         return mainThread_->ScheduleTask(
1100             [=]() { return screenManager_->GetScreenCapability(id); }).get();
1101     }
1102 }
1103 
GetScreenPowerStatus(ScreenId id)1104 ScreenPowerStatus RSRenderServiceConnection::GetScreenPowerStatus(ScreenId id)
1105 {
1106     if (!screenManager_) {
1107         return ScreenPowerStatus::INVALID_POWER_STATUS;
1108     }
1109     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1110     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1111         return RSHardwareThread::Instance().ScheduleTask(
1112             [=]() { return screenManager_->GetScreenPowerStatus(id); }).get();
1113     } else {
1114         return mainThread_->ScheduleTask(
1115             [=]() { return screenManager_->GetScreenPowerStatus(id); }).get();
1116     }
1117 }
1118 
GetScreenData(ScreenId id)1119 RSScreenData RSRenderServiceConnection::GetScreenData(ScreenId id)
1120 {
1121     RSScreenData screenData;
1122     if (!screenManager_) {
1123         return screenData;
1124     }
1125     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1126     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1127         return RSHardwareThread::Instance().ScheduleTask(
1128             [=]() { return screenManager_->GetScreenData(id); }).get();
1129     } else {
1130         return mainThread_->ScheduleTask(
1131             [=]() { return screenManager_->GetScreenData(id); }).get();
1132     }
1133 }
1134 
GetScreenBacklight(ScreenId id)1135 int32_t RSRenderServiceConnection::GetScreenBacklight(ScreenId id)
1136 {
1137     if (!screenManager_) {
1138         return INVALID_BACKLIGHT_VALUE;
1139     }
1140     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1141     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1142         return RSHardwareThread::Instance().ScheduleTask(
1143             [=]() { return screenManager_->GetScreenBacklight(id); }).get();
1144     } else {
1145         return mainThread_->ScheduleTask(
1146             [=]() { return screenManager_->GetScreenBacklight(id); }).get();
1147     }
1148 }
1149 
SetScreenBacklight(ScreenId id,uint32_t level)1150 void RSRenderServiceConnection::SetScreenBacklight(ScreenId id, uint32_t level)
1151 {
1152     if (!screenManager_) {
1153         return;
1154     }
1155     RSLuminanceControl::Get().SetSdrLuminance(id, level);
1156     if (RSLuminanceControl::Get().IsHdrOn(id) && level > 0) {
1157         auto task = [weakThis = wptr<RSRenderServiceConnection>(this)]() {
1158             sptr<RSRenderServiceConnection> connection = weakThis.promote();
1159             if (!connection) {
1160                 RS_LOGE("RSRenderServiceConnection::SetScreenBacklight fail");
1161                 return;
1162             }
1163             connection->mainThread_->SetForceUpdateUniRenderFlag(true);
1164             connection->mainThread_->SetLuminanceChangingStatus(true);
1165             connection->mainThread_->SetDirtyFlag();
1166             connection->mainThread_->RequestNextVSync();
1167         };
1168         mainThread_->PostTask(task);
1169         return;
1170     }
1171 
1172     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1173     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1174         screenManager_->SetScreenBacklight(id, level);
1175     } else {
1176         mainThread_->ScheduleTask(
1177             [=]() { screenManager_->SetScreenBacklight(id, level); }).wait();
1178     }
1179 }
1180 
RegisterBufferClearListener(NodeId id,sptr<RSIBufferClearCallback> callback)1181 void RSRenderServiceConnection::RegisterBufferClearListener(
1182     NodeId id, sptr<RSIBufferClearCallback> callback)
1183 {
1184     if (!mainThread_) {
1185         return;
1186     }
1187     auto registerBufferClearListener =
1188         [id, callback, weakThis = wptr<RSRenderServiceConnection>(this)]() -> bool {
1189             sptr<RSRenderServiceConnection> connection = weakThis.promote();
1190             if (!connection) {
1191                 return false;
1192             }
1193             if (auto node = connection->mainThread_->GetContext().GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
1194                 node->RegisterBufferClearListener(callback);
1195                 return true;
1196             }
1197             return false;
1198     };
1199     if (!registerBufferClearListener()) {
1200         mainThread_->PostTask(registerBufferClearListener);
1201     }
1202 }
1203 
RegisterBufferAvailableListener(NodeId id,sptr<RSIBufferAvailableCallback> callback,bool isFromRenderThread)1204 void RSRenderServiceConnection::RegisterBufferAvailableListener(
1205     NodeId id, sptr<RSIBufferAvailableCallback> callback, bool isFromRenderThread)
1206 {
1207     if (!mainThread_) {
1208         return;
1209     }
1210     auto registerBufferAvailableListener =
1211         [id, callback, isFromRenderThread, weakThis = wptr<RSRenderServiceConnection>(this)]() -> bool {
1212             sptr<RSRenderServiceConnection> connection = weakThis.promote();
1213             if (!connection) {
1214                 return false;
1215             }
1216             if (auto node = connection->mainThread_->GetContext().GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id)) {
1217                 node->RegisterBufferAvailableListener(callback, isFromRenderThread);
1218                 return true;
1219             }
1220             return false;
1221     };
1222     if (!registerBufferAvailableListener()) {
1223         RS_LOGD("RegisterBufferAvailableListener: node not found, post task to retry");
1224         mainThread_->PostTask(registerBufferAvailableListener);
1225     }
1226 }
1227 
GetScreenSupportedColorGamuts(ScreenId id,std::vector<ScreenColorGamut> & mode)1228 int32_t RSRenderServiceConnection::GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode)
1229 {
1230     if (!screenManager_) {
1231         return StatusCode::SCREEN_NOT_FOUND;
1232     }
1233     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1234     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1235         return RSHardwareThread::Instance().ScheduleTask(
1236             [=, &mode]() { return screenManager_->GetScreenSupportedColorGamuts(id, mode); }).get();
1237     } else {
1238         return mainThread_->ScheduleTask(
1239             [=, &mode]() { return screenManager_->GetScreenSupportedColorGamuts(id, mode); }).get();
1240     }
1241 }
1242 
GetScreenSupportedMetaDataKeys(ScreenId id,std::vector<ScreenHDRMetadataKey> & keys)1243 int32_t RSRenderServiceConnection::GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys)
1244 {
1245     if (!screenManager_) {
1246         return StatusCode::SCREEN_NOT_FOUND;
1247     }
1248     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1249     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1250         return RSHardwareThread::Instance().ScheduleTask(
1251             [=, &keys]() { return screenManager_->GetScreenSupportedMetaDataKeys(id, keys); }).get();
1252     } else {
1253         return mainThread_->ScheduleTask(
1254             [=, &keys]() { return screenManager_->GetScreenSupportedMetaDataKeys(id, keys); }).get();
1255     }
1256 }
1257 
GetScreenColorGamut(ScreenId id,ScreenColorGamut & mode)1258 int32_t RSRenderServiceConnection::GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode)
1259 {
1260     if (!screenManager_) {
1261         return StatusCode::SCREEN_NOT_FOUND;
1262     }
1263     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1264     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1265         return RSHardwareThread::Instance().ScheduleTask(
1266             [=, &mode]() { return screenManager_->GetScreenColorGamut(id, mode); }).get();
1267     } else {
1268         return mainThread_->ScheduleTask(
1269             [=, &mode]() { return screenManager_->GetScreenColorGamut(id, mode); }).get();
1270     }
1271 }
1272 
SetScreenColorGamut(ScreenId id,int32_t modeIdx)1273 int32_t RSRenderServiceConnection::SetScreenColorGamut(ScreenId id, int32_t modeIdx)
1274 {
1275     if (!screenManager_) {
1276         return StatusCode::SCREEN_NOT_FOUND;
1277     }
1278     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1279     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1280         return RSHardwareThread::Instance().ScheduleTask(
1281             [=]() { return screenManager_->SetScreenColorGamut(id, modeIdx); }).get();
1282     } else {
1283         return mainThread_->ScheduleTask(
1284             [=]() { return screenManager_->SetScreenColorGamut(id, modeIdx); }).get();
1285     }
1286 }
1287 
SetScreenGamutMap(ScreenId id,ScreenGamutMap mode)1288 int32_t RSRenderServiceConnection::SetScreenGamutMap(ScreenId id, ScreenGamutMap mode)
1289 {
1290     if (!screenManager_) {
1291         return StatusCode::SCREEN_NOT_FOUND;
1292     }
1293     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1294     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1295         return RSHardwareThread::Instance().ScheduleTask(
1296             [=]() { return screenManager_->SetScreenGamutMap(id, mode); }).get();
1297     } else {
1298         return mainThread_->ScheduleTask(
1299             [=]() { return screenManager_->SetScreenGamutMap(id, mode); }).get();
1300     }
1301 }
1302 
SetScreenCorrection(ScreenId id,ScreenRotation screenRotation)1303 int32_t RSRenderServiceConnection::SetScreenCorrection(ScreenId id, ScreenRotation screenRotation)
1304 {
1305     if (!screenManager_) {
1306         return StatusCode::SCREEN_NOT_FOUND;
1307     }
1308     std::lock_guard<std::mutex> lock(mutex_);
1309     return screenManager_->SetScreenCorrection(id, screenRotation);
1310 }
1311 
SetVirtualMirrorScreenCanvasRotation(ScreenId id,bool canvasRotation)1312 bool RSRenderServiceConnection::SetVirtualMirrorScreenCanvasRotation(ScreenId id, bool canvasRotation)
1313 {
1314     if (!screenManager_) {
1315         return false;
1316     }
1317     std::lock_guard<std::mutex> lock(mutex_);
1318     return screenManager_->SetVirtualMirrorScreenCanvasRotation(id, canvasRotation);
1319 }
1320 
SetGlobalDarkColorMode(bool isDark)1321 bool RSRenderServiceConnection::SetGlobalDarkColorMode(bool isDark)
1322 {
1323     if (!mainThread_) {
1324         return false;
1325     }
1326     std::lock_guard<std::mutex> lock(mutex_);
1327     auto task = [weakThis = wptr<RSRenderServiceConnection>(this), isDark]() {
1328         sptr<RSRenderServiceConnection> connection = weakThis.promote();
1329         if (!connection) {
1330             RS_LOGE("RSRenderServiceConnection::SetGlobalDarkColorMode fail");
1331             return;
1332         }
1333         connection->mainThread_->SetGlobalDarkColorMode(isDark);
1334     };
1335     mainThread_->PostTask(task);
1336     return true;
1337 }
1338 
SetVirtualMirrorScreenScaleMode(ScreenId id,ScreenScaleMode scaleMode)1339 bool RSRenderServiceConnection::SetVirtualMirrorScreenScaleMode(ScreenId id, ScreenScaleMode scaleMode)
1340 {
1341     if (!screenManager_) {
1342         return false;
1343     }
1344     std::lock_guard<std::mutex> lock(mutex_);
1345     return screenManager_->SetVirtualMirrorScreenScaleMode(id, scaleMode);
1346 }
1347 
GetScreenGamutMap(ScreenId id,ScreenGamutMap & mode)1348 int32_t RSRenderServiceConnection::GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode)
1349 {
1350     if (!screenManager_) {
1351         return StatusCode::SCREEN_NOT_FOUND;
1352     }
1353     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1354     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1355         return RSHardwareThread::Instance().ScheduleTask(
1356             [=, &mode]() { return screenManager_->GetScreenGamutMap(id, mode); }).get();
1357     } else {
1358         return mainThread_->ScheduleTask(
1359             [=, &mode]() { return screenManager_->GetScreenGamutMap(id, mode); }).get();
1360     }
1361 }
1362 
GetScreenHDRCapability(ScreenId id,RSScreenHDRCapability & screenHdrCapability)1363 int32_t RSRenderServiceConnection::GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability)
1364 {
1365     if (!screenManager_) {
1366         return StatusCode::SCREEN_NOT_FOUND;
1367     }
1368     std::lock_guard<std::mutex> lock(mutex_);
1369     return screenManager_->GetScreenHDRCapability(id, screenHdrCapability);
1370 }
1371 
GetPixelFormat(ScreenId id,GraphicPixelFormat & pixelFormat)1372 int32_t RSRenderServiceConnection::GetPixelFormat(ScreenId id, GraphicPixelFormat& pixelFormat)
1373 {
1374     if (!screenManager_) {
1375         return StatusCode::SCREEN_NOT_FOUND;
1376     }
1377     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1378     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1379         return RSHardwareThread::Instance().ScheduleTask(
1380             [=, &pixelFormat]() { return screenManager_->GetPixelFormat(id, pixelFormat); }).get();
1381     } else {
1382         return mainThread_->ScheduleTask(
1383             [=, &pixelFormat]() { return screenManager_->GetPixelFormat(id, pixelFormat); }).get();
1384     }
1385 }
1386 
SetPixelFormat(ScreenId id,GraphicPixelFormat pixelFormat)1387 int32_t RSRenderServiceConnection::SetPixelFormat(ScreenId id, GraphicPixelFormat pixelFormat)
1388 {
1389     if (!screenManager_) {
1390         return StatusCode::SCREEN_NOT_FOUND;
1391     }
1392     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1393     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1394         return RSHardwareThread::Instance().ScheduleTask(
1395             [=]() { return screenManager_->SetPixelFormat(id, pixelFormat); }).get();
1396     } else {
1397         return mainThread_->ScheduleTask(
1398             [=]() { return screenManager_->SetPixelFormat(id, pixelFormat); }).get();
1399     }
1400 }
1401 
GetScreenSupportedHDRFormats(ScreenId id,std::vector<ScreenHDRFormat> & hdrFormats)1402 int32_t RSRenderServiceConnection::GetScreenSupportedHDRFormats(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats)
1403 {
1404     if (!screenManager_) {
1405         return StatusCode::SCREEN_NOT_FOUND;
1406     }
1407     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1408     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1409         return RSHardwareThread::Instance().ScheduleTask(
1410             [=, &hdrFormats]() { return screenManager_->GetScreenSupportedHDRFormats(id, hdrFormats); }).get();
1411     } else {
1412         return mainThread_->ScheduleTask(
1413             [=, &hdrFormats]() { return screenManager_->GetScreenSupportedHDRFormats(id, hdrFormats); }).get();
1414     }
1415 }
1416 
GetScreenHDRFormat(ScreenId id,ScreenHDRFormat & hdrFormat)1417 int32_t RSRenderServiceConnection::GetScreenHDRFormat(ScreenId id, ScreenHDRFormat& hdrFormat)
1418 {
1419     if (!screenManager_) {
1420         return StatusCode::SCREEN_NOT_FOUND;
1421     }
1422     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1423     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1424         return RSHardwareThread::Instance().ScheduleTask(
1425             [=, &hdrFormat]() { return screenManager_->GetScreenHDRFormat(id, hdrFormat); }).get();
1426     } else {
1427         return mainThread_->ScheduleTask(
1428             [=, &hdrFormat]() { return screenManager_->GetScreenHDRFormat(id, hdrFormat); }).get();
1429     }
1430 }
1431 
SetScreenHDRFormat(ScreenId id,int32_t modeIdx)1432 int32_t RSRenderServiceConnection::SetScreenHDRFormat(ScreenId id, int32_t modeIdx)
1433 {
1434     if (!screenManager_) {
1435         return StatusCode::SCREEN_NOT_FOUND;
1436     }
1437     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1438     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1439         return RSHardwareThread::Instance().ScheduleTask(
1440             [=]() { return screenManager_->SetScreenHDRFormat(id, modeIdx); }).get();
1441     } else {
1442         return mainThread_->ScheduleTask(
1443             [=]() { return screenManager_->SetScreenHDRFormat(id, modeIdx); }).get();
1444     }
1445 }
1446 
GetScreenSupportedColorSpaces(ScreenId id,std::vector<GraphicCM_ColorSpaceType> & colorSpaces)1447 int32_t RSRenderServiceConnection::GetScreenSupportedColorSpaces(
1448     ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces)
1449 {
1450     if (!screenManager_) {
1451         return StatusCode::SCREEN_NOT_FOUND;
1452     }
1453     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1454     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1455         return RSHardwareThread::Instance().ScheduleTask(
1456             [=, &colorSpaces]() { return screenManager_->GetScreenSupportedColorSpaces(id, colorSpaces); }).get();
1457     } else {
1458         return mainThread_->ScheduleTask(
1459             [=, &colorSpaces]() { return screenManager_->GetScreenSupportedColorSpaces(id, colorSpaces); }).get();
1460     }
1461 }
1462 
GetScreenColorSpace(ScreenId id,GraphicCM_ColorSpaceType & colorSpace)1463 int32_t RSRenderServiceConnection::GetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType& colorSpace)
1464 {
1465     if (!screenManager_) {
1466         return StatusCode::SCREEN_NOT_FOUND;
1467     }
1468     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1469     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1470         return RSHardwareThread::Instance().ScheduleTask(
1471             [=, &colorSpace]() { return screenManager_->GetScreenColorSpace(id, colorSpace); }).get();
1472     } else {
1473         return mainThread_->ScheduleTask(
1474             [=, &colorSpace]() { return screenManager_->GetScreenColorSpace(id, colorSpace); }).get();
1475     }
1476 }
1477 
SetScreenColorSpace(ScreenId id,GraphicCM_ColorSpaceType colorSpace)1478 int32_t RSRenderServiceConnection::SetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType colorSpace)
1479 {
1480     if (!screenManager_) {
1481         return StatusCode::SCREEN_NOT_FOUND;
1482     }
1483     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1484     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1485         return RSHardwareThread::Instance().ScheduleTask(
1486             [=]() { return screenManager_->SetScreenColorSpace(id, colorSpace); }).get();
1487     } else {
1488         return mainThread_->ScheduleTask(
1489             [=]() { return screenManager_->SetScreenColorSpace(id, colorSpace); }).get();
1490     }
1491 }
1492 
GetScreenType(ScreenId id,RSScreenType & screenType)1493 int32_t RSRenderServiceConnection::GetScreenType(ScreenId id, RSScreenType& screenType)
1494 {
1495     if (!screenManager_) {
1496         return StatusCode::SCREEN_NOT_FOUND;
1497     }
1498     std::lock_guard<std::mutex> lock(mutex_);
1499     return screenManager_->GetScreenType(id, screenType);
1500 }
1501 
GetBitmap(NodeId id,Drawing::Bitmap & bitmap)1502 bool RSRenderServiceConnection::GetBitmap(NodeId id, Drawing::Bitmap& bitmap)
1503 {
1504     if (!mainThread_) {
1505         return false;
1506     }
1507     std::promise<bool> result;
1508     std::future<bool> future = result.get_future();
1509     RSMainThread* mainThread = mainThread_;
1510     RSUniRenderThread* renderThread = &renderThread_;
1511     auto getBitmapTask = [id, &bitmap, mainThread, renderThread, &result]() {
1512         auto node = mainThread->GetContext().GetNodeMap().GetRenderNode<RSCanvasDrawingRenderNode>(id);
1513         if (node == nullptr) {
1514             RS_LOGE("RSRenderServiceConnection::GetBitmap cannot find NodeId: [%{public}" PRIu64 "]", id);
1515             result.set_value(false);
1516             return;
1517         }
1518         if (node->GetType() != RSRenderNodeType::CANVAS_DRAWING_NODE) {
1519             RS_LOGE("RSRenderServiceConnection::GetBitmap RenderNodeType != RSRenderNodeType::CANVAS_DRAWING_NODE");
1520             result.set_value(false);
1521             return;
1522         }
1523         auto grContext = renderThread->GetRenderEngine()->GetRenderContext()->GetDrGPUContext();
1524         auto drawableNode = DrawableV2::RSRenderNodeDrawableAdapter::OnGenerate(node);
1525         auto getDrawableBitmapTask = [drawableNode, &bitmap, grContext, &result]() {
1526             bitmap = std::static_pointer_cast<DrawableV2::RSCanvasDrawingRenderNodeDrawable>(drawableNode)
1527                 ->GetBitmap(grContext);
1528             result.set_value(!bitmap.IsEmpty());
1529         };
1530         renderThread->PostTask(getDrawableBitmapTask);
1531     };
1532     mainThread_->PostTask(getBitmapTask);
1533     return future.get();
1534 }
1535 
GetPixelmap(NodeId id,const std::shared_ptr<Media::PixelMap> pixelmap,const Drawing::Rect * rect,std::shared_ptr<Drawing::DrawCmdList> drawCmdList)1536 bool RSRenderServiceConnection::GetPixelmap(NodeId id, const std::shared_ptr<Media::PixelMap> pixelmap,
1537     const Drawing::Rect* rect, std::shared_ptr<Drawing::DrawCmdList> drawCmdList)
1538 {
1539     if (!mainThread_) {
1540         return false;
1541     }
1542     std::promise<bool> result;
1543     std::future<bool> future = result.get_future();
1544     RSMainThread* mainThread = mainThread_;
1545     RSUniRenderThread* renderThread = &renderThread_;
1546     auto getPixelMapTask = [id, pixelmap, rect, drawCmdList, mainThread, renderThread, &result]() {
1547         auto node = mainThread->GetContext().GetNodeMap().GetRenderNode<RSCanvasDrawingRenderNode>(id);
1548         if (node == nullptr) {
1549             RS_LOGD("RSRenderServiceConnection::GetPixelmap: cannot find NodeId: [%{public}" PRIu64 "]", id);
1550             result.set_value(false);
1551             return;
1552         }
1553         if (node->GetType() != RSRenderNodeType::CANVAS_DRAWING_NODE) {
1554             RS_LOGE("RSRenderServiceConnection::GetPixelmap: RenderNodeType != RSRenderNodeType::CANVAS_DRAWING_NODE");
1555             result.set_value(false);
1556             return;
1557         }
1558 
1559         auto tid = node->GetTid(); // planning: id may change in subthread
1560         auto drawableNode = DrawableV2::RSRenderNodeDrawableAdapter::OnGenerate(node);
1561         if (drawableNode) {
1562             tid = std::static_pointer_cast<DrawableV2::RSCanvasDrawingRenderNodeDrawable>(drawableNode)->GetTid();
1563         }
1564         auto getDrawablePixelmapTask = [drawableNode, pixelmap, rect, &result, tid, drawCmdList]() {
1565             result.set_value(std::static_pointer_cast<DrawableV2::RSCanvasDrawingRenderNodeDrawable>(drawableNode)->
1566                 GetPixelmap(pixelmap, rect, tid, drawCmdList));
1567         };
1568         if (!node->IsOnTheTree()) {
1569             node->ClearOp();
1570         }
1571         if (tid == UNI_MAIN_THREAD_INDEX) {
1572             if (!mainThread->IsIdle() && mainThread->GetContext().HasActiveNode(node)) {
1573                 result.set_value(false);
1574                 return;
1575             }
1576             result.set_value(node->GetPixelmap(pixelmap, rect, tid, drawCmdList));
1577         } else if (tid == UNI_RENDER_THREAD_INDEX) {
1578             renderThread->PostTask(getDrawablePixelmapTask);
1579         } else {
1580             RSTaskDispatcher::GetInstance().PostTask(
1581                 RSSubThreadManager::Instance()->GetReThreadIndexMap()[tid], getDrawablePixelmapTask, false);
1582         }
1583     };
1584     mainThread_->PostTask(getPixelMapTask);
1585     return future.get();
1586 }
1587 
RegisterTypeface(uint64_t globalUniqueId,std::shared_ptr<Drawing::Typeface> & typeface)1588 bool RSRenderServiceConnection::RegisterTypeface(uint64_t globalUniqueId,
1589     std::shared_ptr<Drawing::Typeface>& typeface)
1590 {
1591     RS_LOGD("RSRenderServiceConnection::RegisterTypeface: pid[%{public}d] register typeface[%{public}u]",
1592         RSTypefaceCache::GetTypefacePid(globalUniqueId), RSTypefaceCache::GetTypefaceId(globalUniqueId));
1593     RSTypefaceCache::Instance().CacheDrawingTypeface(globalUniqueId, typeface);
1594     return true;
1595 }
1596 
UnRegisterTypeface(uint64_t globalUniqueId)1597 bool RSRenderServiceConnection::UnRegisterTypeface(uint64_t globalUniqueId)
1598 {
1599     RS_LOGD("RSRenderServiceConnection::UnRegisterTypeface: pid[%{public}d] unregister typeface[%{public}u]",
1600         RSTypefaceCache::GetTypefacePid(globalUniqueId), RSTypefaceCache::GetTypefaceId(globalUniqueId));
1601     RSTypefaceCache::Instance().AddDelayDestroyQueue(globalUniqueId);
1602     return true;
1603 }
1604 
SetScreenSkipFrameInterval(ScreenId id,uint32_t skipFrameInterval)1605 int32_t RSRenderServiceConnection::SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval)
1606 {
1607     if (!screenManager_) {
1608         return StatusCode::SCREEN_NOT_FOUND;
1609     }
1610     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1611     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1612         return RSHardwareThread::Instance().ScheduleTask(
1613             [=]() { return screenManager_->SetScreenSkipFrameInterval(id, skipFrameInterval); }).get();
1614     } else {
1615         return mainThread_->ScheduleTask(
1616             [=]() { return screenManager_->SetScreenSkipFrameInterval(id, skipFrameInterval); }).get();
1617     }
1618 }
1619 
RegisterOcclusionChangeCallback(sptr<RSIOcclusionChangeCallback> callback)1620 int32_t RSRenderServiceConnection::RegisterOcclusionChangeCallback(sptr<RSIOcclusionChangeCallback> callback)
1621 {
1622     if (!mainThread_) {
1623         return StatusCode::INVALID_ARGUMENTS;
1624     }
1625     std::lock_guard<std::mutex> lock(mutex_);
1626     if (!callback) {
1627         RS_LOGD("RSRenderServiceConnection::RegisterOcclusionChangeCallback: callback is nullptr");
1628         return StatusCode::INVALID_ARGUMENTS;
1629     }
1630     mainThread_->RegisterOcclusionChangeCallback(remotePid_, callback);
1631     return StatusCode::SUCCESS;
1632 }
1633 
RegisterSurfaceOcclusionChangeCallback(NodeId id,sptr<RSISurfaceOcclusionChangeCallback> callback,std::vector<float> & partitionPoints)1634 int32_t RSRenderServiceConnection::RegisterSurfaceOcclusionChangeCallback(
1635     NodeId id, sptr<RSISurfaceOcclusionChangeCallback> callback, std::vector<float>& partitionPoints)
1636 {
1637     if (!mainThread_) {
1638         return StatusCode::INVALID_ARGUMENTS;
1639     }
1640     std::lock_guard<std::mutex> lock(mutex_);
1641     if (!callback) {
1642         RS_LOGD("RSRenderServiceConnection::RegisterSurfaceOcclusionChangeCallback: callback is nullptr");
1643         return StatusCode::INVALID_ARGUMENTS;
1644     }
1645     mainThread_->RegisterSurfaceOcclusionChangeCallback(id, remotePid_, callback, partitionPoints);
1646     return StatusCode::SUCCESS;
1647 }
1648 
UnRegisterSurfaceOcclusionChangeCallback(NodeId id)1649 int32_t RSRenderServiceConnection::UnRegisterSurfaceOcclusionChangeCallback(NodeId id)
1650 {
1651     if (!mainThread_) {
1652         return StatusCode::INVALID_ARGUMENTS;
1653     }
1654     std::lock_guard<std::mutex> lock(mutex_);
1655     mainThread_->UnRegisterSurfaceOcclusionChangeCallback(id);
1656     return StatusCode::SUCCESS;
1657 }
1658 
RegisterHgmConfigChangeCallback(sptr<RSIHgmConfigChangeCallback> callback)1659 int32_t RSRenderServiceConnection::RegisterHgmConfigChangeCallback(sptr<RSIHgmConfigChangeCallback> callback)
1660 {
1661     std::lock_guard<std::mutex> lock(mutex_);
1662     if (!callback) {
1663         RS_LOGD("RSRenderServiceConnection::RegisterHgmConfigChangeCallback: callback is nullptr");
1664         return StatusCode::INVALID_ARGUMENTS;
1665     }
1666 
1667     HgmConfigCallbackManager::GetInstance()->RegisterHgmConfigChangeCallback(remotePid_, callback);
1668     return StatusCode::SUCCESS;
1669 }
1670 
RegisterHgmRefreshRateModeChangeCallback(sptr<RSIHgmConfigChangeCallback> callback)1671 int32_t RSRenderServiceConnection::RegisterHgmRefreshRateModeChangeCallback(
1672     sptr<RSIHgmConfigChangeCallback> callback)
1673 {
1674     std::lock_guard<std::mutex> lock(mutex_);
1675     if (!callback) {
1676         RS_LOGD("RSRenderServiceConnection::RegisterHgmRefreshRateModeChangeCallback: callback is nullptr");
1677         return StatusCode::INVALID_ARGUMENTS;
1678     }
1679 
1680     HgmConfigCallbackManager::GetInstance()->RegisterHgmRefreshRateModeChangeCallback(remotePid_, callback);
1681     return StatusCode::SUCCESS;
1682 }
1683 
RegisterHgmRefreshRateUpdateCallback(sptr<RSIHgmConfigChangeCallback> callback)1684 int32_t RSRenderServiceConnection::RegisterHgmRefreshRateUpdateCallback(
1685     sptr<RSIHgmConfigChangeCallback> callback)
1686 {
1687     std::lock_guard<std::mutex> lock(mutex_);
1688 
1689     HgmConfigCallbackManager::GetInstance()->RegisterHgmRefreshRateUpdateCallback(remotePid_, callback);
1690     return StatusCode::SUCCESS;
1691 }
1692 
SetAppWindowNum(uint32_t num)1693 void RSRenderServiceConnection::SetAppWindowNum(uint32_t num)
1694 {
1695     if (!mainThread_) {
1696         return;
1697     }
1698     auto task = [weakThis = wptr<RSRenderServiceConnection>(this), num]() -> void {
1699         sptr<RSRenderServiceConnection> connection = weakThis.promote();
1700         if (!connection || !connection->mainThread_) {
1701             return;
1702         }
1703         connection->mainThread_->SetAppWindowNum(num);
1704     };
1705     mainThread_->PostTask(task);
1706 }
1707 
SetSystemAnimatedScenes(SystemAnimatedScenes systemAnimatedScenes)1708 bool RSRenderServiceConnection::SetSystemAnimatedScenes(SystemAnimatedScenes systemAnimatedScenes)
1709 {
1710     if (!mainThread_) {
1711         return false;
1712     }
1713     RSUifirstManager::Instance().OnProcessAnimateScene(systemAnimatedScenes);
1714     std::lock_guard<std::mutex> lock(mutex_);
1715     return mainThread_->SetSystemAnimatedScenes(systemAnimatedScenes);
1716 }
1717 
ShowWatermark(const std::shared_ptr<Media::PixelMap> & watermarkImg,bool isShow)1718 void RSRenderServiceConnection::ShowWatermark(const std::shared_ptr<Media::PixelMap> &watermarkImg, bool isShow)
1719 {
1720     if (!mainThread_) {
1721         return;
1722     }
1723     auto task = [weakThis = wptr<RSRenderServiceConnection>(this), watermarkImg, isShow]() -> void {
1724         sptr<RSRenderServiceConnection> connection = weakThis.promote();
1725         if (!connection) {
1726             return;
1727         }
1728         connection->mainThread_->ShowWatermark(watermarkImg, isShow);
1729     };
1730     mainThread_->PostTask(task);
1731 }
1732 
ResizeVirtualScreen(ScreenId id,uint32_t width,uint32_t height)1733 int32_t RSRenderServiceConnection::ResizeVirtualScreen(ScreenId id, uint32_t width, uint32_t height)
1734 {
1735     if (!screenManager_) {
1736         return StatusCode::SCREEN_NOT_FOUND;
1737     }
1738     auto renderType = RSUniRenderJudgement::GetUniRenderEnabledType();
1739     if (renderType == UniRenderEnabledType::UNI_RENDER_ENABLED_FOR_ALL) {
1740         return RSHardwareThread::Instance().ScheduleTask(
1741             [weakThis = wptr<RSRenderServiceConnection>(this), id, width, height]() -> int32_t {
1742                 sptr<RSRenderServiceConnection> connection = weakThis.promote();
1743                 if (!connection) {
1744                     return RS_CONNECTION_ERROR;
1745                 }
1746                 return connection->screenManager_->ResizeVirtualScreen(id, width, height);
1747             }
1748         ).get();
1749     } else {
1750         return mainThread_->ScheduleTask(
1751             [weakThis = wptr<RSRenderServiceConnection>(this), id, width, height]() -> int32_t {
1752                 sptr<RSRenderServiceConnection> connection = weakThis.promote();
1753                 if (!connection) {
1754                     return RS_CONNECTION_ERROR;
1755                 }
1756                 return connection->screenManager_->ResizeVirtualScreen(id, width, height);
1757             }
1758         ).get();
1759     }
1760 }
1761 
ReportJankStats()1762 void RSRenderServiceConnection::ReportJankStats()
1763 {
1764     auto task = []() -> void { RSJankStats::GetInstance().ReportJankStats(); };
1765     renderThread_.PostTask(task);
1766 }
1767 
NotifyLightFactorStatus(bool isSafe)1768 void RSRenderServiceConnection::NotifyLightFactorStatus(bool isSafe)
1769 {
1770     if (!mainThread_) {
1771         return;
1772     }
1773     if (mainThread_->GetFrameRateMgr() == nullptr) {
1774         RS_LOGW("RSRenderServiceConnection::NotifyLightFactorStatus: frameRateMgr is nullptr.");
1775         return;
1776     }
1777     mainThread_->GetFrameRateMgr()->HandleLightFactorStatus(remotePid_, isSafe);
1778 }
1779 
NotifyPackageEvent(uint32_t listSize,const std::vector<std::string> & packageList)1780 void RSRenderServiceConnection::NotifyPackageEvent(uint32_t listSize, const std::vector<std::string>& packageList)
1781 {
1782     if (!mainThread_) {
1783         return;
1784     }
1785     if (mainThread_->GetFrameRateMgr() == nullptr) {
1786         RS_LOGW("RSRenderServiceConnection::NotifyPackageEvent: frameRateMgr is nullptr.");
1787         return;
1788     }
1789     mainThread_->GetFrameRateMgr()->HandlePackageEvent(remotePid_, packageList);
1790 }
1791 
NotifyRefreshRateEvent(const EventInfo & eventInfo)1792 void RSRenderServiceConnection::NotifyRefreshRateEvent(const EventInfo& eventInfo)
1793 {
1794     if (!mainThread_) {
1795         return;
1796     }
1797     if (mainThread_->GetFrameRateMgr() == nullptr) {
1798         RS_LOGW("RSRenderServiceConnection::NotifyRefreshRateEvent: frameRateMgr is nullptr.");
1799         return;
1800     }
1801     mainThread_->GetFrameRateMgr()->HandleRefreshRateEvent(remotePid_, eventInfo);
1802 }
1803 
NotifyTouchEvent(int32_t touchStatus,int32_t touchCnt)1804 void RSRenderServiceConnection::NotifyTouchEvent(int32_t touchStatus, int32_t touchCnt)
1805 {
1806     if (!mainThread_) {
1807         return;
1808     }
1809     if (mainThread_->GetFrameRateMgr() == nullptr) {
1810         RS_LOGW("RSRenderServiceConnection::NotifyTouchEvent: frameRateMgr is nullptr.");
1811         return;
1812     }
1813     mainThread_->GetFrameRateMgr()->HandleTouchEvent(remotePid_, touchStatus, touchCnt);
1814 }
1815 
NotifyDynamicModeEvent(bool enableDynamicModeEvent)1816 void RSRenderServiceConnection::NotifyDynamicModeEvent(bool enableDynamicModeEvent)
1817 {
1818     HgmTaskHandleThread::Instance().PostTask([enableDynamicModeEvent] () {
1819         auto frameRateMgr = HgmCore::Instance().GetFrameRateMgr();
1820         if (frameRateMgr == nullptr) {
1821             RS_LOGW("RSRenderServiceConnection::NotifyDynamicModeEvent: frameRateMgr is nullptr.");
1822             return;
1823         }
1824         frameRateMgr->HandleDynamicModeEvent(enableDynamicModeEvent);
1825     });
1826 }
1827 
ReportEventResponse(DataBaseRs info)1828 void RSRenderServiceConnection::ReportEventResponse(DataBaseRs info)
1829 {
1830     auto task = [info]() -> void {
1831         RSJankStats::GetInstance().SetReportEventResponse(info);
1832     };
1833     renderThread_.PostTask(task);
1834     RSUifirstManager::Instance().OnProcessEventResponse(info);
1835 }
1836 
ReportEventComplete(DataBaseRs info)1837 void RSRenderServiceConnection::ReportEventComplete(DataBaseRs info)
1838 {
1839     auto task = [info]() -> void {
1840         RSJankStats::GetInstance().SetReportEventComplete(info);
1841     };
1842     renderThread_.PostTask(task);
1843     RSUifirstManager::Instance().OnProcessEventComplete(info);
1844 }
1845 
ReportEventJankFrame(DataBaseRs info)1846 void RSRenderServiceConnection::ReportEventJankFrame(DataBaseRs info)
1847 {
1848     bool isReportTaskDelayed = renderThread_.IsMainLooping();
1849     auto task = [info, isReportTaskDelayed]() -> void {
1850         RSJankStats::GetInstance().SetReportEventJankFrame(info, isReportTaskDelayed);
1851     };
1852     renderThread_.PostTask(task);
1853 }
1854 
ReportGameStateData(GameStateData info)1855 void RSRenderServiceConnection::ReportGameStateData(GameStateData info)
1856 {
1857     RS_LOGD("RSRenderServiceConnection::ReportGameStateData = %{public}s, uid = %{public}d, state = %{public}d, "
1858             "pid = %{public}d renderTid = %{public}d ",
1859         info.bundleName.c_str(), info.uid, info.state, info.pid, info.renderTid);
1860 
1861     FrameReport::GetInstance().SetGameScene(info.pid, info.state);
1862 }
1863 
SetHardwareEnabled(NodeId id,bool isEnabled,SelfDrawingNodeType selfDrawingType,bool dynamicHardwareEnable)1864 void RSRenderServiceConnection::SetHardwareEnabled(NodeId id, bool isEnabled, SelfDrawingNodeType selfDrawingType,
1865     bool dynamicHardwareEnable)
1866 {
1867     if (!mainThread_) {
1868         return;
1869     }
1870     auto task = [weakThis = wptr<RSRenderServiceConnection>(this), id, isEnabled, selfDrawingType,
1871         dynamicHardwareEnable]() -> void {
1872         sptr<RSRenderServiceConnection> connection = weakThis.promote();
1873         if (!connection) {
1874             return;
1875         }
1876         auto& context = connection->mainThread_->GetContext();
1877         auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id);
1878         if (node) {
1879             node->SetHardwareEnabled(isEnabled, selfDrawingType, dynamicHardwareEnable);
1880         }
1881     };
1882     mainThread_->PostTask(task);
1883 }
1884 
SetHidePrivacyContent(NodeId id,bool needHidePrivacyContent)1885 uint32_t RSRenderServiceConnection::SetHidePrivacyContent(NodeId id, bool needHidePrivacyContent)
1886 {
1887     if (!mainThread_) {
1888         return static_cast<int32_t>(RSInterfaceErrorCode::UNKNOWN_ERROR);
1889     }
1890     auto task = [weakThis = wptr<RSRenderServiceConnection>(this), id, needHidePrivacyContent]() -> void {
1891         sptr<RSRenderServiceConnection> connection = weakThis.promote();
1892         if (!connection) {
1893             return;
1894         }
1895         auto& context = connection->mainThread_->GetContext();
1896         auto node = context.GetNodeMap().GetRenderNode<RSSurfaceRenderNode>(id);
1897         if (node) {
1898             node->SetHidePrivacyContent(needHidePrivacyContent);
1899         }
1900     };
1901     mainThread_->PostTask(task);
1902     return static_cast<uint32_t>(RSInterfaceErrorCode::NO_ERROR);
1903 }
1904 
SetCacheEnabledForRotation(bool isEnabled)1905 void RSRenderServiceConnection::SetCacheEnabledForRotation(bool isEnabled)
1906 {
1907     if (!mainThread_) {
1908         return;
1909     }
1910     auto task = [isEnabled]() {
1911         RSSystemProperties::SetCacheEnabledForRotation(isEnabled);
1912     };
1913     mainThread_->PostTask(task);
1914 }
1915 
SetDefaultDeviceRotationOffset(uint32_t offset)1916 void RSRenderServiceConnection::SetDefaultDeviceRotationOffset(uint32_t offset)
1917 {
1918     RSSystemProperties::SetDefaultDeviceRotationOffset(offset);
1919 }
1920 
GetActiveDirtyRegionInfo()1921 std::vector<ActiveDirtyRegionInfo> RSRenderServiceConnection::GetActiveDirtyRegionInfo()
1922 {
1923     const auto& activeDirtyRegionInfos = GpuDirtyRegionCollection::GetInstance().GetActiveDirtyRegionInfo();
1924     GpuDirtyRegionCollection::GetInstance().ResetActiveDirtyRegionInfo();
1925     return activeDirtyRegionInfos;
1926 }
1927 
GetGlobalDirtyRegionInfo()1928 GlobalDirtyRegionInfo RSRenderServiceConnection::GetGlobalDirtyRegionInfo()
1929 {
1930     const auto& globalDirtyRegionInfo = GpuDirtyRegionCollection::GetInstance().GetGlobalDirtyRegionInfo();
1931     GpuDirtyRegionCollection::GetInstance().ResetGlobalDirtyRegionInfo();
1932     return globalDirtyRegionInfo;
1933 }
1934 
GetLayerComposeInfo()1935 LayerComposeInfo RSRenderServiceConnection::GetLayerComposeInfo()
1936 {
1937     const auto& layerComposeInfo = LayerComposeCollection::GetInstance().GetLayerComposeInfo();
1938     LayerComposeCollection::GetInstance().ResetLayerComposeInfo();
1939     return layerComposeInfo;
1940 }
1941 
GetHwcDisabledReasonInfo()1942 HwcDisabledReasonInfos RSRenderServiceConnection::GetHwcDisabledReasonInfo()
1943 {
1944     return HwcDisabledReasonCollection::GetInstance().GetHwcDisabledReasonInfo();
1945 }
1946 
SetVmaCacheStatus(bool flag)1947 void RSRenderServiceConnection::SetVmaCacheStatus(bool flag)
1948 {
1949     renderThread_.SetVmaCacheStatus(flag);
1950 }
1951 
1952 #ifdef TP_FEATURE_ENABLE
SetTpFeatureConfig(int32_t feature,const char * config)1953 void RSRenderServiceConnection::SetTpFeatureConfig(int32_t feature, const char* config)
1954 {
1955     if (TOUCH_SCREEN->tsSetFeatureConfig_ == nullptr) {
1956         RS_LOGW("RSRenderServiceConnection::SetTpFeatureConfig: touch screen function symbol is nullptr.");
1957         return;
1958     }
1959     if (TOUCH_SCREEN->tsSetFeatureConfig_(feature, config) < 0) {
1960         RS_LOGW("RSRenderServiceConnection::SetTpFeatureConfig: tsSetFeatureConfig_ failed.");
1961         return;
1962     }
1963 }
1964 #endif
1965 
SetVirtualScreenUsingStatus(bool isVirtualScreenUsingStatus)1966 void RSRenderServiceConnection::SetVirtualScreenUsingStatus(bool isVirtualScreenUsingStatus)
1967 {
1968     if (isVirtualScreenUsingStatus) {
1969         EventInfo event = { "VOTER_VIRTUALDISPLAY", ADD_VOTE, OLED_60_HZ, OLED_60_HZ };
1970         NotifyRefreshRateEvent(event);
1971     } else {
1972         EventInfo event = { "VOTER_VIRTUALDISPLAY", REMOVE_VOTE };
1973         NotifyRefreshRateEvent(event);
1974     }
1975     return;
1976 }
1977 
SetCurtainScreenUsingStatus(bool isCurtainScreenOn)1978 void RSRenderServiceConnection::SetCurtainScreenUsingStatus(bool isCurtainScreenOn)
1979 {
1980     if (!mainThread_) {
1981         return;
1982     }
1983     auto task = [weakThis = wptr<RSRenderServiceConnection>(this), isCurtainScreenOn]() -> void {
1984         sptr<RSRenderServiceConnection> connection = weakThis.promote();
1985         if (!connection) {
1986             return;
1987         }
1988         connection->mainThread_->SetCurtainScreenUsingStatus(isCurtainScreenOn);
1989     };
1990     mainThread_->PostTask(task);
1991 }
1992 
RegisterUIExtensionCallback(uint64_t userId,sptr<RSIUIExtensionCallback> callback)1993 int32_t RSRenderServiceConnection::RegisterUIExtensionCallback(uint64_t userId, sptr<RSIUIExtensionCallback> callback)
1994 {
1995     if (!mainThread_) {
1996         return StatusCode::INVALID_ARGUMENTS;
1997     }
1998     std::lock_guard<std::mutex> lock(mutex_);
1999     if (!callback) {
2000         RS_LOGE("RSRenderServiceConnection::RegisterUIExtensionCallback register null callback, failed.");
2001         return StatusCode::INVALID_ARGUMENTS;
2002     }
2003     mainThread_->RegisterUIExtensionCallback(remotePid_, userId, callback);
2004     return StatusCode::SUCCESS;
2005 }
2006 
SetAncoForceDoDirect(bool direct)2007 bool RSRenderServiceConnection::SetAncoForceDoDirect(bool direct)
2008 {
2009     std::lock_guard<std::mutex> lock(mutex_);
2010     mainThread_->SetAncoForceDoDirect(direct);
2011     return true;
2012 }
2013 
RegisterSurfaceBufferCallback(pid_t pid,uint64_t uid,sptr<RSISurfaceBufferCallback> callback)2014 void RSRenderServiceConnection::RegisterSurfaceBufferCallback(pid_t pid, uint64_t uid,
2015     sptr<RSISurfaceBufferCallback> callback)
2016 {
2017     RSSurfaceBufferCallbackManager::Instance().RegisterSurfaceBufferCallback(pid, uid, callback);
2018 }
2019 
UnregisterSurfaceBufferCallback(pid_t pid,uint64_t uid)2020 void RSRenderServiceConnection::UnregisterSurfaceBufferCallback(pid_t pid, uint64_t uid)
2021 {
2022     RSSurfaceBufferCallbackManager::Instance().UnregisterSurfaceBufferCallback(pid, uid);
2023 }
2024 
SetLayerTop(const std::string & nodeIdStr,bool isTop)2025 void RSRenderServiceConnection::SetLayerTop(const std::string &nodeIdStr, bool isTop)
2026 {
2027     if (mainThread_ == nullptr) {
2028         return;
2029     }
2030     auto task = [weakThis = wptr<RSRenderServiceConnection>(this), nodeIdStr, isTop]() -> void {
2031         sptr<RSRenderServiceConnection> connection = weakThis.promote();
2032         if (!connection) {
2033             return;
2034         }
2035         auto& context = connection->mainThread_->GetContext();
2036         context.GetNodeMap().TraverseSurfaceNodes(
2037             [&nodeIdStr, &isTop](const std::shared_ptr<RSSurfaceRenderNode>& surfaceNode) mutable {
2038             if ((surfaceNode != nullptr) && (surfaceNode->GetName() == nodeIdStr) &&
2039                 (surfaceNode->GetSurfaceNodeType() == RSSurfaceNodeType::SELF_DRAWING_NODE)) {
2040                 surfaceNode->SetLayerTop(isTop);
2041                 return;
2042             }
2043         });
2044         // It can be displayed immediately after layer-top changed.
2045         connection->mainThread_->SetDirtyFlag();
2046         connection->mainThread_->RequestNextVSync();
2047     };
2048     mainThread_->PostTask(task);
2049 }
2050 } // namespace Rosen
2051 } // namespace OHOS
2052