Skip to content

Commit

Permalink
feat(composition): More work on the CompositionNineGridBrush implemen…
Browse files Browse the repository at this point in the history
…tation + sample
  • Loading branch information
ahmed605 committed Sep 5, 2023
1 parent 9dec61d commit 2def209
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
d:DesignWidth="400">

<StackPanel Orientation="Vertical" Spacing="20" VerticalAlignment="Top" HorizontalAlignment="Left">
<StackPanel Orientation="Horizontal" Spacing="20" HorizontalAlignment="Center">
<TextBlock Text="Source" FontSize="27" Width="100" FontWeight="Light" TextAlignment="Center"/>
<TextBlock Text="NineGrid" FontSize="27" Width="100" FontWeight="Light" TextAlignment="Center"/>
<StackPanel Orientation="Horizontal" Spacing="70" HorizontalAlignment="Center">
<TextBlock Text="Source" FontSize="27" Width="100" Margin="50,0,0,0" FontWeight="Light" TextAlignment="Center"/>
<TextBlock Text="NineGrid" FontSize="27" Width="200" FontWeight="Light" TextAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="70" HorizontalAlignment="Center">
<Image x:Name="img" Width="100" Height="100" Margin="50,0,0,0" Stretch="UniformToFill" Source="ms-appx:///Assets/test_image_125_125.png"/>
<Canvas x:Name="canvas" Width="200" Height="200"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="20" HorizontalAlignment="Center">
<Image x:Name="img" Width="100" Height="100" Stretch="UniformToFill" Source="ms-appx:///Assets/test_image_125_125.png"/>
<Canvas x:Name="canvas" Width="100" Height="100"/>
<Canvas x:Name="canvas1" Width="200" Height="200"/>
<Grid>
<TextBlock Text="IsCenterHollow" FontSize="20" FontWeight="Light" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Canvas x:Name="canvas2" Width="200" Height="200"/>
</Grid>
</StackPanel>
</StackPanel>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Uno.UI.Samples.Controls;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
Expand Down Expand Up @@ -42,9 +43,26 @@ private void NineGridTests_Loaded(object sender, RoutedEventArgs e)

var spriteVisual = compositor.CreateSpriteVisual();
spriteVisual.Brush = nineGridBrush;
spriteVisual.Size = new(100, 100);
spriteVisual.Size = new(200, 200);

var brush2 = compositor.CreateColorBrush(Colors.Red);

var spriteVisual1 = compositor.CreateSpriteVisual();
spriteVisual1.Brush = brush2;
spriteVisual1.Size = new(200, 200);

var nineGridBrush2 = compositor.CreateNineGridBrush();
nineGridBrush2.Source = brush2;
nineGridBrush2.IsCenterHollow = true;
nineGridBrush2.SetInsets(20);

var spriteVisual2 = compositor.CreateSpriteVisual();
spriteVisual2.Brush = nineGridBrush2;
spriteVisual2.Size = new(200, 200);

