Lumiverse  2.5
A framework for creating lighting control applications
SimulationAnimationPatch.h
Go to the documentation of this file.
1 
4 #ifndef _SimulationAnimationPATCH_H_
5 #define _SimulationAnimationPATCH_H_
6 
7 #pragma once
8 
9 #include "LumiverseCoreConfig.h"
10 
11 #include "../lib/libjson/libjson.h"
12 #include "SimulationPatch.h"
14 #include "ArnoldFileFrameManager.h"
15 
16 #include <thread>
17 #include <chrono>
18 #include <iostream>
19 
20 namespace Lumiverse {
29  INTERACTIVE, RECORDING, RENDERING, STOPPED
30  };
31 
33  struct FrameDeviceInfo {
34  // The time point of this frame.
35  // It's actually the duration counted from the update is first time
36  // called.
37  time_t time;
38  // Copies for devices connected to this patch.
39  std::set<Device *> devices;
41 
43  FrameDeviceInfo() : time(-1), mode(SimulationAnimationMode::INTERACTIVE) { }
44 
46  void clear() {
47  time = -1;
48 
49  for (Device *d : devices) {
50  if (d != NULL)
51  delete d;
52  d = NULL;
53  }
54  devices.clear();
55  }
56 
58  void copyByValue(const FrameDeviceInfo &other) {
59  time = other.time;
60  mode = other.mode;
61  for (Device *d : other.devices) {
62  devices.insert(new Device(d));
63  }
64  }
65  };
66 
76  {
77  public:
81  SimulationAnimationPatch() : m_worker(NULL),
82  m_startPoint(std::chrono::system_clock::from_time_t(0)),
83 #ifdef USE_ARNOLD
84  m_mem_frameManager(NULL), m_file_frameManager(NULL),
85 #endif
86  m_mode(SimulationAnimationMode::STOPPED) { }
87 
93  SimulationAnimationPatch(const JSONNode data);
94 
98  virtual ~SimulationAnimationPatch();
99 
100  // Callbacks
101  typedef function<void()> FinishedCallbackFunction;
102  typedef function<bool(set<Device *>)> IsUpdateRequiredFunction;
103  typedef function<void(FrameDeviceInfo&)> CreateFrameInfoBodyFunction;
104  typedef function<void()> InterruptFunction;
105  typedef function<void()> ClearUpdateFlagsFunction;
106 
111  void init();
112 
117  virtual void startRecording() {
118 #ifdef USE_ARNOLD
119  m_mem_frameManager->clear();
120 
121  // Overwrite existing frames instead of clearing all
122  if (m_file_frameManager)
123  m_file_frameManager->reset();
124 
125  m_mode = SimulationAnimationMode::RECORDING;
126 #endif
127  }
128 
134  virtual void endRecording();
135 
140  virtual void startInteractive() { m_mode = SimulationAnimationMode::INTERACTIVE; }
141 
147  virtual SimulationAnimationMode getMode() { return m_mode; }
148 
154  virtual string getType() { return "SimulationAnimationPatch"; }
155 
164  void update(set<Device *> devices, IsUpdateRequiredFunction isUpdateRequired,
165  InterruptFunction interruptRender,
166  ClearUpdateFlagsFunction clearUpdateFlags);
167 
176  void close();
177 
178 #ifdef USE_ARNOLD
179 
186  virtual ArnoldFrameManager *getFrameManager() const;
187 #endif
188 
194  virtual void reset(InterruptFunction interruptRender);
195 
196  virtual void reset() = 0;
197 
203  virtual void stop();
204 
214  virtual int addFinishedCallback(FinishedCallbackFunction func);
215 
222  virtual void deleteFinishedCallback(int id);
223 
224  protected:
225 
230  void loadJSON(const JSONNode data);
231 
238  virtual void workerLoop();
239 
243  virtual void onWorkerFinished();
244 
245  virtual void onRecording() { }
246 
247  virtual void onRendering() { }
248 
249  virtual void workerRender(FrameDeviceInfo frame) = 0;
250 
251  virtual void createFrameInfoHeader(FrameDeviceInfo &frame);
252 
253  virtual void createFrameInfoBody(set<Device *> devices, FrameDeviceInfo &frame, bool forceUpdate = false) = 0;
254 
255  virtual void enqueueFrameInfo(const FrameDeviceInfo &frame);
256 
257  // The worker thread.
258  std::thread *m_worker;
259 
260  // The lock for the task queue.
261  std::mutex m_queue;
262 
263  // The task queue.
264  std::vector<FrameDeviceInfo> m_queuedFrameDeviceInfo;
265 
266  // The start point for time points in FrameDeviceInfo.
267  // It's the moment when update function is called for the first
268  // time.
269  std::chrono::time_point<std::chrono::system_clock> m_startPoint;
270 
271 #ifdef USE_ARNOLD
272  // The ArnoldFrameManager object. Used to store frame buffers.
273  ArnoldMemoryFrameManager *m_mem_frameManager;
274  ArnoldFileFrameManager *m_file_frameManager;
275 #endif
276 
280 
283  map<int, FinishedCallbackFunction> m_onFinishedFunctions;
284  };
285 
286 }
287 
288 #endif
virtual void workerLoop()
Worker loop.
Definition: SimulationAnimationPatch.cpp:230
void loadJSON(const JSONNode data)
Loads data from a parsed JSON object.
Definition: SimulationAnimationPatch.cpp:32
virtual void deleteFinishedCallback(int id)
Deletes a registered callback for parameter change.
Definition: SimulationAnimationPatch.cpp:211
The state info for worker thread.
Definition: SimulationAnimationPatch.h:33
FrameDeviceInfo()
Constructor.
Definition: SimulationAnimationPatch.h:43
void init()
Initializes Arnold with function of its parent class and starts a worker thread.
Definition: SimulationAnimationPatch.cpp:52
Definition: Layer.h:437
An implemnetation for frame manager using in-memory buffer.
SimulationAnimationMode
Four working modes of SimulationAnimationPatch. INTERACTIVE: Patch renders the latest sent frame with...
Definition: SimulationAnimationPatch.h:28
virtual ~SimulationAnimationPatch()
Destroys the object.
Definition: SimulationAnimationPatch.cpp:21
virtual void startRecording()
Starts recording. Main thread starts to send frame labeled as RECORDING info to worker.
Definition: SimulationAnimationPatch.h:117
An implemnetation for frame manager using in-memory buffer.
void copyByValue(const FrameDeviceInfo &other)
Deep copy.
Definition: SimulationAnimationPatch.h:58
virtual void onWorkerFinished()
Helper to call all the registered callbacks for rendering finished event.
Definition: SimulationAnimationPatch.cpp:219
virtual void endRecording()
Ends recording. Main thread stops to send frame labeled as RECORDING info to worker. It starts to send INTERACTIVE frame instead.
Definition: SimulationAnimationPatch.cpp:197
map< int, FinishedCallbackFunction > m_onFinishedFunctions
The list for callback functions.
Definition: SimulationAnimationPatch.h:283
SimulationAnimationPatch()
Constructs a SimulationAnimationPatch object.
Definition: SimulationAnimationPatch.h:81
A subclass of ArnoldPatch. Instead of interrupting the worker thread every time a new rendering task ...
Definition: SimulationAnimationPatch.h:75
virtual void startInteractive()
Starts interactive mode. Worker thread can get interrupted. It always takes the most fresh info...
Definition: SimulationAnimationPatch.h:140
void close()
Waits for the worker thread and closes the Arnold session.
Definition: SimulationAnimationPatch.cpp:125
Implementation of a patch for Arnold.
void clear()
Releases the copies for devices.
Definition: SimulationAnimationPatch.h:46
Contains all core Lumiverse functions and variables.
Definition: Device.cpp:2
virtual void stop()
Stops the patch.
Definition: SimulationAnimationPatch.cpp:193
virtual string getType()
Gets the type of this object.
Definition: SimulationAnimationPatch.h:154
SimulationAnimationMode m_mode
Indicates the mode of SimulationAnimationPatch.
Definition: SimulationAnimationPatch.h:279
A Device in Lumiverse maintains information about a lighting device.
Definition: Device.h:55
void update(set< Device * > devices, IsUpdateRequiredFunction isUpdateRequired, InterruptFunction interruptRender, ClearUpdateFlagsFunction clearUpdateFlags)
Updates the rendering queue given the list of devices in the rig.
Definition: SimulationAnimationPatch.cpp:57
virtual int addFinishedCallback(FinishedCallbackFunction func)
Registers a callback function for parameter changed event.
Definition: SimulationAnimationPatch.cpp:204
virtual SimulationAnimationMode getMode()
Returns the mode/state in which the patch is.
Definition: SimulationAnimationPatch.h:147