shibukawa / nanovgo

zlib License
149 stars 32 forks source link

Help Needed: Android Port #11

Open darmie opened 6 years ago

darmie commented 6 years ago

I am trying to Port Nanovgo example to Android using andrioid-go and EGL

I modified Nanovgo to support GLES2 library from https://github.com/xlab/android-go/tree/master/gles2 , see modified version here nanovgo-gles

func init() {
    app.SetLogTag("GolangExample")
    // Initialise gl bindings using the current context.
    // err := gl.Init()
    // if err != nil {
    //  log.Fatalln("gl.Init:", err)
    // }
}

func main() {
    log.Println("NativeActivity has started ^_^")
    log.Printf("Platform: %s %s", runtime.GOOS, runtime.GOARCH)
    nativeWindowEvents := make(chan app.NativeWindowEvent)
    var displayHandle *egl.DisplayHandle
    ctx, err := nanovgo.NewContext(0)
    if err != nil {
        panic(err)
    }

    //demoData = LoadDemo(ctx)
    app.Main(func(a app.NativeActivity) {
        a.HandleNativeWindowEvents(nativeWindowEvents)
        a.InitDone()
        for {
            select {
            case event := <-a.LifecycleEvents():
                switch event.Kind {
                case app.OnCreate:
                    log.Println(event.Kind, "handled")
                default:
                    log.Println(event.Kind, "event ignored")
                }
            case event := <-nativeWindowEvents:
                switch event.Kind {
                case app.NativeWindowRedrawNeeded:
                    a.NativeWindowRedrawDone()
                    draw(displayHandle, ctx)
                    log.Println(event.Kind, "handled")
                case app.NativeWindowCreated:
                    expectedSurface := map[int32]int32{
                        egl.SurfaceType: egl.WindowBit,
                        egl.RedSize:     8,
                        egl.GreenSize:   8,
                        egl.BlueSize:    8,
                    }
                    if handle, err := egl.NewDisplayHandle(event.Window, expectedSurface); err != nil {
                        log.Fatalln("EGL error:", err)
                    } else {
                        displayHandle = handle
                        log.Printf("EGL display res: %dx%d", handle.Width, handle.Height)
                    }
                    initGL()
                case app.NativeWindowDestroyed:
                    displayHandle.Destroy()
                default:
                    log.Println(event.Kind, "event ignored")
                    //demoData.FreeData(ctx)
                }
            }
        }
    })
}

func initGL() {
    gl.Enable(gl.BLEND)
    gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
    gl.Enable(gl.CULL_FACE)
    gl.Disable(gl.DEPTH_TEST)
}

func draw(handle *egl.DisplayHandle, ctx *nanovgo.Context) {
    fmt.Println("draw")
    //fps := perfgraph.NewPerfGraph("Frame Time", "sans")

    //t, _ := fps.UpdateGraph()

    pixelRatio := float32(handle.Width) / float32(handle.Height)

    gl.Viewport(0, 0, int32(handle.Width), int32(handle.Height))
    fmt.Println("view port")

    gl.ClearColor(0, 0, 0, 0)
    fmt.Println("clear color")

    gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

    ctx.BeginFrame(int32(handle.Width), int32(handle.Height), pixelRatio)
    fmt.Println("begin frame")

    demo.RenderDemo(ctx, 0, 0, 300, 600, 0, false, &demo.DemoData{})

    //fps.RenderGraph(ctx, 5, 5)

    ctx.EndFrame()

    //gl.Enable(gl.DEPTH_TEST)

    //glfw.PollEvents()

    handle.SwapBuffers()

}

demo.go :

