| Flutter Impeller |

command_queue_vk.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "fml/status.h"
6
7 #include "impeller/renderer/backend/vulkan/command_queue_vk.h"
8
9 #include "impeller/base/validation.h"
10 #include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
11 #include "impeller/renderer/backend/vulkan/command_encoder_vk.h"
12 #include "impeller/renderer/backend/vulkan/context_vk.h"
13 #include "impeller/renderer/backend/vulkan/fence_waiter_vk.h"
14 #include "impeller/renderer/backend/vulkan/tracked_objects_vk.h"
15 #include "impeller/renderer/command_buffer.h"
16
17 namespace impeller {
18
19 CommandQueueVK::CommandQueueVK(const std::weak_ptr<ContextVK>& context)
20 : context_(context) {}
21
22 CommandQueueVK::~CommandQueueVK() = default;
23
24 fml::Status CommandQueueVK::Submit(
25 const std::vector<std::shared_ptr<CommandBuffer>>& buffers,
26 const CompletionCallback& completion_callback) {
27 if (buffers.empty()) {
28 return fml::Status(fml::StatusCode::kInvalidArgument,
29 "No command buffers provided.");
30 }
31 // Success or failure, you only get to submit once.
32 fml::ScopedCleanupClosure reset([&]() {
33 if (completion_callback) {
34 completion_callback(CommandBuffer::Status::kError);
35 }
36 });
37
38 std::vector<vk::CommandBuffer> vk_buffers;
39 std::vector<std::shared_ptr<TrackedObjectsVK>> tracked_objects;
40 vk_buffers.reserve(buffers.size());
41 tracked_objects.reserve(buffers.size());
42 for (const std::shared_ptr<CommandBuffer>& buffer : buffers) {
43 auto encoder = CommandBufferVK::Cast(*buffer).GetEncoder();
44 if (!encoder->EndCommandBuffer()) {
45 return fml::Status(fml::StatusCode::kCancelled,
46 "Failed to end command buffer.");
47 }
48 tracked_objects.push_back(encoder->tracked_objects_);
49 vk_buffers.push_back(encoder->GetCommandBuffer());
50 encoder->Reset();
51 }
52
53 auto context = context_.lock();
54 if (!context) {
55 VALIDATION_LOG << "Device lost.";
56 return fml::Status(fml::StatusCode::kCancelled, "Device lost.");
57 }
58 auto [fence_result, fence] = context->GetDevice().createFenceUnique({});
59 if (fence_result != vk::Result::eSuccess) {
60 VALIDATION_LOG << "Failed to create fence: " << vk::to_string(fence_result);
61 return fml::Status(fml::StatusCode::kCancelled, "Failed to create fence.");
62 }
63
64 vk::SubmitInfo submit_info;
65 submit_info.setCommandBuffers(vk_buffers);
66 auto status = context->GetGraphicsQueue()->Submit(submit_info, *fence);
67 if (status != vk::Result::eSuccess) {
68 VALIDATION_LOG << "Failed to submit queue: " << vk::to_string(status);
69 return fml::Status(fml::StatusCode::kCancelled, "Failed to submit queue: ");
70 }
71
72 // Submit will proceed, call callback with true when it is done and do not
73 // call when `reset` is collected.
74 auto added_fence = context->GetFenceWaiter()->AddFence(
75 std::move(fence), [completion_callback, tracked_objects = std::move(
76 tracked_objects)]() mutable {
77 // Ensure tracked objects are destructed before calling any final
78 // callbacks.
79 tracked_objects.clear();
80 if (completion_callback) {
81 completion_callback(CommandBuffer::Status::kCompleted);
82 }
83 });
84 if (!added_fence) {
85 return fml::Status(fml::StatusCode::kCancelled, "Failed to add fence.");
86 }
87 reset.Release();
88 return fml::Status();
89 }
90
91 } // namespace impeller
impeller::CommandBuffer::Status::kError
@ kError
fence_waiter_vk.h
tracked_objects_vk.h
impeller::CommandBufferVK::GetEncoder
const std::shared_ptr< CommandEncoderVK > & GetEncoder()
Definition: command_buffer_vk.cc:45
command_encoder_vk.h
validation.h
impeller::CommandQueueVK::Submit
fml::Status Submit(const std::vector< std::shared_ptr< CommandBuffer >> &buffers, const CompletionCallback &completion_callback={}) override
Submit one or more command buffer objects to be encoded and executed on the GPU.
Definition: command_queue_vk.cc:24
command_buffer_vk.h
impeller::CommandQueueVK::~CommandQueueVK
~CommandQueueVK() override
impeller::CommandQueue::CompletionCallback
std::function< void(CommandBuffer::Status)> CompletionCallback
Definition: command_queue.h:19
impeller::CommandQueueVK::CommandQueueVK
CommandQueueVK(const std::weak_ptr< ContextVK > &context)
Definition: command_queue_vk.cc:19
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
command_buffer.h
impeller::CommandBuffer::Status::kCompleted
@ kCompleted
impeller::BackendCast< CommandBufferVK, CommandBuffer >::Cast
static CommandBufferVK & Cast(CommandBuffer &base)
Definition: backend_cast.h:13
command_queue_vk.h
context_vk.h
impeller
Definition: aiks_blur_unittests.cc:20