drmkit_display/pipeline.rs
1// SPDX-FileCopyrightText: (c) 2026 Joel Winarske
2// SPDX-License-Identifier: MIT
3
4//! The CRTC's colour pipeline: degamma, matrix, gamma.
5//!
6//! Three blob properties in a fixed order. Content arrives encoded, degamma
7//! takes it to linear light, the matrix moves it between primaries -- which is
8//! only meaningful in linear light -- and gamma encodes it for the sink.
9
10use drmkit_core::{AtomicRequest, CoreError, Device, PropertyBlob, PropertyStore};
11
12use crate::capabilities::CrtcCapabilities;
13use crate::curves::{
14 ColorCtm, ColorLut, build_bt2020_to_bt709_ctm, build_hlg_oetf_inverse_lut, build_identity_ctm,
15 build_identity_lut, build_pq_eotf_lut, build_pq_oetf_lut,
16};
17
18/// Which stage a call was about.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum Stage {
22 /// `DEGAMMA_LUT`: encoded to linear.
23 Degamma,
24 /// `CTM`: the 3x3 matrix.
25 Ctm,
26 /// `GAMMA_LUT`: linear to encoded.
27 Gamma,
28}
29
30impl Stage {
31 /// The kernel's property name.
32 #[must_use]
33 pub const fn property(self) -> &'static str {
34 match self {
35 Self::Degamma => "DEGAMMA_LUT",
36 Self::Ctm => "CTM",
37 Self::Gamma => "GAMMA_LUT",
38 }
39 }
40}
41
42/// What can go wrong programming the pipeline.
43#[derive(Debug, thiserror::Error)]
44#[non_exhaustive]
45pub enum PipelineError {
46 /// The CRTC exposes none of the three stages, so there is nothing to
47 /// program. Older and simpler display controllers.
48 #[error("this CRTC has no colour pipeline at all")]
49 NoPipeline,
50
51 /// A LUT was asked for on the matrix stage, which does not take one.
52 #[error("{} is a matrix, not a LUT", .0.property())]
53 NotALut(Stage),
54
55 /// The kernel refused.
56 #[error(transparent)]
57 Core(#[from] CoreError),
58
59 /// The CRTC does not have this stage.
60 ///
61 /// Common and not a defect: plenty of drivers expose one or two of the
62 /// three. A caller checks
63 /// [`capabilities`](CrtcColorPipeline::capabilities) and works with what
64 /// is there.
65 #[error("this CRTC has no {} stage", .0.property())]
66 NoSuchStage(Stage),
67
68 /// A custom LUT was the wrong length.
69 ///
70 /// The driver states exactly how many entries it reads, and a blob of any
71 /// other length is rejected -- which fails the whole atomic commit, not
72 /// just this property.
73 #[error("{} wants {expected} entries, not {given}", .stage.property())]
74 WrongLutSize {
75 /// Which stage.
76 stage: Stage,
77 /// What the driver asks for.
78 expected: u32,
79 /// What was supplied.
80 given: usize,
81 },
82}
83
84/// The colour pipeline of one CRTC.
85///
86/// Holds the blobs it creates and destroys them on drop, and borrows the
87/// device so a blob cannot outlive the descriptor that has to free it.
88#[derive(Debug)]
89pub struct CrtcColorPipeline<'a> {
90 device: &'a Device,
91 crtc_id: u32,
92 capabilities: CrtcCapabilities,
93 /// The property ids, resolved once.
94 ///
95 /// The reference re-reads the CRTC's whole property set on every apply --
96 /// nine properties on amdgpu, seven on vkms, so eight or ten ioctls per
97 /// commit for three numbers that never change.
98 properties: StageProperties,
99 degamma: Option<PropertyBlob<'a>>,
100 ctm: Option<PropertyBlob<'a>>,
101 gamma: Option<PropertyBlob<'a>>,
102}
103
104/// The property ids for the three stages, where they exist.
105#[derive(Debug, Clone, Copy, Default)]
106struct StageProperties {
107 degamma: Option<u32>,
108 ctm: Option<u32>,
109 gamma: Option<u32>,
110}
111
112impl<'a> CrtcColorPipeline<'a> {
113 /// Read a CRTC's colour capabilities and prepare to program them.
114 ///
115 /// A driver exposing only part of the pipeline -- vkms has gamma and
116 /// nothing else -- succeeds; the stages it lacks refuse.
117 ///
118 /// # Errors
119 ///
120 /// - [`PipelineError::NoPipeline`] if the CRTC has none of the three.
121 /// - [`PipelineError::Core`] if its properties cannot be read.
122 pub fn new(device: &'a Device, crtc_id: u32) -> Result<Self, PipelineError> {
123 let mut properties = PropertyStore::new();
124 properties.cache_properties(device, crtc_id, drmkit_core::ObjectType::Crtc)?;
125 let capabilities = CrtcCapabilities::from_properties(&properties, crtc_id);
126 if !capabilities.has_degamma_lut && !capabilities.has_ctm && !capabilities.has_gamma_lut {
127 return Err(PipelineError::NoPipeline);
128 }
129 let id = |stage: Stage| properties.property_id(crtc_id, stage.property()).ok();
130 Ok(Self {
131 device,
132 crtc_id,
133 capabilities,
134 properties: StageProperties {
135 degamma: id(Stage::Degamma),
136 ctm: id(Stage::Ctm),
137 gamma: id(Stage::Gamma),
138 },
139 degamma: None,
140 ctm: None,
141 gamma: None,
142 })
143 }
144
145 /// What this CRTC can do.
146 #[must_use]
147 pub const fn capabilities(&self) -> &CrtcCapabilities {
148 &self.capabilities
149 }
150
151 /// The blob currently staged for a stage, if any.
152 #[must_use]
153 pub fn blob_id(&self, stage: Stage) -> Option<u64> {
154 self.slot(stage).map(PropertyBlob::id)
155 }
156
157 /// Set every stage this CRTC has to identity.
158 ///
159 /// For taking over a display from something that left a curve on it: the
160 /// kernel keeps whatever the last client set, so a compositor that never
161 /// writes these inherits them.
162 ///
163 /// # Errors
164 ///
165 /// [`PipelineError::Core`] if a blob cannot be created.
166 pub fn set_identity(&mut self) -> Result<(), PipelineError> {
167 if self.capabilities.has_degamma_lut {
168 self.set_lut(Stage::Degamma, build_identity_lut)?;
169 }
170 if self.capabilities.has_ctm {
171 self.set_ctm_blob(build_identity_ctm())?;
172 }
173 if self.capabilities.has_gamma_lut {
174 self.set_lut(Stage::Gamma, build_identity_lut)?;
175 }
176 Ok(())
177 }
178
179 /// Degamma: PQ encoded to linear, for HDR10 content.
180 ///
181 /// # Errors
182 ///
183 /// [`PipelineError::NoSuchStage`] if the CRTC has no degamma stage.
184 pub fn set_pq_to_linear(&mut self) -> Result<(), PipelineError> {
185 self.require(Stage::Degamma)?;
186 self.set_lut(Stage::Degamma, build_pq_eotf_lut)
187 }
188
189 /// Degamma: HLG encoded to scene-linear.
190 ///
191 /// # Errors
192 ///
193 /// As [`CrtcColorPipeline::set_pq_to_linear`].
194 pub fn set_hlg_to_linear(&mut self) -> Result<(), PipelineError> {
195 self.require(Stage::Degamma)?;
196 self.set_lut(Stage::Degamma, build_hlg_oetf_inverse_lut)
197 }
198
199 /// Matrix: BT.2020 to BT.709, in linear light.
200 ///
201 /// Needs a degamma stage in front of it that produces linear values.
202 ///
203 /// # Errors
204 ///
205 /// [`PipelineError::NoSuchStage`] if the CRTC has no matrix stage.
206 pub fn set_bt2020_to_bt709(&mut self) -> Result<(), PipelineError> {
207 self.require(Stage::Ctm)?;
208 self.set_ctm_blob(build_bt2020_to_bt709_ctm())
209 }
210
211 /// Gamma: linear to PQ encoded, for HDR10 output.
212 ///
213 /// # Errors
214 ///
215 /// [`PipelineError::NoSuchStage`] if the CRTC has no gamma stage.
216 pub fn set_linear_to_pq(&mut self) -> Result<(), PipelineError> {
217 self.require(Stage::Gamma)?;
218 self.set_lut(Stage::Gamma, build_pq_oetf_lut)
219 }
220
221 /// Stage a LUT of the caller's own.
222 ///
223 /// # Errors
224 ///
225 /// - [`PipelineError::NoSuchStage`] if the CRTC has no such stage,
226 /// [`PipelineError::NotALut`] for the matrix, or
227 /// [`PipelineError::WrongLutSize`] if `lut` is not the length the driver
228 /// asks for.
229 /// - [`PipelineError::Core`] if the blob cannot be created.
230 pub fn set_custom_lut(&mut self, stage: Stage, lut: &[ColorLut]) -> Result<(), PipelineError> {
231 self.require(stage)?;
232 let expected = self.lut_size(stage).ok_or(PipelineError::NotALut(stage))?;
233 if lut.len() != expected as usize {
234 return Err(PipelineError::WrongLutSize {
235 stage,
236 expected,
237 given: lut.len(),
238 });
239 }
240 self.replace(stage, self.device.create_property_blob(lut)?);
241 Ok(())
242 }
243
244 /// Stage a matrix of the caller's own.
245 ///
246 /// # Errors
247 ///
248 /// As [`CrtcColorPipeline::set_bt2020_to_bt709`].
249 pub fn set_custom_ctm(&mut self, ctm: ColorCtm) -> Result<(), PipelineError> {
250 self.require(Stage::Ctm)?;
251 self.set_ctm_blob(ctm)
252 }
253
254 /// Write the staged blob ids into `request`.
255 ///
256 /// A stage that has not been set is left out. The kernel keeps whatever
257 /// the property already holds, which is what a caller programming only
258 /// gamma wants -- and is also why [`set_identity`](Self::set_identity)
259 /// exists for a caller that does not.
260 ///
261 /// # Errors
262 ///
263 /// [`PipelineError::Core`] if a property cannot be added to the request.
264 pub fn apply(&self, request: &mut AtomicRequest) -> Result<(), PipelineError> {
265 for stage in [Stage::Degamma, Stage::Ctm, Stage::Gamma] {
266 let (Some(blob), Some(property)) = (self.blob_id(stage), self.property_id(stage))
267 else {
268 continue;
269 };
270 request.add_property(self.crtc_id, property, blob)?;
271 }
272 Ok(())
273 }
274
275 /// Forget every blob without destroying it.
276 ///
277 /// For a session pause, which revokes the descriptor the blobs were made
278 /// on -- the kernel reclaimed them at that moment, and destroying through
279 /// the replacement descriptor would at best fail and at worst free a
280 /// different blob that happens to have the same id there.
281 pub fn forget_for_session_loss(&mut self) {
282 for slot in [&mut self.degamma, &mut self.ctm, &mut self.gamma] {
283 if let Some(blob) = slot.take() {
284 blob.forget();
285 }
286 }
287 }
288
289 fn require(&self, stage: Stage) -> Result<(), PipelineError> {
290 let present = match stage {
291 Stage::Degamma => self.capabilities.has_degamma_lut,
292 Stage::Ctm => self.capabilities.has_ctm,
293 Stage::Gamma => self.capabilities.has_gamma_lut,
294 };
295 if present {
296 Ok(())
297 } else {
298 Err(PipelineError::NoSuchStage(stage))
299 }
300 }
301
302 /// How many entries the driver reads for a LUT stage.
303 ///
304 /// `None` for the matrix, which does not take one.
305 const fn lut_size(&self, stage: Stage) -> Option<u32> {
306 match stage {
307 Stage::Degamma => Some(self.capabilities.degamma_lut_size),
308 Stage::Gamma => Some(self.capabilities.gamma_lut_size),
309 Stage::Ctm => None,
310 }
311 }
312
313 const fn property_id(&self, stage: Stage) -> Option<u32> {
314 match stage {
315 Stage::Degamma => self.properties.degamma,
316 Stage::Ctm => self.properties.ctm,
317 Stage::Gamma => self.properties.gamma,
318 }
319 }
320
321 const fn slot(&self, stage: Stage) -> Option<&PropertyBlob<'a>> {
322 match stage {
323 Stage::Degamma => self.degamma.as_ref(),
324 Stage::Ctm => self.ctm.as_ref(),
325 Stage::Gamma => self.gamma.as_ref(),
326 }
327 }
328
329 /// Replace a stage's blob, destroying whatever was there.
330 ///
331 /// Safe to do before the commit that switches the property over: the
332 /// kernel holds its own reference to the blob a property currently names,
333 /// so the one being replaced stays alive until nothing points at it.
334 fn replace(&mut self, stage: Stage, blob: PropertyBlob<'a>) {
335 let slot = match stage {
336 Stage::Degamma => &mut self.degamma,
337 Stage::Ctm => &mut self.ctm,
338 Stage::Gamma => &mut self.gamma,
339 };
340 *slot = Some(blob);
341 }
342
343 fn set_lut(&mut self, stage: Stage, build: fn(&mut [ColorLut])) -> Result<(), PipelineError> {
344 let size = self.lut_size(stage).ok_or(PipelineError::NotALut(stage))? as usize;
345 let mut lut = vec![ColorLut::default(); size];
346 build(&mut lut);
347 let blob = self.device.create_property_blob(lut.as_slice())?;
348 self.replace(stage, blob);
349 Ok(())
350 }
351
352 fn set_ctm_blob(&mut self, ctm: ColorCtm) -> Result<(), PipelineError> {
353 let blob = self.device.create_property_blob(&ctm)?;
354 self.replace(Stage::Ctm, blob);
355 Ok(())
356 }
357}