1 #include "profiletablemodel.h"
3 typedef trace::Profile::Call Call;
4 typedef trace::Profile::Frame Frame;
14 COLUMN_PIXELS_AVERAGE,
18 static QString columnNames[] = {
21 QString("Total GPU Time"),
22 QString("Total CPU Time"),
23 QString("Total Pixels Drawn"),
24 QString("Avg GPU Time"),
25 QString("Avg CPU Time"),
26 QString("Avg Pixels Drawn")
29 ProfileTableModel::ProfileTableModel(QObject *parent)
30 : QAbstractTableModel(parent),
32 m_sortColumn(COLUMN_GPU_TIME),
33 m_sortOrder(Qt::DescendingOrder)
38 void ProfileTableModel::setProfile(trace::Profile* profile)
41 m_timeMin = m_profile->frames.front().gpuStart;
42 m_timeMax = m_profile->frames.back().gpuStart + m_profile->frames.back().gpuDuration;
47 void ProfileTableModel::setTimeSelection(int64_t start, int64_t end)
52 sort(m_sortColumn, m_sortOrder);
57 * Creates the row data from trace profile
59 void ProfileTableModel::updateModel()
61 if (m_timeMin == m_timeMax) {
62 m_timeMin = m_profile->frames.front().gpuStart;
63 m_timeMax = m_profile->frames.back().gpuStart + m_profile->frames.back().gpuDuration;
66 for (QList<ProfileTableRow>::iterator itr = m_rowData.begin(); itr != m_rowData.end(); ++itr) {
67 ProfileTableRow& row = *itr;
73 row.longestCpu = NULL;
74 row.longestGpu = NULL;
75 row.longestPixel = NULL;
78 for (Frame::const_iterator itr = m_profile->frames.begin(); itr != m_profile->frames.end(); ++itr) {
79 const Frame& frame = *itr;
81 if (frame.gpuStart > m_timeMax) {
85 if ((frame.gpuStart + frame.gpuDuration) < m_timeMin) {
89 for (Call::const_iterator jtr = frame.calls.begin(); jtr != frame.calls.end(); ++jtr) {
90 const Call& call = *jtr;
92 if (call.gpuStart > m_timeMax) {
96 if ((call.gpuStart + call.gpuDuration) < m_timeMin) {
100 ProfileTableRow* row = getRow(call.program);
102 m_rowData.append(ProfileTableRow());
103 row = &m_rowData.back();
107 row->program = call.program;
108 row->gpuTime += call.gpuDuration;
109 row->cpuTime += call.cpuDuration;
110 row->pixels += call.pixels;
112 if (!row->longestGpu || row->longestGpu->gpuDuration < call.gpuDuration) {
113 row->longestGpu = &call;
116 if (!row->longestCpu || row->longestCpu->cpuDuration < call.cpuDuration) {
117 row->longestCpu = &call;
120 if (!row->longestPixel || row->longestPixel->pixels < call.pixels) {
121 row->longestPixel = &call;
129 * Get the appropriate call associated with an item in the table
131 const Call* ProfileTableModel::getJumpCall(const QModelIndex & index) const {
132 const ProfileTableRow& row = m_rowData[index.row()];
134 switch(index.column()) {
135 case COLUMN_GPU_TIME:
136 case COLUMN_GPU_AVERAGE:
137 return row.longestGpu;
138 case COLUMN_CPU_TIME:
139 case COLUMN_CPU_AVERAGE:
140 return row.longestCpu;
141 case COLUMN_PIXELS_DRAWN:
142 case COLUMN_PIXELS_AVERAGE:
143 return row.longestPixel;
150 ProfileTableRow* ProfileTableModel::getRow(unsigned program) {
151 for (QList<ProfileTableRow>::iterator itr = m_rowData.begin(); itr != m_rowData.end(); ++itr) {
152 if (itr->program == program)
160 int ProfileTableModel::rowCount(const QModelIndex & parent) const
162 if (!parent.isValid()) {
163 return m_rowData.size();
170 int ProfileTableModel::columnCount(const QModelIndex & /*parent*/) const
176 QVariant ProfileTableModel::data(const QModelIndex &index, int role) const
178 if (!index.isValid()) {
182 if (role == Qt::DisplayRole) {
183 const ProfileTableRow& row = m_rowData[index.row()];
185 switch(index.column()) {
190 case COLUMN_GPU_TIME:
192 case COLUMN_CPU_TIME:
194 case COLUMN_PIXELS_DRAWN:
196 case COLUMN_GPU_AVERAGE:
197 return (row.uses <= 0) ? 0 : (row.gpuTime / row.uses);
198 case COLUMN_CPU_AVERAGE:
199 return (row.uses <= 0) ? 0 : (row.cpuTime / row.uses);
200 case COLUMN_PIXELS_AVERAGE:
201 return (row.uses <= 0) ? 0 : (row.pixels / row.uses);
203 } else if (role == Qt::TextAlignmentRole) {
204 return Qt::AlignRight;
211 QVariant ProfileTableModel::headerData(int section, Qt::Orientation orientation, int role) const
213 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
214 if (section >= 0 && section < MAX_COLUMN) {
215 return columnNames[section];
223 class ProgramSorter {
225 ProgramSorter(int column, Qt::SortOrder order)
226 : mSortColumn(column),
231 bool operator()(const ProfileTableRow &p1, const ProfileTableRow &p2) const
235 switch(mSortColumn) {
237 result = p1.program < p2.program;
240 result = p1.uses < p2.uses;
242 case COLUMN_GPU_TIME:
243 result = p1.gpuTime < p2.gpuTime;
245 case COLUMN_CPU_TIME:
246 result = p1.cpuTime < p2.cpuTime;
248 case COLUMN_PIXELS_DRAWN:
249 result = p1.pixels < p2.pixels;
251 case COLUMN_GPU_AVERAGE:
252 result = ((p1.uses <= 0) ? 0 : (p1.gpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.gpuTime / p2.uses));
254 case COLUMN_CPU_AVERAGE:
255 result = ((p1.uses <= 0) ? 0 : (p1.cpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.cpuTime / p2.uses));
257 case COLUMN_PIXELS_AVERAGE:
258 result = ((p1.uses <= 0) ? 0 : (p1.pixels / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.pixels / p2.uses));
262 if (mSortOrder == Qt::DescendingOrder) {
271 Qt::SortOrder mSortOrder;
275 void ProfileTableModel::sort(int column, Qt::SortOrder order) {
276 m_sortColumn = column;
278 qSort(m_rowData.begin(), m_rowData.end(), ProgramSorter(column, order));
279 emit dataChanged(createIndex(0, 0), createIndex(m_rowData.size(), MAX_COLUMN));
283 #include "profiletablemodel.moc"