Skip to content

Commit 44a669c

Browse files
authored
Merge pull request #37 from DarthAffe/v3
V3
2 parents ae6cf5c + f314a41 commit 44a669c

33 files changed

+223
-2790
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ using (fullscreen.Lock())
5757
IColor imageColorExample = image[10, 20];
5858

5959
// Get the first row
60-
IImage.IImageRow row = image.Rows[0];
60+
IImageRow row = image.Rows[0];
6161
// Get the 10th pixel of the row
6262
IColor rowColorExample = row[10];
6363

6464
// Get the first column
65-
IImage.IImageColumn column = image.Columns[0];
65+
IImageColumn column = image.Columns[0];
6666
// Get the 10th pixel of the column
6767
IColor columnColorExample = column[10];
6868

@@ -73,7 +73,7 @@ using (fullscreen.Lock())
7373
}
7474
```
7575

76-
IF you know which Capture-provider you're using it performs a bit better to not use the abstraction but a more low-level approach instead.
76+
If you know which Capture-provider you're using it performs a bit better to not use the abstraction but a more low-level approach instead.
7777
This is the same example as above but without using the interfaces:
7878
```csharp
7979
DX11ScreenCaptureService screenCaptureService = new DX11ScreenCaptureService();
@@ -88,17 +88,20 @@ screenCapture.CaptureScreen();
8888

8989
using (fullscreen.Lock())
9090
{
91-
RefImage<ColorBGRA> image = fullscreen.Image;
91+
IImage<ColorBGRA> image = fullscreen.Image;
92+
93+
// You can also get a ref image which has a slight performance benefit in some cases
94+
// RefImage<ColorBGRA> refImage = image.AsRefImage();
9295
9396
foreach (ColorBGRA color in image)
9497
Console.WriteLine($"A: {color.A}, R: {color.R}, G: {color.G}, B: {color.B}");
9598

9699
ColorBGRA imageColorExample = image[10, 20];
97100

98-
ReadOnlyRefEnumerable<ColorBGRA> row = image.Rows[0];
101+
ImageRow<ColorBGRA> row = image.Rows[0];
99102
ColorBGRA rowColorExample = row[10];
100103

101-
ReadOnlyRefEnumerable<ColorBGRA> column = image.Columns[0];
104+
ImageColumn<ColorBGRA> column = image.Columns[0];
102105
ColorBGRA columnColorExample = column[10];
103106

104107
RefImage<ColorBGRA> subImage = image[100, 150, 400, 300];

ScreenCapture.NET.DX11/DX11ScreenCapture.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Runtime.CompilerServices;
44
using System.Runtime.InteropServices;
55
using System.Threading;
6+
using HPPH;
67
using SharpGen.Runtime;
78
using Vortice.Direct3D;
89
using Vortice.Direct3D11;
@@ -23,12 +24,12 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
2324
#region Constants
2425

2526
private static readonly FeatureLevel[] FEATURE_LEVELS =
26-
{
27+
[
2728
FeatureLevel.Level_11_1,
2829
FeatureLevel.Level_11_0,
2930
FeatureLevel.Level_10_1,
3031
FeatureLevel.Level_10_0
31-
};
32+
];
3233

3334
#endregion
3435

@@ -53,7 +54,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
5354
private ID3D11DeviceContext? _context;
5455
private ID3D11Texture2D? _captureTexture;
5556

56-
private readonly Dictionary<CaptureZone<ColorBGRA>, ZoneTextures> _textures = new();
57+
private readonly Dictionary<CaptureZone<ColorBGRA>, ZoneTextures> _textures = [];
5758

5859
#endregion
5960

@@ -135,7 +136,7 @@ protected override bool PerformScreenCapture()
135136
}
136137

137138
/// <inheritdoc />
138-
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
139+
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
139140
{
140141
if (_context == null) return;
141142

@@ -188,7 +189,7 @@ protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZ
188189
}
189190

