File: screen_win.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (344 lines) | stat: -rw-r--r-- 14,978 bytes parent folder | download | duplicates (2)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// 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.

#ifndef UI_DISPLAY_WIN_SCREEN_WIN_H_
#define UI_DISPLAY_WIN_SCREEN_WIN_H_

#include <windows.h>

#include <memory>
#include <vector>

#include "base/scoped_observation.h"
#include "ui/display/display_change_notifier.h"
#include "ui/display/display_export.h"
#include "ui/display/screen.h"
#include "ui/display/win/color_profile_reader.h"
#include "ui/display/win/screen_win_display.h"
#include "ui/display/win/uwp_text_scale_factor.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d_f.h"
#include "ui/gfx/mojom/dxgi_info.mojom.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/win/singleton_hwnd_observer.h"

namespace display::win {

class ScreenWinDisplay;
class FallbackScreenWin;

namespace internal {
class DisplayInfo;
}  // namespace internal

class DISPLAY_EXPORT ScreenWin : public Screen,
                                 public ColorProfileReader::Client,
                                 public UwpTextScaleFactor::Observer {
 public:
  ScreenWin(const ScreenWin&) = delete;
  ScreenWin& operator=(const ScreenWin&) = delete;

  ~ScreenWin() override;

  // Converts a screen physical point to a screen DIP point.
  // The DPI scale is performed relative to the display containing the physical
  // point.
  virtual gfx::PointF ScreenToDIPPoint(const gfx::PointF& pixel_point) const;

  // Converts a screen DIP point to a screen physical point.
  // The DPI scale is performed relative to the display containing the DIP
  // point.
  virtual gfx::Point DIPToScreenPoint(const gfx::Point& dip_point) const;

  // Converts a client physical point relative to |hwnd| to a client DIP point.
  // The DPI scale is performed relative to |hwnd| using an origin of (0, 0).
  virtual gfx::Point ClientToDIPPoint(HWND hwnd,
                                      const gfx::Point& client_point) const;

  // Converts a client DIP point relative to |hwnd| to a client physical point.
  // The DPI scale is performed relative to |hwnd| using an origin of (0, 0).
  virtual gfx::Point DIPToClientPoint(HWND hwnd,
                                      const gfx::Point& dip_point) const;

  // WARNING: There is no right way to scale sizes and rects.
  // Sometimes you may need the enclosing rect (which favors transformations
  // that stretch the bounds towards integral values) or the enclosed rect
  // (transformations that shrink the bounds towards integral values).
  // This implementation favors the enclosing rect.
  //
  // Understand which you need before blindly assuming this is the right way.

  // Converts a screen physical rect to a screen DIP rect.
  // The DPI scale is performed relative to the display nearest to |hwnd|.
  // If |hwnd| is null, scaling will be performed to the display nearest to
  // |pixel_bounds|.
  virtual gfx::Rect ScreenToDIPRect(HWND hwnd,
                                    const gfx::Rect& pixel_bounds) const;

  // Converts a screen DIP rect to a screen physical rect.
  // If |hwnd| is null, scaling will be performed using the DSF of the display
  // nearest to |dip_bounds|; otherwise, scaling will be performed using the DSF
  // of the display nearest to |hwnd|.  Thus if an existing HWND is moving to a
  // different display, it's often more correct to pass null for |hwnd| to get
  // the new display's scale factor rather than the old one's.
  virtual gfx::Rect DIPToScreenRect(HWND hwnd,
                                    const gfx::Rect& dip_bounds) const;

  // Converts a client physical rect to a client DIP rect.
  // The DPI scale is performed relative to |hwnd| using an origin of (0, 0).
  virtual gfx::Rect ClientToDIPRect(HWND hwnd,
                                    const gfx::Rect& pixel_bounds) const;

  // Converts a client DIP rect to a client physical rect.
  // The DPI scale is performed relative to |hwnd| using an origin of (0, 0).
  virtual gfx::Rect DIPToClientRect(HWND hwnd,
                                    const gfx::Rect& dip_bounds) const;

  // Converts a physical size to a DIP size.
  // The DPI scale is performed relative to the display nearest to |hwnd|.
  virtual gfx::Size ScreenToDIPSize(HWND hwnd,
                                    const gfx::Size& size_in_pixels) const;

  // Converts a DIP size to a physical size.
  // The DPI scale is performed relative to the display nearest to |hwnd|.
  virtual gfx::Size DIPToScreenSize(HWND hwnd, const gfx::Size& dip_size) const;

  // Returns the number of physical pixels per inch for a display associated
  // with the point.
  virtual gfx::Vector2dF GetPixelsPerInch(const gfx::PointF& point) const;

  // Returns the result of GetSystemMetrics for |metric| scaled to |monitor|'s
  // DPI. Use this function if you're already working with screen pixels, as
  // this helps reduce any cascading rounding errors from DIP to the |monitor|'s
  // DPI.
  //
  // Note that metrics which correspond to elements drawn by Windows
  // (specifically frame and resize handles) will be scaled by DPI only and not
  // by Text Zoom or other accessibility features.
  virtual int GetSystemMetricsForMonitor(HMONITOR monitor, int metric) const;

  // Returns the result of GetSystemMetrics for |metric| in DIP.
  // Use this function if you need to work in DIP and can tolerate cascading
  // rounding errors towards screen pixels.
  virtual int GetSystemMetricsInDIP(int metric) const;

  // Returns |hwnd|'s scale factor, including accessibility adjustments.
  virtual float GetScaleFactorForHWND(HWND hwnd) const;

  // Returns the unmodified DPI for a particular |hwnd|, without accessibility
  // adjustments.
  virtual int GetDPIForHWND(HWND hwnd) const;

  // Converts dpi to scale factor, including accessibility adjustments.
  virtual float GetScaleFactorForDPI(int dpi) const;

  // Returns the system's global scale factor, ignoring the value of
  // --force-device-scale-factor. Only use this if you are working with Windows
  // metrics global to the system. Otherwise you should call
  // GetScaleFactorForHWND() to get the correct scale factor for the monitor
  // you are targeting.
  virtual float GetSystemScaleFactor() const;

  // Set a callback to use to query the status of HDR. This callback will be
  // called when the status of HDR may have changed.
  using RequestHDRStatusCallback = base::RepeatingClosure;
  virtual void SetRequestHDRStatusCallback(
      RequestHDRStatusCallback request_hdr_status_callback);

  // Set information gathered from DXGI adapters and outputs (e.g, HDR
  // parameters).
  virtual void SetDXGIInfo(gfx::mojom::DXGIInfoPtr dxgi_info);

  // Returns the ScreenWinDisplay with the given id, or a default object if an
  // unrecognized id was specified or if this was called during a screen update.
  virtual ScreenWinDisplay GetScreenWinDisplayWithDisplayId(int64_t id) const;

  // Returns the display id for the given monitor info.
  virtual int64_t DisplayIdFromMonitorInfo(
      const MONITORINFOEX& monitor_info) const;

  // Updates the display infos to make sure they have the right scale factors.
  // This is called before handling WM_DPICHANGED messages, to be sure that we
  // have the right scale factors for the screens.
  virtual void UpdateDisplayInfos();

  // Updates the display infos if it appears that Windows state has changed
  // in a way that requires the display infos to be updated. This currently
  // only detects when the primary monitor changes, which it does when a monitor
  // is added or removed.
  virtual void UpdateDisplayInfosIfNeeded();

  // Returns the HWND associated with the NativeWindow.
  virtual HWND GetHWNDFromNativeWindow(gfx::NativeWindow view) const;

  // Returns the NativeWindow associated with the HWND.
  virtual gfx::NativeWindow GetNativeWindowFromHWND(HWND hwnd) const;

  // Returns true if the native window is occluded.
  virtual bool IsNativeWindowOccluded(gfx::NativeWindow window) const;

  // Returns the cached on_current_workspace() value for the NativeWindow's
  // host.
  virtual std::optional<bool> IsWindowOnCurrentVirtualDesktop(
      gfx::NativeWindow window) const;

  // Resets cached fallback screen for testing. Has no effect if there is no
  // fallback screen. Fallback screen remembers forced device scale factor at
  // the time of creation and thus has to be reset in unit tests running in the
  // same process, similar to Display::ResetForceDeviceScaleFactorForTesting().
  static void ResetFallbackScreenForTesting();

 protected:
  friend class FallbackScreenWin;

  FRIEND_TEST_ALL_PREFIXES(ScreenWinTestSingleDisplay1x,
                           DisconnectPrimaryDisplay);

  ScreenWin();

  // `initialize_from_system` is true if the ScreenWin should be initialized
  // from the Windows desktop environment, e.g., the monitor information and
  // configuration. It is false in unit tests, true in Chrome and browser
  // tests.
  ScreenWin(bool initialize_from_system);

  // Screen:
  gfx::Point GetCursorScreenPoint() override;
  bool IsWindowUnderCursor(gfx::NativeWindow window) override;
  gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) override;
  gfx::NativeWindow GetLocalProcessWindowAtPoint(
      const gfx::Point& point,
      const std::set<gfx::NativeWindow>& ignore) override;
  int GetNumDisplays() const override;
  const std::vector<Display>& GetAllDisplays() const override;
  Display GetDisplayNearestWindow(gfx::NativeWindow window) const override;
  Display GetDisplayNearestPoint(const gfx::Point& point) const override;
  Display GetDisplayMatching(const gfx::Rect& match_rect) const override;
  Display GetPrimaryDisplay() const override;
  void AddObserver(DisplayObserver* observer) override;
  void RemoveObserver(DisplayObserver* observer) override;
  gfx::Rect ScreenToDIPRectInWindow(
      gfx::NativeWindow window,
      const gfx::Rect& screen_rect) const override;
  gfx::Rect DIPToScreenRectInWindow(gfx::NativeWindow window,
                                    const gfx::Rect& dip_rect) const override;

