[Skia] RenderTargetBitmap
improvements
#14955
Labels
area/performance 📈
Categorizes an issue or PR as relevant to performance
area/skia/stability ✏️
area/skia ✏️
Categorizes an issue or PR as relevant to Skia
difficulty/medium 🤔
Categorizes an issue for which the difficulty level is reachable with a good understanding of WinUI
kind/enhancement
New feature or request
Current situation
RenderTargetBitmap
is IDisposable in Uno while it's not in WinUI. We should maintain parity with WinUI.RenderTargetBitmap
is using ArrayPool for renting the array. Generally, RenderTargetBitmap will tend to ask for a very large array that the .NET ArrayPool implementation will just always allocate a new array. So, effectively we are always allocating a large array and not using anything from the pool.Two solutions come to mind:
Solution 1: Avoid
_buffer
fieldCurrently, in
RenderAsBgra8_Premul
, we get an SKBitmap and copy its pixels to_buffer
field.Then, we dispose the SKBitmap, and we use the
_buffer
array for two purposes:Image.Source
)GetPixelsAsync
Both purposes can still be achieved with SKBitmap instead of a
_buffer
array.The remaining part will be disposing the SKBitmap, which we will have to do in the destructor (unfortunately)
Note that this approach is Skia-specific. We might find similar ideas for other platforms, but then this will introduce more platform-specific code to RenderTargetBitmap.
EDIT: I tried this approach, but it's problematic as we need the array anyways in
GetPixelsAsync
(becauseBuffer
needs either an array of bytes orMemory<byte>
, but we have aSpan<byte>
that we can't convert toMemory<byte>
as far as I know.Solution 2: Using
SegmentedArray
sWe can port
SegmentedArray
implementation from Roslyn and use it instead of a regular array. This will help with LOH allocations.EDIT: This too couldn't work, as SkiaSharp needs a pointer to contiguous memory.
Some profiling will be needed to know the performance impact of the changes. This will be most notable in snapshot tests.
The text was updated successfully, but these errors were encountered: