diff --git a/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc b/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc index cf952998ee..95aa03c32b 100644 --- a/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc +++ b/engine/src/flutter/shell/platform/windows/angle_surface_manager.cc @@ -26,6 +26,15 @@ static void LogEglError(std::string message) { namespace flutter { +std::unique_ptr AngleSurfaceManager::Create() { + std::unique_ptr manager; + manager.reset(new AngleSurfaceManager()); + if (!manager->initialize_succeeded_) { + return nullptr; + } + return std::move(manager); +} + AngleSurfaceManager::AngleSurfaceManager() : egl_config_(nullptr), egl_display_(EGL_NO_DISPLAY), diff --git a/engine/src/flutter/shell/platform/windows/angle_surface_manager.h b/engine/src/flutter/shell/platform/windows/angle_surface_manager.h index d2a62ad341..643a4090e3 100644 --- a/engine/src/flutter/shell/platform/windows/angle_surface_manager.h +++ b/engine/src/flutter/shell/platform/windows/angle_surface_manager.h @@ -23,9 +23,7 @@ namespace flutter { // destroy surfaces class AngleSurfaceManager { public: - // Creates a new surface manager retaining reference to the passed-in target - // for the lifetime of the manager. - AngleSurfaceManager(); + static std::unique_ptr Create(); ~AngleSurfaceManager(); // Disallow copy/move. @@ -75,6 +73,10 @@ class AngleSurfaceManager { void CleanUp(); private: + // Creates a new surface manager retaining reference to the passed-in target + // for the lifetime of the manager. + AngleSurfaceManager(); + // Attempts to initialize EGL using ANGLE. bool InitializeEGL( PFNEGLGETPLATFORMDISPLAYEXTPROC egl_get_platform_display_EXT, diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc b/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc index fae6558e59..48c201cc0f 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc @@ -206,4 +206,22 @@ void FlutterWindowWin32::OnCursorRectUpdated(const Rect& rect) { UpdateCursorRect(Rect(origin, size)); } +bool FlutterWindowWin32::OnBitmapSurfaceUpdated(const void* allocation, + size_t row_bytes, + size_t height) { + HDC dc = ::GetDC(std::get(GetRenderTarget())); + BITMAPINFO bmi; + memset(&bmi, 0, sizeof(bmi)); + bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmi.bmiHeader.biWidth = row_bytes / 4; + bmi.bmiHeader.biHeight = -height; + bmi.bmiHeader.biPlanes = 1; + bmi.bmiHeader.biBitCount = 32; + bmi.bmiHeader.biCompression = BI_RGB; + bmi.bmiHeader.biSizeImage = 0; + int ret = SetDIBitsToDevice(dc, 0, 0, row_bytes / 4, height, 0, 0, 0, height, + allocation, &bmi, DIB_RGB_COLORS); + return ret != 0; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_win32.h b/engine/src/flutter/shell/platform/windows/flutter_window_win32.h index 7f67567b1b..817a6498b8 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_win32.h +++ b/engine/src/flutter/shell/platform/windows/flutter_window_win32.h @@ -98,6 +98,11 @@ class FlutterWindowWin32 : public WindowWin32, public WindowBindingHandler { // |FlutterWindowBindingHandler| void OnWindowResized() override; + // |FlutterWindowBindingHandler| + bool OnBitmapSurfaceUpdated(const void* allocation, + size_t row_bytes, + size_t height) override; + private: // A pointer to a FlutterWindowsView that can be used to update engine // windowing and input state. diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc index 2c7cb534df..2a5242229b 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc @@ -338,4 +338,11 @@ FlutterWindowWinUWP::CreateCursorVisual() { return container; } +bool FlutterWindowWinUWP::OnBitmapSurfaceUpdated(const void* allocation, + size_t row_bytes, + size_t height) { + // TODO(gw280): Support software rendering fallback on UWP + return false; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h index 927ec98490..0e388ba549 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h +++ b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h @@ -55,6 +55,11 @@ class FlutterWindowWinUWP : public WindowBindingHandler { // |WindowBindingHandler| void OnWindowResized() override; + // |WindowBindingHandler| + bool OnBitmapSurfaceUpdated(const void* allocation, + size_t row_bytes, + size_t height) override; + private: // Returns a bounds structure containing width and height information // for the backing CoreWindow in either view or physical pixels depending on diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 196f3d9c19..914e8f700f 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -23,9 +23,10 @@ namespace flutter { namespace { // Creates and returns a FlutterRendererConfig that renders to the view (if any) -// of a FlutterWindowsEngine, which should be the user_data received by the -// render callbacks. -FlutterRendererConfig GetRendererConfig() { +// of a FlutterWindowsEngine, using OpenGL (via ANGLE). +// The user_data received by the render callbacks refers to the +// FlutterWindowsEngine. +FlutterRendererConfig GetOpenGLRendererConfig() { FlutterRendererConfig config = {}; config.type = kOpenGL; config.open_gl.struct_size = sizeof(config.open_gl); @@ -85,6 +86,27 @@ FlutterRendererConfig GetRendererConfig() { return config; } +// Creates and returns a FlutterRendererConfig that renders to the view (if any) +// of a FlutterWindowsEngine, using software rasterization. +// The user_data received by the render callbacks refers to the +// FlutterWindowsEngine. +FlutterRendererConfig GetSoftwareRendererConfig() { + FlutterRendererConfig config = {}; + config.type = kSoftware; + config.software.struct_size = sizeof(config.software); + config.software.surface_present_callback = [](void* user_data, + const void* allocation, + size_t row_bytes, + size_t height) { + auto host = static_cast(user_data); + if (!host->view()) { + return false; + } + return host->view()->PresentSoftwareBitmap(allocation, row_bytes, height); + }; + return config; +} + // Converts a FlutterPlatformMessage to an equivalent FlutterDesktopMessage. static FlutterDesktopMessage ConvertToDesktopMessage( const FlutterPlatformMessage& engine_message) { @@ -143,6 +165,7 @@ FlutterWindowsEngine::FlutterWindowsEngine(const FlutterProjectBundle& project) message_dispatcher_ = std::make_unique(messenger_.get()); texture_registrar_ = std::make_unique(this); + surface_manager_ = AngleSurfaceManager::Create(); #ifndef WINUWP window_proc_delegate_manager_ = std::make_unique(); @@ -236,7 +259,9 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) { args.custom_dart_entrypoint = entrypoint; } - FlutterRendererConfig renderer_config = GetRendererConfig(); + FlutterRendererConfig renderer_config = surface_manager_ + ? GetOpenGLRendererConfig() + : GetSoftwareRendererConfig(); auto result = embedder_api_.Run(FLUTTER_ENGINE_VERSION, &renderer_config, &args, this, &engine_); diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index 1f90920386..b8dd97b2ad 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -14,6 +14,7 @@ #include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" #include "flutter/shell/platform/common/incoming_message_dispatcher.h" #include "flutter/shell/platform/embedder/embedder.h" +#include "flutter/shell/platform/windows/angle_surface_manager.h" #include "flutter/shell/platform/windows/flutter_project_bundle.h" #include "flutter/shell/platform/windows/flutter_windows_texture_registrar.h" #include "flutter/shell/platform/windows/public/flutter_windows.h" @@ -85,6 +86,10 @@ class FlutterWindowsEngine { return texture_registrar_.get(); } + // The ANGLE surface manager object. If this is nullptr, then we are + // rendering using software instead of OpenGL. + AngleSurfaceManager* surface_manager() { return surface_manager_.get(); } + #ifndef WINUWP WindowProcDelegateManagerWin32* window_proc_delegate_manager() { return window_proc_delegate_manager_.get(); @@ -174,6 +179,11 @@ class FlutterWindowsEngine { // The texture registrar. std::unique_ptr texture_registrar_; + // An object used for intializing Angle and creating / destroying render + // surfaces. Surface creation functionality requires a valid render_target. + // May be nullptr if ANGLE failed to initialize. + std::unique_ptr surface_manager_; + // The MethodChannel used for communication with the Flutter engine. std::unique_ptr> settings_channel_; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc index fa6d87e72b..2520a56280 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -46,6 +46,8 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) { EXPECT_EQ(version, FLUTTER_ENGINE_VERSION); EXPECT_NE(config, nullptr); + // We have an AngleSurfaceManager, so this should be using OpenGL. + EXPECT_EQ(config->type, kOpenGL); EXPECT_EQ(user_data, engine_instance); // Spot-check arguments. EXPECT_STREQ(args->assets_path, "C:\\foo\\flutter_assets"); @@ -84,12 +86,55 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) { return kSuccess; })); + // Set the AngleSurfaceManager to !nullptr to test ANGLE rendering. + modifier.SetSurfaceManager(reinterpret_cast(1)); + engine->RunWithEntrypoint(nullptr); EXPECT_TRUE(run_called); EXPECT_TRUE(update_locales_called); EXPECT_TRUE(settings_message_sent); + // Ensure that deallocation doesn't call the actual Shutdown with the bogus + // engine pointer that the overridden Run returned. + modifier.embedder_api().Shutdown = [](auto engine) { return kSuccess; }; + modifier.ReleaseSurfaceManager(); +} + +TEST(FlutterWindowsEngine, RunWithoutANGLEUsesSoftware) { + std::unique_ptr engine = GetTestEngine(); + EngineEmbedderApiModifier modifier(engine.get()); + + // The engine should be run with expected configuration values. + bool run_called = false; + modifier.embedder_api().Run = MOCK_ENGINE_PROC( + Run, ([&run_called, engine_instance = engine.get()]( + size_t version, const FlutterRendererConfig* config, + const FlutterProjectArgs* args, void* user_data, + FLUTTER_API_SYMBOL(FlutterEngine) * engine_out) { + run_called = true; + *engine_out = reinterpret_cast(1); + // We don't have an AngleSurfaceManager, so we should be using software. + EXPECT_EQ(config->type, kSoftware); + return kSuccess; + })); + + // Stub out UpdateLocales and SendPlatformMessage as we don't have a fully + // initialized engine instance. + modifier.embedder_api().UpdateLocales = MOCK_ENGINE_PROC( + UpdateLocales, ([](auto engine, const FlutterLocale** locales, + size_t locales_count) { return kSuccess; })); + modifier.embedder_api().SendPlatformMessage = + MOCK_ENGINE_PROC(SendPlatformMessage, + ([](auto engine, auto message) { return kSuccess; })); + + // Set the AngleSurfaceManager to nullptr to test software fallback path. + modifier.SetSurfaceManager(nullptr); + + engine->RunWithEntrypoint(nullptr); + + EXPECT_TRUE(run_called); + // Ensure that deallocation doesn't call the actual Shutdown with the bogus // engine pointer that the overridden Run returned. modifier.embedder_api().Shutdown = [](auto engine) { return kSuccess; }; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc index 1ce3d13d0a..bc09824979 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -32,8 +32,6 @@ static bool SurfaceWillUpdate(size_t cur_width, FlutterWindowsView::FlutterWindowsView( std::unique_ptr window_binding) { - surface_manager_ = std::make_unique(); - // Take the binding handler, and give it a pointer back to self. binding_handler_ = std::move(window_binding); binding_handler_->SetView(this); @@ -118,8 +116,8 @@ uint32_t FlutterWindowsView::GetFrameBufferId(size_t width, size_t height) { if (resize_target_width_ == width && resize_target_height_ == height) { // Platform thread is blocked for the entire duration until the // resize_status_ is set to kDone. - surface_manager_->ResizeSurface(GetRenderTarget(), width, height); - surface_manager_->MakeCurrent(); + engine_->surface_manager()->ResizeSurface(GetRenderTarget(), width, height); + engine_->surface_manager()->MakeCurrent(); resize_status_ = ResizeState::kFrameGenerated; } @@ -140,8 +138,14 @@ void FlutterWindowsView::OnWindowSizeChanged(size_t width, size_t height) { // Called on the platform thread. std::unique_lock lock(resize_mutex_); + if (!engine_->surface_manager()) { + SendWindowMetrics(width, height, binding_handler_->GetDpiScale()); + return; + } + EGLint surface_width, surface_height; - surface_manager_->GetSurfaceDimensions(&surface_width, &surface_height); + engine_->surface_manager()->GetSurfaceDimensions(&surface_width, + &surface_height); bool surface_will_update = SurfaceWillUpdate(surface_width, surface_height, width, height); @@ -409,15 +413,15 @@ void FlutterWindowsView::SendPointerEventWithData( } bool FlutterWindowsView::MakeCurrent() { - return surface_manager_->MakeCurrent(); + return engine_->surface_manager()->MakeCurrent(); } bool FlutterWindowsView::MakeResourceCurrent() { - return surface_manager_->MakeResourceCurrent(); + return engine_->surface_manager()->MakeResourceCurrent(); } bool FlutterWindowsView::ClearContext() { - return surface_manager_->ClearContext(); + return engine_->surface_manager()->ClearContext(); } bool FlutterWindowsView::SwapBuffers() { @@ -431,7 +435,7 @@ bool FlutterWindowsView::SwapBuffers() { case ResizeState::kResizeStarted: return false; case ResizeState::kFrameGenerated: { - bool swap_buffers_result = surface_manager_->SwapBuffers(); + bool swap_buffers_result = engine_->surface_manager()->SwapBuffers(); resize_status_ = ResizeState::kDone; lock.unlock(); resize_cv_.notify_all(); @@ -440,19 +444,28 @@ bool FlutterWindowsView::SwapBuffers() { } case ResizeState::kDone: default: - return surface_manager_->SwapBuffers(); + return engine_->surface_manager()->SwapBuffers(); } } +bool FlutterWindowsView::PresentSoftwareBitmap(const void* allocation, + size_t row_bytes, + size_t height) { + return binding_handler_->OnBitmapSurfaceUpdated(allocation, row_bytes, + height); +} + void FlutterWindowsView::CreateRenderSurface() { - PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); - surface_manager_->CreateSurface(GetRenderTarget(), bounds.width, - bounds.height); + if (engine_ && engine_->surface_manager()) { + PhysicalWindowBounds bounds = binding_handler_->GetPhysicalWindowBounds(); + engine_->surface_manager()->CreateSurface(GetRenderTarget(), bounds.width, + bounds.height); + } } void FlutterWindowsView::DestroyRenderSurface() { - if (surface_manager_) { - surface_manager_->DestroySurface(); + if (engine_ && engine_->surface_manager()) { + engine_->surface_manager()->DestroySurface(); } } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h index 79e3576251..31923960b3 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h @@ -72,6 +72,11 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate, bool MakeResourceCurrent(); bool SwapBuffers(); + // Callback for presenting a software bitmap. + bool PresentSoftwareBitmap(const void* allocation, + size_t row_bytes, + size_t height); + // Send initial bounds to embedder. Must occur after engine has initialized. void SendInitialBounds(); @@ -264,10 +269,6 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate, // os window. std::unique_ptr render_target_; - // An object used for intializing Angle and creating / destroying render - // surfaces. Surface creation functionality requires a valid render_target. - std::unique_ptr surface_manager_; - // The engine associated with this view. std::unique_ptr engine_; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_win32.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_win32.cc index a043538cb6..9c8cf4fa37 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_win32.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_win32.cc @@ -34,11 +34,10 @@ FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate( auto state = std::make_unique(); state->view = std::make_unique(std::move(window_wrapper)); - state->view->CreateRenderSurface(); - // Take ownership of the engine, starting it if necessary. state->view->SetEngine( std::unique_ptr(EngineFromHandle(engine))); + state->view->CreateRenderSurface(); if (!state->view->GetEngine()->running()) { if (!state->view->GetEngine()->RunWithEntrypoint(nullptr)) { return nullptr; diff --git a/engine/src/flutter/shell/platform/windows/testing/engine_embedder_api_modifier.h b/engine/src/flutter/shell/platform/windows/testing/engine_embedder_api_modifier.h index a06f1df5ae..7fb67427cf 100644 --- a/engine/src/flutter/shell/platform/windows/testing/engine_embedder_api_modifier.h +++ b/engine/src/flutter/shell/platform/windows/testing/engine_embedder_api_modifier.h @@ -6,8 +6,8 @@ namespace flutter { -// A test utility class providing the ability to access and alter the embedder -// API proc table for an engine instance. +// A test utility class providing the ability to access and alter various +// private fields in an Engine instance. // // This simply provides a way to access the normally-private embedder proc // table, so the lifetime of any changes made to the proc table is that of the @@ -23,6 +23,25 @@ class EngineEmbedderApiModifier { // engine unless overwritten again. FlutterEngineProcTable& embedder_api() { return engine_->embedder_api_; } + // Explicitly sets the SurfaceManager being used by the FlutterWindowsEngine + // instance. This allows us to test fallback paths when a SurfaceManager fails + // to initialize for whatever reason. + // + // Modifications are to the engine, and will last for the lifetime of the + // engine unless overwritten again. + void SetSurfaceManager(AngleSurfaceManager* surface_manager) { + engine_->surface_manager_.reset(surface_manager); + } + + // Explicitly releases the SurfaceManager being used by the + // FlutterWindowsEngine instance. This should be used if SetSurfaceManager is + // used to explicitly set to a non-null value (but not a valid object) to test + // a successful ANGLE initialization. + // + // Modifications are to the engine, and will last for the lifetime of the + // engine unless overwritten again. + void ReleaseSurfaceManager() { engine_->surface_manager_.release(); } + private: FlutterWindowsEngine* engine_; }; diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h index a85ab92358..bac12fe065 100644 --- a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h @@ -30,6 +30,8 @@ class MockWindowBindingHandler : public WindowBindingHandler { MOCK_METHOD0(GetPhysicalWindowBounds, PhysicalWindowBounds()); MOCK_METHOD1(UpdateFlutterCursor, void(const std::string& cursor_name)); MOCK_METHOD1(OnCursorRectUpdated, void(const Rect& rect)); + MOCK_METHOD3(OnBitmapSurfaceUpdated, + bool(const void* allocation, size_t row_bytes, size_t height)); }; } // namespace testing diff --git a/engine/src/flutter/shell/platform/windows/window_binding_handler.h b/engine/src/flutter/shell/platform/windows/window_binding_handler.h index 749fda203f..674d6990b5 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler.h @@ -65,6 +65,14 @@ class WindowBindingHandler { // Invoked when the cursor/composing rect has been updated in the framework. virtual void OnCursorRectUpdated(const Rect& rect) = 0; + + // Invoked when the Embedder provides us with new bitmap data for the contents + // of this Flutter view. + // + // Returns whether the surface was successfully updated or not. + virtual bool OnBitmapSurfaceUpdated(const void* allocation, + size_t row_bytes, + size_t height) = 0; }; } // namespace flutter