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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use ::{Job,Context};
use ::flow::definitions::*;
use ::ffi::CodecInstance;
use ::internal_prelude::works_everywhere::*;
use petgraph::dot::Dot;
use std::process::Command;
use ::rustc_serialize::base64::ToBase64;
use super::visualize::{notify_graph_changed, GraphRecordingUpdate, GraphRecordingInfo};
use petgraph::EdgeDirection;

pub struct Engine<'a, 'b> {
    c: &'a Context,
    job: &'a mut Job,
    g: &'b mut Graph,
}

impl<'a, 'b> Engine<'a, 'b> {


    pub fn create(context: &'a Context, job: &'a mut Job, g: &'b mut Graph) -> Engine<'a, 'b> {
        Engine {
            c: context,
            job: job,
            g: g,
        }
    }

    fn flow_c(&self) -> *mut ::ffi::ImageflowContext{
        self.c.flow_c()
    }

    pub fn validate_graph(&self) -> Result<()> {
        for index in 0..self.g.node_count() {
            let node_index = NodeIndex::new(index);
            let node_def: &NodeDefinition = self.g
                .node_weight(node_index)
                .unwrap()
                .def;

            let outbound_count = self.g
                .graph()
                .edges_directed(node_index, EdgeDirection::Outgoing).count();

            let input_count = self.g
                .graph()
                .edges_directed(node_index, EdgeDirection::Incoming).filter(|&(node, &kind)| kind == EdgeKind::Input).count();

            let canvas_count = self.g
                .graph()
                .edges_directed(node_index, EdgeDirection::Incoming).filter(|&(node, &kind)| kind == EdgeKind::Canvas).count();


            let inputs_failed = match node_def.inbound_edges {
                EdgesIn::NoInput if input_count > 0 || canvas_count > 0 => true,
                EdgesIn::Aribtary { canvases, inputs, .. } if input_count != inputs as usize || canvas_count != canvases as usize => true,
                EdgesIn::OneInput if input_count != 1 && canvas_count != 0 => true,
                EdgesIn::OneInputOneCanvas if input_count != 1 && canvas_count != 1 => true,
                EdgesIn::OneOptionalInput if canvas_count != 0 && (input_count != 0 && input_count != 1) => true,
                _ =>                false
            };
            if inputs_failed {
                let message = format!("Node type {} requires {:?}, but had {} inputs, {} canvases.", node_def.name, node_def.inbound_edges, input_count, canvas_count);
                return Err(FlowError::InvalidConnectionsToNode {
                    value: self.g.node_weight(node_index).unwrap().params.clone(),
                    index: index, message: message
                });
            }
            if !node_def.outbound_edges && outbound_count > 0 {
                let message = format!("Node type {} prohibits child nodes, but had {} outbound edges.", node_def.name, outbound_count);
                return Err(FlowError::InvalidConnectionsToNode {
                    value: self.g.node_weight(node_index).unwrap().params.clone(),
                    index: index, message: message
                });
            }
        }
        Ok(())
    }

    pub fn execute(&mut self) -> Result<()> {
        self.validate_graph()?;
        self.notify_graph_changed()?;

        self.link_codecs(false)?;

        // States for a node
        // New
        // OutboundDimensionsKnown
        // Flattened
        // Optimized
        // LockedForExecution
        // Executed
        let mut passes = 0;
        loop {
            if self.graph_fully_executed() {
                break;
            }

            if passes >= self.job.max_calc_flatten_execute_passes {
                {
                    self.notify_graph_complete()?;
                }
                panic!("Maximum graph passes exceeded");
                //            error_msg!(c, FlowStatusCode::MaximumGraphPassesExceeded);
                //            return false;
            }
            self.link_codecs(true)?;

            self.populate_dimensions_where_certain()?;
            self.notify_graph_changed()?;

            self.graph_pre_optimize_flatten()?;
            self.notify_graph_changed()?;

            self.link_codecs(true)?;

            self.graph_pre_optimize_flatten()?;
            self.notify_graph_changed()?;

            self.link_codecs(true)?;


            self.populate_dimensions_where_certain()?;
            self.notify_graph_changed()?;

            // graph_optimize()?;
            self.notify_graph_changed()?;

            self.populate_dimensions_where_certain()?;
            self.notify_graph_changed()?;

            self.graph_post_optimize_flatten()?;
            self.notify_graph_changed()?;

            self.populate_dimensions_where_certain()?;
            self.notify_graph_changed()?;

            self.graph_execute()?;
            passes += 1;

            self.notify_graph_changed()?;
        }

        if self.job.next_graph_version > 0 && self.job.graph_recording.render_last_graph.unwrap_or(false) {
            self.notify_graph_complete()?;
        }

        Ok(())
    }

