swr: [rasterizer archrast] add mako template
authorTim Rowley <timothy.o.rowley@intel.com>
Wed, 5 Oct 2016 18:45:12 +0000 (13:45 -0500)
committerTim Rowley <timothy.o.rowley@intel.com>
Tue, 11 Oct 2016 16:22:04 +0000 (11:22 -0500)
Add template for generating code to save events to a file.

Signed-off-by: Tim Rowley <timothy.o.rowley@intel.com>
src/gallium/drivers/swr/Makefile.am
src/gallium/drivers/swr/rasterizer/archrast/archrast.cpp
src/gallium/drivers/swr/rasterizer/scripts/gen_archrast.py
src/gallium/drivers/swr/rasterizer/scripts/templates/ar_eventhandlerfile_h.template [new file with mode: 0644]

index 154f0ca8202786ce3f086ee767bc32be97d95b0c..4299489af4a3f42eb2403384040d4366f90902c0 100644 (file)
@@ -60,7 +60,8 @@ BUILT_SOURCES = \
        rasterizer/jitter/builder_x86.cpp \
        rasterizer/archrast/gen_ar_event.h \
        rasterizer/archrast/gen_ar_event.cpp \
-       rasterizer/archrast/gen_ar_eventhandler.h
+       rasterizer/archrast/gen_ar_eventhandler.h \
+       rasterizer/archrast/gen_ar_eventhandlerfile.h
 
 MKDIR_GEN = $(AM_V_at)$(MKDIR_P) $(@D)
 PYTHON_GEN = $(AM_V_GEN)$(PYTHON2) $(PYTHON_FLAGS)
@@ -137,6 +138,14 @@ rasterizer/archrast/gen_ar_eventhandler.h: rasterizer/scripts/gen_archrast.py ra
                --output rasterizer/archrast/gen_ar_eventhandler.h \
                --gen_eventhandler_h
 
+rasterizer/archrast/gen_ar_eventhandlerfile.h: rasterizer/scripts/gen_archrast.py rasterizer/scripts/templates/ar_eventhandlerfile_h.template rasterizer/archrast/events.proto
+       $(MKDIR_GEN)
+       $(PYTHON_GEN) \
+               $(srcdir)/rasterizer/scripts/gen_archrast.py \
+               --proto $(srcdir)/rasterizer/archrast/events.proto \
+               --output rasterizer/archrast/gen_ar_eventhandlerfile.h \
+               --gen_eventhandlerfile_h
+
 COMMON_LIBADD = \
        $(top_builddir)/src/gallium/auxiliary/libgallium.la \
        $(top_builddir)/src/mesa/libmesagallium.la \
index 48dec28a1059de10c6bae606fbc96e8e97d04244..b1c5134caf18067a5a9718db1f25c57a6b2470b9 100644 (file)
 * @brief Definitions for archrast.
 *
 ******************************************************************************/
+#include <atomic>
+
 #include "common/os.h"
 #include "archrast/archrast.h"
 #include "archrast/eventmanager.h"
