github.com/utopiagio/gio@v0.0.8/app/egl_windows.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  //go:build !noopengl
     4  
     5  package app
     6  
     7  import (
     8  	"golang.org/x/sys/windows"
     9  
    10  	"github.com/utopiagio/gio/internal/egl"
    11  )
    12  
    13  type glContext struct {
    14  	win *window
    15  	*egl.Context
    16  }
    17  
    18  func init() {
    19  	drivers = append(drivers, gpuAPI{
    20  		priority: 2,
    21  		initializer: func(w *window) (context, error) {
    22  			disp := egl.NativeDisplayType(w.HDC())
    23  			ctx, err := egl.NewContext(disp)
    24  			if err != nil {
    25  				return nil, err
    26  			}
    27  			return &glContext{win: w, Context: ctx}, nil
    28  		},
    29  	})
    30  }
    31  
    32  func (c *glContext) Release() {
    33  	if c.Context != nil {
    34  		c.Context.Release()
    35  		c.Context = nil
    36  	}
    37  }
    38  
    39  func (c *glContext) Refresh() error {
    40  	c.Context.ReleaseSurface()
    41  	var (
    42  		win           windows.Handle
    43  		width, height int
    44  	)
    45  	win, width, height = c.win.HWND()
    46  	eglSurf := egl.NativeWindowType(win)
    47  	if err := c.Context.CreateSurface(eglSurf, width, height); err != nil {
    48  		return err
    49  	}
    50  	if err := c.Context.MakeCurrent(); err != nil {
    51  		return err
    52  	}
    53  	c.Context.EnableVSync(true)
    54  	c.Context.ReleaseCurrent()
    55  	return nil
    56  }
    57  
    58  func (c *glContext) Lock() error {
    59  	return c.Context.MakeCurrent()
    60  }
    61  
    62  func (c *glContext) Unlock() {
    63  	c.Context.ReleaseCurrent()
    64  }