    pub fn link_codecs(&mut self, link_only_null_custom_state_nodes: bool) -> Result<()> {
        self.notify_graph_changed()?;

        for index in 0..self.g.node_count() {
            if let Some(func) = self.g
                .node_weight(NodeIndex::new(index))
                .unwrap()
                .def
                .fn_link_state_to_this_io_id {

                let old_custom_state = self.g.node_weight(NodeIndex::new(index)).unwrap().custom_state;
                if !old_custom_state.is_null() && link_only_null_custom_state_nodes{
                    continue;
                }
                let io_id;
                {
                    let mut ctx = self.op_ctx_mut();
                    io_id = func(&mut ctx, NodeIndex::new(index));
                }
                if let Some(io_id) = io_id {
                    let codec_instance_ptr = match self.job.codec_instance_by_io_id(io_id).map(|v| v as *const CodecInstance){
                        Some(v) => v,
                        None => {
                            panic!("Failed to locate codec with io_id {}. There are {} codecs in the job.", io_id, self.job.codecs.iter().count());
                        }
                    };


                    {
                        self.g.node_weight_mut(NodeIndex::new(index)).unwrap().custom_state =
                            unsafe{ mem::transmute(codec_instance_ptr) };
                    }
                    {
                        let weight = self.g.node_weight(NodeIndex::new(index)).unwrap();
                        // Now, try to send decoder its commands
                        // let ref mut weight = ctx.weight_mut(ix);

                        if let NodeParams::Json(s::Node::Decode { io_id, ref commands }) = weight.params {
                            if let Some(ref list) = *commands {
                                for c in list.iter() {
                                    self.job.tell_decoder(io_id, c.to_owned()).unwrap();
                                }
                            }
                        }

                    }
                }
            }
        }
        Ok(())
    }


    fn assign_stable_ids(&mut self) -> Result<()> {
        // Assign stable IDs;
        for index in 0..self.g.node_count() {
            let mut weight = self.g.node_weight_mut(NodeIndex::new(index)).unwrap();
            if weight.stable_id < 0 {
                weight.stable_id = self.job.next_stable_node_id;
                self.job.next_stable_node_id += 1;
            }
        }
        Ok(())
    }


    fn notify_graph_changed(&mut self) -> Result<()> {
        self.assign_stable_ids()?;

        let info = GraphRecordingInfo {
            debug_job_id: self.job.debug_job_id,
            record_graph_versions: self.job.graph_recording.record_graph_versions.unwrap_or(false),
            current_graph_version: self.job.next_graph_version,
            render_graph_versions: self.job.graph_recording.record_graph_versions.unwrap_or(false),
            maximum_graph_versions: 100,
        };
        let update = notify_graph_changed(self.g, info)?;
        if let Some(GraphRecordingUpdate { next_graph_version }) = update {
            self.job.next_graph_version = next_graph_version;
        }
        Ok(())
    }

    fn notify_graph_complete(&mut self) -> Result<()> {
        let prev_filename =
            format!("job_{}_graph_version_{}.dot",
                    self.job.debug_job_id,
                    self.job.next_graph_version - 1);

        super::visualize::render_dotfile_to_png(&prev_filename);
        Ok(())
    }


    pub fn estimate_node(&mut self, node_id: NodeIndex<u32>) -> FrameEstimate {
        let now = time::precise_time_ns();
        let mut ctx = OpCtxMut {
            c: self.c,
            graph: self.g,
            job: self.job,
        };
        // Invoke estimation
        match ctx.weight(node_id).def.fn_estimate {
            Some(f) => {
                f(&mut ctx, node_id);
                ctx.weight_mut(node_id).cost.wall_ns += (time::precise_time_ns() - now) as u32;
            }
            None => {
                ctx.weight_mut(node_id).frame_est = FrameEstimate::Impossible;
            }
        }
        ctx.weight(node_id).frame_est

    }

