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
use ::Graph;
use ::ffi::BitmapBgra;
use ::internal_prelude::works_everywhere::*;
use super::definitions::{FrameEstimate, Node, PixelFormat, EdgeKind, NodeResult};


pub struct GraphRecordingInfo {
    pub debug_job_id: i32,
    pub current_graph_version: i32,
    pub record_graph_versions: bool,
    pub render_graph_versions: bool,
    pub maximum_graph_versions: i32,
}


pub struct GraphRecordingUpdate {
    pub next_graph_version: i32,
}

pub fn notify_graph_changed(graph_ref: &mut Graph,
                            r: GraphRecordingInfo)
                            -> Result<Option<GraphRecordingUpdate>> {
    if !r.record_graph_versions || r.current_graph_version > r.maximum_graph_versions {
        return Ok(None);
        // println!("record_graph_versions=true, current_graph_version={}", current_graph_version);
    }
    if r.current_graph_version == 0 {
        job_delete_graphviz(r.debug_job_id).unwrap();
    }

    let prev_graph_version = r.current_graph_version - 1;

    let frame_prefix = format!("./node_frames/job_{}_node_", r.debug_job_id);

    let current_filename =
        format!("job_{}_graph_version_{}.dot", r.debug_job_id, r.current_graph_version);
    {
        let mut f = File::create(&current_filename).unwrap();
        print_graph(&mut f, graph_ref, Some(&frame_prefix)).unwrap();
        println!("Writing file {}", &current_filename);
    }
    if prev_graph_version >= 0 {
        let prev_filename =
            format!("job_{}_graph_version_{}.dot", r.debug_job_id, prev_graph_version);
        if files_identical(&current_filename, &prev_filename)
            .expect(&format!("Comparison err'd for {} and {}", &current_filename, &prev_filename)) {
            std::fs::remove_file(&current_filename).unwrap();

            // Next time we will overwrite the duplicate graph. The last two graphs may
            // remain dupes
            Ok(None)
        } else {
            if r.render_graph_versions {
                render_dotfile_to_png(&prev_filename)
            }
            Ok(Some(GraphRecordingUpdate { next_graph_version: r.current_graph_version + 1 }))
        }
    } else {
        Ok(None)
    }
}



pub fn render_dotfile_to_png(dotfile_path: &str) {
    let _ = std::process::Command::new("dot")
        .arg("-Tpng")
        .arg("-Gsize=11,16\\!")
        .arg("-Gdpi=150")
        .arg("-O")
        .arg(dotfile_path)
        .spawn();
    // .expect("dot command failed");
}
// pub fn job_render_graph_to_png(c: *mut Context, job: *mut Job, g: &mut Graph, graph_version: int32_t) -> bool
// {
//    let filename = format!("job_{}_graph_version_{}.dot", unsafe { (*job).debug_job_id }, graph_version);
//    let mut file = File::create(&filename).unwrap();
//    let _ = file.write_fmt(format_args!("{:?}", Dot::new(g.graph())));
//
//    return true;
// }



static INDENT: &'static str = "    ";

fn get_pixel_format_name_for(bitmap: *const BitmapBgra) -> &'static str {
    unsafe { get_pixel_format_name((*bitmap).fmt, (*bitmap).alpha_meaningful) }
}

fn get_pixel_format_name(fmt: PixelFormat, alpha_meaningful: bool) -> &'static str {
    match fmt {
        PixelFormat::Bgr24 => "bgra24",
        PixelFormat::Gray8 => "gray8",
        PixelFormat::Bgra32 if alpha_meaningful => "bgra32",
        PixelFormat::Bgra32 => "bgr32",
        // _ => "?"
    }
}

