Skip to content

Commit

Permalink
Refactor Win32FlutterWindow in preparation for UWP windowing implemen…
Browse files Browse the repository at this point in the history
…tation (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 <stuartmorgan@google.com>
  • Loading branch information
clarkezone and stuartmorgan authored Jul 7, 2020
1 parent 110a579 commit d0d6a4c
Show file tree
Hide file tree
Showing 24 changed files with 834 additions and 524 deletions.
14 changes: 9 additions & 5 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1306,29 +1306,33 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flu
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h
FILE: ../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils.h
FILE: ../../../flutter/shell/platform/windows/dpi_utils_unittests.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.cc
FILE: ../../../flutter/shell/platform/windows/flutter_windows_view.h
FILE: ../../../flutter/shell/platform/windows/key_event_handler.cc
FILE: ../../../flutter/shell/platform/windows/key_event_handler.h
FILE: ../../../flutter/shell/platform/windows/keyboard_hook_handler.h
FILE: ../../../flutter/shell/platform/windows/platform_handler.cc
FILE: ../../../flutter/shell/platform/windows/platform_handler.h
FILE: ../../../flutter/shell/platform/windows/public/flutter_windows.h
FILE: ../../../flutter/shell/platform/windows/string_conversion.cc
FILE: ../../../flutter/shell/platform/windows/string_conversion.h
FILE: ../../../flutter/shell/platform/windows/string_conversion_unittests.cc
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.h
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.cc
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils.h
FILE: ../../../flutter/shell/platform/windows/win32_dpi_utils_unittests.cc
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window.cc
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window.h
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window_unittests.cc
FILE: ../../../flutter/shell/platform/windows/win32_platform_handler.cc
FILE: ../../../flutter/shell/platform/windows/win32_platform_handler.h
FILE: ../../../flutter/shell/platform/windows/win32_task_runner.cc
FILE: ../../../flutter/shell/platform/windows/win32_task_runner.h
FILE: ../../../flutter/shell/platform/windows/win32_window.cc
FILE: ../../../flutter/shell/platform/windows/win32_window.h
FILE: ../../../flutter/shell/platform/windows/win32_window_unittests.cc
FILE: ../../../flutter/shell/platform/windows/window_binding_handler.h
FILE: ../../../flutter/shell/platform/windows/window_binding_handler_delegate.h
FILE: ../../../flutter/shell/platform/windows/window_state.h
FILE: ../../../flutter/shell/profiling/sampling_profiler.cc
FILE: ../../../flutter/shell/profiling/sampling_profiler.h
Expand Down
14 changes: 9 additions & 5 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,28 @@ source_set("flutter_windows_source") {
sources = [
"angle_surface_manager.cc",
"angle_surface_manager.h",
"dpi_utils.cc",
"dpi_utils.h",
"flutter_windows.cc",
"flutter_windows_view.cc",
"flutter_windows_view.h",
"key_event_handler.cc",
"key_event_handler.h",
"keyboard_hook_handler.h",
"platform_handler.cc",
"platform_handler.h",
"string_conversion.cc",
"string_conversion.h",
"text_input_plugin.cc",
"text_input_plugin.h",
"win32_dpi_utils.cc",
"win32_dpi_utils.h",
"win32_flutter_window.cc",
"win32_flutter_window.h",
"win32_platform_handler.cc",
"win32_platform_handler.h",
"win32_task_runner.cc",
"win32_task_runner.h",
"win32_window.cc",
"win32_window.h",
"window_binding_handler.h",
"window_binding_handler_delegate.h",
"window_state.h",
]

Expand Down Expand Up @@ -110,12 +114,12 @@ executable("flutter_windows_unittests") {
testonly = true

sources = [
"dpi_utils_unittests.cc",
"string_conversion_unittests.cc",
"testing/win32_flutter_window_test.cc",
"testing/win32_flutter_window_test.h",
"testing/win32_window_test.cc",
"testing/win32_window_test.h",
"win32_dpi_utils_unittests.cc",
"win32_flutter_window_unittests.cc",
"win32_window_unittests.cc",
]
Expand Down
46 changes: 26 additions & 20 deletions shell/platform/windows/angle_surface_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,46 +169,52 @@ void AngleSurfaceManager::CleanUp() {
}
}

EGLSurface AngleSurfaceManager::CreateSurface(HWND window) {
if (!window || !initialize_succeeded_) {
return EGL_NO_SURFACE;
bool AngleSurfaceManager::CreateSurface(WindowsRenderTarget* render_target) {
if (!render_target || !initialize_succeeded_) {
return false;
}

EGLSurface surface = EGL_NO_SURFACE;

const EGLint surfaceAttributes[] = {EGL_NONE};

surface = eglCreateWindowSurface(egl_display_, egl_config_,
static_cast<EGLNativeWindowType>(window),
surfaceAttributes);
surface = eglCreateWindowSurface(
egl_display_, egl_config_,
static_cast<EGLNativeWindowType>(std::get<HWND>(*render_target)),
surfaceAttributes);
if (surface == EGL_NO_SURFACE) {
std::cerr << "Surface creation failed." << std::endl;
}

return surface;
render_surface_ = surface;
return true;
}

void AngleSurfaceManager::GetSurfaceDimensions(const EGLSurface surface,
EGLint* width,
EGLint* height) {
if (surface == EGL_NO_SURFACE || !initialize_succeeded_) {
void AngleSurfaceManager::GetSurfaceDimensions(EGLint* width, EGLint* height) {
if (render_surface_ == EGL_NO_SURFACE || !initialize_succeeded_) {
width = 0;
height = 0;
return;
}

eglQuerySurface(egl_display_, surface, EGL_WIDTH, width);
eglQuerySurface(egl_display_, surface, EGL_HEIGHT, height);
eglQuerySurface(egl_display_, render_surface_, EGL_WIDTH, width);
eglQuerySurface(egl_display_, render_surface_, EGL_HEIGHT, height);
}

void AngleSurfaceManager::DestroySurface(const EGLSurface surface) {
if (egl_display_ != EGL_NO_DISPLAY && surface != EGL_NO_SURFACE) {
eglDestroySurface(egl_display_, surface);
void AngleSurfaceManager::DestroySurface() {
if (egl_display_ != EGL_NO_DISPLAY && render_surface_ != EGL_NO_SURFACE) {
eglDestroySurface(egl_display_, render_surface_);
}
render_surface_ = EGL_NO_SURFACE;
}

bool AngleSurfaceManager::MakeCurrent() {
return (eglMakeCurrent(egl_display_, render_surface_, render_surface_,
egl_context_) == EGL_TRUE);
}

bool AngleSurfaceManager::MakeCurrent(const EGLSurface surface) {
return (eglMakeCurrent(egl_display_, surface, surface, egl_context_) ==
bool AngleSurfaceManager::ClearContext() {
return (eglMakeCurrent(egl_display_, nullptr, nullptr, egl_context_) ==
EGL_TRUE);
}

Expand All @@ -217,8 +223,8 @@ bool AngleSurfaceManager::MakeResourceCurrent() {
egl_resource_context_) == EGL_TRUE);
}

EGLBoolean AngleSurfaceManager::SwapBuffers(const EGLSurface surface) {
return (eglSwapBuffers(egl_display_, surface));
EGLBoolean AngleSurfaceManager::SwapBuffers() {
return (eglSwapBuffers(egl_display_, render_surface_));
}

} // namespace flutter
28 changes: 17 additions & 11 deletions shell/platform/windows/angle_surface_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
// Windows platform specific includes
#include <windows.h>

#include "window_binding_handler.h"

namespace flutter {

// An manager for inializing ANGLE correctly and using it to create and
// A manager for inializing ANGLE correctly and using it to create and
// destroy surfaces
class AngleSurfaceManager {
public:
Expand All @@ -28,31 +30,32 @@ class AngleSurfaceManager {
AngleSurfaceManager(const AngleSurfaceManager&) = delete;
AngleSurfaceManager& operator=(const AngleSurfaceManager&) = delete;

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

// queries EGL for the dimensions of surface in physical
// pixels returning width and height as out params.
void GetSurfaceDimensions(const EGLSurface surface,
EGLint* width,
EGLint* height);
void GetSurfaceDimensions(EGLint* width, EGLint* height);

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

// Binds egl_context_ to the current rendering thread and to the draw and read
// surfaces returning a boolean result reflecting success.
bool MakeCurrent(const EGLSurface surface);
bool MakeCurrent();

// Clears current egl_context_
bool ClearContext();

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

// Swaps the front and back buffers of the DX11 swapchain backing surface if
// not null.
EGLBoolean SwapBuffers(const EGLSurface surface);
EGLBoolean SwapBuffers();

private:
bool Initialize();
Expand All @@ -75,6 +78,9 @@ class AngleSurfaceManager {
// State representing success or failure of display initialization used when
// creating surfaces.
bool initialize_succeeded_;

// Current render_surface that engine will draw into.
EGLSurface render_surface_ = EGL_NO_SURFACE;
};

} // namespace flutter
Expand Down
34 changes: 20 additions & 14 deletions shell/platform/windows/flutter_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
#include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h"
#include "flutter/shell/platform/common/cpp/path_utils.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/windows/dpi_utils.h"
#include "flutter/shell/platform/windows/flutter_windows_view.h"
#include "flutter/shell/platform/windows/key_event_handler.h"
#include "flutter/shell/platform/windows/keyboard_hook_handler.h"
#include "flutter/shell/platform/windows/platform_handler.h"
#include "flutter/shell/platform/windows/text_input_plugin.h"
#include "flutter/shell/platform/windows/win32_dpi_utils.h"
#include "flutter/shell/platform/windows/win32_flutter_window.h"
#include "flutter/shell/platform/windows/win32_platform_handler.h"
#include "flutter/shell/platform/windows/win32_task_runner.h"
#include "flutter/shell/platform/windows/window_binding_handler.h"
#include "flutter/shell/platform/windows/window_state.h"

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

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

window->CreateRenderSurface();
view->CreateRenderSurface();

// Provide the necessary callbacks for rendering within a win32 child window.
FlutterRendererConfig config = {};
config.type = kOpenGL;
config.open_gl.struct_size = sizeof(config.open_gl);
config.open_gl.make_current = [](void* user_data) -> bool {
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
return host->MakeCurrent();
};
config.open_gl.clear_current = [](void* user_data) -> bool {
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
return host->ClearContext();
};
config.open_gl.present = [](void* user_data) -> bool {
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
return host->SwapBuffers();
};
config.open_gl.fbo_callback = [](void* user_data) -> uint32_t { return 0; };
Expand All @@ -103,7 +105,7 @@ static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(
return reinterpret_cast<void*>(eglGetProcAddress(what));
};
config.open_gl.make_resource_current = [](void* user_data) -> bool {
auto host = static_cast<flutter::Win32FlutterWindow*>(user_data);
auto host = static_cast<flutter::FlutterWindowsView*>(user_data);
return host->MakeResourceCurrent();
};

Expand Down Expand Up @@ -178,7 +180,7 @@ static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(
args.platform_message_callback =
[](const FlutterPlatformMessage* engine_message,
void* user_data) -> void {
auto window = reinterpret_cast<flutter::Win32FlutterWindow*>(user_data);
auto window = reinterpret_cast<flutter::FlutterWindowsView*>(user_data);
return window->HandlePlatformMessage(engine_message);
};
args.custom_task_runners = &custom_task_runners;
Expand All @@ -188,7 +190,7 @@ static std::unique_ptr<FlutterDesktopEngineState> RunFlutterEngine(

FLUTTER_API_SYMBOL(FlutterEngine) engine = nullptr;
auto result =
FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args, window, &engine);
FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, &args, view, &engine);
if (result != kSuccess || engine == nullptr) {
std::cerr << "Failed to start Flutter engine: error " << result
<< std::endl;
Expand All @@ -202,8 +204,12 @@ FlutterDesktopViewControllerRef FlutterDesktopCreateViewController(
int width,
int height,
const FlutterDesktopEngineProperties& engine_properties) {
std::unique_ptr<flutter::WindowBindingHandler> window_wrapper =
std::make_unique<flutter::Win32FlutterWindow>(width, height);

FlutterDesktopViewControllerRef state =
flutter::Win32FlutterWindow::CreateWin32FlutterWindow(width, height);
flutter::FlutterWindowsView::CreateFlutterWindowsView(
std::move(window_wrapper));

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

Expand Down Expand Up @@ -261,8 +267,8 @@ FlutterDesktopViewRef FlutterDesktopGetView(
return controller->view_wrapper.get();
}

HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) {
return view->window->GetWindowHandle();
HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view_ref) {
return std::get<HWND>(*view_ref->view->GetRenderTarget());
}

UINT FlutterDesktopGetDpiForHWND(HWND hwnd) {
Expand Down Expand Up @@ -316,7 +322,7 @@ void FlutterDesktopRegistrarSetDestructionHandler(

FlutterDesktopViewRef FlutterDesktopRegistrarGetView(
FlutterDesktopPluginRegistrarRef registrar) {
return registrar->window;
return registrar->view;
}

bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger,
Expand Down
Loading

0 comments on commit d0d6a4c

Please sign in to comment.