Skip to content

Commit c910602

Browse files
jvanverthSkia Commit-Bot
authored andcommitted
Reland "Minimal iOS app: Perform present subsequent to flush"
This is a reland of 4e385e2 Original change's description: > Minimal iOS app: Perform present subsequent to flush > > Currently the backbuffer present is not on the same MTLCommandQueue > as the flush from Skia, which probably means that it's happening > concurrently. This can overwhelm the GPU. This CL submits the present > to the same queue as the flush. > > Change-Id: Ibaf805553931c9dc46368b362a2391425ae3e60e > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/248557 > Reviewed-by: Hal Canary <halcanary@skia.org> > Commit-Queue: Jim Van Verth <jvanverth@google.com> Change-Id: I118047630b8936838d5da8ef4307ad5c5e8d2ade Reviewed-on: https://skia-review.googlesource.com/c/skia/+/248562 Reviewed-by: Hal Canary <halcanary@skia.org> Commit-Queue: Jim Van Verth <jvanverth@google.com>
1 parent 63acfe3 commit c910602

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

experimental/minimal_ios_mtl_skia_app/main.mm

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static void draw_example(SkSurface* surface, const SkPaint& paint, double rotati
4444

4545
@interface AppViewDelegate : NSObject <MTKViewDelegate>
4646
@property (assign, nonatomic) GrContext* grContext; // non-owning pointer.
47+
@property (assign, nonatomic) id<MTLCommandQueue> metalQueue;
4748
@end
4849

4950
@implementation AppViewDelegate {
@@ -70,7 +71,10 @@ - (void)drawInMTKView:(nonnull MTKView *)view {
7071
// Must flush *and* present for this to work!
7172
surface->flush();
7273
surface = nullptr;
73-
[[view currentDrawable] present];
74+
75+
id<MTLCommandBuffer> commandBuffer = [[self metalQueue] commandBuffer];
76+
[commandBuffer presentDrawable:[view currentDrawable]];
77+
[commandBuffer commit];
7478
}
7579

7680
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size {
@@ -82,6 +86,7 @@ - (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size {
8286

8387
@interface AppViewController : UIViewController
8488
@property (strong, nonatomic) id<MTLDevice> metalDevice;
89+
@property (strong, nonatomic) id<MTLCommandQueue> metalQueue;
8590
@end
8691

8792
@implementation AppViewController {
@@ -96,8 +101,9 @@ - (void)viewDidLoad {
96101
[super viewDidLoad];
97102
if (!fGrContext) {
98103
[self setMetalDevice:MTLCreateSystemDefaultDevice()];
104+
[self setMetalQueue:[[self metalDevice] newCommandQueue]];
99105
GrContextOptions grContextOptions; // set different options here.
100-
fGrContext = SkMetalDeviceToGrContext([self metalDevice], grContextOptions);
106+
fGrContext = SkMetalDeviceToGrContext([self metalDevice], [self metalQueue], grContextOptions);
101107
}
102108
if (![self view] || ![self metalDevice]) {
103109
NSLog(@"Metal is not supported on this device");
@@ -110,6 +116,7 @@ - (void)viewDidLoad {
110116
SkMtkViewConfigForSkia(mtkView);
111117
AppViewDelegate* viewDelegate = [[AppViewDelegate alloc] init];
112118
[viewDelegate setGrContext:fGrContext.get()];
119+
[viewDelegate setMetalQueue:[self metalQueue]];
113120
[viewDelegate mtkView:mtkView drawableSizeWillChange:[mtkView bounds].size];
114121
[mtkView setDelegate:viewDelegate];
115122
}

experimental/skottie_ios/SkMetalViewBridge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ template <typename T> class sk_sp;
1212

1313
sk_sp<SkSurface> SkMtkViewToSurface(MTKView*, GrContext*);
1414

15-
sk_sp<GrContext> SkMetalDeviceToGrContext(id<MTLDevice>, const GrContextOptions&);
15+
sk_sp<GrContext> SkMetalDeviceToGrContext(id<MTLDevice>, id<MTLCommandQueue>, const GrContextOptions&);
1616

1717
void SkMtkViewConfigForSkia(MTKView*);
1818

experimental/skottie_ios/SkMetalViewBridge.mm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
}
4242
}
4343

44-
sk_sp<GrContext> SkMetalDeviceToGrContext(id<MTLDevice> device, const GrContextOptions& opts) {
45-
return GrContext::MakeMetal((void*)device,
46-
(void*)[device newCommandQueue], opts);
44+
sk_sp<GrContext> SkMetalDeviceToGrContext(id<MTLDevice> device, id<MTLCommandQueue> queue, const GrContextOptions& opts) {
45+
return GrContext::MakeMetal((void*)device, (void*)queue, opts);
4746
}
4847

4948
void SkMtkViewConfigForSkia(MTKView* mtkView) {

experimental/skottie_ios/SkottieMtkView.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class GrContext;
1414
// e.g.: use SkMetalDeviceToGrContext().
1515
@property (assign) GrContext* grContext; // non-owning pointer.
1616

17+
// Must be set to a valid MTLCommandQueue. Will be used to present.
18+
@property (assign) id<MTLCommandQueue> queue; // non-owning pointer.
19+
1720
// When set, pauses at end of loop.
1821
@property (assign) BOOL stopAtEnd;
1922

experimental/skottie_ios/SkottieMtkView.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ - (void)drawRect:(CGRect)rect {
6363
fAnimation->render(canvas);
6464
surface->flush();
6565
surface = nullptr;
66-
[[self currentDrawable] present];
66+
67+
id<MTLCommandBuffer> commandBuffer = [[self queue] commandBuffer];
68+
[commandBuffer presentDrawable:[self currentDrawable]];
69+
[commandBuffer commit];
6770
}
6871

6972
- (BOOL)loadAnimation:(NSData*) data {

experimental/skottie_ios/main.mm

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
static UIStackView* make_skottie_stack(CGFloat width,
1515
id<MTLDevice> metalDevice,
16+
id<MTLCommandQueue> metalQueue,
1617
GrContext* grContext) {
1718
UIStackView* stack = [[UIStackView alloc] init];
1819
[stack setAxis:UILayoutConstraintAxisVertical];
@@ -35,6 +36,7 @@
3536
continue;
3637
}
3738
[skottieView setDevice:metalDevice];
39+
[skottieView setQueue:metalQueue];
3840
[skottieView setGrContext:grContext];
3941
SkMtkViewConfigForSkia(skottieView);
4042
CGSize animSize = [skottieView size];
@@ -52,6 +54,7 @@
5254

5355
@interface AppViewController : UIViewController
5456
@property (strong) id<MTLDevice> metalDevice;
57+
@property (strong) id<MTLCommandQueue> metalQueue;
5558
@property (strong) UIStackView* stackView;
5659
@end
5760

@@ -76,12 +79,14 @@ - (void)viewDidLoad {
7679
NSLog(@"Metal is not supported on this device");
7780
return;
7881
}
82+
[self setMetalQueue:[[self metalDevice] newCommandQueue]];
7983
GrContextOptions grContextOptions; // set different options here.
80-
fGrContext = SkMetalDeviceToGrContext([self metalDevice], grContextOptions);
84+
fGrContext = SkMetalDeviceToGrContext([self metalDevice], [self metalQueue],
85+
grContextOptions);
8186
}
8287

8388
[self setStackView:make_skottie_stack([[UIScreen mainScreen] bounds].size.width,
84-
[self metalDevice], fGrContext.get())];
89+
[self metalDevice], [self metalQueue], fGrContext.get())];
8590

8691
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
8792
CGSize mainScreenSize = [[UIScreen mainScreen] bounds].size;

0 commit comments

Comments
 (0)