-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathCanvasHelper.cs
52 lines (41 loc) · 2.02 KB
/
CanvasHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.JavaScript;
namespace Avalonia.Browser.Interop;
internal record GLInfo(int ContextId, uint FboId, int Stencils, int Samples, int Depth);
internal static partial class CanvasHelper
{
public static (JSObject CanvasView, GLInfo? GLInfo) CreateSurface(
JSObject container, BrowserRenderingMode renderingMode)
{
var isGlMode = renderingMode is BrowserRenderingMode.WebGL1 or BrowserRenderingMode.WebGL2;
var canvasView = Create(container, (int)renderingMode);
GLInfo? glInfo = null;
if (isGlMode)
{
glInfo = new GLInfo(
canvasView.GetPropertyAsInt32("contextHandle")!,
(uint)canvasView.GetPropertyAsInt32("fboId"),
canvasView.GetPropertyAsInt32("stencil"),
canvasView.GetPropertyAsInt32("sample"),
canvasView.GetPropertyAsInt32("depth"));
}
return (canvasView, glInfo);
}
[JSImport("CanvasFactory.onSizeChanged", AvaloniaModule.MainModuleName)]
public static partial void OnSizeChanged(
JSObject canvasSurface,
[JSMarshalAs<JSType.Function<JSType.Number, JSType.Number, JSType.Number>>]
Action<int, int, double> onSizeChanged);
[JSImport("CanvasFactory.create", AvaloniaModule.MainModuleName)]
private static partial JSObject Create(JSObject canvasSurface, int mode);
[JSImport("CanvasFactory.destroy", AvaloniaModule.MainModuleName)]
public static partial void Destroy(JSObject canvasSurface);
[JSImport("CanvasFactory.ensureSize", AvaloniaModule.MainModuleName)]
public static partial void EnsureSize(JSObject canvasSurface);
[JSImport("CanvasFactory.putPixelData", AvaloniaModule.MainModuleName)]
public static partial void PutPixelData(JSObject canvasSurface, [JSMarshalAs<JSType.MemoryView>] ArraySegment<byte> data, int width, int height);
}