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 #include <boost/shared_ptr.hpp>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 
47 using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
48 using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
49 
50 // We want to use token as a key not std::string, so use an unordered_map over
51 // VtDictionary
52 using HdTaskContext =
53  std::unordered_map<TfToken, VtValue, TfToken::HashFunctor>;
54 
55 class HdTask {
56 public:
62  HD_API
63  HdTask(SdfPath const& id);
64 
65  HD_API
66  virtual ~HdTask();
67 
96 
97  virtual void Sync(HdSceneDelegate* delegate,
98  HdTaskContext* ctx,
99  HdDirtyBits* dirtyBits) = 0;
100 
120  virtual void Prepare(HdTaskContext* ctx,
121  HdRenderIndex* renderIndex) = 0;
122 
132  virtual void Execute(HdTaskContext* ctx) = 0;
133 
152  HD_API
153  virtual const TfTokenVector &GetRenderTags() const;
154 
155  SdfPath const& GetId() const { return _id; }
156 
160  HD_API
161  virtual HdDirtyBits GetInitialDirtyBitsMask() const;
162 
163 
164 protected:
172  template <class T>
173  static bool _GetTaskContextData(HdTaskContext const* ctx,
174  TfToken const& id,
175  T* outValue);
176 
184  template <class T>
185  bool _GetTaskParams(HdSceneDelegate* delegate,
186  T* outValue);
187 
188  HD_API
189  TfTokenVector _GetTaskRenderTags(HdSceneDelegate* delegate);
190 
193  template <class T>
194  static T _GetDriver(
195  HdTaskContext const* ctx,
196  TfToken const& driverName);
197 
198 private:
199  SdfPath _id;
200 
201  HdTask() = delete;
202  HdTask(const HdTask &) = delete;
203  HdTask &operator =(const HdTask &) = delete;
204 };
205 
206 // Inline template body
207 template <class T>
208 bool
209 HdTask::_GetTaskContextData(HdTaskContext const* ctx,
210  TfToken const& id,
211  T* outValue)
212 {
213  TF_DEV_AXIOM(outValue != nullptr);
214 
215  if (!ctx) {
216  return false;
217  }
218 
219  HdTaskContext::const_iterator valueIt = ctx->find(id);
220  if (valueIt == ctx->cend()) {
221  TF_CODING_ERROR("Token %s missing from task context", id.GetText());
222  return false;
223  }
224 
225  const VtValue &valueVt = (valueIt->second);
226  if (!valueVt.IsHolding<T>()) {
227  TF_CODING_ERROR("Token %s in task context is of mismatched type",
228  id.GetText());
229  return false;
230  }
231 
232  *outValue = valueVt.UncheckedGet<T>();
233 
234  return true;
235 }
236 
237 template <class T>
238 bool
239 HdTask::_GetTaskParams(HdSceneDelegate* delegate,
240  T* outValue)
241 {
242  TF_DEV_AXIOM(outValue != nullptr);
243 
244  SdfPath const& taskId = GetId();
245 
246  VtValue valueVt = delegate->Get(taskId, HdTokens->params);
247  if (!valueVt.IsHolding<T>()) {
248  TF_CODING_ERROR("Task params for %s is of unexpected type",
249  taskId.GetText());
250  return false;
251  }
252 
253  *outValue = valueVt.UncheckedGet<T>();
254 
255  return true;
256 }
257 
258 template <class T>
259 T
260 HdTask::_GetDriver(
261  HdTaskContext const* ctx,
262  TfToken const& driverName)
263 {
264  auto it = ctx->find(HdTokens->drivers);
265  if (it != ctx->end()) {
266  VtValue const& value = it->second;
267  if (value.IsHolding<HdDriverVector>()) {
268  HdDriverVector const& drivers= value.UncheckedGet<HdDriverVector>();
269  for (HdDriver* hdDriver : drivers) {
270  if (hdDriver->name == driverName) {
271  if (hdDriver->driver.IsHolding<T>()) {
272  return hdDriver->driver.UncheckedGet<T>();
273  }
274  }
275  }
276  }
277  }
278 
279  return nullptr;
280 }
281 
282 PXR_NAMESPACE_CLOSE_SCOPE
283 
284 #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:121
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1101
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:1073
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:431
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:174
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.