Flutter Impeller
command_encoder_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 
6 #include <string>
7 
8 #include "flutter/fml/closure.h"
9 #include "fml/status.h"
10 #include "fml/status_or.h"
16 
17 namespace impeller {
18 
20  const std::weak_ptr<const ContextVK>& context)
21  : context_(context) {}
22 
23 void CommandEncoderFactoryVK::SetLabel(const std::string& label) {
24  label_ = label;
25 }
26 
27 std::shared_ptr<CommandEncoderVK> CommandEncoderFactoryVK::Create() {
28  auto context = context_.lock();
29  if (!context) {
30  return nullptr;
31  }
32  auto recycler = context->GetCommandPoolRecycler();
33  if (!recycler) {
34  return nullptr;
35  }
36  auto tls_pool = recycler->Get();
37  if (!tls_pool) {
38  return nullptr;
39  }
40 
41  auto tracked_objects = std::make_shared<TrackedObjectsVK>(
42  context, tls_pool, context->GetGPUTracer()->CreateGPUProbe());
43  auto queue = context->GetGraphicsQueue();
44 
45  if (!tracked_objects || !tracked_objects->IsValid() || !queue) {
46  return nullptr;
47  }
48 
49  vk::CommandBufferBeginInfo begin_info;
50  begin_info.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit;
51  if (tracked_objects->GetCommandBuffer().begin(begin_info) !=
52  vk::Result::eSuccess) {
53  VALIDATION_LOG << "Could not begin command buffer.";
54  return nullptr;
55  }
56 
57  if (label_.has_value()) {
58  context->SetDebugName(tracked_objects->GetCommandBuffer(), label_.value());
59  }
60  tracked_objects->GetGPUProbe().RecordCmdBufferStart(
61  tracked_objects->GetCommandBuffer());
62 
63  return std::make_shared<CommandEncoderVK>(context->GetDeviceHolder(),
64  tracked_objects, queue,
65  context->GetFenceWaiter());
66 }
67 
69  std::weak_ptr<const DeviceHolderVK> device_holder,
70  std::shared_ptr<TrackedObjectsVK> tracked_objects,
71  const std::shared_ptr<QueueVK>& queue,
72  std::shared_ptr<FenceWaiterVK> fence_waiter)
73  : device_holder_(std::move(device_holder)),
74  tracked_objects_(std::move(tracked_objects)),
75  queue_(queue),
76  fence_waiter_(std::move(fence_waiter)) {}
77 
79 
81  return is_valid_;
82 }
83 
85  InsertDebugMarker("QueueSubmit");
86 
87  auto command_buffer = GetCommandBuffer();
88  tracked_objects_->GetGPUProbe().RecordCmdBufferEnd(command_buffer);
89 
90  auto status = command_buffer.end();
91  if (status != vk::Result::eSuccess) {
92  VALIDATION_LOG << "Failed to end command buffer: " << vk::to_string(status);
93  return false;
94  }
95  return true;
96 }
97 
98 vk::CommandBuffer CommandEncoderVK::GetCommandBuffer() const {
99  if (tracked_objects_) {
100  return tracked_objects_->GetCommandBuffer();
101  }
102  return {};
103 }
104 
105 void CommandEncoderVK::Reset() {
106  tracked_objects_.reset();
107 
108  queue_ = nullptr;
109  is_valid_ = false;
110 }
111 
112 bool CommandEncoderVK::Track(std::shared_ptr<SharedObjectVK> object) {
113  if (!IsValid()) {
114  return false;
115  }
116  tracked_objects_->Track(std::move(object));
117  return true;
118 }
119 
120 bool CommandEncoderVK::Track(std::shared_ptr<const DeviceBuffer> buffer) {
121  if (!IsValid()) {
122  return false;
123  }
124  tracked_objects_->Track(std::move(buffer));
125  return true;
126 }
127 
129  const std::shared_ptr<const DeviceBuffer>& buffer) const {
130  if (!IsValid()) {
131  return false;
132  }
133  return tracked_objects_->IsTracking(buffer);
134 }
135 
136 bool CommandEncoderVK::Track(std::shared_ptr<const TextureSourceVK> texture) {
137  if (!IsValid()) {
138  return false;
139  }
140  tracked_objects_->Track(std::move(texture));
141  return true;
142 }
143 
144 bool CommandEncoderVK::Track(const std::shared_ptr<const Texture>& texture) {
145  if (!IsValid()) {
146  return false;
147  }
148  if (!texture) {
149  return true;
150  }
151  return Track(TextureVK::Cast(*texture).GetTextureSource());
152 }
153 
155  const std::shared_ptr<const Texture>& texture) const {
156  if (!IsValid()) {
157  return false;
158  }
159  std::shared_ptr<const TextureSourceVK> source =
160  TextureVK::Cast(*texture).GetTextureSource();
161  return tracked_objects_->IsTracking(source);
162 }
163 
164 fml::StatusOr<vk::DescriptorSet> CommandEncoderVK::AllocateDescriptorSets(
165  const vk::DescriptorSetLayout& layout,
166  const ContextVK& context) {
167  if (!IsValid()) {
168  return fml::Status(fml::StatusCode::kUnknown, "command encoder invalid");
169  }
170 
171  return tracked_objects_->GetDescriptorPool().AllocateDescriptorSets(layout,
172  context);
173 }
174 
175 void CommandEncoderVK::PushDebugGroup(std::string_view label) const {
176  if (!HasValidationLayers()) {
177  return;
178  }
179  vk::DebugUtilsLabelEXT label_info;
180  label_info.pLabelName = label.data();
181  if (auto command_buffer = GetCommandBuffer()) {
182  command_buffer.beginDebugUtilsLabelEXT(label_info);
183  }
184 }
185 
187  if (!HasValidationLayers()) {
188  return;
189  }
190  if (auto command_buffer = GetCommandBuffer()) {
191  command_buffer.endDebugUtilsLabelEXT();
192  }
193 }
194 
195 void CommandEncoderVK::InsertDebugMarker(std::string_view label) const {
196  if (!HasValidationLayers()) {
197  return;
198  }
199  vk::DebugUtilsLabelEXT label_info;
200  label_info.pLabelName = label.data();
201  if (auto command_buffer = GetCommandBuffer()) {
202  command_buffer.insertDebugUtilsLabelEXT(label_info);
203  }
204  if (queue_) {
205  queue_->InsertDebugMarker(label);
206  }
207 }
208 
209 } // namespace impeller
fence_waiter_vk.h
gpu_tracer_vk.h
tracked_objects_vk.h
command_encoder_vk.h
impeller::CommandEncoderVK::InsertDebugMarker
void InsertDebugMarker(std::string_view label) const
Definition: command_encoder_vk.cc:195
impeller::TextureVK::GetTextureSource
std::shared_ptr< const TextureSourceVK > GetTextureSource() const
Definition: texture_vk.cc:155
impeller::CommandEncoderFactoryVK::SetLabel
void SetLabel(const std::string &label)
Definition: command_encoder_vk.cc:23
impeller::CommandEncoderVK::Track
bool Track(std::shared_ptr< SharedObjectVK > object)
Definition: command_encoder_vk.cc:112
impeller::CommandEncoderVK::EndCommandBuffer
bool EndCommandBuffer() const
Definition: command_encoder_vk.cc:84
impeller::CommandEncoderVK::IsTracking
bool IsTracking(const std::shared_ptr< const DeviceBuffer > &texture) const
Definition: command_encoder_vk.cc:128
impeller::CommandEncoderVK::PushDebugGroup
void PushDebugGroup(std::string_view label) const
Definition: command_encoder_vk.cc:175
impeller::CommandEncoderFactoryVK::CommandEncoderFactoryVK
CommandEncoderFactoryVK(const std::weak_ptr< const ContextVK > &context)
Definition: command_encoder_vk.cc:19
impeller::CommandEncoderVK::AllocateDescriptorSets
fml::StatusOr< vk::DescriptorSet > AllocateDescriptorSets(const vk::DescriptorSetLayout &layout, const ContextVK &context)
Definition: command_encoder_vk.cc:164
impeller::CommandEncoderVK::PopDebugGroup
void PopDebugGroup() const
Definition: command_encoder_vk.cc:186
texture_vk.h
impeller::ContextVK
Definition: context_vk.h:42
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::CommandEncoderVK::~CommandEncoderVK
~CommandEncoderVK()
std
Definition: comparable.h:95
impeller::BackendCast< TextureVK, Texture >::Cast
static TextureVK & Cast(Texture &base)
Definition: backend_cast.h:13
impeller::CommandEncoderVK::CommandEncoderVK
CommandEncoderVK(std::weak_ptr< const DeviceHolderVK > device_holder, std::shared_ptr< TrackedObjectsVK > tracked_objects, const std::shared_ptr< QueueVK > &queue, std::shared_ptr< FenceWaiterVK > fence_waiter)
Definition: command_encoder_vk.cc:68
context_vk.h
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:48
impeller::CommandEncoderVK::IsValid
bool IsValid() const
Definition: command_encoder_vk.cc:80
impeller
Definition: aiks_blur_unittests.cc:20
impeller::CommandEncoderVK::GetCommandBuffer
vk::CommandBuffer GetCommandBuffer() const
Definition: command_encoder_vk.cc:98
impeller::CommandEncoderFactoryVK::Create
std::shared_ptr< CommandEncoderVK > Create()
Definition: command_encoder_vk.cc:27