Flutter Engine
The Flutter Engine
display.cc
Go to the documentation of this file.
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/win/display.h"
6
7namespace base {
8namespace win {
9
10namespace {
11
12template <typename T>
13bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) {
14 outProc = reinterpret_cast<T*>(GetProcAddress(comBaseModule, name));
15 return *outProc != nullptr;
16}
17
18// Helper class for supporting display scale factor lookup across Windows
19// versions, with fallbacks where these lookups are unavailable.
20class ScaleHelperWin32 {
21 public:
22 ScaleHelperWin32();
23 ~ScaleHelperWin32();
24
25 /// Returns the scale factor for the specified monitor. Sets |scale| to
26 /// SCALE_100_PERCENT if the API is not available.
27 HRESULT GetScaleFactorForMonitor(HMONITOR hmonitor,
28 DEVICE_SCALE_FACTOR* scale) const;
29
30 private:
31 using GetScaleFactorForMonitor_ =
32 HRESULT __stdcall(HMONITOR hmonitor, DEVICE_SCALE_FACTOR* scale);
33
34 GetScaleFactorForMonitor_* get_scale_factor_for_monitor_ = nullptr;
35
36 HMODULE shlib_module_ = nullptr;
37 bool scale_factor_for_monitor_supported_ = false;
38};
39
40ScaleHelperWin32::ScaleHelperWin32() {
41 if ((shlib_module_ = LoadLibraryA("Shcore.dll")) != nullptr) {
42 scale_factor_for_monitor_supported_ =
43 AssignProcAddress(shlib_module_, "GetScaleFactorForMonitor",
44 get_scale_factor_for_monitor_);
45 }
46}
47
48ScaleHelperWin32::~ScaleHelperWin32() {
49 if (shlib_module_ != nullptr) {
50 FreeLibrary(shlib_module_);
51 }
52}
53
54HRESULT ScaleHelperWin32::GetScaleFactorForMonitor(
55 HMONITOR hmonitor,
56 DEVICE_SCALE_FACTOR* scale) const {
57 if (hmonitor == nullptr || scale == nullptr) {
58 return E_INVALIDARG;
59 }
60 if (!scale_factor_for_monitor_supported_) {
61 *scale = SCALE_100_PERCENT;
62 return S_OK;
63 }
64 return get_scale_factor_for_monitor_(hmonitor, scale);
65}
66
67ScaleHelperWin32* GetHelper() {
68 static ScaleHelperWin32* helper = new ScaleHelperWin32();
69 return helper;
70}
71
72} // namespace
73
74float GetScaleFactorForHWND(HWND hwnd) {
75 HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
76 DEVICE_SCALE_FACTOR scale = DEVICE_SCALE_FACTOR_INVALID;
77 if (SUCCEEDED(GetHelper()->GetScaleFactorForMonitor(monitor, &scale))) {
79 }
80 return 1.0f;
81}
82
83float ScaleFactorToFloat(DEVICE_SCALE_FACTOR scale_factor) {
84 switch (scale_factor) {
85 case SCALE_100_PERCENT:
86 return 1.0f;
87 case SCALE_120_PERCENT:
88 return 1.2f;
89 case SCALE_125_PERCENT:
90 return 1.25f;
91 case SCALE_140_PERCENT:
92 return 1.4f;
93 case SCALE_150_PERCENT:
94 return 1.5f;
95 case SCALE_160_PERCENT:
96 return 1.6f;
97 case SCALE_175_PERCENT:
98 return 1.75f;
99 case SCALE_180_PERCENT:
100 return 1.8f;
101 case SCALE_200_PERCENT:
102 return 2.0f;
103 case SCALE_225_PERCENT:
104 return 2.25f;
105 case SCALE_250_PERCENT:
106 return 2.5f;
107 case SCALE_300_PERCENT:
108 return 3.0f;
109 case SCALE_350_PERCENT:
110 return 3.5f;
111 case SCALE_400_PERCENT:
112 return 4.0f;
113 case SCALE_450_PERCENT:
114 return 4.5f;
115 case SCALE_500_PERCENT:
116 return 5.0f;
117 default:
118 return 1.0f;
119 }
120}
121
122} // namespace win
123} // namespace base
float GetScaleFactorForHWND(HWND hwnd)
Definition: display.cc:74
float ScaleFactorToFloat(DEVICE_SCALE_FACTOR scale_factor)
Definition: display.cc:83
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
#define T
Definition: precompiler.cc:65
const Scalar scale
#define SUCCEEDED(hr)
HINSTANCE HMODULE
Definition: windows_types.h:96