feat(host): vendor PyroWave + minimal Granite subset as crates/pyrowave-sys

Phase 0 of design/pyrowave-codec-plan.md — the opt-in wired-LAN ultra-low-
latency codec. Vendored at upstream 509e4f88 (API 0.4.0, Granite 44362775,
volk + vulkan-headers pins in PUNKTFUNK-VENDOR.txt), pruned to the 6.6 MB
the standalone no-renderer build needs; scripts/vendor-pyrowave.sh
reproduces the tree (a pin bump is protocol-affecting, plan §4.2).

build.rs drives the wrapper CMakeLists (static archives incl. a static
C-API lib upstream only ships shared) + bindgen over pyrowave.h; Linux and
Windows only, empty stub elsewhere (Apple gets a native Metal port, §4.7).
Offline-safe by construction: no network, no system lib, vendored Vulkan
headers — same model as the opus dep (flatpak builder has no network).

Phase-0 validation on .21 (RTX 5070 Ti, driver 610.43.03):
- upstream pyrowave-c-test + interop test (incl. dmabuf/DRM-modifier
  Vulkan<->Vulkan) pass, from the pristine AND the pruned tree
- GPU kernel times at ~1.6 bpp noise: encode/decode 0.090/0.042 ms @800p,
  0.146/0.067 @1080p, 0.226/0.103 @1440p, 0.477/0.201 @4K — order of
  magnitude under NVENC's 1-2 ms retrieve, CBR lands within ~100 B of
  target
