diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 14e30b096fc11..761773e7a4b5e 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -536,20 +536,16 @@ FILE: ../../../flutter/shell/common/animator.h FILE: ../../../flutter/shell/common/animator_unittests.cc FILE: ../../../flutter/shell/common/canvas_spy.cc FILE: ../../../flutter/shell/common/canvas_spy.h -FILE: ../../../flutter/shell/common/canvas_spy_unittests.cc FILE: ../../../flutter/shell/common/engine.cc FILE: ../../../flutter/shell/common/engine.h FILE: ../../../flutter/shell/common/fixtures/shell_test.dart FILE: ../../../flutter/shell/common/fixtures/shelltest_screenshot.png -FILE: ../../../flutter/shell/common/input_events_unittests.cc FILE: ../../../flutter/shell/common/isolate_configuration.cc FILE: ../../../flutter/shell/common/isolate_configuration.h FILE: ../../../flutter/shell/common/persistent_cache.cc FILE: ../../../flutter/shell/common/persistent_cache.h -FILE: ../../../flutter/shell/common/persistent_cache_unittests.cc FILE: ../../../flutter/shell/common/pipeline.cc FILE: ../../../flutter/shell/common/pipeline.h -FILE: ../../../flutter/shell/common/pipeline_unittests.cc FILE: ../../../flutter/shell/common/platform_view.cc FILE: ../../../flutter/shell/common/platform_view.h FILE: ../../../flutter/shell/common/pointer_data_dispatcher.cc @@ -563,9 +559,6 @@ FILE: ../../../flutter/shell/common/shell.h FILE: ../../../flutter/shell/common/shell_benchmarks.cc FILE: ../../../flutter/shell/common/shell_io_manager.cc FILE: ../../../flutter/shell/common/shell_io_manager.h -FILE: ../../../flutter/shell/common/shell_test.cc -FILE: ../../../flutter/shell/common/shell_test.h -FILE: ../../../flutter/shell/common/shell_unittests.cc FILE: ../../../flutter/shell/common/skia_event_tracer_impl.cc FILE: ../../../flutter/shell/common/skia_event_tracer_impl.h FILE: ../../../flutter/shell/common/surface.cc @@ -578,8 +571,6 @@ FILE: ../../../flutter/shell/common/vsync_waiter.cc FILE: ../../../flutter/shell/common/vsync_waiter.h FILE: ../../../flutter/shell/common/vsync_waiter_fallback.cc FILE: ../../../flutter/shell/common/vsync_waiter_fallback.h -FILE: ../../../flutter/shell/common/vsync_waiters_test.cc -FILE: ../../../flutter/shell/common/vsync_waiters_test.h FILE: ../../../flutter/shell/gpu/gpu_surface_delegate.h FILE: ../../../flutter/shell/gpu/gpu_surface_gl.cc FILE: ../../../flutter/shell/gpu/gpu_surface_gl.h diff --git a/shell/common/BUILD.gn b/shell/common/BUILD.gn index 373a657b9205d..c79ffc5b04523 100644 --- a/shell/common/BUILD.gn +++ b/shell/common/BUILD.gn @@ -2,6 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("$flutter_root/shell/config.gni") import("$flutter_root/shell/gpu/gpu.gni") import("$flutter_root/testing/testing.gni") @@ -158,17 +159,30 @@ if (current_toolchain == host_toolchain) { } shell_host_executable("shell_unittests") { + defines = [] + + if (shell_enable_metal) { + defines += [ "SHELL_ENABLE_METAL" ] + } + sources = [ - "animator_unittests.cc", - "canvas_spy_unittests.cc", - "input_events_unittests.cc", - "persistent_cache_unittests.cc", - "pipeline_unittests.cc", - "shell_test.cc", - "shell_test.h", - "shell_unittests.cc", - "vsync_waiters_test.cc", - "vsync_waiters_test.h", + "tests/canvas_spy_unittests.cc", + "tests/input_events_unittests.cc", + "tests/persistent_cache_unittests.cc", + "tests/pipeline_unittests.cc", + "tests/shell_test.cc", + "tests/shell_test.h", + "tests/shell_test_platform_view.cc", + "tests/shell_test_platform_view.h", + "tests/shell_test_surface.cc", + "tests/shell_test_surface.h", + "tests/shell_test_surface_gl.cc", + "tests/shell_test_surface_gl.h", + "tests/shell_test_surface_metal.cc", + "tests/shell_test_surface_metal.h", + "tests/shell_test_vsync_waiter.cc", + "tests/shell_test_vsync_waiter.h", + "tests/shell_unittests.cc", ] deps = [ @@ -180,8 +194,25 @@ if (current_toolchain == host_toolchain) { "$flutter_root/lib/ui:ui", "$flutter_root/shell", "$flutter_root/testing:dart", + "$flutter_root/testing:metal", "$flutter_root/testing:opengl", ] + + libs = [] + + if (shell_enable_metal) { + sources += [ + "tests/shell_test_surface_metal_impl.h", + "tests/shell_test_surface_metal_impl.mm", + ] + + deps += [ "//flutter/shell/gpu:gpu_surface_metal" ] + + libs += [ + "Foundation.framework", + "QuartzCore.framework", + ] + } } shell_host_executable("shell_benchmarks") { diff --git a/shell/common/canvas_spy_unittests.cc b/shell/common/tests/canvas_spy_unittests.cc similarity index 97% rename from shell/common/canvas_spy_unittests.cc rename to shell/common/tests/canvas_spy_unittests.cc index 0c64476a6b390..5ae227ca2b9fb 100644 --- a/shell/common/canvas_spy_unittests.cc +++ b/shell/common/tests/canvas_spy_unittests.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "canvas_spy.h" +#include "flutter/shell/common/canvas_spy.h" #include "gtest/gtest.h" #include "third_party/skia/include/core/SkPictureRecorder.h" #include "third_party/skia/include/core/SkSurface.h" diff --git a/shell/common/input_events_unittests.cc b/shell/common/tests/input_events_unittests.cc similarity index 99% rename from shell/common/input_events_unittests.cc rename to shell/common/tests/input_events_unittests.cc index 3e08663fff90e..24be6427eda4b 100644 --- a/shell/common/input_events_unittests.cc +++ b/shell/common/tests/input_events_unittests.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/common/shell_test.h" +#include "flutter/shell/common/tests/shell_test.h" #include "flutter/testing/testing.h" namespace flutter { diff --git a/shell/common/persistent_cache_unittests.cc b/shell/common/tests/persistent_cache_unittests.cc similarity index 98% rename from shell/common/persistent_cache_unittests.cc rename to shell/common/tests/persistent_cache_unittests.cc index 4948a3359bc69..38987a3b57fad 100644 --- a/shell/common/persistent_cache_unittests.cc +++ b/shell/common/tests/persistent_cache_unittests.cc @@ -12,8 +12,8 @@ #include "flutter/fml/file.h" #include "flutter/fml/unique_fd.h" #include "flutter/shell/common/persistent_cache.h" -#include "flutter/shell/common/shell_test.h" #include "flutter/shell/common/switches.h" +#include "flutter/shell/common/tests/shell_test.h" #include "flutter/testing/testing.h" #include "include/core/SkPicture.h" diff --git a/shell/common/pipeline_unittests.cc b/shell/common/tests/pipeline_unittests.cc similarity index 100% rename from shell/common/pipeline_unittests.cc rename to shell/common/tests/pipeline_unittests.cc diff --git a/shell/common/shell_test.cc b/shell/common/tests/shell_test.cc similarity index 78% rename from shell/common/shell_test.cc rename to shell/common/tests/shell_test.cc index 2557d8ca73bed..11e1c1b64082a 100644 --- a/shell/common/shell_test.cc +++ b/shell/common/tests/shell_test.cc @@ -4,16 +4,11 @@ #define FML_USED_ON_EMBEDDER -#include "flutter/shell/common/shell_test.h" +#include "flutter/shell/common/tests/shell_test.h" -#include "flutter/flow/layers/layer_tree.h" #include "flutter/flow/layers/transform_layer.h" -#include "flutter/fml/make_copyable.h" -#include "flutter/fml/mapping.h" -#include "flutter/runtime/dart_vm.h" +#include "flutter/shell/common/tests/shell_test_platform_view.h" #include "flutter/shell/common/vsync_waiter_fallback.h" -#include "flutter/shell/gpu/gpu_surface_gl.h" -#include "flutter/testing/testing.h" namespace flutter { namespace testing { @@ -250,35 +245,66 @@ TaskRunners ShellTest::GetTaskRunnersForFixture() { }; } +static std::unique_ptr CreateTestShell( + Settings settings, + TaskRunners task_runners, + bool simulate_vsync, + ShellTestSurface::ClientRenderingAPI api) { + auto vsync_clock = std::make_shared(); + CreateVsyncWaiter on_create_vsync_waiter = [&]() { + std::unique_ptr waiter; + if (simulate_vsync) { + waiter = + std::make_unique(task_runners, vsync_clock); + } else { + waiter = std::make_unique(task_runners); + } + return waiter; + }; + + Shell::CreateCallback on_create_platform_view = + [&](Shell& shell) { + return std::make_unique( + shell, shell.GetTaskRunners(), vsync_clock, on_create_vsync_waiter, + api); + }; + + Shell::CreateCallback on_create_rasterizer = [](Shell& shell) { + return std::make_unique(shell, shell.GetTaskRunners()); + }; + + return Shell::Create(task_runners, // + settings, // + on_create_platform_view, // + on_create_rasterizer // + ); +} + +std::unique_ptr ShellTest::CreateShell( + Settings settings, + ShellTestSurface::ClientRenderingAPI api) { + return CreateTestShell(settings, GetTaskRunnersForFixture(), false, api); +} + std::unique_ptr ShellTest::CreateShell(Settings settings, bool simulate_vsync) { - return CreateShell(std::move(settings), GetTaskRunnersForFixture(), - simulate_vsync); + return CreateTestShell( + settings, // + GetTaskRunnersForFixture(), // + simulate_vsync, // + ShellTestSurface::ClientRenderingAPI::kClientRenderingAPIOpenGL // + ); } std::unique_ptr ShellTest::CreateShell(Settings settings, TaskRunners task_runners, bool simulate_vsync) { - const auto vsync_clock = std::make_shared(); - CreateVsyncWaiter create_vsync_waiter = [&]() { - if (simulate_vsync) { - return static_cast>( - std::make_unique(task_runners, vsync_clock)); - } else { - return static_cast>( - std::make_unique(task_runners)); - } - }; - return Shell::Create( - task_runners, settings, - [vsync_clock, &create_vsync_waiter](Shell& shell) { - return std::make_unique( - shell, shell.GetTaskRunners(), vsync_clock, - std::move(create_vsync_waiter)); - }, - [](Shell& shell) { - return std::make_unique(shell, shell.GetTaskRunners()); - }); + return CreateTestShell( + settings, // + task_runners, // + simulate_vsync, // + ShellTestSurface::ClientRenderingAPI::kClientRenderingAPIOpenGL // + ); } void ShellTest::DestroyShell(std::unique_ptr shell) { @@ -301,70 +327,5 @@ void ShellTest::AddNativeCallback(std::string name, native_resolver_->AddNativeCallback(std::move(name), callback); } -ShellTestPlatformView::ShellTestPlatformView( - PlatformView::Delegate& delegate, - TaskRunners task_runners, - std::shared_ptr vsync_clock, - CreateVsyncWaiter create_vsync_waiter) - : PlatformView(delegate, std::move(task_runners)), - gl_surface_(SkISize::Make(800, 600)), - create_vsync_waiter_(std::move(create_vsync_waiter)), - vsync_clock_(vsync_clock) {} - -ShellTestPlatformView::~ShellTestPlatformView() = default; - -std::unique_ptr ShellTestPlatformView::CreateVSyncWaiter() { - return create_vsync_waiter_(); -} - -void ShellTestPlatformView::SimulateVSync() { - vsync_clock_->SimulateVSync(); -} - -// |PlatformView| -std::unique_ptr ShellTestPlatformView::CreateRenderingSurface() { - return std::make_unique(this, true); -} - -// |PlatformView| -PointerDataDispatcherMaker ShellTestPlatformView::GetDispatcherMaker() { - return [](DefaultPointerDataDispatcher::Delegate& delegate) { - return std::make_unique(delegate); - }; -} - -// |GPUSurfaceGLDelegate| -bool ShellTestPlatformView::GLContextMakeCurrent() { - return gl_surface_.MakeCurrent(); -} - -// |GPUSurfaceGLDelegate| -bool ShellTestPlatformView::GLContextClearCurrent() { - return gl_surface_.ClearCurrent(); -} - -// |GPUSurfaceGLDelegate| -bool ShellTestPlatformView::GLContextPresent() { - return gl_surface_.Present(); -} - -// |GPUSurfaceGLDelegate| -intptr_t ShellTestPlatformView::GLContextFBO() const { - return gl_surface_.GetFramebuffer(); -} - -// |GPUSurfaceGLDelegate| -GPUSurfaceGLDelegate::GLProcResolver ShellTestPlatformView::GetGLProcResolver() - const { - return [surface = &gl_surface_](const char* name) -> void* { - return surface->GetProcAddress(name); - }; -} - -// |GPUSurfaceGLDelegate| -ExternalViewEmbedder* ShellTestPlatformView::GetExternalViewEmbedder() { - return nullptr; -} - } // namespace testing } // namespace flutter diff --git a/shell/common/shell_test.h b/shell/common/tests/shell_test.h similarity index 68% rename from shell/common/shell_test.h rename to shell/common/tests/shell_test.h index ddbbc94ae1899..3df8b3cbb6a68 100644 --- a/shell/common/shell_test.h +++ b/shell/common/tests/shell_test.h @@ -7,18 +7,18 @@ #include -#include "flutter/common/settings.h" +#include "flutter/common/task_runners.h" #include "flutter/flow/layers/container_layer.h" #include "flutter/fml/macros.h" -#include "flutter/lib/ui/window/platform_message.h" -#include "flutter/shell/common/run_configuration.h" +#include "flutter/fml/unique_fd.h" +#include "flutter/lib/ui/window/pointer_data_packet.h" #include "flutter/shell/common/shell.h" +#include "flutter/shell/common/tests/shell_test_surface.h" #include "flutter/shell/common/thread_host.h" -#include "flutter/shell/common/vsync_waiters_test.h" -#include "flutter/shell/gpu/gpu_surface_gl_delegate.h" #include "flutter/testing/test_dart_native_resolver.h" -#include "flutter/testing/test_gl_surface.h" +#include "flutter/testing/testing.h" #include "flutter/testing/thread_test.h" +#include "flutter/third_party/txt/src/txt/font_collection.h" namespace flutter { namespace testing { @@ -28,13 +28,21 @@ class ShellTest : public ThreadTest { ShellTest(); Settings CreateSettingsForFixture(); + + std::unique_ptr CreateShell(Settings settings, + ShellTestSurface::ClientRenderingAPI api); + std::unique_ptr CreateShell(Settings settings, bool simulate_vsync = false); + std::unique_ptr CreateShell(Settings settings, TaskRunners task_runners, bool simulate_vsync = false); + void DestroyShell(std::unique_ptr shell); + void DestroyShell(std::unique_ptr shell, TaskRunners task_runners); + TaskRunners GetTaskRunnersForFixture(); void SendEnginePlatformMessage(Shell* shell, @@ -44,7 +52,9 @@ class ShellTest : public ThreadTest { static void PlatformViewNotifyCreated( Shell* shell); // This creates the surface + static void RunEngine(Shell* shell, RunConfiguration configuration); + static void RestartEngine(Shell* shell, RunConfiguration configuration); /// Issue as many VSYNC as needed to flush the UI tasks so far, and reset @@ -63,6 +73,7 @@ class ShellTest : public ThreadTest { flutter::ViewportMetrics viewport_metrics, LayerTreeBuilder = {}); static void DispatchFakePointerData(Shell* shell); + static void DispatchPointerData(Shell* shell, std::unique_ptr packet); // Declare |UnreportedTimingsCount|, |GetNeedsReportTimings| and @@ -70,6 +81,7 @@ class ShellTest : public ThreadTest { // declarations as shell unit tests and Shell are in different name spaces. static bool GetNeedsReportTimings(Shell* shell); + static void SetNeedsReportTimings(Shell* shell, bool value); std::shared_ptr GetFontCollection(Shell* shell); @@ -80,61 +92,13 @@ class ShellTest : public ThreadTest { static int UnreportedTimingsCount(Shell* shell); private: - void SetSnapshotsAndAssets(Settings& settings); - std::shared_ptr native_resolver_; ThreadHost thread_host_; fml::UniqueFD assets_dir_; - FML_DISALLOW_COPY_AND_ASSIGN(ShellTest); -}; - -class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate { - public: - ShellTestPlatformView(PlatformView::Delegate& delegate, - TaskRunners task_runners, - std::shared_ptr vsync_clock, - CreateVsyncWaiter create_vsync_waiter); - - ~ShellTestPlatformView() override; - - void SimulateVSync(); - - private: - TestGLSurface gl_surface_; - - CreateVsyncWaiter create_vsync_waiter_; - - std::shared_ptr vsync_clock_; - - // |PlatformView| - std::unique_ptr CreateRenderingSurface() override; - - // |PlatformView| - std::unique_ptr CreateVSyncWaiter() override; - - // |PlatformView| - PointerDataDispatcherMaker GetDispatcherMaker() override; - - // |GPUSurfaceGLDelegate| - bool GLContextMakeCurrent() override; - - // |GPUSurfaceGLDelegate| - bool GLContextClearCurrent() override; - - // |GPUSurfaceGLDelegate| - bool GLContextPresent() override; - - // |GPUSurfaceGLDelegate| - intptr_t GLContextFBO() const override; - - // |GPUSurfaceGLDelegate| - GLProcResolver GetGLProcResolver() const override; - - // |GPUSurfaceGLDelegate| - ExternalViewEmbedder* GetExternalViewEmbedder() override; + void SetSnapshotsAndAssets(Settings& settings); - FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformView); + FML_DISALLOW_COPY_AND_ASSIGN(ShellTest); }; } // namespace testing diff --git a/shell/common/tests/shell_test_platform_view.cc b/shell/common/tests/shell_test_platform_view.cc new file mode 100644 index 0000000000000..e3fa75af78aae --- /dev/null +++ b/shell/common/tests/shell_test_platform_view.cc @@ -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. + +#include "flutter/shell/common/tests/shell_test_platform_view.h" + +namespace flutter { +namespace testing { + +ShellTestPlatformView::ShellTestPlatformView( + PlatformView::Delegate& delegate, + TaskRunners task_runners, + std::shared_ptr vsync_clock, + CreateVsyncWaiter create_vsync_waiter, + ShellTestSurface::ClientRenderingAPI api) + : PlatformView(delegate, std::move(task_runners)), + vsync_clock_(std::move(vsync_clock)), + create_vsync_waiter_(std::move(create_vsync_waiter)), + shell_surface_(ShellTestSurface::CreateTestSurface(api)) { + FML_CHECK(vsync_clock_); + FML_CHECK(create_vsync_waiter_); +} + +ShellTestPlatformView::~ShellTestPlatformView() = default; + +std::unique_ptr ShellTestPlatformView::CreateVSyncWaiter() { + return create_vsync_waiter_(); +} + +void ShellTestPlatformView::SimulateVSync() { + vsync_clock_->SimulateVSync(); +} + +// |PlatformView| +std::unique_ptr ShellTestPlatformView::CreateRenderingSurface() { + if (auto surface = shell_surface_->CreateRenderingSurface()) { + return surface; + } + FML_CHECK(false) << "Could not obtain the rendering surface."; + return nullptr; +} + +// |PlatformView| +PointerDataDispatcherMaker ShellTestPlatformView::GetDispatcherMaker() { + return [](DefaultPointerDataDispatcher::Delegate& delegate) { + return std::make_unique(delegate); + }; +} + +} // namespace testing +} // namespace flutter diff --git a/shell/common/tests/shell_test_platform_view.h b/shell/common/tests/shell_test_platform_view.h new file mode 100644 index 0000000000000..31ef7b624da3e --- /dev/null +++ b/shell/common/tests/shell_test_platform_view.h @@ -0,0 +1,49 @@ +// 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_COMMON_TESTS_SHELL_TEST_PLATFORM_VIEW_H_ +#define FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_PLATFORM_VIEW_H_ + +#include "flutter/fml/macros.h" +#include "flutter/shell/common/platform_view.h" +#include "flutter/shell/common/tests/shell_test_surface.h" +#include "flutter/shell/common/tests/shell_test_vsync_waiter.h" + +namespace flutter { +namespace testing { + +class ShellTestPlatformView : public PlatformView { + public: + ShellTestPlatformView(PlatformView::Delegate& delegate, + TaskRunners task_runners, + std::shared_ptr vsync_clock, + CreateVsyncWaiter create_vsync_waiter, + ShellTestSurface::ClientRenderingAPI api); + + // |PlatformView| + ~ShellTestPlatformView() override; + + void SimulateVSync(); + + private: + std::shared_ptr vsync_clock_; + CreateVsyncWaiter create_vsync_waiter_; + std::unique_ptr shell_surface_; + + // |PlatformView| + std::unique_ptr CreateRenderingSurface() override; + + // |PlatformView| + std::unique_ptr CreateVSyncWaiter() override; + + // |PlatformView| + PointerDataDispatcherMaker GetDispatcherMaker() override; + + FML_DISALLOW_COPY_AND_ASSIGN(ShellTestPlatformView); +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_PLATFORM_VIEW_H_ diff --git a/shell/common/tests/shell_test_surface.cc b/shell/common/tests/shell_test_surface.cc new file mode 100644 index 0000000000000..da52263388298 --- /dev/null +++ b/shell/common/tests/shell_test_surface.cc @@ -0,0 +1,40 @@ +// 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/common/tests/shell_test_surface.h" + +#include "flutter/shell/common/tests/shell_test_surface_gl.h" +#include "flutter/shell/common/tests/shell_test_surface_metal.h" +#include "flutter/testing/test_metal_surface.h" + +namespace flutter { +namespace testing { + +std::unique_ptr ShellTestSurface::CreateTestSurface( + ClientRenderingAPI api) { + switch (api) { + case ClientRenderingAPI::kClientRenderingAPIOpenGL: + // OpenGL is always available on all platform because the test harnesses + // emulate the same via SwiftShader. + return std::make_unique(); + case ClientRenderingAPI::kClientRenderingAPIMetal: + FML_CHECK(TestMetalSurface::PlatformSupportsMetal()) + << "Attempted to use or test a Metal feature on a platform where " + "Metal is not available and cannot be emulated. If not testing " + "anything Metal specific, pick a different client rendering API " + "(OpenGL recommended as it is emulated via SwiftShader). If " + "testing a Metal subsystem, skip the test conditionally on " + "non-Darwin platforms via GTEST_SKIP."; + return ShellTestSurfaceMetal::Create(); + } + FML_CHECK(false) << "Unhandled client rendering API."; + return nullptr; +} + +ShellTestSurface::ShellTestSurface() = default; + +ShellTestSurface::~ShellTestSurface() = default; + +} // namespace testing +} // namespace flutter diff --git a/shell/common/tests/shell_test_surface.h b/shell/common/tests/shell_test_surface.h new file mode 100644 index 0000000000000..16e28b479302e --- /dev/null +++ b/shell/common/tests/shell_test_surface.h @@ -0,0 +1,37 @@ +// 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_COMMON_TESTS_SHELL_TEST_SURFACE_H_ +#define FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_H_ + +#include "flutter/fml/macros.h" +#include "flutter/shell/common/surface.h" + +namespace flutter { +namespace testing { + +class ShellTestSurface { + public: + enum class ClientRenderingAPI { + kClientRenderingAPIOpenGL, + kClientRenderingAPIMetal, + }; + + static std::unique_ptr CreateTestSurface( + ClientRenderingAPI api); + + ShellTestSurface(); + + virtual ~ShellTestSurface(); + + virtual std::unique_ptr CreateRenderingSurface() = 0; + + private: + FML_DISALLOW_COPY_AND_ASSIGN(ShellTestSurface); +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_H_ diff --git a/shell/common/tests/shell_test_surface_gl.cc b/shell/common/tests/shell_test_surface_gl.cc new file mode 100644 index 0000000000000..9c99acd7b15b2 --- /dev/null +++ b/shell/common/tests/shell_test_surface_gl.cc @@ -0,0 +1,56 @@ +// 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/common/tests/shell_test_surface_gl.h" + +#include "flutter/shell/gpu/gpu_surface_gl.h" + +namespace flutter { +namespace testing { + +ShellTestSurfaceGL::ShellTestSurfaceGL() + : gl_surface_(SkISize::Make(800, 600)) {} + +ShellTestSurfaceGL::~ShellTestSurfaceGL() = default; + +// |GPUSurfaceGLDelegate| +bool ShellTestSurfaceGL::GLContextMakeCurrent() { + return gl_surface_.MakeCurrent(); +} + +// |GPUSurfaceGLDelegate| +bool ShellTestSurfaceGL::GLContextClearCurrent() { + return gl_surface_.ClearCurrent(); +} + +// |GPUSurfaceGLDelegate| +bool ShellTestSurfaceGL::GLContextPresent() { + return gl_surface_.Present(); +} + +// |GPUSurfaceGLDelegate| +intptr_t ShellTestSurfaceGL::GLContextFBO() const { + return gl_surface_.GetFramebuffer(); +} + +// |GPUSurfaceGLDelegate| +GPUSurfaceGLDelegate::GLProcResolver ShellTestSurfaceGL::GetGLProcResolver() + const { + return [surface = &gl_surface_](const char* name) -> void* { + return surface->GetProcAddress(name); + }; +} + +// |GPUSurfaceGLDelegate| +ExternalViewEmbedder* ShellTestSurfaceGL::GetExternalViewEmbedder() { + return nullptr; +} + +// |ShellTestSurface| +std::unique_ptr ShellTestSurfaceGL::CreateRenderingSurface() { + return std::make_unique(this, true); +} + +} // namespace testing +} // namespace flutter diff --git a/shell/common/tests/shell_test_surface_gl.h b/shell/common/tests/shell_test_surface_gl.h new file mode 100644 index 0000000000000..04dc89b30d91e --- /dev/null +++ b/shell/common/tests/shell_test_surface_gl.h @@ -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. + +#ifndef FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_GL_H_ +#define FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_GL_H_ + +#include "flutter/fml/macros.h" +#include "flutter/shell/common/tests/shell_test_surface.h" +#include "flutter/shell/gpu/gpu_surface_gl_delegate.h" +#include "flutter/testing/test_gl_surface.h" + +namespace flutter { +namespace testing { + +class ShellTestSurfaceGL : public ShellTestSurface, + public GPUSurfaceGLDelegate { + public: + ShellTestSurfaceGL(); + + virtual ~ShellTestSurfaceGL(); + + private: + TestGLSurface gl_surface_; + + // |ShellTestSurface| + std::unique_ptr CreateRenderingSurface() override; + + // |GPUSurfaceGLDelegate| + bool GLContextMakeCurrent() override; + + // |GPUSurfaceGLDelegate| + bool GLContextClearCurrent() override; + + // |GPUSurfaceGLDelegate| + bool GLContextPresent() override; + + // |GPUSurfaceGLDelegate| + intptr_t GLContextFBO() const override; + + // |GPUSurfaceGLDelegate| + GLProcResolver GetGLProcResolver() const override; + + // |GPUSurfaceGLDelegate| + ExternalViewEmbedder* GetExternalViewEmbedder() override; + + FML_DISALLOW_COPY_AND_ASSIGN(ShellTestSurfaceGL); +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_GL_H_ diff --git a/shell/common/tests/shell_test_surface_metal.cc b/shell/common/tests/shell_test_surface_metal.cc new file mode 100644 index 0000000000000..94c2b3f488c18 --- /dev/null +++ b/shell/common/tests/shell_test_surface_metal.cc @@ -0,0 +1,34 @@ +// 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/common/tests/shell_test_surface_metal.h" + +#include "flutter/shell/common/tests/shell_test_surface_metal_impl.h" + +namespace flutter { +namespace testing { + +std::unique_ptr ShellTestSurfaceMetal::Create() { + std::unique_ptr impl; +#if SHELL_ENABLE_METAL + impl = std::make_unique(); +#endif // SHELL_ENABLE_METAL + // Cannot use make_unique because of the private constructor. + return std::unique_ptr{ + new ShellTestSurfaceMetal(std::move(impl))}; +} + +ShellTestSurfaceMetal::ShellTestSurfaceMetal( + std::unique_ptr impl) + : impl_(std::move(impl)) {} + +ShellTestSurfaceMetal::~ShellTestSurfaceMetal() = default; + +// |ShellTestSurface| +std::unique_ptr ShellTestSurfaceMetal::CreateRenderingSurface() { + return impl_ ? impl_->CreateRenderingSurface() : nullptr; +} + +} // namespace testing +} // namespace flutter diff --git a/shell/common/tests/shell_test_surface_metal.h b/shell/common/tests/shell_test_surface_metal.h new file mode 100644 index 0000000000000..61e486535f1f0 --- /dev/null +++ b/shell/common/tests/shell_test_surface_metal.h @@ -0,0 +1,36 @@ +// 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_COMMON_TESTS_SHELL_TEST_SURFACE_METAL_H_ +#define FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_METAL_H_ + +#include + +#include "flutter/fml/macros.h" +#include "flutter/shell/common/tests/shell_test_surface.h" + +namespace flutter { +namespace testing { + +class ShellTestSurfaceMetal : public ShellTestSurface { + public: + static std::unique_ptr Create(); + + virtual ~ShellTestSurfaceMetal(); + + private: + std::unique_ptr impl_; + + ShellTestSurfaceMetal(std::unique_ptr impl); + + // |ShellTestSurface| + std::unique_ptr CreateRenderingSurface() override; + + FML_DISALLOW_COPY_AND_ASSIGN(ShellTestSurfaceMetal); +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_METAL_H_ diff --git a/shell/common/tests/shell_test_surface_metal_impl.h b/shell/common/tests/shell_test_surface_metal_impl.h new file mode 100644 index 0000000000000..db2a83c4b1320 --- /dev/null +++ b/shell/common/tests/shell_test_surface_metal_impl.h @@ -0,0 +1,35 @@ +// 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_COMMON_TESTS_SHELL_TEST_SURFACE_METAL_IMPL_H_ +#define FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_METAL_IMPL_H_ + +#include "flutter/fml/macros.h" +#include "flutter/shell/common/tests/shell_test_surface_metal.h" +#include "flutter/shell/gpu/gpu_surface_delegate.h" + +namespace flutter { +namespace testing { + +class ShellTestSurfaceMetalImpl : public ShellTestSurface, + public GPUSurfaceDelegate { + public: + ShellTestSurfaceMetalImpl(); + + ~ShellTestSurfaceMetalImpl(); + + private: + // |ShellTestSurface| + std::unique_ptr CreateRenderingSurface() override; + + // |GPUSurfaceDelegate| + ExternalViewEmbedder* GetExternalViewEmbedder() override; + + FML_DISALLOW_COPY_AND_ASSIGN(ShellTestSurfaceMetalImpl); +}; + +} // namespace testing +} // namespace flutter + +#endif // FLUTTER_SHELL_COMMON_TESTS_SHELL_TEST_SURFACE_METAL_IMPL_H_ diff --git a/shell/common/tests/shell_test_surface_metal_impl.mm b/shell/common/tests/shell_test_surface_metal_impl.mm new file mode 100644 index 0000000000000..99574f9d522b4 --- /dev/null +++ b/shell/common/tests/shell_test_surface_metal_impl.mm @@ -0,0 +1,30 @@ +// 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/common/tests/shell_test_surface_metal_impl.h" + +#include +#include + +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "flutter/shell/gpu/gpu_surface_metal.h" + +namespace flutter { +namespace testing { + +ShellTestSurfaceMetalImpl::ShellTestSurfaceMetalImpl() = default; + +ShellTestSurfaceMetalImpl::~ShellTestSurfaceMetalImpl() = default; + +std::unique_ptr ShellTestSurfaceMetalImpl::CreateRenderingSurface() { + fml::scoped_nsobject layer([[CAMetalLayer alloc] init]); + return std::make_unique(this, layer); +} + +ExternalViewEmbedder* ShellTestSurfaceMetalImpl::GetExternalViewEmbedder() { + return nullptr; +} + +} // namespace testing +} // namespace flutter diff --git a/shell/common/vsync_waiters_test.cc b/shell/common/tests/shell_test_vsync_waiter.cc similarity index 97% rename from shell/common/vsync_waiters_test.cc rename to shell/common/tests/shell_test_vsync_waiter.cc index b5437d0199454..ae2bb4354d005 100644 --- a/shell/common/vsync_waiters_test.cc +++ b/shell/common/tests/shell_test_vsync_waiter.cc @@ -4,7 +4,7 @@ #define FML_USED_ON_EMBEDDER -#include "flutter/shell/common/shell_test.h" +#include "flutter/shell/common/tests/shell_test_vsync_waiter.h" #include "flutter/flow/layers/layer_tree.h" #include "flutter/flow/layers/transform_layer.h" diff --git a/shell/common/vsync_waiters_test.h b/shell/common/tests/shell_test_vsync_waiter.h similarity index 100% rename from shell/common/vsync_waiters_test.h rename to shell/common/tests/shell_test_vsync_waiter.h diff --git a/shell/common/shell_unittests.cc b/shell/common/tests/shell_unittests.cc similarity index 95% rename from shell/common/shell_unittests.cc rename to shell/common/tests/shell_unittests.cc index 02b0edf6e0760..06d6005e69ebe 100644 --- a/shell/common/shell_unittests.cc +++ b/shell/common/tests/shell_unittests.cc @@ -21,8 +21,9 @@ #include "flutter/runtime/dart_vm.h" #include "flutter/shell/common/platform_view.h" #include "flutter/shell/common/rasterizer.h" -#include "flutter/shell/common/shell_test.h" #include "flutter/shell/common/switches.h" +#include "flutter/shell/common/tests/shell_test.h" +#include "flutter/shell/common/tests/shell_test_platform_view.h" #include "flutter/shell/common/thread_host.h" #include "flutter/shell/common/vsync_waiter_fallback.h" #include "flutter/testing/testing.h" @@ -126,15 +127,17 @@ TEST_F(ShellTest, auto shell = Shell::Create( std::move(task_runners), settings, [](Shell& shell) { - // This is unused in the platform view as we are not using the simulated - // vsync mechanism. We should have better DI in the tests. - const auto vsync_clock = std::make_shared(); + CreateVsyncWaiter on_create_vsync_waiter = [&]() { + return static_cast>( + std::make_unique(shell.GetTaskRunners())); + }; return std::make_unique( - shell, shell.GetTaskRunners(), vsync_clock, - [task_runners = shell.GetTaskRunners()]() { - return static_cast>( - std::make_unique(task_runners)); - }); + shell, // + shell.GetTaskRunners(), // + std::make_shared(), // + on_create_vsync_waiter, // + ShellTestSurface::ClientRenderingAPI::kClientRenderingAPIOpenGL // + ); }, [](Shell& shell) { return std::make_unique(shell, shell.GetTaskRunners()); @@ -1127,5 +1130,35 @@ TEST_F(ShellTest, CanDecompressImageFromAsset) { DestroyShell(std::move(shell)); } +TEST_F(ShellTest, CanDecompressImageFromAssetWithMetalRasterizer) { + fml::AutoResetWaitableEvent latch; + AddNativeCallback("NotifyWidthHeight", CREATE_NATIVE_ENTRY([&](auto args) { + auto width = tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 0)); + auto height = tonic::DartConverter::FromDart( + Dart_GetNativeArgument(args, 1)); + ASSERT_EQ(width, 100); + ASSERT_EQ(height, 100); + latch.Signal(); + })); + + AddNativeCallback( + "GetFixtureImage", CREATE_NATIVE_ENTRY([](auto args) { + auto fixture = OpenFixtureAsMapping("shelltest_screenshot.png"); + tonic::DartConverter::SetReturnValue( + args, fixture); + })); + + auto settings = CreateSettingsForFixture(); + auto configuration = RunConfiguration::InferFromSettings(settings); + configuration.SetEntrypoint("canDecompressImageFromAsset"); + std::unique_ptr shell = CreateShell( + settings, ShellTestSurface::ClientRenderingAPI::kClientRenderingAPIMetal); + ASSERT_NE(shell.get(), nullptr); + RunEngine(shell.get(), std::move(configuration)); + latch.Wait(); + DestroyShell(std::move(shell)); +} + } // namespace testing } // namespace flutter diff --git a/shell/gpu/gpu_surface_delegate.h b/shell/gpu/gpu_surface_delegate.h index 4f9f16fdbb436..e1c4f2875f8ec 100644 --- a/shell/gpu/gpu_surface_delegate.h +++ b/shell/gpu/gpu_surface_delegate.h @@ -1,3 +1,7 @@ +// 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_GPU_GPU_SURFACE_DELEGATE_H_ #define FLUTTER_SHELL_GPU_GPU_SURFACE_DELEGATE_H_ diff --git a/shell/gpu/gpu_surface_metal.h b/shell/gpu/gpu_surface_metal.h index fc6b0964766ce..e8942694fea52 100644 --- a/shell/gpu/gpu_surface_metal.h +++ b/shell/gpu/gpu_surface_metal.h @@ -20,6 +20,7 @@ namespace flutter { class GPUSurfaceMetal : public Surface { public: GPUSurfaceMetal(GPUSurfaceDelegate* delegate, fml::scoped_nsobject layer); + GPUSurfaceMetal(GPUSurfaceDelegate* delegate, sk_sp gr_context, fml::scoped_nsobject layer);