Skip to content

Commit d0d6a4c

Browse files
Refactor Win32FlutterWindow in preparation for UWP windowing implementation (flutter#18878)
* Add flutter_windows_view and window_binding_handler Switch input handling infra to FlutterWindowsView win32_flutter_window implement WindowBindingHandler Strip unneeded functionality from win32flutterwindow Fulfill WindowBindingHandler interface in Win32FlutterWindow Add implementations for missing input handling in Win32FlutterWindow Cleanup dead code Correctly hook up rendering again Fix resizing clang-format Fix clipboard Cleanup Rename Add comments cleanup * clang-format * CR Feedback * clang-format; gn format * Fix licensing * CR feedback * CR feedback * CR feedback * Git rid of unnecessar :: prefixes * Extract WindowBindingHandlerDelegate as an interface * Missing file * Extract physical window bounds as a struct * CR Feedback * CR feedback * clang-format Co-authored-by: Stuart Morgan <[email protected]>
1 parent 110a579 commit d0d6a4c

24 files changed

+834
-524
lines changed

ci/licenses_golden/licenses_flutter

+9-5
Original file line numberDiff line numberDiff line change
@@ -1306,29 +1306,33 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flu
13061306
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h
13071307
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h
13081308
FILE: ../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
1309-
FILE: ../../../flutter/shell/platform/windows/dpi_utils.cc
1310-
FILE: ../../../flutter/shell/platform/windows/dpi_utils.h
1311-
FILE: ../../../flutter/shell/platform/windows/dpi_utils_unittests.cc
13121309
FILE: ../../../flutter/shell/platform/windows/flutter_windows.cc
1310+
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.cc
1311+
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.h
13131312
FILE: ../../../flutter/shell/platform/windows/key_event_handler.cc
13141313
FILE: ../../../flutter/shell/platform/windows/key_event_handler.h
13151314
FILE: ../../../flutter/shell/platform/windows/keyboard_hook_handler.h
1316-
FILE: ../../../flutter/shell/platform/windows/platform_handler.cc
1317-
FILE: ../../../flutter/shell/platform/windows/platform_handler.h
13181315
FILE: ../../../flutter/shell/platform/windows/public/flutter_windows.h
13191316
FILE: ../../../flutter/shell/platform/windows/string_conversion.cc
13201317
FILE: ../../../flutter/shell/platform/windows/string_conversion.h
13211318
FILE: ../../../flutter/shell/platform/windows/string_conversion_unittests.cc
13221319
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc
13231320
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.h
1321+
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.cc
1322+
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.h
1323+
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils_unittests.cc
13241324
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window.cc
13251325
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window.h
13261326
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window_unittests.cc
1327+
FILE: ../../../flutter/shell/platform/windows/win32_platform_handler.cc
1328+
FILE: ../../../flutter/shell/platform/windows/win32_platform_handler.h
13271329
FILE: ../../../flutter/shell/platform/windows/win32_task_runner.cc
13281330
FILE: ../../../flutter/shell/platform/windows/win32_task_runner.h
13291331
FILE: ../../../flutter/shell/platform/windows/win32_window.cc
13301332
FILE: ../../../flutter/shell/platform/windows/win32_window.h
13311333
FILE: ../../../flutter/shell/platform/windows/win32_window_unittests.cc
1334+
FILE: ../../../flutter/shell/platform/windows/window_binding_handler.h
1335+
FILE: ../../../flutter/shell/platform/windows/window_binding_handler_delegate.h
13321336
FILE: ../../../flutter/shell/platform/windows/window_state.h
13331337
FILE: ../../../flutter/shell/profiling/sampling_profiler.cc
13341338
FILE: ../../../flutter/shell/profiling/sampling_profiler.h

shell/platform/windows/BUILD.gn

+9-5
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,28 @@ source_set("flutter_windows_source") {
4141
sources = [
4242
"angle_surface_manager.cc",
4343
"angle_surface_manager.h",
44-
"dpi_utils.cc",
45-
"dpi_utils.h",
4644
"flutter_windows.cc",
45+
"flutter_windows_view.cc",
46+
"flutter_windows_view.h",
4747
"key_event_handler.cc",
4848
"key_event_handler.h",
4949
"keyboard_hook_handler.h",
50-
"platform_handler.cc",
51-
"platform_handler.h",
5250
"string_conversion.cc",
5351
"string_conversion.h",
5452
"text_input_plugin.cc",
5553
"text_input_plugin.h",
54+
"win32_dpi_utils.cc",
55+
"win32_dpi_utils.h",
5656
"win32_flutter_window.cc",
5757
"win32_flutter_window.h",
58+
"win32_platform_handler.cc",
59+
"win32_platform_handler.h",
5860
"win32_task_runner.cc",
5961
"win32_task_runner.h",
6062
"win32_window.cc",
6163
"win32_window.h",
64+
"window_binding_handler.h",
65+
"window_binding_handler_delegate.h",
6266
"window_state.h",
6367
]
6468

@@ -110,12 +114,12 @@ executable("flutter_windows_unittests") {
110114
testonly = true
111115

112116
sources = [
113-
"dpi_utils_unittests.cc",
114117
"string_conversion_unittests.cc",
115118
"testing/win32_flutter_window_test.cc",
116119
"testing/win32_flutter_window_test.h",
117120
"testing/win32_window_test.cc",
118121
"testing/win32_window_test.h",
122+
"win32_dpi_utils_unittests.cc",
119123
"win32_flutter_window_unittests.cc",
120124
"win32_window_unittests.cc",
121125
]

shell/platform/windows/angle_surface_manager.cc

+26-20
Original file line numberDiff line numberDiff line change
@@ -169,46 +169,52 @@ void AngleSurfaceManager::CleanUp() {
169169
}
170170
}
171171

172-
EGLSurface AngleSurfaceManager::CreateSurface(HWND window) {
173-
if (!window || !initialize_succeeded_) {
174-
return EGL_NO_SURFACE;
172+
bool AngleSurfaceManager::CreateSurface(WindowsRenderTarget* render_target) {
173+
if (!render_target || !initialize_succeeded_) {
174+
return false;
175175
}
176176

177177
EGLSurface surface = EGL_NO_SURFACE;
178178

179179
const EGLint surfaceAttributes[] = {EGL_NONE};
180180

181-
surface = eglCreateWindowSurface(egl_display_, egl_config_,
182-
static_cast<EGLNativeWindowType>(window),
183-
surfaceAttributes);
181+
surface = eglCreateWindowSurface(
182+
egl_display_, egl_config_,
183+
static_cast<EGLNativeWindowType>(std::get<HWND>(*render_target)),
184+
surfaceAttributes);
184185
if (surface == EGL_NO_SURFACE) {
185186
std::cerr << "Surface creation failed." << std::endl;
186187
}
187188

188-
return surface;
189+
render_surface_ = surface;
190+
return true;
189191
}
190192

191-
void AngleSurfaceManager::GetSurfaceDimensions(const EGLSurface surface,
192-
EGLint* width,
193-
EGLint* height) {
194-
if (surface == EGL_NO_SURFACE || !initialize_succeeded_) {
193+
void AngleSurfaceManager::GetSurfaceDimensions(EGLint* width, EGLint* height) {
194+
if (render_surface_ == EGL_NO_SURFACE || !initialize_succeeded_) {
195195
width = 0;
196196
height = 0;
197197
return;
198198
}
199199

200-
eglQuerySurface(egl_display_, surface, EGL_WIDTH, width);
201-
eglQuerySurface(egl_display_, surface, EGL_HEIGHT, height);
200+
eglQuerySurface(egl_display_, render_surface_, EGL_WIDTH, width);
201+
eglQuerySurface(egl_display_, render_surface_, EGL_HEIGHT, height);
202202
}
203203

204-
void AngleSurfaceManager::DestroySurface(const EGLSurface surface) {
205-
if (egl_display_ != EGL_NO_DISPLAY && surface != EGL_NO_SURFACE) {
206-
eglDestroySurface(egl_display_, surface);
204+
void AngleSurfaceManager::DestroySurface() {
205+
if (egl_display_ != EGL_NO_DISPLAY && render_surface_ != EGL_NO_SURFACE) {
206+
eglDestroySurface(egl_display_, render_surface_);
207207
}
208+
render_surface_ = EGL_NO_SURFACE;
209+
}
210+
211+
bool AngleSurfaceManager::MakeCurrent() {
212+
return (eglMakeCurrent(egl_display_, render_surface_, render_surface_,
213+
egl_context_) == EGL_TRUE);
208214
}
209215

210-
bool AngleSurfaceManager::MakeCurrent(const EGLSurface surface) {
211-
return (eglMakeCurrent(egl_display_, surface, surface, egl_context_) ==
216+
bool AngleSurfaceManager::ClearContext() {
217+
return (eglMakeCurrent(egl_display_, nullptr, nullptr, egl_context_) ==
212218
EGL_TRUE);
213219
}
214220

@@ -217,8 +223,8 @@ bool AngleSurfaceManager::MakeResourceCurrent() {
217223
egl_resource_context_) == EGL_TRUE);
218224
}
219225

220-
EGLBoolean AngleSurfaceManager::SwapBuffers(const EGLSurface surface) {
221-
return (eglSwapBuffers(egl_display_, surface));
226+
EGLBoolean AngleSurfaceManager::SwapBuffers() {
227+
return (eglSwapBuffers(egl_display_, render_surface_));
222228
}
223229

224230
} // namespace flutter

shell/platform/windows/angle_surface_manager.h

+17-11
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
// Windows platform specific includes
1616
#include <windows.h>
1717

18+
#include "window_binding_handler.h"
19+
1820
namespace flutter {
1921

20-
// An manager for inializing ANGLE correctly and using it to create and
22+
// A manager for inializing ANGLE correctly and using it to create and
2123
// destroy surfaces
2224
class AngleSurfaceManager {
2325
public:
@@ -28,31 +30,32 @@ class AngleSurfaceManager {
2830
AngleSurfaceManager(const AngleSurfaceManager&) = delete;
2931
AngleSurfaceManager& operator=(const AngleSurfaceManager&) = delete;
3032

31-
// Creates and returns an EGLSurface wrapper and backing DirectX 11 SwapChain
32-
// asociated with window, in the appropriate format for display in a
33-
// HWND-backed window.
34-
EGLSurface CreateSurface(HWND window);
33+
// Creates an EGLSurface wrapper and backing DirectX 11 SwapChain
34+
// asociated with window, in the appropriate format for display.
35+
// Target represents the visual entity to bind to.
36+
bool CreateSurface(WindowsRenderTarget* render_target);
3537

3638
// queries EGL for the dimensions of surface in physical
3739
// pixels returning width and height as out params.
38-
void GetSurfaceDimensions(const EGLSurface surface,
39-
EGLint* width,
40-
EGLint* height);
40+
void GetSurfaceDimensions(EGLint* width, EGLint* height);
4141

4242
// Releases the pass-in EGLSurface wrapping and backing resources if not null.
43-
void DestroySurface(const EGLSurface surface);
43+
void DestroySurface();
4444

4545
// Binds egl_context_ to the current rendering thread and to the draw and read
4646
// surfaces returning a boolean result reflecting success.
47-
bool MakeCurrent(const EGLSurface surface);
47+
bool MakeCurrent();
48+
49+
// Clears current egl_context_
50+
bool ClearContext();
4851

4952
// Binds egl_resource_context_ to the current rendering thread and to the draw
5053
// and read surfaces returning a boolean result reflecting success.
5154
bool MakeResourceCurrent();
5255

5356
// Swaps the front and back buffers of the DX11 swapchain backing surface if
5457
// not null.
55-
EGLBoolean SwapBuffers(const EGLSurface surface);
58+
EGLBoolean SwapBuffers();
5659

5760
private:
5861
bool Initialize();
@@ -75,6 +78,9 @@ class AngleSurfaceManager {
7578
// State representing success or failure of display initialization used when
7679
// creating surfaces.
7780
bool initialize_succeeded_;
81+
82+
// Current render_surface that engine will draw into.
83+
EGLSurface render_surface_ = EGL_NO_SURFACE;
7884
};
7985

8086
} // namespace flutter

shell/platform/windows/flutter_windows.cc

+20-14
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
#include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h"
2020
#include "flutter/shell/platform/common/cpp/path_utils.h"
2121
#include "flutter/shell/platform/embedder/embedder.h"
22-
#include "flutter/shell/platform/windows/dpi_utils.h"
22+
#include "flutter/shell/platform/windows/flutter_windows_view.h"
2323
#include "flutter/shell/platform/windows/key_event_handler.h"
2424
#include "flutter/shell/platform/windows/keyboard_hook_handler.h"
25-
#include "flutter/shell/platform/windows/platform_handler.h"
2625
#include "flutter/shell/platform/windows/text_input_plugin.h"
26+
#include "flutter/shell/platform/windows/win32_dpi_utils.h"
2727
#include "flutter/shell/platform/windows/win32_flutter_window.h"
28+
#include "flutter/shell/platform/windows/win32_platform_handler.h"
2829
#include "flutter/shell/platform/windows/win32_task_runner.h"
30+
#include "flutter/shell/platform/windows/window_binding_handler.h"
2931
#include "flutter/shell/platform/windows/window_state.h"
3032

3133
static_assert(FLUTTER_ENGINE_VERSION == 1, "");
@@ -66,7 +68,7 @@ UniqueAotDataPtr LoadAotData(std::filesystem::path aot_data_path) {
6668
// Returns the state object for the engine, or null on failure to start the
6769
// engine.
6870
static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(
69-
flutter::Win32FlutterWindow* window,
71+
flutter::FlutterWindowsView* view,
7072
const FlutterDesktopEngineProperties& engine_properties) {
7173
auto state = std::make_unique<FlutterDesktopEngineState>();
7274

@@ -79,22 +81,22 @@ static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(
7981
&engine_properties.switches[engine_properties.switches_count]);
8082
}
8183

82-
window->CreateRenderSurface();
84+
view->CreateRenderSurface();
8385

8486
// Provide the necessary callbacks for rendering within a win32 child window.
8587
FlutterRendererConfig config = {};
8688
config.type = kOpenGL;
8789
config.open_gl.struct_size = sizeof(config.open_gl);
8890
config.open_gl.make_current = [](void* user_data) -> bool {
89-
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
91+
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
9092
return host->MakeCurrent();
9193
};
9294
config.open_gl.clear_current = [](void* user_data) -> bool {
93-
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
95+
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
9496
return host->ClearContext();
9597
};
9698
config.open_gl.present = [](void* user_data) -> bool {
97-
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
99+
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
98100
return host->SwapBuffers();
99101
};
100102
config.open_gl.fbo_callback = [](void* user_data) -> uint32_t { return 0; };
@@ -103,7 +105,7 @@ static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(
103105
return reinterpret_cast<void*>(eglGetProcAddress(what));
104106
};
105107
config.open_gl.make_resource_current = [](void* user_data) -> bool {
106-
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
108+
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
107109
return host->MakeResourceCurrent();
108110
};
109111

@@ -178,7 +180,7 @@ static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(
178180
args.platform_message_callback =
179181
[](const FlutterPlatformMessage* engine_message,
180182
void* user_data) -> void {
181-
auto window = reinterpret_cast<flutter::Win32FlutterWindow*>(user_data);
183+
auto window = reinterpret_cast<flutter::FlutterWindowsView*>(user_data);
182184
return window->HandlePlatformMessage(engine_message);
183185
};
184186
args.custom_task_runners = &custom_task_runners;
@@ -188,7 +190,7 @@ static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(
188190

189191
FLUTTER_API_SYMBOL(FlutterEngine) engine = nullptr;
190192
auto result =
191-
FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args, window, &engine);
193+
FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args, view, &engine);
192194
if (result != kSuccess || engine == nullptr) {
193195
std::cerr << "Failed to start Flutter engine: error " << result
194196
<< std::endl;
@@ -202,8 +204,12 @@ FlutterDesktopViewControllerRef FlutterDesktopCreateViewController(
202204
int width,
203205
int height,
204206
const FlutterDesktopEngineProperties& engine_properties) {
207+
std::unique_ptr<flutter::WindowBindingHandler> window_wrapper =
208+
std::make_unique<flutter::Win32FlutterWindow>(width, height);
209+
205210
FlutterDesktopViewControllerRef state =
206-
flutter::Win32FlutterWindow::CreateWin32FlutterWindow(width, height);
211+
flutter::FlutterWindowsView::CreateFlutterWindowsView(
212+
std::move(window_wrapper));
207213

208214
auto engine_state = RunFlutterEngine(state->view.get(), engine_properties);
209215

@@ -261,8 +267,8 @@ FlutterDesktopViewRef FlutterDesktopGetView(
261267
return controller->view_wrapper.get();
262268
}
263269

264-
HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) {
265-
return view->window->GetWindowHandle();
270+
HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view_ref) {
271+
return std::get<HWND>(*view_ref->view->GetRenderTarget());
266272
}
267273

268274
UINT FlutterDesktopGetDpiForHWND(HWND hwnd) {
@@ -316,7 +322,7 @@ void FlutterDesktopRegistrarSetDestructionHandler(
316322

317323
FlutterDesktopViewRef FlutterDesktopRegistrarGetView(
318324
FlutterDesktopPluginRegistrarRef registrar) {
319-
return registrar->window;
325+
return registrar->view;
320326
}
321327

322328
bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,

0 commit comments

Comments
 (0)