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 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 33 #include "ipc_callbacks/pointer_render/pointer_luminance_change_callback.h" 34 #endif 35 #include "ipc_callbacks/rs_surface_buffer_callback.h" 36 #include "ipc_callbacks/screen_change_callback.h" 37 #include "ipc_callbacks/surface_capture_callback.h" 38 #include "memory/rs_memory_graphic.h" 39 #include "platform/drawing/rs_surface.h" 40 #include "rs_irender_client.h" 41 #include "variable_frame_rate/rs_variable_frame_rate.h" 42 #include "screen_manager/rs_screen_capability.h" 43 #include "screen_manager/rs_screen_data.h" 44 #include "screen_manager/rs_screen_hdr_capability.h" 45 #include "screen_manager/rs_screen_mode_info.h" 46 #include "screen_manager/screen_types.h" 47 #include "screen_manager/rs_virtual_screen_resolution.h" 48 #include "vsync_receiver.h" 49 #include "ipc_callbacks/rs_iocclusion_change_callback.h" 50 #include "rs_hgm_config_data.h" 51 #include "rs_occlusion_data.h" 52 #include "rs_self_drawing_node_rect_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 #include "utils/scalar.h" 58 59 namespace OHOS { 60 namespace Rosen { 61 // normal callback functor for client users. 62 using ScreenChangeCallback = std::function<void(ScreenId, ScreenEvent, ScreenChangeReason)>; 63 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 64 using PointerLuminanceChangeCallback = std::function<void(int32_t)>; 65 #endif 66 using BufferAvailableCallback = std::function<void()>; 67 using BufferClearCallback = std::function<void()>; 68 using OcclusionChangeCallback = std::function<void(std::shared_ptr<RSOcclusionData>)>; 69 using SurfaceOcclusionChangeCallback = std::function<void(float)>; 70 using HgmConfigChangeCallback = std::function<void(std::shared_ptr<RSHgmConfigData>)>; 71 using OnRemoteDiedCallback = std::function<void()>; 72 using HgmRefreshRateModeChangeCallback = std::function<void(int32_t)>; 73 using HgmRefreshRateUpdateCallback = std::function<void(int32_t)>; 74 using FrameRateLinkerExpectedFpsUpdateCallback = std::function<void(int32_t, int32_t)>; 75 using UIExtensionCallback = std::function<void(std::shared_ptr<RSUIExtensionData>, uint64_t)>; 76 using SelfDrawingNodeRectChangeCallback = std::function<void(std::shared_ptr<RSSelfDrawingNodeRectData>)>; 77 struct DataBaseRs { 78 int32_t appPid = -1; 79 int32_t eventType = -1; 80 int32_t versionCode = -1; 81 int64_t uniqueId = 0; 82 int64_t inputTime = 0; 83 int64_t beginVsyncTime = 0; 84 int64_t endVsyncTime = 0; 85 bool isDisplayAnimator = false; 86 std::string sceneId; 87 std::string versionName; 88 std::string bundleName; 89 std::string processName; 90 std::string abilityName; 91 std::string pageUrl; 92 std::string sourceType; 93 std::string note; 94 }; 95 96 struct AppInfo { 97 int64_t startTime = 0; 98 int64_t endTime = 0; 99 int32_t pid = 0; 100 std::string versionName; 101 int32_t versionCode = -1; 102 std::string bundleName; 103 std::string processName; 104 }; 105 struct GameStateData { 106 int32_t pid = -1; 107 int32_t uid = 0; 108 int32_t state = 0; 109 int32_t renderTid = -1; 110 std::string bundleName; 111 }; 112 113 class SurfaceCaptureCallback { 114 public: SurfaceCaptureCallback()115 SurfaceCaptureCallback() {} ~SurfaceCaptureCallback()116 virtual ~SurfaceCaptureCallback() {} 117 virtual void OnSurfaceCapture(std::shared_ptr<Media::PixelMap> pixelmap) = 0; 118 }; 119 120 class SurfaceBufferCallback { 121 public: 122 SurfaceBufferCallback() = default; 123 virtual ~SurfaceBufferCallback() noexcept = default; 124 virtual void OnFinish(const FinishCallbackRet& ret) = 0; 125 virtual void OnAfterAcquireBuffer(const AfterAcquireBufferRet& ret) = 0; 126 }; 127 128 class RSB_EXPORT RSRenderServiceClient : public RSIRenderClient { 129 public: 130 RSRenderServiceClient() = default; 131 virtual ~RSRenderServiceClient() = default; 132 133 RSRenderServiceClient(const RSRenderServiceClient&) = delete; 134 void operator=(const RSRenderServiceClient&) = delete; 135 136 void CommitTransaction(std::unique_ptr<RSTransactionData>& transactionData) override; 137 void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task) override; 138 139 bool GetUniRenderEnabled(); 140 141 bool CreateNode(const RSSurfaceRenderNodeConfig& config); 142 bool CreateNode(const RSDisplayNodeConfig& displayNodeConfig, NodeId nodeId); 143 std::shared_ptr<RSSurface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config, bool unobscured = false); 144 std::shared_ptr<VSyncReceiver> CreateVSyncReceiver( 145 const std::string& name, 146 const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &looper = nullptr, 147 uint64_t id = 0, 148 NodeId windowNodeId = 0, 149 bool fromXcomponent = false); 150 151 int32_t GetPixelMapByProcessId(std::vector<PixelMapInfo>& pixelMapInfoVector, pid_t pid); 152 153 std::shared_ptr<Media::PixelMap> CreatePixelMapFromSurfaceId(uint64_t surfaceid, const Rect &srcRect); 154 155 bool TakeSurfaceCapture(NodeId id, std::shared_ptr<SurfaceCaptureCallback> callback, 156 const RSSurfaceCaptureConfig& captureConfig, const RSSurfaceCaptureBlurParam& blurParam = {}, 157 const Drawing::Rect& specifiedAreaRect = Drawing::Rect(0.f, 0.f, 0.f, 0.f)); 158 159 bool TakeSelfSurfaceCapture(NodeId id, std::shared_ptr<SurfaceCaptureCallback> callback, 160 const RSSurfaceCaptureConfig& captureConfig); 161 162 bool SetWindowFreezeImmediately(NodeId id, bool isFreeze, std::shared_ptr<SurfaceCaptureCallback> callback, 163 const RSSurfaceCaptureConfig& captureConfig, const RSSurfaceCaptureBlurParam& blurParam = {}); 164 165 bool SetHwcNodeBounds(int64_t rsNodeId, float positionX, float positionY, 166 float positionZ, float positionW); 167 168 int32_t SetFocusAppInfo(int32_t pid, int32_t uid, const std::string &bundleName, const std::string &abilityName, 169 uint64_t focusNodeId); 170 171 ScreenId GetDefaultScreenId(); 172 ScreenId GetActiveScreenId(); 173 174 std::vector<ScreenId> GetAllScreenIds(); 175 176 #ifndef ROSEN_CROSS_PLATFORM 177 std::shared_ptr<RSSurface> CreateRSSurface(const sptr<Surface> &surface); 178 ScreenId CreateVirtualScreen(const std::string& name, uint32_t width, uint32_t height, sptr<Surface> surface, 179 ScreenId mirrorId, int32_t flags, std::vector<NodeId> whiteList = {}); 180 181 int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface); 182 183 int32_t SetVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector); 184 185 int32_t AddVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector); 186 187 int32_t RemoveVirtualScreenBlackList(ScreenId id, std::vector<NodeId>& blackListVector); 188 #endif 189 190 int32_t SetVirtualScreenSecurityExemptionList(ScreenId id, const std::vector<NodeId>& securityExemptionList); 191 192 int32_t SetScreenSecurityMask(ScreenId id, std::shared_ptr<Media::PixelMap> securityMask); 193 194 int32_t SetMirrorScreenVisibleRect(ScreenId id, const Rect& mainScreenRect, bool supportRotation = false); 195 196 int32_t SetCastScreenEnableSkipWindow(ScreenId id, bool enable); 197 198 bool SetWatermark(const std::string& name, std::shared_ptr<Media::PixelMap> watermark); 199 200 void RemoveVirtualScreen(ScreenId id); 201 202 #ifdef OHOS_BUILD_ENABLE_MAGICCURSOR 203 int32_t SetPointerColorInversionConfig(float darkBuffer, float brightBuffer, int64_t interval, int32_t rangeSize); 204 205 int32_t SetPointerColorInversionEnabled(bool enable); 206 207 int32_t RegisterPointerLuminanceChangeCallback(const PointerLuminanceChangeCallback &callback); 208 209 int32_t UnRegisterPointerLuminanceChangeCallback(); 210 #endif 211 212 int32_t SetScreenChangeCallback(const ScreenChangeCallback& callback); 213 214 #ifndef ROSEN_ARKUI_X 215 void SetScreenActiveMode(ScreenId id, uint32_t modeId); 216 #endif // !ROSEN_ARKUI_X 217 void SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate); 218 219 void SetRefreshRateMode(int32_t refreshRateMode); 220 221 void SyncFrameRateRange(FrameRateLinkerId id, const FrameRateRange& range, 222 int32_t animatorExpectedFrameRate); 223 224 void UnregisterFrameRateLinker(FrameRateLinkerId id); 225 226 uint32_t GetScreenCurrentRefreshRate(ScreenId id); 227 228 int32_t GetCurrentRefreshRateMode(); 229 230 std::vector<int32_t> GetScreenSupportedRefreshRates(ScreenId id); 231 232 bool GetShowRefreshRateEnabled(); 233 234 void SetShowRefreshRateEnabled(bool enabled, int32_t type); 235 236 uint32_t GetRealtimeRefreshRate(ScreenId screenId); 237 238 std::string GetRefreshInfo(pid_t pid); 239 240 #ifndef ROSEN_ARKUI_X 241 int32_t SetPhysicalScreenResolution(ScreenId id, uint32_t width, uint32_t height); 242 243 int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height); 244 245 RSVirtualScreenResolution GetVirtualScreenResolution(ScreenId id); 246 247 void MarkPowerOffNeedProcessOneFrame(); 248 249 void RepaintEverything(); 250 251 void DisablePowerOffRenderControl(ScreenId id); 252 253 void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status); 254 255 RSScreenModeInfo GetScreenActiveMode(ScreenId id); 256 257 std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id); 258 259 RSScreenCapability GetScreenCapability(ScreenId id); 260 261 ScreenPowerStatus GetScreenPowerStatus(ScreenId id); 262 263 RSScreenData GetScreenData(ScreenId id); 264 265 MemoryGraphic GetMemoryGraphic(int pid); 266 267 std::vector<MemoryGraphic> GetMemoryGraphics(); 268 #endif // !ROSEN_ARKUI_X 269 bool GetTotalAppMemSize(float& cpuMemSize, float& gpuMemSize); 270 271 int32_t GetScreenBacklight(ScreenId id); 272 273 void SetScreenBacklight(ScreenId id, uint32_t level); 274 275 bool RegisterBufferAvailableListener( 276 NodeId id, const BufferAvailableCallback &callback, bool isFromRenderThread = false); 277 278 bool RegisterBufferClearListener( 279 NodeId id, const BufferClearCallback &callback); 280 281 bool UnregisterBufferAvailableListener(NodeId id); 282 283 int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode); 284 285 int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys); 286 287 int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode); 288 289 int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx); 290 291 int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode); 292 293 int32_t SetScreenCorrection(ScreenId id, ScreenRotation screenRotation); 294 295 bool SetVirtualMirrorScreenCanvasRotation(ScreenId id, bool canvasRotation); 296 297 bool SetVirtualMirrorScreenScaleMode(ScreenId id, ScreenScaleMode scaleMode); 298 299 bool SetGlobalDarkColorMode(bool isDark); 300 301 int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode); 302 303 int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability); 304 305 int32_t GetPixelFormat(ScreenId id, GraphicPixelFormat& pixelFormat); 306 307 int32_t SetPixelFormat(ScreenId id, GraphicPixelFormat pixelFormat); 308 309 int32_t GetScreenSupportedHDRFormats(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats); 310 311 int32_t GetScreenHDRFormat(ScreenId id, ScreenHDRFormat& hdrFormat); 312 313 int32_t SetScreenHDRFormat(ScreenId id, int32_t modeIdx); 314 315 int32_t GetScreenSupportedColorSpaces(ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces); 316 317 int32_t GetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType& colorSpace); 318 319 int32_t SetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType colorSpace); 320 321 int32_t GetScreenType(ScreenId id, RSScreenType& screenType); 322 323 bool GetBitmap(NodeId id, Drawing::Bitmap& bitmap); 324 bool GetPixelmap(NodeId id, std::shared_ptr<Media::PixelMap> pixelmap, 325 const Drawing::Rect* rect, std::shared_ptr<Drawing::DrawCmdList> drawCmdList); 326 bool RegisterTypeface(std::shared_ptr<Drawing::Typeface>& typeface); 327 bool UnRegisterTypeface(std::shared_ptr<Drawing::Typeface>& typeface); 328 329 int32_t GetDisplayIdentificationData(ScreenId id, uint8_t& outPort, std::vector<uint8_t>& edidData); 330 331 int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval); 332 333 int32_t SetVirtualScreenRefreshRate(ScreenId id, uint32_t maxRefreshRate, uint32_t& actualRefreshRate); 334 335 uint32_t SetScreenActiveRect(ScreenId id, const Rect& activeRect); 336 337 int32_t RegisterOcclusionChangeCallback(const OcclusionChangeCallback& callback); 338 339 int32_t RegisterSurfaceOcclusionChangeCallback( 340 NodeId id, const SurfaceOcclusionChangeCallback& callback, std::vector<float>& partitionPoints); 341 342 int32_t UnRegisterSurfaceOcclusionChangeCallback(NodeId id); 343 344 int32_t RegisterHgmConfigChangeCallback(const HgmConfigChangeCallback& callback); 345 346 int32_t RegisterHgmRefreshRateModeChangeCallback(const HgmRefreshRateModeChangeCallback& callback); 347 348 int32_t RegisterHgmRefreshRateUpdateCallback(const HgmRefreshRateUpdateCallback& callback); 349 350 int32_t RegisterFrameRateLinkerExpectedFpsUpdateCallback(int32_t dstPid, 351 const FrameRateLinkerExpectedFpsUpdateCallback& callback); 352 353 void SetAppWindowNum(uint32_t num); 354 355 bool SetSystemAnimatedScenes(SystemAnimatedScenes systemAnimatedScenes, bool isRegularAnimation = false); 356 357 void ShowWatermark(const std::shared_ptr<Media::PixelMap> &watermarkImg, bool isShow); 358 359 int32_t ResizeVirtualScreen(ScreenId id, uint32_t width, uint32_t height); 360 361 void ReportJankStats(); 362 363 void NotifyLightFactorStatus(int32_t lightFactorStatus); 364 365 void NotifyPackageEvent(uint32_t listSize, const std::vector<std::string>& packageList); 366 367 void NotifyAppStrategyConfigChangeEvent(const std::string& pkgName, uint32_t listSize, 368 const std::vector<std::pair<std::string, std::string>>& newConfig); 369 370 void NotifyRefreshRateEvent(const EventInfo& eventInfo); 371 372 void NotifyTouchEvent(int32_t touchStatus, int32_t touchCnt); 373 374 void NotifyDynamicModeEvent(bool enableDynamicMode); 375 376 void NotifyHgmConfigEvent(const std::string &eventName, bool state); 377 378 void ReportEventResponse(DataBaseRs info); 379 380 void ReportEventComplete(DataBaseRs info); 381 382 void ReportEventJankFrame(DataBaseRs info); 383 384 void ReportRsSceneJankStart(AppInfo info); 385 386 void ReportRsSceneJankEnd(AppInfo info); 387 388 void ReportGameStateData(GameStateData info); 389 390 void SetHardwareEnabled(NodeId id, bool isEnabled, SelfDrawingNodeType selfDrawingType, bool dynamicHardwareEnable); 391 392 uint32_t SetHidePrivacyContent(NodeId id, bool needHidePrivacyContent); 393 394 void SetCacheEnabledForRotation(bool isEnabled); 395 396 void SetOnRemoteDiedCallback(const OnRemoteDiedCallback& callback); 397 398 std::vector<ActiveDirtyRegionInfo> GetActiveDirtyRegionInfo(); 399 400 GlobalDirtyRegionInfo GetGlobalDirtyRegionInfo(); 401 402 LayerComposeInfo GetLayerComposeInfo(); 403 404 HwcDisabledReasonInfos GetHwcDisabledReasonInfo(); 405 406 int64_t GetHdrOnDuration(); 407 408 void SetVmaCacheStatus(bool flag); 409 410 int32_t RegisterUIExtensionCallback(uint64_t userId, const UIExtensionCallback& callback, bool unobscured = false); 411 412 bool SetAncoForceDoDirect(bool direct); 413 414 void SetLayerTop(const std::string &nodeIdStr, bool isTop); 415 416 #ifdef RS_ENABLE_OVERLAY_DISPLAY 417 int32_t SetOverlayDisplayMode(int32_t mode); 418 #endif 419 420 #ifdef TP_FEATURE_ENABLE 421 void SetTpFeatureConfig(int32_t feature, const char* config, 422 TpFeatureConfigType tpFeatureConfigType = TpFeatureConfigType::DEFAULT_TP_FEATURE); 423 #endif 424 void SetVirtualScreenUsingStatus(bool isVirtualScreenUsingStatus); 425 void SetCurtainScreenUsingStatus(bool isCurtainScreenOn); 426 427 void DropFrameByPid(const std::vector<int32_t> pidList); 428 429 bool SetVirtualScreenStatus(ScreenId id, VirtualScreenStatus screenStatus); 430 431 void SetFreeMultiWindowStatus(bool enable); 432 433 bool RegisterSurfaceBufferCallback(pid_t pid, uint64_t uid, 434 std::shared_ptr<SurfaceBufferCallback> callback); 435 436 bool UnregisterSurfaceBufferCallback(pid_t pid, uint64_t uid); 437 438 void NotifyScreenSwitched(); 439 440 void ForceRefreshOneFrameWithNextVSync(); 441 442 void SetWindowContainer(NodeId nodeId, bool value); 443 444 int32_t RegisterSelfDrawingNodeRectChangeCallback(const SelfDrawingNodeRectChangeCallback& callback); 445 446 void NotifyPageName(const std::string &packageName, const std::string &pageName, bool isEnter); 447 448 void TestLoadFileSubTreeToNode(NodeId nodeId, const std::string &filePath); 449 private: 450 void TriggerSurfaceCaptureCallback(NodeId id, const RSSurfaceCaptureConfig& captureConfig, 451 std::shared_ptr<Media::PixelMap> pixelmap); 452 void TriggerOnFinish(const FinishCallbackRet& ret) const; 453 void TriggerOnAfterAcquireBuffer(const AfterAcquireBufferRet& ret) const; 454 struct RectHash { operatorRectHash455 std::size_t operator()(const Drawing::Rect& rect) const { 456 std::size_t h1 = std::hash<Drawing::scalar>()(rect.left_); 457 std::size_t h2 = std::hash<Drawing::scalar>()(rect.top_); 458 std::size_t h3 = std::hash<Drawing::scalar>()(rect.right_); 459 std::size_t h4 = std::hash<Drawing::scalar>()(rect.bottom_); 460 return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3); 461 } 462 }; 463 464 struct PairHash { operatorPairHash465 std::size_t operator()(const std::pair<NodeId, RSSurfaceCaptureConfig>& p) const { 466 std::size_t h1 = std::hash<NodeId>()(p.first); 467 std::size_t h2 = RectHash()(p.second.mainScreenRect); 468 return h1 ^ (h2 << 1); 469 } 470 }; 471 472 std::mutex mutex_; 473 std::map<NodeId, sptr<RSIBufferAvailableCallback>> bufferAvailableCbRTMap_; 474 std::mutex mapMutex_; 475 std::map<NodeId, sptr<RSIBufferAvailableCallback>> bufferAvailableCbUIMap_; 476 sptr<RSIScreenChangeCallback> screenChangeCb_ = nullptr; 477 sptr<RSISurfaceCaptureCallback> surfaceCaptureCbDirector_ = nullptr; 478 std::unordered_map<std::pair<NodeId, RSSurfaceCaptureConfig>, 479 std::vector<std::shared_ptr<SurfaceCaptureCallback>>, PairHash> surfaceCaptureCbMap_; 480 481 sptr<RSISurfaceBufferCallback> surfaceBufferCbDirector_; 482 std::map<uint64_t, std::shared_ptr<SurfaceBufferCallback>> surfaceBufferCallbacks_; 483 mutable std::shared_mutex surfaceBufferCallbackMutex_; 484 485 friend class SurfaceCaptureCallbackDirector; 486 friend class SurfaceBufferCallbackDirector; 487 }; 488 } // namespace Rosen 489 } // namespace OHOS 490 491 #endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H 492