2019-09-09 21:03:38 +00:00
|
|
|
//Copyright 2019 Google LLC.
|
|
|
|
//Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2019-12-11 15:32:59 +00:00
|
|
|
#include "tools/skottie_ios_app/SkottieMtkView.h"
|
2019-09-09 21:03:38 +00:00
|
|
|
|
2019-12-11 15:32:59 +00:00
|
|
|
#include "tools/skottie_ios_app/SkAnimationDraw.h"
|
|
|
|
#include "tools/skottie_ios_app/SkTimeKeeper.h"
|
2019-12-06 20:01:08 +00:00
|
|
|
|
2019-09-09 21:03:38 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
|
|
|
|
#include "modules/skottie/include/Skottie.h"
|
|
|
|
|
2019-12-11 15:32:59 +00:00
|
|
|
#include "tools/skottie_ios_app/SkMetalViewBridge.h"
|
2019-09-09 21:03:38 +00:00
|
|
|
|
|
|
|
@implementation SkottieMtkView {
|
2019-12-06 20:01:08 +00:00
|
|
|
SkAnimationDraw fDraw;
|
|
|
|
SkTimeKeeper fClock;
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawRect:(CGRect)rect {
|
|
|
|
[super drawRect:rect];
|
|
|
|
// TODO(halcanary): Use the rect and the InvalidationController to speed up rendering.
|
2019-12-06 20:01:08 +00:00
|
|
|
if (!fDraw || ![[self currentDrawable] texture] || ![self grContext]) {
|
2019-09-09 21:03:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CGSize size = [self drawableSize];
|
2019-12-06 20:01:08 +00:00
|
|
|
if (!fClock.paused()) {
|
|
|
|
fDraw.seek(fClock.currentTime());
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
sk_sp<SkSurface> surface = SkMtkViewToSurface(self, [self grContext]);
|
|
|
|
if (!surface) {
|
|
|
|
NSLog(@"error: no sksurface");
|
|
|
|
return;
|
|
|
|
}
|
2019-12-06 20:01:08 +00:00
|
|
|
fDraw.draw(SkSize{(float)size.width, (float)size.height}, surface->getCanvas());
|
2019-09-09 21:03:38 +00:00
|
|
|
surface->flush();
|
|
|
|
surface = nullptr;
|
2019-10-14 17:42:57 +00:00
|
|
|
|
|
|
|
id<MTLCommandBuffer> commandBuffer = [[self queue] commandBuffer];
|
|
|
|
[commandBuffer presentDrawable:[self currentDrawable]];
|
|
|
|
[commandBuffer commit];
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)loadAnimation:(NSData*) data {
|
2019-12-06 20:01:08 +00:00
|
|
|
fDraw.load((const void*)[data bytes], (size_t)[data length]);
|
|
|
|
fClock.setDuration(fDraw.duration());
|
|
|
|
return (BOOL)fDraw;
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 20:01:08 +00:00
|
|
|
- (void)setStopAtEnd:stop{ fClock.setStopAtEnd(stop); }
|
2019-09-26 18:29:51 +00:00
|
|
|
|
2019-12-06 20:01:08 +00:00
|
|
|
- (float)animationDurationSeconds { return fClock.duration(); }
|
|
|
|
|
|
|
|
- (float)currentTime { return fDraw ? fClock.currentTime() : 0; }
|
2019-09-26 18:29:51 +00:00
|
|
|
|
|
|
|
- (void)seek:(float)seconds {
|
2019-12-06 20:01:08 +00:00
|
|
|
if (fDraw) {
|
|
|
|
fClock.seek(seconds);
|
|
|
|
[self setNeedsDisplay];
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:01:08 +00:00
|
|
|
- (CGSize)size { return {(CGFloat)fDraw.size().width(), (CGFloat)fDraw.size().height()}; }
|
2019-09-26 18:29:51 +00:00
|
|
|
|
2019-09-09 21:03:38 +00:00
|
|
|
- (BOOL)togglePaused {
|
2019-12-06 20:01:08 +00:00
|
|
|
fClock.togglePaused();
|
|
|
|
[self setNeedsDisplay];
|
|
|
|
return fClock.paused();
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
2019-09-30 13:12:54 +00:00
|
|
|
|
2019-12-06 20:01:08 +00:00
|
|
|
- (BOOL)isPaused { return fClock.paused(); }
|
2019-09-09 21:03:38 +00:00
|
|
|
@end
|