| use super::*; |
| use crate::messages::frontend::utility_types::{ExportBounds, FileType}; |
| use glam::{DAffine2, DVec2}; |
| use graph_craft::concrete; |
| use graph_craft::document::value::TaggedValue; |
| use graph_craft::document::{NodeId, NodeNetwork}; |
| use graph_craft::graphene_compiler::Compiler; |
| use graph_craft::proto::GraphErrors; |
| use graph_craft::wasm_application_io::EditorPreferences; |
| use graphene_std::Context; |
| use graphene_std::application_io::{NodeGraphUpdateMessage, NodeGraphUpdateSender, RenderConfig}; |
| use graphene_std::instances::Instance; |
| use graphene_std::memo::IORecord; |
| use graphene_std::renderer::{GraphicElementRendered, RenderParams, SvgRender}; |
| use graphene_std::renderer::{RenderSvgSegmentList, SvgSegment}; |
| use graphene_std::text::FontCache; |
| use graphene_std::vector::style::ViewMode; |
| use graphene_std::vector::{VectorData, VectorDataTable}; |
| use graphene_std::wasm_application_io::{WasmApplicationIo, WasmEditorApi}; |
| use interpreted_executor::dynamic_executor::{DynamicExecutor, IntrospectError, ResolvedDocumentNodeTypesDelta}; |
| use interpreted_executor::util::wrap_network_in_scope; |
| use once_cell::sync::Lazy; |
| use spin::Mutex; |
| use std::sync::Arc; |
| use std::sync::mpsc::{Receiver, Sender}; |
|
|
| |
| |
| |
| pub struct NodeRuntime { |
| #[cfg(test)] |
| pub(super) executor: DynamicExecutor, |
| #[cfg(not(test))] |
| executor: DynamicExecutor, |
| receiver: Receiver<GraphRuntimeRequest>, |
| sender: InternalNodeGraphUpdateSender, |
| editor_preferences: EditorPreferences, |
| old_graph: Option<NodeNetwork>, |
| update_thumbnails: bool, |
|
|
| editor_api: Arc<WasmEditorApi>, |
| node_graph_errors: GraphErrors, |
| monitor_nodes: Vec<Vec<NodeId>>, |
|
|
| |
| inspect_state: Option<InspectState>, |
|
|
| |
| substitutions: HashMap<String, DocumentNode>, |
|
|
| |
| |
| thumbnail_renders: HashMap<NodeId, Vec<SvgSegment>>, |
| vector_modify: HashMap<NodeId, VectorData>, |
| } |
|
|
| |
| #[derive(Debug, serde::Serialize, serde::Deserialize)] |
| pub enum GraphRuntimeRequest { |
| GraphUpdate(GraphUpdate), |
| ExecutionRequest(ExecutionRequest), |
| FontCacheUpdate(FontCache), |
| EditorPreferencesUpdate(EditorPreferences), |
| } |
|
|
| #[derive(Debug, serde::Serialize, serde::Deserialize)] |
| pub struct GraphUpdate { |
| pub(super) network: NodeNetwork, |
| |
| pub(super) inspect_node: Option<NodeId>, |
| } |
|
|
| #[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize)] |
| pub struct ExportConfig { |
| pub file_name: String, |
| pub file_type: FileType, |
| pub scale_factor: f64, |
| pub bounds: ExportBounds, |
| pub transparent_background: bool, |
| pub size: DVec2, |
| } |
|
|
| #[derive(Clone)] |
| struct InternalNodeGraphUpdateSender(Sender<NodeGraphUpdate>); |
|
|
| impl InternalNodeGraphUpdateSender { |
| fn send_generation_response(&self, response: CompilationResponse) { |
| self.0.send(NodeGraphUpdate::CompilationResponse(response)).expect("Failed to send response") |
| } |
|
|
| fn send_execution_response(&self, response: ExecutionResponse) { |
| self.0.send(NodeGraphUpdate::ExecutionResponse(response)).expect("Failed to send response") |
| } |
| } |
|
|
| impl NodeGraphUpdateSender for InternalNodeGraphUpdateSender { |
| fn send(&self, message: NodeGraphUpdateMessage) { |
| self.0.send(NodeGraphUpdate::NodeGraphUpdateMessage(message)).expect("Failed to send response") |
| } |
| } |
|
|
| pub static NODE_RUNTIME: Lazy<Mutex<Option<NodeRuntime>>> = Lazy::new(|| Mutex::new(None)); |
|
|
| impl NodeRuntime { |
| pub fn new(receiver: Receiver<GraphRuntimeRequest>, sender: Sender<NodeGraphUpdate>) -> Self { |
| Self { |
| executor: DynamicExecutor::default(), |
| receiver, |
| sender: InternalNodeGraphUpdateSender(sender.clone()), |
| editor_preferences: EditorPreferences::default(), |
| old_graph: None, |
| update_thumbnails: true, |
|
|
| editor_api: WasmEditorApi { |
| font_cache: FontCache::default(), |
| editor_preferences: Box::new(EditorPreferences::default()), |
| node_graph_message_sender: Box::new(InternalNodeGraphUpdateSender(sender)), |
|
|
| application_io: None, |
| } |
| .into(), |
|
|
| node_graph_errors: Vec::new(), |
| monitor_nodes: Vec::new(), |
|
|
| substitutions: preprocessor::generate_node_substitutions(), |
|
|
| thumbnail_renders: Default::default(), |
| vector_modify: Default::default(), |
| inspect_state: None, |
| } |
| } |
|
|
| pub async fn run(&mut self) { |
| if self.editor_api.application_io.is_none() { |
| self.editor_api = WasmEditorApi { |
| #[cfg(not(test))] |
| application_io: Some(WasmApplicationIo::new().await.into()), |
| #[cfg(test)] |
| application_io: Some(WasmApplicationIo::new_offscreen().await.into()), |
| font_cache: self.editor_api.font_cache.clone(), |
| node_graph_message_sender: Box::new(self.sender.clone()), |
| editor_preferences: Box::new(self.editor_preferences.clone()), |
| } |
| .into(); |
| } |
|
|
| let mut font = None; |
| let mut preferences = None; |
| let mut graph = None; |
| let mut execution = None; |
| for request in self.receiver.try_iter() { |
| match request { |
| GraphRuntimeRequest::GraphUpdate(_) => graph = Some(request), |
| GraphRuntimeRequest::ExecutionRequest(_) => execution = Some(request), |
| GraphRuntimeRequest::FontCacheUpdate(_) => font = Some(request), |
| GraphRuntimeRequest::EditorPreferencesUpdate(_) => preferences = Some(request), |
| } |
| } |
| let requests = [font, preferences, graph, execution].into_iter().flatten(); |
|
|
| for request in requests { |
| match request { |
| GraphRuntimeRequest::FontCacheUpdate(font_cache) => { |
| self.editor_api = WasmEditorApi { |
| font_cache, |
| application_io: self.editor_api.application_io.clone(), |
| node_graph_message_sender: Box::new(self.sender.clone()), |
| editor_preferences: Box::new(self.editor_preferences.clone()), |
| } |
| .into(); |
| if let Some(graph) = self.old_graph.clone() { |
| |
| let _ = self.update_network(graph).await; |
| } |
| } |
| GraphRuntimeRequest::EditorPreferencesUpdate(preferences) => { |
| self.editor_preferences = preferences.clone(); |
| self.editor_api = WasmEditorApi { |
| font_cache: self.editor_api.font_cache.clone(), |
| application_io: self.editor_api.application_io.clone(), |
| node_graph_message_sender: Box::new(self.sender.clone()), |
| editor_preferences: Box::new(preferences), |
| } |
| .into(); |
| if let Some(graph) = self.old_graph.clone() { |
| |
| let _ = self.update_network(graph).await; |
| } |
| } |
| GraphRuntimeRequest::GraphUpdate(GraphUpdate { mut network, inspect_node }) => { |
| |
| self.inspect_state = inspect_node.map(|inspect| InspectState::monitor_inspect_node(&mut network, inspect)); |
|
|
| self.old_graph = Some(network.clone()); |
| self.node_graph_errors.clear(); |
| let result = self.update_network(network).await; |
| self.update_thumbnails = true; |
| self.sender.send_generation_response(CompilationResponse { |
| result, |
| node_graph_errors: self.node_graph_errors.clone(), |
| }); |
| } |
| GraphRuntimeRequest::ExecutionRequest(ExecutionRequest { execution_id, render_config, .. }) => { |
| let transform = render_config.viewport.transform; |
|
|
| let result = self.execute_network(render_config).await; |
| let mut responses = VecDeque::new(); |
| |
| self.process_monitor_nodes(&mut responses, self.update_thumbnails); |
| self.update_thumbnails = false; |
|
|
| |
| let inspect_result = self.inspect_state.and_then(|state| state.access(&self.executor)); |
|
|
| self.sender.send_execution_response(ExecutionResponse { |
| execution_id, |
| result, |
| responses, |
| transform, |
| vector_modify: self.vector_modify.clone(), |
| inspect_result, |
| }); |
| } |
| } |
| } |
| } |
|
|
| async fn update_network(&mut self, mut graph: NodeNetwork) -> Result<ResolvedDocumentNodeTypesDelta, String> { |
| preprocessor::expand_network(&mut graph, &self.substitutions); |
|
|
| let scoped_network = wrap_network_in_scope(graph, self.editor_api.clone()); |
|
|
| |
| assert_eq!(scoped_network.exports.len(), 1, "Graph with multiple outputs not yet handled"); |
|
|
| let c = Compiler {}; |
| let proto_network = match c.compile_single(scoped_network) { |
| Ok(network) => network, |
| Err(e) => return Err(e), |
| }; |
| self.monitor_nodes = proto_network |
| .nodes |
| .iter() |
| .filter(|(_, node)| node.identifier == "graphene_core::memo::MonitorNode".into()) |
| .map(|(_, node)| node.original_location.path.clone().unwrap_or_default()) |
| .collect::<Vec<_>>(); |
|
|
| assert_ne!(proto_network.nodes.len(), 0, "No proto nodes exist?"); |
| self.executor.update(proto_network).await.map_err(|e| { |
| self.node_graph_errors.clone_from(&e); |
| format!("{e:?}") |
| }) |
| } |
|
|
| async fn execute_network(&mut self, render_config: RenderConfig) -> Result<TaggedValue, String> { |
| use graph_craft::graphene_compiler::Executor; |
|
|
| let result = match self.executor.input_type() { |
| Some(t) if t == concrete!(RenderConfig) => (&self.executor).execute(render_config).await.map_err(|e| e.to_string()), |
| Some(t) if t == concrete!(()) => (&self.executor).execute(()).await.map_err(|e| e.to_string()), |
| Some(t) => Err(format!("Invalid input type {t:?}")), |
| _ => Err(format!("No input type:\n{:?}", self.node_graph_errors)), |
| }; |
| let result = match result { |
| Ok(value) => value, |
| Err(e) => return Err(e), |
| }; |
|
|
| Ok(result) |
| } |
|
|
| |
| pub fn process_monitor_nodes(&mut self, responses: &mut VecDeque<FrontendMessage>, update_thumbnails: bool) { |
| |
| self.thumbnail_renders.retain(|id, _| self.monitor_nodes.iter().any(|monitor_node_path| monitor_node_path.contains(id))); |
|
|
| for monitor_node_path in &self.monitor_nodes { |
| |
| if self.inspect_state.is_some_and(|inspect_state| monitor_node_path.last().copied() == Some(inspect_state.monitor_node)) { |
| continue; |
| } |
| |
| let Some(parent_network_node_id) = monitor_node_path.len().checked_sub(2).and_then(|index| monitor_node_path.get(index)).copied() else { |
| warn!("Monitor node has invalid node id"); |
|
|
| continue; |
| }; |
|
|
| |
| let Ok(introspected_data) = self.executor.introspect(monitor_node_path) else { |
| |
| #[cfg(debug_assertions)] |
| warn!("Failed to introspect monitor node {}", self.executor.introspect(monitor_node_path).unwrap_err()); |
|
|
| continue; |
| }; |
|
|
| if let Some(io) = introspected_data.downcast_ref::<IORecord<Context, graphene_std::GraphicElement>>() { |
| Self::process_graphic_element(&mut self.thumbnail_renders, parent_network_node_id, &io.output, responses, update_thumbnails) |
| } else if let Some(io) = introspected_data.downcast_ref::<IORecord<Context, graphene_std::Artboard>>() { |
| Self::process_graphic_element(&mut self.thumbnail_renders, parent_network_node_id, &io.output, responses, update_thumbnails) |
| |
| } else if let Some(record) = introspected_data.downcast_ref::<IORecord<Context, VectorDataTable>>() { |
| let default = Instance::default(); |
| self.vector_modify.insert( |
| parent_network_node_id, |
| record.output.instance_ref_iter().next().unwrap_or_else(|| default.to_instance_ref()).instance.clone(), |
| ); |
| } else { |
| log::warn!("Failed to downcast monitor node output {parent_network_node_id:?}"); |
| } |
| } |
| } |
|
|
| |
| |
| fn process_graphic_element( |
| thumbnail_renders: &mut HashMap<NodeId, Vec<SvgSegment>>, |
| parent_network_node_id: NodeId, |
| graphic_element: &impl GraphicElementRendered, |
| responses: &mut VecDeque<FrontendMessage>, |
| update_thumbnails: bool, |
| ) { |
| |
|
|
| if !update_thumbnails { |
| return; |
| } |
|
|
| let bounds = graphic_element.bounding_box(DAffine2::IDENTITY, true); |
|
|
| |
| let render_params = RenderParams { |
| view_mode: ViewMode::Normal, |
| culling_bounds: bounds, |
| thumbnail: true, |
| hide_artboards: false, |
| for_export: false, |
| for_mask: false, |
| alignment_parent_transform: None, |
| }; |
| let mut render = SvgRender::new(); |
| graphic_element.render_svg(&mut render, &render_params); |
|
|
| |
| let [min, max] = bounds.unwrap_or_default(); |
| render.format_svg(min, max); |
|
|
| |
|
|
| let new_thumbnail_svg = render.svg; |
| let old_thumbnail_svg = thumbnail_renders.entry(parent_network_node_id).or_default(); |
|
|
| if old_thumbnail_svg != &new_thumbnail_svg { |
| responses.push_back(FrontendMessage::UpdateNodeThumbnail { |
| id: parent_network_node_id, |
| value: new_thumbnail_svg.to_svg_string(), |
| }); |
| *old_thumbnail_svg = new_thumbnail_svg; |
| } |
| } |
| } |
|
|
| pub async fn introspect_node(path: &[NodeId]) -> Result<Arc<dyn std::any::Any + Send + Sync + 'static>, IntrospectError> { |
| let runtime = NODE_RUNTIME.lock(); |
| if let Some(ref mut runtime) = runtime.as_ref() { |
| return runtime.executor.introspect(path); |
| } |
| Err(IntrospectError::RuntimeNotReady) |
| } |
|
|
| pub async fn run_node_graph() -> bool { |
| let Some(mut runtime) = NODE_RUNTIME.try_lock() else { return false }; |
| if let Some(ref mut runtime) = runtime.as_mut() { |
| runtime.run().await; |
| } |
| true |
| } |
|
|
| pub async fn replace_node_runtime(runtime: NodeRuntime) -> Option<NodeRuntime> { |
| let mut node_runtime = NODE_RUNTIME.lock(); |
| node_runtime.replace(runtime) |
| } |
|
|
| |
| #[derive(Debug, Clone, Copy)] |
| struct InspectState { |
| inspect_node: NodeId, |
| monitor_node: NodeId, |
| } |
| |
| #[derive(Clone, Debug, Default)] |
| #[cfg_attr(feature = "decouple-execution", derive(serde::Serialize, serde::Deserialize))] |
| pub struct InspectResult { |
| #[cfg(not(feature = "decouple-execution"))] |
| introspected_data: Option<Arc<dyn std::any::Any + Send + Sync + 'static>>, |
| #[cfg(feature = "decouple-execution")] |
| introspected_data: Option<TaggedValue>, |
| pub inspect_node: NodeId, |
| } |
|
|
| impl InspectResult { |
| pub fn take_data(&mut self) -> Option<Arc<dyn std::any::Any + Send + Sync + 'static>> { |
| #[cfg(not(feature = "decouple-execution"))] |
| return self.introspected_data.clone(); |
|
|
| #[cfg(feature = "decouple-execution")] |
| return self.introspected_data.take().map(|value| value.to_any()); |
| } |
| } |
|
|
| |
| impl PartialEq for InspectResult { |
| fn eq(&self, other: &Self) -> bool { |
| self.inspect_node == other.inspect_node |
| } |
| } |
|
|
| impl InspectState { |
| |
| pub fn monitor_inspect_node(network: &mut NodeNetwork, inspect_node: NodeId) -> Self { |
| let monitor_id = NodeId::new(); |
|
|
| |
| for input in network.nodes.values_mut().flat_map(|node| node.inputs.iter_mut()).chain(&mut network.exports) { |
| let NodeInput::Node { node_id, output_index, .. } = input else { continue }; |
| |
| if *output_index != 0 || *node_id != inspect_node { |
| continue; |
| } |
|
|
| *node_id = monitor_id; |
| } |
|
|
| let monitor_node = DocumentNode { |
| inputs: vec![NodeInput::node(inspect_node, 0)], |
| implementation: DocumentNodeImplementation::proto("graphene_core::memo::MonitorNode"), |
| manual_composition: Some(graph_craft::generic!(T)), |
| skip_deduplication: true, |
| ..Default::default() |
| }; |
| network.nodes.insert(monitor_id, monitor_node); |
|
|
| Self { |
| inspect_node, |
| monitor_node: monitor_id, |
| } |
| } |
| |
| fn access(&self, executor: &DynamicExecutor) -> Option<InspectResult> { |
| let introspected_data = executor.introspect(&[self.monitor_node]).inspect_err(|e| warn!("Failed to introspect monitor node {e}")).ok(); |
| |
| #[cfg(feature = "decouple-execution")] |
| let introspected_data = introspected_data.as_ref().and_then(|data| TaggedValue::try_from_std_any_ref(data).ok()); |
|
|
| Some(InspectResult { |
| inspect_node: self.inspect_node, |
| introspected_data, |
| }) |
| } |
| } |
|
|