skia2/tools/sk_app/ios/main_ios.mm
Adlai Holler da1c4d2a29 Spruce up iOS viewer and cut CPU drain
Main outcome here is that we only draw once per frame instead
of pinning the CPU. We also cut out a little extra traffic – an
autorelease pool per draw, but trivial. We also fix an overrelease
on NSProcessInfo.arguments, and a missing -init for the UIViewController
etc.

Change-Id: Ibb5ea5832bc65d2ff60351d4e3b76fd3b4ff88ee
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/337116
Commit-Queue: Adlai Holler <adlai@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
2020-11-20 20:32:17 +00:00

93 lines
2.5 KiB
Plaintext

/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkTypes.h"
#include "tools/sk_app/Application.h"
#include "tools/sk_app/ios/Window_ios.h"
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#if __has_feature(objc_arc)
#error "File should not be compiled with ARC."
#endif
namespace {
static int gArgc;
static char** gArgv;
}
////////////////////////////////////////////////////////////////////
@interface AppDelegate : UIResponder<UIApplicationDelegate>
@end
@implementation AppDelegate {
CADisplayLink* fDisplayLink; // Owned by the run loop.
std::unique_ptr<sk_app::Application> fApp;
}
#pragma mark - UIApplicationDelegate
- (void)applicationWillResignActive:(UIApplication *)sender {
sk_app::Window_ios* mainWindow = sk_app::Window_ios::MainWindow();
if (mainWindow) {
mainWindow->onActivate(false);
}
}
- (void)applicationDidBecomeActive:(UIApplication *)sender {
sk_app::Window_ios* mainWindow = sk_app::Window_ios::MainWindow();
if (mainWindow) {
mainWindow->onActivate(true);
}
}
- (void)applicationWillTerminate:(UIApplication *)sender {
// Display link retains us, so we break the cycle now.
// Note: dealloc is never called.
[fDisplayLink invalidate];
fDisplayLink = nil;
fApp.reset();
}
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
fApp = std::unique_ptr<sk_app::Application>(sk_app::Application::Create(gArgc, gArgv, nullptr));
auto mainWindow = sk_app::Window_ios::MainWindow();
mainWindow->onActivate(application.applicationState == UIApplicationStateActive);
fDisplayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(displayLinkFired)];
[fDisplayLink addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
return YES;
}
- (void)displayLinkFired {
// TODO: Hook into CAMetalLayer's drawing event loop or our own run loop observer.
// Need to handle animated slides/redraw mode, so we need something that will wake up the
// run loop.
sk_app::Window_ios::PaintWindow();
fApp->onIdle();
}
@end
///////////////////////////////////////////////////////////////////
int main(int argc, char **argv) {
gArgc = argc;
gArgv = argv;
return UIApplicationMain(argc, argv, nil, @"AppDelegate");
}