Skip to content

Commit 0cada84

Browse files
jnschulzecfontas
authored andcommitted
Windows: Client wrapper: Support querying the graphics adapter chosen by ANGLE (flutter#29294)
1 parent b161cad commit 0cada84

File tree

7 files changed

+68
-0
lines changed

7 files changed

+68
-0
lines changed

shell/platform/windows/client_wrapper/flutter_view_unittests.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace {
1616
// Stub implementation to validate calls to the API.
1717
class TestWindowsApi : public testing::StubFlutterWindowsApi {
1818
HWND ViewGetHWND() override { return reinterpret_cast<HWND>(7); }
19+
20+
IDXGIAdapter* ViewGetGraphicsAdapter() override {
21+
return reinterpret_cast<IDXGIAdapter*>(8);
22+
}
1923
};
2024

2125
} // namespace
@@ -28,4 +32,14 @@ TEST(FlutterViewTest, HwndAccessPassesThrough) {
2832
EXPECT_EQ(view.GetNativeWindow(), reinterpret_cast<HWND>(7));
2933
}
3034

35+
TEST(FlutterViewTest, GraphicsAdapterAccessPassesThrough) {
36+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
37+
std::make_unique<TestWindowsApi>());
38+
auto test_api = static_cast<TestWindowsApi*>(scoped_api_stub.stub());
39+
FlutterView view(reinterpret_cast<FlutterDesktopViewRef>(2));
40+
41+
IDXGIAdapter* adapter = view.GetGraphicsAdapter();
42+
EXPECT_EQ(adapter, reinterpret_cast<IDXGIAdapter*>(8));
43+
}
44+
3145
} // namespace flutter

shell/platform/windows/client_wrapper/include/flutter/flutter_view.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class FlutterView {
2323
// Returns the backing HWND for the view.
2424
HWND GetNativeWindow() { return FlutterDesktopViewGetHWND(view_); }
2525

26+
// Returns the DXGI adapter used for rendering or nullptr in case of error.
27+
IDXGIAdapter* GetGraphicsAdapter() {
28+
return FlutterDesktopViewGetGraphicsAdapter(view_);
29+
}
30+
2631
private:
2732
// Handle for interacting with the C API's view.
2833
FlutterDesktopViewRef view_ = nullptr;

shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef controller) {
151151
return reinterpret_cast<HWND>(-1);
152152
}
153153

154+
IDXGIAdapter* FlutterDesktopViewGetGraphicsAdapter(FlutterDesktopViewRef view) {
155+
if (s_stub_implementation) {
156+
return s_stub_implementation->ViewGetGraphicsAdapter();
157+
}
158+
return nullptr;
159+
}
160+
154161
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
155162
FlutterDesktopPluginRegistrarRef controller) {
156163
// The stub ignores this, so just return an arbitrary non-zero value.

shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class StubFlutterWindowsApi {
7272
// Called for FlutterDesktopViewGetHWND.
7373
virtual HWND ViewGetHWND() { return reinterpret_cast<HWND>(1); }
7474

75+
// Called for FlutterDesktopViewGetGraphicsAdapter.
76+
virtual IDXGIAdapter* ViewGetGraphicsAdapter() {
77+
return reinterpret_cast<IDXGIAdapter*>(2);
78+
}
79+
7580
// Called for FlutterDesktopPluginRegistrarRegisterTopLevelWindowProcDelegate.
7681
virtual void PluginRegistrarRegisterTopLevelWindowProcDelegate(
7782
FlutterDesktopWindowProcCallback delegate,

shell/platform/windows/flutter_windows.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) {
188188
return ViewFromHandle(view)->GetPlatformWindow();
189189
}
190190

191+
IDXGIAdapter* FlutterDesktopViewGetGraphicsAdapter(FlutterDesktopViewRef view) {
192+
auto surface_manager = ViewFromHandle(view)->GetEngine()->surface_manager();
193+
if (surface_manager) {
194+
Microsoft::WRL::ComPtr<ID3D11Device> d3d_device;
195+
Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device;
196+
if (surface_manager->GetDevice(d3d_device.GetAddressOf()) &&
197+
SUCCEEDED(d3d_device.As(&dxgi_device))) {
198+
IDXGIAdapter* adapter;
199+
if (SUCCEEDED(dxgi_device->GetAdapter(&adapter))) {
200+
return adapter;
201+
}
202+
}
203+
}
204+
return nullptr;
205+
}
206+
191207
FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView(
192208
FlutterDesktopPluginRegistrarRef registrar) {
193209
return HandleForView(registrar->engine->view());

shell/platform/windows/flutter_windows_unittests.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "flutter/shell/platform/windows/public/flutter_windows.h"
66

7+
#include <dxgi.h>
8+
#include <wrl/client.h>
79
#include <thread>
810

911
#include "flutter/fml/synchronization/count_down_latch.h"
@@ -253,5 +255,19 @@ TEST_F(WindowsTest, NextFrameCallback) {
253255
captures.frame_drawn_latch.Wait();
254256
}
255257

258+
TEST_F(WindowsTest, GetGraphicsAdapter) {
259+
auto& context = GetContext();
260+
WindowsConfigBuilder builder(context);
261+
ViewControllerPtr controller{builder.Run()};
262+
ASSERT_NE(controller, nullptr);
263+
auto view = FlutterDesktopViewControllerGetView(controller.get());
264+
265+
Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter;
266+
dxgi_adapter = FlutterDesktopViewGetGraphicsAdapter(view);
267+
ASSERT_NE(dxgi_adapter, nullptr);
268+
DXGI_ADAPTER_DESC desc{};
269+
ASSERT_TRUE(SUCCEEDED(dxgi_adapter->GetDesc(&desc)));
270+
}
271+
256272
} // namespace testing
257273
} // namespace flutter

shell/platform/windows/public/flutter_windows.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_PUBLIC_FLUTTER_H_
66
#define FLUTTER_SHELL_PLATFORM_WINDOWS_PUBLIC_FLUTTER_H_
77

8+
#include <dxgi.h>
89
#include <stddef.h>
910
#include <stdint.h>
1011
#include <windows.h>
@@ -201,6 +202,10 @@ FLUTTER_EXPORT void FlutterDesktopEngineSetNextFrameCallback(
201202
// Return backing HWND for manipulation in host application.
202203
FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view);
203204

205+
// Returns the DXGI adapter used for rendering or nullptr in case of error.
206+
FLUTTER_EXPORT IDXGIAdapter* FlutterDesktopViewGetGraphicsAdapter(
207+
FlutterDesktopViewRef view);
208+
204209
// ========== Plugin Registrar (extensions) ==========
205210
// These are Windows-specific extensions to flutter_plugin_registrar.h
206211

0 commit comments

Comments
 (0)