1 #include "profiletablemodel.h"
2 #include "profiledialog.h"
7 typedef trace::Profile::Call Call;
8 typedef trace::Profile::Frame Frame;
9 typedef trace::Profile::Program Program;
19 COLUMN_PIXELS_AVERAGE,
23 static QString columnNames[] = {
26 QString("Total GPU Time"),
27 QString("Total CPU Time"),
28 QString("Total Pixels Drawn"),
29 QString("Avg GPU Time"),
30 QString("Avg CPU Time"),
31 QString("Avg Pixels Drawn")
34 ProfileTableModel::ProfileTableModel(QObject *parent)
35 : QAbstractTableModel(parent),
37 m_sortColumn(COLUMN_GPU_TIME),
38 m_sortOrder(Qt::DescendingOrder)
43 void ProfileTableModel::setProfile(trace::Profile* profile)
46 m_timeMin = m_profile->frames.front().cpuStart;
47 m_timeMax = m_profile->frames.back().cpuStart + m_profile->frames.back().cpuDuration;
53 * Set selection to nothing
55 void ProfileTableModel::selectNone()
57 m_timeMin = m_timeMax = 0;
59 sort(m_sortColumn, m_sortOrder);
64 * Set selection to program
66 void ProfileTableModel::selectProgram(unsigned /*program*/)
68 /* We have no program based selection for table */
74 * Set selection to a period of time
76 void ProfileTableModel::selectTime(int64_t start, int64_t end)
82 sort(m_sortColumn, m_sortOrder);
87 * Creates the row data from trace profile
89 void ProfileTableModel::updateModel()
91 if (m_timeMin == m_timeMax) {
92 m_timeMin = m_profile->frames.front().cpuStart;
93 m_timeMax = m_profile->frames.back().cpuStart + m_profile->frames.back().cpuDuration;
96 for (QList<ProfileTableRow>::iterator itr = m_rowData.begin(); itr != m_rowData.end(); ++itr) {
97 ProfileTableRow& row = *itr;
103 row.longestCpu = NULL;
104 row.longestGpu = NULL;
105 row.longestPixel = NULL;
108 for (std::vector<Program>::const_iterator itr = m_profile->programs.begin(); itr != m_profile->programs.end(); ++itr) {
109 ProfileTableRow* row = NULL;
110 const Program& program = *itr;
112 for (std::vector<unsigned>::const_iterator jtr = program.calls.begin(); jtr != program.calls.end(); ++jtr) {
113 const Call& call = m_profile->calls[*jtr];
115 if (call.cpuStart > m_timeMax) {
119 if (call.cpuStart + call.cpuDuration < m_timeMin) {
124 row = getRow(itr - m_profile->programs.begin());
128 row->pixels += call.pixels;
129 row->gpuTime += call.gpuDuration;
130 row->cpuTime += call.cpuDuration;
132 if (!row->longestGpu || row->longestGpu->gpuDuration < call.gpuDuration) {
133 row->longestGpu = &call;
136 if (!row->longestCpu || row->longestCpu->cpuDuration < call.cpuDuration) {
137 row->longestCpu = &call;
140 if (!row->longestPixel || row->longestPixel->pixels < call.pixels) {
141 row->longestPixel = &call;
149 * Get the appropriate call associated with an item in the table
151 const trace::Profile::Call *ProfileTableModel::getJumpCall(const QModelIndex & index) const {
152 const ProfileTableRow& row = m_rowData[index.row()];
154 switch(index.column()) {
155 case COLUMN_GPU_TIME:
156 case COLUMN_GPU_AVERAGE:
157 return row.longestGpu;
158 case COLUMN_CPU_TIME:
159 case COLUMN_CPU_AVERAGE:
160 return row.longestCpu;
161 case COLUMN_PIXELS_DRAWN:
162 case COLUMN_PIXELS_AVERAGE:
163 return row.longestPixel;
171 * Get the shader program associated with an item in the table
173 unsigned ProfileTableModel::getProgram(const QModelIndex & index) const
175 const ProfileTableRow& row = m_rowData[index.row()];
181 * Get the row index for a shader program
183 int ProfileTableModel::getRowIndex(unsigned program) const
185 for (int i = 0; i < m_rowData.size(); ++i) {
186 if (m_rowData[i].program == program)
195 * Get the row data for a shader program
197 ProfileTableRow* ProfileTableModel::getRow(unsigned program) {
198 for (QList<ProfileTableRow>::iterator itr = m_rowData.begin(); itr != m_rowData.end(); ++itr) {
199 if (itr->program == program)
203 m_rowData.append(ProfileTableRow(program));
204 return &m_rowData.back();
208 int ProfileTableModel::rowCount(const QModelIndex & parent) const
210 if (!parent.isValid()) {
211 return m_rowData.size();
218 int ProfileTableModel::columnCount(const QModelIndex & /*parent*/) const
224 QVariant ProfileTableModel::data(const QModelIndex &index, int role) const
226 if (!index.isValid()) {
230 if (role == Qt::DisplayRole) {
231 const ProfileTableRow& row = m_rowData[index.row()];
233 switch(index.column()) {
237 return QLocale::system().toString(row.uses);
238 case COLUMN_GPU_TIME:
239 return Profiling::getTimeString(row.gpuTime);
240 case COLUMN_CPU_TIME:
241 return Profiling::getTimeString(row.cpuTime);
242 case COLUMN_PIXELS_DRAWN:
243 return QLocale::system().toString((qlonglong)row.pixels);
244 case COLUMN_GPU_AVERAGE:
245 return Profiling::getTimeString((row.uses <= 0) ? 0 : (row.gpuTime / row.uses));
246 case COLUMN_CPU_AVERAGE:
247 return Profiling::getTimeString((row.uses <= 0) ? 0 : (row.cpuTime / row.uses));
248 case COLUMN_PIXELS_AVERAGE:
249 return QLocale::system().toString((row.uses <= 0) ? 0 : (row.pixels / row.uses));
251 } else if (role == Qt::TextAlignmentRole) {
252 return Qt::AlignRight;
259 QVariant ProfileTableModel::headerData(int section, Qt::Orientation orientation, int role) const
261 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
262 if (section >= 0 && section < MAX_COLUMN) {
263 return columnNames[section];
271 class ProgramSorter {
273 ProgramSorter(int column, Qt::SortOrder order)
274 : mSortColumn(column),
279 bool operator()(const ProfileTableRow &p1, const ProfileTableRow &p2) const
283 switch(mSortColumn) {
285 result = p1.program < p2.program;
288 result = p1.uses < p2.uses;
290 case COLUMN_GPU_TIME:
291 result = p1.gpuTime < p2.gpuTime;
293 case COLUMN_CPU_TIME:
294 result = p1.cpuTime < p2.cpuTime;
296 case COLUMN_PIXELS_DRAWN:
297 result = p1.pixels < p2.pixels;
299 case COLUMN_GPU_AVERAGE:
300 result = ((p1.uses <= 0) ? 0 : (p1.gpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.gpuTime / p2.uses));
302 case COLUMN_CPU_AVERAGE:
303 result = ((p1.uses <= 0) ? 0 : (p1.cpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.cpuTime / p2.uses));
305 case COLUMN_PIXELS_AVERAGE:
306 result = ((p1.uses <= 0) ? 0 : (p1.pixels / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.pixels / p2.uses));
310 if (mSortOrder == Qt::DescendingOrder) {
319 Qt::SortOrder mSortOrder;
323 void ProfileTableModel::sort(int column, Qt::SortOrder order) {
324 m_sortColumn = column;
326 qSort(m_rowData.begin(), m_rowData.end(), ProgramSorter(column, order));
327 emit dataChanged(createIndex(0, 0), createIndex(m_rowData.size(), MAX_COLUMN));
331 #include "profiletablemodel.moc"