Use metal as backend in skia

296 views
Skip to first unread message

Jarlen John

unread,
Mar 14, 2023, 8:35:47 AM3/14/23
to skia-discuss
want to try the metal with skia, but the surface always return null, and can't find any sample code around..
Here is the code:

      if (_skContext == nullptr) {

        GrContextOptions grContextOptions;

        _skContext = GrDirectContext::MakeMetal((__bridge void*)_device,

                                                (__bridge void*)_commandQueue,

                                                grContextOptions);

        if (_skContext == nullptr) {

            NSAssert(NO, @"Error.");

        }

    }

    

    // Lock Mutex to block the runLoop from overwriting the _texture

    //    std::lock_guard lockGuard(_textureMutex);

    

    id<MTLTexture> texture = _renderTargetTexture;

    

    GrMtlTextureInfo fbInfo;

    fbInfo.fTexture.retain((__bridge void*)texture);

    GrBackendRenderTarget backendRT((int)texture.width,

                                    (int)texture.height,

                                    fbInfo);

    

    const GrSurfaceOrigin origin = kTopLeft_GrSurfaceOrigin;

    const SkColorType colorType = kBGRA_8888_SkColorType;

    sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB();

    SkSurfaceProps surfaceProps(0, kRGB_H_SkPixelGeometry);

    sk_sp<SkSurface> skSurface(SkSurface::MakeFromBackendRenderTarget(_skContext.get(), backendRT, origin, colorType, nullptr, &surfaceProps));

    if (skSurface == nullptr) {

        NSLog(@"skSurface is null.");

        return;

    }

    SkCanvas* canvas = skSurface->getCanvas();

    SkSurface* surface = canvas->getSurface();


skSurface is alway null, any help is appreciate!

Jim Van Verth

unread,
Mar 14, 2023, 1:17:58 PM3/14/23
to skia-d...@googlegroups.com
Looks like in principle it should work. Is the MTLPixelFormat in your rendertarget BGRA8Unorm_sRGB? Does the texture have MTLTextureUsageRenderTarget in its usage flags?

--
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 on the web visit https://groups.google.com/d/msgid/skia-discuss/da82f7f4-ac3a-4d24-9eb1-e91e2005202fn%40googlegroups.com.


--

Jim Van Verth |
 Software Engineer | Google.com

Jim Van Verth

unread,
Mar 14, 2023, 1:23:05 PM3/14/23
to skia-d...@googlegroups.com
Ah, I see you already resolved it.

Jarlen John

unread,
Mar 15, 2023, 8:48:01 AM3/15/23
to skia-discuss
Thank you, Yes, I have resolved it. 
I find pixelFormat not equal in method of "SkSurface::MakeFromBackendRenderTarget" by run code in debug mode,  so we should check the offscreen texture pixelform must same as colorType parameter. Thanks again!

Reply all
Reply to author
Forward
0 new messages