Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageSurface: Support memory pressure #1115

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Libs/cairo-1.0/Internal/SurfaceHandle.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
namespace Cairo.Internal;
using System;

namespace Cairo.Internal;

public partial class SurfaceOwnedHandle
{
private long _memoryPressure;

internal void AddMemoryPressure(long memoryPressure)
{
if (memoryPressure > 0)
{
_memoryPressure = memoryPressure;
GC.AddMemoryPressure(_memoryPressure);
}
}

private void RemoveMemoryPressure()
{
if (_memoryPressure > 0)
GC.RemoveMemoryPressure(_memoryPressure);
}

protected override bool ReleaseHandle()
{
RemoveMemoryPressure();

Surface.Destroy(handle);
return true;
}
Expand Down
13 changes: 10 additions & 3 deletions src/Libs/cairo-1.0/Public/ImageSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ public class ImageSurface : Surface
public ImageSurface(Format format, int width, int height)
: base(Internal.ImageSurface.Create(format, width, height))
{
Handle.AddMemoryPressure(GetSizeInBytes());
}

public Format Format => Internal.ImageSurface.GetFormat(Handle);
public int Height => Internal.ImageSurface.GetHeight(Handle);
public int Width => Internal.ImageSurface.GetWidth(Handle);

/// <summary>
/// Number of bytes per row.
/// </summary>
public int Stride => Internal.ImageSurface.GetStride(Handle);

private int GetSizeInBytes() => Stride * Height;

public Span<byte> GetData()
{
IntPtr data = Internal.ImageSurface.GetData(Handle);
int len = Stride * Height; // Stride is the number of bytes per row.
var data = Internal.ImageSurface.GetData(Handle);

unsafe
{
return new Span<byte>(data.ToPointer(), len);
return new Span<byte>(data.ToPointer(), GetSizeInBytes());
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/Libs/cairo-1.0/Public/Surface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,24 @@ public Surface CreateSimilar(Content content, int width, int height)
=> new Surface(Internal.Surface.CreateSimilar(Handle, content, width, height));

public Surface CreateSimilarImage(Format format, int width, int height)
=> new Surface(Internal.Surface.CreateSimilarImage(Handle, format, width, height));
{
var handle = Internal.Surface.CreateSimilarImage(Handle, format, width, height);

//See: https://www.cairographics.org/manual/cairo-Image-Surfaces.html#cairo-format-t
var bytesPerPixel = format switch
{
Format.A1 => 1 / 8d,
Format.A8 => 1,
Format.Argb32 => 4,
Format.Rgb24 => 4,
Format.Rgb30 => 4,
Format.Rgb16565 => 2,
_ => 0 //No memory pressure is applied
};
handle.AddMemoryPressure((long) (width * height * bytesPerPixel));

return new Surface(handle);
}

public Surface CreateForRectangle(double x, double y, double width, double height)
=> new Surface(Internal.Surface.CreateForRectangle(Handle, x, y, width, height));
Expand Down
Loading