Skip to content

Commit

Permalink
fix: apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Jul 7, 2022
1 parent 3746434 commit 595ee25
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
13 changes: 5 additions & 8 deletions src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,24 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH
return image != null;
}

private (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null)
private (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null)
{

var byteCount = 0;
Bitmap? bitmap = default;
UIElement elementToRender = element
?? XamlRoot.Current.Content
?? throw new global::System.NullReferenceException();

// In UWP RenderTargetBitmap use logical size
// if using logical size to render the element there are generate glycth.
// to avoid this layout the control on Physical and after scale to logical
var logical = elementToRender.ActualSize.ToSize();
var logical = element.ActualSize.ToSize();
var physical = logical.LogicalToPhysicalPixels();
if (physical.IsEmpty)
{
return (0, 0, 0);
}
try
{
SetSoftwareRendering(elementToRender, true);
SetSoftwareRendering(element, true);
bitmap = Bitmap.CreateBitmap((int)physical.Width, (int)physical.Height, Bitmap.Config.Argb8888!, true)
?? throw new InvalidOperationException("Failed to create target native bitmap.");
using var canvas = new Canvas(bitmap);
Expand All @@ -79,7 +76,7 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH
Uno.UI.UnoViewGroup.TryFastRequestLayout(element, false);
// Render on the canvas
canvas.DrawColor(Colors.Transparent, PorterDuff.Mode.Clear!);
elementToRender.Draw(canvas);
element.Draw(canvas);
}

var targetSize = scaledSize ?? logical;
Expand Down Expand Up @@ -111,7 +108,7 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH
finally
{
bitmap?.Dispose();
SetSoftwareRendering(elementToRender, false);
SetSoftwareRendering(element, false);
}


Expand Down
14 changes: 11 additions & 3 deletions src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ public IAsyncAction RenderAsync(UIElement? element, int scaledWidth, int scaledH
{
try
{
(_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(element, ref _buffer, new Size(scaledWidth, scaledHeight));
UIElement elementToRender = element
?? XamlRoot.Current.Content
?? throw new global::System.NullReferenceException("Invalid XamlRoot Content.");

(_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(elementToRender, ref _buffer, new Size(scaledWidth, scaledHeight));
#if __WASM__ || __SKIA__
InvalidateSource();
#endif
Expand All @@ -106,7 +110,11 @@ public IAsyncAction RenderAsync(UIElement? element)
{
try
{
(_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(element, ref _buffer);
UIElement elementToRender = element
?? XamlRoot.Current.Content
?? throw new global::System.NullReferenceException("Invalid XamlRoot Content.");

(_bufferSize, PixelWidth, PixelHeight) = RenderAsBgra8_Premul(elementToRender, ref _buffer);
#if __WASM__ || __SKIA__
InvalidateSource();
#endif
Expand All @@ -131,7 +139,7 @@ public IAsyncOperation<IBuffer> GetPixelsAsync()
});

#if NOT_IMPLEMENTED
private (int ByteCount,int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null)
private (int ByteCount,int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null)
=> throw new NotImplementedException("RenderTargetBitmap is not supported on this platform.");
#endif

Expand Down
9 changes: 3 additions & 6 deletions src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ private protected override bool TryOpenSourceSync([NotNullWhen(true)] out UIImag
return image != null;
}

private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null)
private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null)
{
UIElement elementToRender = element
?? XamlRoot.Current.Content
?? throw new global::System.NullReferenceException();
var size = new Size(elementToRender.ActualSize.X, elementToRender.ActualSize.Y);
var size = new Size(element.ActualSize.X, element.ActualSize.Y);

if (size.IsEmpty)
{
Expand All @@ -61,7 +58,7 @@ private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIEle
UIGraphics.BeginImageContextWithOptions(size, false, 1f);
var ctx = UIGraphics.GetCurrentContext();
ctx.SetFillColor(Colors.Transparent); // This is only for pixels not used, but the bitmap as the same size of the element. We keep it only for safety!
elementToRender.Layer.RenderInContext(ctx);
element.Layer.RenderInContext(ctx);
img = UIGraphics.GetImageFromCurrentImageContext();
}
finally
Expand Down
12 changes: 4 additions & 8 deletions src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@ private protected override bool TryOpenSourceSync([NotNullWhen(true)] out NSImag
return image != null;
}

private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null)
private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null)
{
UIElement elementToRender = element
?? XamlRoot.Current.Content
?? throw new global::System.NullReferenceException();

var size = new Size(elementToRender.ActualSize.X, elementToRender.ActualSize.Y);
var size = new Size(element.ActualSize.X, element.ActualSize.Y);

if (size.IsEmpty)
{
Expand All @@ -62,10 +58,10 @@ private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIEle
try
{
img = new NSImage(size);
img.LockFocusFlipped(elementToRender.IsFlipped);
img.LockFocusFlipped(element.IsFlipped);
var ctx = NSGraphicsContext.CurrentContext!.GraphicsPort;
ctx.SetFillColor(Colors.Transparent); // This is only for pixels not used, but the bitmap as the same size of the element. We keep it only for safety!
elementToRender.Layer!.RenderInContext(ctx);
element.Layer!.RenderInContext(ctx);
}
finally
{
Expand Down
14 changes: 5 additions & 9 deletions src/Uno.UI/UI/Xaml/Media/Imaging/RenderTargetBitmap.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ partial class RenderTargetBitmap
? new SwapColor(SwapRB)
: default(SwapColor);

private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement? element, ref byte[]? buffer, Size? scaledSize = null)
private static (int ByteCount, int Width, int Height) RenderAsBgra8_Premul(UIElement element, ref byte[]? buffer, Size? scaledSize = null)
{
UIElement elementToRender = element
?? XamlRoot.Current.Content
?? throw new global::System.NullReferenceException();
var renderSize = element.RenderSize;
var visual = element.Visual;

var renderSize = elementToRender.RenderSize;
var visual = elementToRender.Visual;

if (elementToRender.RenderSize is { IsEmpty: true }
|| elementToRender.RenderSize is { Width: 0, Height: 0 })
if (element.RenderSize is { IsEmpty: true }
|| element.RenderSize is { Width: 0, Height: 0 })
{
return (0, 0, 0);
}
Expand Down

0 comments on commit 595ee25

Please sign in to comment.