Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,7 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/descriptor_pool_vk.cc
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/descriptor_pool_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/device_buffer_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/device_buffer_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/device_holder.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/fence_waiter_vk.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/fence_waiter_vk.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/vulkan/formats_vk.cc + ../../../flutter/LICENSE
Expand Down Expand Up @@ -4090,6 +4091,7 @@ FILE: ../../../flutter/impeller/renderer/backend/vulkan/descriptor_pool_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/descriptor_pool_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/device_buffer_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/device_buffer_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/device_holder.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/fence_waiter_vk.cc
FILE: ../../../flutter/impeller/renderer/backend/vulkan/fence_waiter_vk.h
FILE: ../../../flutter/impeller/renderer/backend/vulkan/formats_vk.cc
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/gles/context_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace impeller {

class ContextGLES final : public Context,
public BackendCast<ContextGLES, Context> {
public BackendCast<ContextGLES, Context>,
public std::enable_shared_from_this<ContextGLES> {
public:
static std::shared_ptr<ContextGLES> Create(
std::unique_ptr<ProcTableGLES> gl,
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/metal/context_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
namespace impeller {

class ContextMTL final : public Context,
public BackendCast<ContextMTL, Context> {
public BackendCast<ContextMTL, Context>,
public std::enable_shared_from_this<ContextMTL> {
public:
static std::shared_ptr<ContextMTL> Create(
const std::vector<std::string>& shader_library_paths);
Expand Down
6 changes: 3 additions & 3 deletions impeller/renderer/backend/metal/surface_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@
render_target_desc.SetColorAttachment(color0, 0u);

// The constructor is private. So make_unique may not be used.
return std::unique_ptr<SurfaceMTL>(
new SurfaceMTL(context->weak_from_this(), render_target_desc, resolve_tex,
drawable, requires_blit, clip_rect));
return std::unique_ptr<SurfaceMTL>(new SurfaceMTL(context, render_target_desc,
resolve_tex, drawable,
requires_blit, clip_rect));
}

SurfaceMTL::SurfaceMTL(const std::weak_ptr<Context>& context,
Expand Down
12 changes: 6 additions & 6 deletions impeller/renderer/backend/vulkan/blit_command_vk_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace testing {
TEST(BlitCommandVkTest, BlitCopyTextureToTextureCommandVK) {
auto context = CreateMockVulkanContext();
auto pool = CommandPoolVK::GetThreadLocal(context.get());
CommandEncoderVK encoder(context->GetDevice(), context->GetGraphicsQueue(),
pool, context->GetFenceWaiter());
CommandEncoderVK encoder(context, context->GetGraphicsQueue(), pool,
context->GetFenceWaiter());
BlitCopyTextureToTextureCommandVK cmd;
cmd.source = context->GetResourceAllocator()->CreateTexture({
.size = ISize(100, 100),
Expand All @@ -31,8 +31,8 @@ TEST(BlitCommandVkTest, BlitCopyTextureToTextureCommandVK) {
TEST(BlitCommandVkTest, BlitCopyTextureToBufferCommandVK) {
auto context = CreateMockVulkanContext();
auto pool = CommandPoolVK::GetThreadLocal(context.get());
CommandEncoderVK encoder(context->GetDevice(), context->GetGraphicsQueue(),
pool, context->GetFenceWaiter());
CommandEncoderVK encoder(context, context->GetGraphicsQueue(), pool,
context->GetFenceWaiter());
BlitCopyTextureToBufferCommandVK cmd;
cmd.source = context->GetResourceAllocator()->CreateTexture({
.size = ISize(100, 100),
Expand All @@ -49,8 +49,8 @@ TEST(BlitCommandVkTest, BlitCopyTextureToBufferCommandVK) {
TEST(BlitCommandVkTest, BlitGenerateMipmapCommandVK) {
auto context = CreateMockVulkanContext();
auto pool = CommandPoolVK::GetThreadLocal(context.get());
CommandEncoderVK encoder(context->GetDevice(), context->GetGraphicsQueue(),
pool, context->GetFenceWaiter());
CommandEncoderVK encoder(context, context->GetGraphicsQueue(), pool,
context->GetFenceWaiter());
BlitGenerateMipmapCommandVK cmd;
cmd.texture = context->GetResourceAllocator()->CreateTexture({
.size = ISize(100, 100),
Expand Down
26 changes: 16 additions & 10 deletions impeller/renderer/backend/vulkan/command_encoder_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace impeller {

class TrackedObjectsVK {
public:
explicit TrackedObjectsVK(const vk::Device& device,
explicit TrackedObjectsVK(std::weak_ptr<const DeviceHolder> device_holder,
const std::shared_ptr<CommandPoolVK>& pool)
: desc_pool_(device) {
: desc_pool_(device_holder) {
if (!pool) {
return;
}
Expand Down Expand Up @@ -95,12 +95,14 @@ class TrackedObjectsVK {
FML_DISALLOW_COPY_AND_ASSIGN(TrackedObjectsVK);
};

CommandEncoderVK::CommandEncoderVK(vk::Device device,
const std::shared_ptr<QueueVK>& queue,
const std::shared_ptr<CommandPoolVK>& pool,
std::shared_ptr<FenceWaiterVK> fence_waiter)
CommandEncoderVK::CommandEncoderVK(
std::weak_ptr<const DeviceHolder> device_holder,
const std::shared_ptr<QueueVK>& queue,
const std::shared_ptr<CommandPoolVK>& pool,
std::shared_ptr<FenceWaiterVK> fence_waiter)
: fence_waiter_(std::move(fence_waiter)),
tracked_objects_(std::make_shared<TrackedObjectsVK>(device, pool)) {
tracked_objects_(
std::make_shared<TrackedObjectsVK>(device_holder, pool)) {
if (!fence_waiter_ || !tracked_objects_->IsValid() || !queue) {
return;
}
Expand All @@ -111,7 +113,7 @@ CommandEncoderVK::CommandEncoderVK(vk::Device device,
VALIDATION_LOG << "Could not begin command buffer.";
return;
}
device_ = device;
device_holder_ = device_holder;
queue_ = queue;
is_valid_ = true;
}
Expand All @@ -137,7 +139,11 @@ bool CommandEncoderVK::Submit() {
if (command_buffer.end() != vk::Result::eSuccess) {
return false;
}
auto [fence_result, fence] = device_.createFenceUnique({});
std::shared_ptr<const DeviceHolder> strong_device = device_holder_.lock();
if (!strong_device) {
return false;
}
auto [fence_result, fence] = strong_device->GetDevice().createFenceUnique({});
if (fence_result != vk::Result::eSuccess) {
return false;
}
Expand Down Expand Up @@ -166,7 +172,7 @@ void CommandEncoderVK::Reset() {
tracked_objects_.reset();

queue_ = nullptr;
device_ = nullptr;
device_holder_ = {};
is_valid_ = false;
}

Expand Down
5 changes: 3 additions & 2 deletions impeller/renderer/backend/vulkan/command_encoder_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "flutter/fml/macros.h"
#include "impeller/renderer/backend/vulkan/command_pool_vk.h"
#include "impeller/renderer/backend/vulkan/descriptor_pool_vk.h"
#include "impeller/renderer/backend/vulkan/device_holder.h"
#include "impeller/renderer/backend/vulkan/queue_vk.h"
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"
Expand Down Expand Up @@ -69,13 +70,13 @@ class CommandEncoderVK {
friend class ::impeller::testing::
BlitCommandVkTest_BlitGenerateMipmapCommandVK_Test;

vk::Device device_ = {};
std::weak_ptr<const DeviceHolder> device_holder_;
std::shared_ptr<QueueVK> queue_;
std::shared_ptr<FenceWaiterVK> fence_waiter_;
std::shared_ptr<TrackedObjectsVK> tracked_objects_;
bool is_valid_ = false;

CommandEncoderVK(vk::Device device,
CommandEncoderVK(std::weak_ptr<const DeviceHolder> device_holder,
const std::shared_ptr<QueueVK>& queue,
const std::shared_ptr<CommandPoolVK>& pool,
std::shared_ptr<FenceWaiterVK> fence_waiter);
Expand Down
9 changes: 7 additions & 2 deletions impeller/renderer/backend/vulkan/command_pool_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ CommandPoolVK::CommandPoolVK(const ContextVK* context)
return;
}

device_ = context->GetDevice();
device_holder_ = context->weak_from_this();
graphics_pool_ = std::move(pool.value);
is_valid_ = true;
}
Expand All @@ -102,6 +102,10 @@ vk::CommandPool CommandPoolVK::GetGraphicsCommandPool() const {
}

vk::UniqueCommandBuffer CommandPoolVK::CreateGraphicsCommandBuffer() {
std::shared_ptr<const DeviceHolder> strong_device = device_holder_.lock();
if (!strong_device) {
return {};
}
if (std::this_thread::get_id() != owner_id_) {
return {};
}
Expand All @@ -113,7 +117,8 @@ vk::UniqueCommandBuffer CommandPoolVK::CreateGraphicsCommandBuffer() {
alloc_info.commandPool = graphics_pool_.get();
alloc_info.commandBufferCount = 1u;
alloc_info.level = vk::CommandBufferLevel::ePrimary;
auto [result, buffers] = device_.allocateCommandBuffersUnique(alloc_info);
auto [result, buffers] =
strong_device->GetDevice().allocateCommandBuffersUnique(alloc_info);
if (result != vk::Result::eSuccess) {
return {};
}
Expand Down
3 changes: 2 additions & 1 deletion impeller/renderer/backend/vulkan/command_pool_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "flutter/fml/macros.h"
#include "impeller/base/thread.h"
#include "impeller/renderer/backend/vulkan/device_holder.h"
#include "impeller/renderer/backend/vulkan/shared_object_vk.h"
#include "impeller/renderer/backend/vulkan/vk.h"

Expand Down Expand Up @@ -38,7 +39,7 @@ class CommandPoolVK {

private:
const std::thread::id owner_id_;
vk::Device device_ = {};
std::weak_ptr<const DeviceHolder> device_holder_;
vk::UniqueCommandPool graphics_pool_;
Mutex buffers_to_collect_mutex_;
std::set<SharedHandleVK<vk::CommandBuffer>> buffers_to_collect_
Expand Down
30 changes: 16 additions & 14 deletions impeller/renderer/backend/vulkan/context_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,12 @@ void ContextVK::Setup(Settings settings) {
device_info.setPEnabledFeatures(&required_features.value());
// Device layers are deprecated and ignored.

auto device = physical_device->createDeviceUnique(device_info);
if (device.result != vk::Result::eSuccess) {
auto device_result = physical_device->createDeviceUnique(device_info);
if (device_result.result != vk::Result::eSuccess) {
VALIDATION_LOG << "Could not create logical device.";
return;
}
vk::UniqueDevice device = std::move(device_result.value);

if (!caps->SetDevice(physical_device.value())) {
VALIDATION_LOG << "Capabilities could not be updated.";
Expand All @@ -291,7 +292,7 @@ void ContextVK::Setup(Settings settings) {
weak_from_this(), //
application_info.apiVersion, //
physical_device.value(), //
device.value.get(), //
device.get(), //
instance.value.get(), //
dispatcher.vkGetInstanceProcAddr, //
dispatcher.vkGetDeviceProcAddr //
Expand All @@ -306,7 +307,8 @@ void ContextVK::Setup(Settings settings) {
/// Setup the pipeline library.
///
auto pipeline_library = std::shared_ptr<PipelineLibraryVK>(
new PipelineLibraryVK(device.value.get(), //
new PipelineLibraryVK(weak_from_this(), //
device.get(), //
caps, //
std::move(settings.cache_directory), //
settings.worker_task_runner //
Expand All @@ -317,11 +319,11 @@ void ContextVK::Setup(Settings settings) {
return;
}

auto sampler_library = std::shared_ptr<SamplerLibraryVK>(
new SamplerLibraryVK(device.value.get()));
auto sampler_library =
std::shared_ptr<SamplerLibraryVK>(new SamplerLibraryVK(device.get()));

auto shader_library = std::shared_ptr<ShaderLibraryVK>(
new ShaderLibraryVK(device.value.get(), //
new ShaderLibraryVK(device.get(), //
settings.shader_libraries_data) //
);

Expand All @@ -334,7 +336,7 @@ void ContextVK::Setup(Settings settings) {
/// Create the fence waiter.
///
auto fence_waiter =
std::shared_ptr<FenceWaiterVK>(new FenceWaiterVK(device.value.get()));
std::shared_ptr<FenceWaiterVK>(new FenceWaiterVK(device.get()));
if (!fence_waiter->IsValid()) {
VALIDATION_LOG << "Could not create fence waiter.";
return;
Expand All @@ -343,7 +345,7 @@ void ContextVK::Setup(Settings settings) {
//----------------------------------------------------------------------------
/// Fetch the queues.
///
QueuesVK queues(device.value.get(), //
QueuesVK queues(device.get(), //
graphics_queue.value(), //
compute_queue.value(), //
transfer_queue.value() //
Expand All @@ -363,7 +365,7 @@ void ContextVK::Setup(Settings settings) {
instance_ = std::move(instance.value);
debug_report_ = std::move(debug_report);
physical_device_ = physical_device.value();
device_ = std::move(device.value);
device_ = std::move(device);
allocator_ = std::move(allocator);
shader_library_ = std::move(shader_library);
sampler_library_ = std::move(sampler_library);
Expand All @@ -378,7 +380,7 @@ void ContextVK::Setup(Settings settings) {
/// Label all the relevant objects. This happens after setup so that the debug
/// messengers have had a chance to be setup.
///
SetDebugName(device_.get(), device_.get(), "ImpellerDevice");
SetDebugName(GetDevice(), device_.get(), "ImpellerDevice");
}

// |Context|
Expand Down Expand Up @@ -421,8 +423,8 @@ vk::Instance ContextVK::GetInstance() const {
return *instance_;
}

vk::Device ContextVK::GetDevice() const {
return *device_;
const vk::Device& ContextVK::GetDevice() const {
return device_.get();
}

std::unique_ptr<Surface> ContextVK::AcquireNextSurface() {
Expand Down Expand Up @@ -489,7 +491,7 @@ std::unique_ptr<CommandEncoderVK> ContextVK::CreateGraphicsCommandEncoder()
return nullptr;
}
auto encoder = std::unique_ptr<CommandEncoderVK>(new CommandEncoderVK(
*device_, //
weak_from_this(), //
queues_.graphics_queue, //
tls_pool, //
fence_waiter_ //
Expand Down
13 changes: 9 additions & 4 deletions impeller/renderer/backend/vulkan/context_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "flutter/fml/unique_fd.h"
#include "impeller/base/backend_cast.h"
#include "impeller/core/formats.h"
#include "impeller/renderer/backend/vulkan/device_holder.h"
#include "impeller/renderer/backend/vulkan/pipeline_library_vk.h"
#include "impeller/renderer/backend/vulkan/queue_vk.h"
#include "impeller/renderer/backend/vulkan/sampler_library_vk.h"
Expand All @@ -30,7 +31,10 @@ class CommandEncoderVK;
class DebugReportVK;
class FenceWaiterVK;

class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
class ContextVK final : public Context,
public BackendCast<ContextVK, Context>,
public DeviceHolder,
public std::enable_shared_from_this<ContextVK> {
public:
struct Settings {
PFN_vkGetInstanceProcAddr proc_address_callback = nullptr;
Expand Down Expand Up @@ -77,11 +81,11 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {

template <typename T>
bool SetDebugName(T handle, std::string_view label) const {
return SetDebugName(*device_, handle, label);
return SetDebugName(GetDevice(), handle, label);
}

template <typename T>
static bool SetDebugName(vk::Device device,
static bool SetDebugName(const vk::Device& device,
T handle,
std::string_view label) {
if (!HasValidationLayers()) {
Expand All @@ -106,7 +110,8 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {

vk::Instance GetInstance() const;

vk::Device GetDevice() const;
// |DeviceHolder|
const vk::Device& GetDevice() const override;

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

Expand Down
Loading