Skip to content

Commit

Permalink
Remove p/invoke to RtlMoveMemory, cleanup caller (#42749)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks authored Sep 27, 2020
1 parent 92180ed commit a8b793c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -664,32 +664,23 @@ public void Save(Stream outputStream)
}
}

private void CopyBitmapData(BitmapData sourceData, BitmapData targetData)
private unsafe void CopyBitmapData(BitmapData sourceData, BitmapData targetData)
{
int offsetSrc = 0;
int offsetDest = 0;
byte* srcPtr = (byte*)sourceData.Scan0;
byte* destPtr = (byte*)targetData.Scan0;

Debug.Assert(sourceData.Height == targetData.Height, "Unexpected height. How did this happen?");
int height = Math.Min(sourceData.Height, targetData.Height);
long bytesToCopyEachIter = Math.Abs(targetData.Stride);

for (int i = 0; i < Math.Min(sourceData.Height, targetData.Height); i++)
for (int i = 0; i < height; i++)
{
IntPtr srcPtr, destPtr;
if (IntPtr.Size == 4)
{
srcPtr = new IntPtr(sourceData.Scan0.ToInt32() + offsetSrc);
destPtr = new IntPtr(targetData.Scan0.ToInt32() + offsetDest);
}
else
{
srcPtr = new IntPtr(sourceData.Scan0.ToInt64() + offsetSrc);
destPtr = new IntPtr(targetData.Scan0.ToInt64() + offsetDest);
}

UnsafeNativeMethods.CopyMemory(new HandleRef(this, destPtr), new HandleRef(this, srcPtr), Math.Abs(targetData.Stride));

offsetSrc += sourceData.Stride;
offsetDest += targetData.Stride;
Buffer.MemoryCopy(srcPtr, destPtr, bytesToCopyEachIter, bytesToCopyEachIter);
srcPtr += sourceData.Stride;
destPtr += targetData.Stride;
}

GC.KeepAlive(this); // finalizer mustn't deallocate data blobs while this method is running
}

private static bool BitmapHasAlpha(BitmapData bmpData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ namespace System.Drawing
{
internal class UnsafeNativeMethods
{
[DllImport(ExternDll.Kernel32, SetLastError = true, ExactSpelling = true, EntryPoint = "RtlMoveMemory")]
public static extern void CopyMemory(HandleRef destData, HandleRef srcData, int size);

[DllImport(ExternDll.Kernel32, SetLastError = true)]
public static extern int GetSystemDefaultLCID();

Expand Down

0 comments on commit a8b793c

Please sign in to comment.