From 31666632caf9e5f0eea8d8d839e7120e38be97f9 Mon Sep 17 00:00:00 2001
From: Tony Wasserka <NeoBrainX@gmail.com>
Date: Sat, 17 May 2014 22:34:55 +0200
Subject: [PATCH] Add initial graphics debugger interface.

---
 src/core/hle/service/gsp.cpp              |  6 ++
 src/video_core/gpu_debugger.h             | 97 +++++++++++++++++++++++
 src/video_core/video_core.vcxproj         |  5 +-
 src/video_core/video_core.vcxproj.filters |  3 +-
 4 files changed, 108 insertions(+), 3 deletions(-)
 create mode 100644 src/video_core/gpu_debugger.h

diff --git a/src/core/hle/service/gsp.cpp b/src/core/hle/service/gsp.cpp
index 3dda4c934..a42759053 100644
--- a/src/core/hle/service/gsp.cpp
+++ b/src/core/hle/service/gsp.cpp
@@ -16,6 +16,9 @@
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
+// Main graphics debugger object - TODO: Here is probably not the best place for this
+GraphicsDebugger g_debugger;
+
 /// GSP shared memory GX command buffer header
 union GX_CmdBufferHeader {
     u32 hex;
@@ -45,6 +48,9 @@ static inline u8* GX_GetCmdBufferPointer(u32 thread_id, u32 offset=0) {
 /// Finishes execution of a GSP command
 void GX_FinishCommand(u32 thread_id) {
     GX_CmdBufferHeader* header = (GX_CmdBufferHeader*)GX_GetCmdBufferPointer(thread_id);
+
+    g_debugger.GXCommandProcessed(GX_GetCmdBufferPointer(thread_id, 0x20 + (header->index * 0x20)));
+
     header->number_commands = header->number_commands - 1;
     // TODO: Increment header->index?
 }
diff --git a/src/video_core/gpu_debugger.h b/src/video_core/gpu_debugger.h
new file mode 100644
index 000000000..ace9de95f
--- /dev/null
+++ b/src/video_core/gpu_debugger.h
@@ -0,0 +1,97 @@
+// Copyright 2014 Citra Emulator Project
+// Licensed under GPLv2
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <algorithm>
+#include <functional>
+#include <vector>
+
+#include "common/log.h"
+
+#include "core/hle/service/gsp.h"
+
+
+class GraphicsDebugger
+{
+public:
+    // Base class for all objects which need to be notified about GPU events
+    class DebuggerObserver
+    {
+    public:
+        DebuggerObserver() : observed(nullptr) { }
+
+        virtual ~DebuggerObserver()
+        {
+            if (observed)
+                observed->UnregisterObserver(this);
+        }
+
+        /**
+        * Called when a GX command has been processed and is ready for being
+        * read via GraphicsDebugger::ReadGXCommandHistory.
+        * @param total_command_count Total number of commands in the GX history
+        * @note All methods in this class are called from the GSP thread
+        */
+        virtual void GXCommandProcessed(int total_command_count)
+        {
+            const GSP_GPU::GXCommand& cmd = observed->ReadGXCommandHistory(total_command_count-1);
+            ERROR_LOG(GSP, "Received command: id=%x", cmd.id);
+        }
+
+    protected:
+        GraphicsDebugger* GetDebugger()
+        {
+            return observed;
+        }
+
+    private:
+        GraphicsDebugger* observed;
+        bool in_destruction;
+
+        friend class GraphicsDebugger;
+    };
+
+    void GXCommandProcessed(u8* command_data)
+    {
+        gx_command_history.push_back(GSP_GPU::GXCommand());
+        GSP_GPU::GXCommand& cmd = gx_command_history[gx_command_history.size()-1];
+
+        const int cmd_length = sizeof(GSP_GPU::GXCommand);
+        memcpy(cmd.data, command_data, cmd_length);
+
+        ForEachObserver([this](DebuggerObserver* observer) {
+                          observer->GXCommandProcessed(this->gx_command_history.size());
+                        } );
+    }
+
+    const GSP_GPU::GXCommand& ReadGXCommandHistory(int index) const
+    {
+        // TODO: Is this thread-safe?
+        return gx_command_history[index];
+    }
+
+    void RegisterObserver(DebuggerObserver* observer)
+    {
+        // TODO: Check for duplicates
+        observers.push_back(observer);
+        observer->observed = this;
+    }
+
+    void UnregisterObserver(DebuggerObserver* observer)
+    {
+        std::remove(observers.begin(), observers.end(), observer);
+        observer->observed = nullptr;
+    }
+
+private:
+    void ForEachObserver(std::function<void (DebuggerObserver*)> func)
+    {
+        std::for_each(observers.begin(),observers.end(), func);
+    }
+
+    std::vector<DebuggerObserver*> observers;
+
+    std::vector<GSP_GPU::GXCommand> gx_command_history;
+};
diff --git a/src/video_core/video_core.vcxproj b/src/video_core/video_core.vcxproj
index d8c53271e..a4df92386 100644
--- a/src/video_core/video_core.vcxproj
+++ b/src/video_core/video_core.vcxproj
@@ -24,10 +24,11 @@
     <ClCompile Include="video_core.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="gpu_debugger.h" />
     <ClInclude Include="renderer_base.h" />
-    <ClInclude Include="renderer_opengl\renderer_opengl.h" />
     <ClInclude Include="utils.h" />
     <ClInclude Include="video_core.h" />
+    <ClInclude Include="renderer_opengl\renderer_opengl.h" />
   </ItemGroup>
   <ItemGroup>
     <Text Include="CMakeLists.txt" />
@@ -128,4 +129,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
diff --git a/src/video_core/video_core.vcxproj.filters b/src/video_core/video_core.vcxproj.filters
index 4eb2ef0a4..cc173a718 100644
--- a/src/video_core/video_core.vcxproj.filters
+++ b/src/video_core/video_core.vcxproj.filters
@@ -16,6 +16,7 @@
     <ClInclude Include="renderer_opengl\renderer_opengl.h">
       <Filter>renderer_opengl</Filter>
     </ClInclude>
+    <ClInclude Include="gpu_debugger.h" />
     <ClInclude Include="renderer_base.h" />
     <ClInclude Include="utils.h" />
     <ClInclude Include="video_core.h" />
@@ -23,4 +24,4 @@
   <ItemGroup>
     <Text Include="CMakeLists.txt" />
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>