osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthSymbology/ResourceCache.cpp

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 #include <osgEarthSymbology/ResourceCache>
00020 
00021 using namespace osgEarth;
00022 using namespace osgEarth::Symbology;
00023 
00024 osg::StateSet*
00025 ResourceCache::getStateSet( SkinResource* skin )
00026 {
00027     osg::StateSet* result = 0L;
00028 
00029     if ( _threadSafe )
00030     {
00031         // first check if it exists
00032         {
00033             Threading::ScopedReadLock shared( _mutex );
00034 
00035             SkinCache::Record rec = _skinCache.get( skin );
00036             if ( rec.valid() )
00037             {
00038                 result = rec.value();
00039             }
00040         }
00041 
00042         // no? exclusive lock and create it.
00043         if ( !result )
00044         {
00045             Threading::ScopedWriteLock exclusive( _mutex );
00046             
00047             // double check to avoid race condition
00048             SkinCache::Record rec = _skinCache.get( skin );
00049             if ( rec.valid() )
00050             {
00051                 result = rec.value();
00052             }
00053             else
00054             {
00055                 // still not there, make it.
00056                 result = skin->createStateSet();
00057                 if ( result )
00058                     _skinCache.insert( skin, result );
00059             }
00060         }
00061     }
00062 
00063     else
00064     {
00065         SkinCache::Record rec = _skinCache.get( skin );
00066         if ( rec.valid() )
00067         {
00068             result = rec.value();
00069         }
00070         else
00071         {
00072             result = skin->createStateSet();
00073             if ( result )
00074                 _skinCache.insert( skin, result );
00075         }
00076     }
00077 
00078     return result;
00079 }
00080 
00081 osg::Node*
00082 ResourceCache::getMarkerNode( MarkerResource* marker )
00083 {
00084     osg::Node* result = 0L;
00085 
00086     if ( _threadSafe )
00087     {
00088         // first check if it exists
00089         {
00090             Threading::ScopedReadLock shared( _mutex );
00091 
00092             MarkerCache::Record rec = _markerCache.get( marker );
00093             if ( rec.valid() )
00094             {
00095                 result = rec.value();
00096             }
00097         }
00098 
00099         // no? exclusive lock and create it.
00100         if ( !result )
00101         {
00102             Threading::ScopedWriteLock exclusive( _mutex );
00103             
00104             // double check to avoid race condition
00105             MarkerCache::Record rec = _markerCache.get( marker );
00106             if ( rec.valid() )
00107             {
00108                 result = rec.value();
00109             }
00110             else
00111             {
00112                 // still not there, make it.
00113                 result = marker->createNode();
00114                 if ( result )
00115                     _markerCache.insert( marker, result );
00116             }
00117         }
00118     }
00119 
00120     else
00121     {
00122         MarkerCache::Record rec = _markerCache.get( marker );
00123         if ( rec.valid() )
00124         {
00125             result = rec.value();
00126         }
00127         else
00128         {
00129             result = marker->createNode();
00130             if ( result )
00131                 _markerCache.insert( marker, result );
00132         }
00133     }
00134 
00135     return result;
00136 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines