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 ( var (
class_NSInvocation = objc.GetClass("NSInvocation")
class_NSMethodSignature = objc.GetClass("NSMethodSignature")
class_NSAutoreleasePool = objc.GetClass("NSAutoreleasePool") class_NSAutoreleasePool = objc.GetClass("NSAutoreleasePool")
class_NSString = objc.GetClass("NSString") class_NSString = objc.GetClass("NSString")
class_NSProcessInfo = objc.GetClass("NSProcessInfo") class_NSProcessInfo = objc.GetClass("NSProcessInfo")
class_NSColor = objc.GetClass("NSColor") class_NSColor = objc.GetClass("NSColor")
class_NSWindow = objc.GetClass("NSWindow")
class_NSView = objc.GetClass("NSView")
class_NSScreen = objc.GetClass("NSScreen") class_NSScreen = objc.GetClass("NSScreen")
) )
var ( var (
sel_alloc = objc.RegisterName("alloc") sel_alloc = objc.RegisterName("alloc")
sel_new = objc.RegisterName("new") sel_new = objc.RegisterName("new")
sel_release = objc.RegisterName("release") sel_release = objc.RegisterName("release")
sel_invocationWithMethodSignature = objc.RegisterName("invocationWithMethodSignature:") sel_initWithUTF8String = objc.RegisterName("initWithUTF8String:")
sel_setSelector = objc.RegisterName("setSelector:") sel_UTF8String = objc.RegisterName("UTF8String")
sel_setTarget = objc.RegisterName("setTarget:") sel_length = objc.RegisterName("length")
sel_setArgumentAtIndex = objc.RegisterName("setArgument:atIndex:") sel_processInfo = objc.RegisterName("processInfo")
sel_getReturnValue = objc.RegisterName("getReturnValue:") sel_frame = objc.RegisterName("frame")
sel_invoke = objc.RegisterName("invoke") sel_contentView = objc.RegisterName("contentView")
sel_invokeWithTarget = objc.RegisterName("invokeWithTarget:") sel_setBackgroundColor = objc.RegisterName("setBackgroundColor:")
sel_instanceMethodSignatureForSelector = objc.RegisterName("instanceMethodSignatureForSelector:") sel_colorWithSRGBRedGreenBlueAlpha = objc.RegisterName("colorWithSRGBRed:green:blue:alpha:")
sel_signatureWithObjCTypes = objc.RegisterName("signatureWithObjCTypes:") sel_setFrameSize = objc.RegisterName("setFrameSize:")
sel_initWithUTF8String = objc.RegisterName("initWithUTF8String:") sel_object = objc.RegisterName("object")
sel_UTF8String = objc.RegisterName("UTF8String") sel_styleMask = objc.RegisterName("styleMask")
sel_length = objc.RegisterName("length") sel_setStyleMask = objc.RegisterName("setStyleMask:")
sel_processInfo = objc.RegisterName("processInfo") sel_mainScreen = objc.RegisterName("mainScreen")
sel_frame = objc.RegisterName("frame") sel_screen = objc.RegisterName("screen")
sel_contentView = objc.RegisterName("contentView") sel_isVisible = objc.RegisterName("isVisible")
sel_setBackgroundColor = objc.RegisterName("setBackgroundColor:") sel_deviceDescription = objc.RegisterName("deviceDescription")
sel_colorWithSRGBRedGreenBlueAlpha = objc.RegisterName("colorWithSRGBRed:green:blue:alpha:") sel_objectForKey = objc.RegisterName("objectForKey:")
sel_setFrameSize = objc.RegisterName("setFrameSize:") sel_unsignedIntValue = objc.RegisterName("unsignedIntValue")
sel_object = objc.RegisterName("object") sel_setLayer = objc.RegisterName("setLayer:")
sel_styleMask = objc.RegisterName("styleMask") sel_setWantsLayer = objc.RegisterName("setWantsLayer:")
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 ( const (
@ -176,52 +163,6 @@ func (v NSView) SetWantsLayer(wantsLayer bool) {
v.Send(sel_setWantsLayer, wantsLayer) 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 { type NSAutoreleasePool struct {
objc.ID 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. // Reference: https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515583-setscissorrect?language=objc.
func (rce RenderCommandEncoder) SetScissorRect(scissorRect ScissorRect) { func (rce RenderCommandEncoder) SetScissorRect(scissorRect ScissorRect) {
inv := cocoa.NSInvocation_invocationWithMethodSignature(cocoa.NSMethodSignature_signatureWithObjCTypes("v@:{MTLScissorRect=qqqq}")) rce.commandEncoder.Send(sel_setScissorRect, scissorRect)
inv.SetTarget(rce.commandEncoder)
inv.SetSelector(sel_setScissorRect)
inv.SetArgumentAtIndex(unsafe.Pointer(&scissorRect), 2)
inv.Invoke()
} }
// SetVertexBuffer sets a buffer for the vertex shader function at an index // 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. // 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) { 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}")) sel := sel_copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin
inv.SetTarget(bce.commandEncoder) bce.commandEncoder.Send(sel,
inv.SetSelector(sel_copyFromTexture_sourceSlice_sourceLevel_sourceOrigin_sourceSize_toTexture_destinationSlice_destinationLevel_destinationOrigin) sourceTexture.texture, sourceSlice, sourceLevel, sourceOrigin, sourceSize,
inv.SetArgumentAtIndex(unsafe.Pointer(&sourceTexture), 2) destinationTexture.texture, destinationSlice, destinationLevel, destinationOrigin)
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()
} }
// Library is a collection of compiled graphics or compute functions. // 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. // Reference: https://developer.apple.com/documentation/metal/mtltexture/1515751-getbytes?language=objc.
func (t Texture) GetBytes(pixelBytes *byte, bytesPerRow uintptr, region Region, level int) { 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")) t.texture.Send(sel_getBytes_bytesPerRow_fromRegion_mipmapLevel, pixelBytes, bytesPerRow, region, level)
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()
} }
// ReplaceRegion copies a block of pixels from the caller's pointer into the storage allocation for slice 0 of a texture. // 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. // 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) { 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")) t.texture.Send(sel_replaceRegion_mipmapLevel_withBytes_bytesPerRow, region, level, pixelBytes, bytesPerRow)
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()
} }
// Width is the width of the texture image for the base level mipmap, in pixels. // Width is the width of the texture image for the base level mipmap, in pixels.