Skip to content

Commit

Permalink
Extract backend-specific code in ShellTestPlatformView
Browse files Browse the repository at this point in the history
Moves code specific to each graphics backend into the (existing)
translation unit associated with that backend.

Previously, we could not include any Objective-C types in
shell_test_platform_view_metal.h, since that file was included into
shell_test_platform_view.cc, which is pure C++. To work around this, we
had encapsulated Objective-C Metal types in a `DarwinContextMetal`
struct hidden in the implementation file with a pointer to it
forward-declared in the header.

We now use Metal types directly in the header, without the workarounds.

Issue: flutter/flutter#158998
Issue: flutter/flutter#137801
  • Loading branch information
cbracken committed Nov 20, 2024
1 parent 3f19207 commit 34b7c2d
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 103 deletions.
82 changes: 53 additions & 29 deletions shell/common/shell_test_platform_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,9 @@

#include "flutter/shell/common/shell_test_platform_view.h"

#ifdef SHELL_ENABLE_GL
#include "flutter/shell/common/shell_test_platform_view_gl.h"
#endif // SHELL_ENABLE_GL
#ifdef SHELL_ENABLE_VULKAN
#include "flutter/shell/common/shell_test_platform_view_vulkan.h"
#endif // SHELL_ENABLE_VULKAN
#ifdef SHELL_ENABLE_METAL
#include "flutter/shell/common/shell_test_platform_view_metal.h"
#endif // SHELL_ENABLE_METAL

#include "flutter/shell/common/vsync_waiter_fallback.h"