  // ColorProfileReader::Client:
  void OnColorProfilesChanged() override;

  void UpdateFromDisplayInfos(
      const std::vector<internal::DisplayInfo>& display_infos);

  // Virtual to support mocking by unit tests and headless screen.
  virtual std::optional<MONITORINFOEX> MonitorInfoFromScreenPoint(
      const gfx::Point& screen_point) const;
  virtual std::optional<MONITORINFOEX> MonitorInfoFromScreenRect(
      const gfx::Rect& screen_rect) const;
  virtual std::optional<MONITORINFOEX> MonitorInfoFromWindow(
      HWND hwnd,
      DWORD default_options) const;
  virtual int64_t GetDisplayIdFromMonitorInfo(
      const MONITORINFOEX& monitor_info) const;
  virtual HWND GetRootWindow(HWND hwnd) const;
  virtual int GetSystemMetrics(int metric) const;
  virtual void UpdateAllDisplaysAndNotify();
  virtual void UpdateAllDisplaysIfPrimaryMonitorChanged();

  // Returns the ScreenWinDisplay closest to or enclosing |hwnd|.
  virtual ScreenWinDisplay GetScreenWinDisplayNearestHWND(HWND hwnd) const;

  // Returns the ScreenWinDisplay closest to or enclosing |screen_rect|.
  ScreenWinDisplay GetScreenWinDisplayNearestScreenRect(
      const gfx::Rect& screen_rect) const;

