~ubuntu-branches/ubuntu/trusty/openscenegraph/trusty

« back to all changes in this revision

Viewing changes to OpenSceneGraph/src/osgDB/SharedStateManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-07-29 04:34:38 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20080729043438-no1h9h0dpsrlzp1y
* Non-maintainer upload.
* No longer try to detect (using /proc/cpuinfo when available) how many
  CPUs are available, fixing the FTBFS (due to -j0) on various platforms
  (Closes: #477353). The right way to do it is to support parallel=n in
  DEB_BUILD_OPTIONS (see Debian Policy §4.9.1), and adequate support has
  been implemented.
* Add patch to fix FTBFS due to the build system now refusing to handle
  whitespaces (Policy CMP0004 say the logs), thanks to Andreas Putzo who
  provided it (Closes: #482239):
   - debian/patches/fix-cmp0004-build-failure.dpatch
* Remove myself from Uploaders, as requested a while ago, done by Luk in
  his 2.2.0-2.1 NMU, which was never acknowledged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
SharedStateManager::SharedStateManager():
20
20
    osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) 
21
21
{
22
 
    shareMode = SHARE_TEXTURES;
23
 
    //shareMode = SHARE_STATESETS;
24
 
    mutex=0;
 
22
    _shareMode = SHARE_TEXTURES;
 
23
    //_shareMode = SHARE_STATESETS;
 
24
    _mutex=0;
25
25
}
26
26
 
27
27
//----------------------------------------------------------------
29
29
//----------------------------------------------------------------
30
30
void SharedStateManager::prune()
31
31
{
32
 
    StateSetSet::iterator sitr, sitr1;
33
 
    for(sitr=_sharedStateSetList.begin(); sitr!=_sharedStateSetList.end(); sitr=sitr1)
 
32
    StateSetSet::iterator sitr;
 
33
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
 
34
    for(sitr=_sharedStateSetList.begin(); sitr!=_sharedStateSetList.end();)
34
35
    {
35
 
        sitr1=sitr; ++sitr1;
36
 
        if((*sitr)->referenceCount()<=1) _sharedStateSetList.erase(sitr);
 
36
        if ((*sitr)->referenceCount()<=1)
 
37
            _sharedStateSetList.erase(sitr++);
 
38
        else
 
39
            ++sitr;
37
40
    }
38
41
 
39
 
    TextureSet::iterator titr, titr1;
40
 
    for(titr=_sharedTextureList.begin(); titr!=_sharedTextureList.end(); titr=titr1)
 
42
    TextureSet::iterator titr;
 
43
    for(titr=_sharedTextureList.begin(); titr!=_sharedTextureList.end();)
41
44
    {
42
 
        titr1=titr; ++titr1;
43
 
        if((*titr)->referenceCount()<=1) _sharedTextureList.erase(titr);
 
45
        if ((*titr)->referenceCount()<=1)
 
46
            _sharedTextureList.erase(titr++);
 
47
        else
 
48
            ++titr;
44
49
    }
45
50
 
46
51
54
59
//    const osg::Timer& timer = *osg::Timer::instance();
55
60
//    osg::Timer_t start_tick = timer.tick();
56
61
    
57
 
    mutex = mt;
 
62
    _mutex = mt;
58
63
    apply(*node);
59
64
    tmpSharedTextureList.clear();
60
65
    tmpSharedStateSetList.clear();
61
 
    mutex = 0;
 
66
    _mutex = 0;
62
67
 
63
68
//    osg::Timer_t end_tick = timer.tick();
64
69
//     std::cout << "SHARING TIME = "<<timer.delta_m(start_tick,end_tick)<<"ms"<<std::endl;
91
96
    }
92
97
}
93
98
 
 
99
bool SharedStateManager::isShared(osg::StateSet *ss)
 
100
{
 
101
    if ((_shareMode & SHARE_STATESETS) != 0)
 
102
    {
 
103
        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
 
104
        return find(ss) != 0;
 
105
    }
 
106
    else
 
107
        return false;
 
108
}
94
109
 
95
110
//----------------------------------------------------------------
96
111
// SharedStateManager::find
97
112
//----------------------------------------------------------------
 
113
//
 
114
// The find methods don't need to lock _listMutex because the thread
 
