PageRenderTime 134ms CodeModel.GetById 1ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  27. #include "llgl.h"
  28. #include "llfontbitmapcache.h"
  29. LLFontBitmapCache::LLFontBitmapCache():
  30. mNumComponents(0),
  31. mBitmapWidth(0),
  32. mBitmapHeight(0),
  33. mBitmapNum(-1),
  34. mMaxCharWidth(0),
  35. mMaxCharHeight(0),
  36. mCurrentOffsetX(1),
  37. mCurrentOffsetY(1)
  38. {
  39. }
  40. LLFontBitmapCache::~LLFontBitmapCache()
  41. {
  42. }
  43. void LLFontBitmapCache::init(S32 num_components,
  44. S32 max_char_width,
  45. S32 max_char_height)
  46. {
  47. reset();
  48. mNumComponents = num_components;
  49. mMaxCharWidth = max_char_width;
  50. mMaxCharHeight = max_char_height;
  51. }
  52. LLImageRaw *LLFontBitmapCache::getImageRaw(U32 bitmap_num) const
  53. {
  54. if (bitmap_num >= mImageRawVec.size())
  55. return NULL;
  56. return mImageRawVec[bitmap_num];
  57. }
  58. LLImageGL *LLFontBitmapCache::getImageGL(U32 bitmap_num) const
  59. {
  60. if (bitmap_num >= mImageGLVec.size())
  61. return NULL;
  62. return mImageGLVec[bitmap_num];
  63. }
  64. BOOL LLFontBitmapCache::nextOpenPos(S32 width, S32 &pos_x, S32 &pos_y, S32& bitmap_num)
  65. {
  66. if ((mBitmapNum<0) || (mCurrentOffsetX + width + 1) > mBitmapWidth)
  67. {
  68. if ((mBitmapNum<0) || (mCurrentOffsetY + 2*mMaxCharHeight + 2) > mBitmapHeight)
  69. {
  70. // We're out of space in the current image, or no image
  71. // has been allocated yet. Make a new one.
  72. mImageRawVec.push_back(new LLImageRaw);
  73. mBitmapNum = mImageRawVec.size()-1;
  74. LLImageRaw *image_raw = getImageRaw(mBitmapNum);
  75. // Make corresponding GL image.
  76. mImageGLVec.push_back(new LLImageGL(FALSE));
  77. LLImageGL *image_gl = getImageGL(mBitmapNum);
  78. S32 image_width = mMaxCharWidth * 20;
  79. S32 pow_iw = 2;
  80. while (pow_iw < image_width)
  81. {
  82. pow_iw *= 2;
  83. }
  84. image_width = pow_iw;
  85. image_width = llmin(512, image_width); // Don't make bigger than 512x512, ever.
  86. S32 image_height = image_width;
  87. image_raw->resize(image_width, image_height, mNumComponents);
  88. mBitmapWidth = image_width;
  89. mBitmapHeight = image_height;
  90. switch (mNumComponents)
  91. {
  92. case 1:
  93. image_raw->clear();
  94. break;
  95. case 2:
  96. image_raw->clear(255, 0);
  97. break;
  98. }
  99. // Start at beginning of the new image.
  100. mCurrentOffsetX = 1;
  101. mCurrentOffsetY = 1;
  102. // Attach corresponding GL texture.
  103. image_gl->createGLTexture(0, image_raw);
  104. gGL.getTexUnit(0)->bind(image_gl);
  105. image_gl->setFilteringOption(LLTexUnit::TFO_POINT); // was setMipFilterNearest(TRUE, TRUE);
  106. }
  107. else
  108. {
  109. // Move to next row in current image.
  110. mCurrentOffsetX = 1;
  111. mCurrentOffsetY += mMaxCharHeight + 1;
  112. }
  113. }
  114. pos_x = mCurrentOffsetX;
  115. pos_y = mCurrentOffsetY;
  116. bitmap_num = mBitmapNum;
  117. mCurrentOffsetX += width + 1;
  118. return TRUE;
  119. }
  120. void LLFontBitmapCache::destroyGL()
  121. {
  122. for (std::vector<LLPointer<LLImageGL> >::iterator it = mImageGLVec.begin();
  123. it != mImageGLVec.end(); ++it)
  124. {
  125. (*it)->destroyGLTexture();
  126. }
  127. }
  128. void LLFontBitmapCache::reset()
  129. {
  130. mImageRawVec.clear();
  131. mImageGLVec.clear();
  132. mBitmapWidth = 0;
  133. mBitmapHeight = 0;
  134. mBitmapNum = -1;
  135. mCurrentOffsetX = 1;
  136. mCurrentOffsetY = 1;
  137. }