Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.9 KiB

  1. //---------------------------------------------------------------------------
  2. // Cache.cpp - implements the CRenderCache object
  3. //---------------------------------------------------------------------------
  4. #include "stdafx.h"
  5. #include "Cache.h"
  6. #include "Info.h"
  7. #include "tmutils.h"
  8. //---------------------------------------------------------------------------
  9. CRenderCache::CRenderCache(CRenderObj *pRender, __int64 iUniqueId)
  10. {
  11. strcpy(_szHead, "rcache");
  12. strcpy(_szTail, "end");
  13. _pRenderObj = pRender;
  14. _iUniqueId = iUniqueId;
  15. _hFont = NULL;
  16. _plfFont = NULL;
  17. }
  18. //---------------------------------------------------------------------------
  19. CRenderCache::~CRenderCache()
  20. {
  21. //---- delete bitmaps ----
  22. int cnt = _BitmapCache.GetSize();
  23. for (int i=0; i < cnt; i++)
  24. {
  25. Log(LOG_CACHE, L"DELETE cache bitmap: 0x%x", _BitmapCache[i].hBitmap);
  26. DeleteObject(_BitmapCache[i].hBitmap);
  27. }
  28. //---- delete font ----
  29. if (_hFont)
  30. DeleteObject(_hFont);
  31. strcpy(_szHead, "deleted");
  32. }
  33. //---------------------------------------------------------------------------
  34. HRESULT CRenderCache::GetBitmap(int iDibOffset, OUT HBITMAP *phBitmap)
  35. {
  36. HRESULT hr = S_OK;
  37. int cnt = _BitmapCache.GetSize();
  38. for (int i=0; i < cnt; i++)
  39. {
  40. BITMAPENTRY *be = &_BitmapCache[i];
  41. if (be->iDibOffset == iDibOffset)
  42. {
  43. Log(LOG_TM, L"GetBitmap - CACHE HIT: class=%s, diboffset=%d, bitmap=0x%x",
  44. SHARECLASS(_pRenderObj), be->iDibOffset, be->hBitmap);
  45. *phBitmap = be->hBitmap;
  46. goto exit;
  47. }
  48. }
  49. //---- no match found ----
  50. hr = MakeError32(ERROR_NOT_FOUND);
  51. exit:
  52. return hr;
  53. }
  54. //---------------------------------------------------------------------------
  55. HRESULT CRenderCache::AddBitmap(int iDibOffset, HBITMAP hBitmap)
  56. {
  57. HRESULT hr = S_OK;
  58. BITMAPENTRY entry;
  59. //---- add new entry for our part/state ----
  60. entry.iDibOffset = iDibOffset;
  61. entry.hBitmap = hBitmap;
  62. //entry.iRefCount = 1; // new entry
  63. Log(LOG_CACHE, L"ADD cache bitmap: 0x%x", entry.hBitmap);
  64. if (! _BitmapCache.Add(entry))
  65. hr = MakeError32(E_OUTOFMEMORY);
  66. return hr;
  67. }
  68. //---------------------------------------------------------------------------
  69. void CRenderCache::ReturnBitmap(HBITMAP hBitmap)
  70. {
  71. }
  72. //---------------------------------------------------------------------------
  73. HRESULT CRenderCache::GetScaledFontHandle(HDC hdc, LOGFONT *plfUnscaled, HFONT *phFont)
  74. {
  75. HRESULT hr = S_OK;
  76. //---- caches one font only ----
  77. if ((! _plfFont) || (! FONTCOMPARE(*_plfFont, *plfUnscaled)))
  78. {
  79. Log(LOG_TM, L"Font CACHE MISS: %s", plfUnscaled->lfFaceName);
  80. if (_hFont)
  81. {
  82. DeleteObject(_hFont);
  83. _hFont = NULL;
  84. _plfFont = NULL;
  85. }
  86. LOGFONT lfScaled = *plfUnscaled;
  87. //---- convert to current screen dpi ----
  88. ScaleFontForHdcDpi(hdc, &lfScaled);
  89. _hFont = CreateFontIndirect(&lfScaled);
  90. if (! _hFont)
  91. {
  92. hr = MakeError32(E_OUTOFMEMORY);
  93. goto exit;
  94. }
  95. _plfFont = plfUnscaled;
  96. }
  97. else
  98. Log(LOG_TM, L"Font CACHE HIT");
  99. *phFont = _hFont;
  100. exit:
  101. return hr;
  102. }
  103. //---------------------------------------------------------------------------
  104. void CRenderCache::ReturnFontHandle(HFONT hFont)
  105. {
  106. }
  107. //---------------------------------------------------------------------------
  108. BOOL CRenderCache::ValidateObj()
  109. {
  110. BOOL fValid = TRUE;
  111. //---- check object quickly ----
  112. if ( (! this)
  113. || (ULONGAT(_szHead) != 'cacr') // "rcac"
  114. || (ULONGAT(&_szHead[4]) != 'eh') // "he"
  115. || (ULONGAT(_szTail) != 'dne')) // "end"
  116. {
  117. Log(LOG_ERROR, L"Corrupt CRenderCache object: 0x%08x", this);
  118. fValid = FALSE;
  119. }
  120. return fValid;
  121. }
  122. //---------------------------------------------------------------------------