better timing

This commit is contained in:
Jorijn van der Graaf 2025-11-25 21:25:04 +01:00
commit 59b6e2779f
4 changed files with 41 additions and 12 deletions

View file

@ -6,7 +6,7 @@ using namespace Crafter;
int main() { int main() {
WindowWayland window(1280, 720, "Hello Input!"); WindowWayland window(1280, 720, "Hello Input!");
UiElementBufferBuffer* element = new UiElementBufferBuffer( RenderingElement element(
2, //bufferWidth: the width of this elements pixel buffer 2, //bufferWidth: the width of this elements pixel buffer
1, //bufferHeight: the height of this elements pixel buffer 1, //bufferHeight: the height of this elements pixel buffer
FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
@ -19,9 +19,8 @@ int main() {
false //ignoreScaling: wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale false //ignoreScaling: wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale
); );
window.elements.push_back(element); window.elements.push_back(&element);
element.UpdatePosition(window);
element->UpdatePosition(window);
Animation<std::tuple<std::int_fast32_t>> anim({ Animation<std::tuple<std::int_fast32_t>> anim({
{std::chrono::seconds(0), FractionalToMapped(-0.5)}, {std::chrono::seconds(0), FractionalToMapped(-0.5)},
@ -32,14 +31,14 @@ int main() {
EventListener<FrameTime> updateListener(&window.onUpdate, [&](FrameTime time){ EventListener<FrameTime> updateListener(&window.onUpdate, [&](FrameTime time){
std::tuple<std::int_fast32_t> value = anim.Play(time.now); std::tuple<std::int_fast32_t> value = anim.Play(time.now);
element->transform.anchorX = std::get<0>(value); element.anchorX = std::get<0>(value);
element->UpdatePosition(window); element.UpdatePosition(window);
if(anim.currentFrame == anim.keyframes.size()-1) { if(anim.currentFrame == anim.keyframes.size()-1) {
anim.Start(time.now); anim.Start(time.now);
} }
}); });
element->buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; element.buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}};
window.StartUpdate(); window.StartUpdate();
window.StartSync(); window.StartSync();
} }

View file

@ -7,7 +7,7 @@
"dependencies": [ "dependencies": [
{ {
"path":"../../project.json", "path":"../../project.json",
"configuration":"lib-wayland" "configuration":"lib-wayland-timing"
} }
] ]
} }

View file

@ -232,30 +232,45 @@ void WindowWayland::xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base
xdg_wm_base_pong(xdg_wm_base, serial); xdg_wm_base_pong(xdg_wm_base, serial);
} }
#ifdef CRAFTER_TIMING
std::chrono::time_point<std::chrono::high_resolution_clock> framEnd; std::chrono::time_point<std::chrono::high_resolution_clock> framEnd;
#endif
void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) void WindowWayland::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time)
{ {
#ifdef CRAFTER_TIMING
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
auto vblank = duration_cast<std::chrono::milliseconds>(start - framEnd); auto vblank = duration_cast<std::chrono::milliseconds>(start - framEnd);
#endif
wl_callback_destroy(cb); wl_callback_destroy(cb);
WindowWayland* window = reinterpret_cast<WindowWayland*>(data); WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
if(window->updating) { if(window->updating) {
cb = wl_surface_frame(window->surface); cb = wl_surface_frame(window->surface);
wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window); wl_callback_add_listener(cb, &WindowWayland::wl_callback_listener, window);
auto startUpdate = std::chrono::high_resolution_clock::now();
window->onUpdate.Invoke({start, start-window->lastFrameBegin}); window->onUpdate.Invoke({start, start-window->lastFrameBegin});
auto endUpdate = std::chrono::high_resolution_clock::now(); #ifdef CRAFTER_TIMING
std::chrono::nanoseconds totalUpdate = std::chrono::nanoseconds(0);
for (const std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>& entry : window->onUpdate.listenerTimes) {
totalUpdate += entry.second;
}
std::cout << std::format("Update: {}", duration_cast<std::chrono::milliseconds>(totalUpdate)) << std::endl;
for (const std::pair<const EventListener<FrameTime>*, std::chrono::nanoseconds>& entry : window->onUpdate.listenerTimes) {
std::cout << std::format("\t{} {}", reinterpret_cast<const void*>(entry.first), entry.second) << std::endl;
}
auto startRender = std::chrono::high_resolution_clock::now(); auto startRender = std::chrono::high_resolution_clock::now();
#endif
window->Render(); window->Render();
#ifdef CRAFTER_TIMING
auto endRender = std::chrono::high_resolution_clock::now(); auto endRender = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
std::cout << std::format("Update: {}, Render: {}, Vblank: {}, Total: {}", duration_cast<std::chrono::milliseconds>(endUpdate - startUpdate), duration_cast<std::chrono::milliseconds>(endRender - startRender), vblank, duration_cast<std::chrono::milliseconds>((endUpdate - startUpdate)+(endRender - startRender)+vblank)) << std::endl; std::cout << std::format("Render: {}, Vblank: {}, Total: {}", duration_cast<std::chrono::milliseconds>(endRender - startRender), vblank, duration_cast<std::chrono::milliseconds>(totalUpdate+(endRender - startRender)+vblank)) << std::endl;
#endif
} }
#ifdef CRAFTER_TIMING
framEnd = std::chrono::high_resolution_clock::now(); framEnd = std::chrono::high_resolution_clock::now();
#endif
window->lastFrameBegin = start; window->lastFrameBegin = start;
} }

View file

@ -29,6 +29,15 @@
} }
] ]
}, },
{
"name": "deps-timing",
"dependencies": [
{
"path":"https://forgejo.catcrafts.net/Catcrafts/Crafter.Event.git",
"configuration":"lib-timing"
}
]
},
{ {
"name": "deps-debug", "name": "deps-debug",
"debug": true, "debug": true,
@ -44,6 +53,12 @@
"extends": ["wayland", "deps"], "extends": ["wayland", "deps"],
"type": "library" "type": "library"
}, },
{
"name": "lib-wayland-timing",
"extends": ["wayland", "deps-timing"],
"type": "library",
"defines": [{ "name": "CRAFTER_TIMING" }]
},
{ {
"name": "lib-wayland-debug", "name": "lib-wayland-debug",
"type": "library", "type": "library",