[Mammoth/Teton] Add an OpenFile interface and use it to load a font.

This commit is contained in:
Drew Galbraith 2023-11-22 16:42:42 -08:00
parent 4fd17a59ea
commit 86ce0a68a3
6 changed files with 84 additions and 21 deletions

View File

@ -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}"

45
lib/mammoth/file/file.cpp Normal file
View File

@ -0,0 +1,45 @@
#include "file/file.h"
#include <victoriafalls/victoriafalls.yunq.client.h>
#include <yellowstone/yellowstone.yunq.client.h>
#include <zglobal.h>
#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<void*>(file_data_.vaddr()); }
uint8_t* File::byte_ptr() {
return reinterpret_cast<uint8_t*>(file_data_.vaddr());
}
} // namespace mmth

25
lib/mammoth/file/file.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <glacier/memory/move.h>
#include <glacier/string/string_view.h>
#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

View File

@ -9,9 +9,9 @@ const uint32_t kMagic = 0x864AB572;
}
Psf::Psf(mmth::OwnedMemoryRegion&& psf_file)
: psf_file_(glcr::Move(psf_file)),
header_(reinterpret_cast<PsfHeader*>(psf_file_.vaddr())) {
Psf::Psf(glcr::StringView path)
: psf_file_(mmth::File::Open(path)),
header_(reinterpret_cast<PsfHeader*>(psf_file_.raw_ptr())) {
EnsureValid();
}

View File

@ -1,6 +1,6 @@
#pragma once
#include <mammoth/util/memory_region.h>
#include <mammoth/file/file.h>
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<uint8_t*>(psf_file_.vaddr() + header_->headersize +
return reinterpret_cast<uint8_t*>(psf_file_.byte_ptr() +
header_->headersize +
(index * header_->bytesperglyph));
}
private:
mmth::OwnedMemoryRegion psf_file_;
mmth::File psf_file_;
PsfHeader* header_;
void EnsureValid();

View File

@ -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);