Browse Source

Implemented WritableBitmap

Nikita Tsukanov 8 years ago
parent
commit
90a4915549
27 changed files with 237 additions and 21 deletions
  1. 0 1
      src/Avalonia.Controls/Avalonia.Controls.csproj
  2. 1 0
      src/Avalonia.Controls/Platform/Surfaces/IFramebufferPlatformSurface.cs
  3. 3 0
      src/Avalonia.Visuals/Avalonia.Visuals.csproj
  4. 22 0
      src/Avalonia.Visuals/Media/Imaging/WritableBitmap.cs
  5. 1 2
      src/Avalonia.Visuals/Platform/ILockedFramebuffer.cs
  6. 9 0
      src/Avalonia.Visuals/Platform/IPlatformRenderInterface.cs
  7. 16 0
      src/Avalonia.Visuals/Platform/IWritableBitmapImpl.cs
  8. 5 0
      src/Gtk/Avalonia.Cairo/CairoPlatform.cs
  9. 1 0
      src/Gtk/Avalonia.Gtk/FramebufferManager.cs
  10. 1 0
      src/Gtk/Avalonia.Gtk3/FramebufferManager.cs
  11. 41 4
      src/Skia/Avalonia.Skia/BitmapImpl.cs
  12. 5 0
      src/Skia/Avalonia.Skia/PlatformRenderInterface.cs
  13. 11 0
      src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs
  14. 1 0
      src/Windows/Avalonia.Direct2D1/Avalonia.Direct2D1.csproj
  15. 6 0
      src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs
  16. 12 13
      src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs
  17. 47 0
      src/Windows/Avalonia.Direct2D1/Media/Imaging/WritableWicBitmapImpl.cs
  18. 11 0
      src/Windows/Avalonia.Direct2D1/PrimitiveExtensions.cs
  19. 1 0
      src/Windows/Avalonia.Win32/FramebufferManager.cs
  20. 1 0
      src/Windows/Avalonia.Win32/WindowFramebuffer.cs
  21. 5 0
      tests/Avalonia.Input.UnitTests/InputElement_HitTesting.cs
  22. 32 1
      tests/Avalonia.RenderTests/Media/BitmapTests.cs
  23. 5 0
      tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs
  24. BIN
      tests/TestFiles/Direct2D1/Media/Bitmap/WritableBitmapShouldBeUsable_Bgra8888.expected.png
  25. BIN
      tests/TestFiles/Direct2D1/Media/Bitmap/WritableBitmapShouldBeUsable_Rgba8888.expected.png
  26. BIN
      tests/TestFiles/Skia/Media/Bitmap/WritableBitmapShouldBeUsable_Bgra8888.expected.png
  27. BIN
      tests/TestFiles/Skia/Media/Bitmap/WritableBitmapShouldBeUsable_Rgba8888.expected.png

+ 0 - 1
src/Avalonia.Controls/Avalonia.Controls.csproj

@@ -59,7 +59,6 @@
     <Compile Include="IScrollable.cs" />
     <Compile Include="Platform\IWindowBaseImpl.cs" />
     <Compile Include="Platform\Surfaces\IFramebufferPlatformSurface.cs" />
-    <Compile Include="Platform\Surfaces\ILockedFramebuffer.cs" />
     <Compile Include="PointEventArgs.cs" />
     <Compile Include="Embedding\EmbeddableControlRoot.cs" />
     <Compile Include="Platform\IEmbeddableWindowImpl.cs" />

