Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] libImpeller: Add support for Metal and Vulkan rendering. #56906

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 38 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -43538,6 +43538,22 @@ ORIGIN: ../../../flutter/impeller/toolkit/glvk/proc_table.cc + ../../../flutter/
ORIGIN: ../../../flutter/impeller/toolkit/glvk/proc_table.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/glvk/trampoline.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/glvk/trampoline.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/gles/context_gles.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/gles/context_gles.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/gles/reactor_worker_gles.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/gles/reactor_worker_gles.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/gles/surface_gles.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/gles/surface_gles.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/metal/context_mtl.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/metal/context_mtl.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/metal/surface_mtl.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/metal/surface_mtl.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/vulkan/context_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/vulkan/context_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/vulkan/surface_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/vulkan/surface_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/vulkan/swapchain_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/backend/vulkan/swapchain_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/color_filter.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/color_filter.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/color_source.cc + ../../../flutter/LICENSE
Expand All @@ -43548,7 +43564,9 @@ ORIGIN: ../../../flutter/impeller/toolkit/interop/dl.cc + ../../../flutter/LICEN
ORIGIN: ../../../flutter/impeller/toolkit/interop/dl.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/dl_builder.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/dl_builder.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/example.c + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/example_gl.c + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/example_mtl.m + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/example_vk.c + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/formats.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/formats.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/toolkit/interop/image_filter.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -46472,6 +46490,22 @@ FILE: ../../../flutter/impeller/toolkit/glvk/proc_table.cc
FILE: ../../../flutter/impeller/toolkit/glvk/proc_table.h
FILE: ../../../flutter/impeller/toolkit/glvk/trampoline.cc
FILE: ../../../flutter/impeller/toolkit/glvk/trampoline.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/gles/context_gles.cc
FILE: ../../../flutter/impeller/toolkit/interop/backend/gles/context_gles.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/gles/reactor_worker_gles.cc
FILE: ../../../flutter/impeller/toolkit/interop/backend/gles/reactor_worker_gles.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/gles/surface_gles.cc
FILE: ../../../flutter/impeller/toolkit/interop/backend/gles/surface_gles.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/metal/context_mtl.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/metal/context_mtl.mm
FILE: ../../../flutter/impeller/toolkit/interop/backend/metal/surface_mtl.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/metal/surface_mtl.mm
FILE: ../../../flutter/impeller/toolkit/interop/backend/vulkan/context_vk.cc
FILE: ../../../flutter/impeller/toolkit/interop/backend/vulkan/context_vk.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/vulkan/surface_vk.cc
FILE: ../../../flutter/impeller/toolkit/interop/backend/vulkan/surface_vk.h
FILE: ../../../flutter/impeller/toolkit/interop/backend/vulkan/swapchain_vk.cc
FILE: ../../../flutter/impeller/toolkit/interop/backend/vulkan/swapchain_vk.h
FILE: ../../../flutter/impeller/toolkit/interop/color_filter.cc
FILE: ../../../flutter/impeller/toolkit/interop/color_filter.h
FILE: ../../../flutter/impeller/toolkit/interop/color_source.cc
Expand All @@ -46482,7 +46516,9 @@ FILE: ../../../flutter/impeller/toolkit/interop/dl.cc
FILE: ../../../flutter/impeller/toolkit/interop/dl.h
FILE: ../../../flutter/impeller/toolkit/interop/dl_builder.cc
FILE: ../../../flutter/impeller/toolkit/interop/dl_builder.h
FILE: ../../../flutter/impeller/toolkit/interop/example.c
FILE: ../../../flutter/impeller/toolkit/interop/example_gl.c
FILE: ../../../flutter/impeller/toolkit/interop/example_mtl.m
FILE: ../../../flutter/impeller/toolkit/interop/example_vk.c
FILE: ../../../flutter/impeller/toolkit/interop/formats.cc
FILE: ../../../flutter/impeller/toolkit/interop/formats.h
FILE: ../../../flutter/impeller/toolkit/interop/image_filter.cc
Expand Down
10 changes: 9 additions & 1 deletion impeller/playground/backend/vulkan/playground_impl_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ PlaygroundImplVK::PlaygroundImplVK(PlaygroundSwitches switches)
context_settings.enable_validation = switches_.enable_vulkan_validation;
context_settings.fatal_missing_validations =
switches_.enable_vulkan_validation;
;

auto context_vk = ContextVK::Create(std::move(context_settings));
if (!context_vk || !context_vk->IsValid()) {
Expand Down Expand Up @@ -233,4 +232,13 @@ bool PlaygroundImplVK::IsVulkanDriverPresent() {
return false;
}

// |PlaygroundImpl|
Playground::VKProcAddressResolver
PlaygroundImplVK::CreateVKProcAddressResolver() const {
return [](void* instance, const char* proc_name) -> void* {
return reinterpret_cast<void*>(::glfwGetInstanceProcAddress(
reinterpret_cast<VkInstance>(instance), proc_name));
};
}

} // namespace impeller
4 changes: 4 additions & 0 deletions impeller/playground/backend/vulkan/playground_impl_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class PlaygroundImplVK final : public PlaygroundImpl {
std::unique_ptr<Surface> AcquireSurfaceFrame(
std::shared_ptr<Context> context) override;

// |PlaygroundImpl|
Playground::VKProcAddressResolver CreateVKProcAddressResolver()
const override;

PlaygroundImplVK(const PlaygroundImplVK&) = delete;

PlaygroundImplVK& operator=(const PlaygroundImplVK&) = delete;
Expand Down
5 changes: 5 additions & 0 deletions impeller/playground/playground.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,9 @@ Playground::GLProcAddressResolver Playground::CreateGLProcAddressResolver()
return impl_->CreateGLProcAddressResolver();
}

