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 
25 #include "pxr/pxr.h"
26 #include "pxr/usd/pcp/layerPrefetchRequest.h"
27 #include "pxr/usd/pcp/layerStackRegistry.h"
28 #include "pxr/base/work/dispatcher.h"
29 #include "pxr/base/work/threadLimits.h"
30 #include "pxr/base/work/withScopedParallelism.h"
31 
32 #include <tbb/spin_mutex.h>
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 namespace {
37 
38 struct _Opener
39 {
_Opener__anon04aea4390111::_Opener40     explicit _Opener(const Pcp_MutedLayers& mutedLayers,
41                      std::set<SdfLayerRefPtr> *retainedLayers)
42         : _mutedLayers(mutedLayers)
43         , _retainedLayers(retainedLayers) {}
44 
~_Opener__anon04aea4390111::_Opener45     ~_Opener() { _dispatcher.Wait(); }
46 
OpenSublayers__anon04aea4390111::_Opener47     void OpenSublayers(const SdfLayerRefPtr &layer,
48                        const SdfLayer::FileFormatArguments &layerArgs) {
49         TF_FOR_ALL(path, layer->GetSubLayerPaths()) {
50             _dispatcher.Run(
51                 &_Opener::_OpenSublayer, this, *path, layer, layerArgs);
52         }
53     }
54 
55 private:
_OpenSublayer__anon04aea4390111::_Opener56     void _OpenSublayer(std::string path,
57                        const SdfLayerRefPtr &anchorLayer,
58                        const SdfLayer::FileFormatArguments &layerArgs) {
59         if (_mutedLayers.IsLayerMuted(anchorLayer, path)) {
60             return;
61         }
62 
63         // Open this specific sublayer path.
64         // The call to SdfLayer::FindOrOpenRelativeToLayer() may take some
65         // time, potentially multiple seconds.
66         if (SdfLayerRefPtr sublayer =
67             SdfLayer::FindOrOpenRelativeToLayer(anchorLayer, path, layerArgs)) {
68             // Retain this sublayer.
69             bool didInsert;
70             {
71                 tbb::spin_mutex::scoped_lock lock(_retainedLayersMutex);
72                 didInsert = _retainedLayers->insert(sublayer).second;
73             }
74             // Open the nested sublayers.  Only do this if we haven't seen this
75             // layer before, i.e. didInsert is true.
76             if (didInsert)
77                 OpenSublayers(sublayer, layerArgs);
78         }
79     }
80 
81     WorkDispatcher _dispatcher;
82     const Pcp_MutedLayers& _mutedLayers;
83     std::set<SdfLayerRefPtr> *_retainedLayers;
84     mutable tbb::spin_mutex _retainedLayersMutex;
85 };
86 
87 } // anon
88 
89 void
RequestSublayerStack(const SdfLayerRefPtr & layer,const SdfLayer::FileFormatArguments & args)90 PcpLayerPrefetchRequest::RequestSublayerStack(
91     const SdfLayerRefPtr &layer,
92     const SdfLayer::FileFormatArguments &args)
93 {
94     _sublayerRequests.insert(std::make_pair(layer, args));
95 }
96 
97 void
Run(const Pcp_MutedLayers & mutedLayers)98 PcpLayerPrefetchRequest::Run(const Pcp_MutedLayers& mutedLayers)
99 {
100     if (!WorkHasConcurrency()) {
101         // Do not bother pre-fetching if we do not have extra threads
102         // available.
103         return;
104     }
105 
106     // Release the GIL so we don't deadlock when Sd tries to get a path
107     // resolver (which does ref-counting on the resolver, which requires
108     // the GIL to manage TfRefBase identity-uniqueness).
109     TF_PY_ALLOW_THREADS_IN_SCOPE();
110 
111     std::set<_Request> requests;
112     requests.swap(_sublayerRequests);
113 
114     // Open all the sublayers in the request.
115     WorkWithScopedParallelism([&]() {
116             _Opener opener(mutedLayers, &_retainedLayers);
117             TF_FOR_ALL(req, requests)
118                 opener.OpenSublayers(req->first, req->second);
119         });
120 }
121 
122 PXR_NAMESPACE_CLOSE_SCOPE
123