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
//! Renderer with config and runtime state.
//! Sets up all the surfaces and determines the measurement points.

use super::{
    Color, Image, MeasurementPoint, MeasurementPoints, Parameter, Point, RendererConfig, Size,
};
use crate::{animation, svg, STATE};
use cairo::Context;
use elapsed::measure_time;
use log::{debug, info};
use resvg::{prelude::NodeExt, usvg, ScreenSize};
use serde_derive::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc, Mutex, RwLock,
    },
    time::Instant,
};

#[derive(Deserialize, Serialize)]
#[serde(default)]
pub struct Renderer {
    pub config: Arc<RwLock<RendererConfig>>,
    pub hide_sidepanels: bool,

    #[serde(skip)]
    pub running: Arc<AtomicBool>,

    #[serde(skip)]
    pub frozen: Arc<AtomicBool>,

    #[serde(skip)]
    pub image_svg: Arc<Mutex<Option<Image>>>,

    #[serde(skip)]
    pub image_live: Arc<Mutex<Option<Image>>>,

    #[serde(skip)]
    pub image_preview: Arc<Mutex<Option<Image>>>,

    #[serde(skip)]
    pub live: Arc<Mutex<Option<Image>>>,

    #[serde(skip)]
    pub preview: Arc<Mutex<Option<Image>>>,

    #[serde(skip)]
    pub measurement_points: MeasurementPoints,

    #[serde(skip)]
    pub palettes_cycle_last: Arc<Mutex<Instant>>,

    #[serde(skip)]
    pub presets_cycle_last: Arc<Mutex<Instant>>,

    #[serde(skip)]
    pub render_group_cycle_last: Arc<Mutex<Instant>>,
}

impl Default for Renderer {
    fn default() -> Self {
        Self {
            config: Arc::new(RwLock::new(RendererConfig {
                ..Default::default()
            })),
            running: Arc::new(AtomicBool::new(false)),
            frozen: Arc::new(AtomicBool::new(false)),
            image_svg: Arc::new(Mutex::new(None)),
            image_live: Arc::new(Mutex::new(None)),
            image_preview: Arc::new(Mutex::new(None)),
            live: Arc::new(Mutex::new(None)),
            preview: Arc::new(Mutex::new(None)),
            measurement_points: Arc::new(RwLock::new(None)),
            hide_sidepanels: false,
            palettes_cycle_last: Arc::new(Mutex::new(Instant::now())),
            presets_cycle_last: Arc::new(Mutex::new(Instant::now())),
            render_group_cycle_last: Arc::new(Mutex::new(Instant::now())),
        }
    }
}

impl Renderer {
    /// Start animation thread
    pub fn start(&self) {
        self.running.store(true, Ordering::Relaxed);
        animation::spawn(&self);
    }

    /// Stop animation thread
    pub fn stop(&self) {
        self.running.store(false, Ordering::Relaxed);
    }

    /// Plit a rendererd animation onto a given context
    pub fn render_on_context(&self, context: &Context, size: &Size, live: bool) {
        let scaling_factor = {
            let config = self.config.read().unwrap();
            let sx = size.width as f64 / config.size.width as f64;
            let sy = size.height as f64 / config.size.height as f64;
            sx.min(sy)
        };
        context.scale(scaling_factor, scaling_factor);

        let (elapsed, _) = measure_time(|| {
            let mut result = if live {
                self.live.lock().unwrap()
            } else {
                self.preview.lock().unwrap()
            };

            if let Some(result) = result.as_mut() {
                result.with_surface(|result| {
                    context.set_source_surface(&result, 0., 0.);
                    context.paint();
                    context.set_source_rgb(0.0, 0.0, 0.0);
                });
            }
        });
        debug!("Plit: {:?}", elapsed);
    }

    /// (Re)Setup surfaces and SVG render.
    pub fn configure(&self) {
        let config = self.config.read().unwrap();
        create_surface(&config.size, &self.image_svg);
        create_surface(&config.size, &self.image_live);
        create_surface(&config.size, &self.image_preview);
        create_surface(&config.size, &self.live);
        create_surface(&config.size, &self.preview);
        if let Some((parameters, tree)) = config
            .file_path
            .as_ref()
            .map(|file_path| {
                if file_path.is_relative() {
                    crate::path().unwrap().parent().unwrap().join(file_path)
                } else {
                    file_path.clone()
                }
            })
            .map(|file_path| svg::tree(&file_path))
        {
            render_svg(
                &parameters,
                &tree,
                &config.size,
                &self.image_svg,
                &self.measurement_points,
            );
            let state = STATE.read().unwrap();
            state.update_uis.iter().for_each(|update_ui| {
                let _ = update_ui.send(());
            });
        }
    }
}

/// Create surface for given size
fn create_surface(size: &Size, image: &Arc<Mutex<Option<Image>>>) {
    info!("Create surfaces");
    let (elapsed, _) = measure_time(|| {
        *image.lock().unwrap() = Some(Image::new(size.width, size.height));
    });
    debug!("Create surfaces: {:?}", elapsed);
    info!("Done creating surfaces");
}

/// Render SVG file to surface and determine measurement points
fn render_svg(
    parameters: &HashMap<String, Parameter>,
    tree: &usvg::Tree,
    size: &Size,
    image_svg: &Arc<Mutex<Option<Image>>>,
    measurement_points: &MeasurementPoints,
) {
    info!("Render svg");
    let (elapsed, _) = measure_time(|| {
        if let Some(image_svg) = image_svg.lock().unwrap().as_mut() {
            image_svg.with_surface(|image_svg| {
                let opt = resvg::Options::default();
                let context = Context::new(&image_svg);
                resvg::backend_cairo::render_to_canvas(
                    &tree,
                    &opt,
                    ScreenSize::new(size.width as u32, size.height as u32)
                        .expect("Wrong screen size"),
                    &context,
                );
                image_svg.flush();

                let mut measurement_points = measurement_points.write().unwrap();
                *measurement_points = {
                    let mut measurement_points = HashMap::new();
                    tree.root().descendants().for_each(|node| {
                        if let (Some(rect), Some(parameter)) = (
                            &node.calculate_bbox(),
                            parameters.get(&node.id().to_owned()),
                        ) {
                            parameter.render_groups.iter().for_each(|render_group| {
                                let x = rect.x() + rect.width() / 2.0;
                                let y = rect.y() + rect.height() / 2.0;
                                let point = context.user_to_device(x, y);
                                measurement_points
                                    .entry(render_group.to_owned())
                                    .or_insert_with(Vec::new)
                                    .push(MeasurementPoint {
                                        leds: parameter.leds.clone(),
                                        point: Point::new(point.0, point.1),
                                        color: Color {
                                            red: 0,
                                            green: 0,
                                            blue: 0,
                                        },
                                    });
                            });
                        }
                    });
                    Some(measurement_points)
                };
            });
        }
    });
    debug!("Render SVG: {:?}", elapsed);
    info!("Done rendering svg");

    crate::set_has_ui_changes();
}