Playground::VKProcAddressResolver Playground::CreateVKProcAddressResolver()
const {
return impl_->CreateVKProcAddressResolver();
}

} // namespace impeller
4 changes: 4 additions & 0 deletions impeller/playground/playground.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ class Playground {
using GLProcAddressResolver = std::function<void*(const char* proc_name)>;
GLProcAddressResolver CreateGLProcAddressResolver() const;

using VKProcAddressResolver =
std::function<void*(void* instance, const char* proc_name)>;
VKProcAddressResolver CreateVKProcAddressResolver() const;

protected:
const PlaygroundSwitches switches_;

Expand Down
5 changes: 5 additions & 0 deletions impeller/playground/playground_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ Playground::GLProcAddressResolver PlaygroundImpl::CreateGLProcAddressResolver()
return nullptr;
}

Playground::VKProcAddressResolver PlaygroundImpl::CreateVKProcAddressResolver()
const {
return nullptr;
}

} // namespace impeller
2 changes: 2 additions & 0 deletions impeller/playground/playground_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class PlaygroundImpl {

virtual Playground::GLProcAddressResolver CreateGLProcAddressResolver() const;

virtual Playground::VKProcAddressResolver CreateVKProcAddressResolver() const;

protected:
const PlaygroundSwitches switches_;

Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/vulkan/context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void ContextVK::Setup(Settings settings) {
TRACE_EVENT0("impeller", "ContextVK::Setup");

if (!settings.proc_address_callback) {
VALIDATION_LOG << "Missing proc address callback.";
return;
}

Expand All @@ -154,7 +155,7 @@ void ContextVK::Setup(Settings settings) {
fml::RequestAffinity(fml::CpuAffinity::kNotPerformance);
#ifdef FML_OS_ANDROID
if (::setpriority(PRIO_PROCESS, gettid(), -5) != 0) {
FML_LOG(ERROR) << "Failed to set Workers task runner priority";
VALIDATION_LOG << "Failed to set Workers task runner priority";
}
#endif // FML_OS_ANDROID
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,38 @@ const ISize& KHRSwapchainImplVK::GetSize() const {
return size_;
}

std::optional<ISize> KHRSwapchainImplVK::GetCurrentUnderlyingSurfaceSize()
const {
if (!IsValid()) {
return std::nullopt;
}

auto context = context_.lock();
if (!context) {
return std::nullopt;
}

auto& vk_context = ContextVK::Cast(*context);
const auto [result, surface_caps] =
vk_context.GetPhysicalDevice().getSurfaceCapabilitiesKHR(surface_.get());
if (result != vk::Result::eSuccess) {
return std::nullopt;
}

// From the spec: `currentExtent` is the current width and height of the
// surface, or the special value (0xFFFFFFFF, 0xFFFFFFFF) indicating that the
// surface size will be determined by the extent of a swapchain targeting the
// surface.
constexpr uint32_t kCurrentExtentsPlaceholder = 0xFFFFFFFF;
if (surface_caps.currentExtent.width == kCurrentExtentsPlaceholder ||
surface_caps.currentExtent.height == kCurrentExtentsPlaceholder) {
return std::nullopt;
}

return ISize::MakeWH(surface_caps.currentExtent.width,
surface_caps.currentExtent.height);
}

bool KHRSwapchainImplVK::IsValid() const {
return is_valid_;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class KHRSwapchainImplVK final

const ISize& GetSize() const;

std::optional<ISize> GetCurrentUnderlyingSurfaceSize() const;

private:
std::weak_ptr<Context> context_;
vk::UniqueSurfaceKHR surface_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ void KHRSwapchainVK::UpdateSurfaceSize(const ISize& size) {
}

std::unique_ptr<Surface> KHRSwapchainVK::AcquireNextDrawable() {
return AcquireNextDrawable(0u);
}

std::unique_ptr<Surface> KHRSwapchainVK::AcquireNextDrawable(
size_t resize_retry_count) {
if (!IsValid()) {
return nullptr;
}
Expand All @@ -51,8 +56,19 @@ std::unique_ptr<Surface> KHRSwapchainVK::AcquireNextDrawable() {
return std::move(result.surface);
}

constexpr const size_t kMaxResizeAttempts = 3u;
if (resize_retry_count == kMaxResizeAttempts) {
VALIDATION_LOG << "Attempted to resize the swapchain" << kMaxResizeAttempts
<< " time unsuccessfully. This platform likely doesn't "
"support returning the current swapchain extents and "
"must recreate the swapchain using the actual size.";
return nullptr;
}

TRACE_EVENT0("impeller", "RecreateSwapchain");

size_ = impl_->GetCurrentUnderlyingSurfaceSize().value_or(size_);

// This swapchain implementation indicates that it is out of date. Tear it
// down and make a new one.
auto context = impl_->GetContext();
Expand All @@ -76,7 +92,7 @@ std::unique_ptr<Surface> KHRSwapchainVK::AcquireNextDrawable() {
//----------------------------------------------------------------------------
/// We managed to recreate the swapchain in the new configuration. Try again.
///
return AcquireNextDrawable();
return AcquireNextDrawable(resize_retry_count + 1);
}

vk::Format KHRSwapchainVK::GetSurfaceFormat() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class KHRSwapchainVK final : public SwapchainVK {
KHRSwapchainVK(const KHRSwapchainVK&) = delete;

KHRSwapchainVK& operator=(const KHRSwapchainVK&) = delete;

std::unique_ptr<Surface> AcquireNextDrawable(size_t resize_retry_count);
};

} // namespace impeller
Expand Down
Loading
Loading