Skip to content

Commit

Permalink
Cef buffer improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Voltstro committed Feb 5, 2024
1 parent 0fc907b commit a4b3c96
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
9 changes: 4 additions & 5 deletions src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using UnityWebBrowser.Engine.Cef.Browser.Popups;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core.Logging;
Expand Down Expand Up @@ -78,12 +79,10 @@ public void Dispose()
/// Gets the pixel data of the CEF window
/// </summary>
/// <returns></returns>
public byte[] GetPixels()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlyMemory<byte> GetPixels()
{
if (browserHost == null)
return Array.Empty<byte>();

return renderHandler.Pixels;
return browserHost == null ? Array.Empty<byte>() : renderHandler.Pixels;
}

protected override CefLoadHandler GetLoadHandler()
Expand Down
43 changes: 28 additions & 15 deletions src/UnityWebBrowser.Engine.Cef/Browser/UwbCefRenderHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using UnityWebBrowser.Engine.Cef.Core;
Expand All @@ -18,9 +19,12 @@ namespace UnityWebBrowser.Engine.Cef.Browser;
/// </summary>
public class UwbCefRenderHandler : CefRenderHandler
{
private readonly object pixelsLock;
private CefSize cefSize;
private byte[] pixels;

private readonly object pixelsLock;
private Memory<byte> userPixelsBuffer;
private byte[] pixelsBuffer;
private int pixelsLength;

private readonly ClientControlsActions clientControls;

Expand All @@ -35,27 +39,32 @@ public UwbCefRenderHandler(UwbCefClient client, CefSize size)
Resize(size);
clientControls = client.ClientControls;
}

public byte[] Pixels
public ReadOnlyMemory<byte> Pixels
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
lock (pixelsLock)
{
byte[] pixelsCopyBuffer = new byte[pixels.Length];
Array.Copy(pixels, pixelsCopyBuffer, pixels.Length);
return pixelsCopyBuffer;
pixelsBuffer.CopyTo(userPixelsBuffer);
}

return userPixelsBuffer;
}
}

public void Resize(CefSize size)
{
pixelsLength = size.Width * size.Height * 4;

lock (pixelsLock)
{
pixels = new byte[size.Width * size.Height * 4];
cefSize = size;
pixelsBuffer = new byte[pixelsLength];
userPixelsBuffer = new Memory<byte>(new byte[pixelsLength]);
}

cefSize = size;
}

protected override CefAccessibilityHandler GetAccessibilityHandler()
Expand Down Expand Up @@ -88,12 +97,16 @@ protected override void OnPaint(CefBrowser browser, CefPaintElementType type, Ce
IntPtr buffer, int width,
int height)
{
if (browser != null)
//Copy our pixel buffer to our pixels
lock (pixelsLock)
{
Marshal.Copy(buffer, pixels, 0, pixels.Length);
}
//Ensure buffer sizes are the same
int myBufferSize = width * height * 4;
if(myBufferSize != pixelsLength)
return;

//Copy our pixel buffer to our pixels
lock (pixelsLock)
{
Marshal.Copy(buffer, pixelsBuffer, 0, pixelsLength);
}
}

protected override bool GetScreenInfo(CefBrowser browser, CefScreenInfo screenInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using UnityWebBrowser.Engine.Cef.Browser;
using VoltstroStudios.UnityWebBrowser.Engine.Shared;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core;
Expand Down Expand Up @@ -171,6 +172,7 @@ public static void PostTask(CefThreadId threadId, Action action)

#region Engine Actions

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public PixelsEvent GetPixels()
{
return new PixelsEvent
Expand Down

0 comments on commit a4b3c96

Please sign in to comment.