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