From 2b0041b8c12d144638512df03c92461cb16291a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=A0=D7=99=D7=A8?= Date: Tue, 4 Mar 2025 22:56:03 +0200 Subject: [PATCH 1/5] wip --- .../shell/platform/windows/flutter_windows.cc | 12 ++++++++++++ .../shell/platform/windows/flutter_windows_engine.cc | 5 +++++ .../shell/platform/windows/flutter_windows_engine.h | 2 ++ .../shell/platform/windows/public/flutter_windows.h | 9 +++++++++ 4 files changed, 28 insertions(+) diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index 9b01ca30c8b12..bcb37de9245df 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -304,6 +304,18 @@ FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById( return HandleForView(registrar->engine->view(view_id)); } +bool FlutterDesktopPluginRegistrarGetID3D11Device( + FlutterDesktopPluginRegistrarRef registrar, + FlutterID3D11DeviceRef* device) { + ID3D11Device* d3d_device; + if (reinterpret_cast(registrar->engine) + ->GetID3D11Device(d3d_device)) { + device = reinterpret_cast(&d3d_device); + return true; + } + return false; +} + void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, 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 6f993167ad47d..13d05964fe712 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -679,6 +679,11 @@ FlutterWindowsView* FlutterWindowsEngine::view(FlutterViewId view_id) const { return iterator->second; } +bool FlutterWindowsEngine::GetID3D11Device(ID3D11Device** device) const { + return this->egl_manager_->GetDevice(device); +} + + // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() { return plugin_registrar_.get(); 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 a2a33e277bdc0..ddc5577f8ef26 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -141,6 +141,8 @@ class FlutterWindowsEngine { // Returns null if the view does not exist. FlutterWindowsView* view(FlutterViewId view_id) const; + bool GetID3D11Device(ID3D11Device** device) const; + // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef GetRegistrar(); diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 5d2bff132be99..551ef98ef9762 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -28,6 +28,10 @@ typedef struct FlutterDesktopViewController* FlutterDesktopViewControllerRef; struct FlutterDesktopView; typedef struct FlutterDesktopView* FlutterDesktopViewRef; +// Opaque reference to a Flutter's ID3D11Device. +struct FlutterID3D11Device; +typedef struct FlutterID3D11Device* FlutterID3D11DeviceRef; + // Opaque reference to a Flutter engine instance. struct FlutterDesktopEngine; typedef struct FlutterDesktopEngine* FlutterDesktopEngineRef; @@ -130,6 +134,11 @@ FLUTTER_EXPORT void FlutterDesktopViewControllerDestroy( FLUTTER_EXPORT FlutterDesktopViewId FlutterDesktopViewControllerGetViewId( FlutterDesktopViewControllerRef view_controller); +// assign the D3D11 device associated with the engine to the given pointer +FLUTTER_EXPORT bool FlutterDesktopPluginRegistrarGetID3D11Device( + FlutterDesktopPluginRegistrarRef registrar, + FlutterID3D11DeviceRef* device); + // Returns the handle for the engine running in FlutterDesktopViewControllerRef. // // Its lifetime is the same as the |controller|'s. From 78b99ab22c647cb5e8ad6632e5fb325a623f8f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=A0=D7=99=D7=A8?= Date: Tue, 4 Mar 2025 22:57:20 +0200 Subject: [PATCH 2/5] fmt --- .../src/flutter/shell/platform/windows/flutter_windows_engine.cc | 1 - 1 file changed, 1 deletion(-) 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 13d05964fe712..64dcc3dab622b 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -683,7 +683,6 @@ bool FlutterWindowsEngine::GetID3D11Device(ID3D11Device** device) const { return this->egl_manager_->GetDevice(device); } - // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() { return plugin_registrar_.get(); From 1a6c4e2b73e47fc13b0adbbd63b2eae82f6c83aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=A0=D7=99=D7=A8?= Date: Wed, 5 Mar 2025 09:14:27 +0200 Subject: [PATCH 3/5] [Windows] Add function to retrieve ID3D11Device for Flutter Desktop plugin --- .../shell/platform/windows/flutter_windows.cc | 27 ++++++++++--------- .../windows/flutter_windows_engine.cc | 4 --- .../platform/windows/flutter_windows_engine.h | 2 -- .../platform/windows/public/flutter_windows.h | 9 +++---- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index bcb37de9245df..80da0f9a83b6f 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -269,6 +269,21 @@ IDXGIAdapter* FlutterDesktopViewGetGraphicsAdapter(FlutterDesktopViewRef view) { return nullptr; } +FlutterID3D11DeviceRef FlutterDesktopPluginViewGetID3D11Device( + FlutterDesktopViewRef view) { + auto egl_manager = ViewFromHandle(view)->GetEngine()->egl_manager(); + if (egl_manager) { + Microsoft::WRL::ComPtr d3d_device; + if (egl_manager->GetDevice(d3d_device.GetAddressOf())) { + // Since we pass this to C (which can't use smart pointers), we need to + // AddRef the device, later on the caller will need to Release it. + d3d_device->AddRef(); + return reinterpret_cast(d3d_device.Detach()); + } + } + return nullptr; +} + bool FlutterDesktopEngineProcessExternalWindowMessage( FlutterDesktopEngineRef engine, HWND hwnd, @@ -304,18 +319,6 @@ FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetViewById( return HandleForView(registrar->engine->view(view_id)); } -bool FlutterDesktopPluginRegistrarGetID3D11Device( - FlutterDesktopPluginRegistrarRef registrar, - FlutterID3D11DeviceRef* device) { - ID3D11Device* d3d_device; - if (reinterpret_cast(registrar->engine) - ->GetID3D11Device(d3d_device)) { - device = reinterpret_cast(&d3d_device); - return true; - } - return false; -} - void FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate( FlutterDesktopPluginRegistrarRef registrar, FlutterDesktopWindowProcCallback delegate, 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 64dcc3dab622b..6f993167ad47d 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -679,10 +679,6 @@ FlutterWindowsView* FlutterWindowsEngine::view(FlutterViewId view_id) const { return iterator->second; } -bool FlutterWindowsEngine::GetID3D11Device(ID3D11Device** device) const { - return this->egl_manager_->GetDevice(device); -} - // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() { return plugin_registrar_.get(); 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 ddc5577f8ef26..a2a33e277bdc0 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -141,8 +141,6 @@ class FlutterWindowsEngine { // Returns null if the view does not exist. FlutterWindowsView* view(FlutterViewId view_id) const; - bool GetID3D11Device(ID3D11Device** device) const; - // Returns the currently configured Plugin Registrar. FlutterDesktopPluginRegistrarRef GetRegistrar(); diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 551ef98ef9762..725f29fad0ed0 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -134,11 +134,6 @@ FLUTTER_EXPORT void FlutterDesktopViewControllerDestroy( FLUTTER_EXPORT FlutterDesktopViewId FlutterDesktopViewControllerGetViewId( FlutterDesktopViewControllerRef view_controller); -// assign the D3D11 device associated with the engine to the given pointer -FLUTTER_EXPORT bool FlutterDesktopPluginRegistrarGetID3D11Device( - FlutterDesktopPluginRegistrarRef registrar, - FlutterID3D11DeviceRef* device); - // Returns the handle for the engine running in FlutterDesktopViewControllerRef. // // Its lifetime is the same as the |controller|'s. @@ -254,6 +249,10 @@ FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view); FLUTTER_EXPORT IDXGIAdapter* FlutterDesktopViewGetGraphicsAdapter( FlutterDesktopViewRef view); +// Returns the ID3D11Device used for rendering or nullptr in case of error. +FLUTTER_EXPORT FlutterID3D11DeviceRef FlutterDesktopPluginViewGetID3D11Device( + FlutterDesktopViewRef view); + // Called to pass an external window message to the engine for lifecycle // state updates. Non-Flutter windows must call this method in their WndProc // in order to be included in the logic for application lifecycle state From a9c50a7cfe191fb3d70b7cd02e9ae59a9feb69c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=A0=D7=99=D7=A8?= Date: Sun, 4 May 2025 21:54:21 +0300 Subject: [PATCH 4/5] Enhance D3D11 device handling and update .gitignore for VSCode --- .gitignore | 2 +- .../windows/client_wrapper/include/flutter/flutter_view.h | 5 +++++ .../src/flutter/shell/platform/windows/flutter_windows.cc | 8 ++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 73728cebbfccf..6c32b342edb03 100644 --- a/.gitignore +++ b/.gitignore @@ -23,7 +23,7 @@ .classpath .project .settings/ -.vscode/* +**/.vscode/* .ccls-cache # This file, on the master branch, should never exist or be checked-in. diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h index 48e7607c966fc..40a7b277394a0 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h @@ -32,6 +32,11 @@ class FlutterView { return FlutterDesktopViewGetGraphicsAdapter(view_); } + FlutterID3D11DeviceRef GetID3D11Device() { + return FlutterDesktopPluginViewGetID3D11Device(view_); + } + + private: // Handle for interacting with the C API's view. FlutterDesktopViewRef view_ = nullptr; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index 80da0f9a83b6f..3eb22dcd92e67 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -275,10 +275,10 @@ FlutterID3D11DeviceRef FlutterDesktopPluginViewGetID3D11Device( if (egl_manager) { Microsoft::WRL::ComPtr d3d_device; if (egl_manager->GetDevice(d3d_device.GetAddressOf())) { - // Since we pass this to C (which can't use smart pointers), we need to - // AddRef the device, later on the caller will need to Release it. - d3d_device->AddRef(); - return reinterpret_cast(d3d_device.Detach()); + // Since we pass this to C we can't use smart pointers. + // The user should not use the pointer for the device when flutter + // disposes the engine. + return reinterpret_cast(d3d_device.Get()); } } return nullptr; From 93bdf36c526853b6794350139203e5562022a253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=A0=D7=99=D7=A8?= Date: Sun, 4 May 2025 21:55:51 +0300 Subject: [PATCH 5/5] fmt --- .../windows/client_wrapper/include/flutter/flutter_view.h | 1 - 1 file changed, 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h index 40a7b277394a0..a62c4415cb10b 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h @@ -36,7 +36,6 @@ class FlutterView { return FlutterDesktopPluginViewGetID3D11Device(view_); } - private: // Handle for interacting with the C API's view. FlutterDesktopViewRef view_ = nullptr;