PageRenderTime 149ms CodeModel.GetById 134ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llworldmipmap.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 268 lines | 164 code | 27 blank | 77 comment | 29 complexity | 0a471134f4d60384344cc5b689538042 MD5 | raw file
  1/** 
  2 * @file llworldmipmap.cpp
  3 * @brief Data storage for the S3 mipmap of the entire world.
  4 *
  5 * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "llworldmipmap.h"
 30#include "llviewercontrol.h"		// LLControlGroup
 31
 32#include "llviewertexturelist.h"
 33#include "math.h"	// log()
 34
 35// Turn this on to output tile stats in the standard output
 36#define DEBUG_TILES_STAT 0
 37
 38LLWorldMipmap::LLWorldMipmap() :
 39	mCurrentLevel(0)
 40{
 41}
 42
 43LLWorldMipmap::~LLWorldMipmap()
 44{
 45	reset();
 46}
 47
 48// Delete all sublevel maps and clean them
 49void LLWorldMipmap::reset()
 50{
 51	for (int level = 0; level < MAP_LEVELS; level++)
 52	{
 53		mWorldObjectsMipMap[level].clear();
 54	}
 55}
 56
 57// This method should be called before each use of the mipmap (typically, before each draw), so that to let
 58// the boost level of unused tiles to drop to 0 (BOOST_NONE).
 59// Tiles that are accessed have had their boost level pushed to BOOST_MAP_VISIBLE so we can identify them.
 60// The result of this strategy is that if a tile is not used during 2 consecutive loops, its boost level drops to 0.
 61void LLWorldMipmap::equalizeBoostLevels()
 62{
 63#if DEBUG_TILES_STAT
 64	S32 nb_missing = 0;
 65	S32 nb_tiles = 0;
 66	S32 nb_visible = 0;
 67#endif // DEBUG_TILES_STAT
 68	// For each level
 69	for (S32 level = 0; level < MAP_LEVELS; level++)
 70	{
 71		sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level];
 72		// For each tile
 73		for (sublevel_tiles_t::iterator iter = level_mipmap.begin(); iter != level_mipmap.end(); iter++)
 74		{
 75			LLPointer<LLViewerFetchedTexture> img = iter->second;
 76			S32 current_boost_level = img->getBoostLevel();
 77			if (current_boost_level == LLViewerTexture::BOOST_MAP_VISIBLE)
 78			{
 79				// If level was BOOST_MAP_VISIBLE, the tile has been used in the last draw so keep it high
 80				img->setBoostLevel(LLViewerTexture::BOOST_MAP);
 81			}
 82			else
 83			{
 84				// If level was BOOST_MAP only (or anything else...), the tile wasn't used in the last draw 
 85				// so we drop its boost level to BOOST_NONE.
 86				img->setBoostLevel(LLViewerTexture::BOOST_NONE);
 87			}
 88#if DEBUG_TILES_STAT
 89			// Increment some stats if compile option on
 90			nb_tiles++;
 91			if (current_boost_level == LLViewerTexture::BOOST_MAP_VISIBLE)
 92			{
 93				nb_visible++;
 94			}
 95			if (img->isMissingAsset())
 96			{
 97				nb_missing++;
 98			}
 99#endif // DEBUG_TILES_STAT
100		}
101	}
102#if DEBUG_TILES_STAT
103	LL_INFOS("World Map") << "LLWorldMipmap tile stats : total requested = " << nb_tiles << ", visible = " << nb_visible << ", missing = " << nb_missing << LL_ENDL;
104#endif // DEBUG_TILES_STAT
105}
106
107// This method should be used when the mipmap is not actively used for a while, e.g., the map UI is hidden
108void LLWorldMipmap::dropBoostLevels()
109{
110	// For each level
111	for (S32 level = 0; level < MAP_LEVELS; level++)
112	{
113		sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level];
114		// For each tile
115		for (sublevel_tiles_t::iterator iter = level_mipmap.begin(); iter != level_mipmap.end(); iter++)
116		{
117			LLPointer<LLViewerFetchedTexture> img = iter->second;
118			img->setBoostLevel(LLViewerTexture::BOOST_NONE);
119		}
120	}
121}
122
123LLPointer<LLViewerFetchedTexture> LLWorldMipmap::getObjectsTile(U32 grid_x, U32 grid_y, S32 level, bool load)
124{
125	// Check the input data
126	llassert(level <= MAP_LEVELS);
127	llassert(level >= 1);
128
129	// If the *loading* level changed, cleared the new level from "missed" tiles
130	// so that we get a chance to reload them
131	if (load && (level != mCurrentLevel))
132	{
133		cleanMissedTilesFromLevel(level);
134		mCurrentLevel = level;
135	}
136
137	// Build the region handle
138	U64 handle = convertGridToHandle(grid_x, grid_y);
139
140	// Check if the image is around already
141	sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level-1];
142	sublevel_tiles_t::iterator found = level_mipmap.find(handle);
143
144	// If not there and load on, go load it
145	if (found == level_mipmap.end())
146	{
147		if (load)
148		{
149			// Load it 
150			LLPointer<LLViewerFetchedTexture> img = loadObjectsTile(grid_x, grid_y, level);
151			// Insert the image in the map
152			level_mipmap.insert(sublevel_tiles_t::value_type( handle, img ));
153			// Find the element again in the map (it's there now...)
154			found = level_mipmap.find(handle);
155		}
156		else
157		{
158			// Return with NULL if not found and we're not trying to load
159			return NULL;
160		}
161	}
162
163	// Get the image pointer and check if this asset is missing
164	LLPointer<LLViewerFetchedTexture> img = found->second;
165	if (img->isMissingAsset())
166	{
167		// Return NULL if asset missing
168		return NULL;
169	}
170	else
171	{
172		// Boost the tile level so to mark it's in use *if* load on
173		if (load)
174		{
175			img->setBoostLevel(LLViewerTexture::BOOST_MAP_VISIBLE);
176		}
177		return img;
178	}
179}
180
181LLPointer<LLViewerFetchedTexture> LLWorldMipmap::loadObjectsTile(U32 grid_x, U32 grid_y, S32 level)
182{
183	// Get the grid coordinates
184	std::string imageurl = gSavedSettings.getString("CurrentMapServerURL") + llformat("map-%d-%d-%d-objects.jpg", level, grid_x, grid_y);
185
186	// DO NOT COMMIT!! DEBUG ONLY!!!
187	// Use a local jpeg for every tile to test map speed without S3 access
188	//imageurl = "file://C:\\Develop\\mapserver-distribute-3\\indra\\build-vc80\\mapserver\\relwithdebinfo\\regions\\00995\\01001\\region-995-1001-prims.jpg";
189	// END DEBUG
190	//LL_INFOS("World Map") << "LLWorldMipmap::loadObjectsTile(), URL = " << imageurl << LL_ENDL;
191
192	LLPointer<LLViewerFetchedTexture> img = LLViewerTextureManager::getFetchedTextureFromUrl(imageurl, TRUE, LLViewerTexture::BOOST_NONE, LLViewerTexture::LOD_TEXTURE);
193	img->setBoostLevel(LLViewerTexture::BOOST_MAP);
194
195	// Return the smart pointer
196	return img;
197}
198
199// This method is used to clean up a level from tiles marked as "missing".
200// The idea is to allow tiles that have been improperly marked missing to be reloaded when retraversing the level again.
201// When zooming in and out rapidly, some tiles are never properly loaded and, eventually marked missing.
202// This creates "blue" areas in a subresolution that never got a chance to reload if we don't clean up the level.
203void LLWorldMipmap::cleanMissedTilesFromLevel(S32 level)
204{
205	// Check the input data
206	llassert(level <= MAP_LEVELS);
207	llassert(level >= 0);
208
209	// This happens when the object is first initialized
210	if (level == 0)
211	{
212		return;
213	}
214
215	// Iterate through the subresolution level and suppress the tiles that are marked as missing
216	// Note: erasing in a map while iterating through it is bug prone. Using a postfix increment is mandatory here.
217	sublevel_tiles_t& level_mipmap = mWorldObjectsMipMap[level-1];
218	sublevel_tiles_t::iterator it = level_mipmap.begin();
219	while (it != level_mipmap.end())
220	{
221		LLPointer<LLViewerFetchedTexture> img = it->second;
222		if (img->isMissingAsset())
223		{
224			level_mipmap.erase(it++);
225		}
226		else
227		{
228			++it;
229		}
230	}
231	return;
232}
233
234// static methods
235// Compute the level in the world mipmap (between 1 and MAP_LEVELS, as in the URL) given the scale (size of a sim in screen pixels)
236S32 LLWorldMipmap::scaleToLevel(F32 scale)
237{
238	// If scale really small, picks up the higest level there is (lowest resolution)
239	if (scale <= F32_MIN)
240		return MAP_LEVELS;
241	// Compute the power of two resolution level knowing the base level
242	S32 level = llfloor((log(REGION_WIDTH_METERS/scale)/log(2.0f)) + 1.0f);
243	// Check bounds and return the value
244	if (level > MAP_LEVELS)
245		return MAP_LEVELS;
246	else if (level < 1)
247		return 1;
248	else
249		return level;
250}
251
252// Convert world coordinates to mipmap grid coordinates at a given level (between 1 and MAP_LEVELS)
253void LLWorldMipmap::globalToMipmap(F64 global_x, F64 global_y, S32 level, U32* grid_x, U32* grid_y)
254{
255	// Check the input data
256	llassert(level <= MAP_LEVELS);
257	llassert(level >= 1);
258
259	// Convert world coordinates into grid coordinates
260	*grid_x = lltrunc(global_x/REGION_WIDTH_METERS);
261	*grid_y = lltrunc(global_y/REGION_WIDTH_METERS);
262	// Compute the valid grid coordinates at that level of the mipmap
263	S32 regions_in_tile = 1 << (level - 1);
264	*grid_x = *grid_x - (*grid_x % regions_in_tile);
265	*grid_y = *grid_y - (*grid_y % regions_in_tile);
266}
267
268