Flutter Engine
The Flutter Engine
MtlCommandBuffer.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef skgpu_graphite_MtlCommandBuffer_DEFINED
9#define skgpu_graphite_MtlCommandBuffer_DEFINED
10
14
15#include <memory>
16
19
20#import <Metal/Metal.h>
21
22namespace skgpu::graphite {
23class ComputePipeline;
24class MtlBlitCommandEncoder;
25class MtlComputeCommandEncoder;
26class MtlRenderCommandEncoder;
27class MtlResourceProvider;
28class MtlSharedContext;
29struct WorkgroupSize;
30
31class MtlCommandBuffer final : public CommandBuffer {
32public:
33 static std::unique_ptr<MtlCommandBuffer> Make(id<MTLCommandQueue>,
34 const MtlSharedContext*,
36 ~MtlCommandBuffer() override;
37
38 bool setNewCommandBufferResources() override;
39
40 void addWaitSemaphores(size_t numWaitSemaphores,
41 const BackendSemaphore* waitSemaphores) override;
42 void addSignalSemaphores(size_t numSignalSemaphores,
43 const BackendSemaphore* signalSemaphores) override;
44
45 bool isFinished() {
46 return (*fCommandBuffer).status == MTLCommandBufferStatusCompleted ||
47 (*fCommandBuffer).status == MTLCommandBufferStatusError;
48
49 }
51 // TODO: it's not clear what do to if status is Enqueued. Commit and then wait?
52 if ((*fCommandBuffer).status == MTLCommandBufferStatusScheduled ||
53 (*fCommandBuffer).status == MTLCommandBufferStatusCommitted) {
54 [(*fCommandBuffer) waitUntilCompleted];
55 }
56 if (!this->isFinished()) {
57 SKGPU_LOG_E("Unfinished command buffer status: %d",
58 (int)(*fCommandBuffer).status);
59 SkASSERT(false);
60 }
61 }
62 bool commit();
63
64private:
65 MtlCommandBuffer(id<MTLCommandQueue>,
66 const MtlSharedContext* sharedContext,
67 MtlResourceProvider* resourceProvider);
68
69 bool createNewMTLCommandBuffer();
70
71 void onResetCommandBuffer() override;
72
73 bool onAddRenderPass(const RenderPassDesc&,
74 const Texture* colorTexture,
75 const Texture* resolveTexture,
76 const Texture* depthStencilTexture,
77 SkRect viewport,
78 const DrawPassList&) override;
79 bool onAddComputePass(DispatchGroupSpan) override;
80
81 // Methods for populating a MTLRenderCommandEncoder:
82 bool beginRenderPass(const RenderPassDesc&,
83 const Texture* colorTexture,
84 const Texture* resolveTexture,
85 const Texture* depthStencilTexture);
86 void endRenderPass();
87
88 void addDrawPass(const DrawPass*);
89
90 void bindGraphicsPipeline(const GraphicsPipeline*);
91 void setBlendConstants(float* blendConstants);
92
93 void bindUniformBuffer(const BindBufferInfo& info, UniformSlot);
94 void bindDrawBuffers(const BindBufferInfo& vertices,
95 const BindBufferInfo& instances,
96 const BindBufferInfo& indices,
97 const BindBufferInfo& indirect);
98 void bindVertexBuffers(const Buffer* vertexBuffer, size_t vertexOffset,
99 const Buffer* instanceBuffer, size_t instanceOffset);
100 void bindIndexBuffer(const Buffer* indexBuffer, size_t offset);
101 void bindIndirectBuffer(const Buffer* indirectBuffer, size_t offset);
102
103 void bindTextureAndSampler(const Texture*, const Sampler*, unsigned int bindIndex);
104
105 void setScissor(unsigned int left, unsigned int top,
106 unsigned int width, unsigned int height);
107 void setViewport(float x, float y, float width, float height,
108 float minDepth, float maxDepth);
109
110 void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount);
111 void drawIndexed(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount,
112 unsigned int baseVertex);
113 void drawInstanced(PrimitiveType type,
114 unsigned int baseVertex, unsigned int vertexCount,
115 unsigned int baseInstance, unsigned int instanceCount);
116 void drawIndexedInstanced(PrimitiveType type, unsigned int baseIndex,
117 unsigned int indexCount, unsigned int baseVertex,
118 unsigned int baseInstance, unsigned int instanceCount);
119 void drawIndirect(PrimitiveType type);
120 void drawIndexedIndirect(PrimitiveType type);
121
122 // Methods for populating a MTLComputeCommandEncoder:
123 void beginComputePass();
124 void bindComputePipeline(const ComputePipeline*);
125 void bindBuffer(const Buffer* buffer, unsigned int offset, unsigned int index);
126 void bindTexture(const Texture* texture, unsigned int index);
127 void bindSampler(const Sampler* sampler, unsigned int index);
128 void dispatchThreadgroups(const WorkgroupSize& globalSize, const WorkgroupSize& localSize);
129 void dispatchThreadgroupsIndirect(const WorkgroupSize& localSize,
130 const Buffer* indirectBuffer,
131 size_t indirectBufferOffset);
132 void endComputePass();
133
134 // Methods for populating a MTLBlitCommandEncoder:
135 bool onCopyBufferToBuffer(const Buffer* srcBuffer,
136 size_t srcOffset,
137 const Buffer* dstBuffer,
138 size_t dstOffset,
139 size_t size) override;
140 bool onCopyTextureToBuffer(const Texture*,
141 SkIRect srcRect,
142 const Buffer*,
143 size_t bufferOffset,
144 size_t bufferRowBytes) override;
145 bool onCopyBufferToTexture(const Buffer*,
146 const Texture*,
147 const BufferTextureCopyData* copyData,
148 int count) override;
149 bool onCopyTextureToTexture(const Texture* src,
150 SkIRect srcRect,
151 const Texture* dst,
152 SkIPoint dstPoint,
153 int mipLevel) override;
154 bool onSynchronizeBufferToCpu(const Buffer*, bool* outDidResultInWork) override;
155 bool onClearBuffer(const Buffer*, size_t offset, size_t size) override;
156
157 MtlBlitCommandEncoder* getBlitCommandEncoder();
158 void endBlitCommandEncoder();
159
160 sk_cfp<id<MTLCommandBuffer>> fCommandBuffer;
161 sk_sp<MtlRenderCommandEncoder> fActiveRenderCommandEncoder;
162 sk_sp<MtlComputeCommandEncoder> fActiveComputeCommandEncoder;
163 sk_sp<MtlBlitCommandEncoder> fActiveBlitCommandEncoder;
164
165 id<MTLBuffer> fCurrentIndexBuffer;
166 id<MTLBuffer> fCurrentIndirectBuffer;
167 size_t fCurrentIndexBufferOffset = 0;
168 size_t fCurrentIndirectBufferOffset = 0;
169
170 // The command buffer will outlive the MtlQueueManager which owns the MTLCommandQueue.
171 id<MTLCommandQueue> fQueue;
172 const MtlSharedContext* fSharedContext;
173 MtlResourceProvider* fResourceProvider;
174
175 // If true, the draw commands being added are entirely offscreen and can be skipped.
176 // This can happen if a recording is being replayed with a transform that moves the recorded
177 // commands outside of the render target bounds.
178 bool fDrawIsOffscreen = false;
179};
180
181} // namespace skgpu::graphite
182
183#endif // skgpu_graphite_MtlCommandBuffer_DEFINED
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
int count
Definition: FontMgrTest.cpp:50
#define SKGPU_LOG_E(fmt,...)
Definition: Log.h:38
#define SkASSERT(cond)
Definition: SkAssert.h:116
GLenum type
skia_private::TArray< std::unique_ptr< DrawPass > > DrawPassList
Definition: CommandBuffer.h:40
SkSpan< const std::unique_ptr< DispatchGroup > > DispatchGroupSpan
Definition: CommandBuffer.h:41
void addWaitSemaphores(size_t numWaitSemaphores, const BackendSemaphore *waitSemaphores) override
void addSignalSemaphores(size_t numSignalSemaphores, const BackendSemaphore *signalSemaphores) override
static std::unique_ptr< MtlCommandBuffer > Make(id< MTLCommandQueue >, const MtlSharedContext *, MtlResourceProvider *)
FlTexture * texture
double y
double x
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
dst
Definition: cp.py:12
int32_t height
int32_t width
SeparatedVector2 offset
Definition: SkRect.h:32