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

[release/2.x] Backport some new APIs from 3.0 to 2.x allowing smoother migration. #2756

Closed
wants to merge 9 commits into from

Conversation

maxkatz6
Copy link
Contributor

Description of Change

With these changes Avalonia should be able to work with both 2.x and 3.0 referenced (need to double check once nightly builds are there).

API Changes

Added:

  • SKCanvas.SetMatrix(in SKMatrix);
  • SKPath.Transform(in SKMatrix, SKPath destination);
  • SKImageFilter methods without SKImageFilter and SKImageFilter.CropRect overloads

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

@maxkatz6
Copy link
Contributor Author

maxkatz6 commented Feb 15, 2024

@mattleibow I went through breaking changes page, and these changes were the low hanging fruits of what can be backported from 3.0 to 2.x. Which is also enough for Avalonia to run both versions.

Initially I also planned to backport some new SKRuntimeEffect methods (which we don't need), but it feels a bit intrusive, as I am not sure what to pass as isOpaque parameter.

I also didn't want to mark obsolete any old APIs that are going to be removed in 3.0.

And I am not sure what to do about SKFilterQuality. Looks like 3.0 still handles SKFilterQuality well everywhere, except SKPaint and DrawImage
I guess I could do:

  • Backport SKSamplingOptions to 2.88 (only managed part)
  • Add SKCanvas.DrawImage method that accepts SKSamplingOptions and maps it back to SKPaint.SKFilterQuality (ABI compatible with 3.0)
  • Add SKBitmap.Scale/Resize methods that also accept SKSamplingOptions (Optional, as 3.0 still has working SKFilterQuality methods).

And that would be enough? Without changing 3.0.

@@ -952,6 +952,13 @@ public void SetMatrix (SKMatrix matrix)
SkiaApi.sk_canvas_set_matrix (Handle, &matrix);
}

public void SetMatrix (in SKMatrix matrix)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: C# will always prefer more specific overload. Which means for 2.x builds old code will still use old SetMatrix(SKMatrix), unless they specify in explicitly.

Copy link
Contributor

Choose a reason for hiding this comment

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

One annoying thing is that this will make this method ambiguous to visual basic... VB appears not to be able to distinguish between in/ref and nothing. I know we have a scattering of ref and nothing in 2.x and I tried to clean this all up. However, maybe this is making migrations a bit more annoying.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, you are right. Just tested locally, and found this issue confirming it dotnet/vblang#505

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess the only choice would be to use reflection/UnsafeAccessor then. I don't really want to modify SkiaSharp 3.0 API surface.

@maxkatz6 maxkatz6 changed the title [release/2.x] Backport new APIs from 3.0 to 2.x allowing smoother migration. [release/2.x] Backport some new APIs from 3.0 to 2.x allowing smoother migration. Feb 15, 2024
@workgroupengineering
Copy link

What do you think if instead of adding members to existing classes you added them as extension methods in a class called CompatibilyExtensions?

@maxkatz6
Copy link
Contributor Author

@workgroupengineering this extension method would have to be added to 3.0 as well. So, it can be binary compatible. Imo, it would add more unwanted API surface to maintain.

@@ -952,6 +952,13 @@ public void SetMatrix (SKMatrix matrix)
SkiaApi.sk_canvas_set_matrix (Handle, &matrix);
}

public void SetMatrix (in SKMatrix matrix)
Copy link
Contributor

Choose a reason for hiding this comment

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

One annoying thing is that this will make this method ambiguous to visual basic... VB appears not to be able to distinguish between in/ref and nothing. I know we have a scattering of ref and nothing in 2.x and I tried to clean this all up. However, maybe this is making migrations a bit more annoying.

@@ -74,13 +74,22 @@ internal SKImageFilter(IntPtr handle, bool owns)

// CreateMatrix

public static SKImageFilter CreateMatrix(ref SKMatrix matrix)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is in in 3.x.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@maxkatz6
Copy link
Contributor Author

maxkatz6 commented Mar 7, 2024

@mattleibow update the PR, removed "in" overloads that will break VB. Current PR changes will already help with some migration, though not ideal.

@mattleibow
Copy link
Contributor

Ok. Afk right now but will look tonight or tomorrow. I also think I may have some idea of how to do something quite nice for compat. It is an idea still, but it is coming together nicely.

@mattleibow
Copy link
Contributor

This was idea and my testing of prototypes seemed to work nicely.

#2789

@maxkatz6
Copy link
Contributor Author

maxkatz6 commented Mar 8, 2024

@mattleibow I like your idea. Closing this PR then.

@maxkatz6 maxkatz6 closed this Mar 8, 2024
@maxkatz6 maxkatz6 reopened this Mar 8, 2024
@maxkatz6
Copy link
Contributor Author

maxkatz6 commented Mar 8, 2024

Although, wait, that kind of API changes of this PR cannot be added to 3.0, as it already has these methods :D

Unless we add back overloads with SKImageFilter and SKImageFilter.CropRect:
https://github.com/mono/SkiaSharp/blob/v2.88.7/binding/Binding/SKImageFilter.cs#L101

Imo, for these methods specifically, it would be simpler to get this PR to 2.88 with overloads without optional parameters, keeping common API.

@mattleibow
Copy link
Contributor

Ah yeah. Both. We can backport anything too ugly to do - such as croprect. I'm working on making 2.x shippable again.

@mattleibow
Copy link
Contributor

mattleibow commented Mar 27, 2024

I did some looking around and I had to also remove the default args from the existing ones. #2810

For example, if there is this:

public static SKImageFilter CreatePaint(SKPaint paint, SKImageFilter.CropRect cropRect = null)

And we just add:

public static SKImageFilter CreatePaint (SKPaint paint)

People that are using it like this:

SKImageFilter.CreatePaint(mypaint);

Will now get an ambiguous overload as both match. So I removed the = null value so if you are not specifying a crop rect, then you are going to use the new overload. If you are, well you have specified a value.

@mattleibow
Copy link
Contributor

Thanks for this PR. I am going to close this one as I think I got all the APIs from 3.x in my PR and a few more things: #2810

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants