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
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,8 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc +
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/shader_library_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/shared_object_vk.cc + ../../../flutter/LICENSE
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/surface_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/surface_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/swapchain_image_vk.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -4240,6 +4242,8 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/shader_library_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/shader_library_vk.h
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/surface_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/surface_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/swapchain_image_vk.cc
Expand Down
26 changes: 14 additions & 12 deletions impeller/playground/backend/vulkan/playground_impl_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "impeller/playground/imgui/vk/imgui_shaders_vk.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/formats_vk.h"
#include "impeller/renderer/backend/vulkan/surface_context_vk.h"
#include "impeller/renderer/backend/vulkan/surface_vk.h"
#include "impeller/renderer/backend/vulkan/texture_vk.h"
#include "impeller/renderer/vk/compute_shaders_vk.h"
Expand Down Expand Up @@ -86,27 +87,27 @@ PlaygroundImplVK::PlaygroundImplVK(PlaygroundSwitches switches)
context_settings.cache_directory = fml::paths::GetCachesDirectory();
context_settings.enable_validation = switches_.enable_vulkan_validation;

auto context = ContextVK::Create(std::move(context_settings));

if (!context || !context->IsValid()) {
auto context_vk = ContextVK::Create(std::move(context_settings));
if (!context_vk || !context_vk->IsValid()) {
VALIDATION_LOG << "Could not create Vulkan context in the playground.";
return;
}

VkSurfaceKHR vk_surface;
auto res =
vk::Result{::glfwCreateWindowSurface(context->GetInstance(), // instance
window, // window
nullptr, // allocator
&vk_surface // surface
)};
auto res = vk::Result{::glfwCreateWindowSurface(
context_vk->GetInstance(), // instance
window, // window
nullptr, // allocator
&vk_surface // surface
)};
if (res != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not create surface for GLFW window: "
<< vk::to_string(res);
return;
}

vk::UniqueSurfaceKHR surface{vk_surface, context->GetInstance()};
vk::UniqueSurfaceKHR surface{vk_surface, context_vk->GetInstance()};
auto context = context_vk->CreateSurfaceContext();
if (!context->SetWindowSurface(std::move(surface))) {
VALIDATION_LOG << "Could not set up surface for context.";
return;
Expand All @@ -130,8 +131,9 @@ PlaygroundImpl::WindowHandle PlaygroundImplVK::GetWindowHandle() const {
// |PlaygroundImpl|
std::unique_ptr<Surface> PlaygroundImplVK::AcquireSurfaceFrame(
std::shared_ptr<Context> context) {
ContextVK* context_vk = reinterpret_cast<ContextVK*>(context_.get());
return context_vk->AcquireNextSurface();
SurfaceContextVK* surface_context_vk =
reinterpret_cast<SurfaceContextVK*>(context_.get());
return surface_context_vk->AcquireNextSurface();
}

} // namespace impeller
2 changes: 2 additions & 0 deletions impeller/renderer/backend/vulkan/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ impeller_component("vulkan") {
"shader_library_vk.h",
"shared_object_vk.cc",
"shared_object_vk.h",
"surface_context_vk.cc",
"surface_context_vk.h",
"surface_vk.cc",
"surface_vk.h",
"swapchain_image_vk.cc",
Expand Down
46 changes: 3 additions & 43 deletions impeller/renderer/backend/vulkan/context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "impeller/renderer/backend/vulkan/fence_waiter_vk.h"
#include "impeller/renderer/backend/vulkan/formats_vk.h"
#include "impeller/renderer/backend/vulkan/resource_manager_vk.h"
#include "impeller/renderer/backend/vulkan/surface_context_vk.h"
#include "impeller/renderer/backend/vulkan/surface_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/capabilities.h"
Expand Down Expand Up @@ -489,49 +490,8 @@ void ContextVK::Shutdown() {
raster_message_loop_->Terminate();
}

std::unique_ptr<Surface> ContextVK::AcquireNextSurface() {
TRACE_EVENT0("impeller", __FUNCTION__);
auto surface = swapchain_ ? swapchain_->AcquireNextDrawable() : nullptr;
if (surface && pipeline_library_) {
pipeline_library_->DidAcquireSurfaceFrame();
}
if (allocator_) {
allocator_->DidAcquireSurfaceFrame();
}
return surface;
}

#ifdef FML_OS_ANDROID

vk::UniqueSurfaceKHR ContextVK::CreateAndroidSurface(
ANativeWindow* window) const {
if (!device_holder_->instance) {
return vk::UniqueSurfaceKHR{VK_NULL_HANDLE};
}

auto create_info = vk::AndroidSurfaceCreateInfoKHR().setWindow(window);
auto surface_res =
device_holder_->instance->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

bool ContextVK::SetWindowSurface(vk::UniqueSurfaceKHR surface) {
auto swapchain = SwapchainVK::Create(shared_from_this(), std::move(surface));
if (!swapchain) {
VALIDATION_LOG << "Could not create swapchain.";
return false;
}
swapchain_ = std::move(swapchain);
return true;
std::shared_ptr<SurfaceContextVK> ContextVK::CreateSurfaceContext() {
return std::make_shared<SurfaceContextVK>(shared_from_this());
}

const std::shared_ptr<const Capabilities>& ContextVK::GetCapabilities() const {
Expand Down
10 changes: 2 additions & 8 deletions impeller/renderer/backend/vulkan/context_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CommandEncoderVK;
class DebugReportVK;
class FenceWaiterVK;
class ResourceManagerVK;
class SurfaceContextVK;

class ContextVK final : public Context,
public BackendCast<ContextVK, Context>,
Expand Down Expand Up @@ -127,13 +128,7 @@ class ContextVK final : public Context,
const std::shared_ptr<fml::ConcurrentTaskRunner>
GetConcurrentWorkerTaskRunner() const;

[[nodiscard]] bool SetWindowSurface(vk::UniqueSurfaceKHR surface);

std::unique_ptr<Surface> AcquireNextSurface();

#ifdef FML_OS_ANDROID
vk::UniqueSurfaceKHR CreateAndroidSurface(ANativeWindow* window) const;
#endif // FML_OS_ANDROID
std::shared_ptr<SurfaceContextVK> CreateSurfaceContext();

const std::shared_ptr<QueueVK>& GetGraphicsQueue() const;

Expand Down Expand Up @@ -164,7 +159,6 @@ class ContextVK final : public Context,
std::shared_ptr<SamplerLibraryVK> sampler_library_;
std::shared_ptr<PipelineLibraryVK> pipeline_library_;
QueuesVK queues_;
std::shared_ptr<SwapchainVK> swapchain_;
std::shared_ptr<const Capabilities> device_capabilities_;
std::shared_ptr<FenceWaiterVK> fence_waiter_;
std::shared_ptr<ResourceManagerVK> resource_manager_;
Expand Down
107 changes: 107 additions & 0 deletions impeller/renderer/backend/vulkan/surface_context_vk.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 "impeller/renderer/backend/vulkan/surface_context_vk.h"

#include "flutter/fml/trace_event.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"

namespace impeller {

SurfaceContextVK::SurfaceContextVK(const std::shared_ptr<ContextVK>& parent)
: parent_(parent) {}

SurfaceContextVK::~SurfaceContextVK() = default;

Context::BackendType SurfaceContextVK::GetBackendType() const {
return parent_->GetBackendType();
}

std::string SurfaceContextVK::DescribeGpuModel() const {
return parent_->DescribeGpuModel();
}

bool SurfaceContextVK::IsValid() const {
return parent_->IsValid();
}

std::shared_ptr<Allocator> SurfaceContextVK::GetResourceAllocator() const {
return parent_->GetResourceAllocator();
}

std::shared_ptr<ShaderLibrary> SurfaceContextVK::GetShaderLibrary() const {
return parent_->GetShaderLibrary();
}

std::shared_ptr<SamplerLibrary> SurfaceContextVK::GetSamplerLibrary() const {
return parent_->GetSamplerLibrary();
}

std::shared_ptr<PipelineLibrary> SurfaceContextVK::GetPipelineLibrary() const {
return parent_->GetPipelineLibrary();
}

std::shared_ptr<CommandBuffer> SurfaceContextVK::CreateCommandBuffer() const {
return parent_->CreateCommandBuffer();
}

const std::shared_ptr<const Capabilities>& SurfaceContextVK::GetCapabilities()
const {
return parent_->GetCapabilities();
}

void SurfaceContextVK::Shutdown() {
parent_->Shutdown();
}

bool SurfaceContextVK::SetWindowSurface(vk::UniqueSurfaceKHR surface) {
auto swapchain = SwapchainVK::Create(parent_, std::move(surface));
if (!swapchain) {
VALIDATION_LOG << "Could not create swapchain.";
return false;
}
swapchain_ = std::move(swapchain);
return true;
}

std::unique_ptr<Surface> SurfaceContextVK::AcquireNextSurface() {
TRACE_EVENT0("impeller", __FUNCTION__);
auto surface = swapchain_ ? swapchain_->AcquireNextDrawable() : nullptr;
auto pipeline_library = parent_->GetPipelineLibrary();
if (surface && pipeline_library) {
impeller::PipelineLibraryVK::Cast(*pipeline_library)
.DidAcquireSurfaceFrame();
}
auto allocator = parent_->GetResourceAllocator();
if (allocator) {
allocator->DidAcquireSurfaceFrame();
}
return surface;
}

#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

} // namespace impeller
70 changes: 70 additions & 0 deletions impeller/renderer/backend/vulkan/surface_context_vk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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.

#pragma once

#include <memory>

#include "impeller/base/backend_cast.h"
#include "impeller/renderer/backend/vulkan/vk.h"
#include "impeller/renderer/context.h"

namespace impeller {

class ContextVK;
class Surface;
class SwapchainVK;

class SurfaceContextVK : public Context,
public BackendCast<SurfaceContextVK, Context> {
public:
SurfaceContextVK(const std::shared_ptr<ContextVK>& parent);

// |Context|
~SurfaceContextVK() override;

// |Context|
BackendType GetBackendType() const override;

// |Context|
std::string DescribeGpuModel() const override;

// |Context|
bool IsValid() const override;

// |Context|
std::shared_ptr<Allocator> GetResourceAllocator() const override;

// |Context|
std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;

// |Context|
std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;

// |Context|
std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;

// |Context|
std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;

// |Context|
const std::shared_ptr<const Capabilities>& GetCapabilities() const override;

// |Context|
void Shutdown() override;

[[nodiscard]] bool SetWindowSurface(vk::UniqueSurfaceKHR surface);

std::unique_ptr<Surface> AcquireNextSurface();

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

private:
std::shared_ptr<ContextVK> parent_;
std::shared_ptr<SwapchainVK> swapchain_;
};

} // namespace impeller
5 changes: 3 additions & 2 deletions shell/gpu/gpu_surface_vulkan_impeller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include "flutter/fml/make_copyable.h"
#include "flutter/impeller/display_list/dl_dispatcher.h"
#include "flutter/impeller/renderer/renderer.h"
#include "impeller/renderer/backend/vulkan/context_vk.h"
#include "impeller/renderer/backend/vulkan/surface_context_vk.h"
#include "impeller/renderer/surface.h"

namespace flutter {

Expand Down Expand Up @@ -54,7 +55,7 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceVulkanImpeller::AcquireFrame(
return nullptr;
}

auto& context_vk = impeller::ContextVK::Cast(*impeller_context_);
auto& context_vk = impeller::SurfaceContextVK::Cast(*impeller_context_);
std::unique_ptr<impeller::Surface> surface = context_vk.AcquireNextSurface();

SurfaceFrame::SubmitCallback submit_callback =
Expand Down
Loading