gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/gpu/headless/headless_darwin.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package headless 4 5 import ( 6 "errors" 7 8 "gioui.org/gpu" 9 _ "gioui.org/internal/cocoainit" 10 ) 11 12 /* 13 #cgo CFLAGS: -Werror -Wno-deprecated-declarations -fobjc-arc -x objective-c 14 #cgo LDFLAGS: -framework CoreGraphics -framework Metal -framework Foundation 15 16 #import <Metal/Metal.h> 17 18 static CFTypeRef createDevice(void) { 19 @autoreleasepool { 20 id dev = MTLCreateSystemDefaultDevice(); 21 return CFBridgingRetain(dev); 22 } 23 } 24 25 static CFTypeRef newCommandQueue(CFTypeRef devRef) { 26 @autoreleasepool { 27 id<MTLDevice> dev = (__bridge id<MTLDevice>)devRef; 28 return CFBridgingRetain([dev newCommandQueue]); 29 } 30 } 31 */ 32 import "C" 33 34 type mtlContext struct { 35 dev C.CFTypeRef 36 queue C.CFTypeRef 37 } 38 39 func init() { 40 newContextPrimary = func() (context, error) { 41 dev := C.createDevice() 42 if dev == 0 { 43 return nil, errors.New("headless: failed to create Metal device") 44 } 45 queue := C.newCommandQueue(dev) 46 if queue == 0 { 47 C.CFRelease(dev) 48 return nil, errors.New("headless: failed to create MTLQueue") 49 } 50 return &mtlContext{dev: dev, queue: queue}, nil 51 } 52 } 53 54 func (c *mtlContext) API() gpu.API { 55 return gpu.Metal{ 56 Device: uintptr(c.dev), 57 Queue: uintptr(c.queue), 58 PixelFormat: int(C.MTLPixelFormatRGBA8Unorm_sRGB), 59 } 60 } 61 62 func (c *mtlContext) MakeCurrent() error { 63 return nil 64 } 65 66 func (c *mtlContext) ReleaseCurrent() {} 67 68 func (d *mtlContext) Release() { 69 C.CFRelease(d.dev) 70 C.CFRelease(d.queue) 71 *d = mtlContext{} 72 }