Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/engine/src/media/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,20 @@ impl DecodedSource {
mixed
}

/// Cache cost: the picture, its segmentation mattes, its denoised twin and room for the lazy mix.
/// Cache cost: pixels, decoder tables, mattes, denoised twin and room for its lazy mix.
pub fn bytes(&self) -> usize {
let mattes = self.info.as_ref().and_then(|i| i.mattes.as_ref()).map_or(0, |m| m.bytes());
let mattes = mattes + self.info.as_ref().and_then(|i| i.local_tone.as_ref()).map_or(0, |t| t.map.gains.len() * 4);
self.image.data.len() * 12 + 64 + std::mem::size_of::<SourceInfo>() + mattes + self.denoised.as_ref().map_or(0, |t| t.bytes())
let local = self.info.as_ref().and_then(|i| i.local_tone.as_ref()).map_or(0, |t| t.map.gains.capacity().saturating_mul(4));
let color = self.info.as_ref().and_then(|i| i.camera_color.as_ref()).map_or(0, |c| c.bytes());
self.image
.data
.len()
.saturating_mul(12)
.saturating_add(64 + std::mem::size_of::<SourceInfo>())
.saturating_add(mattes)
.saturating_add(local)
.saturating_add(color)
.saturating_add(self.denoised.as_ref().map_or(0, |t| t.bytes()))
}

/// What to render these pixels against: the decoder's facts, else `header` (the catalog's)
Expand Down
87 changes: 87 additions & 0 deletions crates/engine/src/media/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,93 @@ fn denoise_mix_is_reserved_before_the_source_enters_the_cache() {
assert_eq!(source.bytes(), reserved, "changing Amount replaces the reserved mix");
}

fn camera_color_with_spare_tables() -> lightcraft_pipeline::CameraColor {
let table = || lightcraft_raw::profile::HsvTable {
hue_divisions: 1,
sat_divisions: 2,
val_divisions: 1,
data: {
let mut data = Vec::with_capacity(128);
data.extend([[0.0, 1.0, 1.0]; 2]);
data
},
srgb_value: false,
};
let mut tags = lightcraft_raw::ColorData::default();
tags.profile.hue_sat_map = [Some(table()), Some(table())];
tags.profile.look_table = Some(table());
lightcraft_pipeline::CameraColor { tags, developed_for: lightcraft_color::D65 }
}

#[test]
fn retained_camera_color_counts_spare_table_capacity() {
let mut color = camera_color_with_spare_tables();
let mut points = Vec::with_capacity(64);
points.extend([[0.0, 0.0], [1.0, 1.0]]);
color.tags.profile.tone_curve = Some(lightcraft_raw::profile::ToneCurve { points });
color.tags.kelvin_presets =
lightcraft_raw::kelvin_presets::KelvinPresets::new([(2500.0, [1.0, 1.0, 3.0]), (4500.0, [2.0, 1.0, 2.0]), (6500.0, [3.0, 1.0, 1.0])]);
assert!(color.tags.kelvin_presets.is_some());
let mut source = DecodedSource::new(Arc::new(Rgb32f::new(8, 8)), Some(SourceInfo::default()));
let before = source.bytes();
source.info.as_mut().unwrap().camera_color = Some(Arc::new(color));
let retained = 3 * 128 * std::mem::size_of::<[f32; 3]>()
+ 64 * std::mem::size_of::<[f32; 2]>()
+ 3 * std::mem::size_of::<(f32, [f32; 3])>()
+ std::mem::size_of::<lightcraft_pipeline::CameraColor>();
assert!(source.bytes() >= before + retained, "retained capacity must count even when only two entries are used");
}

#[test]
fn retained_camera_color_and_local_tone_count_gain_capacity() {
use lightcraft_raw::gaintable::{GainTableMap, SourcePlacement};
let map = || {
let mut gains = Vec::with_capacity(128);
gains.extend([1.0; 2]);
GainTableMap {
points_v: 1,
points_h: 1,
points_n: 2,
spacing_v: 1.0,
spacing_h: 1.0,
origin_v: 0.0,
origin_h: 0.0,
weights: [0.0, 0.0, 0.0, 1.0, 0.0],
gamma: 1.0,
gains,
}
};
let mut color = lightcraft_pipeline::CameraColor { tags: lightcraft_raw::ColorData::default(), developed_for: lightcraft_color::D65 };
color.tags.profile.gain_table_map = Some(map());
let mut source = DecodedSource::new(Arc::new(Rgb32f::new(8, 8)), Some(SourceInfo::default()));
let before = source.bytes();
let info = source.info.as_mut().unwrap();
info.camera_color = Some(Arc::new(color));
info.local_tone = Some(Arc::new(lightcraft_pipeline::local_tone::LocalTone {
map: map(),
placement: SourcePlacement { rect: [0.0, 0.0, 1.0, 1.0], orientation: lightcraft_geom::Orientation::Normal },
}));
let retained = 2 * 128 * std::mem::size_of::<f32>() + std::mem::size_of::<lightcraft_pipeline::CameraColor>();
assert_eq!(source.bytes(), before + retained, "both retained gain maps include spare capacity");
}