    pub fn estimate_node_recursive(&mut self, node_id: NodeIndex<u32>) -> FrameEstimate {
        // If we're already done, no need
        if let FrameEstimate::Some(info) = self.g.node_weight(node_id).unwrap().frame_est {
            return FrameEstimate::Some(info);
        }

        // Otherwise let's try again
        let inputs_good = inputs_estimated(self.g, node_id);
        if !inputs_good {
            // TODO: support UpperBound eventually; for now, use Impossible until all nodes implement
            let give_up = inputs_estimates(self.g, node_id).iter().any(|est| match *est {
                FrameEstimate::Impossible |
                FrameEstimate::UpperBound(_) => true,
                _ => false,
            });

            // If it's possible, let's try to estimate parent nodes
            // This is problematic if we want a single call to 'fix' all Impossible nodes.
            // For nodes already populated by Impossible/UpperBound, they will have to be called directly.
            // We won't retry them recursively
            if !give_up {

                let input_indexes = self.g
                    .parents(node_id)
                    .iter(self.g)
                    .map(|(edge_ix, ix)| ix)
                    .collect::<Vec<NodeIndex<u32>>>();

                // println!("Estimating recursively {:?}", input_indexes);
                for ix in input_indexes {

                    self.estimate_node_recursive(ix);
                }
            }

            if give_up || !inputs_estimated(self.g, node_id) {
                self.g.node_weight_mut(node_id).unwrap().frame_est = FrameEstimate::Impossible;
                return FrameEstimate::Impossible;
            }
        }
        // Should be good on inputs here
        if self.estimate_node(node_id) == FrameEstimate::None {
            panic!("Node estimation misbehaved on {}. Cannot leave FrameEstimate::None, must chose an alternative",
                   self.g.node_weight(node_id).unwrap().def.name);
        }
        self.g.node_weight(node_id).unwrap().frame_est
    }

    pub fn populate_dimensions_where_certain(&mut self) -> Result<()> {

        for ix in 0..self.g.node_count() {
            // If any node returns FrameEstimate::Impossible, we might as well move on to execution pass.
            self.estimate_node_recursive(NodeIndex::new(ix));
        }

        Ok(())
    }
    fn invoke_estimated_or_non_estimable_nodes<F>(&mut self, f: F) -> Result<()>
    where F: Fn(&NodeDefinition) -> Option<fn(&mut OpCtxMut, NodeIndex<u32>)> {


        // Just find all nodes that offer the given function and whose parents are completed
        // Try to estimate if not already complete
        // TODO: support other values for FrameEstimate
        // TODO: Compare Node value; should differ afterwards
        loop {
            let mut next = None;
            for ix in 0..(self.g.node_count()) {
                let nix = NodeIndex::new(ix);
                if let Some(func) = f(self.g
                    .node_weight(nix)
                    .unwrap()
                    .def){

                    if self.parents_complete(nix) {
                        if let FrameEstimate::Some(_) = self.g
                            .node_weight(nix)
                            .unwrap()
                            .frame_est {} else {
                            //Try estimation one last time if it didn't happen yet
                            let _ = self.estimate_node(nix);
                        }
                        next = Some((nix, func));
                        break;
                    }

                }
            }
            match next {
                None => return Ok(()),
                Some((next_ix, next_func)) => {
                    let mut ctx = self.op_ctx_mut();
                    next_func(&mut ctx, next_ix);

                }
            }

        }
    }


    pub fn graph_pre_optimize_flatten(&mut self) -> Result<()> {
        self.invoke_estimated_or_non_estimable_nodes(|d| d.fn_flatten_pre_optimize )
    }

    pub fn graph_post_optimize_flatten(&mut self) -> Result<()> {
        self.invoke_estimated_or_non_estimable_nodes(|d| d.fn_flatten_post_optimize )
    }

