Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -40731,6 +40731,15 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/shared_object_vk.cc +
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/shared_object_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/surface_context_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/surface_context_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_formats.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/external_fence_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/external_fence_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_image_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_image_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_impl_vk.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -43609,6 +43618,15 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/shared_object_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/shared_object_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_context_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_context_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_formats.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/external_fence_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/ahb/external_fence_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_image_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_image_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain/khr/khr_swapchain_impl_vk.cc
Expand Down
13 changes: 8 additions & 5 deletions fml/closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ class ScopedCleanupClosure final {
explicit ScopedCleanupClosure(const fml::closure& closure)
: closure_(closure) {}

~ScopedCleanupClosure() {
if (closure_) {
closure_();
}
}
~ScopedCleanupClosure() { Reset(); }

fml::closure SetClosure(const fml::closure& closure) {
auto old_closure = closure_;
Expand All @@ -63,6 +59,13 @@ class ScopedCleanupClosure final {
return closure;
}

void Reset() {
if (closure_) {
closure_();
closure_ = nullptr;
}
}

private:
fml::closure closure_;

Expand Down
13 changes: 13 additions & 0 deletions impeller/renderer/backend/vulkan/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ impeller_component("vulkan") {
sources += [
"android/ahb_texture_source_vk.cc",
"android/ahb_texture_source_vk.h",
"swapchain/ahb/ahb_formats.h",
"swapchain/ahb/ahb_swapchain_impl_vk.cc",
"swapchain/ahb/ahb_swapchain_impl_vk.h",
"swapchain/ahb/ahb_swapchain_vk.cc",
"swapchain/ahb/ahb_swapchain_vk.h",
"swapchain/ahb/ahb_texture_pool_vk.cc",
"swapchain/ahb/ahb_texture_pool_vk.h",
"swapchain/ahb/external_fence_vk.cc",
"swapchain/ahb/external_fence_vk.h",
]
}

Expand All @@ -144,4 +153,8 @@ impeller_component("vulkan") {
"//flutter/third_party/vulkan-deps/vulkan-headers/src:vulkan_headers",
"//flutter/third_party/vulkan_memory_allocator",
]

if (is_android) {
public_deps += [ "../../../toolkit/android" ]
}
}
43 changes: 32 additions & 11 deletions impeller/renderer/backend/vulkan/android/ahb_texture_source_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ static vk::UniqueImage CreateVKImageWrapperForAndroidHarwareBuffer(
if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) {
image_usage_flags |= vk::ImageUsageFlagBits::eColorAttachment;
}
if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY) {
image_usage_flags |= vk::ImageUsageFlagBits::eColorAttachment;
image_usage_flags |= vk::ImageUsageFlagBits::eInputAttachment;
image_usage_flags |= vk::ImageUsageFlagBits::eTransferDst;
}

vk::ImageCreateFlags image_create_flags;
if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) {
Expand Down Expand Up @@ -280,17 +285,18 @@ static TextureDescriptor ToTextureDescriptor(
}

AHBTextureSourceVK::AHBTextureSourceVK(
const std::shared_ptr<ContextVK>& context,
const std::shared_ptr<Context>& p_context,
struct AHardwareBuffer* ahb,
const AHardwareBuffer_Desc& ahb_desc)
: TextureSourceVK(ToTextureDescriptor(ahb_desc)) {
if (!context) {
VALIDATION_LOG << "Invalid context.";
if (!p_context) {
return;
}

const auto& device = context->GetDevice();
const auto& physical_device = context->GetPhysicalDevice();
const auto& context = ContextVK::Cast(*p_context);

const auto& device = context.GetDevice();
const auto& physical_device = context.GetPhysicalDevice();

AHBProperties ahb_props;

Expand Down Expand Up @@ -327,7 +333,7 @@ AHBTextureSourceVK::AHBTextureSourceVK(
}

// Figure out how to perform YUV conversions.
auto yuv_conversion = CreateYUVConversion(*context, ahb_props);
auto yuv_conversion = CreateYUVConversion(context, ahb_props);
if (!yuv_conversion || !yuv_conversion->IsValid()) {
return;
}
Expand All @@ -350,15 +356,26 @@ AHBTextureSourceVK::AHBTextureSourceVK(
image_view_ = std::move(image_view);

#ifdef IMPELLER_DEBUG
context->SetDebugName(device_memory_.get(), "AHB Device Memory");
context->SetDebugName(image_.get(), "AHB Image");
context->SetDebugName(yuv_conversion_->GetConversion(), "AHB YUV Conversion");
context->SetDebugName(image_view_.get(), "AHB ImageView");
context.SetDebugName(device_memory_.get(), "AHB Device Memory");
context.SetDebugName(image_.get(), "AHB Image");
context.SetDebugName(yuv_conversion_->GetConversion(), "AHB YUV Conversion");
context.SetDebugName(image_view_.get(), "AHB ImageView");
#endif // IMPELLER_DEBUG

is_valid_ = true;
}

AHBTextureSourceVK::AHBTextureSourceVK(
const std::shared_ptr<Context>& context,
std::unique_ptr<android::HardwareBuffer> backing_store,
bool is_swapchain_image)
: AHBTextureSourceVK(context,
backing_store->GetHandle(),
backing_store->GetAndroidDescriptor()) {
backing_store_ = std::move(backing_store);
is_swapchain_image_ = is_swapchain_image;
}

// |TextureSourceVK|
AHBTextureSourceVK::~AHBTextureSourceVK() = default;

Expand All @@ -383,12 +400,16 @@ vk::ImageView AHBTextureSourceVK::GetRenderTargetView() const {

// |TextureSourceVK|
bool AHBTextureSourceVK::IsSwapchainImage() const {
return false;
return is_swapchain_image_;
}

// |TextureSourceVK|
std::shared_ptr<YUVConversionVK> AHBTextureSourceVK::GetYUVConversion() const {
return needs_yuv_conversion_ ? yuv_conversion_ : nullptr;
}

const android::HardwareBuffer* AHBTextureSourceVK::GetBackingStore() const {
return backing_store_.get();
}

} // namespace impeller
11 changes: 10 additions & 1 deletion impeller/renderer/backend/vulkan/android/ahb_texture_source_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "impeller/renderer/backend/vulkan/texture_source_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/backend/vulkan/yuv_conversion_vk.h"
#include "impeller/toolkit/android/hardware_buffer.h"

#include <android/hardware_buffer.h>
#include <android/hardware_buffer_jni.h>
Expand All @@ -33,10 +34,14 @@ class ContextVK;
///
class AHBTextureSourceVK final : public TextureSourceVK {
public:
AHBTextureSourceVK(const std::shared_ptr<ContextVK>& context,
AHBTextureSourceVK(const std::shared_ptr<Context>& context,
struct AHardwareBuffer* hardware_buffer,
const AHardwareBuffer_Desc& hardware_buffer_desc);

AHBTextureSourceVK(const std::shared_ptr<Context>& context,
std::unique_ptr<android::HardwareBuffer> backing_store,
bool is_swapchain_image);

// |TextureSourceVK|
~AHBTextureSourceVK() override;

Expand All @@ -57,12 +62,16 @@ class AHBTextureSourceVK final : public TextureSourceVK {
// |TextureSourceVK|
std::shared_ptr<YUVConversionVK> GetYUVConversion() const override;

const android::HardwareBuffer* GetBackingStore() const;

private:
std::unique_ptr<android::HardwareBuffer> backing_store_;
vk::UniqueDeviceMemory device_memory_ = {};
vk::UniqueImage image_ = {};
vk::UniqueImageView image_view_ = {};
std::shared_ptr<YUVConversionVK> yuv_conversion_ = {};
bool needs_yuv_conversion_ = false;
bool is_swapchain_image_ = false;
bool is_valid_ = false;

AHBTextureSourceVK(const AHBTextureSourceVK&) = delete;
Expand Down
4 changes: 4 additions & 0 deletions impeller/renderer/backend/vulkan/capabilities_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ static const char* GetExtensionName(RequiredAndroidDeviceExtensionVK ext) {
return VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME;
case RequiredAndroidDeviceExtensionVK::kKHRDedicatedAllocation:
return VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME;
case RequiredAndroidDeviceExtensionVK::kKHRExternalFenceFd:
return VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
case RequiredAndroidDeviceExtensionVK::kKHRExternalFence:
return VK_KHR_EXTERNAL_FENCE_EXTENSION_NAME;
case RequiredAndroidDeviceExtensionVK::kLast:
return "Unknown";
}
Expand Down
14 changes: 14 additions & 0 deletions impeller/renderer/backend/vulkan/capabilities_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ enum class RequiredAndroidDeviceExtensionVK : uint32_t {
///
kKHRDedicatedAllocation,

//----------------------------------------------------------------------------
/// For exporting file descriptors from fences to interact with platform APIs.
///
/// https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_external_fence_fd.html
///
kKHRExternalFenceFd,

//----------------------------------------------------------------------------
/// Dependency of kKHRExternalFenceFd.
///
/// https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_external_fence.html
///
kKHRExternalFence,

kLast,
};

Expand Down
4 changes: 2 additions & 2 deletions impeller/renderer/backend/vulkan/driver_info_vk_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ INSTANTIATE_VULKAN_PLAYGROUND_SUITE(DriverInfoVKTest);
TEST_P(DriverInfoVKTest, CanQueryDriverInfo) {
ASSERT_TRUE(GetContext());
const auto& driver_info =
SurfaceContextVK::Cast(*GetContext()).GetParent().GetDriverInfo();
SurfaceContextVK::Cast(*GetContext()).GetParent()->GetDriverInfo();
ASSERT_NE(driver_info, nullptr);
// 1.1 is the base Impeller version. The driver can't be lower than that.
ASSERT_TRUE(driver_info->GetAPIVersion().IsAtLeast(Version{1, 1, 0}));
Expand All @@ -26,7 +26,7 @@ TEST_P(DriverInfoVKTest, CanQueryDriverInfo) {
TEST_P(DriverInfoVKTest, CanDumpToLog) {
ASSERT_TRUE(GetContext());
const auto& driver_info =
SurfaceContextVK::Cast(*GetContext()).GetParent().GetDriverInfo();
SurfaceContextVK::Cast(*GetContext()).GetParent()->GetDriverInfo();
ASSERT_NE(driver_info, nullptr);
fml::testing::LogCapture log;
driver_info->DumpToLog();
Expand Down
5 changes: 5 additions & 0 deletions impeller/renderer/backend/vulkan/queue_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ vk::Result QueueVK::Submit(const vk::SubmitInfo& submit_info,
return queue_.submit(submit_info, fence);
}

vk::Result QueueVK::Submit(const vk::Fence& fence) const {
Lock lock(queue_mutex_);
return queue_.submit({}, fence);
}

vk::Result QueueVK::Present(const vk::PresentInfoKHR& present_info) {
Lock lock(queue_mutex_);
return queue_.presentKHR(present_info);
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/backend/vulkan/queue_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class QueueVK {
vk::Result Submit(const vk::SubmitInfo& submit_info,
const vk::Fence& fence) const;

vk::Result Submit(const vk::Fence& fence) const;

vk::Result Present(const vk::PresentInfoKHR& present_info);

void InsertDebugMarker(std::string_view label) const;
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/backend/vulkan/shared_object_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class SharedObjectVKT : public SharedObjectVK {

const Resource& Get() const { return *resource_; }

const UniqueResource& GetUniqueWrapper() const { return resource_; }

private:
UniqueResource resource_;

Expand Down
40 changes: 8 additions & 32 deletions impeller/renderer/backend/vulkan/surface_context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,12 @@ void SurfaceContextVK::Shutdown() {

bool SurfaceContextVK::SetWindowSurface(vk::UniqueSurfaceKHR surface,
const ISize& size) {
auto swapchain = SwapchainVK::Create(parent_, std::move(surface), size);
if (!swapchain) {
VALIDATION_LOG << "Could not create swapchain.";
return false;
}
if (!swapchain->IsValid()) {
VALIDATION_LOG << "Could not create valid swapchain.";
return SetSwapchain(SwapchainVK::Create(parent_, std::move(surface), size));
}

bool SurfaceContextVK::SetSwapchain(std::shared_ptr<SwapchainVK> swapchain) {
if (!swapchain || !swapchain->IsValid()) {
VALIDATION_LOG << "Invalid swapchain.";
return false;
}
swapchain_ = std::move(swapchain);
Expand All @@ -96,29 +95,6 @@ void SurfaceContextVK::UpdateSurfaceSize(const ISize& size) const {
swapchain_->UpdateSurfaceSize(size);
}

#ifdef FML_OS_ANDROID

vk::UniqueSurfaceKHR SurfaceContextVK::CreateAndroidSurface(
ANativeWindow* window) const {
if (!parent_->GetInstance()) {
return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
}

auto create_info = vk::AndroidSurfaceCreateInfoKHR().setWindow(window);
auto surface_res =
parent_->GetInstance().createAndroidSurfaceKHRUnique(create_info);

if (surface_res.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not create Android surface, error: "
<< vk::to_string(surface_res.result);
return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
}

return std::move(surface_res.value);
}

#endif // FML_OS_ANDROID

const vk::Device& SurfaceContextVK::GetDevice() const {
return parent_->GetDevice();
}
Expand All @@ -127,8 +103,8 @@ void SurfaceContextVK::InitializeCommonlyUsedShadersIfNeeded() const {
parent_->InitializeCommonlyUsedShadersIfNeeded();
}

const ContextVK& SurfaceContextVK::GetParent() const {
return *parent_;
const std::shared_ptr<ContextVK>& SurfaceContextVK::GetParent() const {
return parent_;
}

} // namespace impeller
8 changes: 3 additions & 5 deletions impeller/renderer/backend/vulkan/surface_context_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class SurfaceContextVK : public Context,
[[nodiscard]] bool SetWindowSurface(vk::UniqueSurfaceKHR surface,
const ISize& size);

[[nodiscard]] bool SetSwapchain(std::shared_ptr<SwapchainVK> swapchain);

std::unique_ptr<Surface> AcquireNextSurface();

/// @brief Mark the current swapchain configuration as dirty, forcing it to be
Expand All @@ -80,13 +82,9 @@ class SurfaceContextVK : public Context,

void InitializeCommonlyUsedShadersIfNeeded() const override;

#ifdef FML_OS_ANDROID
vk::UniqueSurfaceKHR CreateAndroidSurface(ANativeWindow* window) const;
#endif // FML_OS_ANDROID

const vk::Device& GetDevice() const;

const ContextVK& GetParent() const;
const std::shared_ptr<ContextVK>& GetParent() const;

private:
std::shared_ptr<ContextVK> parent_;
Expand Down
Loading