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 #ifndef RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_CONNECTION_H
17 #define RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_CONNECTION_H
18 
19 #include <mutex>
20 #include <unordered_set>
21 
22 #include "hgm_config_callback_manager.h"
23 #include "ipc_callbacks/buffer_available_callback.h"
24 #include "ipc_callbacks/buffer_clear_callback.h"
25 #include "pipeline/rs_render_service.h"
26 #include "pipeline/rs_hardware_thread.h"
27 #include "pipeline/rs_uni_render_thread.h"
28 #include "screen_manager/rs_screen_manager.h"
29 #include "transaction/rs_render_service_connection_stub.h"
30 #include "vsync_distributor.h"
31 
32 namespace OHOS {
33 namespace Rosen {
34 class HgmFrameRateManager;
35 class RSRenderServiceConnection : public RSRenderServiceConnectionStub {
36 public:
37     RSRenderServiceConnection(
38         pid_t remotePid,
39         wptr<RSRenderService> renderService,
40         RSMainThread* mainThread,
41         sptr<RSScreenManager> screenManager,
42         sptr<IRemoteObject> token,
43         sptr<VSyncDistributor> distributor);
44     ~RSRenderServiceConnection() noexcept;
45     RSRenderServiceConnection(const RSRenderServiceConnection&) = delete;
46     RSRenderServiceConnection& operator=(const RSRenderServiceConnection&) = delete;
47 
GetToken()48     sptr<IRemoteObject> GetToken() const
49     {
50         return token_;
51     }
52 
53 private:
54     void CleanVirtualScreens() noexcept;
55     void CleanRenderNodes() noexcept;
56     void CleanFrameRateLinkers() noexcept;
57     void CleanFrameRateLinkerExpectedFpsCallbacks() noexcept;
58     void CleanAll(bool toDelete = false) noexcept;
59 
60     // IPC RSIRenderServiceConnection Interfaces
61     void CommitTransaction(std::unique_ptr<RSTransactionData>& transactionData) override;
62     void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task) override;
63     MemoryGraphic GetMemoryGraphic(int pid) override;
64     std::vector<MemoryGraphic> GetMemoryGraphics() override;
65     bool GetTotalAppMemSize(float& cpuMemSize, float& gpuMemSize) override;
66     bool GetUniRenderEnabled() override;
67 
68     bool CreateNode(const RSSurfaceRenderNodeConfig& config) override;
69     bool CreateNode(const RSDisplayNodeConfig& displayNodeConfig, NodeId nodeId) override;
70     sptr<Surface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config) override;
71 
72     sptr<IVSyncConnection> CreateVSyncConnection(const std::string& name,
73                                                  const sptr<VSyncIConnectionToken>& token,
74                                                  uint64_t id,
75                                                  NodeId windowNodeId = 0,
76                                                  bool fromXcomponent = false) override;
77 
78     std::shared_ptr<Media::PixelMap> CreatePixelMapFromSurface(sptr<Surface> surface, const Rect &srcRect) override;
79 
80     int32_t SetFocusAppInfo(
81         int32_t pid, int32_t uid, const std::string &bundleName, const std::string &abilityName,
82         uint64_t focusNodeId) override;
83 
84     ScreenId GetDefaultScreenId() override;
85 
86     ScreenId GetActiveScreenId() override;
87 
88     std::vector<ScreenId> GetAllScreenIds() override;
89 
90     ScreenId CreateVirtualScreen(
91         const std::string &name,
92         uint32_t width,
93         uint32_t height,
94         sptr<Surface> surface,
95         ScreenId mirrorId = 0,
96         int32_t flags = 0,
97         std::vector<NodeId> whiteList = {}) override;
98 
99     int32_t SetVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector) override;
100 
101     int32_t AddVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector) override;
102 
103     int32_t RemoveVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector) override;
104 
105     int32_t SetVirtualScreenSecurityExemptionList(
106         ScreenId id, const std::vector<NodeId>& securityExemptionList) override;
107 
108     int32_t SetCastScreenEnableSkipWindow(ScreenId id, bool enable) override;
109 
110     int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface) override;
111 
112     void RemoveVirtualScreen(ScreenId id) override;
113 
114     int32_t SetScreenChangeCallback(sptr<RSIScreenChangeCallback> callback) override;
115 
116     void SetScreenActiveMode(ScreenId id, uint32_t modeId) override;
117 
118     void SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate) override;
119 
120     void SetRefreshRateMode(int32_t refreshRateMode) override;
121 
122     void SyncFrameRateRange(FrameRateLinkerId id, const FrameRateRange& range,
123         int32_t animatorExpectedFrameRate) override;
124 
125     void UnregisterFrameRateLinker(FrameRateLinkerId id) override;
126 
127     uint32_t GetScreenCurrentRefreshRate(ScreenId id) override;
128 
129     int32_t GetCurrentRefreshRateMode() override;
130 
131     std::vector<int32_t> GetScreenSupportedRefreshRates(ScreenId id) override;
132 
133     bool GetShowRefreshRateEnabled() override;
134 
135     void SetShowRefreshRateEnabled(bool enable) override;
136 
137     std::string GetRefreshInfo(pid_t pid) override;
138 
139     int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height) override;
140 
141     void MarkPowerOffNeedProcessOneFrame() override;
142 
143     void DisablePowerOffRenderControl(ScreenId id) override;
144 
145     void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status) override;
146 
147     void TakeSurfaceCapture(NodeId id, sptr<RSISurfaceCaptureCallback> callback,
148         const RSSurfaceCaptureConfig& captureConfig,
149         RSSurfaceCapturePermissions permissions = RSSurfaceCapturePermissions()) override;
150 
151     void SetWindowFreezeImmediately(NodeId id, bool isFreeze, sptr<RSISurfaceCaptureCallback> callback,
152         const RSSurfaceCaptureConfig& captureConfig) override;
153 
154     void RegisterApplicationAgent(uint32_t pid, sptr<IApplicationAgent> app) override;
155 
156     void UnRegisterApplicationAgent(sptr<IApplicationAgent> app);
157 
158     RSVirtualScreenResolution GetVirtualScreenResolution(ScreenId id) override;
159 
160     RSScreenModeInfo GetScreenActiveMode(ScreenId id) override;
161 
162     std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id) override;
163 
164     RSScreenCapability GetScreenCapability(ScreenId id) override;
165 
166     ScreenPowerStatus GetScreenPowerStatus(ScreenId id) override;
167 
168     RSScreenData GetScreenData(ScreenId id) override;
169 
170     int32_t GetScreenBacklight(ScreenId id) override;
171 
172     void SetScreenBacklight(ScreenId id, uint32_t level) override;
173 
174     void RegisterBufferAvailableListener(
175         NodeId id, sptr<RSIBufferAvailableCallback> callback, bool isFromRenderThread) override;
176 
177     void RegisterBufferClearListener(
178         NodeId id, sptr<RSIBufferClearCallback> callback) override;
179 
180     int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode) override;
181 
182     int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys) override;
183 
184     int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode) override;
185 
186     int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx) override;
187 
188     int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode) override;
189 
190     int32_t SetScreenCorrection(ScreenId id, ScreenRotation screenRotation) override;
191 
192     bool SetVirtualMirrorScreenCanvasRotation(ScreenId id, bool canvasRotation) override;
193 
194     bool SetVirtualMirrorScreenScaleMode(ScreenId id, ScreenScaleMode scaleMode) override;
195 
196     bool SetGlobalDarkColorMode(bool isDark) override;
197 
198     int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode) override;
199 
200     int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability) override;
201 
202     int32_t GetPixelFormat(ScreenId id, GraphicPixelFormat& pixelFormat) override;
203 
204     int32_t SetPixelFormat(ScreenId id, GraphicPixelFormat pixelFormat) override;
205 
206     int32_t GetScreenSupportedHDRFormats(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats) override;
207 
208     int32_t GetScreenHDRFormat(ScreenId id, ScreenHDRFormat& hdrFormat) override;
209 
210     int32_t SetScreenHDRFormat(ScreenId id, int32_t modeIdx) override;
211 
212     int32_t GetScreenSupportedColorSpaces(ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces) override;
213 
214     int32_t GetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType& colorSpace) override;
215 
216     int32_t SetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType colorSpace) override;
217 
218     int32_t GetScreenType(ScreenId id, RSScreenType& screenType) override;
219 
220     bool GetBitmap(NodeId id, Drawing::Bitmap& bitmap) override;
221     bool GetPixelmap(NodeId id, std::shared_ptr<Media::PixelMap> pixelmap,
222         const Drawing::Rect* rect, std::shared_ptr<Drawing::DrawCmdList> drawCmdList) override;
223     bool RegisterTypeface(uint64_t globalUniqueId, std::shared_ptr<Drawing::Typeface>& typeface) override;
224     bool UnRegisterTypeface(uint64_t globalUniqueId) override;
225 
226     int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval) override;
227 
228     int32_t SetVirtualScreenRefreshRate(ScreenId id, uint32_t maxRefreshRate, uint32_t& actualRefreshRate) override;
229 
230     int32_t RegisterOcclusionChangeCallback(sptr<RSIOcclusionChangeCallback> callback) override;
231 
232     int32_t RegisterSurfaceOcclusionChangeCallback(
233         NodeId id, sptr<RSISurfaceOcclusionChangeCallback> callback, std::vector<float>& partitionPoints) override;
234 
235     int32_t UnRegisterSurfaceOcclusionChangeCallback(NodeId id) override;
236 
237     int32_t RegisterHgmConfigChangeCallback(sptr<RSIHgmConfigChangeCallback> callback) override;
238 
239     int32_t RegisterHgmRefreshRateModeChangeCallback(sptr<RSIHgmConfigChangeCallback> callback) override;
240 
241     int32_t RegisterHgmRefreshRateUpdateCallback(sptr<RSIHgmConfigChangeCallback> callback) override;
242 
243     int32_t RegisterFrameRateLinkerExpectedFpsUpdateCallback(int32_t dstPid,
244         sptr<RSIFrameRateLinkerExpectedFpsUpdateCallback> callback) override;
245 
246     void SetAppWindowNum(uint32_t num) override;
247 
248     bool SetSystemAnimatedScenes(SystemAnimatedScenes systemAnimatedScenes) override;
249 
250     void ShowWatermark(const std::shared_ptr<Media::PixelMap> &watermarkImg, bool isShow) override;
251 
252     int32_t ResizeVirtualScreen(ScreenId id, uint32_t width, uint32_t height) override;
253 
254     void ReportJankStats() override;
255 
256     void ReportEventResponse(DataBaseRs info) override;
257 
258     void ReportEventComplete(DataBaseRs info) override;
259 
260     void ReportEventJankFrame(DataBaseRs info) override;
261 
262     void ReportGameStateData(GameStateData info) override;
263 
264     void SetHardwareEnabled(NodeId id, bool isEnabled, SelfDrawingNodeType selfDrawingType,
265         bool dynamicHardwareEnable) override;
266 
267     uint32_t SetHidePrivacyContent(NodeId id, bool needHidePrivacyContent) override;
268 
269     void NotifyLightFactorStatus(bool isSafe) override;
270 
271     void NotifyPackageEvent(uint32_t listSize, const std::vector<std::string>& packageList) override;
272 
273     void NotifyRefreshRateEvent(const EventInfo& eventInfo) override;
274 
275     void NotifyTouchEvent(int32_t touchStatus, int32_t touchCnt) override;
276 
277     void NotifyDynamicModeEvent(bool enableDynamicModeEvent) override;
278 
279     void SetCacheEnabledForRotation(bool isEnabled) override;
280 
281     std::vector<ActiveDirtyRegionInfo> GetActiveDirtyRegionInfo() override;
282 
283     GlobalDirtyRegionInfo GetGlobalDirtyRegionInfo() override;
284 
285     LayerComposeInfo GetLayerComposeInfo() override;
286 
287     HwcDisabledReasonInfos GetHwcDisabledReasonInfo() override;
288 
289     void SetVmaCacheStatus(bool flag) override;
290 
291     int32_t RegisterUIExtensionCallback(uint64_t userId, sptr<RSIUIExtensionCallback> callback) override;
292 
293 #ifdef TP_FEATURE_ENABLE
294     void SetTpFeatureConfig(int32_t feature, const char* config, TpFeatureConfigType tpFeatureConfigType) override;
295 #endif
296 
297     void SetVirtualScreenUsingStatus(bool isVirtualScreenUsingStatus) override;
298     void SetCurtainScreenUsingStatus(bool isCurtainScreenOn) override;
299 
300     void DropFrameByPid(const std::vector<int32_t> pidList) override;
301 
302     bool SetAncoForceDoDirect(bool direct) override;
303 
304     void RegisterSurfaceBufferCallback(pid_t pid, uint64_t uid,
305         sptr<RSISurfaceBufferCallback> callback) override;
306     void UnregisterSurfaceBufferCallback(pid_t pid, uint64_t uid) override;
307     void SetLayerTop(const std::string &nodeIdStr, bool isTop) override;
308 
309     pid_t remotePid_;
310     wptr<RSRenderService> renderService_;
311     RSMainThread* mainThread_ = nullptr;
312     RSUniRenderThread& renderThread_;
313     sptr<RSScreenManager> screenManager_;
314     sptr<IRemoteObject> token_;
315 
316     class RSConnectionDeathRecipient : public IRemoteObject::DeathRecipient {
317     public:
318         explicit RSConnectionDeathRecipient(wptr<RSRenderServiceConnection> conn);
319         virtual ~RSConnectionDeathRecipient() = default;
320 
321         void OnRemoteDied(const wptr<IRemoteObject>& token) override;
322 
323     private:
324         wptr<RSRenderServiceConnection> conn_;
325     };
326     friend class RSConnectionDeathRecipient;
327     sptr<RSConnectionDeathRecipient> connDeathRecipient_;
328 
329     class RSApplicationRenderThreadDeathRecipient : public IRemoteObject::DeathRecipient {
330     public:
331         explicit RSApplicationRenderThreadDeathRecipient(wptr<RSRenderServiceConnection> conn);
332         virtual ~RSApplicationRenderThreadDeathRecipient() = default;
333 
334         void OnRemoteDied(const wptr<IRemoteObject>& token) override;
335 
336     private:
337         wptr<RSRenderServiceConnection> conn_;
338     };
339     friend class RSApplicationRenderThreadDeathRecipient;
340     sptr<RSApplicationRenderThreadDeathRecipient> ApplicationDeathRecipient_;
341 
342     mutable std::mutex mutex_;
343     bool cleanDone_ = false;
344 
345     // save all virtual screenIds created by this connection.
346     std::unordered_set<ScreenId> virtualScreenIds_;
347     sptr<RSIScreenChangeCallback> screenChangeCallback_;
348     sptr<VSyncDistributor> appVSyncDistributor_;
349 
350 #ifdef RS_PROFILER_ENABLED
351     friend class RSProfiler;
352 #endif
353 };
354 } // namespace Rosen
355 } // namespace OHOS
356 
357 #endif // RENDER_SERVICE_PIPELINE_RS_RENDER_SERVICE_CONNECTION_H
358