1 /*
2 * Copyright (c) 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 #include "benchmarks/rs_recording_thread.h"
17
18 #include "benchmarks/file_utils.h"
19 #include "common/rs_thread_handler.h"
20 #include "message_parcel.h"
21 #include "platform/common/rs_system_properties.h"
22 #include "platform/common/rs_log.h"
23 #include "rs_trace.h"
24
25 namespace OHOS::Rosen {
26
Instance()27 RSRecordingThread &RSRecordingThread::Instance()
28 {
29 static RSRecordingThread instance;
30 return instance;
31 }
32
Start()33 void RSRecordingThread::Start()
34 {
35 runner_ = AppExecFwk::EventRunner::Create("RRecordingThread");
36 handler_ = std::make_shared<AppExecFwk::EventHandler>(runner_);
37 }
38
PostTask(const std::function<void ()> & task)39 void RSRecordingThread::PostTask(const std::function<void()> &task)
40 {
41 if (handler_) {
42 handler_->PostTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
43 }
44 }
45
CheckAndRecording()46 bool RSRecordingThread::CheckAndRecording()
47 {
48 if (!handler_) {
49 RS_LOGE("RSRecordingThread::CheckAndRecording handler_ is nullptr");
50 return false;
51 }
52 RSTaskMessage::RSTask task = [this]() {
53 std::string line = "RSRecordingThread::CheckAndRecording curDumpFrame = " + std::to_string(curDumpFrame_) +
54 ", dumpFrameNum = " + std::to_string(dumpFrameNum_);
55 RS_LOGD(line.c_str());
56 isRecordingEnabled_ = RSSystemProperties::GetRecordingEnabled();
57 // init curDumpFrame
58 if (isRecordingEnabled_ && curDumpFrame_ == 0) {
59 RS_TRACE_NAME(line);
60 dumpFrameNum_ = RSSystemProperties::GetDumpFrameNum();
61 fileDir_ = RSSystemProperties::GetRecordingFile();
62 if (access(fileDir_.c_str(), F_OK) != 0) {
63 mkdir(fileDir_.c_str(), (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH));
64 }
65 }
66 };
67 PostTask(task);
68 return isRecordingEnabled_;
69 }
70
FinishRecordingOneFrame()71 void RSRecordingThread::FinishRecordingOneFrame()
72 {
73 std::string line = "RSRecordingThread::FinishRecordingOneFrame curDumpFrame = " + std::to_string(curDumpFrame_) +
74 ", dumpFrameNum = " + std::to_string(dumpFrameNum_);
75 RS_LOGD(line.c_str());
76 RS_TRACE_NAME(line);
77 if (curDumpFrame_ < dumpFrameNum_) {
78 curDumpFrame_++;
79 } else {
80 isRecordingEnabled_ = false;
81 curDumpFrame_ = 0;
82 dumpFrameNum_ = 0;
83 fileDir_ = "";
84 RSSystemProperties::SetRecordingDisenabled();
85 RS_LOGD("RSRecordingThread::FinishRecordingOneFrame isRecordingEnabled = false");
86 }
87 }
88
89 #ifndef USE_ROSEN_DRAWING
RecordingToFile(const std::shared_ptr<DrawCmdList> & drawCmdList)90 void RSRecordingThread::RecordingToFile(const std::shared_ptr<DrawCmdList>& drawCmdList)
91 #else
92 void RSRecordingThread::RecordingToFile(const std::shared_ptr<Drawing::DrawCmdList>& drawCmdList)
93 #endif
94 {
95 if (curDumpFrame_ < 0) {
96 return;
97 }
98 int tmpCurDumpFrame = curDumpFrame_;
99 #ifndef USE_ROSEN_DRAWING
100 std::shared_ptr<MessageParcel> messageParcel = std::make_shared<MessageParcel>();
101 messageParcel->SetMaxCapacity(RECORDING_PARCEL_MAX_CAPCITY);
102 drawCmdList->Marshalling(*messageParcel);
103 #else
104 auto cmdListData = drawCmdList->GetData();
105 auto messageParcel = std::make_shared<Drawing::Data>();
106 messageParcel->BuildWithCopy(cmdListData.first, cmdListData.second);
107 #endif
108 FinishRecordingOneFrame();
109 RSTaskMessage::RSTask task = [this, tmpCurDumpFrame, drawCmdList, messageParcel]() {
110 std::string line = "RSRecordingThread::RecordingToFile curDumpFrame = " + std::to_string(curDumpFrame_) +
111 ", dumpFrameNum = " + std::to_string(dumpFrameNum_);
112 RS_LOGD(line.c_str());
113 RS_TRACE_NAME(line);
114 // file name
115 std::string drawCmdListFile = fileDir_ + "/frame" + std::to_string(tmpCurDumpFrame) + ".drawing";
116 std::string opsFile = fileDir_ + "/ops_frame" + std::to_string(tmpCurDumpFrame) + ".txt";
117 // get data
118 #ifndef USE_ROSEN_DRAWING
119 size_t sz = messageParcel->GetDataSize();
120 uintptr_t buf = messageParcel->GetData();
121 std::string opsDescription = drawCmdList->GetOpsWithDesc();
122 #else
123 size_t sz = messageParcel->GetSize();
124 auto buf = reinterpret_cast<uintptr_t>(messageParcel->GetData());
125 std::string opsDescription = "drawing ops no description";
126 #endif
127
128 OHOS::Rosen::Benchmarks::WriteToFile(buf, sz, drawCmdListFile);
129 OHOS::Rosen::Benchmarks::WriteStringToFile(opsDescription, opsFile);
130 };
131 PostTask(task);
132 }
133 }
134