• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 RS_PROFILER_FILE_H
17 #define RS_PROFILER_FILE_H
18 
19 #include <mutex>
20 
21 #include "rs_profiler_capturedata.h"
22 #include "rs_profiler_utils.h"
23 
24 #ifdef RENDER_PROFILER_APPLICATION
25 #include <memory>
26 
27 #include "rs_adapt.h"
28 #endif
29 
30 namespace OHOS::Rosen {
31 
32 #define RSFILE_VERSION_RENDER_METRICS_ADDED 0x240701
33 #define RSFILE_VERSION_RENDER_ANIMESTARTTIMES_ADDED 0x240723
34 #define RSFILE_VERSION_RENDER_TYPEFACE_FIX 0x240801
35 #define RSFILE_VERSION_ISREPAINT_BOUNDARY 0x250516
36 #define RSFILE_VERSION_LOG_EVENTS_ADDED 0x250519
37 #define RSFILE_VERSION_LATEST RSFILE_VERSION_LOG_EVENTS_ADDED
38 
39 struct RSFileLayer final {
40     std::pair<uint32_t, uint32_t> layerHeader; // to put in GLOBAL HEADER
41 
42     RSCaptureData property;
43 
44     using TrackMarkup = std::vector<std::pair<uint32_t, uint32_t>>;
45     static constexpr size_t MARKUP_SIZE = sizeof(TrackMarkup::value_type);
46 
47     TrackMarkup rsData;
48     TrackMarkup oglData;
49     TrackMarkup rsMetrics;
50     TrackMarkup oglMetrics;
51     TrackMarkup gfxMetrics;
52     TrackMarkup renderMetrics;
53     TrackMarkup logEvents;
54 
55     uint32_t readindexRsData = 0;
56     uint32_t readindexOglData = 0;
57     uint32_t readindexRsMetrics = 0;
58     uint32_t readindexOglMetrics = 0;
59     uint32_t readindexGfxMetrics = 0;
60     uint32_t readindexRenderMetrics = 0;
61     uint32_t readindexLogEvents = 0;
62 };
63 
64 class RSFile final {
65 public:
66     RSFile();
67 
68     bool Create(const std::string& fname);
69     bool Open(const std::string& fname);
70 
71     bool IsOpen() const;
72 
73     void SetWriteTime(double time);
74     double GetWriteTime() const;
75 
76     const std::string& GetHeaderFirstFrame() const;
77     void AddHeaderFirstFrame(const std::string& dataFirstFrame);
78 
79     const std::vector<std::pair<uint64_t, int64_t>>& GetAnimeStartTimes() const;
80     void AddAnimeStartTimes(const std::vector<std::pair<uint64_t, int64_t>>& startTimes);
81 
82     void AddHeaderPid(pid_t pid);
83     const std::vector<pid_t>& GetHeaderPids() const;
84 
85     uint32_t AddLayer();
86     void LayerAddHeaderProperty(uint32_t layer, const std::string& name, const std::string& value);
87 
88     void UnwriteRSData();
89 
90     void WriteRSData(double time, const void* data, size_t size);
91     void WriteOGLData(uint32_t layer, double time, const void* data, size_t size);
92     void WriteRSMetrics(uint32_t layer, double time, const void* data, size_t size);
93     void WriteRenderMetrics(uint32_t layer, double time, const void* data, size_t size);
94     void WriteOGLMetrics(uint32_t layer, double time, uint32_t frame, const void* data, size_t size);
95     void WriteGFXMetrics(uint32_t layer, double time, uint32_t frame, const void* data, size_t size);
96     void WriteLogEvent(uint32_t layer, double time, const void* data, size_t size);
97 
98     void ReadRSDataRestart();
99     void ReadOGLDataRestart(uint32_t layer);
100     void ReadRSMetricsRestart(uint32_t layer);
101     void ReadRenderMetricsRestart(uint32_t layer);
102     void ReadOGLMetricsRestart(uint32_t layer);
103     void ReadGFXMetricsRestart(uint32_t layer);
104     void ReadLogEventRestart(uint32_t layer);
105 
106     bool RSDataEOF() const;
107     bool OGLDataEOF(uint32_t layer) const;
108     bool RSMetricsEOF(uint32_t layer) const;
109     bool RenderMetricsEOF(uint32_t layer) const;
110     bool OGLMetricsEOF(uint32_t layer) const;
111     bool GFXMetricsEOF(uint32_t layer) const;
112     bool LogEventEOF(uint32_t layer) const;
113 
114     bool ReadRSData(double untilTime, std::vector<uint8_t>& data, double& readTime);
115     bool ReadOGLData(double untilTime, uint32_t layer, std::vector<uint8_t>& data, double& readTime);
116     bool ReadRSMetrics(double untilTime, uint32_t layer, std::vector<uint8_t>& data, double& readTime);
117     bool ReadRenderMetrics(double untilTime, uint32_t layer, std::vector<uint8_t>& data, double& readTime);
118     bool ReadOGLMetrics(double untilTime, uint32_t layer, std::vector<uint8_t>& data, double& readTime);
119     bool ReadGFXMetrics(double untilTime, uint32_t layer, std::vector<uint8_t>& data, double& readTime);
120     bool ReadLogEvent(double untilTime, uint32_t layer, std::vector<uint8_t>& data, double& readTime);
121     bool GetDataCopy(std::vector<uint8_t>& data); // copy the content of RSFile so far
122 
123     bool HasLayer(uint32_t layer) const;
124 
125     void SetPreparedHeader(const std::vector<uint8_t>& headerData);
126     void GetPreparedHeader(std::vector<uint8_t>& headerData);
127     void SetPreparedHeaderMode(bool mode);
128     void Close();
129 
130     uint32_t GetVersion() const;
131     void SetVersion(uint32_t version);
132 
133     static const std::string& GetDefaultPath();
134 
135     void CacheVsyncId2Time(uint32_t layer);
136     int64_t GetClosestVsyncId(int64_t vsyncId);
137     double ConvertVsyncId2Time(int64_t vsyncId);
138     int64_t ConvertTime2VsyncId(double time) const;
139 
140 private:
141     void WriteHeaders();
142     void WriteHeader();
143     void LayerWriteHeader(uint32_t layer);
144 
145     template<typename Track>
LayerWriteHeaderOfTrack(const Track & track)146     void LayerWriteHeaderOfTrack(const Track& track)
147     {
148         uint32_t recordSize = track.size();
149         Utils::FileWrite(&recordSize, sizeof(recordSize), 1, file_);
150         for (auto item : track) {
151             Utils::FileWrite(&item.first, sizeof(item.first), 1, file_);
152             Utils::FileWrite(&item.second, sizeof(item.second), 1, file_);
153         }
154     }
155 
156     std::string ReadHeaders();
157     std::string ReadHeader();
158     bool ReadHeaderPidList();
159     bool ReadHeaderFirstScreen();
160     std::string LayerReadHeader(uint32_t layer);
161 
162     template<typename Track>
LayerReadHeaderOfTrack(Track & track)163     void LayerReadHeaderOfTrack(Track& track)
164     {
165         uint32_t recordSize;
166         Utils::FileRead(&recordSize, sizeof(recordSize), 1, file_);
167         track.resize(recordSize);
168         for (size_t i = 0; i < recordSize; i++) {
169             Utils::FileRead(&track[i].first, sizeof(track[i].first), 1, file_);
170             Utils::FileRead(&track[i].second, sizeof(track[i].second), 1, file_);
171         }
172     }
173 
174     using LayerTrackIndexPtr = uint32_t RSFileLayer::*;
175     using LayerTrackMarkupPtr = RSFileLayer::TrackMarkup RSFileLayer::*;
176     struct LayerTrackPtr {
177         LayerTrackIndexPtr index;
178         LayerTrackMarkupPtr markup;
179     };
180 
181     void UnwriteTrackData(LayerTrackMarkupPtr trackMarkup, uint32_t layer);
182     void WriteTrackData(LayerTrackMarkupPtr trackMarkup, uint32_t layer, double time, const void* data, size_t size);
183     bool ReadTrackData(
184         LayerTrackPtr track, double untilTime, uint32_t layer, std::vector<uint8_t>& data, double& readTime);
185     void ReadTrackDataRestart(LayerTrackIndexPtr trackIndex, uint32_t layer);
186     bool TrackEOF(LayerTrackPtr track, uint32_t layer) const;
187 
188 private:
189     FILE* file_ = nullptr;
190     uint32_t versionId_ = 0;
191     double writeStartTime_ = 0.0;
192     uint32_t headerOff_ = 0u;
193     std::vector<pid_t> headerPidList_;
194     std::vector<RSFileLayer> layerData_;
195     uint32_t writeDataOff_ = 0u; // last byte of file where we can continue writing
196     std::string headerFirstFrame_;
197     std::vector<std::pair<uint64_t, int64_t>> headerAnimeStartTimes_;
198     std::mutex writeMutex_;
199     bool wasChanged_ = false;
200     std::vector<uint8_t> preparedHeader_;
201     bool preparedHeaderMode_ = false;
202     std::map<int64_t, double> mapVsyncId2Time_;
203 
204     static constexpr size_t chunkSizeMax = 100'000'000;
205     static constexpr size_t headerSizeMax = 512u * 1024u * 1024u;
206 };
207 
208 } // namespace OHOS::Rosen
209 
210 #endif // RS_PROFILER_FILE_H