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 ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H
17 #define ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <mutex>
23 #include <shared_mutex>
24 #include <refbase.h>
25 #include <surface_type.h>
26 #ifndef ROSEN_CROSS_PLATFORM
27 #include <surface.h>
28 #endif
29 
30 #include "ipc_callbacks/buffer_available_callback.h"
31 #include "ipc_callbacks/iapplication_agent.h"
32 #include "ipc_callbacks/rs_surface_buffer_callback.h"
33 #include "ipc_callbacks/screen_change_callback.h"
34 #include "ipc_callbacks/surface_capture_callback.h"
35 #include "memory/rs_memory_graphic.h"
36 #ifdef NEW_RENDER_CONTEXT
37 #include "render_backend/rs_render_surface.h"
38 #else
39 #include "platform/drawing/rs_surface.h"
40 #endif
41 #include "rs_irender_client.h"
42 #include "variable_frame_rate/rs_variable_frame_rate.h"
43 #include "screen_manager/rs_screen_capability.h"
44 #include "screen_manager/rs_screen_data.h"
45 #include "screen_manager/rs_screen_hdr_capability.h"
46 #include "screen_manager/rs_screen_mode_info.h"
47 #include "screen_manager/screen_types.h"
48 #include "screen_manager/rs_virtual_screen_resolution.h"
49 #include "vsync_receiver.h"
50 #include "ipc_callbacks/rs_iocclusion_change_callback.h"
51 #include "rs_hgm_config_data.h"
52 #include "rs_occlusion_data.h"
53 #include "rs_uiextension_data.h"
54 #include "info_collection/rs_gpu_dirty_region_collection.h"
55 #include "info_collection/rs_hardware_compose_disabled_reason_collection.h"
56 #include "info_collection/rs_layer_compose_collection.h"
57 
58 namespace OHOS {
59 namespace Rosen {
60 // normal callback functor for client users.
61 using ScreenChangeCallback = std::function<void(ScreenId, ScreenEvent)>;
62 using BufferAvailableCallback = std::function<void()>;
63 using BufferClearCallback = std::function<void()>;
64 using OcclusionChangeCallback = std::function<void(std::shared_ptr<RSOcclusionData>)>;
65 using SurfaceOcclusionChangeCallback = std::function<void(float)>;
66 using HgmConfigChangeCallback = std::function<void(std::shared_ptr<RSHgmConfigData>)>;
67 using OnRemoteDiedCallback = std::function<void()>;
68 using HgmRefreshRateModeChangeCallback = std::function<void(int32_t)>;
69 using HgmRefreshRateUpdateCallback = std::function<void(int32_t)>;
70 using FrameRateLinkerExpectedFpsUpdateCallback = std::function<void(int32_t, int32_t)>;
71 using UIExtensionCallback = std::function<void(std::shared_ptr<RSUIExtensionData>, uint64_t)>;
72 struct DataBaseRs {
73     int32_t appPid = -1;
74     int32_t eventType = -1;
75     int32_t versionCode = -1;
76     int64_t uniqueId = 0;
77     int64_t inputTime = 0;
78     int64_t beginVsyncTime = 0;
79     int64_t endVsyncTime = 0;
80     bool isDisplayAnimator = false;
81     std::string sceneId;
82     std::string versionName;
83     std::string bundleName;
84     std::string processName;
85     std::string abilityName;
86     std::string pageUrl;
87     std::string sourceType;
88     std::string note;
89 };
90 
91 struct GameStateData {
92     int32_t pid = -1;
93     int32_t uid = 0;
94     int32_t state = 0;
95     int32_t renderTid = -1;
96     std::string bundleName;
97 };
98 
99 class SurfaceCaptureCallback {
100 public:
SurfaceCaptureCallback()101     SurfaceCaptureCallback() {}
~SurfaceCaptureCallback()102     virtual ~SurfaceCaptureCallback() {}
103     virtual void OnSurfaceCapture(std::shared_ptr<Media::PixelMap> pixelmap) = 0;
104 };
105 
106 class SurfaceBufferCallback {
107 public:
108     SurfaceBufferCallback() = default;
109     virtual ~SurfaceBufferCallback() noexcept = default;
110     virtual void OnFinish(const FinishCallbackRet& ret) = 0;
111     virtual void OnAfterAcquireBuffer(const AfterAcquireBufferRet& ret) = 0;
112 };
113 
114 class RSB_EXPORT RSRenderServiceClient : public RSIRenderClient {
115 public:
116     RSRenderServiceClient() = default;
117     virtual ~RSRenderServiceClient() = default;
118 
119     RSRenderServiceClient(const RSRenderServiceClient&) = delete;
120     void operator=(const RSRenderServiceClient&) = delete;
121 
122     void CommitTransaction(std::unique_ptr<RSTransactionData>& transactionData) override;
123     void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task) override;
124 
125     bool GetUniRenderEnabled();
126 
127     bool CreateNode(const RSSurfaceRenderNodeConfig& config);
128     bool CreateNode(const RSDisplayNodeConfig& displayNodeConfig, NodeId nodeId);
129 #ifdef NEW_RENDER_CONTEXT
130     std::shared_ptr<RSRenderSurface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config);
131 #else
132     std::shared_ptr<RSSurface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config);
133 #endif
134     std::shared_ptr<VSyncReceiver> CreateVSyncReceiver(
135         const std::string& name,
136         const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &looper = nullptr,
137         uint64_t id = 0,
138         NodeId windowNodeId = 0,
139         bool fromXcomponent = false);
140 
141     std::shared_ptr<Media::PixelMap> CreatePixelMapFromSurfaceId(uint64_t surfaceid, const Rect &srcRect);
142 
143     bool TakeSurfaceCapture(
144         NodeId id, std::shared_ptr<SurfaceCaptureCallback> callback, const RSSurfaceCaptureConfig& captureConfig);
145 
146     bool SetWindowFreezeImmediately(NodeId id, bool isFreeze, std::shared_ptr<SurfaceCaptureCallback> callback,
147         const RSSurfaceCaptureConfig& captureConfig);
148 
149     int32_t SetFocusAppInfo(int32_t pid, int32_t uid, const std::string &bundleName, const std::string &abilityName,
150         uint64_t focusNodeId);
151 
152     ScreenId GetDefaultScreenId();
153     ScreenId GetActiveScreenId();
154 
155     std::vector<ScreenId> GetAllScreenIds();
156 
157 #ifndef ROSEN_CROSS_PLATFORM
158 #if defined(NEW_RENDER_CONTEXT)
159     std::shared_ptr<RSRenderSurface> CreateRSSurface(const sptr<Surface> &surface);
160 #else
161     std::shared_ptr<RSSurface> CreateRSSurface(const sptr<Surface> &surface);
162 #endif
163     ScreenId CreateVirtualScreen(const std::string& name, uint32_t width, uint32_t height, sptr<Surface> surface,
164         ScreenId mirrorId, int32_t flags, std::vector<NodeId> whiteList = {});
165 
166     int32_t SetVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector);
167 
168     int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface);
169 
170     int32_t AddVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector);
171 
172     int32_t RemoveVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector);
173 #endif
174 
175     int32_t SetVirtualScreenSecurityExemptionList(ScreenId id, const std::vector<NodeId>& securityExemptionList);
176 
177     int32_t SetCastScreenEnableSkipWindow(ScreenId id, bool enable);
178 
179     void RemoveVirtualScreen(ScreenId id);
180 
181     int32_t SetScreenChangeCallback(const ScreenChangeCallback& callback);
182 
183 #ifndef ROSEN_ARKUI_X
184     void SetScreenActiveMode(ScreenId id, uint32_t modeId);
185 #endif // !ROSEN_ARKUI_X
186     void SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate);
187 
188     void SetRefreshRateMode(int32_t refreshRateMode);
189 
190     void SyncFrameRateRange(FrameRateLinkerId id, const FrameRateRange& range,
191         int32_t animatorExpectedFrameRate);
192 
193     void UnregisterFrameRateLinker(FrameRateLinkerId id);
194 
195     uint32_t GetScreenCurrentRefreshRate(ScreenId id);
196 
197     int32_t GetCurrentRefreshRateMode();
198 
199     std::vector<int32_t> GetScreenSupportedRefreshRates(ScreenId id);
200 
201     bool GetShowRefreshRateEnabled();
202 
203     void SetShowRefreshRateEnabled(bool enable);
204 
205     std::string GetRefreshInfo(pid_t pid);
206 
207 #ifndef ROSEN_ARKUI_X
208     int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height);
209 
210     RSVirtualScreenResolution GetVirtualScreenResolution(ScreenId id);
211 
212     void MarkPowerOffNeedProcessOneFrame();
213 
214     void DisablePowerOffRenderControl(ScreenId id);
215 
216     void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status);
217 
218     RSScreenModeInfo GetScreenActiveMode(ScreenId id);
219 
220     std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id);
221 
222     RSScreenCapability GetScreenCapability(ScreenId id);
223 
224     ScreenPowerStatus GetScreenPowerStatus(ScreenId id);
225 
226     RSScreenData GetScreenData(ScreenId id);
227 
228     MemoryGraphic GetMemoryGraphic(int pid);
229 
230     std::vector<MemoryGraphic> GetMemoryGraphics();
231 #endif // !ROSEN_ARKUI_X
232     bool GetTotalAppMemSize(float& cpuMemSize, float& gpuMemSize);
233 
234     int32_t GetScreenBacklight(ScreenId id);
235 
236     void SetScreenBacklight(ScreenId id, uint32_t level);
237 
238     bool RegisterBufferAvailableListener(
239         NodeId id, const BufferAvailableCallback &callback, bool isFromRenderThread = false);
240 
241     bool RegisterBufferClearListener(
242         NodeId id, const BufferClearCallback &callback);
243 
244     bool UnregisterBufferAvailableListener(NodeId id);
245 
246     int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode);
247 
248     int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys);
249 
250     int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode);
251 
252     int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx);
253 
254     int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode);
255 
256     int32_t SetScreenCorrection(ScreenId id, ScreenRotation screenRotation);
257 
258     bool SetVirtualMirrorScreenCanvasRotation(ScreenId id, bool canvasRotation);
259 
260     bool SetVirtualMirrorScreenScaleMode(ScreenId id, ScreenScaleMode scaleMode);
261 
262     bool SetGlobalDarkColorMode(bool isDark);
263 
264     int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode);
265 
266     int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability);
267 
268     int32_t GetPixelFormat(ScreenId id, GraphicPixelFormat& pixelFormat);
269 
270     int32_t SetPixelFormat(ScreenId id, GraphicPixelFormat pixelFormat);
271 
272     int32_t GetScreenSupportedHDRFormats(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats);
273 
274     int32_t GetScreenHDRFormat(ScreenId id, ScreenHDRFormat& hdrFormat);
275 
276     int32_t SetScreenHDRFormat(ScreenId id, int32_t modeIdx);
277 
278     int32_t GetScreenSupportedColorSpaces(ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces);
279 
280     int32_t GetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType& colorSpace);
281 
282     int32_t SetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType colorSpace);
283 
284     int32_t GetScreenType(ScreenId id, RSScreenType& screenType);
285 
286     bool GetBitmap(NodeId id, Drawing::Bitmap& bitmap);
287     bool GetPixelmap(NodeId id, std::shared_ptr<Media::PixelMap> pixelmap,
288         const Drawing::Rect* rect, std::shared_ptr<Drawing::DrawCmdList> drawCmdList);
289     bool RegisterTypeface(std::shared_ptr<Drawing::Typeface>& typeface);
290     bool UnRegisterTypeface(std::shared_ptr<Drawing::Typeface>& typeface);
291 
292     int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval);
293 
294     int32_t SetVirtualScreenRefreshRate(ScreenId id, uint32_t maxRefreshRate, uint32_t& actualRefreshRate);
295 
296     int32_t RegisterOcclusionChangeCallback(const OcclusionChangeCallback& callback);
297 
298     int32_t RegisterSurfaceOcclusionChangeCallback(
299         NodeId id, const SurfaceOcclusionChangeCallback& callback, std::vector<float>& partitionPoints);
300 
301     int32_t UnRegisterSurfaceOcclusionChangeCallback(NodeId id);
302 
303     int32_t RegisterHgmConfigChangeCallback(const HgmConfigChangeCallback& callback);
304 
305     int32_t RegisterHgmRefreshRateModeChangeCallback(const HgmRefreshRateModeChangeCallback& callback);
306 
307     int32_t RegisterHgmRefreshRateUpdateCallback(const HgmRefreshRateUpdateCallback& callback);
308 
309     int32_t RegisterFrameRateLinkerExpectedFpsUpdateCallback(int32_t dstPid,
310         const FrameRateLinkerExpectedFpsUpdateCallback& callback);
311 
312     void SetAppWindowNum(uint32_t num);
313 
314     bool SetSystemAnimatedScenes(SystemAnimatedScenes systemAnimatedScenes);
315 
316     void ShowWatermark(const std::shared_ptr<Media::PixelMap> &watermarkImg, bool isShow);
317 
318     int32_t ResizeVirtualScreen(ScreenId id, uint32_t width, uint32_t height);
319 
320     void ReportJankStats();
321 
322     void NotifyLightFactorStatus(bool isSafe);
323 
324     void NotifyPackageEvent(uint32_t listSize, const std::vector<std::string>& packageList);
325 
326     void NotifyRefreshRateEvent(const EventInfo& eventInfo);
327 
328     void NotifyTouchEvent(int32_t touchStatus, int32_t touchCnt);
329 
330     void NotifyDynamicModeEvent(bool enableDynamicMode);
331 
332     void ReportEventResponse(DataBaseRs info);
333 
334     void ReportEventComplete(DataBaseRs info);
335 
336     void ReportEventJankFrame(DataBaseRs info);
337 
338     void ReportGameStateData(GameStateData info);
339 
340     void SetHardwareEnabled(NodeId id, bool isEnabled, SelfDrawingNodeType selfDrawingType, bool dynamicHardwareEnable);
341 
342     uint32_t SetHidePrivacyContent(NodeId id, bool needHidePrivacyContent);
343 
344     void SetCacheEnabledForRotation(bool isEnabled);
345 
346     void SetOnRemoteDiedCallback(const OnRemoteDiedCallback& callback);
347 
348     std::vector<ActiveDirtyRegionInfo> GetActiveDirtyRegionInfo();
349 
350     GlobalDirtyRegionInfo GetGlobalDirtyRegionInfo();
351 
352     LayerComposeInfo GetLayerComposeInfo();
353 
354     HwcDisabledReasonInfos GetHwcDisabledReasonInfo();
355 
356     void SetVmaCacheStatus(bool flag);
357 
358     int32_t RegisterUIExtensionCallback(uint64_t userId, const UIExtensionCallback& callback);
359 
360     bool SetAncoForceDoDirect(bool direct);
361 
362     void SetLayerTop(const std::string &nodeIdStr, bool isTop);
363 #ifdef TP_FEATURE_ENABLE
364     void SetTpFeatureConfig(int32_t feature, const char* config,
365         TpFeatureConfigType tpFeatureConfigType = TpFeatureConfigType::DEFAULT_TP_FEATURE);
366 #endif
367     void SetVirtualScreenUsingStatus(bool isVirtualScreenUsingStatus);
368     void SetCurtainScreenUsingStatus(bool isCurtainScreenOn);
369 
370     void DropFrameByPid(const std::vector<int32_t> pidList);
371 
372     bool RegisterSurfaceBufferCallback(pid_t pid, uint64_t uid,
373         std::shared_ptr<SurfaceBufferCallback> callback);
374 
375     bool UnregisterSurfaceBufferCallback(pid_t pid, uint64_t uid);
376 private:
377     void TriggerSurfaceCaptureCallback(NodeId id, std::shared_ptr<Media::PixelMap> pixelmap);
378     void TriggerOnFinish(const FinishCallbackRet& ret) const;
379     void TriggerOnAfterAcquireBuffer(const AfterAcquireBufferRet& ret) const;
380 
381     std::mutex mutex_;
382     std::map<NodeId, sptr<RSIBufferAvailableCallback>> bufferAvailableCbRTMap_;
383     std::mutex mapMutex_;
384     std::map<NodeId, sptr<RSIBufferAvailableCallback>> bufferAvailableCbUIMap_;
385     sptr<RSIScreenChangeCallback> screenChangeCb_;
386     sptr<RSISurfaceCaptureCallback> surfaceCaptureCbDirector_;
387     std::map<NodeId, std::vector<std::shared_ptr<SurfaceCaptureCallback>>> surfaceCaptureCbMap_;
388 
389     sptr<RSISurfaceBufferCallback> surfaceBufferCbDirector_;
390     std::map<uint64_t, std::shared_ptr<SurfaceBufferCallback>> surfaceBufferCallbacks_;
391     mutable std::shared_mutex surfaceBufferCallbackMutex_;
392 
393     friend class SurfaceCaptureCallbackDirector;
394     friend class SurfaceBufferCallbackDirector;
395 };
396 } // namespace Rosen
397 } // namespace OHOS
398 
399 #endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H
400