  // Returns the ScreenWinDisplay closest to or enclosing |screen_point|.
  ScreenWinDisplay GetScreenWinDisplayNearestScreenPoint(
      const gfx::Point& screen_point) const;

  // Returns the ScreenWinDisplay closest to or enclosing |dip_point|.
  ScreenWinDisplay GetScreenWinDisplayNearestDIPPoint(
      const gfx::Point& dip_point) const;

  // Returns the ScreenWinDisplay closest to or enclosing |dip_rect|.
  ScreenWinDisplay GetScreenWinDisplayNearestDIPRect(
      const gfx::Rect& dip_rect) const;

  // Returns the ScreenWinDisplay corresponding to the primary monitor.
  virtual ScreenWinDisplay GetPrimaryScreenWinDisplay() const;

  // Returns the ScreenWinDisplay corresponding to the given monitor info.
  virtual ScreenWinDisplay GetScreenWinDisplay(
      std::optional<MONITORINFOEX> monitor_info) const;

  // Returns the result of GetSystemMetrics for |metric| scaled to the specified
  // |scale_factor|.
  int GetSystemMetricsForScaleFactor(float scale_factor, int metric) const;

 private:
  void Initialize();

  void OnWndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);

  // Returns the result of calling |getter| with |value| on the global
  // ScreenWin if it exists, otherwise return the default ScreenWinDisplay.
  template <typename Getter, typename GetterType>
  static ScreenWinDisplay GetScreenWinDisplayVia(Getter getter,
                                                 GetterType value);

  //-----------------------------------------------------------------
  // UwpTextScaleFactor::Observer:

  void OnUwpTextScaleFactorChanged() override;
  void OnUwpTextScaleFactorCleanup(UwpTextScaleFactor* source) override;

  // Tests don't want to use the actual DPI settings of the monitor(s) on
  // the machine running the test.
  // Returns false if running in unit tests, if the ScreenWin constructor was
  // called with initialize set to false.
  bool PerProcessDPIAwarenessDisabledForTesting() const;

  // Helper implementing the DisplayObserver handling.
  DisplayChangeNotifier change_notifier_;

  std::unique_ptr<gfx::SingletonHwndObserver> singleton_hwnd_observer_;

  // Current list of ScreenWinDisplays.
  std::vector<ScreenWinDisplay> screen_win_displays_;

  // The Displays corresponding to |screen_win_displays_| for GetAllDisplays().
  // This must be updated anytime |screen_win_displays_| is updated.
  std::vector<Display> displays_;

  // A helper to read color profiles from the filesystem.
  std::unique_ptr<ColorProfileReader> color_profile_reader_ =
      std::make_unique<ColorProfileReader>(this);

  // Callback to use to query when the HDR status may have changed.
  RequestHDRStatusCallback request_hdr_status_callback_;

  // Information gathered from DXGI adapters and outputs.
  gfx::mojom::DXGIInfoPtr dxgi_info_;

  base::ScopedObservation<UwpTextScaleFactor, UwpTextScaleFactor::Observer>
      scale_factor_observation_{this};

  // Used to avoid calling GetSystemMetricsForDpi in unit tests.
  bool per_process_dpi_awareness_disabled_for_testing_ = false;

  // Used to track if primary_monitor_ changes, which is used as a signal that
  // screen_win_displays_ needs to be updated. This should be updated when
  // screen_win_displays_ is updated.
  HMONITOR primary_monitor_ = nullptr;
};

// Returns a ScreenWin instance. If one does not exist, creates a fallback
// ScreenWin instance that may be replaced with the real one later if necessary.
DISPLAY_EXPORT ScreenWin* GetScreenWin();

}  // namespace display::win

#endif  // UI_DISPLAY_WIN_SCREEN_WIN_H_