diff --git a/engine/src/flutter/shell/common/BUILD.gn b/engine/src/flutter/shell/common/BUILD.gn index c2821e2a4b..3ed3e929cb 100644 --- a/engine/src/flutter/shell/common/BUILD.gn +++ b/engine/src/flutter/shell/common/BUILD.gn @@ -3,6 +3,7 @@ # found in the LICENSE file. import("$flutter_root/testing/testing.gni") +import("$flutter_root/shell/gpu/gpu.gni") # Template to generate a dart embedder resource.cc file. # Required invoker inputs: @@ -107,8 +108,8 @@ source_set("common") { public_deps = [ "$flutter_root/shell/version", "$flutter_root/third_party/txt", - "//third_party/tonic", "//third_party/rapidjson", + "//third_party/tonic", ] public_configs = [ "$flutter_root:config" ] @@ -139,12 +140,20 @@ template("shell_host_executable") { } } +shell_gpu_configuration("shell_unittests_gpu_configuration") { + enable_software = true + enable_vulkan = false + enable_gl = false +} + shell_host_executable("shell_unittests") { sources = [ "shell_unittests.cc", ] deps = [ + ":shell_unittests_gpu_configuration", + "$flutter_root/shell/", "$flutter_root/testing", ] } diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index 6c1cc66c9f..91e0cf6f12 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -14,6 +14,7 @@ #include "flutter/shell/common/rasterizer.h" #include "flutter/shell/common/shell.h" #include "flutter/shell/common/thread_host.h" +#include "flutter/shell/gpu/gpu_surface_software.h" #include "gtest/gtest.h" #define CURRENT_TEST_NAME \ @@ -23,6 +24,64 @@ namespace shell { +class TestPlatformView : public PlatformView, + public GPUSurfaceSoftwareDelegate { + public: + TestPlatformView(PlatformView::Delegate& delegate, + blink::TaskRunners task_runners) + : PlatformView(delegate, std::move(task_runners)) {} + + private: + // |PlatformView| + std::unique_ptr CreateRenderingSurface() override { + return std::make_unique(this); + } + + // |GPUSurfaceSoftwareDelegate| + virtual sk_sp AcquireBackingStore(const SkISize& size) override { + return SkSurface::MakeRasterN32Premul(size.width(), size.height()); + } + + // |GPUSurfaceSoftwareDelegate| + virtual bool PresentBackingStore(sk_sp backing_store) override { + return true; + } + + FML_DISALLOW_COPY_AND_ASSIGN(TestPlatformView); +}; + +static bool ValidateShell(Shell* shell) { + if (!shell) { + return false; + } + + if (!shell->IsSetup()) { + return false; + } + + { + fml::AutoResetWaitableEvent latch; + fml::TaskRunner::RunNowOrPostTask( + shell->GetTaskRunners().GetPlatformTaskRunner(), [shell, &latch]() { + shell->GetPlatformView()->NotifyCreated(); + latch.Signal(); + }); + latch.Wait(); + } + + { + fml::AutoResetWaitableEvent latch; + fml::TaskRunner::RunNowOrPostTask( + shell->GetTaskRunners().GetPlatformTaskRunner(), [shell, &latch]() { + shell->GetPlatformView()->NotifyDestroyed(); + latch.Signal(); + }); + latch.Wait(); + } + + return true; +} + TEST(ShellTest, InitializeWithInvalidThreads) { blink::Settings settings = {}; settings.task_observer_add = [](intptr_t, fml::closure) {}; @@ -31,7 +90,8 @@ TEST(ShellTest, InitializeWithInvalidThreads) { auto shell = Shell::Create( std::move(task_runners), settings, [](Shell& shell) { - return std::make_unique(shell, shell.GetTaskRunners()); + return std::make_unique(shell, + shell.GetTaskRunners()); }, [](Shell& shell) { return std::make_unique(shell.GetTaskRunners()); @@ -54,12 +114,13 @@ TEST(ShellTest, InitializeWithDifferentThreads) { auto shell = Shell::Create( std::move(task_runners), settings, [](Shell& shell) { - return std::make_unique(shell, shell.GetTaskRunners()); + return std::make_unique(shell, + shell.GetTaskRunners()); }, [](Shell& shell) { return std::make_unique(shell.GetTaskRunners()); }); - ASSERT_TRUE(shell); + ASSERT_TRUE(ValidateShell(shell.get())); } TEST(ShellTest, InitializeWithSingleThread) { @@ -74,12 +135,13 @@ TEST(ShellTest, InitializeWithSingleThread) { auto shell = Shell::Create( std::move(task_runners), settings, [](Shell& shell) { - return std::make_unique(shell, shell.GetTaskRunners()); + return std::make_unique(shell, + shell.GetTaskRunners()); }, [](Shell& shell) { return std::make_unique(shell.GetTaskRunners()); }); - ASSERT_TRUE(shell); + ASSERT_TRUE(ValidateShell(shell.get())); } TEST(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) { @@ -93,12 +155,13 @@ TEST(ShellTest, InitializeWithSingleThreadWhichIsTheCallingThread) { auto shell = Shell::Create( std::move(task_runners), settings, [](Shell& shell) { - return std::make_unique(shell, shell.GetTaskRunners()); + return std::make_unique(shell, + shell.GetTaskRunners()); }, [](Shell& shell) { return std::make_unique(shell.GetTaskRunners()); }); - ASSERT_TRUE(shell); + ASSERT_TRUE(ValidateShell(shell.get())); } TEST(ShellTest, InitializeWithMultipleThreadButCallingThreadAsPlatformThread) { @@ -117,12 +180,41 @@ TEST(ShellTest, InitializeWithMultipleThreadButCallingThreadAsPlatformThread) { auto shell = Shell::Create( std::move(task_runners), settings, [](Shell& shell) { - return std::make_unique(shell, shell.GetTaskRunners()); + return std::make_unique(shell, + shell.GetTaskRunners()); }, [](Shell& shell) { return std::make_unique(shell.GetTaskRunners()); }); - ASSERT_TRUE(shell); + ASSERT_TRUE(ValidateShell(shell.get())); +} + +// Reported in Bug: Engine deadlocks when gpu and platforms threads are the same +// #21398 (https://github.com/flutter/flutter/issues/21398) +TEST(ShellTest, DISABLED_InitializeWithGPUAndPlatformThreadsTheSame) { + blink::Settings settings = {}; + settings.task_observer_add = [](intptr_t, fml::closure) {}; + settings.task_observer_remove = [](intptr_t) {}; + ThreadHost thread_host( + "io.flutter.test." + CURRENT_TEST_NAME + ".", + ThreadHost::Type::Platform | ThreadHost::Type::IO | ThreadHost::Type::UI); + blink::TaskRunners task_runners( + "test", + thread_host.platform_thread->GetTaskRunner(), // platform + thread_host.platform_thread->GetTaskRunner(), // gpu + thread_host.ui_thread->GetTaskRunner(), // ui + thread_host.io_thread->GetTaskRunner() // io + ); + auto shell = Shell::Create( + std::move(task_runners), settings, + [](Shell& shell) { + return std::make_unique(shell, + shell.GetTaskRunners()); + }, + [](Shell& shell) { + return std::make_unique(shell.GetTaskRunners()); + }); + ASSERT_TRUE(ValidateShell(shell.get())); } } // namespace shell diff --git a/engine/src/flutter/shell/gpu/BUILD.gn b/engine/src/flutter/shell/gpu/BUILD.gn new file mode 100644 index 0000000000..2ad78b45d7 --- /dev/null +++ b/engine/src/flutter/shell/gpu/BUILD.gn @@ -0,0 +1,46 @@ +# Copyright 2018 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. + +import("$flutter_root/shell/config.gni") + +gpu_dir = "$flutter_root/shell/gpu" + +gpu_common_deps = [ + "$flutter_root/common", + "$flutter_root/flow", + "$flutter_root/fml", + "$flutter_root/shell/common", + "$flutter_root/synchronization", + "//third_party/skia", +] + +source_set("gpu_surface_software") { + sources = [ + "$gpu_dir/gpu_surface_software.cc", + "$gpu_dir/gpu_surface_software.h", + ] + + deps = gpu_common_deps +} + +source_set("gpu_surface_gl") { + sources = [ + "$gpu_dir/gpu_surface_gl.cc", + "$gpu_dir/gpu_surface_gl.h", + ] + + deps = gpu_common_deps + [ "//third_party/skia:gpu" ] +} + +source_set("gpu_surface_vulkan") { + sources = [ + "$gpu_dir/gpu_surface_vulkan.cc", + "$gpu_dir/gpu_surface_vulkan.h", + ] + + deps = gpu_common_deps + [ + "//third_party/skia:gpu", + "$flutter_root/vulkan", + ] +} diff --git a/engine/src/flutter/shell/gpu/gpu.gni b/engine/src/flutter/shell/gpu/gpu.gni index 48cb476017..28eb061aea 100644 --- a/engine/src/flutter/shell/gpu/gpu.gni +++ b/engine/src/flutter/shell/gpu/gpu.gni @@ -12,51 +12,19 @@ template("shell_gpu_configuration") { assert(defined(invoker.enable_gl), "Caller must declare if the Open GL backend must be enabled.") - source_set(target_name) { - # vulkan_backend_enabled = shell_enable_vulkan - # gl_backend_enabled = !is_fuchsia - # software_backend_enabled = true - - sources = [] - - gpu_dir = "$flutter_root/shell/gpu" + group(target_name) { + public_deps = [] if (invoker.enable_software) { - sources += [ - "$gpu_dir/gpu_surface_software.cc", - "$gpu_dir/gpu_surface_software.h", - ] + public_deps += [ "$flutter_root/shell/gpu:gpu_surface_software" ] } if (invoker.enable_gl) { - sources += [ - "$gpu_dir/gpu_surface_gl.cc", - "$gpu_dir/gpu_surface_gl.h", - ] + public_deps += [ "$flutter_root/shell/gpu:gpu_surface_gl" ] } if (invoker.enable_vulkan) { - sources += [ - "$gpu_dir/gpu_surface_vulkan.cc", - "$gpu_dir/gpu_surface_vulkan.h", - ] - } - - deps = [ - "$flutter_root/common", - "$flutter_root/flow", - "$flutter_root/fml", - "$flutter_root/shell/common", - "$flutter_root/synchronization", - "//third_party/skia", - ] - - if (invoker.enable_vulkan || invoker.enable_gl) { - deps += [ "//third_party/skia:gpu" ] - } - - if (invoker.enable_vulkan) { - deps += [ "$flutter_root/vulkan" ] + public_deps += [ "$flutter_root/shell/gpu:gpu_surface_vulkan" ] } } }