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

[Impeller] make GPU tracing off by default. #50215

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ struct Settings {
// Some devices claim to support the required APIs but crash on their usage.
bool enable_opengl_gpu_tracing = false;

// Enable GPU tracing in Vulkan backends.
bool enable_vulkan_gpu_tracing = false;

// Data set by platform-specific embedders for use in font initialization.
uint32_t font_initialization_data = 0;

Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/vulkan/context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ void ContextVK::Setup(Settings settings) {

// Create the GPU Tracer later because it depends on state from
// the ContextVK.
gpu_tracer_ = std::make_shared<GPUTracerVK>(weak_from_this());
gpu_tracer_ = std::make_shared<GPUTracerVK>(weak_from_this(),
settings.enable_gpu_tracing);
gpu_tracer_->InitializeQueryPool(*this);

//----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions impeller/renderer/backend/vulkan/context_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ContextVK final : public Context,
std::vector<std::shared_ptr<fml::Mapping>> shader_libraries_data;
fml::UniqueFD cache_directory;
bool enable_validation = false;
bool enable_gpu_tracing = false;

Settings() = default;

Expand Down
6 changes: 5 additions & 1 deletion impeller/renderer/backend/vulkan/gpu_tracer_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ namespace impeller {

static constexpr uint32_t kPoolSize = 128u;

GPUTracerVK::GPUTracerVK(std::weak_ptr<ContextVK> context)
GPUTracerVK::GPUTracerVK(std::weak_ptr<ContextVK> context,
bool enable_gpu_tracing)
: context_(std::move(context)) {
if (!enable_gpu_tracing) {
return;
}
timestamp_period_ = context_.lock()
->GetDeviceHolder()
->GetPhysicalDevice()
Expand Down
7 changes: 6 additions & 1 deletion impeller/renderer/backend/vulkan/gpu_tracer_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ class GPUProbe;

/// @brief A class that uses timestamp queries to record the approximate GPU
/// execution time.
///
/// To enable, add the following metadata to the application's Android manifest:
/// <meta-data
/// android:name="io.flutter.embedding.android.EnableVulkanGPUTracing"
/// android:value="false" />
class GPUTracerVK : public std::enable_shared_from_this<GPUTracerVK> {
public:
explicit GPUTracerVK(std::weak_ptr<ContextVK> context);
GPUTracerVK(std::weak_ptr<ContextVK> context, bool enable_gpu_tracing);

~GPUTracerVK() = default;

Expand Down
3 changes: 1 addition & 2 deletions impeller/renderer/backend/vulkan/swapchain_impl_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ bool SwapchainImplVK::Present(const std::shared_ptr<SwapchainImageVK>& image,

const auto& context = ContextVK::Cast(*context_strong);
const auto& sync = synchronizers_[current_frame_];
context.GetGPUTracer()->MarkFrameEnd();

//----------------------------------------------------------------------------
/// Transition the image to color-attachment-optimal.
Expand Down Expand Up @@ -489,8 +490,6 @@ bool SwapchainImplVK::Present(const std::shared_ptr<SwapchainImageVK>& image,
}
}

context.GetGPUTracer()->MarkFrameEnd();

auto task = [&, index, current_frame = current_frame_] {
auto context_strong = context_.lock();
if (!context_strong) {
Expand Down
33 changes: 30 additions & 3 deletions impeller/renderer/backend/vulkan/test/gpu_tracer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,25 @@ namespace impeller {
namespace testing {

#ifdef IMPELLER_DEBUG
TEST(GPUTracerVK, CanBeDisabled) {
auto const context =
MockVulkanContextBuilder()
.SetSettingsCallback([](ContextVK::Settings& settings) {
settings.enable_gpu_tracing = false;
})
.Build();
auto tracer = context->GetGPUTracer();

ASSERT_FALSE(tracer->IsEnabled());
}

TEST(GPUTracerVK, CanTraceCmdBuffer) {
auto const context = MockVulkanContextBuilder().Build();
auto const context =
MockVulkanContextBuilder()
.SetSettingsCallback([](ContextVK::Settings& settings) {
settings.enable_gpu_tracing = true;
})
.Build();
auto tracer = context->GetGPUTracer();

ASSERT_TRUE(tracer->IsEnabled());
Expand Down Expand Up @@ -48,7 +65,12 @@ TEST(GPUTracerVK, CanTraceCmdBuffer) {
}

TEST(GPUTracerVK, DoesNotTraceOutsideOfFrameWorkload) {
auto const context = MockVulkanContextBuilder().Build();
auto const context =
MockVulkanContextBuilder()
.SetSettingsCallback([](ContextVK::Settings& settings) {
settings.enable_gpu_tracing = true;
})
.Build();
auto tracer = context->GetGPUTracer();

ASSERT_TRUE(tracer->IsEnabled());
Expand Down Expand Up @@ -78,7 +100,12 @@ TEST(GPUTracerVK, DoesNotTraceOutsideOfFrameWorkload) {
// This cmd buffer starts when there is a frame but finishes when there is none.
// This should result in the same recorded work.
TEST(GPUTracerVK, TracesWithPartialFrameOverlap) {
auto const context = MockVulkanContextBuilder().Build();
auto const context =
MockVulkanContextBuilder()
.SetSettingsCallback([](ContextVK::Settings& settings) {
settings.enable_gpu_tracing = true;
})
.Build();
auto tracer = context->GetGPUTracer();

ASSERT_TRUE(tracer->IsEnabled());
Expand Down
2 changes: 2 additions & 0 deletions shell/common/switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
command_line.HasOption(FlagForSwitch(Switch::EnableVulkanValidation));
settings.enable_opengl_gpu_tracing =
command_line.HasOption(FlagForSwitch(Switch::EnableOpenGLGPUTracing));
settings.enable_vulkan_gpu_tracing =
command_line.HasOption(FlagForSwitch(Switch::EnableVulkanGPUTracing));

settings.enable_embedder_api =
command_line.HasOption(FlagForSwitch(Switch::EnableEmbedderAPI));
Expand Down
4 changes: 4 additions & 0 deletions shell/common/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ DEF_SWITCH(EnableOpenGLGPUTracing,
"enable-opengl-gpu-tracing",
"Enable tracing of GPU execution time when using the Impeller "
"OpenGLES backend.")
DEF_SWITCH(EnableVulkanGPUTracing,
"enable-vulkan-gpu-tracing",
"Enable tracing of GPU execution time when using the Impeller "
"Vulkan backend.")
DEF_SWITCH(LeakVM,
"leak-vm",
"When the last shell shuts down, the shared VM is leaked by default "
Expand Down
10 changes: 7 additions & 3 deletions shell/platform/android/android_context_vulkan_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace flutter {

static std::shared_ptr<impeller::Context> CreateImpellerContext(
const fml::RefPtr<vulkan::VulkanProcTable>& proc_table,
bool enable_vulkan_validation) {
bool enable_vulkan_validation,
bool enable_gpu_tracing) {
std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_vk_data,
impeller_entity_shaders_vk_length),
Expand All @@ -41,6 +42,7 @@ static std::shared_ptr<impeller::Context> CreateImpellerContext(
settings.shader_libraries_data = std::move(shader_mappings);
settings.cache_directory = fml::paths::GetCachesDirectory();
settings.enable_validation = enable_vulkan_validation;
settings.enable_gpu_tracing = enable_gpu_tracing;

auto context = impeller::ContextVK::Create(std::move(settings));

Expand All @@ -56,10 +58,12 @@ static std::shared_ptr<impeller::Context> CreateImpellerContext(
}

AndroidContextVulkanImpeller::AndroidContextVulkanImpeller(
bool enable_validation)
bool enable_validation,
bool enable_gpu_tracing)
: AndroidContext(AndroidRenderingAPI::kVulkan),
proc_table_(fml::MakeRefCounted<vulkan::VulkanProcTable>()) {
auto impeller_context = CreateImpellerContext(proc_table_, enable_validation);
auto impeller_context =
CreateImpellerContext(proc_table_, enable_validation, enable_gpu_tracing);
SetImpellerContext(impeller_context);
is_valid_ =
proc_table_->HasAcquiredMandatoryProcAddresses() && impeller_context;
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/android/android_context_vulkan_impeller.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace flutter {

class AndroidContextVulkanImpeller : public AndroidContext {
public:
explicit AndroidContextVulkanImpeller(bool enable_validation);
AndroidContextVulkanImpeller(bool enable_validation, bool enable_gpu_tracing);

~AndroidContextVulkanImpeller();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class FlutterLoader {
"io.flutter.embedding.android.ImpellerBackend";
private static final String IMPELLER_OPENGL_GPU_TRACING_DATA_KEY =
"io.flutter.embedding.android.EnableOpenGLGPUTracing";
private static final String IMPELLER_VULKAN_GPU_TRACING_DATA_KEY =
"io.flutter.embedding.android.EnableVulkanGPUTracing";

/**
* Set whether leave or clean up the VM after the last shell shuts down. It can be set from app's
Expand Down Expand Up @@ -333,6 +335,9 @@ ENABLE_VULKAN_VALIDATION_META_DATA_KEY, areValidationLayersOnByDefault())) {
if (metaData.getBoolean(IMPELLER_OPENGL_GPU_TRACING_DATA_KEY, false)) {
shellArgs.add("--enable-opengl-gpu-tracing");
}
if (metaData.getBoolean(IMPELLER_VULKAN_GPU_TRACING_DATA_KEY, false)) {
shellArgs.add("--enable-vulkan-gpu-tracing");
}
String backend = metaData.getString(IMPELLER_BACKEND_META_DATA_KEY);
if (backend != null) {
shellArgs.add("--impeller-backend=" + backend);
Expand Down
10 changes: 6 additions & 4 deletions shell/platform/android/platform_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
bool enable_impeller,
const std::optional<std::string>& impeller_backend,
bool enable_vulkan_validation,
bool enable_opengl_gpu_tracing) {
bool enable_opengl_gpu_tracing,
bool enable_vulkan_gpu_tracing) {
if (use_software_rendering) {
FML_DCHECK(!enable_impeller);
return std::make_shared<AndroidContext>(AndroidRenderingAPI::kSoftware);
Expand Down Expand Up @@ -119,10 +120,10 @@ static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
enable_opengl_gpu_tracing);
case AndroidRenderingAPI::kVulkan:
return std::make_unique<AndroidContextVulkanImpeller>(
enable_vulkan_validation);
enable_vulkan_validation, enable_vulkan_gpu_tracing);
case AndroidRenderingAPI::kAutoselect: {
auto vulkan_backend = std::make_unique<AndroidContextVulkanImpeller>(
enable_vulkan_validation);
enable_vulkan_validation, enable_vulkan_gpu_tracing);
if (!vulkan_backend->IsValid()) {
return std::make_unique<AndroidContextGLImpeller>(
std::make_unique<impeller::egl::Display>(),
Expand Down Expand Up @@ -159,7 +160,8 @@ PlatformViewAndroid::PlatformViewAndroid(
delegate.OnPlatformViewGetSettings().enable_impeller,
delegate.OnPlatformViewGetSettings().impeller_backend,
delegate.OnPlatformViewGetSettings().enable_vulkan_validation,
delegate.OnPlatformViewGetSettings().enable_opengl_gpu_tracing)) {
delegate.OnPlatformViewGetSettings().enable_opengl_gpu_tracing,
delegate.OnPlatformViewGetSettings().enable_vulkan_gpu_tracing)) {
}

PlatformViewAndroid::PlatformViewAndroid(
Expand Down