Debian Codegraph @

lib/database/src/so/caches/SoGLRenderCache.c++

raw
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 | SoGLRenderCache
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 <Inventor/actions/SoGLRenderAction.h>
55 #include <Inventor/caches/SoGLRenderCache.h>
56 #include <Inventor/elements/SoCacheElement.h>
57 #include <Inventor/elements/SoGLCacheContextElement.h>
58 #include <Inventor/errors/SoDebugError.h>
59 #include <SoDebug.h>
60
61
62 ////////////////////////////////////////////////////////////////////////
63 //
64 // Description:
65 //
66 // Constructor
67 //
68 // Use: public
69
70 SoGLRenderCache::SoGLRenderCache(SoState *state) : SoCache(state)
71 //
72 ////////////////////////////////////////////////////////////////////////
73 {
74 // We haven't compiled a list yet
75 list = NULL;
76 listOpen = FALSE;
77 saveState = NULL;
78 GLCacheLazyElement = NULL;
79 checkGLFlag = 0;
80 checkIVFlag = 0;
81 doSendFlag = 0;
82 }
83
84 ////////////////////////////////////////////////////////////////////////
85 //
86 // Description:
87 // Destructor
88 //
89 // Use: public
90
91 SoGLRenderCache::~SoGLRenderCache()
92 //
93 ////////////////////////////////////////////////////////////////////////
94 {
95 // Unref all of the nested caches
96 for (int i = 0; i < nestedCaches.getLength(); i++)
97 ((SoGLDisplayList *) nestedCaches[i])->unref();
98
99 // delete the GLLazyElement
100 if (GLCacheLazyElement != NULL)
101 delete GLCacheLazyElement;
102 }
103
104 ///////////////////////////////////////////////////////////////////////
105 //
106 // Description:
107 // Returns TRUE if cache is valid with respect to the given state
108 //
109 // Use: public, virtual
110
111 SbBool
112 SoGLRenderCache::isValid(const SoState *state) const
113 {
114 if (!SoCache::isValid(state)) return(FALSE);
115 // get the lazy element from the state:
116 SoGLLazyElement *eltInState = SoGLLazyElement::getInstance(state);
117
118 // Send the components of the lazy element requiring IV=GL,
119 // do this prior to checking GL matches and IV matches.
120 if (doSendFlag) eltInState->send(state, doSendFlag);
121
122 // If cache's version of lazy element doesn't match what's in the
123 // state, the cache is not valid.
124
125 if (!GLCacheLazyElement->
126 lazyMatches(checkGLFlag,checkIVFlag,eltInState)){
127 #ifdef DEBUG
128 if (SoDebug::GetEnv("IV_DEBUG_CACHES")) {
129 fprintf(stderr, "CACHE DEBUG: cache(%p) not valid ",
130 this);
131 fprintf(stderr, "Because a lazy element match failed,\n");
132 fprintf(stderr, "GL and IV flags were %d %d\n", checkGLFlag,
133 checkIVFlag);
134 }
135 #endif /*DEBUG*/
136 return(FALSE);
137 }
138
139 return(TRUE);
140 }
141
142 ////////////////////////////////////////////////////////////////////////
143 //
144 // Description:
145 // Destroy this cache. Called by unref(); frees up OpenGL display
146 // list.
147 //
148 // Use: protected, virtual
149
150 void
151 SoGLRenderCache::destroy(SoState *state)
152 //
153 ////////////////////////////////////////////////////////////////////////
154 {
155 if (listOpen) close();
156
157 // Let the GLCacheContext element know that this display list can
158 // be freed when the cache context is valid (this destructor may
159 // be called when it is illegal to issue any GL commands!)
160 if (list) {
161 list->unref();
162 list = NULL;
163 }
164
165 delete GLCacheLazyElement;
166 GLCacheLazyElement = NULL;
167 SoCache::destroy(state);
168 }
169
170 ////////////////////////////////////////////////////////////////////////
171 //
172 // Description:
173 // Adds a dependency of this instance on another cache instance.
174 //
175 // Use: public, virtual
176
177 void
178 SoGLRenderCache::addNestedCache(SoGLDisplayList *child)
179 //
180 ////////////////////////////////////////////////////////////////////////
181 {
182 // Add the cache to the list of nested caches
183 nestedCaches.append(child);
184
185 // Increment the reference count on the nested cache
186 child->ref();
187 }
188
189 ////////////////////////////////////////////////////////////////////////
190 //
191 // Description:
192 // Opens the cache. All GL commands made until the next close()
193 // will be part of the the display list cache.
194 //
195 // Use: public
196
197 void
198 SoGLRenderCache::open(SoState *state)
199 //
200 ////////////////////////////////////////////////////////////////////////
201 {
202 #ifdef DEBUG
203 // if there is already an open GLRender cache, bail.
204 if (SoCacheElement::anyOpen(state) || listOpen) {
205 SoDebugError::post("SoGLRenderCache::open",
206 "A cache is already open!");
207 return;
208 }
209 #endif /* DEBUG */
210
211 listOpen = TRUE;
212
213 // We are now the currently open cache...
214 SoCacheElement::set(state, this);
215
216 // save state for comparison later:
217 saveState = state;
218
219 // set up the GLLazyElement:
220 #ifdef DEBUG
221 if (GLCacheLazyElement != NULL)
222 SoDebugError::post("SoGLRenderCache::open",
223 "Error reallocating CacheLazyElement");
224 #endif /*DEBUG*/
225
226 //Get the top Lazy Element, and have it make a copy for the cache:
227 SoGLLazyElement *le = SoGLLazyElement::getInstance(state);
228 GLCacheLazyElement = le->copyLazyMatchInfo(state);
229
230 //initialize flags:
231 checkGLFlag = 0;
232 checkIVFlag = 0;
233 doSendFlag = 0;
234
235 // Create a new display list
236 list = new SoGLDisplayList(state, SoGLDisplayList::DISPLAY_LIST, 1);
237 list->ref();
238 list->open(state);
239 }
240
241 ////////////////////////////////////////////////////////////////////////
242 //
243 // Description:
244 // Closes the cache.
245 //
246 // Use: public
247
248 void
249 SoGLRenderCache::close()
250 //
251 ////////////////////////////////////////////////////////////////////////
252 {
253 #ifdef DEBUG
254 if (! listOpen) {
255 SoDebugError::post("SoGLRenderCache::close",
256 "No cache is currently open!");
257 return;
258 }
259
260 #endif /* DEBUG */
261
262 listOpen = FALSE;
263
264 // copy final GLState into CacheLazyElement
265 // for anything in lazy element that was sent
266
267 SoGLLazyElement* le = (SoGLLazyElement*)
268 saveState->getConstElement(SoGLLazyElement::getClassStackIndex());
269 le->getCopyGL(GLCacheLazyElement, cachedGLState);
270
271 list->close(saveState);
272 }
273
274 ////////////////////////////////////////////////////////////////////////
275 //
276 // Description:
277 // Calls the cache. The display list will be sent to GL.
278 //
279 // Use: public
280
281 void
282 SoGLRenderCache::call(SoState *state)
283 //
284 ////////////////////////////////////////////////////////////////////////
285 {
286
287 #ifdef DEBUG
288 if (! list) {
289 SoDebugError::post("SoGLRenderCache::call",
290 "Cache was never compiled!");
291 return;
292 }
293 #endif /* DEBUG */
294
295 // Make sure all open caches depend on us
296 SoCacheElement::addCacheDependency(state, this);
297
298 list->call(state);
299
300 // get the current lazy element from the state
301 SoGLLazyElement* currentLazyElt = SoGLLazyElement::getInstance(state);
302
303 //If this cache call occurred within a cache, must pass info to
304 //parent cache.
305 if (state->isCacheOpen()){
306 SoGLRenderCache* parentCache = (SoGLRenderCache*)
307 SoCacheElement::getCurrentCache(state);
308 SoGLRenderCache* childCache = this;
309
310 currentLazyElt->mergeCacheInfo(childCache, parentCache, doSendFlag,
311 checkIVFlag, checkGLFlag);
312 }
313
314 // copy back the CacheLazyElement's GL State
315 // also set GLSendBits and invalidBits.
316 currentLazyElt->copyBackGL(GLCacheLazyElement, cachedGLState);
317 }