Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit bc4a27f

Browse files
authored
[shell tests] Integrate Vulkan with Shell Tests (#16621)
This change creates a test only implementation of flutter::Surface backed by an offscreen Vulkan GrContext. Much of the code in this test Surface was lifted from flutter::VulkanWindow which I was unable to use without extricating it from the VkSurface/VkSwapchain code which we do not want to use in offscreen tests. I would recommend refactoring VulkanWindow to separate GrContext creation and VkSwapchain creation in order to promote greater code reuse between onscreen and offscreen vulkan paths. This change is excersised thoroughly by the shell tests and was manually tested against these tests on Fuchsia on Intel.
1 parent 0ef67b5 commit bc4a27f

File tree

3 files changed

+166
-11
lines changed

3 files changed

+166
-11
lines changed

shell/common/shell_test_platform_view_vulkan.cc

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// found in the LICENSE file.
44

55
#include "flutter/shell/common/shell_test_platform_view_vulkan.h"
6-
#include "flutter/shell/gpu/gpu_surface_vulkan.h"
76

87
namespace flutter {
98
namespace testing {
@@ -30,7 +29,7 @@ void ShellTestPlatformViewVulkan::SimulateVSync() {
3029

3130
// |PlatformView|
3231
std::unique_ptr<Surface> ShellTestPlatformViewVulkan::CreateRenderingSurface() {
33-
return std::make_unique<GPUSurfaceVulkan>(this, nullptr, true);
32+
return std::make_unique<OffScreenSurface>(proc_table_);
3433
}
3534

3635
// |PlatformView|
@@ -40,9 +39,132 @@ PointerDataDispatcherMaker ShellTestPlatformViewVulkan::GetDispatcherMaker() {
4039
};
4140
}
4241

43-
// |GPUSurfaceVulkanDelegate|
44-
fml::RefPtr<vulkan::VulkanProcTable> ShellTestPlatformViewVulkan::vk() {
45-
return proc_table_;
42+
// TODO(gw280): This code was forked from vulkan_window.cc specifically for
43+
// shell_test.
44+
// We need to merge this functionality back into //vulkan.
45+
// https://github.com/flutter/flutter/issues/51132
46+
ShellTestPlatformViewVulkan::OffScreenSurface::OffScreenSurface(
47+
fml::RefPtr<vulkan::VulkanProcTable> vk)
48+
: valid_(false), vk_(std::move(vk)) {
49+
if (!vk_ || !vk_->HasAcquiredMandatoryProcAddresses()) {
50+
FML_DLOG(ERROR) << "Proc table has not acquired mandatory proc addresses.";
51+
return;
52+
}
53+
54+
// Create the application instance.
55+
std::vector<std::string> extensions = {};
56+
57+
application_ = std::make_unique<vulkan::VulkanApplication>(
58+
*vk_, "FlutterTest", std::move(extensions));
59+
60+
if (!application_->IsValid() || !vk_->AreInstanceProcsSetup()) {
61+
// Make certain the application instance was created and it setup the
62+
// instance proc table entries.
63+
FML_DLOG(ERROR) << "Instance proc addresses have not been setup.";
64+
return;
65+
}
66+
67+
// Create the device.
68+
69+
logical_device_ = application_->AcquireFirstCompatibleLogicalDevice();
70+
71+
if (logical_device_ == nullptr || !logical_device_->IsValid() ||
72+
!vk_->AreDeviceProcsSetup()) {
73+
// Make certain the device was created and it setup the device proc table
74+
// entries.
75+
FML_DLOG(ERROR) << "Device proc addresses have not been setup.";
76+
return;
77+
}
78+
79+
// Create the Skia GrContext.
80+
if (!CreateSkiaGrContext()) {
81+
FML_DLOG(ERROR) << "Could not create Skia context.";
82+
return;
83+
}
84+
85+
valid_ = true;
86+
}
87+
88+
bool ShellTestPlatformViewVulkan::OffScreenSurface::CreateSkiaGrContext() {
89+
GrVkBackendContext backend_context;
90+
91+
if (!CreateSkiaBackendContext(&backend_context)) {
92+
FML_DLOG(ERROR) << "Could not create Skia backend context.";
93+
return false;
94+
}
95+
96+
sk_sp<GrContext> context = GrContext::MakeVulkan(backend_context);
97+
98+
if (context == nullptr) {
99+
FML_DLOG(ERROR) << "Failed to create GrContext";
100+
return false;
101+
}
102+
103+
context->setResourceCacheLimits(vulkan::kGrCacheMaxCount,
104+
vulkan::kGrCacheMaxByteSize);
105+
106+
context_ = context;
107+
108+
return true;
109+
}
110+
111+
bool ShellTestPlatformViewVulkan::OffScreenSurface::CreateSkiaBackendContext(
112+
GrVkBackendContext* context) {
113+
auto getProc = vk_->CreateSkiaGetProc();
114+
115+
if (getProc == nullptr) {
116+
FML_DLOG(ERROR) << "GetProcAddress is null";
117+
return false;
118+
}
119+
120+
uint32_t skia_features = 0;
121+
if (!logical_device_->GetPhysicalDeviceFeaturesSkia(&skia_features)) {
122+
FML_DLOG(ERROR) << "Failed to get Physical Device features";
123+
return false;
124+
}
125+
126+
context->fInstance = application_->GetInstance();
127+
context->fPhysicalDevice = logical_device_->GetPhysicalDeviceHandle();
128+
context->fDevice = logical_device_->GetHandle();
129+
context->fQueue = logical_device_->GetQueueHandle();
130+
context->fGraphicsQueueIndex = logical_device_->GetGraphicsQueueIndex();
131+
context->fMinAPIVersion = application_->GetAPIVersion();
132+
context->fMaxAPIVersion = application_->GetAPIVersion();
133+
context->fFeatures = skia_features;
134+
context->fGetProc = std::move(getProc);
135+
context->fOwnsInstanceAndDevice = false;
136+
return true;
137+
}
138+
139+
ShellTestPlatformViewVulkan::OffScreenSurface::~OffScreenSurface() {}
140+
141+
bool ShellTestPlatformViewVulkan::OffScreenSurface::IsValid() {
142+
return valid_;
143+
}
144+
145+
std::unique_ptr<SurfaceFrame>
146+
ShellTestPlatformViewVulkan::OffScreenSurface::AcquireFrame(
147+
const SkISize& size) {
148+
auto image_info = SkImageInfo::Make(size, SkColorType::kRGBA_8888_SkColorType,
149+
SkAlphaType::kOpaque_SkAlphaType);
150+
auto surface = SkSurface::MakeRenderTarget(context_.get(), SkBudgeted::kNo,
151+
image_info, 0, nullptr);
152+
SurfaceFrame::SubmitCallback callback =
153+
[](const SurfaceFrame&, SkCanvas* canvas) -> bool { return true; };
154+
155+
return std::make_unique<SurfaceFrame>(std::move(surface), true,
156+
std::move(callback));
157+
}
158+
159+
GrContext* ShellTestPlatformViewVulkan::OffScreenSurface::GetContext() {
160+
return context_.get();
161+
}
162+
163+
SkMatrix ShellTestPlatformViewVulkan::OffScreenSurface::GetRootTransformation()
164+
const {
165+
SkMatrix matrix;
166+
matrix.reset();
167+
return matrix;
46168
}
47169

48170
} // namespace testing

shell/common/shell_test_platform_view_vulkan.h

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
#include "flutter/shell/common/shell_test_platform_view.h"
99
#include "flutter/shell/gpu/gpu_surface_vulkan_delegate.h"
10+
#include "flutter/vulkan/vulkan_application.h"
11+
#include "flutter/vulkan/vulkan_device.h"
1012

1113
namespace flutter {
1214
namespace testing {
1315

14-
class ShellTestPlatformViewVulkan : public ShellTestPlatformView,
15-
public GPUSurfaceVulkanDelegate {
16+
class ShellTestPlatformViewVulkan : public ShellTestPlatformView {
1617
public:
1718
ShellTestPlatformViewVulkan(PlatformView::Delegate& delegate,
1819
TaskRunners task_runners,
@@ -24,6 +25,36 @@ class ShellTestPlatformViewVulkan : public ShellTestPlatformView,
2425
void SimulateVSync() override;
2526

2627
private:
28+
class OffScreenSurface : public flutter::Surface {
29+
public:
30+
OffScreenSurface(fml::RefPtr<vulkan::VulkanProcTable> vk);
31+
32+
~OffScreenSurface() override;
33+
34+
// |Surface|
35+
bool IsValid() override;
36+
37+
// |Surface|
38+
std::unique_ptr<SurfaceFrame> AcquireFrame(const SkISize& size) override;
39+
40+
SkMatrix GetRootTransformation() const override;
41+
42+
// |Surface|
43+
GrContext* GetContext() override;
44+
45+
private:
46+
bool valid_;
47+
fml::RefPtr<vulkan::VulkanProcTable> vk_;
48+
std::unique_ptr<vulkan::VulkanApplication> application_;
49+
std::unique_ptr<vulkan::VulkanDevice> logical_device_;
50+
sk_sp<GrContext> context_;
51+
52+
bool CreateSkiaGrContext();
53+
bool CreateSkiaBackendContext(GrVkBackendContext* context);
54+
55+
FML_DISALLOW_COPY_AND_ASSIGN(OffScreenSurface);
56+
};
57+
2758
CreateVsyncWaiter create_vsync_waiter_;
2859

2960
std::shared_ptr<ShellTestVsyncClock> vsync_clock_;
@@ -39,9 +70,6 @@ class ShellTestPlatformViewVulkan : public ShellTestPlatformView,
3970
// |PlatformView|
4071
PointerDataDispatcherMaker GetDispatcherMaker() override;
4172

42-
// |GPUSurfaceVulkanDelegate|
43-
fml::RefPtr<vulkan::VulkanProcTable> vk() override;
44-
4573
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformViewVulkan);
4674
};
4775

testing/fuchsia/meta/fuchsia_test.cmx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
],
1010
"services": [
1111
"fuchsia.accessibility.semantics.SemanticsManager",
12-
"fuchsia.process.Launcher"
12+
"fuchsia.process.Launcher",
13+
"fuchsia.deprecatedtimezone.Timezone",
14+
"fuchsia.netstack.Netstack",
15+
"fuchsia.vulkan.loader.Loader",
16+
"fuchsia.logger.LogSink",
17+
"fuchsia.tracing.provider.Registry"
1318
]
1419
}
1520
}

0 commit comments

Comments
 (0)