/// <summary> /// Create Skia surface backed by given framebuffer. /// </summary> /// <param name="desiredImageInfo">Desired image info.</param> /// <param name="framebuffer">Backing framebuffer.</param> private void CreateSurface(SKImageInfo desiredImageInfo, ILockedFramebuffer framebuffer) { if (_framebufferSurface != null && AreImageInfosCompatible(_currentImageInfo, desiredImageInfo) && _currentFramebufferAddress == framebuffer.Address) { return; } FreeSurface(); _currentFramebufferAddress = framebuffer.Address; var surface = SKSurface.Create(desiredImageInfo, _currentFramebufferAddress, framebuffer.RowBytes); // If surface cannot be created - try to create a compatibility shim first if (surface == null) { _conversionShim = new PixelFormatConversionShim(desiredImageInfo, framebuffer.Address); _preFramebufferCopyHandler = _conversionShim.SurfaceCopyHandler; surface = _conversionShim.Surface; } _framebufferSurface = surface ?? throw new Exception("Unable to create a surface for pixel format " + framebuffer.Format + " or pixel format translator"); _currentImageInfo = desiredImageInfo; }
/// <summary> /// Free Skia surface. /// </summary> private void FreeSurface() { _conversionShim?.Dispose(); _conversionShim = null; _preFramebufferCopyHandler = null; _framebufferSurface?.Dispose(); _framebufferSurface = null; _currentFramebufferAddress = IntPtr.Zero; }