PageRenderTime 20ms CodeModel.GetById 7ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llrender/llfontbitmapcache.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 163 lines | 106 code | 26 blank | 31 comment | 10 complexity | f2fff381ac078a3a335efe5e758612c9 MD5 | raw file
  1/** 
  2 * @file llfontbitmapcache.cpp
  3 * @brief Storage for previously rendered glyphs.
  4 *
  5 * $LicenseInfo:firstyear=2008&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 "linden_common.h"
 28
 29#include "llgl.h"
 30#include "llfontbitmapcache.h"
 31
 32LLFontBitmapCache::LLFontBitmapCache():
 33	mNumComponents(0),
 34	mBitmapWidth(0),
 35	mBitmapHeight(0),
 36	mBitmapNum(-1),
 37	mMaxCharWidth(0),
 38	mMaxCharHeight(0),
 39	mCurrentOffsetX(1),
 40	mCurrentOffsetY(1)
 41{
 42}
 43
 44LLFontBitmapCache::~LLFontBitmapCache()
 45{
 46}
 47
 48void LLFontBitmapCache::init(S32 num_components,
 49							 S32 max_char_width,
 50							 S32 max_char_height)
 51{
 52	reset();
 53	
 54	mNumComponents = num_components;
 55	mMaxCharWidth = max_char_width;
 56	mMaxCharHeight = max_char_height;
 57}
 58
 59LLImageRaw *LLFontBitmapCache::getImageRaw(U32 bitmap_num) const
 60{
 61	if (bitmap_num >= mImageRawVec.size())
 62		return NULL;
 63
 64	return mImageRawVec[bitmap_num];
 65}
 66
 67LLImageGL *LLFontBitmapCache::getImageGL(U32 bitmap_num) const
 68{
 69	if (bitmap_num >= mImageGLVec.size())
 70		return NULL;
 71
 72	return mImageGLVec[bitmap_num];
 73}
 74
 75
 76BOOL LLFontBitmapCache::nextOpenPos(S32 width, S32 &pos_x, S32 &pos_y, S32& bitmap_num)
 77{
 78	if ((mBitmapNum<0) || (mCurrentOffsetX + width + 1) > mBitmapWidth)
 79	{
 80		if ((mBitmapNum<0) || (mCurrentOffsetY + 2*mMaxCharHeight + 2) > mBitmapHeight)
 81		{
 82			// We're out of space in the current image, or no image
 83			// has been allocated yet.  Make a new one.
 84			mImageRawVec.push_back(new LLImageRaw);
 85			mBitmapNum = mImageRawVec.size()-1;
 86			LLImageRaw *image_raw = getImageRaw(mBitmapNum);
 87
 88			// Make corresponding GL image.
 89			mImageGLVec.push_back(new LLImageGL(FALSE));
 90			LLImageGL *image_gl = getImageGL(mBitmapNum);
 91			
 92			S32 image_width = mMaxCharWidth * 20;
 93			S32 pow_iw = 2;
 94			while (pow_iw < image_width)
 95			{
 96				pow_iw *= 2;
 97			}
 98			image_width = pow_iw;
 99			image_width = llmin(512, image_width); // Don't make bigger than 512x512, ever.
100			S32 image_height = image_width;
101
102			image_raw->resize(image_width, image_height, mNumComponents);
103
104			mBitmapWidth = image_width;
105			mBitmapHeight = image_height;
106
107			switch (mNumComponents)
108			{
109				case 1:
110					image_raw->clear();
111				break;
112				case 2:
113					image_raw->clear(255, 0);
114				break;
115			}
116
117			// Start at beginning of the new image.
118			mCurrentOffsetX = 1;
119			mCurrentOffsetY = 1;
120
121			// Attach corresponding GL texture.
122			image_gl->createGLTexture(0, image_raw);
123			gGL.getTexUnit(0)->bind(image_gl);
124			image_gl->setFilteringOption(LLTexUnit::TFO_POINT); // was setMipFilterNearest(TRUE, TRUE);
125		}
126		else
127		{
128			// Move to next row in current image.
129			mCurrentOffsetX = 1;
130			mCurrentOffsetY += mMaxCharHeight + 1;
131		}
132	}
133
134	pos_x = mCurrentOffsetX;
135	pos_y = mCurrentOffsetY;
136	bitmap_num = mBitmapNum;
137
138	mCurrentOffsetX += width + 1;
139
140	return TRUE;
141}
142
143void LLFontBitmapCache::destroyGL()
144{
145	for (std::vector<LLPointer<LLImageGL> >::iterator it = mImageGLVec.begin();
146		 it != mImageGLVec.end(); ++it)
147	{
148		(*it)->destroyGLTexture();
149	}
150}
151
152void LLFontBitmapCache::reset()
153{
154	mImageRawVec.clear();
155	mImageGLVec.clear();
156	
157	mBitmapWidth = 0;
158	mBitmapHeight = 0;
159	mBitmapNum = -1;
160	mCurrentOffsetX = 1;
161	mCurrentOffsetY = 1;
162}
163