Skip to content

Commit

Permalink
bench: Update for better experience (#189)
Browse files Browse the repository at this point in the history
- Implement timer to tie minimum fps to 1
- Update default options for better results
- Drop frames_per_update option
  • Loading branch information
soreau authored Oct 2, 2023
1 parent aeac706 commit 64a7dba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
8 changes: 1 addition & 7 deletions metadata/bench.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
<_short>Bench</_short>
<_long>Display fps on each output.</_long>
<category>Utility</category>
<option name="frames_per_update" type="int">
<_short>Frames per Update</_short>
<_long>How many frames will pass before each fps sample is displayed.</_long>
<default>3</default>
<min>1</min>
</option>
<option name="average_frames" type="int">
<_short>Average frames</_short>
<_long>How many frames to average to get the FPS value.</_long>
<default>1</default>
<default>25</default>
<min>1</min>
</option>
<option name="position" type="string">
Expand Down
54 changes: 35 additions & 19 deletions src/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class wayfire_bench_screen : public wf::per_output_plugin_instance_t
uint32_t last_time = wf::get_current_time();
double current_fps;
double widget_radius;
wf::wl_timer<false> timer;
wf::simple_texture_t bench_tex;
wf::geometry_t cairo_geometry;
cairo_surface_t *cairo_surface;
Expand All @@ -56,7 +57,6 @@ class wayfire_bench_screen : public wf::per_output_plugin_instance_t
int frames_since_last_update = 0;
wf::option_wrapper_t<std::string> position{"bench/position"};
wf::option_wrapper_t<int> average_frames{"bench/average_frames"};
wf::option_wrapper_t<int> frames_per_update{"bench/frames_per_update"};

public:
void init() override
Expand All @@ -68,6 +68,34 @@ class wayfire_bench_screen : public wf::per_output_plugin_instance_t
output->connect(&workarea_changed);
position.set_callback(position_changed);
update_texture_position();

reset_timeout();
}

void compute_timing()
{
uint32_t current_time = wf::get_current_time();
uint32_t elapsed = current_time - last_time;
last_time = current_time;
while ((int)last_frame_times.size() >= average_frames)
{
last_frame_times.pop_front();
}

last_frame_times.push_back(elapsed);

render_bench();

reset_timeout();
}

void reset_timeout()
{
timer.disconnect();
timer.set_timeout(1000, [=] ()
{
output->render->damage(cairo_geometry);
});
}

wf::config::option_base_t::updated_callback_t position_changed = [=] ()
Expand Down Expand Up @@ -203,7 +231,7 @@ class wayfire_bench_screen : public wf::per_output_plugin_instance_t
last_frame_times.begin(), last_frame_times.end(), 0.0);
average /= last_frame_times.size();

current_fps = (double)1000 / average;
current_fps = 1000.0 / average;

if (current_fps > max_fps)
{
Expand All @@ -218,7 +246,7 @@ class wayfire_bench_screen : public wf::per_output_plugin_instance_t
if (output->handle->current_mode)
{
fps_angle = max_angle + (current_fps /
((double)output->handle->current_mode->refresh / 1000)) *
((double)std::max(1, output->handle->current_mode->refresh) / 1000)) *
(target_angle - max_angle);
} else
{
Expand Down Expand Up @@ -266,24 +294,11 @@ class wayfire_bench_screen : public wf::per_output_plugin_instance_t

wf::effect_hook_t pre_hook = [=] ()
{
uint32_t current_time = wf::get_current_time();
uint32_t elapsed = current_time - last_time;

while ((int)last_frame_times.size() >= average_frames)
{
last_frame_times.pop_front();
}

last_frame_times.push_back(elapsed);

if (++frames_since_last_update >= frames_per_update)
if (!output->render->get_scheduled_damage().empty())
{
render_bench();
frames_since_last_update = 0;
output->render->damage(cairo_geometry);
compute_timing();
}

last_time = current_time;
output->render->damage(cairo_geometry);
};

wf::effect_hook_t overlay_hook = [=] ()
Expand All @@ -298,6 +313,7 @@ class wayfire_bench_screen : public wf::per_output_plugin_instance_t

void fini() override
{
timer.disconnect();
output->render->set_redraw_always(false);
output->render->rem_effect(&pre_hook);
output->render->rem_effect(&overlay_hook);
Expand Down

0 comments on commit 64a7dba

Please sign in to comment.