@@ -20,6 +20,7 @@ use std::{
2020use super :: { AppData , Buffer , Capture , CaptureImage , Event } ;
2121
2222pub struct ScreencopySession {
23+ formats : Option < Formats > ,
2324 // swapchain buffers
2425 buffers : Option < [ Buffer ; 2 ] > ,
2526 session : CaptureSession ,
@@ -45,6 +46,7 @@ impl ScreencopySession {
4546 . unwrap ( ) ;
4647
4748 Self {
49+ formats : None ,
4850 buffers : None ,
4951 session,
5052 release : None ,
@@ -121,8 +123,9 @@ impl ScreencopyHandler for AppData {
121123 return ;
122124 } ;
123125
126+ session. formats = Some ( formats. clone ( ) ) ;
127+
124128 // Create new buffer if none, then start capturing
125- // XXX What if formats have changed?
126129 if session. buffers . is_none ( ) {
127130 session. buffers = Some ( array:: from_fn ( |_| self . create_buffer ( formats) ) ) ;
128131 session. attach_buffer_and_commit ( & capture, conn, & self . qh ) ;
@@ -209,15 +212,29 @@ impl ScreencopyHandler for AppData {
209212
210213 fn failed (
211214 & mut self ,
212- _conn : & Connection ,
215+ conn : & Connection ,
213216 _qh : & QueueHandle < Self > ,
214217 capture_frame : & CaptureFrame ,
215218 reason : WEnum < FailureReason > ,
216219 ) {
217- // TODO
218- log:: error!( "Screencopy failed: {:?}" , reason) ;
219220 let capture = & capture_frame. data :: < FrameData > ( ) . unwrap ( ) . capture ;
220- if let Some ( capture) = capture. upgrade ( ) {
221+ let Some ( capture) = capture. upgrade ( ) else {
222+ return ;
223+ } ;
224+ if reason == WEnum :: Value ( FailureReason :: BufferConstraints ) {
225+ // Re-allocate buffers, then trigger another capture
226+ log:: info!( "buffer constraint failure; re-allocating" ) ;
227+ let mut session = capture. session . lock ( ) . unwrap ( ) ;
228+ let Some ( session) = session. as_mut ( ) else {
229+ return ;
230+ } ;
231+ if let Some ( formats) = & session. formats {
232+ session. buffers = Some ( array:: from_fn ( |_| self . create_buffer ( & formats) ) ) ;
233+ }
234+ session. attach_buffer_and_commit ( & capture, conn, & self . qh ) ;
235+ } else {
236+ // TODO
237+ log:: error!( "Screencopy failed: {:?}" , reason) ;
221238 capture. stop ( ) ;
222239 }
223240 }
0 commit comments