diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index b76cc6560e..2450e4a369 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -1538,8 +1538,11 @@ TEST_F(ShellTest, SetResourceCacheSize) { PumpOneFrame(shell.get()); // The Vulkan and GL backends set different default values for the resource - // cache size. -#ifdef SHELL_ENABLE_VULKAN + // cache size. The default backend (specified by the default param of + // `CreateShell` in this test) will only resolve to Vulkan (in + // `ShellTestPlatformView::Create`) if GL is disabled. This situation arises + // when targeting the Fuchsia Emulator. +#if defined(SHELL_ENABLE_VULKAN) && !defined(SHELL_ENABLE_GL) EXPECT_EQ(GetRasterizerResourceCacheBytesSync(*shell), vulkan::kGrCacheMaxByteSize); #else diff --git a/engine/src/flutter/shell/config.gni b/engine/src/flutter/shell/config.gni index 781727443e..85dd360cbc 100644 --- a/engine/src/flutter/shell/config.gni +++ b/engine/src/flutter/shell/config.gni @@ -4,16 +4,19 @@ declare_args() { shell_enable_gl = !is_fuchsia - shell_enable_metal = false - # TODO(dworsham): Enable once Fuchsia supports Vulkan through the embedder. + # The logic for enabling Vulkan and Metal is in tools/gn. + shell_enable_metal = false shell_enable_vulkan = false + shell_enable_software = true } declare_args() { test_enable_gl = shell_enable_gl test_enable_metal = shell_enable_metal - test_enable_vulkan = is_fuchsia + + # The Vulkan unittests are combined with the GL unittests. + test_enable_vulkan = is_fuchsia || shell_enable_gl test_enable_software = shell_enable_software } diff --git a/engine/src/flutter/shell/platform/android/context/BUILD.gn b/engine/src/flutter/shell/platform/android/context/BUILD.gn index e7c6c44725..8991849118 100644 --- a/engine/src/flutter/shell/platform/android/context/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/context/BUILD.gn @@ -3,6 +3,7 @@ # found in the LICENSE file. import("//flutter/common/config.gni") +import("//flutter/shell/config.gni") source_set("context") { sources = [ @@ -16,4 +17,8 @@ source_set("context") { "//flutter/fml", "//third_party/skia", ] + + if (shell_enable_vulkan) { + deps += [ "//flutter/vulkan" ] + } } diff --git a/engine/src/flutter/shell/platform/android/surface/BUILD.gn b/engine/src/flutter/shell/platform/android/surface/BUILD.gn index 73cab0833c..c3e2b90260 100644 --- a/engine/src/flutter/shell/platform/android/surface/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/surface/BUILD.gn @@ -2,6 +2,8 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import("//flutter/shell/config.gni") + source_set("surface") { sources = [ "android_surface.cc", @@ -54,4 +56,8 @@ source_set("surface_mock") { "//third_party/googletest:gtest", "//third_party/skia", ] + + if (shell_enable_vulkan) { + deps += [ "//flutter/vulkan" ] + } } diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index 9324c70663..97e030136d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -7,6 +7,7 @@ assert(is_ios) import("//build/config/ios/ios_sdk.gni") import("//build/toolchain/clang.gni") import("//flutter/common/config.gni") +import("//flutter/shell/config.gni") import("//flutter/shell/gpu/gpu.gni") import("//flutter/shell/platform/darwin/common/framework_shared.gni") import("//flutter/testing/testing.gni") @@ -216,6 +217,10 @@ source_set("ios_test_flutter_mrc") { "//third_party/rapidjson", "//third_party/skia", ] + + if (shell_enable_vulkan) { + deps += [ "//flutter/vulkan" ] + } } shared_library("ios_test_flutter") { diff --git a/engine/src/flutter/shell/platform/embedder/BUILD.gn b/engine/src/flutter/shell/platform/embedder/BUILD.gn index 767d98fff2..529f48a7eb 100644 --- a/engine/src/flutter/shell/platform/embedder/BUILD.gn +++ b/engine/src/flutter/shell/platform/embedder/BUILD.gn @@ -242,6 +242,13 @@ if (enable_unittests) { deps += [ "//flutter/testing:metal" ] } + + if (test_enable_vulkan) { + deps += [ + "//flutter/testing:vulkan", + "//flutter/vulkan", + ] + } } # Tests the build in FLUTTER_ENGINE_NO_PROTOTYPES mode. 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 8e20e40d16..c8086191c8 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 @@ -29,6 +29,7 @@ #include "flutter/shell/platform/embedder/tests/embedder_unittests_util.h" #include "flutter/testing/assertions_skia.h" #include "flutter/testing/test_gl_surface.h" +#include "flutter/testing/test_vulkan_context.h" #include "flutter/testing/testing.h" #include "third_party/skia/include/core/SkSurface.h" #include "third_party/skia/src/gpu/gl/GrGLDefines.h" @@ -39,6 +40,16 @@ namespace testing { using EmbedderTest = testing::EmbedderTest; +//------------------------------------------------------------------------------ +/// This is a sanity check to ensure Swiftshader Vulkan is working. Once Vulkan +/// support lands in the embedder API, it'll be tested via a new +/// EmbedderTestContext type/config. +/// +TEST_F(EmbedderTest, CanInitializeTestVulkanContext) { + TestVulkanContext ctx; + ASSERT_TRUE(ctx.IsValid()); +} + TEST_F(EmbedderTest, CanCreateOpenGLRenderingEngine) { EmbedderConfigBuilder builder( GetEmbedderContext(EmbedderTestContextType::kOpenGLContext)); diff --git a/engine/src/flutter/testing/BUILD.gn b/engine/src/flutter/testing/BUILD.gn index fa9d9ef1d8..31b0edec48 100644 --- a/engine/src/flutter/testing/BUILD.gn +++ b/engine/src/flutter/testing/BUILD.gn @@ -125,9 +125,17 @@ if (enable_unittests) { source_set("vulkan") { testonly = true + sources = [ + "test_vulkan_context.cc", + "test_vulkan_context.h", + ] + + defines = [ "TEST_VULKAN_PROCS" ] + deps = [ ":skia", "//flutter/fml", + "//flutter/vulkan", ] # SwiftShader only supports x86/x86_64 @@ -174,9 +182,12 @@ if (enable_unittests) { "test_metal_surface_impl.mm", ] + # Skia's Vulkan support is enabled for all platforms, and so parts of + # Skia's graphics context reference Vulkan symbols. deps = [ ":skia", "//flutter/fml", + "//flutter/vulkan", ] } @@ -199,6 +210,7 @@ if (enable_unittests) { ":testing", ":testing_fixtures", "//flutter/runtime:libdart", + "//flutter/vulkan", ] if (shell_enable_metal) { diff --git a/engine/src/flutter/testing/test_vulkan_context.cc b/engine/src/flutter/testing/test_vulkan_context.cc new file mode 100644 index 0000000000..ab83100982 --- /dev/null +++ b/engine/src/flutter/testing/test_vulkan_context.cc @@ -0,0 +1,52 @@ +// 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 "test_vulkan_context.h" + +#include "flutter/vulkan/vulkan_proc_table.h" + +#ifdef OS_MACOSX +#define VULKAN_SO_PATH "libvk_swiftshader.dylib" +#elif OS_WIN +#define VULKAN_SO_PATH "vk_swiftshader.dll" +#else +#define VULKAN_SO_PATH "libvk_swiftshader.so" +#endif + +namespace flutter { + +TestVulkanContext::TestVulkanContext() : valid_(false) { + vk_ = fml::MakeRefCounted(VULKAN_SO_PATH); + if (!vk_ || !vk_->HasAcquiredMandatoryProcAddresses()) { + FML_DLOG(ERROR) << "Proc table has not acquired mandatory proc addresses."; + return; + } + + application_ = std::unique_ptr( + new vulkan::VulkanApplication(*vk_, "Flutter Unittests", {})); + if (!application_->IsValid()) { + FML_DLOG(ERROR) << "Failed to initialize basic Vulkan state."; + return; + } + if (!vk_->AreInstanceProcsSetup()) { + FML_DLOG(ERROR) << "Failed to acquire full proc table."; + return; + } + + logical_device_ = application_->AcquireFirstCompatibleLogicalDevice(); + if (!logical_device_ || !logical_device_->IsValid()) { + FML_DLOG(ERROR) << "Failed to create compatible logical device."; + return; + } + + valid_ = true; +} + +TestVulkanContext::~TestVulkanContext() = default; + +bool TestVulkanContext::IsValid() { + return valid_; +} + +} // namespace flutter diff --git a/engine/src/flutter/testing/test_vulkan_context.h b/engine/src/flutter/testing/test_vulkan_context.h new file mode 100644 index 0000000000..5d79971809 --- /dev/null +++ b/engine/src/flutter/testing/test_vulkan_context.h @@ -0,0 +1,33 @@ +// 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_VULKAN_CONTEXT_H_ +#define FLUTTER_TESTING_TEST_VULKAN_CONTEXT_H_ + +#include "flutter/vulkan/vulkan_application.h" +#include "flutter/vulkan/vulkan_device.h" +#include "flutter/vulkan/vulkan_proc_table.h" + +namespace flutter { + +/// @brief Utility class to create a Vulkan device context, a corresponding +/// Skia context, and device resources. +class TestVulkanContext { + public: + TestVulkanContext(); + ~TestVulkanContext(); + bool IsValid(); + + private: + bool valid_ = false; + fml::RefPtr vk_; + std::unique_ptr application_; + std::unique_ptr logical_device_; + + FML_DISALLOW_COPY_AND_ASSIGN(TestVulkanContext); +}; + +} // namespace flutter + +#endif // FLUTTER_TESTING_TEST_VULKAN_CONTEXT_H_ diff --git a/engine/src/flutter/tools/gn b/engine/src/flutter/tools/gn index 01812082b8..e674f0b698 100755 --- a/engine/src/flutter/tools/gn +++ b/engine/src/flutter/tools/gn @@ -312,6 +312,17 @@ def to_gn_args(args): gn_args['skia_use_metal'] = True gn_args['shell_enable_metal'] = True + # Enable Vulkan on all platforms except for Android and iOS. This is just + # to save on mobile binary size, as there's no reason the Vulkan embedder + # features can't work on these platforms. + if args.target_os not in ['android', 'ios']: + gn_args['skia_use_vulkan'] = True + gn_args['shell_enable_vulkan'] = True + # Disable VMA's use of std::shared_mutex in environments where the + # standard library doesn't support it. + if args.target_os == 'ios' or sys.platform.startswith(('cygwin', 'win')): + gn_args['skia_disable_vma_stl_shared_mutex'] = True + if sys.platform.startswith(('cygwin', 'win')): # The buildroot currently isn't set up to support Vulkan in the # Windows ANGLE build, so disable it regardless of enable_vulkan's value. diff --git a/engine/src/flutter/vulkan/vulkan_debug_report.cc b/engine/src/flutter/vulkan/vulkan_debug_report.cc index 1e54787536..f086746229 100644 --- a/engine/src/flutter/vulkan/vulkan_debug_report.cc +++ b/engine/src/flutter/vulkan/vulkan_debug_report.cc @@ -4,6 +4,7 @@ #include "vulkan_debug_report.h" +#include #include #include diff --git a/engine/src/flutter/vulkan/vulkan_image.cc b/engine/src/flutter/vulkan/vulkan_image.cc index 7f3b5eb9cd..ccd69fee9c 100644 --- a/engine/src/flutter/vulkan/vulkan_image.cc +++ b/engine/src/flutter/vulkan/vulkan_image.cc @@ -31,7 +31,7 @@ bool VulkanImage::InsertImageMemoryBarrier( const VulkanCommandBuffer& command_buffer, VkPipelineStageFlagBits src_pipline_bits, VkPipelineStageFlagBits dest_pipline_bits, - VkAccessFlagBits dest_access_flags, + VkAccessFlags dest_access_flags, VkImageLayout dest_layout) { const VkImageMemoryBarrier image_memory_barrier = { .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, diff --git a/engine/src/flutter/vulkan/vulkan_image.h b/engine/src/flutter/vulkan/vulkan_image.h index 9402f0eade..9c401a640d 100644 --- a/engine/src/flutter/vulkan/vulkan_image.h +++ b/engine/src/flutter/vulkan/vulkan_image.h @@ -26,7 +26,7 @@ class VulkanImage { const VulkanCommandBuffer& command_buffer, VkPipelineStageFlagBits src_pipline_bits, VkPipelineStageFlagBits dest_pipline_bits, - VkAccessFlagBits dest_access_flags, + VkAccessFlags dest_access_flags, VkImageLayout dest_layout); private: diff --git a/engine/src/flutter/vulkan/vulkan_proc_table.cc b/engine/src/flutter/vulkan/vulkan_proc_table.cc index 513a9dba7d..ceea6a9655 100644 --- a/engine/src/flutter/vulkan/vulkan_proc_table.cc +++ b/engine/src/flutter/vulkan/vulkan_proc_table.cc @@ -4,8 +4,6 @@ #include "vulkan_proc_table.h" -#include - #include "flutter/fml/logging.h" #define ACQUIRE_PROC(name, context) \ @@ -16,10 +14,12 @@ namespace vulkan { -VulkanProcTable::VulkanProcTable() +VulkanProcTable::VulkanProcTable() : VulkanProcTable("libvulkan.so"){}; + +VulkanProcTable::VulkanProcTable(const char* path) : handle_(nullptr), acquired_mandatory_proc_addresses_(false) { acquired_mandatory_proc_addresses_ = - OpenLibraryHandle() && SetupLoaderProcAddresses(); + OpenLibraryHandle(path) && SetupLoaderProcAddresses(); } VulkanProcTable::~VulkanProcTable() { @@ -43,7 +43,7 @@ bool VulkanProcTable::AreDeviceProcsSetup() const { } bool VulkanProcTable::SetupLoaderProcAddresses() { - if (handle_ == nullptr) { + if (!handle_) { return true; } @@ -51,8 +51,8 @@ bool VulkanProcTable::SetupLoaderProcAddresses() { #if VULKAN_LINK_STATICALLY GetInstanceProcAddr = &vkGetInstanceProcAddr; #else // VULKAN_LINK_STATICALLY - reinterpret_cast( - dlsym(handle_, "vkGetInstanceProcAddr")); + reinterpret_cast(const_cast( + handle_->ResolveSymbol("vkGetInstanceProcAddr"))); #endif // VULKAN_LINK_STATICALLY if (!GetInstanceProcAddr) { @@ -129,6 +129,7 @@ bool VulkanProcTable::SetupDeviceProcAddresses( ACQUIRE_PROC(ResetCommandBuffer, handle); ACQUIRE_PROC(ResetFences, handle); ACQUIRE_PROC(WaitForFences, handle); +#ifndef TEST_VULKAN_PROCS #if OS_ANDROID ACQUIRE_PROC(AcquireNextImageKHR, handle); ACQUIRE_PROC(CreateSwapchainKHR, handle); @@ -145,42 +146,23 @@ bool VulkanProcTable::SetupDeviceProcAddresses( ACQUIRE_PROC(SetBufferCollectionConstraintsFUCHSIAX, handle); ACQUIRE_PROC(GetBufferCollectionPropertiesFUCHSIAX, handle); #endif // OS_FUCHSIA +#endif // TEST_VULKAN_PROCS device_ = {handle, nullptr}; return true; } -bool VulkanProcTable::OpenLibraryHandle() { +bool VulkanProcTable::OpenLibraryHandle(const char* path) { #if VULKAN_LINK_STATICALLY - static char kDummyLibraryHandle = '\0'; - handle_ = reinterpret_cast(&kDummyLibraryHandle); - return true; + handle_ = fml::NativeLibrary::CreateForCurrentProcess(); #else // VULKAN_LINK_STATICALLY - dlerror(); // clear existing errors on thread. - handle_ = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL); - if (handle_ == nullptr) { - FML_DLOG(WARNING) << "Could not open the vulkan library: " << dlerror(); - return false; - } - return true; + handle_ = fml::NativeLibrary::Create(path); #endif // VULKAN_LINK_STATICALLY + return !!handle_; } bool VulkanProcTable::CloseLibraryHandle() { -#if VULKAN_LINK_STATICALLY handle_ = nullptr; return true; -#else - if (handle_ != nullptr) { - dlerror(); // clear existing errors on thread. - if (dlclose(handle_) != 0) { - FML_DLOG(ERROR) << "Could not close the vulkan library handle. This " - "indicates a leak."; - FML_DLOG(ERROR) << dlerror(); - } - handle_ = nullptr; - } - return handle_ == nullptr; -#endif } PFN_vkVoidFunction VulkanProcTable::AcquireProc( diff --git a/engine/src/flutter/vulkan/vulkan_proc_table.h b/engine/src/flutter/vulkan/vulkan_proc_table.h index 291ad0f751..1b81f74b22 100644 --- a/engine/src/flutter/vulkan/vulkan_proc_table.h +++ b/engine/src/flutter/vulkan/vulkan_proc_table.h @@ -8,6 +8,7 @@ #include "flutter/fml/macros.h" #include "flutter/fml/memory/ref_counted.h" #include "flutter/fml/memory/ref_ptr.h" +#include "flutter/fml/native_library.h" #include "third_party/skia/include/core/SkRefCnt.h" #include "third_party/skia/include/gpu/vk/GrVkBackendContext.h" #include "vulkan_handle.h" @@ -105,6 +106,7 @@ class VulkanProcTable : public fml::RefCountedThreadSafe { DEFINE_PROC(ResetCommandBuffer); DEFINE_PROC(ResetFences); DEFINE_PROC(WaitForFences); +#ifndef TEST_VULKAN_PROCS #if OS_ANDROID DEFINE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); DEFINE_PROC(GetPhysicalDeviceSurfaceFormatsKHR); @@ -123,18 +125,20 @@ class VulkanProcTable : public fml::RefCountedThreadSafe { DEFINE_PROC(SetBufferCollectionConstraintsFUCHSIAX); DEFINE_PROC(GetBufferCollectionPropertiesFUCHSIAX); #endif // OS_FUCHSIA +#endif // TEST_VULKAN_PROCS #undef DEFINE_PROC private: - void* handle_; + fml::RefPtr handle_; bool acquired_mandatory_proc_addresses_; VulkanHandle instance_; VulkanHandle device_; VulkanProcTable(); + VulkanProcTable(const char* path); ~VulkanProcTable(); - bool OpenLibraryHandle(); + bool OpenLibraryHandle(const char* path); bool SetupLoaderProcAddresses(); bool CloseLibraryHandle(); PFN_vkVoidFunction AcquireProc(