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

Commit f5007e9

Browse files
authored
[Impeller] Added a switch to turn on vulkan (#42585)
fixes flutter/flutter#128286 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 2cd02eb commit f5007e9

File tree

7 files changed

+63
-24
lines changed

7 files changed

+63
-24
lines changed

common/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ struct Settings {
217217
bool enable_impeller = false;
218218
#endif
219219

220+
// Requests a particular backend to be used (ex "opengles" or "vulkan")
221+
std::optional<std::string> impeller_backend;
222+
220223
// Enable Vulkan validation on backends that support it. The validation layers
221224
// must be available to the application.
222225
bool enable_vulkan_validation = false;

shell/common/switches.cc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,23 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
443443
settings.use_asset_fonts =
444444
!command_line.HasOption(FlagForSwitch(Switch::DisableAssetFonts));
445445

446-
std::string enable_impeller_value;
447-
if (command_line.GetOptionValue(FlagForSwitch(Switch::EnableImpeller),
448-
&enable_impeller_value)) {
449-
settings.enable_impeller =
450-
enable_impeller_value.empty() || "true" == enable_impeller_value;
446+
{
447+
std::string enable_impeller_value;
448+
if (command_line.GetOptionValue(FlagForSwitch(Switch::EnableImpeller),
449+
&enable_impeller_value)) {
450+
settings.enable_impeller =
451+
enable_impeller_value.empty() || "true" == enable_impeller_value;
452+
}
453+
}
454+
455+
{
456+
std::string impeller_backend_value;
457+
if (command_line.GetOptionValue(FlagForSwitch(Switch::ImpellerBackend),
458+
&impeller_backend_value)) {
459+
if (!impeller_backend_value.empty()) {
460+
settings.impeller_backend = impeller_backend_value;
461+
}
462+
}
451463
}
452464

453465
settings.enable_vulkan_validation =

shell/common/switches.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ DEF_SWITCH(EnableImpeller,
261261
"enable-impeller",
262262
"Enable the Impeller renderer on supported platforms. Ignored if "
263263
"Impeller is not supported on the platform.")
264+
DEF_SWITCH(ImpellerBackend,
265+
"impeller-backend",
266+
"Requests a particular Impeller backend on platforms that support "
267+
"multiple backends. (ex `opengles` or `vulkan`)")
264268
DEF_SWITCH(EnableVulkanValidation,
265269
"enable-vulkan-validation",
266270
"Enable loading Vulkan validation layers. The layers must be "

shell/platform/android/android_context_gl_impeller.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static std::shared_ptr<impeller::Context> CreateImpellerContext(
7474
FML_LOG(ERROR) << "Could not add reactor worker.";
7575
return nullptr;
7676
}
77-
FML_LOG(ERROR) << "Using the Impeller rendering backend.";
77+
FML_LOG(ERROR) << "Using the Impeller rendering backend (OpenGLES).";
7878
return context;
7979
}
8080

shell/platform/android/android_context_vulkan_impeller.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ static std::shared_ptr<impeller::Context> CreateImpellerContext(
3434
settings.cache_directory = fml::paths::GetCachesDirectory();
3535
settings.worker_task_runner = std::move(worker_task_runner);
3636
settings.enable_validation = enable_vulkan_validation;
37+
38+
FML_LOG(ERROR) << "Using the Impeller rendering backend (Vulkan).";
39+
3740
return impeller::ContextVK::Create(std::move(settings));
3841
}
3942

shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class FlutterLoader {
4141
"io.flutter.embedding.android.OldGenHeapSize";
4242
private static final String ENABLE_IMPELLER_META_DATA_KEY =
4343
"io.flutter.embedding.android.EnableImpeller";
44+
private static final String IMPELLER_BACKEND_META_DATA_KEY =
45+
"io.flutter.embedding.android.ImpellerBackend";
4446

4547
/**
4648
* Set whether leave or clean up the VM after the last shell shuts down. It can be set from app's
@@ -316,8 +318,12 @@ public void ensureInitializationComplete(
316318

317319
shellArgs.add("--prefetched-default-font-manager");
318320

319-
if (metaData != null && metaData.getBoolean(ENABLE_IMPELLER_META_DATA_KEY, false)) {
320-
shellArgs.add("--enable-impeller");
321+
if (metaData != null) {
322+
if (metaData.getBoolean(ENABLE_IMPELLER_META_DATA_KEY, false)) {
323+
shellArgs.add("--enable-impeller");
324+
}
325+
String backend = metaData.getString(IMPELLER_BACKEND_META_DATA_KEY, "opengles");
326+
shellArgs.add("--impeller-backend=" + backend);
321327
}
322328

323329
final String leakVM = isLeakVM(metaData) ? "true" : "false";

shell/platform/android/platform_view_android.cc

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,9 @@ std::unique_ptr<AndroidSurface> AndroidSurfaceFactoryImpl::CreateSurface() {
5151
}
5252
case AndroidRenderingAPI::kVulkan:
5353
FML_DCHECK(enable_impeller_);
54-
// TODO(kaushikiska@): Enable this after wiring a preference for Vulkan
55-
// backend.
56-
#if false
57-
return std::make_unique<AndroidSurfaceVulkanImpeller>(
54+
return std::make_unique<AndroidSurfaceVulkanImpeller>(
5855
std::static_pointer_cast<AndroidContextVulkanImpeller>(
5956
android_context_));
60-
#else
61-
return std::make_unique<AndroidSurfaceGLImpeller>(
62-
std::static_pointer_cast<AndroidContextGLImpeller>(android_context_));
63-
#endif
6457
default:
6558
FML_DCHECK(false);
6659
return nullptr;
@@ -73,19 +66,36 @@ static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
7366
const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
7467
uint8_t msaa_samples,
7568
bool enable_impeller,
69+
const std::optional<std::string>& impeller_backend,
7670
bool enable_vulkan_validation) {
7771
if (use_software_rendering) {
7872
return std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
7973
}
8074
if (enable_impeller) {
81-
// TODO(kaushikiska@): Enable this after wiring a preference for Vulkan
82-
// backend.
83-
#if false
84-
return std::make_unique<AndroidContextVulkanImpeller>(enable_vulkan_validation, std::move(worker_task_runner));
85-
#else
86-
return std::make_unique<AndroidContextGLImpeller>(
87-
std::make_unique<impeller::egl::Display>());
88-
#endif
75+
// TODO(gaaclarke): We need to devise a more complete heuristic about what
76+
// backend to use by default.
77+
// Default value is OpenGLES.
78+
AndroidRenderingAPI backend = AndroidRenderingAPI::kOpenGLES;
79+
if (impeller_backend.has_value()) {
80+
if (impeller_backend.value() == "opengles") {
81+
backend = AndroidRenderingAPI::kOpenGLES;
82+
} else if (impeller_backend.value() == "vulkan") {
83+
backend = AndroidRenderingAPI::kVulkan;
84+
} else {
85+
FML_CHECK(impeller_backend.value() == "vulkan" ||
86+
impeller_backend.value() == "opengles");
87+
}
88+
}
89+
switch (backend) {
90+
case AndroidRenderingAPI::kOpenGLES:
91+
return std::make_unique<AndroidContextGLImpeller>(
92+
std::make_unique<impeller::egl::Display>());
93+
case AndroidRenderingAPI::kVulkan:
94+
return std::make_unique<AndroidContextVulkanImpeller>(
95+
enable_vulkan_validation, worker_task_runner);
96+
default:
97+
FML_UNREACHABLE();
98+
}
8999
}
90100
return std::make_unique<AndroidContextGLSkia>(
91101
AndroidRenderingAPI::kOpenGLES, //
@@ -112,6 +122,7 @@ PlatformViewAndroid::PlatformViewAndroid(
112122
worker_task_runner,
113123
msaa_samples,
114124
delegate.OnPlatformViewGetSettings().enable_impeller,
125+
delegate.OnPlatformViewGetSettings().impeller_backend,
115126
delegate.OnPlatformViewGetSettings().enable_vulkan_validation)) {}
116127

117128
PlatformViewAndroid::PlatformViewAndroid(

0 commit comments

Comments
 (0)