BitmapTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using Avalonia.Controls;
  7. using Avalonia.Controls.Platform.Surfaces;
  8. using Avalonia.Controls.Shapes;
  9. using Avalonia.Layout;
  10. using Avalonia.Media;
  11. using Avalonia.Media.Imaging;
  12. using Avalonia.Platform;
  13. using Xunit;
  14. #if AVALONIA_SKIA
  15. namespace Avalonia.Skia.RenderTests
  16. #else
  17. namespace Avalonia.Direct2D1.RenderTests.Media
  18. #endif
  19. {
  20. public class BitmapTests : TestBase
  21. {
  22. public BitmapTests()
  23. : base(@"Media\Bitmap")
  24. {
  25. Directory.CreateDirectory(OutputPath);
  26. }
  27. class Framebuffer : ILockedFramebuffer, IFramebufferPlatformSurface
  28. {
  29. public Framebuffer(PixelFormat fmt, int width, int height)
  30. {
  31. Format = fmt;
  32. var bpp = fmt == PixelFormat.Rgb565 ? 2 : 4;
  33. Width = width;
  34. Height = height;
  35. RowBytes = bpp * width;
  36. Address = Marshal.AllocHGlobal(Height * RowBytes);
  37. }
  38. public IntPtr Address { get; }
  39. public Vector Dpi { get; } = new Vector(96, 96);
  40. public PixelFormat Format { get; }
  41. public int Height { get; }
  42. public int RowBytes { get; }
  43. public int Width { get; }
  44. public void Dispose()
  45. {
  46. //no-op
  47. }
  48. public ILockedFramebuffer Lock()
  49. {
  50. return this;
  51. }
  52. public void Deallocate() => Marshal.FreeHGlobal(Address);
  53. }
  54. #if AVALONIA_SKIA
  55. [Theory]
  56. #else
  57. [Theory(Skip = "Framebuffer not supported")]
  58. #endif
  59. [InlineData(PixelFormat.Rgba8888), InlineData(PixelFormat.Bgra8888), InlineData(PixelFormat.Rgb565)]
  60. public void FramebufferRenderResultsShouldBeUsableAsBitmap(PixelFormat fmt)
  61. {
  62. var testName = nameof(FramebufferRenderResultsShouldBeUsableAsBitmap) + "_" + fmt;
  63. var fb = new Framebuffer(fmt, 80, 80);
  64. var r = Avalonia.AvaloniaLocator.Current.GetService<IPlatformRenderInterface>();
  65. using (var target = r.CreateRenderTarget(new object[] { fb }))
  66. using (var ctx = target.CreateDrawingContext(null))
  67. {
  68. ctx.Clear(Colors.Transparent);
  69. ctx.PushOpacity(0.8);
  70. ctx.FillRectangle(Brushes.Chartreuse, new Rect(0, 0, 20, 100));
  71. ctx.FillRectangle(Brushes.Crimson, new Rect(20, 0, 20, 100));
  72. ctx.FillRectangle(Brushes.Gold, new Rect(40, 0, 20, 100));
  73. }
  74. var bmp = new Bitmap(fmt, fb.Address, fb.Width, fb.Height, fb.RowBytes);
  75. fb.Deallocate();
  76. using (var rtb = new RenderTargetBitmap(100, 100))
  77. {
  78. using (var ctx = rtb.CreateDrawingContext(null))
  79. {
  80. ctx.FillRectangle(Brushes.Blue, new Rect(0, 0, 100, 100));
  81. ctx.FillRectangle(Brushes.Pink, new Rect(0, 20, 100, 10));
  82. var rc = new Rect(0, 0, 60, 60);
  83. ctx.DrawImage(bmp.PlatformImpl, 1, rc, rc);
  84. }
  85. rtb.Save(System.IO.Path.Combine(OutputPath, testName + ".out.png"));
  86. }
  87. CompareImagesNoRenderer(testName);
  88. }
  89. [Theory]
  90. [InlineData(PixelFormat.Bgra8888), InlineData(PixelFormat.Rgba8888)]
  91. public void WritableBitmapShouldBeUsable(PixelFormat fmt)
  92. {
  93. var writableBitmap = new WritableBitmap(256, 256, fmt);
  94. var data = new int[256 * 256];
  95. for (int y = 0; y < 256; y++)
  96. for (int x = 0; x < 256; x++)
  97. data[y * 256 + x] =(int)((uint)(x + (y << 8)) | 0xFF000000u);
  98. using (var l = writableBitmap.Lock())
  99. {
  100. for(var r = 0; r<256; r++)
  101. {
  102. Marshal.Copy(data, r * 256, new IntPtr(l.Address.ToInt64() + r * l.RowBytes), 256);
  103. }
  104. }
  105. var name = nameof(WritableBitmapShouldBeUsable) + "_" + fmt;
  106. writableBitmap.Save(System.IO.Path.Combine(OutputPath, name + ".out.png"));
  107. CompareImagesNoRenderer(name);
  108. }
  109. }
  110. }