[iOS] Ensure FlutterMetalLayer has correct backpressure. (flutter/engine#50486)

See also https://github.com/flutter/flutter/issues/140901 . We were not accounting for GPU backpressure in the FML, this applies the patch from @knopp to track this.
This commit is contained in:
Jonah Williams
2024-02-15 08:28:33 -08:00
committed by GitHub
parent e1136b1caf
commit dac5a4b792
5 changed files with 102 additions and 19 deletions

View File

@@ -13,6 +13,10 @@
#include "impeller/renderer/backend/metal/texture_mtl.h"
#include "impeller/renderer/render_target.h"
@protocol FlutterMetalDrawable <MTLDrawable>
- (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
@end
namespace impeller {
#pragma GCC diagnostic push
@@ -254,6 +258,14 @@ bool SurfaceMTL::Present() const {
id<MTLCommandBuffer> command_buffer =
ContextMTL::Cast(context.get())
->CreateMTLCommandBuffer("Present Waiter Command Buffer");
id<CAMetalDrawable> metal_drawable =
reinterpret_cast<id<CAMetalDrawable>>(drawable_);
if ([metal_drawable conformsToProtocol:@protocol(FlutterMetalDrawable)]) {
[(id<FlutterMetalDrawable>)metal_drawable
flutterPrepareForPresent:command_buffer];
}
// If the threads have been merged, or there is a pending frame capture,
// then block on cmd buffer scheduling to ensure that the
// transaction/capture work correctly.

View File

@@ -28,4 +28,16 @@
@end
@protocol MTLCommandBuffer;
@protocol FlutterMetalDrawable <CAMetalDrawable>
/// In order for FlutterMetalLayer to provide back pressure it must have access
/// to the command buffer that is used to render into the drawable to schedule
/// a completion handler.
/// This method must be called before the command buffer is committed.
- (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
@end
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERMETALLAYER_H_

View File

@@ -60,6 +60,7 @@ extern CFTimeInterval display_link_target;
@property(readonly, nonatomic) id<MTLTexture> texture;
@property(readonly, nonatomic) IOSurface* surface;
@property(readwrite, nonatomic) CFTimeInterval presentedTime;
@property(readwrite, atomic) BOOL waitingForCompletion;
@end
@@ -68,6 +69,7 @@ extern CFTimeInterval display_link_target;
@synthesize texture = _texture;
@synthesize surface = _surface;
@synthesize presentedTime = _presentedTime;
@synthesize waitingForCompletion;
- (instancetype)initWithTexture:(id<MTLTexture>)texture surface:(IOSurface*)surface {
if (self = [super init]) {
@@ -79,7 +81,7 @@ extern CFTimeInterval display_link_target;
@end
@interface FlutterDrawable : NSObject <CAMetalDrawable> {
@interface FlutterDrawable : NSObject <FlutterMetalDrawable> {
FlutterTexture* _texture;
__weak FlutterMetalLayer* _layer;
NSUInteger _drawableId;
@@ -147,6 +149,14 @@ extern CFTimeInterval display_link_target;
FML_LOG(WARNING) << "FlutterMetalLayer drawable does not implement presentAfterMinimumDuration:";
}
- (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer {
FlutterTexture* texture = _texture;
texture.waitingForCompletion = YES;
[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
texture.waitingForCompletion = NO;
}];
}
@end
@implementation FlutterMetalLayer
@@ -283,7 +293,25 @@ extern CFTimeInterval display_link_target;
}
- (FlutterTexture*)nextTexture {
CFTimeInterval start = CACurrentMediaTime();
while (true) {
FlutterTexture* texture = [self tryNextTexture];
if (texture != nil) {
return texture;
}
CFTimeInterval elapsed = CACurrentMediaTime() - start;
if (elapsed > 1.0) {
NSLog(@"Waited %f seconds for a drawable, giving up.", elapsed);
return nil;
}
}
}
- (FlutterTexture*)tryNextTexture {
@synchronized(self) {
if (_front != nil && _front.waitingForCompletion) {
return nil;
}
if (_totalTextures < 3) {
++_totalTextures;
IOSurface* surface = [self createIOSurface];
@@ -309,21 +337,6 @@ extern CFTimeInterval display_link_target;
surface:surface];
return flutterTexture;
} else {
// Make sure raster thread doesn't have too many drawables in flight.
if (_availableTextures.count == 0) {
CFTimeInterval start = CACurrentMediaTime();
while (_availableTextures.count == 0 && CACurrentMediaTime() - start < 1.0) {
usleep(100);
}
CFTimeInterval elapsed = CACurrentMediaTime() - start;
if (_availableTextures.count == 0) {
NSLog(@"Waited %f seconds for a drawable, giving up.", elapsed);
return nil;
} else {
NSLog(@"Had to wait %f seconds for a drawable", elapsed);
}
}
// Prefer surface that is not in use and has been presented the longest
// time ago.
// When isInUse is false, the surface is definitely not used by the compositor.
@@ -345,7 +358,9 @@ extern CFTimeInterval display_link_target;
res = texture;
}
}
[_availableTextures removeObject:res];
if (res != nil) {
[_availableTextures removeObject:res];
}
return res;
}
}
@@ -370,7 +385,6 @@ extern CFTimeInterval display_link_target;
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.contents = texture.surface;
texture.presentedTime = CACurrentMediaTime();
[CATransaction commit];
_displayLink.paused = NO;
_displayLinkPauseCountdown = 0;
@@ -388,6 +402,7 @@ extern CFTimeInterval display_link_target;
[_availableTextures addObject:_front];
}
_front = texture;
texture.presentedTime = CACurrentMediaTime();
if ([NSThread isMainThread]) {
[self presentOnMainThread:texture];
} else {

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#import <Metal/Metal.h>
#import <OCMock/OCMock.h>
#import <QuartzCore/QuartzCore.h>
#import <XCTest/XCTest.h>
@@ -236,4 +237,37 @@
[self removeMetalLayer:layer];
}
- (void)testTimeout {
FlutterMetalLayer* layer = [self addMetalLayer];
TestCompositor* compositor = [[TestCompositor alloc] initWithLayer:layer];
id<CAMetalDrawable> drawable = [layer nextDrawable];
BAIL_IF_NO_DRAWABLE(drawable);
__block MTLCommandBufferHandler handler;
id<MTLCommandBuffer> mockCommandBuffer = OCMProtocolMock(@protocol(MTLCommandBuffer));
OCMStub([mockCommandBuffer addCompletedHandler:OCMOCK_ANY]).andDo(^(NSInvocation* invocation) {
MTLCommandBufferHandler handlerOnStack;
[invocation getArgument:&handlerOnStack atIndex:2];
// Required to copy stack block to heap.
handler = handlerOnStack;
});
[(id<FlutterMetalDrawable>)drawable flutterPrepareForPresent:mockCommandBuffer];
[drawable present];
[compositor commitTransaction];
// Drawable will not be available until the command buffer completes.
drawable = [layer nextDrawable];
XCTAssertNil(drawable);
handler(mockCommandBuffer);
drawable = [layer nextDrawable];
XCTAssertNotNil(drawable);
[self removeMetalLayer:layer];
}
@end

View File

@@ -8,6 +8,10 @@
#include "flutter/shell/gpu/gpu_surface_metal_skia.h"
#include "flutter/shell/platform/darwin/ios/ios_context_metal_skia.h"
@protocol FlutterMetalDrawable <MTLDrawable>
- (void)flutterPrepareForPresent:(nonnull id<MTLCommandBuffer>)commandBuffer;
@end
namespace flutter {
static IOSContextMetalSkia* CastToMetalContext(const std::shared_ptr<IOSContext>& context) {
@@ -80,10 +84,16 @@ bool IOSSurfaceMetalSkia::PresentDrawable(GrMTLHandle drawable) const {
auto command_buffer =
fml::scoped_nsprotocol<id<MTLCommandBuffer>>([[command_queue_ commandBuffer] retain]);
id<CAMetalDrawable> metal_drawable = reinterpret_cast<id<CAMetalDrawable>>(drawable);
if ([metal_drawable conformsToProtocol:@protocol(FlutterMetalDrawable)]) {
[(id<FlutterMetalDrawable>)metal_drawable flutterPrepareForPresent:command_buffer.get()];
}
[command_buffer.get() commit];
[command_buffer.get() waitUntilScheduled];
[reinterpret_cast<id<CAMetalDrawable>>(drawable) present];
[metal_drawable present];
return true;
}