orinium_browser/engine/
image_decoder.rs1use crate::engine::background_worker::BackgroundWorker;
2use crate::engine::renderer_model::Image;
3
4const IMAGE_DECODE_WORKERS: usize = 2;
5
6struct DecodeCommand {
7 source: String,
8 bytes: Vec<u8>,
9}
10
11struct DecodeResult {
12 source: String,
13 result: anyhow::Result<Image>,
14}
15
16pub struct ImageDecoder {
17 worker: BackgroundWorker<DecodeCommand, DecodeResult>,
18}
19
20impl Default for ImageDecoder {
21 fn default() -> Self {
22 Self::new()
23 }
24}
25
26impl ImageDecoder {
27 pub fn new() -> Self {
28 Self {
29 worker: BackgroundWorker::new(IMAGE_DECODE_WORKERS, |cmd: DecodeCommand| {
30 DecodeResult {
31 source: cmd.source,
32 result: Image::decode(&cmd.bytes),
33 }
34 }),
35 }
36 }
37
38 pub fn decode(&self, source: String, bytes: Vec<u8>) {
39 self.worker.send(DecodeCommand { source, bytes });
40 }
41
42 pub fn try_receive(&self) -> Option<(String, anyhow::Result<Image>)> {
43 self.worker.try_receive().map(|r| (r.source, r.result))
44 }
45}