Skip to content

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

Closed
@yowl

Description

@yowl

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);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions