// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Buffers; using osu.Framework.Graphics.Primitives; using osuTK.Graphics.ES30; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; namespace osu.Framework.Graphics.Textures { public class MemoryAllocatorTextureUpload : ITextureUpload { public Span RawData => memoryOwner.Memory.Span; public ReadOnlySpan Data => RawData; private readonly IMemoryOwner memoryOwner; public int Level { get; set; } public virtual PixelFormat Format => PixelFormat.Rgba; public RectangleI Bounds { get; set; } /// /// Create an empty raw texture with an efficient shared memory backing. /// /// The width of the texture. /// The height of the texture. /// The source to retrieve memory from. Shared default is used if null. public MemoryAllocatorTextureUpload(int width, int height, MemoryAllocator memoryAllocator = null) { memoryOwner = (memoryAllocator ?? SixLabors.ImageSharp.Configuration.Default.MemoryAllocator).Allocate(width * height); } #region IDisposable Support private bool disposed; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool isDisposing) { if (disposed) return; memoryOwner?.Dispose(); disposed = true; } #endregion } }