From 86ce0a68a325f8104b796a11bf7d2af5b924d831 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 22 Nov 2023 16:42:42 -0800 Subject: [PATCH] [Mammoth/Teton] Add an OpenFile interface and use it to load a font. --- lib/mammoth/CMakeLists.txt | 6 ++++- lib/mammoth/file/file.cpp | 45 +++++++++++++++++++++++++++++++++++ lib/mammoth/file/file.h | 25 +++++++++++++++++++ sys/teton/framebuffer/psf.cpp | 6 ++--- sys/teton/framebuffer/psf.h | 9 +++---- sys/teton/teton.cpp | 14 +---------- 6 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 lib/mammoth/file/file.cpp create mode 100644 lib/mammoth/file/file.h diff --git a/lib/mammoth/CMakeLists.txt b/lib/mammoth/CMakeLists.txt index 0029633..95754d5 100644 --- a/lib/mammoth/CMakeLists.txt +++ b/lib/mammoth/CMakeLists.txt @@ -1,4 +1,5 @@ add_library(mammoth STATIC + file/file.cpp ipc/channel.cpp ipc/endpoint_client.cpp ipc/endpoint_server.cpp @@ -23,7 +24,10 @@ target_include_directories(mammoth target_link_libraries(mammoth glacier c - zion_stub) + victoriafalls_yunq + yellowstone_yunq + zion_stub + ) set_target_properties(mammoth PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${BASE_COMPILE_FLAGS}" diff --git a/lib/mammoth/file/file.cpp b/lib/mammoth/file/file.cpp new file mode 100644 index 0000000..77c50d4 --- /dev/null +++ b/lib/mammoth/file/file.cpp @@ -0,0 +1,45 @@ +#include "file/file.h" + +#include +#include +#include + +#include "util/debug.h" + +namespace mmth { +namespace { + +VFSClient* gVfsClient = nullptr; + +} // namespace + +File File::Open(glcr::StringView path) { + if (gVfsClient == 0) { + YellowstoneClient client(gInitEndpointCap); + + GetEndpointRequest yreq; + yreq.set_endpoint_name("victoriafalls"); + Endpoint yresp; + check(client.GetEndpoint(yreq, yresp)); + + gVfsClient = new VFSClient(yresp.endpoint()); + } + + OpenFileRequest req; + req.set_path(path); + OpenFileResponse resp; + check(gVfsClient->OpenFile(req, resp)); + + return File(OwnedMemoryRegion::FromCapability(resp.memory())); +} + +glcr::StringView File::as_str() { + return glcr::StringView((char*)raw_ptr(), file_data_.size()); +} + +void* File::raw_ptr() { return reinterpret_cast(file_data_.vaddr()); } +uint8_t* File::byte_ptr() { + return reinterpret_cast(file_data_.vaddr()); +} + +} // namespace mmth diff --git a/lib/mammoth/file/file.h b/lib/mammoth/file/file.h new file mode 100644 index 0000000..15021c5 --- /dev/null +++ b/lib/mammoth/file/file.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include "mammoth/util/memory_region.h" + +namespace mmth { + +class File { + public: + static File Open(glcr::StringView path); + + glcr::StringView as_str(); + + void* raw_ptr(); + uint8_t* byte_ptr(); + + private: + OwnedMemoryRegion file_data_; + + File(OwnedMemoryRegion&& file) : file_data_(glcr::Move(file)) {} +}; + +} // namespace mmth diff --git a/sys/teton/framebuffer/psf.cpp b/sys/teton/framebuffer/psf.cpp index 6c9770e..43714c6 100644 --- a/sys/teton/framebuffer/psf.cpp +++ b/sys/teton/framebuffer/psf.cpp @@ -9,9 +9,9 @@ const uint32_t kMagic = 0x864AB572; } -Psf::Psf(mmth::OwnedMemoryRegion&& psf_file) - : psf_file_(glcr::Move(psf_file)), - header_(reinterpret_cast(psf_file_.vaddr())) { +Psf::Psf(glcr::StringView path) + : psf_file_(mmth::File::Open(path)), + header_(reinterpret_cast(psf_file_.raw_ptr())) { EnsureValid(); } diff --git a/sys/teton/framebuffer/psf.h b/sys/teton/framebuffer/psf.h index 944d406..bfa890d 100644 --- a/sys/teton/framebuffer/psf.h +++ b/sys/teton/framebuffer/psf.h @@ -1,6 +1,6 @@ #pragma once -#include +#include struct PsfHeader { uint32_t magic; /* magic bytes to identify PSF */ @@ -15,7 +15,7 @@ struct PsfHeader { class Psf { public: - Psf(mmth::OwnedMemoryRegion&& psf_file); + Psf(glcr::StringView path); void DumpHeader(); @@ -24,12 +24,13 @@ class Psf { uint32_t height() { return header_->height; } uint8_t* glyph(uint32_t index) { - return reinterpret_cast(psf_file_.vaddr() + header_->headersize + + return reinterpret_cast(psf_file_.byte_ptr() + + header_->headersize + (index * header_->bytesperglyph)); } private: - mmth::OwnedMemoryRegion psf_file_; + mmth::File psf_file_; PsfHeader* header_; void EnsureValid(); diff --git a/sys/teton/teton.cpp b/sys/teton/teton.cpp index e530b09..e0b4900 100644 --- a/sys/teton/teton.cpp +++ b/sys/teton/teton.cpp @@ -25,19 +25,7 @@ uint64_t main(uint64_t init_port) { // 2. Parse a font file. - GetEndpointRequest req; - req.set_endpoint_name("victoriafalls"); - Endpoint resp; - check(client.GetEndpoint(req, resp)); - - VFSClient vfs(resp.endpoint()); - - OpenFileRequest freq; - freq.set_path("/default8x16.psfu"); - OpenFileResponse fresp; - check(vfs.OpenFile(freq, fresp)); - - Psf psf(mmth::OwnedMemoryRegion::FromCapability(fresp.memory())); + Psf psf("/default8x16.psfu"); psf.DumpHeader(); Console console(fbuf, psf);