package main import ( "github.com/haleyrom/skia-go" "image/color" "log" "runtime" "github.com/go-gl/gl/v3.3-core/gl" "github.com/go-gl/glfw/v3.3/glfw" ) func init() { // GLFW event handling must run on the main OS thread runtime.LockOSThread() } func main() { // Initialize GLFW if err := glfw.Init(); err != nil { log.Fatalf("failed to initialize GLFW: %v", err) } defer glfw.Terminate() // Create GLFW window glfw.WindowHint(glfw.ContextVersionMajor, 3) glfw.WindowHint(glfw.ContextVersionMinor, 3) glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile) glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True) window, err := glfw.CreateWindow(800, 600, "GLFW + Skia Demo", nil, nil) if err != nil { log.Fatalf("failed to create GLFW window: %v", err) } window.MakeContextCurrent() // Initialize OpenGL if err := gl.Init(); err != nil { log.Fatalf("failed to initialize OpenGL: %v", err) } // Set up Skia surface glInterface := skia.NewNativeGrGlinterface() if glInterface == nil { log.Fatalf("failed to create Skia OpenGL interface") } grContext := skia.NewGLGrContext(glInterface) if grContext == nil { log.Fatalf("failed to create Skia GrContext") } // Get framebuffer info var framebufferInfo skia.GrGlFramebufferinfo var fboID int32 gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &fboID) framebufferInfo.FFBOID = uint32(fboID) framebufferInfo.FFormat = gl.RGBA8 width, height := window.GetSize() renderTarget := skia.NewGlGrBackendrendertarget(int32(width), int32(height), 1, 8, &framebufferInfo) if renderTarget == nil { log.Fatalf("failed to create Skia render target") } surface := skia.NewSurfaceBackendRenderTarget(grContext, renderTarget, skia.GR_SURFACE_ORIGIN_BOTTOM_LEFT, skia.SK_COLORTYPE_RGBA_8888, nil, nil) if surface == nil { log.Fatalf("failed to create Skia surface") } defer surface.Unref() c := color.RGBA{R: 255, G: 100, B: 50, A: 255} f := skia.NewColor4f(c) colorWhite := f.ToColor() for !window.ShouldClose() { gl.ClearColor(0.3, 0.3, 0.3, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) canvas := surface.GetCanvas() paint := skia.NewPaint() paint.SetColor(0xffff0000) // Clear the canvas with white color canvas.Clear(colorWhite) // Create a rectangle and draw it rect := skia.Rect{ Left: 100, Top: 100, Right: 300, Bottom: 300, } canvas.DrawRect(&rect, paint) grContext.Flush() // Swap OpenGL buffers window.SwapBuffers() // Poll for GLFW events glfw.PollEvents() } // Cleanup surface.Unref() grContext.Unref() }