- cargo test -p pyrowave-sys green (static link + API-version pin check)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-15 00:35:10 +02:00
parent 1b73361372
commit 4c3b11445c
396 changed files with 140058 additions and 0 deletions
@@ -0,0 +1,541 @@
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "os_filesystem.hpp"
#include "path_utils.hpp"
#include "logging.hpp"
#include <algorithm>
#include <stdexcept>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <atomic>
#ifdef __linux__
#include <sys/inotify.h>
#endif
#if defined(__FreeBSD__) || defined(__APPLE__)
#define FSTAT64 fstat
#define FTRUNCATE64 ftruncate
#define MMAP64 mmap
#define STAT64 stat
#define off64_t off_t
#else
#define FSTAT64 fstat64
#define FTRUNCATE64 ftruncate64
#define MMAP64 mmap64
#define STAT64 stat64
#endif
namespace Granite
{
static bool ensure_directory_inner(const std::string &path)
{
if (Path::is_root_path(path))
return false;
struct STAT64 s = {};
if (::STAT64(path.c_str(), &s) >= 0 && S_ISDIR(s.st_mode))
return true;
auto basedir = Path::basedir(path);
if (!ensure_directory_inner(basedir))
return false;
return (mkdir(path.c_str(), 0750) >= 0) || (errno == EEXIST);
}
static bool ensure_directory(const std::string &path)
{
auto basedir = Path::basedir(path);
return ensure_directory_inner(basedir);
}
FileHandle MMapFile::open(const std::string &path, FileMode mode)
{
auto file = Util::make_handle<MMapFile>();
if (!file->init(path, mode))
file.reset();
return file;
}
static std::atomic_uint32_t global_transaction_counter;
bool MMapFile::init(const std::string &path, FileMode mode)
{
int modeflags = 0;
switch (mode)
{
case FileMode::ReadOnly:
modeflags = O_RDONLY;
break;
case FileMode::WriteOnly:
case FileMode::WriteOnlyTransactional:
if (!ensure_directory(path))
{
LOGE("MMapFile failed to create directory.\n");
return false;
}
modeflags = O_RDWR | O_CREAT | O_TRUNC; // Need read access for mmap.
break;
case FileMode::ReadWrite:
if (!ensure_directory(path))
{
LOGE("MMapFile failed to create directory.\n");
return false;
}
modeflags = O_RDWR | O_CREAT;
break;
}
const char *open_path = path.c_str();
if (mode == FileMode::WriteOnlyTransactional)
{
// Use atomic file rename to ensure that a file is written atomically.
rename_to_on_close = path;
rename_from_on_close =
path + ".tmp." +
std::to_string(getpid()) + "." +
std::to_string(global_transaction_counter.fetch_add(1, std::memory_order_relaxed));
open_path = rename_from_on_close.c_str();
}
fd = ::open(open_path, modeflags, 0640);
if (fd < 0)
{
rename_to_on_close.clear();
rename_from_on_close.clear();
return false;
}
if (!query_stat())
{
close(fd);
rename_to_on_close.clear();
rename_from_on_close.clear();
return false;
}
return true;
}
FileMappingHandle MMapFile::map_write(size_t map_size)
{
if (has_write_map)
return {};
if (FTRUNCATE64(fd, off64_t(map_size)) < 0)
{
LOGE("Failed to truncate.\n");
report_error();
return {};
}
size = map_size;
void *mapped = MMAP64(nullptr, map_size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
if (mapped == MAP_FAILED)
{
report_error();
return {};
}
has_write_map = true;
return Util::make_handle<FileMapping>(
reference_from_this(),
0,
mapped, map_size,
0, map_size);
}
void MMapFile::report_error()
{
#ifdef __linux__
int err = errno;
char fdpath[PATH_MAX];
char path[PATH_MAX];
snprintf(fdpath, sizeof(fdpath), "/proc/%u/fd/%d", getpid(), fd);
int ret = readlink(fdpath, path, sizeof(path) - 1);
if (ret > 0)
{
path[ret] = '\0';
LOGE("mmap failed for \"%s\" (%s).\n", path, strerror(err));
}
#endif
}
FileMappingHandle MMapFile::map_subset(uint64_t offset, size_t range)
{
uint64_t page_size = sysconf(_SC_PAGESIZE);
uint64_t begin_map = offset & ~(page_size - 1);
uint64_t end_map = offset + range;
size_t mapped_size = end_map - begin_map;
// length need not be aligned.
void *mapped = MMAP64(nullptr, mapped_size, PROT_READ, MAP_PRIVATE, fd, off64_t(begin_map));
if (mapped == MAP_FAILED)
{
report_error();
return {};
}
return Util::make_handle<FileMapping>(
reference_from_this(),
offset,
mapped, mapped_size,
offset - begin_map, range);
}
uint64_t MMapFile::get_size()
{
return size;
}
bool MMapFile::query_stat()
{
struct STAT64 s = {};
if (FSTAT64(fd, &s) < 0)
return false;
if (uint64_t(s.st_size) > SIZE_MAX)
return false;
size = static_cast<size_t>(s.st_size);
return true;
}
void MMapFile::unmap(void *mapped, size_t mapped_size)
{
munmap(mapped, mapped_size);
}
MMapFile::~MMapFile()
{
if (fd >= 0)
close(fd);
if (!rename_from_on_close.empty() && !rename_to_on_close.empty())
{
int ret = rename(rename_from_on_close.c_str(), rename_to_on_close.c_str());
if (ret != 0)
LOGE("Failed to rename file %s -> %s.\n", rename_from_on_close.c_str(), rename_to_on_close.c_str());
}
}
OSFilesystem::OSFilesystem(const std::string &base_)
: base(base_)
{
#ifdef __linux__
notify_fd = inotify_init1(IN_NONBLOCK);
if (notify_fd < 0)
LOGE("Failed to init inotify.\n");
#else
notify_fd = -1;
#endif
}
OSFilesystem::~OSFilesystem()
{
#ifdef __linux__
if (notify_fd > 0)
{
for (auto &handler : handlers)
inotify_rm_watch(notify_fd, handler.first);
close(notify_fd);
}
#endif
}
FileHandle OSFilesystem::open(const std::string &path, FileMode mode)
{
return MMapFile::open(Path::join(base, path), mode);
}
std::string OSFilesystem::get_filesystem_path(const std::string &path)
{
return Path::join(base, path);
}
int OSFilesystem::get_notification_fd() const
{
return notify_fd;
}
void OSFilesystem::poll_notifications()
{
#ifdef __linux__
if (notify_fd < 0)
return;
for (;;)
{
alignas(inotify_event) char buffer[sizeof(inotify_event) + NAME_MAX + 1];
ssize_t ret = read(notify_fd, buffer, sizeof(buffer));
if (ret < 0)
{
if (errno != EAGAIN)
LOGE("failed to read inotify fd.\n");
break;
}
struct inotify_event *current = nullptr;
for (ssize_t i = 0; i < ret; i += current->len + sizeof(struct inotify_event))
{
current = reinterpret_cast<inotify_event *>(buffer + i);
auto mask = current->mask;
int wd = current->wd;
auto itr = handlers.find(wd);
if (itr == end(handlers))
continue;
FileNotifyType type;
if (mask & IN_CLOSE_WRITE)
type = FileNotifyType::FileChanged;
else if (mask & (IN_CREATE | IN_MOVED_TO))
type = FileNotifyType::FileCreated;
else if (mask & (IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM))
type = FileNotifyType::FileDeleted;
else
continue;
for (auto &func : itr->second.funcs)
{
if (func.func)
{
if (itr->second.directory)
{
auto notify_path = protocol + "://" + Path::join(func.path, current->name);
func.func({ std::move(notify_path), type, func.virtual_handle });
}
else
func.func({ protocol + "://" + func.path, type, func.virtual_handle });
}
}
}
}
#endif
}
void OSFilesystem::uninstall_notification(FileNotifyHandle handle)
{
#ifdef __linux__
if (handle < 0)
return;
if (notify_fd < 0)
return;
//LOGI("Uninstalling notification: %d\n", handle);
auto real = virtual_to_real.find(handle);
if (real == end(virtual_to_real))
{
LOGE("unknown virtual inotify handler.\n");
return;
}
auto itr = handlers.find(static_cast<int>(real->second));
if (itr == end(handlers))
{
LOGE("unknown inotify handler.\n");
return;
}
auto handler_instance = find_if(begin(itr->second.funcs), end(itr->second.funcs), [=](const VirtualHandler &v) {
return v.virtual_handle == handle;
});
if (handler_instance == end(itr->second.funcs))
{
LOGE("unknown inotify handler path.\n");
return;
}
itr->second.funcs.erase(handler_instance);
if (itr->second.funcs.empty())
{
inotify_rm_watch(notify_fd, real->second);
handlers.erase(itr);
}
virtual_to_real.erase(real);
#else
(void)handle;
#endif
}
FileNotifyHandle OSFilesystem::install_notification(const std::string &path,
std::function<void (const FileNotifyInfo &)> func)
{
#ifdef __linux__
//LOGI("Installing notification for: %s\n", path.c_str());
if (notify_fd < 0)
return -1;
FileStat s = {};
if (!stat(path, s))
{
LOGE("inotify: path doesn't exist.\n");
return -1;
}
auto resolved_path = Path::join(base, path);
int wd = inotify_add_watch(notify_fd, resolved_path.c_str(),
IN_MOVE | IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_DELETE_SELF);
if (wd < 0)
{
LOGE("Failed to create watch handle.\n");
return -1;
}
// We could have different paths which look different but resolve to the same wd, so handle that.
auto itr = handlers.find(wd);
if (itr == end(handlers))
handlers[wd] = { {{ path, std::move(func), ++virtual_handle }}, s.type == PathType::Directory };
else
itr->second.funcs.push_back({ path, std::move(func), ++virtual_handle });
//LOGI(" Got handle: %d\n", virtual_handle);
virtual_to_real[virtual_handle] = wd;
return static_cast<FileNotifyHandle>(virtual_handle);
#else
(void)path;
(void)func;
return -1;
#endif
}
bool OSFilesystem::remove(const std::string &path)
{
auto resolved_path = Path::join(base, path);
return unlink(resolved_path.c_str()) == 0;
}
bool OSFilesystem::move_yield(const std::string &dst, const std::string &src)
{
auto resolved_dst = Path::join(base, dst);
auto resolved_src = Path::join(base, src);
#if !defined(__linux__) || (defined(ANDROID) && (__ANDROID_API__ < __ANDROID_API_R__))
// Workaround since Android does not have renameat2 until API level 30.
// If we can exclusive create a new file, we can rename with replace somewhat safely.
int fd = ::open(resolved_dst.c_str(), O_EXCL | O_RDWR | O_CREAT, 0600);
if (fd >= 0)
{
::close(fd);
return rename(resolved_src.c_str(), resolved_dst.c_str()) == 0;
}
else
return false;
#else
return renameat2(AT_FDCWD, resolved_src.c_str(), AT_FDCWD, resolved_dst.c_str(), RENAME_NOREPLACE) == 0;
#endif
}
bool OSFilesystem::move_replace(const std::string &dst, const std::string &src)
{
auto resolved_dst = Path::join(base, dst);
auto resolved_src = Path::join(base, src);
return rename(resolved_src.c_str(), resolved_dst.c_str()) == 0;
}
std::vector<ListEntry> OSFilesystem::list(const std::string &path)
{
auto directory = Path::join(base, path);
DIR *dir = opendir(directory.c_str());
if (!dir)
{
LOGE("Failed to open directory %s\n", path.c_str());
return {};
}
std::vector<ListEntry> entries;
struct dirent *entry;
while ((entry = readdir(dir)))
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
auto joined_path = Path::join(path, entry->d_name);
PathType type;
if (entry->d_type == DT_DIR)
type = PathType::Directory;
else if (entry->d_type == DT_REG)
type = PathType::File;
else if (entry->d_type != DT_UNKNOWN && entry->d_type != DT_LNK)
type = PathType::Special;
else
{
FileStat s;
if (!stat(joined_path, s))
{
LOGE("Failed to stat file: %s\n", joined_path.c_str());
continue;
}
type = s.type;
}
entries.push_back({ std::move(joined_path), type });
}
closedir(dir);
return entries;
}
bool OSFilesystem::stat(const std::string &path, FileStat &stat)
{
auto resolved_path = Path::join(base, path);
struct STAT64 buf = {};
if (::STAT64(resolved_path.c_str(), &buf) < 0)
return false;
if (S_ISREG(buf.st_mode))
stat.type = PathType::File;
else if (S_ISDIR(buf.st_mode))
stat.type = PathType::Directory;
else
stat.type = PathType::Special;
stat.size = uint64_t(buf.st_size);
#ifdef __linux__
stat.last_modified = buf.st_mtim.tv_sec * 1000000000ull + buf.st_mtim.tv_nsec;
#else
stat.last_modified = buf.st_mtimespec.tv_sec * 1000000000ull + buf.st_mtimespec.tv_nsec;
#endif
return true;
}
}
@@ -0,0 +1,92 @@
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "../filesystem.hpp"
#include <unordered_map>
namespace Granite
{
class MMapFile final : public File
{
public:
static FileHandle open(const std::string &path, FileMode mode);
~MMapFile() override;
FileMappingHandle map_subset(uint64_t offset, size_t size) override;
FileMappingHandle map_write(size_t map_size) override;
void unmap(void *mapped, size_t size) override;
uint64_t get_size() override;
private:
bool init(const std::string &path, FileMode mode);
bool query_stat();
int fd = -1;
size_t size = 0;
bool has_write_map = false;
std::string rename_from_on_close;
std::string rename_to_on_close;
void report_error();
};
class OSFilesystem : public FilesystemBackend
{
public:
OSFilesystem(const std::string &base);
~OSFilesystem();
std::vector<ListEntry> list(const std::string &path) override;
FileHandle open(const std::string &path, FileMode mode) override;
bool stat(const std::string &path, FileStat &stat) override;
FileNotifyHandle install_notification(const std::string &path, std::function<void (const FileNotifyInfo &)> func) override;
void uninstall_notification(FileNotifyHandle handle) override;
void poll_notifications() override;
int get_notification_fd() const override;
std::string get_filesystem_path(const std::string &path) override;
bool remove(const std::string &path) override;
// Must implement atomic renaming semantics.
// Atomically replaces dst.
bool move_replace(const std::string &dst, const std::string &src) override;
// If dst exists, nothing happens (and false is returned).
bool move_yield(const std::string &dst, const std::string &src) override;
private:
std::string base;
struct VirtualHandler
{
std::string path;
std::function<void (const FileNotifyInfo &)> func;
FileNotifyHandle virtual_handle;
};
struct Handler
{
std::vector<VirtualHandler> funcs;
bool directory;
};
std::unordered_map<FileNotifyHandle, Handler> handlers;
std::unordered_map<FileNotifyHandle, FileNotifyHandle> virtual_to_real;
int notify_fd;
FileNotifyHandle virtual_handle = 0;
};
}