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

API Cleanup: Use PixelSize for device-dependent sizes. #1889

Merged
merged 9 commits into from
Oct 29, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public AndroidFramebuffer(Surface surface)
ANativeWindow_Buffer buffer;
var rc = new ARect()
{
right = Width = ANativeWindow_getWidth(_window),
bottom = Height = ANativeWindow_getHeight(_window)
right = ANativeWindow_getWidth(_window),
bottom = ANativeWindow_getHeight(_window)
};
Size = new PixelSize(rc.right, rc.bottom);
ANativeWindow_lock(_window, out buffer, ref rc);

Format = buffer.format == AndroidPixelFormat.WINDOW_FORMAT_RGB_565
Expand All @@ -41,8 +42,7 @@ public void Dispose()
}

public IntPtr Address { get; set; }
public int Width { get; }
public int Height { get; }
public PixelSize Size { get; }
public int RowBytes { get; }
public Vector Dpi { get; } = new Vector(96, 96);
public PixelFormat Format { get; }
Expand Down
5 changes: 2 additions & 3 deletions src/Avalonia.Controls/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void Render(DrawingContext context)
if (source != null)
{
Rect viewPort = new Rect(Bounds.Size);
Size sourceSize = new Size(source.PixelWidth, source.PixelHeight);
Size sourceSize = new Size(source.PixelSize.Width, source.PixelSize.Height);
Vector scale = Stretch.CalculateScaling(Bounds.Size, sourceSize);
Size scaledSize = sourceSize * scale;
Rect destRect = viewPort
Expand All @@ -84,8 +84,7 @@ protected override Size MeasureOverride(Size availableSize)

if (source != null)
{
Size sourceSize = new Size(source.PixelWidth, source.PixelHeight);

Size sourceSize = new Size(source.PixelSize.Width, source.PixelSize.Height);
if (double.IsInfinity(availableSize.Width) || double.IsInfinity(availableSize.Height))
{
return sourceSize;
Expand Down
8 changes: 4 additions & 4 deletions src/Avalonia.Controls/Remote/RemoteWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ public override void Render(DrawingContext context)
if (_lastFrame != null)
{
var fmt = (PixelFormat) _lastFrame.Format;
if (_bitmap == null || _bitmap.PixelWidth != _lastFrame.Width ||
_bitmap.PixelHeight != _lastFrame.Height)
_bitmap = new WriteableBitmap(_lastFrame.Width, _lastFrame.Height, fmt);
if (_bitmap == null || _bitmap.PixelSize.Width != _lastFrame.Width ||
_bitmap.PixelSize.Height != _lastFrame.Height)
_bitmap = new WriteableBitmap(new PixelSize(_lastFrame.Width, _lastFrame.Height), new Vector(96, 96), fmt);
using (var l = _bitmap.Lock())
{
var lineLen = (fmt == PixelFormat.Rgb565 ? 2 : 4) * _lastFrame.Width;
for (var y = 0; y < _lastFrame.Height; y++)
Marshal.Copy(_lastFrame.Data, y * _lastFrame.Stride,
new IntPtr(l.Address.ToInt64() + l.RowBytes * y), lineLen);
}
context.DrawImage(_bitmap, 1, new Rect(0, 0, _bitmap.PixelWidth, _bitmap.PixelHeight),
context.DrawImage(_bitmap, 1, new Rect(0, 0, _bitmap.PixelSize.Width, _bitmap.PixelSize.Height),
new Rect(Bounds.Size));
}
base.Render(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ FrameMessage RenderFrame(int width, int height, ProtocolPixelFormat? format)
var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
_framebuffer = new LockedFramebuffer(handle.AddrOfPinnedObject(), width, height, width * bpp, _dpi, (PixelFormat)fmt,
_framebuffer = new LockedFramebuffer(handle.AddrOfPinnedObject(), new PixelSize(width, height), width * bpp, _dpi, (PixelFormat)fmt,
null);
Paint?.Invoke(new Rect(0, 0, width, height));
}
Expand Down
9 changes: 4 additions & 5 deletions src/Avalonia.Native/DeferredFramebuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ public DeferredFramebuffer(Func<Action<IAvnWindowBase>, bool> lockWindow,
{
_lockWindow = lockWindow;
Address = Marshal.AllocHGlobal(width * height * 4);
Width = width;
Height = height;
Size = new PixelSize(width, height);
RowBytes = width * 4;
Dpi = dpi;
Format = PixelFormat.Rgba8888;
}

public IntPtr Address { get; set; }
public int Width { get; set; }
public PixelSize Size { get; set; }
public int Height { get; set; }
public int RowBytes { get; set; }
public Vector Dpi { get; set; }
Expand Down Expand Up @@ -66,8 +65,8 @@ public void Dispose()
X = Dpi.X,
Y = Dpi.Y
},
Width = Width,
Height = Height,
Width = Size.Width,
Height = Size.Height,
PixelFormat = (AvnPixelFormat)Format,
Stride = RowBytes
};
Expand Down
6 changes: 2 additions & 4 deletions src/Avalonia.Native/WindowImplBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,13 @@ class FramebufferWrapper : ILockedFramebuffer
public FramebufferWrapper(AvnFramebuffer fb)
{
Address = fb.Data;
Width = fb.Width;
Height = fb.Height;
Size = new PixelSize(fb.Width, fb.Height);
RowBytes = fb.Stride;
Dpi = new Vector(fb.Dpi.X, fb.Dpi.Y);
Format = (PixelFormat)fb.PixelFormat;
}
public IntPtr Address { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public PixelSize Size { get; set; }
public int RowBytes {get;set;}
public Vector Dpi { get; set; }
public PixelFormat Format { get; }
Expand Down
31 changes: 15 additions & 16 deletions src/Avalonia.Visuals/Media/Imaging/Bitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,29 @@ public virtual void Dispose()
{
PlatformImpl.Dispose();
}

/// <summary>
/// Initializes a new instance of the <see cref="Bitmap"/> class.
/// </summary>
/// <param name="format">Pixel format</param>
/// <param name="data">Pointer to source bytes</param>
/// <param name="width">Bitmap width</param>
/// <param name="height">Bitmap height</param>
/// <param name="stride">Bytes per row</param>
public Bitmap(PixelFormat format, IntPtr data, int width, int height, int stride)
/// <param name="format">The pixel format.</param>
/// <param name="data">The pointer to the source bytes.</param>
/// <param name="size">The size of the bitmap in device pixels.</param>
/// <param name="dpi">The DPI of the bitmap.</param>
/// <param name="stride">The number of bytes per row.</param>
public Bitmap(PixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride)
{
PlatformImpl = RefCountable.Create(AvaloniaLocator.Current.GetService<IPlatformRenderInterface>()
.LoadBitmap(format, data, width, height, stride));
.LoadBitmap(format, data, size, dpi, stride));
}

/// <summary>
/// Gets the width of the bitmap, in pixels.
/// </summary>
public int PixelWidth => PlatformImpl.Item.PixelWidth;
/// <inheritdoc/>
public Vector Dpi => PlatformImpl.Item.Dpi;

/// <summary>
/// Gets the height of the bitmap, in pixels.
/// </summary>
public int PixelHeight => PlatformImpl.Item.PixelHeight;
/// <inheritdoc/>
public Size Size => PlatformImpl.Item.PixelSize.ToSize(Dpi);

/// <inheritdoc/>
public PixelSize PixelSize => PlatformImpl.Item.PixelSize;

/// <summary>
/// Gets the platform-specific bitmap implementation.
Expand Down
21 changes: 17 additions & 4 deletions src/Avalonia.Visuals/Media/Imaging/IBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,33 @@ namespace Avalonia.Media.Imaging
public interface IBitmap : IDisposable
{
/// <summary>
/// Gets the width of the bitmap, in pixels.
/// Gets the dots per inch (DPI) of the image.
/// </summary>
int PixelWidth { get; }
/// <remarks>
/// Note that Skia does not currently support reading the DPI of an image so this value
/// will always be 96dpi on Skia.
/// </remarks>
Vector Dpi { get; }

/// <summary>
/// Gets the height of the bitmap, in pixels.
/// Gets the size of the bitmap, in device pixels.
/// </summary>
int PixelHeight { get; }
PixelSize PixelSize { get; }

/// <summary>
/// Gets the platform-specific bitmap implementation.
/// </summary>
IRef<IBitmapImpl> PlatformImpl { get; }

/// <summary>
/// Gets the size of the image, in device independent pixels.
/// </summary>
/// <remarks>
/// Note that Skia does not currently support reading the DPI of an image so this value
/// will equal <see cref="PixelSize"/> on Skia.
/// </remarks>
Size Size { get; }

/// <summary>
/// Saves the bitmap to a file.
/// </summary>
Expand Down
29 changes: 17 additions & 12 deletions src/Avalonia.Visuals/Media/Imaging/RenderTargetBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@ public class RenderTargetBitmap : Bitmap, IDisposable, IRenderTarget
/// <summary>
/// Initializes a new instance of the <see cref="RenderTargetBitmap"/> class.
/// </summary>
/// <param name="pixelWidth">The width of the bitmap in pixels.</param>
/// <param name="pixelHeight">The height of the bitmap in pixels.</param>
/// <param name="dpiX">The horizontal DPI of the bitmap.</param>
/// <param name="dpiY">The vertical DPI of the bitmap.</param>
public RenderTargetBitmap(int pixelWidth, int pixelHeight, double dpiX = 96, double dpiY = 96)
: this(RefCountable.Create(CreateImpl(pixelWidth, pixelHeight, dpiX, dpiY)))
/// <param name="pixelSize">The size of the bitmap.</param>
public RenderTargetBitmap(PixelSize pixelSize)
: this(pixelSize, new Vector(96, 96))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="RenderTargetBitmap"/> class.
/// </summary>
/// <param name="pixelSize">The size of the bitmap in device pixels.</param>
/// <param name="dpi">The DPI of the bitmap.</param>
public RenderTargetBitmap(PixelSize pixelSize, Vector dpi)
: this(RefCountable.Create(CreateImpl(pixelSize, dpi)))
{
}

Expand All @@ -45,15 +52,13 @@ private RenderTargetBitmap(IRef<IRenderTargetBitmapImpl> impl) : base(impl)
/// <summary>
/// Creates a platform-specific implementation for a <see cref="RenderTargetBitmap"/>.
/// </summary>
/// <param name="width">The width of the bitmap.</param>
/// <param name="height">The height of the bitmap.</param>
/// <param name="dpiX">The horizontal DPI of the bitmap.</param>
/// <param name="dpiY">The vertical DPI of the bitmap.</param>
/// <param name="size">The size of the bitmap in device pixels.</param>
/// <param name="dpi">The DPI of the bitmap.</param>
/// <returns>The platform-specific implementation.</returns>
private static IRenderTargetBitmapImpl CreateImpl(int width, int height, double dpiX, double dpiY)
private static IRenderTargetBitmapImpl CreateImpl(PixelSize size, Vector dpi)
{
IPlatformRenderInterface factory = AvaloniaLocator.Current.GetService<IPlatformRenderInterface>();
return factory.CreateRenderTargetBitmap(width, height, dpiX, dpiY);
return factory.CreateRenderTargetBitmap(size, dpi);
}

/// <inheritdoc/>
Expand Down
11 changes: 9 additions & 2 deletions src/Avalonia.Visuals/Media/Imaging/WriteableBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ namespace Avalonia.Media.Imaging
/// </summary>
public class WriteableBitmap : Bitmap
{
public WriteableBitmap(int width, int height, PixelFormat? format = null)
: base(AvaloniaLocator.Current.GetService<IPlatformRenderInterface>().CreateWriteableBitmap(width, height, format))
/// <summary>
/// Initializes a new instance of the <see cref="WriteableBitmap"/> class.
/// </summary>
/// <param name="size">The size of the bitmap in device pixels.</param>
/// <param name="dpi">The DPI of the bitmap.</param>
/// <param name="format">The pixel format (optional).</param>
/// <returns>An <see cref="IWriteableBitmapImpl"/>.</returns>
public WriteableBitmap(PixelSize size, Vector dpi, PixelFormat? format = null)
: base(AvaloniaLocator.Current.GetService<IPlatformRenderInterface>().CreateWriteableBitmap(size, dpi, format))
{
}

Expand Down
Loading