Skip to content

Commit fb8c933

Browse files
guidocellakasper93
authored andcommitted
osc.lua: cache observed properties
For properties that were already being observed but still retrieved over and over, cache their values using the new observe_cached(). osd-dimensions observer said that if cached we may have to worry about property ordering but it seems to work fine. It is nice to cache it because it was the property retrieved the most frequently, in 5 different places.
1 parent d4ae20b commit fb8c933

1 file changed

Lines changed: 36 additions & 40 deletions

File tree

player/lua/osc.lua

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ local state = {
312312
sub_track_count = 0,
313313
current_tracks = {},
314314
no_video = false,
315+
playlist_count = 0,
316+
playlist_pos_1 = 0,
317+
pause = false,
318+
volume = 0,
319+
mute = false,
320+
osd_dimensions = {w = 0, h = 0, aspect = 0},
321+
osd_scale_by_window = false,
315322
file_loaded = false,
316323
enabled = true,
317324
input_enabled = true,
@@ -400,11 +407,11 @@ end
400407

401408
-- scale factor for translating between real and virtual ASS coordinates
402409
local function get_virt_scale_factor()
403-
local w, h = mp.get_osd_size()
404-
if w <= 0 or h <= 0 then
410+
if state.osd_dimensions.w == 0 or state.osd_dimensions.h == 0 then
405411
return 0, 0
406412
end
407-
return osc_param.playresx / w, osc_param.playresy / h
413+
return osc_param.playresx / state.osd_dimensions.w,
414+
osc_param.playresy / state.osd_dimensions.h
408415
end
409416

410417
local function recently_touched()
@@ -1189,13 +1196,12 @@ local function render_elements(master_ass)
11891196
mp.set_property_number("user-data/osc/hover-sec", hover_sec)
11901197

11911198
-- thumbnail
1192-
local osd_w, osd_h = mp.get_osd_size()
11931199
local vop = mp.get_property_native("video-out-params")
1194-
local draw_thumbnail = osd_w > 0 and vop
1200+
local draw_thumbnail = state.osd_dimensions.w > 0 and vop
11951201
if draw_thumbnail then
11961202
local r_w, r_h = get_virt_scale_factor()
11971203
local thumb_max = math.min(user_opts.max_thumb_size,
1198-
math.min(osd_w, osd_h) * 0.25)
1204+
math.min(state.osd_dimensions.w, state.osd_dimensions.h) * 0.25)
11991205
local scale = thumb_max / math.max(vop.dw, vop.dh)
12001206
local thumb_w = math.floor(vop.dw * scale + 0.5)
12011207
local thumb_h = math.floor(vop.dh * scale + 0.5)
@@ -1207,7 +1213,7 @@ local function render_elements(master_ass)
12071213
local thumb_pad = 4
12081214
local thumb_margin_x = 20 / r_w
12091215
local thumb_margin_y = (4 + user_opts.tooltipborder) / r_h + thumb_pad
1210-
local thumb_x = math.min(osd_w - thumb_w - thumb_margin_x,
1216+
local thumb_x = math.min(state.osd_dimensions.w - thumb_w - thumb_margin_x,
12111217
math.max(thumb_margin_x, thumb_tx / r_w - thumb_w / 2))
12121218
local thumb_y = thumb_ty / r_h + (user_opts.layout ~= "topbar" and
12131219
-(thumb_h + tooltip_font_size / r_h + thumb_margin_y) or
@@ -2299,7 +2305,6 @@ local function osc_init()
22992305

23002306
-- set canvas resolution according to display aspect and scaling setting
23012307
local baseResY = 720
2302-
local _, display_h, display_aspect = mp.get_osd_size()
23032308
local scale
23042309

23052310
if state.fullscreen then
@@ -2310,19 +2315,19 @@ local function osc_init()
23102315

23112316
local scale_with_video
23122317
if user_opts.vidscale == "auto" then
2313-
scale_with_video = mp.get_property_native("osd-scale-by-window")
2318+
scale_with_video = state.osd_scale_by_window
23142319
else
23152320
scale_with_video = user_opts.vidscale == "yes"
23162321
end
23172322

23182323
if scale_with_video then
23192324
osc_param.unscaled_y = baseResY
23202325
else
2321-
osc_param.unscaled_y = display_h
2326+
osc_param.unscaled_y = state.osd_dimensions.h
23222327
end
23232328
osc_param.playresy = osc_param.unscaled_y / scale
2324-
if display_aspect > 0 then
2325-
osc_param.display_aspect = display_aspect
2329+
if state.osd_dimensions.aspect > 0 then
2330+
osc_param.display_aspect = state.osd_dimensions.aspect
23262331
end
23272332
osc_param.playresx = osc_param.playresy * osc_param.display_aspect
23282333

@@ -2334,9 +2339,6 @@ local function osc_init()
23342339
elements = {}
23352340

23362341
-- some often needed stuff
2337-
local pl_count = mp.get_property_number("playlist-count", 0)
2338-
local have_pl = (pl_count > 1)
2339-
local pl_pos = mp.get_property_number("playlist-pos", 0) + 1
23402342
local have_ch = (mp.get_property_number("chapters", 0) > 0)
23412343
local loop = mp.get_property("loop-playlist", "no")
23422344

@@ -2364,14 +2366,15 @@ local function osc_init()
23642366
ne = new_element("playlist_prev", "button")
23652367

23662368
ne.content = icons.prev
2367-
ne.enabled = (pl_pos > 1) or (loop ~= "no")
2369+
ne.enabled = state.playlist_pos_1 > 1 or loop ~= "no"
23682370
bind_mouse_buttons("playlist_prev")
23692371

23702372
--next
23712373
ne = new_element("playlist_next", "button")
23722374

23732375
ne.content = icons.next
2374-
ne.enabled = (have_pl and (pl_pos < pl_count)) or (loop ~= "no")
2376+
ne.enabled = (state.playlist_count > 1 and state.playlist_pos_1 < state.playlist_count)
2377+
or loop ~= "no"
23752378
bind_mouse_buttons("playlist_next")
23762379

23772380

@@ -2385,7 +2388,7 @@ local function osc_init()
23852388
return icons.clock
23862389
end
23872390

2388-
if not mp.get_property_native("pause") then
2391+
if not state.pause then
23892392
return icons.pause
23902393
end
23912394

@@ -2643,12 +2646,11 @@ local function osc_init()
26432646
ne = new_element("volume", "button")
26442647

26452648
ne.content = function()
2646-
local volume = mp.get_property_number("volume")
2647-
if volume == 0 or mp.get_property_native("mute") then
2649+
if state.volume == 0 or state.mute then
26482650
return icons.mute
26492651
end
26502652

2651-
return icons.volume[math.min(4, math.ceil(volume / (100/3)))]
2653+
return icons.volume[math.min(4, math.ceil(state.volume / (100/3)))]
26522654
end
26532655
bind_mouse_buttons("volume")
26542656

@@ -2897,18 +2899,17 @@ end
28972899

28982900
local function render()
28992901
msg.trace("rendering")
2900-
local current_screen_sizeX, current_screen_sizeY = mp.get_osd_size()
29012902
local mouseX, mouseY = get_virt_mouse_pos()
29022903
local now = mp.get_time()
29032904

29042905
-- check if display changed, if so request reinit
2905-
if state.screen_sizeX ~= current_screen_sizeX
2906-
or state.screen_sizeY ~= current_screen_sizeY then
2906+
if state.screen_sizeX ~= state.osd_dimensions.w
2907+
or state.screen_sizeY ~= state.osd_dimensions.h then
29072908

29082909
request_init_resize()
29092910

2910-
state.screen_sizeX = current_screen_sizeX
2911-
state.screen_sizeY = current_screen_sizeY
2911+
state.screen_sizeX = state.osd_dimensions.w
2912+
state.screen_sizeY = state.osd_dimensions.h
29122913
end
29132914

29142915
-- init management
@@ -3044,12 +3045,11 @@ local function render()
30443045
end
30453046

30463047
local function render_logo()
3047-
local _, _, display_aspect = mp.get_osd_size()
3048-
if display_aspect == 0 then
3048+
if state.osd_dimensions.aspect == 0 then
30493049
return
30503050
end
30513051
local display_h = 360
3052-
local display_w = display_h * display_aspect
3052+
local display_w = display_h * state.osd_dimensions.aspect
30533053
-- logo is rendered at 2^(6-1) = 32 times resolution with size 1800x1800
30543054
local icon_x, icon_y = (display_w - 1800 / 32) / 2, (display_h - 1800 / 32) / 2
30553055
local line_prefix = ("{\\rDefault\\an7\\1a&H00&\\bord0\\shad0\\pos(%f,%f)}"):format(icon_x,
@@ -3185,8 +3185,8 @@ end
31853185
mp.register_event("shutdown", shutdown)
31863186
mp.register_event("start-file", request_init)
31873187
mp.observe_property("track-list", "native", update_tracklist)
3188-
mp.observe_property("playlist-count", "native", request_init)
3189-
mp.observe_property("playlist-pos", "native", request_init)
3188+
observe_cached("playlist-count", request_init)
3189+
observe_cached("playlist-pos-1", request_init)
31903190
observe_cached("chapter-list", function ()
31913191
table.sort(state.chapter_list, function(a, b) return a.time < b.time end)
31923192
update_duration_watch()
@@ -3249,18 +3249,14 @@ mp.add_hook("on_unload", 50, function()
32493249
end)
32503250

32513251
mp.observe_property("display-fps", "number", set_tick_delay)
3252-
mp.observe_property("pause", "bool", request_tick)
3253-
mp.observe_property("volume", "number", request_tick)
3254-
mp.observe_property("mute", "bool", request_tick)
3252+
observe_cached("pause", request_tick)
3253+
observe_cached("volume", request_tick)
3254+
observe_cached("mute", request_tick)
32553255
observe_cached("demuxer-cache-state", request_tick)
32563256
mp.observe_property("vo-configured", "bool", request_tick)
32573257
mp.observe_property("playback-time", "number", request_tick)
3258-
mp.observe_property("osd-dimensions", "native", function()
3259-
-- (we could use the value instead of re-querying it all the time, but then
3260-
-- we might have to worry about property update ordering)
3261-
request_init_resize()
3262-
end)
3263-
mp.observe_property('osd-scale-by-window', 'native', request_init_resize)
3258+
observe_cached("osd-dimensions", request_init_resize)
3259+
observe_cached("osd-scale-by-window", request_init_resize)
32643260
mp.observe_property('touch-pos', 'native', handle_touch)
32653261

32663262
-- mouse show/hide bindings

0 commit comments

Comments
 (0)