Skia crashed on my mac using metal

242 views
Skip to first unread message

kekeyilv

unread,
Feb 24, 2023, 11:22:36 AM2/24/23
to skia-discuss
I am using MacOS 13.1(M1) , Skia 98. Here is my code
- (void)load {
self.metalView.device = MTLCreateSystemDefaultDevice();
if (!MPSSupportsMTLDevice(self.metalView.device)) {
return;
}
self.metalView.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
self.metalView.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
self.metalView.sampleCount = 1;
self.metalView.enableSetNeedsDisplay = false;
self.metalView.paused = false;
self.commandQueue = [self.metalView.device newCommandQueue];
nativeWindow->context = GrDirectContext::MakeMetal((__bridge void*)self.metalView.device, (__bridge void*)self.commandQueue, GrContextOptions());
}

....
sk_sp<SkSurface> surface = SkSurface::MakeFromMTKView(context, (__bridge GrMTLHandle)mtkView, origin, sampleCount, colorType, colorSpace, &surfaceProps);

It could be compiled successfully but an exception occurred while running "EXC_BAD_ACCESS (code=1, address=0x38)".The following is the stack trace:
GrProxyProvider::createLazyRenderTargetProxy(std::__1::function<GrSurfaceProxy::LazyCallbackResult (GrResourceProvider*, GrSurfaceProxy::LazySurfaceDesc const&)>&&, GrBackendFormat const&, SkISize, int, GrInternalSurfaceFlags, GrProxyProvider::TextureInfo const*, GrMipmapStatus, SkBackingFit, skgpu::Budgeted, GrProtected, bool, GrSurfaceProxy::UseAllocator) (@GrProxyProvider::createLazyRenderTargetProxy(std::__1::function<GrSurfaceProxy::LazyCallbackResult (GrResourceProvider*, GrSurfaceProxy::LazySurfaceDesc const&)>&&, GrBackendFormat const&, SkISize, int, GrInternalSurfaceFlags, GrProxyProvider::TextureInfo const*, GrMipmapStatus, SkBackingFit, skgpu::Budgeted, GrProtected, bool, GrSurfaceProxy::UseAllocator):31)
SkSurface::MakeFromMTKView(GrRecordingContext*, void const*, GrSurfaceOrigin, int, SkColorType, sk_sp<SkColorSpace>, SkSurfaceProps const*) (@SkSurface::MakeFromMTKView(GrRecordingContext*, void const*, GrSurfaceOrigin, int, SkColorType, sk_sp<SkColorSpace>, SkSurfaceProps const*):127)
...

So I wondered if there are any problems in my code.

黄世平

unread,
Nov 21, 2024, 8:24:47 AM11/21/24
to skia-discuss
I had the same problem. Did you solve yours

Jim Van Verth

unread,
Dec 4, 2024, 10:47:50 AM12/4/24
to skia-d...@googlegroups.com
I think you might need __bridge_retained instead of __bridge.

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/skia-discuss/8109a971-1dbe-4f8f-9078-9316e2727fdbn%40googlegroups.com.


--

Jim Van Verth |
 Software Engineer | Google.com

Name Surname

unread,
Jan 3, 2025, 9:56:43 AM1/3/25
to skia-discuss

Same issue here .I have this code:

#import "SkiaView.h"


#import <MetalKit/MetalKit.h>

#import <Metal/Metal.h>


#define SK_GANESH

#define SK_METAL


#import "include/gpu/ganesh/GrTypes.h"

#import "include/gpu/ganesh/SkSurfaceGanesh.h"

#import "include/gpu/ganesh/mtl/GrMtlTypes.h"


#import "include/gpu/ganesh/GrBackendSurface.h"

#import "include/gpu/ganesh/SkSurfaceGanesh.h"

#import "include/gpu/ganesh/mtl/GrMtlBackendContext.h"

#import "include/gpu/ganesh/mtl/GrMtlBackendSurface.h"

#import "include/gpu/ganesh/mtl/GrMtlDirectContext.h"

#import "include/gpu/ganesh/mtl/SkSurfaceMetal.h"


#import "include/gpu/ganesh/GrDirectContext.h"

#import "include/core/SkCanvas.h"

#import "include/core/SkColorSpace.h"


@implementation SkiaView


- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        [self commonInit];

    }

    return self;

}


- (instancetype)initWithCoder:(NSCoder *)coder {

    self = [super initWithCoder:coder];

    if (self) {

        [self commonInit];

    }

    return self;

}


- (void)commonInit {

#ifndef SK_GANESH

    NSLog(@"SK_GANESH is undefined");

#endif

    

#ifndef SK_METAL

    NSLog(@"SK_METAL is undefined");

#endif

    

    self.metalDevice = MTLCreateSystemDefaultDevice();

    

    if (!self.metalDevice) {

        NSLog(@"Metal is not supported on this device");

        return;

    }

    

    self.metalQueue = [self.metalDevice newCommandQueue];

    

    if (!self.metalQueue) {

        NSLog(@"Failed to create Metal command queue");

        return;

    }

    

    self.device = self.metalDevice;

    self.delegate = self;

    self.enableSetNeedsDisplay = YES;

    

    self.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;

    self.colorPixelFormat = MTLPixelFormatBGRA8Unorm;

    self.sampleCount = 1;

    

    self.layer.borderWidth = 2.0;

    self.layer.borderColor = [UIColor redColor].CGColor;

}


- (void) didMoveToWindow {

    NSLog(@"didMoveToWindow");

    

    GrMtlBackendContext backendContext = {};

    backendContext.fDevice.retain((__bridge_retained GrMTLHandle)[self metalDevice]);

    backendContext.fQueue.retain((__bridge_retained GrMTLHandle)[self metalQueue]);

    

    GrContextOptions grContextOptions;

    self.grDirectContext = GrDirectContexts::MakeMetal(backendContext, grContextOptions);

    

    if (!self.grDirectContext) {

        NSLog(@"Failed to create GrDirectContext");

        return;

    }

    

    NSLog(@"Created GrDirectContext");

    

    GrMTLHandle mtlHandle = (__bridge_retained GrMTLHandle)self;

    self.surface = SkSurfaces::WrapMTKView(

                                           self.grDirectContext.get(),

                                           mtlHandle,

                                           kTopLeft_GrSurfaceOrigin,

                                           1,

                                           kBGRA_8888_SkColorType,

                                           SkColorSpace::MakeSRGB(),

                                           nullptr

                                           );

    

    if (!self.surface) {

        NSLog(@"Failed to create Skia surface");

        return;

    }

    

    NSLog(@"Created Surface");

}


#pragma mark - MTKViewDelegate


- (void)drawInMTKView:(nonnull MTKView *)view {

}

- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size {

}

@end

And it's always crashing in MakeMetal when I'm using it in a static iOS library (on an iOS simulator). And it works in application project for some reason. I've also tried `__bridge_retained`, nothing changed.

Usually it looks like this:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x20)

    frame #0: 0x0000000180069858 libobjc.A.dylib`objc_retain + 16

  * frame #1: 0x0000000105190618 SkiaViewTestApp`GrMtlGpu::GrMtlGpu(this=0x0000000107117630,

I'm not an iOS developer and not familiar that much with Obj-C memory management, so I have no idea how to fix it — could someone please help with it?

Taublast

unread,
Jan 20, 2025, 8:11:25 AM1/20/25
to skia-discuss
Reply all
Reply to author
Forward
0 new messages