diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index ce5a8a2e86..a00dc1f618 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -393,7 +393,7 @@ namespace Avalonia.Headless { } - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default, BitmapBlendingMode bitmapBlendingMode = BitmapBlendingMode.SourceOver) { } diff --git a/src/Avalonia.Visuals/ApiCompatBaseline.txt b/src/Avalonia.Visuals/ApiCompatBaseline.txt index f51f26974e..e781cb1d3e 100644 --- a/src/Avalonia.Visuals/ApiCompatBaseline.txt +++ b/src/Avalonia.Visuals/ApiCompatBaseline.txt @@ -1,6 +1,9 @@ Compat issues with assembly Avalonia.Visuals: +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode)' is present in the contract but not in the implementation. +MembersMustExist : Member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode)' does not exist in the implementation but it does exist in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawBitmap(Avalonia.Utilities.IRef, System.Double, Avalonia.Rect, Avalonia.Rect, Avalonia.Visuals.Media.Imaging.BitmapInterpolationMode, Avalonia.Visuals.Media.Imaging.BitmapBlendingMode)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Double Avalonia.Platform.IGeometryImpl.ContourLength.get()' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAndTangentAtDistance(System.Double, Avalonia.Point, Avalonia.Point)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IGeometryImpl.TryGetPointAtDistance(System.Double, Avalonia.Point)' is present in the implementation but not in the contract. -Total Issues: 4 +Total Issues: 7 diff --git a/src/Avalonia.Visuals/Media/Imaging/BitmapBlendingMode.cs b/src/Avalonia.Visuals/Media/Imaging/BitmapBlendingMode.cs new file mode 100644 index 0000000000..473b43dab3 --- /dev/null +++ b/src/Avalonia.Visuals/Media/Imaging/BitmapBlendingMode.cs @@ -0,0 +1,57 @@ +namespace Avalonia.Visuals.Media.Imaging +{ + /// + /// Controls the way the bitmaps are drawn together. + /// + public enum BitmapBlendingMode + { + /// + /// Source is placed over the destination. + /// + SourceOver, + /// + /// Only the source will be present. + /// + Source, + /// + /// Only the destination will be present. + /// + Destination, + /// + /// Destination is placed over the source. + /// + DestinationOver, + /// + /// The source that overlaps the destination, replaces the destination. + /// + SourceIn, + /// + /// Destination which overlaps the source, replaces the source. + /// + DestinationIn, + /// + /// Source is placed, where it falls outside of the destination. + /// + SourceOut, + /// + /// Destination is placed, where it falls outside of the source. + /// + DestinationOut, + /// + /// Source which overlaps the destination, replaces the destination. + /// + SourceAtop, + /// + /// Destination which overlaps the source replaces the source. + /// + DestinationAtop, + /// + /// The non-overlapping regions of source and destination are combined. + /// + Xor, + /// + /// Display the sum of the source image and destination image. + /// + Plus, + } +} diff --git a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs index d6e88a7507..e9a1a2a6ae 100644 --- a/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Platform/IDrawingContextImpl.cs @@ -30,7 +30,8 @@ namespace Avalonia.Platform /// The rect in the image to draw. /// The rect in the output to draw to. /// The bitmap interpolation mode. - void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default); + /// The bitmap blending mode. + void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode = BitmapInterpolationMode.Default, BitmapBlendingMode bitmapBlendMode = BitmapBlendingMode.SourceOver); /// /// Draws a bitmap image. diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs index 28f426266d..1705bc6b1c 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/DeferredDrawingContextImpl.cs @@ -112,13 +112,13 @@ namespace Avalonia.Rendering.SceneGraph } /// - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { var next = NextDrawAs(); - if (next == null || !next.Item.Equals(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode)) + if (next == null || !next.Item.Equals(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode, bitmapBlendingMode)) { - Add(new ImageNode(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode)); + Add(new ImageNode(Transform, source, opacity, sourceRect, destRect, bitmapInterpolationMode, bitmapBlendingMode)); } else { diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs index c9052c6ef2..c50601120f 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/ImageNode.cs @@ -18,7 +18,7 @@ namespace Avalonia.Rendering.SceneGraph /// The source rect. /// The destination rect. /// The bitmap interpolation mode. - public ImageNode(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public ImageNode(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) : base(destRect, transform) { Transform = transform; @@ -27,6 +27,7 @@ namespace Avalonia.Rendering.SceneGraph SourceRect = sourceRect; DestRect = destRect; BitmapInterpolationMode = bitmapInterpolationMode; + BitmapBlendingMode = bitmapBlendingMode; SourceVersion = Source.Item.Version; } @@ -67,6 +68,14 @@ namespace Avalonia.Rendering.SceneGraph /// The scaling mode. /// public BitmapInterpolationMode BitmapInterpolationMode { get; } + + /// + /// The bitmap blending mode. + /// + /// + /// The blending mode. + /// + public BitmapBlendingMode BitmapBlendingMode { get; } /// /// Determines if this draw operation equals another. @@ -82,22 +91,23 @@ namespace Avalonia.Rendering.SceneGraph /// The properties of the other draw operation are passed in as arguments to prevent /// allocation of a not-yet-constructed draw operation object. /// - public bool Equals(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public bool Equals(Matrix transform, IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { return transform == Transform && - Equals(source.Item, Source.Item) && - source.Item.Version == SourceVersion && - opacity == Opacity && - sourceRect == SourceRect && - destRect == DestRect && - bitmapInterpolationMode == BitmapInterpolationMode; + Equals(source.Item, Source.Item) && + source.Item.Version == SourceVersion && + opacity == Opacity && + sourceRect == SourceRect && + destRect == DestRect && + bitmapInterpolationMode == BitmapInterpolationMode && + bitmapBlendingMode == BitmapBlendingMode; } /// public override void Render(IDrawingContextImpl context) { context.Transform = Transform; - context.DrawBitmap(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode); + context.DrawBitmap(Source, Opacity, SourceRect, DestRect, BitmapInterpolationMode, BitmapBlendingMode); } /// diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs index 2a79a4bb50..692d40a653 100644 --- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs +++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs @@ -132,7 +132,7 @@ namespace Avalonia.Skia } /// - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { var drawableImage = (IDrawableBitmapImpl)source.Item; var s = sourceRect.ToSKRect(); @@ -145,6 +145,7 @@ namespace Avalonia.Skia }) { paint.FilterQuality = bitmapInterpolationMode.ToSKFilterQuality(); + paint.BlendMode = bitmapBlendingMode.ToSKBlendMode(); drawableImage.Draw(this, s, d, paint); } @@ -154,7 +155,7 @@ namespace Avalonia.Skia public void DrawBitmap(IRef source, IBrush opacityMask, Rect opacityMaskRect, Rect destRect) { PushOpacityMask(opacityMask, opacityMaskRect); - DrawBitmap(source, 1, new Rect(0, 0, source.Item.PixelSize.Width, source.Item.PixelSize.Height), destRect, BitmapInterpolationMode.Default); + DrawBitmap(source, 1, new Rect(0, 0, source.Item.PixelSize.Width, source.Item.PixelSize.Height), destRect, BitmapInterpolationMode.Default, BitmapBlendingMode.SourceOver); PopOpacityMask(); } diff --git a/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs b/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs index bb3dbbfadc..75b4231640 100644 --- a/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs +++ b/src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs @@ -25,6 +25,39 @@ namespace Avalonia.Skia } } + public static SKBlendMode ToSKBlendMode(this BitmapBlendingMode blendingMode) + { + switch (blendingMode) + { + case BitmapBlendingMode.SourceOver: + return SKBlendMode.SrcOver; + case BitmapBlendingMode.Source: + return SKBlendMode.Src; + case BitmapBlendingMode.SourceIn: + return SKBlendMode.SrcIn; + case BitmapBlendingMode.SourceOut: + return SKBlendMode.SrcOut; + case BitmapBlendingMode.SourceAtop: + return SKBlendMode.SrcATop; + case BitmapBlendingMode.Destination: + return SKBlendMode.Dst; + case BitmapBlendingMode.DestinationIn: + return SKBlendMode.DstIn; + case BitmapBlendingMode.DestinationOut: + return SKBlendMode.DstOut; + case BitmapBlendingMode.DestinationOver: + return SKBlendMode.DstOver; + case BitmapBlendingMode.DestinationAtop: + return SKBlendMode.DstATop; + case BitmapBlendingMode.Xor: + return SKBlendMode.Xor; + case BitmapBlendingMode.Plus: + return SKBlendMode.Plus; + default: + throw new ArgumentOutOfRangeException(nameof(blendingMode), blendingMode, null); + } + } + public static SKPoint ToSKPoint(this Point p) { return new SKPoint((float)p.X, (float)p.Y); diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs index 47a19aad8c..0f5a932ef3 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs @@ -5,6 +5,7 @@ using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Rendering.SceneGraph; using Avalonia.Utilities; +using Avalonia.Visuals.Media.Imaging; using SharpDX; using SharpDX.Direct2D1; using SharpDX.Mathematics.Interop; @@ -116,12 +117,14 @@ namespace Avalonia.Direct2D1.Media /// The rect in the image to draw. /// The rect in the output to draw to. /// The bitmap interpolation mode. - public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) + public void DrawBitmap(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode, BitmapBlendingMode bitmapBlendingMode) { using (var d2d = ((BitmapImpl)source.Item).GetDirect2DBitmap(_deviceContext)) { var interpolationMode = GetInterpolationMode(bitmapInterpolationMode); - + + // TODO: How to implement CompositeMode here? + _deviceContext.DrawBitmap( d2d.Value, destRect.ToSharpDX(), @@ -149,6 +152,35 @@ namespace Avalonia.Direct2D1.Media } } + public static CompositeMode GetCompositeMode(BitmapBlendingMode blendingMode) + { + switch (blendingMode) + { + case BitmapBlendingMode.SourceIn: + return CompositeMode.SourceIn; + case BitmapBlendingMode.SourceOut: + return CompositeMode.SourceOut; + case BitmapBlendingMode.SourceOver: + return CompositeMode.SourceOver; + case BitmapBlendingMode.SourceAtop: + return CompositeMode.SourceAtop; + case BitmapBlendingMode.DestinationIn: + return CompositeMode.DestinationIn; + case BitmapBlendingMode.DestinationOut: + return CompositeMode.DestinationOut; + case BitmapBlendingMode.DestinationOver: + return CompositeMode.DestinationOver; + case BitmapBlendingMode.DestinationAtop: + return CompositeMode.DestinationAtop; + case BitmapBlendingMode.Xor: + return CompositeMode.Xor; + case BitmapBlendingMode.Plus: + return CompositeMode.Plus; + default: + throw new ArgumentOutOfRangeException(nameof(blendingMode), blendingMode, null); + } + } + /// /// Draws a bitmap image. ///