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

More Span<T> and ReadOnlySpan<T> #2669

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft

Conversation

ScrubN
Copy link

@ScrubN ScrubN commented Nov 10, 2023

Description of Change

I have add Span overloads to many methods that previously only accepted Arrays, IntPtrs, or Strings.

Some methods could not be given span overloads because the Array/Strings must be marshaled as null-terminated types. This could be solved by copying the contents of the span into a rented array and filling the rest of the array with null, however I wanted to discuss this change before making it.

The next changes aren't strictly related to converting to spans, but I figured they were related enough to include them here. If not, I can move them to a new PR.

I changed a few return new T[0] to return Array.Empty<T> () to reduce pointless allocations. As far as I can tell, this does not change the behaviour of dereferencing a pointer to the array.

I also changed the algorithm of implicit operator SKRuntimeEffectUniform (float[][] value) to make allocations linearly associated with the length of value rather than using a variable length list.

Lastly, I believe I fixed 4 memory leaks in SKCanvas.DrawVertices, swapped out 3 SKTypeFace.ToFont calls to SKTypeFace.GetFont in SKTypeFace.GetGlyphs, and fixed a potential(?) nullptr dereference in SKPath.GetPoints. If any changed behaviour occurs in those methods as a result, that might have been my fault.

Bugs Fixed

API Changes

  • SKCanvas.cs
  • SKCodec.cs
  • SKColorFilter.cs
  • SKColorSpace.cs
  • SKColorSpaceStructs.cs
  • SKData.cs
  • SKFont.cs
  • SKMaskFilter.cs
  • SKMatrix.cs
  • SKMatrix44.cs
    • Removed some redundant null checks
  • SKPMColor.cs
  • SKPath.cs
  • SKPathEffect.cs
  • SKRoundRect.cs
    • Removed some redundant null checks
  • SKRuntimeEffect.cs
  • SKShader.cs
  • SKString.cs
  • SKTypeface.cs
  • SKVertices.cs
  • Util.cs
    • Swapped new T[0] for Array.Empty<T> ()
  • SKShaper
  • CanvasExtensions

Behavioral Changes

None.

Required skia PR

None.

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Merged related skia PRs
  • Changes adhere to coding standard
  • Updated documentation

@ScrubN
Copy link
Author

ScrubN commented Nov 10, 2023

I'll make some tests later.

Feedback on the null-terminated LPArray/LPStr situation would be appreciated!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason public Result Shape(string text, float xOffset, float yOffset, SKFont font) uses buffer.AddUtf8 instead of buffer.AddUtf16?

@ScrubN ScrubN marked this pull request as ready for review November 14, 2023 06:42
@ScrubN
Copy link
Author

ScrubN commented Nov 14, 2023

Since the CI didn't run, here's a screenshot of the test results from my IDE. Was a bit of a pain to get it working since its setup for azure pipelines.
image

@ScrubN

This comment was marked as outdated.

@DJGosnell
Copy link

This would be fantastic to include especially for rendering partial arrays like on the SKCanvas.DrawPoints method. Is the only reason this is stalled due to the numerous changes on the main or is there a more fundamental reason?

Copy link
Contributor

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry folks for the extended delay in getting to this one. I had a quick check and I think it is mostly good.

However, this is going to break ABI because the parameters have changed and there will be a bunch of missing member exceptions. Is it possible to instead add overloads and make the old array methods call those overloads.

Could you rebase and rather add overloads.

I also added a few extra comments to things that I saw maybe should not be changed in this PR.

Thanks again for this effort!

README.md Outdated Show resolved Hide resolved
binding/SkiaSharp/SKCanvas.cs Outdated Show resolved Hide resolved
binding/SkiaSharp/SKCanvas.cs Show resolved Hide resolved
binding/SkiaSharp/SKColorSpace.cs Show resolved Hide resolved
binding/SkiaSharp/SKData.cs Show resolved Hide resolved
binding/SkiaSharp/SKRuntimeEffect.cs Outdated Show resolved Hide resolved
binding/SkiaSharp/SKStream.cs Outdated Show resolved Hide resolved
binding/SkiaSharp/SKString.cs Show resolved Hide resolved
Comment on lines -230 to +236
public ushort[] GetGlyphs (ReadOnlySpan<char> text)
{
using var font = ToFont ();
return font.GetGlyphs (text);
}
public ushort[] GetGlyphs (ReadOnlySpan<char> text) =>
GetFont ().GetGlyphs (text);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this changes ToFont into GetFont, and I am not sure why I did this in the beginning. Maybe just use ToFont for now and then if I was wrong, then we can fix it later.

Copy link
Author

@ScrubN ScrubN Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing as the surrounding methods (including some overloads) use GetFont instead of ToFont and because SKFont.GetGlyphs is a pure method, I think it is safe to use ToFont in this situation. If I'm wrong though, I can move the change to a new PR for later review.

@ScrubN
Copy link
Author

ScrubN commented Nov 6, 2024

Thanks for the response! I'll get working on the requested changes.

Could you rebase

I presume this means this PR will supersede #2617?

I also added a few extra comments to things that I saw maybe should not be changed in this PR.

Yeah, I'm not sure what I was thinking with some of the changes either. It probably had something to do with the changes being authored at 4am 😅

@ScrubN ScrubN changed the base branch from dev/reduce-arrays to main November 6, 2024 23:03
@ScrubN
Copy link
Author

ScrubN commented Nov 7, 2024

All tests currently still pass
image

@ScrubN
Copy link
Author

ScrubN commented Nov 7, 2024

In SKShaper, I would like to redirect public Result Shape(string text, float xOffset, float yOffset, SKFont font) to the span overload for consistency, however the string overload currently uses Buffer.AddUtf8 which only accepts string/byte[]/span<byte>. Because of this, the span overload currently uses Buffer.AddUtf16, which produces a different (but technically more correct) cluster sequence when shaped by harfbuzz.

Because this is a breaking change, I do not want to make the call in this PR, but I would like to start a discussion on whether it can be done in order to maintain output parity between the string and span overloads.

@ScrubN ScrubN marked this pull request as draft November 7, 2024 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants