From 85e2cc08444ef38b46e4d648f49769c4a7ab39be Mon Sep 17 00:00:00 2001 From: Doris Wu Date: Sat, 18 Jan 2025 10:47:22 +0800 Subject: [PATCH] Create data transmitting class for fgviewer (#8332) * Introduce FrameGraphInfo class * Move the assignment into pimpl * Make ctors explicit * Add ctors to fg info structs * Revert the macro change to align with existing * Address the comments * Remove pimpl and move func def to .cc * Fix * Address the comment --- libs/fgviewer/CMakeLists.txt | 2 + libs/fgviewer/include/fgviewer/DebugServer.h | 23 ++---- .../include/fgviewer/FrameGraphInfo.h | 82 +++++++++++++++++++ libs/fgviewer/src/DebugServer.cpp | 27 +++--- libs/fgviewer/src/FrameGraphInfo.cpp | 46 +++++++++++ 5 files changed, 151 insertions(+), 29 deletions(-) create mode 100644 libs/fgviewer/include/fgviewer/FrameGraphInfo.h create mode 100644 libs/fgviewer/src/FrameGraphInfo.cpp diff --git a/libs/fgviewer/CMakeLists.txt b/libs/fgviewer/CMakeLists.txt index cf6bdb526df..892232f433a 100644 --- a/libs/fgviewer/CMakeLists.txt +++ b/libs/fgviewer/CMakeLists.txt @@ -15,12 +15,14 @@ endif() set(PUBLIC_HDRS include/fgviewer/DebugServer.h include/fgviewer/JsonWriter.h + include/fgviewer/FrameGraphInfo.h ) set(SRCS src/ApiHandler.cpp src/ApiHandler.h src/DebugServer.cpp + src/FrameGraphInfo.cpp ) # ================================================================================================== diff --git a/libs/fgviewer/include/fgviewer/DebugServer.h b/libs/fgviewer/include/fgviewer/DebugServer.h index ef054483334..d56addf9097 100644 --- a/libs/fgviewer/include/fgviewer/DebugServer.h +++ b/libs/fgviewer/include/fgviewer/DebugServer.h @@ -17,6 +17,8 @@ #ifndef FGVIEWER_DEBUGSERVER_H #define FGVIEWER_DEBUGSERVER_H +#include + #include #include @@ -27,17 +29,9 @@ class CivetServer; namespace filament::fgviewer { -using FrameGraphInfoKey = uint32_t; +using ViewHandle = uint32_t; -struct FrameGraphPassInfo { - utils::CString pass_name; - // TODO: Add struct detail properties -}; -struct FrameGraphInfo { - utils::CString view_name; - std::vector passes; -}; /** * Server-side frame graph debugger. @@ -50,30 +44,31 @@ class DebugServer { static std::string_view const kSuccessHeader; static std::string_view const kErrorHeader; - DebugServer(int port); + explicit DebugServer(int port); ~DebugServer(); /** * Notifies the debugger that a new view has been added. */ - void addView(const utils::CString& name, FrameGraphInfo info); + ViewHandle createView(utils::CString name); /** * Notifies the debugger that the given view has been deleted. */ - void removeView(const utils::CString& name); + void destroyView(ViewHandle h); /** * Updates the information for a given view. */ - void updateView(const utils::CString& name, FrameGraphInfo info); + void update(ViewHandle h, FrameGraphInfo info); bool isReady() const { return mServer; } private: CivetServer* mServer; - std::unordered_map mViews; + std::unordered_map mViews; + uint32_t mViewCounter = 0; mutable utils::Mutex mViewsMutex; class FileRequestHandler* mFileHandler = nullptr; diff --git a/libs/fgviewer/include/fgviewer/FrameGraphInfo.h b/libs/fgviewer/include/fgviewer/FrameGraphInfo.h new file mode 100644 index 00000000000..7de8dd02def --- /dev/null +++ b/libs/fgviewer/include/fgviewer/FrameGraphInfo.h @@ -0,0 +1,82 @@ +/* +* Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FGVIEWER_FRAMEGRAPHINFO_H +#define FGVIEWER_FRAMEGRAPHINFO_H + +#include + +#include +#include +#include + +namespace filament::fgviewer { +using ResourceId = uint32_t; + +class FrameGraphInfo { +public: + explicit FrameGraphInfo(utils::CString viewName); + + ~FrameGraphInfo(); + + FrameGraphInfo(FrameGraphInfo &&rhs) noexcept; + + FrameGraphInfo(FrameGraphInfo const &) = delete; + + struct Pass { + Pass(utils::CString name, std::vector reads, + std::vector writes); + + utils::CString name; + std::vector reads; + std::vector writes; + }; + + struct Resource { + struct Property { + utils::CString name; + utils::CString value; + }; + + Resource(ResourceId id, utils::CString name, + std::vector properties); + + ResourceId id; + utils::CString name; + // We use a vector of Property here to store the resource properties, + // so different kinds of resources could choose different types of + // properties to record. + // ex. + // Texture2D --> { {"name","XXX"}, {"sizeX", "1024"}, {"sizeY", "768"} } + // Buffer1D --> { {"name", "XXX"}, {"size", "512"} } + std::vector properties; + }; + + void setResources(std::unordered_map resources); + + // The incoming passes should be sorted by the execution order. + void setPasses(std::vector sortedPasses); + +private: + utils::CString viewName; + // The order of the passes in the vector indicates the execution + // order of the passes. + std::vector passes; + std::unordered_map resources; +}; +} // namespace filament::fgviewer + +#endif //FGVIEWER_FRAMEGRAPHINFO_H diff --git a/libs/fgviewer/src/DebugServer.cpp b/libs/fgviewer/src/DebugServer.cpp index ce1ccaa36b2..70d2af08947 100644 --- a/libs/fgviewer/src/DebugServer.cpp +++ b/libs/fgviewer/src/DebugServer.cpp @@ -15,6 +15,7 @@ */ #include +#include #include "ApiHandler.h" @@ -31,12 +32,6 @@ namespace filament::fgviewer { namespace { std::string const BASE_URL = "libs/fgviewer/web"; - -FrameGraphInfoKey getKeybyString(const utils::CString &input, - uint32_t seed) { - return utils::hash::murmurSlow(reinterpret_cast( - input.c_str()), input.size(), 0); -} } // anonymous using namespace utils; @@ -49,6 +44,7 @@ std::string_view const DebugServer::kErrorHeader = "HTTP/1.1 404 Not Found\r\nContent-Type: %s\r\n" "Connection: close\r\n\r\n"; + class FileRequestHandler : public CivetHandler { public: FileRequestHandler(DebugServer* server) : mServer(server) {} @@ -110,22 +106,23 @@ DebugServer::~DebugServer() { delete mServer; } -void DebugServer::addView(const utils::CString &name, FrameGraphInfo info) { +ViewHandle DebugServer::createView(utils::CString name) { std::unique_lock lock(mViewsMutex); - const FrameGraphInfoKey key = getKeybyString(name, 0); - mViews.insert({key, info}); + ViewHandle handle = mViewCounter++; + mViews.emplace(handle, FrameGraphInfo(std::move(name))); + + return handle; } -void DebugServer::removeView(const utils::CString& name) { +void DebugServer::destroyView(ViewHandle h) { std::unique_lock lock(mViewsMutex); - const FrameGraphInfoKey key = getKeybyString(name, 0); - mViews.erase(key); + mViews.erase(h); } -void DebugServer::updateView(const utils::CString& name, FrameGraphInfo info) { +void DebugServer::update(ViewHandle h, FrameGraphInfo info) { std::unique_lock lock(mViewsMutex); - const FrameGraphInfoKey key = getKeybyString(name, 0); - mViews[key] = info; + mViews.erase(h); + mViews.emplace(h, std::move(info)); } } // namespace filament::fgviewer diff --git a/libs/fgviewer/src/FrameGraphInfo.cpp b/libs/fgviewer/src/FrameGraphInfo.cpp new file mode 100644 index 00000000000..59fcaea489b --- /dev/null +++ b/libs/fgviewer/src/FrameGraphInfo.cpp @@ -0,0 +1,46 @@ +/* +* Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +namespace filament::fgviewer { + +FrameGraphInfo::FrameGraphInfo(utils::CString viewName): + viewName(std::move(viewName)), passes({}), resources({}) {} + +FrameGraphInfo::~FrameGraphInfo() = default; + +FrameGraphInfo::FrameGraphInfo(FrameGraphInfo&& rhs) noexcept = default; + +FrameGraphInfo::Pass::Pass(utils::CString name, std::vector reads, + std::vector writes): name(std::move(name)), + reads(std::move(reads)), + writes(std::move(writes)) {} + +FrameGraphInfo::Resource::Resource(ResourceId id, utils::CString name, + std::vector properties): id(id), + name(std::move(name)), properties(std::move(properties)) {} + +void FrameGraphInfo::setResources( + std::unordered_map resources) { + this->resources = std::move(resources); +} + +void FrameGraphInfo::setPasses(std::vector sortedPasses) { + passes = std::move(sortedPasses); +} + +} // namespace filament::fgviewer