• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "flutter/shell/platform/windows/win32_dpi_helper.h"
2 
3 namespace flutter {
4 
5 namespace {
6 
7 template <typename T>
AssignProcAddress(HMODULE comBaseModule,const char * name,T * & outProc)8 bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) {
9   outProc = reinterpret_cast<T*>(GetProcAddress(comBaseModule, name));
10   return *outProc != nullptr;
11 }
12 
13 }  // namespace
14 
Win32DpiHelper()15 Win32DpiHelper::Win32DpiHelper() {
16   // TODO ensure that this helper works correctly on downlevel builds.
17   user32_module_ = LoadLibraryA("User32.dll");
18   if (user32_module_ == nullptr) {
19     return;
20   }
21 
22   if (!AssignProcAddress(user32_module_, "EnableNonClientDpiScaling",
23                          enable_non_client_dpi_scaling_)) {
24     return;
25   }
26 
27   if (!AssignProcAddress(user32_module_, "GetDpiForWindow",
28                          get_dpi_for_window_)) {
29     return;
30   }
31 
32   if (!AssignProcAddress(user32_module_, "SetProcessDpiAwarenessContext",
33                          set_process_dpi_awareness_context_)) {
34     return;
35   }
36 
37   permonitorv2_supported_ = true;
38 }
39 
~Win32DpiHelper()40 Win32DpiHelper::~Win32DpiHelper() {
41   if (user32_module_ != nullptr) {
42     FreeLibrary(user32_module_);
43   }
44 }
45 
IsPerMonitorV2Available()46 bool Win32DpiHelper::IsPerMonitorV2Available() {
47   return permonitorv2_supported_;
48 }
49 
EnableNonClientDpiScaling(HWND hwnd)50 BOOL Win32DpiHelper::EnableNonClientDpiScaling(HWND hwnd) {
51   if (!permonitorv2_supported_) {
52     return false;
53   }
54   return enable_non_client_dpi_scaling_(hwnd);
55 }
56 
GetDpiForWindow(HWND hwnd)57 UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) {
58   if (!permonitorv2_supported_) {
59     return false;
60   }
61   return get_dpi_for_window_(hwnd);
62 }
63 
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT context)64 BOOL Win32DpiHelper::SetProcessDpiAwarenessContext(
65     DPI_AWARENESS_CONTEXT context) {
66   if (!permonitorv2_supported_) {
67     return false;
68   }
69   return set_process_dpi_awareness_context_(context);
70 }
71 
72 }  // namespace flutter
73