]> git.cworth.org Git - apitrace/blob - trace_model.cpp
List GL_TEXTURE_xxx and GL_TEXTURE_BINDING_xxx params per texture unit.
[apitrace] / trace_model.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
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
27 #include "formatter.hpp"
28 #include "trace_model.hpp"
29
30
31 namespace Trace {
32
33
34 Call::~Call() {
35     for (unsigned i = 0; i < args.size(); ++i) {
36         delete args[i];
37     }
38
39     if (ret) {
40         delete ret;
41     }
42 }
43
44
45 Struct::~Struct() {
46     for (std::vector<Value *>::iterator it = members.begin(); it != members.end(); ++it) {
47         delete *it;
48     }
49 }
50
51
52 Array::~Array() {
53     for (std::vector<Value *>::iterator it = values.begin(); it != values.end(); ++it) {
54         delete *it;
55     }
56 }
57
58 Blob::~Blob() {
59     // Blobs are often bound and referred during many calls, so we can't delete
60     // them here in that case.
61     //
62     // Once bound there is no way to know when they were unbound, which
63     // effectively means we have to leak them.  A better solution would be to
64     // keep a list of bound pointers, and defer the destruction to when the
65     // trace in question has been fully processed.
66     if (!bound) {
67         delete [] buf;
68     }
69 }
70
71
72 // bool cast
73 bool Null   ::toBool(void) const { return false; }
74 bool Bool   ::toBool(void) const { return value; }
75 bool SInt   ::toBool(void) const { return value != 0; }
76 bool UInt   ::toBool(void) const { return value != 0; }
77 bool Float  ::toBool(void) const { return value != 0; }
78 bool String ::toBool(void) const { return true; }
79 bool Enum   ::toBool(void) const { return sig->value != 0; }
80 bool Struct ::toBool(void) const { return true; }
81 bool Array  ::toBool(void) const { return true; }
82 bool Blob   ::toBool(void) const { return true; }
83 bool Pointer::toBool(void) const { return value != 0; }
84
85
86 // signed integer cast
87 signed long long Value  ::toSInt(void) const { assert(0); return 0; }
88 signed long long Null   ::toSInt(void) const { return 0; }
89 signed long long Bool   ::toSInt(void) const { return static_cast<signed long long>(value); }
90 signed long long SInt   ::toSInt(void) const { return value; }
91 signed long long UInt   ::toSInt(void) const { assert(static_cast<signed long long>(value) >= 0); return static_cast<signed long long>(value); }
92 signed long long Float  ::toSInt(void) const { return static_cast<signed long long>(value); }
93 signed long long Enum   ::toSInt(void) const { return sig->value; }
94
95
96 // unsigned integer cast
97 unsigned long long Value  ::toUInt(void) const { assert(0); return 0; }
98 unsigned long long Null   ::toUInt(void) const { return 0; }
99 unsigned long long Bool   ::toUInt(void) const { return static_cast<unsigned long long>(value); }
100 unsigned long long SInt   ::toUInt(void) const { assert(value >= 0); return static_cast<signed long long>(value); }
101 unsigned long long UInt   ::toUInt(void) const { return value; }
102 unsigned long long Float  ::toUInt(void) const { return static_cast<unsigned long long>(value); }
103 unsigned long long Enum   ::toUInt(void) const { assert(sig->value >= 0); return sig->value; }
104
105
106 // floating point cast
107 float Value  ::toFloat(void) const { assert(0); return 0; }
108 float Null   ::toFloat(void) const { return 0; }
109 float Bool   ::toFloat(void) const { return static_cast<float>(value); }
110 float SInt   ::toFloat(void) const { return static_cast<float>(value); }
111 float UInt   ::toFloat(void) const { return static_cast<float>(value); }
112 float Float  ::toFloat(void) const { return value; }
113 float Enum   ::toFloat(void) const { return static_cast<float>(sig->value); }
114
115
116 // floating point cast
117 double Value  ::toDouble(void) const { assert(0); return 0; }
118 double Null   ::toDouble(void) const { return 0; }
119 double Bool   ::toDouble(void) const { return static_cast<double>(value); }
120 double SInt   ::toDouble(void) const { return static_cast<double>(value); }
121 double UInt   ::toDouble(void) const { return static_cast<double>(value); }
122 double Float  ::toDouble(void) const { return value; }
123 double Enum   ::toDouble(void) const { return static_cast<double>(sig->value); }
124
125
126 // pointer cast
127 void * Value  ::toPointer(void) const { assert(0); return NULL; }
128 void * Null   ::toPointer(void) const { return NULL; }
129 void * Blob   ::toPointer(void) const { return buf; }
130 void * Pointer::toPointer(void) const { return (void *)value; }
131
132 void * Value  ::toPointer(bool bind) { assert(0); return NULL; }
133 void * Null   ::toPointer(bool bind) { return NULL; }
134 void * Blob   ::toPointer(bool bind) { if (bind) bound = true; return buf; }
135 void * Pointer::toPointer(bool bind) { return (void *)value; }
136
137
138 // pointer cast
139 unsigned long long Value  ::toUIntPtr(void) const { assert(0); return 0; }
140 unsigned long long Null   ::toUIntPtr(void) const { return 0; }
141 unsigned long long Pointer::toUIntPtr(void) const { return value; }
142
143
144 // string cast
145 const char * Value ::toString(void) const { assert(0); return NULL; }
146 const char * Null  ::toString(void) const { return NULL; }
147 const char * String::toString(void) const { return value; }
148
149
150 // virtual Value::visit()
151 void Null   ::visit(Visitor &visitor) { visitor.visit(this); }
152 void Bool   ::visit(Visitor &visitor) { visitor.visit(this); }
153 void SInt   ::visit(Visitor &visitor) { visitor.visit(this); }
154 void UInt   ::visit(Visitor &visitor) { visitor.visit(this); }
155 void Float  ::visit(Visitor &visitor) { visitor.visit(this); }
156 void String ::visit(Visitor &visitor) { visitor.visit(this); }
157 void Enum   ::visit(Visitor &visitor) { visitor.visit(this); }
158 void Bitmask::visit(Visitor &visitor) { visitor.visit(this); }
159 void Struct ::visit(Visitor &visitor) { visitor.visit(this); }
160 void Array  ::visit(Visitor &visitor) { visitor.visit(this); }
161 void Blob   ::visit(Visitor &visitor) { visitor.visit(this); }
162 void Pointer::visit(Visitor &visitor) { visitor.visit(this); }
163
164
165 void Visitor::visit(Null *) { assert(0); }
166 void Visitor::visit(Bool *) { assert(0); }
167 void Visitor::visit(SInt *) { assert(0); }
168 void Visitor::visit(UInt *) { assert(0); }
169 void Visitor::visit(Float *) { assert(0); }
170 void Visitor::visit(String *) { assert(0); }
171 void Visitor::visit(Enum *node) { assert(0); }
172 void Visitor::visit(Bitmask *node) { visit(static_cast<UInt *>(node)); }
173 void Visitor::visit(Struct *) { assert(0); }
174 void Visitor::visit(Array *) { assert(0); }
175 void Visitor::visit(Blob *) { assert(0); }
176 void Visitor::visit(Pointer *) { assert(0); }
177
178
179 class Dumper : public Visitor
180 {
181 protected:
182     std::ostream &os;
183     Formatter::Formatter *formatter;
184     Formatter::Attribute *normal;
185     Formatter::Attribute *bold;
186     Formatter::Attribute *italic;
187     Formatter::Attribute *red;
188     Formatter::Attribute *pointer;
189     Formatter::Attribute *literal;
190
191 public:
192     Dumper(std::ostream &_os, bool color) : os(_os) {
193         formatter = Formatter::defaultFormatter(color);
194         normal = formatter->normal();
195         bold = formatter->bold();
196         italic = formatter->italic();
197         red = formatter->color(Formatter::RED);
198         pointer = formatter->color(Formatter::GREEN);
199         literal = formatter->color(Formatter::BLUE);
200     }
201
202     ~Dumper() {
203         delete normal;
204         delete bold;
205         delete italic;
206         delete red;
207         delete pointer;
208         delete literal;
209         delete formatter;
210     }
211
212     void visit(Null *) {
213         os << "NULL";
214     }
215
216     void visit(Bool *node) {
217         os << literal << (node->value ? "true" : "false") << normal;
218     }
219
220     void visit(SInt *node) {
221         os << literal << node->value << normal;
222     }
223
224     void visit(UInt *node) {
225         os << literal << node->value << normal;
226     }
227
228     void visit(Float *node) {
229         os << literal << node->value << normal;
230     }
231
232     void visit(String *node) {
233         os << literal << "\"";
234         for (const char *it = node->value; *it; ++it) {
235             unsigned char c = (unsigned char) *it;
236             if (c == '\"')
237                 os << "\\\"";
238             else if (c == '\\')
239                 os << "\\\\";
240             else if (c >= 0x20 && c <= 0x7e)
241                 os << c;
242             else if (c == '\t') {
243                 os << "\t";
244             } else if (c == '\r') {
245                 // Ignore carriage-return
246             } else if (c == '\n') {
247                 // Reset formatting so that it looks correct with 'less -R'
248                 os << normal << '\n' << literal;
249             } else {
250                 unsigned octal0 = c & 0x7;
251                 unsigned octal1 = (c >> 3) & 0x7;
252                 unsigned octal2 = (c >> 3) & 0x7;
253                 os << "\\";
254                 if (octal2)
255                     os << octal2;
256                 if (octal1)
257                     os << octal1;
258                 os << octal0;
259             }
260         }
261         os << "\"" << normal;
262     }
263
264     void visit(Enum *node) {
265         os << literal << node->sig->name << normal;
266     }
267
268     void visit(Bitmask *bitmask) {
269         unsigned long long value = bitmask->value;
270         const BitmaskSig *sig = bitmask->sig;
271         bool first = true;
272         for (const BitmaskFlag *it = sig->flags; value != 0 && it != sig->flags + sig->num_flags; ++it) {
273             if ((it->value && (value & it->value) == it->value) ||
274                 (!it->value && value == 0)) {
275                 if (!first) {
276                     os << " | ";
277                 }
278                 os << literal << it->name << normal;
279                 value &= ~it->value;
280                 first = false;
281             }
282         }
283         if (value || first) {
284             if (!first) {
285                 os << " | ";
286             }
287             os << literal << "0x" << std::hex << value << std::dec << normal;
288         }
289     }
290
291     void visit(Struct *s) {
292         const char *sep = "";
293         os << "{";
294         for (unsigned i = 0; i < s->members.size(); ++i) {
295             os << sep << italic << s->sig->member_names[i] << normal << " = ";
296             _visit(s->members[i]);
297             sep = ", ";
298         }
299         os << "}";
300     }
301
302     void visit(Array *array) {
303         if (array->values.size() == 1) {
304             os << "&";
305             _visit(array->values[0]);
306         }
307         else {
308             const char *sep = "";
309             os << "{";
310             for (std::vector<Value *>::iterator it = array->values.begin(); it != array->values.end(); ++it) {
311                 os << sep;
312                 _visit(*it);
313                 sep = ", ";
314             }
315             os << "}";
316         }
317     }
318
319     void visit(Blob *blob) {
320         os << pointer << "blob(" << blob->size << ")" << normal;
321     }
322
323     void visit(Pointer *p) {
324         os << pointer << "0x" << std::hex << p->value << std::dec << normal;
325     }
326
327     void visit(Call *call) {
328         const char *sep = "";
329         os << bold << call->sig->name << normal << "(";
330         for (unsigned i = 0; i < call->args.size(); ++i) {
331             os << sep << italic << call->sig->arg_names[i] << normal << " = ";
332             if (call->args[i]) {
333                 _visit(call->args[i]);
334             } else {
335                os << "?";
336             }
337             sep = ", ";
338         }
339         os << ")";
340         if (call->ret) {
341             os << " = ";
342             _visit(call->ret);
343         }
344         os << "\n";
345     }
346 };
347
348
349 void Value::dump(std::ostream &os, bool color) {
350     Dumper d(os, color);
351     visit(d);
352 }
353
354
355 static Null null;
356
357 const Value & Value::operator[](size_t index) const {
358     const Array *array = dynamic_cast<const Array *>(this);
359     if (array) {
360         if (index < array->values.size()) {
361             return *array->values[index];
362         }
363     }
364     return null;
365 }
366
367 void Call::dump(std::ostream &os, bool color) {
368     Dumper d(os, color);
369     os << no << " ";
370     d.visit(this);
371 }
372
373
374 } /* namespace Trace */