All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
task.h
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef HD_TASK_H
25 #define HD_TASK_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 
31 #include "pxr/imaging/hd/sceneDelegate.h"
32 
33 #include "pxr/usd/sdf/path.h"
34 #include "pxr/base/tf/hashmap.h"
35 #include "pxr/base/vt/value.h"
36 #include "pxr/base/vt/dictionary.h"
37 
38 #include <vector>
39 #include <unordered_map>
40 #include <boost/shared_ptr.hpp>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
44 
45 typedef boost::shared_ptr<class HdTask> HdTaskSharedPtr;
46 typedef std::vector<HdTaskSharedPtr> HdTaskSharedPtrVector;
47 
48 typedef boost::shared_ptr<class HdSceneTask> HdSceneTaskSharedPtr;
49 typedef std::vector<HdSceneTaskSharedPtr> HdSceneTaskSharedPtrVector;
50 
51 // We want to use token as a key not std::string, so use an unordered_map over
52 // VtDictionary
53 typedef std::unordered_map<TfToken, VtValue, TfToken::HashFunctor>
54  HdTaskContext;
55 
56 class HdTask {
57 public:
63  HD_API
64  HdTask(SdfPath const& id);
65 
66  HD_API
67  virtual ~HdTask();
68 
97 
98  virtual void Sync(HdSceneDelegate* delegate,
99  HdTaskContext* ctx,
100  HdDirtyBits* dirtyBits) = 0;
101 
121  virtual void Prepare(HdTaskContext* ctx,
122  HdRenderIndex* renderIndex) = 0;
123 
133  virtual void Execute(HdTaskContext* ctx) = 0;
134 
153  HD_API
154  virtual const TfTokenVector &GetRenderTags() const;
155 
156  SdfPath const& GetId() const { return _id; }
157 
161  HD_API
162  virtual HdDirtyBits GetInitialDirtyBitsMask() const;
163 
164 
165 protected:
173  template <class T>
174  static bool _GetTaskContextData(HdTaskContext const* ctx,
175  TfToken const& id,
176  T* outValue);
177 
185  template <class T>
186  bool _GetTaskParams(HdSceneDelegate* delegate,
187  T* outValue);
188 
189  HD_API
190  TfTokenVector _GetTaskRenderTags(HdSceneDelegate* delegate);
191 
192 private:
193  SdfPath _id;
194 
195  HdTask() = delete;
196  HdTask(const HdTask &) = delete;
197  HdTask &operator =(const HdTask &) = delete;
198 };
199 
200 // Inline template body
201 template <class T>
202 bool
203 HdTask::_GetTaskContextData(HdTaskContext const* ctx,
204  TfToken const& id,
205  T* outValue)
206 {
207  TF_DEV_AXIOM(outValue != nullptr);
208 
209  if (!ctx) {
210  return false;
211  }
212 
213  HdTaskContext::const_iterator valueIt = ctx->find(id);
214  if (valueIt == ctx->cend()) {
215  TF_CODING_ERROR("Token %s missing from task context", id.GetText());
216  return false;
217  }
218 
219  const VtValue &valueVt = (valueIt->second);
220  if (!valueVt.IsHolding<T>()) {
221  TF_CODING_ERROR("Token %s in task context is of mismatched type",
222  id.GetText());
223  return false;
224  }
225 
226  *outValue = valueVt.UncheckedGet<T>();
227 
228  return true;
229 }
230 
231 template <class T>
232 bool
233 HdTask::_GetTaskParams(HdSceneDelegate* delegate,
234  T* outValue)
235 {
236  TF_DEV_AXIOM(outValue != nullptr);
237 
238  SdfPath const& taskId = GetId();
239 
240  VtValue valueVt = delegate->Get(taskId, HdTokens->params);
241  if (!valueVt.IsHolding<T>()) {
242  TF_CODING_ERROR("Task params for %s is of unexpected type",
243  taskId.GetText());
244  return false;
245  }
246 
247  *outValue = valueVt.UncheckedGet<T>();
248 
249  return true;
250 }
251 
252 PXR_NAMESPACE_CLOSE_SCOPE
253 
254 #endif // HD_TASK_H
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:227
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:119
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:836
virtual HD_API VtValue Get(SdfPath const &id, TfToken const &key)
Returns a named value.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:87
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:808
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.