A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using System.Buffers;
6using osu.Framework.Graphics.Primitives;
7using osuTK.Graphics.ES30;
8using SixLabors.ImageSharp.Memory;
9using SixLabors.ImageSharp.PixelFormats;
10
11namespace osu.Framework.Graphics.Textures
12{
13 public class MemoryAllocatorTextureUpload : ITextureUpload
14 {
15 public Span<Rgba32> RawData => memoryOwner.Memory.Span;
16
17 public ReadOnlySpan<Rgba32> Data => RawData;
18
19 private readonly IMemoryOwner<Rgba32> memoryOwner;
20
21 public int Level { get; set; }
22
23 public virtual PixelFormat Format => PixelFormat.Rgba;
24
25 public RectangleI Bounds { get; set; }
26
27 /// <summary>
28 /// Create an empty raw texture with an efficient shared memory backing.
29 /// </summary>
30 /// <param name="width">The width of the texture.</param>
31 /// <param name="height">The height of the texture.</param>
32 /// <param name="memoryAllocator">The source to retrieve memory from. Shared default is used if null.</param>
33 public MemoryAllocatorTextureUpload(int width, int height, MemoryAllocator memoryAllocator = null)
34 {
35 memoryOwner = (memoryAllocator ?? SixLabors.ImageSharp.Configuration.Default.MemoryAllocator).Allocate<Rgba32>(width * height);
36 }
37
38 #region IDisposable Support
39
40 private bool disposed;
41
42 public void Dispose()
43 {
44 Dispose(true);
45 GC.SuppressFinalize(this);
46 }
47
48 protected virtual void Dispose(bool isDisposing)
49 {
50 if (disposed)
51 return;
52
53 memoryOwner?.Dispose();
54
55 disposed = true;
56 }
57
58 #endregion
59 }
60}