目标:(八十二)中的问题162
图层在创建完成后,需要添加到osgEarth::Map中,添加过程中会执行图层的打开操作。每个图层只打开一次,打开过的图层就不再打开了。
osgEarth/TerrainLayer.cpp
const Status&
TerrainLayer::open()
{
if ( !_openCalled )
{
// Call base class
if (VisibleLayer::open().isError())
return getStatus();
// Create an L2 mem cache that sits atop the main cache, if necessary.
// For now: use the same L2 cache size at the driver.
int l2CacheSize = options().driver()->L2CacheSize().get();
// See if it was overridden with an env var.
char const* l2env = ::getenv( "OSGEARTH_L2_CACHE_SIZE" );
if ( l2env )
{
l2CacheSize = as<int>( std::string(l2env), 0 );
OE_INFO << LC << "L2 cache size set from environment = " << l2CacheSize << "\n";
}
// Env cache-only mode also disables the L2 cache.
char const* noCacheEnv = ::getenv( "OSGEARTH_MEMORY_PROFILE" );
if ( noCacheEnv )
{
l2CacheSize = 0;
}
// Initialize the l2 cache if it's size is > 0
if ( l2CacheSize > 0 )
{
_memCache = new MemCache( l2CacheSize );
}
// create the unique cache ID for the cache bin.
//std::string cacheId;
if (options().cacheId().isSet() && !options().cacheId()->empty())
{
// user expliticy set a cacheId in the terrain layer options.
// this appears to be a NOP; review for removal -gw
_runtimeCacheId = options().cacheId().get();
}
else
{
// system will generate a cacheId from the layer configuration.
Config hashConf = options().getConfig();
// remove non-data properties.
hashConf.remove("name");
hashConf.remove("enabled");
hashConf.remove("cacheid");
hashConf.remove("cache_only");
hashConf.remove("cache_enabled");
hashConf.remove("cache_policy");
hashConf.remove("visible");
hashConf.remove("l2_cache_size");
OE_DEBUG << "hashConfFinal = " << hashConf.toJSON(true) << std::endl;
unsigned hash = osgEarth::hashString(hashConf.toJSON());
_runtimeCacheId = Stringify() << std::hex << std::setw(8) << std::setfill('0') << hash;
}
// Now that we know the cache ID, establish the cache settings for this Layer.
// Start by cloning whatever CacheSettings were inherited in the read options
// (typically from the Map).
CacheSettings* oldSettings = CacheSettings::get(_readOptions.get());
_cacheSettings = oldSettings ? new CacheSettings(*oldSettings) : new CacheSettings();
// Store for further propagation!
_cacheSettings->store(_readOptions.get());
// Integrate a cache policy from this Layer's options:
_cacheSettings->integrateCachePolicy(options().cachePolicy());
// If you created the layer with a pre-created tile source, it will already by set.
if (!_tileSource.valid())
{
osg::ref_ptr<TileSource> ts;
// as long as we're not in cache-only mode, try to create the TileSource.
if (_cacheSettings->cachePolicy()->isCacheOnly())
{
OE_INFO << LC << "Opening in cache-only mode\n";
}
else if (isTileSourceExpected())
{
// Initialize the tile source once and only once.
ts = createAndOpenTileSource();
}
// All good
if (ts.valid() && !_tileSource.valid())
{
_tileS