diff --git a/build/skia/BUILD.gn b/build/skia/BUILD.gn index 6845e7d48fc180b776f44594036224aac6c18b78..f1f8fc19a887a57f0e46e74c1e64ae77f03ad711 100644 --- a/build/skia/BUILD.gn +++ b/build/skia/BUILD.gn @@ -177,6 +177,8 @@ config("skia_deps_include_config") { include_dirs += [ "$libs_root/shared_library/${target_os}_${target_cpu}/include/frameworks/native/libs/nativewindow/include", "$libs_root/shared_library/${target_os}_${target_cpu}/include/frameworks/native/libs/arect/include", + "//foundation/graphic/graphic_2d/frameworks/vulkan_wrapper/include", + "//third_party/vulkan-headers/include", ] } @@ -317,6 +319,8 @@ config("skia_config") { "$flutter_root/skia", "$third_party_skia_override_path/third_party/skia", "$third_party_skia_override_path/third_party/skia/include/config/android", + "//foundation/graphic/graphic_2d/frameworks/vulkan_wrapper/include", + "//third_party/vulkan-headers/include", ] defines = ace_skia_common_defines diff --git a/build/skia/ohos/BUILD.gn b/build/skia/ohos/BUILD.gn index 5c93a941bb9426aa1d95ef66f031cfb333466b33..781e92cdcdbcdb64ceb615fa974f58cfe7fb1561 100644 --- a/build/skia/ohos/BUILD.gn +++ b/build/skia/ohos/BUILD.gn @@ -88,6 +88,9 @@ skia_common_ccflags = [ "-Wno-suggest-destructor-override", "-Wno-suggest-override", "-Wno-psabi", + "-Wno-unused-variable", + "-Wno-extra-semi-stmt", + "-Wno-missing-field-initializers", "-Os", ] @@ -162,6 +165,7 @@ skia_common_defines = [ "GR_TEST_UTILS=1", "SKIA_IMPLEMENTATION=1", "SK_GL", + "SK_VULKAN", "SK_SUPPORT_PDF", "SK_HAS_JPEG_LIBRARY", "SK_HAS_HEIF_LIBRARY", @@ -184,7 +188,10 @@ skia_common_defines = [ # ------------------------------------------------ config("skia_core_config") { visibility = [ ":*" ] - include_dirs = [ "$flutter_root/skia" ] + include_dirs = [ + "$flutter_root/skia", + "//third_party/skia/third_party/vulkanmemoryallocator", + ] cflags_cc = skia_common_ccflags cflags_cc += [ "-Wno-extra-semi" ] cflags = skia_common_cflags @@ -453,6 +460,47 @@ ohos_source_set("skia_core_ohos") { "$flutter_root/skia/src/effects/imagefilters/SkPictureImageFilter.cpp", "$flutter_root/skia/src/effects/imagefilters/SkTileImageFilter.cpp", "$flutter_root/skia/src/effects/imagefilters/SkXfermodeImageFilter.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkAMDMemoryAllocator.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkBuffer.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkBufferView.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkCaps.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkCommandBuffer.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkCommandPool.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkDescriptorPool.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkDescriptorSet.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkDescriptorSetManager.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkExtensions.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkFramebuffer.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkGpu.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkGpuCommandBuffer.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkGpuCommandBuffer.h", + "$flutter_root/skia/src/gpu/vk/GrVkImage.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkImageView.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkIndexBuffer.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkInterface.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkMemory.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkPipeline.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkPipelineState.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkPipelineStateBuilder.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkPipelineStateCache.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkPipelineStateDataManager.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkRenderPass.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkRenderTarget.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkResourceProvider.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkSampler.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkSamplerYcbcrConversion.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkSecondaryCBDrawContext.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkSemaphore.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkStencilAttachment.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkTexture.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkTextureRenderTarget.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkTransferBuffer.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkTypesPriv.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkUniformBuffer.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkUniformHandler.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkUtil.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkVaryingHandler.cpp", + "$flutter_root/skia/src/gpu/vk/GrVkVertexBuffer.cpp", "$flutter_root/skia/src/image/SkImage.cpp", "$flutter_root/skia/src/image/SkImage_Lazy.cpp", "$flutter_root/skia/src/image/SkImage_Raster.cpp", @@ -581,6 +629,7 @@ ohos_source_set("skia_core_ohos") { "$flutter_root/skia/src/utils/win/SkWGL_win.cpp", "$flutter_root/skia/src/xps/SkXPSDevice.cpp", "$flutter_root/skia/src/xps/SkXPSDocument.cpp", + "//third_party/skia/third_party/vulkanmemoryallocator/GrVulkanMemoryAllocator.cpp", ] if (enable_dump_drawcmd) { diff --git a/engine/flutter/vulkan/BUILD.gn b/engine/flutter/vulkan/BUILD.gn index 0ac6c00e3edc116fe31350ff742818d50133e4cc..b11c6bedb963a2307a089d71c10b0b1109dbff89 100644 --- a/engine/flutter/vulkan/BUILD.gn +++ b/engine/flutter/vulkan/BUILD.gn @@ -2,72 +2,174 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -import("//build/fuchsia/sdk.gni") - -config("vulkan_config") { - if (using_fuchsia_sdk) { - include_dirs = [ "$fuchsia_sdk_root/vulkan/include" ] - } else if (is_fuchsia) { - include_dirs = - [ "//third_party/vulkan_loader_and_validation_layers/include" ] - } else { - include_dirs = [ "//third_party/vulkan/src" ] +import("//build/ohos.gni") +import("//foundation/graphic/graphic_2d/graphic_config.gni") +import("//third_party/flutter/flutter_config.gni") + +if (graphic_standard_feature_enable_vulkan) { + config("vulkan_flutter_config") { + visibility = [ ":*" ] + + include_dirs = [ + "vulkan_application.h", + "vulkan_backbuffer.h", + "vulkan_command_buffer.h", + "vulkan_debug_report.h", + "vulkan_device.h", + "vulkan_handle.h", + "vulkan_hilog.h", + "vulkan_image.h", + "vulkan_interface.h", + "vulkan_native_surface.h", + "vulkan_native_surface_ohos.h", + "vulkan_proc_table.h", + "vulkan_provider.h", + "vulkan_surface.h", + "vulkan_swapchain.h", + "vulkan_utilities.h", + "vulkan_window.h", + ] + + cflags = [ + "-Wall", + "-Wno-unused-function", + "-Wno-unused-variable", + "-Wno-missing-braces", + "-Wno-unused-private-field", + "-Werror", + "-g3", + ] } - if (is_fuchsia) { - defines = [ "VK_USE_PLATFORM_FUCHSIA=1" ] + config("vulkan_flutter_public_config") { + include_dirs = [ + "//third_party/flutter/engine/flutter/vulkan", + "//foundation/graphic/graphic_2d/frameworks/surface/include", + "//foundation/graphic/graphic_2d/frameworks/vulkan_wrapper/include", + "//foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include", + "//foundation/graphic/graphic_2d/interfaces/inner_api/surface", + "//commonlibrary/c_utils/base/include", + "//third_party/flutter/skia", + ] + + cflags = [ "-Wno-error=deprecated-declarations" ] + + defines = [ + "VK_USE_PLATFORM_OHOS_KHR", + "NDEBUG", + ] } -} -source_set("vulkan") { - sources = [ - "vulkan_application.cc", - "vulkan_application.h", - "vulkan_backbuffer.cc", - "vulkan_backbuffer.h", - "vulkan_command_buffer.cc", - "vulkan_command_buffer.h", - "vulkan_debug_report.cc", - "vulkan_debug_report.h", - "vulkan_device.cc", - "vulkan_device.h", - "vulkan_handle.cc", - "vulkan_handle.h", - "vulkan_image.cc", - "vulkan_image.h", - "vulkan_interface.cc", - "vulkan_interface.h", - "vulkan_native_surface.cc", - "vulkan_native_surface.h", - "vulkan_proc_table.cc", - "vulkan_proc_table.h", - "vulkan_surface.cc", - "vulkan_surface.h", - "vulkan_swapchain.h", - "vulkan_utilities.cc", - "vulkan_utilities.h", - "vulkan_window.cc", - "vulkan_window.h", - ] - - if (is_android) { - sources += [ - "vulkan_native_surface_android.cc", - "vulkan_native_surface_android.h", + ohos_shared_library("vulkan_flutter") { + sources = [ + "vulkan_application.cc", + "vulkan_backbuffer.cc", + "vulkan_command_buffer.cc", + "vulkan_debug_report.cc", + "vulkan_device.cc", + "vulkan_handle.cc", + "vulkan_image.cc", + "vulkan_interface.cc", + "vulkan_native_surface.cc", + "vulkan_native_surface_ohos.cc", + "vulkan_proc_table.cc", + "vulkan_provider.cc", + "vulkan_surface.cc", "vulkan_swapchain.cc", + "vulkan_utilities.cc", + "vulkan_window.cc", + ] + + if (graphic_standard_feature_enable_vulkan) { + defines = [ "RS_ENABLE_VK" ] + } + + configs = [ ":vulkan_flutter_config" ] + + public_configs = [ ":vulkan_flutter_public_config" ] + + external_deps = [ + "c_utils:utils", + "hilog_native:libhilog", + ] + + public_deps = [ + "//foundation/graphic/graphic_2d:libsurface", + "//foundation/graphic/graphic_2d/frameworks/vulkan_wrapper:vulkan", + "//third_party/flutter/build/skia:ace_skia_ohos", ] + + part_name = "flutter" + subsystem_name = flutter_subsystem } - if (is_fuchsia) { - sources += [ "vulkan_swapchain_stub.cc" ] +} else { + import("//build/fuchsia/sdk.gni") + + config("vulkan_config") { + if (using_fuchsia_sdk) { + include_dirs = [ "$fuchsia_sdk_root/vulkan/include" ] + } else if (is_fuchsia) { + include_dirs = + [ "//third_party/vulkan_loader_and_validation_layers/include" ] + } else { + include_dirs = [ "//third_party/vulkan/src" ] + } + + if (is_fuchsia) { + defines = [ "VK_USE_PLATFORM_FUCHSIA=1" ] + } } - deps = [ - "$flutter_root/fml", - "//third_party/skia", - ] + source_set("vulkan") { + sources = [ + "vulkan_application.cc", + "vulkan_application.h", + "vulkan_backbuffer.cc", + "vulkan_backbuffer.h", + "vulkan_command_buffer.cc", + "vulkan_command_buffer.h", + "vulkan_debug_report.cc", + "vulkan_debug_report.h", + "vulkan_device.cc", + "vulkan_device.h", + "vulkan_handle.cc", + "vulkan_handle.h", + "vulkan_image.cc", + "vulkan_image.h", + "vulkan_interface.cc", + "vulkan_interface.h", + "vulkan_native_surface.cc", + "vulkan_native_surface.h", + "vulkan_proc_table.cc", + "vulkan_proc_table.h", + "vulkan_surface.cc", + "vulkan_surface.h", + "vulkan_swapchain.h", + "vulkan_utilities.cc", + "vulkan_utilities.h", + "vulkan_window.cc", + "vulkan_window.h", + ] + + if (is_android) { + sources += [ + "vulkan_native_surface_android.cc", + "vulkan_native_surface_android.h", + "vulkan_swapchain.cc", + ] + } + if (is_fuchsia) { + sources += [ "vulkan_swapchain_stub.cc" ] + } + + deps = [ + "$flutter_root/fml", + "//third_party/skia", + ] - public_configs = [ - ":vulkan_config", - "$flutter_root:config", - ] + public_configs = [ + ":vulkan_config", + "$flutter_root:config", + ] + } } diff --git a/engine/flutter/vulkan/vulkan_application.cc b/engine/flutter/vulkan/vulkan_application.cc index 87c194ad7ee70fe73e4f1dcc58cf7b22789b6ad6..52f027a6e4efc653c993bba98005d62a8a520c8e 100644 --- a/engine/flutter/vulkan/vulkan_application.cc +++ b/engine/flutter/vulkan/vulkan_application.cc @@ -2,14 +2,17 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_application.h" +#include "vulkan_application.h" #include #include -#include "flutter/vulkan/vulkan_device.h" -#include "flutter/vulkan/vulkan_proc_table.h" -#include "flutter/vulkan/vulkan_utilities.h" +#include "vulkan_device.h" +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#include "vulkan_proc_table.h" +#include "vulkan_utilities.h" namespace vulkan { @@ -64,6 +67,17 @@ VulkanApplication::VulkanApplication( // Configure init structs. +#ifdef RS_ENABLE_VK + const VkApplicationInfo info = { + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .pNext = nullptr, + .pApplicationName = application_name.c_str(), + .applicationVersion = application_version, + .pEngineName = "Rosen", + .engineVersion = VK_MAKE_VERSION(1, 0, 0), + .apiVersion = api_version_, + }; +#else const VkApplicationInfo info = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .pNext = nullptr, @@ -73,6 +87,7 @@ VulkanApplication::VulkanApplication( .engineVersion = VK_MAKE_VERSION(1, 0, 0), .apiVersion = api_version_, }; +#endif const VkInstanceCreateInfo create_info = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, @@ -91,29 +106,49 @@ VulkanApplication::VulkanApplication( if (VK_CALL_LOG_ERROR(vk.CreateInstance(&create_info, nullptr, &instance)) != VK_SUCCESS) { +#ifdef RS_ENABLE_VK + LOGE("Could not create application instance."); +#else FML_DLOG(INFO) << "Could not create application instance."; +#endif return; } // Now that we have an instance, setup instance proc table entries. if (!vk.SetupInstanceProcAddresses(instance)) { +#ifdef RS_ENABLE_VK + LOGE("Could not setup instance proc addresses."); +#else FML_DLOG(INFO) << "Could not setup instance proc addresses."; +#endif return; } instance_ = {instance, [this](VkInstance i) { +#ifdef RS_ENABLE_VK + LOGE("Destroying Vulkan instance"); +#else FML_LOG(INFO) << "Destroying Vulkan instance"; +#endif vk.DestroyInstance(i, nullptr); }}; if (enable_instance_debugging) { auto debug_report = std::make_unique(vk, instance_); if (!debug_report->IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("Vulkan debugging was enabled but could not be setup for this instance."); +#else FML_LOG(INFO) << "Vulkan debugging was enabled but could not be setup " "for this instance."; +#endif } else { debug_report_ = std::move(debug_report); +#ifdef RS_ENABLE_VK + LOGE("Debug reporting is enabled."); +#else FML_DLOG(INFO) << "Debug reporting is enabled."; +#endif } } @@ -146,13 +181,21 @@ std::vector VulkanApplication::GetPhysicalDevices() const { uint32_t device_count = 0; if (VK_CALL_LOG_ERROR(vk.EnumeratePhysicalDevices(instance_, &device_count, nullptr)) != VK_SUCCESS) { +#ifdef RS_ENABLE_VK + LOGE("Could not enumerate physical device."); +#else FML_DLOG(INFO) << "Could not enumerate physical device."; +#endif return {}; } if (device_count == 0) { // No available devices. +#ifdef RS_ENABLE_VK + LOGE("No physical devices found."); +#else FML_DLOG(INFO) << "No physical devices found."; +#endif return {}; } @@ -162,7 +205,11 @@ std::vector VulkanApplication::GetPhysicalDevices() const { if (VK_CALL_LOG_ERROR(vk.EnumeratePhysicalDevices( instance_, &device_count, physical_devices.data())) != VK_SUCCESS) { +#ifdef RS_ENABLE_VK + LOGE("Could not enumerate physical device."); +#else FML_DLOG(INFO) << "Could not enumerate physical device."; +#endif return {}; } @@ -177,7 +224,11 @@ VulkanApplication::AcquireFirstCompatibleLogicalDevice() const { return logical_device; } } +#ifdef RS_ENABLE_VK + LOGE("Could not acquire compatible logical device."); +#else FML_DLOG(INFO) << "Could not acquire compatible logical device."; +#endif return nullptr; } diff --git a/engine/flutter/vulkan/vulkan_application.h b/engine/flutter/vulkan/vulkan_application.h index fd2e7994efd93d8a2529bcae16d18d58d2836ba8..2247752b029769491dad4303e7705cd2f841ca82 100644 --- a/engine/flutter/vulkan/vulkan_application.h +++ b/engine/flutter/vulkan/vulkan_application.h @@ -8,9 +8,11 @@ #include #include #include +#ifndef RS_ENABLE_VK #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_debug_report.h" -#include "flutter/vulkan/vulkan_handle.h" +#endif +#include "vulkan_debug_report.h" +#include "vulkan_handle.h" namespace vulkan { @@ -56,7 +58,9 @@ class VulkanApplication { const std::vector& supported_extensions, std::string extension_name); +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanApplication); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_backbuffer.cc b/engine/flutter/vulkan/vulkan_backbuffer.cc index e61045eaa8ad9739fe7e87f84551f477032a4ab1..c646a7d67461f23129fd2a0fb47a506e6961fe0e 100644 --- a/engine/flutter/vulkan/vulkan_backbuffer.cc +++ b/engine/flutter/vulkan/vulkan_backbuffer.cc @@ -2,11 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_backbuffer.h" +#include "vulkan_backbuffer.h" #include -#include "flutter/vulkan/vulkan_proc_table.h" +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#include "vulkan_proc_table.h" #include "third_party/skia/include/gpu/vk/GrVkTypes.h" #include "vulkan/vulkan.h" @@ -21,17 +24,29 @@ VulkanBackbuffer::VulkanBackbuffer(const VulkanProcTable& p_vk, render_command_buffer_(p_vk, device, pool), valid_(false) { if (!usage_command_buffer_.IsValid() || !render_command_buffer_.IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("Command buffers were not valid."); +#else FML_DLOG(INFO) << "Command buffers were not valid."; +#endif return; } if (!CreateSemaphores()) { +#ifdef RS_ENABLE_VK + LOGE("Could not create semaphores."); +#else FML_DLOG(INFO) << "Could not create semaphores."; +#endif return; } if (!CreateFences()) { +#ifdef RS_ENABLE_VK + LOGE("Could not create fences."); +#else FML_DLOG(INFO) << "Could not create fences."; +#endif return; } @@ -39,7 +54,11 @@ VulkanBackbuffer::VulkanBackbuffer(const VulkanProcTable& p_vk, } VulkanBackbuffer::~VulkanBackbuffer() { +#ifdef RS_ENABLE_VK + WaitFences(); +#else FML_ALLOW_UNUSED_LOCAL(WaitFences()); +#endif } bool VulkanBackbuffer::IsValid() const { diff --git a/engine/flutter/vulkan/vulkan_backbuffer.h b/engine/flutter/vulkan/vulkan_backbuffer.h index b6cdac62b89b33449e90d28ff9cf2779870ce6c9..e944796cf66b53f0cdb9413a571ae97dcfe2722c 100644 --- a/engine/flutter/vulkan/vulkan_backbuffer.h +++ b/engine/flutter/vulkan/vulkan_backbuffer.h @@ -7,10 +7,12 @@ #include +#include "vulkan_command_buffer.h" +#include "vulkan_handle.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/compiler_specific.h" #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_command_buffer.h" -#include "flutter/vulkan/vulkan_handle.h" +#endif #include "third_party/skia/include/core/SkSize.h" #include "third_party/skia/include/core/SkSurface.h" @@ -26,10 +28,14 @@ class VulkanBackbuffer { bool IsValid() const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool WaitFences(); +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool ResetFences(); const VulkanHandle& GetUsageFence() const; @@ -57,7 +63,9 @@ class VulkanBackbuffer { bool CreateFences(); +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanBackbuffer); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_command_buffer.cc b/engine/flutter/vulkan/vulkan_command_buffer.cc index 09ccf0e975805482c963ec0160a5670bf407f867..a917df4baba3712d10f98f2c9b5c20d142a07541 100644 --- a/engine/flutter/vulkan/vulkan_command_buffer.cc +++ b/engine/flutter/vulkan/vulkan_command_buffer.cc @@ -2,9 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_command_buffer.h" +#include "vulkan_command_buffer.h" -#include "flutter/vulkan/vulkan_proc_table.h" +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#include "vulkan_proc_table.h" namespace vulkan { @@ -25,7 +28,11 @@ VulkanCommandBuffer::VulkanCommandBuffer( if (VK_CALL_LOG_ERROR(vk.AllocateCommandBuffers(device_, &allocate_info, &buffer)) != VK_SUCCESS) { +#ifdef RS_ENABLE_VK + LOGE("Could not allocate command buffers."); +#else FML_DLOG(INFO) << "Could not allocate command buffers."; +#endif return; } diff --git a/engine/flutter/vulkan/vulkan_command_buffer.h b/engine/flutter/vulkan/vulkan_command_buffer.h index bad0be185cfbff0ec077c97c5497b97e6155ae93..48f72871ff64b43be8bf22921016a518dc3754af 100644 --- a/engine/flutter/vulkan/vulkan_command_buffer.h +++ b/engine/flutter/vulkan/vulkan_command_buffer.h @@ -5,9 +5,11 @@ #ifndef FLUTTER_VULKAN_VULKAN_COMMAND_BUFFER_H_ #define FLUTTER_VULKAN_VULKAN_COMMAND_BUFFER_H_ +#include "vulkan_handle.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/compiler_specific.h" #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_handle.h" +#endif namespace vulkan { @@ -25,13 +27,19 @@ class VulkanCommandBuffer { VkCommandBuffer Handle() const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool Begin() const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool End() const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool InsertPipelineBarrier( VkPipelineStageFlagBits src_stage_flags, VkPipelineStageFlagBits dest_stage_flags, @@ -50,7 +58,9 @@ class VulkanCommandBuffer { VulkanHandle handle_; bool valid_; +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanCommandBuffer); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_debug_report.cc b/engine/flutter/vulkan/vulkan_debug_report.cc index bd8f36ff46f2d22f051be56f50a9d3d250de24b4..5580b393832ba8a84bfd9a4afbbd039bab770c57 100644 --- a/engine/flutter/vulkan/vulkan_debug_report.cc +++ b/engine/flutter/vulkan/vulkan_debug_report.cc @@ -2,22 +2,33 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_debug_report.h" +#include "vulkan_debug_report.h" #include #include +#include "vulkan_utilities.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/compiler_specific.h" -#include "flutter/vulkan/vulkan_utilities.h" +#endif namespace vulkan { +#ifdef RS_ENABLE_VK +static const VkDebugReportFlagsEXT kVulkanErrorFlags = + VK_DEBUG_REPORT_WARNING_BIT_EXT | + VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | VK_DEBUG_REPORT_ERROR_BIT_EXT; + +static const VkDebugReportFlagsEXT kVulkanInfoFlags = + VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT; +#else static const VkDebugReportFlagsEXT kVulkanErrorFlags FML_ALLOW_UNUSED_TYPE = VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | VK_DEBUG_REPORT_ERROR_BIT_EXT; static const VkDebugReportFlagsEXT kVulkanInfoFlags FML_ALLOW_UNUSED_TYPE = VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT; +#endif std::string VulkanDebugReport::DebugExtensionName() { return VK_EXT_DEBUG_REPORT_EXTENSION_NAME; @@ -148,6 +159,7 @@ OnVulkanDebugReportCallback(VkDebugReportFlagsEXT flags, padding += 1; +#ifndef RS_ENABLE_VK std::stringstream stream; stream << std::endl; @@ -172,6 +184,7 @@ OnVulkanDebugReportCallback(VkDebugReportFlagsEXT flags, } else { FML_LOG(INFO) << stream.str(); } +#endif // Returning false tells the layer not to stop when the event occurs, so // they see the same behavior with and without validation layers enabled. diff --git a/engine/flutter/vulkan/vulkan_debug_report.h b/engine/flutter/vulkan/vulkan_debug_report.h index aa71f8cb4cd4af3a8fa59dd7730a310fa313fcff..1979a27745498edd4d19b2265b05a4c99ade0211 100644 --- a/engine/flutter/vulkan/vulkan_debug_report.h +++ b/engine/flutter/vulkan/vulkan_debug_report.h @@ -5,10 +5,12 @@ #ifndef FLUTTER_VULKAN_VULKAN_DEBUG_REPORT_H_ #define FLUTTER_VULKAN_VULKAN_DEBUG_REPORT_H_ +#include "vulkan_handle.h" +#include "vulkan_interface.h" +#include "vulkan_proc_table.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_handle.h" -#include "flutter/vulkan/vulkan_interface.h" -#include "flutter/vulkan/vulkan_proc_table.h" +#endif namespace vulkan { @@ -29,7 +31,9 @@ class VulkanDebugReport { VulkanHandle handle_; bool valid_; +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanDebugReport); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_device.cc b/engine/flutter/vulkan/vulkan_device.cc index a69e4712b0652bfc6128e964cd5d9421890f41a0..f5e3601d0f1be12b53a2593a74f1c69a8725b75d 100644 --- a/engine/flutter/vulkan/vulkan_device.cc +++ b/engine/flutter/vulkan/vulkan_device.cc @@ -2,15 +2,18 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_device.h" +#include "vulkan_device.h" #include #include #include -#include "flutter/vulkan/vulkan_proc_table.h" -#include "flutter/vulkan/vulkan_surface.h" -#include "flutter/vulkan/vulkan_utilities.h" +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#include "vulkan_proc_table.h" +#include "vulkan_surface.h" +#include "vulkan_utilities.h" #include "third_party/skia/include/gpu/vk/GrVkBackendContext.h" namespace vulkan { @@ -18,6 +21,16 @@ namespace vulkan { constexpr auto kVulkanInvalidGraphicsQueueIndex = std::numeric_limits::max(); +#ifdef RS_ENABLE_VK +static uint32_t FindQueueIndex(const std::vector& properties, VkQueueFlagBits flagBits) { + for (uint32_t i = 0, count = static_cast(properties.size()); i < count; i++) { + if (properties[i].queueFlags & flagBits) { + return i; + } + } + return kVulkanInvalidGraphicsQueueIndex; +} +#else static uint32_t FindGraphicsQueueIndex( const std::vector& properties) { for (uint32_t i = 0, count = static_cast(properties.size()); @@ -28,7 +41,123 @@ static uint32_t FindGraphicsQueueIndex( } return kVulkanInvalidGraphicsQueueIndex; } +#endif + +#ifdef RS_ENABLE_VK +VulkanDevice::VulkanDevice(VulkanProcTable& p_vk, VulkanHandle physical_device) + : vk(p_vk), physical_device_(std::move(physical_device)), + graphics_queue_index_(std::numeric_limits::max()), + compute_queue_index_(std::numeric_limits::max()), valid_(false) +{ + if (!physical_device_ || !vk.AreInstanceProcsSetup()) { + return; + } + + std::vector properties = GetQueueFamilyProperties(); + graphics_queue_index_ = FindQueueIndex(properties, VK_QUEUE_GRAPHICS_BIT); + compute_queue_index_ = FindQueueIndex(properties, VK_QUEUE_COMPUTE_BIT); + + if (graphics_queue_index_ == kVulkanInvalidGraphicsQueueIndex) { + LOGE("Could not find the graphics queue index."); + return; + } + + const float priorities[1] = {1.0f}; + + std::vector queue_create { { + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .queueFamilyIndex = graphics_queue_index_, + .queueCount = 1, + .pQueuePriorities = priorities, + } }; + + if (compute_queue_index_ != kVulkanInvalidGraphicsQueueIndex && compute_queue_index_ != graphics_queue_index_) { + queue_create.push_back({ + .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .queueFamilyIndex = graphics_queue_index_, + .queueCount = 1, + .pQueuePriorities = priorities, + }); + } + + const char* extensions[] = { + VK_KHR_SWAPCHAIN_EXTENSION_NAME, + }; + + auto enabled_layers = DeviceLayersToEnable(vk, physical_device_); + + const char* layers[enabled_layers.size()]; + + for (size_t i = 0; i < enabled_layers.size(); i++) { + layers[i] = enabled_layers[i].c_str(); + } + + const VkDeviceCreateInfo create_info = { + .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .queueCreateInfoCount = queue_create.size(), + .pQueueCreateInfos = queue_create.data(), + .enabledLayerCount = static_cast(enabled_layers.size()), + .ppEnabledLayerNames = layers, + .enabledExtensionCount = sizeof(extensions) / sizeof(const char*), + .ppEnabledExtensionNames = extensions, + .pEnabledFeatures = nullptr, + }; + + VkDevice device = VK_NULL_HANDLE; + + if (VK_CALL_LOG_ERROR(vk.CreateDevice(physical_device_, &create_info, nullptr, + &device)) != VK_SUCCESS) { + LOGE("Could not create device."); + return; + } + + device_ = {device, + [this](VkDevice device) { vk.DestroyDevice(device, nullptr); }}; + + if (!vk.SetupDeviceProcAddresses(device_)) { + LOGE("Could not setup device proc addresses."); + return; + } + + VkQueue queue = VK_NULL_HANDLE; + + vk.GetDeviceQueue(device_, graphics_queue_index_, 0, &queue); + + if (queue == VK_NULL_HANDLE) { + LOGE("Could not get the device queue handle."); + return; + } + + queue_ = queue; + + const VkCommandPoolCreateInfo command_pool_create_info = { + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .pNext = nullptr, + .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, + .queueFamilyIndex = 0, + }; + + VkCommandPool command_pool = VK_NULL_HANDLE; + if (VK_CALL_LOG_ERROR(vk.CreateCommandPool(device_, &command_pool_create_info, + nullptr, &command_pool)) != + VK_SUCCESS) { + LOGE("Could not create the command pool."); + return; + } + command_pool_ = {command_pool, [this](VkCommandPool pool) { + vk.DestroyCommandPool(device_, pool, nullptr); + }}; + + valid_ = true; +} +#else VulkanDevice::VulkanDevice(VulkanProcTable& p_vk, VulkanHandle physical_device) : vk(p_vk), @@ -138,9 +267,14 @@ VulkanDevice::VulkanDevice(VulkanProcTable& p_vk, valid_ = true; } +#endif VulkanDevice::~VulkanDevice() { +#ifdef RS_ENABLE_VK + WaitIdle(); +#else FML_ALLOW_UNUSED_LOCAL(WaitIdle()); +#endif } bool VulkanDevice::IsValid() const { @@ -179,6 +313,40 @@ uint32_t VulkanDevice::GetGraphicsQueueIndex() const { bool VulkanDevice::GetSurfaceCapabilities( const VulkanSurface& surface, VkSurfaceCapabilitiesKHR* capabilities) const { +#ifdef RS_ENABLE_VK +if (!surface.IsValid() || capabilities == nullptr) { + LOGE("GetSurfaceCapabilities surface is not valid or capabilities is null"); + return false; + } + + bool success = + VK_CALL_LOG_ERROR(vk.GetPhysicalDeviceSurfaceCapabilitiesKHR( + physical_device_, surface.Handle(), capabilities)) == VK_SUCCESS; + + if (!success) { + LOGE("GetPhysicalDeviceSurfaceCapabilitiesKHR not success"); + return false; + } + + // Check if the physical device surface capabilities are valid. If so, there + // is nothing more to do. + if (capabilities->currentExtent.width != 0xFFFFFFFF && + capabilities->currentExtent.height != 0xFFFFFFFF) { + return true; + } + + // Ask the native surface for its size as a fallback. + SkISize size = surface.GetSize(); + + if (size.width() == 0 || size.height() == 0) { + LOGE("GetSurfaceCapabilities surface size is 0"); + return false; + } + + capabilities->currentExtent.width = size.width(); + capabilities->currentExtent.height = size.height(); + return true; +#else #if OS_ANDROID if (!surface.IsValid() || capabilities == nullptr) { return false; @@ -211,7 +379,8 @@ bool VulkanDevice::GetSurfaceCapabilities( return true; #else return false; -#endif +#endif // OS_ANDROID +#endif // RS_ENABLE_VK } bool VulkanDevice::GetPhysicalDeviceFeatures( @@ -268,6 +437,48 @@ std::vector VulkanDevice::GetQueueFamilyProperties() int VulkanDevice::ChooseSurfaceFormat(const VulkanSurface& surface, std::vector desired_formats, VkSurfaceFormatKHR* format) const { +#ifdef RS_ENABLE_VK + if (!surface.IsValid() || format == nullptr) { + LOGE("ChooseSurfaceFormat surface not valid or format == null"); + return -1; + } + + uint32_t format_count = 0; + if (VK_CALL_LOG_ERROR(vk.GetPhysicalDeviceSurfaceFormatsKHR( + physical_device_, surface.Handle(), &format_count, nullptr)) != + VK_SUCCESS) { + LOGE("ChooseSurfaceFormat sGetPhysicalDeviceSurfaceFormatsKHR not success"); + return -1; + } + + if (format_count == 0) { + LOGE("ChooseSurfaceFormat format count = 0"); + return -1; + } + + VkSurfaceFormatKHR formats[format_count]; + if (VK_CALL_LOG_ERROR(vk.GetPhysicalDeviceSurfaceFormatsKHR( + physical_device_, surface.Handle(), &format_count, formats)) != + VK_SUCCESS) { + LOGE("ChooseSurfaceFormat sGetPhysicalDeviceSurfaceFormatsKHR not success 2"); + return -1; + } + + std::map supported_formats; + for (uint32_t i = 0; i < format_count; i++) { + supported_formats[formats[i].format] = formats[i]; + } + + // Try to find the first supported format in the list of desired formats. + for (size_t i = 0; i < desired_formats.size(); ++i) { + auto found = supported_formats.find(desired_formats[i]); + if (found != supported_formats.end()) { + *format = found->second; + return static_cast(i); + } + } + LOGE("ChooseSurfaceFormat failded"); +#else #if OS_ANDROID if (!surface.IsValid() || format == nullptr) { return -1; @@ -304,13 +515,17 @@ int VulkanDevice::ChooseSurfaceFormat(const VulkanSurface& surface, return static_cast(i); } } -#endif +#endif // OS_ANDROID +#endif // RS_ENABLE_VK return -1; } bool VulkanDevice::ChoosePresentMode(const VulkanSurface& surface, VkPresentModeKHR* present_mode) const { if (!surface.IsValid() || present_mode == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("ChoosePresentMode surface not valid or presentmode is null"); +#endif return false; } diff --git a/engine/flutter/vulkan/vulkan_device.h b/engine/flutter/vulkan/vulkan_device.h index f050fb889c7a7adb9320fd142faa72a2f4bc8141..b92caf111ab405b98d4f0085aa658a02671caa2e 100644 --- a/engine/flutter/vulkan/vulkan_device.h +++ b/engine/flutter/vulkan/vulkan_device.h @@ -7,9 +7,11 @@ #include +#include "vulkan_handle.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/compiler_specific.h" #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_handle.h" +#endif namespace vulkan { @@ -37,34 +39,48 @@ class VulkanDevice { void ReleaseDeviceOwnership(); +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool GetSurfaceCapabilities(const VulkanSurface& surface, VkSurfaceCapabilitiesKHR* capabilities) const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool GetPhysicalDeviceFeatures(VkPhysicalDeviceFeatures* features) const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool GetPhysicalDeviceFeaturesSkia( uint32_t* /* mask of GrVkFeatureFlags */ features) const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif int ChooseSurfaceFormat(const VulkanSurface& surface, std::vector desired_formats, VkSurfaceFormatKHR* format) const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool ChoosePresentMode(const VulkanSurface& surface, VkPresentModeKHR* present_mode) const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool QueueSubmit(std::vector wait_dest_pipeline_stages, const std::vector& wait_semaphores, const std::vector& signal_semaphores, const std::vector& command_buffers, const VulkanHandle& fence) const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool WaitIdle() const; private: @@ -74,11 +90,16 @@ class VulkanDevice { VulkanHandle queue_; VulkanHandle command_pool_; uint32_t graphics_queue_index_; +#ifdef RS_ENABLE_VK + uint32_t compute_queue_index_; +#endif bool valid_; std::vector GetQueueFamilyProperties() const; +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanDevice); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_handle.h b/engine/flutter/vulkan/vulkan_handle.h index a829d0d10f6b700d03251d119edf411376cf6d2e..a85251a3a8cb0f390f8a2c4dcbf670f9a60d5f21 100644 --- a/engine/flutter/vulkan/vulkan_handle.h +++ b/engine/flutter/vulkan/vulkan_handle.h @@ -7,9 +7,11 @@ #include +#include "vulkan_interface.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/logging.h" #include "flutter/fml/macros.h" -#include "vulkan_interface.h" +#endif namespace vulkan { @@ -72,7 +74,9 @@ class VulkanHandle { disposer_ = nullptr; } +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanHandle); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_hilog.h b/engine/flutter/vulkan/vulkan_hilog.h new file mode 100644 index 0000000000000000000000000000000000000000..5710a275c8e7326778fab7c95c23f5649e634011 --- /dev/null +++ b/engine/flutter/vulkan/vulkan_hilog.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef VULKAN_HILOG_H +#define VULKAN_HILOG_H + +#include + +#define LOGD(fmt, ...) \ + ::OHOS::HiviewDFX::HiLog::Debug( \ + ::OHOS::HiviewDFX::HiLogLabel{LOG_CORE, 0xD001400, "VulkanShell"}, \ + "%{public}s: " fmt, __func__, ##__VA_ARGS__) +#define LOGI(fmt, ...) \ + ::OHOS::HiviewDFX::HiLog::Info( \ + ::OHOS::HiviewDFX::HiLogLabel{LOG_CORE, 0xD001400, "VulkanShell"}, \ + "%{public}s: " fmt, __func__, ##__VA_ARGS__) +#define LOGW(fmt, ...) \ + ::OHOS::HiviewDFX::HiLog::Warn( \ + ::OHOS::HiviewDFX::HiLogLabel{LOG_CORE, 0xD001400, "VulkanShell"}, \ + "%{public}s: " fmt, __func__, ##__VA_ARGS__) +#define LOGE(fmt, ...) \ + ::OHOS::HiviewDFX::HiLog::Error( \ + ::OHOS::HiviewDFX::HiLogLabel{LOG_CORE, 0xD001400, "VulkanShell"}, \ + "%{public}s: " fmt, __func__, ##__VA_ARGS__) + +#endif \ No newline at end of file diff --git a/engine/flutter/vulkan/vulkan_image.cc b/engine/flutter/vulkan/vulkan_image.cc index 492f9f310336aa03c3d7fe9bf42351f5ddf275c6..7f3b5eb9cdde4ab80b71f7993f6971d7b2743f22 100644 --- a/engine/flutter/vulkan/vulkan_image.cc +++ b/engine/flutter/vulkan/vulkan_image.cc @@ -2,10 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_image.h" +#include "vulkan_image.h" -#include "flutter/vulkan/vulkan_command_buffer.h" -#include "flutter/vulkan/vulkan_proc_table.h" +#include "vulkan_command_buffer.h" +#include "vulkan_proc_table.h" namespace vulkan { diff --git a/engine/flutter/vulkan/vulkan_image.h b/engine/flutter/vulkan/vulkan_image.h index ac4690b8ca589415f3ca63b21ef544efbd354167..c87a0ba266a36d2104be9f1f27e5b02f3f5c1bab 100644 --- a/engine/flutter/vulkan/vulkan_image.h +++ b/engine/flutter/vulkan/vulkan_image.h @@ -5,9 +5,11 @@ #ifndef FLUTTER_VULKAN_VULKAN_IMAGE_H_ #define FLUTTER_VULKAN_VULKAN_IMAGE_H_ +#include "vulkan_handle.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/compiler_specific.h" #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_handle.h" +#endif namespace vulkan { @@ -22,7 +24,9 @@ class VulkanImage { bool IsValid() const; +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool InsertImageMemoryBarrier(const VulkanCommandBuffer& command_buffer, VkPipelineStageFlagBits src_pipline_bits, VkPipelineStageFlagBits dest_pipline_bits, @@ -35,7 +39,9 @@ class VulkanImage { uint32_t /* mask of VkAccessFlagBits */ access_flags_; bool valid_; +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanImage); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_interface.cc b/engine/flutter/vulkan/vulkan_interface.cc index 56cb828e9db04223366dc6893340902b16a24d97..a0798d9fb67e8ebcf926763af4cd09ce0346b0e9 100644 --- a/engine/flutter/vulkan/vulkan_interface.cc +++ b/engine/flutter/vulkan/vulkan_interface.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/vulkan/vulkan_interface.h" +#include "vulkan_interface.h" namespace vulkan { @@ -58,8 +58,10 @@ std::string VulkanResultToString(VkResult result) { return "VK_ERROR_VALIDATION_FAILED_EXT"; case VK_ERROR_INVALID_SHADER_NV: return "VK_ERROR_INVALID_SHADER_NV"; +#ifndef RS_ENABLE_VK case VK_RESULT_RANGE_SIZE: return "VK_RESULT_RANGE_SIZE"; +#endif case VK_RESULT_MAX_ENUM: return "VK_RESULT_MAX_ENUM"; case VK_ERROR_INVALID_EXTERNAL_HANDLE: diff --git a/engine/flutter/vulkan/vulkan_interface.h b/engine/flutter/vulkan/vulkan_interface.h index 13f4ad9ffc44b90c8bfcafa4e79430150b6d6b58..5a9368529f389a0ae26ce5edc5aeea647870e618 100644 --- a/engine/flutter/vulkan/vulkan_interface.h +++ b/engine/flutter/vulkan/vulkan_interface.h @@ -7,9 +7,12 @@ #include +#ifndef RS_ENABLE_VK #include "flutter/fml/build_config.h" #include "flutter/fml/logging.h" +#endif +#ifndef RS_ENABLE_VK #if !defined(FUCHSIA_SDK) #define VULKAN_LINK_STATICALLY OS_FUCHSIA #endif // !defined(FUCHSIA_SDK) @@ -28,6 +31,13 @@ #define VK_USE_PLATFORM_FUCHSIA 1 #endif // VK_USE_PLATFORM_FUCHSIA #endif // OS_FUCHSIA +#endif // not define RS_ENABLE_VK + +#ifdef RS_ENABLE_VK +#ifndef VK_USE_PLATFORM_OHOS_KHR +#define VK_USE_PLATFORM_OHOS_KHR +#endif // VK_USE_PLATFORM_OHOS_KHR +#endif // RS_ENABLE_VK #if !VULKAN_LINK_STATICALLY #define VK_NO_PROTOTYPES 1 @@ -37,6 +47,17 @@ #ifndef NDEBUG +#ifdef RS_ENABLE_VK +#define VK_CALL_LOG_ERROR(expression) \ + ({ \ + __typeof__(expression) _rc = (expression); \ + if (_rc != VK_SUCCESS) { \ + LOGE("Vulkan call '" #expression "' failed with error %s", \ + vulkan::VulkanResultToString(_rc)); \ + } \ + _rc; \ + }) +#else #define VK_CALL_LOG_ERROR(expression) \ ({ \ __typeof__(expression) _rc = (expression); \ @@ -47,6 +68,7 @@ } \ _rc; \ }) +#endif #else // NDEBUG diff --git a/engine/flutter/vulkan/vulkan_native_surface.cc b/engine/flutter/vulkan/vulkan_native_surface.cc index e85023208344eea42b517eb68288c243df07d11d..4d10bf4a3578371afc9f885e67d4993e5b376de6 100644 --- a/engine/flutter/vulkan/vulkan_native_surface.cc +++ b/engine/flutter/vulkan/vulkan_native_surface.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/vulkan/vulkan_native_surface.h" +#include "vulkan_native_surface.h" namespace vulkan { diff --git a/engine/flutter/vulkan/vulkan_native_surface.h b/engine/flutter/vulkan/vulkan_native_surface.h index 0cf3c33fa54314ceca032b02bd42052b00d40974..495c9d1059a39d8e27fa8216914bff026dc99eff 100644 --- a/engine/flutter/vulkan/vulkan_native_surface.h +++ b/engine/flutter/vulkan/vulkan_native_surface.h @@ -5,10 +5,12 @@ #ifndef FLUTTER_VULKAN_VULKAN_NATIVE_SURFACE_H_ #define FLUTTER_VULKAN_VULKAN_NATIVE_SURFACE_H_ -#include "flutter/fml/macros.h" -#include "third_party/skia/include/core/SkSize.h" #include "vulkan_handle.h" #include "vulkan_proc_table.h" +#ifndef RS_ENABLE_VK +#include "flutter/fml/macros.h" +#endif +#include "third_party/skia/include/core/SkSize.h" namespace vulkan { diff --git a/engine/flutter/vulkan/vulkan_native_surface_android.h b/engine/flutter/vulkan/vulkan_native_surface_android.h index 9628eb5252925e33c47d4b5eed4a88a30fd4c3ce..0c4d6ac4488f16173639f36cd11906dfc4a5e831 100644 --- a/engine/flutter/vulkan/vulkan_native_surface_android.h +++ b/engine/flutter/vulkan/vulkan_native_surface_android.h @@ -36,7 +36,9 @@ class VulkanNativeSurfaceAndroid : public VulkanNativeSurface { private: ANativeWindow* native_window_; +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanNativeSurfaceAndroid); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_native_surface_ohos.cc b/engine/flutter/vulkan/vulkan_native_surface_ohos.cc new file mode 100644 index 0000000000000000000000000000000000000000..35f4333f34edf6cfb3087600fc1c07d6fc580980 --- /dev/null +++ b/engine/flutter/vulkan/vulkan_native_surface_ohos.cc @@ -0,0 +1,75 @@ +// 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 "vulkan_native_surface_ohos.h" + +#include "vulkan_hilog.h" +#include "window.h" +#include "third_party/skia/include/gpu/vk/GrVkBackendContext.h" +#include + +namespace vulkan { + +VulkanNativeSurfaceOHOS::VulkanNativeSurfaceOHOS(struct NativeWindow* native_window) : native_window_(native_window) +{ + if (native_window_ == nullptr) { + return; + } +} + +VulkanNativeSurfaceOHOS::~VulkanNativeSurfaceOHOS() +{ + if (native_window_ == nullptr) { + return; + } +} + +const char* VulkanNativeSurfaceOHOS::GetExtensionName() const +{ + return VK_OPENHARMONY_OHOS_SURFACE_EXTENSION_NAME; +} + +uint32_t VulkanNativeSurfaceOHOS::GetSkiaExtensionName() const +{ + return kKHR_ohos_surface_GrVkExtensionFlag; +} + +VkSurfaceKHR VulkanNativeSurfaceOHOS::CreateSurfaceHandle( + VulkanProcTable& vk, const VulkanHandle& instance) const +{ + if (!vk.IsValid() || !instance) { + LOGE("CreateSurfaceHandle vk or instance is not valid"); + return VK_NULL_HANDLE; + } + + const VkOHOSSurfaceCreateInfoOpenHarmony create_info = { + .sType = VK_STRUCTURE_TYPE_OHOS_SURFACE_CREATE_INFO_OPENHARMONY, + .pNext = nullptr, + .flags = 0, + .window = native_window_, + }; + + VkSurfaceKHR surface = VK_NULL_HANDLE; + + if (VK_CALL_LOG_ERROR(vk.CreateOHOSSurfaceOpenHarmony(instance, &create_info, nullptr, &surface)) != VK_SUCCESS) { + LOGE("CreateSurfaceHandle CreateOHOSSurfaceKHR not success"); + return VK_NULL_HANDLE; + } + + return surface; +} + +bool VulkanNativeSurfaceOHOS::IsValid() const +{ + return native_window_ != nullptr; +} + +SkISize VulkanNativeSurfaceOHOS::GetSize() const +{ + int width, height; + int err = NativeWindowHandleOpt(native_window_, GET_BUFFER_GEOMETRY, &height, &width); + return native_window_ == nullptr || err != OHOS::GSERROR_OK ? SkISize::Make(0, 0) : SkISize::Make(width, height); +} + +} // namespace vulkan \ No newline at end of file diff --git a/engine/flutter/vulkan/vulkan_native_surface_ohos.h b/engine/flutter/vulkan/vulkan_native_surface_ohos.h new file mode 100644 index 0000000000000000000000000000000000000000..b2fd553ed826990939291daadc8f632096fb67b8 --- /dev/null +++ b/engine/flutter/vulkan/vulkan_native_surface_ohos.h @@ -0,0 +1,39 @@ +// 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_VULKAN_VULKAN_NATIVE_SURFACE_OHOS_H_ +#define FLUTTER_VULKAN_VULKAN_NATIVE_SURFACE_OHOS_H_ + +#include "vulkan_native_surface.h" +#include "window.h" + +namespace vulkan { + +class VulkanNativeSurfaceOHOS : public VulkanNativeSurface { + public: + /// Create a native surface from the valid NativeWindow reference. Ownership + /// of the NativeWindow is assumed by this instance. + VulkanNativeSurfaceOHOS(struct NativeWindow* native_window); + + ~VulkanNativeSurfaceOHOS(); + + const char* GetExtensionName() const override; + + uint32_t GetSkiaExtensionName() const override; + + VkSurfaceKHR CreateSurfaceHandle( + VulkanProcTable& vk, + const VulkanHandle& instance) const override; + + bool IsValid() const override; + + SkISize GetSize() const override; + + private: + struct NativeWindow* native_window_; +}; + +} // namespace vulkan + +#endif // FLUTTER_VULKAN_VULKAN_NATIVE_SURFACE_OHOS_H_ diff --git a/engine/flutter/vulkan/vulkan_proc_table.cc b/engine/flutter/vulkan/vulkan_proc_table.cc index 4b106fb37083393af6dda8341e7364ec32f0e558..b4fe1631101d836a0d1aaea9a49aa3efb0c5f458 100644 --- a/engine/flutter/vulkan/vulkan_proc_table.cc +++ b/engine/flutter/vulkan/vulkan_proc_table.cc @@ -2,17 +2,30 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_proc_table.h" +#include "vulkan_proc_table.h" #include +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#ifndef RS_ENABLE_VK #include "flutter/fml/logging.h" +#endif +#ifdef RS_ENABLE_VK +#define ACQUIRE_PROC(name, context) \ + if (!(name = AcquireProc("vk" #name, context))) { \ + LOGE("Could not acquire proc: vk" #name); \ + return false; \ + } +#else #define ACQUIRE_PROC(name, context) \ if (!(name = AcquireProc("vk" #name, context))) { \ FML_DLOG(INFO) << "Could not acquire proc: vk" << #name; \ return false; \ } +#endif namespace vulkan { @@ -47,6 +60,18 @@ bool VulkanProcTable::SetupLoaderProcAddresses() { return true; } +#ifdef RS_ENABLE_VK + GetInstanceProcAddr = +#if VULKAN_LINK_STATICALLY + GetInstanceProcAddr = &vkGetInstanceProcAddr; +#else // VULKAN_LINK_STATICALLY + reinterpret_cast(dlsym(handle_, "vkGetInstanceProcAddr")); + GetDeviceProcAddr = reinterpret_cast(dlsym(handle_, "vkGetDeviceProcAddr")); + EnumerateInstanceExtensionProperties = reinterpret_cast( + dlsym(handle_, "vkEnumerateInstanceExtensionProperties")); + CreateInstance = reinterpret_cast(dlsym(handle_, "vkCreateInstance")); +#endif // VULKAN_LINK_STATICALLY +#else GetInstanceProcAddr = #if VULKAN_LINK_STATICALLY GetInstanceProcAddr = &vkGetInstanceProcAddr; @@ -54,16 +79,23 @@ bool VulkanProcTable::SetupLoaderProcAddresses() { reinterpret_cast( dlsym(handle_, "vkGetInstanceProcAddr")); #endif // VULKAN_LINK_STATICALLY +#endif if (!GetInstanceProcAddr) { +#ifdef RS_ENABLE_VK + LOGE("Could not acquire vkGetInstanceProcAddr."); +#else FML_DLOG(WARNING) << "Could not acquire vkGetInstanceProcAddr."; +#endif return false; } VulkanHandle null_instance(VK_NULL_HANDLE, nullptr); +#ifndef RS_ENABLE_VK ACQUIRE_PROC(CreateInstance, null_instance); ACQUIRE_PROC(EnumerateInstanceExtensionProperties, null_instance); +#endif ACQUIRE_PROC(EnumerateInstanceLayerProperties, null_instance); return true; @@ -76,6 +108,16 @@ bool VulkanProcTable::SetupInstanceProcAddresses( ACQUIRE_PROC(DestroyInstance, handle); ACQUIRE_PROC(EnumerateDeviceLayerProperties, handle); ACQUIRE_PROC(EnumeratePhysicalDevices, handle); +#ifdef RS_ENABLE_VK + ACQUIRE_PROC(GetPhysicalDeviceFeatures, handle); + ACQUIRE_PROC(GetPhysicalDeviceQueueFamilyProperties, handle); + ACQUIRE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR, handle); + ACQUIRE_PROC(GetPhysicalDeviceSurfaceFormatsKHR, handle); + ACQUIRE_PROC(GetPhysicalDeviceSurfacePresentModesKHR, handle); + ACQUIRE_PROC(GetPhysicalDeviceSurfaceSupportKHR, handle); + ACQUIRE_PROC(DestroySurfaceKHR, handle); + ACQUIRE_PROC(CreateOHOSSurfaceOpenHarmony, handle); +#else ACQUIRE_PROC(GetDeviceProcAddr, handle); ACQUIRE_PROC(GetPhysicalDeviceFeatures, handle); ACQUIRE_PROC(GetPhysicalDeviceQueueFamilyProperties, handle); @@ -87,17 +129,20 @@ bool VulkanProcTable::SetupInstanceProcAddresses( ACQUIRE_PROC(DestroySurfaceKHR, handle); ACQUIRE_PROC(CreateAndroidSurfaceKHR, handle); #endif // OS_ANDROID +#endif // RS_ENABLE_VK // The debug report functions are optional. We don't want proc acquisition to // fail here because the optional methods were not present (since ACQUIRE_PROC // returns false on failure). Wrap the optional proc acquisitions in an // anonymous lambda and invoke it. We don't really care about the result since // users of Debug reporting functions check for their presence explicitly. +#ifndef RS_ENABLE_VK [this, &handle]() -> bool { ACQUIRE_PROC(CreateDebugReportCallbackEXT, handle); ACQUIRE_PROC(DestroyDebugReportCallbackEXT, handle); return true; }(); +#endif instance_ = {handle, nullptr}; return true; @@ -129,6 +174,13 @@ bool VulkanProcTable::SetupDeviceProcAddresses( ACQUIRE_PROC(ResetCommandBuffer, handle); ACQUIRE_PROC(ResetFences, handle); ACQUIRE_PROC(WaitForFences, handle); +#ifdef RS_ENABLE_VK + ACQUIRE_PROC(AcquireNextImageKHR, handle); + ACQUIRE_PROC(CreateSwapchainKHR, handle); + ACQUIRE_PROC(DestroySwapchainKHR, handle); + ACQUIRE_PROC(GetSwapchainImagesKHR, handle); + ACQUIRE_PROC(QueuePresentKHR, handle); +#else #if OS_ANDROID ACQUIRE_PROC(AcquireNextImageKHR, handle); ACQUIRE_PROC(CreateSwapchainKHR, handle); @@ -140,6 +192,7 @@ bool VulkanProcTable::SetupDeviceProcAddresses( ACQUIRE_PROC(GetMemoryZirconHandleFUCHSIA, handle); ACQUIRE_PROC(ImportSemaphoreZirconHandleFUCHSIA, handle); #endif // OS_FUCHSIA +#endif // RS_ENABLE_VK device_ = {handle, nullptr}; return true; } @@ -153,7 +206,11 @@ bool VulkanProcTable::OpenLibraryHandle() { dlerror(); // clear existing errors on thread. handle_ = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL); if (handle_ == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("Could not open the vulkan library: %s", dlerror()); +#else FML_DLOG(WARNING) << "Could not open the vulkan library: " << dlerror(); +#endif return false; } return true; @@ -168,9 +225,15 @@ bool VulkanProcTable::CloseLibraryHandle() { if (handle_ != nullptr) { dlerror(); // clear existing errors on thread. if (dlclose(handle_) != 0) { +#ifdef RS_ENABLE_VK + LOGE("Could not close the vulkan library handle. This " + "indicates a leak."); + LOGE("%s", dlerror()); +#else FML_DLOG(ERROR) << "Could not close the vulkan library handle. This " "indicates a leak."; FML_DLOG(ERROR) << dlerror(); +#endif } handle_ = nullptr; } diff --git a/engine/flutter/vulkan/vulkan_proc_table.h b/engine/flutter/vulkan/vulkan_proc_table.h index 8164fb785ca64af4fc4fa91518047f3ff0f5be78..14bd1fefce5fa84b1068cd377b6bcca0385b974e 100644 --- a/engine/flutter/vulkan/vulkan_proc_table.h +++ b/engine/flutter/vulkan/vulkan_proc_table.h @@ -5,6 +5,131 @@ #ifndef FLUTTER_VULKAN_VULKAN_PROC_TABLE_H_ #define FLUTTER_VULKAN_VULKAN_PROC_TABLE_H_ +#include "vulkan_handle.h" +#include "vulkan_interface.h" +#include "third_party/skia/include/core/SkRefCnt.h" +#include "third_party/skia/include/gpu/vk/GrVkBackendContext.h" + +namespace vulkan { + +#ifdef RS_ENABLE_VK +class VulkanProcTable { + public: + template + class Proc { + public: + using Proto = T; + + Proc(T proc = nullptr) : proc_(proc) {} + + ~Proc() { proc_ = nullptr; } + + Proc operator=(T proc) { + proc_ = proc; + return *this; + } + + Proc operator=(PFN_vkVoidFunction proc) { + proc_ = reinterpret_cast(proc); + return *this; + } + + operator bool() const { return proc_ != nullptr; } + + operator T() const { return proc_; } + + private: + T proc_; + }; + + VulkanProcTable(); + + ~VulkanProcTable(); + + bool HasAcquiredMandatoryProcAddresses() const; + + bool IsValid() const; + + bool AreInstanceProcsSetup() const; + + bool AreDeviceProcsSetup() const; + + bool SetupInstanceProcAddresses(const VulkanHandle& instance); + + bool SetupDeviceProcAddresses(const VulkanHandle& device); + + GrVkGetProc CreateSkiaGetProc() const; + +#define DEFINE_PROC(name) Proc name + + DEFINE_PROC(AcquireNextImageKHR); + DEFINE_PROC(AllocateCommandBuffers); + DEFINE_PROC(AllocateMemory); + DEFINE_PROC(BeginCommandBuffer); + DEFINE_PROC(BindImageMemory); + DEFINE_PROC(CmdPipelineBarrier); + DEFINE_PROC(CreateCommandPool); + DEFINE_PROC(CreateDebugReportCallbackEXT); + DEFINE_PROC(CreateDevice); + DEFINE_PROC(CreateFence); + DEFINE_PROC(CreateImage); + DEFINE_PROC(CreateInstance); + DEFINE_PROC(CreateSemaphore); + DEFINE_PROC(CreateSwapchainKHR); + DEFINE_PROC(DestroyCommandPool); + DEFINE_PROC(DestroyDebugReportCallbackEXT); + DEFINE_PROC(DestroyDevice); + DEFINE_PROC(DestroyFence); + DEFINE_PROC(DestroyImage); + DEFINE_PROC(DestroyInstance); + DEFINE_PROC(DestroySemaphore); + DEFINE_PROC(DestroySurfaceKHR); + DEFINE_PROC(DestroySwapchainKHR); + DEFINE_PROC(DeviceWaitIdle); + DEFINE_PROC(EndCommandBuffer); + DEFINE_PROC(EnumerateDeviceLayerProperties); + DEFINE_PROC(EnumerateInstanceExtensionProperties); + DEFINE_PROC(EnumerateInstanceLayerProperties); + DEFINE_PROC(EnumeratePhysicalDevices); + DEFINE_PROC(FreeCommandBuffers); + DEFINE_PROC(FreeMemory); + DEFINE_PROC(GetDeviceProcAddr); + DEFINE_PROC(GetDeviceQueue); + DEFINE_PROC(GetImageMemoryRequirements); + DEFINE_PROC(GetInstanceProcAddr); + DEFINE_PROC(GetPhysicalDeviceFeatures); + DEFINE_PROC(GetPhysicalDeviceQueueFamilyProperties); + DEFINE_PROC(QueueSubmit); + DEFINE_PROC(QueueWaitIdle); + DEFINE_PROC(ResetCommandBuffer); + DEFINE_PROC(ResetFences); + DEFINE_PROC(WaitForFences); + DEFINE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); + DEFINE_PROC(GetPhysicalDeviceSurfaceFormatsKHR); + DEFINE_PROC(GetPhysicalDeviceSurfacePresentModesKHR); + DEFINE_PROC(GetPhysicalDeviceSurfaceSupportKHR); + DEFINE_PROC(GetSwapchainImagesKHR); + DEFINE_PROC(QueuePresentKHR); + DEFINE_PROC(CreateOHOSSurfaceOpenHarmony); + +#undef DEFINE_PROC + + private: + void* handle_; + bool acquired_mandatory_proc_addresses_; + VulkanHandle instance_; + VulkanHandle device_; + + bool OpenLibraryHandle(); + bool SetupLoaderProcAddresses(); + bool CloseLibraryHandle(); + PFN_vkVoidFunction AcquireProc( + const char* proc_name, + const VulkanHandle& instance) const; + PFN_vkVoidFunction AcquireProc(const char* proc_name, + const VulkanHandle& device) const; +}; +#else #include "flutter/fml/macros.h" #include "flutter/fml/memory/ref_counted.h" #include "flutter/fml/memory/ref_ptr.h" @@ -140,7 +265,7 @@ class VulkanProcTable : public fml::RefCountedThreadSafe { FML_DISALLOW_COPY_AND_ASSIGN(VulkanProcTable); }; - +#endif // RS_ENABLE_VK } // namespace vulkan #endif // FLUTTER_VULKAN_VULKAN_PROC_TABLE_H_ diff --git a/engine/flutter/vulkan/vulkan_provider.cc b/engine/flutter/vulkan/vulkan_provider.cc index 4b8cb5433e62274108c99c34b71dd5bea58a4399..55dc8b6fa1b5f390a2611de4223671bffe275f05 100644 --- a/engine/flutter/vulkan/vulkan_provider.cc +++ b/engine/flutter/vulkan/vulkan_provider.cc @@ -2,10 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_provider.h" +#include "vulkan_provider.h" namespace vulkan { - +#ifndef RS_ENABLE_VK vulkan::VulkanHandle VulkanProvider::CreateFence() { const VkFenceCreateInfo create_info = { .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, @@ -21,5 +21,6 @@ vulkan::VulkanHandle VulkanProvider::CreateFence() { vk().DestroyFence(vk_device(), fence, nullptr); }}; } +#endif } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_provider.h b/engine/flutter/vulkan/vulkan_provider.h index eca43f53a0a3bd166409c6536c938fab41a42123..973f5cce954342d827081d21af56e4eb45253053 100644 --- a/engine/flutter/vulkan/vulkan_provider.h +++ b/engine/flutter/vulkan/vulkan_provider.h @@ -5,7 +5,8 @@ #ifndef FLUTTER_VULKAN_VULKAN_PROVIDER_H_ #define FLUTTER_VULKAN_VULKAN_PROVIDER_H_ -#include "flutter/vulkan/vulkan_handle.h" +#include "vulkan_handle.h" +#include "vulkan_proc_table.h" namespace vulkan { diff --git a/engine/flutter/vulkan/vulkan_surface.cc b/engine/flutter/vulkan/vulkan_surface.cc index 21224724d4f295a30bd0752c48419b10eeeec35f..b1e2bfd233c0253e7f1d18bd5c6ed250f1005cc2 100644 --- a/engine/flutter/vulkan/vulkan_surface.cc +++ b/engine/flutter/vulkan/vulkan_surface.cc @@ -2,10 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_surface.h" +#include "vulkan_surface.h" -#include "flutter/vulkan/vulkan_application.h" -#include "flutter/vulkan/vulkan_native_surface.h" +#include "vulkan_application.h" +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#include "vulkan_native_surface.h" namespace vulkan { @@ -18,7 +21,11 @@ VulkanSurface::VulkanSurface( native_surface_(std::move(native_surface)), valid_(false) { if (native_surface_ == nullptr || !native_surface_->IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("Native surface was invalid."); +#else FML_DLOG(INFO) << "Native surface was invalid."; +#endif return; } @@ -26,7 +33,11 @@ VulkanSurface::VulkanSurface( native_surface_->CreateSurfaceHandle(vk, application.GetInstance()); if (surface == VK_NULL_HANDLE) { +#ifdef RS_ENABLE_VK + LOGE("Could not create the surface handle."); +#else FML_DLOG(INFO) << "Could not create the surface handle."; +#endif return; } diff --git a/engine/flutter/vulkan/vulkan_surface.h b/engine/flutter/vulkan/vulkan_surface.h index c1be63ed0ce7119eacaad5b5440300fb7ec68985..8189e51747c0e423b2fe3d1ab8022d6a53ecde00 100644 --- a/engine/flutter/vulkan/vulkan_surface.h +++ b/engine/flutter/vulkan/vulkan_surface.h @@ -5,8 +5,10 @@ #ifndef FLUTTER_VULKAN_VULKAN_SURFACE_H_ #define FLUTTER_VULKAN_VULKAN_SURFACE_H_ +#include "vulkan_handle.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_handle.h" +#endif #include "third_party/skia/include/core/SkSize.h" namespace vulkan { @@ -39,7 +41,9 @@ class VulkanSurface { VulkanHandle surface_; bool valid_; +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanSurface); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_swapchain.cc b/engine/flutter/vulkan/vulkan_swapchain.cc index c69e9d263f7d096577c2f75422edd6222a198ef9..dd030a682721e586fa75f23e069ea419f3e74514 100644 --- a/engine/flutter/vulkan/vulkan_swapchain.cc +++ b/engine/flutter/vulkan/vulkan_swapchain.cc @@ -2,13 +2,16 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_swapchain.h" - -#include "flutter/vulkan/vulkan_backbuffer.h" -#include "flutter/vulkan/vulkan_device.h" -#include "flutter/vulkan/vulkan_image.h" -#include "flutter/vulkan/vulkan_proc_table.h" -#include "flutter/vulkan/vulkan_surface.h" +#include "vulkan_swapchain.h" + +#include "vulkan_backbuffer.h" +#include "vulkan_device.h" +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#include "vulkan_image.h" +#include "vulkan_proc_table.h" +#include "vulkan_surface.h" #include "third_party/skia/include/gpu/GrBackendSurface.h" #include "third_party/skia/include/gpu/GrContext.h" #include "third_party/skia/include/gpu/vk/GrVkTypes.h" @@ -23,6 +26,20 @@ struct FormatInfo { }; } // namespace +#ifdef RS_ENABLE_VK +static std::vector DesiredFormatInfos() { + return {{VK_FORMAT_R8G8B8A8_SRGB, kRGBA_8888_SkColorType, + SkColorSpace::MakeSRGBLinear()}, + {VK_FORMAT_B8G8R8A8_SRGB, kBGRA_8888_SkColorType, + SkColorSpace::MakeSRGBLinear()}, + {VK_FORMAT_R16G16B16A16_SFLOAT, kRGBA_F16_SkColorType, + SkColorSpace::MakeSRGBLinear()}, + {VK_FORMAT_R8G8B8A8_UNORM, kRGBA_8888_SkColorType, + SkColorSpace::MakeSRGB()}, + {VK_FORMAT_B8G8R8A8_UNORM, kRGBA_8888_SkColorType, + SkColorSpace::MakeSRGB()}}; +} +#else static std::vector DesiredFormatInfos() { return {{VK_FORMAT_R8G8B8A8_SRGB, kRGBA_8888_SkColorType, SkColorSpace::MakeSRGB()}, @@ -35,6 +52,7 @@ static std::vector DesiredFormatInfos() { {VK_FORMAT_B8G8R8A8_UNORM, kRGBA_8888_SkColorType, SkColorSpace::MakeSRGB()}}; } +#endif VulkanSwapchain::VulkanSwapchain(const VulkanProcTable& p_vk, const VulkanDevice& device, @@ -51,12 +69,20 @@ VulkanSwapchain::VulkanSwapchain(const VulkanProcTable& p_vk, current_image_index_(0), valid_(false) { if (!device_.IsValid() || !surface.IsValid() || skia_context == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("Device or surface is invalid."); +#else FML_DLOG(INFO) << "Device or surface is invalid."; +#endif return; } if (!device_.GetSurfaceCapabilities(surface, &capabilities_)) { +#ifdef RS_ENABLE_VK + LOGE("Could not find surface capabilities."); +#else FML_DLOG(INFO) << "Could not find surface capabilities."; +#endif return; } @@ -74,13 +100,21 @@ VulkanSwapchain::VulkanSwapchain(const VulkanProcTable& p_vk, int format_index = device_.ChooseSurfaceFormat(surface, desired_formats, &surface_format_); if (format_index < 0) { +#ifdef RS_ENABLE_VK + LOGE("Could not choose surface format."); +#else FML_DLOG(INFO) << "Could not choose surface format."; +#endif return; } VkPresentModeKHR present_mode = VK_PRESENT_MODE_FIFO_KHR; if (!device_.ChoosePresentMode(surface, &present_mode)) { +#ifdef RS_ENABLE_VK + LOGE("Could not choose present mode."); +#else FML_DLOG(INFO) << "Could not choose present mode."; +#endif return; } @@ -92,12 +126,20 @@ VulkanSwapchain::VulkanSwapchain(const VulkanProcTable& p_vk, queue_family_index, // queue family surface.Handle(), // surface to test &supported)) != VK_SUCCESS) { +#ifdef RS_ENABLE_VK + LOGE("Could not get physical device surface support."); +#else FML_DLOG(INFO) << "Could not get physical device surface support."; +#endif return; } if (supported != VK_TRUE) { +#ifdef RS_ENABLE_VK + LOGE("Surface was not supported by the physical device."); +#else FML_DLOG(INFO) << "Surface was not supported by the physical device."; +#endif return; } @@ -139,20 +181,35 @@ VulkanSwapchain::VulkanSwapchain(const VulkanProcTable& p_vk, if (VK_CALL_LOG_ERROR(vk.CreateSwapchainKHR(device_.GetHandle(), &create_info, nullptr, &swapchain)) != VK_SUCCESS) { +#ifdef RS_ENABLE_VK + LOGE("Could not create the swapchain."); +#else FML_DLOG(INFO) << "Could not create the swapchain."; +#endif return; } +#ifdef RS_ENABLE_VK + swapchain_ = {swapchain, [this](VkSwapchainKHR swapchain) { + device_.WaitIdle(); + vk.DestroySwapchainKHR(device_.GetHandle(), swapchain, nullptr); + }}; +#else swapchain_ = {swapchain, [this](VkSwapchainKHR swapchain) { FML_ALLOW_UNUSED_LOCAL(device_.WaitIdle()); vk.DestroySwapchainKHR(device_.GetHandle(), swapchain, nullptr); }}; +#endif if (!CreateSwapchainImages(skia_context, format_infos[format_index].color_type_, format_infos[format_index].color_space_)) { +#ifdef RS_ENABLE_VK + LOGE("Could not create swapchain images."); +#else FML_DLOG(INFO) << "Could not create swapchain images."; +#endif return; } @@ -287,9 +344,17 @@ bool VulkanSwapchain::CreateSwapchainImages(GrContext* skia_context, surfaces_.emplace_back(std::move(surface)); } - +#ifdef RS_ENABLE_VK + if (backbuffers_.size() != images_.size()) { + LOGE("backbuffers_.size() != images_.size()"); + } + if (images_.size() != surfaces_.size()) { + LOGE("images_.size() != surfaces_.size()"); + } +#else FML_DCHECK(backbuffers_.size() == images_.size()); FML_DCHECK(images_.size() == surfaces_.size()); +#endif return true; } @@ -318,7 +383,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { AcquireResult error = {AcquireStatus::ErrorSurfaceLost, nullptr}; if (!IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("Swapchain was invalid."); +#else FML_DLOG(INFO) << "Swapchain was invalid."; +#endif return error; } @@ -329,7 +398,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { auto backbuffer = GetNextBackbuffer(); if (backbuffer == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("Could not get the next backbuffer."); +#else FML_DLOG(INFO) << "Could not get the next backbuffer."; +#endif return error; } @@ -338,7 +411,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { // Wait for use readiness. // --------------------------------------------------------------------------- if (!backbuffer->WaitFences()) { +#ifdef RS_ENABLE_VK + LOGE("Failed waiting on fences."); +#else FML_DLOG(INFO) << "Failed waiting on fences."; +#endif return error; } @@ -347,7 +424,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { // Put semaphores in unsignaled state. // --------------------------------------------------------------------------- if (!backbuffer->ResetFences()) { +#ifdef RS_ENABLE_VK + LOGE("Could not reset fences."); +#else FML_DLOG(INFO) << "Could not reset fences."; +#endif return error; } @@ -373,20 +454,32 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { case VK_ERROR_SURFACE_LOST_KHR: return {AcquireStatus::ErrorSurfaceLost, nullptr}; default: +#ifdef RS_ENABLE_VK + LOGE("Unexpected result from AcquireNextImageKHR: %d", acquire_result); +#else FML_LOG(INFO) << "Unexpected result from AcquireNextImageKHR: " << acquire_result; +#endif return {AcquireStatus::ErrorSurfaceLost, nullptr}; } // Simple sanity checking of image index. if (next_image_index >= images_.size()) { +#ifdef RS_ENABLE_VK + LOGE("Image index returned was out-of-bounds."); +#else FML_DLOG(INFO) << "Image index returned was out-of-bounds."; +#endif return error; } auto& image = images_[next_image_index]; if (!image->IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("Image at index was invalid."); +#else FML_DLOG(INFO) << "Image at index was invalid."; +#endif return error; } @@ -395,7 +488,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { // Start recording to the command buffer. // --------------------------------------------------------------------------- if (!backbuffer->GetUsageCommandBuffer().Begin()) { +#ifdef RS_ENABLE_VK + LOGE("Could not begin recording to the command buffer."); +#else FML_DLOG(INFO) << "Could not begin recording to the command buffer."; +#endif return error; } @@ -415,7 +512,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, // dest_access_flags destination_image_layout // dest_layout )) { +#ifdef RS_ENABLE_VK + LOGE("Could not insert image memory barrier."); +#else FML_DLOG(INFO) << "Could not insert image memory barrier."; +#endif return error; } else { current_pipeline_stage_ = destination_pipeline_stage; @@ -426,7 +527,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { // End recording to the command buffer. // --------------------------------------------------------------------------- if (!backbuffer->GetUsageCommandBuffer().End()) { +#ifdef RS_ENABLE_VK + LOGE("Could not end recording to the command buffer."); +#else FML_DLOG(INFO) << "Could not end recording to the command buffer."; +#endif return error; } @@ -446,7 +551,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { command_buffers, // command_buffers backbuffer->GetUsageFence() // fence )) { +#ifdef RS_ENABLE_VK + LOGE("Could not submit to the device queue."); +#else FML_DLOG(INFO) << "Could not submit to the device queue."; +#endif return error; } @@ -457,14 +566,22 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { sk_sp surface = surfaces_[next_image_index]; if (surface == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("Could not access surface at the image index."); +#else FML_DLOG(INFO) << "Could not access surface at the image index."; +#endif return error; } GrBackendRenderTarget backendRT = surface->getBackendRenderTarget( SkSurface::kFlushRead_BackendHandleAccess); if (!backendRT.isValid()) { +#ifdef RS_ENABLE_VK + LOGE("Could not get backend render target."); +#else FML_DLOG(INFO) << "Could not get backend render target."; +#endif return error; } backendRT.setVkImageLayout(destination_image_layout); @@ -476,7 +593,11 @@ VulkanSwapchain::AcquireResult VulkanSwapchain::AcquireSurface() { bool VulkanSwapchain::Submit() { if (!IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("Swapchain was invalid."); +#else FML_DLOG(INFO) << "Swapchain was invalid."; +#endif return false; } @@ -495,7 +616,11 @@ bool VulkanSwapchain::Submit() { // Start recording to the command buffer. // --------------------------------------------------------------------------- if (!backbuffer->GetRenderCommandBuffer().Begin()) { +#ifdef RS_ENABLE_VK + LOGE("Could not start recording to the command buffer."); +#else FML_DLOG(INFO) << "Could not start recording to the command buffer."; +#endif return false; } @@ -514,7 +639,11 @@ bool VulkanSwapchain::Submit() { VK_ACCESS_MEMORY_READ_BIT, // dest_access_flags destination_image_layout // dest_layout )) { +#ifdef RS_ENABLE_VK + LOGE("Could not insert memory barrier."); +#else FML_DLOG(INFO) << "Could not insert memory barrier."; +#endif return false; } else { current_pipeline_stage_ = destination_pipeline_stage; @@ -525,7 +654,11 @@ bool VulkanSwapchain::Submit() { // End recording to the command buffer. // --------------------------------------------------------------------------- if (!backbuffer->GetRenderCommandBuffer().End()) { +#ifdef RS_ENABLE_VK + LOGE("Could not end recording to the command buffer."); +#else FML_DLOG(INFO) << "Could not end recording to the command buffer."; +#endif return false; } @@ -547,7 +680,11 @@ bool VulkanSwapchain::Submit() { command_buffers, // command_buffers backbuffer->GetRenderFence() // fence )) { +#ifdef RS_ENABLE_VK + LOGE("Could not submit to the device queue."); +#else FML_DLOG(INFO) << "Could not submit to the device queue."; +#endif return false; } @@ -571,7 +708,11 @@ bool VulkanSwapchain::Submit() { if (VK_CALL_LOG_ERROR(vk.QueuePresentKHR(device_.GetQueueHandle(), &present_info)) != VK_SUCCESS) { +#ifdef RS_ENABLE_VK + LOGE("Could not submit the present operation."); +#else FML_DLOG(INFO) << "Could not submit the present operation."; +#endif return false; } diff --git a/engine/flutter/vulkan/vulkan_swapchain.h b/engine/flutter/vulkan/vulkan_swapchain.h index 047b27ca6b157326b7882e87e0c56eedbb414784..c99ef5fbb185b4a32617840cc8fad5129f0e2011 100644 --- a/engine/flutter/vulkan/vulkan_swapchain.h +++ b/engine/flutter/vulkan/vulkan_swapchain.h @@ -9,9 +9,11 @@ #include #include +#include "vulkan_handle.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/compiler_specific.h" #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_handle.h" +#endif #include "third_party/skia/include/core/SkSize.h" #include "third_party/skia/include/core/SkSurface.h" @@ -56,11 +58,42 @@ class VulkanSwapchain { /// Submit a previously acquired. There must not be consecutive calls to /// |Submit| without and interleaving |AcquireFrame|. +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool Submit(); SkISize GetSize() const; +#ifdef RS_ENABLE_VK + private: + const VulkanProcTable& vk; + const VulkanDevice& device_; + VkSurfaceCapabilitiesKHR capabilities_; + VkSurfaceFormatKHR surface_format_; + VulkanHandle swapchain_; + std::vector> backbuffers_; + std::vector> images_; + std::vector> surfaces_; + VkPipelineStageFlagBits current_pipeline_stage_; + size_t current_backbuffer_index_; + size_t current_image_index_; + bool valid_; + + std::vector GetImages() const; + + bool CreateSwapchainImages(GrContext* skia_context, + SkColorType color_type, + sk_sp color_space); + + sk_sp CreateSkiaSurface(GrContext* skia_context, + VkImage image, + const SkISize& size, + SkColorType color_type, + sk_sp color_space) const; + + VulkanBackbuffer* GetNextBackbuffer(); +#else #if OS_ANDROID private: const VulkanProcTable& vk; @@ -90,8 +123,11 @@ class VulkanSwapchain { VulkanBackbuffer* GetNextBackbuffer(); #endif // OS_ANDROID +#endif // RS_ENABLE_VK +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanSwapchain); +#endif }; } // namespace vulkan diff --git a/engine/flutter/vulkan/vulkan_swapchain_stub.cc b/engine/flutter/vulkan/vulkan_swapchain_stub.cc index bdd96ad729858bbe240df7062af680595070fe86..8d9416e4dcf9d1e83c6a14b54756a71dac32c0a6 100644 --- a/engine/flutter/vulkan/vulkan_swapchain_stub.cc +++ b/engine/flutter/vulkan/vulkan_swapchain_stub.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/vulkan/vulkan_swapchain.h" +#include "vulkan_swapchain.h" namespace vulkan { diff --git a/engine/flutter/vulkan/vulkan_utilities.cc b/engine/flutter/vulkan/vulkan_utilities.cc index 6d952777a6cfdcf06557577138ea219b7e396541..a8db9d87bfe88ff33a4157b91b1226c06e1d9ade 100644 --- a/engine/flutter/vulkan/vulkan_utilities.cc +++ b/engine/flutter/vulkan/vulkan_utilities.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/vulkan/vulkan_utilities.h" +#include "vulkan_utilities.h" #include #include diff --git a/engine/flutter/vulkan/vulkan_utilities.h b/engine/flutter/vulkan/vulkan_utilities.h index 2e071075a26719e70f61987a7d781e05bf7e6cf5..bb1e42e3dc4d8ca97992b32e6d1bc46efa1ab6ab 100644 --- a/engine/flutter/vulkan/vulkan_utilities.h +++ b/engine/flutter/vulkan/vulkan_utilities.h @@ -8,9 +8,11 @@ #include #include +#include "vulkan_handle.h" +#include "vulkan_proc_table.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_handle.h" -#include "flutter/vulkan/vulkan_proc_table.h" +#endif namespace vulkan { diff --git a/engine/flutter/vulkan/vulkan_window.cc b/engine/flutter/vulkan/vulkan_window.cc index 44ef7f4a22723dcce036d042449cfd24c5648618..048d0ea3beceadac312c3603b2635a07ecf6289e 100644 --- a/engine/flutter/vulkan/vulkan_window.cc +++ b/engine/flutter/vulkan/vulkan_window.cc @@ -2,20 +2,105 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/vulkan/vulkan_window.h" +#include "vulkan_window.h" #include #include -#include "flutter/vulkan/vulkan_application.h" -#include "flutter/vulkan/vulkan_device.h" -#include "flutter/vulkan/vulkan_native_surface.h" -#include "flutter/vulkan/vulkan_surface.h" -#include "flutter/vulkan/vulkan_swapchain.h" +#include "vulkan_application.h" +#include "vulkan_device.h" +#ifdef RS_ENABLE_VK +#include "vulkan_hilog.h" +#endif +#include "vulkan_native_surface.h" +#include "vulkan_surface.h" +#include "vulkan_swapchain.h" #include "third_party/skia/include/gpu/GrContext.h" namespace vulkan { +#ifdef RS_ENABLE_VK +VulkanProcTable* VulkanWindow::vk; +std::unique_ptr VulkanWindow::application_; +std::unique_ptr VulkanWindow::logical_device_; + +void VulkanWindow::InitializeVulkan() +{ + if (logical_device_ != nullptr) { + LOGI("Vulkan Already Initialized"); + return; + } + LOGI("First Initialize Vulkan"); + vk = new VulkanProcTable(); + if (!vk->HasAcquiredMandatoryProcAddresses()) { + LOGE("Proc table has not acquired mandatory proc addresses."); + return; + } + + // Create the application instance. + std::vector extensions = { + VK_KHR_SURFACE_SPEC_VERSION, // parent extension + VK_OPENHARMONY_OHOS_SURFACE_EXTENSION_NAME // child extension + }; + + application_ = std::make_unique(*vk, "Rosen", std::move(extensions)); + if (!application_->IsValid() || !vk->AreInstanceProcsSetup()) { + // Make certain the application instance was created and it setup the + // instance proc table entries. + LOGE("Instance proc addresses have not been setup."); + return; + } + + // Create the device. + logical_device_ = application_->AcquireFirstCompatibleLogicalDevice(); + if (logical_device_ == nullptr || !logical_device_->IsValid() || !vk->AreDeviceProcsSetup()) { + // Make certain the device was created and it setup the device proc table + // entries. + LOGE("Device proc addresses have not been setup."); + return; + } +} + +VulkanWindow::VulkanWindow(std::unique_ptr native_surface, bool is_offscreen) + : valid_(false), is_offscreen_(is_offscreen) +{ + LOGE("VulkanWindow init enter"); + + InitializeVulkan(); + if (logical_device_ == nullptr) { + LOGE("InitializeVulkan failed"); + return; + } + + if (!is_offscreen && (native_surface == nullptr || !native_surface->IsValid())) { + LOGE("Native surface is invalid."); + return; + } + + // Create the logical surface from the native platform surface. + if (!is_offscreen) { + surface_ = std::make_unique(*vk, *application_, std::move(native_surface)); + if (!surface_->IsValid()) { + LOGE("Vulkan surface is invalid."); + return; + } + } + + // Create the Skia GrContext. + if (!CreateSkiaGrContext()) { + LOGE("Could not create Skia context."); + return; + } + + // Create the swapchain. + if (!is_offscreen && !RecreateSwapchain()) { + LOGE("Could not setup the swapchain initially."); + return; + } + LOGE("VulkanWindow init success"); + valid_ = true; +} +#else VulkanWindow::VulkanWindow(fml::RefPtr proc_table, std::unique_ptr native_surface) : valid_(false), vk(std::move(proc_table)) { @@ -84,6 +169,7 @@ VulkanWindow::VulkanWindow(fml::RefPtr proc_table, valid_ = true; } +#endif VulkanWindow::~VulkanWindow() = default; @@ -99,12 +185,18 @@ bool VulkanWindow::CreateSkiaGrContext() { GrVkBackendContext backend_context; if (!CreateSkiaBackendContext(&backend_context)) { +#ifdef RS_ENABLE_VK + LOGE("CreateSkiaGrContext CreateSkiaBackendContext is false"); +#endif return false; } sk_sp context = GrContext::MakeVulkan(backend_context); if (context == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("CreateSkiaGrContext context is null"); +#endif return false; } @@ -119,11 +211,17 @@ bool VulkanWindow::CreateSkiaBackendContext(GrVkBackendContext* context) { auto getProc = vk->CreateSkiaGetProc(); if (getProc == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("CreateSkiaBackendContext getProc is null"); +#endif return false; } uint32_t skia_features = 0; if (!logical_device_->GetPhysicalDeviceFeaturesSkia(&skia_features)) { +#ifdef RS_ENABLE_VK + LOGE("CreateSkiaBackendContext GetPhysicalDeviceFeaturesSkia is false"); +#endif return false; } @@ -133,9 +231,18 @@ bool VulkanWindow::CreateSkiaBackendContext(GrVkBackendContext* context) { context->fQueue = logical_device_->GetQueueHandle(); context->fGraphicsQueueIndex = logical_device_->GetGraphicsQueueIndex(); context->fMinAPIVersion = application_->GetAPIVersion(); +#ifdef RS_ENABLE_VK + uint32_t extensionFlags = kKHR_surface_GrVkExtensionFlag; + if (!is_offscreen_) { + extensionFlags |= kKHR_swapchain_GrVkExtensionFlag; + extensionFlags |= surface_->GetNativeSurface().GetSkiaExtensionName(); + } + context->fExtensions = extensionFlags; +#else context->fExtensions = kKHR_surface_GrVkExtensionFlag | kKHR_swapchain_GrVkExtensionFlag | surface_->GetNativeSurface().GetSkiaExtensionName(); +#endif context->fFeatures = skia_features; context->fGetProc = std::move(getProc); context->fOwnsInstanceAndDevice = false; @@ -143,10 +250,17 @@ bool VulkanWindow::CreateSkiaBackendContext(GrVkBackendContext* context) { } sk_sp VulkanWindow::AcquireSurface() { +#ifdef RS_ENABLE_VK + if (is_offscreen_ || !IsValid()) { + LOGE("Surface is invalid or offscreen."); + return nullptr; + } +#else if (!IsValid()) { FML_DLOG(INFO) << "Surface is invalid."; return nullptr; } +#endif auto surface_size = surface_->GetSize(); @@ -158,10 +272,18 @@ sk_sp VulkanWindow::AcquireSurface() { // size. if (surface_size != SkISize::Make(0, 0) && surface_size != swapchain_->GetSize()) { +#ifdef RS_ENABLE_VK + LOGE("Swapchain and surface sizes are out of sync. Recreating swapchain."); +#else FML_DLOG(INFO) << "Swapchain and surface sizes are out of sync. Recreating " "swapchain."; +#endif if (!RecreateSwapchain()) { +#ifdef RS_ENABLE_VK + LOGE("Could not recreate swapchain."); +#else FML_DLOG(INFO) << "Could not recreate swapchain."; +#endif valid_ = false; return nullptr; } @@ -180,21 +302,34 @@ sk_sp VulkanWindow::AcquireSurface() { if (acquire_result == VulkanSwapchain::AcquireStatus::ErrorSurfaceLost) { // Surface is lost. This is an unrecoverable error. +#ifdef RS_ENABLE_VK + LOGE("Swapchain reported surface was lost."); +#else FML_DLOG(INFO) << "Swapchain reported surface was lost."; +#endif return nullptr; } if (acquire_result == VulkanSwapchain::AcquireStatus::ErrorSurfaceOutOfDate) { +#ifdef RS_ENABLE_VK + LOGE("AcquireSurface surface out of date"); +#endif // Surface out of date. Recreate the swapchain at the new configuration. if (RecreateSwapchain()) { // Swapchain was recreated, try surface acquisition again. continue; } else { // Could not recreate the swapchain at the new configuration. +#ifdef RS_ENABLE_VK + LOGE("Swapchain reported surface was out of date but " + "could not recreate the swapchain at the new " + "configuration."); +#else FML_DLOG(INFO) << "Swapchain reported surface was out of date but " "could not recreate the swapchain at the new " "configuration."; +#endif valid_ = false; return nullptr; } @@ -203,37 +338,66 @@ sk_sp VulkanWindow::AcquireSurface() { break; } +#ifdef RS_ENABLE_VK + LOGE("Unhandled VulkanSwapchain::AcquireResult"); +#else FML_DCHECK(false) << "Unhandled VulkanSwapchain::AcquireResult"; +#endif return nullptr; } bool VulkanWindow::SwapBuffers() { +#ifdef RS_ENABLE_VK + if (is_offscreen_ || !IsValid()) { + LOGE("Window was invalid or offscreen."); + return false; + } +#else if (!IsValid()) { FML_DLOG(INFO) << "Window was invalid."; return false; } +#endif return swapchain_->Submit(); } bool VulkanWindow::RecreateSwapchain() { +#ifdef RS_ENABLE_VK + if (is_offscreen_) { + LOGE("offscreen vulkan window, don't need swapchian"); + return false; + } +#endif // This way, we always lose our reference to the old swapchain. Even if we // cannot create a new one to replace it. auto old_swapchain = std::move(swapchain_); if (!vk->IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("RecreateSwapchain vk not valid"); +#endif return false; } if (logical_device_ == nullptr || !logical_device_->IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("RecreateSwapchain logical_device_ not valid"); +#endif return false; } if (surface_ == nullptr || !surface_->IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("RecreateSwapchain surface_ not valid"); +#endif return false; } if (skia_gr_context_ == nullptr) { +#ifdef RS_ENABLE_VK + LOGE("RecreateSwapchain skia_gr_context_ not valid"); +#endif return false; } @@ -242,6 +406,9 @@ bool VulkanWindow::RecreateSwapchain() { std::move(old_swapchain), logical_device_->GetGraphicsQueueIndex()); if (!swapchain->IsValid()) { +#ifdef RS_ENABLE_VK + LOGE("RecreateSwapchain swapchain not valid"); +#endif return false; } diff --git a/engine/flutter/vulkan/vulkan_window.h b/engine/flutter/vulkan/vulkan_window.h index ade09d57ac76509f4fe6b5f9f6986b8b0a4dfa56..e43420a5e2899d9850753c66036dbec94a99b23a 100644 --- a/engine/flutter/vulkan/vulkan_window.h +++ b/engine/flutter/vulkan/vulkan_window.h @@ -10,9 +10,11 @@ #include #include +#include "vulkan_proc_table.h" +#ifndef RS_ENABLE_VK #include "flutter/fml/compiler_specific.h" #include "flutter/fml/macros.h" -#include "flutter/vulkan/vulkan_proc_table.h" +#endif #include "third_party/skia/include/core/SkRefCnt.h" #include "third_party/skia/include/core/SkSize.h" #include "third_party/skia/include/core/SkSurface.h" @@ -31,8 +33,13 @@ class VulkanBackbuffer; class VulkanWindow { public: +#ifdef RS_ENABLE_VK + VulkanWindow(std::unique_ptr native_surface, + bool is_offscreen = false); +#else VulkanWindow(fml::RefPtr proc_table, std::unique_ptr native_surface); +#endif ~VulkanWindow(); @@ -46,21 +53,35 @@ class VulkanWindow { private: bool valid_; +#ifdef RS_ENABLE_VK + static VulkanProcTable* vk; + static std::unique_ptr application_; + static std::unique_ptr logical_device_; + bool is_offscreen_ = false; +#else fml::RefPtr vk; std::unique_ptr application_; std::unique_ptr logical_device_; +#endif std::unique_ptr surface_; std::unique_ptr swapchain_; sk_sp skia_gr_context_; +#ifdef RS_ENABLE_VK + static void InitializeVulkan(); +#endif bool CreateSkiaGrContext(); bool CreateSkiaBackendContext(GrVkBackendContext* context); +#ifndef RS_ENABLE_VK FML_WARN_UNUSED_RESULT +#endif bool RecreateSwapchain(); +#ifndef RS_ENABLE_VK FML_DISALLOW_COPY_AND_ASSIGN(VulkanWindow); +#endif }; } // namespace vulkan diff --git a/skia/include/gpu/vk/GrVkBackendContext.h b/skia/include/gpu/vk/GrVkBackendContext.h index a4fd336ff7d55ec7aa590ec4600235b86632abe9..18c12f3fdb42893d323c31fb5237e3722e5a10fb 100644 --- a/skia/include/gpu/vk/GrVkBackendContext.h +++ b/skia/include/gpu/vk/GrVkBackendContext.h @@ -22,6 +22,7 @@ enum GrVkExtensionFlags { kKHR_win32_surface_GrVkExtensionFlag = 0x0010, kKHR_android_surface_GrVkExtensionFlag = 0x0020, kKHR_xcb_surface_GrVkExtensionFlag = 0x0040, + kKHR_ohos_surface_GrVkExtensionFlag = 0x0080, }; enum GrVkFeatureFlags { diff --git a/skia/src/gpu/vk/GrVkAMDMemoryAllocator.cpp b/skia/src/gpu/vk/GrVkAMDMemoryAllocator.cpp index 65577bcba8787b785b68c0ad4f773956267d20a9..d41aa624569fc278f878f54e7f4fa72bccb4898c 100644 --- a/skia/src/gpu/vk/GrVkAMDMemoryAllocator.cpp +++ b/skia/src/gpu/vk/GrVkAMDMemoryAllocator.cpp @@ -41,6 +41,7 @@ GrVkAMDMemoryAllocator::GrVkAMDMemoryAllocator(VkPhysicalDevice physicalDevice, functions.vkGetImageMemoryRequirements2KHR = nullptr; VmaAllocatorCreateInfo info; + memset(&info, 0, sizeof(info)); info.flags = 0; info.physicalDevice = physicalDevice; info.device = device; @@ -67,6 +68,7 @@ bool GrVkAMDMemoryAllocator::allocateMemoryForImage(VkImage image, AllocationPro GrVkBackendMemory* backendMemory) { TRACE_EVENT0("skia.gpu", TRACE_FUNC); VmaAllocationCreateInfo info; + memset(&info, 0, sizeof(info)); info.flags = 0; info.usage = VMA_MEMORY_USAGE_UNKNOWN; info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; @@ -101,6 +103,7 @@ bool GrVkAMDMemoryAllocator::allocateMemoryForBuffer(VkBuffer buffer, BufferUsag GrVkBackendMemory* backendMemory) { TRACE_EVENT0("skia.gpu", TRACE_FUNC); VmaAllocationCreateInfo info; + memset(&info, 0, sizeof(info)); info.flags = 0; info.usage = VMA_MEMORY_USAGE_UNKNOWN; info.memoryTypeBits = 0; diff --git a/skia/src/gpu/vk/GrVkCaps.cpp b/skia/src/gpu/vk/GrVkCaps.cpp index f15ee7dab8f9d46e520b1642abb009955c39730a..384448104fd10ad8e669fb7395ebd7ae64acdf5e 100644 --- a/skia/src/gpu/vk/GrVkCaps.cpp +++ b/skia/src/gpu/vk/GrVkCaps.cpp @@ -1606,6 +1606,8 @@ GrBackendFormat GrVkCaps::getBackendFormatFromCompressionType( switch (compressionType) { case SkImage::kETC1_CompressionType: return GrBackendFormat::MakeVk(VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK); + case SkImage::kASTC_CompressionType | SkImage::kLast_CompressionType: + return GrBackendFormat::MakeVk(VK_FORMAT_ASTC_4x4_UNORM_BLOCK); } SK_ABORT("Invalid compression type"); }