+ 1 - 0
src/Avalonia.Controls/Platform/Surfaces/IFramebufferPlatformSurface.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using Avalonia.Platform;
 
 namespace Avalonia.Controls.Platform.Surfaces
 {

+ 3 - 0
src/Avalonia.Visuals/Avalonia.Visuals.csproj

@@ -69,6 +69,7 @@
     <Compile Include="Media\BrushMappingMode.cs" />
     <Compile Include="Media\Color.cs" />
     <Compile Include="Media\Colors.cs" />
+    <Compile Include="Media\Imaging\WritableBitmap.cs" />
     <Compile Include="Media\TextWrapping.cs" />
     <Compile Include="Media\TransformGroup.cs" />
     <Compile Include="Media\DashStyle.cs" />
@@ -102,7 +103,9 @@
     <Compile Include="Media\Geometry.cs" />
     <Compile Include="Media\IDrawingContext.cs" />
     <Compile Include="Platform\ExportRenderingSubsystemAttribute.cs" />
+    <Compile Include="Platform\ILockedFramebuffer.cs" />
     <Compile Include="Platform\IModuleEnvironmentChecker.cs" />
+    <Compile Include="Platform\IWritableBitmapImpl.cs" />
     <Compile Include="Platform\PixelFormat.cs" />
     <Compile Include="Rendering\IRenderer.cs" />
     <Compile Include="Rendering\IRendererFactory.cs" />

+ 22 - 0
src/Avalonia.Visuals/Media/Imaging/WritableBitmap.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Avalonia.Platform;
+
+namespace Avalonia.Media.Imaging
+{
+    /// <summary>
+    /// Holds a writable bitmap image.
+    /// </summary>
+    public class WritableBitmap : Bitmap
+    {
+        public WritableBitmap(int width, int height, PixelFormat? format = null) 
+            : base(AvaloniaLocator.Current.GetService<IPlatformRenderInterface>().CreateWritableBitmap(width, height, format))
+        {
+        }
+
+        public ILockedFramebuffer Lock() => ((IWritableBitmapImpl) PlatformImpl).Lock();
+    }
+}

+ 1 - 2
src/Avalonia.Controls/Platform/Surfaces/ILockedFramebuffer.cs → src/Avalonia.Visuals/Platform/ILockedFramebuffer.cs

@@ -1,7 +1,6 @@
 using System;
-using Avalonia.Platform;
 
-namespace Avalonia.Controls.Platform.Surfaces
+namespace Avalonia.Platform
 {
     public interface ILockedFramebuffer : IDisposable
     {

+ 9 - 0
src/Avalonia.Visuals/Platform/IPlatformRenderInterface.cs

@@ -56,6 +56,15 @@ namespace Avalonia.Platform
         /// <returns>An <see cref="IRenderTargetBitmapImpl"/>.</returns>
         IRenderTargetBitmapImpl CreateRenderTargetBitmap(int width, int height);
 
+        /// <summary>
+        /// Creates a writable bitmap implementation.
+        /// </summary>
+        /// <param name="width">The width of the bitmap.</param>
+        /// <param name="height">The height of the bitmap.</param>
+        /// <param name="format">Pixel format (optional).</param>
+        /// <returns>An <see cref="IWritableBitmapImpl"/>.</returns>
+        IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? format = null);
+
         /// <summary>
         /// Loads a bitmap implementation from a file..
         /// </summary>

+ 16 - 0
src/Avalonia.Visuals/Platform/IWritableBitmapImpl.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Avalonia.Platform
+{
+    /// <summary>
+    /// Defines the platform-specific interface for a <see cref="Avalonia.Media.Imaging.WritableBitmap"/>.
+    /// </summary>
+    public interface IWritableBitmapImpl : IBitmapImpl
+    {
+        ILockedFramebuffer Lock();
+    }
+}

+ 5 - 0
src/Gtk/Avalonia.Cairo/CairoPlatform.cs

@@ -96,5 +96,10 @@ namespace Avalonia.Cairo
         {
             throw new NotSupportedException("No proper control over pixel format with Cairo, use Skia backend instead");
         }
+
+        public IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? fmt)
+        {
+            throw new NotSupportedException("No proper support with Cairo, use Skia backend instead");
+        }
     }
 }

+ 1 - 0
src/Gtk/Avalonia.Gtk/FramebufferManager.cs

@@ -1,5 +1,6 @@
 using System;
 using Avalonia.Controls.Platform.Surfaces;
