1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
26 //----------------------------------------------------------------------------------------------------------------------
27 // File: vogl_trace_file_writer.h
28 //----------------------------------------------------------------------------------------------------------------------
29 #ifndef VOGL_TRACE_FILE_WRITER_H
30 #define VOGL_TRACE_FILE_WRITER_H
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"
40 //----------------------------------------------------------------------------------------------------------------------
41 // vogl_trace_file_writer
42 //----------------------------------------------------------------------------------------------------------------------
43 class vogl_trace_file_writer
46 vogl_trace_file_writer(const vogl_ctypes *pCTypes);
47 ~vogl_trace_file_writer();
49 inline bool is_opened() const
51 return m_stream.is_opened();
53 inline const dynamic_string &get_filename() const
58 inline data_stream &get_stream()
63 inline vogl_archive_blob_manager *get_trace_archive()
65 return m_pTrace_archive.get();
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 *));
72 inline uint64_t get_cur_gl_call_counter()
74 return m_gl_call_counter;
77 inline uint64_t get_next_gl_call_counter()
84 atomic64_t cur_ctr = m_gl_call_counter;
85 ctr = atomic_compare_exchange64(&m_gl_call_counter, cur_ctr + 1, cur_ctr);
92 inline bool write_packet(const vogl_trace_packet &packet)
96 if (!m_stream.is_opened())
99 if (!packet.serialize(m_stream))
102 if (vogl_is_swap_buffers_entrypoint(packet.get_entrypoint_id()))
103 m_frame_file_offsets.push_back(m_stream.get_ofs());
108 inline bool write_packet(const void *pPacket, uint packet_size, bool is_swap)
112 if (!m_stream.is_opened())
115 if (m_stream.write(pPacket, packet_size) != packet_size)
119 m_frame_file_offsets.push_back(m_stream.get_ofs());
128 return m_stream.flush();
134 atomic64_t m_gl_call_counter;
136 const vogl_ctypes *m_pCTypes;
137 dynamic_string m_filename;
138 cfile_stream m_stream;
140 vogl_unique_ptr<vogl_archive_blob_manager> m_pTrace_archive;
141 bool m_delete_archive;
143 vogl_trace_stream_start_of_file_packet m_sof_packet;
145 vogl::vector<uint64_t> m_frame_file_offsets;
147 void write_ctypes_packet();
149 void write_entrypoints_packet();
151 bool write_eof_packet();
153 bool write_frame_file_offsets_to_archive();
155 void close_archive(const char *pArchive_filename);
158 #endif // VOGL_TRACE_FILE_WRITER_H