From 75a7e0a32c449db33b6032b752cc28a50fb3b478 Mon Sep 17 00:00:00 2001 From: Jon Ringle Date: Mon, 18 Mar 2024 20:23:13 -0400 Subject: [PATCH 1/3] Fix STDIO_TIMEOUT: monotonic_time() returns milliseconds The STDIO_TIMEOUT returns millisecond units, so the STDIO_TIMEOUT should also be in millisecond units --- c_src/scenic/comms.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/c_src/scenic/comms.c b/c_src/scenic/comms.c index 7cef107..62d804f 100644 --- a/c_src/scenic/comms.c +++ b/c_src/scenic/comms.c @@ -21,19 +21,11 @@ The caller will typically be erlang, so use the 2-byte length indicator #include "script.h" #include "utils.h" -// handy time definitions in microseconds -#define MILLISECONDS_8 8000 -#define MILLISECONDS_16 16000 -#define MILLISECONDS_20 20000 -#define MILLISECONDS_32 32000 -#define MILLISECONDS_64 64000 -#define MILLISECONDS_128 128000 - // Setting the timeout too high means input will be laggy as you // are starving the input polling. Setting it too low means using // energy for no purpose. Probably best if set similar to the // frame rate of the application -#define STDIO_TIMEOUT MILLISECONDS_32 +#define STDIO_TIMEOUT 32 extern device_info_t g_device_info; From 4a2073b20c93036c9215d5ec33e3905246298ffc Mon Sep 17 00:00:00 2001 From: Jon Ringle Date: Mon, 18 Mar 2024 20:24:20 -0400 Subject: [PATCH 2/3] debug fps: log number of frames rendered in each second --- c_src/scenic/comms.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/c_src/scenic/comms.c b/c_src/scenic/comms.c index 62d804f..5321b83 100644 --- a/c_src/scenic/comms.c +++ b/c_src/scenic/comms.c @@ -399,6 +399,14 @@ void receive_crash() //--------------------------------------------------------- void render(driver_data_t* p_data) { + // Setup FPS calc + static int64_t start_real = 0; + static int64_t time_remaining = -1; + static clock_t render_fps = 0; + static uint32_t frames = 0; + + clock_t begin_frame = clock(); + // prep the id to the root scene sid_t id; id.p_data = "_root_"; @@ -420,6 +428,27 @@ void render(driver_data_t* p_data) } device_end_render(p_data); + clock_t end_frame = clock(); + clock_t delta_ticks = end_frame - begin_frame; + + if (delta_ticks > 0) { + render_fps = CLOCKS_PER_SEC / delta_ticks; + // log_debug("render_fps (cpu time): %d", render_fps); + } + + frames++; + + int64_t end_real = monotonic_time(); + int64_t delta_real = (end_real - start_real); + start_real = end_real; + time_remaining -= delta_real; + + if (time_remaining <= 0) { + log_debug("real_fps: %d", frames); + start_real = monotonic_time(); + time_remaining = 1000; + frames = 0; + } // all done send_ready(); From 9ae099b430ab512022d32901a6e5065db43ea771 Mon Sep 17 00:00:00 2001 From: Jon Ringle Date: Mon, 18 Mar 2024 22:37:45 -0400 Subject: [PATCH 3/3] debug_fps: Add configurable option --- c_src/main.c | 11 ++++++----- c_src/scenic/comms.c | 9 +++++---- c_src/scenic/scenic_types.h | 1 + lib/driver.ex | 4 +++- test/driver_test.exs | 1 + 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/c_src/main.c b/c_src/main.c index 87fe063..167be27 100644 --- a/c_src/main.c +++ b/c_src/main.c @@ -30,7 +30,7 @@ int main(int argc, char **argv) driver_data_t data = {0}; // super simple arg check - if (argc != 10) { + if (argc != 11) { log_error("Wrong number of parameters"); return -1; } @@ -41,10 +41,11 @@ int main(int argc, char **argv) g_opts.global_opacity = atoi(argv[3]); g_opts.antialias = atoi(argv[4]); g_opts.debug_mode = atoi(argv[5]); - g_opts.width = atoi(argv[6]); - g_opts.height = atoi(argv[7]); - g_opts.resizable = atoi(argv[8]); - g_opts.title = argv[9]; + g_opts.debug_fps = atoi(argv[6]); + g_opts.width = atoi(argv[7]); + g_opts.height = atoi(argv[8]); + g_opts.resizable = atoi(argv[9]); + g_opts.title = argv[10]; // init the hashtables init_scripts(); diff --git a/c_src/scenic/comms.c b/c_src/scenic/comms.c index 5321b83..d589736 100644 --- a/c_src/scenic/comms.c +++ b/c_src/scenic/comms.c @@ -28,6 +28,7 @@ The caller will typically be erlang, so use the 2-byte length indicator #define STDIO_TIMEOUT 32 extern device_info_t g_device_info; +extern device_opts_t g_opts; //============================================================================= // raw comms with host app @@ -431,9 +432,9 @@ void render(driver_data_t* p_data) clock_t end_frame = clock(); clock_t delta_ticks = end_frame - begin_frame; - if (delta_ticks > 0) { + if ((g_opts.debug_fps > 1) && (delta_ticks > 0)) { render_fps = CLOCKS_PER_SEC / delta_ticks; - // log_debug("render_fps (cpu time): %d", render_fps); + log_debug("render_fps (cpu time): %d", render_fps); } frames++; @@ -443,8 +444,8 @@ void render(driver_data_t* p_data) start_real = end_real; time_remaining -= delta_real; - if (time_remaining <= 0) { - log_debug("real_fps: %d", frames); + if ((g_opts.debug_fps > 0) && (time_remaining <= 0)) { + log_info("real_fps: %d", frames); start_real = monotonic_time(); time_remaining = 1000; frames = 0; diff --git a/c_src/scenic/scenic_types.h b/c_src/scenic/scenic_types.h index 4858a73..fef12c6 100644 --- a/c_src/scenic/scenic_types.h +++ b/c_src/scenic/scenic_types.h @@ -61,6 +61,7 @@ typedef struct { typedef struct { // options from the command line int debug_mode; + int debug_fps; int layer; int global_opacity; int antialias; diff --git a/lib/driver.ex b/lib/driver.ex index b2890e9..990a44a 100644 --- a/lib/driver.ex +++ b/lib/driver.ex @@ -26,6 +26,7 @@ defmodule Scenic.Driver.Local do opacity: [type: :integer, default: @default_opacity], debug: [type: :boolean, default: false], debugger: [type: :string, default: ""], + debug_fps: [type: :integer, default: 0], antialias: [type: :boolean, default: true], calibration: [ type: {:custom, __MODULE__, :validate_calibration, []}, @@ -214,6 +215,7 @@ defmodule Scenic.Driver.Local do end {:ok, debugger} = Keyword.fetch(opts, :debugger) + {:ok, debug_fps} = Keyword.fetch(opts, :debug_fps) {:ok, layer} = Keyword.fetch(opts, :layer) {:ok, opacity} = Keyword.fetch(opts, :opacity) @@ -227,7 +229,7 @@ defmodule Scenic.Driver.Local do end args = - " #{internal_cursor} #{layer} #{opacity} #{antialias} #{debug_mode}" <> + " #{internal_cursor} #{layer} #{opacity} #{antialias} #{debug_mode} #{debug_fps}" <> " #{width} #{height} #{resizeable} \"#{title}\"" # open and initialize the window diff --git a/test/driver_test.exs b/test/driver_test.exs index 316f7d1..ad9dc55 100644 --- a/test/driver_test.exs +++ b/test/driver_test.exs @@ -9,6 +9,7 @@ defmodule Scenic.Driver.LocalTest do opacity: 1, debug: false, debugger: "", + debug_fps: 0, antialias: true, calibration: [{"calibration_name", {{0, 0, 0}, {0, 0, 0}}}], position: [