+using Avalonia.Platform;
 
 namespace Avalonia.Gtk
 {

+ 1 - 0
src/Gtk/Avalonia.Gtk3/FramebufferManager.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Avalonia.Controls.Platform.Surfaces;
+using Avalonia.Platform;
 
 namespace Avalonia.Gtk3
 {

+ 41 - 4
src/Skia/Avalonia.Skia/BitmapImpl.cs

@@ -9,7 +9,7 @@ using SkiaSharp;
 
 namespace Avalonia.Skia
 {
-    class BitmapImpl : IRenderTargetBitmapImpl
+    class BitmapImpl : IRenderTargetBitmapImpl, IWritableBitmapImpl
     {
         public SKBitmap Bitmap { get; private set; }
 
@@ -20,11 +20,11 @@ namespace Avalonia.Skia
             PixelWidth = bm.Width;
         }
 
-        public BitmapImpl(int width, int height)
+        public BitmapImpl(int width, int height, PixelFormat? fmt = null)
         {
             PixelHeight = height;
             PixelWidth = width;
-            var colorType = SKImageInfo.PlatformColorType;
+            var colorType = fmt?.ToSkColorType() ?? SKImageInfo.PlatformColorType;
             var runtime = AvaloniaLocator.Current?.GetService<IRuntimePlatform>()?.GetRuntimeInfo();
             if (runtime?.IsDesktop == true && runtime?.OperatingSystem == OperatingSystemType.Linux)
                 colorType = SKColorType.Bgra8888;
@@ -38,10 +38,21 @@ namespace Avalonia.Skia
 
         public void Save(string fileName)
         {
+            
 #if DESKTOP
+            if(Bitmap.ColorType != SKColorType.Bgra8888)
+            {
+                using (var tmp = new BitmapImpl(Bitmap.Copy(SKColorType.Bgra8888)))
+                    tmp.Save(fileName);
+                return;
+            }
+
             IntPtr length;
             using (var sdb = new System.Drawing.Bitmap(PixelWidth, PixelHeight, Bitmap.RowBytes,
-                System.Drawing.Imaging.PixelFormat.Format32bppArgb, Bitmap.GetPixels(out length)))
+                
+                System.Drawing.Imaging.PixelFormat.Format32bppArgb,
+                
+                Bitmap.GetPixels(out length)))
                 sdb.Save(fileName);
 #else
             //SkiaSharp doesn't expose image encoders yet
@@ -96,5 +107,31 @@ namespace Avalonia.Skia
                 data.SaveTo(stream);
             }
         }
+
+        class BitmapFramebuffer : ILockedFramebuffer
+        {
+            private SKBitmap _bmp;
+
+            public BitmapFramebuffer(SKBitmap bmp)
+            {
+                _bmp = bmp;
+                _bmp.LockPixels();
+            }
+
+            public void Dispose()
+            {
+                _bmp.UnlockPixels();
+                _bmp = null;
+            }
+
+            public IntPtr Address => _bmp.GetPixels();
+            public int Width => _bmp.Width;
+            public int Height => _bmp.Height;
+            public int RowBytes => _bmp.RowBytes;
+            public Size Dpi { get; } = new Size(96, 96);
+            public PixelFormat Format => _bmp.ColorType.ToPixelFormat();
+        }
+
+        public ILockedFramebuffer Lock() => new BitmapFramebuffer(Bitmap);
     }
 }

+ 5 - 0
src/Skia/Avalonia.Skia/PlatformRenderInterface.cs

@@ -84,5 +84,10 @@ namespace Avalonia.Skia
                 throw new Exception("Skia backend currently only supports framebuffer render target");
             return new FramebufferRenderTarget(fb);
         }
+
+        public IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? format = null)
+        {
+            return new BitmapImpl(width, height, format);
+        }
     }
 }

+ 11 - 0
src/Skia/Avalonia.Skia/SkiaSharpExtensions.cs

@@ -57,6 +57,17 @@ namespace Avalonia.Skia
             throw new ArgumentException("Unknown pixel format: " + fmt);
         }
 
+        public static PixelFormat ToPixelFormat(this SKColorType fmt)
+        {
+            if (fmt == SKColorType.Rgb565)
+                return PixelFormat.Rgb565;
+            if (fmt == SKColorType.Bgra8888)
+                return PixelFormat.Bgra8888;
+            if (fmt == SKColorType.Rgba8888)
+                return PixelFormat.Rgba8888;
+            throw new ArgumentException("Unknown pixel format: " + fmt);
+        }
+
         public static SKShaderTileMode ToSKShaderTileMode(this Media.GradientSpreadMethod m)
         {
             switch (m)

+ 1 - 0
src/Windows/Avalonia.Direct2D1/Avalonia.Direct2D1.csproj

@@ -70,6 +70,7 @@
     <Compile Include="Media\Imaging\D2DBitmapImpl.cs" />
     <Compile Include="Media\Imaging\RenderTargetBitmapImpl.cs" />
     <Compile Include="Media\Imaging\WicBitmapImpl.cs" />
+    <Compile Include="Media\Imaging\WritableWicBitmapImpl.cs" />
     <Compile Include="Media\RadialGradientBrushImpl.cs" />
     <Compile Include="Media\LinearGradientBrushImpl.cs" />
     <Compile Include="Media\AvaloniaTextRenderer.cs" />

+ 6 - 0
src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs

@@ -10,6 +10,7 @@ using Avalonia.Media;
 using Avalonia.Platform;
 using Avalonia.Controls;
 using Avalonia.Controls.Platform.Surfaces;
+using Avalonia.Direct2D1.Media.Imaging;
 using Avalonia.Rendering;
 
 namespace Avalonia
@@ -119,6 +120,11 @@ namespace Avalonia.Direct2D1
             return new RenderTargetBitmapImpl(s_imagingFactory, s_d2D1Device.Factory, width, height);
         }
 
