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