skia2/tools/skottie_ios_app/SkiaMetalContext.mm
Greg Daniel 49de1031d4 Reland "Remove deprecated flush calls."
This reverts commit 5e6d789ce4.

Reason for revert: Relanding with fix landed in chrome

Original change's description:
> Revert "Remove deprecated flush calls."
> 
> This reverts commit d8fd0bf574.
> 
> Reason for revert: chrome roll
> 
> Original change's description:
> > Remove deprecated flush calls.
> > 
> > Bug: skia:10118
> > Change-Id: I13758b5416784c296b8b5be9f3228230ac1be05f
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290540
> > Commit-Queue: Greg Daniel <egdaniel@google.com>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
> 
> TBR=egdaniel@google.com,bsalomon@google.com
> 
> Change-Id: I49d35cdb258e632f645974c5ec62075d3392efe0
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:10118
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290834
> Reviewed-by: Greg Daniel <egdaniel@google.com>
> Commit-Queue: Greg Daniel <egdaniel@google.com>

TBR=egdaniel@google.com,bsalomon@google.com

# Not skipping CQ checks because this is a reland.

Bug: skia:10118
Change-Id: Ie24d7845a4ad75f95afe59037ea80a9f38082f13
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/290918
Reviewed-by: Greg Daniel <egdaniel@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
2020-05-20 15:48:46 +00:00

118 lines
3.9 KiB
Plaintext

// Copyright 2020 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/skottie_ios_app/SkiaContext.h"
#include "include/core/SkSurface.h"
#include "include/gpu/GrContext.h"
#include "tools/skottie_ios_app/SkMetalViewBridge.h"
#import <Metal/Metal.h>
#import <MetalKit/MetalKit.h>
#import <UIKit/UIKit.h>
// A UIView that uses a Metal-backed SkSurface to draw.
@interface SkiaMtkView : MTKView
@property (strong) SkiaViewController* controller;
// Override of the MTKView interface. Uses Skia+Metal to draw.
- (void)drawRect:(CGRect)rect;
// Required initializer.
- (instancetype)initWithFrame:(CGRect)frameRect
device:(id<MTLDevice>)device
queue:(id<MTLCommandQueue>)queue
grDevice:(GrContext*)grContext;
@end
@implementation SkiaMtkView {
id<MTLCommandQueue> fQueue;
GrContext* fGrContext;
}
- (instancetype)initWithFrame:(CGRect)frameRect
device:(id<MTLDevice>)mtlDevice
queue:(id<MTLCommandQueue>)queue
grDevice:(GrContext*)grContext {
self = [super initWithFrame:frameRect device:mtlDevice];
fQueue = queue;
fGrContext = grContext;
SkMtkViewConfigForSkia(self);
return self;
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
// TODO(halcanary): Use the rect and the InvalidationController to speed up rendering.
SkiaViewController* viewController = [self controller];
if (!viewController || ![[self currentDrawable] texture] || !fGrContext) {
return;
}
CGSize size = [self drawableSize];
sk_sp<SkSurface> surface = SkMtkViewToSurface(self, fGrContext);
if (!surface) {
NSLog(@"error: no sksurface");
return;
}
[viewController draw:rect toCanvas:surface->getCanvas() atSize:size];
surface->flushAndSubmit();
surface = nullptr;
id<MTLCommandBuffer> commandBuffer = [fQueue commandBuffer];
[commandBuffer presentDrawable:[self currentDrawable]];
[commandBuffer commit];
bool paused = [viewController isPaused];
[self setEnableSetNeedsDisplay:paused];
[self setPaused:paused];
}
@end
@interface SkiaMetalContext : SkiaContext
@property (strong) id<MTLDevice> metalDevice;
@property (strong) id<MTLCommandQueue> metalQueue;
- (instancetype) init;
- (UIView*) makeViewWithController:(SkiaViewController*)vc withFrame:(CGRect)frame;
- (SkiaViewController*) getViewController:(UIView*)view;
@end
@implementation SkiaMetalContext {
sk_sp<GrContext> fGrContext;
}
- (instancetype) init {
self = [super init];
[self setMetalDevice:MTLCreateSystemDefaultDevice()];
if(![self metalDevice]) {
NSLog(@"Metal is not supported on this device");
return nil;
}
[self setMetalQueue:[[self metalDevice] newCommandQueue]];
fGrContext = GrContext::MakeMetal((__bridge void*)[self metalDevice],
(__bridge void*)[self metalQueue],
GrContextOptions());
if (!fGrContext) {
NSLog(@"GrContext::MakeMetal failed");
return nil;
}
return self;
}
- (UIView*) makeViewWithController:(SkiaViewController*)vc withFrame:(CGRect)frame {
SkiaMtkView* skiaView = [[SkiaMtkView alloc] initWithFrame:frame
device:[self metalDevice]
queue:[self metalQueue]
grDevice:fGrContext.get()];
[skiaView setPreferredFramesPerSecond:30];
[skiaView setController:vc];
return skiaView;
}
- (SkiaViewController*) getViewController:(UIView*)view {
return [view isKindOfClass:[SkiaMtkView class]] ? [(SkiaMtkView*)view controller] : nil;
}
@end
SkiaContext* MakeSkiaMetalContext() { return [[SkiaMetalContext alloc] init]; }