Use the mapped_file class instead of asset for assets

This commit is contained in:
Guillaume Meunier
2025-10-25 10:30:27 +02:00
parent 7963efc034
commit a9f774c67c
14 changed files with 143 additions and 241 deletions
-118
View File
@@ -1,118 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "asset.h"
#include "application.h"
#include "utils/files.h"
#include <filesystem>
#include <fstream>
#include <spdlog/spdlog.h>
#ifdef __ANDROID__
#include <android/asset_manager.h>
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<const std::byte>{reinterpret_cast<const std::byte *>(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<std::byte>(locale_root() / path.native().substr(7));
}
else
{
bytes = utils::read_whole_file<std::byte>(asset_root() / path);
}
}
#endif
-77
View File
@@ -1,77 +0,0 @@
/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <guillaume.meunier@centraliens.net>
* Copyright (C) 2024 Patrick Nicolas <patricknicolas@laposte.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstddef>
#include <filesystem>
#include <span>
#include <vector>
#ifdef __ANDROID__
struct AAsset;
#endif
class asset
{
#ifdef __ANDROID__
AAsset * android_asset = nullptr;
std::span<const std::byte> bytes;
#else
static std::filesystem::path asset_root();
static std::filesystem::path locale_root();
std::vector<std::byte> 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 std::byte>() const
{
return bytes;
}
operator std::string() const
{
return std::string(reinterpret_cast<const char *>(data()), size());
}
};
+2 -2
View File
@@ -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";
}
}
+4 -4
View File
@@ -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<const std::byte>{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<const std::byte> & bytes, vk::raii::Sampler && sampler, const std::string & name)
+4 -4
View File
@@ -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 <algorithm>
@@ -465,8 +465,8 @@ class loader_context
thread_safe<vk::raii::Queue> & queue;
vk::raii::CommandPool & cb_pool;
std::vector<asset> loaded_assets;
asset & load_from_asset(const std::filesystem::path & path)
std::vector<utils::mapped_file> loaded_assets;
utils::mapped_file & load_from_asset(const std::filesystem::path & path)
{
try
{
@@ -1098,7 +1098,7 @@ std::shared_ptr<entt::registry> 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)
+1 -1
View File
@@ -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()
+3 -3
View File
@@ -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 <entt/entt.hpp>
#include <glm/gtc/matrix_access.hpp>
@@ -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<const char *>(json.data()), json.size());
id = std::string(root["profileId"]);
+7 -7
View File
@@ -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));
+2 -2
View File
@@ -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<lobby>
std::optional<xr::foveation_profile> foveation;
std::string selected_item;
std::unique_ptr<asset> license;
std::unique_ptr<utils::mapped_file> license;
static inline const uint32_t layer_lobby = 1 << 0;
static inline const uint32_t layer_controllers = 1 << 1;
+3 -3
View File
@@ -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<asset>(std::filesystem::path("licenses") / selected_item);
license = std::make_unique<utils::mapped_file>(std::filesystem::path("assets://licenses") / selected_item);
}
catch (...)
{
@@ -1113,7 +1113,7 @@ void scenes::lobby::gui_licenses()
{
try
{
auto current = std::make_unique<asset>(std::filesystem::path("licenses") / component);
auto current = std::make_unique<utils::mapped_file>(std::filesystem::path("assets://licenses") / component);
if (ImGui::Selectable(component, component == selected_item, ImGuiSelectableFlags_SelectOnRelease))
{
selected_item = component;
+2 -2
View File
@@ -19,7 +19,7 @@
#include "i18n.h"
#include "asset.h"
#include "utils/mapped_file.h"
#include <map>
#include <string>
#include <vector>
@@ -58,7 +58,7 @@ std::vector<char> open_locale_file(const std::string & file_name, const std::str
std::vector<char> buffer;
try
{
asset file(file_name);
utils::mapped_file file("assets://" + file_name);
buffer.resize(file.size());
memcpy(buffer.data(), file.data(), file.size());
}
+2
View File
@@ -17,6 +17,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <boost/locale/gnu_gettext.hpp>
#define _(x) boost::locale::gettext(x)
+100 -15
View File
@@ -17,6 +17,7 @@
*/
#include "mapped_file.h"
#include "application.h"
#include <asm-generic/mman-common.h>
#include <fcntl.h>
#include <string>
@@ -24,38 +25,82 @@
#include <system_error>
#include <unistd.h>
#ifdef __ANDROID__
#include <android/asset_manager.h>
#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<std::byte *>(bytes.data()), bytes.size());
#else
::munmap(const_cast<std::byte *>(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<std::byte *>(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<const std::byte *>(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);
}
+13 -3
View File
@@ -22,12 +22,22 @@
#include <filesystem>
#include <span>
#ifdef __ANDROID__
struct AAsset;
#endif
namespace utils
{
class mapped_file
{
int fd = -1;
std::span<std::byte> bytes{};
#ifdef __ANDROID__
AAsset * android_asset = nullptr;
#endif
std::span<const std::byte> 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 std::byte>() const