Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Minimal WebAssembly example with NativeAOT-LLVM #1928

Closed
yowl opened this issue Jan 23, 2022 · 1 comment · Fixed by #2917
Closed

[QUESTION] Minimal WebAssembly example with NativeAOT-LLVM #1928

yowl opened this issue Jan 23, 2022 · 1 comment · Fixed by #2917

Comments

@yowl
Copy link

yowl commented Jan 23, 2022

Hi,

I'm trying to see if its possible to use SkiaSharp with NativeAOT-LLVM (https://github.com/dotnet/runtimelab/tree/feature/NativeAOT-LLVM). I've created what I think is a pretty small example (https://github.com/yowl/wskia) . You can build it with the command line (assuming you have SkiaSharp 2.0.12)

dotnet publish /p:SelfContained=true -r browser-wasm -c Debug /p:TargetArchitecture=wasm /p:PlatformTarget=AnyCPU /p:MSBuildEnableWorkloadResolver=false  --self-contained /p:EmccExtraArgs=.packages\skiasharp.nativeassets.webassembly\2.80.3\build\netstandard1.0\libSkiaSharp.a\2.0.12\libSkiaSharp.a

It attempts to fill the canvas with white and draws some rectangles, there's no errors but nothing appears on the canvas, it stays black. Is the set up code ok? Copied below (and removed the debug that's in the original for clarity):

        [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);
            }
        }
        static SKSurface surface;

        static void Main()
        {
            EmscriptenWebGLContextAttributes attrs;
            emscripten_webgl_init_context_attributes(&attrs);

            attrs.stencil = 8;
            // attrs.majorVersion = 2;
            fixed (byte* n = &(s[0]))
            {
                glContext = emscripten_webgl_create_context(n, &attrs); // this is "#canvas"
            }

            emscripten_webgl_make_context_current(glContext);

            GRGlInterface grGlInterface = GRGlInterface.Create();

            GRContext grContext = GRContext.CreateGl(grGlInterface);

            emscripten_set_main_loop(&MainLoop, 0, 0);

            const SKColorType colorType = SKColorType.Rgba8888;

            int width, height;
            fixed (byte* n = &(s[0]))
            {
                emscripten_get_canvas_element_size(n, &width, &height);
            }

            var info = new GRGlFramebufferInfo(0, colorType.ToGlSizedFormat());

            surface = SKSurface.Create(grContext, new GRBackendRenderTarget(width, height, 0, 8, info),
                colorType);
            if (surface != null)
            {
                Console.WriteLine("got a surface");
            }
            else
            {
                Console.WriteLine("Failed to create surface");
            }

            surface.Canvas.DrawColor(SKColors.White);
    }
@mattleibow
Copy link
Contributor

I wonder if you are just missing a canvas and grcontext Flush()? That has gotten me numerous times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants