re_viewer_context 0.31.1

Rerun viewer state that is shared with the viewer's code components.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use ahash::HashMap;

use re_chunk::RowId;
use re_chunk_store::ChunkStoreEvent;
use re_entity_db::EntityDb;
use re_log_types::hash::Hash64;
use re_rvl::{RosRvlMetadata, decode_rvl_with_quantization};
use re_sdk_types::ComponentIdentifier;
use re_sdk_types::components::{ImageBuffer, ImageFormat as ImageFormatComponent, MediaType};
use re_sdk_types::datatypes::{Blob, ChannelDatatype, ColorModel, ImageFormat};
use re_sdk_types::image::{ImageKind, ImageLoadError};

use crate::cache::filter_blob_removed_events;
use crate::image_info::StoredBlobCacheKey;
use crate::{Cache, ImageInfo};

struct DecodedImageResult {
    /// Cached `Result` from decoding the image
    result: Result<ImageInfo, ImageLoadError>,

    /// Total memory used by this image.
    memory_used: u64,

    /// At which [`ImageDecodeCache::generation`] was this image last used?
    last_use_generation: u64,
}

/// Caches the results of decoding [`re_sdk_types::archetypes::EncodedImage`] and [`re_sdk_types::archetypes::EncodedDepthImage`].
#[derive(Default)]
pub struct ImageDecodeCache {
    cache: HashMap<StoredBlobCacheKey, HashMap<Hash64, DecodedImageResult>>,
    memory_used: u64,
    generation: u64,
}

impl ImageDecodeCache {
    /// Decode some image data and cache the result.
    ///
    /// The `RowId`, if available, may be used to generate the cache key.
    /// NOTE: images are never batched atm (they are mono-archetypes),
    /// so we don't need the instance id here.
    pub fn entry_encoded_color(
        &mut self,
        blob_row_id: RowId,
        blob_component: ComponentIdentifier,
        image_bytes: &[u8],
        media_type: Option<&MediaType>,
    ) -> Result<ImageInfo, ImageLoadError> {
        re_tracing::profile_function!();

        let Some(media_type) = media_type
            .cloned()
            .or_else(|| MediaType::guess_from_data(image_bytes))
        else {
            return Err(ImageLoadError::UnrecognizedMimeType);
        };

        let inner_key = Hash64::hash(&media_type);

        self.cache_lookup_or_decode(blob_row_id, blob_component, inner_key, || {
            decode_color_image(
                blob_row_id,
                blob_component,
                image_bytes,
                media_type.as_str(),
            )
        })
    }

    /// Decode some depth image data and cache the result.
    ///
    /// The `RowId`, if available, may be used to generate the cache key.
    /// NOTE: depth images are never batched atm (they are mono-archetypes),
    /// so we don't need the instance id here.
    pub fn entry_encoded_depth(
        &mut self,
        blob_row_id: RowId,
        blob_component: ComponentIdentifier,
        image_bytes: &[u8],
        media_type: Option<&MediaType>,
    ) -> Result<ImageInfo, ImageLoadError> {
        re_tracing::profile_function!();

        let Some(media_type) = media_type
            .cloned()
            .or_else(|| MediaType::guess_from_data(image_bytes))
        else {
            return Err(ImageLoadError::UnrecognizedMimeType);
        };

        let inner_key = Hash64::hash(&media_type);

        self.cache_lookup_or_decode(blob_row_id, blob_component, inner_key, || {
            decode_encoded_depth(
                blob_row_id,
                blob_component,
                image_bytes,
                media_type.as_str(),
            )
        })
    }

    fn cache_lookup_or_decode<F>(
        &mut self,
        blob_row_id: RowId,
        blob_component: ComponentIdentifier,
        cache_key: Hash64,
        decode: F,
    ) -> Result<ImageInfo, ImageLoadError>
    where
        F: FnOnce() -> Result<ImageInfo, ImageLoadError>,
    {
        let blob_cache_key = StoredBlobCacheKey::new(blob_row_id, blob_component);

        if let Some(existing) = self
            .cache
            .get_mut(&blob_cache_key)
            .and_then(|per_blob| per_blob.get_mut(&cache_key))
        {
            existing.last_use_generation = self.generation;
            return existing.result.clone();
        }

        let result = decode();
        let memory_used = result.as_ref().map_or(0, |image| image.buffer.len() as u64);
        self.memory_used += memory_used;

        self.cache.entry(blob_cache_key).or_default().insert(
            cache_key,
            DecodedImageResult {
                result: result.clone(),
                memory_used,
                last_use_generation: self.generation,
            },
        );

        result
    }
}

