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