-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Introduce the DisplayManager so that we send real display info down to the client #173301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
894f7ff
Introduce the DisplayManager so that we send real display info down t…
mattkae 2f8b475
Initial tests for the DisplayManager
mattkae 07e3f07
Completing the unittests
mattkae 0ae0bde
Remove stray comment
mattkae 1905500
There is no reason to set the ID to zero
mattkae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
engine/src/flutter/shell/platform/windows/display_manager.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/shell/platform/windows/display_manager.h" | ||
| #include "flutter/shell/platform/windows/dpi_utils.h" | ||
| #include "flutter/shell/platform/windows/flutter_windows_engine.h" | ||
|
|
||
| #include <memory> | ||
| #include "flutter/fml/logging.h" | ||
|
|
||
| namespace flutter { | ||
| namespace { | ||
| struct CallbackData { | ||
| std::shared_ptr<WindowsProcTable> windows_proc_table; | ||
| std::vector<FlutterEngineDisplay> displays; | ||
| }; | ||
| } // namespace | ||
|
|
||
| DisplayManager::DisplayManager(FlutterWindowsEngine* engine) : engine_(engine) { | ||
| WNDCLASS window_class = RegisterWindowClass(); | ||
| window_handle_ = | ||
| CreateWindowEx(0, window_class.lpszClassName, L"", 0, 0, 0, 0, 0, | ||
| HWND_MESSAGE, nullptr, window_class.hInstance, nullptr); | ||
|
|
||
| if (window_handle_) { | ||
| SetWindowLongPtr(window_handle_, GWLP_USERDATA, | ||
| reinterpret_cast<LONG_PTR>(this)); | ||
| } else { | ||
| auto error = GetLastError(); | ||
| LPWSTR message = nullptr; | ||
| size_t size = FormatMessageW( | ||
| FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | | ||
| FORMAT_MESSAGE_IGNORE_INSERTS, | ||
| NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
| reinterpret_cast<LPWSTR>(&message), 0, NULL); | ||
| OutputDebugString(message); | ||
| LocalFree(message); | ||
| } | ||
| } | ||
|
|
||
| DisplayManager::~DisplayManager() { | ||
| if (window_handle_) { | ||
| DestroyWindow(window_handle_); | ||
| window_handle_ = nullptr; | ||
| } | ||
| UnregisterClass(window_class_name_.c_str(), nullptr); | ||
| } | ||
|
|
||
| std::vector<FlutterEngineDisplay> DisplayManager::displays() const { | ||
| auto windows_proc_table = engine_->windows_proc_table(); | ||
| CallbackData data = {windows_proc_table, std::vector<FlutterEngineDisplay>()}; | ||
| windows_proc_table->EnumDisplayMonitors(nullptr, nullptr, MonitorEnumProc, | ||
| reinterpret_cast<LPARAM>(&data)); | ||
|
|
||
| if (data.displays.size() == 1) { | ||
| data.displays[0].single_display = true; | ||
| } | ||
|
|
||
| return data.displays; | ||
| } | ||
|
|
||
| WNDCLASS DisplayManager::RegisterWindowClass() { | ||
| window_class_name_ = L"FlutterDisplayManager"; | ||
|
|
||
| WNDCLASS window_class{}; | ||
| window_class.hCursor = nullptr; | ||
| window_class.lpszClassName = window_class_name_.c_str(); | ||
| window_class.style = 0; | ||
| window_class.cbClsExtra = 0; | ||
| window_class.cbWndExtra = 0; | ||
| window_class.hInstance = GetModuleHandle(nullptr); | ||
| window_class.hIcon = nullptr; | ||
| window_class.hbrBackground = 0; | ||
| window_class.lpszMenuName = nullptr; | ||
| window_class.lpfnWndProc = WndProc; | ||
| RegisterClass(&window_class); | ||
| return window_class; | ||
| } | ||
|
|
||
| LRESULT | ||
| DisplayManager::HandleMessage(UINT const message, | ||
| WPARAM const wparam, | ||
| LPARAM const lparam) noexcept { | ||
| switch (message) { | ||
| case WM_DISPLAYCHANGE: | ||
| case WM_DEVICECHANGE: { | ||
| if (engine_->running()) { | ||
| engine_->OnDisplaysChanged(displays()); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| return DefWindowProcW(window_handle_, message, wparam, lparam); | ||
| } | ||
|
|
||
| LRESULT DisplayManager::WndProc(HWND const window, | ||
| UINT const message, | ||
| WPARAM const wparam, | ||
| LPARAM const lparam) noexcept { | ||
| if (auto* that = reinterpret_cast<DisplayManager*>( | ||
| GetWindowLongPtr(window, GWLP_USERDATA))) { | ||
| return that->HandleMessage(message, wparam, lparam); | ||
| } else { | ||
| return DefWindowProc(window, message, wparam, lparam); | ||
| } | ||
| } | ||
|
|
||
| BOOL CALLBACK DisplayManager::MonitorEnumProc(HMONITOR hMonitor, | ||
| HDC, | ||
| LPRECT, | ||
| LPARAM lParam) { | ||
| auto data = reinterpret_cast<CallbackData*>(lParam); | ||
|
|
||
| MONITORINFOEX monitor_info = {}; | ||
| monitor_info.cbSize = sizeof(MONITORINFOEX); | ||
| if (!data->windows_proc_table->GetMonitorInfo(hMonitor, &monitor_info)) { | ||
| return TRUE; | ||
| } | ||
|
|
||
| // Get display settings | ||
| DEVMODE dev_mode = {}; | ||
| dev_mode.dmSize = sizeof(DEVMODE); | ||
| bool has_display_settings = data->windows_proc_table->EnumDisplaySettingsW( | ||
| monitor_info.szDevice, ENUM_CURRENT_SETTINGS, &dev_mode); | ||
|
|
||
| FlutterEngineDisplay display; | ||
| display.struct_size = sizeof(FlutterEngineDisplay); | ||
| display.display_id = reinterpret_cast<FlutterEngineDisplayId>(hMonitor); | ||
| display.single_display = false; | ||
| display.width = monitor_info.rcMonitor.right - monitor_info.rcMonitor.left; | ||
| display.height = monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top; | ||
| display.refresh_rate = has_display_settings | ||
| ? static_cast<double>(dev_mode.dmDisplayFrequency) | ||
| : 0.0; | ||
| display.device_pixel_ratio = GetDpiForMonitor(hMonitor); | ||
|
|
||
| data->displays.push_back(display); | ||
| return TRUE; | ||
| } | ||
|
|
||
| } // namespace flutter | ||
51 changes: 51 additions & 0 deletions
51
engine/src/flutter/shell/platform/windows/display_manager.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_DISPLAY_MANAGER_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_WINDOWS_DISPLAY_MANAGER_H_ | ||
|
|
||
| #include <windows.h> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "flutter/shell/platform/embedder/embedder.h" | ||
|
|
||
| namespace flutter { | ||
| class FlutterWindowsEngine; | ||
|
|
||
| class DisplayManager { | ||
| public: | ||
| DisplayManager(FlutterWindowsEngine* engine); | ||
| virtual ~DisplayManager(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The destructor is marked Please consider making the destructor non-virtual. ~DisplayManager();Style Guide ReferencesFootnotes |
||
|
|
||
| std::vector<FlutterEngineDisplay> displays() const; | ||
|
|
||
| HWND get_window_handle() const { return window_handle_; } | ||
|
|
||
| private: | ||
| WNDCLASS | ||
| RegisterWindowClass(); | ||
|
|
||
| LRESULT | ||
| HandleMessage(UINT const message, | ||
| WPARAM const wparam, | ||
| LPARAM const lparam) noexcept; | ||
|
|
||
| static LRESULT CALLBACK WndProc(HWND const window, | ||
| UINT const message, | ||
| WPARAM const wparam, | ||
| LPARAM const lparam) noexcept; | ||
|
|
||
| static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, | ||
| HDC, | ||
| LPRECT, | ||
| LPARAM lParam); | ||
|
|
||
| FlutterWindowsEngine* engine_; | ||
| HWND window_handle_; | ||
| std::wstring window_class_name_; | ||
| }; | ||
| } // namespace flutter | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DISPLAY_MANAGER_H_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the rest of the Flutter engine code, it's better to use
FML_LOG(ERROR)instead ofOutputDebugString. This ensures that error messages are routed through the standard logging channels.The current implementation also doesn't check if
FormatMessageWsucceeds, which could lead to issues ifmessageisnullptr. The suggested change below addresses these points.Please also add
#include "flutter/fml/platform/win/wstring_conversion.h"at the top of the file to usefml::WideToUtf8.