Skip to content

Commit

Permalink
EP-166 Add CEF popups support #close
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Fomkin committed Apr 18, 2019
1 parent 2b66686 commit 9580547
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
3 changes: 2 additions & 1 deletion Assets/Resources/Expload/OffscreenCEF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace Expload
public class OffscreenCEF : MonoBehaviour
{
[SerializeField]
private string url = "http://localhost:8087/ui/overlay/";
private string url = "https://fomkin.org/select.html";
//private string url = "http://localhost:8087/ui/overlay/";

[Space]
[SerializeField]
Expand Down
94 changes: 56 additions & 38 deletions Assets/Resources/Expload/OffscreenCEFClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ internal class OffscreenCEFClient : CefClient
{
private readonly OffscreenLoadHandler _loadHandler;
private readonly OffscreenRenderHandler _renderHandler;
private readonly object sPixelLock = new object();
private readonly int _windowWidth;
private readonly int _windowHeight;

private byte[] sPixelBuffer;
private byte[] sPopupPixelBufer;
private CefRectangle _popupSize;
private bool _popupShow;

private CefBrowserHost sHost;

public OffscreenCEFClient(int windowWidth, int windowHeight, bool hideScrollbars = false)
{
this._windowWidth = windowWidth;
this._windowHeight = windowHeight;
this._loadHandler = new OffscreenLoadHandler(this, hideScrollbars);
this._renderHandler = new OffscreenRenderHandler(windowWidth, windowHeight, this);
this._renderHandler = new OffscreenRenderHandler(this);

this.sPixelBuffer = new byte[windowWidth * windowHeight * 4];

Expand All @@ -30,14 +36,26 @@ public OffscreenCEFClient(int windowWidth, int windowHeight, bool hideScrollbars

public void UpdateTexture(Texture2D pTexture)
{
lock (sPixelLock)
{
if (this.sHost == null)
return;

pTexture.LoadRawTextureData(this.sPixelBuffer);
pTexture.Apply(false);
if (this.sHost == null)
return;

byte[] buffer = sPixelBuffer;

if (_popupShow && sPopupPixelBufer != null) {
// Clone view buffer
buffer = new byte[sPixelBuffer.Length];
sPixelBuffer.CopyTo(buffer, 0);
// Copy subrect of popup
for (int y = 0; y < _popupSize.Height; y++)
{
int sourceOffset = _popupSize.Width * y * 4;
int targetOffset = (_popupSize.Y + y) * _windowWidth * 4 + _popupSize.X;
Array.Copy(sPopupPixelBufer, sourceOffset, buffer, targetOffset, _popupSize.Width * 4);
}
}

pTexture.LoadRawTextureData(buffer);
pTexture.Apply(false);
}

public void SendMouseMove(CefMouseEvent e)
Expand Down Expand Up @@ -88,6 +106,15 @@ protected override CefLoadHandler GetLoadHandler()
return this._loadHandler;
}

private void AjustPopupBuffer(int width, int height)
{
int size = width * height * 4;
// Reallocate popup buffer if nessasery
if (sPopupPixelBufer == null || sPopupPixelBufer.Length != size)
sPopupPixelBufer = new byte[size];
}


#endregion Interface

#region Handlers
Expand Down Expand Up @@ -132,19 +159,15 @@ private void HideScrollbars(CefFrame frame)
"head.appendChild(style);";
frame.ExecuteJavaScript(jsScript, string.Empty, 107);
}

}

internal class OffscreenRenderHandler : CefRenderHandler
{
private OffscreenCEFClient client;

private readonly int _windowWidth;
private readonly int _windowHeight;

public OffscreenRenderHandler(int windowWidth, int windowHeight, OffscreenCEFClient client)
public OffscreenRenderHandler(OffscreenCEFClient client)
{
this._windowWidth = windowWidth;
this._windowHeight = windowHeight;
this.client = client;
}

Expand All @@ -164,38 +187,37 @@ protected override bool GetViewRect(CefBrowser browser, ref CefRectangle rect)
{
rect.X = 0;
rect.Y = 0;
rect.Width = this._windowWidth;
rect.Height = this._windowHeight;
rect.Width = client._windowWidth;
rect.Height = client._windowHeight;
return true;
}

protected override void OnPopupShow(CefBrowser browser, bool show)
{
base.OnPopupShow(browser, show);
client._popupShow = show;
}

// private int drawCount = 0;
protected override void OnPopupSize(CefBrowser browser, CefRectangle rect)
{
client._popupSize = rect;
client.AjustPopupBuffer(rect.Width, rect.Height);
}

[SecurityCritical]
protected override void OnPaint(CefBrowser browser, CefPaintElementType type, CefRectangle[] dirtyRects, IntPtr buffer, int width, int height)
{
//Debug.Log("drawCount=" + drawCount);
//drawCount++;
//if (type == CefPaintElementType.Popup)
//{
// Debug.Log(dirtyRects.ToString());
// Debug.Log("w="+ width + ",h="+height);
// Debug.Log(dirtyRects.ToString());
// return;
//}
if (browser != null)
if (browser == null)
return;

byte[] targetBuffer = client.sPixelBuffer;
if (type == CefPaintElementType.Popup)
{
lock (this.client.sPixelLock)
{
// TODO Use dirtyRects
Marshal.Copy(buffer, this.client.sPixelBuffer, 0, this.client.sPixelBuffer.Length);
}
client.AjustPopupBuffer(width, height);
targetBuffer = client.sPopupPixelBufer;
}


Marshal.Copy(buffer, targetBuffer, 0, targetBuffer.Length);
}

protected override bool GetScreenInfo(CefBrowser browser, CefScreenInfo screenInfo)
Expand All @@ -207,10 +229,6 @@ protected override void OnCursorChange(CefBrowser browser, IntPtr cursorHandle,
{
}

protected override void OnPopupSize(CefBrowser browser, CefRectangle rect)
{
}

protected override void OnScrollOffsetChanged(CefBrowser browser, double x, double y)
{
}
Expand Down

0 comments on commit 9580547

Please sign in to comment.