00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef _SO_CACHE_ELEMENT_
00052 #define _SO_CACHE_ELEMENT_
00053
00054 #include <Inventor/SbBasic.h>
00055 #include <Inventor/elements/SoSubElement.h>
00056 #include <Inventor/threads/SbThreadLocalStorage.h>
00057 #include <Inventor/misc/SoRef.h>
00058 #include <Inventor/caches/SoCache.h>
00059 #include <Inventor/SoDB.h>
00060 #include <unordered_map>
00061 #include <bitset>
00062 #include <stack>
00063
00064 class SoCache;
00065
00077 SoEXTENDER_Documented class SoCacheElement : public SoElement {
00078
00079 SO_ELEMENT_HEADER(SoCacheElement);
00080
00081 public:
00085 static void set(SoState *state, SoCache *cache);
00086
00090 SoCache * getCache() const { return cache.ptr(); }
00091
00095 static SbBool anyOpen(SoState *state);
00096
00100 static void closeAnyOpen(SoState* state);
00101
00106 static void invalidate(SoState *state);
00107
00111 virtual void pop(SoState *state, const SoElement *prevTopElement);
00112
00116 SoCacheElement * getNextCacheElement() const
00117 { return (SoCacheElement *) getNextInStack(); }
00118
00120 virtual void push(SoState *state);
00121
00122 private:
00123
00125 virtual void init(SoState *state);
00126
00132 virtual SbBool matches(const SoElement* elt) const;
00133
00137 virtual SoElement* copyMatchInfo() const;
00138
00139 private:
00140
00141 static void initClass();
00142 static void exitClass();
00143
00144 typedef std::bitset<SO_ELEMENT_MAX_KEY> ElementBitList;
00145
00150 void invalidate() const;
00151
00155 static void pushElementSetters(SoState* state, SoElementKeyType keyToPush);
00156 static void popElementSetters(SoState* state, SoElementKeyType keyToPop);
00157
00159 static void addNodeTraversalStart(SoState* state);
00160
00162 static void trackDependencies(SoState *state, const SoElement *elt);
00163
00165 static void trackGetterDependencies(SoState *state, const SoElement *elt);
00166
00168 static void clearGettersSetters(SoState* state);
00169
00170
00171
00172 static void addElement(SoState *state, const SoElement *elt);
00173
00174
00175
00176 static void addCacheDependency(SoState *state, SoCache *cache);
00177
00178
00179
00180 static SbBool setInvalid(SbBool newValue);
00181
00182
00183
00184 static SoCache * getCurrentCache(SoState *state)
00185 {return state->getElementNoPush<SoCacheElement>()->cache.ptr();}
00186
00188 static void flagDelayedPath(SoState* state);
00189
00190 static SbBool listIsOpen(SoState* state);
00191
00196 SoRef<SoGroup> getDependencies(const ElementBitList& elementKeys, std::vector<SoNode*>& dependencies) const;
00197
00199 enum LazyEvaluationPolicy
00200 {
00201 DISABLED = (0),
00202 ENABLED = (1<<0),
00203 INHERITED = (1<<1)
00204 };
00205
00206 inline void setLazyEvaluationPolicy(int32_t flag)
00207 {
00208 m_lazyEvaluationPolicy = flag;
00209 if (!hasLazyEvaluation())
00210 m_lazyEvaluationPolicy = DISABLED;
00211 if (!isLazyEvaluationEnabled())
00212 invalidate();
00213 }
00214
00215 inline bool isLazyEvaluationEnabled() const
00216 {
00217 return (m_lazyEvaluationPolicy & ENABLED) > 0;
00218 }
00219
00220 inline bool isLazyEvaluationInherited() const
00221 {
00222 return (m_lazyEvaluationPolicy & INHERITED) > 0;
00223 }
00224
00230 bool needLazyEval() const;
00231
00235 static void setLazyEvaluationPolicy(SoState *state, int32_t lazyEvaluationPolicyFlag);
00236
00240 static void set(SoState *state, SoCache *cache, int32_t lazyEvaluationPolicyFlag);
00241
00243 static bool needTracking(SoState *state, const SoElement* elt);
00244
00246 static bool needTracking(SoState *state);
00247
00249 static bool hasLazyEvaluation();
00250
00252 struct ElementInfos
00253 {
00254 enum Type { PUSH, POP, ELEMENT, NODE_TRAVERSAL_START } ;
00255 SoElementKeyType elementKey;
00256 SoNode* node;
00257 Type type;
00258
00259 ElementInfos(){}
00260 ElementInfos(SoElementKeyType elementKey, SoNode* node, Type type)
00261 {
00262 this->elementKey = elementKey;
00263 this->node = node;
00264 this->type = type;
00265 }
00266 };
00267
00273 template<typename Data>
00274 struct ListContainer : public SoRefCounter
00275 {
00276 ListContainer()
00277 {}
00278
00279 void push()
00280 {
00281 m_lastSize.push( m_list.size() );
00282 }
00283
00284 void pop()
00285 {
00286 size_t lastSize = m_lastSize.top();
00287 m_list.resize( lastSize );
00288 m_lastSize.pop();
00289 }
00290
00291 std::stack<size_t> m_lastSize;
00292 std::vector<Data> m_list;
00293 };
00294
00296 typedef ListContainer<ElementInfos> ElementInfosListContainer;
00297
00298 typedef std::vector<ElementInfos> ElementInfosList;
00299
00301 const ElementInfosList& getElementSetters() const { return m_elementSetters->m_list; }
00302
00304 typedef ListContainer<SoNode*> LazyEvalNodesListContainer;
00305
00316 static const std::vector<uint64_t>& getLazyEvalNodesHashes( const SoState* state );
00317
00318 struct MTstruct
00319 {
00320 bool invalidated;
00321 };
00322
00323 SB_THREAD_TLS_HEADER();
00324
00325 private:
00326 virtual ~SoCacheElement();
00327
00328 private:
00329
00334 static SoNode* getSetterGetterNode(SoState *state, const SoElement *elt);
00335
00336 SoRef<SoCache> cache;
00337
00340 SoRef<ElementInfosListContainer> m_elementSetters;
00341
00347 std::vector<SoRef<LazyEvalNodesListContainer> > m_lazyEvalNodesLists;
00348
00350 std::vector<uint64_t> m_lazyEvalNodesHashes;
00351
00356 int32_t m_lazyEvaluationPolicy;
00357
00358 friend class SoElement;
00359 };
00360
00361 #endif
00362
00363
00364