Skip to content

Commit

Permalink
Eliminate ShellTestPlatformView::BackendType::kDefaultBackendType
Browse files Browse the repository at this point in the history
`kDefaultBackendType` is intended to make life easier for authors of
tests, but in any switch statement where it's used (currently just a
single location), we rely on ordering it first and `#ifdef`ing out all
backends that aren't available.

Instead, we define a static function that returns the default that
callers can invoke instead. This avoids relinace on case ordering and
fallthrough.

In #56722 we split backends out
into separate translation units, and remove the `#ifdef`s which means we
can't rely on this trick anymore.
  • Loading branch information
cbracken committed Nov 22, 2024
1 parent 03eb3db commit cb6ff1a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion shell/common/animator_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ TEST_F(ShellTest, VSyncTargetTime) {
[vsync_clock, &create_vsync_waiter](Shell& shell) {
return ShellTestPlatformView::Create(
shell, shell.GetTaskRunners(), vsync_clock, create_vsync_waiter,
ShellTestPlatformView::BackendType::kDefaultBackend, nullptr,
ShellTestPlatformView::DefaultBackendType(), nullptr,
shell.GetIsGpuDisabledSyncSwitch());
},
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
Expand Down
20 changes: 12 additions & 8 deletions shell/common/shell_test_platform_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,33 @@ std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::Create(
// TODO(gw280): https://github.com/flutter/flutter/issues/50298
// Make this fully runtime configurable
switch (backend) {
case BackendType::kDefaultBackend:
#ifdef SHELL_ENABLE_GL
case BackendType::kGLBackend:
#ifdef SHELL_ENABLE_GL
return std::make_unique<ShellTestPlatformViewGL>(
delegate, task_runners, vsync_clock, create_vsync_waiter,
shell_test_external_view_embedder);
#else
FML_LOG(FATAL) << "OpenGL not enabled in this build";
return nullptr;
#endif // SHELL_ENABLE_GL
#ifdef SHELL_ENABLE_VULKAN
case BackendType::kVulkanBackend:
#ifdef SHELL_ENABLE_VULKAN
return std::make_unique<ShellTestPlatformViewVulkan>(
delegate, task_runners, vsync_clock, create_vsync_waiter,
shell_test_external_view_embedder);
#else
FML_LOG(FATAL) << "Vulkan not enabled in this build";
return nullptr;
#endif // SHELL_ENABLE_VULKAN
#ifdef SHELL_ENABLE_METAL
case BackendType::kMetalBackend:
#ifdef SHELL_ENABLE_METAL
return std::make_unique<ShellTestPlatformViewMetal>(
delegate, task_runners, vsync_clock, create_vsync_waiter,
shell_test_external_view_embedder, is_gpu_disabled_sync_switch);
#endif // SHELL_ENABLE_METAL

default:
FML_LOG(FATAL) << "No backends supported for ShellTestPlatformView";
#else
FML_LOG(FATAL) << "Metal not enabled in this build";
return nullptr;
#endif // SHELL_ENABLE_METAL
}
}

Expand Down
18 changes: 16 additions & 2 deletions shell/common/shell_test_platform_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#ifndef FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_H_
#define FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_H_

#include <exception>

#include "flutter/shell/common/platform_view.h"
#include "flutter/shell/common/shell_test_external_view_embedder.h"
#include "flutter/shell/common/vsync_waiters_test.h"
Expand All @@ -15,12 +17,24 @@ namespace testing {
class ShellTestPlatformView : public PlatformView {
public:
enum class BackendType {
kDefaultBackend = 0,
kGLBackend,
kVulkanBackend,
kMetalBackend,
};

static BackendType DefaultBackendType() {
#if defined(SHELL_ENABLE_GL)
return BackendType::kGLBackend;
#elif defined(SHELL_ENABLE_METAL)
return BackendType::kMetalBackend;
#elif defined(SHELL_ENABLE_VULKAN)
return BackendType::kMetalBackend;
#else
FML_LOG(FATAL) << "No backend is enabled in this build.";
std::terminate();
#endif
}

static std::unique_ptr<ShellTestPlatformView> Create(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
Expand Down Expand Up @@ -50,7 +64,7 @@ class ShellTestPlatformViewBuilder {
std::shared_ptr<ShellTestExternalViewEmbedder>
shell_test_external_view_embedder = nullptr;
ShellTestPlatformView::BackendType rendering_backend =
ShellTestPlatformView::BackendType::kDefaultBackend;
ShellTestPlatformView::DefaultBackendType();
};

explicit ShellTestPlatformViewBuilder(Config config);
Expand Down
2 changes: 1 addition & 1 deletion shell/common/shell_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ TEST_F(ShellTest,
return static_cast<std::unique_ptr<VsyncWaiter>>(
std::make_unique<VsyncWaiterFallback>(task_runners));
},
ShellTestPlatformView::BackendType::kDefaultBackend, nullptr,
ShellTestPlatformView::DefaultBackendType(), nullptr,
shell.GetIsGpuDisabledSyncSwitch());
},
[](Shell& shell) { return std::make_unique<Rasterizer>(shell); });
Expand Down

0 comments on commit cb6ff1a

Please sign in to comment.