Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
command_buffer_mtl.mm
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
9
14
15namespace impeller {
16
17API_AVAILABLE(ios(14.0), macos(11.0))
18static NSString* MTLCommandEncoderErrorStateToString(
19 MTLCommandEncoderErrorState state) {
20 switch (state) {
21 case MTLCommandEncoderErrorStateUnknown:
22 return @"unknown";
23 case MTLCommandEncoderErrorStateCompleted:
24 return @"completed";
25 case MTLCommandEncoderErrorStateAffected:
26 return @"affected";
27 case MTLCommandEncoderErrorStatePending:
28 return @"pending";
29 case MTLCommandEncoderErrorStateFaulted:
30 return @"faulted";
31 }
32 return @"unknown";
33}
34
35static NSString* MTLCommandBufferErrorToString(MTLCommandBufferError code) {
36 switch (code) {
37 case MTLCommandBufferErrorNone:
38 return @"none";
39 case MTLCommandBufferErrorInternal:
40 return @"internal";
41 case MTLCommandBufferErrorTimeout:
42 return @"timeout";
43 case MTLCommandBufferErrorPageFault:
44 return @"page fault";
45 case MTLCommandBufferErrorNotPermitted:
46 return @"not permitted";
47 case MTLCommandBufferErrorOutOfMemory:
48 return @"out of memory";
49 case MTLCommandBufferErrorInvalidResource:
50 return @"invalid resource";
51 case MTLCommandBufferErrorMemoryless:
52 return @"memory-less";
53 default:
54 break;
55 }
56
57 return [NSString stringWithFormat:@"<unknown> %zu", code];
58}
59
60static bool LogMTLCommandBufferErrorIfPresent(id<MTLCommandBuffer> buffer) {
61 if (!buffer) {
62 return true;
63 }
64
65 if (buffer.status == MTLCommandBufferStatusCompleted) {
66 return true;
67 }
68
69 std::stringstream stream;
70 stream << ">>>>>>>" << std::endl;
71 stream << "Impeller command buffer could not be committed!" << std::endl;
72
73 if (auto desc = buffer.error.localizedDescription) {
74 stream << desc.UTF8String << std::endl;
75 }
76
77 if (buffer.error) {
78 stream << "Domain: "
79 << (buffer.error.domain.length > 0u ? buffer.error.domain.UTF8String
80 : "<unknown>")
81 << " Code: "
83 static_cast<MTLCommandBufferError>(buffer.error.code))
84 .UTF8String
85 << std::endl;
86 }
87
88 if (@available(iOS 14.0, macOS 11.0, *)) {
89 NSArray<id<MTLCommandBufferEncoderInfo>>* infos =
90 buffer.error.userInfo[MTLCommandBufferEncoderInfoErrorKey];
91 for (id<MTLCommandBufferEncoderInfo> info in infos) {
92 stream << (info.label.length > 0u ? info.label.UTF8String
93 : "<Unlabelled Render Pass>")
94 << ": "
95 << MTLCommandEncoderErrorStateToString(info.errorState).UTF8String
96 << std::endl;
97
98 auto signposts = [info.debugSignposts componentsJoinedByString:@", "];
99 if (signposts.length > 0u) {
100 stream << signposts.UTF8String << std::endl;
101 }
102 }
103
104 for (id<MTLFunctionLog> log in buffer.logs) {
105 auto desc = log.description;
106 if (desc.length > 0u) {
107 stream << desc.UTF8String << std::endl;
108 }
109 }
110 }
111
112 stream << "<<<<<<<";
113 VALIDATION_LOG << stream.str();
114 return false;
115}
116
117static id<MTLCommandBuffer> CreateCommandBuffer(id<MTLCommandQueue> queue) {
118#ifndef FLUTTER_RELEASE
119 if (@available(iOS 14.0, macOS 11.0, *)) {
120 auto desc = [[MTLCommandBufferDescriptor alloc] init];
121 // Degrades CPU performance slightly but is well worth the cost for typical
122 // Impeller workloads.
123 desc.errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus;
124 return [queue commandBufferWithDescriptor:desc];
125 }
126#endif // FLUTTER_RELEASE
127 return [queue commandBuffer];
128}
129
130CommandBufferMTL::CommandBufferMTL(const std::weak_ptr<const Context>& context,
131 id<MTLDevice> device,
132 id<MTLCommandQueue> queue)
133 : CommandBuffer(context),
134 buffer_(CreateCommandBuffer(queue)),
135 device_(device) {}
136
137CommandBufferMTL::~CommandBufferMTL() = default;
138
139bool CommandBufferMTL::IsValid() const {
140 return buffer_ != nil;
141}
142
143void CommandBufferMTL::SetLabel(std::string_view label) const {
144#ifdef IMPELLER_DEBUG
145 if (label.empty()) {
146 return;
147 }
148
149 [buffer_ setLabel:@(label.data())];
150#endif // IMPELLER_DEBUG
151}
152
153static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status) {
154 switch (status) {
155 case MTLCommandBufferStatusCompleted:
156 return CommandBufferMTL::Status::kCompleted;
157 case MTLCommandBufferStatusEnqueued:
158 return CommandBufferMTL::Status::kPending;
159 default:
160 break;
161 }
162 return CommandBufferMTL::Status::kError;
163}
164
165bool CommandBufferMTL::OnSubmitCommands(bool block_on_schedule,
166 CompletionCallback callback) {
167 auto context = context_.lock();
168 if (!context) {
169 return false;
170 }
171#ifdef IMPELLER_DEBUG
172 ContextMTL::Cast(*context).GetGPUTracer()->RecordCmdBuffer(buffer_);
173#endif // IMPELLER_DEBUG
174
175 // Copied so the block keeps the tracker alive past context teardown.
176 std::shared_ptr<GpuSubmissionTracker> tracker =
177 ContextMTL::Cast(*context).GetMutableSubmissionTracker();
178 uint64_t submission_id = tracker->RecordSubmission();
179 [buffer_ addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
180 tracker->RecordCompletion(submission_id);
181 }];
182
183 if (callback) {
184 [buffer_
185 addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
186 [[maybe_unused]] auto result =
188 FML_DCHECK(result)
189 << "Must not have errors during command buffer submission.";
191 }];
192 }
193
194 [buffer_ commit];
195 if (block_on_schedule) {
196 [buffer_ waitUntilScheduled];
197 }
198
199 buffer_ = nil;
200 return true;
201}
202
203void CommandBufferMTL::OnWaitUntilCompleted() {}
204
205void CommandBufferMTL::OnWaitUntilScheduled() {}
206
207std::shared_ptr<RenderPass> CommandBufferMTL::OnCreateRenderPass(
208 RenderTarget target) {
209 if (!buffer_) {
210 return nullptr;
211 }
212
213 auto context = context_.lock();
214 if (!context) {
215 return nullptr;
216 }
217 auto pass = std::shared_ptr<RenderPassMTL>(
218 new RenderPassMTL(context, target, buffer_));
219 if (!pass->IsValid()) {
220 return nullptr;
221 }
222
223 return pass;
224}
225
226std::shared_ptr<BlitPass> CommandBufferMTL::OnCreateBlitPass() {
227 if (!buffer_) {
228 return nullptr;
229 }
230
231 auto pass = std::shared_ptr<BlitPassMTL>(new BlitPassMTL(buffer_, device_));
232 if (!pass->IsValid()) {
233 return nullptr;
234 }
235
236 return pass;
237}
238
239std::shared_ptr<ComputePass> CommandBufferMTL::OnCreateComputePass() {
240 if (!buffer_) {
241 return nullptr;
242 }
243 auto context = context_.lock();
244 if (!context) {
245 return nullptr;
246 }
247
248 auto pass =
249 std::shared_ptr<ComputePassMTL>(new ComputePassMTL(context, buffer_));
250 if (!pass->IsValid()) {
251 return nullptr;
252 }
253
254 return pass;
255}
256
257} // namespace impeller
VkDevice device
Definition main.cc:69
VkQueue queue
Definition main.cc:71
uint32_t * target
FlutterDesktopBinaryReply callback
#define FML_DCHECK(condition)
Definition logging.h:122
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 disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98
static bool LogMTLCommandBufferErrorIfPresent(id< MTLCommandBuffer > buffer)
API_AVAILABLE(ios(14.0), macos(11.0)) static NSString *MTLCommandEncoderErrorStateToString(MTLCommandEncoderErrorState state)
static NSString * MTLCommandBufferErrorToString(MTLCommandBufferError code)
static id< MTLCommandBuffer > CreateCommandBuffer(id< MTLCommandQueue > queue)
static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status)
std::shared_ptr< ContextGLES > context
#define VALIDATION_LOG
Definition validation.h:91