Phase 3a-3: low-res image proxy for cold-scrub raster frames
Scrubbing onto a paged-out raster keyframe flashed blank for the 1-2 frames its full pixels took to page in. Now a low-res proxy is shown in that gap. - core: `MediaKind::RasterProxy` (id derived from the keyframe id via `raster_proxy_media_id`); `brush_engine::encode_raster_proxy_png` downscales a full RGBA buffer to a ≤192px-long-edge PNG. Save writes a proxy beside each resident frame's full PNG (paged-out frames keep their existing proxy row, like the full). Load eagerly decodes proxies (small) into `RasterKeyframe::proxy`. - editor: a separate `proxy_layer_cache` in the GPU brush (own recency LRU, budget 64 since each is ~1/100th a full frame) + `ensure_proxy_texture`/`get_proxy_texture`. The raster render, when the full texture isn't resident, blits the proxy mapped to the keyframe's FULL logical dims so it upscales via the sampler. F3 VRAM figure now includes proxy textures. When the full pixels land (async fault-in), the full path takes over automatically. Proxies only exist after a save+reload; freshly-painted unsaved frames stay resident so they need none. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
a1e3cc841f
commit
1bfd09f151
@@ -58,6 +58,10 @@ pub enum MediaKind {
|
||||
/// A pack of precomputed video thumbnails for a video clip (keyed by a
|
||||
/// sentinel-derived id from the clip id). Opaque blob; format owned by the editor.
|
||||
Thumbnail = 5,
|
||||
/// A low-res PNG proxy of a raster keyframe (keyed by a sentinel-derived id from
|
||||
/// the keyframe id). Decoded eagerly on load and shown while the full-res pixels
|
||||
/// page in, so cold scrubs don't flash blank. See `raster_proxy_media_id`.
|
||||
RasterProxy = 6,
|
||||
}
|
||||
|
||||
impl MediaKind {
|
||||
@@ -69,6 +73,7 @@ impl MediaKind {
|
||||
3 => Some(Self::ImageAsset),
|
||||
4 => Some(Self::Waveform),
|
||||
5 => Some(Self::Thumbnail),
|
||||
6 => Some(Self::RasterProxy),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -575,6 +575,27 @@ pub fn encode_png(img: &RgbaImage) -> Result<Vec<u8>, String> {
|
||||
Ok(buf.into_inner())
|
||||
}
|
||||
|
||||
/// Long-edge cap (pixels) for raster keyframe proxies — low-res page-in placeholders.
|
||||
pub const RASTER_PROXY_MAX_EDGE: u32 = 192;
|
||||
|
||||
/// Downscale a full RGBA buffer to a low-res proxy PNG (long edge ≤
|
||||
/// `RASTER_PROXY_MAX_EDGE`). Returns `None` on invalid dims/length or encode failure.
|
||||
pub fn encode_raster_proxy_png(raw: &[u8], width: u32, height: u32) -> Option<Vec<u8>> {
|
||||
if width == 0 || height == 0 || raw.len() != (width as usize * height as usize * 4) {
|
||||
return None;
|
||||
}
|
||||
let long = width.max(height);
|
||||
let (pw, ph) = if long <= RASTER_PROXY_MAX_EDGE {
|
||||
(width, height)
|
||||
} else {
|
||||
let s = RASTER_PROXY_MAX_EDGE as f32 / long as f32;
|
||||
(((width as f32 * s).round() as u32).max(1), ((height as f32 * s).round() as u32).max(1))
|
||||
};
|
||||
let img = RgbaImage::from_raw(width, height, raw.to_vec())?;
|
||||
let resized = image::imageops::resize(&img, pw, ph, image::imageops::FilterType::Triangle);
|
||||
encode_png(&resized).ok()
|
||||
}
|
||||
|
||||
/// Decode PNG bytes into an `RgbaImage`
|
||||
pub fn decode_png(data: &[u8]) -> Result<RgbaImage, String> {
|
||||
image::load_from_memory(data)
|
||||
|
||||
@@ -254,6 +254,14 @@ fn thumbnail_media_id(clip_id: Uuid) -> Uuid {
|
||||
Uuid::from_u128(clip_id.as_u128() ^ SENTINEL)
|
||||
}
|
||||
|
||||
/// Derived id for a raster keyframe's low-res proxy row (distinct from the keyframe's
|
||||
/// own full-res `Raster` row, which is keyed by the raw keyframe id).
|
||||
fn raster_proxy_media_id(kf_id: Uuid) -> Uuid {
|
||||
// "LBPX" repeated — distinct id region from the full raster + thumbnail rows.
|
||||
const SENTINEL: u128 = 0x4C42_5058_4C42_5058_4C42_5058_4C42_5058;
|
||||
Uuid::from_u128(kf_id.as_u128() ^ SENTINEL)
|
||||
}
|
||||
|
||||
pub fn save_beam(
|
||||
path: &Path,
|
||||
document: &Document,
|
||||
@@ -418,9 +426,24 @@ pub fn save_beam(
|
||||
}
|
||||
Err(e) => eprintln!("⚠️ [SAVE_BEAM] Failed to encode raster {}: {}", kf.id, e),
|
||||
}
|
||||
// Low-res proxy alongside the full PNG (shown while the full pages
|
||||
// in on a later load). Regenerated from the resident pixels.
|
||||
let proxy_id = raster_proxy_media_id(kf.id);
|
||||
if let Some(proxy_png) =
|
||||
crate::brush_engine::encode_raster_proxy_png(&kf.raw_pixels, kf.width, kf.height)
|
||||
{
|
||||
txn.put_media_packed(
|
||||
proxy_id, MediaKind::RasterProxy, "png", &proxy_png, MediaMeta::default(),
|
||||
)?;
|
||||
live_media.insert(proxy_id);
|
||||
}
|
||||
} else if txn.media_exists(kf.id)? {
|
||||
// Pixels not resident but already stored — keep the row.
|
||||
// Pixels not resident but already stored — keep both rows.
|
||||
live_media.insert(kf.id);
|
||||
let proxy_id = raster_proxy_media_id(kf.id);
|
||||
if txn.media_exists(proxy_id)? {
|
||||
live_media.insert(proxy_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -574,15 +597,33 @@ fn load_beam_sqlite(path: &Path) -> Result<LoadedProject, String> {
|
||||
// instant and only the resident window lives in RAM. Mark every keyframe
|
||||
// `needs_fault_in` (recursively, incl. nested layers) so the renderer requests a
|
||||
// page-in; a freshly-created keyframe stays `false` (blank-resident, nothing to load).
|
||||
// Proxies (low-res, ~tens of KB each) ARE decoded eagerly so a cold scrub onto a
|
||||
// not-yet-paged frame shows the proxy instantly instead of flashing blank.
|
||||
let mut raster_load_count = 0usize;
|
||||
let mut proxy_load_count = 0usize;
|
||||
for layer in document.all_layers_mut() {
|
||||
if let crate::layer::AnyLayer::Raster(rl) = layer {
|
||||
for kf in &mut rl.keyframes {
|
||||
kf.needs_fault_in = true;
|
||||
raster_load_count += 1;
|
||||
let proxy_id = raster_proxy_media_id(kf.id);
|
||||
if let Ok(Some(_)) = archive.media_info(proxy_id) {
|
||||
if let Ok(bytes) = archive.read_media_full(proxy_id) {
|
||||
if let Ok(img) = crate::brush_engine::decode_png(&bytes) {
|
||||
let (w, h) = (img.width(), img.height());
|
||||
kf.proxy = Some(crate::raster_layer::RasterProxy {
|
||||
width: w,
|
||||
height: h,
|
||||
pixels: img.into_raw(),
|
||||
});
|
||||
proxy_load_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = proxy_load_count;
|
||||
|
||||
// Missing external files (referenced entries whose file no longer exists).
|
||||
let project_dir = path.parent().unwrap_or_else(|| Path::new("."));
|
||||
|
||||
@@ -92,6 +92,16 @@ impl Default for TweenType {
|
||||
}
|
||||
}
|
||||
|
||||
/// A low-res decoded RGBA proxy of a keyframe's pixels, shown while the full-res
|
||||
/// buffer pages in from the container so cold scrubs don't flash blank.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RasterProxy {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
/// RGBA, `width * height * 4` bytes.
|
||||
pub pixels: Vec<u8>,
|
||||
}
|
||||
|
||||
/// A single keyframe of a raster layer
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct RasterKeyframe {
|
||||
@@ -130,6 +140,11 @@ pub struct RasterKeyframe {
|
||||
/// unsaved edit). Cleared on a successful save. Never serialized.
|
||||
#[serde(skip)]
|
||||
pub dirty: bool,
|
||||
/// Phase 3a-3: low-res proxy decoded from the container on load, rendered while
|
||||
/// the full pixels page in (removes the cold-scrub blank flash). `None` if the
|
||||
/// keyframe has no persisted proxy yet (new/unsaved, or pre-proxy project).
|
||||
#[serde(skip)]
|
||||
pub proxy: Option<RasterProxy>,
|
||||
}
|
||||
|
||||
fn default_true() -> bool { true }
|
||||
@@ -155,6 +170,7 @@ impl RasterKeyframe {
|
||||
texture_dirty: true,
|
||||
needs_fault_in: false,
|
||||
dirty: false,
|
||||
proxy: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user