190191
[MethodImpl(MethodImplOptions.AggressiveInlining)]
191-
private static void CopyRotate0(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
192+
private static void CopyRotate0(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
192193
{
193194
int height = captureZone.Height;
194195
int stride = captureZone.Stride;
@@ -204,7 +205,7 @@ private static void CopyRotate0(in ReadOnlySpan<byte> source, int sourceStride,
204205
}
205206

206207
[MethodImpl(MethodImplOptions.AggressiveInlining)]
207-
private static void CopyRotate90(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
208+
private static void CopyRotate90(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
208209
{
209210
int width = captureZone.Width;
210211
int height = captureZone.Height;
@@ -220,7 +221,7 @@ private static void CopyRotate90(in ReadOnlySpan<byte> source, int sourceStride,
220221
}
221222

222223
[MethodImpl(MethodImplOptions.AggressiveInlining)]
223-
private static void CopyRotate180(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
224+
private static void CopyRotate180(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
224225
{
225226
int width = captureZone.Width;
226227
int height = captureZone.Height;
@@ -237,7 +238,7 @@ private static void CopyRotate180(in ReadOnlySpan<byte> source, int sourceStride
237238
}
238239

239240
[MethodImpl(MethodImplOptions.AggressiveInlining)]
240-
private static void CopyRotate270(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
241+
private static void CopyRotate270(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
241242
{
242243
int width = captureZone.Width;
243244
int height = captureZone.Height;
@@ -309,7 +310,7 @@ protected override void ValidateCaptureZoneAndThrow(int x, int y, int width, int
309310
base.ValidateCaptureZoneAndThrow(x, y, width, height, downscaleLevel);
310311
}
311312

312-
private void InitializeCaptureZone(in CaptureZone<ColorBGRA> captureZone)
313+
private void InitializeCaptureZone(CaptureZone<ColorBGRA> captureZone)
313314
{
314315
int x;
315316
int y;

ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
3+
<TargetFrameworks>net8.0</TargetFrameworks>
44
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
55
<LangVersion>latest</LangVersion>
66
<Nullable>enable</Nullable>
@@ -27,12 +27,11 @@
2727
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2828

2929
<PackageReleaseNotes>
30-
- Fixed a memory-leak when disposing the ScreenCapture
3130
</PackageReleaseNotes>
3231

33-
<Version>2.0.4</Version>
34-
<AssemblyVersion>2.0.4</AssemblyVersion>
35-
<FileVersion>2.0.4</FileVersion>
32+
<Version>3.0.0</Version>
33+
<AssemblyVersion>3.0.0</AssemblyVersion>
34+
<FileVersion>3.0.0</FileVersion>
3635

3736
<OutputPath>..\bin\</OutputPath>
3837
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -63,7 +62,7 @@
6362
</ItemGroup>
6463

6564
<ItemGroup>
66-
<PackageReference Include="Vortice.Direct3D11" Version="3.2.0" />
65+
<PackageReference Include="Vortice.Direct3D11" Version="3.5.0" />
6766
</ItemGroup>
6867

6968
<ItemGroup>

ScreenCapture.NET.DX9/DX9ScreenCapture.cs

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Runtime.CompilerServices;
33
using System.Runtime.InteropServices;
44
using System.Threading;
5-
using ScreenCapture.NET.Downscale;
5+
using HPPH;
66
using SharpGen.Runtime;
77
using Vortice.Direct3D9;
88

@@ -92,7 +92,7 @@ protected override bool PerformScreenCapture()
9292
}
9393

9494
/// <inheritdoc />
95-
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
95+
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
9696
{
9797
if (_buffer == null) return;
9898

@@ -106,55 +106,26 @@ protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZ
106106
}
107107

108108
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109-
private void CopyZone(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
109+
private void CopyZone(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
110110
{
111-
ReadOnlySpan<ColorBGRA> source = MemoryMarshal.Cast<byte, ColorBGRA>(_buffer);
112-
Span<ColorBGRA> target = MemoryMarshal.Cast<byte, ColorBGRA>(buffer);
113-
114-
int offsetX = captureZone.X;
115-
int offsetY = captureZone.Y;
116-
int width = captureZone.Width;
117-
int height = captureZone.Height;
118-
119-
for (int y = 0; y < height; y++)
120-
{
121-
int sourceOffset = ((y + offsetY) * Display.Width) + offsetX;
122-
int targetOffset = y * width;
123-
124-
source.Slice(sourceOffset, width).CopyTo(target.Slice(targetOffset, width));
125-
}
111+
RefImage<ColorBGRA>.Wrap(_buffer, Display.Width, Display.Height, _stride)[captureZone.X, captureZone.Y, captureZone.Width, captureZone.Height]
112+
.CopyTo(MemoryMarshal.Cast<byte, ColorBGRA>(buffer));
126113
}
127114

128115
[MethodImpl(MethodImplOptions.AggressiveInlining)]
129-
private void DownscaleZone(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
116+
private void DownscaleZone(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
130117
{
131-
ReadOnlySpan<byte> source = _buffer;
132-
Span<byte> target = buffer;
118+
RefImage<ColorBGRA> source = RefImage<ColorBGRA>.Wrap(_buffer, Display.Width, Display.Height, _stride)[captureZone.X, captureZone.Y, captureZone.UnscaledWidth, captureZone.UnscaledHeight];
119+
Span<ColorBGRA> target = MemoryMarshal.Cast<byte, ColorBGRA>(buffer);
133120

134121
int blockSize = 1 << captureZone.DownscaleLevel;
135122

136-
int offsetX = captureZone.X;
137-
int offsetY = captureZone.Y;
138123
int width = captureZone.Width;
139124
int height = captureZone.Height;
140-
int stride = captureZone.Stride;
141-
int bpp = captureZone.ColorFormat.BytesPerPixel;
142-
int unscaledWith = captureZone.UnscaledWidth;
143125

144-
Span<byte> scaleBuffer = stackalloc byte[bpp];
145126
for (int y = 0; y < height; y++)
146127
for (int x = 0; x < width; x++)
147-
{
148-
AverageByteSampler.Sample(new SamplerInfo<byte>((x + offsetX) * blockSize, (y + offsetY) * blockSize, blockSize, blockSize, unscaledWith, bpp, source), scaleBuffer);
149-
150-
int targetOffset = (y * stride) + (x * bpp);
151-
152-
// DarthAffe 07.09.2023: Unroll as optimization since we know it's always 4 bpp - not ideal but it does quite a lot
153-
target[targetOffset] = scaleBuffer[0];
154-
target[targetOffset + 1] = scaleBuffer[1];
155-
target[targetOffset + 2] = scaleBuffer[2];
156-
target[targetOffset + 3] = scaleBuffer[3];
157-
}
128+
target[(y * width) + x] = source[x * blockSize, y * blockSize, blockSize, blockSize].Average();
158129
}
159130

160131
/// <inheritdoc />

ScreenCapture.NET.DX9/Downscale/AverageByteSampler.cs

Lines changed: 0 additions & 149 deletions
This file was deleted.

0 commit comments

Comments
 (0)