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 
119  // Get an TIL_Raster associated with the COP in the path specified
120  // YOU MUST CALL doneWithRaster or returnRaster when done!
121  // Do not free it, and do not dangle it.
122  static TIL_Raster *getRaster(const char *fullpath,
123  int &opid,
124  int udim_tile = 0,
125  bool *specific_frame = nullptr);
126  static fpreal getRasterTime(int opid);
127  static size_t getRasterVersion(int opid);
128 
129  static fpreal getTimeFromFrame(fpreal frame);
130 
131  /// Tell the resolver we're done with the raster.
132  /// Some resolvers will delete rasters, others will hold onto them. If the
133  /// raster actually dirties/deletes the raster, @c doneWithRaster() will
134  /// return true.
135  static bool doneWithRaster(const TIL_Raster *r);
136 
137  /// Returns shared IMX_Layer with COW semantics. Only works
138  /// for copernicus, will need getRaster for Images or COP2.
139  static UT_SharedPtr<const IMX_Layer> getLayer(
140  const char *fullpath,
141  int &opid,
142  int udim_tile = 0,
143  bool *specific_frame = nullptr);
144 
145  // Returns 1 if the frame is specified in the filename. The id will be -1
146  // if there is no such path.
147  static int getIdFrame(const char *fullpath, int &id,
148  fpreal &frame);
149  static void splitFile(const char *fullpath,
150  UT_String &name,
151  int &xres,int &yres,
152  float &quality);
153 
154  // These split net/node methods are now out of style with networks
155  // within networks. Now you should specify full, absolute, paths.
156  static int getNodeId(const char *net, const char *node);
157  static void getNodeRes(const char *net, const char *node,
158  int &xres, int &yres);
159 
160  static int getNodeId(const char *fullpath);
161  static void getNodeRes(const char *fullpath,
162  int &xres, int &yres);
163 
164  static void getFileRes(const char *file,
165  int &xres, int &yres);
166 
167  static int splitPath(const char *cpath, int &id,
168  fpreal &frame,
169  UT_WorkBuffer &color, int &cindex,
170  UT_WorkBuffer &alpha, int &aindex,
171  int &xres, int &yres);
172 
173  // builds menus for plane selection (COPs2 only).
174  static void buildColorMenu(const char *net,
175  const char *node,
176  UT_ValArray<char *> &items);
177 
178  static void buildAlphaMenu(const char *net,
179  const char *node,
180  UT_ValArray<char *> &items);
181 
182  static bool canResolve(const char *fullpath);
183 
184  // These methods should really only be used inside the TIL_CopResolver
185  // class. They shouldn't be called directly.
186  virtual int resolveId(const char *fullpath) = 0;
187 
188  virtual TIL_Raster *resolveRaster(int id) = 0;
189  virtual TIL_Raster *resolveRasterFrame(int id, fpreal frame,
190  int xres=0, int yres=0) = 0;
191  virtual TIL_Raster *resolveRasterPlane(int id, fpreal frame,
192  const char *color, int cindex,
193  const char *alpha, int aindex,
194  int xres=0, int yres = 0,
195  int udim_tile=0,
196  TIL_DataFormat f =
198 
199  virtual UT_SharedPtr<const IMX_Layer> resolveLayer(int id) = 0;
200  virtual UT_SharedPtr<const IMX_Layer> resolveLayerFrame(int id, fpreal frame) = 0;
201  virtual UT_SharedPtr<const IMX_Layer> resolveLayerPlane(int id,
202  fpreal frame,
203  const char *color,
204  int udim_tile) = 0;
205  // Returns whether or not the resolver knows how to handle this path.
206  virtual bool resolveIsPossible(const char *fullpath) = 0;
207 
208  /// You gain ownership of a FileRaster and must delete it:
209  virtual TIL_Raster *getFileRaster(const char *file,
211  int xres = 0, int yres = 0);
212  /// You do not gain ownershipo of a node raster, and must call
213  /// returnRaster or doneWithRaster on it.
214  /// @param fullpath The path to the COP node, eg "op:/path/to/cop"
215  /// @param cmenu The name of the color plane, eg "C" or
216  /// TIL_DEFAULT_COLOR_PLANE.
217  /// @param amenu The name of the alpha plane, eg "A" or
218  /// TIL_DEFAULT_ALPHA_PLANE, or TIL_NO_PLANE.
219  /// @param override_frame If true, the current time frame is used
220  /// for cooking the COP node to obtain the raster
221  /// (ie, the following 'frame' parameter is ignored).
222  /// @param frame Frame at which to cook the COP node to obtain the raster
223  /// @param f Desired data format (bit precision) of the returned
224  /// raster.
225  /// @param xres Width that the raster will be scaled to. If set to 0,
226  /// will use the node's default width.
227  /// @param yres Height that the raster will be scaled to. If set to 0,
228  /// will use the node's default height.
229  virtual TIL_Raster *getNodeRaster(const char *fullpath,
230  const char *cmenu, const char *amenu,
231  bool override_frame = false,
232  fpreal frame = 1.0,
233  TIL_DataFormat f =
235  int xres = 0, int yres = 0);
236 
237  virtual void getRes(int id, int &xres, int &yres);
238 
239  virtual TIL_Sequence *getSequence(int id);
240 
241  /// This cooks a bunch of planes simulataneously, and adds the created
242  /// rasters to 'new_rasters'. You are responsible for deleting them.
243  virtual bool resolveRasters(int id,
244  const UT_StringArray &plane_names,
245  fpreal t,
246  UT_ValArray<TIL_Raster *> &new_rasters,
248 
249 
250  virtual void buildPlaneMenu(const char *net, const char *node,
251  UT_ValArray<char *> &items);
252 
253  virtual TIL_ImageSource *resolveImageSource(int id);
254 
255  /// Caller is finished with the raster for now, so delete memory if
256  /// possible. Returns true if the raster is no longer valid, but false if
257  /// the raster hasn't been touched (and is still valid). This is
258  /// implementation dependent.
259  virtual bool returnRaster(const TIL_Raster *raster);
260 
261  // If the id returned is really an OP_Node, then this will return 1.
262  virtual int isOpBased();
263 
264  virtual fpreal getTimeForRaster(int id);
265  virtual size_t getVersionForRaster(int id);
266 
267  /// Apply the COP_Node unload prevention via the id.
268  /// Must be paired.
269  virtual void addAutoUnloadPrevention(int id);
270  virtual void removeAutoUnloadPrevention(int id);
271 
272 protected:
273  // Returns 1 if the frame is defined, 0 otherwise. It's possible that
274  // the name returned may be a reference to the path. Please harden if
275  // you need to keep it around.
276  int splitPath(const char *fullpath,
277  UT_String &name, fpreal &frame);
278 
279  virtual fpreal timeFromFrame(fpreal frame);
280  virtual fpreal getGlobalTime();
281  virtual void setGlobalTime(fpreal time);
282 };
283 
285 {
286 public:
287  TIL_CopHandle() = default;
288  TIL_CopHandle(const char *filename)
289  : myRaster(TIL_CopResolver::getRaster(filename, myOpId, 0, &mySpecificFrame))
290  {
291  }
292 
293  TIL_CopHandle(const TIL_CopHandle &) = delete;
294  TIL_CopHandle &operator=(const TIL_CopHandle &ch) = delete;
295 
297  {
298  moveFrom(ch);
299  }
301  {
302  moveFrom(ch);
303  return *this;
304  }
306  {
307  clear();
308  }
309 
310  bool isValid() const { return myRaster != nullptr; }
311  explicit operator bool() const { return isValid(); }
312  int opid() const { return myOpId; }
313  bool specificFrame() const { return mySpecificFrame; }
314  const TIL_Raster *raster() const { return myRaster; }
315  const TIL_Raster *operator->() const { return myRaster; }
316 
317  void clear()
318  {
319  if (myRaster)
321  }
322 
323  bool resolve(const char *filename)
324  {
325  clear();
326  myRaster = TIL_CopResolver::getRaster(filename, myOpId, 0, &mySpecificFrame);
327  return isValid();
328  }
329 
330 private:
331  void moveFrom(TIL_CopHandle &ch)
332  {
333  myRaster = ch.myRaster;
334  myOpId = ch.myOpId;
335  mySpecificFrame = ch.mySpecificFrame;
336  ch.myRaster = nullptr;
337  }
338 
339  const TIL_Raster *myRaster = nullptr;
340  int myOpId = -1;
341  bool mySpecificFrame = false;
342 };
343 
344 #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)