1 /*
2  * Copyright (c) 2024 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_PROFILER_H
17 #define RENDER_SERVICE_PROFILER_H
18 
19 #ifdef RS_PROFILER_ENABLED
20 
21 #include <map>
22 #include <string>
23 
24 #include "common/rs_vector4.h"
25 
26 #include "params/rs_display_render_params.h"
27 
28 #define RS_PROFILER_INIT(renderSevice) RSProfiler::Init(renderSevice)
29 #define RS_PROFILER_ON_FRAME_BEGIN() RSProfiler::OnFrameBegin()
30 #define RS_PROFILER_ON_FRAME_END() RSProfiler::OnFrameEnd()
31 #define RS_PROFILER_ON_RENDER_BEGIN() RSProfiler::OnRenderBegin()
32 #define RS_PROFILER_ON_RENDER_END() RSProfiler::OnRenderEnd()
33 #define RS_PROFILER_ON_PROCESS_COMMAND() RSProfiler::OnProcessCommand()
34 #define RS_PROFILER_ON_CREATE_CONNECTION(pid) RSProfiler::OnCreateConnection(pid)
35 #define RS_PROFILER_ON_REMOTE_REQUEST(connection, code, data, reply, option) \
36     RSProfiler::OnRemoteRequest(connection, code, data, reply, option)
37 #define RS_PROFILER_ON_PARCEL_RECEIVE(parcel, data) RSProfiler::OnRecvParcel(parcel, data)
38 #define RS_PROFILER_COPY_PARCEL(parcel) RSProfiler::CopyParcel(parcel)
39 #define RS_PROFILER_PATCH_NODE_ID(parcel, id) id = RSProfiler::PatchNodeId(parcel, id)
40 #define RS_PROFILER_PATCH_PID(parcel, pid) pid = RSProfiler::PatchPid(parcel, pid)
41 #define RS_PROFILER_PATCH_TIME(time) time = RSProfiler::PatchTime(time)
42 #define RS_PROFILER_PATCH_TRANSACTION_TIME(parcel, time) time = RSProfiler::PatchTransactionTime(parcel, time)
43 #define RS_PROFILER_PATCH_COMMAND(parcel, command) RSProfiler::PatchCommand(parcel, command)
44 #define RS_PROFILER_EXECUTE_COMMAND(command) RSProfiler::ExecuteCommand(command)
45 #define RS_PROFILER_MARSHAL_PIXELMAP(parcel, map) RSProfiler::MarshalPixelMap(parcel, map)
46 #define RS_PROFILER_UNMARSHAL_PIXELMAP(parcel) RSProfiler::UnmarshalPixelMap(parcel)
47 #define RS_PROFILER_MARSHAL_DRAWINGIMAGE(image, compressData) RSProfiler::MarshalDrawingImage(image, compressData)
48 #define RS_PROFILER_SET_DIRTY_REGION(dirtyRegion) RSProfiler::SetDirtyRegion(dirtyRegion)
49 #define RS_PROFILER_WRITE_PARCEL_DATA(parcel) RSProfiler::WriteParcelData(parcel)
50 #define RS_PROFILER_READ_PARCEL_DATA(parcel, size, isMalloc) RSProfiler::ReadParcelData(parcel, size, isMalloc)
51 #define RS_PROFILER_GET_FRAME_NUMBER() RSProfiler::GetFrameNumber()
52 #define RS_PROFILER_ON_PARALLEL_RENDER_BEGIN() RSProfiler::OnParallelRenderBegin()
53 #define RS_PROFILER_ON_PARALLEL_RENDER_END(renderFrameNumber) RSProfiler::OnParallelRenderEnd(renderFrameNumber)
54 #define RS_PROFILER_SHOULD_BLOCK_HWCNODE() RSProfiler::ShouldBlockHWCNode()
55 #else
56 #define RS_PROFILER_INIT(renderSevice)
57 #define RS_PROFILER_ON_FRAME_BEGIN()
58 #define RS_PROFILER_ON_FRAME_END()
59 #define RS_PROFILER_ON_RENDER_BEGIN()
60 #define RS_PROFILER_ON_RENDER_END()
61 #define RS_PROFILER_ON_PROCESS_COMMAND()
62 #define RS_PROFILER_ON_CREATE_CONNECTION(pid)
63 #define RS_PROFILER_ON_REMOTE_REQUEST(connection, code, data, reply, option)
64 #define RS_PROFILER_ON_PARCEL_RECEIVE(parcel, data)
65 #define RS_PROFILER_COPY_PARCEL(parcel) std::make_shared<MessageParcel>()
66 #define RS_PROFILER_PATCH_NODE_ID(parcel, id)
67 #define RS_PROFILER_PATCH_PID(parcel, pid)
68 #define RS_PROFILER_PATCH_TIME(time)
69 #define RS_PROFILER_PATCH_TRANSACTION_TIME(parcel, time)
70 #define RS_PROFILER_PATCH_COMMAND(parcel, command)
71 #define RS_PROFILER_EXECUTE_COMMAND(command)
72 #define RS_PROFILER_MARSHAL_PIXELMAP(parcel, map) (map)->Marshalling(parcel)
73 #define RS_PROFILER_UNMARSHAL_PIXELMAP(parcel) Media::PixelMap::Unmarshalling(parcel)
74 #define RS_PROFILER_MARSHAL_DRAWINGIMAGE(image, compressData)
75 #define RS_PROFILER_SET_DIRTY_REGION(dirtyRegion)
76 #define RS_PROFILER_WRITE_PARCEL_DATA(parcel)
77 #define RS_PROFILER_READ_PARCEL_DATA(parcel, size, isMalloc) RSMarshallingHelper::ReadFromAshmem(parcel, size, isMalloc)
78 #define RS_PROFILER_GET_FRAME_NUMBER() 0
79 #define RS_PROFILER_ON_PARALLEL_RENDER_BEGIN()
80 #define RS_PROFILER_ON_PARALLEL_RENDER_END(renderFrameNumber)
81 #define RS_PROFILER_SHOULD_BLOCK_HWCNODE() false
82 #endif
83 
84 #ifdef RS_PROFILER_ENABLED
85 
86 namespace OHOS {
87 class Parcel;
88 class MessageParcel;
89 class MessageOption;
90 
91 } // namespace OHOS
92 
93 namespace OHOS::Media {
94 class PixelMap;
95 } // namespace OHOS::Media
96 
97 namespace OHOS::Rosen {
98 
99 class RSRenderService;
100 class RSMainThread;
101 class RSIRenderServiceConnection;
102 class RSRenderServiceConnection;
103 class RSTransactionData;
104 class RSRenderNode;
105 class RSRenderModifier;
106 class RSProperties;
107 class RSContext;
108 class RSDisplayRenderNode;
109 class RSRenderNodeMap;
110 class RSAnimationManager;
111 class RSRenderAnimation;
112 class RSCommand;
113 class ArgList;
114 class JsonWriter;
115 class RSFile;
116 
117 enum class Mode { NONE = 0, READ = 1, WRITE = 2, READ_EMUL = 3, WRITE_EMUL = 4 };
118 
119 class RSProfiler final {
120 public:
121     static void Init(RSRenderService* renderService);
122 
123     // see RSMainThread::Init
124     static void OnFrameBegin();
125     static void OnFrameEnd();
126     static void OnRenderBegin();
127     static void OnRenderEnd();
128     static void OnParallelRenderBegin();
129     static void OnParallelRenderEnd(uint32_t frameNumber);
130     static void OnProcessCommand();
131 
132     // see RSRenderService::CreateConnection
133     static void OnCreateConnection(pid_t pid);
134 
135     // see RenderServiceConnection::OnRemoteRequest
136     static void OnRemoteRequest(RSIRenderServiceConnection* connection, uint32_t code, MessageParcel& parcel,
137         MessageParcel& reply, MessageOption& option);
138 
139     // see UnmarshalThread::RecvParcel
140     static void OnRecvParcel(const MessageParcel* parcel, RSTransactionData* data);
141 
142     RSB_EXPORT static std::shared_ptr<MessageParcel> CopyParcel(const MessageParcel& parcel);
143     RSB_EXPORT static uint64_t PatchTime(uint64_t time);
144     RSB_EXPORT static uint64_t PatchTransactionTime(const Parcel& parcel, uint64_t timeAtRecordProcess);
145 
146     template<typename T>
PatchNodeId(const Parcel & parcel,T id)147     static T PatchNodeId(const Parcel& parcel, T id)
148     {
149         return static_cast<T>(PatchPlainNodeId(parcel, static_cast<NodeId>(id)));
150     }
151 
152     template<typename T>
PatchPid(const Parcel & parcel,T pid)153     static T PatchPid(const Parcel& parcel, T pid)
154     {
155         return static_cast<T>(PatchPlainPid(parcel, static_cast<pid_t>(pid)));
156     }
157 
158     RSB_EXPORT static void PatchCommand(const Parcel& parcel, RSCommand* command);
159     RSB_EXPORT static void ExecuteCommand(const RSCommand* command);
160     RSB_EXPORT static bool MarshalPixelMap(Parcel& parcel, const std::shared_ptr<Media::PixelMap>& map);
161     RSB_EXPORT static Media::PixelMap* UnmarshalPixelMap(Parcel& parcel);
162     RSB_EXPORT static void MarshalDrawingImage(std::shared_ptr<Drawing::Image>& image,
163         std::shared_ptr<Drawing::Data>& compressData);
164     RSB_EXPORT static void SetDirtyRegion(const Occlusion::Region& dirtyRegion);
165 
166     RSB_EXPORT static void WriteParcelData(Parcel& parcel);
167     RSB_EXPORT static const void* ReadParcelData(Parcel& parcel, size_t size, bool& isMalloc);
168 
169     RSB_EXPORT static uint32_t GetFrameNumber();
170     RSB_EXPORT static bool ShouldBlockHWCNode();
171 
172 public:
173     RSB_EXPORT static bool IsParcelMock(const Parcel& parcel);
174     RSB_EXPORT static bool IsSharedMemoryEnabled();
175     RSB_EXPORT static bool IsBetaRecordEnabled();
176     RSB_EXPORT static bool IsBetaRecordEnabledWithMetrics();
177 
178 private:
179     static const char* GetProcessNameByPid(int pid);
180 
181     RSB_EXPORT static void EnableSharedMemory();
182     RSB_EXPORT static void DisableSharedMemory();
183 
184     // Beta record
185     RSB_EXPORT static void EnableBetaRecord();
186     RSB_EXPORT static bool IsBetaRecordSavingTriggered();
187     static void StartBetaRecord();
188     static bool IsBetaRecordStarted();
189     static void UpdateBetaRecord();
190     static bool SaveBetaRecord();
191     static bool IsBetaRecordInactive();
192     static void RequestVSyncOnBetaRecordInactivity();
193     static void SendBetaRecordPath();
194     static void LaunchBetaRecordNotificationThread();
195     static void LaunchBetaRecordMetricsUpdateThread();
196     static void WriteBetaRecordFileThread(RSFile& file, const std::string path);
197     static void RenameAndSendFilenameThread();
198     static bool OpenBetaRecordFile(RSFile& file);
199     static bool SaveBetaRecordFile(RSFile& file);
200     static void WriteBetaRecordMetrics(RSFile& file, double time);
201     static void UpdateDirtyRegionBetaRecord(double currentFrameDirtyRegion);
202 
203     RSB_EXPORT static void SetMode(Mode mode);
204     RSB_EXPORT static Mode GetMode();
205     RSB_EXPORT static bool IsEnabled();
206 
207     RSB_EXPORT static uint32_t GetCommandCount();
208     RSB_EXPORT static uint32_t GetCommandExecuteCount();
209     RSB_EXPORT static std::string GetCommandParcelList(double recordStartTime);
210 
211     RSB_EXPORT static const std::vector<pid_t>& GetPids();
212     RSB_EXPORT static NodeId GetParentNode();
213     RSB_EXPORT static void SetSubstitutingPid(const std::vector<pid_t>& pids, pid_t pid, NodeId parent);
214     RSB_EXPORT static pid_t GetSubstitutingPid();
215 
216 private:
217     RSB_EXPORT static void SetTransactionTimeCorrection(double replayStartTime, double recordStartTime);
218     RSB_EXPORT static void TimePauseAt(uint64_t curTime, uint64_t newPauseAfterTime);
219     RSB_EXPORT static void TimePauseResume(uint64_t curTime);
220     RSB_EXPORT static void TimePauseClear();
221 
222     RSB_EXPORT static std::shared_ptr<RSDisplayRenderNode> GetDisplayNode(const RSContext& context);
223     RSB_EXPORT static Vector4f GetScreenRect(const RSContext& context);
224 
225     // RSRenderNodeMap
226     RSB_EXPORT static void FilterForPlayback(RSContext& context, pid_t pid);
227     RSB_EXPORT static void FilterMockNode(RSContext& context);
228 
229     RSB_EXPORT static void GetSurfacesTrees(
230         const RSContext& context, std::map<std::string, std::tuple<NodeId, std::string>>& list);
231     RSB_EXPORT static void GetSurfacesTrees(const RSContext& context, pid_t pid, std::map<NodeId, std::string>& list);
232     RSB_EXPORT static size_t GetRenderNodeCount(const RSContext& context);
233     RSB_EXPORT static NodeId GetRandomSurfaceNode(const RSContext& context);
234 
235     RSB_EXPORT static void MarshalNodes(const RSContext& context, std::stringstream& data);
236     RSB_EXPORT static void MarshalTree(const RSRenderNode* node, std::stringstream& data);
237     RSB_EXPORT static void MarshalNode(const RSRenderNode* node, std::stringstream& data);
238     RSB_EXPORT static void MarshalNode(const RSRenderNode& node, std::stringstream& data);
239 
240     RSB_EXPORT static void UnmarshalNodes(RSContext& context, std::stringstream& data, uint32_t fileVersion);
241     RSB_EXPORT static void UnmarshalTree(RSContext& context, std::stringstream& data, uint32_t fileVersion);
242     RSB_EXPORT static void UnmarshalNode(RSContext& context, std::stringstream& data, uint32_t fileVersion);
243     RSB_EXPORT static void UnmarshalNode(
244         RSContext& context, std::stringstream& data, NodeId nodeId, uint32_t fileVersion);
245     RSB_EXPORT static void UnmarshalNode(RSRenderNode& node, std::stringstream& data, uint32_t fileVersion);
246 
247     // RSRenderNode
248     RSB_EXPORT static std::string DumpRenderProperties(const RSRenderNode& node);
249     RSB_EXPORT static std::string DumpModifiers(const RSRenderNode& node);
250     RSB_EXPORT static std::string DumpSurfaceNode(const RSRenderNode& node);
251 
252     // JSON
253     static void RenderServiceTreeDump(JsonWriter& outWrapper);
254     RSB_EXPORT static void DumpNode(const RSRenderNode& node, JsonWriter& outWrapper);
255     RSB_EXPORT static void DumpNodeBaseInfo(const RSRenderNode& node, JsonWriter& outWrapper);
256     RSB_EXPORT static void DumpNodeSubsurfaces(const RSRenderNode& node, JsonWriter& outWrapper);
257     RSB_EXPORT static void DumpNodeSubClassNode(const RSRenderNode& node, JsonWriter& outWrapper);
258     RSB_EXPORT static void DumpNodeOptionalFlags(const RSRenderNode& node, JsonWriter& outWrapper);
259     RSB_EXPORT static void DumpNodeDrawCmdModifiers(const RSRenderNode& node, JsonWriter& outWrapper);
260     RSB_EXPORT static void DumpNodeDrawCmdModifier(
261         const RSRenderNode& node, JsonWriter& outWrapper, int type, RSRenderModifier& modifier);
262     RSB_EXPORT static void DumpNodeProperties(const RSProperties& properties, JsonWriter& outWrapper);
263     RSB_EXPORT static void DumpNodePropertiesClip(const RSProperties& properties, JsonWriter& outWrapper);
264     RSB_EXPORT static void DumpNodePropertiesTransform(const RSProperties& properties, JsonWriter& outWrapper);
265     RSB_EXPORT static void DumpNodePropertiesDecoration(const RSProperties& properties, JsonWriter& outWrapper);
266     RSB_EXPORT static void DumpNodePropertiesEffects(const RSProperties& properties, JsonWriter& outWrapper);
267     RSB_EXPORT static void DumpNodePropertiesShadow(const RSProperties& properties, JsonWriter& outWrapper);
268     RSB_EXPORT static void DumpNodePropertiesColor(const RSProperties& properties, JsonWriter& outWrapper);
269     RSB_EXPORT static void DumpNodeAnimations(const RSAnimationManager& animationManager, JsonWriter& outWrapper);
270     RSB_EXPORT static void DumpNodeAnimation(const RSRenderAnimation& animation, JsonWriter& outWrapper);
271     RSB_EXPORT static void DumpNodeChildrenListUpdate(const RSRenderNode& node, JsonWriter& outWrapper);
272 
273     // RSAnimationManager
274     RSB_EXPORT static void FilterAnimationForPlayback(RSAnimationManager& manager);
275 
276     RSB_EXPORT static NodeId PatchPlainNodeId(const Parcel& parcel, NodeId id);
277     RSB_EXPORT static pid_t PatchPlainPid(const Parcel& parcel, pid_t pid);
278 
279     RSB_EXPORT static int PerfTreeFlatten(
280         const RSRenderNode& node, std::unordered_set<NodeId>& nodeSet, std::unordered_map<NodeId, int>& mapNode2Count);
281 
282     static uint64_t RawNowNano();
283     static uint64_t NowNano();
284     static double Now();
285 
286     static bool IsRecording();
287     static bool IsPlaying();
288 
289     static bool IsLoadSaveFirstScreenInProgress();
290     static std::string FirstFrameMarshalling();
291     static void FirstFrameUnmarshalling(const std::string& data, uint32_t fileVersion);
292     static void HiddenSpaceTurnOff();
293     static void HiddenSpaceTurnOn();
294 
295     static void ScheduleTask(std::function<void()> && task);
296     static void RequestNextVSync();
297     static void AwakeRenderServiceThread();
298     static void AwakeRenderServiceThreadResetCaches();
299     static void ResetAnimationStamp();
300 
301     static void CreateMockConnection(pid_t pid);
302     static RSRenderServiceConnection* GetConnection(pid_t pid);
303     static pid_t GetConnectionPid(RSIRenderServiceConnection* connection);
304     static std::vector<pid_t> GetConnectionsPids();
305 
306     static std::shared_ptr<RSRenderNode> GetRenderNode(uint64_t id);
307     static void ProcessSendingRdc();
308 
309     static void CalcPerfNodeAllStep();
310     static void CalcNodeWeigthOnFrameEnd(uint64_t frameLength);
311 
312     RSB_EXPORT static uint32_t GetNodeDepth(const std::shared_ptr<RSRenderNode> node);
313 
314     // Network interface
315     using Command = void (*)(const ArgList&);
316     static Command GetCommand(const std::string& command);
317     static void ProcessCommands();
318     static void Respond(const std::string& message);
319     static void SetSystemParameter(const ArgList& args);
320     static void GetSystemParameter(const ArgList& args);
321     static void DumpSystemParameters(const ArgList& args);
322     static void DumpNodeModifiers(const ArgList& args);
323     static void DumpConnections(const ArgList& args);
324     static void DumpNodeProperties(const ArgList& args);
325     static void DumpTree(const ArgList& args);
326     static void DumpTreeToJson(const ArgList& args);
327     static void DumpSurfaces(const ArgList& args);
328     static void DumpNodeSurface(const ArgList& args);
329     static void PatchNode(const ArgList& args);
330     static void KillNode(const ArgList& args);
331     static void AttachChild(const ArgList& args);
332     static void KillPid(const ArgList& args);
333     static void GetRoot(const ArgList& args);
334     static void GetDeviceInfo(const ArgList& args);
335     static void GetDeviceFrequency(const ArgList& args);
336     static void FixDeviceEnv(const ArgList& args);
337     static void GetPerfTree(const ArgList& args);
338     static void CalcPerfNode(const ArgList& args);
339     static void CalcPerfNodeAll(const ArgList& args);
340     static void SocketShutdown(const ArgList& args);
341 
342     static void Version(const ArgList& args);
343     static void FileVersion(const ArgList& args);
344 
345     static void SaveSkp(const ArgList& args);
346     static void SaveRdc(const ArgList& args);
347 
348     static void RecordStart(const ArgList& args);
349     static void RecordStop(const ArgList& args);
350     static void RecordUpdate();
351 
352     static void PlaybackStart(const ArgList& args);
353     static void PlaybackStop(const ArgList& args);
354     static void PlaybackUpdate();
355 
356     static void PlaybackPrepare(const ArgList& args);
357     static void PlaybackPrepareFirstFrame(const ArgList& args);
358     static void PlaybackPause(const ArgList& args);
359     static void PlaybackPauseAt(const ArgList& args);
360     static void PlaybackPauseClear(const ArgList& args);
361     static void PlaybackResume(const ArgList& args);
362 
363     static void TestSaveFrame(const ArgList& args);
364     static void TestLoadFrame(const ArgList& args);
365     static void TestSwitch(const ArgList& args);
366     // set to true in DT only
367     RSB_EXPORT static bool testing_;
368 };
369 
370 } // namespace OHOS::Rosen
371 
372 #endif // RS_PROFILER_ENABLED
373 
374 #endif // RENDER_SERVICE_PROFILER_H