DisplayManager.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // DisplayManager.cpp
  3. // konvergo
  4. //
  5. // Created by Lionel CHAZALLON on 28/09/2014.
  6. //
  7. //
  8. #include "QsLog.h"
  9. #include "DisplayManager.h"
  10. #include "math.h"
  11. ///////////////////////////////////////////////////////////////////////////////////////////////////
  12. DisplayManager::DisplayManager(QObject* parent) : QObject(parent) {}
  13. ///////////////////////////////////////////////////////////////////////////////////////////////////
  14. bool DisplayManager::initialize()
  15. {
  16. QLOG_INFO() << QString("DisplayManager found %1 Display(s).").arg(m_displays.size());
  17. // list video modes
  18. for(int displayid : m_displays.keys())
  19. {
  20. DMDisplayPtr display = m_displays[displayid];
  21. QLOG_INFO() << QString("Available modes for Display #%1 (%2)").arg(displayid).arg(display->m_name);
  22. for (int modeid = 0; modeid < display->m_videoModes.size(); modeid++)
  23. {
  24. DMVideoModePtr mode = display->m_videoModes[modeid];
  25. QLOG_INFO() << QString("Mode %1: %2").arg(modeid, 2).arg(mode->getPrettyName());
  26. }
  27. }
  28. // Log current display mode
  29. int mainDisplay = getMainDisplay();
  30. if (mainDisplay >= 0)
  31. {
  32. int currentMode = getCurrentDisplayMode(mainDisplay);
  33. if (currentMode >= 0)
  34. QLOG_INFO() << QString("DisplayManager : Current Display Mode on Display #%1 is %2")
  35. .arg(mainDisplay)
  36. .arg(m_displays[mainDisplay]->m_videoModes[currentMode]->getPrettyName());
  37. else
  38. QLOG_ERROR() << "DisplayManager : unable to retrieve current video mode";
  39. }
  40. else
  41. QLOG_ERROR() << "DisplayManager : unable to retrieve main display";
  42. return true;
  43. }
  44. ///////////////////////////////////////////////////////////////////////////////////////////////////
  45. DMVideoModePtr DisplayManager::getCurrentVideoMode(int display)
  46. {
  47. int currentMode = getCurrentDisplayMode(display);
  48. DMVideoModePtr currentVideoMode;
  49. if (currentMode >= 0)
  50. currentVideoMode = m_displays[display]->m_videoModes[currentMode];
  51. return currentVideoMode;
  52. }
  53. ///////////////////////////////////////////////////////////////////////////////////////////////////
  54. bool DisplayManager::isValidDisplay(int display) { return m_displays.contains(display); }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. bool DisplayManager::isValidDisplayMode(int display, int mode)
  57. {
  58. if (isValidDisplay(display))
  59. if (mode >= 0 && mode < m_displays[display]->m_videoModes.size())
  60. return true;
  61. return false;
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////
  64. bool DisplayManager::isRateMultipleOf(float refresh, float multiple, bool exact)
  65. {
  66. if (((int)refresh == 0) || ((int)multiple == 0))
  67. return false;
  68. if (((int)multiple % (int)refresh) == 0)
  69. return true;
  70. int roundedRefresh = (int)round(refresh);
  71. int roundedMultiple = (int)round(multiple);
  72. return ((roundedMultiple % roundedRefresh) == 0) && (!exact);
  73. }
  74. ///////////////////////////////////////////////////////////////////////////////////////////////////
  75. int DisplayManager::findBestMatch(int display, DMMatchMediaInfo& matchInfo)
  76. {
  77. // Grab current videomode information
  78. DMVideoModePtr currentVideoMode = getCurrentVideoMode(display);
  79. if (!currentVideoMode)
  80. return -1;
  81. // now we try to find the exact match in current resolution
  82. // then fill a list
  83. DMVideoModeWeightMap weights;
  84. DMVideoModeMap::const_iterator modeit = m_displays[display]->m_videoModes.constBegin();
  85. while (modeit != m_displays[display]->m_videoModes.constEnd())
  86. {
  87. DMVideoModePtr candidate = modeit.value();
  88. weights[candidate->m_id] = DMVideoModeWeightPtr(new DMVideoModeWeight);
  89. weights[candidate->m_id]->m_mode = candidate;
  90. weights[candidate->m_id]->m_weight = 0;
  91. // Weight Resolution match
  92. if ((candidate->m_width == currentVideoMode->m_width) &&
  93. (candidate->m_height == currentVideoMode->m_height) &&
  94. (candidate->m_bitsPerPixel == currentVideoMode->m_bitsPerPixel))
  95. {
  96. weights[candidate->m_id]->m_weight += MATCH_WEIGHT_RES;
  97. }
  98. // weight refresh rate
  99. // exact Match
  100. if (candidate->m_refreshRate == matchInfo.m_refreshRate)
  101. weights[candidate->m_id]->m_weight += MATCH_WEIGHT_REFRESH_RATE_EXACT;
  102. // exact multiple refresh rate
  103. if (isRateMultipleOf(matchInfo.m_refreshRate, candidate->m_refreshRate, true))
  104. weights[candidate->m_id]->m_weight += MATCH_WEIGHT_REFRESH_RATE_MULTIPLE;
  105. // close refresh match (less than 1 hz diff to match all 23.xxx modes to 24p)
  106. if (fabs(candidate->m_refreshRate - matchInfo.m_refreshRate) <= 0.5)
  107. {
  108. weights[candidate->m_id]->m_weight += MATCH_WEIGHT_REFRESH_RATE_CLOSE;
  109. }
  110. // approx multiple refresh rate
  111. if (isRateMultipleOf(matchInfo.m_refreshRate, candidate->m_refreshRate, false))
  112. weights[candidate->m_id]->m_weight += MATCH_WEIGHT_REFRESH_RATE_MULTIPLE_CLOSE;
  113. // weight interlacing
  114. if (candidate->m_interlaced == matchInfo.m_interlaced)
  115. weights[candidate->m_id]->m_weight += MATCH_WEIGHT_INTERLACE;
  116. if (candidate->m_id == currentVideoMode->m_id)
  117. weights[candidate->m_id]->m_weight += MATCH_WEIGHT_CURRENT;
  118. modeit++;
  119. }
  120. // now grab the mode with the highest weight
  121. DMVideoModeWeightPtr chosen;
  122. float maxWeight = 0;
  123. DMVideoModeWeightMap::const_iterator weightit = weights.constBegin();
  124. while (weightit != weights.constEnd())
  125. {
  126. QLOG_DEBUG() << "Mode " << weightit.value()->m_mode->m_id << "("
  127. << weightit.value()->m_mode->getPrettyName() << ") has weight "
  128. << weightit.value()->m_weight;
  129. if (weightit.value()->m_weight > maxWeight)
  130. {
  131. chosen = weightit.value();
  132. maxWeight = chosen->m_weight;
  133. }
  134. weightit++;
  135. }
  136. if ((chosen) && (chosen->m_weight > MATCH_WEIGHT_RES))
  137. {
  138. QLOG_INFO() << "DisplayManager RefreshMatch : found a suitable mode : "
  139. << chosen->m_mode->getPrettyName();
  140. return chosen->m_mode->m_id;
  141. }
  142. QLOG_INFO() << "DisplayManager RefreshMatch : found no suitable videomode";
  143. return -1;
  144. }
  145. ///////////////////////////////////////////////////////////////////////////////////////////////////
  146. int DisplayManager::findBestMode(int display)
  147. {
  148. int bestMode = -1;
  149. for(auto mode : m_displays[display]->m_videoModes)
  150. {
  151. if (bestMode < 0)
  152. {
  153. bestMode = mode->m_id;
  154. }
  155. else
  156. {
  157. DMVideoModePtr best = m_displays[display]->m_videoModes[bestMode];
  158. DMVideoModePtr candidate = mode;
  159. // Highest priority: prefer non-interlaced modes.
  160. if (!best->m_interlaced && candidate->m_interlaced)
  161. continue;
  162. if (best->m_bitsPerPixel > candidate->m_bitsPerPixel)
  163. continue;
  164. if (best->m_width > candidate->m_width)
  165. continue;
  166. if (best->m_height > candidate->m_height)
  167. continue;
  168. if (best->m_refreshRate > candidate->m_refreshRate)
  169. continue;
  170. bestMode = candidate->m_id;
  171. }
  172. }
  173. return bestMode;
  174. }
  175. ///////////////////////////////////////////////////////////////////////////////////////////////////
  176. int DisplayManager::getDisplayFromPoint(const QPoint& pt)
  177. {
  178. return getDisplayFromPoint(pt.x(), pt.y());
  179. }