Store gamma changes to apply on session resume
Browse files at this commit642c6e7512f25f1fdc47dfa6a6eaec38c5faa69bParent: 6839a113 changed files +32 -7
- src/backend/tty.rs +28 -2
- src/handlers/mod.rs +1 -1
- src/protocols/gamma_control.rs +3 -4
src/backend/tty.rsmodified+28 -2
| @@ -169,6 +169,8 @@ struct Surface { | |||
| 169 | compositor: GbmDrmCompositor, | 169 | compositor: GbmDrmCompositor, |
| 170 | dmabuf_feedback: Option<SurfaceDmabufFeedback>, | 170 | dmabuf_feedback: Option<SurfaceDmabufFeedback>, |
| 171 | gamma_props: Option<GammaProps>, | 171 | gamma_props: Option<GammaProps>, |
| 172 | + /// Gamma change to apply upon session resume. | ||
| 173 | + pending_gamma_change: Option<Option<Vec<u16>>>, | ||
| 172 | /// Tracy frame that goes from vblank to vblank. | 174 | /// Tracy frame that goes from vblank to vblank. |
| 173 | vblank_frame: Option<tracy_client::Frame>, | 175 | vblank_frame: Option<tracy_client::Frame>, |
| 174 | /// Frame name for the VBlank frame. | 176 | /// Frame name for the VBlank frame. |
| @@ -394,6 +396,22 @@ impl Tty { | |||
| 394 | 396 | ||
| 395 | // Refresh the connectors. | 397 | // Refresh the connectors. |
| 396 | self.device_changed(node.dev_id(), niri); | 398 | self.device_changed(node.dev_id(), niri); |
| 399 | + | ||
| 400 | + // Apply pending gamma changes. | ||
| 401 | + let device = self.devices.get_mut(&node).unwrap(); | ||
| 402 | + for (crtc, surface) in device.surfaces.iter_mut() { | ||
| 403 | + if let Some(ramp) = surface.pending_gamma_change.take() { | ||
| 404 | + let ramp = ramp.as_deref(); | ||
| 405 | + let res = if let Some(gamma_props) = &mut surface.gamma_props { | ||
| 406 | + gamma_props.set_gamma(&device.drm, ramp) | ||
| 407 | + } else { | ||
| 408 | + set_gamma_for_crtc(&device.drm, *crtc, ramp) | ||
| 409 | + }; | ||
| 410 | + if let Err(err) = res { | ||
| 411 | + warn!("error applying pending gamma change: {err:?}"); | ||
| 412 | + } | ||
| 413 | + } | ||
| 414 | + } | ||
| 397 | } | 415 | } |
| 398 | 416 | ||
| 399 | // Add new devices. | 417 | // Add new devices. |
| @@ -839,6 +857,7 @@ impl Tty { | |||
| 839 | compositor, | 857 | compositor, |
| 840 | dmabuf_feedback, | 858 | dmabuf_feedback, |
| 841 | gamma_props, | 859 | gamma_props, |
| 860 | + pending_gamma_change: None, | ||
| 842 | vblank_frame: None, | 861 | vblank_frame: None, |
| 843 | vblank_frame_name, | 862 | vblank_frame_name, |
| 844 | time_since_presentation_plot_name, | 863 | time_since_presentation_plot_name, |
| @@ -1292,7 +1311,7 @@ impl Tty { | |||
| 1292 | } | 1311 | } |
| 1293 | } | 1312 | } |
| 1294 | 1313 | ||
| 1295 | - pub fn set_gamma(&mut self, output: &Output, ramp: Option<&[u16]>) -> anyhow::Result<()> { | 1314 | + pub fn set_gamma(&mut self, output: &Output, ramp: Option<Vec<u16>>) -> anyhow::Result<()> { |
| 1296 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); | 1315 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); |
| 1297 | let crtc = tty_state.crtc; | 1316 | let crtc = tty_state.crtc; |
| 1298 | 1317 | ||
| @@ -1300,8 +1319,15 @@ impl Tty { | |||
| 1300 | .devices | 1319 | .devices |
| 1301 | .get_mut(&tty_state.node) | 1320 | .get_mut(&tty_state.node) |
| 1302 | .context("missing device")?; | 1321 | .context("missing device")?; |
| 1303 | - | ||
| 1304 | let surface = device.surfaces.get_mut(&crtc).context("missing surface")?; | 1322 | let surface = device.surfaces.get_mut(&crtc).context("missing surface")?; |
| 1323 | + | ||
| 1324 | + // Cannot change properties while the device is inactive. | ||
| 1325 | + if !self.session.is_active() { | ||
| 1326 | + surface.pending_gamma_change = Some(ramp); | ||
| 1327 | + return Ok(()); | ||
| 1328 | + } | ||
| 1329 | + | ||
| 1330 | + let ramp = ramp.as_deref(); | ||
| 1305 | if let Some(gamma_props) = &mut surface.gamma_props { | 1331 | if let Some(gamma_props) = &mut surface.gamma_props { |
| 1306 | gamma_props.set_gamma(&device.drm, ramp) | 1332 | gamma_props.set_gamma(&device.drm, ramp) |
| 1307 | } else { | 1333 | } else { |
src/handlers/mod.rsmodified+1 -1
| @@ -460,7 +460,7 @@ impl GammaControlHandler for State { | |||
| 460 | } | 460 | } |
| 461 | } | 461 | } |
| 462 | 462 | ||
| 463 | - fn set_gamma(&mut self, output: &Output, ramp: Option<&[u16]>) -> Option<()> { | 463 | + fn set_gamma(&mut self, output: &Output, ramp: Option<Vec<u16>>) -> Option<()> { |
| 464 | match self.backend.tty().set_gamma(output, ramp) { | 464 | match self.backend.tty().set_gamma(output, ramp) { |
| 465 | Ok(()) => Some(()), | 465 | Ok(()) => Some(()), |
| 466 | Err(err) => { | 466 | Err(err) => { |
src/protocols/gamma_control.rsmodified+3 -4
| @@ -28,7 +28,7 @@ pub struct GammaControlManagerGlobalData { | |||
| 28 | pub trait GammaControlHandler { | 28 | pub trait GammaControlHandler { |
| 29 | fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState; | 29 | fn gamma_control_manager_state(&mut self) -> &mut GammaControlManagerState; |
| 30 | fn get_gamma_size(&mut self, output: &Output) -> Option<u32>; | 30 | fn get_gamma_size(&mut self, output: &Output) -> Option<u32>; |
| 31 | - fn set_gamma(&mut self, output: &Output, ramp: Option<&[u16]>) -> Option<()>; | 31 | + fn set_gamma(&mut self, output: &Output, ramp: Option<Vec<u16>>) -> Option<()>; |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | pub struct GammaControlState { | 34 | pub struct GammaControlState { |
| @@ -162,8 +162,8 @@ where | |||
| 162 | trace!("setting gamma for output {}", output.name()); | 162 | trace!("setting gamma for output {}", output.name()); |
| 163 | 163 | ||
| 164 | // Start with a u16 slice so it's aligned correctly. | 164 | // Start with a u16 slice so it's aligned correctly. |
| 165 | - let mut buf = vec![0u16; data.gamma_size as usize * 3]; | 165 | + let mut gamma = vec![0u16; data.gamma_size as usize * 3]; |
| 166 | - let buf = bytemuck::cast_slice_mut(&mut buf); | 166 | + let buf = bytemuck::cast_slice_mut(&mut gamma); |
| 167 | let mut file = File::from(fd); | 167 | let mut file = File::from(fd); |
| 168 | { | 168 | { |
| 169 | let _span = tracy_client::span!("read gamma from fd"); | 169 | let _span = tracy_client::span!("read gamma from fd"); |
| @@ -195,7 +195,6 @@ where | |||
| 195 | } | 195 | } |
| 196 | } | 196 | } |
| 197 | } | 197 | } |
| 198 | - let gamma = bytemuck::cast_slice(buf); | ||
| 199 | 198 | ||
| 200 | if state.set_gamma(&output, Some(gamma)).is_none() { | 199 | if state.set_gamma(&output, Some(gamma)).is_none() { |
| 201 | resource.failed(); | 200 | resource.failed(); |