Browse Source

[Metal] Dispose GRBackendRenderTarget and @autoreleasepool for metal objects (#20815)

* Add @autoreleasepool to release memory

* Pass in and dispose GRBackendRenderTarget

* Use Macros
pull/20934/head
Tim Miller 2 weeks ago
committed by GitHub
parent
commit
f3df9b1f30
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      native/Avalonia.Native/src/OSX/metal.mm
  2. 9
      src/Skia/Avalonia.Skia/Gpu/Metal/SkiaMetalGpu.cs

9
native/Avalonia.Native/src/OSX/metal.mm

@ -88,6 +88,7 @@ public:
}
HRESULT ImportIOSurface(void *handle, AvnPixelFormat pixelFormat, IAvnMetalTexture **ppv) override {
START_COM_ARP_CALL;
auto surf = (IOSurfaceRef)handle;
auto width = IOSurfaceGetWidth(surf);
auto height = IOSurfaceGetHeight(surf);
@ -112,7 +113,6 @@ public:
return E_FAIL;
*ppv = new AvnMetalTexture(texture);
return S_OK;
}
HRESULT ImportSharedEvent(void *mtlSharedEventInstance, IAvnMTLSharedEvent**ppv) override {
@ -132,11 +132,12 @@ public:
HRESULT SignalOrWait(IAvnMTLSharedEvent *ev, uint64_t value, bool wait)
{
START_ARP_CALL;
if (@available(macOS 12.0, *))
{
auto e = dynamic_cast<AvnMTLSharedEvent*>(ev);
if(e == nullptr)
return E_FAIL;;
return E_FAIL;
auto buf = [queue commandBuffer];
if(wait)
[buf encodeWaitForEvent:e->GetEvent() value:value];
@ -204,6 +205,7 @@ public:
~AvnMetalRenderSession()
{
START_ARP_CALL;
auto buffer = [_queue commandBuffer];
[buffer presentDrawable: _drawable];
[buffer commit];
@ -227,6 +229,7 @@ public:
}
HRESULT BeginDrawing(IAvnMetalRenderingSession **ret) override {
START_COM_ARP_CALL;
if([NSThread isMainThread])
{
// Flush all existing rendering
@ -289,7 +292,7 @@ class AvnMetalDisplay : public ComSingleObject<IAvnMetalDisplay, &IID_IAvnMetalD
public:
FORWARD_IUNKNOWN()
HRESULT CreateDevice(IAvnMetalDevice **ret) override {
START_COM_ARP_CALL;
auto device = MTLCreateSystemDefaultDevice();
if(device == nil) {
ret = nil;

9
src/Skia/Avalonia.Skia/Gpu/Metal/SkiaMetalGpu.cs

@ -105,7 +105,7 @@ internal class SkiaMetalGpu : ISkiaGpu
session.IsYFlipped ? GRSurfaceOrigin.BottomLeft : GRSurfaceOrigin.TopLeft,
SKColorType.Bgra8888);
return new SkiaMetalRenderSession(_gpu, surface, session);
return new SkiaMetalRenderSession(_gpu, surface, session, backendTarget);
}
public bool IsCorrupted => false;
@ -118,14 +118,17 @@ internal class SkiaMetalGpu : ISkiaGpu
private readonly SkiaMetalGpu _gpu;
private SKSurface? _surface;
private IMetalPlatformSurfaceRenderingSession? _session;
private GRBackendRenderTarget? _backendTarget;
public SkiaMetalRenderSession(SkiaMetalGpu gpu,
SKSurface surface,
IMetalPlatformSurfaceRenderingSession session)
IMetalPlatformSurfaceRenderingSession session,
GRBackendRenderTarget backendTarget)
{
_gpu = gpu;
_surface = surface;
_session = session;
_backendTarget = backendTarget;
}
public void Dispose()
@ -138,6 +141,8 @@ internal class SkiaMetalGpu : ISkiaGpu
_surface = null;
_session?.Dispose();
_session = null;
_backendTarget?.Dispose();
_backendTarget = null;
}
public GRContext GrContext => _gpu._context!;

Loading…
Cancel
Save