Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c0e7cd5

Browse files
authored
[Impeller] iOS/macOS: Only wait for command scheduling prior to present (#40781)
1 parent f3e86bd commit c0e7cd5

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

impeller/renderer/backend/metal/command_buffer_mtl.mm

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,27 +167,6 @@ static bool LogMTLCommandBufferErrorIfPresent(id<MTLCommandBuffer> buffer) {
167167

168168
[buffer_ commit];
169169

170-
#if (FML_OS_MACOSX || FML_OS_IOS_SIMULATOR)
171-
// We're using waitUntilScheduled on macOS and iOS simulator to force a hard
172-
// barrier between the execution of different command buffers. This forces all
173-
// renderable texture access to be synchronous (i.e. a write from a previous
174-
// command buffer will not get scheduled to happen at the same time as a read
175-
// in a future command buffer).
176-
//
177-
// Metal hazard tracks shared memory resources by default, and we don't need
178-
// to do any additional work to synchronize access to MTLTextures and
179-
// MTLBuffers on iOS devices with UMA. However, shared textures are disallowed
180-
// on macOS according to the documentation:
181-
// https://developer.apple.com/documentation/metal/mtlstoragemode/shared
182-
// And so this is a stopgap solution that has been present in Impeller since
183-
// multi-pass rendering/SaveLayer support was first set up.
184-
//
185-
// TODO(bdero): Remove this for all targets once a solution for resource
186-
// tracking that works everywhere is established:
187-
// https://github.com/flutter/flutter/issues/120406
188-
[buffer_ waitUntilScheduled];
189-
#endif
190-
191170
buffer_ = nil;
192171
return true;
193172
}

impeller/renderer/backend/metal/context_mtl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class ContextMTL final : public Context,
6464
// |Context|
6565
bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override;
6666

67+
id<MTLCommandBuffer> CreateMTLCommandBuffer() const;
68+
6769
private:
6870
id<MTLDevice> device_ = nullptr;
6971
id<MTLCommandQueue> command_queue_ = nullptr;

impeller/renderer/backend/metal/context_mtl.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,8 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
291291
return true;
292292
}
293293

294+
id<MTLCommandBuffer> ContextMTL::CreateMTLCommandBuffer() const {
295+
return [command_queue_ commandBuffer];
296+
}
297+
294298
} // namespace impeller

impeller/renderer/backend/metal/surface_mtl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ class SurfaceMTL final : public Surface {
4343
id<MTLDrawable> drawable() const { return drawable_; }
4444

4545
private:
46+
std::weak_ptr<Context> context_;
4647
id<MTLDrawable> drawable_ = nil;
4748

48-
SurfaceMTL(const RenderTarget& target, id<MTLDrawable> drawable);
49+
SurfaceMTL(const std::weak_ptr<Context>& context,
50+
const RenderTarget& target,
51+
id<MTLDrawable> drawable);
4952

5053
// |Surface|
5154
bool Present() const override;

impeller/renderer/backend/metal/surface_mtl.mm

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "flutter/fml/trace_event.h"
88
#include "impeller/base/validation.h"
9+
#include "impeller/renderer/backend/metal/context_mtl.h"
910
#include "impeller/renderer/backend/metal/formats_mtl.h"
1011
#include "impeller/renderer/backend/metal/texture_mtl.h"
1112
#include "impeller/renderer/render_target.h"
@@ -111,12 +112,14 @@
111112
render_target_desc.SetStencilAttachment(stencil0);
112113

113114
// The constructor is private. So make_unique may not be used.
114-
return std::unique_ptr<SurfaceMTL>(
115-
new SurfaceMTL(render_target_desc, current_drawable));
115+
return std::unique_ptr<SurfaceMTL>(new SurfaceMTL(
116+
context->weak_from_this(), render_target_desc, current_drawable));
116117
}
117118

118-
SurfaceMTL::SurfaceMTL(const RenderTarget& target, id<MTLDrawable> drawable)
119-
: Surface(target), drawable_(drawable) {}
119+
SurfaceMTL::SurfaceMTL(const std::weak_ptr<Context>& context,
120+
const RenderTarget& target,
121+
id<MTLDrawable> drawable)
122+
: Surface(target), context_(context), drawable_(drawable) {}
120123

121124
// |Surface|
122125
SurfaceMTL::~SurfaceMTL() = default;
@@ -127,7 +130,16 @@
127130
return false;
128131
}
129132

130-
[drawable_ present];
133+
auto context = context_.lock();
134+
if (!context) {
135+
return false;
136+
}
137+
138+
id<MTLCommandBuffer> command_buffer =
139+
ContextMTL::Cast(context.get())->CreateMTLCommandBuffer();
140+
[command_buffer presentDrawable:drawable_];
141+
[command_buffer commit];
142+
131143
return true;
132144
}
133145
#pragma GCC diagnostic pop

0 commit comments

Comments
 (0)