-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
217 lines (188 loc) · 7.77 KB
/
Program.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Runtime.InteropServices;
using SkiaSharp;
//using SkiaSharp;
namespace wskia
{
unsafe class Program
{
[StructLayout(LayoutKind.Sequential)]
struct EmscriptenWebGLContextAttributes
{
internal int alpha;
internal int depth;
internal int stencil;
internal int antialias;
internal int premultipliedAlpha;
internal int preserveDrawingBuffer;
internal int powerPreference;
internal int failIfMajorPerformanceCaveat;
internal int majorVersion;
internal int minorVersion;
internal int enableExtensionsByDefault;
internal int explicitSwapControl;
internal int proxyContextToMainThread;
internal int renderViaOffscreenBackBuffer;
}
[DllImport("*")]
internal static extern unsafe void emscripten_set_main_loop(delegate* unmanaged <void> f, int fps, int simulate_infinite_loop);
#if WEBGL
[DllImport("*")]
static extern IntPtr emscripten_webgl_create_context(byte* s, EmscriptenWebGLContextAttributes* attr);
[DllImport("*")]
static extern void emscripten_webgl_init_context_attributes(EmscriptenWebGLContextAttributes* attr);
[DllImport("*")]
static extern void emscripten_webgl_make_context_current(IntPtr context);
#else
[DllImport("*")]
static extern IntPtr copyToCanvas(byte* buffer, int width, int height);
#endif
[DllImport("*")]
static extern void emscripten_get_canvas_element_size(byte* s, int* width, int* height);
[DllImport("*")]
static extern void glGetIntegerv(int code, int* intv);
[DllImport("*")]
static extern void glBindFramebuffer(int target, int buffer);
[DllImport("*")]
static extern void glClearColor(int c1, int c2, int c3, int c4);
[DllImport("*")]
static extern void glClearStencil(int stencil);
[DllImport("*")]
static extern void glClear(int flag);
static IntPtr glContext;
// "#canvas" in the DOM
static ReadOnlySpan<byte> s => new byte[]
{
0x23,
0x63,0x61,0x6E,0x76, 0x61, 0x73,
0x0
};
static int s_width, s_height;
[UnmanagedCallersOnly(EntryPoint = "MainLoop")]
static void MainLoop()
{
SKCanvas canvas = surface.Canvas;
// canvas.Clear(SKColors.White);
// configure our brush
var redBrush = new SKPaint
{
Color = new SKColor(0xff, 0, 0),
IsStroke = true
};
var blueBrush = new SKPaint
{
Color = new SKColor(0, 0, 0xff),
IsStroke = true
};
for (int i = 0; i < 64; i += 8)
{
var rect = new SKRect(i, i, 256 - i - 1, 256 - i - 1);
canvas.DrawRect(rect, (i % 16 == 0) ? redBrush : blueBrush);
}
copyToCanvas((byte*)surface.PeekPixels().GetPixels(), s_width, s_height);
}
static SKSurface surface;
static void Main()
{
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
#if WEBGL
EmscriptenWebGLContextAttributes attrs;
emscripten_webgl_init_context_attributes(&attrs);
Console.WriteLine("emscripten_webgl_init_context_attributes");
attrs.stencil = 8;
attrs.majorVersion = 2;
attrs.depth = 1;
attrs.alpha = 1;
attrs.enableExtensionsByDefault = 1;
attrs.premultipliedAlpha = 1;
fixed (byte* n = &(s[0]))
{
glContext = emscripten_webgl_create_context(n, &attrs);
}
Console.WriteLine("emscripten_webgl_create_context:");
Console.WriteLine(glContext.ToInt32().ToString());
emscripten_webgl_make_context_current(glContext);
Console.WriteLine("emscripten_webgl_make_context_current");
// var grContext = GRContext.CreateGl();
GRGlInterface grGlInterface = GRGlInterface.Create();
Console.WriteLine("GRGlInterface.CreateWebGl");
Console.WriteLine(grGlInterface.Handle.ToString());
GRContext grContext = GRContext.CreateGl(grGlInterface);
if (grContext.Handle == IntPtr.Zero)
{
Console.WriteLine("GRContext.CreateGl failed");
}
else
{
Console.WriteLine("GRContext.CreateGl");
}
var grContext = GRContext.CreateGl();
int samples;
glGetIntegerv(0x80A9, &samples); // GL_SAMPLES
Console.WriteLine("Samples");
Console.WriteLine(samples.ToString());
int stencils;
glGetIntegerv(0x0D57, &stencils); // GL_STENCIL_BITS
Console.WriteLine("Stencils");
Console.WriteLine(stencils.ToString());
#endif
const SKColorType colorType = SKColorType.Rgba8888;
int width, height;
fixed (byte* n = &(s[0]))
{
emscripten_get_canvas_element_size(n, &width, &height);
Console.WriteLine("emscripten_get_canvas_element_size");
Console.WriteLine(width + "," + height);
}
s_width = width;
s_height = height;
#if WEBGL
var info = new GRGlFramebufferInfo(0, colorType.ToGlSizedFormat());
Console.WriteLine("GRGlFramebufferInfo");
var backendRenderTarget = new GRBackendRenderTarget(width, height, 1 /*samples */, stencils, info);
if (backendRenderTarget.Handle == IntPtr.Zero)
{
Console.WriteLine("failed to create GRBackendRenderTarget ");
}
if (!backendRenderTarget.IsValid)
{
Console.WriteLine("backendRenderTarget is not valid ");
}
else
{
Console.WriteLine("backendRenderTarget is valid ");
}
// WebGL should already be clearing the color and stencil buffers, but do it again here to
// ensure Skia receives them in the expected state.
// glBindFramebuffer(0x8D40 /* GL_FRAMEBUFFER */, 0);
//glClearColor(0, 0, 0, 0);
// glClearStencil(0);
// glClear(0x00004000 /* GL_COLOR_BUFFER_BIT */ | 0x00000400 /* GL_STENCIL_BUFFER_BIT*/ );
// grContext.ResetContext(GRGlBackendState.RenderTarget | GRGlBackendState.Misc);
// Console.WriteLine("context reset");
#endif
#if WEBGL
surface = SKSurface.Create(grContext, backendRenderTarget, colorType);
#else
surface = SKSurface.Create(new SKImageInfo(width, height, colorType));
#endif
if (surface != null)
{
Console.WriteLine("got a surface");
}
else
{
Console.WriteLine("Failed to create surface");
return;
}
surface.Canvas.GetLocalClipBounds(out SKRect bounds);
Console.WriteLine("clip");
Console.WriteLine(bounds.Top + ", " + bounds.Left + " " + bounds.Bottom);
surface.Canvas.DrawColor(SKColors.White);
surface.Canvas.Flush();
copyToCanvas((byte*)surface.PeekPixels().GetPixels(), width, height);
emscripten_set_main_loop(&MainLoop, 0, 0);
}
}
}