summaryrefslogtreecommitdiff
path: root/common/trace_loader.cpp
blob: 51f3b556ea13a9d28bc59af9f749ec089a52df98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "trace_loader.hpp"


using namespace Trace;

Loader::Loader()
    : m_frameMarker(FrameMarker_SwapBuffers)
{
}

Loader::~Loader()
{
    close();
}

Loader::FrameMarker Loader::frameMarker() const
{
    return m_frameMarker;
}

void Loader::setFrameMarker(Loader::FrameMarker marker)
{
    m_frameMarker = marker;
}

unsigned Loader::numberOfFrames() const
{
    return m_frameBookmarks.size();
}

unsigned Loader::numberOfCallsInFrame(unsigned frameIdx) const
{
    if (frameIdx > m_frameBookmarks.size()) {
        return 0;
    }
    FrameBookmarks::const_iterator itr =
        m_frameBookmarks.find(frameIdx);
    return itr->second.numberOfCalls;
}

bool Loader::open(const char *filename)
{
    if (!m_parser.open(filename)) {
        std::cerr << "error: failed to open " << filename << "\n";
        return false;
    }
    if (!m_parser.supportsOffsets()) {
        std::cerr << "error: " <<filename<< " doesn't support seeking "
                  << "\n";
        return false;
    }

    Trace::Call *call;
    ParseBookmark startBookmark;
    unsigned numOfFrames = 0;
    unsigned numOfCalls = 0;
    int lastPercentReport = 0;

    m_parser.getBookmark(startBookmark);

    while ((call = m_parser.scan_call())) {
        ++numOfCalls;

        if (isCallAFrameMarker(call)) {
            FrameBookmark frameBookmark(startBookmark);
            frameBookmark.numberOfCalls = numOfCalls;

            m_frameBookmarks[numOfFrames] = frameBookmark;
            ++numOfFrames;

            if (m_parser.percentRead() - lastPercentReport >= 5) {
                std::cerr << "\tPercent scanned = "
                          << m_parser.percentRead()
                          << "..."<<std::endl;
                lastPercentReport = m_parser.percentRead();
            }
            
            m_parser.getBookmark(startBookmark);
            numOfCalls = 0;
        }
        //call->dump(std::cout, color);
        delete call;
    }
    return true;
}

void Loader::close()
{
    m_parser.close();
}

bool Loader::isCallAFrameMarker(const Trace::Call *call) const
{
    std::string name = call->name();

    switch (m_frameMarker) {
    case FrameMarker_SwapBuffers:
        return  name.find("SwapBuffers") != std::string::npos ||
                name == "CGLFlushDrawable" ||
                name == "glFrameTerminatorGREMEDY";
        break;
    case FrameMarker_Flush:
        return name == "glFlush";
        break;
    case FrameMarker_Finish:
        return name == "glFinish";
        break;
    case FrameMarker_Clear:
        return name == "glClear";
        break;
    }
    return false;
}

std::vector<Trace::Call *> Loader::frame(unsigned idx)
{
    unsigned numOfCalls = numberOfCallsInFrame(idx);
    if (numOfCalls) {
        const FrameBookmark &frameBookmark = m_frameBookmarks[idx];
        std::vector<Trace::Call*> calls(numOfCalls);
        m_parser.setBookmark(frameBookmark.start);

        Trace::Call *call;
        unsigned parsedCalls = 0;
        while ((call = m_parser.parse_call())) {

            calls[parsedCalls] = call;
            ++parsedCalls;

            if (isCallAFrameMarker(call)) {
                break;
            }

        }
        assert(parsedCalls == numOfCalls);
        return calls;
    }
    return std::vector<Trace::Call*>();
}