diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 177517471d..463bcba2f2 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -1898,6 +1898,8 @@ FILE: ../../../flutter/shell/platform/embedder/embedder_surface.cc FILE: ../../../flutter/shell/platform/embedder/embedder_surface.h FILE: ../../../flutter/shell/platform/embedder/embedder_surface_gl.cc FILE: ../../../flutter/shell/platform/embedder/embedder_surface_gl.h +FILE: ../../../flutter/shell/platform/embedder/embedder_surface_metal.h +FILE: ../../../flutter/shell/platform/embedder/embedder_surface_metal.mm FILE: ../../../flutter/shell/platform/embedder/embedder_surface_software.cc FILE: ../../../flutter/shell/platform/embedder/embedder_surface_software.h FILE: ../../../flutter/shell/platform/embedder/embedder_task_runner.cc @@ -1913,6 +1915,7 @@ FILE: ../../../flutter/shell/platform/embedder/fixtures/compositor_with_root_lay FILE: ../../../flutter/shell/platform/embedder/fixtures/dpr_noxform.png FILE: ../../../flutter/shell/platform/embedder/fixtures/dpr_xform.png FILE: ../../../flutter/shell/platform/embedder/fixtures/gradient.png +FILE: ../../../flutter/shell/platform/embedder/fixtures/gradient_metal.png FILE: ../../../flutter/shell/platform/embedder/fixtures/gradient_xform.png FILE: ../../../flutter/shell/platform/embedder/fixtures/main.dart FILE: ../../../flutter/shell/platform/embedder/fixtures/scene_without_custom_compositor.png diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal.mm b/engine/src/flutter/shell/gpu/gpu_surface_metal.mm index 0a1922e793..96ec00a325 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_metal.mm +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal.mm @@ -125,7 +125,7 @@ std::unique_ptr GPUSurfaceMetal::AcquireFrameFromMTLTexture( return nullptr; } - auto submit_callback = [texture_id = texture.texture_id, delegate = delegate_]( + auto submit_callback = [texture = texture, delegate = delegate_]( const SurfaceFrame& surface_frame, SkCanvas* canvas) -> bool { TRACE_EVENT0("flutter", "GPUSurfaceMetal::PresentTexture"); if (canvas == nullptr) { @@ -135,7 +135,7 @@ std::unique_ptr GPUSurfaceMetal::AcquireFrameFromMTLTexture( canvas->flush(); - return delegate->PresentTexture(texture_id); + return delegate->PresentTexture(texture); }; return std::make_unique(std::move(surface), true, submit_callback); diff --git a/engine/src/flutter/shell/gpu/gpu_surface_metal_delegate.h b/engine/src/flutter/shell/gpu/gpu_surface_metal_delegate.h index aa88ecfda4..4185e409e4 100644 --- a/engine/src/flutter/shell/gpu/gpu_surface_metal_delegate.h +++ b/engine/src/flutter/shell/gpu/gpu_surface_metal_delegate.h @@ -24,10 +24,10 @@ typedef void* GPUMTLCommandQueueHandle; typedef void* GPUCAMetalLayerHandle; // expected to be id -typedef void* GPUMTLTextureHandle; +typedef const void* GPUMTLTextureHandle; struct GPUMTLTextureInfo { - intptr_t texture_id; + int64_t texture_id; GPUMTLTextureHandle texture; }; @@ -87,7 +87,7 @@ class GPUSurfaceMetalDelegate { /// /// @see |GPUSurfaceMetalDelegate::GetMTLTexture| /// - virtual bool PresentTexture(intptr_t texture_id) const = 0; + virtual bool PresentTexture(GPUMTLTextureInfo texture) const = 0; MTLRenderTargetType GetRenderTargetType(); diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.h b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.h index 2ebcfee383..c2e9b8c50d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.h @@ -47,7 +47,7 @@ class SK_API_AVAILABLE_CA_METAL_LAYER IOSSurfaceMetal final : public IOSSurface, GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override; // |GPUSurfaceMetalDelegate| - bool PresentTexture(intptr_t texture_id) const override; + bool PresentTexture(GPUMTLTextureInfo texture) const override; FML_DISALLOW_COPY_AND_ASSIGN(IOSSurfaceMetal); }; diff --git a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.mm b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.mm index 176f50313b..35304938d1 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/ios_surface_metal.mm @@ -93,7 +93,7 @@ GPUMTLTextureInfo IOSSurfaceMetal::GetMTLTexture(const SkISize& frame_info) cons } // |GPUSurfaceMetalDelegate| -bool IOSSurfaceMetal::PresentTexture(intptr_t texture_id) const { +bool IOSSurfaceMetal::PresentTexture(GPUMTLTextureInfo texture) const { FML_CHECK(false) << "render to texture not supported on ios"; return false; } diff --git a/engine/src/flutter/shell/platform/embedder/BUILD.gn b/engine/src/flutter/shell/platform/embedder/BUILD.gn index 7868f0c144..7cbba62bbc 100644 --- a/engine/src/flutter/shell/platform/embedder/BUILD.gn +++ b/engine/src/flutter/shell/platform/embedder/BUILD.gn @@ -88,6 +88,18 @@ template("embedder_source_set") { "//third_party/skia", ] + if (embedder_enable_metal) { + sources += [ + "embedder_surface_metal.h", + "embedder_surface_metal.mm", + ] + + cflags_objc = flutter_cflags_objc + cflags_objcc = flutter_cflags_objcc + + deps += [ "//flutter/shell/platform/darwin/graphics" ] + } + public_deps = [ ":embedder_headers" ] public_configs += [ @@ -143,6 +155,7 @@ test_fixtures("fixtures") { "fixtures/dpr_noxform.png", "fixtures/dpr_xform.png", "fixtures/gradient.png", + "fixtures/gradient_metal.png", "fixtures/gradient_xform.png", "fixtures/scene_without_custom_compositor.png", "fixtures/scene_without_custom_compositor_with_xform.png", @@ -208,6 +221,16 @@ if (enable_unittests) { deps += [ "//flutter/testing:opengl" ] } + + if (test_enable_metal) { + sources += [ + "tests/embedder_test_context_metal.cc", + "tests/embedder_test_context_metal.h", + "tests/embedder_unittests_metal.cc", + ] + + deps += [ "//flutter/testing:metal" ] + } } # Tests the build in FLUTTER_ENGINE_NO_PROTOTYPES mode. diff --git a/engine/src/flutter/shell/platform/embedder/embedder.cc b/engine/src/flutter/shell/platform/embedder/embedder.cc index 1dc3ea6b44..b2b9f26a53 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder.cc @@ -61,6 +61,10 @@ extern const intptr_t kPlatformStrongDillSize; #include "flutter/shell/platform/embedder/embedder_external_texture_gl.h" #endif +#ifdef SHELL_ENABLE_METAL +#include "flutter/shell/platform/embedder/embedder_surface_metal.h" +#endif + const int32_t kFlutterSemanticsNodeIdBatchEnd = -1; const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1; @@ -121,6 +125,24 @@ static bool IsSoftwareRendererConfigValid(const FlutterRendererConfig* config) { return true; } +static bool IsMetalRendererConfigValid(const FlutterRendererConfig* config) { + if (config->type != kMetal) { + return false; + } + + const FlutterMetalRendererConfig* metal_config = &config->metal; + + bool device = SAFE_ACCESS(metal_config, device, nullptr); + bool command_queue = + SAFE_ACCESS(metal_config, present_command_queue, nullptr); + + bool present = SAFE_ACCESS(metal_config, present_drawable_callback, nullptr); + bool get_texture = + SAFE_ACCESS(metal_config, get_next_drawable_callback, nullptr); + + return device && command_queue && present && get_texture; +} + static bool IsRendererValid(const FlutterRendererConfig* config) { if (config == nullptr) { return false; @@ -131,6 +153,8 @@ static bool IsRendererValid(const FlutterRendererConfig* config) { return IsOpenGLRendererConfigValid(config); case kSoftware: return IsSoftwareRendererConfigValid(config); + case kMetal: + return IsMetalRendererConfigValid(config); default: return false; } @@ -275,6 +299,74 @@ InferOpenGLPlatformViewCreationCallback( #endif } +static flutter::Shell::CreateCallback +InferMetalPlatformViewCreationCallback( + const FlutterRendererConfig* config, + void* user_data, + flutter::PlatformViewEmbedder::PlatformDispatchTable + platform_dispatch_table, + std::unique_ptr + external_view_embedder) { + if (config->type != kMetal) { + return nullptr; + } + +#ifdef SHELL_ENABLE_METAL + std::function metal_present = + [ptr = config->metal.present_drawable_callback, + user_data](flutter::GPUMTLTextureInfo texture) { + FlutterMetalTexture embedder_texture; + embedder_texture.struct_size = sizeof(FlutterMetalTexture); + embedder_texture.texture = texture.texture; + embedder_texture.texture_id = texture.texture_id; + return ptr(user_data, &embedder_texture); + }; + auto metal_get_texture = + [ptr = config->metal.get_next_drawable_callback, + user_data](const SkISize& frame_size) -> flutter::GPUMTLTextureInfo { + FlutterFrameInfo frame_info = {}; + frame_info.struct_size = sizeof(FlutterFrameInfo); + frame_info.size = {static_cast(frame_size.width()), + static_cast(frame_size.height())}; + flutter::GPUMTLTextureInfo texture_info; + + FlutterMetalTexture metal_texture = ptr(user_data, &frame_info); + texture_info.texture_id = metal_texture.texture_id; + texture_info.texture = metal_texture.texture; + return texture_info; + }; + + flutter::EmbedderSurfaceMetal::MetalDispatchTable metal_dispatch_table = { + .present = metal_present, + .get_texture = metal_get_texture, + }; + + std::shared_ptr view_embedder = + std::move(external_view_embedder); + + std::unique_ptr embedder_surface = + std::make_unique( + const_cast(config->metal.device), + const_cast( + config->metal.present_command_queue), + metal_dispatch_table, view_embedder); + + return fml::MakeCopyable( + [embedder_surface = std::move(embedder_surface), platform_dispatch_table, + external_view_embedder = view_embedder](flutter::Shell& shell) mutable { + return std::make_unique( + shell, // delegate + shell.GetTaskRunners(), // task runners + std::move(embedder_surface), // embedder surface + platform_dispatch_table, // platform dispatch table + std::move(external_view_embedder) // external view embedder + ); + }); +#else + return nullptr; +#endif +} + static flutter::Shell::CreateCallback InferSoftwarePlatformViewCreationCallback( const FlutterRendererConfig* config, @@ -333,6 +425,10 @@ InferPlatformViewCreationCallback( return InferSoftwarePlatformViewCreationCallback( config, user_data, platform_dispatch_table, std::move(external_view_embedder)); + case kMetal: + return InferMetalPlatformViewCreationCallback( + config, user_data, platform_dispatch_table, + std::move(external_view_embedder)); default: return nullptr; } diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index 4819aa87fa..7122869630 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -72,6 +72,10 @@ typedef enum { typedef enum { kOpenGL, kSoftware, + /// Metal is only supported on Darwin platforms (macOS / iOS). + /// iOS version >= 10.0 (device), 13.0 (simulator) + /// macOS version >= 10.14 + kMetal, } FlutterRendererType; /// Additional accessibility features that may be enabled by the platform. @@ -434,6 +438,55 @@ typedef struct { BoolPresentInfoCallback present_with_info; } FlutterOpenGLRendererConfig; +/// Alias for id. +typedef const void* FlutterMetalDeviceHandle; + +/// Alias for id. +typedef const void* FlutterMetalCommandQueueHandle; + +/// Alias for id. +typedef const void* FlutterMetalTextureHandle; + +typedef struct { + /// The size of this struct. Must be sizeof(FlutterMetalTexture). + size_t struct_size; + /// Embedder provided unique identifier to the texture buffer. Given that the + /// `texture` handle is passed to the engine to render to, the texture buffer + /// is itseld owned by the embedder. This `texture_id` is then also given to + /// the embedder in the present callback. + int64_t texture_id; + /// Handle to the MTLTexture that is owned by the embedder. Engine will render + /// the frame into this texture. + FlutterMetalTextureHandle texture; +} FlutterMetalTexture; + +/// Callback for when a metal texture is requested. +typedef FlutterMetalTexture (*FlutterMetalTextureCallback)( + void* /* user data */, + const FlutterFrameInfo* /* frame info */); + +/// Callback for when a metal texture is presented. The texture_id here +/// corresponds to the texture_id provided by the embedder in the +/// `FlutterMetalTextureCallback` callback. +typedef bool (*FlutterMetalPresentCallback)( + void* /* user data */, + const FlutterMetalTexture* /* texture */); + +typedef struct { + /// The size of this struct. Must be sizeof(FlutterMetalRendererConfig). + size_t struct_size; + /// Alias for id. + FlutterMetalDeviceHandle device; + /// Alias for id. + FlutterMetalCommandQueueHandle present_command_queue; + /// The callback that gets invoked when the engine requests the embedder for a + /// texture to render to. + FlutterMetalTextureCallback get_next_drawable_callback; + /// The callback presented to the embedder to present a fully populated metal + /// texture to the user. + FlutterMetalPresentCallback present_drawable_callback; +} FlutterMetalRendererConfig; + typedef struct { /// The size of this struct. Must be sizeof(FlutterSoftwareRendererConfig). size_t struct_size; @@ -449,6 +502,7 @@ typedef struct { union { FlutterOpenGLRendererConfig open_gl; FlutterSoftwareRendererConfig software; + FlutterMetalRendererConfig metal; }; } FlutterRendererConfig; diff --git a/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.h b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.h new file mode 100644 index 0000000000..ba0d28130f --- /dev/null +++ b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.h @@ -0,0 +1,68 @@ +// 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_PLATFORM_EMBEDDER_EMBEDDER_SURFACE_METAL_H_ +#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SURFACE_METAL_H_ + +#include "flutter/fml/macros.h" +#include "flutter/shell/gpu/gpu_surface_metal.h" +#include "flutter/shell/gpu/gpu_surface_metal_delegate.h" +#include "flutter/shell/platform/embedder/embedder_external_view_embedder.h" +#include "flutter/shell/platform/embedder/embedder_surface.h" + +namespace flutter { + +class EmbedderSurfaceMetal final : public EmbedderSurface, + public GPUSurfaceMetalDelegate { + public: + struct MetalDispatchTable { + std::function present; // required + std::function + get_texture; // required + }; + + EmbedderSurfaceMetal( + GPUMTLDeviceHandle device, + GPUMTLCommandQueueHandle command_queue, + MetalDispatchTable dispatch_table, + std::shared_ptr external_view_embedder); + + ~EmbedderSurfaceMetal() override; + + private: + bool valid_ = false; + MetalDispatchTable metal_dispatch_table_; + std::shared_ptr external_view_embedder_; + sk_sp surface_; + sk_sp main_context_; + sk_sp resource_context_; + + // |EmbedderSurface| + bool IsValid() const override; + + // |EmbedderSurface| + std::unique_ptr CreateGPUSurface() override; + + // |EmbedderSurface| + sk_sp CreateResourceContext() const override; + + // |GPUSurfaceMetalDelegate| + GPUCAMetalLayerHandle GetCAMetalLayer( + const SkISize& frame_size) const override; + + // |GPUSurfaceMetalDelegate| + bool PresentDrawable(GrMTLHandle drawable) const override; + + // |GPUSurfaceMetalDelegate| + GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_size) const override; + + // |GPUSurfaceMetalDelegate| + bool PresentTexture(GPUMTLTextureInfo texture) const override; + + FML_DISALLOW_COPY_AND_ASSIGN(EmbedderSurfaceMetal); +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SURFACE_METAL_H_ diff --git a/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.mm b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.mm new file mode 100644 index 0000000000..bce856164d --- /dev/null +++ b/engine/src/flutter/shell/platform/embedder/embedder_surface_metal.mm @@ -0,0 +1,74 @@ +// 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. + +#include "flutter/shell/platform/embedder/embedder_surface_metal.h" + +#include "flutter/fml/logging.h" +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "flutter/shell/gpu/gpu_surface_metal_delegate.h" +#import "flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetal.h" +#include "third_party/skia/include/gpu/GrDirectContext.h" + +namespace flutter { + +EmbedderSurfaceMetal::EmbedderSurfaceMetal( + GPUMTLDeviceHandle device, + GPUMTLCommandQueueHandle command_queue, + MetalDispatchTable metal_dispatch_table, + std::shared_ptr external_view_embedder) + : GPUSurfaceMetalDelegate(MTLRenderTargetType::kMTLTexture), + metal_dispatch_table_(metal_dispatch_table), + external_view_embedder_(external_view_embedder) { + auto darwin_metal_context = + fml::scoped_nsobject{[[[FlutterDarwinContextMetal alloc] + initWithMTLDevice:(id)device + commandQueue:(id)command_queue] retain]}; + main_context_ = darwin_metal_context.get().mainContext; + resource_context_ = darwin_metal_context.get().resourceContext; + valid_ = main_context_ && resource_context_; +} + +EmbedderSurfaceMetal::~EmbedderSurfaceMetal() = default; + +bool EmbedderSurfaceMetal::IsValid() const { + return valid_; +} + +std::unique_ptr EmbedderSurfaceMetal::CreateGPUSurface() { + if (!IsValid()) { + return nullptr; + } + + auto surface = std::make_unique(this, main_context_); + + if (!surface->IsValid()) { + return nullptr; + } + + return surface; +} + +sk_sp EmbedderSurfaceMetal::CreateResourceContext() const { + return resource_context_; +} + +GPUCAMetalLayerHandle EmbedderSurfaceMetal::GetCAMetalLayer(const SkISize& frame_info) const { + FML_CHECK(false) << "Only rendering to MTLTexture is supported."; + return nullptr; +} + +bool EmbedderSurfaceMetal::PresentDrawable(GrMTLHandle drawable) const { + FML_CHECK(false) << "Only rendering to MTLTexture is supported."; + return false; +} + +GPUMTLTextureInfo EmbedderSurfaceMetal::GetMTLTexture(const SkISize& frame_info) const { + return metal_dispatch_table_.get_texture(frame_info); +} + +bool EmbedderSurfaceMetal::PresentTexture(GPUMTLTextureInfo texture) const { + return metal_dispatch_table_.present(texture); +} + +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/embedder/fixtures/gradient_metal.png b/engine/src/flutter/shell/platform/embedder/fixtures/gradient_metal.png new file mode 100644 index 0000000000..618a31cbfa Binary files /dev/null and b/engine/src/flutter/shell/platform/embedder/fixtures/gradient_metal.png differ diff --git a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc index d5cbb54d67..0e566f3782 100644 --- a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.cc @@ -36,6 +36,19 @@ PlatformViewEmbedder::PlatformViewEmbedder( platform_dispatch_table_(platform_dispatch_table) {} #endif +#ifdef SHELL_ENABLE_METAL +PlatformViewEmbedder::PlatformViewEmbedder( + PlatformView::Delegate& delegate, + flutter::TaskRunners task_runners, + std::unique_ptr embedder_surface, + PlatformDispatchTable platform_dispatch_table, + std::shared_ptr external_view_embedder) + : PlatformView(delegate, std::move(task_runners)), + external_view_embedder_(external_view_embedder), + embedder_surface_(std::move(embedder_surface)), + platform_dispatch_table_(platform_dispatch_table) {} +#endif + PlatformViewEmbedder::~PlatformViewEmbedder() = default; void PlatformViewEmbedder::UpdateSemantics( diff --git a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h index 76fc26a99b..e3d89c5278 100644 --- a/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h +++ b/engine/src/flutter/shell/platform/embedder/platform_view_embedder.h @@ -19,6 +19,10 @@ #include "flutter/shell/platform/embedder/embedder_surface_gl.h" #endif +#ifdef SHELL_ENABLE_METAL +#include "flutter/shell/platform/embedder/embedder_surface_metal.h" +#endif + namespace flutter { class PlatformViewEmbedder final : public PlatformView { @@ -63,6 +67,16 @@ class PlatformViewEmbedder final : public PlatformView { std::shared_ptr external_view_embedder); #endif +#ifdef SHELL_ENABLE_METAL + // Creates a platform view that sets up an metal rasterizer. + PlatformViewEmbedder( + PlatformView::Delegate& delegate, + flutter::TaskRunners task_runners, + std::unique_ptr embedder_surface, + PlatformDispatchTable platform_dispatch_table, + std::shared_ptr external_view_embedder); +#endif + ~PlatformViewEmbedder() override; // |PlatformView| diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_a11y_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_a11y_unittests.cc index b1d27f8a07..bf5b8b6d04 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_a11y_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_a11y_unittests.cc @@ -23,7 +23,7 @@ using Embedder11yTest = testing::EmbedderTest; // TODO(52372): De-flake and re-enable. TEST_F(Embedder11yTest, DISABLED_A11yTreeIsConsistent) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); fml::AutoResetWaitableEvent latch; diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc index 3b8961cd8d..05cda13023 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.cc @@ -13,6 +13,10 @@ #include "flutter/shell/platform/embedder/tests/embedder_test_context_gl.h" #endif +#ifdef SHELL_ENABLE_METAL +#include "flutter/shell/platform/embedder/tests/embedder_test_context_metal.h" +#endif + namespace flutter { namespace testing { @@ -65,6 +69,10 @@ EmbedderConfigBuilder::EmbedderConfigBuilder( }; #endif +#ifdef SHELL_ENABLE_METAL + InitializeMetalRendererConfig(); +#endif + software_renderer_config_.struct_size = sizeof(FlutterSoftwareRendererConfig); software_renderer_config_.surface_present_callback = [](void* context, const void* allocation, size_t row_bytes, @@ -153,6 +161,14 @@ void EmbedderConfigBuilder::SetOpenGLRendererConfig(SkISize surface_size) { #endif } +void EmbedderConfigBuilder::SetMetalRendererConfig(SkISize surface_size) { +#ifdef SHELL_ENABLE_METAL + renderer_config_.type = FlutterRendererType::kMetal; + renderer_config_.metal = metal_renderer_config_; + context_.SetupSurface(surface_size); +#endif +} + void EmbedderConfigBuilder::SetAssetsPath() { project_args_.assets_path = context_.GetAssetsPath().c_str(); } @@ -356,5 +372,46 @@ UniqueEngine EmbedderConfigBuilder::SetupEngine(bool run) const { return UniqueEngine{engine}; } +#ifdef SHELL_ENABLE_METAL + +void EmbedderConfigBuilder::InitializeMetalRendererConfig() { + if (context_.GetContextType() != EmbedderTestContextType::kMetalContext) { + return; + } + + metal_renderer_config_.struct_size = sizeof(metal_renderer_config_); + EmbedderTestContextMetal& metal_context = + reinterpret_cast(context_); + + metal_renderer_config_.device = + metal_context.GetTestMetalContext()->GetMetalDevice(); + metal_renderer_config_.present_command_queue = + metal_context.GetTestMetalContext()->GetMetalCommandQueue(); + metal_renderer_config_.get_next_drawable_callback = + [](void* user_data, const FlutterFrameInfo* frame_info) { + EmbedderTestContextMetal* metal_context = + reinterpret_cast(user_data); + SkISize surface_size = + SkISize::Make(frame_info->size.width, frame_info->size.height); + TestMetalContext::TextureInfo texture_info = + metal_context->GetTestMetalContext()->CreateMetalTexture( + surface_size); + FlutterMetalTexture texture; + texture.struct_size = sizeof(FlutterMetalTexture); + texture.texture_id = texture_info.texture_id; + texture.texture = + reinterpret_cast(texture_info.texture); + return texture; + }; + metal_renderer_config_.present_drawable_callback = + [](void* user_data, const FlutterMetalTexture* texture) -> bool { + EmbedderTestContextMetal* metal_context = + reinterpret_cast(user_data); + return metal_context->Present(texture->texture_id); + }; +} + +#endif // SHELL_ENABLE_METAL + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h index 808719def6..b013002acf 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h @@ -48,6 +48,8 @@ class EmbedderConfigBuilder { void SetOpenGLRendererConfig(SkISize surface_size); + void SetMetalRendererConfig(SkISize surface_size); + // Used to explicitly set an `open_gl.fbo_callback`. Using this method will // cause your test to fail since the ctor for this class sets // `open_gl.fbo_callback_with_frame_info`. This method exists as a utility to @@ -103,6 +105,10 @@ class EmbedderConfigBuilder { FlutterSoftwareRendererConfig software_renderer_config_ = {}; #ifdef SHELL_ENABLE_GL FlutterOpenGLRendererConfig opengl_renderer_config_ = {}; +#endif +#ifdef SHELL_ENABLE_METAL + void InitializeMetalRendererConfig(); + FlutterMetalRendererConfig metal_renderer_config_ = {}; #endif std::string dart_entrypoint_; FlutterCustomTaskRunners custom_task_runners_ = {}; diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test.cc index 36d8f7e446..b816ab5d22 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test.cc @@ -9,6 +9,10 @@ #include "flutter/shell/platform/embedder/tests/embedder_test_context_gl.h" #endif +#ifdef SHELL_ENABLE_METAL +#include "flutter/shell/platform/embedder/tests/embedder_test_context_metal.h" +#endif + namespace flutter { namespace testing { @@ -18,21 +22,28 @@ std::string EmbedderTest::GetFixturesDirectory() const { return GetFixturesPath(); } -EmbedderTestContext& EmbedderTest::GetEmbedderContext(ContextType type) { +EmbedderTestContext& EmbedderTest::GetEmbedderContext( + EmbedderTestContextType type) { // Setup the embedder context lazily instead of in the constructor because we // don't to do all the work if the test won't end up using context. if (!embedder_contexts_[type]) { switch (type) { - case ContextType::kSoftwareContext: + case EmbedderTestContextType::kSoftwareContext: embedder_contexts_[type] = std::make_unique( GetFixturesDirectory()); break; #ifdef SHELL_ENABLE_GL - case ContextType::kOpenGLContext: + case EmbedderTestContextType::kOpenGLContext: embedder_contexts_[type] = std::make_unique(GetFixturesDirectory()); break; +#endif +#ifdef SHELL_ENABLE_METAL + case EmbedderTestContextType::kMetalContext: + embedder_contexts_[type] = + std::make_unique(GetFixturesDirectory()); + break; #endif default: FML_DCHECK(false) << "Invalid context type specified."; diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test.h index 275d47bc8c..f00d802e0f 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test.h @@ -18,19 +18,14 @@ namespace testing { class EmbedderTest : public ThreadTest { public: - enum class ContextType { - kSoftwareContext, - kOpenGLContext, - }; - EmbedderTest(); std::string GetFixturesDirectory() const; - EmbedderTestContext& GetEmbedderContext(ContextType type); + EmbedderTestContext& GetEmbedderContext(EmbedderTestContextType type); private: - std::map> + std::map> embedder_contexts_; FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTest); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h index b44eabe453..39cbbc0c62 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context.h @@ -38,6 +38,12 @@ struct AOTDataDeleter { using UniqueAOTData = std::unique_ptr<_FlutterEngineAOTData, AOTDataDeleter>; +enum class EmbedderTestContextType { + kSoftwareContext, + kOpenGLContext, + kMetalContext, +}; + class EmbedderTestContext { public: EmbedderTestContext(std::string assets_path = ""); @@ -77,6 +83,8 @@ class EmbedderTestContext { virtual size_t GetSurfacePresentCount() const = 0; + virtual EmbedderTestContextType GetContextType() const = 0; + // TODO(gw280): encapsulate these properly for subclasses to use protected: // This allows the builder to access the hooks. diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.cc index c967794f87..e5eef963f8 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.cc @@ -3,13 +3,14 @@ // found in the LICENSE file. #include "flutter/shell/platform/embedder/tests/embedder_test_context_gl.h" -#include "flutter/shell/platform/embedder/tests/embedder_test_compositor_gl.h" #include "flutter/fml/make_copyable.h" #include "flutter/fml/paths.h" #include "flutter/runtime/dart_vm.h" #include "flutter/shell/platform/embedder/tests/embedder_assertions.h" +#include "flutter/shell/platform/embedder/tests/embedder_test_compositor_gl.h" #include "flutter/testing/testing.h" +#include "tests/embedder_test.h" #include "third_party/dart/runtime/bin/elf_loader.h" #include "third_party/skia/include/core/SkSurface.h" @@ -55,11 +56,7 @@ bool EmbedderTestContextGL::GLPresent(uint32_t fbo_id) { FireRootSurfacePresentCallbackIfPresent( [&]() { return gl_surface_->GetRasterSurfaceSnapshot(); }); - if (!gl_surface_->Present()) { - return false; - } - - return true; + return gl_surface_->Present(); } void EmbedderTestContextGL::SetGLGetFBOCallback(GLGetFBOCallback callback) { @@ -103,6 +100,10 @@ size_t EmbedderTestContextGL::GetSurfacePresentCount() const { return gl_surface_present_count_; } +EmbedderTestContextType EmbedderTestContextGL::GetContextType() const { + return EmbedderTestContextType::kOpenGLContext; +} + uint32_t EmbedderTestContextGL::GetWindowFBOId() const { FML_CHECK(gl_surface_); return gl_surface_->GetWindowFBOId(); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.h index 370e3d87db..6ab911f079 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_gl.h @@ -22,6 +22,9 @@ class EmbedderTestContextGL : public EmbedderTestContext { size_t GetSurfacePresentCount() const override; + // |EmbedderTestContext| + EmbedderTestContextType GetContextType() const override; + //---------------------------------------------------------------------------- /// @brief Sets a callback that will be invoked (on the raster task /// runner) when the engine asks the embedder for a new FBO ID at diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.cc new file mode 100644 index 0000000000..3514262b8b --- /dev/null +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.cc @@ -0,0 +1,53 @@ +// 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. + +#include "flutter/shell/platform/embedder/tests/embedder_test_context_metal.h" + +#include + +#include "flutter/fml/logging.h" + +namespace flutter { +namespace testing { + +EmbedderTestContextMetal::EmbedderTestContextMetal(std::string assets_path) + : EmbedderTestContext(assets_path) { + metal_context_ = std::make_unique(); +} + +EmbedderTestContextMetal::~EmbedderTestContextMetal() {} + +void EmbedderTestContextMetal::SetupSurface(SkISize surface_size) { + FML_CHECK(surface_size_.isEmpty()); + surface_size_ = surface_size; +} + +size_t EmbedderTestContextMetal::GetSurfacePresentCount() const { + return present_count_; +} + +EmbedderTestContextType EmbedderTestContextMetal::GetContextType() const { + return EmbedderTestContextType::kMetalContext; +} + +void EmbedderTestContextMetal::SetupCompositor() { + FML_CHECK(false) << "Compositor rendering not supported in metal."; +} + +TestMetalContext* EmbedderTestContextMetal::GetTestMetalContext() { + return metal_context_.get(); +} + +bool EmbedderTestContextMetal::Present(int64_t texture_id) { + FireRootSurfacePresentCallbackIfPresent([&]() { + auto metal_surface_ = + TestMetalSurface::Create(*metal_context_, texture_id, surface_size_); + return metal_surface_->GetRasterSurfaceSnapshot(); + }); + present_count_++; + return metal_context_->Present(texture_id); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.h new file mode 100644 index 0000000000..788e7bfacf --- /dev/null +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_metal.h @@ -0,0 +1,50 @@ +// 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_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_METAL_H_ +#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_METAL_H_ + +#include "flutter/shell/platform/embedder/tests/embedder_test_context.h" +#include "flutter/testing/test_metal_context.h" +#include "flutter/testing/test_metal_surface.h" + +namespace flutter { +namespace testing { + +class EmbedderTestContextMetal : public EmbedderTestContext { + public: + explicit EmbedderTestContextMetal(std::string assets_path = ""); + + ~EmbedderTestContextMetal() override; + + // |EmbedderTestContext| + EmbedderTestContextType GetContextType() const override; + + // |EmbedderTestContext| + size_t GetSurfacePresentCount() const override; + + // |EmbedderTestContext| + void SetupCompositor() override; + + bool Present(int64_t texture_id); + + TestMetalContext* GetTestMetalContext(); + + private: + // This allows the builder to access the hooks. + friend class EmbedderConfigBuilder; + + SkISize surface_size_ = SkISize::MakeEmpty(); + std::unique_ptr metal_context_; + size_t present_count_ = 0; + + void SetupSurface(SkISize surface_size) override; + + FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestContextMetal); +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_CONTEXT_METAL_H_ diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.cc index ae35dd9198..9ac6e62d44 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.cc @@ -34,6 +34,10 @@ size_t EmbedderTestContextSoftware::GetSurfacePresentCount() const { return software_surface_present_count_; } +EmbedderTestContextType EmbedderTestContextSoftware::GetContextType() const { + return EmbedderTestContextType::kSoftwareContext; +} + void EmbedderTestContextSoftware::SetupSurface(SkISize surface_size) { surface_size_ = surface_size; } diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.h b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.h index 7f146edeff..7f5b815c29 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.h +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_test_context_software.h @@ -18,6 +18,9 @@ class EmbedderTestContextSoftware : public EmbedderTestContext { size_t GetSurfacePresentCount() const override; + // |EmbedderTestContext| + EmbedderTestContextType GetContextType() const override; + bool Present(sk_sp image); protected: diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc index 538ab9ae15..2fe1cb1912 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests.cc @@ -42,7 +42,7 @@ TEST(EmbedderTestNoFixture, MustNotRunWithInvalidArgs) { } TEST_F(EmbedderTest, CanLaunchAndShutdownWithValidProjectArgs) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); fml::AutoResetWaitableEvent latch; context.AddIsolateCreateCallback([&latch]() { latch.Signal(); }); EmbedderConfigBuilder builder(context); @@ -57,7 +57,7 @@ TEST_F(EmbedderTest, CanLaunchAndShutdownWithValidProjectArgs) { // TODO(41999): Disabled because flaky. TEST_F(EmbedderTest, DISABLED_CanLaunchAndShutdownMultipleTimes) { EmbedderConfigBuilder builder( - GetEmbedderContext(ContextType::kSoftwareContext)); + GetEmbedderContext(EmbedderTestContextType::kSoftwareContext)); builder.SetSoftwareRendererConfig(); for (size_t i = 0; i < 3; ++i) { auto engine = builder.LaunchEngine(); @@ -67,7 +67,7 @@ TEST_F(EmbedderTest, DISABLED_CanLaunchAndShutdownMultipleTimes) { } TEST_F(EmbedderTest, CanInvokeCustomEntrypoint) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); static fml::AutoResetWaitableEvent latch; Dart_NativeFunction entrypoint = [](Dart_NativeArguments args) { latch.Signal(); @@ -82,7 +82,7 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypoint) { } TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); fml::AutoResetWaitableEvent latch1; fml::AutoResetWaitableEvent latch2; @@ -125,7 +125,7 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) { std::atomic_size_t EmbedderTestTaskRunner::sEmbedderTaskRunnerIdentifiers = {}; TEST_F(EmbedderTest, CanSpecifyCustomPlatformTaskRunner) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); fml::AutoResetWaitableEvent latch; // Run the test on its own thread with a message loop so that it can safely @@ -197,7 +197,7 @@ TEST(EmbedderTestNoFixture, CanGetCurrentTimeInNanoseconds) { } TEST_F(EmbedderTest, CanReloadSystemFonts) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); auto engine = builder.LaunchEngine(); @@ -208,7 +208,7 @@ TEST_F(EmbedderTest, CanReloadSystemFonts) { } TEST_F(EmbedderTest, IsolateServiceIdSent) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); fml::AutoResetWaitableEvent latch; fml::Thread thread; @@ -251,7 +251,7 @@ TEST_F(EmbedderTest, IsolateServiceIdSent) { /// immediately collects the same. /// TEST_F(EmbedderTest, CanCreateAndCollectCallbacks) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); builder.SetDartEntrypoint("platform_messages_response"); @@ -289,7 +289,8 @@ TEST_F(EmbedderTest, PlatformMessagesCanReceiveResponse) { CreateNewThread()->PostTask([&]() { captures.thread_id = std::this_thread::get_id(); - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = + GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); builder.SetDartEntrypoint("platform_messages_response"); @@ -345,7 +346,7 @@ TEST_F(EmbedderTest, PlatformMessagesCanReceiveResponse) { /// callback with the response is invoked to assert integrity. /// TEST_F(EmbedderTest, PlatformMessagesCanBeSentWithoutResponseHandles) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); builder.SetDartEntrypoint("platform_messages_no_response"); @@ -390,7 +391,7 @@ TEST_F(EmbedderTest, PlatformMessagesCanBeSentWithoutResponseHandles) { /// Tests that a null platform message can be sent. /// TEST_F(EmbedderTest, NullPlatformMessagesCanBeSent) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); builder.SetDartEntrypoint("null_platform_messages"); @@ -432,7 +433,7 @@ TEST_F(EmbedderTest, NullPlatformMessagesCanBeSent) { /// isn't equals to 0. /// TEST_F(EmbedderTest, InvalidPlatformMessages) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); auto engine = builder.LaunchEngine(); @@ -456,7 +457,7 @@ TEST_F(EmbedderTest, InvalidPlatformMessages) { /// set to true by default in these unit-tests). /// TEST_F(EmbedderTest, VMShutsDownWhenNoEnginesInProcess) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); const auto launch_count = DartVM::GetVMLaunchCount(); @@ -475,7 +476,7 @@ TEST_F(EmbedderTest, VMShutsDownWhenNoEnginesInProcess) { //------------------------------------------------------------------------------ /// TEST_F(EmbedderTest, DartEntrypointArgs) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); builder.AddDartEntrypointArgument("foo"); @@ -509,7 +510,7 @@ TEST_F(EmbedderTest, VMAndIsolateSnapshotSizesAreRedundantInAOTMode) { GTEST_SKIP(); return; } - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); @@ -529,7 +530,7 @@ TEST_F(EmbedderTest, VMAndIsolateSnapshotSizesAreRedundantInAOTMode) { /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownSceneWithSoftwareCompositor) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(SkISize::Make(800, 600)); @@ -701,7 +702,7 @@ TEST_F(EmbedderTest, /// TEST_F(EmbedderTest, CanCreateInitializedEngine) { EmbedderConfigBuilder builder( - GetEmbedderContext(ContextType::kSoftwareContext)); + GetEmbedderContext(EmbedderTestContextType::kSoftwareContext)); builder.SetSoftwareRendererConfig(); auto engine = builder.InitializeEngine(); ASSERT_TRUE(engine.is_valid()); @@ -713,7 +714,7 @@ TEST_F(EmbedderTest, CanCreateInitializedEngine) { /// TEST_F(EmbedderTest, CanRunInitializedEngine) { EmbedderConfigBuilder builder( - GetEmbedderContext(ContextType::kSoftwareContext)); + GetEmbedderContext(EmbedderTestContextType::kSoftwareContext)); builder.SetSoftwareRendererConfig(); auto engine = builder.InitializeEngine(); ASSERT_TRUE(engine.is_valid()); @@ -728,7 +729,7 @@ TEST_F(EmbedderTest, CanRunInitializedEngine) { /// TEST_F(EmbedderTest, CaDeinitializeAnEngine) { EmbedderConfigBuilder builder( - GetEmbedderContext(ContextType::kSoftwareContext)); + GetEmbedderContext(EmbedderTestContextType::kSoftwareContext)); builder.SetSoftwareRendererConfig(); auto engine = builder.InitializeEngine(); ASSERT_TRUE(engine.is_valid()); @@ -751,7 +752,7 @@ TEST_F(EmbedderTest, CaDeinitializeAnEngine) { } TEST_F(EmbedderTest, CanUpdateLocales) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); builder.SetDartEntrypoint("can_receive_locale_updates"); @@ -810,7 +811,7 @@ TEST_F(EmbedderTest, CanUpdateLocales) { } TEST_F(EmbedderTest, LocalizationCallbacksCalled) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); fml::AutoResetWaitableEvent latch; context.AddIsolateCreateCallback([&latch]() { latch.Signal(); }); EmbedderConfigBuilder builder(context); @@ -842,7 +843,7 @@ TEST_F(EmbedderTest, CanQueryDartAOTMode) { } TEST_F(EmbedderTest, VerifyB143464703WithSoftwareBackend) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(SkISize::Make(1024, 600)); @@ -961,7 +962,7 @@ TEST_F(EmbedderTest, VerifyB143464703WithSoftwareBackend) { } TEST_F(EmbedderTest, CanSendLowMemoryNotification) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); @@ -990,7 +991,8 @@ TEST_F(EmbedderTest, CanPostTaskToAllNativeThreads) { auto platform_task_runner = CreateNewThread("platform_thread"); platform_task_runner->PostTask([&]() { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = + GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetSoftwareRendererConfig(); @@ -1128,7 +1130,7 @@ TEST_F(EmbedderTest, MustNotRunWithMultipleAOTSources) { GTEST_SKIP(); return; } - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder( context, @@ -1170,7 +1172,7 @@ TEST_F(EmbedderTest, CanLaunchAndShutdownWithAValidElfSource) { GTEST_SKIP(); return; } - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); fml::AutoResetWaitableEvent latch; context.AddIsolateCreateCallback([&latch]() { latch.Signal(); }); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_gl.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_gl.cc index 0b7b4e3312..a7fc2e9316 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_gl.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_gl.cc @@ -36,7 +36,7 @@ using EmbedderTest = testing::EmbedderTest; TEST_F(EmbedderTest, CanCreateOpenGLRenderingEngine) { EmbedderConfigBuilder builder( - GetEmbedderContext(ContextType::kOpenGLContext)); + GetEmbedderContext(EmbedderTestContextType::kOpenGLContext)); builder.SetOpenGLRendererConfig(SkISize::Make(1, 1)); auto engine = builder.LaunchEngine(); ASSERT_TRUE(engine.is_valid()); @@ -49,7 +49,7 @@ TEST_F(EmbedderTest, CanCreateOpenGLRenderingEngine) { /// TEST_F(EmbedderTest, MustPreventEngineLaunchWhenRequiredCompositorArgsAreAbsent) { - auto& context = GetEmbedderContext(ContextType::kSoftwareContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(1, 1)); builder.SetCompositor(); @@ -65,7 +65,7 @@ TEST_F(EmbedderTest, /// complete OpenGL textures. /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderToOpenGLFramebuffer) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -156,7 +156,7 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderToOpenGLFramebuffer) { /// Layers in a hierarchy containing a platform view should not be cached. The /// other layers in the hierarchy should be, however. TEST_F(EmbedderTest, RasterCacheDisabledWithPlatformViews) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -260,7 +260,7 @@ TEST_F(EmbedderTest, RasterCacheDisabledWithPlatformViews) { /// The RasterCache should normally be enabled. /// TEST_F(EmbedderTest, RasterCacheEnabled) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -331,7 +331,7 @@ TEST_F(EmbedderTest, RasterCacheEnabled) { /// the individual layers are OpenGL textures. /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderToOpenGLTexture) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -423,7 +423,7 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderToOpenGLTexture) { /// individual layers are software buffers. /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderToSoftwareBuffer) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -514,7 +514,7 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderToSoftwareBuffer) { /// Test the layer structure and pixels rendered when using a custom compositor. /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownScene) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -685,7 +685,7 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownScene) { /// thread merging mechanism must not interfere with the custom compositor. /// TEST_F(EmbedderTest, CustomCompositorMustWorkWithCustomTaskRunner) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); @@ -810,7 +810,7 @@ TEST_F(EmbedderTest, CustomCompositorMustWorkWithCustomTaskRunner) { /// and a single layer. /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderWithRootLayerOnly) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -877,7 +877,7 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderWithRootLayerOnly) { /// and ensure that a redundant layer is not added. /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderWithPlatformLayerOnBottom) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -990,7 +990,7 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderWithPlatformLayerOnBottom) { /// TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownSceneWithRootSurfaceTransformation) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 800)); @@ -1164,7 +1164,7 @@ TEST_F(EmbedderTest, } TEST_F(EmbedderTest, CanRenderSceneWithoutCustomCompositor) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); @@ -1190,7 +1190,7 @@ TEST_F(EmbedderTest, CanRenderSceneWithoutCustomCompositor) { } TEST_F(EmbedderTest, CanRenderSceneWithoutCustomCompositorWithTransformation) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); const auto root_surface_transformation = SkMatrix().preTranslate(0, 800).preRotate(-90, 0, 0); @@ -1223,7 +1223,7 @@ TEST_F(EmbedderTest, CanRenderSceneWithoutCustomCompositorWithTransformation) { } TEST_F(EmbedderTest, CanRenderGradientWithoutCompositor) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); @@ -1248,7 +1248,7 @@ TEST_F(EmbedderTest, CanRenderGradientWithoutCompositor) { } TEST_F(EmbedderTest, CanRenderGradientWithoutCompositorWithXform) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); const auto root_surface_transformation = SkMatrix().preTranslate(0, 800).preRotate(-90, 0, 0); @@ -1281,7 +1281,7 @@ TEST_F(EmbedderTest, CanRenderGradientWithoutCompositorWithXform) { } TEST_F(EmbedderTest, CanRenderGradientWithCompositor) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); @@ -1309,7 +1309,7 @@ TEST_F(EmbedderTest, CanRenderGradientWithCompositor) { } TEST_F(EmbedderTest, CanRenderGradientWithCompositorWithXform) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); // This must match the transformation provided in the // |CanRenderGradientWithoutCompositorWithXform| test to ensure that @@ -1346,7 +1346,7 @@ TEST_F(EmbedderTest, CanRenderGradientWithCompositorWithXform) { } TEST_F(EmbedderTest, CanRenderGradientWithCompositorOnNonRootLayer) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); @@ -1451,7 +1451,7 @@ TEST_F(EmbedderTest, CanRenderGradientWithCompositorOnNonRootLayer) { } TEST_F(EmbedderTest, CanRenderGradientWithCompositorOnNonRootLayerWithXform) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); // This must match the transformation provided in the // |CanRenderGradientWithoutCompositorWithXform| test to ensure that @@ -1565,7 +1565,7 @@ TEST_F(EmbedderTest, CanRenderGradientWithCompositorOnNonRootLayerWithXform) { } TEST_F(EmbedderTest, VerifyB141980393) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); @@ -1686,7 +1686,7 @@ TEST_F(EmbedderTest, CanCreateEmbedderWithCustomRenderTaskRunner) { } }); EmbedderConfigBuilder builder( - GetEmbedderContext(ContextType::kOpenGLContext)); + GetEmbedderContext(EmbedderTestContextType::kOpenGLContext)); builder.SetDartEntrypoint("can_render_scene_without_custom_compositor"); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); builder.SetRenderTaskRunner( @@ -1745,7 +1745,7 @@ TEST_F(EmbedderTest, platform_task_runner->PostTask([&]() { EmbedderConfigBuilder builder( - GetEmbedderContext(ContextType::kOpenGLContext)); + GetEmbedderContext(EmbedderTestContextType::kOpenGLContext)); builder.SetDartEntrypoint("can_render_scene_without_custom_compositor"); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); builder.SetRenderTaskRunner( @@ -1799,7 +1799,7 @@ TEST_F(EmbedderTest, TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownScenePixelRatioOnSurface) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -1890,7 +1890,7 @@ TEST_F(EmbedderTest, TEST_F( EmbedderTest, CompositorMustBeAbleToRenderKnownScenePixelRatioOnSurfaceWithRootSurfaceXformation) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 800)); @@ -1984,7 +1984,7 @@ TEST_F( TEST_F(EmbedderTest, PushingMutlipleFramesSetsUpNewRecordingCanvasWithCustomCompositor) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 1024)); @@ -2026,7 +2026,7 @@ TEST_F(EmbedderTest, TEST_F(EmbedderTest, PushingMutlipleFramesSetsUpNewRecordingCanvasWithoutCustomCompositor) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 1024)); @@ -2063,7 +2063,7 @@ TEST_F(EmbedderTest, } TEST_F(EmbedderTest, PlatformViewMutatorsAreValid) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -2159,7 +2159,7 @@ TEST_F(EmbedderTest, PlatformViewMutatorsAreValid) { } TEST_F(EmbedderTest, PlatformViewMutatorsAreValidWithPixelRatio) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -2256,7 +2256,7 @@ TEST_F(EmbedderTest, PlatformViewMutatorsAreValidWithPixelRatio) { TEST_F(EmbedderTest, PlatformViewMutatorsAreValidWithPixelRatioAndRootSurfaceTransformation) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -2358,7 +2358,7 @@ TEST_F(EmbedderTest, } TEST_F(EmbedderTest, EmptySceneIsAcceptable) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -2384,7 +2384,7 @@ TEST_F(EmbedderTest, EmptySceneIsAcceptable) { } TEST_F(EmbedderTest, SceneWithNoRootContainerIsAcceptable) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -2414,7 +2414,7 @@ TEST_F(EmbedderTest, SceneWithNoRootContainerIsAcceptable) { // Verifies that https://skia-review.googlesource.com/c/skia/+/259174 is pulled // into the engine. TEST_F(EmbedderTest, ArcEndCapsAreDrawnCorrectly) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 1024)); @@ -2448,7 +2448,7 @@ TEST_F(EmbedderTest, ArcEndCapsAreDrawnCorrectly) { } TEST_F(EmbedderTest, ClipsAreCorrectlyCalculated) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(400, 300)); @@ -2527,7 +2527,7 @@ TEST_F(EmbedderTest, ClipsAreCorrectlyCalculated) { } TEST_F(EmbedderTest, ComplexClipsAreCorrectlyCalculated) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(1024, 600)); @@ -2611,7 +2611,7 @@ TEST_F(EmbedderTest, ComplexClipsAreCorrectlyCalculated) { } TEST_F(EmbedderTest, ObjectsCanBePostedViaPorts) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 1024)); builder.SetDartEntrypoint("objects_can_be_posted"); @@ -2810,7 +2810,7 @@ TEST_F(EmbedderTest, ObjectsCanBePostedViaPorts) { } TEST_F(EmbedderTest, CompositorCanPostZeroLayersForPresentation) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(300, 200)); @@ -2843,7 +2843,7 @@ TEST_F(EmbedderTest, CompositorCanPostZeroLayersForPresentation) { } TEST_F(EmbedderTest, CompositorCanPostOnlyPlatformViews) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(300, 200)); @@ -2906,7 +2906,7 @@ TEST_F(EmbedderTest, CompositorCanPostOnlyPlatformViews) { } TEST_F(EmbedderTest, CompositorRenderTargetsAreRecycled) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(300, 200)); @@ -2951,7 +2951,7 @@ TEST_F(EmbedderTest, CompositorRenderTargetsAreRecycled) { } TEST_F(EmbedderTest, CompositorRenderTargetsAreInStableOrder) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(300, 200)); @@ -3020,7 +3020,7 @@ TEST_F(EmbedderTest, CompositorRenderTargetsAreInStableOrder) { } TEST_F(EmbedderTest, FrameInfoContainsValidWidthAndHeight) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 1024)); @@ -3063,7 +3063,7 @@ TEST_F(EmbedderTest, FrameInfoContainsValidWidthAndHeight) { } TEST_F(EmbedderTest, MustNotRunWithBothFBOCallbacksSet) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 1024)); @@ -3074,7 +3074,7 @@ TEST_F(EmbedderTest, MustNotRunWithBothFBOCallbacksSet) { } TEST_F(EmbedderTest, MustNotRunWithBothPresentCallbacksSet) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 1024)); @@ -3085,7 +3085,7 @@ TEST_F(EmbedderTest, MustNotRunWithBothPresentCallbacksSet) { } TEST_F(EmbedderTest, PresentInfoContainsValidFBOId) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(600, 1024)); @@ -3128,7 +3128,7 @@ TEST_F(EmbedderTest, PresentInfoContainsValidFBOId) { } TEST_F(EmbedderTest, SetSingleDisplayConfigurationWithDisplayId) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -3170,7 +3170,7 @@ TEST_F(EmbedderTest, SetSingleDisplayConfigurationWithDisplayId) { } TEST_F(EmbedderTest, SetSingleDisplayConfigurationWithoutDisplayId) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -3212,7 +3212,7 @@ TEST_F(EmbedderTest, SetSingleDisplayConfigurationWithoutDisplayId) { } TEST_F(EmbedderTest, SetValidMultiDisplayConfiguration) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -3261,7 +3261,7 @@ TEST_F(EmbedderTest, SetValidMultiDisplayConfiguration) { } TEST_F(EmbedderTest, MultipleDisplaysWithSingleDisplayTrueIsInvalid) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -3307,7 +3307,7 @@ TEST_F(EmbedderTest, MultipleDisplaysWithSingleDisplayTrueIsInvalid) { } TEST_F(EmbedderTest, MultipleDisplaysWithSameDisplayIdIsInvalid) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(800, 600)); @@ -3353,7 +3353,7 @@ TEST_F(EmbedderTest, MultipleDisplaysWithSameDisplayIdIsInvalid) { } TEST_F(EmbedderTest, CompositorRenderTargetsNotRecycledWhenAvoidsCacheSet) { - auto& context = GetEmbedderContext(ContextType::kOpenGLContext); + auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext); EmbedderConfigBuilder builder(context); builder.SetOpenGLRendererConfig(SkISize::Make(300, 200)); diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_metal.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_metal.cc new file mode 100644 index 0000000000..d95d0b9c72 --- /dev/null +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_metal.cc @@ -0,0 +1,50 @@ +// 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 +#include + +#include "embedder.h" +#include "flutter/shell/platform/embedder/tests/embedder_assertions.h" +#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h" +#include "flutter/shell/platform/embedder/tests/embedder_test.h" +#include "flutter/shell/platform/embedder/tests/embedder_test_context_gl.h" +#include "flutter/shell/platform/embedder/tests/embedder_unittests_util.h" +#include "flutter/testing/assertions_skia.h" +#include "flutter/testing/testing.h" + +namespace flutter { +namespace testing { + +using EmbedderTest = testing::EmbedderTest; + +TEST_F(EmbedderTest, CanRenderGradientWithMetal) { + auto& context = GetEmbedderContext(EmbedderTestContextType::kMetalContext); + + EmbedderConfigBuilder builder(context); + + builder.SetDartEntrypoint("render_gradient"); + builder.SetMetalRendererConfig(SkISize::Make(800, 600)); + + auto renderered_scene = context.GetNextSceneImage(); + + auto engine = builder.LaunchEngine(); + ASSERT_TRUE(engine.is_valid()); + + // Send a window metrics events so frames may be scheduled. + FlutterWindowMetricsEvent event = {}; + event.struct_size = sizeof(event); + event.width = 800; + event.height = 600; + event.pixel_ratio = 1.0; + ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), + kSuccess); + + ASSERT_TRUE(ImageMatchesFixture("gradient_metal.png", renderered_scene)); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_util.cc b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_util.cc index 5058e2c308..59479a808b 100644 --- a/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_util.cc +++ b/engine/src/flutter/shell/platform/embedder/tests/embedder_unittests_util.cc @@ -4,6 +4,8 @@ #define FML_USED_ON_EMBEDDER +#include + #include "flutter/shell/platform/embedder/tests/embedder_unittests_util.h" namespace flutter { @@ -58,6 +60,30 @@ bool RasterImagesAreSame(sk_sp a, sk_sp b) { return false; } + if (pixmapA.info() != pixmapB.info()) { + // Given that the sizes are equal but the infos aren't equal + // we have a situation where the color-type, color-space and + // alpha type are potentially mismatched. We fallback to compating + // pixel-wise values for alpha and color. + for (int x = 0; x < pixmapA.width(); x++) { + for (int y = 0; y < pixmapA.height(); y++) { + SkColor color_a = pixmapA.getColor(x, y); + SkColor color_b = pixmapB.getColor(x, y); + if (color_a != color_b) { + return false; + } + float alpha_a = pixmapA.getAlphaf(x, y); + float alpha_b = pixmapB.getAlphaf(x, y); + if (std::abs(alpha_a - alpha_b) > + std::numeric_limits::epsilon()) { + return false; + } + } + } + + return true; + } + return ::memcmp(pixmapA.addr(), pixmapB.addr(), sizeA) == 0; } @@ -68,7 +94,7 @@ bool WriteImageToDisk(const fml::UniqueFD& directory, return false; } - auto data = image->encodeToData(SkEncodedImageFormat::kPNG, 100); + auto data = image->encodeToData(); if (!data) { return false; @@ -94,6 +120,8 @@ bool ImageMatchesFixture(const std::string& fixture_file_name, FML_CHECK(fixture_image) << "Could not create image from fixture: " << fixture_file_name; + FML_CHECK(scene_image) << "Invalid scene image."; + auto scene_image_subset = scene_image->makeSubset( SkIRect::MakeWH(fixture_image->width(), fixture_image->height())); diff --git a/engine/src/flutter/testing/BUILD.gn b/engine/src/flutter/testing/BUILD.gn index 71fc74b08b..3b1c3bd19d 100644 --- a/engine/src/flutter/testing/BUILD.gn +++ b/engine/src/flutter/testing/BUILD.gn @@ -137,24 +137,23 @@ if (enable_unittests) { # backend. This way, all tests compile on all platforms but the Metal backend # is exercised on platforms where Metal itself is available. source_set("metal") { - testonly = true - - sources = [ - "test_metal_surface.cc", - "test_metal_surface.h", - ] - - defines = [] - if (shell_enable_metal) { - sources += [ "test_metal_surface_impl.mm" ] - defines += [ "TESTING_ENABLE_METAL" ] + sources = [ + "test_metal_context.h", + "test_metal_context.mm", + "test_metal_surface.cc", + "test_metal_surface.h", + "test_metal_surface_impl.h", + "test_metal_surface_impl.mm", + ] + + deps = [ + ":skia", + "//flutter/fml", + ] } - deps = [ - ":skia", - "//flutter/fml", - ] + testonly = true } test_fixtures("testing_fixtures") { @@ -166,17 +165,19 @@ if (enable_unittests) { executable("testing_unittests") { testonly = true - sources = [ - "mock_canvas_unittests.cc", - "test_metal_surface_unittests.cc", - ] + sources = [ "mock_canvas_unittests.cc" ] deps = [ - ":metal", ":skia", ":testing", ":testing_fixtures", "//flutter/runtime:libdart", ] + + if (shell_enable_metal) { + sources += [ "test_metal_surface_unittests.cc" ] + + deps += [ ":metal" ] + } } } diff --git a/engine/src/flutter/testing/test_metal_context.h b/engine/src/flutter/testing/test_metal_context.h new file mode 100644 index 0000000000..936f66fcb2 --- /dev/null +++ b/engine/src/flutter/testing/test_metal_context.h @@ -0,0 +1,51 @@ +// 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_TESTING_TEST_METAL_CONTEXT_H_ +#define FLUTTER_TESTING_TEST_METAL_CONTEXT_H_ + +#include +#include + +#include "third_party/skia/include/gpu/GrDirectContext.h" +#include "third_party/skia/include/gpu/mtl/GrMtlTypes.h" + +namespace flutter { + +class TestMetalContext { + public: + struct TextureInfo { + int64_t texture_id; + void* texture; + }; + + TestMetalContext(); + + ~TestMetalContext(); + + void* GetMetalDevice() const; + + void* GetMetalCommandQueue() const; + + sk_sp GetSkiaContext() const; + + /// Returns texture_id = -1 when texture creation fails. + TextureInfo CreateMetalTexture(const SkISize& size); + + bool Present(int64_t texture_id); + + TextureInfo GetTextureInfo(int64_t texture_id); + + private: + void* device_; + void* command_queue_; + sk_sp skia_context_; + std::mutex textures_mutex; + int64_t texture_id_ctr_ = 1; // guarded by textures_mutex + std::map> textures_; // guarded by textures_mutex +}; + +} // namespace flutter + +#endif // FLUTTER_TESTING_TEST_METAL_CONTEXT_H_ diff --git a/engine/src/flutter/testing/test_metal_context.mm b/engine/src/flutter/testing/test_metal_context.mm new file mode 100644 index 0000000000..e6f5e480d9 --- /dev/null +++ b/engine/src/flutter/testing/test_metal_context.mm @@ -0,0 +1,126 @@ +// 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. + +#include "flutter/testing/test_metal_context.h" + +#include +#include + +#include "flutter/fml/logging.h" +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "third_party/skia/include/core/SkSurface.h" + +namespace flutter { + +TestMetalContext::TestMetalContext() { + auto device = fml::scoped_nsprotocol{[MTLCreateSystemDefaultDevice() retain]}; + if (!device) { + FML_LOG(ERROR) << "Could not acquire Metal device."; + return; + } + + auto command_queue = fml::scoped_nsobject{[device.get() newCommandQueue]}; + if (!command_queue) { + FML_LOG(ERROR) << "Could not create the default command queue."; + return; + } + + [command_queue.get() setLabel:@"Flutter Test Queue"]; + + // Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later + // when the GrDirectContext is collected. + skia_context_ = GrDirectContext::MakeMetal([device.get() retain], [command_queue.get() retain]); + if (!skia_context_) { + FML_LOG(ERROR) << "Could not create the GrDirectContext from the Metal Device " + "and command queue."; + } + + device_ = [device.get() retain]; + command_queue_ = [command_queue.get() retain]; +} + +TestMetalContext::~TestMetalContext() { + std::scoped_lock lock(textures_mutex); + textures_.clear(); + if (device_) { + [(__bridge id)device_ release]; + } + if (command_queue_) { + [(__bridge id)command_queue_ release]; + } +} + +void* TestMetalContext::GetMetalDevice() const { + return device_; +} + +void* TestMetalContext::GetMetalCommandQueue() const { + return command_queue_; +} + +sk_sp TestMetalContext::GetSkiaContext() const { + return skia_context_; +} + +TestMetalContext::TextureInfo TestMetalContext::CreateMetalTexture(const SkISize& size) { + std::scoped_lock lock(textures_mutex); + auto texture_descriptor = fml::scoped_nsobject{ + [[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm + width:size.width() + height:size.height() + mipmapped:NO] retain]}; + + // The most pessimistic option and disables all optimizations but allows tests + // the most flexible access to the surface. They may read and write to the + // surface from shaders or use as a pixel view. + texture_descriptor.get().usage = MTLTextureUsageUnknown; + + if (!texture_descriptor) { + FML_CHECK(false) << "Invalid texture descriptor."; + return {.texture_id = -1, .texture = nullptr}; + } + + id device = (__bridge id)GetMetalDevice(); + sk_cf_obj texture = + sk_cf_obj{[[device newTextureWithDescriptor:texture_descriptor.get()] retain]}; + + if (!texture) { + FML_CHECK(false) << "Could not create texture from texture descriptor."; + return {.texture_id = -1, .texture = nullptr}; + } + + const int64_t texture_id = texture_id_ctr_++; + textures_[texture_id] = texture; + std::cout << "inserting texture with id: " << texture_id << std::endl; + + return { + .texture_id = texture_id, + .texture = texture.get(), + }; +} + +// Don't remove the texture from the map here. +bool TestMetalContext::Present(int64_t texture_id) { + std::scoped_lock lock(textures_mutex); + if (textures_.find(texture_id) == textures_.end()) { + return false; + } else { + return true; + } +} + +TestMetalContext::TextureInfo TestMetalContext::GetTextureInfo(int64_t texture_id) { + std::scoped_lock lock(textures_mutex); + if (textures_.find(texture_id) == textures_.end()) { + FML_CHECK(false) << "Invalid texture id: " << texture_id; + return {.texture_id = -1, .texture = nullptr}; + } else { + return { + .texture_id = texture_id, + .texture = textures_[texture_id].get(), + }; + } +} + +} // namespace flutter diff --git a/engine/src/flutter/testing/test_metal_surface.cc b/engine/src/flutter/testing/test_metal_surface.cc index 9d93715b7c..e7136d8d08 100644 --- a/engine/src/flutter/testing/test_metal_surface.cc +++ b/engine/src/flutter/testing/test_metal_surface.cc @@ -4,27 +4,28 @@ #include "flutter/testing/test_metal_surface.h" -#if TESTING_ENABLE_METAL +#include "flutter/fml/logging.h" #include "flutter/testing/test_metal_surface_impl.h" -#endif // TESTING_ENABLE_METAL namespace flutter { bool TestMetalSurface::PlatformSupportsMetal() { -#if TESTING_ENABLE_METAL return true; -#else // TESTING_ENABLE_METAL - return false; -#endif // TESTING_ENABLE_METAL } std::unique_ptr TestMetalSurface::Create( + const TestMetalContext& test_metal_context, SkISize surface_size) { -#if TESTING_ENABLE_METAL - return std::make_unique(surface_size); -#else // TESTING_ENABLE_METAL - return nullptr; -#endif // TESTING_ENABLE_METAL + return std::make_unique(test_metal_context, + surface_size); +} + +std::unique_ptr TestMetalSurface::Create( + const TestMetalContext& test_metal_context, + int64_t texture_id, + SkISize surface_size) { + return std::make_unique(test_metal_context, texture_id, + surface_size); } TestMetalSurface::TestMetalSurface() = default; @@ -43,4 +44,8 @@ sk_sp TestMetalSurface::GetSurface() const { return impl_ ? impl_->GetSurface() : nullptr; } +sk_sp TestMetalSurface::GetRasterSurfaceSnapshot() { + return impl_ ? impl_->GetRasterSurfaceSnapshot() : nullptr; +} + } // namespace flutter diff --git a/engine/src/flutter/testing/test_metal_surface.h b/engine/src/flutter/testing/test_metal_surface.h index ed81b4bf33..bf40f8effa 100644 --- a/engine/src/flutter/testing/test_metal_surface.h +++ b/engine/src/flutter/testing/test_metal_surface.h @@ -6,6 +6,7 @@ #define FLUTTER_TESTING_TEST_METAL_SURFACE_H_ #include "flutter/fml/macros.h" +#include "flutter/testing/test_metal_context.h" #include "third_party/skia/include/core/SkSize.h" #include "third_party/skia/include/core/SkSurface.h" #include "third_party/skia/include/gpu/GrDirectContext.h" @@ -21,6 +22,12 @@ class TestMetalSurface { static bool PlatformSupportsMetal(); static std::unique_ptr Create( + const TestMetalContext& test_metal_context, + SkISize surface_size = SkISize::MakeEmpty()); + + static std::unique_ptr Create( + const TestMetalContext& test_metal_context, + int64_t texture_id, SkISize surface_size = SkISize::MakeEmpty()); virtual ~TestMetalSurface(); @@ -31,13 +38,15 @@ class TestMetalSurface { virtual sk_sp GetSurface() const; + virtual sk_sp GetRasterSurfaceSnapshot(); + protected: TestMetalSurface(); private: std::unique_ptr impl_; - TestMetalSurface(std::unique_ptr impl); + explicit TestMetalSurface(std::unique_ptr impl); FML_DISALLOW_COPY_AND_ASSIGN(TestMetalSurface); }; diff --git a/engine/src/flutter/testing/test_metal_surface_impl.h b/engine/src/flutter/testing/test_metal_surface_impl.h index 5ef5e63d2b..3eba1840b3 100644 --- a/engine/src/flutter/testing/test_metal_surface_impl.h +++ b/engine/src/flutter/testing/test_metal_surface_impl.h @@ -6,20 +6,29 @@ #define FLUTTER_TESTING_TEST_METAL_SURFACE_IMPL_H_ #include "flutter/fml/macros.h" +#include "flutter/testing/test_metal_context.h" #include "flutter/testing/test_metal_surface.h" namespace flutter { class TestMetalSurfaceImpl : public TestMetalSurface { public: - TestMetalSurfaceImpl(SkISize surface_size); + TestMetalSurfaceImpl(const TestMetalContext& test_metal_context, + const SkISize& surface_size); + + TestMetalSurfaceImpl(const TestMetalContext& test_metal_context, + int64_t texture_id, + const SkISize& surface_size); // |TestMetalSurface| ~TestMetalSurfaceImpl() override; private: + void Init(const TestMetalContext::TextureInfo& texture_info, + const SkISize& surface_size); + + const TestMetalContext& test_metal_context_; bool is_valid_ = false; - sk_sp context_; sk_sp surface_; // |TestMetalSurface| @@ -31,6 +40,9 @@ class TestMetalSurfaceImpl : public TestMetalSurface { // |TestMetalSurface| sk_sp GetSurface() const override; + // |TestMetalSurface| + sk_sp GetRasterSurfaceSnapshot() override; + FML_DISALLOW_COPY_AND_ASSIGN(TestMetalSurfaceImpl); }; diff --git a/engine/src/flutter/testing/test_metal_surface_impl.mm b/engine/src/flutter/testing/test_metal_surface_impl.mm index 9ba283545f..2da07333e5 100644 --- a/engine/src/flutter/testing/test_metal_surface_impl.mm +++ b/engine/src/flutter/testing/test_metal_surface_impl.mm @@ -8,28 +8,13 @@ #include "flutter/fml/logging.h" #include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "flutter/testing/test_metal_context.h" #include "third_party/skia/include/core/SkSurface.h" namespace flutter { -TestMetalSurfaceImpl::TestMetalSurfaceImpl(SkISize surface_size) { - if (surface_size.isEmpty()) { - FML_LOG(ERROR) << "Size of test Metal surface was empty."; - return; - } - - auto device = fml::scoped_nsobject{[MTLCreateSystemDefaultDevice() retain]}; - if (!device) { - FML_LOG(ERROR) << "Could not acquire Metal device."; - return; - } - - auto command_queue = fml::scoped_nsobject{[device.get() newCommandQueue]}; - if (!command_queue) { - FML_LOG(ERROR) << "Could not create the default command queue."; - return; - } - +void TestMetalSurfaceImpl::Init(const TestMetalContext::TextureInfo& texture_info, + const SkISize& surface_size) { auto texture_descriptor = fml::scoped_nsobject{ [[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm width:surface_size.width() @@ -46,48 +31,16 @@ TestMetalSurfaceImpl::TestMetalSurfaceImpl(SkISize surface_size) { return; } - auto texture = - fml::scoped_nsobject{[device.get() newTextureWithDescriptor:texture_descriptor.get()]}; - - if (!texture) { - FML_LOG(ERROR) << "Could not create texture from texture descriptor."; - return; - } - - auto skia_context = GrDirectContext::MakeMetal(device.get(), command_queue.get()); - - if (skia_context) { - // Skia wants ownership of the device and queue. If a context was created, - // we now no longer own the argument. Release the arguments only on - // successful creation of the context. - FML_ALLOW_UNUSED_LOCAL(device.release()); - FML_ALLOW_UNUSED_LOCAL(command_queue.release()); - } else { - FML_LOG(ERROR) << "Could not create the GrDirectContext from the Metal Device " - "and command queue."; - return; - } + id texture = (__bridge id)texture_info.texture; GrMtlTextureInfo skia_texture_info; - skia_texture_info.fTexture = sk_cf_obj{[texture.get() retain]}; + skia_texture_info.fTexture.reset([texture retain]); + GrBackendTexture backend_texture(surface_size.width(), surface_size.height(), GrMipmapped::kNo, + skia_texture_info); - auto backend_render_target = GrBackendRenderTarget{ - surface_size.width(), // width - surface_size.height(), // height - 1, // sample count - skia_texture_info // texture info - }; - - auto surface = SkSurface::MakeFromBackendRenderTarget( - skia_context.get(), // context - backend_render_target, // backend render target - kTopLeft_GrSurfaceOrigin, // surface origin - kBGRA_8888_SkColorType, // color type - nullptr, // color space - nullptr, // surface properties - nullptr, // release proc (texture is already ref counted in sk_cf_obj) - nullptr // release context - ); + sk_sp surface = SkSurface::MakeFromBackendTexture( + test_metal_context_.GetSkiaContext().get(), backend_texture, kTopLeft_GrSurfaceOrigin, 1, + kBGRA_8888_SkColorType, nullptr, nullptr); if (!surface) { FML_LOG(ERROR) << "Could not create Skia surface from a Metal texture."; @@ -95,11 +48,60 @@ TestMetalSurfaceImpl::TestMetalSurfaceImpl(SkISize surface_size) { } surface_ = std::move(surface); - context_ = std::move(skia_context); - is_valid_ = true; } +TestMetalSurfaceImpl::TestMetalSurfaceImpl(const TestMetalContext& test_metal_context, + int64_t texture_id, + const SkISize& surface_size) + : test_metal_context_(test_metal_context) { + TestMetalContext::TextureInfo texture_info = + const_cast(test_metal_context_).GetTextureInfo(texture_id); + Init(texture_info, surface_size); +} + +TestMetalSurfaceImpl::TestMetalSurfaceImpl(const TestMetalContext& test_metal_context, + const SkISize& surface_size) + : test_metal_context_(test_metal_context) { + if (surface_size.isEmpty()) { + FML_LOG(ERROR) << "Size of test Metal surface was empty."; + return; + } + TestMetalContext::TextureInfo texture_info = + const_cast(test_metal_context_).CreateMetalTexture(surface_size); + Init(texture_info, surface_size); +} + +sk_sp TestMetalSurfaceImpl::GetRasterSurfaceSnapshot() { + if (!IsValid()) { + return nullptr; + } + + if (!surface_) { + FML_LOG(ERROR) << "Aborting snapshot because of on-screen surface " + "acquisition failure."; + return nullptr; + } + + auto device_snapshot = surface_->makeImageSnapshot(); + + if (!device_snapshot) { + FML_LOG(ERROR) << "Could not create the device snapshot while attempting " + "to snapshot the Metal surface."; + return nullptr; + } + + auto host_snapshot = device_snapshot->makeRasterImage(); + + if (!host_snapshot) { + FML_LOG(ERROR) << "Could not create the host snapshot while attempting to " + "snapshot the Metal surface."; + return nullptr; + } + + return host_snapshot; +} + // |TestMetalSurface| TestMetalSurfaceImpl::~TestMetalSurfaceImpl() = default; @@ -107,10 +109,12 @@ TestMetalSurfaceImpl::~TestMetalSurfaceImpl() = default; bool TestMetalSurfaceImpl::IsValid() const { return is_valid_; } + // |TestMetalSurface| sk_sp TestMetalSurfaceImpl::GetGrContext() const { - return IsValid() ? context_ : nullptr; + return IsValid() ? test_metal_context_.GetSkiaContext() : nullptr; } + // |TestMetalSurface| sk_sp TestMetalSurfaceImpl::GetSurface() const { return IsValid() ? surface_ : nullptr; diff --git a/engine/src/flutter/testing/test_metal_surface_unittests.cc b/engine/src/flutter/testing/test_metal_surface_unittests.cc index 5765e6e656..daf53170dc 100644 --- a/engine/src/flutter/testing/test_metal_surface_unittests.cc +++ b/engine/src/flutter/testing/test_metal_surface_unittests.cc @@ -2,18 +2,22 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "flutter/testing/test_metal_context.h" #include "flutter/testing/test_metal_surface.h" #include "flutter/testing/testing.h" namespace flutter { namespace testing { +#ifdef SHELL_ENABLE_METAL + TEST(TestMetalSurface, EmptySurfaceIsInvalid) { if (!TestMetalSurface::PlatformSupportsMetal()) { GTEST_SKIP(); } - auto surface = TestMetalSurface::Create(); + TestMetalContext metal_context = TestMetalContext(); + auto surface = TestMetalSurface::Create(metal_context); ASSERT_NE(surface, nullptr); ASSERT_FALSE(surface->IsValid()); } @@ -23,12 +27,16 @@ TEST(TestMetalSurface, CanCreateValidTestMetalSurface) { GTEST_SKIP(); } - auto surface = TestMetalSurface::Create(SkISize::Make(100, 100)); + TestMetalContext metal_context = TestMetalContext(); + auto surface = + TestMetalSurface::Create(metal_context, SkISize::Make(100, 100)); ASSERT_NE(surface, nullptr); ASSERT_TRUE(surface->IsValid()); ASSERT_NE(surface->GetSurface(), nullptr); ASSERT_NE(surface->GetGrContext(), nullptr); } +#endif + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/tools/gn b/engine/src/flutter/tools/gn index e65a60abed..9ba1b15604 100755 --- a/engine/src/flutter/tools/gn +++ b/engine/src/flutter/tools/gn @@ -247,6 +247,18 @@ def to_gn_args(args): # attributes in release modes till the toolchain is updated. gn_args['skia_enable_api_available_macro'] = args.runtime_mode != "release" + if sys.platform == 'darwin' and args.macos_enable_metal: + # OpenGL is deprecated on macOS > 10.11. + # This is not neccessarily needed but enabling this until we have a way to + # build a macOS metal only shell and a gl only shell. + gn_args['allow_deprecated_api_calls'] = True + gn_args['skia_use_metal'] = True + gn_args['shell_enable_metal'] = True + # Skia has Metal support on macOS version >= 10.14. + MACOS_SKIA_METAL_SUPPORTED_MIN_VERSION = '10.14' + gn_args['mac_sdk_min'] = MACOS_SKIA_METAL_SUPPORTED_MIN_VERSION + gn_args['mac_deployment_target'] = MACOS_SKIA_METAL_SUPPORTED_MIN_VERSION + if args.enable_vulkan: # Enable vulkan in the Flutter shell. gn_args['shell_enable_vulkan'] = True @@ -365,6 +377,7 @@ def parse_args(args): parser.add_argument('--target-triple', type=str) parser.add_argument('--operator-new-alignment', dest='operator_new_alignment', type=str, default=None) + parser.add_argument('--macos-enable-metal', action='store_true', default=False) parser.add_argument('--enable-vulkan', action='store_true', default=False) parser.add_argument('--enable-fontconfig', action='store_true', default=False)