internal/cocoa: remove NSInvocation

Closes #2950
This commit is contained in:
Hajime Hoshi 2024-10-19 13:30:47 +09:00
parent 38e4f4c110
commit c3ea539cdb
2 changed files with 30 additions and 116 deletions

View File

@ -21,50 +21,37 @@ import (
)
var (
class_NSInvocation = objc.GetClass("NSInvocation")
class_NSMethodSignature = objc.GetClass("NSMethodSignature")
class_NSAutoreleasePool = objc.GetClass("NSAutoreleasePool")
class_NSString = objc.GetClass("NSString")
class_NSProcessInfo = objc.GetClass("NSProcessInfo")
class_NSColor = objc.GetClass("NSColor")
class_NSWindow = objc.GetClass("NSWindow")
class_NSView = objc.GetClass("NSView")
class_NSScreen = objc.GetClass("NSScreen")
)
var (
sel_alloc = objc.RegisterName("alloc")
sel_new = objc.RegisterName("new")
sel_release = objc.RegisterName("release")
sel_invocationWithMethodSignature = objc.RegisterName("invocationWithMethodSignature:")
sel_setSelector = objc.RegisterName("setSelector:")
sel_setTarget = objc.RegisterName("setTarget:")
sel_setArgumentAtIndex = objc.RegisterName("setArgument:atIndex:")
sel_getReturnValue = objc.RegisterName("getReturnValue:")
sel_invoke = objc.RegisterName("invoke")
sel_invokeWithTarget = objc.RegisterName("invokeWithTarget:")
sel_instanceMethodSignatureForSelector = objc.RegisterName("instanceMethodSignatureForSelector:")
sel_signatureWithObjCTypes = objc.RegisterName("signatureWithObjCTypes:")
sel_initWithUTF8String = objc.RegisterName("initWithUTF8String:")
sel_UTF8String = objc.RegisterName("UTF8String")
sel_length = objc.RegisterName("length")
sel_processInfo = objc.RegisterName("processInfo")
sel_frame = objc.RegisterName("frame")
sel_contentView = objc.RegisterName("contentView")
sel_setBackgroundColor = objc.RegisterName("setBackgroundColor:")
sel_colorWithSRGBRedGreenBlueAlpha = objc.RegisterName("colorWithSRGBRed:green:blue:alpha:")
sel_setFrameSize = objc.RegisterName("setFrameSize:")
sel_object = objc.RegisterName("object")
sel_styleMask = objc.RegisterName("styleMask")
sel_setStyleMask = objc.RegisterName("setStyleMask:")
sel_mainScreen = objc.RegisterName("mainScreen")
sel_screen = objc.RegisterName("screen")
sel_isVisible = objc.RegisterName("isVisible")
sel_deviceDescription = objc.RegisterName("deviceDescription")
sel_objectForKey = objc.RegisterName("objectForKey:")
sel_unsignedIntValue = objc.RegisterName("unsignedIntValue")
sel_setLayer = objc.RegisterName("setLayer:")
sel_setWantsLayer = objc.RegisterName("setWantsLayer:")
sel_alloc = objc.RegisterName("alloc")
sel_new = objc.RegisterName("new")
sel_release = objc.RegisterName("release")
sel_initWithUTF8String = objc.RegisterName("initWithUTF8String:")
sel_UTF8String = objc.RegisterName("UTF8String")
sel_length = objc.RegisterName("length")
sel_processInfo = objc.RegisterName("processInfo")
sel_frame = objc.RegisterName("frame")
sel_contentView = objc.RegisterName("contentView")
sel_setBackgroundColor = objc.RegisterName("setBackgroundColor:")
sel_colorWithSRGBRedGreenBlueAlpha = objc.RegisterName("colorWithSRGBRed:green:blue:alpha:")
sel_setFrameSize = objc.RegisterName("setFrameSize:")
sel_object = objc.RegisterName("object")
sel_styleMask = objc.RegisterName("styleMask")
sel_setStyleMask = objc.RegisterName("setStyleMask:")
sel_mainScreen = objc.RegisterName("mainScreen")
sel_screen = objc.RegisterName("screen")
sel_isVisible = objc.RegisterName("isVisible")
sel_deviceDescription = objc.RegisterName("deviceDescription")
sel_objectForKey = objc.RegisterName("objectForKey:")
sel_unsignedIntValue = objc.RegisterName("unsignedIntValue")
sel_setLayer = objc.RegisterName("setLayer:")
sel_setWantsLayer = objc.RegisterName("setWantsLayer:")
)
const (
@ -176,52 +163,6 @@ func (v NSView) SetWantsLayer(wantsLayer bool) {
v.Send(sel_setWantsLayer, wantsLayer)
}
// NSInvocation is being used to call functions that can't be called directly with purego.SyscallN.
// See the downsides of that function for what it cannot do.
type NSInvocation struct {
objc.ID
}
func NSInvocation_invocationWithMethodSignature(sig NSMethodSignature) NSInvocation {
return NSInvocation{objc.ID(class_NSInvocation).Send(sel_invocationWithMethodSignature, sig.ID)}
}
func (i NSInvocation) SetSelector(cmd objc.SEL) {
i.Send(sel_setSelector, cmd)
}
func (i NSInvocation) SetTarget(target objc.ID) {
i.Send(sel_setTarget, target)
}
func (i NSInvocation) SetArgumentAtIndex(arg unsafe.Pointer, idx int) {
i.Send(sel_setArgumentAtIndex, arg, idx)
}
func (i NSInvocation) GetReturnValue(ret unsafe.Pointer) {
i.Send(sel_getReturnValue, ret)
}
func (i NSInvocation) Invoke() {
i.Send(sel_invoke)
}
func (i NSInvocation) InvokeWithTarget(target objc.ID) {
i.Send(sel_invokeWithTarget, target)
}
type NSMethodSignature struct {
objc.ID
}
// NSMethodSignature_signatureWithObjCTypes takes a string that represents the type signature of a method.
// It follows the encoding specified in the Apple Docs.
//
// [Apple Docs]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100
func NSMethodSignature_signatureWithObjCTypes(types string) NSMethodSignature {
return NSMethodSignature{objc.ID(class_NSMethodSignature).Send(sel_signatureWithObjCTypes, types)}
}
type NSAutoreleasePool struct {
objc.ID
}

View File

@ -913,11 +913,7 @@ func (rce RenderCommandEncoder) SetViewport(viewport Viewport) {
//
// Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515583-setscissorrect?language=objc.
func (rce RenderCommandEncoder) SetScissorRect(scissorRect ScissorRect) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:{MTLScissorRect=qqqq}"))
inv.SetTarget(rce.commandEncoder)
inv.SetSelector(sel_setScissorRect)
inv.SetArgumentAtIndex(unsafe.Pointer(&scissorRect), 2)
inv.Invoke()
rce.commandEncoder.Send(sel_setScissorRect, scissorRect)
}
// SetVertexBuffer sets a buffer for the vertex shader function at an index
@ -1007,19 +1003,10 @@ func (bce BlitCommandEncoder) SynchronizeTexture(texture Texture, slice int, lev
//
// Reference: https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400754-copyfromtexture?language=objc.
func (bce BlitCommandEncoder) CopyFromTexture(sourceTexture Texture, sourceSlice int, sourceLevel int, sourceOrigin Origin, sourceSize Size, destinationTexture Texture, destinationSlice int, destinationLevel int, destinationOrigin Origin) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:@QQ{MTLOrigin=qqq}{MTLSize=qqq}@QQ{MTLOrigin=qqq}"))
inv.SetTarget(bce.commandEncoder)
inv.SetSelector(sel_copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin)
inv.SetArgumentAtIndex(unsafe.Pointer(&sourceTexture), 2)
inv.SetArgumentAtIndex(unsafe.Pointer(&sourceSlice), 3)
inv.SetArgumentAtIndex(unsafe.Pointer(&sourceLevel), 4)
inv.SetArgumentAtIndex(unsafe.Pointer(&sourceOrigin), 5)
inv.SetArgumentAtIndex(unsafe.Pointer(&sourceSize), 6)
inv.SetArgumentAtIndex(unsafe.Pointer(&destinationTexture), 7)
inv.SetArgumentAtIndex(unsafe.Pointer(&destinationSlice), 8)
inv.SetArgumentAtIndex(unsafe.Pointer(&destinationLevel), 9)
inv.SetArgumentAtIndex(unsafe.Pointer(&destinationOrigin), 10)
inv.Invoke()
sel := sel_copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin
bce.commandEncoder.Send(sel,
sourceTexture.texture, sourceSlice, sourceLevel, sourceOrigin, sourceSize,
destinationTexture.texture, destinationSlice, destinationLevel, destinationOrigin)
}
// Library is a collection of compiled graphics or compute functions.
@ -1073,28 +1060,14 @@ func (t Texture) Release() {
//
// Reference: https://developer.apple.com/documentation/metal/mtltexture/1515751-getbytes?language=objc.
func (t Texture) GetBytes(pixelBytes *byte, bytesPerRow uintptr, region Region, level int) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:^vQ{MTLRegion={MTLOrigin=qqq}{MTLSize=qqq}}Q"))
inv.SetTarget(t.texture)
inv.SetSelector(sel_getBytes_bytesPerRow_fromRegion_mipmapLevel)
inv.SetArgumentAtIndex(unsafe.Pointer(&pixelBytes), 2)
inv.SetArgumentAtIndex(unsafe.Pointer(&bytesPerRow), 3)
inv.SetArgumentAtIndex(unsafe.Pointer(&region), 4)
inv.SetArgumentAtIndex(unsafe.Pointer(&level), 5)
inv.Invoke()
t.texture.Send(sel_getBytes_bytesPerRow_fromRegion_mipmapLevel, pixelBytes, bytesPerRow, region, level)
}
// ReplaceRegion copies a block of pixels from the caller's pointer into the storage allocation for slice 0 of a texture.
//
// Reference: https://developer.apple.com/documentation/metal/mtltexture/1515464-replaceregion?language=objc.
func (t Texture) ReplaceRegion(region Region, level int, pixelBytes unsafe.Pointer, bytesPerRow int) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:{MTLRegion={MTLOrigin=qqq}{MTLSize=qqq}}Q^vQ"))
inv.SetTarget(t.texture)
inv.SetSelector(sel_replaceRegion_mipmapLevel_withBytes_bytesPerRow)
inv.SetArgumentAtIndex(unsafe.Pointer(&region), 2)
inv.SetArgumentAtIndex(unsafe.Pointer(&level), 3)
inv.SetArgumentAtIndex(unsafe.Pointer(&pixelBytes), 4)
inv.SetArgumentAtIndex(unsafe.Pointer(&bytesPerRow), 5)
inv.Invoke()
t.texture.Send(sel_replaceRegion_mipmapLevel_withBytes_bytesPerRow, region, level, pixelBytes, bytesPerRow)
}
// Width is the width of the texture image for the base level mipmap, in pixels.