Adds metal background to engine dart tests (#180700)

Dart tests now also run against the metal backend on platforms where
that is supported.

test exempt: it is a test

issue: https://github.com/flutter/flutter/issues/180601

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
gaaclarke 2026-01-13 08:09:24 -08:00 committed by GitHub
parent fa41ee32cb
commit 141886358e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 182 additions and 12 deletions

View File

@ -13,7 +13,7 @@ shell_gpu_configuration("tester_gpu_configuration") {
enable_software = true
enable_gl = true
enable_vulkan = true
enable_metal = false
enable_metal = shell_enable_metal
}
executable("testing") {
@ -26,6 +26,7 @@ executable("testing") {
sources = [
"tester_context.h",
"tester_context_mtl_factory.h",
"tester_context_vk_factory.h",
"tester_main.cc",
]
@ -61,6 +62,9 @@ executable("testing") {
"//flutter/third_party/swiftshader/src/Vulkan:swiftshader_libvulkan_static",
]
sources += [ "tester_context_vk_factory.cc" ]
if (shell_enable_metal) {
sources += [ "tester_context_mtl_factory.mm" ]
}
}
metadata = {

View File

@ -0,0 +1,24 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_TESTING_TESTER_CONTEXT_MTL_FACTORY_H_
#define FLUTTER_SHELL_TESTING_TESTER_CONTEXT_MTL_FACTORY_H_
#include <memory>
#include "flutter/shell/testing/tester_context.h"
// Impeller should only be enabled if the Metal backend is enabled.
#define TESTER_ENABLE_METAL \
(IMPELLER_SUPPORTS_RENDERING && IMPELLER_ENABLE_METAL)
namespace flutter {
class TesterContextMTLFactory {
public:
static std::unique_ptr<TesterContext> Create();
};
} // namespace flutter
#endif // FLUTTER_SHELL_TESTING_TESTER_CONTEXT_MTL_FACTORY_H_

View File

@ -0,0 +1,133 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#define FML_USED_ON_EMBEDDER
#include "flutter/shell/testing/tester_context_mtl_factory.h"
#include <Foundation/Foundation.h>
#include <QuartzCore/QuartzCore.h>
#include <deque>
#include <optional>
#include <type_traits>
#include <vector>
#include "flutter/fml/mapping.h"
#include "flutter/fml/synchronization/sync_switch.h"
#include "flutter/shell/gpu/gpu_surface_metal_impeller.h"
#include "impeller/base/validation.h"
#include "impeller/display_list/aiks_context.h"
#include "impeller/entity/mtl/entity_shaders.h"
#include "impeller/entity/mtl/framebuffer_blend_shaders.h"
#include "impeller/entity/mtl/modern_shaders.h"
#include "impeller/renderer/backend/metal/context_mtl.h"
#include "impeller/typographer/backends/skia/typographer_context_skia.h"
#include "impeller/typographer/typographer_context.h"
namespace flutter {
namespace {
class TesterGPUSurfaceMetalDelegate : public GPUSurfaceMetalDelegate {
public:
explicit TesterGPUSurfaceMetalDelegate(id<MTLDevice> device)
: GPUSurfaceMetalDelegate(MTLRenderTargetType::kCAMetalLayer) {
layer_ = [[CAMetalLayer alloc] init];
layer_.device = device;
layer_.pixelFormat = MTLPixelFormatBGRA8Unorm;
}
~TesterGPUSurfaceMetalDelegate() = default;
GPUCAMetalLayerHandle GetCAMetalLayer(const DlISize& frame_info) const override {
layer_.drawableSize = CGSizeMake(frame_info.width, frame_info.height);
return (__bridge GPUCAMetalLayerHandle)(layer_);
}
bool PresentDrawable(GrMTLHandle drawable) const override { return true; }
GPUMTLTextureInfo GetMTLTexture(const DlISize& frame_info) const override { return {}; }
bool PresentTexture(GPUMTLTextureInfo texture) const override { return true; }
bool AllowsDrawingWhenGpuDisabled() const override { return true; }
private:
CAMetalLayer* layer_ = nil;
};
std::vector<std::shared_ptr<fml::Mapping>> ShaderLibraryMappings() {
return {
std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_data,
impeller_entity_shaders_length),
std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_data,
impeller_modern_shaders_length),
std::make_shared<fml::NonOwnedMapping>(impeller_framebuffer_blend_shaders_data,
impeller_framebuffer_blend_shaders_length),
};
}
} // namespace
class TesterContextMTL : public TesterContext {
public:
TesterContextMTL() = default;
~TesterContextMTL() override {
if (context_) {
context_->Shutdown();
}
}
bool Initialize() {
std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = ShaderLibraryMappings();
auto sync_switch = std::make_shared<fml::SyncSwitch>(false);
context_ = impeller::ContextMTL::Create(impeller::Flags{}, shader_mappings, sync_switch,
"Impeller Library");
if (!context_ || !context_->IsValid()) {
VALIDATION_LOG << "Could not create Metal context.";
return false;
}
auto device = context_->GetMTLDevice();
if (!device) {
VALIDATION_LOG << "Could not get Metal device.";
return false;
}
delegate_ = std::make_unique<TesterGPUSurfaceMetalDelegate>(device);
aiks_context_ = std::make_shared<impeller::AiksContext>(
context_, /*typographer_context=*/impeller::TypographerContextSkia::Make());
return true;
}
// |TesterContext|
std::shared_ptr<impeller::Context> GetImpellerContext() const override { return context_; }
// |TesterContext|
std::unique_ptr<Surface> CreateRenderingSurface() override {
auto surface = std::make_unique<GPUSurfaceMetalImpeller>(delegate_.get(), aiks_context_);
if (!surface->IsValid()) {
return nullptr;
}
return surface;
}
private:
std::shared_ptr<impeller::ContextMTL> context_;
std::unique_ptr<TesterGPUSurfaceMetalDelegate> delegate_;
std::shared_ptr<impeller::AiksContext> aiks_context_;
};
std::unique_ptr<TesterContext> TesterContextMTLFactory::Create() {
auto context = std::make_unique<TesterContextMTL>();
if (!context->Initialize()) {
return nullptr;
}
return context;
}
} // namespace flutter

View File

@ -31,6 +31,7 @@
#include "third_party/skia/include/core/SkSurface.h"
#include "flutter/shell/testing/tester_context.h"
#include "flutter/shell/testing/tester_context_mtl_factory.h"
#include "flutter/shell/testing/tester_context_vk_factory.h"
#if defined(FML_OS_WIN)
@ -48,8 +49,14 @@ static absl::NoDestructor<std::unique_ptr<Shell>> g_shell;
namespace {
std::unique_ptr<TesterContext> CreateTesterContext(const Settings& settings) {
std::unique_ptr<TesterContext> tester_context;
#if TESTER_ENABLE_METAL
if (settings.enable_impeller &&
settings.requested_rendering_backend == "metal") {
tester_context = TesterContextMTLFactory::Create();
}
#endif
#if TESTER_ENABLE_VULKAN
if (settings.enable_impeller) {
if (settings.enable_impeller && !tester_context) {
tester_context =
TesterContextVKFactory::Create(settings.enable_vulkan_validation);
}

View File

@ -651,13 +651,13 @@ class FlutterTesterOptions():
def __init__( # pylint: disable=too-many-arguments
self,
multithreaded: bool = False,
enable_impeller: bool = False,
impeller_backend: str = '',
enable_vm_service: bool = False,
enable_microtask_profiling: bool = False,
expect_failure: bool = False
) -> None:
self.multithreaded = multithreaded
self.enable_impeller = enable_impeller
self.impeller_backend = impeller_backend
self.enable_vm_service = enable_vm_service
self.enable_microtask_profiling = enable_microtask_profiling
self.expect_failure = expect_failure
@ -666,8 +666,10 @@ class FlutterTesterOptions():
if not self.enable_vm_service:
command_args.append('--disable-vm-service')
if self.enable_impeller:
command_args += ['--enable-impeller', '--enable-flutter-gpu']
if self.impeller_backend != '':
command_args += [
'--enable-impeller', '--enable-flutter-gpu', f'--impeller-backend={self.impeller_backend}'
]
else:
command_args += ['--no-enable-impeller']
@ -683,8 +685,8 @@ class FlutterTesterOptions():
return 'single-threaded'
def impeller_enabled(self) -> str:
if self.enable_impeller:
return 'impeller swiftshader'
if self.impeller_backend != '':
return f'impeller {self.impeller_backend}'
return 'skia software'
@ -935,12 +937,12 @@ def gather_dart_tests(
else:
_logger.info("Gathering dart test '%s' with VM service enabled", dart_test_file)
for multithreaded in [False, True]:
for enable_impeller in [False, True]:
for impeller in ['', 'vulkan', 'metal']:
yield gather_dart_test(
build_dir, dart_test_file,
FlutterTesterOptions(
multithreaded=multithreaded,
enable_impeller=enable_impeller,
impeller_backend=impeller,
enable_vm_service=True,
enable_microtask_profiling=dart_test_basename ==
'microtask_profiling_test.dart',
@ -953,10 +955,10 @@ def gather_dart_tests(
else:
_logger.info("Gathering dart test '%s'", dart_test_file)
for multithreaded in [False, True]:
for enable_impeller in [False, True]:
for impeller in ['', 'vulkan', 'metal']:
yield gather_dart_test(
build_dir, dart_test_file,
FlutterTesterOptions(multithreaded=multithreaded, enable_impeller=enable_impeller)
FlutterTesterOptions(multithreaded=multithreaded, impeller_backend=impeller)
)