HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TIL_CopResolver.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  * NAME: TIL_CopResolver.h ( TIL Library, C++)
7  *
8  * COMMENTS:
9  * Class to get a pointer to a COPs raster. This class is used by the
10  * texture mapping code to resolve names of the form:
11  *
12  * op:/comp/cop2net/cop{C,A}[frame]
13  * file:filename{xres,yres}
14  *
15  * The frame is optional. Sub-classes don't need to worry about parsing
16  * out the [frame], this is all handled by the static methods.
17  */
18 
19 #ifndef __TIL_CopResolver__
20 #define __TIL_CopResolver__
21 
22 #include "TIL_API.h"
23 #include "TIL_Defines.h"
24 #include <IMG/IMG_FileTypes.h>
25 #include <UT/UT_NonCopyable.h>
26 #include <UT/UT_ValArray.h>
27 #include <UT/UT_WorkBuffer.h>
28 #include <UT/UT_SharedPtr.h>
29 #include <UT/UT_NonCopyable.h>
30 #include <stddef.h>
31 
32 
33 #define TIL_DEFAULT_COLOR_PLANE "(Default Color)"
34 #define TIL_DEFAULT_ALPHA_PLANE "(Default Alpha)"
35 #define TIL_NO_PLANE "(None)"
36 
37 class TIL_Raster;
38 class TIL_Sequence;
39 class TIL_ImageSource;
40 class UT_String;
41 class IMX_Layer;
42 
44 {
45 public:
47  virtual ~TIL_CopResolver();
48 
50 
51  /// Class to push/pop the enable state
54  {
55  public:
56  autoEnableCache(bool enable)
57  {
58  myOldEnabled = TIL_CopResolver::enableThreadResolverCache(enable);
59  }
61  {
63  }
64  private:
65  bool myOldEnabled;
66  };
67 
68  /// Class to push/pop the evaluate time
71  {
72  public:
74  {
75  if (getResolver())
76  {
77  myOldSeconds = getResolver()->getGlobalTime();
78  getResolver()->setGlobalTime(time);
79  }
80  }
82  {
83  if (getResolver())
84  {
85  getResolver()->setGlobalTime(myOldSeconds);
86  }
87  }
88 
89  private:
90  fpreal myOldSeconds;
91  };
92 
93  static void setResolver(TIL_CopResolver *resolver);
94  static TIL_CopResolver *getResolver();
95 
96  /// Turns on resolver cache for THIS thread. This allows in process
97  /// renders to only hit op: once per render.
98  /// Returns old value.
99  static bool enableThreadResolverCache(bool enable);
100 
101  /// Returns true if this thread has the resolver enabled
102  static bool threadResolverCacheEnabled();
103 
104  /// Provides a watermark for the resolver cache, bumped on every
105  /// clear. So if you acquired with a similar water mark you don't
106  /// need to check for dirty.
107  static exint threadResolverCacheWatermark();
108 
109  /// Clears any out of date resolver cache, ie, where cop version
110  /// doesn't match the stashed version. The force_full_clear flag can be
111  /// set to true to clear everything out of the cache. This is necessary
112  /// if a time change happens and a time dependent COP texture is in the
113  /// cache. The "version" will be unchanged, but we need to clear anyway.
114  static void clearResolverCache(bool force_full_clear);
115 
116  /// Gets node ids of any COPs with textures in the resolver cache.
117  static void getResolverCacheNodeIds(UT_IntArray &ids,
118  UT_StringArray &invalid_paths);
119 
120  // Get an TIL_Raster associated with the COP in the path specified
121  // YOU MUST CALL doneWithRaster or returnRaster when done!
122  // Do not free it, and do not dangle it.
123  static TIL_Raster *getRaster(const char *fullpath,
124  int &opid,
125  int udim_tile = 0,
126  bool *specific_frame = nullptr);
127  static fpreal getRasterTime(int opid);
128  static size_t getRasterVersion(int opid);
129 
130  static fpreal getTimeFromFrame(fpreal frame);
131 
132  /// Tell the resolver we're done with the raster.
133  /// Some resolvers will delete rasters, others will hold onto them. If the
134  /// raster actually dirties/deletes the raster, @c doneWithRaster() will
135  /// return true.
136  static bool doneWithRaster(const TIL_Raster *r);
137 
138  /// Returns shared IMX_Layer with COW semantics. Only works
139  /// for copernicus, will need getRaster for Images or COP2.
140  static UT_SharedPtr<const IMX_Layer> getLayer(
141  const char *fullpath,
142  int &opid,
143  int udim_tile = 0,
144  bool *specific_frame = nullptr);
145 
146  // Returns 1 if the frame is specified in the filename. The id will be -1
147  // if there is no such path.
148  static int getIdFrame(const char *fullpath, int &id,
149  fpreal &frame);
150  static void splitFile(const char *fullpath,
151  UT_String &name,
152  int &xres,int &yres,
153  float &quality);
154 
155  // These split net/node methods are now out of style with networks
156  // within networks. Now you should specify full, absolute, paths.
157  static int getNodeId(const char *net, const char *node);
158  static void getNodeRes(const char *net, const char *node,
159  int &xres, int &yres);
160 
161  static int getNodeId(const char *fullpath);
162  static void getNodeRes(const char *fullpath,
163  int &xres, int &yres);
164 
165  static void getFileRes(const char *file,
166  int &xres, int &yres);
167 
168  static int splitPath(const char *cpath, int &id,
169  fpreal &frame,
170  UT_WorkBuffer &color, int &cindex,
171  UT_WorkBuffer &alpha, int &aindex,
172  int &xres, int &yres);
173 
174  // builds menus for plane selection (COPs2 only).
175  static void buildColorMenu(const char *net,
176  const char *node,
177  UT_ValArray<char *> &items);
178 
179  static void buildAlphaMenu(const char *net,
180  const char *node,
181  UT_ValArray<char *> &items);
182 
183  static bool canResolve(const char *fullpath);
184 
185  // These methods should really only be used inside the TIL_CopResolver
186  // class. They shouldn't be called directly.
187  virtual int resolveId(const char *fullpath) = 0;
188 
189  virtual TIL_Raster *resolveRaster(int id) = 0;
190  virtual TIL_Raster *resolveRasterFrame(int id, fpreal frame,
191  int xres=0, int yres=0) = 0;
192  virtual TIL_Raster *resolveRasterPlane(int id, fpreal frame,
193  const char *color, int cindex,
194  const char *alpha, int aindex,
195  int xres=0, int yres = 0,
196  int udim_tile=0,
197  TIL_DataFormat f =
199 
200  virtual UT_SharedPtr<const IMX_Layer> resolveLayer(int id) = 0;
201  virtual UT_SharedPtr<const IMX_Layer> resolveLayerFrame(int id, fpreal frame) = 0;
202  virtual UT_SharedPtr<const IMX_Layer> resolveLayerPlane(int id,
203  fpreal frame,
204  const char *color,
205  int udim_tile) = 0;
206  // Returns whether or not the resolver knows how to handle this path.
207  virtual bool resolveIsPossible(const char *fullpath) = 0;
208 
209  /// You gain ownership of a FileRaster and must delete it:
210  virtual TIL_Raster *getFileRaster(const char *file,
212  int xres = 0, int yres = 0);
213  /// You do not gain ownershipo of a node raster, and must call
214  /// returnRaster or doneWithRaster on it.
215  /// @param fullpath The path to the COP node, eg "op:/path/to/cop"
216  /// @param cmenu The name of the color plane, eg "C" or
217  /// TIL_DEFAULT_COLOR_PLANE.
218  /// @param amenu The name of the alpha plane, eg "A" or
219  /// TIL_DEFAULT_ALPHA_PLANE, or TIL_NO_PLANE.
220  /// @param override_frame If true, the current time frame is used
221  /// for cooking the COP node to obtain the raster
222  /// (ie, the following 'frame' parameter is ignored).
223  /// @param frame Frame at which to cook the COP node to obtain the raster
224  /// @param f Desired data format (bit precision) of the returned
225  /// raster.
226  /// @param xres Width that the raster will be scaled to. If set to 0,
227  /// will use the node's default width.
228  /// @param yres Height that the raster will be scaled to. If set to 0,
229  /// will use the node's default height.
230  virtual TIL_Raster *getNodeRaster(const char *fullpath,
231  const char *cmenu, const char *amenu,
232  bool override_frame = false,
233  fpreal frame = 1.0,
234  TIL_DataFormat f =
236  int xres = 0, int yres = 0);
237 
238  virtual void getRes(int id, int &xres, int &yres);
239 
240  virtual TIL_Sequence *getSequence(int id);
241 
242  /// This cooks a bunch of planes simulataneously, and adds the created
243  /// rasters to 'new_rasters'. You are responsible for deleting them.
244  virtual bool resolveRasters(int id,
245  const UT_StringArray &plane_names,
246  fpreal t,
247  UT_ValArray<TIL_Raster *> &new_rasters,
249 
250 
251  virtual void buildPlaneMenu(const char *net, const char *node,
252  UT_ValArray<char *> &items);
253 
254  virtual TIL_ImageSource *resolveImageSource(int id);
255 
256  /// Caller is finished with the raster for now, so delete memory if
257  /// possible. Returns true if the raster is no longer valid, but false if
258  /// the raster hasn't been touched (and is still valid). This is
259  /// implementation dependent.
260  virtual bool returnRaster(const TIL_Raster *raster);
261 
262  // If the id returned is really an OP_Node, then this will return 1.
263  virtual int isOpBased();
264 
265  virtual fpreal getTimeForRaster(int id);
266  virtual size_t getVersionForRaster(int id);
267 
268  /// Apply the COP_Node unload prevention via the id.
269  /// Must be paired.
270  virtual void addAutoUnloadPrevention(int id);
271  virtual void removeAutoUnloadPrevention(int id);
272 
273 protected:
274  // Returns 1 if the frame is defined, 0 otherwise. It's possible that
275  // the name returned may be a reference to the path. Please harden if
276  // you need to keep it around.
277  int splitPath(const char *fullpath,
278  UT_String &name, fpreal &frame);
279 
280  virtual fpreal timeFromFrame(fpreal frame);
281  virtual fpreal getGlobalTime();
282  virtual void setGlobalTime(fpreal time);
283 };
284 
286 {
287 public:
288  TIL_CopHandle() = default;
289  TIL_CopHandle(const char *filename)
290  : myRaster(TIL_CopResolver::getRaster(filename, myOpId, 0, &mySpecificFrame))
291  {
292  }
293 
294  TIL_CopHandle(const TIL_CopHandle &) = delete;
295  TIL_CopHandle &operator=(const TIL_CopHandle &ch) = delete;
296 
298  {
299  moveFrom(ch);
300  }
302  {
303  moveFrom(ch);
304  return *this;
305  }
307  {
308  clear();
309  }
310 
311  bool isValid() const { return myRaster != nullptr; }
312  explicit operator bool() const { return isValid(); }
313  int opid() const { return myOpId; }
314  bool specificFrame() const { return mySpecificFrame; }
315  const TIL_Raster *raster() const { return myRaster; }
316  const TIL_Raster *operator->() const { return myRaster; }
317 
318  void clear()
319  {
320  if (myRaster)
322  }
323 
324  bool resolve(const char *filename)
325  {
326  clear();
327  myRaster = TIL_CopResolver::getRaster(filename, myOpId, 0, &mySpecificFrame);
328  return isValid();
329  }
330 
331 private:
332  void moveFrom(TIL_CopHandle &ch)
333  {
334  myRaster = ch.myRaster;
335  myOpId = ch.myOpId;
336  mySpecificFrame = ch.mySpecificFrame;
337  ch.myRaster = nullptr;
338  }
339 
340  const TIL_Raster *myRaster = nullptr;
341  int myOpId = -1;
342  bool mySpecificFrame = false;
343 };
344 
345 #endif
const TIL_Raster * operator->() const
GT_API const UT_StringHolder filename
*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
GT_API const UT_StringHolder time
const TIL_Raster * raster() const
static TIL_Raster * getRaster(const char *fullpath, int &opid, int udim_tile=0, bool *specific_frame=nullptr)
int64 exint
Definition: SYS_Types.h:125
static TIL_CopResolver * getResolver()
OutGridT const XformOp bool bool
bool isValid() const
bool resolve(const char *filename)
GLfloat f
Definition: glcorearb.h:1926
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
#define TIL_DataFormat
Definition: TIL_Defines.h:64
#define TILE_MAX_DATA_FORMAT
Definition: TIL_Defines.h:70
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
static bool enableThreadResolverCache(bool enable)
int opid() const
IMG_DataType
Definition: IMG_FileTypes.h:17
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
HUSD_API const char * raster()
GLfloat GLfloat GLfloat alpha
Definition: glcorearb.h:112
GLuint const GLchar * name
Definition: glcorearb.h:786
GLdouble t
Definition: glad.h:2397
Class to push/pop the enable state.
Class to push/pop the evaluate time.
GLuint color
Definition: glcorearb.h:1261
fpreal64 fpreal
Definition: SYS_Types.h:283
LeafData & operator=(const LeafData &)=delete
bool specificFrame() const
TIL_CopHandle(const char *filename)
GLboolean r
Definition: glcorearb.h:1222
virtual bool returnRaster(const TIL_Raster *raster)
#define TIL_API
Definition: TIL_API.h:10
TIL_CopHandle & operator=(TIL_CopHandle &&ch)
GLuint * ids
Definition: glcorearb.h:652
TIL_CopHandle(TIL_CopHandle &&ch)