fn decode_color_image(
    blob_row_id: RowId,
    blob_component: ComponentIdentifier,
    image_bytes: &[u8],
    media_type: &str,
) -> Result<ImageInfo, ImageLoadError> {
    re_tracing::profile_function!(media_type);

    let mut reader = image::ImageReader::new(std::io::Cursor::new(image_bytes));

    if let Some(format) = image::ImageFormat::from_mime_type(media_type) {
        reader.set_format(format);
    } else {
        return Err(ImageLoadError::UnsupportedMimeType(media_type.to_owned()));
    }

    let dynamic_image = reader.decode()?;

    let (buffer, format) = ImageBuffer::from_dynamic_image(dynamic_image)?;

    Ok(ImageInfo::from_stored_blob(
        blob_row_id,
        blob_component,
        buffer.0,
        format.0,
        ImageKind::Color,
    ))
}

fn decode_encoded_depth(
    blob_row_id: RowId,
    blob_component: ComponentIdentifier,
    image_bytes: &[u8],
    media_type: &str,
) -> Result<ImageInfo, ImageLoadError> {
    match media_type {
        MediaType::PNG => decode_png_depth(blob_row_id, blob_component, image_bytes),
        MediaType::RVL => decode_rvl_depth(blob_row_id, blob_component, image_bytes),
        other => Err(ImageLoadError::UnsupportedMimeType(other.to_owned())),
    }
}

fn decode_png_depth(
    blob_row_id: RowId,
    blob_component: ComponentIdentifier,
    image_bytes: &[u8],
) -> Result<ImageInfo, ImageLoadError> {
    re_tracing::profile_function!();

    let mut reader = image::ImageReader::new(std::io::Cursor::new(image_bytes));
    reader.set_format(image::ImageFormat::Png);

    let dynamic_image = reader.decode()?;
    let (buffer, mut format) = ImageBuffer::from_dynamic_image(dynamic_image)?;

    if format.color_model != Some(ColorModel::L) {
        return Err(ImageLoadError::DecodeError(format!(
            "Encoded depth PNG must be single-channel (L); got {:?}",
            format.color_model
        )));
    }
    // .. but in our semantics we treat depth as `None` color model since there _is_ no color. (see `ImageKind::Depth`)
    format.color_model = None;

    let expected_num_bytes = format.num_bytes();
    let ImageBuffer(blob) = buffer;
    let actual_num_bytes = blob.len();
    if actual_num_bytes != expected_num_bytes {
        return Err(ImageLoadError::DecodeError(format!(
            "Encoded depth PNG payload is {actual_num_bytes} B, but {format:?} requires {expected_num_bytes} B",
        )));
    }

    Ok(ImageInfo::from_stored_blob(
        blob_row_id,
        blob_component,
        blob,
        *format,
        ImageKind::Depth,
    ))
}

fn decode_rvl_depth(
    blob_row_id: RowId,
    blob_component: ComponentIdentifier,
    image_bytes: &[u8],
) -> Result<ImageInfo, ImageLoadError> {
    let metadata = RosRvlMetadata::parse(image_bytes)
        .map_err(|err| ImageLoadError::DecodeError(err.to_string()))?;

    let format = ImageFormatComponent::from(ImageFormat::depth(
        [metadata.width, metadata.height],
        ChannelDatatype::F32, // We always use the quantization information from the metadata to convert to f32.
    ));

    let buffer: Vec<u8> = decode_rvl_with_quantization(image_bytes, &metadata)
        .map(|v| {
            bytemuck::try_cast_vec(v).unwrap_or_else(|(_err, v)| bytemuck::cast_slice(&v).to_vec())
        })
        .map_err(|err| ImageLoadError::DecodeError(err.to_string()))?;

    let expected_num_bytes = format.num_bytes();
    let actual_num_bytes = buffer.len();
    if actual_num_bytes != expected_num_bytes {
        return Err(ImageLoadError::DecodeError(format!(
            "RVL payload decoded to {actual_num_bytes} B, but {format:?} requires {expected_num_bytes} B"
        )));
    }

    Ok(ImageInfo::from_stored_blob(
        blob_row_id,
        blob_component,
        Blob::from(buffer),
        format.0,
        ImageKind::Depth,
    ))
}

impl Cache for ImageDecodeCache {
    fn name(&self) -> &'static str {
        "ImageDecodeCache"
    }

    fn begin_frame(&mut self) {
        #[cfg(not(target_arch = "wasm32"))]
        let max_decode_cache_use = 4_000_000_000;

        #[cfg(target_arch = "wasm32")]
        let max_decode_cache_use = 1_000_000_000;

        // TODO(jleibs): a more incremental purging mechanism, maybe switching to an LRU Cache
        // would likely improve the behavior.

        if self.memory_used > max_decode_cache_use {
            self.purge_memory();
        }

        self.generation += 1;
    }

    fn purge_memory(&mut self) {
        re_tracing::profile_function!();

        // Very aggressively flush everything not used in this frame

        let before = self.memory_used;

        self.cache.retain(|_cache_key, per_key| {
            per_key.retain(|_, ci| {
                let retain = ci.last_use_generation == self.generation;
                if !retain {
                    self.memory_used -= ci.memory_used;
                }
                retain
            });

            !per_key.is_empty()
        });

        re_log::trace!(
            "Flushed tensor decode cache. Before: {:.2} GB. After: {:.2} GB",
            before as f64 / 1e9,
            self.memory_used as f64 / 1e9,
        );
    }

    fn on_store_events(&mut self, events: &[&ChunkStoreEvent], _entity_db: &EntityDb) {
        re_tracing::profile_function!();

        let cache_key_removed = filter_blob_removed_events(events);
        self.cache
            .retain(|cache_key, _per_key| !cache_key_removed.contains(cache_key));
    }
}

impl re_byte_size::MemUsageTreeCapture for ImageDecodeCache {
    fn capture_mem_usage_tree(&self) -> re_byte_size::MemUsageTree {
        let mut node = re_byte_size::MemUsageNode::new();

        // Add per-item breakdown
        let mut items: Vec<_> = self
            .cache
            .iter()
            .map(|(k, images)| {
                let bytes_cpu: u64 = images.values().map(|image| image.memory_used).sum();
                (format!("{:x}", k.0.hash64()), bytes_cpu)
            })
            .collect();
        items.sort_by(|a, b| a.0.cmp(&b.0));

        for (item_name, bytes_cpu) in items {
            node.add(item_name, re_byte_size::MemUsageTree::Bytes(bytes_cpu));
        }

        node.into_tree()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use image::{ColorType, ImageEncoder as _, codecs::png::PngEncoder};

    #[test]
    fn entry_encoded_depth_guesses_png_media_type() {
        let width = 2;
        let height = 2;
        let depth_values: [u16; 4] = [0, 1, 2, 3];

        let mut encoded_png = Vec::new();
        {
            let encoder = PngEncoder::new(&mut encoded_png);
            encoder
                .write_image(
                    bytemuck::cast_slice(&depth_values),
                    width,
                    height,
                    ColorType::L16.into(),
                )
                .expect("encoding png failed");
        }

        let mut cache = ImageDecodeCache::default();

        let image_info = cache
            .entry_encoded_depth(
                RowId::ZERO,
                ComponentIdentifier::from("test"),
                &encoded_png,
                None,
            )
            .expect("decoding encoded depth image failed");

        assert_eq!(image_info.kind, ImageKind::Depth);
        assert_eq!(
            image_info.format,
            ImageFormat::depth([width, height], ChannelDatatype::U16)
        );
    }

    #[test]
    fn decoding_png_depth_works() {
        let width = 2;
        let height = 2;
        let depth_values: [u16; 4] = [0, 1, 2, 3];

        let mut encoded_png = Vec::new();
        {
            let encoder = PngEncoder::new(&mut encoded_png);
            encoder
                .write_image(
                    bytemuck::cast_slice(&depth_values),
                    width,
                    height,
                    ColorType::L16.into(),
                )
                .expect("encoding png failed");
        }

        let image_info =
            decode_png_depth(RowId::ZERO, ComponentIdentifier::from("test"), &encoded_png)
                .expect("decoding png depth failed");

        assert_eq!(image_info.kind, ImageKind::Depth);
        assert_eq!(
            image_info.format,
            ImageFormat::depth([width, height], ChannelDatatype::U16)
        );
        assert_eq!(
            image_info.buffer.len(),
            depth_values.len() * std::mem::size_of::<u16>()
        );
    }
}