SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

More Span<T> and ReadOnlySpan<T>

Open ScrubN opened this issue 2 years ago • 11 comments

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

  • Related to #2616

API Changes

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

Behavioral Changes

None.

Required skia PR

None.

PR Checklist

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

ScrubN avatar Nov 10 '23 09:11 ScrubN

I'll make some tests later.

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

ScrubN avatar Nov 10 '23 09:11 ScrubN

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 avatar Nov 14 '23 06:11 ScrubN

@mattleibow What steps are needed to get this PR merged into dev/reduce-arrays, then dev/reduce-arrays into main?

ScrubN avatar Aug 13 '24 00:08 ScrubN