| 1 |
// Copyright 2019 Google LLC. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 |
|
| 4 |
// This is an example of a minimal iOS application that uses Skia to draw to |
| 5 |
// a Metal drawable. |
| 6 |
|
| 7 |
// Much of this code is copied from the default application created by XCode. |
| 8 |
|
| 9 |
#include "tools/skottie_ios_app/SkMetalViewBridge.h" |
| 10 |
|
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkSurface.h" |
| 16 |
#include "include/effects/SkGradient.h" |
| 17 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 18 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 19 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 20 |
#include "include/gpu/ganesh/mtl/GrMtlTypes.h" |
| 21 |
#include "src/base/SkTime.h" |
| 22 |
|
| 23 |
#import <Metal/Metal.h> |
| 24 |
#import <MetalKit/MetalKit.h> |
| 25 |
#import <UIKit/UIKit.h> |
| 26 |
|
| 27 |
//////////////////////////////////////////////////////////////////////////////// |
| 28 |
|
| 29 |
static void config_paint(SkPaint* paint) { |
| 30 |
if (!paint->getShader()) { |
| 31 |
const SkColor4f colors[2] = {SkColors::kBlack, SkColors::kWhite}; |
| 32 |
const SkPoint points[2] = {{0, -1024}, {0, 1024}}; |
| 33 |
paint->setShader(SkShaders::LinearGradient(points, {{colors, {}, SkTileMode::kClamp}, {}})); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
static void draw_example(SkSurface* surface, const SkPaint& paint, double rotation) { |
| 38 |
SkCanvas* canvas = surface->getCanvas(); |
| 39 |
canvas->translate(surface->width() * 0.5f, surface->height() * 0.5f); |
| 40 |
canvas->rotate(rotation); |
| 41 |
canvas->drawPaint(paint); |
| 42 |
} |
| 43 |
|
| 44 |
//////////////////////////////////////////////////////////////////////////////// |
| 45 |
|
| 46 |
@interface AppViewDelegate : NSObject <MTKViewDelegate> |
| 47 |
@property (assign, nonatomic) GrDirectContext* grContext; // non-owning pointer. |
| 48 |
@property (assign, nonatomic) id<MTLCommandQueue> metalQueue; |
| 49 |
@end |
| 50 |
|
| 51 |
@implementation AppViewDelegate { |
| 52 |
SkPaint fPaint; |
| 53 |
} |
| 54 |
|
| 55 |
- (void)drawInMTKView:(nonnull MTKView *)view { |
| 56 |
if (![self grContext] || !view) { |
| 57 |
return; |
| 58 |
} |
| 59 |
// Do as much as possible before creating surface. |
| 60 |
config_paint(&fPaint); |
| 61 |
float rotation = (float)(180 * 1e-9 * SkTime::GetNSecs()); |
| 62 |
|
| 63 |
// Create surface: |
| 64 |
sk_sp<SkSurface> surface = SkMtkViewToSurface(view, [self grContext]); |
| 65 |
if (!surface) { |
| 66 |
NSLog(@"error: no sksurface"); |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
draw_example(surface.get(), fPaint, rotation); |
| 71 |
|
| 72 |
// Must flush *and* present for this to work! |
| 73 |
skgpu::ganesh::Flush(surface); |
| 74 |
surface = nullptr; |
| 75 |
|
| 76 |
id<MTLCommandBuffer> commandBuffer = [[self metalQueue] commandBuffer]; |
| 77 |
[commandBuffer presentDrawable:[view currentDrawable]]; |
| 78 |
[commandBuffer commit]; |
| 79 |
} |
| 80 |
|
| 81 |
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size { |
| 82 |
// change anything on size change? |
| 83 |
} |
| 84 |
@end |
| 85 |
|
| 86 |
//////////////////////////////////////////////////////////////////////////////// |
| 87 |
|
| 88 |
@interface AppViewController : UIViewController |
| 89 |
@property (strong, nonatomic) id<MTLDevice> metalDevice; |
| 90 |
@property (strong, nonatomic) id<MTLCommandQueue> metalQueue; |
| 91 |
@end |
| 92 |
|
| 93 |
@implementation AppViewController { |
| 94 |
GrContextHolder fGrContext; |
| 95 |
} |
| 96 |
|
| 97 |
- (void)loadView { |
| 98 |
[self setView:[[MTKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] device:nil]]; |
| 99 |
} |
| 100 |
|
| 101 |
- (void)viewDidLoad { |
| 102 |
[super viewDidLoad]; |
| 103 |
if (!fGrContext) { |
| 104 |
[self setMetalDevice:MTLCreateSystemDefaultDevice()]; |
| 105 |
[self setMetalQueue:[[self metalDevice] newCommandQueue]]; |
| 106 |
fGrContext = SkMetalDeviceToGrContext([self metalDevice], [self metalQueue]); |
| 107 |
} |
| 108 |
if (![self view] || ![self metalDevice]) { |
| 109 |
NSLog(@"Metal is not supported on this device"); |
| 110 |
self.view = [[UIView alloc] initWithFrame:self.view.frame]; |
| 111 |
return; |
| 112 |
} |
| 113 |
MTKView* mtkView = (MTKView*)[self view]; |
| 114 |
[mtkView setDevice:[self metalDevice]]; |
| 115 |
[mtkView setBackgroundColor:[UIColor blackColor]]; |
| 116 |
SkMtkViewConfigForSkia(mtkView); |
| 117 |
AppViewDelegate* viewDelegate = [[AppViewDelegate alloc] init]; |
| 118 |
[viewDelegate setGrContext:fGrContext.get()]; |
| 119 |
[viewDelegate setMetalQueue:[self metalQueue]]; |
| 120 |
[viewDelegate mtkView:mtkView drawableSizeWillChange:[mtkView bounds].size]; |
| 121 |
[mtkView setDelegate:viewDelegate]; |
| 122 |
} |
| 123 |
@end |
| 124 |
|
| 125 |
//////////////////////////////////////////////////////////////////////////////// |
| 126 |
|
| 127 |
@interface AppDelegate : UIResponder <UIApplicationDelegate> |
| 128 |
@property (strong, nonatomic) UIWindow *window; |
| 129 |
@end |
| 130 |
|
| 131 |
@implementation AppDelegate |
| 132 |
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary*)opts { |
| 133 |
// Override point for customization after application launch. |
| 134 |
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]]; |
| 135 |
[[self window] setFrame:[[UIScreen mainScreen] bounds]]; |
| 136 |
[[self window] setRootViewController:[[AppViewController alloc] init]]; |
| 137 |
[[self window] makeKeyAndVisible]; |
| 138 |
return YES; |
| 139 |
} |
| 140 |
@end |
| 141 |
|
| 142 |
//////////////////////////////////////////////////////////////////////////////// |
| 143 |
|
| 144 |
int main(int argc, char* argv[]) { |
| 145 |
@autoreleasepool { |
| 146 |
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); |
| 147 |
} |
| 148 |
} |