HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PDG_ApplicationShim.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_APPLICATION_SHIM_H__
10 #define __PDG_APPLICATION_SHIM_H__
11 
12 #include "PDG_API.h"
13 
14 #include "PDG_EvaluationContext.h"
15 
16 #include <UT/UT_ConcurrentQueue.h>
17 #include <UT/UT_NonCopyable.h>
18 #include <UT/UT_SharedPtr.h>
19 #include <UT/UT_StringHolder.h>
20 
21 #include <SYS/SYS_Types.h>
22 
23 class PDG_FileUtils;
24 class PDG_GraphContext;
25 class PDG_Node;
26 class PDG_Port;
27 class PDG_WorkItem;
28 
30 
31 class UT_WorkBuffer;
32 
33 /**
34  * Shim class for integrating PDG with another application, e.g. Houdini. A
35  * single subclass of this should be installed statically with PDG using
36  * PDG_ApplicationShim::setShim.
37  */
39 {
40 public:
41  /**
42  * Geometry class for binding a PDG geometry attribute to an application-
43  * specific geometry object
44  */
46  {
47  public:
48  Geometry() {}
49  virtual ~Geometry() {}
51 
52  /// Returns the memory usage of the geometry
53  virtual int64 getMemoryUsage(bool inclusive) const = 0;
54 
55  /// Clones the object into a new instance
56  virtual Geometry* copy() const = 0;
57 
58  /// Returns a brief description of the geometry, for UI display
59  virtual UT_StringHolder desc() const = 0;
60  };
61 
62  /**
63  * Node class for binding a PDG node to an application-specific node, such
64  * as a TOP node.
65  */
67  {
68  public:
69  Node() {}
70  virtual ~Node() {}
71  UT_NON_COPYABLE(Node)
72 
73  /// Returns the icon name of the application specific node
74  virtual UT_StringHolder iconName() const = 0;
75 
76  /// Returns a unique id for the application specific node
77  virtual int nodeID() const = 0;
78 
79  /// Returns true if the app node has cooked at least once, using its
80  /// own definition of cooked
81  virtual bool hasCooked() const = 0;
82 
83  /// Pushes the application specific node as a the active node, for the
84  /// specified thread
85  virtual bool push(int thread) = 0;
86 
87  /// Pops the application specific active node
88  virtual bool pop(int thread) = 0;
89 
90 
91  /// Evaluates an integer parameter on the node
92  virtual bool parmEval(const PDG_Port* port,
93  const PDG_WorkItem* work_item,
94  int index,
95  exint& result,
96  int thread,
97  UT_WorkBuffer& errors) const = 0;
98 
99  /// Evaluates a float parameter on the node
100  virtual bool parmEval(const PDG_Port* port,
101  const PDG_WorkItem* work_item,
102  int index,
103  fpreal& result,
104  int thread,
105  UT_WorkBuffer& errors) const = 0;
106 
107  /// Evaluates a string parameter on the node
108  virtual bool parmEval(const PDG_Port* port,
109  const PDG_WorkItem* work_item,
110  int index,
111  bool expand,
112  UT_StringHolder& result,
113  int thread,
114  UT_WorkBuffer& errors) const = 0;
115 
116  /// Returns the path to the node
117  virtual void path(UT_WorkBuffer& buffer) const = 0;
118  };
119 
120  /**
121  * Graph class for binding a PDG node to an application-specific graph, such
122  * as a TOP parent.
123  */
125  {
126  public:
127  Graph() {}
128  virtual ~Graph() {}
129  UT_NON_COPYABLE(Graph)
130 
131  /// Called when a graph cook begins
132  virtual void startCook(const PDGE_EvaluationOptions&) = 0;
133 
134  /// Called when a graph cook should update progress. Returns true if
135  /// a blocking cook should cancel.
136  virtual bool tickCook(const PDGE_EvaluationOptions&) = 0;
137 
138  /// Called when a graph cook completes
139  virtual void stopCook(
140  const PDGE_EvaluationOptions&,
141  bool canceled) = 0;
142 
143  /// Called to determine the default label for a work item
144  virtual bool defaultWorkItemLabel(
145  UT_StringHolder& label) const = 0;
146  };
147 
148  /**
149  * Base class for a task that can be queued. Should be subclassed to
150  * implement a concrete task type that stores data and logic.
151  */
153  {
154  public:
155  DeferredTask(
156  PDG_GraphContext* context,
157  const PDG_Node* node,
158  const PDG_WorkItem* work_item,
160  virtual ~DeferredTask() {}
161 
162  /// Evaluates the task
163  void execute();
164 
165  /// Waits for the task to execute. Returns true if the task completed,
166  /// or false if it was interupted for some reason.
167  bool wait();
168 
169  protected:
170  /// Called when the task is evaluated
171  virtual void evaluate() = 0;
172 
173  protected:
175  const PDG_Node* myNode;
177 
180  int myCookId;
182  };
183 
184  /// Managed pointer to a deferred task
186  using DeferredQueue = UT_ConcurrentQueue<TaskPtr>;
187 
188 public:
189  /// Adds a new Python search path to the PDG type system
190  static void addPythonPath(const UT_StringHolder& path);
191 
192  /// Returns true if the integration is allowed to use farm schedulers
193  static bool canUseFarm();
194 
195  /// Returns true if the application-specific work item dependency is
196  /// dirty
197  static bool areDependenciesDirty(fpreal t);
198 
199  /// Notifies the application that the global PDG work item has been changed
200  static void dirtyDependencies(fpreal t, const UT_StringHolder& name);
201 
202  /// Adds a dependency on the global PDG work item for the specified thread
203  static void addDependency(int thread, const UT_StringHolder& name);
204 
205  /// Queries the name of the evaluating channel for the specified thread
206  static bool evaluatingChannel(UT_WorkBuffer& buffer,
207  bool full_path,
208  int thread);
209 
210  /// Queries the value of a global variable
211  static bool getVariable(const char* name, UT_StringHolder& value);
212 
213  /// Sets the value of a global variable
214  static void setVariable(const char* name, const UT_StringHolder& value);
215 
216  /// Returns true if the file in the specified path is cached
217  static bool checkCached(const PDG_FileUtils& file_utils,
218  const UT_StringHolder& path,
219  const UT_StringHolder& tag);
220 
221  /// Queue a task with the deferred processing queue, to be run on the
222  /// main thread.
223  template <typename Task, typename... Args>
224  static TaskPtr pushDeferred(Args&&... args)
225  {
226  TaskPtr new_task = UTmakeShared<Task>(
227  std::forward<Args>(args)...);
228  pushDeferredTask(new_task);
229  return new_task;
230  }
231 
232  /// Runs a functor with the application unlocked.
233  template <typename Func>
234  static void unlockedExecute(const Func& func)
235  {
236  bool did_unlock = false;
237  if (theAppShim)
238  did_unlock = theAppShim->unlockApp();
239 
240  func();
241 
242  if (did_unlock)
243  theAppShim->lockApp();
244  }
245 
246  /// Runs the application shims logic for a main thread event tick. The
247  /// base application instance will drain pending deferred events from the
248  /// queue and run some of them.
249  static bool tickMainThread();
250 
251  /// Enable experimental features
252  static bool isExperimental();
253 
254  /// Saves the current application scene. Returns false if the application
255  /// wants to abort the current operationi because of save conflict or a
256  /// request to cancel, otherwise returns true.
257  static bool trySaveScene();
258 
259  /// Sets the shared static shim instance. This can only be called once
260  /// during the lifetime of the program.
261  static void setShim(PDG_ApplicationShim* shim);
262 
263  /// Constructs a new application shim and caches configuration vars
265  virtual ~PDG_ApplicationShim() {}
266 
267 protected:
268  virtual void addAppPythonPath(const UT_StringHolder& path) const = 0;
269 
270  virtual bool canAppUseFarm() const = 0;
271  virtual bool areAppDependenciesDirty(fpreal t) const = 0;
272  virtual void dirtyAppDependencies(fpreal t,
273  const UT_StringHolder& name) = 0;
274  virtual void addAppDependency(int thread,
275  const UT_StringHolder& name) = 0;
276  virtual bool evaluatingAppChannel(UT_WorkBuffer& buffer,
277  bool full_path,
278  int thread) = 0;
279  virtual bool getAppVariable(const char* name,
280  UT_StringHolder& value) const = 0;
281  virtual void setAppVariable(const char* name,
282  const UT_StringHolder& value) = 0;
283  virtual bool checkAppCached(const PDG_FileUtils& file_utils,
284  const UT_StringHolder& path,
285  const UT_StringHolder& tag) const = 0;
286  virtual bool tickAppMainThread();
287 
288  virtual bool unlockApp() const = 0;
289  virtual bool lockApp() const = 0;
290 
291  virtual bool trySaveAppScene() const = 0;
292 
293 private:
294  static void pushDeferredTask(TaskPtr& ptr);
295 
296 private:
297  static PDG_ApplicationShim* theAppShim;
298 
299  DeferredQueue myDeferredTasks;
300  bool myIsExperimental;
301 };
302 
303 #endif
*pool push(my_func, arg1,...)
GLuint GLsizei const GLchar * label
Definition: glcorearb.h:2545
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
#define PDG_API
Definition: PDG_API.h:23
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
State
Evaluation context state.
static TaskPtr pushDeferred(Args &&...args)
int64 exint
Definition: SYS_Types.h:125
**But if you need a result
Definition: thread.h:613
PDG_EvaluationContext::State myState
UT_ConcurrentQueue< TaskPtr > DeferredQueue
static void unlockedExecute(const Func &func)
Runs a functor with the application unlocked.
Definition: core.h:760
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
UT_SharedPtr< DeferredTask > TaskPtr
Managed pointer to a deferred task.
long long int64
Definition: SYS_Types.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
GLdouble t
Definition: glad.h:2397
*tasks wait()
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
GLenum func
Definition: glcorearb.h:783
fpreal64 fpreal
Definition: SYS_Types.h:277
GLuint index
Definition: glcorearb.h:786
auto ptr(T p) -> const void *
Definition: format.h:2448
**If you just want to fire and args
Definition: thread.h:609
Definition: core.h:1131
#define const
Definition: zconf.h:214