-
Notifications
You must be signed in to change notification settings - Fork 532
Expand file tree
/
Copy pathPAGRunTimeDataModel.cpp
More file actions
334 lines (300 loc) · 12 KB
/
Copy pathPAGRunTimeDataModel.cpp
File metadata and controls
334 lines (300 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////
#include "PAGRunTimeDataModel.h"
#include <cmath>
#include "base/utils/TimeUtil.h"
#include "pag/pag.h"
namespace pag {
FrameTimeMetrics::FrameTimeMetrics(int64_t renderTime, int64_t presentTime, int64_t imageDecodeTime)
: renderTime(renderTime), presentTime(presentTime), imageDecodeTime(imageDecodeTime) {
}
PAGRunTimeDataModel::PAGRunTimeDataModel(QObject* parent) : QObject(parent) {
}
QString PAGRunTimeDataModel::getTotalFrame() const {
return QString::number(totalFrame);
}
QString PAGRunTimeDataModel::getCurrentFrame() const {
return QString::number(currentFrame);
}
int PAGRunTimeDataModel::getChartDataSize() const {
return chartDataSize;
}
const PAGFileInfoModel* PAGRunTimeDataModel::getFileInfoModel() const {
return &fileInfoModel;
}
const PAGChartDataModel* PAGRunTimeDataModel::getChartDataModel() const {
return &chartDataModel;
}
const PAGFrameDisplayInfoModel* PAGRunTimeDataModel::getFrameDisplayInfoModel() const {
return &frameDisplayInfoModel;
}
const PAGNodeStatsModel* PAGRunTimeDataModel::getNodeStatsModel() const {
return &nodeStatsModel;
}
bool PAGRunTimeDataModel::hasFrameTimeline() const {
return frameModeEnabled;
}
void PAGRunTimeDataModel::setCurrentFrame(const QString& currentFrame) {
if (this->currentFrame == currentFrame.toLongLong()) {
return;
}
this->currentFrame = currentFrame.toLongLong();
Q_EMIT dataChanged();
}
void PAGRunTimeDataModel::setChartDataSize(int chartDataSize) {
if (this->chartDataSize == chartDataSize) {
return;
}
this->chartDataSize = chartDataSize;
if (chartDataSize > 0) {
refreshChartDataModel();
}
}
void PAGRunTimeDataModel::updateData(int64_t currentFrame, int64_t renderTime, int64_t presentTime,
int64_t imageDecodeTime) {
if (currentFrame < 0 || currentFrame >= totalFrame || this->currentFrame == currentFrame) {
return;
}
this->currentFrame = currentFrame;
while (frameTimeMetricsVector.size() <= currentFrame) {
frameTimeMetricsVector.push_back(FrameTimeMetrics(0, 0, 0));
}
frameTimeMetricsVector[currentFrame] = FrameTimeMetrics(renderTime, presentTime, imageDecodeTime);
updateFrameDisplayInfo(renderTime, presentTime, imageDecodeTime);
updateChartData();
Q_EMIT dataChanged();
}
void PAGRunTimeDataModel::setPAGFile(std::shared_ptr<PAGFile> pagFile) {
if (pagFile == nullptr) {
frameModeEnabled = false;
totalFrame = 0;
currentFrame = -1;
lastUpdatedFrame = -1;
chartDataModel.clearItems();
frameTimeMetricsVector.clear();
fileInfoModel.setPAGFile(nullptr);
nodeStatsModel.setPAGXDocument(nullptr);
updateFrameDisplayInfo(0, 0, 0);
Q_EMIT fileInfoModelChanged();
Q_EMIT nodeStatsModelChanged();
Q_EMIT hasFrameTimelineChanged();
Q_EMIT dataChanged();
return;
}
frameModeEnabled = true;
totalFrame = TimeToFrame(pagFile->duration(), pagFile->frameRate());
currentFrame = -1;
lastUpdatedFrame = -1;
chartDataModel.clearItems();
frameTimeMetricsVector.clear();
frameTimeMetricsVector.reserve(totalFrame);
fileInfoModel.setPAGFile(pagFile);
nodeStatsModel.setPAGXDocument(nullptr);
updateFrameDisplayInfo(0, 0, 0);
Q_EMIT fileInfoModelChanged();
Q_EMIT nodeStatsModelChanged();
Q_EMIT hasFrameTimelineChanged();
Q_EMIT dataChanged();
}
void PAGRunTimeDataModel::setPAGXDocument(std::shared_ptr<pagx::PAGXDocument> pagxDocument) {
if (pagxDocument != nullptr && !pagxDocument->animations.empty()) {
// The animations vector may contain Animation or StateMachine entries; only Animation exposes
// frameRate and duration fields.
auto* firstAnim = pagxDocument->animations[0];
if (firstAnim != nullptr && firstAnim->nodeType() == pagx::NodeType::Animation) {
auto* anim = static_cast<pagx::Animation*>(firstAnim);
// Derive totalFrame from the microsecond duration using the same two-step rounding as the
// render path (PAGTimeline::duration truncates frames→microseconds, then frames are recovered
// via round). Using anim->duration directly would diverge by up to one frame at non-integer
// frame rates, causing the render-side clamped last frame to be dropped by updateData's guard.
if (anim->frameRate > 0.0f && anim->duration > 0) {
auto durationUs = static_cast<int64_t>(static_cast<double>(anim->duration) * 1000000.0 /
static_cast<double>(anim->frameRate));
totalFrame = static_cast<int64_t>(std::round(
static_cast<double>(durationUs) * static_cast<double>(anim->frameRate) / 1000000.0));
} else {
totalFrame = 0;
}
if (totalFrame < 1) {
totalFrame = 1;
}
} else {
// A StateMachine-first document has no single duration/frameRate to derive a frame count
// from; fall back to a single frame so frame mode does not run with a stale/invalid value,
// matching PAGXViewModel::updateAnimationState()'s no-animation fallback.
totalFrame = 1;
}
frameModeEnabled = true;
} else {
totalFrame = pagxDocument ? 1 : 0;
frameModeEnabled = false;
}
currentFrame = pagxDocument ? 0 : -1;
lastUpdatedFrame = -1;
chartDataModel.clearItems();
frameTimeMetricsVector.clear();
fileInfoModel.setPAGXDocument(pagxDocument);
nodeStatsModel.setPAGXDocument(pagxDocument);
updateFrameDisplayInfo(0, 0, 0);
Q_EMIT fileInfoModelChanged();
Q_EMIT nodeStatsModelChanged();
Q_EMIT hasFrameTimelineChanged();
Q_EMIT dataChanged();
}
void PAGRunTimeDataModel::updatePAGXRenderTime(int64_t renderTime, int64_t imageTime,
int64_t presentTime) {
FrameDisplayInfo renderInfo("Render", "#0096D8", renderTime, renderTime, renderTime);
FrameDisplayInfo presentInfo("Present", "#DDB259", presentTime, presentTime, presentTime);
FrameDisplayInfo imageDecodeInfo("Image", "#74AD59", imageTime, imageTime, imageTime);
frameDisplayInfoModel.updateData(renderInfo, imageDecodeInfo, presentInfo);
Q_EMIT frameDisplayInfoModelChanged();
}
void PAGRunTimeDataModel::updateMetrics(int64_t renderTime, int64_t presentTime,
int64_t imageDecodeTime, int64_t currentFrame) {
if (currentFrame == -1) {
updatePAGXRenderTime(renderTime, imageDecodeTime, presentTime);
} else {
updateData(currentFrame, renderTime, presentTime, imageDecodeTime);
}
}
void PAGRunTimeDataModel::updateChartData() {
if (totalFrame == 0) {
return;
}
if (chartDataSize == 0) {
return;
}
double ratio = static_cast<double>(chartDataSize) / totalFrame;
int64_t startFrame = lastUpdatedFrame + 1;
int64_t endFrame = currentFrame;
for (int64_t frame = startFrame; frame <= endFrame; ++frame) {
auto chartDataIndex = static_cast<int64_t>(frame * ratio);
auto frameStartIndex =
static_cast<int64_t>(std::floor(static_cast<double>(chartDataIndex) / ratio));
auto frameEndIndex =
static_cast<int64_t>(std::ceil(static_cast<double>((chartDataIndex + 1)) / ratio));
if (frameEndIndex < (frameStartIndex + 1)) {
frameEndIndex = frameStartIndex + 1;
}
int64_t count = 0;
int64_t renderTime = 0;
int64_t presentTime = 0;
int64_t imageDecodeTime = 0;
for (int64_t i = frameStartIndex; i < frameEndIndex && i < frameTimeMetricsVector.size(); ++i) {
renderTime += frameTimeMetricsVector[i].renderTime;
presentTime += frameTimeMetricsVector[i].presentTime;
imageDecodeTime += frameTimeMetricsVector[i].imageDecodeTime;
count++;
}
if (count > 0) {
renderTime /= count;
presentTime /= count;
imageDecodeTime /= count;
}
PAGCharDataItem item;
item.renderTime = renderTime;
item.presentTime = presentTime;
item.imageDecodeTime = imageDecodeTime;
int64_t endChartDataIndex = static_cast<int64_t>((frame + 1) * ratio);
for (int64_t i = chartDataIndex; i < endChartDataIndex; ++i) {
chartDataModel.updateOrInsertItem(i, &item);
}
}
lastUpdatedFrame = currentFrame;
}
void PAGRunTimeDataModel::refreshChartDataModel() {
if (totalFrame == 0 || frameTimeMetricsVector.isEmpty()) {
return;
}
int64_t maxIndex = frameTimeMetricsVector.size() - 1;
int64_t lastChartDataIndex = 0;
int64_t count = 0;
int64_t renderTime = 0;
int64_t presentTime = 0;
int64_t imageDecodeTime = 0;
PAGChartDataModel newChartDataModel;
double ratio = static_cast<double>(chartDataSize) / totalFrame;
for (int64_t i = 0; i <= maxIndex; i++) {
int64_t currentChartDataIndex = std::floor(i * ratio);
if (currentChartDataIndex != lastChartDataIndex && count != 0) {
PAGCharDataItem item;
item.renderTime = renderTime / count;
item.presentTime = presentTime / count;
item.imageDecodeTime = imageDecodeTime / count;
while (lastChartDataIndex != currentChartDataIndex) {
newChartDataModel.updateOrInsertItem(lastChartDataIndex, &item);
++lastChartDataIndex;
}
renderTime = 0;
presentTime = 0;
imageDecodeTime = 0;
count = 0;
}
auto& item = frameTimeMetricsVector[i];
renderTime += item.renderTime;
presentTime += item.presentTime;
imageDecodeTime += item.imageDecodeTime;
count++;
}
if (count > 0) {
int currentChartDataIndex = std::floor(maxIndex * ratio);
currentChartDataIndex = currentChartDataIndex == lastChartDataIndex ? lastChartDataIndex + 1
: currentChartDataIndex;
PAGCharDataItem item;
item.renderTime = renderTime / count;
item.presentTime = presentTime / count;
item.imageDecodeTime = imageDecodeTime / count;
while (lastChartDataIndex != currentChartDataIndex) {
newChartDataModel.updateOrInsertItem(lastChartDataIndex, &item);
++lastChartDataIndex;
}
}
chartDataModel.resetItems(&newChartDataModel);
}
void PAGRunTimeDataModel::updateFrameDisplayInfo(int64_t renderTime, int64_t presentTime,
int64_t imageDecodeTime) {
int64_t renderAvg = 0;
int64_t renderMax = 0;
int64_t renderTotal = 0;
int64_t presentAvg = 0;
int64_t presentMax = 0;
int64_t presentTotal = 0;
int64_t imageDecodeAvg = 0;
int64_t imageDecodeMax = 0;
int64_t imageDecodeTotal = 0;
int64_t size = static_cast<int64_t>(frameTimeMetricsVector.size());
if (size > 0) {
for (auto& item : frameTimeMetricsVector) {
renderTotal += item.renderTime;
renderMax = std::max(item.renderTime, renderMax);
presentTotal += item.presentTime;
presentMax = std::max(item.presentTime, presentMax);
imageDecodeTotal += item.imageDecodeTime;
imageDecodeMax = std::max(item.imageDecodeTime, imageDecodeMax);
}
renderAvg = renderTotal / size;
presentAvg = presentTotal / size;
imageDecodeAvg = imageDecodeTotal / size;
}
FrameDisplayInfo renderInfo("Render", "#0096D8", renderTime, renderAvg, renderMax);
FrameDisplayInfo presentInfo("Present", "#DDB259", presentTime, presentAvg, presentMax);
FrameDisplayInfo imageDecodeInfo("Image", "#74AD59", imageDecodeTime, imageDecodeAvg,
imageDecodeMax);
frameDisplayInfoModel.updateData(renderInfo, imageDecodeInfo, presentInfo);
}
} // namespace pag