| 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 "impeller/renderer/blit_pass.h"
12 #include "impeller/renderer/compute_pass.h"
13
14 namespace impeller {
15
16 class ComputePass;
17 class Context;
18 class RenderPass;
19 class RenderTarget;
20 class CommandQueue;
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 ///
45 class CommandBuffer {
46 friend class testing::CommandBufferMock;
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(std::string_view label) const = 0;
62
63 //----------------------------------------------------------------------------
64 /// @brief Block the current thread until the GPU has completed execution
65 /// of the commands.
66 ///
67 void WaitUntilCompleted();
68
69 //----------------------------------------------------------------------------
70 /// @brief Block the current thread until the GPU has completed
71 /// scheduling execution of the commands.
72 ///
73 void WaitUntilScheduled();
74
75 //----------------------------------------------------------------------------
76 /// @brief Create a render pass to record render commands into.
77 ///
78 /// @param[in] render_target The description of the render target this pass
79 /// will target.
80 ///
81 /// @return A valid render pass or null.
82 ///
83 std::shared_ptr<RenderPass> CreateRenderPass(
84 const RenderTarget& render_target);
85
86 //----------------------------------------------------------------------------
87 /// @brief Create a blit pass to record blit commands into.
88 ///
89 /// @return A valid blit pass or null.
90 ///
91 std::shared_ptr<BlitPass> CreateBlitPass();
92
93 //----------------------------------------------------------------------------
94 /// @brief Create a compute pass to record compute commands into.
95 ///
96 /// @return A valid compute pass or null.
97 ///
98 std::shared_ptr<ComputePass> CreateComputePass();
99
100 protected:
101 std::weak_ptr<const Context> context_;
102
103 explicit CommandBuffer(std::weak_ptr<const Context> context);
104
105 virtual std::shared_ptr<RenderPass> OnCreateRenderPass(
106 RenderTarget render_target) = 0;
107
108 virtual std::shared_ptr<BlitPass> OnCreateBlitPass() = 0;
109
110 [[nodiscard]] virtual bool OnSubmitCommands(CompletionCallback callback) = 0;
111
112 virtual void OnWaitUntilCompleted() = 0;
113
114 virtual void OnWaitUntilScheduled() = 0;
115
116 virtual std::shared_ptr<ComputePass> OnCreateComputePass() = 0;
117
118 private:
119 friend class CommandQueue;
120
121 //----------------------------------------------------------------------------
122 /// @brief Schedule the command encoded by render passes within this
123 /// command buffer on the GPU. The encoding of these commnands is
124 /// performed immediately on the calling thread.
125 ///
126 /// A command buffer may only be committed once.
127 ///
128 /// @param[in] callback The completion callback.
129 ///
130 [[nodiscard]] bool SubmitCommands(const CompletionCallback& callback);
131
132 [[nodiscard]] bool SubmitCommands();
133
134 CommandBuffer(const CommandBuffer&) = delete;
135
136 CommandBuffer& operator=(const CommandBuffer&) = delete;
137 };
138
139 } // namespace impeller
140
141 #endif // FLUTTER_IMPELLER_RENDERER_COMMAND_BUFFER_H_
blit_pass.h
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::CreateBlitPass
std::shared_ptr< BlitPass > CreateBlitPass()
Create a blit pass to record blit commands into.
Definition: command_buffer.cc:51
impeller::CommandBuffer::OnWaitUntilCompleted
virtual void OnWaitUntilCompleted()=0
impeller::CommandBuffer::CreateComputePass
std::shared_ptr< ComputePass > CreateComputePass()
Create a compute pass to record compute commands into.
Definition: command_buffer.cc:60
impeller::CommandBuffer::CommandBuffer
CommandBuffer(std::weak_ptr< const Context > context)
Definition: command_buffer.cc:13
impeller::CommandBuffer::IsValid
virtual bool IsValid() const =0
impeller::CommandBuffer::OnCreateComputePass
virtual std::shared_ptr< ComputePass > OnCreateComputePass()=0
impeller::CommandBuffer::OnSubmitCommands
virtual bool OnSubmitCommands(CompletionCallback callback)=0
impeller::CommandBuffer::CompletionCallback
std::function< void(Status)> CompletionCallback
Definition: command_buffer.h:55
impeller::CommandBuffer::OnCreateRenderPass
virtual std::shared_ptr< RenderPass > OnCreateRenderPass(RenderTarget render_target)=0
impeller::CommandBuffer::WaitUntilCompleted
void WaitUntilCompleted()
Block the current thread until the GPU has completed execution of the commands.
Definition: command_buffer.cc:33
impeller::CommandBuffer::SetLabel
virtual void SetLabel(std::string_view label) const =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:41
impeller::CommandBuffer::OnWaitUntilScheduled
virtual void OnWaitUntilScheduled()=0
impeller::CommandBuffer::OnCreateBlitPass
virtual std::shared_ptr< BlitPass > OnCreateBlitPass()=0
impeller::CommandBuffer::~CommandBuffer
virtual ~CommandBuffer()
impeller::CommandBuffer::context_
std::weak_ptr< const Context > context_
Definition: command_buffer.h:101
impeller::CommandBuffer::Status
Status
Definition: command_buffer.h:49
impeller::CommandBuffer::Status::kCompleted
@ kCompleted
impeller::CommandBuffer::Status::kPending
@ kPending
impeller::CommandBuffer::Status::kError
@ kError
impeller::CommandBuffer::CommandBufferMock
friend class testing::CommandBufferMock
Definition: command_buffer.h:46
impeller::CommandBuffer::WaitUntilScheduled
void WaitUntilScheduled()
Block the current thread until the GPU has completed scheduling execution of the commands.
Definition: command_buffer.cc:37
impeller::CommandQueue
An interface for submitting command buffers to the GPU for encoding and execution.
Definition: command_queue.h:17
impeller::RenderTarget
Definition: render_target.h:38
compute_pass.h
impeller
Definition: allocation.cc:12