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 <refbase.h> 24 #include <surface_type.h> 25 #ifndef ROSEN_CROSS_PLATFORM 26 #include <surface.h> 27 #endif 28 29 #include "ipc_callbacks/buffer_available_callback.h" 30 #include "ipc_callbacks/iapplication_agent.h" 31 #include "ipc_callbacks/screen_change_callback.h" 32 #include "ipc_callbacks/surface_capture_callback.h" 33 #include "memory/rs_memory_graphic.h" 34 #ifdef NEW_RENDER_CONTEXT 35 #include "render_backend/rs_render_surface.h" 36 #else 37 #include "platform/drawing/rs_surface.h" 38 #endif 39 #include "rs_irender_client.h" 40 #include "variable_frame_rate/rs_variable_frame_rate.h" 41 #include "screen_manager/rs_screen_capability.h" 42 #include "screen_manager/rs_screen_data.h" 43 #include "screen_manager/rs_screen_hdr_capability.h" 44 #include "screen_manager/rs_screen_mode_info.h" 45 #include "screen_manager/screen_types.h" 46 #include "screen_manager/rs_virtual_screen_resolution.h" 47 #include "vsync_receiver.h" 48 #include "ipc_callbacks/rs_iocclusion_change_callback.h" 49 #include "rs_hgm_config_data.h" 50 #include "rs_occlusion_data.h" 51 52 namespace OHOS { 53 namespace Rosen { 54 // normal callback functor for client users. 55 using ScreenChangeCallback = std::function<void(ScreenId, ScreenEvent)>; 56 using BufferAvailableCallback = std::function<void()>; 57 using BufferClearCallback = std::function<void()>; 58 using OcclusionChangeCallback = std::function<void(std::shared_ptr<RSOcclusionData>)>; 59 using SurfaceOcclusionChangeCallback = std::function<void(float)>; 60 using HgmConfigChangeCallback = std::function<void(std::shared_ptr<RSHgmConfigData>)>; 61 using OnRemoteDiedCallback = std::function<void()>; 62 using HgmRefreshRateModeChangeCallback = std::function<void(int32_t)>; 63 64 struct DataBaseRs { 65 int32_t appPid = -1; 66 int32_t eventType = -1; 67 int32_t versionCode = -1; 68 int64_t uniqueId = 0; 69 int64_t inputTime = 0; 70 int64_t beginVsyncTime = 0; 71 int64_t endVsyncTime = 0; 72 bool isDisplayAnimator = false; 73 std::string sceneId; 74 std::string versionName; 75 std::string bundleName; 76 std::string processName; 77 std::string abilityName; 78 std::string pageUrl; 79 std::string sourceType; 80 std::string note; 81 }; 82 83 struct GameStateData { 84 int32_t pid = -1; 85 int32_t uid = 0; 86 int32_t state = 0; 87 int32_t renderTid = -1; 88 std::string bundleName; 89 }; 90 91 class SurfaceCaptureCallback { 92 public: SurfaceCaptureCallback()93 SurfaceCaptureCallback() {} ~SurfaceCaptureCallback()94 virtual ~SurfaceCaptureCallback() {} 95 virtual void OnSurfaceCapture(std::shared_ptr<Media::PixelMap> pixelmap) = 0; 96 }; 97 98 class RSB_EXPORT RSRenderServiceClient : public RSIRenderClient { 99 public: 100 RSRenderServiceClient() = default; 101 virtual ~RSRenderServiceClient() = default; 102 103 RSRenderServiceClient(const RSRenderServiceClient&) = delete; 104 void operator=(const RSRenderServiceClient&) = delete; 105 106 void CommitTransaction(std::unique_ptr<RSTransactionData>& transactionData) override; 107 void ExecuteSynchronousTask(const std::shared_ptr<RSSyncTask>& task) override; 108 109 bool GetUniRenderEnabled(); 110 111 bool CreateNode(const RSSurfaceRenderNodeConfig& config); 112 #ifdef NEW_RENDER_CONTEXT 113 std::shared_ptr<RSRenderSurface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config); 114 #else 115 std::shared_ptr<RSSurface> CreateNodeAndSurface(const RSSurfaceRenderNodeConfig& config); 116 #endif 117 std::shared_ptr<VSyncReceiver> CreateVSyncReceiver( 118 const std::string& name, 119 const std::shared_ptr<OHOS::AppExecFwk::EventHandler> &looper = nullptr, 120 uint64_t id = 0); 121 122 bool TakeSurfaceCapture( 123 NodeId id, std::shared_ptr<SurfaceCaptureCallback> callback, float scaleX, float scaleY, 124 SurfaceCaptureType surfaceCaptureType = SurfaceCaptureType::DEFAULT_CAPTURE); 125 126 int32_t SetFocusAppInfo(int32_t pid, int32_t uid, const std::string &bundleName, const std::string &abilityName, 127 uint64_t focusNodeId); 128 129 ScreenId GetDefaultScreenId(); 130 ScreenId GetActiveScreenId(); 131 132 std::vector<ScreenId> GetAllScreenIds(); 133 134 #ifndef ROSEN_CROSS_PLATFORM 135 #if defined(NEW_RENDER_CONTEXT) 136 std::shared_ptr<RSRenderSurface> CreateRSSurface(const sptr<Surface> &surface); 137 #else 138 std::shared_ptr<RSSurface> CreateRSSurface(const sptr<Surface> &surface); 139 #endif 140 ScreenId CreateVirtualScreen(const std::string& name, uint32_t width, uint32_t height, sptr<Surface> surface, 141 ScreenId mirrorId, int32_t flags, std::vector<NodeId> filteredAppVector = {}); 142 143 int32_t SetVirtualScreenSurface(ScreenId id, sptr<Surface> surface); 144 #endif 145 146 void RemoveVirtualScreen(ScreenId id); 147 148 int32_t SetScreenChangeCallback(const ScreenChangeCallback& callback); 149 150 #ifndef ROSEN_ARKUI_X 151 void SetScreenActiveMode(ScreenId id, uint32_t modeId); 152 #endif // !ROSEN_ARKUI_X 153 void SetScreenRefreshRate(ScreenId id, int32_t sceneId, int32_t rate); 154 155 void SetRefreshRateMode(int32_t refreshRateMode); 156 157 void SyncFrameRateRange(const FrameRateRange& range); 158 159 uint32_t GetScreenCurrentRefreshRate(ScreenId id); 160 161 int32_t GetCurrentRefreshRateMode(); 162 163 std::vector<int32_t> GetScreenSupportedRefreshRates(ScreenId id); 164 165 bool GetShowRefreshRateEnabled(); 166 167 void SetShowRefreshRateEnabled(bool enable); 168 169 #ifndef ROSEN_ARKUI_X 170 int32_t SetVirtualScreenResolution(ScreenId id, uint32_t width, uint32_t height); 171 172 RSVirtualScreenResolution GetVirtualScreenResolution(ScreenId id); 173 174 void SetScreenPowerStatus(ScreenId id, ScreenPowerStatus status); 175 176 RSScreenModeInfo GetScreenActiveMode(ScreenId id); 177 178 std::vector<RSScreenModeInfo> GetScreenSupportedModes(ScreenId id); 179 180 RSScreenCapability GetScreenCapability(ScreenId id); 181 182 ScreenPowerStatus GetScreenPowerStatus(ScreenId id); 183 184 RSScreenData GetScreenData(ScreenId id); 185 186 MemoryGraphic GetMemoryGraphic(int pid); 187 188 std::vector<MemoryGraphic> GetMemoryGraphics(); 189 #endif // !ROSEN_ARKUI_X 190 bool GetTotalAppMemSize(float& cpuMemSize, float& gpuMemSize); 191 192 int32_t GetScreenBacklight(ScreenId id); 193 194 void SetScreenBacklight(ScreenId id, uint32_t level); 195 196 bool RegisterBufferAvailableListener( 197 NodeId id, const BufferAvailableCallback &callback, bool isFromRenderThread = false); 198 199 bool RegisterBufferClearListener( 200 NodeId id, const BufferClearCallback &callback); 201 202 bool UnregisterBufferAvailableListener(NodeId id); 203 204 int32_t GetScreenSupportedColorGamuts(ScreenId id, std::vector<ScreenColorGamut>& mode); 205 206 int32_t GetScreenSupportedMetaDataKeys(ScreenId id, std::vector<ScreenHDRMetadataKey>& keys); 207 208 int32_t GetScreenColorGamut(ScreenId id, ScreenColorGamut& mode); 209 210 int32_t SetScreenColorGamut(ScreenId id, int32_t modeIdx); 211 212 int32_t SetScreenGamutMap(ScreenId id, ScreenGamutMap mode); 213 214 int32_t SetScreenCorrection(ScreenId id, ScreenRotation screenRotation); 215 216 bool SetVirtualMirrorScreenCanvasRotation(ScreenId id, bool canvasRotation); 217 218 int32_t GetScreenGamutMap(ScreenId id, ScreenGamutMap& mode); 219 220 int32_t GetScreenHDRCapability(ScreenId id, RSScreenHDRCapability& screenHdrCapability); 221 222 int32_t GetPixelFormat(ScreenId id, GraphicPixelFormat& pixelFormat); 223 224 int32_t SetPixelFormat(ScreenId id, GraphicPixelFormat pixelFormat); 225 226 int32_t GetScreenSupportedHDRFormats(ScreenId id, std::vector<ScreenHDRFormat>& hdrFormats); 227 228 int32_t GetScreenHDRFormat(ScreenId id, ScreenHDRFormat& hdrFormat); 229 230 int32_t SetScreenHDRFormat(ScreenId id, int32_t modeIdx); 231 232 int32_t GetScreenSupportedColorSpaces(ScreenId id, std::vector<GraphicCM_ColorSpaceType>& colorSpaces); 233 234 int32_t GetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType& colorSpace); 235 236 int32_t SetScreenColorSpace(ScreenId id, GraphicCM_ColorSpaceType colorSpace); 237 238 int32_t GetScreenType(ScreenId id, RSScreenType& screenType); 239 240 #ifndef USE_ROSEN_DRAWING 241 bool GetBitmap(NodeId id, SkBitmap& bitmap); 242 bool GetPixelmap(NodeId id, std::shared_ptr<Media::PixelMap> pixelmap, 243 const SkRect* rect, std::shared_ptr<DrawCmdList> drawCmdList); 244 #else 245 bool GetBitmap(NodeId id, Drawing::Bitmap& bitmap); 246 bool GetPixelmap(NodeId id, std::shared_ptr<Media::PixelMap> pixelmap, 247 const Drawing::Rect* rect, std::shared_ptr<Drawing::DrawCmdList> drawCmdList); 248 #endif 249 250 int32_t SetScreenSkipFrameInterval(ScreenId id, uint32_t skipFrameInterval); 251 252 int32_t RegisterOcclusionChangeCallback(const OcclusionChangeCallback& callback); 253 254 int32_t RegisterSurfaceOcclusionChangeCallback( 255 NodeId id, const SurfaceOcclusionChangeCallback& callback, std::vector<float>& partitionPoints); 256 257 int32_t UnRegisterSurfaceOcclusionChangeCallback(NodeId id); 258 259 int32_t RegisterHgmConfigChangeCallback(const HgmConfigChangeCallback& callback); 260 261 int32_t RegisterHgmRefreshRateModeChangeCallback(const HgmRefreshRateModeChangeCallback& callback); 262 263 void SetAppWindowNum(uint32_t num); 264 265 bool SetSystemAnimatedScenes(SystemAnimatedScenes systemAnimatedScenes); 266 267 void ShowWatermark(const std::shared_ptr<Media::PixelMap> &watermarkImg, bool isShow); 268 269 int32_t ResizeVirtualScreen(ScreenId id, uint32_t width, uint32_t height); 270 271 void ReportJankStats(); 272 273 void NotifyLightFactorStatus(bool isSafe); 274 275 void NotifyPackageEvent(uint32_t listSize, const std::vector<std::string>& packageList); 276 277 void NotifyRefreshRateEvent(const EventInfo& eventInfo); 278 279 void NotifyTouchEvent(int32_t touchStatus); 280 281 void ReportEventResponse(DataBaseRs info); 282 283 void ReportEventComplete(DataBaseRs info); 284 285 void ReportEventJankFrame(DataBaseRs info); 286 287 void ReportGameStateData(GameStateData info); 288 289 void SetHardwareEnabled(NodeId id, bool isEnabled, SelfDrawingNodeType selfDrawingType); 290 291 void SetCacheEnabledForRotation(bool isEnabled); 292 293 void SetOnRemoteDiedCallback(const OnRemoteDiedCallback& callback); 294 295 #ifdef TP_FEATURE_ENABLE 296 void SetTpFeatureConfig(int32_t feature, const char* config); 297 #endif 298 void SetVirtualScreenUsingStatus(bool isVirtualScreenUsingStatus); 299 private: 300 void TriggerSurfaceCaptureCallback(NodeId id, Media::PixelMap* pixelmap); 301 std::mutex mutex_; 302 std::map<NodeId, sptr<RSIBufferAvailableCallback>> bufferAvailableCbRTMap_; 303 std::mutex mapMutex_; 304 std::map<NodeId, sptr<RSIBufferAvailableCallback>> bufferAvailableCbUIMap_; 305 sptr<RSIScreenChangeCallback> screenChangeCb_; 306 sptr<RSISurfaceCaptureCallback> surfaceCaptureCbDirector_; 307 std::map<NodeId, std::vector<std::shared_ptr<SurfaceCaptureCallback>>> surfaceCaptureCbMap_; 308 309 friend class SurfaceCaptureCallbackDirector; 310 }; 311 } // namespace Rosen 312 } // namespace OHOS 313 314 #endif // ROSEN_RENDER_SERVICE_BASE_TRANSACTION_RS_RENDER_SERVICE_CLIENT_H 315