func RenderDemo(ctx *nanovgo.Context, mx, my, width, height, t float32, blowup bool, data *DemoData) {
    // Widgets
    drawWindow(ctx, "Widgets `n Stuff", 50, 50, 300, 400)
    var x float32 = 60.0
    var y float32 = 95.0
    fmt.Println("Draw Window")
    drawSearchBox(ctx, "Search", x, y, 280, 25)
    y += 40
    fmt.Println("Draw Search")
    drawDropDown(ctx, "Effects", x, y, 280, 28)
    //popy := y + 14
    y += 45
    fmt.Println("Draw DropDown")
    // Form
    drawLabel(ctx, "Login", x, y, 280, 20)
    y += 25
    fmt.Println("Draw Label")
    drawEditBox(ctx, "Email", x, y, 280, 28)
    y += 35
    fmt.Println("Draw Edit Box")
    drawEditBox(ctx, "Password", x, y, 280, 28)
    y += 38
    fmt.Println("Draw Password")
    drawCheckBox(ctx, "Remember me", x, y, 140, 28)
    fmt.Println("Draw  Checkbox")
    drawButton(ctx, IconLOGIN, "Sign in", x+138, y, 140, 28, nanovgo.RGBA(0, 96, 128, 255))
    y += 45
    fmt.Println("Draw Button")

    // Slider
    drawLabel(ctx, "Diameter", x, y, 280, 20)
    y += 25
    drawEditBoxNum(ctx, "123.00", "px", x+180, y, 100, 28)
    drawSlider(ctx, 0.4, x, y, 170, 28)
    y += 55

    drawButton(ctx, IconTRASH, "Delete", x, y, 160, 28, nanovgo.RGBA(128, 16, 8, 255))
    drawButton(ctx, 0, "Cancel", x+170, y, 110, 28, nanovgo.RGBA(0, 0, 0, 0))

    fmt.Println("finished rendering")
    // Thumbnails box
    //drawThumbnails(ctx, 365, popy-30, 160, 300, data.Images, t)
}

BUG:

i am getting a blank screen with these errors

06-08 11:37:00.885 23648-23666/? I/GolangExample: NativeActivity has started ^_^
06-08 11:37:00.885 23648-23666/? I/GolangExample: Platform: android arm
06-08 11:37:00.887 23648-23666/? I/GolangExample: Shader shader/vert error:
06-08 11:37:00.888 23648-23667/? I/GolangExample: onCreate handled
06-08 11:37:00.890 23648-23666/? I/GolangExample: onStart event ignored
06-08 11:37:00.893 23648-23666/? I/GolangExample: onResume event ignored
06-08 11:37:00.894 23648-23648/? D/ActivityThread: EYE startEyeVerifyBroadcast packagename=com.go_android.minimal; ClassName=android.app.NativeActivity
06-08 11:37:00.903 23648-23648/? V/PhoneWindow: DecorView setVisiblity: visibility = 4, Parent = null, this = DecorView@2359018[],statusBarBackground visible =false,statusColor: 0xff000000->
06-08 11:37:00.919 23648-23648/? D/WindowClient: Add to mViews: DecorView@2359018[NativeActivity], this = android.view.WindowManagerGlobal@4cddbcf
06-08 11:37:00.934 23648-23648/? V/PhoneWindow: DecorView setVisiblity: visibility = 0, Parent = ViewRoot{5b9d95c com.go_android.minimal/android.app.NativeActivity,ident = 0}, this = DecorView@2359018[NativeActivity],statusBarBackground visible =false,statusColor: 0xff000000->
06-08 11:37:00.968 23648-23648/? I/lulingjie--screenshot--observer--: observer is rigistedDecorView@2359018[NativeActivity]
06-08 11:37:00.994 23648-23648/? V/InputMethodManager: onWindowFocus: null softInputMode=272 first=true flags=#10100
06-08 11:37:01.027 23648-23666/? D/Surface: Surface::setBuffersUserDimensions(this=0xe663a000,w=0,h=0)
06-08 11:37:01.040 23648-23666/? D/Surface: Surface::connect(this=0xe663a000,api=1)
06-08 11:37:01.042 23648-23666/? W/libEGL: [ANDROID_RECORDABLE] format: 2
06-08 11:37:01.047 23648-23666/? D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
06-08 11:37:01.051 23648-23666/? I/GolangExample: EGL display res: 640x1070
06-08 11:37:01.052 23648-23664/? I/GolangExample: draw
06-08 11:37:01.052 23648-23664/? I/GolangExample: view port
06-08 11:37:01.052 23648-23664/? I/GolangExample: clear color
06-08 11:37:01.066 23648-23666/? D/GraphicBuffer: register, handle(0xe91913c0) (w:640 h:1070 s:640 f:0x2 u:0x000b00)
06-08 11:37:01.068 23648-23664/? I/GolangExample: begin frame
06-08 11:37:01.068 23648-23664/? I/GolangExample: Draw Window
06-08 11:37:01.068 23648-23664/? I/GolangExample: Draw Search
06-08 11:37:01.069 23648-23664/? I/GolangExample: Draw DropDown
06-08 11:37:01.069 23648-23664/? I/GolangExample: Draw Label
06-08 11:37:01.069 23648-23664/? I/GolangExample: Draw Edit Box
06-08 11:37:01.069 23648-23664/? I/GolangExample: Draw Password
06-08 11:37:01.069 23648-23664/? I/GolangExample: Draw  Checkbox
06-08 11:37:01.070 23648-23664/? I/GolangExample: Draw Button
06-08 11:37:01.070 23648-23666/? D/MALI: gles_state_set_error_internal:75: [MALI] GLES ctx: 0xa6680008, error code:0x502
06-08 11:37:01.070 23648-23666/? D/MALI: gles_state_set_error_internal:76: [MALI] GLES error info: OpenGL ES API version mismatch
lukebayes commented 6 years ago

