gioui.org/ui@v0.0.0-20190926171558-ce74bc0cbaea/app/egl_windows.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package app
     4  
     5  import (
     6  	"os"
     7  	"unsafe"
     8  
     9  	syscall "golang.org/x/sys/windows"
    10  
    11  	"gioui.org/ui/app/internal/gl"
    12  )
    13  
    14  type (
    15  	_EGLint               int32
    16  	_EGLDisplay           uintptr
    17  	_EGLConfig            uintptr
    18  	_EGLContext           uintptr
    19  	_EGLSurface           uintptr
    20  	_EGLNativeDisplayType uintptr
    21  	_EGLNativeWindowType  uintptr
    22  )
    23  
    24  var (
    25  	libEGL                  = syscall.NewLazyDLL("libEGL.dll")
    26  	_eglChooseConfig        = libEGL.NewProc("eglChooseConfig")
    27  	_eglCreateContext       = libEGL.NewProc("eglCreateContext")
    28  	_eglCreateWindowSurface = libEGL.NewProc("eglCreateWindowSurface")
    29  	_eglDestroyContext      = libEGL.NewProc("eglDestroyContext")
    30  	_eglDestroySurface      = libEGL.NewProc("eglDestroySurface")
    31  	_eglGetConfigAttrib     = libEGL.NewProc("eglGetConfigAttrib")
    32  	_eglGetDisplay          = libEGL.NewProc("eglGetDisplay")
    33  	_eglGetError            = libEGL.NewProc("eglGetError")
    34  	_eglInitialize          = libEGL.NewProc("eglInitialize")
    35  	_eglMakeCurrent         = libEGL.NewProc("eglMakeCurrent")
    36  	_eglReleaseThread       = libEGL.NewProc("eglReleaseThread")
    37  	_eglSwapInterval        = libEGL.NewProc("eglSwapInterval")
    38  	_eglSwapBuffers         = libEGL.NewProc("eglSwapBuffers")
    39  	_eglTerminate           = libEGL.NewProc("eglTerminate")
    40  	_eglQueryString         = libEGL.NewProc("eglQueryString")
    41  )
    42  
    43  func init() {
    44  	mustLoadDLL(libEGL, "libEGL.dll")
    45  	mustLoadDLL(gl.LibGLESv2, "libGLESv2.dll")
    46  	// d3dcompiler_47.dll is needed internally for shader compilation to function.
    47  	mustLoadDLL(syscall.NewLazyDLL("d3dcompiler_47.dll"), "d3dcompiler_47.dll")
    48  }
    49  
    50  func mustLoadDLL(dll *syscall.LazyDLL, name string) {
    51  	loadErr := dll.Load()
    52  	if loadErr == nil {
    53  		return
    54  	}
    55  	pmsg := syscall.StringToUTF16Ptr("Failed to load " + name)
    56  	ptitle := syscall.StringToUTF16Ptr("Error")
    57  	syscall.MessageBox(0 /* HWND */, pmsg, ptitle, syscall.MB_ICONERROR|syscall.MB_SYSTEMMODAL)
    58  	os.Exit(1)
    59  }
    60  
    61  func eglChooseConfig(disp _EGLDisplay, attribs []_EGLint) (_EGLConfig, bool) {
    62  	var cfg _EGLConfig
    63  	var ncfg _EGLint
    64  	a := &attribs[0]
    65  	r, _, _ := _eglChooseConfig.Call(uintptr(disp), uintptr(unsafe.Pointer(a)), uintptr(unsafe.Pointer(&cfg)), 1, uintptr(unsafe.Pointer(&ncfg)))
    66  	issue34474KeepAlive(a)
    67  	return cfg, r != 0
    68  }
    69  
    70  func eglCreateContext(disp _EGLDisplay, cfg _EGLConfig, shareCtx _EGLContext, attribs []_EGLint) _EGLContext {
    71  	a := &attribs[0]
    72  	c, _, _ := _eglCreateContext.Call(uintptr(disp), uintptr(cfg), uintptr(shareCtx), uintptr(unsafe.Pointer(a)))
    73  	issue34474KeepAlive(a)
    74  	return _EGLContext(c)
    75  }
    76  
    77  func eglCreateWindowSurface(disp _EGLDisplay, cfg _EGLConfig, win _EGLNativeWindowType, attribs []_EGLint) _EGLSurface {
    78  	a := &attribs[0]
    79  	s, _, _ := _eglCreateWindowSurface.Call(uintptr(disp), uintptr(cfg), uintptr(win), uintptr(unsafe.Pointer(a)))
    80  	issue34474KeepAlive(a)
    81  	return _EGLSurface(s)
    82  }
    83  
    84  func eglDestroySurface(disp _EGLDisplay, surf _EGLSurface) bool {
    85  	r, _, _ := _eglDestroySurface.Call(uintptr(disp), uintptr(surf))
    86  	return r != 0
    87  }
    88  
    89  func eglDestroyContext(disp _EGLDisplay, ctx _EGLContext) bool {
    90  	r, _, _ := _eglDestroyContext.Call(uintptr(disp), uintptr(ctx))
    91  	return r != 0
    92  }
    93  
    94  func eglGetConfigAttrib(disp _EGLDisplay, cfg _EGLConfig, attr _EGLint) (_EGLint, bool) {
    95  	var val uintptr
    96  	r, _, _ := _eglGetConfigAttrib.Call(uintptr(disp), uintptr(cfg), uintptr(attr), uintptr(unsafe.Pointer(&val)))
    97  	return _EGLint(val), r != 0
    98  }
    99  
   100  func eglGetDisplay(disp _EGLNativeDisplayType) _EGLDisplay {
   101  	d, _, _ := _eglGetDisplay.Call(uintptr(disp))
   102  	return _EGLDisplay(d)
   103  }
   104  
   105  func eglGetError() _EGLint {
   106  	e, _, _ := _eglGetError.Call()
   107  	return _EGLint(e)
   108  }
   109  
   110  func eglInitialize(disp _EGLDisplay) (_EGLint, _EGLint, bool) {
   111  	var maj, min uintptr
   112  	r, _, _ := _eglInitialize.Call(uintptr(disp), uintptr(unsafe.Pointer(&maj)), uintptr(unsafe.Pointer(&min)))
   113  	return _EGLint(maj), _EGLint(min), r != 0
   114  }
   115  
   116  func eglMakeCurrent(disp _EGLDisplay, draw, read _EGLSurface, ctx _EGLContext) bool {
   117  	r, _, _ := _eglMakeCurrent.Call(uintptr(disp), uintptr(draw), uintptr(read), uintptr(ctx))
   118  	return r != 0
   119  }
   120  
   121  func eglReleaseThread() bool {
   122  	r, _, _ := _eglReleaseThread.Call()
   123  	return r != 0
   124  }
   125  
   126  func eglSwapInterval(disp _EGLDisplay, interval _EGLint) bool {
   127  	r, _, _ := _eglSwapInterval.Call(uintptr(disp), uintptr(interval))
   128  	return r != 0
   129  }
   130  
   131  func eglSwapBuffers(disp _EGLDisplay, surf _EGLSurface) bool {
   132  	r, _, _ := _eglSwapBuffers.Call(uintptr(disp), uintptr(surf))
   133  	return r != 0
   134  }
   135  
   136  func eglTerminate(disp _EGLDisplay) bool {
   137  	r, _, _ := _eglTerminate.Call(uintptr(disp))
   138  	return r != 0
   139  }
   140  
   141  func eglQueryString(disp _EGLDisplay, name _EGLint) string {
   142  	r, _, _ := _eglQueryString.Call(uintptr(disp), uintptr(name))
   143  	return gl.GoString(gl.SliceOf(r))
   144  }