    fn parents_complete(&self, ix: NodeIndex) -> bool{
        self.g
            .parents(ix)
            .iter(self.g)
            .all(|(ex, parent_ix)| {
                self.g.node_weight(parent_ix).unwrap().result != NodeResult::None
            })
    }
    fn parents_estimated(&self, ix: NodeIndex) -> bool{
        self.g
            .parents(ix)
            .iter(self.g)
            .all(|(ex, parent_ix)| {
                if let FrameEstimate::Some(_) = self.g.node_weight(parent_ix).unwrap().frame_est
                    { true } else { false }
            })
    }




    pub fn graph_execute(&mut self) -> Result<()> {
        // Find nodes with fn_execute, which also have been estimated, and whose parents are complete
        // AND who are not already complete
        loop {
            let mut next = None;
            for ix in 0..(self.g.node_count()) {
                if let Some(func) = self.g.node_weight(NodeIndex::new(ix)).unwrap().def.fn_execute {
                    if self.g.node_weight(NodeIndex::new(ix)).unwrap().result ==
                        NodeResult::None && self.parents_complete(NodeIndex::new(ix)) {
                        if let FrameEstimate::Some(_) = self.g
                            .node_weight(NodeIndex::new(ix))
                            .unwrap()
                            .frame_est {
                        }else{
                            //Try estimation one last time if it didn't happen yet
                            let _ = self.estimate_node(NodeIndex::new(ix));
                        }
                        next = Some((NodeIndex::new(ix), func));
                        break;
                    }

                }
            }
            match next {
                None => return Ok(()),
                Some((next_ix, next_func)) => {
                    {
                        let mut ctx = self.op_ctx_mut();
                        next_func(&mut ctx, next_ix);
                    }
                    if self.g.node_weight(next_ix).unwrap().result == NodeResult::None {
                        panic!("fn_execute of {} failed to save a result",
                               self.g.node_weight(next_ix).unwrap().def.name);
                    } else {
                        unsafe {
                            if self.job.graph_recording.record_frame_images.unwrap_or(false) {
                                if let NodeResult::Frame(ptr) = self.g
                                    .node_weight(next_ix)
                                    .unwrap()
                                    .result {
                                    let path = format!("node_frames/job_{}_node_{}.png",
                                                self.job.debug_job_id,
                                                self.g.node_weight(next_ix).unwrap().stable_id);
                                    let path_copy = path.clone();
                                    let path_cstr = std::ffi::CString::new(path).unwrap();
                                    let _ = std::fs::create_dir("node_frames");
                                    if !::ffi::flow_bitmap_bgra_save_png(self.c.flow_c(),
                                                                         ptr,
                                                                         path_cstr.as_ptr()) {

                                        println!("Failed to save frame {} (from node {})",
                                                 path_copy,
                                                 next_ix.index());
                                        self.c.c_error().unwrap().panic_time();

                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
    }
    fn op_ctx_mut(&mut self) -> OpCtxMut {
        OpCtxMut {
            c: self.c,
            graph: self.g,
            job: self.job,
        }
    }

    fn graph_fully_executed(&self) -> bool {
        for node in self.g.raw_nodes() {
            if node.weight.result == NodeResult::None {
                return false;
            }
        }
        true
    }
}
impl<'a> OpCtxMut<'a> {
    // TODO: Should return Result<String,??>
    pub fn graph_to_str(&mut self) -> Result<String> {
        let mut vec = Vec::new();
        super::visualize::print_graph(&mut vec, self.graph, None).unwrap();
        Ok(String::from_utf8(vec).unwrap())
    }
}



use daggy::walker::Walker;



pub fn flow_node_has_dimensions(g: &Graph, node_id: NodeIndex<u32>) -> bool {
    g.node_weight(node_id)
        .map(|node| match node.frame_est {
            FrameEstimate::Some(_) => true,
            _ => false,
        })
        .unwrap_or(false)
}

pub fn inputs_estimated(g: &Graph, node_id: NodeIndex<u32>) -> bool {
    inputs_estimates(g, node_id).iter().all(|est| match *est {
        FrameEstimate::Some(_) => true,
        _ => false,
    })
}

// -> impl Iterator<Item = FrameEstimate> caused compiler panic

pub fn inputs_estimates(g: &Graph, node_id: NodeIndex<u32>) -> Vec<FrameEstimate> {
    g.parents(node_id)
        .iter(g)
        .filter_map(|(_, node_index)| g.node_weight(node_index).map(|w| w.frame_est))
        .collect()
}