2014-05-17 13:34:55 -07:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
2014-12-16 21:38:14 -08:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-05-17 13:34:55 -07:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
#include <vector>
|
2014-10-28 20:08:37 -07:00
|
|
|
#include "core/hle/service/gsp_gpu.h"
|
2014-07-26 05:42:46 -07:00
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
class GraphicsDebugger {
|
2014-05-17 13:34:55 -07:00
|
|
|
public:
|
|
|
|
// Base class for all objects which need to be notified about GPU events
|
2016-09-17 17:38:01 -07:00
|
|
|
class DebuggerObserver {
|
2014-05-17 13:34:55 -07:00
|
|
|
public:
|
2016-09-18 18:01:46 -07:00
|
|
|
DebuggerObserver() : observed(nullptr) {}
|
2014-05-17 13:34:55 -07:00
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
virtual ~DebuggerObserver() {
|
2014-05-17 13:34:55 -07:00
|
|
|
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
|
|
|
|
*/
|
2016-09-17 17:38:01 -07:00
|
|
|
virtual void GXCommandProcessed(int total_command_count) {
|
2016-12-10 04:51:50 -08:00
|
|
|
const Service::GSP::Command& cmd =
|
|
|
|
observed->ReadGXCommandHistory(total_command_count - 1);
|
2014-12-05 17:53:49 -08:00
|
|
|
LOG_TRACE(Debug_GPU, "Received command: id=%x", (int)cmd.id.Value());
|
2014-05-17 13:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-09-17 17:38:01 -07:00
|
|
|
const GraphicsDebugger* GetDebugger() const {
|
2014-05-17 13:34:55 -07:00
|
|
|
return observed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GraphicsDebugger* observed;
|
|
|
|
|
|
|
|
friend class GraphicsDebugger;
|
|
|
|
};
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
void GXCommandProcessed(u8* command_data) {
|
2014-06-24 12:27:18 -07:00
|
|
|
if (observers.empty())
|
|
|
|
return;
|
|
|
|
|
2015-02-17 14:42:28 -08:00
|
|
|
gx_command_history.emplace_back();
|
2016-12-10 04:51:50 -08:00
|
|
|
Service::GSP::Command& cmd = gx_command_history.back();
|
2014-05-17 13:34:55 -07:00
|
|
|
|
2016-12-10 04:51:50 -08:00
|
|
|
memcpy(&cmd, command_data, sizeof(Service::GSP::Command));
|
2014-05-17 13:34:55 -07:00
|
|
|
|
|
|
|
ForEachObserver([this](DebuggerObserver* observer) {
|
2016-09-17 17:38:01 -07:00
|
|
|
observer->GXCommandProcessed(static_cast<int>(this->gx_command_history.size()));
|
|
|
|
});
|
2014-05-17 13:34:55 -07:00
|
|
|
}
|
|
|
|
|
2016-12-10 04:51:50 -08:00
|
|
|
const Service::GSP::Command& ReadGXCommandHistory(int index) const {
|
2014-05-17 13:34:55 -07:00
|
|
|
// TODO: Is this thread-safe?
|
|
|
|
return gx_command_history[index];
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
void RegisterObserver(DebuggerObserver* observer) {
|
2014-05-17 13:34:55 -07:00
|
|
|
// TODO: Check for duplicates
|
|
|
|
observers.push_back(observer);
|
|
|
|
observer->observed = this;
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
void UnregisterObserver(DebuggerObserver* observer) {
|
2014-12-18 15:42:24 -08:00
|
|
|
observers.erase(std::remove(observers.begin(), observers.end(), observer), observers.end());
|
2014-05-17 13:34:55 -07:00
|
|
|
observer->observed = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-17 17:38:01 -07:00
|
|
|
void ForEachObserver(std::function<void(DebuggerObserver*)> func) {
|
|
|
|
std::for_each(observers.begin(), observers.end(), func);
|
2014-05-17 13:34:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<DebuggerObserver*> observers;
|
|
|
|
|
2016-12-10 04:51:50 -08:00
|
|
|
std::vector<Service::GSP::Command> gx_command_history;
|
2014-05-17 13:34:55 -07:00
|
|
|
};
|