From a9f774c67c9120d94d1cb79f2d6c1e86d51128e7 Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Wed, 24 Sep 2025 20:58:18 +0200 Subject: [PATCH] Use the mapped_file class instead of asset for assets --- client/asset.cpp | 118 -------------------------------- client/asset.h | 77 --------------------- client/hardware.cpp | 4 +- client/render/imgui_impl.cpp | 8 +-- client/render/scene_loader.cpp | 8 +-- client/scenes/app_launcher.cpp | 2 +- client/scenes/input_profile.cpp | 6 +- client/scenes/lobby.cpp | 14 ++-- client/scenes/lobby.h | 4 +- client/scenes/lobby_gui.cpp | 6 +- client/utils/i18n.cpp | 4 +- client/utils/i18n.h | 2 + client/utils/mapped_file.cpp | 115 +++++++++++++++++++++++++++---- client/utils/mapped_file.h | 16 ++++- 14 files changed, 143 insertions(+), 241 deletions(-) delete mode 100644 client/asset.cpp delete mode 100644 client/asset.h diff --git a/client/asset.cpp b/client/asset.cpp deleted file mode 100644 index 44a040fc..00000000 --- a/client/asset.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/* - * WiVRn VR streaming - * Copyright (C) 2024 Guillaume Meunier - * Copyright (C) 2024 Patrick Nicolas - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "asset.h" -#include "application.h" -#include "utils/files.h" -#include -#include -#include - -#ifdef __ANDROID__ -#include - -asset::asset(const std::filesystem::path & path) -{ - spdlog::debug("Loading Android asset {}", path.string()); - android_asset = AAssetManager_open(application::asset_manager(), path.c_str(), AASSET_MODE_BUFFER); - - if (!android_asset) - throw std::runtime_error("Cannot open Android asset " + path.string()); - - bytes = std::span{reinterpret_cast(AAsset_getBuffer(android_asset)), (size_t)AAsset_getLength64(android_asset)}; -} - -asset::asset(asset && other) : - android_asset(other.android_asset), - bytes(other.bytes) -{ - other.android_asset = nullptr; -} - -asset & asset::operator=(asset && other) -{ - std::swap(android_asset, other.android_asset); - std::swap(bytes, other.bytes); - return *this; -} - -asset::~asset() -{ - if (android_asset) - AAsset_close(android_asset); -} - -#else - -static std::filesystem::path get_exe_path() -{ - // Linux only: see https://stackoverflow.com/a/1024937 - return std::filesystem::read_symlink("/proc/self/exe"); -} - -static std::filesystem::path get_asset_root() -{ - const char * path = std::getenv("WIVRN_ASSET_ROOT"); - if (path && strcmp(path, "")) - return path; - - return get_exe_path().parent_path().parent_path() / "share" / "wivrn" / "assets"; -} - -static std::filesystem::path get_locale_root() -{ - const char * path = std::getenv("WIVRN_LOCALE_ROOT"); - if (path && strcmp(path, "")) - return path; - - return get_exe_path().parent_path().parent_path() / "share" / "locale"; -} - -std::filesystem::path asset::asset_root() -{ - static std::filesystem::path root = get_asset_root(); - return root; -} - -std::filesystem::path asset::locale_root() -{ - static std::filesystem::path root = get_locale_root(); - return root; -} - -asset::asset(const std::filesystem::path & path) -{ - assert(path.is_relative()); - - // TODO mmap - // TODO load only once if it is already loaded - - spdlog::debug("Loading file asset {}", path.string()); - - if (path.native().starts_with("locale/")) - { - bytes = utils::read_whole_file(locale_root() / path.native().substr(7)); - } - else - { - bytes = utils::read_whole_file(asset_root() / path); - } -} - -#endif diff --git a/client/asset.h b/client/asset.h deleted file mode 100644 index f3866a2c..00000000 --- a/client/asset.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * WiVRn VR streaming - * Copyright (C) 2024 Guillaume Meunier - * Copyright (C) 2024 Patrick Nicolas - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include -#include -#include -#include - -#ifdef __ANDROID__ -struct AAsset; -#endif - -class asset -{ -#ifdef __ANDROID__ - AAsset * android_asset = nullptr; - std::span bytes; -#else - static std::filesystem::path asset_root(); - static std::filesystem::path locale_root(); - std::vector bytes; -#endif - -public: - asset() = default; - asset(const std::filesystem::path & path); - -#ifdef __ANDROID__ - asset(asset && other); - asset & operator=(asset && other); - ~asset(); -#else - asset(asset && other) = default; - asset & operator=(asset && other) = default; -#endif - - asset(const asset &) = delete; - asset & operator=(const asset &) = delete; - - const std::byte * data() const - { - return bytes.data(); - } - - size_t size() const - { - return bytes.size(); - } - - operator std::span() const - { - return bytes; - } - - operator std::string() const - { - return std::string(reinterpret_cast(data()), size()); - } -}; diff --git a/client/hardware.cpp b/client/hardware.cpp index 85dde92f..63b64123 100644 --- a/client/hardware.cpp +++ b/client/hardware.cpp @@ -403,9 +403,9 @@ std::string controller_ray_model_name() case model::htc_vive_xr_elite: // XR Elite's runtime always assume alpha is unpremultiplied in the composition layers // Assume it's the same for all HTC headsets - return "ray-htc.glb"; + return "assets://ray-htc.glb"; default: - return "ray.glb"; + return "assets://ray.glb"; } } diff --git a/client/render/imgui_impl.cpp b/client/render/imgui_impl.cpp index 0ed35d6b..c45ee60e 100644 --- a/client/render/imgui_impl.cpp +++ b/client/render/imgui_impl.cpp @@ -21,10 +21,10 @@ #include "implot.h" #include "application.h" -#include "asset.h" #include "constants.h" #include "image_loader.h" #include "openxr/openxr.h" +#include "utils/mapped_file.h" #include "utils/ranges.h" #include "utils/strings.h" #include "vulkan/vulkan_enums.hpp" @@ -527,8 +527,8 @@ void imgui_context::initialize_fonts() } } - asset font_awesome_regular("Font Awesome 6 Free-Regular-400.otf"); - asset font_awesome_solid("Font Awesome 6 Free-Solid-900.otf"); + utils::mapped_file font_awesome_regular("assets://Font Awesome 6 Free-Regular-400.otf"); + utils::mapped_file font_awesome_solid("assets://Font Awesome 6 Free-Solid-900.otf"); ImFontConfig config; config.FontDataOwnedByAtlas = false; @@ -1041,7 +1041,7 @@ imgui_context::~imgui_context() ImTextureID imgui_textures::load_texture(const std::string & filename, vk::raii::Sampler && sampler) { - return load_texture(std::span{asset{filename}}, std::move(sampler), filename); + return load_texture(utils::mapped_file{filename}, std::move(sampler), filename); } ImTextureID imgui_textures::load_texture(const std::span & bytes, vk::raii::Sampler && sampler, const std::string & name) diff --git a/client/render/scene_loader.cpp b/client/render/scene_loader.cpp index cef8edfb..76d6eeda 100644 --- a/client/render/scene_loader.cpp +++ b/client/render/scene_loader.cpp @@ -18,11 +18,11 @@ #include "scene_loader.h" -#include "asset.h" #include "gpu_buffer.h" #include "image_loader.h" #include "render/scene_components.h" #include "render/vertex_layout.h" +#include "utils/mapped_file.h" #include "utils/ranges.h" #include "vk/shader.h" #include @@ -465,8 +465,8 @@ class loader_context thread_safe & queue; vk::raii::CommandPool & cb_pool; - std::vector loaded_assets; - asset & load_from_asset(const std::filesystem::path & path) + std::vector loaded_assets; + utils::mapped_file & load_from_asset(const std::filesystem::path & path) { try { @@ -1098,7 +1098,7 @@ std::shared_ptr scene_loader::operator()(const std::filesystem:: { spdlog::debug("Loading {}", gltf_path.native()); - asset asset_file(gltf_path); + utils::mapped_file asset_file(gltf_path); auto data_buffer = fastgltf::GltfDataBuffer::FromBytes(asset_file.data(), asset_file.size()); if (auto error = data_buffer.error(); error != fastgltf::Error::None) diff --git a/client/scenes/app_launcher.cpp b/client/scenes/app_launcher.cpp index 11b97dcc..2015b185 100644 --- a/client/scenes/app_launcher.cpp +++ b/client/scenes/app_launcher.cpp @@ -100,7 +100,7 @@ app_launcher::app_launcher( stream.queue_family_index, stream.queue) { - default_icon = textures.load_texture("default_icon.ktx2"); + default_icon = textures.load_texture("assets://default_icon.ktx2"); } app_launcher::~app_launcher() diff --git a/client/scenes/input_profile.cpp b/client/scenes/input_profile.cpp index 2ff6ef9b..efcf6e1c 100644 --- a/client/scenes/input_profile.cpp +++ b/client/scenes/input_profile.cpp @@ -19,9 +19,9 @@ #include "input_profile.h" #include "application.h" -#include "asset.h" #include "hardware.h" #include "render/scene_components.h" +#include "utils/mapped_file.h" #include "xr/space.h" #include #include @@ -175,10 +175,10 @@ struct json_visual_response input_profile::input_profile(scene & scene, const std::filesystem::path & json_profile, uint32_t layer_mask_controller, uint32_t layer_mask_ray) { - std::string json = asset(json_profile); + utils::mapped_file json{json_profile}; simdjson::dom::parser parser; - simdjson::dom::element root = parser.parse(json); + simdjson::dom::element root = parser.parse(reinterpret_cast(json.data()), json.size()); id = std::string(root["profileId"]); diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 1dd96dfd..5cf2f254 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -811,12 +811,12 @@ void scenes::lobby::on_focused() // assert(std::ranges::all_of(views, [width](const XrViewConfigurationView & view) { return view.recommendedImageRectWidth == width; })); // assert(std::ranges::all_of(views, [height](const XrViewConfigurationView & view) { return view.recommendedImageRectHeight == height; })); - lobby_entity = add_gltf("ground.glb", layer_lobby).first; + lobby_entity = add_gltf("assets://ground.glb", layer_lobby).first; std::string profile = controller_name(); input.emplace( *this, - "controllers/" + profile + "/profile.json", + "assets://controllers/" + profile + "/profile.json", layer_controllers, layer_rays); @@ -842,8 +842,8 @@ void scenes::lobby::on_focused() offset_orientation = glm::degrees(glm::eulerAngles(input->offset[xr::spaces::grip_left].second)); ray_offset = input->offset[xr::spaces::aim_left].first.z; - xyz_axes_left_controller = add_gltf("xyz-arrows.glb", layer_controllers).first; - xyz_axes_right_controller = add_gltf("xyz-arrows.glb", layer_controllers).first; + xyz_axes_left_controller = add_gltf("assets://xyz-arrows.glb", layer_controllers).first; + xyz_axes_right_controller = add_gltf("assets://xyz-arrows.glb", layer_controllers).first; #endif recenter_left_action = get_action("recenter_left").first; @@ -872,8 +872,8 @@ void scenes::lobby::on_focused() { left_hand = session.create_hand_tracker(XR_HAND_LEFT_EXT); right_hand = session.create_hand_tracker(XR_HAND_RIGHT_EXT); - hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "left-hand.glb", layer_controllers); - hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "right-hand.glb", layer_controllers); + hand_model::add_hand(*this, XR_HAND_LEFT_EXT, "assets://left-hand.glb", layer_controllers); + hand_model::add_hand(*this, XR_HAND_RIGHT_EXT, "assets://right-hand.glb", layer_controllers); imgui_inputs.push_back({.hand = &*left_hand}); imgui_inputs.push_back({.hand = &*right_hand}); } @@ -934,7 +934,7 @@ void scenes::lobby::on_focused() auto tm = std::localtime(&t); std::string image = tm->tm_mon == 5 ? "wivrn-pride" : "wivrn"; - about_picture = imgui_ctx->load_texture(image + ".ktx2"); + about_picture = imgui_ctx->load_texture("assets://" + image + ".ktx2"); setup_passthrough(); session.set_refresh_rate(application::get_config().preferred_refresh_rate.value_or(0)); diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 7f5cd7c8..eb003964 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -19,11 +19,11 @@ #pragma once -#include "asset.h" #include "configuration.h" #include "crypto.h" #include "scene.h" #include "scenes/lobby_keyboard.h" +#include "utils/mapped_file.h" #include "utils/thread_safe.h" #include "wifi_lock.h" #include "wivrn_config.h" @@ -75,7 +75,7 @@ class lobby : public scene_impl std::optional foveation; std::string selected_item; - std::unique_ptr license; + std::unique_ptr license; static inline const uint32_t layer_lobby = 1 << 0; static inline const uint32_t layer_controllers = 1 << 1; diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 168a5673..94eeb8e5 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -24,7 +24,6 @@ #include "android/battery.h" #endif #include "application.h" -#include "asset.h" #include "configuration.h" #include "constants.h" #include "imgui.h" @@ -32,6 +31,7 @@ #include "lobby.h" #include "scenes/stream.h" #include "utils/i18n.h" +#include "utils/mapped_file.h" #include "utils/overloaded.h" #include "version.h" #include "xr/body_tracker.h" @@ -1100,7 +1100,7 @@ void scenes::lobby::gui_licenses() selected_item = *components.begin(); try { - license = std::make_unique(std::filesystem::path("licenses") / selected_item); + license = std::make_unique(std::filesystem::path("assets://licenses") / selected_item); } catch (...) { @@ -1113,7 +1113,7 @@ void scenes::lobby::gui_licenses() { try { - auto current = std::make_unique(std::filesystem::path("licenses") / component); + auto current = std::make_unique(std::filesystem::path("assets://licenses") / component); if (ImGui::Selectable(component, component == selected_item, ImGuiSelectableFlags_SelectOnRelease)) { selected_item = component; diff --git a/client/utils/i18n.cpp b/client/utils/i18n.cpp index 3ce25fd6..911d9428 100644 --- a/client/utils/i18n.cpp +++ b/client/utils/i18n.cpp @@ -19,7 +19,7 @@ #include "i18n.h" -#include "asset.h" +#include "utils/mapped_file.h" #include #include #include @@ -58,7 +58,7 @@ std::vector open_locale_file(const std::string & file_name, const std::str std::vector buffer; try { - asset file(file_name); + utils::mapped_file file("assets://" + file_name); buffer.resize(file.size()); memcpy(buffer.data(), file.data(), file.size()); } diff --git a/client/utils/i18n.h b/client/utils/i18n.h index 45cf14fa..819ece17 100644 --- a/client/utils/i18n.h +++ b/client/utils/i18n.h @@ -17,6 +17,8 @@ * along with this program. If not, see . */ +#pragma once + #include #define _(x) boost::locale::gettext(x) diff --git a/client/utils/mapped_file.cpp b/client/utils/mapped_file.cpp index ea7441ee..ef27cba0 100644 --- a/client/utils/mapped_file.cpp +++ b/client/utils/mapped_file.cpp @@ -17,6 +17,7 @@ */ #include "mapped_file.h" +#include "application.h" #include #include #include @@ -24,38 +25,82 @@ #include #include +#ifdef __ANDROID__ +#include +#endif + +namespace +{ +#ifndef __ANDROID__ +std::filesystem::path get_exe_path() +{ + // Linux only: see https://stackoverflow.com/a/1024937 + return std::filesystem::read_symlink("/proc/self/exe"); +} + +std::filesystem::path asset_root() +{ + static std::filesystem::path root = []() -> std::filesystem::path { + const char * path = std::getenv("WIVRN_ASSET_ROOT"); + if (path && strcmp(path, "")) + return path; + + return get_exe_path().parent_path().parent_path() / "share" / "wivrn" / "assets"; + }(); + + return root; +} + +std::filesystem::path locale_root() +{ + static std::filesystem::path root = []() -> std::filesystem::path { + const char * path = std::getenv("WIVRN_LOCALE_ROOT"); + if (path && strcmp(path, "")) + return path; + + return get_exe_path().parent_path().parent_path() / "share" / "locale"; + }(); + + return root; +} +#endif +} // namespace + utils::mapped_file::~mapped_file() { - if (fd >= 0) - { - ::munmap(bytes.data(), bytes.size()); - ::close(fd); - } +#ifdef __ANDROID__ + if (android_asset) + AAsset_close(android_asset); + else + ::munmap(const_cast(bytes.data()), bytes.size()); +#else + ::munmap(const_cast(bytes.data()), bytes.size()); +#endif } utils::mapped_file::mapped_file(mapped_file && other) : - fd(other.fd), +#ifdef __ANDROID__ + android_asset(other.android_asset), +#endif bytes(other.bytes) { - other.fd = -1; +#ifdef __ANDROID__ + other.android_asset = nullptr; +#endif other.bytes = {}; } utils::mapped_file & utils::mapped_file::operator=(mapped_file && other) { - std::swap(fd, other.fd); +#ifdef __ANDROID__ + std::swap(android_asset, other.android_asset); +#endif std::swap(bytes, other.bytes); return *this; } -utils::mapped_file::mapped_file(const std::filesystem::path & path) : - mapped_file(::open(path.c_str(), O_RDONLY)) -{ -} - -utils::mapped_file::mapped_file(int fd) : - fd(fd) +void utils::mapped_file::map(int fd) { off_t size = lseek(fd, 0, SEEK_END); void * addr = ::mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0 /* offset */); @@ -70,3 +115,43 @@ utils::mapped_file::mapped_file(int fd) : bytes = {reinterpret_cast(addr), (size_t)size}; } + +void utils::mapped_file::open(const std::filesystem::path & path) +{ + int fd = ::open(path.native().c_str(), O_RDONLY); + + if (fd < 0) + throw std::system_error(errno, std::system_category(), "open " + std::string{path}); + + map(fd); + ::close(fd); +} + +utils::mapped_file::mapped_file(const std::filesystem::path & path) +{ + if (path.native().starts_with("assets://")) + { + std::filesystem::path asset_path = path.native().substr(9); + +#ifdef __ANDROID__ + android_asset = AAssetManager_open(application::asset_manager(), asset_path.c_str(), AASSET_MODE_BUFFER); + + if (!android_asset) + throw std::runtime_error("Cannot open Android asset " + path.string()); + + bytes = {reinterpret_cast(AAsset_getBuffer(android_asset)), (size_t)AAsset_getLength64(android_asset)}; +#else + if (asset_path.native().starts_with("locale/")) + open(locale_root() / asset_path.native().substr(7)); + else + open(asset_root() / asset_path.native()); +#endif + } + else + open(path); +} + +utils::mapped_file::mapped_file(int fd) +{ + map(fd); +} diff --git a/client/utils/mapped_file.h b/client/utils/mapped_file.h index c03e250f..914fee0a 100644 --- a/client/utils/mapped_file.h +++ b/client/utils/mapped_file.h @@ -22,12 +22,22 @@ #include #include +#ifdef __ANDROID__ +struct AAsset; +#endif + namespace utils { class mapped_file { - int fd = -1; - std::span bytes{}; +#ifdef __ANDROID__ + AAsset * android_asset = nullptr; +#endif + + std::span bytes{}; + + void map(int fd); + void open(const std::filesystem::path & path); public: mapped_file() = default; @@ -37,7 +47,7 @@ public: mapped_file & operator=(mapped_file &&); ~mapped_file(); - mapped_file(int fd); // Takes ownership of the file descriptor + mapped_file(int fd); // Does not take ownership of the file descriptor mapped_file(const std::filesystem::path & path); operator std::span() const