1+ use cidre:: { arc, ns, sc} ;
2+ use core_graphics:: { display:: CGDirectDisplayID , window:: CGWindowID } ;
3+ use std:: sync:: Arc ;
14use std:: {
25 collections:: HashMap ,
36 sync:: { OnceLock , RwLock } ,
47 time:: Instant ,
58} ;
6-
7- use cidre:: { arc, ns, sc} ;
8- use core_graphics:: { display:: CGDirectDisplayID , window:: CGWindowID } ;
9- use std:: sync:: Arc ;
109use tokio:: sync:: { Mutex , Notify } ;
1110use tracing:: { debug, info, trace} ;
1211
@@ -30,7 +29,7 @@ fn state() -> &'static CacheState {
3029 STATE . get_or_init ( CacheState :: default)
3130}
3231
33- pub ( super ) async fn prewarm_shareable_content ( ) -> Result < ( ) , arc:: R < ns:: Error > > {
32+ pub async fn prewarm_shareable_content ( ) -> Result < ( ) , arc:: R < ns:: Error > > {
3433 if state ( ) . cache . read ( ) . unwrap ( ) . is_some ( ) {
3534 trace ! ( "ScreenCaptureKit shareable content already warmed" ) ;
3635 return Ok ( ( ) ) ;
@@ -61,6 +60,35 @@ pub(super) async fn prewarm_shareable_content() -> Result<(), arc::R<ns::Error>>
6160 . expect ( "ScreenCaptureKit warmup task missing result" )
6261}
6362
63+ pub async fn get_shareable_content ( )
64+ -> Result < Option < arc:: R < sc:: ShareableContent > > , arc:: R < ns:: Error > > {
65+ let lookup_start = Instant :: now ( ) ;
66+
67+ if let Some ( content) = state ( )
68+ . cache
69+ . read ( )
70+ . unwrap ( )
71+ . as_ref ( )
72+ . map ( |v| v. content . retained ( ) )
73+ {
74+ trace ! (
75+ elapsed_ms = lookup_start. elapsed( ) . as_micros( ) as f64 / 1000.0 ,
76+ "Resolved ScreenCaptureKit from warmed cache"
77+ ) ;
78+ return Ok ( Some ( content) ) ;
79+ }
80+
81+ prewarm_shareable_content ( ) . await ?;
82+
83+ let content = state ( ) . cache . read ( ) . unwrap ( ) ;
84+ trace ! (
85+ elapsed_ms = lookup_start. elapsed( ) . as_micros( ) as f64 / 1000.0 ,
86+ cache_hit = content. is_some( ) ,
87+ "Resolved ScreenCaptureKit after cache populate"
88+ ) ;
89+ Ok ( content. as_ref ( ) . map ( |v| v. content . retained ( ) ) )
90+ }
91+
6492async fn run_warmup ( task : WarmupTask ) {
6593 let result = async {
6694 let warm_start = Instant :: now ( ) ;
@@ -97,80 +125,6 @@ async fn run_warmup(task: WarmupTask) {
97125 }
98126}
99127
100- pub ( super ) async fn get_display (
101- id : CGDirectDisplayID ,
102- ) -> Result < Option < arc:: R < sc:: Display > > , arc:: R < ns:: Error > > {
103- let lookup_start = Instant :: now ( ) ;
104-
105- if let Some ( display) = state ( )
106- . cache
107- . read ( )
108- . unwrap ( )
109- . as_ref ( )
110- . and_then ( |cache| cache. display ( id) )
111- {
112- trace ! (
113- display_id = id,
114- elapsed_ms = lookup_start. elapsed( ) . as_micros( ) as f64 / 1000.0 ,
115- "Resolved ScreenCaptureKit display from warmed cache"
116- ) ;
117- return Ok ( Some ( display) ) ;
118- }
119-
120- prewarm_shareable_content ( ) . await ?;
121-
122- let result = state ( )
123- . cache
124- . read ( )
125- . unwrap ( )
126- . as_ref ( )
127- . and_then ( |cache| cache. display ( id) ) ;
128- trace ! (
129- display_id = id,
130- elapsed_ms = lookup_start. elapsed( ) . as_micros( ) as f64 / 1000.0 ,
131- cache_hit = result. is_some( ) ,
132- "Resolved ScreenCaptureKit display after cache populate"
133- ) ;
134- Ok ( result)
135- }
136-
137- pub ( super ) async fn get_window (
138- id : CGWindowID ,
139- ) -> Result < Option < arc:: R < sc:: Window > > , arc:: R < ns:: Error > > {
140- let lookup_start = Instant :: now ( ) ;
141-
142- if let Some ( window) = state ( )
143- . cache
144- . read ( )
145- . unwrap ( )
146- . as_ref ( )
147- . and_then ( |cache| cache. window ( id) )
148- {
149- trace ! (
150- window_id = id,
151- elapsed_ms = lookup_start. elapsed( ) . as_micros( ) as f64 / 1000.0 ,
152- "Resolved ScreenCaptureKit window from warmed cache"
153- ) ;
154- return Ok ( Some ( window) ) ;
155- }
156-
157- prewarm_shareable_content ( ) . await ?;
158-
159- let result = state ( )
160- . cache
161- . read ( )
162- . unwrap ( )
163- . as_ref ( )
164- . and_then ( |cache| cache. window ( id) ) ;
165- trace ! (
166- window_id = id,
167- elapsed_ms = lookup_start. elapsed( ) . as_micros( ) as f64 / 1000.0 ,
168- cache_hit = result. is_some( ) ,
169- "Resolved ScreenCaptureKit window after cache populate"
170- ) ;
171- Ok ( result)
172- }
173-
174128#[ derive( Debug ) ]
175129struct ShareableContentCache {
176130 #[ allow( dead_code) ]
0 commit comments