namespace flutter {
namespace testing {
namespace flutter::testing {

std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::Create(
PlatformView::Delegate& delegate,
Expand All @@ -32,28 +21,18 @@ std::unique_ptr<ShellTestPlatformView> ShellTestPlatformView::Create(
// Make this fully runtime configurable
switch (backend) {
case BackendType::kDefaultBackend:
#ifdef SHELL_ENABLE_GL
case BackendType::kGLBackend:
return std::make_unique<ShellTestPlatformViewGL>(
return CreatePlatformViewGL(
delegate, task_runners, vsync_clock, create_vsync_waiter,
shell_test_external_view_embedder);
#endif // SHELL_ENABLE_GL
#ifdef SHELL_ENABLE_VULKAN
shell_test_external_view_embedder, is_gpu_disabled_sync_switch);
case BackendType::kVulkanBackend:
return std::make_unique<ShellTestPlatformViewVulkan>(
return CreatePlatformViewVulkan(
delegate, task_runners, vsync_clock, create_vsync_waiter,
shell_test_external_view_embedder);
#endif // SHELL_ENABLE_VULKAN
#ifdef SHELL_ENABLE_METAL
shell_test_external_view_embedder, is_gpu_disabled_sync_switch);
case BackendType::kMetalBackend:
return std::make_unique<ShellTestPlatformViewMetal>(
return CreatePlatformViewMetal(
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";
return nullptr;
}
}

Expand Down Expand Up @@ -86,5 +65,50 @@ std::unique_ptr<PlatformView> ShellTestPlatformViewBuilder::operator()(
);
}

} // namespace testing
} // namespace flutter
#ifndef SHELL_ENABLE_GL
// Fallback implementation.
// See: flutter/shell/common/shell_test_platform_view_gl.cc
std::unique_ptr<ShellTestPlatformView>
ShellTestPlatformView::CreatePlatformViewGL(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
const CreateVsyncWaiter& create_vsync_waiter,
const std::shared_ptr<ShellTestExternalViewEmbedder>&
shell_test_external_view_embedder,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
return nullptr;
}
#endif // SHELL_ENABLE_GL
#ifndef SHELL_ENABLE_METAL
// Fallback implementation.
// See: flutter/shell/common/shell_test_platform_view_metal.mm
std::unique_ptr<ShellTestPlatformView>
ShellTestPlatformView::CreatePlatformViewMetal(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
const CreateVsyncWaiter& create_vsync_waiter,
const std::shared_ptr<ShellTestExternalViewEmbedder>&
shell_test_external_view_embedder,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
return nullptr;
}
#endif // SHELL_ENABLE_METAL
#ifndef SHELL_ENABLE_VULKAN
// Fallback implementation.
// See: flutter/shell/common/shell_test_platform_view_vulkan.cc
std::unique_ptr<ShellTestPlatformView>
ShellTestPlatformView::CreatePlatformViewVulkan(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
const CreateVsyncWaiter& create_vsync_waiter,
const std::shared_ptr<ShellTestExternalViewEmbedder>&
shell_test_external_view_embedder,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
return nullptr;
}
#endif // SHELL_ENABLE_VULKAN

} // namespace flutter::testing
35 changes: 31 additions & 4 deletions shell/common/shell_test_platform_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include "flutter/shell/common/shell_test_external_view_embedder.h"
#include "flutter/shell/common/vsync_waiters_test.h"

namespace flutter {
namespace testing {
namespace flutter::testing {

class ShellTestPlatformView : public PlatformView {
public:
Expand Down Expand Up @@ -39,6 +38,35 @@ class ShellTestPlatformView : public PlatformView {
const TaskRunners& task_runners)
: PlatformView(delegate, task_runners) {}

private:
static std::unique_ptr<ShellTestPlatformView> CreatePlatformViewGL(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
const CreateVsyncWaiter& create_vsync_waiter,
const std::shared_ptr<ShellTestExternalViewEmbedder>&
shell_test_external_view_embedder,
const std::shared_ptr<const fml::SyncSwitch>&
is_gpu_disabled_sync_switch);
static std::unique_ptr<ShellTestPlatformView> CreatePlatformViewMetal(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
const CreateVsyncWaiter& create_vsync_waiter,
const std::shared_ptr<ShellTestExternalViewEmbedder>&
shell_test_external_view_embedder,
const std::shared_ptr<const fml::SyncSwitch>&
is_gpu_disabled_sync_switch);
static std::unique_ptr<ShellTestPlatformView> CreatePlatformViewVulkan(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
const CreateVsyncWaiter& create_vsync_waiter,
const std::shared_ptr<ShellTestExternalViewEmbedder>&
shell_test_external_view_embedder,
const std::shared_ptr<const fml::SyncSwitch>&
is_gpu_disabled_sync_switch);

FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformView);
};

Expand All @@ -63,7 +91,6 @@ class ShellTestPlatformViewBuilder {
Config config_;
};

} // namespace testing
} // namespace flutter
} // namespace flutter::testing

#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_H_
25 changes: 20 additions & 5 deletions shell/common/shell_test_platform_view_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@

#include <EGL/egl.h>

#include "flutter/shell/common/shell_test_platform_view_gl.h"
#include "flutter/shell/gpu/gpu_surface_gl_skia.h"
#include "impeller/entity/gles/entity_shaders_gles.h"

namespace flutter {
namespace testing {
namespace flutter::testing {

static std::vector<std::shared_ptr<fml::Mapping>> ShaderLibraryMappings() {
namespace {
std::vector<std::shared_ptr<fml::Mapping>> ShaderLibraryMappings() {
return {
std::make_shared<fml::NonOwnedMapping>(
impeller_entity_shaders_gles_data,
impeller_entity_shaders_gles_length),
};
}
} // namespace

std::unique_ptr<ShellTestPlatformView>
ShellTestPlatformView::CreatePlatformViewGL(
PlatformView::Delegate& delegate,
const TaskRunners& task_runners,
const std::shared_ptr<ShellTestVsyncClock>& vsync_clock,
const CreateVsyncWaiter& create_vsync_waiter,
const std::shared_ptr<ShellTestExternalViewEmbedder>&
shell_test_external_view_embedder,
const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch) {
return std::make_unique<ShellTestPlatformViewGL>(
delegate, task_runners, vsync_clock, create_vsync_waiter,
shell_test_external_view_embedder);
}

ShellTestPlatformViewGL::ShellTestPlatformViewGL(
PlatformView::Delegate& delegate,
Expand Down Expand Up @@ -113,5 +129,4 @@ ShellTestPlatformViewGL::GetGLProcResolver() const {
};
}

} // namespace testing
} // namespace flutter
} // namespace flutter::testing
6 changes: 2 additions & 4 deletions shell/common/shell_test_platform_view_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
#include "flutter/testing/test_gl_surface.h"
#include "impeller/renderer/backend/gles/context_gles.h"

namespace flutter {
namespace testing {
namespace flutter::testing {

class ShellTestPlatformViewGL : public ShellTestPlatformView,
public GPUSurfaceGLDelegate {
Expand Down Expand Up @@ -77,7 +76,6 @@ class ShellTestPlatformViewGL : public ShellTestPlatformView,
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformViewGL);
};

} // namespace testing
} // namespace flutter
} // namespace flutter::testing

#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_GL_H_
19 changes: 11 additions & 8 deletions shell/common/shell_test_platform_view_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#ifndef FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_METAL_H_
#define FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_METAL_H_

#include "flutter/fml/macros.h"
#include "flutter/shell/common/shell_test_platform_view.h"
#include "flutter/shell/gpu/gpu_surface_metal_delegate.h"

namespace flutter {
namespace testing {
#import <Metal/Metal.h>

#include "flutter/fml/macros.h"
#include "flutter/shell/gpu/gpu_surface_metal_delegate.h"
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.h"
#include "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalSkia.h"

struct DarwinContextMetal;
namespace flutter::testing {

class ShellTestPlatformViewMetal final : public ShellTestPlatformView,
public GPUSurfaceMetalDelegate {
Expand All @@ -30,7 +32,9 @@ class ShellTestPlatformViewMetal final : public ShellTestPlatformView,
virtual ~ShellTestPlatformViewMetal() override;

private:
const std::unique_ptr<DarwinContextMetal> metal_context_;
FlutterDarwinContextMetalSkia* skia_context_;
FlutterDarwinContextMetalImpeller* impeller_context_;
id<MTLTexture> offscreen_texture_;
const CreateVsyncWaiter create_vsync_waiter_;
const std::shared_ptr<ShellTestVsyncClock> vsync_clock_;
const std::shared_ptr<ShellTestExternalViewEmbedder>
Expand Down Expand Up @@ -70,7 +74,6 @@ class ShellTestPlatformViewMetal final : public ShellTestPlatformView,
FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformViewMetal);
};

} // namespace testing
} // namespace flutter
} // namespace flutter::testing

#endif // FLUTTER_SHELL_COMMON_SHELL_TEST_PLATFORM_VIEW_METAL_H_
Loading

0 comments on commit 34b7c2d

Please sign in to comment.