+        public IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? format = null)
+        {
+            return new WritableWicBitmapImpl(s_imagingFactory, width, height, format);
+        }
+
         public IStreamGeometryImpl CreateStreamGeometry()
         {
             return new StreamGeometryImpl();

+ 12 - 13
src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs

@@ -56,29 +56,26 @@ namespace Avalonia.Direct2D1.Media
         /// <param name="factory">The WIC imaging factory to use.</param>
         /// <param name="width">The width of the bitmap.</param>
         /// <param name="height">The height of the bitmap.</param>
-        public WicBitmapImpl(ImagingFactory factory, int width, int height)
+        /// <param name="pixelFormat">Pixel format</param>
+        public WicBitmapImpl(ImagingFactory factory, int width, int height, APixelFormat? pixelFormat = null)
         {
+            if (!pixelFormat.HasValue)
+                pixelFormat = APixelFormat.Rgba8888;
+
             _factory = factory;
+            PixelFormat = pixelFormat;
             WicImpl = new Bitmap(
                 factory,
                 width,
                 height,
-                PixelFormat.Format32bppPBGRA,
+                pixelFormat.Value.ToWic(),
                 BitmapCreateCacheOption.CacheOnLoad);
         }
 
         public WicBitmapImpl(ImagingFactory factory, Platform.PixelFormat format, IntPtr data, int width, int height, int stride)
         {
-            Guid fmt;
-            if (format == APixelFormat.Rgb565)
-                fmt = PixelFormat.Format16bppBGR565;
-            else if (format == APixelFormat.Bgra8888)
-                fmt = PixelFormat.Format32bppPBGRA;
-            else if (format == APixelFormat.Rgba8888)
-                fmt = PixelFormat.Format32bppPRGBA;
-            else throw new ArgumentException("Unknown pixel format");
-
-            WicImpl = new Bitmap(factory, width, height, fmt, BitmapCreateCacheOption.CacheOnDemand);
+            WicImpl = new Bitmap(factory, width, height, format.ToWic(), BitmapCreateCacheOption.CacheOnDemand);
+            PixelFormat = format;
             using (var l = WicImpl.Lock(BitmapLockFlags.Write))
             {
                 for (var row = 0; row < height; row++)
@@ -89,6 +86,8 @@ namespace Avalonia.Direct2D1.Media
             }
         }
 
+        protected APixelFormat? PixelFormat { get; }
+
         /// <summary>
         /// Gets the width of the bitmap, in pixels.
         /// </summary>
@@ -120,7 +119,7 @@ namespace Avalonia.Direct2D1.Media
             if (_direct2D == null)
             {
                 FormatConverter converter = new FormatConverter(_factory);
-                converter.Initialize(WicImpl, PixelFormat.Format32bppPBGRA);
+                converter.Initialize(WicImpl, SharpDX.WIC.PixelFormat.Format32bppPBGRA);
                 _direct2D = SharpDX.Direct2D1.Bitmap.FromWicBitmap(renderTarget, converter);
             }
 

+ 47 - 0
src/Windows/Avalonia.Direct2D1/Media/Imaging/WritableWicBitmapImpl.cs

@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Avalonia.Platform;
+using SharpDX.WIC;
+using PixelFormat = Avalonia.Platform.PixelFormat;
+
+namespace Avalonia.Direct2D1.Media.Imaging
+{
+    class WritableWicBitmapImpl : WicBitmapImpl, IWritableBitmapImpl
+    {
+        public WritableWicBitmapImpl(ImagingFactory factory, int width, int height, PixelFormat? pixelFormat) 
+            : base(factory, width, height, pixelFormat)
+        {
+        }
+
+        class LockedBitmap : ILockedFramebuffer
+        {
+            private readonly BitmapLock _lock;
+            private readonly PixelFormat _format;
+
+            public LockedBitmap(BitmapLock l, PixelFormat format)
+            {
+                _lock = l;
+                _format = format;
+            }
+
+
+            public void Dispose()
+            {
+                _lock.Dispose();
+            }
+
+            public IntPtr Address => _lock.Data.DataPointer;
+            public int Width => _lock.Size.Width;
+            public int Height => _lock.Size.Height;
+            public int RowBytes => _lock.Stride;
+            public Size Dpi { get; } = new Size(96, 96);
+            public PixelFormat Format => _format;
+
+        }
+
+        public ILockedFramebuffer Lock() => new LockedBitmap(WicImpl.Lock(BitmapLockFlags.Write), PixelFormat.Value);
+    }
+}

+ 11 - 0
src/Windows/Avalonia.Direct2D1/PrimitiveExtensions.cs

@@ -88,6 +88,17 @@ namespace Avalonia.Direct2D1
                 return CapStyle.Triangle;
         }
 
