gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/app/vulkan_android.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  //go:build !novulkan
     4  // +build !novulkan
     5  
     6  package app
     7  
     8  import (
     9  	"unsafe"
    10  
    11  	"gioui.org/gpu"
    12  	"gioui.org/internal/vk"
    13  )
    14  
    15  type wlVkContext struct {
    16  	win  *window
    17  	inst vk.Instance
    18  	surf vk.Surface
    19  	ctx  *vkContext
    20  }
    21  
    22  func init() {
    23  	newAndroidVulkanContext = func(w *window) (context, error) {
    24  		inst, err := vk.CreateInstance("VK_KHR_surface", "VK_KHR_android_surface")
    25  		if err != nil {
    26  			return nil, err
    27  		}
    28  		window, _, _ := w.nativeWindow()
    29  		surf, err := vk.CreateAndroidSurface(inst, unsafe.Pointer(window))
    30  		if err != nil {
    31  			vk.DestroyInstance(inst)
    32  			return nil, err
    33  		}
    34  		ctx, err := newVulkanContext(inst, surf)
    35  		if err != nil {
    36  			vk.DestroySurface(inst, surf)
    37  			vk.DestroyInstance(inst)
    38  			return nil, err
    39  		}
    40  		c := &wlVkContext{
    41  			win:  w,
    42  			inst: inst,
    43  			surf: surf,
    44  			ctx:  ctx,
    45  		}
    46  		return c, nil
    47  	}
    48  }
    49  
    50  func (c *wlVkContext) RenderTarget() (gpu.RenderTarget, error) {
    51  	return c.ctx.RenderTarget()
    52  }
    53  
    54  func (c *wlVkContext) API() gpu.API {
    55  	return c.ctx.api()
    56  }
    57  
    58  func (c *wlVkContext) Release() {
    59  	c.ctx.release()
    60  	if c.surf != 0 {
    61  		vk.DestroySurface(c.inst, c.surf)
    62  	}
    63  	vk.DestroyInstance(c.inst)
    64  	*c = wlVkContext{}
    65  }
    66  
    67  func (c *wlVkContext) Present() error {
    68  	return c.ctx.present()
    69  }
    70  
    71  func (c *wlVkContext) Lock() error {
    72  	return nil
    73  }
    74  
    75  func (c *wlVkContext) Unlock() {}
    76  
    77  func (c *wlVkContext) Refresh() error {
    78  	win, w, h := c.win.nativeWindow()
    79  	if c.surf != 0 {
    80  		c.ctx.destroySwapchain()
    81  		vk.DestroySurface(c.inst, c.surf)
    82  		c.surf = 0
    83  	}
    84  	surf, err := vk.CreateAndroidSurface(c.inst, unsafe.Pointer(win))
    85  	if err != nil {
    86  		return err
    87  	}
    88  	c.surf = surf
    89  	return c.ctx.refresh(c.surf, w, h)
    90  }