1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/desktop_resizer.h"
#include <windows.h>
#include <map>
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
namespace {
// TODO(jamiewalch): Use the correct DPI for the mode: http://crbug.com/172405.
const int kDefaultDPI = 96;
} // namespace
namespace remoting {
// Provide comparison operation for ScreenResolution so we can use it in
// std::map.
static inline bool operator<(const ScreenResolution& a,
const ScreenResolution& b) {
if (a.dimensions().width() != b.dimensions().width()) {
return a.dimensions().width() < b.dimensions().width();
}
if (a.dimensions().height() != b.dimensions().height()) {
return a.dimensions().height() < b.dimensions().height();
}
if (a.dpi().x() != b.dpi().x()) {
return a.dpi().x() < b.dpi().x();
}
return a.dpi().y() < b.dpi().y();
}
class DesktopResizerWin : public DesktopResizer {
public:
DesktopResizerWin();
DesktopResizerWin(const DesktopResizerWin&) = delete;
DesktopResizerWin& operator=(const DesktopResizerWin&) = delete;
~DesktopResizerWin() override;
// DesktopResizer interface.
ScreenResolution GetCurrentResolution(webrtc::ScreenId screen_id) override;
std::list<ScreenResolution> GetSupportedResolutions(
const ScreenResolution& preferred,
webrtc::ScreenId screen_id) override;
void SetResolution(const ScreenResolution& resolution,
webrtc::ScreenId screen_id) override;
void RestoreResolution(const ScreenResolution& original,
webrtc::ScreenId screen_id) override;
void SetVideoLayout(const protocol::VideoLayout& layout) override;
private:
void UpdateBestModeForResolution(const DEVMODE& current_mode,
const DEVMODE& candidate_mode);
static bool IsResizeSupported();
// Calls EnumDisplaySettingsEx() for the primary monitor.
// Returns false if |mode_number| does not exist.
static bool GetPrimaryDisplayMode(DWORD mode_number,
DWORD flags,
DEVMODE* mode);
// Returns true if the mode has width, height, bits-per-pixel, frequency
// and orientation fields.
static bool IsModeValid(const DEVMODE& mode);
// Returns the width & height of |mode|, or 0x0 if they are missing.
static ScreenResolution GetModeResolution(const DEVMODE& mode);
std::map<ScreenResolution, DEVMODE> best_mode_for_resolution_;
DEVMODE initial_mode_;
};
DesktopResizerWin::DesktopResizerWin() {
if (!GetPrimaryDisplayMode(ENUM_CURRENT_SETTINGS, 0, &initial_mode_) ||
!IsModeValid(initial_mode_)) {
LOG(ERROR) << "GetPrimaryDisplayMode failed. Resize will not prefer "
<< "initial orientation or frequency settings.";
initial_mode_.dmFields = 0;
}
}
DesktopResizerWin::~DesktopResizerWin() {}
ScreenResolution DesktopResizerWin::GetCurrentResolution(
webrtc::ScreenId screen_id) {
DEVMODE current_mode;
if (GetPrimaryDisplayMode(ENUM_CURRENT_SETTINGS, 0, ¤t_mode) &&
IsModeValid(current_mode)) {
return GetModeResolution(current_mode);
}
return ScreenResolution();
}
std::list<ScreenResolution> DesktopResizerWin::GetSupportedResolutions(
const ScreenResolution& preferred,
webrtc::ScreenId screen_id) {
if (!IsResizeSupported()) {
return std::list<ScreenResolution>();
}
// Enumerate the resolutions to return, and where there are multiple modes of
// the same resolution, store the one most closely matching the current mode
// in |best_mode_for_resolution_|.
DEVMODE current_mode;
if (!GetPrimaryDisplayMode(ENUM_CURRENT_SETTINGS, 0, ¤t_mode) ||
!IsModeValid(current_mode)) {
return std::list<ScreenResolution>();
}
best_mode_for_resolution_.clear();
for (DWORD i = 0;; ++i) {
DEVMODE candidate_mode;
if (!GetPrimaryDisplayMode(i, EDS_ROTATEDMODE, &candidate_mode)) {
break;
}
UpdateBestModeForResolution(current_mode, candidate_mode);
}
std::list<ScreenResolution> resolutions;
for (const auto& kv : best_mode_for_resolution_) {
resolutions.push_back(kv.first);
}
return resolutions;
}
void DesktopResizerWin::SetResolution(const ScreenResolution& resolution,
webrtc::ScreenId screen_id) {
if (best_mode_for_resolution_.count(resolution) == 0) {
return;
}
DEVMODE new_mode = best_mode_for_resolution_[resolution];
DWORD result = ChangeDisplaySettings(&new_mode, CDS_FULLSCREEN);
if (result != DISP_CHANGE_SUCCESSFUL) {
LOG(ERROR) << "SetResolution failed: " << result;
}
}
void DesktopResizerWin::RestoreResolution(const ScreenResolution& original,
webrtc::ScreenId screen_id) {
// Restore the display mode based on the registry configuration.
DWORD result = ChangeDisplaySettings(nullptr, 0);
if (result != DISP_CHANGE_SUCCESSFUL) {
LOG(ERROR) << "RestoreResolution failed: " << result;
}
}
void DesktopResizerWin::SetVideoLayout(const protocol::VideoLayout& layout) {
NOTIMPLEMENTED();
}
void DesktopResizerWin::UpdateBestModeForResolution(
const DEVMODE& current_mode,
const DEVMODE& candidate_mode) {
// Ignore modes missing the fields that we expect.
if (!IsModeValid(candidate_mode)) {
LOG(INFO) << "Ignoring mode " << candidate_mode.dmPelsWidth << "x"
<< candidate_mode.dmPelsHeight << ": invalid fields " << std::hex
<< candidate_mode.dmFields;
return;
}
// Ignore modes with differing bits-per-pixel.
if (candidate_mode.dmBitsPerPel != current_mode.dmBitsPerPel) {
LOG(INFO) << "Ignoring mode " << candidate_mode.dmPelsWidth << "x"
<< candidate_mode.dmPelsHeight << ": mismatched BPP: expected "
<< current_mode.dmFields << " but got "
<< candidate_mode.dmFields;
return;
}
// If there are multiple modes with the same dimensions:
// - Prefer the modes which match either the initial (preferred) or the
// current rotation.
// - Among those, prefer modes which match the initial (preferred) or the
// current frequency.
// - Otherwise, prefer modes with a higher frequency.
ScreenResolution candidate_resolution = GetModeResolution(candidate_mode);
if (best_mode_for_resolution_.count(candidate_resolution) != 0) {
DEVMODE best_mode = best_mode_for_resolution_[candidate_resolution];
bool best_mode_matches_initial_orientation =
(initial_mode_.dmDisplayOrientation & DM_DISPLAYORIENTATION) &&
(best_mode.dmDisplayOrientation == initial_mode_.dmDisplayOrientation);
bool candidate_mode_matches_initial_orientation =
candidate_mode.dmDisplayOrientation ==
initial_mode_.dmDisplayOrientation;
if (best_mode_matches_initial_orientation &&
!candidate_mode_matches_initial_orientation) {
LOG(INFO) << "Ignoring mode " << candidate_mode.dmPelsWidth << "x"
<< candidate_mode.dmPelsHeight
<< ": mode matching initial orientation already found.";
return;
}
bool best_mode_matches_current_orientation =
best_mode.dmDisplayOrientation == current_mode.dmDisplayOrientation;
bool candidate_mode_matches_current_orientation =
candidate_mode.dmDisplayOrientation ==
current_mode.dmDisplayOrientation;
if (best_mode_matches_current_orientation &&
!candidate_mode_matches_initial_orientation &&
!candidate_mode_matches_current_orientation) {
LOG(INFO) << "Ignoring mode " << candidate_mode.dmPelsWidth << "x"
<< candidate_mode.dmPelsHeight
<< ": mode matching current orientation already found.";
return;
}
bool best_mode_matches_initial_frequency =
(initial_mode_.dmDisplayOrientation & DM_DISPLAYFREQUENCY) &&
(best_mode.dmDisplayFrequency == initial_mode_.dmDisplayFrequency);
bool candidate_mode_matches_initial_frequency =
candidate_mode.dmDisplayFrequency == initial_mode_.dmDisplayFrequency;
if (best_mode_matches_initial_frequency &&
!candidate_mode_matches_initial_frequency) {
LOG(INFO) << "Ignoring mode " << candidate_mode.dmPelsWidth << "x"
<< candidate_mode.dmPelsHeight
<< ": mode matching initial frequency already found.";
return;
}
bool best_mode_matches_current_frequency =
best_mode.dmDisplayFrequency == current_mode.dmDisplayFrequency;
bool candidate_mode_matches_current_frequency =
candidate_mode.dmDisplayFrequency == current_mode.dmDisplayFrequency;
if (best_mode_matches_current_frequency &&
!candidate_mode_matches_initial_frequency &&
!candidate_mode_matches_current_frequency) {
LOG(INFO) << "Ignoring mode " << candidate_mode.dmPelsWidth << "x"
<< candidate_mode.dmPelsHeight
<< ": mode matching current frequency already found.";
return;
}
}
// If we haven't seen this resolution before, or if it's a better match than
// one we enumerated previously, save it.
best_mode_for_resolution_[candidate_resolution] = candidate_mode;
}
// static
bool DesktopResizerWin::IsResizeSupported() {
// Resize is supported only on single-monitor systems.
return GetSystemMetrics(SM_CMONITORS) == 1;
}
// static
bool DesktopResizerWin::GetPrimaryDisplayMode(DWORD mode_number,
DWORD flags,
DEVMODE* mode) {
UNSAFE_TODO(memset(mode, 0, sizeof(DEVMODE)));
mode->dmSize = sizeof(DEVMODE);
if (!EnumDisplaySettingsEx(nullptr, mode_number, mode, flags)) {
return false;
}
return true;
}
// static
bool DesktopResizerWin::IsModeValid(const DEVMODE& mode) {
const DWORD kRequiredFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL |
DM_DISPLAYFREQUENCY | DM_DISPLAYORIENTATION;
return (mode.dmFields & kRequiredFields) == kRequiredFields;
}
// static
ScreenResolution DesktopResizerWin::GetModeResolution(const DEVMODE& mode) {
DCHECK(IsModeValid(mode));
return ScreenResolution(
webrtc::DesktopSize(mode.dmPelsWidth, mode.dmPelsHeight),
webrtc::DesktopVector(kDefaultDPI, kDefaultDPI));
}
std::unique_ptr<DesktopResizer> DesktopResizer::Create() {
return base::WrapUnique(new DesktopResizerWin);
}
} // namespace remoting
|