]> git.cworth.org Git - apitrace/blob - retrace/retrace.hpp
cli: Rename replay -> retrace.
[apitrace] / retrace / retrace.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011-2012 Jose Fonseca
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 #ifndef _RETRACE_HPP_
27 #define _RETRACE_HPP_
28
29 #include <assert.h>
30 #include <string.h>
31 #include <stdint.h>
32
33 #include <list>
34 #include <map>
35 #include <ostream>
36
37 #include "trace_model.hpp"
38 #include "trace_parser.hpp"
39 #include "trace_profiler.hpp"
40
41
42 namespace image {
43     class Image;
44 }
45
46
47 namespace retrace {
48
49
50 extern trace::Parser parser;
51 extern trace::Profiler profiler;
52
53
54 /**
55  * Similar to alloca(), but implemented with malloc.
56  */
57 class ScopedAllocator
58 {
59 private:
60     uintptr_t next;
61
62 public:
63     ScopedAllocator() :
64         next(0) {
65     }
66
67     inline void *
68     alloc(size_t size) {
69         /* Always return valid address, even when size is zero */
70         size = std::max(size, sizeof(uintptr_t));
71
72         uintptr_t * buf = static_cast<uintptr_t *>(malloc(sizeof(uintptr_t) + size));
73         if (!buf) {
74             return NULL;
75         }
76
77         *buf = next;
78         next = reinterpret_cast<uintptr_t>(buf);
79         assert((next & 1) == 0);
80
81         return static_cast<void *>(&buf[1]);
82     }
83
84     template< class T >
85     inline T *
86     alloc(size_t n = 1) {
87         return static_cast<T *>(alloc(sizeof(T) * n));
88     }
89
90     /**
91      * Allocate an array with the same dimensions as the specified value.
92      */
93     template< class T >
94     inline T *
95     alloc(const trace::Value *value) {
96         const trace::Array *array = dynamic_cast<const trace::Array *>(value);
97         if (array) {
98             return alloc<T>(array->size());
99         }
100         const trace::Null *null = dynamic_cast<const trace::Null *>(value);
101         if (null) {
102             return NULL;
103         }
104         assert(0);
105         return NULL;
106     }
107
108     /**
109      * Prevent this pointer from being automatically freed.
110      */
111     template< class T >
112     inline void
113     bind(T *ptr) {
114         if (ptr) {
115             reinterpret_cast<uintptr_t *>(ptr)[-1] |= 1;
116         }
117     }
118
119     inline
120     ~ScopedAllocator() {
121         while (next) {
122             uintptr_t temp = *reinterpret_cast<uintptr_t *>(next);
123
124             bool bind = temp & 1;
125             temp &= ~1;
126
127             if (!bind) {
128                 free(reinterpret_cast<void *>(next));
129             }
130
131             next = temp;
132         }
133     }
134 };
135
136
137 /**
138  * Output verbosity when retracing files.
139  */
140 extern int verbosity;
141
142 /**
143  * Debugging checks.
144  */
145 extern bool debug;
146
147 /**
148  * Add profiling data to the dump when retracing.
149  */
150 extern bool profiling;
151 extern bool profilingCpuTimes;
152 extern bool profilingGpuTimes;
153 extern bool profilingPixelsDrawn;
154
155 /**
156  * State dumping.
157  */
158 extern bool dumpingState;
159
160
161 extern bool doubleBuffer;
162 extern bool coreProfile;
163
164 extern unsigned frameNo;
165 extern unsigned callNo;
166
167
168 std::ostream &warning(trace::Call &call);
169
170
171 void ignore(trace::Call &call);
172 void unsupported(trace::Call &call);
173
174
175 typedef void (*Callback)(trace::Call &call);
176
177 struct Entry {
178     const char *name;
179     Callback callback;
180 };
181
182
183 struct stringComparer {
184   bool operator() (const char *a, const  char *b) const {
185     return strcmp(a, b) < 0;
186   }
187 };
188
189
190 extern const Entry stdc_callbacks[];
191
192
193 class Retracer
194 {
195     typedef std::map<const char *, Callback, stringComparer> Map;
196     Map map;
197
198     std::vector<Callback> callbacks;
199
200 public:
201     Retracer() {
202         addCallbacks(stdc_callbacks);
203     }
204
205     virtual ~Retracer() {}
206
207     void addCallback(const Entry *entry);
208     void addCallbacks(const Entry *entries);
209
210     void retrace(trace::Call &call);
211 };
212
213
214 class Dumper
215 {
216 public:
217     virtual image::Image *
218     getSnapshot(void) {
219         return NULL;
220     }
221
222     virtual bool
223     dumpState(std::ostream &os) {
224         return false;
225     }
226 };
227
228
229 extern Dumper *dumper;
230
231
232 void
233 setUp(void);
234
235 void
236 addCallbacks(retrace::Retracer &retracer);
237
238 void
239 frameComplete(trace::Call &call);
240
241
242
243 void
244 flushRendering(void);
245
246 void
247 waitForInput(void);
248
249 void
250 cleanUp(void);
251
252
253 } /* namespace retrace */
254
255 #endif /* _RETRACE_HPP_ */