I’ve compiled Skia for iOS:
bin/gn gen out/ios-apple --args="target_os="ios" target_cpu="arm64" ios_use_simulator=false skia_enable_tools=false is_official_build=false is_debug=true is_trivial_abi=false skia_use_metal=true skia_use_expat=false skia_use_system_expat=false skia_use_system_libpng=false skia_use_system_libwebp=false skia_use_system_zlib=false skia_use_system_freetype2=false skia_use_system_harfbuzz=false skia_use_system_icu=false skia_enable_gpu=true skia_enable_skottie=false skia_compile_modules=true extra_cflags=["-F/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks"] extra_ldflags=["-framework", "CoreFoundation", "-framework", "Metal"]"
That is how I am utilizing it in a View Controller:
#import
#import
#import
#outline SK_GANESH
#outline SK_METAL
#import
#import
@interface SkiaViewController : UIViewController
@property (nonatomic, sturdy) MTKView *mtkView;
@property (nonatomic, sturdy) id metalDevice;
@property (nonatomic, sturdy) id metalQueue;
@property (nonatomic, assign) sk_sp grDirectContext;
@property (nonatomic, assign) sk_sp floor;
@finish
#import
#import
#outline SK_GANESH
#outline SK_METAL
#import "embody/gpu/ganesh/GrTypes.h"
#import "embody/gpu/ganesh/SkSurfaceGanesh.h"
#import "embody/gpu/ganesh/mtl/GrMtlTypes.h"
#import
#import
#import
#import
#import
#import
#import
#import
#import
#import
#import "SkiaViewController.h"
@implementation SkiaViewController
- (void)viewDidLoad {
[super viewDidLoad];
#ifndef SK_GANESH
NSLog(@"SK_GANESH is undefined");
#endif
#ifndef SK_METAL
NSLog(@"SK_METAL is undefined");
#endif
[self setMetalDevice:MTLCreateSystemDefaultDevice()];
[self setMetalQueue:[[self metalDevice] newCommandQueue]];
if(![self metalDevice]) {
NSLog(@"Metallic will not be supported on this machine");
return;
}
if(![self metalQueue]) {
NSLog(@"Didn't create Metallic command queue");
return;
}
// Initialize MTKView
self.mtkView = [[MTKView alloc] initWithFrame:self.view.bounds machine:self.metalDevice];
self.mtkView.delegate = self;
self.mtkView.enableSetNeedsDisplay = YES;
[self.mtkView setDepthStencilPixelFormat:MTLPixelFormatDepth32Float_Stencil8];
[self.mtkView setColorPixelFormat:MTLPixelFormatBGRA8Unorm];
[self.mtkView setSampleCount:1];
NSLog(@"self.metalDevice.maxBufferLength=%lu", self.metalDevice.maxBufferLength);
self.mtkView.layer.borderWidth = 2.0; // Set border width
self.mtkView.layer.borderColor = [UIColor redColor].CGColor; // Set border colour
[self.view addSubview:self.mtkView];
// Initialize Skia with Metallic
GrMtlBackendContext backendContext = {};
backendContext.fDevice.retain((__bridge void*)[self metalDevice]); // additionally tried __bridge_retained
backendContext.fQueue.retain((__bridge void*)[self metalQueue]);
GrContextOptions grContextOptions;
self.grDirectContext = GrDirectContexts::MakeMetal(backendContext, grContextOptions);
if (![self grDirectContext]) {
NSLog(@"Didn't create GrDirectContext");
return;
}
NSLog(@"Created GrDirectContext");
}
#pragma mark - MTKViewDelegate
- (void)drawInMTKView:(nonnull MTKView *)view {
}
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)measurement {
}
@finish
On this case it really works effective.
But when I create a static library inside the identical undertaking, transfer ViewController into that static library and use view controller from the static library in my app, I am going through reminiscence points in GrDirectContexts::MakeMetal
.
Typically it crashes with unhealthy entry error, typically a few of pointers have gotten nullptr
, for instance in backendContext
proper after setting these values.
Similar if I transfer this Skia initialization in a separate C++ library (compiled by CMake, not in Xcode instantly).
I suppose that that is some situation with Skia’s sensible pointers (possibly one thing is stripped when linking with static lib?), however I am not conversant in Obj-C improvement and interop with C++ for iOS.
What could also be improper right here?