1 #include "profiletablemodel.h"
2 #include "profiledialog.h"
6 typedef trace::Profile::Call Call;
7 typedef trace::Profile::Frame Frame;
8 typedef trace::Profile::Program Program;
18 COLUMN_PIXELS_AVERAGE,
22 static QString columnNames[] = {
25 QString("Total GPU Time"),
26 QString("Total CPU Time"),
27 QString("Total Pixels Drawn"),
28 QString("Avg GPU Time"),
29 QString("Avg CPU Time"),
30 QString("Avg Pixels Drawn")
33 ProfileTableModel::ProfileTableModel(QObject *parent)
34 : QAbstractTableModel(parent),
36 m_sortColumn(COLUMN_GPU_TIME),
37 m_sortOrder(Qt::DescendingOrder)
42 void ProfileTableModel::setProfile(trace::Profile* profile)
45 m_timeMin = m_profile->frames.front().cpuStart;
46 m_timeMax = m_profile->frames.back().cpuStart + m_profile->frames.back().cpuDuration;
52 * Set selection to nothing
54 void ProfileTableModel::selectNone()
56 m_timeMin = m_timeMax = 0;
58 sort(m_sortColumn, m_sortOrder);
63 * Set selection to program
65 void ProfileTableModel::selectProgram(unsigned /*program*/)
67 /* We have no program based selection for table */
73 * Set selection to a period of time
75 void ProfileTableModel::selectTime(int64_t start, int64_t end)
81 sort(m_sortColumn, m_sortOrder);
86 * Creates the row data from trace profile
88 void ProfileTableModel::updateModel()
90 if (m_timeMin == m_timeMax) {
91 m_timeMin = m_profile->frames.front().cpuStart;
92 m_timeMax = m_profile->frames.back().cpuStart + m_profile->frames.back().cpuDuration;
95 for (QList<ProfileTableRow>::iterator itr = m_rowData.begin(); itr != m_rowData.end(); ++itr) {
96 ProfileTableRow& row = *itr;
102 row.longestCpu = NULL;
103 row.longestGpu = NULL;
104 row.longestPixel = NULL;
107 for (std::vector<Program>::const_iterator itr = m_profile->programs.begin(); itr != m_profile->programs.end(); ++itr) {
108 ProfileTableRow* row = NULL;
109 const Program& program = *itr;
111 for (std::vector<unsigned>::const_iterator jtr = program.calls.begin(); jtr != program.calls.end(); ++jtr) {
112 const Call& call = m_profile->calls[*jtr];
114 if (call.cpuStart > m_timeMax) {
118 if (call.cpuStart + call.cpuDuration < m_timeMin) {
123 row = getRow(itr - m_profile->programs.begin());
127 row->pixels += call.pixels;
128 row->gpuTime += call.gpuDuration;
129 row->cpuTime += call.cpuDuration;
131 if (!row->longestGpu || row->longestGpu->gpuDuration < call.gpuDuration) {
132 row->longestGpu = &call;
135 if (!row->longestCpu || row->longestCpu->cpuDuration < call.cpuDuration) {
136 row->longestCpu = &call;
139 if (!row->longestPixel || row->longestPixel->pixels < call.pixels) {
140 row->longestPixel = &call;
148 * Get the appropriate call associated with an item in the table
150 const trace::Profile::Call *ProfileTableModel::getJumpCall(const QModelIndex & index) const {
151 const ProfileTableRow& row = m_rowData[index.row()];
153 switch(index.column()) {
154 case COLUMN_GPU_TIME:
155 case COLUMN_GPU_AVERAGE:
156 return row.longestGpu;
157 case COLUMN_CPU_TIME:
158 case COLUMN_CPU_AVERAGE:
159 return row.longestCpu;
160 case COLUMN_PIXELS_DRAWN:
161 case COLUMN_PIXELS_AVERAGE:
162 return row.longestPixel;
170 * Get the shader program associated with an item in the table
172 unsigned ProfileTableModel::getProgram(const QModelIndex & index) const
174 const ProfileTableRow& row = m_rowData[index.row()];
180 * Get the row index for a shader program
182 int ProfileTableModel::getRowIndex(unsigned program) const
184 for (int i = 0; i < m_rowData.size(); ++i) {
185 if (m_rowData[i].program == program)
194 * Get the row data for a shader program
196 ProfileTableRow* ProfileTableModel::getRow(unsigned program) {
197 for (QList<ProfileTableRow>::iterator itr = m_rowData.begin(); itr != m_rowData.end(); ++itr) {
198 if (itr->program == program)
202 m_rowData.append(ProfileTableRow(program));
203 return &m_rowData.back();
207 int ProfileTableModel::rowCount(const QModelIndex & parent) const
209 if (!parent.isValid()) {
210 return m_rowData.size();
217 int ProfileTableModel::columnCount(const QModelIndex & /*parent*/) const
223 QVariant ProfileTableModel::data(const QModelIndex &index, int role) const
225 if (!index.isValid()) {
229 if (role == Qt::DisplayRole) {
230 const ProfileTableRow& row = m_rowData[index.row()];
232 switch(index.column()) {
236 return QLocale::system().toString(row.uses);
237 case COLUMN_GPU_TIME:
238 return getTimeString(row.gpuTime);
239 case COLUMN_CPU_TIME:
240 return getTimeString(row.cpuTime);
241 case COLUMN_PIXELS_DRAWN:
242 return QLocale::system().toString((qlonglong)row.pixels);
243 case COLUMN_GPU_AVERAGE:
244 return getTimeString((row.uses <= 0) ? 0 : (row.gpuTime / row.uses));
245 case COLUMN_CPU_AVERAGE:
246 return getTimeString((row.uses <= 0) ? 0 : (row.cpuTime / row.uses));
247 case COLUMN_PIXELS_AVERAGE:
248 return QLocale::system().toString((row.uses <= 0) ? 0 : (row.pixels / row.uses));
250 } else if (role == Qt::TextAlignmentRole) {
251 return Qt::AlignRight;
258 QVariant ProfileTableModel::headerData(int section, Qt::Orientation orientation, int role) const
260 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
261 if (section >= 0 && section < MAX_COLUMN) {
262 return columnNames[section];
270 class ProgramSorter {
272 ProgramSorter(int column, Qt::SortOrder order)
273 : mSortColumn(column),
278 bool operator()(const ProfileTableRow &p1, const ProfileTableRow &p2) const
282 switch(mSortColumn) {
284 result = p1.program < p2.program;
287 result = p1.uses < p2.uses;
289 case COLUMN_GPU_TIME:
290 result = p1.gpuTime < p2.gpuTime;
292 case COLUMN_CPU_TIME:
293 result = p1.cpuTime < p2.cpuTime;
295 case COLUMN_PIXELS_DRAWN:
296 result = p1.pixels < p2.pixels;
298 case COLUMN_GPU_AVERAGE:
299 result = ((p1.uses <= 0) ? 0 : (p1.gpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.gpuTime / p2.uses));
301 case COLUMN_CPU_AVERAGE:
302 result = ((p1.uses <= 0) ? 0 : (p1.cpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.cpuTime / p2.uses));
304 case COLUMN_PIXELS_AVERAGE:
305 result = ((p1.uses <= 0) ? 0 : (p1.pixels / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.pixels / p2.uses));
309 if (mSortOrder == Qt::DescendingOrder) {
318 Qt::SortOrder mSortOrder;
322 void ProfileTableModel::sort(int column, Qt::SortOrder order) {
323 m_sortColumn = column;
325 qSort(m_rowData.begin(), m_rowData.end(), ProgramSorter(column, order));
326 emit dataChanged(createIndex(0, 0), createIndex(m_rowData.size(), MAX_COLUMN));
330 #include "profiletablemodel.moc"