]> git.cworth.org Git - vogl/blob - src/voglcommon/vogl_trace_file_writer.h
Initial vogl checkin
[vogl] / src / voglcommon / vogl_trace_file_writer.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 //----------------------------------------------------------------------------------------------------------------------
27 // File: vogl_trace_file_writer.h
28 //----------------------------------------------------------------------------------------------------------------------
29 #ifndef VOGL_TRACE_FILE_WRITER_H
30 #define VOGL_TRACE_FILE_WRITER_H
31
32 #include "vogl_common.h"
33 #include "vogl_trace_stream_types.h"
34 #include "vogl_trace_packet.h"
35 #include "vogl_cfile_stream.h"
36 #include "vogl_dynamic_stream.h"
37 #include "vogl_json.h"
38 #include "vogl_unique_ptr.h"
39
40 //----------------------------------------------------------------------------------------------------------------------
41 // vogl_trace_file_writer
42 //----------------------------------------------------------------------------------------------------------------------
43 class vogl_trace_file_writer
44 {
45 public:
46     vogl_trace_file_writer(const vogl_ctypes *pCTypes);
47     ~vogl_trace_file_writer();
48
49     inline bool is_opened() const
50     {
51         return m_stream.is_opened();
52     }
53     inline const dynamic_string &get_filename() const
54     {
55         return m_filename;
56     }
57
58     inline data_stream &get_stream()
59     {
60         return m_stream;
61     }
62
63     inline vogl_archive_blob_manager *get_trace_archive()
64     {
65         return m_pTrace_archive.get();
66     }
67
68     // pTrace_archive may be NULL. Takes ownership of pTrace_archive.
69     // TODO: Get rid of the demarcation packet, etc. Make the initial sequence of packets more explicit.
70     bool open(const char *pFilename, vogl_archive_blob_manager *pTrace_archive = NULL, bool delete_archive = true, bool write_demarcation_packet = true, uint pointer_sizes = sizeof(void *));
71
72     inline uint64_t get_cur_gl_call_counter()
73     {
74         return m_gl_call_counter;
75     }
76
77     inline uint64_t get_next_gl_call_counter()
78     {
79         VOGL_FUNC_TRACER
80
81         atomic64_t ctr;
82         for (;;)
83         {
84             atomic64_t cur_ctr = m_gl_call_counter;
85             ctr = atomic_compare_exchange64(&m_gl_call_counter, cur_ctr + 1, cur_ctr);
86             if (ctr == cur_ctr)
87                 break;
88         }
89         return ctr;
90     }
91
92     inline bool write_packet(const vogl_trace_packet &packet)
93     {
94         VOGL_FUNC_TRACER
95
96         if (!m_stream.is_opened())
97             return false;
98
99         if (!packet.serialize(m_stream))
100             return false;
101
102         if (vogl_is_swap_buffers_entrypoint(packet.get_entrypoint_id()))
103             m_frame_file_offsets.push_back(m_stream.get_ofs());
104
105         return true;
106     }
107
108     inline bool write_packet(const void *pPacket, uint packet_size, bool is_swap)
109     {
110         VOGL_FUNC_TRACER
111
112         if (!m_stream.is_opened())
113             return false;
114
115         if (m_stream.write(pPacket, packet_size) != packet_size)
116             return false;
117
118         if (is_swap)
119             m_frame_file_offsets.push_back(m_stream.get_ofs());
120
121         return true;
122     }
123
124     inline bool flush()
125     {
126         VOGL_FUNC_TRACER
127
128         return m_stream.flush();
129     }
130
131     bool close();
132
133 private:
134     atomic64_t m_gl_call_counter;
135
136     const vogl_ctypes *m_pCTypes;
137     dynamic_string m_filename;
138     cfile_stream m_stream;
139
140     vogl_unique_ptr<vogl_archive_blob_manager> m_pTrace_archive;
141     bool m_delete_archive;
142
143     vogl_trace_stream_start_of_file_packet m_sof_packet;
144
145     vogl::vector<uint64_t> m_frame_file_offsets;
146
147     void write_ctypes_packet();
148
149     void write_entrypoints_packet();
150
151     bool write_eof_packet();
152
153     bool write_frame_file_offsets_to_archive();
154
155     void close_archive(const char *pArchive_filename);
156 };
157
158 #endif // VOGL_TRACE_FILE_WRITER_H