]> git.cworth.org Git - apitrace/blob - retrace/retrace.hpp
228c81352e979046ece2e8040d2f885ef7b27051
[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 enum Driver {
156     DRIVER_DEFAULT,
157     DRIVER_HARDWARE, // force hardware
158     DRIVER_SOFTWARE,
159     DRIVER_REFERENCE,
160     DRIVER_NULL,
161     DRIVER_MODULE,
162 };
163
164 extern Driver driver;
165 extern const char *driverModule;
166
167 extern bool doubleBuffer;
168 extern bool coreProfile;
169
170 extern unsigned frameNo;
171 extern unsigned callNo;
172
173
174 std::ostream &warning(trace::Call &call);
175
176
177 void ignore(trace::Call &call);
178 void unsupported(trace::Call &call);
179
180
181 typedef void (*Callback)(trace::Call &call);
182
183 struct Entry {
184     const char *name;
185     Callback callback;
186 };
187
188
189 struct stringComparer {
190   bool operator() (const char *a, const  char *b) const {
191     return strcmp(a, b) < 0;
192   }
193 };
194
195
196 extern const Entry stdc_callbacks[];
197
198
199 class Retracer
200 {
201     typedef std::map<const char *, Callback, stringComparer> Map;
202     Map map;
203
204     std::vector<Callback> callbacks;
205
206 public:
207     Retracer() {
208         addCallbacks(stdc_callbacks);
209     }
210
211     virtual ~Retracer() {}
212
213     void addCallback(const Entry *entry);
214     void addCallbacks(const Entry *entries);
215
216     void retrace(trace::Call &call);
217 };
218
219
220 class Dumper
221 {
222 public:
223     virtual image::Image *
224     getSnapshot(void) {
225         return NULL;
226     }
227
228     virtual bool
229     dumpState(std::ostream &os) {
230         return false;
231     }
232 };
233
234
235 extern Dumper *dumper;
236
237
238 void
239 setUp(void);
240
241 void
242 addCallbacks(retrace::Retracer &retracer);
243
244 void
245 frameComplete(trace::Call &call);
246
247
248
249 void
250 flushRendering(void);
251
252 void
253 waitForInput(void);
254
255 void
256 cleanUp(void);
257
258
259 } /* namespace retrace */
260
261 #endif /* _RETRACE_HPP_ */