skia2/experimental/skottie_ios/SkottieUIView.mm
Hal Canary 2748306830 experimental/skottie_ios/SkottieUiView
skottie_ios builds either with or without Metal.  The SkAnimationDraw
and SkTimeKeeper classes help abstract out the common elements of the
UIView and the MTKView implementations.

Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm-Debug-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm-Release-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Debug-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Debug-iOS_Metal
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Release-iOS
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-arm64-Release-iOS_Metal
Cq-Include-Trybots: skia/skia.primary:Build-Mac-Clang-x64-Release-iOS
Change-Id: I2c5d217b6349188f32a81e013eb29c6254428831
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/258636
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
2019-12-11 23:02:00 +00:00

83 lines
2.5 KiB
Plaintext

// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "experimental/skottie_ios/SkottieUIView.h"
#include "experimental/skottie_ios/SkAnimationDraw.h"
#include "experimental/skottie_ios/SkTimeKeeper.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkPaint.h"
#include "include/core/SkSurface.h"
#include "include/core/SkTime.h"
#include "include/utils/mac/SkCGUtils.h"
#include "modules/skottie/include/Skottie.h"
@implementation SkottieUIView {
SkAnimationDraw fDraw;
SkTimeKeeper fClock;
SkBitmap fBackBuffer;
}
- (void)drawRect:(CGRect)rect {
double next = fClock.paused() ? 0 : (1.0 / 30.0) + SkTime::GetNSecs() * 1e-9;
[super drawRect:rect];
// TODO(halcanary): Use the rect and the InvalidationController to speed up rendering.
if (rect.size.width > 0 && rect.size.height > 0 && fDraw) {
CGSize size = [self bounds].size;
SkISize iSize = {(int)size.width, (int)size.height};
if (fBackBuffer.drawsNothing() || iSize != fBackBuffer.dimensions()) {
fBackBuffer.allocN32Pixels(iSize.fWidth, iSize.fHeight);
}
fBackBuffer.eraseColor(SK_ColorTRANSPARENT);
{
SkCanvas canvas(fBackBuffer);
if (!fClock.paused()) {
fDraw.seek(fClock.currentTime());
}
fDraw.draw({(float)size.width, (float)size.height}, &canvas);
}
SkCGDrawBitmap(UIGraphicsGetCurrentContext(), fBackBuffer, 0, 0);
}
if (next) {
[NSTimer scheduledTimerWithTimeInterval:std::max(0.0, next - SkTime::GetNSecs() * 1e-9)
target:self
selector:@selector(setNeedsDisplay)
userInfo:nil
repeats:NO];
}
}
- (BOOL)loadAnimation:(NSData*) data {
fDraw.load((const void*)[data bytes], (size_t)[data length]);
fClock.setDuration(fDraw.duration());
[self setNeedsDisplay];
return (bool)fDraw;
}
- (void)setStopAtEnd:stop{ fClock.setStopAtEnd(stop); }
- (float)animationDurationSeconds { return fClock.duration(); }
- (float)currentTime { return fDraw ? fClock.currentTime() : 0; }
- (void)seek:(float)seconds {
if (fDraw) {
fClock.seek(seconds);
[self setNeedsDisplay];
}
}
- (CGSize)size { return {(CGFloat)fDraw.size().width(), (CGFloat)fDraw.size().height()}; }
- (BOOL)togglePaused {
fClock.togglePaused();
[self setNeedsDisplay];
return fClock.paused();
}
- (BOOL)isPaused { return fClock.paused(); }
@end