GfxContext.hpp
1 #pragma once
2 #include <Gfx/Graph/Node.hpp>
3 
4 #include <ossia/dataflow/nodes/media.hpp>
5 #include <ossia/dataflow/token_request.hpp>
6 #include <ossia/detail/buffer_pool.hpp>
7 #include <ossia/detail/flat_set.hpp>
8 #include <ossia/detail/hash_map.hpp>
9 #include <ossia/detail/mutex.hpp>
10 #include <ossia/detail/variant.hpp>
11 #include <ossia/gfx/port_index.hpp>
12 #include <ossia/network/value/value.hpp>
13 
14 #include <concurrentqueue.h>
15 #include <score_plugin_gfx_export.h>
16 namespace score::gfx
17 {
18 struct Graph;
19 class OutputNode;
20 }
21 namespace score
22 {
23 struct DocumentContext;
24 }
25 namespace Gfx
26 {
27 using port_index = ossia::gfx::port_index;
28 
29 using Edge = std::pair<port_index, port_index>;
30 class GfxExecutionAction;
31 class SCORE_PLUGIN_GFX_EXPORT GfxContext : public QObject
32 {
33  friend class GfxExecutionAction;
34 
35 public:
36  using NodePtr = std::unique_ptr<score::gfx::Node>;
37 
38  explicit GfxContext(const score::DocumentContext& ctx);
39  ~GfxContext();
40 
41  int32_t register_node(NodePtr node);
42  int32_t register_preview_node(NodePtr node);
43  void connect_preview_node(Edge e);
44  void disconnect_preview_node(Edge e);
45  void unregister_node(int32_t idx);
46  void unregister_preview_node(int32_t idx);
47 
48  void recompute_edges();
49  void recompute_graph();
50  void recompute_connections();
51 
52  void update_inputs();
53  void updateGraph();
54 
55  void send_message(score::gfx::Message&& msg) noexcept
56  {
57  tick_messages.enqueue(std::move(msg));
58  }
59 
60 private:
61  void run_commands();
62  void add_preview_output(score::gfx::OutputNode& out);
63  void remove_preview_output();
64  void add_edge(Edge e);
65  void remove_edge(Edge e);
66  void remove_node(std::vector<std::unique_ptr<score::gfx::Node>>& nursery, int32_t id);
67 
68  void timerEvent(QTimerEvent*) override;
69  const score::DocumentContext& m_context;
70  int32_t index{};
71  ossia::hash_map<int32_t, NodePtr> nodes;
72 
73  score::gfx::Graph* m_graph{};
74  QThread m_thread;
75 
76  struct NodeCommand
77  {
78  enum
79  {
80  ADD_NODE,
81  ADD_PREVIEW_NODE,
82  REMOVE_NODE,
83  REMOVE_PREVIEW_NODE,
84  RELINK
85  } cmd{};
86  int32_t index{};
87  std::unique_ptr<score::gfx::Node> node;
88  };
89 
90  struct EdgeCommand
91  {
92  enum
93  {
94  CONNECT_PREVIEW_NODE,
95  DISCONNECT_PREVIEW_NODE
96  } cmd{};
97  Edge edge;
98  };
99 
100  using Command = ossia::variant<NodeCommand, EdgeCommand>;
101  moodycamel::ConcurrentQueue<Command> tick_commands;
102  moodycamel::ConcurrentQueue<score::gfx::Message> tick_messages;
103 
104  std::mutex edges_lock;
105  ossia::flat_set<Edge> new_edges TS_GUARDED_BY(edges_lock);
106  ossia::flat_set<Edge> edges;
107  ossia::flat_set<Edge> preview_edges;
108  std::atomic_bool edges_changed{};
109 
110  int m_timer{-1};
111 
112  ossia::flat_map<int, score::gfx::OutputNode*> m_manualTimers;
113 
114  ossia::object_pool<std::vector<score::gfx::gfx_input>> m_buffers;
115 };
116 
117 }
Definition: GfxContext.hpp:32
Definition: GfxExecContext.hpp:16
Base class for sink nodes (QWindow, spout, syphon, NDI output, ...)
Definition: OutputNode.hpp:23
Binds the rendering pipeline to ossia processes.
Definition: CameraDevice.cpp:28
Graphics rendering pipeline for ossia score.
Definition: PreviewWidget.hpp:12
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: DocumentContext.hpp:18
Represents a graph of renderers.
Definition: score-plugin-gfx/Gfx/Graph/Graph.hpp:16
Definition: score-plugin-gfx/Gfx/Graph/Node.hpp:51