I'm in no way an expert on these things, but I've been playing with this library for awhile too (though I haven't actually tried to get it up and running on Android).

I'm guessing these are the critical lines:

gles_state_set_error_internal:75: [MALI] GLES ctx: 0xa6680008, error code:0x502
gles_state_set_error_internal:76: [MALI] GLES error info: OpenGL ES API version mismatch

I got similar errors when attempting to run it on MacOS, where I needed to select a more appropriate OpenGL version number (i.e., not simply the highest possible version).

I can see a few different versions being identified in your const file. I suspect one of them might not be valid on the particular hardware/OS device you're working with.

The second error (0x502) is an Invalid Operation, which is pretty likely if the environment version is incompatible.

Hope that's helpful.

darmie commented 6 years ago

@lukebayes thanks for your contribution, I was able to fix those issues and they no longer come up. However, I am still getting a blank screen. Could it be how I am passing the vertexes?

06-09 02:51:09.538 13579-13617/? D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
06-09 02:51:09.542 13579-13617/? I/GolangExample: EGL display res: 720x1280
06-09 02:51:09.542 13579-13614/? I/GolangExample: INIT GL
06-09 02:51:09.553 13579-13614/? I/GolangExample: NewContext &{false 0xb7aea080}
06-09 02:51:09.554 13579-13614/? I/GolangExample: Context &{0xb7ae6010 [] 0 0 [] {[] [] [] [0 0 0 0]} 0 0 0 0 <nil> [0 0 0 0] 0 0 0 0 0}
06-09 02:51:09.585 13579-13614/? I/GolangExample: vertex shader status 1
06-09 02:51:09.610 13579-13614/? I/GolangExample: fragment shader status 1
06-09 02:51:09.672 13579-13614/? I/GolangExample: Program status 1
06-09 02:51:09.673 13579-13614/? I/GolangExample: GetUniforms &{1 3 2 [1 0 -1] 0 1}
06-09 02:51:09.673 13579-13617/? I/GolangExample: 1
06-09 02:51:09.673 13579-13614/? I/GolangExample: Render Create
06-09 02:51:09.674 13579-13614/? I/GolangExample: Create Internal
06-09 02:51:09.873 13579-13614/? I/GolangExample: draw
06-09 02:51:09.873 13579-13614/? I/GolangExample: view port
06-09 02:51:09.873 13579-13614/? I/GolangExample: clear color
06-09 02:51:09.929 13579-13617/? D/GraphicBuffer: register, handle(0xef634640) (w:720 h:1280 s:720 f:0x1 u:0x000b00)
06-09 02:51:09.931 13579-13614/? I/GolangExample: begin frame
06-09 02:51:09.936 13579-13614/? I/GolangExample: Draw Window
06-09 02:51:09.939 13579-13614/? I/GolangExample: Draw Search
06-09 02:51:09.941 13579-13614/? I/GolangExample: Draw DropDown
06-09 02:51:09.942 13579-13614/? I/GolangExample: Draw Label
06-09 02:51:09.943 13579-13614/? I/GolangExample: Draw Edit Box
06-09 02:51:09.944 13579-13614/? I/GolangExample: Draw Password
06-09 02:51:09.946 13579-13614/? I/GolangExample: Draw  Checkbox
06-09 02:51:09.948 13579-13614/? I/GolangExample: Draw Button
06-09 02:51:09.954 13579-13614/? I/GolangExample: finished rendering
06-09 02:51:09.994 13579-13617/? I/GolangExample: vertex: [50 53 0.5 1 50 447 0.5 1 50.878677 449.12134 0.5 1 53 450 0.5 1 347 450 0.5 1 349.12134 449.12134 0.5 1 350 447 0.5 1 350 53 0.5 1 349.12134 50.878677 0.5 1 347 50 0.5 1 53 50 0.5 1 50.878677 50.878677 0.5 1 50 450 0.5 1 350 450 0.5 1 350 50 0.5 1 50 450 0.5 1 350 50 0.5 1 50 50 0.5 1 40 40 0.5 1 40 470 0.5 1 360 470 0.5 1 360 40 0.5 1 50.878677 50.878677 0.5 1 53 50 0.5 1 347 50 0.5 1 349.12134 50.878677 0.5 1 350 53 0.5 1 350 447 0.5 1 349.12134 449.12134 0.5 1 347 450 0.5 1 53 450 0.5 1 50.878677 449.12134 0.5 1 50 447 0.5 1 50 53 0.5 1 40 470 0.5 1 360 470 0.5 1 360 40 0.5 1 40 470 0.5 1 360 40 0.5 1 40 40 0.5 1 51 53 0.5 1 51 79 0.5 1 51.585785 80.414215 0.5 1 53 81 0.5 1 347 81 0.5 1 348.41425 80.414215 0.5 1 349 79 0.5 1 349 53 0.5 1 348.41425 51.585785 0.5 1 347 51 0.5 1 53 51 0.5 1 51.585785 51.585785 0.5 1 51 81 0.5 1 349 81 0.5 1 349 51 0.5 1 51 81 0.5 1 349 51 0.5 1 51 51 0.5 1 49.61111 79.611115 0 0 49.61111 81.388885 1 0 51.38889 79.611115 0 1 51.38889 81.388885 1 1 348.6111 79.611115 0 1 348.6111 81.388885 1 1 350.3889 79.611115 0 0 350.3889 81.388885 1 0 165.33333 56.88889 0.02734375 0.001953125 145.77779 56.88889 0.005859375 0.001953125 165.33333 74.666664 0.02734375 0.021484375 145.77779 74.666664 0.005859375 0.021484375 168.88889 56.88889 0.04296875 0.001953125 158.22223 56.88889 0.03125 0.001953125 168.88889 74.666664 0.04296875 0.021484375 158.22223 74.666664 0.03125 0.021484375 176 56.88889 0.0625 0.001953125 161.77779 56.88889 0.046875 0.001953125 176 76.44444 0.0625 0.0234375 161.77779 76.44444 0.046875 0.0234375 183.11111 58.666668 0.08203125 0.001953125 168.88889 58.666668 0.06640625 0.001953125 183.11111 78.22222 0.08203125 0.0234375 168.88889 78.22222 0.06640625 0.0234375 190.22223 58.666668 0.1015625 0.001953125 176 58.666668 0.0859375 0.001953125 190.22223 76.44444 0.1015625 0.021484375 176 76.44444 0.0859375 0.021484375 195.55556 56.88889 0.119140625 0.001953125 183.11111 56.88889 0.10546875 0.001953125 195.55556 76.44444 0.119140625 0.0234375 183.11111 76.44444 0.10546875 0.0234375 202.66667 58.666668 0.13867188 0.001953125 188.44444 58.666668 0.123046875 0.001953125 202.66667 76.44444 0.13867188 0.021484375 188.44444 76.44444 0.123046875 0.021484375 202.66667 67.55556 0.15039062 0.001953125 195.55556 67.55556 0.14257812 0.001953125 202.66667 74.666664 0.15039062 0.009765625 195.55556 74.666664 0.14257812 0.009765625 211.55556 56.88889 0.16796875 0.001953125 199.11111 56.88889 0.15429688 0.001953125 211.55556 67.55556 0.16796875 0.013671875 199.11111 67.55556 0.15429688 0.013671875 218.66667 58.666668 0.1875 0.001953125 204.44444 58.666668 0.171875 0.001953125 218.66667 74.666664 0.1875 0.01953125 204.44444 74.666664 0.171875 0.01953125 218.66667 67.55556 0.15039062 0.001953125 211.55556 67.55556 0.14257812 0.001953125 218.66667 74.666664 0.15039062 0.009765625 211.55556 74.666664 0.14257812 0.009765625 231.11111 56.88889 0.20898438 0.001953125 215.11111 56.88889 0.19140625 0.001953125 231.11111 76.44444 0.20898438 0.0234375 215.11111 76.44444 0.19140625 0.0234375 236.44444 56.88889 0.119140625 0.001953125 224 56.88889 0.10546875 0.001953125 236.44444 76.44444 0.119140625 0.0234375 224 76.44444 0.10546875 0.0234375 243.55556 58.666668 0.22851562 0.001953125 229.33333 58.666668 0.21289062 0.001953125 243.55556 76.44444 0.22851562 0.021484375 229.33333 76.44444 0.21289062 0.021484375 248.88889 56.88889 0.24609375 0.001953125 236.44444 56.88889 0.23242188 0.001953125 248.88889 74.666664 0.24609375 0.021484375 236.44444 74.666664 0.23242188 0.021484375 254.22223 56.88889 0.24609375 0.001953125 241.77779 56.88889 0.23242188 0.001953125 254.22223 74.666664 0.24609375 0.021484375 241.77779 74.666664 0.23242188 0.021484375 163.55556 58.666668 0.26757812 0.001953125 147.55556 58.666668 0.25 0.001953125 163.55556 72.88889 0.26757812 0.017578125 147.55556 72.88889 0.25 0.017578125 167.11111 58.666668 0.27929688 0.001953125 160 58.666668 0.27148438 0.001953125 167.11111 72.88889 0.27929688 0.017578125 160 72.88889 0.27148438 0.017578125 174.22223 58
06-09 02:51:09.995 13579-13617/? I/GolangExample: view: [720 1280]
06-09 02:51:10.002 13579-13614/? I/GolangExample: VertexAttribArray 1
06-09 02:51:10.007 13579-13617/? I/GolangExample: nativeWindowRedrawNeeded handled
lukebayes commented 6 years ago

I'm not sure about what might be causing your blank screen at this point, but that has also happened to me. Here are some things that I've done that can help narrow the search space:

darmie commented 6 years ago

@lukebayes I tried everything you mentioned, but it did not work. Can you paste a link to source code of your working version, so that i can compare notes to see what I might have missed?

lukebayes commented 6 years ago

I'm sorry to say that I haven't tackled Android rendering with Nanovgo yet, so I'm probably not going to be too much help with the specifics of your case. My project is intended to run on everything (but doesn't yet), so I have a fair amount of indirection that will probably prove obfuscatory.

If you're stuck, I'd recommend removing every single thing you can to get a simple rectangle to draw on the screen on the device.

Once that's working, you can start to add your features one at a time to see where it breaks down. Based on my limited experience, I'd place my bets that it's either fonts or some device-specific arcana related to shaders or version numbers, so do what you can to remove/simplify those first.

darmie commented 6 years ago

Thanks @lukebayes, I got it to work on mobile target using Haxe programming language.

But using Go language would be a good boost tho.