pub fn print_graph(f: &mut std::io::Write,
                   g: &Graph,
                   node_frame_filename_prefix: Option<&str>)
                   -> std::io::Result<()> {
    try!(writeln!(f, "digraph g {{\n"));
    try!(writeln!(f, "{}node [shape=box, fontsize=20, fontcolor=\"#5AFA0A\" fontname=\"sans-serif bold\"]\n  size=\"12,18\"\n", INDENT));
    try!(writeln!(f, "{}edge [fontsize=20, fontname=\"sans-serif\"]\n", INDENT));


    // output all edges
    for (i, edge) in g.raw_edges().iter().enumerate() {
        try!(write!(f, "{}n{} -> n{}",
                    INDENT,
                    edge.source().index(),
                    edge.target().index()));

        let weight = g.node_weight(edge.source()).unwrap();

        let dimensions = match weight.result {
            NodeResult::Frame(ptr) => {
                unsafe {
                    format!("frame {}x{} {}", (*ptr).w, (*ptr).h, get_pixel_format_name_for(ptr))
                }
            }
            _ => {
                match weight.frame_est {
                    FrameEstimate::None => "?x?".to_owned(),
                    FrameEstimate::Some(info) => format!("est {}x{} {}", info.w, info.h, get_pixel_format_name(info.fmt, info.alpha_meaningful)),
                    _ => "!x!".to_owned(),
                }
            }
        };
        try!(write!(f, " [label=\"e{}: {}{}\"]\n", i, dimensions, match g.edge_weight(EdgeIndex::new(i)).unwrap() {
            &EdgeKind::Canvas => " canvas",
            _ => ""
        }));
    }

    let mut total_ns: u64 = 0;

    // output all labels
    for index in g.graph().node_indices() {
        let weight: &Node = g.node_weight(index).unwrap();
        total_ns += weight.cost.wall_ns as u64;
        let ms = weight.cost.wall_ns as f64 / 1000f64;

        try!(write!(f, "{}n{} [", INDENT, index.index()));

        if let Some(prefix) = node_frame_filename_prefix {
            try!(write!(f, "image=\"{}{}.png\", ", prefix, weight.stable_id));
        }
        try!(write!(f, "label=\"n{}: ",  index.index()));
        try!(weight.graphviz_node_label(f));
        try!(write!(f, "\n{:.5}ms\"]\n", ms));
    }
    let total_ms = (total_ns as f64) / 1000.0f64;
    try!(writeln!(f, "{}graphinfo [label=\"{} nodes\n{} edges\nExecution time: {:.3}ms\"]\n",
                  INDENT, g.node_count(), g.edge_count(), total_ms));
    try!(writeln!(f, "}}"));
    Ok(())
}

fn remove_file_if_exists(path: &str) -> io::Result<()> {
    let result = std::fs::remove_file(path);
    if result.as_ref().err().and_then(|e| Some(e.kind() == io::ErrorKind::NotFound)) == Some(true) {
        return Ok(());
    }
    result
}
fn files_identical(filename_a: &str, filename_b: &str) -> std::io::Result<bool> {
    let mut a = try!(File::open(filename_a));
    let mut a_str = Vec::new();
    try!(a.read_to_end(&mut a_str));
    let mut b = try!(File::open(filename_b));
    let mut b_str = Vec::new();
    try!(b.read_to_end(&mut b_str));

    Ok(a_str == b_str)
}


fn job_delete_graphviz(job_id: i32) -> io::Result<()> {
    let safety_limit = 8000;

    // Keep deleting until we run out of files or hit a safety limit
    let mut node_index = 0;
    loop {
        let next = format!("./node_frames/job_{}_node_{}.png", job_id, node_index);
        if !Path::new(&next).exists() || node_index > safety_limit {
            break;
        } else {
            node_index += 1;
            try!(remove_file_if_exists(&next));
        }
    }
    let mut version_index = 0;
    loop {
        let next = format!("job_{}_graph_version_{}.dot", job_id, version_index);
        let next_png = format!("job_{}_graph_version_{}.dot.png", job_id, version_index);
        let next_svg = format!("job_{}_graph_version_{}.dot.svg", job_id, version_index);
        if !Path::new(&next).exists() || version_index > safety_limit {
            break;
        } else {
            version_index += 1;
            try!(remove_file_if_exists(&next));
            try!(remove_file_if_exists(&next_png));
            try!(remove_file_if_exists(&next_svg));
        }
    }
    Ok(())
}