Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright Contributors to the OpenVDB Project | ||
2 | // SPDX-License-Identifier: MPL-2.0 | ||
3 | |||
4 | /// @file points/AttributeArray.cc | ||
5 | |||
6 | #include "AttributeArray.h" | ||
7 | #include <map> | ||
8 | |||
9 | namespace openvdb { | ||
10 | OPENVDB_USE_VERSION_NAMESPACE | ||
11 | namespace OPENVDB_VERSION_NAME { | ||
12 | namespace points { | ||
13 | |||
14 | |||
15 | //////////////////////////////////////// | ||
16 | |||
17 | |||
18 | namespace { | ||
19 | |||
20 | using AttributeFactoryMap = std::map<NamePair, AttributeArray::FactoryMethod>; | ||
21 | |||
22 | struct LockedAttributeRegistry | ||
23 | { | ||
24 | tbb::spin_mutex mMutex; | ||
25 | AttributeFactoryMap mMap; | ||
26 | }; | ||
27 | |||
28 | // Global function for accessing the registry | ||
29 | LockedAttributeRegistry* | ||
30 | 191421 | getAttributeRegistry() | |
31 | { | ||
32 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 191419 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
191421 | static LockedAttributeRegistry registry; |
33 | 191421 | return ®istry; | |
34 | } | ||
35 | |||
36 | } // unnamed namespace | ||
37 | |||
38 | |||
39 | //////////////////////////////////////// | ||
40 | |||
41 | // AttributeArray::ScopedRegistryLock implementation | ||
42 | |||
43 | 11950 | AttributeArray::ScopedRegistryLock::ScopedRegistryLock() | |
44 | 11950 | : lock(getAttributeRegistry()->mMutex) | |
45 | { | ||
46 | 11950 | } | |
47 | |||
48 | |||
49 | //////////////////////////////////////// | ||
50 | |||
51 | // AttributeArray implementation | ||
52 | |||
53 | |||
54 | ✗ | AttributeArray::AttributeArray(const AttributeArray& rhs) | |
55 | ✗ | : AttributeArray(rhs, tbb::spin_mutex::scoped_lock(rhs.mMutex)) | |
56 | { | ||
57 | } | ||
58 | |||
59 | |||
60 | 11679 | AttributeArray::AttributeArray(const AttributeArray& rhs, const tbb::spin_mutex::scoped_lock&) | |
61 | 11679 | : mIsUniform(rhs.mIsUniform) | |
62 | 11679 | , mFlags(rhs.mFlags) | |
63 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11677 times.
|
11679 | , mUsePagedRead(rhs.mUsePagedRead) |
64 | , mOutOfCore(rhs.mOutOfCore.load()) | ||
65 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11677 times.
|
11679 | , mPageHandle() |
66 | { | ||
67 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11677 times.
|
11679 | if (mFlags & PARTIALREAD) mCompressedBytes = rhs.mCompressedBytes; |
68 |
3/4✓ Branch 0 taken 6266 times.
✓ Branch 1 taken 5411 times.
✓ Branch 3 taken 6266 times.
✗ Branch 4 not taken.
|
17943 | else if (rhs.mPageHandle) mPageHandle = rhs.mPageHandle->copy(); |
69 | 11679 | } | |
70 | |||
71 | |||
72 | AttributeArray& | ||
73 | ✗ | AttributeArray::operator=(const AttributeArray& rhs) | |
74 | { | ||
75 | // if this AttributeArray has been partially read, zero the compressed bytes, | ||
76 | // so the page handle won't attempt to clean up invalid memory | ||
77 | ✗ | if (mFlags & PARTIALREAD) mCompressedBytes = 0; | |
78 | ✗ | mIsUniform = rhs.mIsUniform; | |
79 | ✗ | mFlags = rhs.mFlags; | |
80 | ✗ | mUsePagedRead = rhs.mUsePagedRead; | |
81 | mOutOfCore.store(rhs.mOutOfCore); | ||
82 | ✗ | if (mFlags & PARTIALREAD) mCompressedBytes = rhs.mCompressedBytes; | |
83 | ✗ | else if (rhs.mPageHandle) mPageHandle = rhs.mPageHandle->copy(); | |
84 | else mPageHandle.reset(); | ||
85 | ✗ | return *this; | |
86 | } | ||
87 | |||
88 | |||
89 | AttributeArray::Ptr | ||
90 | 114572 | AttributeArray::create(const NamePair& type, Index length, Index stride, | |
91 | bool constantStride, const Metadata* metadata, const ScopedRegistryLock* lock) | ||
92 | { | ||
93 | 114572 | auto* registry = getAttributeRegistry(); | |
94 | tbb::spin_mutex::scoped_lock _lock; | ||
95 |
2/2✓ Branch 0 taken 39648 times.
✓ Branch 1 taken 74924 times.
|
114572 | if (!lock) _lock.acquire(registry->mMutex); |
96 | |||
97 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 114571 times.
|
114572 | auto iter = registry->mMap.find(type); |
98 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 114571 times.
|
114572 | if (iter == registry->mMap.end()) { |
99 |
1/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
4 | OPENVDB_THROW(LookupError, |
100 | "Cannot create attribute of unregistered type " << type.first << "_" << type.second); | ||
101 | } | ||
102 |
2/2✓ Branch 1 taken 114568 times.
✓ Branch 2 taken 3 times.
|
229139 | return (iter->second)(length, stride, constantStride, metadata); |
103 | } | ||
104 | |||
105 | |||
106 | bool | ||
107 | 55786 | AttributeArray::isRegistered(const NamePair& type, const ScopedRegistryLock* lock) | |
108 | { | ||
109 | 55786 | LockedAttributeRegistry* registry = getAttributeRegistry(); | |
110 | tbb::spin_mutex::scoped_lock _lock; | ||
111 |
1/2✓ Branch 0 taken 55786 times.
✗ Branch 1 not taken.
|
55786 | if (!lock) _lock.acquire(registry->mMutex); |
112 |
1/2✓ Branch 0 taken 55786 times.
✗ Branch 1 not taken.
|
55786 | return (registry->mMap.find(type) != registry->mMap.end()); |
113 | } | ||
114 | |||
115 | |||
116 | void | ||
117 | 368 | AttributeArray::clearRegistry(const ScopedRegistryLock* lock) | |
118 | { | ||
119 | 368 | LockedAttributeRegistry* registry = getAttributeRegistry(); | |
120 | tbb::spin_mutex::scoped_lock _lock; | ||
121 |
1/2✓ Branch 0 taken 368 times.
✗ Branch 1 not taken.
|
368 | if (!lock) _lock.acquire(registry->mMutex); |
122 | registry->mMap.clear(); | ||
123 | 368 | } | |
124 | |||
125 | |||
126 | void | ||
127 | 8745 | AttributeArray::registerType(const NamePair& type, FactoryMethod factory, const ScopedRegistryLock* lock) | |
128 | { | ||
129 | { // check the type of the AttributeArray generated by the factory method | ||
130 | 8745 | auto array = (*factory)(/*length=*/0, /*stride=*/0, /*constantStride=*/false, /*metadata=*/nullptr); | |
131 |
1/2✓ Branch 1 taken 8745 times.
✗ Branch 2 not taken.
|
8745 | const NamePair& factoryType = array->type(); |
132 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8744 times.
|
8745 | if (factoryType != type) { |
133 |
2/6✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
5 | OPENVDB_THROW(KeyError, "Attribute type " << type.first << "_" << type.second |
134 | << " does not match the type created by the factory method " | ||
135 | << factoryType.first << "_" << factoryType.second << "."); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | 8744 | LockedAttributeRegistry* registry = getAttributeRegistry(); | |
140 | tbb::spin_mutex::scoped_lock _lock; | ||
141 |
1/2✓ Branch 0 taken 8744 times.
✗ Branch 1 not taken.
|
8744 | if (!lock) _lock.acquire(registry->mMutex); |
142 | |||
143 |
2/4✓ Branch 1 taken 8744 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8744 times.
✗ Branch 4 not taken.
|
8744 | registry->mMap[type] = factory; |
144 | 8744 | } | |
145 | |||
146 | |||
147 | void | ||
148 | 1 | AttributeArray::unregisterType(const NamePair& type, const ScopedRegistryLock* lock) | |
149 | { | ||
150 | 1 | LockedAttributeRegistry* registry = getAttributeRegistry(); | |
151 | tbb::spin_mutex::scoped_lock _lock; | ||
152 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!lock) _lock.acquire(registry->mMutex); |
153 | |||
154 | registry->mMap.erase(type); | ||
155 | 1 | } | |
156 | |||
157 | |||
158 | void | ||
159 | 14 | AttributeArray::setTransient(bool state) | |
160 | { | ||
161 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
|
14 | if (state) mFlags = static_cast<uint8_t>(mFlags | Int16(TRANSIENT)); |
162 | 3 | else mFlags = static_cast<uint8_t>(mFlags & ~Int16(TRANSIENT)); | |
163 | 14 | } | |
164 | |||
165 | |||
166 | void | ||
167 | 14 | AttributeArray::setHidden(bool state) | |
168 | { | ||
169 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
|
14 | if (state) mFlags = static_cast<uint8_t>(mFlags | Int16(HIDDEN)); |
170 | 1 | else mFlags = static_cast<uint8_t>(mFlags & ~Int16(HIDDEN)); | |
171 | 14 | } | |
172 | |||
173 | |||
174 | void | ||
175 | 15647 | AttributeArray::setStreaming(bool state) | |
176 | { | ||
177 |
1/2✓ Branch 0 taken 15647 times.
✗ Branch 1 not taken.
|
15647 | if (state) mFlags = static_cast<uint8_t>(mFlags | Int16(STREAMING)); |
178 | ✗ | else mFlags = static_cast<uint8_t>(mFlags & ~Int16(STREAMING)); | |
179 | 15647 | } | |
180 | |||
181 | |||
182 | void | ||
183 | 123437 | AttributeArray::setConstantStride(bool state) | |
184 | { | ||
185 |
2/2✓ Branch 0 taken 114681 times.
✓ Branch 1 taken 8756 times.
|
123437 | if (state) mFlags = static_cast<uint8_t>(mFlags | Int16(CONSTANTSTRIDE)); |
186 | 8756 | else mFlags = static_cast<uint8_t>(mFlags & ~Int16(CONSTANTSTRIDE)); | |
187 | 123437 | } | |
188 | |||
189 | |||
190 | bool | ||
191 | 29 | AttributeArray::operator==(const AttributeArray& other) const | |
192 | { | ||
193 | 29 | this->loadData(); | |
194 | 29 | other.loadData(); | |
195 | |||
196 |
1/2✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 | if (this->mUsePagedRead != other.mUsePagedRead || |
197 | this->mFlags != other.mFlags) return false; | ||
198 | 29 | return this->isEqual(other); | |
199 | } | ||
200 | |||
201 | } // namespace points | ||
202 | } // namespace OPENVDB_VERSION_NAME | ||
203 | } // namespace openvdb | ||
204 |