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 PXR_IMAGING_HD_TASK_H
25 #define PXR_IMAGING_HD_TASK_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/driver.h"
30 #include "pxr/imaging/hd/version.h"
31 
32 #include "pxr/imaging/hd/sceneDelegate.h"
33 
34 #include "pxr/usd/sdf/path.h"
35 #include "pxr/base/tf/hashmap.h"
36 #include "pxr/base/vt/value.h"
37 #include "pxr/base/vt/dictionary.h"
38 
39 #include <memory>
40 #include <vector>
41 #include <unordered_map>
42 
43 PXR_NAMESPACE_OPEN_SCOPE
44 
45 
46 using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
47 using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
48 
49 // We want to use token as a key not std::string, so use an unordered_map over
50 // VtDictionary
51 using HdTaskContext =
52  std::unordered_map<TfToken, VtValue, TfToken::HashFunctor>;
53 
54 class HdTask {
55 public:
61  HD_API
62  HdTask(SdfPath const& id);
63 
64  HD_API
65  virtual ~HdTask();
66 
95 
96  virtual void Sync(HdSceneDelegate* delegate,
97  HdTaskContext* ctx,
98  HdDirtyBits* dirtyBits) = 0;
99 
119  virtual void Prepare(HdTaskContext* ctx,
120  HdRenderIndex* renderIndex) = 0;
121 
131  virtual void Execute(HdTaskContext* ctx) = 0;
132 
151  HD_API
152  virtual const TfTokenVector &GetRenderTags() const;
153 
154  SdfPath const& GetId() const { return _id; }
155 
159  HD_API
160  virtual HdDirtyBits GetInitialDirtyBitsMask() const;
161 
162 
163 protected:
165  HD_API
166  static bool _HasTaskContextData(HdTaskContext const* ctx,
167  TfToken const& id);
168 
176  template <class T>
177  static bool _GetTaskContextData(HdTaskContext const* ctx,
178  TfToken const& id,
179  T* outValue);
180 
188  template <class T>
189  bool _GetTaskParams(HdSceneDelegate* delegate,
190  T* outValue);
191 
192  HD_API
193  TfTokenVector _GetTaskRenderTags(HdSceneDelegate* delegate);
194 
197  template <class T>
198  static T _GetDriver(
199  HdTaskContext const* ctx,
200  TfToken const& driverName);
201 
202 private:
203  SdfPath _id;
204 
205  HdTask() = delete;
206  HdTask(const HdTask &) = delete;
207  HdTask &operator =(const HdTask &) = delete;
208 };
209 
210 // Inline template body
211 template <class T>
212 bool
213 HdTask::_GetTaskContextData(HdTaskContext const* ctx,
214  TfToken const& id,
215  T* outValue)
216 {
217  TF_DEV_AXIOM(outValue != nullptr);
218 
219  if (!ctx) {
220  return false;
221  }
222 
223  HdTaskContext::const_iterator valueIt = ctx->find(id);
224  if (valueIt == ctx->cend()) {
225  TF_CODING_ERROR("Token %s missing from task context", id.GetText());
226  return false;
227  }
228 
229  const VtValue &valueVt = (valueIt->second);
230  if (!valueVt.IsHolding<T>()) {
231  TF_CODING_ERROR("Token %s in task context is of mismatched type",
232  id.GetText());
233  return false;
234  }
235 
236  *outValue = valueVt.UncheckedGet<T>();
237 
238  return true;
239 }
240 
241 template <class T>
242 bool
243 HdTask::_GetTaskParams(HdSceneDelegate* delegate,
244  T* outValue)
245 {
246  TF_DEV_AXIOM(outValue != nullptr);
247 
248  SdfPath const& taskId = GetId();
249 
250  VtValue valueVt = delegate->Get(taskId, HdTokens->params);
251  if (!valueVt.IsHolding<T>()) {
252  TF_CODING_ERROR("Task params for %s is of unexpected type",
253  taskId.GetText());
254  return false;
255  }
256 
257  *outValue = valueVt.UncheckedGet<T>();
258 
259  return true;
260 }
261 
262 template <class T>
263 T
264 HdTask::_GetDriver(
265  HdTaskContext const* ctx,
266  TfToken const& driverName)
267 {
268  auto it = ctx->find(HdTokens->drivers);
269  if (it != ctx->end()) {
270  VtValue const& value = it->second;
271  if (value.IsHolding<HdDriverVector>()) {
272  HdDriverVector const& drivers= value.UncheckedGet<HdDriverVector>();
273  for (HdDriver* hdDriver : drivers) {
274  if (hdDriver->name == driverName) {
275  if (hdDriver->driver.IsHolding<T>()) {
276  return hdDriver->driver.UncheckedGet<T>();
277  }
278  }
279  }
280  }
281  }
282 
283  return nullptr;
284 }
285 
286 PXR_NAMESPACE_CLOSE_SCOPE
287 
288 #endif // PXR_IMAGING_HD_TASK_H
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:222
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:116
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1090
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:85
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1062
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HdDriver represents a device object, commonly a render device, that is owned by the application and p...
Definition: driver.h:40
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:168
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.