/indra/newview/llviewertexture.cpp
C++ | 2430 lines | 1914 code | 309 blank | 207 comment | 327 complexity | 0741c41b5059d516ab42952772805c6b MD5 | raw file
Possible License(s): LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /**
- * @file llviewertexture.cpp
- * @brief Object which handles a received image (and associated texture(s))
- *
- * $LicenseInfo:firstyear=2000&license=viewerlgpl$
- * Second Life Viewer Source Code
- * Copyright (C) 2010, Linden Research, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation;
- * version 2.1 of the License only.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
- * $/LicenseInfo$
- */
- #include "llviewerprecompiledheaders.h"
- #include "llviewertexture.h"
- // Library includes
- #include "imageids.h"
- #include "llmath.h"
- #include "llerror.h"
- #include "llgl.h"
- #include "llglheaders.h"
- #include "llhost.h"
- #include "llimage.h"
- #include "llimagebmp.h"
- #include "llimagej2c.h"
- #include "llimagetga.h"
- #include "llmemtype.h"
- #include "llstl.h"
- #include "llvfile.h"
- #include "llvfs.h"
- #include "message.h"
- #include "lltimer.h"
- // viewer includes
- #include "llimagegl.h"
- #include "lldrawpool.h"
- #include "lltexturefetch.h"
- #include "llviewertexturelist.h"
- #include "llviewercontrol.h"
- #include "pipeline.h"
- #include "llappviewer.h"
- #include "llface.h"
- #include "llviewercamera.h"
- #include "lltextureatlas.h"
- #include "lltextureatlasmanager.h"
- #include "lltextureentry.h"
- #include "llmediaentry.h"
- #include "llvovolume.h"
- #include "llviewermedia.h"
- ///////////////////////////////////////////////////////////////////////////////
- // statics
- LLPointer<LLViewerTexture> LLViewerTexture::sNullImagep = NULL;
- LLPointer<LLViewerTexture> LLViewerTexture::sBlackImagep = NULL;
- LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sMissingAssetImagep = NULL;
- LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sWhiteImagep = NULL;
- LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sDefaultImagep = NULL;
- LLPointer<LLViewerFetchedTexture> LLViewerFetchedTexture::sSmokeImagep = NULL;
- LLViewerMediaTexture::media_map_t LLViewerMediaTexture::sMediaMap ;
- LLTexturePipelineTester* LLViewerTextureManager::sTesterp = NULL ;
- const std::string sTesterName("TextureTester");
- S32 LLViewerTexture::sImageCount = 0;
- S32 LLViewerTexture::sRawCount = 0;
- S32 LLViewerTexture::sAuxCount = 0;
- LLFrameTimer LLViewerTexture::sEvaluationTimer;
- F32 LLViewerTexture::sDesiredDiscardBias = 0.f;
- F32 LLViewerTexture::sDesiredDiscardScale = 1.1f;
- S32 LLViewerTexture::sBoundTextureMemoryInBytes = 0;
- S32 LLViewerTexture::sTotalTextureMemoryInBytes = 0;
- S32 LLViewerTexture::sMaxBoundTextureMemInMegaBytes = 0;
- S32 LLViewerTexture::sMaxTotalTextureMemInMegaBytes = 0;
- S32 LLViewerTexture::sMaxDesiredTextureMemInBytes = 0 ;
- S8 LLViewerTexture::sCameraMovingDiscardBias = 0 ;
- S32 LLViewerTexture::sMaxSculptRez = 128 ; //max sculpt image size
- const S32 MAX_CACHED_RAW_IMAGE_AREA = 64 * 64 ;
- const S32 MAX_CACHED_RAW_SCULPT_IMAGE_AREA = LLViewerTexture::sMaxSculptRez * LLViewerTexture::sMaxSculptRez ;
- const S32 MAX_CACHED_RAW_TERRAIN_IMAGE_AREA = 128 * 128 ;
- S32 LLViewerTexture::sMinLargeImageSize = 65536 ; //256 * 256.
- S32 LLViewerTexture::sMaxSmallImageSize = MAX_CACHED_RAW_IMAGE_AREA ;
- BOOL LLViewerTexture::sFreezeImageScalingDown = FALSE ;
- F32 LLViewerTexture::sCurrentTime = 0.0f ;
- BOOL LLViewerTexture::sUseTextureAtlas = FALSE ;
- const F32 desired_discard_bias_min = -2.0f; // -max number of levels to improve image quality by
- const F32 desired_discard_bias_max = (F32)MAX_DISCARD_LEVEL; // max number of levels to reduce image quality by
- const F64 log_2 = log(2.0);
- //----------------------------------------------------------------------------------------------
- //namespace: LLViewerTextureAccess
- //----------------------------------------------------------------------------------------------
- LLLoadedCallbackEntry::LLLoadedCallbackEntry(loaded_callback_func cb,
- S32 discard_level,
- BOOL need_imageraw, // Needs image raw for the callback
- void* userdata,
- LLLoadedCallbackEntry::source_callback_list_t* src_callback_list,
- LLViewerFetchedTexture* target,
- BOOL pause)
- : mCallback(cb),
- mLastUsedDiscard(MAX_DISCARD_LEVEL+1),
- mDesiredDiscard(discard_level),
- mNeedsImageRaw(need_imageraw),
- mUserData(userdata),
- mSourceCallbackList(src_callback_list),
- mPaused(pause)
- {
- if(mSourceCallbackList)
- {
- mSourceCallbackList->insert(target->getID());
- }
- }
- LLLoadedCallbackEntry::~LLLoadedCallbackEntry()
- {
- }
- void LLLoadedCallbackEntry::removeTexture(LLViewerFetchedTexture* tex)
- {
- if(mSourceCallbackList)
- {
- mSourceCallbackList->erase(tex->getID()) ;
- }
- }
- //static
- void LLLoadedCallbackEntry::cleanUpCallbackList(LLLoadedCallbackEntry::source_callback_list_t* callback_list)
- {
- //clear texture callbacks.
- if(callback_list && !callback_list->empty())
- {
- for(LLLoadedCallbackEntry::source_callback_list_t::iterator iter = callback_list->begin();
- iter != callback_list->end(); ++iter)
- {
- LLViewerFetchedTexture* tex = gTextureList.findImage(*iter) ;
- if(tex)
- {
- tex->deleteCallbackEntry(callback_list) ;
- }
- }
- callback_list->clear() ;
- }
- }
- LLViewerMediaTexture* LLViewerTextureManager::createMediaTexture(const LLUUID &media_id, BOOL usemipmaps, LLImageGL* gl_image)
- {
- return new LLViewerMediaTexture(media_id, usemipmaps, gl_image) ;
- }
-
- LLViewerTexture* LLViewerTextureManager::findTexture(const LLUUID& id)
- {
- LLViewerTexture* tex ;
- //search fetched texture list
- tex = gTextureList.findImage(id) ;
-
- //search media texture list
- if(!tex)
- {
- tex = LLViewerTextureManager::findMediaTexture(id) ;
- }
- return tex ;
- }
-
- LLViewerMediaTexture* LLViewerTextureManager::findMediaTexture(const LLUUID &media_id)
- {
- return LLViewerMediaTexture::findMediaTexture(media_id) ;
- }
- LLViewerMediaTexture* LLViewerTextureManager::getMediaTexture(const LLUUID& id, BOOL usemipmaps, LLImageGL* gl_image)
- {
- LLViewerMediaTexture* tex = LLViewerMediaTexture::findMediaTexture(id) ;
- if(!tex)
- {
- tex = LLViewerTextureManager::createMediaTexture(id, usemipmaps, gl_image) ;
- }
- tex->initVirtualSize() ;
- return tex ;
- }
- LLViewerFetchedTexture* LLViewerTextureManager::staticCastToFetchedTexture(LLTexture* tex, BOOL report_error)
- {
- if(!tex)
- {
- return NULL ;
- }
- S8 type = tex->getType() ;
- if(type == LLViewerTexture::FETCHED_TEXTURE || type == LLViewerTexture::LOD_TEXTURE)
- {
- return static_cast<LLViewerFetchedTexture*>(tex) ;
- }
- if(report_error)
- {
- llerrs << "not a fetched texture type: " << type << llendl ;
- }
- return NULL ;
- }
- LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(BOOL usemipmaps, BOOL generate_gl_tex)
- {
- LLPointer<LLViewerTexture> tex = new LLViewerTexture(usemipmaps) ;
- if(generate_gl_tex)
- {
- tex->generateGLTexture() ;
- tex->setCategory(LLViewerTexture::LOCAL) ;
- }
- return tex ;
- }
- LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(const LLUUID& id, BOOL usemipmaps, BOOL generate_gl_tex)
- {
- LLPointer<LLViewerTexture> tex = new LLViewerTexture(id, usemipmaps) ;
- if(generate_gl_tex)
- {
- tex->generateGLTexture() ;
- tex->setCategory(LLViewerTexture::LOCAL) ;
- }
- return tex ;
- }
- LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(const LLImageRaw* raw, BOOL usemipmaps)
- {
- LLPointer<LLViewerTexture> tex = new LLViewerTexture(raw, usemipmaps) ;
- tex->setCategory(LLViewerTexture::LOCAL) ;
- return tex ;
- }
- LLPointer<LLViewerTexture> LLViewerTextureManager::getLocalTexture(const U32 width, const U32 height, const U8 components, BOOL usemipmaps, BOOL generate_gl_tex)
- {
- LLPointer<LLViewerTexture> tex = new LLViewerTexture(width, height, components, usemipmaps) ;
- if(generate_gl_tex)
- {
- tex->generateGLTexture() ;
- tex->setCategory(LLViewerTexture::LOCAL) ;
- }
- return tex ;
- }
- LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTexture(
- const LLUUID &image_id,
- BOOL usemipmaps,
- LLViewerTexture::EBoostLevel boost_priority,
- S8 texture_type,
- LLGLint internal_format,
- LLGLenum primary_format,
- LLHost request_from_host)
- {
- return gTextureList.getImage(image_id, usemipmaps, boost_priority, texture_type, internal_format, primary_format, request_from_host) ;
- }
-
- LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTextureFromFile(
- const std::string& filename,
- BOOL usemipmaps,
- LLViewerTexture::EBoostLevel boost_priority,
- S8 texture_type,
- LLGLint internal_format,
- LLGLenum primary_format,
- const LLUUID& force_id)
- {
- return gTextureList.getImageFromFile(filename, usemipmaps, boost_priority, texture_type, internal_format, primary_format, force_id) ;
- }
- //static
- LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTextureFromUrl(const std::string& url,
- BOOL usemipmaps,
- LLViewerTexture::EBoostLevel boost_priority,
- S8 texture_type,
- LLGLint internal_format,
- LLGLenum primary_format,
- const LLUUID& force_id
- )
- {
- return gTextureList.getImageFromUrl(url, usemipmaps, boost_priority, texture_type, internal_format, primary_format, force_id) ;
- }
- LLViewerFetchedTexture* LLViewerTextureManager::getFetchedTextureFromHost(const LLUUID& image_id, LLHost host)
- {
- return gTextureList.getImageFromHost(image_id, host) ;
- }
- void LLViewerTextureManager::init()
- {
- {
- LLPointer<LLImageRaw> raw = new LLImageRaw(1,1,3);
- raw->clear(0x77, 0x77, 0x77, 0xFF);
- LLViewerTexture::sNullImagep = LLViewerTextureManager::getLocalTexture(raw.get(), TRUE) ;
- }
- const S32 dim = 128;
- LLPointer<LLImageRaw> image_raw = new LLImageRaw(dim,dim,3);
- U8* data = image_raw->getData();
-
- memset(data, 0, dim * dim * 3) ;
- LLViewerTexture::sBlackImagep = LLViewerTextureManager::getLocalTexture(image_raw.get(), TRUE) ;
- #if 1
- LLPointer<LLViewerFetchedTexture> imagep = LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT);
- LLViewerFetchedTexture::sDefaultImagep = imagep;
-
- for (S32 i = 0; i<dim; i++)
- {
- for (S32 j = 0; j<dim; j++)
- {
- #if 0
- const S32 border = 2;
- if (i<border || j<border || i>=(dim-border) || j>=(dim-border))
- {
- *data++ = 0xff;
- *data++ = 0xff;
- *data++ = 0xff;
- }
- else
- #endif
- {
- *data++ = 0x7f;
- *data++ = 0x7f;
- *data++ = 0x7f;
- }
- }
- }
- imagep->createGLTexture(0, image_raw);
- //cache the raw image
- imagep->setCachedRawImage(0, image_raw) ;
- image_raw = NULL;
- #else
- LLViewerFetchedTexture::sDefaultImagep = LLViewerTextureManager::getFetchedTexture(IMG_DEFAULT, TRUE, LLViewerTexture::BOOST_UI);
- #endif
- LLViewerFetchedTexture::sDefaultImagep->dontDiscard();
- LLViewerFetchedTexture::sDefaultImagep->setCategory(LLViewerTexture::OTHER) ;
- LLViewerFetchedTexture::sSmokeImagep = LLViewerTextureManager::getFetchedTexture(IMG_SMOKE, TRUE, LLViewerTexture::BOOST_UI);
- LLViewerFetchedTexture::sSmokeImagep->setNoDelete() ;
- LLViewerTexture::initClass() ;
- if (LLMetricPerformanceTesterBasic::isMetricLogRequested(sTesterName) && !LLMetricPerformanceTesterBasic::getTester(sTesterName))
- {
- sTesterp = new LLTexturePipelineTester() ;
- if (!sTesterp->isValid())
- {
- delete sTesterp;
- sTesterp = NULL;
- }
- }
- }
- void LLViewerTextureManager::cleanup()
- {
- stop_glerror();
- LLImageGL::sDefaultGLTexture = NULL ;
- LLViewerTexture::sNullImagep = NULL;
- LLViewerTexture::sBlackImagep = NULL;
- LLViewerFetchedTexture::sDefaultImagep = NULL;
- LLViewerFetchedTexture::sSmokeImagep = NULL;
- LLViewerFetchedTexture::sMissingAssetImagep = NULL;
- LLViewerFetchedTexture::sWhiteImagep = NULL;
- LLViewerMediaTexture::cleanUpClass() ;
- }
- //----------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------
- //start of LLViewerTexture
- //----------------------------------------------------------------------------------------------
- // static
- void LLViewerTexture::initClass()
- {
- LLImageGL::sDefaultGLTexture = LLViewerFetchedTexture::sDefaultImagep->getGLTexture() ;
- if(gAuditTexture)
- {
- LLImageGL::setHighlightTexture(LLViewerTexture::OTHER) ;
- }
- }
- // static
- S32 LLViewerTexture::getTotalNumOfCategories()
- {
- return MAX_GL_IMAGE_CATEGORY - (BOOST_HIGH - BOOST_SCULPTED) + 2 ;
- }
- // static
- //index starts from zero.
- S32 LLViewerTexture::getIndexFromCategory(S32 category)
- {
- return (category < BOOST_HIGH) ? category : category - (BOOST_HIGH - BOOST_SCULPTED) + 1 ;
- }
- //static
- S32 LLViewerTexture::getCategoryFromIndex(S32 index)
- {
- return (index < BOOST_HIGH) ? index : index + (BOOST_HIGH - BOOST_SCULPTED) - 1 ;
- }
- // tuning params
- const F32 discard_bias_delta = .25f;
- const F32 discard_delta_time = 0.5f;
- const S32 min_non_tex_system_mem = (128<<20); // 128 MB
- // non-const (used externally
- F32 texmem_lower_bound_scale = 0.85f;
- F32 texmem_middle_bound_scale = 0.925f;
- static LLFastTimer::DeclareTimer FTM_TEXTURE_MEMORY_CHECK("Memory Check");
- //static
- bool LLViewerTexture::isMemoryForTextureLow()
- {
- const F32 WAIT_TIME = 1.0f ; //second
- static LLFrameTimer timer ;
- if(timer.getElapsedTimeF32() < WAIT_TIME) //call this once per second.
- {
- return false;
- }
- timer.reset() ;
- LLFastTimer t(FTM_TEXTURE_MEMORY_CHECK);
- const S32 MIN_FREE_TEXTURE_MEMORY = 5 ; //MB
- const S32 MIN_FREE_MAIN_MEMORy = 100 ; //MB
- bool low_mem = false ;
- if (gGLManager.mHasATIMemInfo)
- {
- S32 meminfo[4];
- glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, meminfo);
- if(meminfo[0] / 1024 < MIN_FREE_TEXTURE_MEMORY)
- {
- low_mem = true ;
- }
- if(!low_mem) //check main memory, only works for windows.
- {
- LLMemory::updateMemoryInfo() ;
- if(LLMemory::getAvailableMemKB() / 1024 < MIN_FREE_MAIN_MEMORy)
- {
- low_mem = true ;
- }
- }
- }
- #if 0 //ignore nVidia cards
- else if (gGLManager.mHasNVXMemInfo)
- {
- S32 free_memory;
- glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &free_memory);
-
- if(free_memory / 1024 < MIN_FREE_TEXTURE_MEMORY)
- {
- low_mem = true ;
- }
- }
- #endif
- return low_mem ;
- }
- static LLFastTimer::DeclareTimer FTM_TEXTURE_UPDATE_MEDIA("Media");
- static LLFastTimer::DeclareTimer FTM_TEXTURE_UPDATE_TEST("Test");
- //static
- void LLViewerTexture::updateClass(const F32 velocity, const F32 angular_velocity)
- {
- sCurrentTime = gFrameTimeSeconds ;
- LLTexturePipelineTester* tester = (LLTexturePipelineTester*)LLMetricPerformanceTesterBasic::getTester(sTesterName);
- if (tester)
- {
- LLFastTimer t(FTM_TEXTURE_UPDATE_TEST);
- tester->update() ;
- }
- {
- LLFastTimer t(FTM_TEXTURE_UPDATE_MEDIA);
- LLViewerMediaTexture::updateClass() ;
- }
- sBoundTextureMemoryInBytes = LLImageGL::sBoundTextureMemoryInBytes;//in bytes
- sTotalTextureMemoryInBytes = LLImageGL::sGlobalTextureMemoryInBytes;//in bytes
- sMaxBoundTextureMemInMegaBytes = gTextureList.getMaxResidentTexMem();//in MB
- sMaxTotalTextureMemInMegaBytes = gTextureList.getMaxTotalTextureMem() ;//in MB
- sMaxDesiredTextureMemInBytes = MEGA_BYTES_TO_BYTES(sMaxTotalTextureMemInMegaBytes) ; //in Bytes, by default and when total used texture memory is small.
- if (BYTES_TO_MEGA_BYTES(sBoundTextureMemoryInBytes) >= sMaxBoundTextureMemInMegaBytes ||
- BYTES_TO_MEGA_BYTES(sTotalTextureMemoryInBytes) >= sMaxTotalTextureMemInMegaBytes)
- {
- //when texture memory overflows, lower down the threashold to release the textures more aggressively.
- sMaxDesiredTextureMemInBytes = llmin((S32)(sMaxDesiredTextureMemInBytes * 0.75f) , MEGA_BYTES_TO_BYTES(MAX_VIDEO_RAM_IN_MEGA_BYTES)) ;//512 MB
-
- // If we are using more texture memory than we should,
- // scale up the desired discard level
- if (sEvaluationTimer.getElapsedTimeF32() > discard_delta_time)
- {
- sDesiredDiscardBias += discard_bias_delta;
- sEvaluationTimer.reset();
- }
- }
- else if(sEvaluationTimer.getElapsedTimeF32() > discard_delta_time && isMemoryForTextureLow())
- {
- sDesiredDiscardBias += discard_bias_delta;
- sEvaluationTimer.reset();
- }
- else if (sDesiredDiscardBias > 0.0f &&
- BYTES_TO_MEGA_BYTES(sBoundTextureMemoryInBytes) < sMaxBoundTextureMemInMegaBytes * texmem_lower_bound_scale &&
- BYTES_TO_MEGA_BYTES(sTotalTextureMemoryInBytes) < sMaxTotalTextureMemInMegaBytes * texmem_lower_bound_scale)
- {
- // If we are using less texture memory than we should,
- // scale down the desired discard level
- if (sEvaluationTimer.getElapsedTimeF32() > discard_delta_time)
- {
- sDesiredDiscardBias -= discard_bias_delta;
- sEvaluationTimer.reset();
- }
- }
- sDesiredDiscardBias = llclamp(sDesiredDiscardBias, desired_discard_bias_min, desired_discard_bias_max);
- LLViewerTexture::sUseTextureAtlas = gSavedSettings.getBOOL("EnableTextureAtlas") ;
-
- F32 camera_moving_speed = LLViewerCamera::getInstance()->getAverageSpeed() ;
- F32 camera_angular_speed = LLViewerCamera::getInstance()->getAverageAngularSpeed();
- sCameraMovingDiscardBias = (S8)llmax(0.2f * camera_moving_speed, 2.0f * camera_angular_speed - 1) ;
- LLViewerTexture::sFreezeImageScalingDown = (BYTES_TO_MEGA_BYTES(sBoundTextureMemoryInBytes) < 0.75f * sMaxBoundTextureMemInMegaBytes * texmem_middle_bound_scale) &&
- (BYTES_TO_MEGA_BYTES(sTotalTextureMemoryInBytes) < 0.75f * sMaxTotalTextureMemInMegaBytes * texmem_middle_bound_scale) ;
- }
- //end of static functions
- //-------------------------------------------------------------------------------------------
- const U32 LLViewerTexture::sCurrentFileVersion = 1;
- LLViewerTexture::LLViewerTexture(BOOL usemipmaps)
- {
- init(true);
- mUseMipMaps = usemipmaps ;
- mID.generate();
- sImageCount++;
- }
- LLViewerTexture::LLViewerTexture(const LLUUID& id, BOOL usemipmaps)
- : mID(id)
- {
- init(true);
- mUseMipMaps = usemipmaps ;
-
- sImageCount++;
- }
- LLViewerTexture::LLViewerTexture(const U32 width, const U32 height, const U8 components, BOOL usemipmaps)
- {
- init(true);
- mFullWidth = width ;
- mFullHeight = height ;
- mUseMipMaps = usemipmaps ;
- mComponents = components ;
- setTexelsPerImage();
- mID.generate();
- sImageCount++;
- }
- LLViewerTexture::LLViewerTexture(const LLImageRaw* raw, BOOL usemipmaps)
- {
- init(true);
- mUseMipMaps = usemipmaps ;
- mGLTexturep = new LLImageGL(raw, usemipmaps) ;
-
- // Create an empty image of the specified size and width
- mID.generate();
- sImageCount++;
- }
- LLViewerTexture::~LLViewerTexture()
- {
- cleanup();
- sImageCount--;
- }
- void LLViewerTexture::init(bool firstinit)
- {
- mBoostLevel = LLViewerTexture::BOOST_NONE;
- mFullWidth = 0;
- mFullHeight = 0;
- mTexelsPerImage = 0 ;
- mUseMipMaps = FALSE ;
- mComponents = 0 ;
- mTextureState = NO_DELETE ;
- mDontDiscard = FALSE;
- mMaxVirtualSize = 0.f;
- mNeedsGLTexture = FALSE ;
- mMaxVirtualSizeResetInterval = 1;
- mMaxVirtualSizeResetCounter = mMaxVirtualSizeResetInterval ;
- mAdditionalDecodePriority = 0.f ;
- mParcelMedia = NULL ;
- mNumFaces = 0 ;
- mNumVolumes = 0;
- mFaceList.clear() ;
- mVolumeList.clear();
- }
- //virtual
- S8 LLViewerTexture::getType() const
- {
- return LLViewerTexture::LOCAL_TEXTURE ;
- }
- void LLViewerTexture::cleanup()
- {
- mFaceList.clear() ;
- mVolumeList.clear();
- if(mGLTexturep)
- {
- mGLTexturep->cleanup();
- }
- }
- // virtual
- void LLViewerTexture::dump()
- {
- if(mGLTexturep)
- {
- mGLTexturep->dump();
- }
- llinfos << "LLViewerTexture"
- << " mID " << mID
- << llendl;
- }
- void LLViewerTexture::setBoostLevel(S32 level)
- {
- if(mBoostLevel != level)
- {
- mBoostLevel = level ;
- if(mBoostLevel != LLViewerTexture::BOOST_NONE)
- {
- setNoDelete() ;
- }
- if(gAuditTexture)
- {
- setCategory(mBoostLevel);
- }
- }
- }
- bool LLViewerTexture::bindDefaultImage(S32 stage)
- {
- if (stage < 0) return false;
- bool res = true;
- if (LLViewerFetchedTexture::sDefaultImagep.notNull() && (this != LLViewerFetchedTexture::sDefaultImagep.get()))
- {
- // use default if we've got it
- res = gGL.getTexUnit(stage)->bind(LLViewerFetchedTexture::sDefaultImagep);
- }
- if (!res && LLViewerTexture::sNullImagep.notNull() && (this != LLViewerTexture::sNullImagep))
- {
- res = gGL.getTexUnit(stage)->bind(LLViewerTexture::sNullImagep);
- }
- if (!res)
- {
- llwarns << "LLViewerTexture::bindDefaultImage failed." << llendl;
- }
- stop_glerror();
- //check if there is cached raw image and switch to it if possible
- switchToCachedImage() ;
- LLTexturePipelineTester* tester = (LLTexturePipelineTester*)LLMetricPerformanceTesterBasic::getTester(sTesterName);
- if (tester)
- {
- tester->updateGrayTextureBinding() ;
- }
- return res;
- }
- //virtual
- BOOL LLViewerTexture::isMissingAsset()const
- {
- return FALSE;
- }
- //virtual
- void LLViewerTexture::forceImmediateUpdate()
- {
- }
- void LLViewerTexture::addTextureStats(F32 virtual_size, BOOL needs_gltexture) const
- {
- if(needs_gltexture)
- {
- mNeedsGLTexture = TRUE ;
- }
- if(!mMaxVirtualSizeResetCounter)
- {
- //flag to reset the values because the old values are used.
- resetMaxVirtualSizeResetCounter() ;
- mMaxVirtualSize = virtual_size;
- mAdditionalDecodePriority = 0.f ;
- mNeedsGLTexture = needs_gltexture ;
- }
- else if (virtual_size > mMaxVirtualSize)
- {
- mMaxVirtualSize = virtual_size;
- }
- }
- void LLViewerTexture::resetTextureStats()
- {
- mMaxVirtualSize = 0.0f ;
- mAdditionalDecodePriority = 0.f ;
- mMaxVirtualSizeResetCounter = 0 ;
- }
- //virtual
- F32 LLViewerTexture::getMaxVirtualSize()
- {
- return mMaxVirtualSize ;
- }
- //virtual
- void LLViewerTexture::setKnownDrawSize(S32 width, S32 height)
- {
- //nothing here.
- }
- //virtual
- void LLViewerTexture::addFace(LLFace* facep)
- {
- if(mNumFaces >= mFaceList.size())
- {
- mFaceList.resize(2 * mNumFaces + 1) ;
- }
- mFaceList[mNumFaces] = facep ;
- facep->setIndexInTex(mNumFaces) ;
- mNumFaces++ ;
- mLastFaceListUpdateTimer.reset() ;
- }
- //virtual
- void LLViewerTexture::removeFace(LLFace* facep)
- {
- if(mNumFaces > 1)
- {
- S32 index = facep->getIndexInTex() ;
- mFaceList[index] = mFaceList[--mNumFaces] ;
- mFaceList[index]->setIndexInTex(index) ;
- }
- else
- {
- mFaceList.clear() ;
- mNumFaces = 0 ;
- }
- mLastFaceListUpdateTimer.reset() ;
- }
- S32 LLViewerTexture::getNumFaces() const
- {
- return mNumFaces ;
- }
- //virtual
- void LLViewerTexture::addVolume(LLVOVolume* volumep)
- {
- if( mNumVolumes >= mVolumeList.size())
- {
- mVolumeList.resize(2 * mNumVolumes + 1) ;
- }
- mVolumeList[mNumVolumes] = volumep ;
- volumep->setIndexInTex(mNumVolumes) ;
- mNumVolumes++ ;
- mLastVolumeListUpdateTimer.reset() ;
- }
- //virtual
- void LLViewerTexture::removeVolume(LLVOVolume* volumep)
- {
- if(mNumVolumes > 1)
- {
- S32 index = volumep->getIndexInTex() ;
- mVolumeList[index] = mVolumeList[--mNumVolumes] ;
- mVolumeList[index]->setIndexInTex(index) ;
- }
- else
- {
- mVolumeList.clear() ;
- mNumVolumes = 0 ;
- }
- mLastVolumeListUpdateTimer.reset() ;
- }
- S32 LLViewerTexture::getNumVolumes() const
- {
- return mNumVolumes ;
- }
- void LLViewerTexture::reorganizeFaceList()
- {
- static const F32 MAX_WAIT_TIME = 20.f; // seconds
- static const U32 MAX_EXTRA_BUFFER_SIZE = 4 ;
- if(mNumFaces + MAX_EXTRA_BUFFER_SIZE > mFaceList.size())
- {
- return ;
- }
- if(mLastFaceListUpdateTimer.getElapsedTimeF32() < MAX_WAIT_TIME)
- {
- return ;
- }
- mLastFaceListUpdateTimer.reset() ;
- mFaceList.erase(mFaceList.begin() + mNumFaces, mFaceList.end());
- }
- void LLViewerTexture::reorganizeVolumeList()
- {
- static const F32 MAX_WAIT_TIME = 20.f; // seconds
- static const U32 MAX_EXTRA_BUFFER_SIZE = 4 ;
- if(mNumVolumes + MAX_EXTRA_BUFFER_SIZE > mVolumeList.size())
- {
- return ;
- }
- if(mLastVolumeListUpdateTimer.getElapsedTimeF32() < MAX_WAIT_TIME)
- {
- return ;
- }
- mLastVolumeListUpdateTimer.reset() ;
- mVolumeList.erase(mVolumeList.begin() + mNumVolumes, mVolumeList.end());
- }
- //virtual
- void LLViewerTexture::switchToCachedImage()
- {
- //nothing here.
- }
- void LLViewerTexture::forceActive()
- {
- mTextureState = ACTIVE ;
- }
- void LLViewerTexture::setActive()
- {
- if(mTextureState != NO_DELETE)
- {
- mTextureState = ACTIVE ;
- }
- }
- //set the texture to stay in memory
- void LLViewerTexture::setNoDelete()
- {
- mTextureState = NO_DELETE ;
- }
- void LLViewerTexture::generateGLTexture()
- {
- if(mGLTexturep.isNull())
- {
- mGLTexturep = new LLImageGL(mFullWidth, mFullHeight, mComponents, mUseMipMaps) ;
- }
- }
- LLImageGL* LLViewerTexture::getGLTexture() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep ;
- }
- BOOL LLViewerTexture::createGLTexture()
- {
- if(mGLTexturep.isNull())
- {
- generateGLTexture() ;
- }
- return mGLTexturep->createGLTexture() ;
- }
- BOOL LLViewerTexture::createGLTexture(S32 discard_level, const LLImageRaw* imageraw, S32 usename, BOOL to_create, S32 category)
- {
- llassert(mGLTexturep.notNull()) ;
- BOOL ret = mGLTexturep->createGLTexture(discard_level, imageraw, usename, to_create, category) ;
- if(ret)
- {
- mFullWidth = mGLTexturep->getCurrentWidth() ;
- mFullHeight = mGLTexturep->getCurrentHeight() ;
- mComponents = mGLTexturep->getComponents() ;
- setTexelsPerImage();
- }
- return ret ;
- }
- //virtual
- void LLViewerTexture::setCachedRawImage(S32 discard_level, LLImageRaw* imageraw)
- {
- //nothing here.
- }
- void LLViewerTexture::setExplicitFormat(LLGLint internal_format, LLGLenum primary_format, LLGLenum type_format, BOOL swap_bytes)
- {
- llassert(mGLTexturep.notNull()) ;
-
- mGLTexturep->setExplicitFormat(internal_format, primary_format, type_format, swap_bytes) ;
- }
- void LLViewerTexture::setAddressMode(LLTexUnit::eTextureAddressMode mode)
- {
- llassert(mGLTexturep.notNull()) ;
- mGLTexturep->setAddressMode(mode) ;
- }
- void LLViewerTexture::setFilteringOption(LLTexUnit::eTextureFilterOptions option)
- {
- llassert(mGLTexturep.notNull()) ;
- mGLTexturep->setFilteringOption(option) ;
- }
- //virtual
- S32 LLViewerTexture::getWidth(S32 discard_level) const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getWidth(discard_level) ;
- }
- //virtual
- S32 LLViewerTexture::getHeight(S32 discard_level) const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getHeight(discard_level) ;
- }
- S32 LLViewerTexture::getMaxDiscardLevel() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getMaxDiscardLevel() ;
- }
- S32 LLViewerTexture::getDiscardLevel() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getDiscardLevel() ;
- }
- S8 LLViewerTexture::getComponents() const
- {
- llassert(mGLTexturep.notNull()) ;
-
- return mGLTexturep->getComponents() ;
- }
- LLGLuint LLViewerTexture::getTexName() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getTexName() ;
- }
- BOOL LLViewerTexture::hasGLTexture() const
- {
- if(mGLTexturep.notNull())
- {
- return mGLTexturep->getHasGLTexture() ;
- }
- return FALSE ;
- }
- BOOL LLViewerTexture::getBoundRecently() const
- {
- if(mGLTexturep.notNull())
- {
- return mGLTexturep->getBoundRecently() ;
- }
- return FALSE ;
- }
- LLTexUnit::eTextureType LLViewerTexture::getTarget(void) const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getTarget() ;
- }
- BOOL LLViewerTexture::setSubImage(const LLImageRaw* imageraw, S32 x_pos, S32 y_pos, S32 width, S32 height)
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->setSubImage(imageraw, x_pos, y_pos, width, height) ;
- }
- BOOL LLViewerTexture::setSubImage(const U8* datap, S32 data_width, S32 data_height, S32 x_pos, S32 y_pos, S32 width, S32 height)
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->setSubImage(datap, data_width, data_height, x_pos, y_pos, width, height) ;
- }
- void LLViewerTexture::setGLTextureCreated (bool initialized)
- {
- llassert(mGLTexturep.notNull()) ;
- mGLTexturep->setGLTextureCreated (initialized) ;
- }
- void LLViewerTexture::setCategory(S32 category)
- {
- llassert(mGLTexturep.notNull()) ;
- mGLTexturep->setCategory(category) ;
- }
- LLTexUnit::eTextureAddressMode LLViewerTexture::getAddressMode(void) const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getAddressMode() ;
- }
- S32 LLViewerTexture::getTextureMemory() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->mTextureMemory ;
- }
- LLGLenum LLViewerTexture::getPrimaryFormat() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getPrimaryFormat() ;
- }
- BOOL LLViewerTexture::getIsAlphaMask() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getIsAlphaMask() ;
- }
- BOOL LLViewerTexture::getMask(const LLVector2 &tc)
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getMask(tc) ;
- }
- F32 LLViewerTexture::getTimePassedSinceLastBound()
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getTimePassedSinceLastBound() ;
- }
- BOOL LLViewerTexture::getMissed() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getMissed() ;
- }
- BOOL LLViewerTexture::isJustBound() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->isJustBound() ;
- }
- void LLViewerTexture::forceUpdateBindStats(void) const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->forceUpdateBindStats() ;
- }
- U32 LLViewerTexture::getTexelsInAtlas() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getTexelsInAtlas() ;
- }
- U32 LLViewerTexture::getTexelsInGLTexture() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getTexelsInGLTexture() ;
- }
- BOOL LLViewerTexture::isGLTextureCreated() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->isGLTextureCreated() ;
- }
- S32 LLViewerTexture::getDiscardLevelInAtlas() const
- {
- llassert(mGLTexturep.notNull()) ;
- return mGLTexturep->getDiscardLevelInAtlas() ;
- }
- void LLViewerTexture::destroyGLTexture()
- {
- if(mGLTexturep.notNull() && mGLTexturep->getHasGLTexture())
- {
- mGLTexturep->destroyGLTexture() ;
- mTextureState = DELETED ;
- }
- }
- void LLViewerTexture::setTexelsPerImage()
- {
- S32 fullwidth = llmin(mFullWidth,(S32)MAX_IMAGE_SIZE_DEFAULT);
- S32 fullheight = llmin(mFullHeight,(S32)MAX_IMAGE_SIZE_DEFAULT);
- mTexelsPerImage = (F32)fullwidth * fullheight;
- }
- BOOL LLViewerTexture::isLargeImage()
- {
- return (S32)mTexelsPerImage > LLViewerTexture::sMinLargeImageSize ;
- }
- //virtual
- void LLViewerTexture::updateBindStatsForTester()
- {
- LLTexturePipelineTester* tester = (LLTexturePipelineTester*)LLMetricPerformanceTesterBasic::getTester(sTesterName);
- if (tester)
- {
- tester->updateTextureBindingStats(this) ;
- }
- }
- //----------------------------------------------------------------------------------------------
- //end of LLViewerTexture
- //----------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------
- //start of LLViewerFetchedTexture
- //----------------------------------------------------------------------------------------------
- LLViewerFetchedTexture::LLViewerFetchedTexture(const LLUUID& id, const LLHost& host, BOOL usemipmaps)
- : LLViewerTexture(id, usemipmaps),
- mTargetHost(host)
- {
- init(TRUE) ;
- generateGLTexture() ;
- }
-
- LLViewerFetchedTexture::LLViewerFetchedTexture(const LLImageRaw* raw, BOOL usemipmaps)
- : LLViewerTexture(raw, usemipmaps)
- {
- init(TRUE) ;
- }
-
- LLViewerFetchedTexture::LLViewerFetchedTexture(const std::string& url, const LLUUID& id, BOOL usemipmaps)
- : LLViewerTexture(id, usemipmaps),
- mUrl(url)
- {
- init(TRUE) ;
- generateGLTexture() ;
- }
- void LLViewerFetchedTexture::init(bool firstinit)
- {
- mOrigWidth = 0;
- mOrigHeight = 0;
- mNeedsAux = FALSE;
- mRequestedDiscardLevel = -1;
- mRequestedDownloadPriority = 0.f;
- mFullyLoaded = FALSE;
- mCanUseHTTP = true ;
- mDesiredDiscardLevel = MAX_DISCARD_LEVEL + 1;
- mMinDesiredDiscardLevel = MAX_DISCARD_LEVEL + 1;
-
- mDecodingAux = FALSE;
- mKnownDrawWidth = 0;
- mKnownDrawHeight = 0;
- mKnownDrawSizeChanged = FALSE ;
- if (firstinit)
- {
- mDecodePriority = 0.f;
- mInImageList = 0;
- }
- // Only set mIsMissingAsset true when we know for certain that the database
- // does not contain this image.
- mIsMissingAsset = FALSE;
- mLoadedCallbackDesiredDiscardLevel = S8_MAX;
- mPauseLoadedCallBacks = TRUE ;
- mNeedsCreateTexture = FALSE;
-
- mIsRawImageValid = FALSE;
- mRawDiscardLevel = INVALID_DISCARD_LEVEL;
- mMinDiscardLevel = 0;
- mHasFetcher = FALSE;
- mIsFetching = FALSE;
- mFetchState = 0;
- mFetchPriority = 0;
- mDownloadProgress = 0.f;
- mFetchDeltaTime = 999999.f;
- mRequestDeltaTime = 0.f;
- mForSculpt = FALSE ;
- mIsFetched = FALSE ;
- mCachedRawImage = NULL ;
- mCachedRawDiscardLevel = -1 ;
- mCachedRawImageReady = FALSE ;
- mSavedRawImage = NULL ;
- mForceToSaveRawImage = FALSE ;
- mSaveRawImage = FALSE ;
- mSavedRawDiscardLevel = -1 ;
- mDesiredSavedRawDiscardLevel = -1 ;
- mLastReferencedSavedRawImageTime = 0.0f ;
- mKeptSavedRawImageTime = 0.f ;
- mLastCallBackActiveTime = 0.f;
- }
- LLViewerFetchedTexture::~LLViewerFetchedTexture()
- {
- //*NOTE getTextureFetch can return NULL when Viewer is shutting down.
- // This is due to LLWearableList is singleton and is destroyed after
- // LLAppViewer::cleanup() was called. (see ticket EXT-177)
- if (mHasFetcher && LLAppViewer::getTextureFetch())
- {
- LLAppViewer::getTextureFetch()->deleteRequest(getID(), true);
- }
- cleanup();
- }
- //virtual
- S8 LLViewerFetchedTexture::getType() const
- {
- return LLViewerTexture::FETCHED_TEXTURE ;
- }
- void LLViewerFetchedTexture::cleanup()
- {
- for(callback_list_t::iterator iter = mLoadedCallbackList.begin();
- iter != mLoadedCallbackList.end(); )
- {
- LLLoadedCallbackEntry *entryp = *iter++;
- // We never finished loading the image. Indicate failure.
- // Note: this allows mLoadedCallbackUserData to be cleaned up.
- entryp->mCallback( FALSE, this, NULL, NULL, 0, TRUE, entryp->mUserData );
- entryp->removeTexture(this) ;
- delete entryp;
- }
- mLoadedCallbackList.clear();
- mNeedsAux = FALSE;
-
- // Clean up image data
- destroyRawImage();
- mCachedRawImage = NULL ;
- mCachedRawDiscardLevel = -1 ;
- mCachedRawImageReady = FALSE ;
- mSavedRawImage = NULL ;
- }
- void LLViewerFetchedTexture::setForSculpt()
- {
- static const S32 MAX_INTERVAL = 8 ; //frames
- mForSculpt = TRUE ;
- if(isForSculptOnly() && !getBoundRecently())
- {
- destroyGLTexture() ; //sculpt image does not need gl texture.
- }
- checkCachedRawSculptImage() ;
- setMaxVirtualSizeResetInterval(MAX_INTERVAL) ;
- }
- BOOL LLViewerFetchedTexture::isForSculptOnly() const
- {
- return mForSculpt && !mNeedsGLTexture ;
- }
- BOOL LLViewerFetchedTexture::isDeleted()
- {
- return mTextureState == DELETED ;
- }
- BOOL LLViewerFetchedTexture::isInactive()
- {
- return mTextureState == INACTIVE ;
- }
- BOOL LLViewerFetchedTexture::isDeletionCandidate()
- {
- return mTextureState == DELETION_CANDIDATE ;
- }
- void LLViewerFetchedTexture::setDeletionCandidate()
- {
- if(mGLTexturep.notNull() && mGLTexturep->getTexName() && (mTextureState == INACTIVE))
- {
- mTextureState = DELETION_CANDIDATE ;
- }
- }
- //set the texture inactive
- void LLViewerFetchedTexture::setInactive()
- {
- if(mTextureState == ACTIVE && mGLTexturep.notNull() && mGLTexturep->getTexName() && !mGLTexturep->getBoundRecently())
- {
- mTextureState = INACTIVE ;
- }
- }
- BOOL LLViewerFetchedTexture::isFullyLoaded() const
- {
- // Unfortunately, the boolean "mFullyLoaded" is never updated correctly so we use that logic
- // to check if the texture is there and completely downloaded
- return (mFullWidth != 0) && (mFullHeight != 0) && !mIsFetching && !mHasFetcher;
- }
- // virtual
- void LLViewerFetchedTexture::dump()
- {
- LLViewerTexture::dump();
- llinfos << "Dump : " << mID
- << ", mIsMissingAsset = " << (S32)mIsMissingAsset
- << ", mFullWidth = " << (S32)mFullWidth
- << ", mFullHeight = " << (S32)mFullHeight
- << ", mOrigWidth = " << (S32)mOrigWidth
- << ", mOrigHeight = " << (S32)mOrigHeight
- << llendl;
- llinfos << " : "
- << " mFullyLoaded = " << (S32)mFullyLoaded
- << ", mFetchState = " << (S32)mFetchState
- << ", mFetchPriority = " << (S32)mFetchPriority
- << ", mDownloadProgress = " << (F32)mDownloadProgress
- << llendl;
- llinfos << " : "
- << " mHasFetcher = " << (S32)mHasFetcher
- << ", mIsFetching = " << (S32)mIsFetching
- << ", mIsFetched = " << (S32)mIsFetched
- << ", mBoostLevel = " << (S32)mBoostLevel
- << llendl;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // ONLY called from LLViewerFetchedTextureList
- void LLViewerFetchedTexture::destroyTexture()
- {
- if(LLImageGL::sGlobalTextureMemoryInBytes < sMaxDesiredTextureMemInBytes)//not ready to release unused memory.
- {
- return ;
- }
- if (mNeedsCreateTexture)//return if in the process of generating a new texture.
- {
- return ;
- }
-
- destroyGLTexture() ;
- mFullyLoaded = FALSE ;
- }
- void LLViewerFetchedTexture::addToCreateTexture()
- {
- bool force_update = false ;
- if (getComponents() != mRawImage->getComponents())
- {
- // We've changed the number of components, so we need to move any
- // objects using this pool to a different pool.
- mComponents = mRawImage->getComponents();
- mGLTexturep->setComponents(mComponents) ;
- force_update = true ;
- for(U32 i = 0 ; i < mNumFaces ; i++)
- {
- mFaceList[i]->dirtyTexture() ;
- }
- //discard the cached raw image and the saved raw image
- mCachedRawImageReady = FALSE ;
- mCachedRawDiscardLevel = -1 ;
- mCachedRawImage = NULL ;
- mSavedRawDiscardLevel = -1 ;
- mSavedRawImage = NULL ;
- }
- if(isForSculptOnly())
- {
- //just update some variables, not to create a real GL texture.
- createGLTexture(mRawDiscardLevel, mRawImage, 0, FALSE) ;
- mNeedsCreateTexture = FALSE ;
- destroyRawImage();
- }
- else if(!force_update && getDiscardLevel() > -1 && getDiscardLevel() <= mRawDiscardLevel)
- {
- mNeedsCreateTexture = FALSE ;
- destroyRawImage();
- }
- else
- {
- #if 1
- //
- //if mRequestedDiscardLevel > mDesiredDiscardLevel, we assume the required image res keep going up,
- //so do not scale down the over qualified image.
- //Note: scaling down image is expensensive. Do it only when very necessary.
- //
- if(mRequestedDiscardLevel <= mDesiredDiscardLevel && !mForceToSaveRawImage)
- {
- S32 w = mFullWidth >> mRawDiscardLevel;
- S32 h = mFullHeight >> mRawDiscardLevel;
- //if big image, do not load extra data
- //scale it down to size >= LLViewerTexture::sMinLargeImageSize
- if(w * h > LLViewerTexture::sMinLargeImageSize)
- {
- S32 d_level = llmin(mRequestedDiscardLevel, (S32)mDesiredDiscardLevel) - mRawDiscardLevel ;
-
- if(d_level > 0)
- {
- S32 i = 0 ;
- while((d_level > 0) && ((w >> i) * (h >> i) > LLViewerTexture::sMinLargeImageSize))
- {
- i++;
- d_level--;
- }
- if(i > 0)
- {
- mRawDiscardLevel += i ;
- if(mRawDiscardLevel >= getDiscardLevel() && getDiscardLevel() > 0)
- {
- mNeedsCreateTexture = FALSE ;
- destroyRawImage();
- return ;
- }
- mRawImage->scale(w >> i, h >> i) ;
- }
- }
- }
- }
- #endif
- mNeedsCreateTexture = TRUE;
- gTextureList.mCreateTextureList.insert(this);
- }
- return ;
- }
- // ONLY called from LLViewerTextureList
- BOOL LLViewerFetchedTexture::createTexture(S32 usename/*= 0*/)
- {
- if (!mNeedsCreateTexture)
- {
- destroyRawImage();
- return FALSE;
- }
- mNeedsCreateTexture = FALSE;
- if (mRawImage.isNull())
- {
- llerrs << "LLViewerTexture trying to create texture with no Raw Image" << llendl;
- }
- // llinfos << llformat("IMAGE Creating (%d) [%d x %d] Bytes: %d ",
- // mRawDiscardLevel,
- // mRawImage->getWidth(), mRawImage->getHeight(),mRawImage->getDataSize())
- // << mID.getString() << llendl;
- BOOL res = TRUE;
- // store original size only for locally-sourced images
- if (mUrl.compare(0, 7, "file://") == 0)
- {
- mOrigWidth = mRawImage->getWidth();
- mOrigHeight = mRawImage->getHeight();
-
- if (mBoostLevel == BOOST_PREVIEW)
- {
- mRawImage->biasedScaleToPowerOfTwo(1024);
- }
- else
- { // leave black border, do not scale image content
- mRawImage->expandToPowerOfTwo(MAX_IMAGE_SIZE, FALSE);
- }
-
- mFullWidth = mRawImage->getWidth();
- mFullHeight = mRawImage->getHeight();
- setTexelsPerImage();
- }
- else
- {
- mOrigWidth = mFullWidth;
- mOrigHeight = mFullHeight;
- }
- bool size_okay = true;
-
- U32 raw_width = mRawImage->getWidth() << mRawDiscardLevel;
- U32 raw_height = mRawImage->getHeight() << mRawDiscardLevel;
- if( raw_width > MAX_IMAGE_SIZE || raw_height > MAX_IMAGE_SIZE )
- {
- llinfos << "Width or height is greater than " << MAX_IMAGE_SIZE << ": (" << raw_width << "," << raw_height << ")" << llendl;
- size_okay = false;
- }
-
- if (!LLImageGL::checkSize(mRawImage->getWidth(), mRawImage->getHeight()))
- {
- // A non power-of-two image was uploaded (through a non standard client)
- llinfos << "Non power of two width or height: (" << mRawImage->getWidth() << "," << mRawImage->getHeight() << ")" << llendl;
- size_okay = false;
- }
-
- if( !size_okay )
- {
- // An inappropriately-sized image was uploaded (through a non standard client)
- // We treat these images as missing assets which causes them to
- // be renderd as 'missing image' and to stop requesting data
- setIsMissingAsset();
- destroyRawImage();
- return FALSE;
- }
-
- if(!(res = insertToAtlas()))
- {
- res = mGLTexturep->createGLTexture(mRawDiscardLevel, mRawImage, usename, TRUE, mBoostLevel);
- resetFaceAtlas() ;
- }
- setActive() ;
- if (!needsToSaveRawImage())
- {
- mNeedsAux = FALSE;
- destroyRawImage();
- }
- return res;
- }
- // Call with 0,0 to turn this feature off.
- //virtual
- void LLViewerFetchedTexture::setKnownDrawSize(S32 width, S32 height)
- {
- if(mKnownDrawWidth < width || mKnownDrawHeight < height)
- {
- mKnownDrawWidth = llmax(mKnownDrawWidth, width) ;
- mKnownDrawHeight = llmax(mKnownDrawHeight, height) ;
- mKnownDrawSizeChanged = TRUE ;
- mFullyLoaded = FALSE ;
- }
- addTextureStats((F32)(mKnownDrawWidth * mKnownDrawHeight));
- }
- //virtual
- void LLViewerFetchedTexture::processTextureStats()
- {
- if(mFullyLoaded)
- {
- if(mDesiredDiscardLevel > mMinDesiredDiscardLevel)//need to load more
- {
- mDesiredDiscardLevel = llmin(mDesiredDiscardLevel, mMinDesiredDiscardLevel) ;
- mFullyLoaded = FALSE ;
- }
- }
- else
- {
- updateVirtualSize() ;
-
- static LLCachedControl<bool> textures_fullres(gSavedSettings,"TextureLoadFullRes");
-
- if (textures_fullres)
- {
- mDesiredDiscardLevel = 0;
- }
- else if(!mFullWidth || !mFullHeight)
- {
- mDesiredDiscardLevel = llmin(getMaxDiscardLevel(), (S32)mLoadedCallbackDesiredDiscardLevel) ;
- }
- else
- {
- if(!mKnownDrawWidth || !mKnownDrawHeight || mFullWidth <= mKnownDrawWidth || mFullHeight <= mKnownDrawHeight)
- {
- if (mFullWidth > MAX_IMAGE_SIZE_DEFAULT || mFullHeight > MAX_IMAGE_SIZE_DEFAULT)
- {
- mDesiredDiscardLevel = 1; // MAX_IMAGE_SIZE_DEFAULT = 1024 and max size ever is 2048
- }
- else
- {
- mDesiredDiscardLevel = 0;
- }
- }
- else if(mKnownDrawSizeChanged)//known draw size is set
- {
- mDesiredDiscardLevel = (S8)llmin(log((F32)mFullWidth / mKnownDrawWidth) / log_2,
- log((F32)mFullHeight / mKnownDrawHeight) / log_2) ;
- mDesiredDiscardLevel = llclamp(mDesiredDiscardLevel, (S8)0, (S8)getMaxDiscardLevel()) ;
- mDesiredDiscardLevel = llmin(mDesiredDiscardLevel, mMinDesiredDiscardLevel) ;
- }
- mKnownDrawSizeChanged = FALSE ;
-
- if(getDiscardLevel() >= 0 && (getDiscardLevel() <= mDesiredDiscardLevel))
- {
- mFullyLoaded = TRUE ;
- }
- }
- }
- if(mForceToSaveRawImage && mDesiredSavedRawDiscardLevel >= 0) //force to refetch the texture.
- {
- mDesiredDiscardLevel = llmin(mDesiredDiscardLevel, (S8)mDesiredSavedRawDiscardLevel) ;
- if(getDiscardLevel() < 0 || getDiscardLevel() > mDesiredDiscardLevel)
- {
- mFullyLoaded = FALSE ;
- }
- }
- }
- const F32 MAX_PRIORITY_PIXEL = 999.f ; //pixel area
- const F32 PRIORITY_BOOST_LEVEL_FACTOR = 1000.f ; //boost level
- const F32 PRIORITY_DELTA_DISCARD_LEVEL_FACTOR = 100000.f ; //delta discard
- const S32 MAX_DELTA_DISCARD_LEVEL_FOR_PRIORITY = 4 ;
- const F32 PRIORITY_ADDITIONAL_FACTOR = 1000000.f ; //additional
- const S32 MAX_ADDITIONAL_LEVEL_FOR_PRIORITY = 8 ;
- const F32 PRIORITY_BOOST_HIGH_FACTOR = 10000000.f ;//boost high
- F32 LLViewerFetchedTexture::calcDecodePriority()
- {
- #ifndef LL_RELEASE_FOR_DOWNLOAD
- if (mID == LLAppViewer::getTextureFetch()->mDebugID)
- {
- LLAppViewer::getTextureFetch()->mDebugCount++; // for setting breakpoints
- }
- #endif
-
- if (mNeedsCreateTexture)
- {
- return mDecodePriority; // no change while waiting to create
- }
- if(mFullyLoaded && !mForceToSaveRawImage)//already loaded for static texture
- {
- return -1.0f ; //alreay fetched
- }
- S32 cur_discard = getCurrentDiscardLevelForFetching();
- bool have_all_data = (cur_discard >= 0 && (cur_discard <= mDesiredDiscardLevel));
- F32 pixel_priority = (F32) sqrt(mMaxVirtualSize);
- F32 priority = 0.f;
- if (mIsMissingAsset)
- {
- priority = 0.0f;
- }
- else if(mDesiredDiscardLevel >= cur_discard && cur_discard > -1)
- {
- priority = -2.0f ;
- }
- else if(mCachedRawDiscardLevel > -1 && mDesiredDiscardLevel >= mCachedRawDiscardLevel)
- {
- priority = -3.0f;
- }
- else if (mDesiredDiscardLevel > getMaxDiscardLevel())
- {
- // Don't decode anything we don't need
- priority = -4.0f;
- }
- else if ((mBoostLevel == LLViewerTexture::BOOST_UI || mBoostLevel == LLViewerTexture::BOOST_ICON) && !have_all_data)
- {
- priority = 1.f;
- }
- else if (pixel_priority < 0.001f && !have_all_data)
- {
- // Not on screen but we might want some data
- if (mBoostLevel > BOOST_HIGH)
- {
- // Always want high boosted images
- priority = 1.f;
- }
- else
- {
- priority = -5.f; //stop fetching
- }
- }
- else if (cur_discard < 0)
- {
- //texture does not have any data, so we don't know the size of the image, treat it like 32 * 32.
- // priority range = 100,000 - 500,000
- static const F64 log_2 = log(2.0);
- F32 desired = (F32)(log(32.0/pixel_priority) / log_2);
- S32 ddiscard = MAX_DISCARD_LEVEL - (S32)desired;
- ddiscard = llclamp(ddiscard, 0, MAX_DELTA_DISCARD_LEVEL_FOR_PRIORITY);
- priority = (ddiscard + 1) * PRIORITY_DELTA_DISCARD_LEVEL_FACTOR;
- setAdditionalDecodePriority(1.0f) ;//boost the textures without any data so far.
- }
- else if ((mMinDiscardLevel > 0) && (cur_discard <= mMinDiscardLevel))
- {
- // larger mips are corrupted
- priority = -6.0f;
- }
- else
- {
- // priority range = 100,000 - 500,000
- S32 desired_discard = mDesiredDiscardLevel;
- if (!isJustBound() && mCachedRawImageReady)
- {
- if(mBoostLevel < BOOST_HIGH)
- {
- // We haven't rendered this in a while, de-prioritize it
- desired_discard += 2;
- }
- else
- {
- // We haven't rendered this in the last half second, and we have a cached raw image, leave the desired discard as-is
- desired_discard = cur_discard;
- }
- }
- S32 ddiscard = cur_discard - desired_discard;
- ddiscard = llclamp(ddiscard, -1, MAX_DELTA_DISCARD_LEVEL_FOR_PRIORITY);
- priority = (ddiscard + 1) * PRIORITY_DELTA_DISCARD_LEVEL_FACTOR;
- }
- // Priority Formula:
- // BOOST_HIGH + ADDITIONAL PRI + DELTA DISCARD + BOOST LEVEL + PIXELS
- // [10,000,000] + [1,000,000-9,000,000] + [100,000-500,000] + [1-20,000] + [0-999]
- if (priority > 0.0f)
- {
- bool large_enough = mCachedRawImageReady && ((S32)mTexelsPerImage > sMinLargeImageSize) ;
- if(large_enough)
- {
- //Note:
- //to give small, low-priority textures some chance to be fetched,
- //cut the priority in half if the texture size is larger than 256 * 256 and has a 64*64 ready.
- priority *= 0.5f ;
- }
- pixel_priority = llclamp(pixel_priority, 0.0f, MAX_PRIORITY_PIXEL);
- priority += pixel_priority + PRIORITY_BOOST_LEVEL_FACTOR * mBoostLevel;
- if ( mBoostLevel > BOOST_HIGH)
- {
- if(mBoostLevel > BOOST_SUPER_HIGH)
- {
- //for very important textures, always grant the highest priority.
- priority += PRIORITY_BOOST_HIGH_FACTOR;
- }
- else if(mCachedRawImageReady)
- {
- //Note:
- //to give small, low-priority textures some chance to be fetched,
- //if high priority texture has a 64*64 ready, lower its fetching priority.
- setAdditionalDecodePriority(0.5f) ;
- }
- else
- {
- priority += PRIORITY_BOOST_HIGH_FACTOR;
- }
- }
- if(mAdditionalDecodePriority > 0.0f)
- {
- // priority range += 1,000,000.f-9,000,000.f
- F32 additional = PRIORITY_ADDITIONAL_FACTOR * (1.0 + mAdditionalDecodePriority * MAX_ADDITIONAL_LEVEL_FOR_PRIORITY);
- if(large_enough)
- {
- //Note:
- //to give small, low-priority textures some chance to be fetched,
- //cut the additional priority to a quarter if the texture size is larger than 256 * 256 and has a 64*64 ready.
- additional *= 0.25f ;
- }
- priority += additional;
- }
- }
- return priority;
- }
- //static
- F32 LLViewerFetchedTexture::maxDecodePriority()
- {
- static const F32 max_priority = PRIORITY_BOOST_HIGH_FACTOR + //boost_high
- PRIORITY_ADDITIONAL_FACTOR * (MAX_ADDITIONAL_LEVEL_FOR_PRIORITY + 1) + //additional (view dependent factors)
- PRIORITY_DELTA_DISCARD_LEVEL_FACTOR * (MAX_DELTA_DISCARD_LEVEL_FOR_PRIORITY + 1) + //delta discard
- PRIORITY_BOOST_LEVEL_FACTOR * (BOOST_MAX_LEVEL - 1) + //boost level
- MAX_PRIORITY_PIXEL + 1.0f ; //pixel area.
-
- return max_priority ;
- }
- //============================================================================
- void LLViewerFetchedTexture::setDecodePriority(F32 priority)
- {
- llassert(!mInImageList);
-
- mDecodePriority = priority;
- if(mDecodePriority < F_ALMOST_ZERO)
- {
- mStopFetchingTimer.reset() ;
- }
- }
- void LLViewerFetchedTexture::setAdditionalDecodePriority(F32 priority)
- {
- priority = llclamp(priority, 0.f, 1.f);
- if(mAdditionalDecodePriority < priority)
- {
- mAdditionalDecodePriority = priority;
- }
- }
- void LLViewerFetchedTexture::updateVirtualSize()
- {
- if(!mMaxVirtualSizeResetCounter)
- {
- addTextureStats(0.f, FALSE) ;//reset
- }
- for(U32 i = 0 ; i < mNumFaces ; i++)
- {
- LLFace* facep = mFaceList[i] ;
- if(facep->getDrawable()->isRecentlyVisible())
- {
- addTextureStats(facep->getVirtualSize()) ;
- setAdditionalDecodePriority(facep->getImportanceToCamera()) ;
- }
- }
- if(mMa…
Large files files are truncated, but you can click here to view the full file