157 lines
8 KiB
Text
157 lines
8 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
module;
|
||
|
|
#include "vulkan/vulkan.h"
|
||
|
|
export module Nostro.Ui.Shell:Canvas;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import Crafter.Graphics;
|
||
|
|
import Crafter.Event;
|
||
|
|
import Nostro.Ui;
|
||
|
|
|
||
|
|
// The window and what turns a frame's draw list into what the window library draws. The library draws from compute
|
||
|
|
// shaders, one pass over the whole window per dispatch, in which every box comes before every glyph and everything is
|
||
|
|
// cut to one box: that is a Batch of the draw list, so a frame is one dispatch per batch, in the list's order. A
|
||
|
|
// dispatch reads its items from buffers of its own, from their start, so each batch needs a pair of buffers, and a
|
||
|
|
// frame still on the GPU reads its buffers as it runs, so each frame in flight has a set of pairs: a pool, made once.
|
||
|
|
|
||
|
|
export namespace Nostro::Ui::Shell {
|
||
|
|
// The app's fonts. They are read from files and measured without a window, so the toolkit's Context, which
|
||
|
|
// measures every text through them, is made before the window and may outlive it.
|
||
|
|
class Fonts {
|
||
|
|
public:
|
||
|
|
// Reads NotoSans-Regular.ttf and NotoSans-Medium.ttf from the directory; throws when one is not there.
|
||
|
|
explicit Fonts(const std::filesystem::path& directory);
|
||
|
|
Fonts(const Fonts&) = delete;
|
||
|
|
Fonts& operator=(const Fonts&) = delete;
|
||
|
|
|
||
|
|
Crafter::Font& Face(FontFace face) noexcept;
|
||
|
|
// The fonts' sizes for the toolkit; good for as long as the fonts are.
|
||
|
|
TextMeasure Measure();
|
||
|
|
|
||
|
|
private:
|
||
|
|
Crafter::Font regular;
|
||
|
|
Crafter::Font medium;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct CanvasOptions {
|
||
|
|
// The window's size where the compositor leaves it to the app, as a desktop does and a phone does not.
|
||
|
|
std::uint32_t widthDp = 1024;
|
||
|
|
std::uint32_t heightDp = 720;
|
||
|
|
std::string title;
|
||
|
|
std::string appId; // what the compositor finds the desktop file, and so the icon, by
|
||
|
|
std::filesystem::path assets; // where the window library's shaders are
|
||
|
|
std::filesystem::path cache; // where compiled shaders are kept between runs
|
||
|
|
// For looking at another kind of display than the one at hand, a phone's on a desktop: everything is drawn as
|
||
|
|
// at this scale, in a window of the size above times it in pixels, whatever the display's own scale is and
|
||
|
|
// whatever size the compositor asks for.
|
||
|
|
std::optional<float> scale;
|
||
|
|
};
|
||
|
|
|
||
|
|
class Canvas {
|
||
|
|
public:
|
||
|
|
// Sets the graphics device up, once per process, and makes the window. Throws when there is no compositor to
|
||
|
|
// connect to, no graphics device that can draw, or a shader is missing.
|
||
|
|
Canvas(Fonts& fonts, const CanvasOptions& options);
|
||
|
|
// Waits for the frames still being drawn, then lets go of everything.
|
||
|
|
~Canvas();
|
||
|
|
Canvas(const Canvas&) = delete;
|
||
|
|
Canvas& operator=(const Canvas&) = delete;
|
||
|
|
|
||
|
|
Crafter::Window& Native() noexcept;
|
||
|
|
const Crafter::Window& Native() const noexcept;
|
||
|
|
// How many pixels a dp is: the scale of the display the window is on, or the one it was told to draw at.
|
||
|
|
float Scale() const noexcept;
|
||
|
|
// The window, in pixels.
|
||
|
|
Box Bounds() const noexcept;
|
||
|
|
// Gives the window the size it should have at the display's scale, where the window library has not: the size
|
||
|
|
// the compositor asked for last, or the app's own until it asks for one. The library passes over the size that
|
||
|
|
// comes with a new window, which is the whole screen on a phone, and applies a new scale only with the next
|
||
|
|
// new size. True when the size changed, which the library says through onResize too.
|
||
|
|
bool Reconcile();
|
||
|
|
// Asks for another size, in dp: the window takes it at the next Reconcile, and keeps it until the compositor
|
||
|
|
// asks for one of its own, which a compositor that tiles or maximises the window does at once.
|
||
|
|
void SetSize(std::uint32_t widthDp, std::uint32_t heightDp) noexcept;
|
||
|
|
// Draws one frame: the list over the background colour. Throws what the window library does at a graphics
|
||
|
|
// error, after which the window draws nothing more.
|
||
|
|
void Render(const DrawList& list, Color background);
|
||
|
|
// The next frame is also written to this file, as a PNG.
|
||
|
|
void Capture(const std::filesystem::path& png);
|
||
|
|
|
||
|
|
// What the last frame took, for whoever watches the pool: a frame that needed more than there is drew less
|
||
|
|
// than it was given.
|
||
|
|
struct Stats {
|
||
|
|
std::uint32_t batches = 0;
|
||
|
|
std::uint32_t boxes = 0;
|
||
|
|
std::uint32_t glyphs = 0;
|
||
|
|
std::uint32_t glyphsWaiting = 0; // drawn blank for want of a cell in the font atlas: there after a later frame
|
||
|
|
bool overflowed = false;
|
||
|
|
};
|
||
|
|
const Stats& Last() const noexcept;
|
||
|
|
std::uint64_t Frames() const noexcept;
|
||
|
|
// The graphics device's name, and how it reaches what it draws: for a log.
|
||
|
|
std::string Device() const;
|
||
|
|
|
||
|
|
// The frames a glyph without a cell waits at most: the frames in flight and one.
|
||
|
|
static constexpr std::uint32_t atlasPatience = Crafter::Window::numFrames + 1;
|
||
|
|
|
||
|
|
private:
|
||
|
|
// A batch's buffers: boxes (circles are boxes with round corners) and glyphs.
|
||
|
|
struct Pair {
|
||
|
|
Crafter::GraphicsBuffer<Crafter::QuadItem, true> boxes;
|
||
|
|
Crafter::GraphicsBuffer<Crafter::GlyphItem, true> glyphs;
|
||
|
|
std::optional<Crafter::BufferSlot> boxSlot;
|
||
|
|
std::optional<Crafter::BufferSlot> glyphSlot;
|
||
|
|
std::uint32_t boxCap = 0;
|
||
|
|
std::uint32_t glyphCap = 0;
|
||
|
|
};
|
||
|
|
// Most batches are a bar or a sheet; a list is one batch of many items. So a frame has many small pairs and a
|
||
|
|
// few large ones.
|
||
|
|
static constexpr std::uint32_t smallPairs = 20;
|
||
|
|
static constexpr std::uint32_t smallBoxes = 128;
|
||
|
|
static constexpr std::uint32_t smallGlyphs = 1024;
|
||
|
|
static constexpr std::uint32_t largePairs = 4;
|
||
|
|
static constexpr std::uint32_t largeBoxes = 2048;
|
||
|
|
static constexpr std::uint32_t largeGlyphs = 8192;
|
||
|
|
struct FrameSet {
|
||
|
|
std::vector<Pair> small;
|
||
|
|
std::vector<Pair> large;
|
||
|
|
std::size_t smallUsed = 0;
|
||
|
|
std::size_t largeUsed = 0;
|
||
|
|
};
|
||
|
|
// In the order they are made, which is the reverse of the order they go in: the pairs hand their slots back to
|
||
|
|
// the heap, the renderer holds slots of its own, and the heap holds a view of the atlas's image.
|
||
|
|
struct Parts {
|
||
|
|
Parts(std::uint32_t width, std::uint32_t height, std::string_view title);
|
||
|
|
|
||
|
|
Crafter::Window window;
|
||
|
|
Crafter::FontAtlas atlas;
|
||
|
|
Crafter::GraphicsDescriptorHeap heap;
|
||
|
|
Crafter::UIRenderer renderer;
|
||
|
|
std::optional<Crafter::EventListener<Crafter::UIBuildArgs>> build;
|
||
|
|
std::array<FrameSet, Crafter::Window::numFrames> pool;
|
||
|
|
};
|
||
|
|
|
||
|
|
void Release() noexcept;
|
||
|
|
void MakePairs(std::vector<Pair>& pairs, std::uint32_t count, std::uint32_t boxCap, std::uint32_t glyphCap);
|
||
|
|
void Build(VkCommandBuffer commands, std::uint32_t frameIndex);
|
||
|
|
// One dispatch: the batch, over `under` when there is one.
|
||
|
|
void Submit(VkCommandBuffer commands, FrameSet& set, const Batch& batch, const RectItem* under = nullptr);
|
||
|
|
Pair* Take(FrameSet& set, std::size_t boxes, std::size_t glyphs) noexcept;
|
||
|
|
|
||
|
|
Fonts& fonts;
|
||
|
|
// The size the window should have, in the compositor's units, and the size the compositor was last seen to ask
|
||
|
|
// for, (0, 0) when it left the choice to the app.
|
||
|
|
std::int32_t logicalWidth;
|
||
|
|
std::int32_t logicalHeight;
|
||
|
|
std::int32_t askedWidth = 0;
|
||
|
|
std::int32_t askedHeight = 0;
|
||
|
|
std::optional<float> pretend; // CanvasOptions::scale
|
||
|
|
std::optional<Parts> parts;
|
||
|
|
const DrawList* drawing = nullptr; // the list of the frame being drawn
|
||
|
|
Color background;
|
||
|
|
Stats stats;
|
||
|
|
std::uint64_t frames = 0;
|
||
|
|
};
|
||
|
|
}
|