Flutter Impeller
command_buffer.h
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 #ifndef FLUTTER_IMPELLER_RENDERER_COMMAND_BUFFER_H_
6 #define FLUTTER_IMPELLER_RENDERER_COMMAND_BUFFER_H_
7 
8 #include <functional>
9 #include <memory>
10 
11 #include "flutter/fml/macros.h"
14 
15 namespace impeller {
16 
17 class ComputePass;
18 class Context;
19 class RenderPass;
20 class RenderTarget;
21 
22 namespace testing {
23 class CommandBufferMock;
24 }
25 
26 //------------------------------------------------------------------------------
27 /// @brief A collection of encoded commands to be submitted to the GPU for
28 /// execution. A command buffer is obtained from a graphics
29 /// `Context`.
30 ///
31 /// To submit commands to the GPU, acquire a `RenderPass` from the
32 /// command buffer and record `Command`s into that pass. A
33 /// `RenderPass` describes the configuration of the various
34 /// attachments when the command is submitted.
35 ///
36 /// A command buffer is only meant to be used on a single thread. If
37 /// a frame workload needs to be encoded from multiple threads,
38 /// set up and record into multiple command buffers. The order of
39 /// submission of commands encoded in multiple command buffers can
40 /// be controlled via either the order in which the command buffers
41 /// were created, or, using the `ReserveSpotInQueue` command which
42 /// allows for encoding commands for submission in an order that is
43 /// different from the encoding order.
44 ///
47 
48  public:
49  enum class Status {
50  kPending,
51  kError,
52  kCompleted,
53  };
54 
55  using CompletionCallback = std::function<void(Status)>;
56 
57  virtual ~CommandBuffer();
58 
59  virtual bool IsValid() const = 0;
60 
61  virtual void SetLabel(const std::string& label) const = 0;
62 
63  //----------------------------------------------------------------------------
64  /// @brief Schedule the command encoded by render passes within this
65  /// command buffer on the GPU. The encoding of these commnands is
66  /// performed immediately on the calling thread.
67  ///
68  /// A command buffer may only be committed once.
69  ///
70  /// @param[in] callback The completion callback.
71  ///
72  [[nodiscard]] bool SubmitCommands(const CompletionCallback& callback);
73 
74  [[nodiscard]] bool SubmitCommands();
75 
76  //----------------------------------------------------------------------------
77  /// @brief Schedule the command encoded by render passes within this
78  /// command buffer on the GPU. The enqueing of this buffer is
79  /// performed immediately but encoding is pushed to a worker
80  /// thread if possible.
81  ///
82  /// A command buffer may only be committed once.
83  ///
84  [[nodiscard]] virtual bool EncodeAndSubmit(
85  const std::shared_ptr<RenderPass>& render_pass);
86 
87  //----------------------------------------------------------------------------
88  /// @brief Schedule the command encoded by blit passes within this
89  /// command buffer on the GPU. The enqueing of this buffer is
90  /// performed immediately but encoding is pushed to a worker
91  /// thread if possible.
92  ///
93  /// A command buffer may only be committed once.
94  ///
95  [[nodiscard]] virtual bool EncodeAndSubmit(
96  const std::shared_ptr<BlitPass>& blit_pass,
97  const std::shared_ptr<Allocator>& allocator);
98 
99  //----------------------------------------------------------------------------
100  /// @brief Force execution of pending GPU commands.
101  ///
102  void WaitUntilScheduled();
103 
104  //----------------------------------------------------------------------------
105  /// @brief Create a render pass to record render commands into.
106  ///
107  /// @param[in] render_target The description of the render target this pass
108  /// will target.
109  ///
110  /// @return A valid render pass or null.
111  ///
112  std::shared_ptr<RenderPass> CreateRenderPass(
113  const RenderTarget& render_target);
114 
115  //----------------------------------------------------------------------------
116  /// @brief Create a blit pass to record blit commands into.
117  ///
118  /// @return A valid blit pass or null.
119  ///
120  std::shared_ptr<BlitPass> CreateBlitPass();
121 
122  //----------------------------------------------------------------------------
123  /// @brief Create a compute pass to record compute commands into.
124  ///
125  /// @return A valid compute pass or null.
126  ///
127  std::shared_ptr<ComputePass> CreateComputePass();
128 
129  protected:
130  std::weak_ptr<const Context> context_;
131 
132  explicit CommandBuffer(std::weak_ptr<const Context> context);
133 
134  virtual std::shared_ptr<RenderPass> OnCreateRenderPass(
135  RenderTarget render_target) = 0;
136 
137  virtual std::shared_ptr<BlitPass> OnCreateBlitPass() = 0;
138 
139  [[nodiscard]] virtual bool OnSubmitCommands(CompletionCallback callback) = 0;
140 
141  virtual void OnWaitUntilScheduled() = 0;
142 
143  virtual std::shared_ptr<ComputePass> OnCreateComputePass() = 0;
144 
145  private:
146  CommandBuffer(const CommandBuffer&) = delete;
147 
148  CommandBuffer& operator=(const CommandBuffer&) = delete;
149 };
150 
151 } // namespace impeller
152 
153 #endif // FLUTTER_IMPELLER_RENDERER_COMMAND_BUFFER_H_
impeller::CommandBuffer::Status::kError
@ kError
impeller::CommandBuffer::SubmitCommands
bool SubmitCommands()
Definition: command_buffer.cc:31
impeller::CommandBuffer::OnSubmitCommands
virtual bool OnSubmitCommands(CompletionCallback callback)=0
impeller::CommandBuffer::CreateRenderPass
std::shared_ptr< RenderPass > CreateRenderPass(const RenderTarget &render_target)
Create a render pass to record render commands into.
Definition: command_buffer.cc:66
impeller::CommandBuffer::OnCreateRenderPass
virtual std::shared_ptr< RenderPass > OnCreateRenderPass(RenderTarget render_target)=0
impeller::CommandBuffer::OnCreateComputePass
virtual std::shared_ptr< ComputePass > OnCreateComputePass()=0
impeller::CommandBuffer::CompletionCallback
std::function< void(Status)> CompletionCallback
Definition: command_buffer.h:55
impeller::CommandBuffer::CreateComputePass
std::shared_ptr< ComputePass > CreateComputePass()
Create a compute pass to record compute commands into.
Definition: command_buffer.cc:85
impeller::CommandBuffer::WaitUntilScheduled
void WaitUntilScheduled()
Force execution of pending GPU commands.
Definition: command_buffer.cc:35
impeller::CommandBuffer::context_
std::weak_ptr< const Context > context_
Definition: command_buffer.h:130
impeller::CommandBuffer::Status::kPending
@ kPending
impeller::CommandBuffer::IsValid
virtual bool IsValid() const =0
blit_pass.h
impeller::RenderTarget
Definition: render_target.h:49
impeller::CommandBuffer::SetLabel
virtual void SetLabel(const std::string &label) const =0
impeller::CommandBuffer::OnCreateBlitPass
virtual std::shared_ptr< BlitPass > OnCreateBlitPass()=0
impeller::CommandBuffer::OnWaitUntilScheduled
virtual void OnWaitUntilScheduled()=0
impeller::CommandBuffer::CreateBlitPass
std::shared_ptr< BlitPass > CreateBlitPass()
Create a blit pass to record blit commands into.
Definition: command_buffer.cc:76
impeller::CommandBuffer::Status::kCompleted
@ kCompleted
impeller::CommandBuffer::CommandBufferMock
friend class testing::CommandBufferMock
Definition: command_buffer.h:46
impeller::CommandBuffer::Status
Status
Definition: command_buffer.h:49
impeller::CommandBuffer::EncodeAndSubmit
virtual bool EncodeAndSubmit(const std::shared_ptr< RenderPass > &render_pass)
Schedule the command encoded by render passes within this command buffer on the GPU....
Definition: command_buffer.cc:39
impeller::CommandBuffer::CommandBuffer
CommandBuffer(std::weak_ptr< const Context > context)
Definition: command_buffer.cc:14
compute_pass.h
impeller
Definition: aiks_context.cc:10
impeller::CommandBuffer
A collection of encoded commands to be submitted to the GPU for execution. A command buffer is obtain...
Definition: command_buffer.h:45
impeller::CommandBuffer::~CommandBuffer
virtual ~CommandBuffer()