|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
| 7 | +namespace System.Windows.Media.Imaging; |
| 8 | + |
| 9 | +[Collection("WriteableBitmapTests")] |
| 10 | +public sealed class WriteableBitmapTests |
| 11 | +{ |
| 12 | + // Under 2GB back-buffer (4 channels) |
| 13 | + [InlineData(128, 128, 96.0, 96.0)] |
| 14 | + [InlineData(256, 512, 96.0, 96.0)] |
| 15 | + [InlineData(256, 256, 120.0, 120.0)] |
| 16 | + [InlineData(512, 256, 120.0, 120.0)] |
| 17 | + [InlineData(10_000, 10_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 18 | + [InlineData(20_000, 20_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 19 | + // Over 2GB back-buffer (4 channels) -- NOTE: These tests shall not be run on x86 without PAE |
| 20 | + [InlineData(25_000, 25_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 21 | + [InlineData(30_000, 30_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 22 | + [InlineData(32_000, 32_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 23 | + [Theory] |
| 24 | + public void Constructor_CreationSucceeds_HasCorrectParameters(int width, int height, double dpiX, double dpiY) |
| 25 | + { |
| 26 | + WriteableBitmap writeableBitmap = new(width, height, dpiX, dpiY, PixelFormats.Pbgra32, null); |
| 27 | + |
| 28 | + // Assert |
| 29 | + Assert.Equal(width, writeableBitmap.PixelWidth); |
| 30 | + Assert.Equal(height, writeableBitmap.PixelHeight); |
| 31 | + |
| 32 | + Assert.Equal(dpiX, writeableBitmap.DpiX); |
| 33 | + Assert.Equal(dpiY, writeableBitmap.DpiY); |
| 34 | + |
| 35 | + Assert.Equal(PixelFormats.Pbgra32, writeableBitmap.Format); |
| 36 | + } |
| 37 | + |
| 38 | + // Under 2GB back-buffer (4 channels) |
| 39 | + [InlineData(2_000, 2_000, 96.0, 96.0)] |
| 40 | + [InlineData(4_000, 4_000, 120, 120)] |
| 41 | + [InlineData(10_000, 10_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 42 | + [InlineData(20_000, 20_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 43 | + // Over 2GB back-buffer (4 channels) -- NOTE: These tests shall not be run on x86 without PAE |
| 44 | + [InlineData(25_000, 25_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 45 | + [InlineData(32_000, 32_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 46 | + [Theory] |
| 47 | + public void WritePixels_SmallRect_Safe_Succeeds(int width, int height, double dpiX, double dpiY) |
| 48 | + { |
| 49 | + const int tileSize = 500; |
| 50 | + const int channels = 4; |
| 51 | + |
| 52 | + // Create 1000x1000 rectangle with 4 channels, fill the rectangle with teal color |
| 53 | + byte[] smallRect = GC.AllocateUninitializedArray<byte>(tileSize * tileSize * channels); |
| 54 | + MemoryMarshal.Cast<byte, uint>(smallRect.AsSpan()).Fill(0xFF00E6FF); |
| 55 | + |
| 56 | + WriteableBitmap writeableBitmap = new(width, height, dpiX, dpiY, PixelFormats.Pbgra32, null); |
| 57 | + |
| 58 | + // Top-Left |
| 59 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), |
| 60 | + smallRect, tileSize * channels, 0, 0); |
| 61 | + |
| 62 | + // Top-Right |
| 63 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), |
| 64 | + smallRect, tileSize * channels, width - tileSize, 0); |
| 65 | + |
| 66 | + // Middle Rect |
| 67 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), |
| 68 | + smallRect, tileSize * channels, (width - tileSize) / 2, (height - tileSize) / 2); |
| 69 | + |
| 70 | + // Bottom-Left |
| 71 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), |
| 72 | + smallRect, tileSize * channels, 0, height - tileSize); |
| 73 | + |
| 74 | + // Bottom-Right |
| 75 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), |
| 76 | + smallRect, tileSize * channels, width - tileSize, height - tileSize); |
| 77 | + } |
| 78 | + |
| 79 | + // Under 2GB back-buffer (4 channels) |
| 80 | + [InlineData(2_000, 2_000, 96.0, 96.0)] |
| 81 | + [InlineData(4_000, 4_000, 120, 120)] |
| 82 | + [InlineData(10_000, 10_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 83 | + [InlineData(20_000, 20_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 84 | + // Over 2GB back-buffer (4 channels) -- NOTE: These tests shall not be run on x86 without PAE |
| 85 | + [InlineData(25_000, 25_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 86 | + [InlineData(32_000, 32_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 87 | + [Theory] |
| 88 | + public unsafe void WritePixels_SmallRect_Unsafe_Succeeds(int width, int height, double dpiX, double dpiY) |
| 89 | + { |
| 90 | + const int tileSize = 500; |
| 91 | + const int channels = 4; |
| 92 | + |
| 93 | + // Create 1000x1000 rectangle with 4 channels, fill the rectangle with teal color |
| 94 | + Span<byte> smallRect = GC.AllocateUninitializedArray<byte>(tileSize * tileSize * channels, pinned: true); |
| 95 | + MemoryMarshal.Cast<byte, uint>(smallRect).Fill(0xFF00E6FF); |
| 96 | + |
| 97 | + WriteableBitmap writeableBitmap = new(width, height, dpiX, dpiY, PixelFormats.Pbgra32, null); |
| 98 | + |
| 99 | + // Top-Left |
| 100 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), smallRect.AsNativePointer(), |
| 101 | + smallRect.Length, tileSize * channels, 0, 0); |
| 102 | + |
| 103 | + // Top-Right |
| 104 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), smallRect.AsNativePointer(), |
| 105 | + smallRect.Length, tileSize * channels, width - tileSize, 0); |
| 106 | + |
| 107 | + // Middle Rect |
| 108 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), smallRect.AsNativePointer(), |
| 109 | + smallRect.Length, tileSize * channels, (width - tileSize) / 2, (height - tileSize) / 2); |
| 110 | + |
| 111 | + // Bottom-Left |
| 112 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), smallRect.AsNativePointer(), |
| 113 | + smallRect.Length, tileSize * channels, 0, height - tileSize); |
| 114 | + |
| 115 | + // Bottom-Right |
| 116 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, tileSize, tileSize), smallRect.AsNativePointer(), |
| 117 | + smallRect.Length, tileSize * channels, width - tileSize, height - tileSize); |
| 118 | + } |
| 119 | + |
| 120 | + // Under 2GB back-buffer (4 channels) |
| 121 | + [InlineData(512, 512, 96.0, 96.0)] |
| 122 | + [InlineData(4_000, 4_000, 120, 120)] |
| 123 | + [InlineData(10_000, 10_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 124 | + [InlineData(20_000, 20_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 125 | + // Over 2GB back-buffer (4 channels) -- NOTE: These tests shall not be run on x86 without PAE |
| 126 | + [InlineData(25_000, 25_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 127 | + [InlineData(32_000, 32_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 128 | + [Theory] |
| 129 | + public void WritePixels_FullRect_Safe_Succeeds(int width, int height, double dpiX, double dpiY) |
| 130 | + { |
| 131 | + const int channels = 4; |
| 132 | + |
| 133 | + // Create same-sized rectangle with 4 channels, fill the rectangle with teal color |
| 134 | + // NOTE: We use uint[] over byte[] to avoid Array.MaxLength limit for single-dims on 2GB+ bitmaps |
| 135 | + uint[] bigRect = GC.AllocateUninitializedArray<uint>(width * height); |
| 136 | + Array.Fill(bigRect, 0xFF00E6FF); |
| 137 | + |
| 138 | + WriteableBitmap writeableBitmap = new(width, height, dpiX, dpiY, PixelFormats.Pbgra32, null); |
| 139 | + |
| 140 | + // Paint the full rect teal |
| 141 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, width, height), bigRect, width * channels, 0, 0); |
| 142 | + } |
| 143 | + |
| 144 | + // Under 2GB back-buffer (4 channels) |
| 145 | + [InlineData(512, 512, 96.0, 96.0)] |
| 146 | + [InlineData(4_000, 4_000, 120, 120)] |
| 147 | + [InlineData(10_000, 10_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 148 | + [InlineData(20_000, 20_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 149 | + // Over 2GB back-buffer (4 channels) -- NOTE: These tests shall not be run on x86 without PAE |
| 150 | + [InlineData(25_000, 25_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 151 | + [InlineData(32_000, 32_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 152 | + [Theory] |
| 153 | + public unsafe void WritePixels_FullRect_Unsafe_Succeeds(int width, int height, double dpiX, double dpiY) |
| 154 | + { |
| 155 | + const int channels = 4; |
| 156 | + |
| 157 | + // Create same-sized rectangle with 4 channels, fill the rectangle with teal color |
| 158 | + // NOTE: We use uint[] over byte[] to avoid Array.MaxLength limit for single-dims on 2GB+ bitmaps |
| 159 | + Span<uint> bigRect = GC.AllocateUninitializedArray<uint>(width * height, pinned: true); |
| 160 | + bigRect.Fill(0xFF00E6FF); |
| 161 | + |
| 162 | + WriteableBitmap writeableBitmap = new(width, height, dpiX, dpiY, PixelFormats.Pbgra32, null); |
| 163 | + |
| 164 | + // Paint the full rect teal |
| 165 | + writeableBitmap.WritePixels(new Int32Rect(0, 0, width, height), bigRect.AsNativePointer(), |
| 166 | + bigRect.Length * channels, width * channels, 0, 0); |
| 167 | + } |
| 168 | + |
| 169 | + // Under 2GB back-buffer (4 channels) |
| 170 | + [InlineData(128, 128, 96.0, 96.0)] |
| 171 | + [InlineData(256, 512, 96.0, 96.0)] |
| 172 | + [InlineData(256, 256, 120.0, 120.0)] |
| 173 | + [InlineData(512, 256, 120.0, 120.0)] |
| 174 | + [InlineData(10_000, 10_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 175 | + [InlineData(20_000, 20_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 176 | + // Over 2GB back-buffer (4 channels) -- NOTE: These tests shall not be run on x86 without PAE |
| 177 | + [InlineData(25_000, 25_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 178 | + [InlineData(32_000, 32_000, 96.0, 96.0, Skip = "Disabled to reduce working set")] |
| 179 | + [Theory] |
| 180 | + public void Clone_CopyPixels_Succeeds(int width, int height, double dpiX, double dpiY) |
| 181 | + { |
| 182 | + WriteableBitmap writeableBitmap = new(width, height, dpiX, dpiY, PixelFormats.Pbgra32, null); |
| 183 | + |
| 184 | + // Invoke bitmap copy |
| 185 | + BitmapSource bitmapSource = writeableBitmap.Clone(); |
| 186 | + |
| 187 | + // Must succeed |
| 188 | + Assert.NotNull(bitmapSource); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +public static unsafe class SpanExtensions |
| 193 | +{ |
| 194 | + /// <summary>Retrieves the data pointer of the underlying <see cref="Span{T}"/> data reference.</summary> |
| 195 | + /// <param name="span">The <see cref="Span{T}"/> to retrieve a pointer to.</param> |
| 196 | + /// <returns>A <see cref="nuint"/> pointer of the underlying data reference.</returns> |
| 197 | + /// <remarks>The pointer reference is not pinned, use only on <see langword="fixed"/> buffers or <see langword="stackalloc"/> pointers.</remarks> |
| 198 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 199 | + public static nint AsNativePointer<T>(this Span<T> span) => (nint)Unsafe.AsPointer(ref MemoryMarshal.GetReference(span)); |
| 200 | + |
| 201 | +} |
0 commit comments