+#include "gen_ar_eventhandlerfile.h"
 
 namespace ArchRast
 {
@@ -39,8 +42,12 @@ namespace ArchRast
     // Construct an event manager and associate a handler with it.
     HANDLE CreateThreadContext()
     {
+        // Can we assume single threaded here?
+        static std::atomic<uint32_t> counter(0);
+        uint32_t id = counter.fetch_add(1);
+
         EventManager* pManager = new EventManager();
-        EventHandler* pHandler = new EventHandler();
+        EventHandler* pHandler = new EventHandlerFile(id);
 
         if (pManager && pHandler)
         {
index deb16bed8cd05a9865ff91331e88c32c1bfae2d1..1b89a91b8fd69da20014bfabbf0e7de089ec4a00 100644 (file)
@@ -103,6 +103,7 @@ def parse_protos(filename):
         protos['enums'] = {}
         protos['enum_names'] = []
 
+        eventId = 0
         raw_text = []
         while idx < len(lines):
             line = lines[idx].rstrip()
@@ -112,10 +113,12 @@ def parse_protos(filename):
             match = re.match(r"(\s*)event(\s*)(\w+)", line)
 
             if match:
+                eventId += 1
                 event_name = match.group(3)
                 protos['event_names'].append(event_name)
 
                 protos['events'][event_name] = {}
+                protos['events'][event_name]['event_id'] = eventId
                 idx = parse_event_fields(lines, idx, protos['events'][event_name])
 
             # search for enums.
@@ -139,6 +142,7 @@ def main():
     parser.add_argument("--gen_event_h", "-geh", help="Generate event header", action="store_true", default=False)
     parser.add_argument("--gen_event_cpp", "-gec", help="Generate event cpp", action="store_true", default=False)
     parser.add_argument("--gen_eventhandler_h", "-gehh", help="Generate eventhandler header", action="store_true", default=False)
+    parser.add_argument("--gen_eventhandlerfile_h", "-gehf", help="Generate eventhandler header for writing to files", action="store_true", default=False)
     args = parser.parse_args()
 
     proto_filename = args.proto
@@ -188,6 +192,17 @@ def main():
                 event_header="gen_ar_event.h",   # todo: fix this!
                 protos=protos)
 
+    # Generate event handler header
+    if args.gen_eventhandlerfile_h:
+        curdir = os.path.dirname(os.path.abspath(__file__))
+        template_file = os.sep.join([curdir, 'templates', 'ar_eventhandlerfile_h.template'])
+        output_fullpath = os.sep.join([output_dir, output_filename])
+
+        write_template_to_file(template_file, output_fullpath,
+                filename=output_filename,
+                event_header="gen_ar_eventhandler.h",   # todo: fix this!
+                protos=protos)
+
     return 0
 
 if __name__ == '__main__':
diff --git a/src/gallium/drivers/swr/rasterizer/scripts/templates/ar_eventhandlerfile_h.template b/src/gallium/drivers/swr/rasterizer/scripts/templates/ar_eventhandlerfile_h.template
new file mode 100644 (file)
index 0000000..6264442
--- /dev/null
@@ -0,0 +1,84 @@
+/****************************************************************************
+* Copyright (C) 2016 Intel Corporation.   All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice (including the next
+* paragraph) shall be included in all copies or substantial portions of the
+* Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+* IN THE SOFTWARE.
+*
+* @file ${filename}
+*
+* @brief Event handler interface.  auto-generated file
+* 
+* DO NOT EDIT
+* 
+******************************************************************************/
+#pragma once
+
+#include "common/os.h"
+#include "${event_header}"
+#include <fstream>
+
+namespace ArchRast
+{
+    //////////////////////////////////////////////////////////////////////////
+    /// EventHandlerFile - interface for handling events.
+    //////////////////////////////////////////////////////////////////////////
+    class EventHandlerFile : public EventHandler
+    {
+    public:
+        EventHandlerFile(uint32_t id)
+        {
+#if defined(_WIN32)
+            char buf[255];
+            // There could be multiple threads creating thread pools. We
+            // want to make sure they are uniquly identified by adding in
+            // the creator's thread id into the filename.
+            sprintf(buf, "\\ar_event%d_%d.bin", GetCurrentThreadId(), id);
+            mFilename = std::string(buf);
+#else
+            SWR_ASSERT(0);
+#endif
+        }
+
+        ~EventHandlerFile()
+        {
+            if (mFile.is_open()) mFile.close();
+        }
+
+        void write(uint32_t eventId, const char* pBlock, uint32_t size)
+        {
+            if (!mFile.is_open())
+            {
+                mFile.open(mFilename, std::ios::out | std::ios::app | std::ios::binary);
+            }
+
+            mFile.write((char*)&eventId, sizeof(eventId));
+            mFile.write(pBlock, size);
+        }
+
+% for name in protos['event_names']:
+        virtual void handle(${name}& event)
+        {
+            write(${protos['events'][name]['event_id']}, (char*)&event, sizeof(event));
+        }
+% endfor
+
+        std::ofstream mFile;
+        std::string mFilename;
+    };
+}