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