color: non-sRGB preview, per-monitor display ICC, pipeline hardening

Preview now follows the project output colorspace end to end: the
display chain derives its content space from the project's OutputColorSpec
instead of a hardcoded sRGB name, self-managed ICC transforms go through
an XYZ D65 interchange stage (OCIO cie_xyz_d65_interchange) for non-sRGB
targets, and the platform layer declares the content colorspace (gpui
submodule bump). macOS defaults to OS-managed (fixes wide-gamut UI
oversaturation); Windows ACM warns once on non-sRGB targets.

Multi-monitor: the display ICC is looked up per the window's current
screen (macOS display id, Windows per-monitor DC, X11 RandR output
profile) with a throttled poll that invalidates frame caches on moves.

Pipeline precision: 10-bit+ sources fall back to YUV444P16LE + a Rust
matrix conversion when swscale lacks F32 output (no more 8-bit
truncation); BT.709/2020 SDR decodes with BT.1886 gamma 2.4 instead of
the sRGB EOTF; working-space compositing no longer clamps RGB to [0,1]
(alpha still clamped); the output node clamps to the target gamut;
frames without colorimetry metadata convert with BT.709 defaults
(warned once) instead of passing through; scopes read the
output-colorspace signal on both F32 paths.

Also: only emit rerun-if-changed for .env when it exists (a missing file
made every build fully dirty).
This commit is contained in:
2026-08-29 00:24:15 +08:00
parent 879ff8da5b
commit fdb5caabd5
42 changed files with 4423 additions and 146 deletions
+36 -13
View File
@@ -717,7 +717,11 @@ impl<E: AppEngine> OakApp<E> {
cx.background_executor()
.timer(Duration::from_millis(16))
.await;
let _ = cx.update(|_window, app| {
let _ = cx.update(|window, app| {
// Multi-monitor: track which physical display the window
// sits on so the self-managed display transform follows
// window moves (throttled inside `poll_monitor`).
crate::oakui::displaycolor::poll_monitor(window, app);
if let Some(this) = this.upgrade() {
this.update(app, |this, cx| this.tick(cx));
}
@@ -1990,6 +1994,9 @@ impl<E: AppEngine> OakApp<E> {
crate::dialogs::PreferencesEvent::ShortcutsChanged => {
this.rebind_keys(cx);
}
crate::dialogs::PreferencesEvent::DisplayColorChanged => {
this.reapply_display_color(cx);
}
},
)
.detach();
@@ -2002,6 +2009,10 @@ impl<E: AppEngine> OakApp<E> {
if let ModalState::Preferences { content, .. } = &self.modal {
content.update(cx, |content, cx| content.commit_cache_dir(cx));
}
// The committed custom-ICC path may change the effective display
// transform (or the policy, if the profile availability flipped):
// re-declare on every window.
self.reapply_display_color(cx);
}
/// Re-applies the global key bindings and rebuilds the menu bar after a
@@ -2015,6 +2026,20 @@ impl<E: AppEngine> OakApp<E> {
cx.notify();
}
/// Re-evaluates the display color policy and retags every window after
/// the color-management preference changed (the single-mapping rule must
/// hold at runtime, not just at startup). Drops the cached display
/// processors, then re-declares the policy on each open window.
fn reapply_display_color(&mut self, cx: &mut Context<Self>) {
crate::oakui::displaycolor::invalidate();
for handle in cx.windows() {
let _ = cx.update_window(handle, |_root, window, _app| {
crate::oakui::displaycolor::apply_to_window(window);
});
}
cx.notify();
}
/// Opens the action search dialog (Help > Search Actions…, the `/` key).
/// Enter / double-click in the dialog executes the action through the same
/// [`Self::dispatch_action_id`] path the menu clicks take, so the behavior
@@ -3001,20 +3026,15 @@ fn run_with<E: AppEngine>(args: AppArgs) {
if plugin_count > 0 {
println!("[ofx] registered {plugin_count} OFX plugin node type(s)");
}
// Display color management: when the app transforms viewer frames
// through the display ICC itself, the macOS Metal layer must be
// tagged with the display colorspace so ColorSync passes the
// pixels through (otherwise the OS re-corrects them). Read by
// gpui_macos at layer creation, which happens below.
if crate::oakui::displaycolor::is_active() {
// SAFETY: single-threaded startup, before any window exists.
unsafe { std::env::set_var("OAK_MACOS_LAYER_COLORSPACE", "display") };
}
// Display color management: the platform layer must know who maps
// the pixels to the display (the single-mapping rule). Declared
// per-window below, right after the window exists, and re-declared
// whenever the preference changes (see displaycolor::apply_to_window).
// Display bit depth: the wgpu window layer must pick the swapchain
// format before the surface is created, so surface the persisted
// choice the same way the colorspace tag is passed to the platform
// layer above. Read by gpui_wgpu as OAK_DISPLAY_BIT_DEPTH ("8" opts
// into the 8-bit pair; anything else requests 10-bit).
// choice through the process environment. Read by gpui_wgpu as
// OAK_DISPLAY_BIT_DEPTH ("8" opts into the 8-bit pair; anything
// else requests 10-bit).
let bit_depth = oak_render::backend::DisplayBitDepth::from_config_string(
&crate::oakui::real::config_get_string(
oak_render::backend::CONFIG_KEY_DISPLAY_BIT_DEPTH,
@@ -3034,6 +3054,9 @@ fn run_with<E: AppEngine>(args: AppArgs) {
..Default::default()
},
|window, cx| {
// Declare who maps this window's pixels to the display
// before the first frame paints.
crate::oakui::displaycolor::apply_to_window(window);
// The 10-bit display path: hand the window's wgpu device
// to the engine so it can upload RGBA16F textures gpui's
// renderer samples straight into the swapchain (no 8-bit
+124 -3
View File
@@ -76,6 +76,9 @@ pub enum PreferencesEvent {
/// must re-bind the global key map and rebuild the menu bar so the new
/// keys take effect immediately.
ShortcutsChanged,
/// The display color-management mode changed (the host re-evaluates the
/// platform policy and retags the windows immediately — no restart).
DisplayColorChanged,
}
impl gpui::EventEmitter<PreferencesEvent> for PreferencesContent {}
@@ -373,8 +376,8 @@ impl PreferencesContent {
// --- 色彩 Color: display ICC color management -----------------------
// On by default: the viewer frames are transformed through the
// display's ICC profile (system profile, or a custom file below).
// The macOS layer tag is applied at startup, so a mode change takes
// effect after a restart.
// A mode change re-evaluates the platform display policy and
// retags the windows immediately (no restart).
use crate::oakui::displaycolor::{CONFIG_KEY_COLOR_MODE, CONFIG_KEY_CUSTOM_ICC};
let display_icc = cx.new(|cx| {
let mode = config_get_string(CONFIG_KEY_COLOR_MODE);
@@ -395,6 +398,10 @@ impl PreferencesContent {
let enabled = *state == CheckState::Checked;
config_set_string(CONFIG_KEY_COLOR_MODE, if enabled { "icc" } else { "off" });
check.update(cx, |check, cx| check.set_state(*state, cx));
// Drop the cached processors and tell the host to retag the
// windows for the new policy.
crate::oakui::displaycolor::invalidate();
cx.emit(PreferencesEvent::DisplayColorChanged);
}
})
.detach();
@@ -1557,6 +1564,12 @@ pub struct ProjectPropertiesContent<E: crate::oakui::engine::AppEngine> {
/// 1 = alongside the project, 2 = custom path; see
/// [`crate::oakui::engine::AppEngine::project_cache_location`]).
cache_setting: i32,
/// The pipeline working colorspace combo (ACEScg / sRGB legacy).
working_space: Entity<ComboBox>,
/// The delivery output gamut combo (sRGB / Display P3 / BT.2020).
output_gamut: Entity<ComboBox>,
/// The delivery output transfer combo (sRGB / gamma 2.2 / PQ / HLG).
output_transfer: Entity<ComboBox>,
/// The commit error shown under the OCIO row (an invalid config keeps
/// the dialog open, like the C++ accept()).
error: Option<String>,
@@ -1608,12 +1621,54 @@ impl<E: crate::oakui::engine::AppEngine> ProjectPropertiesContent<E> {
});
custom_cache_path.update(cx, |field, cx| field.set_path(custom_path, cx));
// --- Color pipeline: working colorspace + delivery output --------
let working_options = vec![
ComboBoxOption::new(0, i18n::tr("projprops.color.working.acescg")),
ComboBoxOption::new(1, i18n::tr("projprops.color.working.srgb")),
];
let working_space = cx.new(|cx| ComboBox::new(30, working_options, window, cx));
let gamut_options = vec![
ComboBoxOption::new(0, i18n::tr("projprops.color.gamut.srgb")),
ComboBoxOption::new(1, i18n::tr("projprops.color.gamut.p3")),
ComboBoxOption::new(2, i18n::tr("projprops.color.gamut.bt2020")),
];
let output_gamut = cx.new(|cx| ComboBox::new(30, gamut_options, window, cx));
let transfer_options = vec![
ComboBoxOption::new(0, i18n::tr("projprops.color.transfer.srgb")),
ComboBoxOption::new(1, i18n::tr("projprops.color.transfer.gamma22")),
ComboBoxOption::new(2, i18n::tr("projprops.color.transfer.pq")),
ComboBoxOption::new(3, i18n::tr("projprops.color.transfer.hlg")),
];
let output_transfer = cx.new(|cx| ComboBox::new(30, transfer_options, window, cx));
let (working, gamut, transfer) = engine.read(cx).project_color_settings();
working_space.update(cx, |combo, cx| {
combo.set_selected(
Some(oak_common::colormath::WorkingColorSpace::from_setting(&working) as usize),
cx,
)
});
output_gamut.update(cx, |combo, cx| {
combo.set_selected(
Some(oak_common::colormath::OutputGamut::from_setting(&gamut) as usize),
cx,
)
});
output_transfer.update(cx, |combo, cx| {
combo.set_selected(
Some(oak_common::colormath::OutputTransfer::from_setting(&transfer) as usize),
cx,
)
});
Self {
engine,
ocio_config,
cache_location,
custom_cache_path,
cache_setting,
working_space,
output_gamut,
output_transfer,
error: None,
}
}
@@ -1660,7 +1715,8 @@ impl<E: crate::oakui::engine::AppEngine> ProjectPropertiesContent<E> {
/// Applies the edited settings (the C++ `accept()`): validates and
/// applies the OCIO config override first — an invalid config keeps the
/// dialog open — then the disk-cache location. Ok clears the error row.
/// dialog open — then the disk-cache location and the color pipeline
/// settings. Ok clears the error row.
pub fn commit(&mut self, cx: &mut Context<Self>) -> Result<(), String> {
let ocio = self.ocio_config_path(cx).to_string();
self.engine
@@ -1671,10 +1727,57 @@ impl<E: crate::oakui::engine::AppEngine> ProjectPropertiesContent<E> {
self.engine.update(cx, |engine, cx| {
engine.set_project_cache_location(setting, path, cx)
});
// The color pipeline settings: combo index → canonical setting
// string via the colormath enums (single source of truth).
let (working, gamut, transfer) = self.color_settings(cx);
self.engine.update(cx, |engine, cx| {
engine.set_project_color_settings(working, gamut, transfer, cx)
});
self.set_error(None, cx);
Ok(())
}
/// The color pipeline settings currently selected in the combos, as
/// the canonical persisted strings.
fn color_settings(&self, cx: &App) -> (String, String, String) {
use oak_common::colormath::{OutputGamut, OutputTransfer, WorkingColorSpace};
let working = self
.working_space
.read(cx)
.selected()
.map(|i| match i {
1 => WorkingColorSpace::SrgbLegacy,
_ => WorkingColorSpace::AcesCg,
})
.unwrap_or_default();
let gamut = self
.output_gamut
.read(cx)
.selected()
.map(|i| match i {
1 => OutputGamut::DisplayP3,
2 => OutputGamut::Bt2020,
_ => OutputGamut::Srgb,
})
.unwrap_or_default();
let transfer = self
.output_transfer
.read(cx)
.selected()
.map(|i| match i {
1 => OutputTransfer::Gamma22,
2 => OutputTransfer::Pq,
3 => OutputTransfer::Hlg,
_ => OutputTransfer::Srgb,
})
.unwrap_or_default();
(
working.as_setting().to_string(),
gamut.as_setting().to_string(),
transfer.as_setting().to_string(),
)
}
/// The error shown under the OCIO row after a rejected commit.
pub fn set_error(&mut self, msg: Option<String>, cx: &mut Context<Self>) {
self.error = msg;
@@ -1771,6 +1874,21 @@ impl<E: crate::oakui::engine::AppEngine> Render for ProjectPropertiesContent<E>
self.cache_location.clone(),
))
.child(custom_row)
.child(form_row(
&colors,
i18n::tr("projprops.color.working").into(),
self.working_space.clone(),
))
.child(form_row(
&colors,
i18n::tr("projprops.color.gamut").into(),
self.output_gamut.clone(),
))
.child(form_row(
&colors,
i18n::tr("projprops.color.transfer").into(),
self.output_transfer.clone(),
))
}
}
@@ -1827,6 +1945,9 @@ impl PreferencesDialogContent {
}
PreferencesEvent::LanguageChanged => cx.emit(PreferencesEvent::LanguageChanged),
PreferencesEvent::ShortcutsChanged => {}
PreferencesEvent::DisplayColorChanged => {
cx.emit(PreferencesEvent::DisplayColorChanged);
}
},
)
.detach();
+8
View File
@@ -336,6 +336,14 @@ mod tests {
"1/2",
"1/4",
"1/8",
// Standard colorimetry names — identical across languages.
"sRGB / Rec.709",
"Display P3",
"Rec.2020",
"sRGB",
"Gamma 2.2",
"PQ (ST 2084)",
"HLG",
];
let tables = tables().read().unwrap_or_else(|e| e.into_inner());
let en = tables.get("en-US").unwrap();
+5
View File
@@ -60,11 +60,16 @@ pub mod actions;
pub mod app;
pub mod dialogs;
pub mod i18n;
pub mod logging;
pub mod manager;
pub mod oakui;
pub mod panels;
/// The application entry point (called from `main.rs`).
pub fn run() {
// Install the stderr `log` backend before anything else runs, so wgpu
// validation errors and platform warnings are visible (RUST_LOG sets
// the verbosity; default is warn).
logging::init();
app::run();
}
+191
View File
@@ -0,0 +1,191 @@
// Oak Video Editor - Non-Linear Video Editor
// Copyright (C) 2026 Oak Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! A minimal stderr backend for the `log` facade.
//!
//! The gpui / wgpu / oak stack all emit through `log::*`, but nothing in the
//! binary installed a logger, so every validation error, adapter warning and
//! diagnostic was silently dropped (this is what made the viewer-black-screen
//! fault invisible for so long). [`init`] installs a small logger that writes
//! to stderr; `RUST_LOG` selects the verbosity:
//!
//! * unset — `warn` and above;
//! * a level name (`error` / `warn` / `info` / `debug` / `trace`) — that
//! level and above for every target;
//! * comma-separated `target=level` pairs (`wgpu=debug,oak_render=info`) —
//! per-target overrides on top of the global level. A bare level among the
//! pairs sets the global level.
use std::sync::Once;
use log::{Level, LevelFilter, Metadata, Record};
/// One parsed `RUST_LOG` directive set.
struct Filter {
/// The global (default) level.
global: LevelFilter,
/// Per-target overrides (`target` prefix → level).
targets: Vec<(String, LevelFilter)>,
}
impl Filter {
/// Parse the `RUST_LOG` value (`None` / empty → warn-only).
fn parse(spec: Option<&str>) -> Self {
let mut global = LevelFilter::Warn;
let mut targets = Vec::new();
if let Some(spec) = spec.filter(|s| !s.trim().is_empty()) {
for part in spec.split(',') {
let part = part.trim();
if part.is_empty() {
continue;
}
match part.split_once('=') {
Some((target, level)) => {
if let Some(level) = parse_level(level.trim()) {
targets.push((target.trim().to_string(), level));
}
}
None => {
// A bare level sets the global filter.
if let Some(level) = parse_level(part) {
global = level;
}
}
}
}
}
Self { global, targets }
}
/// The effective level for `target` (longest matching prefix wins).
fn level_for(&self, target: &str) -> LevelFilter {
let mut best: Option<(usize, LevelFilter)> = None;
for (prefix, level) in &self.targets {
if target.starts_with(prefix.as_str()) {
let len = prefix.len();
if best.map(|(b, _)| len >= b).unwrap_or(true) {
best = Some((len, *level));
}
}
}
best.map(|(_, level)| level).unwrap_or(self.global)
}
}
/// Map a level name to a [`LevelFilter`].
fn parse_level(text: &str) -> Option<LevelFilter> {
match text.to_ascii_lowercase().as_str() {
"off" => Some(LevelFilter::Off),
"error" => Some(LevelFilter::Error),
"warn" | "warning" => Some(LevelFilter::Warn),
"info" => Some(LevelFilter::Info),
"debug" => Some(LevelFilter::Debug),
"trace" => Some(LevelFilter::Trace),
_ => None,
}
}
/// The logger installed by [`init`].
struct StderrLogger {
filter: Filter,
}
impl log::Log for StderrLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= self.filter.level_for(metadata.target())
}
fn log(&self, record: &Record) {
if !self.enabled(record.metadata()) {
return;
}
let level = match record.level() {
Level::Error => "ERROR",
Level::Warn => "WARN",
Level::Info => "INFO",
Level::Debug => "DEBUG",
Level::Trace => "TRACE",
};
eprintln!(
"[{level} {}] {}",
record.target(),
record.args()
);
}
fn flush(&self) {}
}
static INIT: Once = Once::new();
/// Install the stderr logger once (idempotent). Honors `RUST_LOG`.
pub fn init() {
INIT.call_once(|| {
let filter = Filter::parse(std::env::var("RUST_LOG").ok().as_deref());
let max = filter
.targets
.iter()
.map(|(_, level)| *level)
.max()
.unwrap_or(LevelFilter::Off)
.max(filter.global);
let logger = Box::leak(Box::new(StderrLogger { filter }));
if log::set_logger(logger).is_ok() {
log::set_max_level(max);
}
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_warn() {
let f = Filter::parse(None);
assert_eq!(f.level_for("anything"), LevelFilter::Warn);
assert_eq!(f.level_for(""), LevelFilter::Warn);
}
#[test]
fn bare_level_sets_global() {
let f = Filter::parse(Some("info"));
assert_eq!(f.level_for("wgpu_hal"), LevelFilter::Info);
assert_eq!(f.level_for("oak_render"), LevelFilter::Info);
}
#[test]
fn target_overrides_with_longest_prefix() {
let f = Filter::parse(Some("warn,wgpu=debug,wgpu_hal=trace"));
assert_eq!(f.level_for("wgpu"), LevelFilter::Debug);
assert_eq!(f.level_for("wgpu_core"), LevelFilter::Debug);
assert_eq!(f.level_for("wgpu_hal::vulkan"), LevelFilter::Trace);
assert_eq!(f.level_for("oak_render"), LevelFilter::Warn);
}
#[test]
fn parse_level_aliases() {
assert_eq!(parse_level("warning"), Some(LevelFilter::Warn));
assert_eq!(parse_level("OFF"), Some(LevelFilter::Off));
assert_eq!(parse_level("bogus"), None);
}
#[test]
fn empty_spec_falls_back_to_warn() {
let f = Filter::parse(Some(" "));
assert_eq!(f.level_for("x"), LevelFilter::Warn);
}
}
+387 -52
View File
@@ -14,81 +14,320 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! Display color management: the display's ICC profile applied to viewer
//! frames at present time.
//! Display color management: the display mapping applied to viewer frames
//! at present time, and — critically — WHO performs it.
//!
//! The frame content is treated as display-referred sRGB/Rec.709 (the
//! decode/render pipeline performs no input transfer conversion today);
//! the chain maps it through the display ICC (system profile or a custom
//! file from Preferences) so wide-gamut displays render correctly.
//! ## The single-mapping rule
//!
//! Double-correction discipline: when this module transforms pixels, the
//! OS must not transform them again. macOS: the app sets the CAMetalLayer
//! colorspace to the display profile at startup (see the `OAK_METAL_*`
//! wiring in app.rs) so ColorSync passes our output through. Windows:
//! the SDR desktop applies no per-app transform (and ACM honors the
//! swapchain's declared sRGB space, which is the default). Linux: no
//! compositor-level correction exists to conflict with.
//! The frame content leaves the pipeline in a colorimetrically defined
//! space (the project's output target; sRGB by default). The final mapping
//! to the physical display must happen EXACTLY ONCE — either the OS does it
//! (color-managed compositors / ColorSync / DWM-ACM) or the app does it
//! (an OCIO chain through the display ICC). Both at once doubles the
//! correction; neither leaves wide-gamut displays wrong.
//!
//! [`display_policy`] makes that choice per platform:
//!
//! * **Wayland** — always [`DisplayPolicy::OsManaged`]. A Wayland
//! compositor owns the display mapping (color-management-v1 declares the
//! content as sRGB; compositors without the protocol assume sRGB, which
//! the content is). Self-applying the display ICC here would double-map
//! on every color-managed compositor (KWin 6.2+, GNOME 50+…).
//! * **Windows** — [`DisplayPolicy::OsManaged`] when Windows 11 Auto Color
//! Management is active (it maps the sRGB-declared swapchain to the
//! display), otherwise [`DisplayPolicy::SelfManaged`]: older Windows has
//! no per-app OS mapping, so the app applies the ICM profile itself.
//! * **macOS** — [`DisplayPolicy::OsManaged`] by default: ColorSync maps
//! the content (named via the layer's content colorspace) to the
//! display. An explicit `DisplayColorMode=icc` preference hands the
//! mapping to the app (the layer colorspace tag then makes the OS path
//! a pass-through).
//! * **X11** — the user's preference ([`CONFIG_KEY_COLOR_MODE`]): X11 has
//! no compositor mapping, so self-management is the only way to honor
//! wide-gamut displays there (default self-managed).
//!
//! `OAK_DISPLAY_POLICY=self|os` overrides the platform decision for
//! debugging.
//!
//! ## The content space
//!
//! The chain starts from the project's output spec
//! ([`oak_render::color::pipeline_output_spec`]): sRGB content runs
//! through the named sRGB/Rec.709 space of the active OCIO config, and
//! non-sRGB content (P3/BT.2020 gamuts, PQ/HLG transfers) is converted to
//! CIE XYZ (D65, unit luminance) first and flows through the ICC's
//! connection space (the `cie_xyz_d65_interchange` chain of
//! [`ColorProcessor::create_display_icc_xyz`]). A non-empty
//! [`CONFIG_KEY_CONTENT_SPACE`] overrides the spec with an explicit OCIO
//! colorspace name.
//!
//! When the policy is OS-managed this module transforms nothing and the
//! platform layer declares the content colorspace instead (macOS layer
//! colorspace, Windows `SetColorSpace1`, Wayland color-management-v1),
//! re-declared whenever the project output spec changes.
//!
//! ## Multi-monitor tracking
//!
//! The display ICC depends on WHICH physical monitor the window is on, so
//! the effective key additionally carries the current monitor fingerprint.
//! [`poll_monitor`] runs on the app's tick loop and records the fingerprint
//! (throttled to one probe every 2s); [`note_monitor`] sets it directly
//! (tests, other callers). Fingerprints are opaque strings —
//! `"mac:<CGDirectDisplayID>"`, `"win:<device name>"`, `"x11:<RandR
//! output>"` — and the empty string means "no known monitor", which falls
//! back to the main-display profile. The `SelfManaged` policy keys the
//! transform chain per fingerprint, so dragging the window onto another
//! display re-resolves its ICC without touching the OS-managed paths.
use std::sync::{Arc, LazyLock, Mutex};
use std::time::{Duration, Instant};
use oak_common::colormath::{output_spec_to_xyz_d65, OutputColorSpec, OutputGamut, OutputTransfer};
use oak_common::configstore::ConfigStore;
use oak_render::color::ColorProcessor;
use oak_render::color::{pipeline_output_spec, ColorProcessor};
/// Config key: the display color management mode ("icc" / "off").
/// Config key: the display color management mode ("icc" / "off"). The
/// preference only applies where the platform policy allows self-management
/// (macOS, X11, non-ACM Windows).
pub const CONFIG_KEY_COLOR_MODE: &str = "DisplayColorMode";
/// Config key: a custom ICC profile path (empty = the system display
/// profile).
pub const CONFIG_KEY_CUSTOM_ICC: &str = "DisplayColorCustomIcc";
/// Config key: the content colorspace the chain starts from (an OCIO
/// colorspace name of the active config).
/// Config key: the content colorspace the display chain starts from (an
/// OCIO colorspace name of the active config). Empty (the default) =
/// follow the project's output spec.
pub const CONFIG_KEY_CONTENT_SPACE: &str = "DisplayColorContentSpace";
/// The default content space (OCIO 2.2 builtin config name for
/// gamma-encoded Rec.709/sRGB display-referred content).
const DEFAULT_CONTENT_SPACE: &str = "sRGB Encoded Rec.709 (sRGB)";
/// The cached processor pair (F32 RGBA and packed BGRA8 variants of the
/// same chain), keyed by (mode, icc path, content space).
/// Who performs the final mapping to the physical display.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DisplayPolicy {
/// The app applies the display transform itself (the display ICC chain);
/// the platform layer must be tagged so the OS passes pixels through.
SelfManaged,
/// The OS maps the (sRGB-declared) content to the display; the app must
/// not apply any display transform.
OsManaged,
}
/// The cached processor set for one (policy, mode, icc, content, monitor)
/// key: the sRGB-named chain for F32 samples, the XYZ (PCS) chain for
/// non-sRGB project output, and the BGRA8 variant of the sRGB chain — the
/// viewer's 8-bit slot always runs the sRGB chain, a degradation for
/// non-sRGB specs whose encoding 8-bit cannot honor (PQ/HLG highlights
/// clamp).
struct State {
key: (String, String, String),
key: (DisplayPolicy, String, String, String, String),
f32: Option<Arc<ColorProcessor>>,
xyz: Option<Arc<ColorProcessor>>,
bgra: Option<Arc<ColorProcessor>>,
}
static STATE: LazyLock<Mutex<Option<State>>> = LazyLock::new(|| Mutex::new(None));
/// Bumped every time the effective key changes (mode / ICC path /
/// content space): the engine's frame caches compare against it and drop
/// images produced with a stale transform.
/// Bumped every time the effective key changes (policy / mode / ICC path /
/// content space / monitor): the engine's frame caches compare against it
/// and drop images produced with a stale transform.
static GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// The current monitor fingerprint (see [`poll_monitor`]): `None` (the
/// default) means the window's monitor is unknown and the main-display
/// profile is used. Updated only on a successful probe — a failed probe
/// keeps the last known monitor rather than degrading mid-session.
static CURRENT_MONITOR: Mutex<Option<String>> = Mutex::new(None);
/// When [`poll_monitor`] last probed the monitor; the first call always
/// probes immediately, later ones at most every [`MONITOR_PROBE_INTERVAL`].
static LAST_MONITOR_PROBE: Mutex<Option<Instant>> = Mutex::new(None);
/// Minimum time between two monitor probes in [`poll_monitor`].
const MONITOR_PROBE_INTERVAL: Duration = Duration::from_secs(2);
/// The current transform generation (see [`GENERATION`]).
pub fn generation() -> u64 {
GENERATION.load(std::sync::atomic::Ordering::Relaxed)
}
/// The active (mode, icc, content-space) key from the config.
fn current_key() -> (String, String, String) {
let store = ConfigStore::instance();
let mode = store
.get(None, CONFIG_KEY_COLOR_MODE)
.unwrap_or_else(|_| "icc".to_string());
let custom = store
.get(None, CONFIG_KEY_CUSTOM_ICC)
.unwrap_or_default();
let space = store
.get(None, CONFIG_KEY_CONTENT_SPACE)
.unwrap_or_else(|_| DEFAULT_CONTENT_SPACE.to_string());
(mode, custom, space)
/// True when the session runs on a Wayland compositor (the same probe the
/// platform backend uses, `gpui::guess_compositor`).
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
fn on_wayland() -> bool {
gpui::guess_compositor() == "Wayland"
}
/// Drop the cached processors (call after a preferences change).
/// The platform policy decision for this session (see the module docs).
/// Stable for the process lifetime except the macOS/X11 preference, which
/// the config key governs at runtime.
pub fn display_policy() -> DisplayPolicy {
if let Ok(override_mode) = std::env::var("OAK_DISPLAY_POLICY") {
match override_mode.to_ascii_lowercase().as_str() {
"os" => return DisplayPolicy::OsManaged,
"self" => return DisplayPolicy::SelfManaged,
other => {
log::warn!(
"OAK_DISPLAY_POLICY={other:?} ignored (expected \"os\" or \"self\")"
);
}
}
}
// Wayland: the compositor owns the display mapping; declaring sRGB
// content (color-management-v1) is the only correct behavior.
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
if on_wayland() {
return DisplayPolicy::OsManaged;
}
// Windows: Auto Color Management (Windows 11) maps the sRGB-declared
// swapchain for us — self-applying the ICM profile would double-map.
// Without ACM (Windows 10 / ACM off) no per-app OS mapping exists, so
// the app applies the profile itself. ACM's swapchain mapping is SDR
// sRGB; a non-sRGB project output can't be honored through it — warn
// once so the degradation is visible in the log.
#[cfg(target_os = "windows")]
if oak_common::displayicc::windows_acm_active() {
if pipeline_output_spec() != OutputColorSpec::default() {
static WARNED: std::sync::OnceLock<()> = std::sync::OnceLock::new();
WARNED.get_or_init(|| {
log::warn!(
"Windows ACM: project output {:?} is not sRGB; ACM maps the sRGB swapchain only, \
non-sRGB previews will not match the project colorspace",
pipeline_output_spec()
);
});
}
return DisplayPolicy::OsManaged;
}
// macOS: ColorSync owns the display mapping by default; only an
// explicit "icc" preference hands it to the app (the layer colorspace
// tag then turns the OS path into a pass-through). X11 has no OS
// mapping, so self-management is the only way to honor wide-gamut
// displays there (the "off" preference opts back into OS-management).
#[cfg(target_os = "macos")]
match ConfigStore::instance().get(None, CONFIG_KEY_COLOR_MODE).as_deref() {
Ok("icc") => DisplayPolicy::SelfManaged,
_ => DisplayPolicy::OsManaged,
}
#[cfg(not(target_os = "macos"))]
match configured_mode().as_str() {
"off" => DisplayPolicy::OsManaged,
_ => DisplayPolicy::SelfManaged,
}
}
/// The persisted mode preference ("icc" = self-manage, "off" = OS-managed).
fn configured_mode() -> String {
ConfigStore::instance()
.get(None, CONFIG_KEY_COLOR_MODE)
.unwrap_or_else(|_| "icc".to_string())
}
/// Drop the cached processors (call after a preference change).
pub fn invalidate() {
*STATE.lock().unwrap_or_else(|e| e.into_inner()) = None;
}
/// The cached state, (re)built when the config key changed.
/// Record the window's current monitor fingerprint (see [`poll_monitor`]).
/// `None` clears the tracking back to the main-display lookup.
pub fn note_monitor(id: Option<String>) {
*CURRENT_MONITOR.lock().unwrap_or_else(|e| e.into_inner()) = id;
}
/// Track which physical monitor the window sits on, so the self-managed
/// display transform follows window moves between displays.
///
/// Runs on the app's tick loop. No-op unless the policy is [`DisplayPolicy::SelfManaged`]
/// (an OS-managed session never consults the display ICC). Probes are
/// throttled to one per [`MONITOR_PROBE_INTERVAL`] — the first call probes
/// immediately — and only a successful probe updates the tracked monitor:
/// a transient failure keeps the last known one rather than falling back
/// mid-session.
pub fn poll_monitor(window: &gpui::Window, cx: &gpui::App) {
if display_policy() != DisplayPolicy::SelfManaged {
return;
}
{
let mut last = LAST_MONITOR_PROBE.lock().unwrap_or_else(|e| e.into_inner());
if let Some(stamp) = *last {
if stamp.elapsed() < MONITOR_PROBE_INTERVAL {
return;
}
}
*last = Some(Instant::now());
}
if let Some(fingerprint) = current_monitor_fingerprint(window, cx) {
note_monitor(Some(fingerprint));
}
}
/// The current monitor of `window` as a fingerprint string (see the module
/// docs), or `None` when the platform cannot resolve one. The fingerprint
/// is derived from the window's platform display: macOS and Windows use the
/// gpui display id of the window's current screen (`window.display`), Linux
/// resolves the RandR output whose geometry covers the window center
/// (window bounds are logical pixels there; multiplied by the scale factor
/// they become device pixels).
fn current_monitor_fingerprint(window: &gpui::Window, cx: &gpui::App) -> Option<String> {
#[cfg(target_os = "macos")]
{
let id = u64::from(window.display(cx)?.id());
return Some(format!("mac:{id}"));
}
#[cfg(target_os = "windows")]
{
let id = u64::from(window.display(cx)?.id());
return oak_common::displayicc::windows_monitor_fingerprint(id);
}
#[cfg(target_os = "linux")]
{
let _ = cx;
let center = window.bounds().center();
let x = f64::from(center.x) * f64::from(window.scale_factor());
let y = f64::from(center.y) * f64::from(window.scale_factor());
return oak_common::displayicc::x11_monitor_fingerprint_at(x, y);
}
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "linux")))]
{
let _ = (window, cx);
None
}
}
/// The active (policy, mode, icc, content, monitor) key.
fn current_key() -> (DisplayPolicy, String, String, String, String) {
let policy = display_policy();
let custom = ConfigStore::instance()
.get(None, CONFIG_KEY_CUSTOM_ICC)
.unwrap_or_default();
// The content space: a non-empty override (an explicit OCIO colorspace
// name) wins; otherwise the chain follows the project's output spec —
// its (gamut, transfer) pair rekeys (and so rebuilds) the chain, so a
// project settings change flows through the same GENERATION machinery
// as a preference change.
let space = ConfigStore::instance()
.get(None, CONFIG_KEY_CONTENT_SPACE)
.unwrap_or_default();
let space = if space.is_empty() {
let spec = pipeline_output_spec();
format!("{}:{}", spec.gamut.as_setting(), spec.transfer.as_setting())
} else {
format!("override:{space}")
};
// The monitor fingerprint the window currently sits on; empty = unknown
// (falls back to the main display).
let monitor = CURRENT_MONITOR
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
.unwrap_or_default();
(policy, configured_mode(), custom, space, monitor)
}
/// The cached state, (re)built when the key changed.
fn current() -> Option<State> {
let key = current_key();
let mut guard = STATE.lock().unwrap_or_else(|e| e.into_inner());
@@ -105,33 +344,64 @@ fn current() -> Option<State> {
if guard.is_some() {
GENERATION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
let (mode, icc_path, space) = &key;
if mode != "icc" {
let (policy, mode, icc_path, space, monitor) = &key;
// OS-managed: the compositor/ColorSync/DWM performs the display
// mapping; applying anything here would be a second correction.
if *policy == DisplayPolicy::OsManaged || mode != "icc" {
let state = State {
key,
f32: None,
xyz: None,
bgra: None,
};
let out = clone_state(&state);
*guard = Some(state);
return out;
}
// The custom override wins; empty = the platform's display profile.
// Self-managed: content (in `space`) through the display ICC. The
// custom override wins; empty = the current monitor's profile (a known
// fingerprint resolves per-monitor, anything else the main display).
let icc = if icc_path.is_empty() {
oak_common::displayicc::system_display_icc()
monitor_icc_path(monitor)
} else {
Some(icc_path.clone())
};
let (f32p, bgrap) = match icc {
Some(path) => (
ColorProcessor::create_display_icc(space, &path).map(Arc::new),
ColorProcessor::create_display_icc_bgra8(space, &path).map(Arc::new),
),
None => (None, None),
let (f32p, xyzp, bgrap) = match icc {
// Explicit content-space override: the whole chain starts from
// that OCIO colorspace name (both slots).
Some(path) if space.starts_with("override:") => {
let name = space.trim_start_matches("override:");
(
ColorProcessor::create_display_icc(name, &path).map(Arc::new),
None,
ColorProcessor::create_display_icc_bgra8(name, &path).map(Arc::new),
)
}
// Project output spec: sRGB content through the named sRGB space;
// non-sRGB content (P3/BT.2020 gamuts, PQ/HLG transfers) has no
// named space in the builtin OCIO configs, so the F32 chain
// converts it to CIE XYZ (PCS) first and runs the ICC half — the
// sRGB chain stays as the degradation fallback and for the 8-bit
// slot (whose encoding cannot honor non-sRGB specs).
Some(path) => {
let xyz = if pipeline_output_spec() != OutputColorSpec::default() {
ColorProcessor::create_display_icc_xyz(&path).map(Arc::new)
} else {
None
};
(
ColorProcessor::create_display_icc(DEFAULT_CONTENT_SPACE, &path).map(Arc::new),
xyz,
ColorProcessor::create_display_icc_bgra8(DEFAULT_CONTENT_SPACE, &path)
.map(Arc::new),
)
}
None => (None, None, None),
};
let state = State {
key,
f32: f32p,
xyz: xyzp,
bgra: bgrap,
};
let out = clone_state(&state);
@@ -139,18 +409,76 @@ fn current() -> Option<State> {
out
}
/// The display ICC path for the current monitor fingerprint: a known
/// fingerprint resolves per-monitor (with a fallback to the main display
/// inside `system_display_icc_for`); an unknown one (empty or malformed)
/// falls back to the main-display profile — the pre-multi-monitor behavior.
fn monitor_icc_path(monitor: &str) -> Option<String> {
match oak_common::displayicc::monitor_ref_from_fingerprint(monitor) {
Some(monitor) => oak_common::displayicc::system_display_icc_for(&monitor),
None => oak_common::displayicc::system_display_icc(),
}
}
fn clone_state(state: &State) -> Option<State> {
Some(State {
key: state.key.clone(),
f32: state.f32.clone(),
xyz: state.xyz.clone(),
bgra: state.bgra.clone(),
})
}
/// Whether display color management is active (a valid ICC processor
/// exists). When false the OS owns the output mapping.
/// Whether the app is self-applying a display transform right now (a valid
/// ICC processor under the SelfManaged policy). When false the OS owns the
/// output mapping and the platform layer must declare the content space.
pub fn is_active() -> bool {
current().map(|s| s.f32.is_some() || s.bgra.is_some()).unwrap_or(false)
if display_policy() != DisplayPolicy::SelfManaged {
return false;
}
current()
.map(|s| s.f32.is_some() || s.xyz.is_some() || s.bgra.is_some())
.unwrap_or(false)
}
/// Declare the current policy to the window's platform layer (macOS: the
/// Metal layer colorspace tag; other platforms handle it at the surface
/// level or ignore it). Call once per window at creation, after any
/// preference change that flips [`is_active`], and after the project
/// output spec changes (which rekeys the content-colorspace declaration).
pub fn apply_to_window(window: &mut gpui::Window) {
let active = is_active();
let mode = if active {
gpui::LayerColorManagement::SelfManaged
} else {
gpui::LayerColorManagement::OsManaged
};
window.set_layer_color_management(mode);
// OS-managed: name the content colorspace so the OS maps from it
// (ColorSync, DWM-ACM, color-management-v1). The self-managed path is
// tagged pass-through, so the declaration is meaningless there.
if !active {
window.set_content_colorspace(content_colorspace());
}
}
/// The gpui content-colorspace declaration for the current project output
/// spec (only meaningful while the OS performs the display mapping).
fn content_colorspace() -> gpui::WindowContentColorspace {
let spec = pipeline_output_spec();
gpui::WindowContentColorspace {
primaries: match spec.gamut {
OutputGamut::Srgb => gpui::ContentPrimaries::Srgb,
OutputGamut::DisplayP3 => gpui::ContentPrimaries::DisplayP3,
OutputGamut::Bt2020 => gpui::ContentPrimaries::Bt2020,
},
transfer: match spec.transfer {
OutputTransfer::Srgb => gpui::ContentTransfer::Srgb,
OutputTransfer::Gamma22 => gpui::ContentTransfer::Gamma22,
OutputTransfer::Pq => gpui::ContentTransfer::Pq,
OutputTransfer::Hlg => gpui::ContentTransfer::Hlg,
},
}
}
/// Apply the display transform to an F32 RGBA buffer in place (no-op
@@ -159,7 +487,14 @@ pub fn apply_f32_rgba(samples: &mut [f32], pixels: i64) {
let Some(state) = current() else {
return;
};
if let Some(processor) = &state.f32 {
if let Some(xyz) = &state.xyz {
// Non-sRGB project output: the samples are in the output spec's
// encoded form — linearize the transfer and gamut-map to CIE XYZ
// (D65, unit luminance) first, then let the ICC chain map the
// connection space to the display.
output_spec_to_xyz_d65(samples, pipeline_output_spec());
let _ = xyz.convert_f32_rgba(samples, pixels);
} else if let Some(processor) = &state.f32 {
let _ = processor.convert_f32_rgba(samples, pixels);
}
}
+27
View File
@@ -925,6 +925,33 @@ pub trait AppEngine:
let _ = (setting, custom_path, cx);
}
/// The project's color pipeline settings:
/// `(working colorspace, output gamut, output transfer)` as the
/// persisted setting strings (see `oak_common::colormath`). The
/// working colorspace is the pipeline's scene space (ACEScg by
/// default, not hard-coded sRGB); the output pair is the delivery
/// target for export and presentation.
fn project_color_settings(&self) -> (String, String, String) {
(
oak_common::colormath::WorkingColorSpace::default().as_setting().to_string(),
oak_common::colormath::OutputGamut::default().as_setting().to_string(),
oak_common::colormath::OutputTransfer::default().as_setting().to_string(),
)
}
/// Applies the project color pipeline settings: stores them in the
/// project properties and invalidates every cached frame (they were
/// rendered under the old pipeline).
fn set_project_color_settings(
&mut self,
working: String,
gamut: String,
transfer: String,
cx: &mut Context<Self>,
) {
let _ = (working, gamut, transfer, cx);
}
// -------------------------------------------------------------------
// Sequence management (the C++ File > New Sequence / New Folder and
// the project-explorer sequence context menu): creating, querying and
+42 -12
View File
@@ -143,17 +143,28 @@ pub(crate) fn bgra_bytes_to_render_image(
/// 1.0), so every displayable code survives the round trip. Samples must
/// hold exactly `width * height * 4` values (tightly packed rows); values
/// clamp to `0.0..=1.0` before packing.
pub(crate) fn f32_rgba_to_16f_bytes(width: u32, height: u32, samples: &[f32]) -> Option<Vec<u8>> {
if samples.len() != (width * height * 4) as usize {
return None;
///
/// Returns `(bytes, bytes_per_row)`: rows are padded to wgpu's
/// `COPY_BYTES_PER_ROW_ALIGNMENT` (256) because `write_texture` requires
/// an explicit, aligned row pitch for multi-row copies — a `None` pitch
/// fails validation and the texture stays black.
pub(crate) fn f32_rgba_to_16f_bytes(width: u32, height: u32, samples: &[f32]) -> Option<(Vec<u8>, u32)> {
if samples.len() != (width * height * 4) as usize {
return None;
}
let row_samples = (width * 4) as usize;
let unpadded_row_bytes = row_samples * 2;
let row_bytes = unpadded_row_bytes.next_multiple_of(256);
let mut bytes = vec![0u8; row_bytes * height as usize];
for (row, chunk) in samples.chunks_exact(row_samples).enumerate() {
let dst = &mut bytes[row * row_bytes..row * row_bytes + unpadded_row_bytes];
for (i, &v) in chunk.iter().enumerate() {
let h = half::f16::from_f32(v.clamp(0.0, 1.0));
dst[i * 2..i * 2 + 2].copy_from_slice(&h.to_bits().to_le_bytes());
}
}
Some((bytes, row_bytes as u32))
}
let mut bytes = Vec::with_capacity(samples.len() * 2);
for &v in samples {
let h = half::f16::from_f32(v.clamp(0.0, 1.0));
bytes.extend_from_slice(&h.to_bits().to_le_bytes());
}
Some(bytes)
}
#[cfg(test)]
mod tests {
@@ -178,8 +189,11 @@ mod tests {
// same code 255.
let (v1, v2) = (1022.0f32 / 1023.0, 1.0f32);
let samples = [v1, 0.0, 0.0, 1.0, v2, 0.0, 0.0, 1.0];
let bytes = f32_rgba_to_16f_bytes(2, 1, &samples).expect("packed 16f bytes");
assert_eq!(bytes.len(), 2 * 1 * 4 * 2, "two pixels, four f16 channels");
let (bytes, row_bytes) = f32_rgba_to_16f_bytes(2, 1, &samples).expect("packed 16f bytes");
// Two pixels = 16 content bytes; the row pads to the 256-byte
// copy alignment.
assert_eq!(bytes.len(), 256, "one padded row");
assert_eq!(row_bytes, 256);
let code_of = |value: f32| {
let h = half::f16::from_f32(value);
((h.to_f32() * 1023.0).round()) as u32
@@ -201,4 +215,20 @@ mod tests {
"the same two values collapse in 8-bit — proving the 16f path carries the resolution"
);
}
#[test]
fn f32_to_16f_packs_multi_row_with_padding() {
// A 3x2 frame: unpadded row is 3*4*2 = 24 bytes, padded to 256.
let samples = vec![0.5f32; 3 * 2 * 4];
let (bytes, row_bytes) = f32_rgba_to_16f_bytes(3, 2, &samples).expect("packed");
assert_eq!(row_bytes, 256);
assert_eq!(bytes.len(), 512);
// First pixel of each row carries the 0.5 code; padding is zero.
let half_bits = half::f16::from_f32(0.5).to_bits();
for row in 0..2 {
let off = row * 256;
assert_eq!(&bytes[off..off + 2], &half_bits.to_le_bytes());
assert_eq!(&bytes[off + 24..off + 28], &[0, 0, 0, 0], "padding zeroed");
}
}
}
+5 -2
View File
@@ -69,7 +69,8 @@ pub fn upload_rgba16f(
let ctx = GPU_CONTEXT.lock().ok()?;
ctx.as_ref().map(|(d, q)| (d.clone(), q.clone()))?
};
let bytes = super::frames::f32_rgba_to_16f_bytes(width, height, samples)?;
let (bytes, bytes_per_row) =
super::frames::f32_rgba_to_16f_bytes(width, height, samples)?;
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("oak_display_rgba16f"),
size: wgpu::Extent3d {
@@ -94,7 +95,9 @@ pub fn upload_rgba16f(
&bytes,
wgpu::TexelCopyBufferLayout {
offset: 0,
bytes_per_row: None,
// Multi-row copies require an explicit, 256-aligned pitch; a
// `None` pitch fails validation and the texture stays black.
bytes_per_row: Some(bytes_per_row),
rows_per_image: None,
},
wgpu::Extent3d {
+119 -1
View File
@@ -629,6 +629,7 @@ fn rendered_to_owned_image(rendered: &super::renderops::RenderedFrame) -> Option
// image below is only the CPU fallback (scope / eyedropper /
// cached fills without a GPU).
let mut samples = repack_f32_row_bytes(meta.width, meta.height, meta.linesize, data)?;
apply_output_node_f32(&mut samples);
super::displaycolor::apply_f32_rgba(&mut samples, (w * h) as i64);
let image = f32_rgba_to_bgra_image(w, h, &samples);
super::gpu::register_display_frame(image.id.0, w, h, &samples);
@@ -637,7 +638,11 @@ fn rendered_to_owned_image(rendered: &super::renderops::RenderedFrame) -> Option
bgra_bytes_to_render_image(w, h, data).map(Arc::new)
}
super::renderops::RenderedFrame::CpuF32 { .. } => {
let (w, h, samples) = read_f32_frame(rendered)?;
let (w, h, mut samples) = read_f32_frame(rendered)?;
// Output node + display transform, same as the shm F32 path, so
// cached fills match the on-screen picture regardless of backend.
apply_output_node_f32(&mut samples);
super::displaycolor::apply_f32_rgba(&mut samples, (w * h) as i64);
let image = f32_rgba_to_bgra_image(w, h, &samples);
super::gpu::register_display_frame(image.id.0, w, h, &samples);
Some(Arc::new(image))
@@ -645,6 +650,16 @@ fn rendered_to_owned_image(rendered: &super::renderops::RenderedFrame) -> Option
}
}
/// The app-side output node for F32 frames (working colorspace → the
/// project's output colorspace); pass-through in the legacy working space.
fn apply_output_node_f32(samples: &mut [f32]) {
oak_common::colormath::working_to_display_target(
samples,
oak_render::color::pipeline_working_space(),
oak_render::color::pipeline_output_spec(),
);
}
/// Repack one F32 RGBA shm slot (rows padded to `linesize`) into tightly
/// packed samples. The in-process variant is handled by [`read_f32_frame`].
fn repack_f32_row_bytes(width: i32, height: i32, linesize: i32, data: &[u8]) -> Option<Vec<f32>> {
@@ -2788,6 +2803,16 @@ impl RealEngine {
};
self.project = Some(project.clone());
self.storage = Some(AuxHandle(graphops::storage_bind(&project)));
// The project's color pipeline properties (working colorspace +
// delivery target) drive the app-side decode/output transforms;
// the render workers pick them up at graph-load time.
{
let guard = graphops::lock(&project);
oak_render::color::set_pipeline_color_settings(
guard.working_color_space(),
guard.output_color_spec(),
);
}
// Footage loaded from a file may lack stream metadata (C++ projects
// have no `<streams>` segment; older Rust saves predate the probe
@@ -2813,6 +2838,9 @@ impl RealEngine {
// The project's stored OCIO override (if any) drives the display
// color pipeline from here on.
Self::apply_project_color_config(Some(&project));
// The project's colorspace settings may differ from the previous
// project's: re-declare the display policy on every open window.
self.reapply_display_policy_to_windows(cx);
// 全局代理开关开启时,工程里未就绪素材的代理在后台自动生成
// (打开长素材工程不等待:生成走任务线程)。
@@ -4897,6 +4925,79 @@ impl AppEngine for RealEngine {
cx.notify();
}
fn project_color_settings(&self) -> (String, String, String) {
let Some(project) = self.project_ref() else {
return (
oak_common::colormath::WorkingColorSpace::default().as_setting().to_string(),
oak_common::colormath::OutputGamut::default().as_setting().to_string(),
oak_common::colormath::OutputTransfer::default().as_setting().to_string(),
);
};
let guard = graphops::lock(project);
let get = |key: &str, default: &str| {
guard
.settings
.get(key)
.map(String::as_str)
.unwrap_or(default)
.to_string()
};
(
get(
oak_node::project::SETTING_WORKING_COLOR_SPACE,
oak_common::colormath::WorkingColorSpace::default().as_setting(),
),
get(
oak_node::project::SETTING_OUTPUT_GAMUT,
oak_common::colormath::OutputGamut::default().as_setting(),
),
get(
oak_node::project::SETTING_OUTPUT_TRANSFER,
oak_common::colormath::OutputTransfer::default().as_setting(),
),
)
}
fn set_project_color_settings(
&mut self,
working: String,
gamut: String,
transfer: String,
cx: &mut Context<Self>,
) {
let Some(project) = self.project.clone() else {
return;
};
// Normalize through the parsers so only canonical values persist.
let working = oak_common::colormath::WorkingColorSpace::from_setting(&working);
let spec = oak_common::colormath::OutputColorSpec::from_settings(&gamut, &transfer);
{
let mut guard = graphops::lock(&project);
guard.settings.insert(
oak_node::project::SETTING_WORKING_COLOR_SPACE.to_string(),
working.as_setting().to_string(),
);
guard.settings.insert(
oak_node::project::SETTING_OUTPUT_GAMUT.to_string(),
spec.gamut.as_setting().to_string(),
);
guard.settings.insert(
oak_node::project::SETTING_OUTPUT_TRANSFER.to_string(),
spec.transfer.as_setting().to_string(),
);
guard.modified = true;
}
// The app-side transforms read the process global; the workers pick
// the new settings up with the next graph upload.
oak_render::color::set_pipeline_color_settings(working, spec);
// The pipeline colorspace changed: every cached frame (CPU image,
// GPU texture, preview slot) was rendered under the old space.
super::displaycolor::invalidate();
self.invalidate_rendered_frames();
self.reapply_display_policy_to_windows(cx);
cx.notify();
}
fn entry_is_sequence(&self, id: u64) -> bool {
let Some(project) = self.project_ref() else {
return false;
@@ -5649,6 +5750,23 @@ impl ProjectFormat {
}
impl RealEngine {
/// Re-declares the display color policy (and the content colorspace) on
/// every open window after the project's color pipeline settings changed.
/// Runs deferred: [`Self::set_project_color_settings`] is committed from
/// inside the settings dialog's window update, where a direct
/// `update_window` would fail; `defer` re-enters the app after that
/// update completes.
fn reapply_display_policy_to_windows(&self, cx: &mut Context<Self>) {
let windows = cx.windows();
cx.defer(move |app| {
for handle in windows {
let _ = app.update_window(handle, |_root, window, _app| {
crate::oakui::displaycolor::apply_to_window(window);
});
}
});
}
/// Auto-creates a sequence for a footage drop onto an empty timeline (see
/// `drop_footage`): the format mirrors the footage's first video stream;
/// pure-audio footage uses the default sequence format. Returns the new
+39 -10
View File
@@ -464,16 +464,23 @@ impl RenderedFrame {
let (w, h) = (meta.width.max(0) as u32, meta.height.max(0) as u32);
let pixels = f.shm.slot_bytes(f.slot);
let data = pixels.get(..meta.data_size.max(0) as usize)?;
if meta.format == PIXEL_FORMAT_F32 {
// M15 S3: the worker rendered F32 (the 10-bit display
// path) — repack the padded rows, transform and hand the
// samples back for the RGBA16F texture.
let mut samples = repack_f32_rows(meta.width, meta.height, meta.linesize, data)?;
let scope = analyze_f32_rgba(w, h, &samples);
super::displaycolor::apply_f32_rgba(&mut samples, (w * h) as i64);
let image = f32_rgba_to_bgra_image(w, h, &samples);
Some((image, scope, Some(samples)))
} else {
if meta.format == PIXEL_FORMAT_F32 {
// M15 S3: the worker rendered F32 (the 10-bit display
// path) — repack the padded rows, transform and hand the
// samples back for the RGBA16F texture.
let mut samples = repack_f32_rows(meta.width, meta.height, meta.linesize, data)?;
// Output node: working space → the project's output
// colorspace. The scopes below read the output-colorspace
// signal (same convention as the BGRA8 slot); the display
// policy then decides whether the display ICC is applied on
// top (self-managed) or the OS maps the declared content
// colorspace (OS-managed).
apply_output_node_f32(&mut samples);
let scope = analyze_f32_rgba(w, h, &samples);
super::displaycolor::apply_f32_rgba(&mut samples, (w * h) as i64);
let image = f32_rgba_to_bgra_image(w, h, &samples);
Some((image, scope, Some(samples)))
} else {
// BGRA8 slot: the worker already downconverted — wrap the
// bytes directly (no 10-bit path available for them).
let scope = analyze_bgra8(w, h, data);
@@ -492,6 +499,9 @@ impl RenderedFrame {
} => {
let (w, h) = ((*width).max(0) as u32, (*height).max(0) as u32);
let mut samples = repack_f32_rows(*width, *height, *linesize, data)?;
// Output node first, as in the shm path: the scopes read the
// output-colorspace signal (BGRA8-slot convention).
apply_output_node_f32(&mut samples);
let scope = analyze_f32_rgba(w, h, &samples);
super::displaycolor::apply_f32_rgba(&mut samples, (w * h) as i64);
Some((
@@ -504,6 +514,17 @@ impl RenderedFrame {
}
}
/// The app-side output node for F32 delivery frames: working colorspace →
/// the project's output colorspace, in place on tightly packed samples.
/// Pass-through in the legacy sRGB working space.
fn apply_output_node_f32(samples: &mut [f32]) {
oak_common::colormath::working_to_display_target(
samples,
oak_render::color::pipeline_working_space(),
oak_render::color::pipeline_output_spec(),
);
}
/// Repack one F32 RGBA rendered frame (rows padded to `linesize`) into
/// tightly packed samples. Returns `(width, height, samples)`-style
/// samples only; geometry is validated by the caller.
@@ -1195,6 +1216,14 @@ mod tests {
#[test]
fn montage_effect_stack_reaches_the_rendered_pixels() {
let _media = media_lock();
// This test verifies the effect stack MECHANICS (opacity changes
// pixels; disabling restores them), not the color pipeline. Pin the
// legacy sRGB pass-through so the pixel-value assertions hold
// regardless of the ACEScg default.
oak_render::color::set_pipeline_color_settings(
oak_common::colormath::WorkingColorSpace::SrgbLegacy,
oak_common::colormath::OutputColorSpec::default(),
);
oak_undo::global::clear().unwrap();
let media =
std::env::temp_dir().join(format!("oakapp_montage_fx_{}.mp4", std::process::id()));
+13 -5
View File
@@ -23,6 +23,12 @@
//! viewer costs nothing and no frame is ever walked twice. The scope widgets
//! own the graphing math (histogram binning, waveform envelopes, vectorscope
//! projection); this module only turns pixels into their input samples.
//!
//! The samples are the pipeline / output-colorspace signal, taken BEFORE the
//! display-ICC transform is applied: a scope reads the content's colorimetry,
//! not the viewing monitor's mapping, so the display transform must never
//! feed the scopes (it would make the readings depend on which monitor the
//! app happens to run on).
use std::sync::Arc;
@@ -79,11 +85,13 @@ pub(crate) fn analyze_f32_rgba(width: u32, height: u32, samples: &[f32]) -> Scop
/// Analyzes one BGRA8 frame (the process backend's slot format, M15 S2)
/// into its [`ScopeData`]. The worker converts its F32 pipeline output to
/// BGRA8 at the end of the render, so the scopes read exactly the
/// displayed values with the viewer's 8-bit quantization — precision loss
/// vs the F32 analysis is bounded by 1/255 per channel (acceptable for
/// the scopes; the F32 path stays for the in-process test backend).
/// `bytes` must hold at least `width * height * 4` values.
/// BGRA8 at the end of the render, so the scopes read the output-colorspace
/// signal with the viewer's 8-bit quantization — precision loss vs the F32
/// analysis is bounded by 1/255 per channel (acceptable for the scopes; the
/// F32 path stays for the in-process test backend). Like the F32 path, this
/// runs before the display-ICC transform: scopes read the content signal,
/// not the monitor mapping. `bytes` must hold at least `width * height * 4`
/// values.
pub(crate) fn analyze_bgra8(width: u32, height: u32, bytes: &[u8]) -> ScopeData {
let pixels = (width * height) as usize;
let mut luma = Vec::with_capacity(pixels);