gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/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  	"gioui.org/internal/egl"
     9  )
    10  
    11  type glContext struct {
    12  	win *window
    13  	*egl.Context
    14  }
    15  
    16  func init() {
    17  	drivers = append(drivers, gpuAPI{
    18  		priority: 2,
    19  		initializer: func(w *window) (context, error) {
    20  			disp := egl.NativeDisplayType(w.HDC())
    21  			ctx, err := egl.NewContext(disp)
    22  			if err != nil {
    23  				return nil, err
    24  			}
    25  			win, _, _ := w.HWND()
    26  			eglSurf := egl.NativeWindowType(win)
    27  			if err := ctx.CreateSurface(eglSurf); err != nil {
    28  				ctx.Release()
    29  				return nil, err
    30  			}
    31  			if err := ctx.MakeCurrent(); err != nil {
    32  				ctx.Release()
    33  				return nil, err
    34  			}
    35  			defer ctx.ReleaseCurrent()
    36  			ctx.EnableVSync(true)
    37  			return &glContext{win: w, Context: ctx}, nil
    38  		},
    39  	})
    40  }
    41  
    42  func (c *glContext) Release() {
    43  	if c.Context != nil {
    44  		c.Context.Release()
    45  		c.Context = nil
    46  	}
    47  }
    48  
    49  func (c *glContext) Refresh() error {
    50  	return nil
    51  }
    52  
    53  func (c *glContext) Lock() error {
    54  	return c.Context.MakeCurrent()
    55  }
    56  
    57  func (c *glContext) Unlock() {
    58  	c.Context.ReleaseCurrent()
    59  }