]> git.cworth.org Git - apitrace/blob - gui/apitrace.cpp
Introduce ApiTrace which encapsulates the data.
[apitrace] / gui / apitrace.cpp
1 #include "apitrace.h"
2
3 #include "loaderthread.h"
4
5 ApiTrace::ApiTrace()
6     : m_frameMarker(ApiTrace::FrameMarker_SwapBuffers)
7 {
8     m_loader = new LoaderThread(this);
9     connect(m_loader, SIGNAL(parsedFrames(const QList<ApiTraceFrame*>)),
10             this, SLOT(addFrames(const QList<ApiTraceFrame*>)));
11 }
12
13 ApiTrace::~ApiTrace()
14 {
15     qDeleteAll(m_calls);
16     qDeleteAll(m_frames);
17     delete m_loader;
18 }
19
20 bool ApiTrace::isCallAFrameMarker(const ApiTraceCall *call,
21                                   ApiTrace::FrameMarker marker)
22 {
23     if (!call)
24         return false;
25
26     switch (marker) {
27     case FrameMarker_SwapBuffers:
28         return call->name.contains(QLatin1String("SwapBuffers"));
29     case FrameMarker_Flush:
30         return call->name == QLatin1String("glFlush");
31     case FrameMarker_Finish:
32         return call->name == QLatin1String("glFinish");
33     case FrameMarker_Clear:
34         return call->name == QLatin1String("glClear");
35     }
36
37     Q_ASSERT(!"unknown frame marker");
38
39     return false;
40 }
41
42 bool ApiTrace::isEmpty() const
43 {
44     return m_calls.isEmpty();
45 }
46
47 QString ApiTrace::fileName() const
48 {
49     return m_fileName;
50 }
51
52 ApiTrace::FrameMarker ApiTrace::frameMarker() const
53 {
54     return m_frameMarker;
55 }
56
57 QList<ApiTraceCall*> ApiTrace::calls() const
58 {
59     return m_calls;
60 }
61
62 ApiTraceCall * ApiTrace::callAt(int idx) const
63 {
64     return m_calls.value(idx);
65 }
66
67 int ApiTrace::numCalls() const
68 {
69     return m_calls.count();
70 }
71
72 QList<ApiTraceFrame*> ApiTrace::frames() const
73 {
74     return m_frames;
75 }
76
77 ApiTraceFrame * ApiTrace::frameAt(int idx) const
78 {
79     return m_frames.value(idx);
80 }
81
82 int ApiTrace::numFrames() const
83 {
84     return m_frames.count();
85 }
86
87 int ApiTrace::numCallsInFrame(int idx) const
88 {
89     const ApiTraceFrame *frame = frameAt(idx);
90     if (frame)
91         return frame->calls.count();
92     else
93         return 0;
94 }
95
96 void ApiTrace::setFileName(const QString &name)
97 {
98     if (m_fileName != name) {
99         m_fileName = name;
100
101         if (m_loader->isRunning()) {
102             m_loader->terminate();
103             m_loader->wait();
104         }
105         emit invalidated();
106
107         m_loader->loadFile(m_fileName);
108     }
109 }
110
111 void ApiTrace::setFrameMarker(FrameMarker marker)
112 {
113     if (m_frameMarker != marker) {
114         emit framesInvalidated();
115
116         qDeleteAll(m_frames);
117         m_frames.clear();
118         detectFrames();
119     }
120 }
121
122 void ApiTrace::addFrames(const QList<ApiTraceFrame*> &frames)
123 {
124     int currentFrames = m_frames.count();
125     int numNewFrames = frames.count();
126     m_frames += frames;
127
128     int currentCalls = m_calls.count();
129     int numNewCalls = 0;
130     foreach(ApiTraceFrame *frame, frames) {
131         numNewCalls += frame->calls.count();
132         m_calls += frame->calls;
133     }
134
135     emit framesAdded(currentFrames, numNewFrames);
136     emit callsAdded(currentCalls, numNewCalls);
137 }
138
139 void ApiTrace::detectFrames()
140 {
141     if (m_calls.isEmpty())
142         return;
143
144     ApiTraceFrame *currentFrame = 0;
145     foreach(ApiTraceCall *apiCall, m_calls) {
146         if (!currentFrame) {
147             currentFrame = new ApiTraceFrame();
148             currentFrame->number = m_frames.count();
149         }
150         apiCall->parentFrame = currentFrame;
151         apiCall->index = currentFrame->calls.count();
152         currentFrame->calls.append(apiCall);
153         if (ApiTrace::isCallAFrameMarker(apiCall,
154                                          m_frameMarker)) {
155             m_frames.append(currentFrame);
156             currentFrame = 0;
157         }
158     }
159     //last frames won't have markers
160     //  it's just a bunch of Delete calls for every object
161     //  after the last SwapBuffers
162     if (currentFrame) {
163         m_frames.append(currentFrame);
164         currentFrame = 0;
165     }
166     emit framesAdded(0, m_frames.count());
167 }
168
169 #include "apitrace.moc"