TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_ui_cache_subsystem.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <unordered_map>
5#include <utility>
6#include <vector>
7
8#include <gui_core/gui_id.h>
11
12namespace tachyon
13{
16
17 class FramePipeline;
18 struct FrameContext;
19 class ElementStorage;
20 class GUIStateManager;
21 class ITextSystem;
22
35 {
36 public:
39 struct Scope
40 {
43 std::vector<ID> scopeIDs;
47 std::vector<std::pair<ID, int2>> scrollSnapshot;
51 std::vector<ID> textEditorIds;
53 std::vector<ID> claimedSwatches;
54
61
62 uint64_t signature = 0;
66 bool hasCachedSize = false;
68 bool usesPostLayout = false;
69
70 bool captured = false;
71 bool nonCacheable = false;
72 bool dirty = false;
73 bool missedThisFrame = false;
74 uint64_t firstCaptureFrame = 0;
75
77 uint64_t themeEpoch = 0;
78 uint64_t lastUsedFrame = 0;
79
83 };
84
86
91
93 struct HitInput
94 {
95 bool dragging = false;
96 bool keyboard = false;
97 bool rebuildAll = false;
98 };
99
101 bool EvaluateHit(const Scope& s, uint64_t signature, const HitInput& input) const;
102
105 void Invalidate(ID id);
106
108 void InvalidateAll() { m_themeEpoch++; }
109
111 uint64_t CurrentFrame() const { return m_frame; }
113 uint64_t ThemeEpoch() const { return m_themeEpoch; }
114
116 void SetDrawCacheEnabled(bool e) { m_drawCacheEnabled = e; }
117 bool IsDrawCacheEnabled() const { return m_drawCacheEnabled; }
119 static constexpr float kDrawSettleSecs = 0.15f;
120
121 // Per-frame diagnostics, reset at the start of each frame
122
124 void RecordResult(bool hit) { if (hit) m_frameHits++; else m_frameMisses++; }
125 void RecordDrawReplay() { m_frameDrawReplays++; }
126 void RecordDrawCapture() { m_frameDrawCaptures++; }
127 uint32_t FrameHits() const { return m_frameHits; }
128 uint32_t FrameMisses() const { return m_frameMisses; }
129 uint32_t FrameDrawReplays() const { return m_frameDrawReplays; }
130 uint32_t FrameDrawCaptures() const { return m_frameDrawCaptures; }
131 size_t ScopeCount() const { return m_scopes.size(); }
132
134 static ID MemberOrNull(const Scope& s, ID id)
135 {
136 return IsMember(s, id) ? id : ID::Invalid();
137 }
138
139 static bool IsMember(const Scope& s, ID id)
140 {
141 return id != ID::Invalid() && std::binary_search(s.scopeIDs.begin(), s.scopeIDs.end(), id);
142 }
143
144 private:
145 void OnPreFrame();
146 void OnPostLayout(ITextSystem* textSystem);
147 void OnEndFrame();
148
151 void CaptureScope(Scope& s, ITextSystem* textSystem);
152
153 std::unordered_map<ID, Scope> m_scopes;
154
155 ElementStorage& m_storage;
156 GUIStateManager& m_stateManager;
157
158 uint64_t m_frame = 0;
159 uint64_t m_themeEpoch = 0;
160 uint32_t m_frameHits = 0;
161 uint32_t m_frameMisses = 0;
162 uint32_t m_frameDrawReplays = 0;
163 uint32_t m_frameDrawCaptures = 0;
164 bool m_drawCacheEnabled = true;
165
167 static constexpr uint64_t kEvictAfterFrames = 240;
168
171 static constexpr uint64_t kSettleFrames = 2;
172 };
174}
The frame's element tree.
Definition gui_element_storage.h:96
Stage callbacks run in registration order (no priorities): if A must run before B,...
Definition gui_frame_pipeline.h:74
Owns the per-frame state objects and runs their BeginFrame / EndFrame in order.
Definition gui_state_manager.h:65
Text and icon registration, measurement, rendering and editing.
Definition gui_text_system.h:189
bool EvaluateHit(const Scope &s, uint64_t signature, const HitInput &input) const
True when scope s, built with data signature, can be served from cache this frame.
void SetDrawCacheEnabled(bool e)
When off, EvaluateHit always misses and every scope rebuilds (for A/B comparison).
Definition gui_ui_cache_subsystem.h:116
uint64_t ThemeEpoch() const
Global epoch that InvalidateAll() bumps.
Definition gui_ui_cache_subsystem.h:113
uint64_t CurrentFrame() const
Frame counter, advanced once per frame.
Definition gui_ui_cache_subsystem.h:111
void RecordDrawCapture()
a scope's draw (re)captured this frame
Definition gui_ui_cache_subsystem.h:126
void RecordDrawReplay()
a scope's draw blitted from cache
Definition gui_ui_cache_subsystem.h:125
Scope & GetOrCreate(ID id)
The scope with id, created on first use.
UICacheManager(FramePipeline &frame, ElementStorage &storage, GUIStateManager &stateManager)
size_t ScopeCount() const
Definition gui_ui_cache_subsystem.h:131
uint32_t FrameDrawReplays() const
Definition gui_ui_cache_subsystem.h:129
Scope * Find(ID id)
The scope with id, or nullptr.
uint32_t FrameMisses() const
Definition gui_ui_cache_subsystem.h:128
static ID MemberOrNull(const Scope &s, ID id)
id when scope s contains it, else Invalid.
Definition gui_ui_cache_subsystem.h:134
bool IsDrawCacheEnabled() const
Definition gui_ui_cache_subsystem.h:117
void RecordResult(bool hit)
Counts a scope served from cache (hit) or rebuilt.
Definition gui_ui_cache_subsystem.h:124
uint32_t FrameHits() const
Definition gui_ui_cache_subsystem.h:127
void InvalidateAll()
Bumps the global epoch so every scope rebuilds (theme reload, display scale change).
Definition gui_ui_cache_subsystem.h:108
static bool IsMember(const Scope &s, ID id)
True when scope s contains id.
Definition gui_ui_cache_subsystem.h:139
uint32_t FrameDrawCaptures() const
Definition gui_ui_cache_subsystem.h:130
static constexpr float kDrawSettleSecs
Seconds for transitions to settle after a rebuild before capturing.
Definition gui_ui_cache_subsystem.h:119
void Invalidate(ID id)
Rebuilds scope id the next time it is reached, and its enclosing scopes (an outer hit skips inner con...
Definition gui_logging.h:24
An element's slot in ElementStorage: its depth and index within that depth.
Definition gui_element.h:126
static constexpr ElementRef Invalid()
Definition gui_element.h:134
Per-frame values plus references to long-lived managers, passed to every stage callback.
Definition gui_frame_pipeline.h:32
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
Definition gui_base_types.h:406
Per-frame input facts that decide whether a scope may be served from cache.
Definition gui_ui_cache_subsystem.h:94
bool dragging
a drag-and-drop is in progress
Definition gui_ui_cache_subsystem.h:95
bool keyboard
a key/text event will be dispatched to the focused element this frame
Definition gui_ui_cache_subsystem.h:96
bool rebuildAll
Tab: the tab chain is built from construction, so every scope must run.
Definition gui_ui_cache_subsystem.h:97
Only what's needed to decide whether a scope is still clean and to blit it; not the element tree.
Definition gui_ui_cache_subsystem.h:40
uint64_t firstCaptureFrame
frame of first capture (for the deferred-measurement settle)
Definition gui_ui_cache_subsystem.h:74
ID snapCapture
Definition gui_ui_cache_subsystem.h:60
ID parentScopeID
enclosing cached scope (for invalidation propagation)
Definition gui_ui_cache_subsystem.h:76
bool nonCacheable
the subtree has a window; always rebuilds
Definition gui_ui_cache_subsystem.h:71
uint64_t signature
data signature of the last build
Definition gui_ui_cache_subsystem.h:62
std::vector< ID > textEditorIds
Text-editor element ids in the scope (recorded at capture).
Definition gui_ui_cache_subsystem.h:51
std::vector< ID > scopeIDs
Every element id in the subtree, sorted (membership tests by binary search).
Definition gui_ui_cache_subsystem.h:43
bool dirty
forced rebuild (Invalidate / size change / propagation)
Definition gui_ui_cache_subsystem.h:72
uint64_t lastUsedFrame
last frame BeginCached reached it, for eviction
Definition gui_ui_cache_subsystem.h:78
int2 drawCapturePos
Draw cache: the container's window-local position when its primitives were captured,...
Definition gui_ui_cache_subsystem.h:82
uint64_t themeEpoch
global epoch at capture; a newer one rebuilds
Definition gui_ui_cache_subsystem.h:77
int2 cachedSize
container inner size at capture
Definition gui_ui_cache_subsystem.h:64
bool usesPostLayout
The subtree uses AddPostLayout, so it needs the deferred-measurement settle.
Definition gui_ui_cache_subsystem.h:68
bool hasCachedSize
Definition gui_ui_cache_subsystem.h:66
std::vector< ID > claimedSwatches
Swatch color-style ids claimed while building the scope; kept alive on hit frames.
Definition gui_ui_cache_subsystem.h:53
bool missedThisFrame
rebuilt (not replayed) this frame
Definition gui_ui_cache_subsystem.h:73
bool captured
a snapshot exists
Definition gui_ui_cache_subsystem.h:70
ID snapActive
Definition gui_ui_cache_subsystem.h:58
ID snapHover
Hovered element at capture time when it is in the scope, else Invalid; the other snap* fields work th...
Definition gui_ui_cache_subsystem.h:57
IntRect cachedContentBounds
the built content's bounds, restored on clean frames
Definition gui_ui_cache_subsystem.h:65
std::vector< std::pair< ID, int2 > > scrollSnapshot
Scroll offsets of scroll containers in the scope at capture time.
Definition gui_ui_cache_subsystem.h:47
ID snapFocus
Definition gui_ui_cache_subsystem.h:59
ElementRef lastContainerRef
refreshed every frame in BeginCached
Definition gui_ui_cache_subsystem.h:63
Integer 2D vector.
Definition gui_base_types.h:18