Open Inventor Release 2024.1.3
 
Loading...
Searching...
No Matches
SoCacheElement.h
1/*=======================================================================
2 * Copyright 1991-1996, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 *
5 * UNPUBLISHED -- Rights reserved under the copyright laws of the United
6 * States. Use of a copyright notice is precautionary only and does not
7 * imply publication or disclosure.
8 *
9 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
10 * Use, duplication or disclosure by the Government is subject to restrictions
11 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
12 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
13 * in similar or successor clauses in the FAR, or the DOD or NASA FAR
14 * Supplement. Contractor/manufacturer is Silicon Graphics, Inc.,
15 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
16 *
17 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
18 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
19 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
20 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
21 * GRAPHICS, INC.
22**=======================================================================*/
23/*=======================================================================
24** Author : Paul S. Strauss (MMM yyyy)
25** Modified by : Gavin Bell (MMM yyyy)
26**=======================================================================*/
27/*=======================================================================
28 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
29 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
30 *** ***
31 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
32 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
33 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
34 *** ***
35 *** RESTRICTED RIGHTS LEGEND ***
36 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
37 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
38 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
39 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
40 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
41 *** ***
42 *** COPYRIGHT (C) 1996-2020 BY FEI S.A.S, ***
43 *** BORDEAUX, FRANCE ***
44 *** ALL RIGHTS RESERVED ***
45**=======================================================================*/
46/*=======================================================================
47** Modified by : VSG (MMM YYYY)
48**=======================================================================*/
49
50
51#ifndef _SO_CACHE_ELEMENT_
52#define _SO_CACHE_ELEMENT_
53
54#include <Inventor/SbBasic.h>
55#include <Inventor/elements/SoSubElement.h>
56#include <Inventor/threads/SbThreadLocalStorage.h>
57#include <Inventor/misc/SoRef.h>
58#include <Inventor/caches/SoCache.h>
59#include <Inventor/SoDB.h>
60#include <unordered_map>
61#include <bitset>
62#include <stack>
63
64class SoCache;
65
77SoEXTENDER_Documented class SoCacheElement : public SoElement {
78
79 SO_ELEMENT_HEADER(SoCacheElement);
80
81 public:
85 static void set(SoState *state, SoCache *cache);
86
90 SoCache * getCache() const { return cache.ptr(); }
91
95 static SbBool anyOpen(SoState *state);
96
100 static void closeAnyOpen(SoState* state);
101
106 static void invalidate(SoState *state);
107
111 virtual void pop(SoState *state, const SoElement *prevTopElement);
112
117 { return (SoCacheElement *) getNextInStack(); }
118
120 virtual void push(SoState *state);
121
122private:
123
125 virtual void init(SoState *state);
126
132 virtual SbBool matches(const SoElement* elt) const;
133
137 virtual SoElement* copyMatchInfo() const;
138
139 private:
140 // Initializes the SoCacheElement class
141 static void initClass();
142 static void exitClass();
143
144 typedef std::bitset<SO_ELEMENT_MAX_KEY> ElementBitList;
145
150 void invalidate() const;
151
155 static void pushElementSetters(SoState* state, SoElementKeyType keyToPush);
156 static void popElementSetters(SoState* state, SoElementKeyType keyToPop);
157
159 static void addNodeTraversalStart(SoState* state);
160
162 static void trackDependencies(SoState *state, const SoElement *elt);
163
165 static void trackGetterDependencies(SoState *state, const SoElement *elt);
166
168 static void clearGettersSetters(SoState* state);
169
170 // Adds the given element to the elements used lists of all
171 // currently open caches in the state
172 static void addElement(SoState *state, const SoElement *elt);
173
174 // Adds a dependency on the given cache to all currently open
175 // caches in the state
176 static void addCacheDependency(SoState *state, SoCache *cache);
177
178 // Sets invalidated bit, and returns its old value. Used by
179 // SoGLCacheList so auto-caching doesn't cache too much.
180 static SbBool setInvalid(SbBool newValue);
181
182 // returns the current cache, from the top of the stack. Does not
183 // cause a cache dependence like getConstElement().
184 static SoCache * getCurrentCache(SoState *state)
185 {return state->getElementNoPush<SoCacheElement>()->cache.ptr();}
186
188 static void flagDelayedPath(SoState* state);
189
190 static SbBool listIsOpen(SoState* state);
191
196 SoRef<SoGroup> getDependencies(const ElementBitList& elementKeys, std::vector<SoNode*>& dependencies) const;
197
199 enum LazyEvaluationPolicy
200 {
201 DISABLED = (0), // Lazy evaluation is disabled
202 ENABLED = (1<<0), // Lazy evaluation is enabled
203 INHERITED = (1<<1) // Lazy evaluation policy is propagated to children
204 };
205
206 inline void setLazyEvaluationPolicy(int32_t flag)
207 {
208 m_lazyEvaluationPolicy = flag;
209 if (!hasLazyEvaluation())
210 m_lazyEvaluationPolicy = DISABLED;
211 if (!isLazyEvaluationEnabled())
212 invalidate();
213 }
214
215 inline bool isLazyEvaluationEnabled() const
216 {
217 return (m_lazyEvaluationPolicy & ENABLED) > 0;
218 }
219
220 inline bool isLazyEvaluationInherited() const
221 {
222 return (m_lazyEvaluationPolicy & INHERITED) > 0;
223 }
224
230 bool needLazyEval() const;
231
235 static void setLazyEvaluationPolicy(SoState *state, int32_t lazyEvaluationPolicyFlag);
236
240 static void set(SoState *state, SoCache *cache, int32_t lazyEvaluationPolicyFlag);
241
243 static bool needTracking(SoState *state, const SoElement* elt);
244
246 static bool needTracking(SoState *state);
247
249 static bool hasLazyEvaluation();
250
252 struct ElementInfos
253 {
254 enum Type { PUSH, POP, ELEMENT, NODE_TRAVERSAL_START } ;
255 SoElementKeyType elementKey;
256 SoNode* node;
257 Type type;
258
259 ElementInfos(){}
260 ElementInfos(SoElementKeyType elementKey, SoNode* node, Type type)
261 {
262 this->elementKey = elementKey;
263 this->node = node;
264 this->type = type;
265 }
266 };
267
273 template<typename Data>
274 struct ListContainer : public SoRefCounter
275 {
276 ListContainer()
277 {}
278
279 void push()
280 {
281 m_lastSize.push( m_list.size() );
282 }
283
284 void pop()
285 {
286 size_t lastSize = m_lastSize.top();
287 m_list.resize( lastSize );
288 m_lastSize.pop();
289 }
290
291 std::stack<size_t> m_lastSize;
292 std::vector<Data> m_list;
293 };
294
296 typedef ListContainer<ElementInfos> ElementInfosListContainer;
297
298 typedef std::vector<ElementInfos> ElementInfosList;
299
301 const ElementInfosList& getElementSetters() const { return m_elementSetters->m_list; }
302
304 typedef ListContainer<SoNode*> LazyEvalNodesListContainer;
305
316 static const std::vector<uint64_t>& getLazyEvalNodesHashes( const SoState* state );
317
318 struct MTstruct
319 {
320 bool invalidated;
321 };
322
323 SB_THREAD_TLS_HEADER();
324
325 private:
326 virtual ~SoCacheElement();
327
328 private:
329
334 static SoNode* getSetterGetterNode(SoState *state, const SoElement *elt);
335
336 SoRef<SoCache> cache; // Stores pointer to cache
337
340 SoRef<ElementInfosListContainer> m_elementSetters;
341
347 std::vector<SoRef<LazyEvalNodesListContainer> > m_lazyEvalNodesLists;
348
350 std::vector<uint64_t> m_lazyEvalNodesHashes;
351
356 int32_t m_lazyEvaluationPolicy;
357
358 friend class SoElement;
359};
360
361#endif /* _SO_CACHE_ELEMENT_ */
362
363
Stores the most recently opened cache.
static void invalidate(SoState *state)
Invalidate any open caches.
SoCacheElement * getNextCacheElement() const
Returns the next cache element in the stack.
virtual void pop(SoState *state, const SoElement *prevTopElement)
Overrides this method to unref cache.
static SbBool anyOpen(SoState *state)
Returns TRUE if any cache is currently open in the state.
static void set(SoState *state, SoCache *cache)
Sets cache in element accessed from state.
virtual void push(SoState *state)
push to save dependencies list
SoCache * getCache() const
Returns the cache stored in an instance.
static void closeAnyOpen(SoState *state)
Closes any open cache.
Abstract base class for all state elements.
Definition SoElement.h:102
friend class SoCache
Definition SoElement.h:407
Abstract base class for all database nodes.
Definition SoNode.h:145
Base class for ref-counted objects.
Smart pointer for any class inheriting SoRefCounter.
Definition SoRef.h:90
T * ptr() const
Cast to C pointer.
Definition SoRef.h:167
Traversal state.
Definition SoState.h:74
int SbBool
Boolean type.
Definition SbBase.h:87