HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
COP_SlapcompProgramManager.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #pragma once
9 
10 #include "COP_API.h"
11 #include "COP_ApexProgram.h"
12 #include "COP_CableStructure.h"
14 
15 #include <OP/OP_Node.h>
16 
17 /// Abstract interface for managing slapcomp.
19 {
20  // Virtual interface that needs to be implemented by concrete subclasses...
21 public:
22  COP_SlapcompManager() = default;
27  virtual ~COP_SlapcompManager() = default;
28 
29  /// Enable slapcomp with the given node.
30  virtual void enable(int target_node_id, const OP_Context&) = 0;
31  void enable(OP_Node *const node, const OP_Context &ctx)
32  { enable(node ? node->getUniqueId() : -1, ctx); }
33  /// Disable slapcomp.
34  virtual void disable() = 0;
35  /// Returns the on-off state of slapcomp.
36  virtual bool isEnabled() const = 0;
37  /// Returns true if the last run of slapcomp had errors.
38  virtual bool hasErrors() const = 0;
39  /// Returns the id of the node slapcomp was built from.
40  virtual int getTargetNode() const = 0;
41 
42  /// Sets a callback that gets called when reporting slapcomp errors.
43  virtual void setErrorCallback(void (*cb)(void*, const char*),
44  void *data) = 0;
45  /// Sets a callback that gets called when reporting slapcomp warnings.
46  virtual void setWarningCallback(void (*cb)(void*, const char*),
47  void *data) = 0;
48  /// Sets a callback that gets called when slapcomp is run.
49  virtual void setRunCallback(void (*cb)(void*), void *data) = 0;
50  /// Sets a callback that gets called when slapcomp changes.
51  virtual void setUpdateCallback(void (*cb)(void*), void *data) = 0;
52 
53  /// Returns true if the slapcomp program ouputs an AOV with the given name.
54  /// Should accept "color" as an alias for the "C" AOV.
55  virtual bool isSlapcompAOV(const UT_StringHolder& name) const = 0;
56  /// Should return true if this slapcomp requires the input layers to bring
57  /// in their cameras.
58  virtual bool expectsLayerCameras() const { return false; }
59 
60  /// Optionally builds and then runs slapcomp with the specified inputs and
61  /// context. Returns successfulness.
63  const OP_Context& context,
64  bool should_build=true)
65  {
66  if (should_build && !build(input_fetcher, context))
67  return false;
68  runSlapcompImpl(input_fetcher, context);
69  return true;
70  }
71 
72  /// Prepares for applying slapcomp for the provided set of inputs.
73  bool build(const COP_SlapcompInputFetcher& input_fetcher,
74  const OP_Context& context)
75  {
76  return buildForInputs(input_fetcher.getLayerStructure(), context);
77  }
78  /// Prepares for applying slapcomp for inputs of the given types.
79  virtual bool buildForInputs(const COP_CableStructure& inputs,
80  const OP_Context& context) = 0;
81 
82  /// Should run slapcomp with the specified inputs and context.
83  virtual void runSlapcompImpl(COP_SlapcompInputFetcher& input_fetcher,
84  const OP_Context& context) = 0;
85 
86  /// Should return the outputs that this slapcomp produces.
87  virtual COP_CableStructure
88  getOutputStructure() const = 0;
89 
90  /// Returns a particular output by name.
91  virtual IMX_LayerPtr
92  getOutputLayer(const UT_StringHolder& name) const = 0;
93 
94  /// Returns a version number, which is incremented every time the program is
95  /// rebuilt.
96  virtual int getVersion() const = 0;
97  /// Returns true if the last attempted application of slapcomp may have
98  /// changed the possible outputs. This flag is is reset before each
99  /// application of slapcomp.
100  virtual bool hasStateChanged() const = 0;
101 
102 protected:
103  /// Reports an error (through the callback if possible).
104  virtual void reportError(const char *message) = 0;
105  /// Reports a warning (through the callback if possible).
106  virtual void reportWarning(const char *message) = 0;
107 
108 public:
109  struct Callbacks
110  {
111  /// Callback function to report errors to the outside world.
112  void (*myErrorCB)(void*, const char*) = nullptr;
113  /// Data to send to the error reporting callback.
114  void *myErrorCBData = nullptr;
115  /// Callback function to report warnings to the outside world.
116  void (*myWarningCB)(void*, const char*) = nullptr;
117  /// Data to send to the warning reporting callback.
118  void *myWarningCBData = nullptr;
119  /// Callback function for when the slapcomp program is run.
120  void (*myRunCB)(void*) = nullptr;
121  /// Data to send to the run callback.
122  void *myRunCBData = nullptr;
123  /// Callback function for when the slapcomp program is updated.
124  void (*myUpdateCB)(void*) = nullptr;
125  /// Data to send to the update callback.
126  void *myUpdateCBData = nullptr;
127  };
128  struct Output
129  {
132  };
133 
134  /// Returns an output map constructed from the outputs of the last execution
135  /// of the slapcomp program. This map is safe to use even after the program
136  /// outputs change as a result of building or running it. If the program is
137  /// not enabled or in an error state, the returned map will be empty.
139  getOutputs() const;
140 
141  /// Set all callbacks using a struct.
142  void setCallbacks(const Callbacks &cbs)
143  {
144  setErrorCallback(cbs.myErrorCB, cbs.myErrorCBData);
145  setWarningCallback(cbs.myWarningCB, cbs.myWarningCBData);
146  setRunCallback(cbs.myRunCB, cbs.myRunCBData);
147  setUpdateCallback(cbs.myUpdateCB, cbs.myUpdateCBData);
148  }
149 
150 protected:
151  /// Relays all warnings and messages from the error manager.
152  void reportAllErrorManagerErrors(const UT_ErrorManager&);
153 
154 public:
155  /// This should really be private, but due to difficult visibility rules, we
156  /// just settle for this...
157  /// This should only be called by higher-level managers that interface with
158  /// other managers.
159  virtual void setVersion(int version) = 0;
160 };
161 
162 /// Manages building and running a slapcomp program from a block, including
163 /// handling of errors.
165 {
166 public:
168 
173  delete;
175 
176  /// Enable and set up the slapcomp program.
177  void enable(int target_node_id,
178  const OP_Context&) override;
179  /// Disable the slapcomp program.
180  void disable() override;
181  /// Returns true if slapcomp is enabled.
182  bool isEnabled() const override { return myEnabled; }
183  /// Returns true if the last run of slapcomp had errors.
184  bool hasErrors() const override { return myHasErrors; }
185 
186  int getTargetNode() const override;
187 
188  /// Rebuilds the slapcomp program if necessary. Any errors or warnings are
189  /// reported through the callbacks.
190  /// @see setErrorCallback
191  /// @see setWarningCallback
193  const OP_Context&) override;
194 
195  /// Run the slapcomp program using the latest build. It is likely you want
196  /// to rebuild the program prior to running it. Any errors or warnings are
197  /// reported through the callbacks.
198  /// @see buildProgram
199  /// @see setErrorCallback
200  /// @see setWarningCallback
202  const OP_Context&) override;
203 
204  /// Returns the specified slapcomp output.
206  const override;
207 
208  /// Returns true if the slapcomp program ouputs an AOV with the given name.
209  /// Accepts "color" as an alias for the "C" AOV.
210  bool isSlapcompAOV(
211  const UT_StringHolder &name)
212  const override;
213  bool expectsLayerCameras() const override;
214  COP_CableStructure getOutputStructure() const override;
215 
216  /// Get the managed slapcomp program.
217  const COP_ApexProgram &getProgram() const { return *myProgram; }
218 
219  /// If this returns true, then the last attempted application of slapcomp
220  /// may have changed the possible outputs.
221  /// This flag is reset before each application of slapcomp.
222  bool hasStateChanged() const override
223  { return myStateChanged; }
224 
225  /// Slapcomp program version number that is incremented each time the
226  /// program is sucessfully rebuilt.
227  int getVersion() const override
228  { return myVersion; }
229 
230  /// Sets a callback that gets called when reporting errors.
231  void setErrorCallback(void (*cb)(void*, const char*),
232  void *data) override
233  {
234  myCallbacks.myErrorCB = cb;
235  myCallbacks.myErrorCBData = data;
236  }
237  /// Sets a callback that gets called when reporting warnings.
238  void setWarningCallback(void (*cb)(void*, const char*),
239  void *data) override
240  {
241  myCallbacks.myWarningCB = cb;
242  myCallbacks.myWarningCBData = data;
243  }
244  /// Sets a callback that gets called when the slapcomp program is run.
245  void setRunCallback(void (*cb)(void*), void *data) override
246  {
247  myCallbacks.myRunCB = cb;
248  myCallbacks.myRunCBData = data;
249  }
250  /// Sets a callback that gets called when the slapcomp program is updated.
251  void setUpdateCallback(void (*cb)(void*), void *data) override
252  {
253  myCallbacks.myUpdateCB = cb;
254  myCallbacks.myUpdateCBData = data;
255  }
256 
257 // semi-public:
258  /// Called by the slapcomp program callback when it is updated.
259  void update(const OP_Context&);
260 
261  void setVersion(int version) override
262  { myVersion = version; }
263 
264 protected:
265  void reportError(const char *message) override;
266  void reportWarning(const char *message) override;
267 
268 private:
269  /// Resets the state and error flags.
270  void clearFlags();
271 
272  /// Reports an error if an output layer does not match the input size.
273  /// This is to ensure that all AOVs have the same size.
274  void reportOutputSizeErrors(
275  UT_Vector2I input_size);
276 
277  /// The slapcomp program.
279  /// Incremented each time the program is sucessfully rebuilt.
280  int myVersion;
281  /// Enable state of slapcomp.
282  bool myEnabled;
283  /// If this flag is true, last application of slapcomp incurred a rebuild of
284  /// the program that may have changed the available output layers.
285  bool myStateChanged;
286  /// If this flag is true, last application of slapcomp had errors and
287  /// failed.
288  bool myHasErrors;
289 
290  Callbacks myCallbacks;
291 };
292 
virtual IMX_LayerPtr getOutputLayer(const UT_StringHolder &name) const =0
Returns a particular output by name.
int getUniqueId() const
Definition: OP_Node.h:750
void * myErrorCBData
Data to send to the error reporting callback.
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
int getVersion(int version)
Definition: ImfVersion.h:99
bool build(const COP_SlapcompInputFetcher &input_fetcher, const OP_Context &context)
Prepares for applying slapcomp for the provided set of inputs.
void
Definition: png.h:1083
GLboolean * data
Definition: glcorearb.h:131
virtual COP_CableStructure getOutputStructure() const =0
Should return the outputs that this slapcomp produces.
void(* myErrorCB)(void *, const char *)
Callback function to report errors to the outside world.
COP_SlapcompManager & operator=(const COP_SlapcompManager &)=delete
void setVersion(int version) override
const COP_ApexProgram & getProgram() const
Get the managed slapcomp program.
void(* myRunCB)(void *)
Callback function for when the slapcomp program is run.
void * myWarningCBData
Data to send to the warning reporting callback.
void * myUpdateCBData
Data to send to the update callback.
void setRunCallback(void(*cb)(void *), void *data) override
Sets a callback that gets called when the slapcomp program is run.
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
bool hasErrors() const override
Returns true if the last run of slapcomp had errors.
void setWarningCallback(void(*cb)(void *, const char *), void *data) override
Sets a callback that gets called when reporting warnings.
bool isEnabled() const override
Returns true if slapcomp is enabled.
UT_SharedPtr< const IMX_Layer > IMX_LayerConstPtr
Definition: IMX_Layer.h:27
bool runSlapcomp(COP_SlapcompInputFetcher &input_fetcher, const OP_Context &context, bool should_build=true)
OPENVDB_API void setVersion(std::ios_base &, const VersionId &libraryVersion, uint32_t fileVersion)
Associate specific file format and library version numbers with the given stream. ...
void setCallbacks(const Callbacks &cbs)
Set all callbacks using a struct.
void * myRunCBData
Data to send to the run callback.
GLuint const GLchar * name
Definition: glcorearb.h:786
virtual COP_CableStructure getLayerStructure() const =0
Returns types and names of all inputs available to this fetcher.
Interface for getting IMX_Layer inputs for a slapcomp program on demand.
virtual void reportWarning(const char *message)=0
Reports a warning (through the callback if possible).
Abstract interface for managing slapcomp.
virtual void reportError(const char *message)=0
Reports an error (through the callback if possible).
GT_API const UT_StringHolder version
virtual bool isSlapcompAOV(const UT_StringHolder &name) const =0
COP_Type
Types of basic data that are passed around a COP network.
Definition: COP_Signature.h:17
LeafData & operator=(const LeafData &)=delete
virtual bool expectsLayerCameras() const
A global error manager scope.
virtual void enable(int target_node_id, const OP_Context &)=0
Enable slapcomp with the given node.
#define COP_API
Definition: COP_API.h:8
void setErrorCallback(void(*cb)(void *, const char *), void *data) override
Sets a callback that gets called when reporting errors.
virtual bool buildForInputs(const COP_CableStructure &inputs, const OP_Context &context)=0
Prepares for applying slapcomp for inputs of the given types.
void setUpdateCallback(void(*cb)(void *), void *data) override
Sets a callback that gets called when the slapcomp program is updated.
void enable(OP_Node *const node, const OP_Context &ctx)
virtual void disable()=0
Disable slapcomp.
virtual int getTargetNode() const =0
Returns the id of the node slapcomp was built from.
void(* myWarningCB)(void *, const char *)
Callback function to report warnings to the outside world.
UT_SharedPtr< IMX_Layer > IMX_LayerPtr
Definition: IMX_Layer.h:26
void(* myUpdateCB)(void *)
Callback function for when the slapcomp program is updated.
Definition: format.h:1821
virtual void runSlapcompImpl(COP_SlapcompInputFetcher &input_fetcher, const OP_Context &context)=0
Should run slapcomp with the specified inputs and context.