llfontbitmapcache.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @file llfontbitmapcache.cpp
  3. * @brief Storage for previously rendered glyphs.
  4. *
  5. * $LicenseInfo:firstyear=2008&license=viewergpl$
  6. *
  7. * Copyright (c) 2008-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llfontbitmapcache.h"
  34. #include "llgl.h"
  35. LLFontBitmapCache::LLFontBitmapCache()
  36. : mMaxCharWidth(0),
  37. mMaxCharHeight(0),
  38. mBitmapWidth(0),
  39. mBitmapHeight(0)
  40. {
  41. // Note: we cheat for speed, avoiding a for() loop. This works as long as
  42. // EFontGlyphType::Count == 2. HB
  43. mCurrentOffsetX[0] = mCurrentOffsetX[1] = 1;
  44. mCurrentOffsetY[0] = mCurrentOffsetY[1] = 1;
  45. }
  46. void LLFontBitmapCache::reset()
  47. {
  48. mBitmapWidth = mBitmapHeight = 0;
  49. // Note: we cheat for speed, avoiding a for() loop. This works as long as
  50. // EFontGlyphType::Count == 2. HB
  51. mImageRawVec[0].clear();
  52. mImageRawVec[1].clear();
  53. mImageGLVec[0].clear();
  54. mImageGLVec[1].clear();
  55. mCurrentOffsetX[0] = mCurrentOffsetX[1] = 1;
  56. mCurrentOffsetY[0] = mCurrentOffsetY[1] = 1;
  57. }
  58. void LLFontBitmapCache::init(S32 max_char_width, S32 max_char_height)
  59. {
  60. reset();
  61. mMaxCharWidth = max_char_width;
  62. mMaxCharHeight = max_char_height;
  63. S32 image_width = mMaxCharWidth * 20;
  64. S32 pow_iw = 2;
  65. while (pow_iw < image_width)
  66. {
  67. pow_iw *= 2;
  68. }
  69. image_width = llmin(pow_iw, 512); // Never bigger than 512x512 !
  70. mBitmapWidth = mBitmapHeight = image_width;
  71. }
  72. LLImageRaw* LLFontBitmapCache::getImageRaw(U32 b_type, U32 b_num) const
  73. {
  74. if (b_type >= EFontGlyphType::Count ||
  75. (size_t)b_num >= mImageRawVec[b_type].size())
  76. {
  77. return NULL;
  78. }
  79. return mImageRawVec[b_type][b_num].get();
  80. }
  81. LLImageGL* LLFontBitmapCache::getImageGL(U32 b_type, U32 b_num) const
  82. {
  83. if (b_type >= EFontGlyphType::Count ||
  84. (size_t)b_num >= mImageGLVec[b_type].size())
  85. {
  86. return NULL;
  87. }
  88. return mImageGLVec[b_type][b_num].get();
  89. }
  90. void LLFontBitmapCache::nextOpenPos(S32 width, S32& pos_x, S32& pos_y,
  91. U32 b_type, U32& bitmap_num)
  92. {
  93. if (b_type >= EFontGlyphType::Count)
  94. {
  95. return;
  96. }
  97. bool no_image = mImageRawVec[b_type].empty();
  98. if (no_image || mCurrentOffsetX[b_type] + width + 1 > mBitmapWidth)
  99. {
  100. if (no_image ||
  101. mCurrentOffsetY[b_type] + 2 * mMaxCharHeight + 2 > mBitmapHeight)
  102. {
  103. // We Are out of space in the current image, or no image has been
  104. // allocated yet. Make a new one.
  105. S32 image_width = mMaxCharWidth * 20;
  106. S32 pow_iw = 2;
  107. while (pow_iw < image_width)
  108. {
  109. pow_iw *= 2;
  110. }
  111. image_width = llmin(pow_iw, 512); // Never bigger than 512x512 !
  112. S32 image_height = image_width;
  113. mBitmapWidth = image_width;
  114. mBitmapHeight = image_height;
  115. U32 num_components = getNumComponents(b_type);
  116. mImageRawVec[b_type].emplace_back(new LLImageRaw(mBitmapWidth,
  117. mBitmapHeight,
  118. num_components));
  119. bitmap_num = mImageRawVec[b_type].size() - 1;
  120. LLImageRaw* imagep = getImageRaw(b_type, bitmap_num);
  121. if (num_components == 2)
  122. {
  123. imagep->clear(255, 0);
  124. }
  125. // Make corresponding GL image.
  126. mImageGLVec[b_type].emplace_back(new LLImageGL(imagep, false));
  127. LLImageGL* glimgp = getImageGL(b_type, bitmap_num);
  128. // Start at beginning of the new image.
  129. mCurrentOffsetX[b_type] = 1;
  130. mCurrentOffsetY[b_type] = 1;
  131. // Attach corresponding GL texture.
  132. gGL.getTexUnit(0)->bind(glimgp);
  133. // Was: setMipFilterNearest(true, true);
  134. glimgp->setFilteringOption(LLTexUnit::TFO_POINT);
  135. }
  136. else
  137. {
  138. // Move to next row in current image.
  139. mCurrentOffsetX[b_type] = 1;
  140. mCurrentOffsetY[b_type] += mMaxCharHeight + 1;
  141. }
  142. }
  143. pos_x = mCurrentOffsetX[b_type];
  144. pos_y = mCurrentOffsetY[b_type];
  145. bitmap_num = getNumBitmaps(b_type) - 1;
  146. mCurrentOffsetX[b_type] += width + 1;
  147. }
  148. void LLFontBitmapCache::destroyGL()
  149. {
  150. for (U32 j = 0; j < EFontGlyphType::Count; ++j)
  151. {
  152. for (size_t i = 0, count = mImageGLVec[j].size(); i < count; ++i)
  153. {
  154. mImageGLVec[j][i]->destroyGLTexture();
  155. }
  156. }
  157. }