diff options
author | der richter <der.richter@gmx.de> | 2024-04-06 23:31:05 +0200 |
---|---|---|
committer | der richter <der.richter@gmx.de> | 2024-04-10 19:14:20 +0200 |
commit | ee6794225d2be8cb56afb9ba441a89d5e1319b87 (patch) | |
tree | 5e8e61c5e828a87987523422517f6c1e98c99ab5 /osdep | |
parent | 6df07ce90c894d561c179090675f30d811f9629b (diff) | |
download | mpv-ee6794225d2be8cb56afb9ba441a89d5e1319b87.tar.bz2 mpv-ee6794225d2be8cb56afb9ba441a89d5e1319b87.tar.xz |
mac/vulkan: add support for frame timing via presentation feedback
Diffstat (limited to 'osdep')
-rw-r--r-- | osdep/mac/app_bridge.h | 5 | ||||
-rw-r--r-- | osdep/mac/app_bridge.m | 3 | ||||
-rw-r--r-- | osdep/mac/presentation.swift | 56 | ||||
-rw-r--r-- | osdep/timer-darwin.c | 7 | ||||
-rw-r--r-- | osdep/timer.c | 7 | ||||
-rw-r--r-- | osdep/timer.h | 9 |
6 files changed, 82 insertions, 5 deletions
diff --git a/osdep/mac/app_bridge.h b/osdep/mac/app_bridge.h index fe1180a7ec..db03c8ebad 100644 --- a/osdep/mac/app_bridge.h +++ b/osdep/mac/app_bridge.h @@ -29,9 +29,10 @@ enum { }; enum { - RENDER_TIMER_CALLBACK = 0, - RENDER_TIMER_PRECISE, + RENDER_TIMER_PRESENTATION_FEEDBACK = -1, RENDER_TIMER_SYSTEM, + RENDER_TIMER_CALLBACK, + RENDER_TIMER_PRECISE, }; struct macos_opts { diff --git a/osdep/mac/app_bridge.m b/osdep/mac/app_bridge.m index 8e9c4a444b..bf39efe603 100644 --- a/osdep/mac/app_bridge.m +++ b/osdep/mac/app_bridge.m @@ -51,7 +51,7 @@ const struct m_sub_options macos_conf = { {"visible", FRAME_VISIBLE}, {"whole", FRAME_WHOLE})}, {"macos-render-timer", OPT_CHOICE(macos_render_timer, {"callback", RENDER_TIMER_CALLBACK}, {"precise", RENDER_TIMER_PRECISE}, - {"system", RENDER_TIMER_SYSTEM})}, + {"system", RENDER_TIMER_SYSTEM}, {"feedback", RENDER_TIMER_PRESENTATION_FEEDBACK})}, {"cocoa-cb-sw-renderer", OPT_CHOICE(cocoa_cb_sw_renderer, {"auto", -1}, {"no", 0}, {"yes", 1})}, {"cocoa-cb-10bit-context", OPT_BOOL(cocoa_cb_10bit_context)}, @@ -61,6 +61,7 @@ const struct m_sub_options macos_conf = { .defaults = &(const struct macos_opts){ .macos_title_bar_color = {0, 0, 0, 0}, .macos_fs_animation_duration = -1, + .macos_render_timer = RENDER_TIMER_CALLBACK, .cocoa_cb_sw_renderer = -1, .cocoa_cb_10bit_context = true }, diff --git a/osdep/mac/presentation.swift b/osdep/mac/presentation.swift new file mode 100644 index 0000000000..c1d521a0c8 --- /dev/null +++ b/osdep/mac/presentation.swift @@ -0,0 +1,56 @@ +/* + * This file is part of mpv. + * + * mpv is free software) you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation) either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY) without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +extension Presentation { + struct Time { + let cvTime: CVTimeStamp + var skipped: Int64 = 0 + var time: Int64 { return mp_time_ns_from_raw_time(mp_raw_time_ns_from_mach(cvTime.hostTime)) } + var duration: Int64 { + let durationSeconds = Double(cvTime.videoRefreshPeriod) / Double(cvTime.videoTimeScale) + return Int64(durationSeconds * Presentation.nanoPerSecond * cvTime.rateScalar) + } + + init(_ time: CVTimeStamp) { + cvTime = time + } + } +} + +class Presentation { + unowned var common: Common + var times: [Time] = [] + static let nanoPerSecond: Double = 1e+9 + + init(common com: Common) { + common = com + } + + func add(time: CVTimeStamp) { + times.append(Time(time)) + } + + func next() -> Time? { + let now = mp_time_ns() + let count = times.count + times.removeAll(where: { $0.time <= now }) + var time = times.first + time?.skipped = Int64(max(count - times.count - 1, 0)) + + return time + } +} diff --git a/osdep/timer-darwin.c b/osdep/timer-darwin.c index bb8a9b4324..36e719425e 100644 --- a/osdep/timer-darwin.c +++ b/osdep/timer-darwin.c @@ -36,7 +36,12 @@ void mp_sleep_ns(int64_t ns) uint64_t mp_raw_time_ns(void) { - return mach_absolute_time() * timebase_ratio_ns; + return mp_raw_time_ns_from_mach(mach_absolute_time()); +} + +uint64_t mp_raw_time_ns_from_mach(uint64_t mach_time) +{ + return mach_time * timebase_ratio_ns; } void mp_raw_time_init(void) diff --git a/osdep/timer.c b/osdep/timer.c index d0a8a92f8a..907ba50a65 100644 --- a/osdep/timer.c +++ b/osdep/timer.c @@ -46,7 +46,12 @@ void mp_time_init(void) int64_t mp_time_ns(void) { - return mp_raw_time_ns() - raw_time_offset; + return mp_time_ns_from_raw_time(mp_raw_time_ns()); +} + +int64_t mp_time_ns_from_raw_time(uint64_t raw_time) +{ + return raw_time - raw_time_offset; } double mp_time_sec(void) diff --git a/osdep/timer.h b/osdep/timer.h index d8ec185bfe..5fedbb69cd 100644 --- a/osdep/timer.h +++ b/osdep/timer.h @@ -19,6 +19,7 @@ #define MPLAYER_TIMER_H #include <inttypes.h> +#include "config.h" // Initialize timer, must be called at least once at start. void mp_time_init(void); @@ -26,6 +27,9 @@ void mp_time_init(void); // Return time in nanoseconds. Never wraps. Never returns negative values. int64_t mp_time_ns(void); +// Return time in nanoseconds. Coverts raw time in nanoseconds to mp time, subtracts init offset. +int64_t mp_time_ns_from_raw_time(uint64_t raw_time); + // Return time in seconds. Can have down to 1 nanosecond resolution, but will // be much worse when casted to float. double mp_time_sec(void); @@ -38,6 +42,11 @@ uint64_t mp_raw_time_ns(void); // Sleep in nanoseconds. void mp_sleep_ns(int64_t ns); +#if HAVE_DARWIN +// Coverts mach time to raw time in nanoseconds and returns it. +uint64_t mp_raw_time_ns_from_mach(uint64_t mach_time); +#endif + #ifdef _WIN32 // returns: timer resolution in ns if needed and started successfully, else 0 int64_t mp_start_hires_timers(int64_t wait_ns); |