| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.1.1.1 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoCache base class (SoEXTENDER). |
| 47 |
| |
| 48 |
| Author(s) : Paul S. Strauss |
| 49 |
| |
| 50 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 |
_______________________________________________________________________ |
| 52 |
*/ |
| 53 |
|
| 54 |
#include <cstdlib> |
| 55 |
|
| 56 |
#include <Inventor/caches/SoCache.h> |
| 57 |
#include <Inventor/elements/SoElement.h> |
| 58 |
#include <Inventor/misc/SoState.h> |
| 59 |
|
| 60 |
#include <SoDebug.h> |
| 61 |
|
| 62 |
//////////////////////////////////////////////////////////////////////// |
| 63 |
// |
| 64 |
// Description: |
| 65 |
// Constructor. Takes the state in effect when the cache is |
| 66 |
// created; it is assumed that the state is pushed before the cache |
| 67 |
// is created. |
| 68 |
// |
| 69 |
// Use: public |
| 70 |
|
| 71 |
SoCache::SoCache(SoState *state) |
| 72 |
// |
| 73 |
//////////////////////////////////////////////////////////////////////// |
| 74 |
{ |
| 75 |
refCount = 0; |
| 76 |
invalidated = FALSE; |
| 77 |
|
| 78 |
depth = state->getDepth(); |
| 79 |
|
| 80 |
int bytesNeeded = (SoElement::getNumStackIndices()+7)/8; |
| 81 |
|
| 82 |
// Set the size of the elementsUsedFlags to be the maximum number |
| 83 |
// of elements currently enabled. This is safe because if more |
| 84 |
// elements are enabled later, there is no way this cache could |
| 85 |
// care about them-- if a node that used the new element was added |
| 86 |
// below this cache, it would cause notification and would cause |
| 87 |
// the cache to be blown. |
| 88 |
elementsUsedFlags = new unsigned char[bytesNeeded]; |
| 89 |
memset(elementsUsedFlags, 0, bytesNeeded); |
| 90 |
} |
| 91 |
|
| 92 |
//////////////////////////////////////////////////////////////////////// |
| 93 |
// |
| 94 |
// Description: |
| 95 |
// Destructor. Unreferences any elements this cache depends on. |
| 96 |
// |
| 97 |
// Use: private |
| 98 |
|
| 99 |
SoCache::~SoCache() |
| 100 |
// |
| 101 |
//////////////////////////////////////////////////////////////////////// |
| 102 |
{ |
| 103 |
int i; |
| 104 |
|
| 105 |
// Get rid of all the elements in list |
| 106 |
for (i = 0; i < elementsUsed.getLength(); i++) |
| 107 |
delete (SoElement *) elementsUsed[i]; |
| 108 |
|
| 109 |
delete[] elementsUsedFlags; |
| 110 |
} |
| 111 |
|
| 112 |
//////////////////////////////////////////////////////////////////////// |
| 113 |
// |
| 114 |
// Description: |
| 115 |
// Destroy this cache. Called by unref(); by default, just calls the |
| 116 |
// destructor. SoGLRenderCaches use this method to free up display |
| 117 |
// lists, if they can. |
| 118 |
// |
| 119 |
// Use: protected, virtual |
| 120 |
|
| 121 |
void |
| 122 |
SoCache::destroy(SoState *) |
| 123 |
// |
| 124 |
//////////////////////////////////////////////////////////////////////// |
| 125 |
{ |
| 126 |
delete this; |
| 127 |
} |
| 128 |
|
| 129 |
//////////////////////////////////////////////////////////////////////// |
| 130 |
// |
| 131 |
// Description: |
| 132 |
// Increments reference count. |
| 133 |
// |
| 134 |
// Use: public |
| 135 |
|
| 136 |
void |
| 137 |
SoCache::ref() |
| 138 |
// |
| 139 |
//////////////////////////////////////////////////////////////////////// |
| 140 |
{ |
| 141 |
refCount++; |
| 142 |
} |
| 143 |
|
| 144 |
//////////////////////////////////////////////////////////////////////// |
| 145 |
// |
| 146 |
// Description: |
| 147 |
// Decrements reference count, destroying instance if count is now 0. |
| 148 |
// |
| 149 |
// Use: public |
| 150 |
|
| 151 |
void |
| 152 |
SoCache::unref(SoState *state) |
| 153 |
// |
| 154 |
//////////////////////////////////////////////////////////////////////// |
| 155 |
{ |
| 156 |
if (--refCount == 0) |
| 157 |
destroy(state); |
| 158 |
} |
| 159 |
|
| 160 |
//////////////////////////////////////////////////////////////////////// |
| 161 |
// |
| 162 |
// Description: |
| 163 |
// Adds an element to elements-used list if not already there and |
| 164 |
// its depth is less than the depth of this cache |
| 165 |
// |
| 166 |
// Use: public |
| 167 |
|
| 168 |
void |
| 169 |
SoCache::addElement(const SoElement *elt) |
| 170 |
// |
| 171 |
//////////////////////////////////////////////////////////////////////// |
| 172 |
{ |
| 173 |
// See if element is already on list |
| 174 |
int byte = elt->getStackIndex() / 8; |
| 175 |
int bit = elt->getStackIndex() % 8; |
| 176 |
if (elt->getDepth() >= depth |
| 177 |
|| ((elementsUsedFlags[byte] >> bit) & 1)) |
| 178 |
return; |
| 179 |
|
| 180 |
// Make a copy we can later call matches() on: |
| 181 |
SoElement *newElt = elt->copyMatchInfo(); |
| 182 |
newElt->setDepth(elt->getDepth()); |
| 183 |
|
| 184 |
// Add it to the list |
| 185 |
elementsUsed.append((void *) newElt); |
| 186 |
elementsUsedFlags[byte] |= (1 << bit); |
| 187 |
} |
| 188 |
|
| 189 |
//////////////////////////////////////////////////////////////////////// |
| 190 |
// |
| 191 |
// Description: |
| 192 |
// Adds a dependency of this instance on another cache instance. |
| 193 |
// The default method makes sure this cache depends on any elements |
| 194 |
// that the sub-cache depends on. |
| 195 |
// |
| 196 |
// Use: public, virtual |
| 197 |
|
| 198 |
void |
| 199 |
SoCache::addCacheDependency(const SoState *state, SoCache *subCache) |
| 200 |
// |
| 201 |
//////////////////////////////////////////////////////////////////////// |
| 202 |
{ |
| 203 |
for (int i = 0; i < subCache->elementsUsed.getLength(); i++) { |
| 204 |
const SoElement *eltInCache = |
| 205 |
(const SoElement *) subCache->elementsUsed[i]; |
| 206 |
|
| 207 |
// |
| 208 |
// This is pretty subtle: the depth of the element in the |
| 209 |
// cache may not match the depth of the (matching) element in |
| 210 |
// the state (a cache may be built at a node that is instanced |
| 211 |
// at two different depths, for example). Because the depths |
| 212 |
// must be correct for the elementsUsed list to work |
| 213 |
// correctly, we must depend on the element in the state. |
| 214 |
// |
| 215 |
addElement(state->getConstElement(eltInCache->getStackIndex())); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
//////////////////////////////////////////////////////////////////////// |
| 220 |
// |
| 221 |
// Description: |
| 222 |
// Returns TRUE if the cache is valid with resepect to the given |
| 223 |
// state. To be valid, each element in the elements-used list in |
| 224 |
// the cache must match (using the matches() method) the |
| 225 |
// corresponding element in the state, AND the list of overridden |
| 226 |
// elements in the cache must be exactly the same as the |
| 227 |
// corresponding list in the state. |
| 228 |
// |
| 229 |
// Use: public |
| 230 |
|
| 231 |
SbBool |
| 232 |
SoCache::isValid(const SoState *state) const |
| 233 |
// |
| 234 |
//////////////////////////////////////////////////////////////////////// |
| 235 |
{ |
| 236 |
// If explicitly invalidated, always return FALSE. |
| 237 |
if (invalidated) { |
| 238 |
#ifdef DEBUG |
| 239 |
if (SoDebug::GetEnv("IV_DEBUG_CACHES")) { |
| 240 |
fprintf(stderr, "CACHE DEBUG: cache(%p) not valid ", |
| 241 |
this); |
| 242 |
fprintf(stderr, "because SoCache::invalidate was called, "); |
| 243 |
fprintf(stderr, "probably because a field or node\n"); |
| 244 |
fprintf(stderr, "underneath a node with a cache changed "); |
| 245 |
fprintf(stderr, "or because a node with a cache contained\n"); |
| 246 |
fprintf(stderr, "an uncacheable node.\n"); |
| 247 |
} |
| 248 |
#endif |
| 249 |
return FALSE; |
| 250 |
} |
| 251 |
|
| 252 |
int i; |
| 253 |
|
| 254 |
// Compare used elements for match |
| 255 |
for (i = 0; i < elementsUsed.getLength(); i++) { |
| 256 |
const SoElement *eltInCache = (const SoElement *) elementsUsed[i]; |
| 257 |
const SoElement *eltInState = |
| 258 |
state->getConstElement(eltInCache->getStackIndex()); |
| 259 |
|
| 260 |
// If cache's version of element doesn't match what's in the |
| 261 |
// state, the cache is not valid. |
| 262 |
if (!eltInCache->matches(eltInState)) { |
| 263 |
|
| 264 |
#ifdef DEBUG |
| 265 |
if (SoDebug::GetEnv("IV_DEBUG_CACHES")) { |
| 266 |
fprintf(stderr, "CACHE DEBUG: cache(%p) not valid", |
| 267 |
this); |
| 268 |
fprintf(stderr, " because element %s does not match:\n", |
| 269 |
eltInState->getTypeId().getName().getString()); |
| 270 |
fprintf(stderr, "------\nElement in state:\n"); |
| 271 |
eltInState->print(stderr); |
| 272 |
fprintf(stderr, "------\nElement in cache:\n"); |
| 273 |
eltInCache->print(stderr); |
| 274 |
} |
| 275 |
#endif |
| 276 |
return FALSE; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return TRUE; |
| 281 |
} |
| 282 |
|
| 283 |
//////////////////////////////////////////////////////////////////////// |
| 284 |
// |
| 285 |
// Description: |
| 286 |
// Returns the first element in the state that doesn't match an |
| 287 |
// element on the cache's dependency list. Returns NULL if they |
| 288 |
// all match. |
| 289 |
// |
| 290 |
// Use: public |
| 291 |
|
| 292 |
const SoElement * |
| 293 |
SoCache::getInvalidElement(const SoState *state) const |
| 294 |
// |
| 295 |
//////////////////////////////////////////////////////////////////////// |
| 296 |
{ |
| 297 |
int i; |
| 298 |
|
| 299 |
// Compare used elements for match |
| 300 |
for (i = 0; i < elementsUsed.getLength(); i++) { |
| 301 |
const SoElement *eltInCache = (const SoElement *) elementsUsed[i]; |
| 302 |
const SoElement *eltInState = |
| 303 |
state->getConstElement(eltInCache->getStackIndex()); |
| 304 |
|
| 305 |
if (eltInCache != eltInState && ! |
| 306 |
eltInCache->matches(eltInState)) { |
| 307 |
|
| 308 |
return eltInState; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return NULL; |
| 313 |
} |
| 314 |
|
| 315 |
//////////////////////////////////////////////////////////////////////// |
| 316 |
// |
| 317 |
// Description: |
| 318 |
// Invalidates this cache. Nodes sometime invalidate open caches. |
| 319 |
// |
| 320 |
// Use: public |
| 321 |
|
| 322 |
void |
| 323 |
SoCache::invalidate() |
| 324 |
// |
| 325 |
//////////////////////////////////////////////////////////////////////// |
| 326 |
{ |
| 327 |
invalidated = TRUE; |
| 328 |
} |
| 329 |
|