#[test]
fn retained_camera_color_evicts_the_oldest_thumbnail_source() {
let color = Arc::new(camera_color_with_spare_tables());
let source = || {
let info = SourceInfo { camera_color: Some(color.clone()), ..Default::default() };
DecodedSource::new(Arc::new(Rgb32f::new(1, 1)), Some(info))
};
let mut cache = MediaCache::default();
cache.set_budget(8 << 10);
cache.insert_source(PhotoId(1), SourceLevel::Thumb, source());
assert!(cache.has_source(PhotoId(1), SourceLevel::Thumb), "one camera model fits");
cache.insert_source(PhotoId(2), SourceLevel::Thumb, source());
assert!(!cache.has_source(PhotoId(1), SourceLevel::Thumb), "shared models are charged to each source conservatively");
assert!(cache.has_source(PhotoId(2), SourceLevel::Thumb), "the newest source fits after eviction");
assert!(cache.held() <= cache.budget());
}

#[test]
fn hidden_grading_wheels_render_neutral_without_touching_the_photo() {
let mut s = crate::Session::with_demo();
Expand Down
19 changes: 19 additions & 0 deletions crates/pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ pub struct CameraColor {
pub developed_for: lightcraft_color::Xy,
}

impl CameraColor {
/// Retained camera model and heap tables, including spare vector capacity. Charge shared
/// models conservatively to each cached source so independent decodes cannot evade eviction.
pub fn bytes(&self) -> usize {
let p = &self.tags.profile;
let tables = p
.hue_sat_map
.iter()
.flatten()
.chain(p.look_table.iter())
.fold(0_usize, |n, t| n.saturating_add(t.data.capacity().saturating_mul(std::mem::size_of::<[f32; 3]>())));
std::mem::size_of::<Self>()
.saturating_add(tables)
.saturating_add(p.tone_curve.as_ref().map_or(0, |t| t.points.capacity().saturating_mul(std::mem::size_of::<[f32; 2]>())))
.saturating_add(p.gain_table_map.as_ref().map_or(0, |m| m.gains.capacity().saturating_mul(std::mem::size_of::<f32>())))
.saturating_add(self.tags.kelvin_presets.as_ref().map_or(0, |p| p.bytes()))
}
}

/// Facts about the source the settings are interpreted against.
#[derive(Clone, Debug, PartialEq)]
pub struct SourceInfo {
Expand Down
13 changes: 13 additions & 0 deletions crates/raw/src/kelvin_presets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ fn locus_uv(t: f64) -> (f64, f64) {
}

impl KelvinPresets {
/// Heap storage retained by the calibrated temperature/multiplier points.
pub fn bytes(&self) -> usize {
self.points.capacity().saturating_mul(std::mem::size_of::<(f32, [f32; 3])>())
}

/// `points` as (Kelvin, multipliers), ascending in Kelvin; `None` unless they make a curve: three to
/// [`MAX_POINTS`] of them, positive multipliers, red rising and blue falling from one preset to the next.
pub fn new(points: impl IntoIterator<Item = (f32, [f32; 3])>) -> Option<KelvinPresets> {
Expand Down Expand Up @@ -266,6 +271,14 @@ pub fn as_shot_white(color: &ColorData, wb_multipliers: Option<[f32; 3]>) -> Opt
mod tests {
use super::*;

#[test]
fn retained_points_include_spare_capacity() {
let mut presets = a99m2();
let used = presets.points.len();
presets.points.reserve_exact(128);
assert!(presets.bytes() >= (used + 128) * std::mem::size_of::<(f32, [f32; 3])>());
}

/// The ILCA-99M2's presets (every file of the body carries the same).
fn a99m2() -> KelvinPresets {
let g = |r: f32, b: f32| [r / 1024.0, 1.0, b / 1024.0];
Expand Down