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