ElementCompositionPreview.SetElementChildVisual(canvas, spriteVisual);
ElementCompositionPreview.SetElementChildVisual(canvas1, spriteVisual1);
ElementCompositionPreview.SetElementChildVisual(canvas2, spriteVisual2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using SkiaSharp;
Expand All @@ -15,7 +16,7 @@ public partial class CompositionNineGridBrush : CompositionBrush, IOnlineBrush
private SKPaint _sourcePaint = new SKPaint() { IsAntialias = true };
private SKImage? _sourceImage;
private SKSurface? _surface;
//private SKPaint? _hollowPaint; // TODO: Implement IsCenterHollow
private SKPaint? _filterPaint = new SKPaint() { FilterQuality = SKFilterQuality.High, IsAntialias = true, IsAutohinted = true, IsDither = true };
private SKRectI _insetRect;

bool IOnlineBrush.IsOnline => true; // TODO: `Source is IOnlineBrush onlineBrush && onlineBrush.IsOnline`
Expand All @@ -29,16 +30,22 @@ internal override void UpdatePaint(SKPaint paint, SKRect bounds)

void IOnlineBrush.Draw(in DrawingSession session, SKRect bounds)
{
SKRect sourceBounds;
if (Source is ISizedBrush sizedBrush && sizedBrush.IsSized && sizedBrush.Size is Vector2 sourceSize)
sourceBounds = new(0, 0, sourceSize.X, sourceSize.Y);
else
sourceBounds = bounds;

if ((Source is IOnlineBrush onlineBrush && onlineBrush.IsOnline) || _sourcePaint.Shader is null || _sourceImage is null)
{
Source?.UpdatePaint(_sourcePaint, bounds);
Source?.UpdatePaint(_sourcePaint, sourceBounds);

if (_surface is null)
_surface = SKSurface.Create(new SKImageInfo((int)bounds.Width, (int)bounds.Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul));
_surface = SKSurface.Create(new SKImageInfo((int)sourceBounds.Width, (int)sourceBounds.Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul));

if (_surface is not null)
{
_surface.Canvas.DrawRect(bounds, _sourcePaint);
_surface.Canvas.DrawRect(sourceBounds, _sourcePaint);
_surface.Canvas.Flush();
_sourceImage?.Dispose();
_sourceImage = _surface.Snapshot();
Expand All @@ -48,11 +55,14 @@ void IOnlineBrush.Draw(in DrawingSession session, SKRect bounds)
if (_sourceImage is not null)
{
_insetRect.Top = (int)(TopInset * TopInsetScale);
_insetRect.Bottom = (int)(bounds.Height - (BottomInset * BottomInsetScale));
_insetRect.Right = (int)(bounds.Width - (RightInset * RightInsetScale));
_insetRect.Bottom = (int)(sourceBounds.Height - (BottomInset * BottomInsetScale));
_insetRect.Right = (int)(sourceBounds.Width - (RightInset * RightInsetScale));
_insetRect.Left = (int)(LeftInset * LeftInsetScale);

session.Surface?.Canvas.DrawImageNinePatch(_sourceImage, _insetRect, bounds);
if (IsCenterHollow)
session.Surface?.Canvas.ClipRect(_insetRect, SKClipOperation.Difference, true);

session.Surface?.Canvas.DrawImageNinePatch(_sourceImage, _insetRect, bounds, _filterPaint);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

namespace Windows.UI.Composition
{
public partial class CompositionSurfaceBrush : CompositionBrush, IOnlineBrush
public partial class CompositionSurfaceBrush : CompositionBrush, IOnlineBrush, ISizedBrush
{
bool IOnlineBrush.IsOnline => Surface is ISkiaSurface skiaSurface;

bool ISizedBrush.IsSized => true;

Vector2? ISizedBrush.Size => Surface is SkiaCompositionSurface scs && scs.Image is not null ? new(scs.Image.Width, scs.Image.Height) : (Surface is ISkiaSurface skiaSurface && skiaSurface.Surface is not null ? new((float)skiaSurface.Surface.Canvas.DeviceClipBounds.Width, (float)skiaSurface.Surface.Canvas.DeviceClipBounds.Height) : null);

internal override void UpdatePaint(SKPaint fillPaint, SKRect bounds)
{
if (Surface is SkiaCompositionSurface scs)
Expand All @@ -30,6 +34,9 @@ internal override void UpdatePaint(SKPaint fillPaint, SKRect bounds)
{
fillPaint.Shader = skiaSurface.Surface.Snapshot().ToShader(SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, TransformMatrix.ToSKMatrix());
fillPaint.IsAntialias = true;
fillPaint.FilterQuality = SKFilterQuality.High;
fillPaint.IsAutohinted = true;
fillPaint.IsDither = true;
}
else
fillPaint.Shader = null;
Expand Down
13 changes: 13 additions & 0 deletions src/Uno.UI.Composition/Composition/Uno/ISizedBrush.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#nullable enable

using System.Numerics;
using SkiaSharp;

namespace Uno.UI.Composition
{
internal interface ISizedBrush
{
internal bool IsSized { get; }
internal Vector2? Size { get; }
}
}

0 comments on commit 2def209

Please sign in to comment.