HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_NodeCallback.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  * COMMENTS:
7  */
8 
9 #ifndef __PDG_NODE_CALLBACK_H__
10 #define __PDG_NODE_CALLBACK_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_LogUtils.h"
15 #include "PDG_Node.h"
16 #include "PDG_RegisteredType.h"
17 
18 #include <UT/UT_ParallelUtil.h>
19 #include <UT/UT_PerfMonAutoEvent.h>
20 #include <UT/UT_Performance.h>
21 #include <UT/UT_WorkBuffer.h>
22 
23 #define _PDG_INVOKE_CALLBACK(result, type, state, node, item, localize, ...) \
24 { \
25  PDG_LogUtils::nodeLog( \
26  PDG_LogUtils::eNodeDebugCallback, node, "CALLBACK on" #type, \
27  UT_StringHolder::theEmptyString); \
28  UT_WorkBuffer _event_path; \
29  bool path_has_item = node->performancePath(_event_path, #type, item); \
30  UT_PerfMonAutoPDGCookEvent event( \
31  _event_path.buffer(), (path_has_item ? nullptr : #type)); \
32  PDG_EvaluationContext::StateScope scope(SYSgetSTID(), state, \
33  localize, node, item, node->context()); \
34  UTisolate([&]() \
35  { \
36  try \
37  {\
38  result = node->callback()->postInvoke( \
39  node->callback()->on ## type (__VA_ARGS__)); \
40  } \
41  catch (...) \
42  { \
43  result = PDG_CallbackResult::eFailure; \
44  }\
45  });\
46 }
47 
48 #define PDG_NODE_CALLBACK(result, type, state, node, ...) \
49 { \
50  result = PDG_CallbackResult::eMissing; \
51  if (node->callback()->hasCallback(PDG_NodeCallback::e ## type))\
52  {\
53  _PDG_INVOKE_CALLBACK( \
54  result, type, state, node, nullptr, false, __VA_ARGS__); \
55  }\
56 }
57 
58 #define PDG_TASK_CALLBACK(result, type, item, localize) \
59 { \
60  result = PDG_CallbackResult::eMissing; \
61  if (item->node()->callback()->hasCallback(PDG_NodeCallback::e ## type)) \
62  {\
63  PDG_AttributeMap::ScopedLock<false> auto_lock(item->attributes()); \
64  _PDG_INVOKE_CALLBACK( \
65  result, type, PDG_EvaluationContext::eStateCooking, \
66  item->node(), item, localize, item); \
67  } \
68 }
69 
71 class PDG_Node;
72 class PDG_NodeOptions;
74 class PDG_WorkItemHolder;
75 class PDG_WorkItem;
76 
78 {
79 public:
80  /// Work item generation type
82  {
83  eStaticGenerate = 0,
84  eDynamicGenerate = 1,
85 
86  eStaticRegenerate = 2,
87  eDynamicRegenerate = 3,
88  };
89 
90  /// Enumeration of node callbacks. These enum values are a
91  /// power of 2 since they go into a bit vector for checking
92  /// the presence of callbacks
94  {
95  eGenerate = 0x0001,
96  eRegenerate = 0x0002,
97 
98  eAddInternalDependencies = 0x0004,
99 
100  ePartition = 0x0008,
101  ePostPartition = 0x0010,
102  eMapStatic = 0x0020,
103  eMapDynamic = 0x0040,
104 
105  ePreCook = 0x0080,
106  ePostCook = 0x0100,
107 
108  ePrepareTask = 0x0200,
109  eCookTask = 0x0400,
110  ePostCookTask = 0x0800,
111 
112  eSelectTask = 0x1000,
113  eDeselectTask = 0x2000,
114 
115  eConfigureNode = 0x4000,
116 
117  eProcessorBits = eGenerate | eRegenerate | eConfigureNode,
118  ePartitionerBits = ePartition,
119  eMapperBits = eMapStatic | eMapDynamic,
120  };
121 
122 public:
124  const PDGT_ValueArgs& extra_args,
125  PDG_Node* node);
126  ~PDG_NodeCallback() override;
127 
128  /// Returns the memory usage of this callback
129  int64 getMemoryUsage(bool inclusive) const override;
130 
131  /// Returns true if the node callback has the specified callback installed,
132  /// else returns false
133  bool hasCallback(CallbackType callback_type) const;
134 
135  /// Enables a specific node callback
136  void enableCallback(CallbackType callback_type);
137 
138  /// Resets this node callback to match changes to the type object
139  bool reloadInstance(UT_WorkBuffer& errors) override;
140 
141  /// Performs post callback invocation logic and returns a final
142  /// callback result
144  postInvoke(PDG_CallbackResult invoke_result) const;
145 
146 
147  /// Generates static items via a work item holder. Called on processors.
148  virtual PDG_CallbackResult
149  onGenerate(PDG_WorkItemHolder*,
150  const PDG_WorkItemArray&,
151  GenerationType generation_type);
152 
153  /// Regenerates static items, only called when items already exist. The
154  /// callback can add new items, or dirty/delete existing items.
155  virtual PDG_CallbackResult
156  onRegenerate(PDG_WorkItemHolder*,
157  const PDG_WorkItemArray&,
158  const PDG_WorkItemArray&,
159  GenerationType generation_type);
160 
161  /// Adds dependencies between newly created work items. Called on
162  /// processors.
163  virtual PDG_CallbackResult
164  onAddInternalDependencies(PDG_DependencyHolder*,
165  const PDG_WorkItemArray&,
166  bool is_static);
167 
168  /// Groups upstream work items into partitions that can be non-disjoint and
169  /// do not necessarily cover the set of all items. Called on partitioner
170  /// nodes.
171  virtual PDG_CallbackResult
172  onPartition(PDG_PartitionHolder*,
173  const PDG_WorkItemArray&,
174  const PDG_WorkItem*);
175 
176  /// Runs after partitions have been created, so that the node can edit
177  /// attributes on the partition
178  virtual PDG_CallbackResult
179  onPostPartition(
180  PDG_WorkItem* partition,
181  const PDG_WorkItemConstArray& partition_items);
182 
183  /// Adds dependencies between static itrems below a mapper node and static
184  /// ancestors above. Called on mapper nodes.
185  virtual PDG_CallbackResult
186  onMapStatic(PDG_DependencyHolder* holder,
187  const PDG_WorkItemArray& downstream_items,
188  const PDG_WorkItemArray& upstream_items);
189 
190  /// Adds dependencies between dynamic items above a mapper and static items
191  /// below the mapper.
192  virtual PDG_CallbackResult
193  onMapDynamic(PDG_DependencyHolder* holder,
194  const PDG_WorkItemArray& downstream_items,
195  const PDG_WorkItemArray& upstream_items);
196 
197  /// Called before the graph begins cooking. This is only called on nodes
198  /// that are on the active cook path, and is guaranteed to be called on each
199  /// node before any threaded cooking begins. If a Failure result is returned
200  /// from any of the nodes, the cook is aborted
201  virtual PDG_CallbackResult
202  onPreCook();
203 
204  /// Called on each node in the graph after the cook finishes.
205  virtual PDG_CallbackResult
206  onPostCook();
207 
208  /// Called when a work item has all of its dependencies satisfied, and will
209  /// be submitted to the scheduler or cooked in-process shortly.
210  virtual PDG_CallbackResult
211  onPrepareTask(PDG_WorkItem* work_item);
212 
213  /// Called when a work item wants to cook in-process, but does not have its
214  /// own cook implementation
215  virtual PDG_CallbackResult
216  onCookTask(PDG_WorkItem* work_item);
217 
218  /// Called when a work item has completed cooking.
219  virtual PDG_CallbackResult
220  onPostCookTask(PDG_WorkItem* work_item);
221 
222  /// Called when a work item is selected by TOPs.
223  virtual PDG_CallbackResult
224  onSelectTask(PDG_WorkItem* work_item);
225 
226  /// Called when a work item is deselcted
227  virtual PDG_CallbackResult
228  onDeselectTask(PDG_WorkItem* work_item);
229 
230  /// Called when the node should describe itself for the UI
231  virtual PDG_CallbackResult
232  onConfigureNode(PDG_NodeOptions* node_options);
233 
234  /// Returns the node associated with this callback
235  PDG_Node* node() const;
236 
237  /// Returns the cook type of the node associated with this callback,
238  /// if it's a processor node
240  cookType() const;
241 
242 
243  /// Convenience method for evaluating a parameter on the underlying node
244  template <typename T>
245  bool evaluate(
246  T& result,
247  const UT_StringHolder& name,
248  const PDG_WorkItem* work_item=nullptr,
249  int index=0,
250  int multi=-1) const
251  {
252  return myNode->evaluate(result, name, work_item, index, multi);
253  }
254 
255  /// Convenience method for evaluating a parameter as an array on the
256  /// underlying node
257  template <typename T>
259  T& result,
260  const UT_StringHolder& name,
261  const PDG_WorkItem* work_item=nullptr,
262  int multi=-1) const
263  {
264  return myNode->evaluate(result, name, work_item, multi);
265  }
266 
267  /// Evaluates a parameter as a string and returns the value back, instead
268  /// of an error code.
270  const UT_StringHolder& name,
271  const PDG_WorkItem* work_item=nullptr,
272  int index=0,
273  int multi=-1) const
274  {
275  UT_StringHolder result = "";
276  myNode->evaluate(result, name, work_item, index, multi);
277  return result;
278  }
279 
280  /// Evaluates a parameter as a raw string and returns the value back,
281  /// instead of an error code.
283  const UT_StringHolder& name,
284  const PDG_WorkItem* work_item=nullptr,
285  int index=0,
286  int multi=-1) const
287  {
288  UT_StringHolder result = "";
289  myNode->evaluateRaw(result, name, work_item, index, multi);
290  return result;
291  }
292 
293  /// Evaluates a parameter as a float and returns the value back, instead
294  /// of an error code.
296  const UT_StringHolder& name,
297  const PDG_WorkItem* work_item=nullptr,
298  int index=0,
299  int multi=-1) const
300  {
301  fpreal result = 0;
302  myNode->evaluate(result, name, work_item, index, multi);
303  return result;
304  }
305 
306  /// Evaluates a parameter as an integer and returns the value back, instead
307  /// of an error code.
309  const UT_StringHolder& name,
310  const PDG_WorkItem* work_item=nullptr,
311  int index=0,
312  int multi=-1) const
313  {
314  exint result = 0;
315  myNode->evaluate(result, name, work_item, index, multi);
316  return result;
317  }
318 
319  /// Evaluates a parameter as a boolean and returns the value back, instead
320  /// of an error code.
321  bool evaluateB(
322  const UT_StringHolder& name,
323  const PDG_WorkItem* work_item=nullptr,
324  int index=0,
325  int multi=-1) const
326  {
327  bool result = false;
328  myNode->evaluate(result, name, work_item, index, multi);
329  return result;
330  }
331 
332  /// Evaluates a parameter as an arbitrary type and returns the value back,
333  /// instad of an error code.
334  template <typename T>
336  const UT_StringHolder& name,
337  const PDG_WorkItem* work_item=nullptr,
338  int index=0,
339  int multi=-1) const
340  {
341  T result{};
342  myNode->evaluate(result, name, work_item, index, multi);
343  return result;
344  }
345 
346 protected:
349 };
350 
351 #endif
UT_StringHolder evaluateS(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
#define PDG_API
Definition: PDG_API.h:23
int64 exint
Definition: SYS_Types.h:125
fpreal evaluateF(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
**But if you need a result
Definition: thread.h:622
exint evaluateI(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
virtual bool reloadInstance(UT_WorkBuffer &errors)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
virtual int64 getMemoryUsage(bool inclusive) const
T evaluateT(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
long long int64
Definition: SYS_Types.h:116
PDG_WorkItemCookType
Enumeration of work item cook types.
GLuint const GLchar * name
Definition: glcorearb.h:786
fpreal64 fpreal
Definition: SYS_Types.h:278
GLuint index
Definition: glcorearb.h:786
bool evaluateB(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
bool evaluateArray(T &result, const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int multi=-1) const
PDG_CallbackResult
Enumeration of callback return types.
Definition: PDG_NodeTypes.h:48
GenerationType
Work item generation type.
UT_StringHolder evaluateRaw(const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
A partition, which has a list of upstream components.
bool evaluate(T &result, const UT_StringHolder &name, const PDG_WorkItem *work_item=nullptr, int index=0, int multi=-1) const
Convenience method for evaluating a parameter on the underlying node.