python/trace: Control the interpreter from command line options.
authorJosé Fonseca <jfonseca@vmware.com>
Wed, 25 Mar 2009 13:31:27 +0000 (13:31 +0000)
committerJosé Fonseca <jfonseca@vmware.com>
Wed, 25 Mar 2009 21:03:38 +0000 (21:03 +0000)
src/gallium/state_trackers/python/retrace/interpreter.py
src/gallium/state_trackers/python/retrace/parser.py

index 66d73ed903fea1189749bda3ab212a89f946d739..23038c5c1c0993d9c103e9cdf179ddb2bf84f6ba 100755 (executable)
@@ -75,15 +75,6 @@ def show_image(surface):
     root.mainloop()
 
 
-# Verbosity level: 0, 1, 2 
-verbose = 1
-# Dump images to disk instead of showing: True, False
-images = False
-
-
-image_no = 0
-
-
 class Struct:
     """C-like struct"""
 
@@ -381,7 +372,7 @@ class Context(Object):
         self.real.set_clip(_state)
 
     def dump_constant_buffer(self, buffer):
-        if verbose < 2:
+        if not self.interpreter.verbosity(2):
             return
 
         data = buffer.read()
@@ -447,7 +438,7 @@ class Context(Object):
         pass
     
     def dump_vertices(self, start, count):
-        if verbose < 2:
+        if not self.interpreter.verbosity(2):
             return
 
         for index in range(start, start + count):
@@ -474,7 +465,7 @@ class Context(Object):
             sys.stdout.write('\t},\n')
 
     def dump_indices(self, ibuf, isize, start, count):
-        if verbose < 2:
+        if not self.interpreter.verbosity(2):
             return
 
         format = {
@@ -506,51 +497,48 @@ class Context(Object):
         self.dump_vertices(start, count)
             
         self.real.draw_arrays(mode, start, count)
-
-        self.dirty = True
+        self._set_dirty()
     
     def draw_elements(self, indexBuffer, indexSize, mode, start, count):
-        if verbose >= 2:
+        if self.interpreter.verbosity(2):
             minindex, maxindex = self.dump_indices(indexBuffer, indexSize, start, count)
             self.dump_vertices(minindex, maxindex - minindex)
 
         self.real.draw_elements(indexBuffer, indexSize, mode, start, count)
-
-        self.dirty = True
+        self._set_dirty()
         
     def draw_range_elements(self, indexBuffer, indexSize, minIndex, maxIndex, mode, start, count):
-        if verbose >= 2:
+        if self.interpreter.verbosity(2):
             minindex, maxindex = self.dump_indices(indexBuffer, indexSize, start, count)
             minindex = min(minindex, minIndex)
             maxindex = min(maxindex, maxIndex)
             self.dump_vertices(minindex, maxindex - minindex)
 
         self.real.draw_range_elements(indexBuffer, indexSize, minIndex, maxIndex, mode, start, count)
-
-        self.dirty = True
+        self._set_dirty()
         
+    def _set_dirty(self):
+        if self.interpreter.options.step:
+            self._present()
+        else:
+            self.dirty = True
+
     def flush(self, flags):
         self.real.flush(flags)
         if self.dirty:
             if flags & gallium.PIPE_FLUSH_FRAME:
-                self._update()
+                self._present()
             self.dirty = False
         return None
 
     def clear(self, surface, value):
         self.real.surface_clear(surface, value)
         
-    def _update(self):
+    def _present(self):
         self.real.flush()
     
         if self.cbufs and self.cbufs[0]:
-            if images:
-                global image_no
-                image_no += 1
-                filename = 'cbuf_%04u.png' % image_no 
-                save_image(filename, self.cbufs[0])
-            else:
-                show_image(self.cbufs[0])
+            self.interpreter.present(self.cbufs[0], "cbuf")
     
 
 class Interpreter(parser.TraceDumper):
@@ -561,11 +549,13 @@ class Interpreter(parser.TraceDumper):
             ('pipe_screen', 'get_paramf'),
     ))
 
-    def __init__(self, stream):
+    def __init__(self, stream, options):
         parser.TraceDumper.__init__(self, stream)
+        self.options = options
         self.objects = {}
         self.result = None
         self.globl = Global(self, None)
+        self.image_no = 0
 
     def register_object(self, address, object):
         self.objects[address] = object
@@ -586,7 +576,7 @@ class Interpreter(parser.TraceDumper):
         if (call.klass, call.method) in self.ignore_calls:
             return
 
-        if verbose >= 1:
+        if self.verbosity(1):
             parser.TraceDumper.handle_call(self, call)
         
         args = [self.interpret_arg(arg) for name, arg in call.args] 
@@ -606,7 +596,33 @@ class Interpreter(parser.TraceDumper):
     def interpret_arg(self, node):
         translator = Translator(self)
         return translator.visit(node)
+
+    def verbosity(self, level):
+        return self.options.verbosity >= level
+
+    def present(self, surface, description):
+        if self.options.images:
+            self.image_no += 1
+            filename = '%s_%04u.png' % (description, self.image_no)
+            save_image(filename, surface)
+        else:
+            show_image(surface)
     
 
+class Main(parser.Main):
+
+    def get_optparser(self):
+        optparser = parser.Main.get_optparser(self)
+        optparser.add_option("-q", "--quiet", action="store_const", const=0, dest="verbosity", help="no messages")
+        optparser.add_option("-v", "--verbose", action="count", dest="verbosity", default=1, help="increase verbosity level")
+        optparser.add_option("-i", "--images", action="store_true", dest="images", default=False, help="save images instead of showing them")
+        optparser.add_option("-s", "--step", action="store_true", dest="step", default=False, help="step trhough every draw")
+        return optparser
+
+    def process_arg(self, stream, options):
+        parser = Interpreter(stream, options)
+        parser.parse()
+
+
 if __name__ == '__main__':
-    parser.main(Interpreter)
+    Main().main()
index 5205f2d8dd588e82b964744a39e5f4fc7b6ae690..db9bcc8226cf90efaf286ea8f007719a68fa52ef 100755 (executable)
@@ -30,6 +30,7 @@
 import sys
 import xml.parsers.expat
 import binascii
+import optparse
 
 from model import *
 
@@ -342,16 +343,39 @@ class TraceDumper(TraceParser):
         self.formatter.newline()
         
 
-def main(ParserFactory):
-    for arg in sys.argv[1:]:
-        if arg.endswith('.gz'):
-            import gzip
-            stream = gzip.GzipFile(arg, 'rt')
+class Main:
+    '''Common main class for all retrace command line utilities.''' 
+
+    def __init__(self):
+        pass
+
+    def main(self):
+        optparser = self.get_optparser()
+        (options, args) = optparser.parse_args(sys.argv[1:])
+    
+        if args:
+            for arg in args:
+                if arg.endswith('.gz'):
+                    from gzip import GzipFile
+                    stream = GzipFile(arg, 'rt')
+                elif arg.endswith('.bz2'):
+                    from bz2 import BZ2File
+                    stream = BZ2File(arg, 'rt')
+                else:
+                    stream = open(arg, 'rt')
+                self.process_arg(stream, options)
         else:
-            stream = open(arg, 'rt')
-        parser = ParserFactory(stream)
+            self.process_arg(stream, options)
+
+    def get_optparser(self):
+        optparser = optparse.OptionParser(
+            usage="\n\t%prog [options] [traces] ...")
+        return optparser
+
+    def process_arg(self, stream, options):
+        parser = TraceDumper(stream)
         parser.parse()
 
 
 if __name__ == '__main__':
-    main(TraceDumper)
+    Main().main()