115
// from which they are called is doing the writing to the lists.
98
116
osg::StateSet *SharedStateManager::find(osg::StateSet *ss)
99
117
{
100
 
    for(StateSetSet::iterator itr=_sharedStateSetList.begin(); 
101
 
        itr!=_sharedStateSetList.end(); 
102
 
        ++itr)
103
 
    {
104
 
        if(ss->compare(*(itr->get()),true)==0) 
105
 
            return (osg::StateSet *)itr->get();
106
 
    }
107
 
    return NULL;
 
118
    StateSetSet::iterator result
 
119
        = _sharedStateSetList.find(osg::ref_ptr<osg::StateSet>(ss));
 
120
    if (result == _sharedStateSetList.end())
 
121
        return NULL;
 
122
    else
 
123
        return result->get();
108
124
}
109
125
osg::StateAttribute *SharedStateManager::find(osg::StateAttribute *sa)
110
126
{
111
 
    for(TextureSet::iterator itr=_sharedTextureList.begin(); 
112
 
    itr!=_sharedTextureList.end(); 
113
 
    ++itr)
114
 
    {
115
 
        if(sa->compare(*(itr->get()))==0) 
116
 
            return (osg::StateAttribute *)itr->get();
117
 
    }
118
 
    return NULL;
 
127
    TextureSet::iterator result
 
128
        = _sharedTextureList.find(osg::ref_ptr<osg::StateAttribute>(sa));
 
129
    if (result == _sharedTextureList.end())
 
130
        return NULL;
 
131
    else
 
132
        return result->get();
119
133
}
120
134
   
121
135
 
163
177
                {
164
178
                    // Texture is in sharedAttributeList: 
165
179
                    // Share now. Required to be shared all next times
166
 
                    if(mutex) mutex->lock();
 
180
                    if(_mutex) _mutex->lock();
167
181
                    ss->setTextureAttributeAndModes(unit, textureFromSharedList, osg::StateAttribute::ON);
168
 
                    if(mutex) mutex->unlock();
 
182
                    if(_mutex) _mutex->unlock();
169
183
                    tmpSharedTextureList[texture] = TextureSharePair(textureFromSharedList, true);
170
184
                }
171
185
                else
172
186
                {
173
187
                    // Texture is not in _sharedAttributeList: 
174
 
                    // Add to _sharedAttributeList. Not needed to be shared all next times.
 
188
                    // Add to _sharedAttributeList. Not needed to be
 
189
                    // shared all next times.
 
190
                    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
175
191
                    _sharedTextureList.insert(texture); 
176
192
                    tmpSharedTextureList[texture] = TextureSharePair(texture, false);            
177
193
                }
180
196
            {
181
197
                // Texture is in tmpSharedAttributeList and share flag is on:
182
198
                // It should be shared
183
 
                if(mutex) mutex->lock();
 
199
                if(_mutex) _mutex->lock();
184
200
                ss->setTextureAttributeAndModes(unit, titr->second.first, osg::StateAttribute::ON);
185
 
                if(mutex) mutex->unlock();
 
201
                if(_mutex) _mutex->unlock();
186
202
            }
187
203
        }
188
204
    }
194
210
//----------------------------------------------------------------
195
211
void SharedStateManager::process(osg::StateSet* ss, osg::Object* parent)
196
212
{
197
 
    if(shareMode & SHARE_STATESETS)
 
213
    if(_shareMode & SHARE_STATESETS)
198
214
    {
199
215
        // Valid StateSet to be shared
200
216
        if(ss->getDataVariance()==osg::Object::STATIC)
209
225
                {
210
226
                    // StateSet is in sharedStateSetList: 
211
227
                    // Share now. Required to be shared all next times
212
 
                    if(mutex) mutex->lock();
 
228
                    if(_mutex) _mutex->lock();
213
229
                    setStateSet(ssFromSharedList, parent);
214
 
                    if(mutex) mutex->unlock();
 
230
                    if(_mutex) _mutex->unlock();
215
231
                    tmpSharedStateSetList[ss] = StateSetSharePair(ssFromSharedList, true);
216
232
                }
217
233
                else
218
234
                {
219
235
                    // StateSet is not in sharedStateSetList: 
220
236
                    // Add to sharedStateSetList. Not needed to be shared all next times.
221
 
                    _sharedStateSetList.insert(ss); 
222
 
                    tmpSharedStateSetList[ss] = StateSetSharePair(ss, false);            
223
 
 
 
237
                    {
 
238
                        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
 
239
                        _sharedStateSetList.insert(ss); 
 
240
                        tmpSharedStateSetList[ss]
 
241
                            = StateSetSharePair(ss, false);            
 
242
                    }
224
243
                    // Only in this case sharing textures is also required
225
 
                    if(shareMode & SHARE_TEXTURES)
 
244
                    if(_shareMode & SHARE_TEXTURES)
226
245
                    {
227
246
                        shareTextures(ss);
228
247
                    }
232
251
            {
233
252
                // StateSet is in tmpSharedStateSetList and share flag is on:
234
253
                // It should be shared
235
 
                if(mutex) mutex->lock();
 
254
                if(_mutex) _mutex->lock();
236
255
                setStateSet(sitr->second.first, parent);
237
 
                if(mutex) mutex->unlock();
 
256
                if(_mutex) _mutex->unlock();
238
257
            }
239
258
        }
240
259
    }
241
260
 
242
 
    else if(shareMode & SHARE_TEXTURES)
 
261
    else if(_shareMode & SHARE_TEXTURES)
243
262
    {
244
263
        shareTextures(ss);
245
264
    }