Skip to content

Eliminate memory copy when reading font data #6254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface
// guarantee that this problem will be fixed so we will use the GetUnmanagedStream(). Note: This path will only
// be taken for embedded fonts among which XPS is a main scenario. For local fonts we use DWrite's APIs.
_fontSourceStream = fontSource->GetUnmanagedStream();
_fontSourcePointer = _fontSourceStream->PositionPointer - _fontSourceStream->Position;
try
{
_lastWriteTime = fontSource->GetLastWriteTimeUtc().ToFileTimeUtc();
}
}
catch(System::ArgumentOutOfRangeException^) //The resulting file time would represent a date and time before 12:00 midnight January 1, 1601 C.E. UTC.
{
_lastWriteTime = -1;
}

// Create lock to control access to font source stream.
_fontSourceStreamLock = gcnew Object();
}
}

FontFileStream::~FontFileStream()
Expand Down Expand Up @@ -61,31 +59,9 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface
return E_INVALIDARG;
}

int fragmentSizeInt = (int)fragmentSize;
array<byte>^ buffer = gcnew array<byte>(fragmentSizeInt);

// DWrite may call this method from multiple threads. We need to ensure thread safety by making Seek and Read atomic.
System::Threading::Monitor::Enter(_fontSourceStreamLock);
try
{
_fontSourceStream->Seek(fileOffset, //long
System::IO::SeekOrigin::Begin);

_fontSourceStream->Read(buffer, //byte[]
0, //int
fragmentSizeInt //int
);
}
finally
{
System::Threading::Monitor::Exit(_fontSourceStreamLock);
}

GCHandle gcHandle = GCHandle::Alloc(buffer, GCHandleType::Pinned);

*fragmentStart = (byte*)(gcHandle.AddrOfPinnedObject().ToPointer());

*fragmentContext = GCHandle::ToIntPtr(gcHandle).ToPointer();
// Return a pointer to the font data that is already loaded in memory (because the font source resource is mmapped into the process' address space).
*fragmentStart = _fontSourcePointer + fileOffset;
*fragmentContext = nullptr;
}
catch(System::Exception^ exception)
{
Expand All @@ -102,11 +78,6 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface
void* fragmentContext
)
{
if (fragmentContext != NULL)
{
GCHandle gcHandle = GCHandle::FromIntPtr(IntPtr(fragmentContext));
gcHandle.Free();
}
}

[ComVisible(true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace MS { namespace Internal { namespace Text { namespace TextInterface
private ref class FontFileStream : public IDWriteFontFileStreamMirror
{
private:
Stream^ _fontSourceStream;
UnmanagedMemoryStream^ _fontSourceStream;
Byte* _fontSourcePointer;
INT64 _lastWriteTime;
Object^ _fontSourceStreamLock;

public:

Expand Down