+        public static Guid ToWic(this Platform.PixelFormat format)
+        {
+            if (format == Platform.PixelFormat.Rgb565)
+                return SharpDX.WIC.PixelFormat.Format16bppBGR565;
+            if (format == Platform.PixelFormat.Bgra8888)
+                return SharpDX.WIC.PixelFormat.Format32bppPBGRA;
+            if (format == Platform.PixelFormat.Rgba8888)
+                return SharpDX.WIC.PixelFormat.Format32bppPRGBA;
+            throw new ArgumentException("Unknown pixel format");
+        }
+
         /// <summary>
         /// Converts a pen to a Direct2D stroke style.
         /// </summary>

+ 1 - 0
src/Windows/Avalonia.Win32/FramebufferManager.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Avalonia.Controls.Platform.Surfaces;
+using Avalonia.Platform;
 using Avalonia.Win32.Interop;
 
 namespace Avalonia.Win32

+ 1 - 0
src/Windows/Avalonia.Win32/WindowFramebuffer.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Runtime.InteropServices;
 using Avalonia.Controls.Platform.Surfaces;
+using Avalonia.Platform;
 using Avalonia.Win32.Interop;
 using PixelFormat = Avalonia.Platform.PixelFormat;
 

+ 5 - 0
tests/Avalonia.Input.UnitTests/InputElement_HitTesting.cs

@@ -370,6 +370,11 @@ namespace Avalonia.Input.UnitTests
                 throw new NotImplementedException();
             }
 
+            public IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? fmt)
+            {
+                throw new NotImplementedException();
+            }
+
             class MockStreamGeometry : Avalonia.Platform.IStreamGeometryImpl
             {
                 private MockStreamGeometryContext _impl = new MockStreamGeometryContext();

+ 32 - 1
tests/Avalonia.RenderTests/Media/BitmapTests.cs

@@ -102,7 +102,38 @@ namespace Avalonia.Direct2D1.RenderTests.Media
                 rtb.Save(System.IO.Path.Combine(OutputPath, testName + ".out.png"));
             }
             CompareImages(testName);
-            
+        }
+
+#if AVALONIA_CAIRO
+        //wontfix
+#else
+        [Theory]
+#endif
+        [InlineData(PixelFormat.Bgra8888), InlineData(PixelFormat.Rgba8888)]
+        public void WritableBitmapShouldBeUsable(PixelFormat fmt)
+        {
+            var writableBitmap = new WritableBitmap(256, 256, fmt);
+
+            var data = new int[256 * 256];
+            for (int y = 0; y < 256; y++)
+                for (int x = 0; x < 256; x++)
+                    data[y * 256 + x] =(int)((uint)(x + (y << 8)) | 0xFF000000u);
+
+
+            using (var l = writableBitmap.Lock())
+            {
+                for(var r = 0; r<256; r++)
+                {
+                    Marshal.Copy(data, r * 256, new IntPtr(l.Address.ToInt64() + r * l.RowBytes), 256);
+                }
+            }
+
+
+            var name = nameof(WritableBitmapShouldBeUsable) + "_" + fmt;
+
+            writableBitmap.Save(System.IO.Path.Combine(OutputPath, name + ".out.png"));
+            CompareImages(name);
+
         }
     }
 }

+ 5 - 0
tests/Avalonia.Visuals.UnitTests/VisualTree/MockRenderInterface.cs

@@ -50,6 +50,11 @@ namespace Avalonia.Visuals.UnitTests.VisualTree
             throw new NotImplementedException();
         }
 
+        public IWritableBitmapImpl CreateWritableBitmap(int width, int height, PixelFormat? fmt)
+        {
+            throw new NotImplementedException();
+        }
+
         class MockStreamGeometry : IStreamGeometryImpl
         {
             private MockStreamGeometryContext _impl = new MockStreamGeometryContext();

BIN
tests/TestFiles/Direct2D1/Media/Bitmap/WritableBitmapShouldBeUsable_Bgra8888.expected.png


BIN
tests/TestFiles/Direct2D1/Media/Bitmap/WritableBitmapShouldBeUsable_Rgba8888.expected.png


BIN
tests/TestFiles/Skia/Media/Bitmap/WritableBitmapShouldBeUsable_Bgra8888.expected.png


BIN
tests/TestFiles/Skia/Media/Bitmap/WritableBitmapShouldBeUsable_Rgba8888.expected.png