luv

Workshop wiki

iosurface.lisp

hal/metal/iosurface.lisp

system luv · 24 definitions · on GitHub

IOSurface: a GPU-visible pixel buffer that survives a process boundary.

A surface created here has a small integer identity that any process on the machine can turn back into the same surface with IOSurfaceLookup, and Metal can wrap either end as an ordinary texture. That is the whole trick behind rendering one luvcraft inside another: the parent creates the surface, hands the child the number, and samples the texture the child draws into.

in-package#:luv.metal
eval-when
:compile-toplevel:load-toplevel:execute
cffi:define-foreign-libraryiosurface-framework
:darwin
:framework"IOSurface"
cffi:use-foreign-libraryiosurface-framework
cffi:defcfun
"IOSurfaceCreate"%iosurface-create:libraryiosurface-framework
:pointer
properties:pointer
cffi:defcfun
"IOSurfaceLookup"%iosurface-lookup:libraryiosurface-framework
:pointer
id:uint32
cffi:defcfun
"IOSurfaceGetID"iosurface-id:libraryiosurface-framework
:uint32
surface:pointer
cffi:defcfun
"IOSurfaceGetWidth"iosurface-width:libraryiosurface-framework
:size
surface:pointer
cffi:defcfun
"IOSurfaceGetHeight"iosurface-height:libraryiosurface-framework
:size
surface:pointer
cffi:defcfun
"IOSurfaceGetBytesPerRow"iosurface-bytes-per-row:libraryiosurface-framework
:size
surface:pointer
cffi:defcfun
"IOSurfaceGetBaseAddress"iosurface-base-address:libraryiosurface-framework
:pointer
surface:pointer
cffi:defcfun
"IOSurfaceLock"%iosurface-lock:libraryiosurface-framework
:int32
surface:pointer
options:uint32
seed:pointer
cffi:defcfun
"IOSurfaceUnlock"%iosurface-unlock:libraryiosurface-framework
:int32
surface:pointer
options:uint32
seed:pointer
cffi:defcfun
"CFRelease"%cf-release:libraryluv.objective-c::foundation-framework
:void
object:pointer
defuniosurface-property-key
name

The CFStringRef constant name (kIOSurfaceWidth and friends) as a pointer.

let
symbol
cffi:foreign-symbol-pointername:library'iosurface-framework
unlesssymbol
error"IOSurface does not export ~A."name
cffi:mem-refsymbol:pointer
objc:define-objective-c-message%new-mutable-dictionary
"new":object:ownership:owned:class"NSMutableDictionary"
objc:define-objective-c-message%dictionary-set-object
"setObject:forKey:":void
object:object
key:pointer
objc:define-objective-c-message%number-with-unsigned-long
"numberWithUnsignedLong:":object:ownership:borrowed:class"NSNumber"
value:uint64

'BGRA' as a four-character code.

defconstant+iosurface-pixel-format-bgra+
logior
ash
char-code#\B
24
ash
char-code#\G
16
ash
char-code#\R
8
char-code#\A
objc:define-objective-c-message%number-with-bool
"numberWithBool:":object:ownership:borrowed:class"NSNumber"
value:int8
defuncreate-iosurface
widthheight&key
bytes-per-element4
global-pt

Create a width x height IOSurface and return its raw IOSurfaceRef. The caller owns one CF reference; release it with release-iosurface. global-p (deprecated by Apple, still honoured) is what lets another process find the surface by its integer ID with lookup-iosurface.

objc:with-autorelease-pool
objc:with-owned-objective-c-object
let
number-class
flet
put"kIOSurfaceWidth"width
put"kIOSurfaceHeight"height
put"kIOSurfaceBytesPerElement"bytes-per-element
put"kIOSurfacePixelFormat"pixel-format
whenglobal-p
%dictionary-set-objectproperties
%number-with-boolnumber-class1
iosurface-property-key"kIOSurfaceIsGlobal"
let
surface
%iosurface-create
when
cffi:null-pointer-psurface
error"IOSurfaceCreate refused a ~Dx~D surface."widthheight
surface
defunlookup-iosurface
id

Return a new owned IOSurfaceRef for the surface numbered id, or NIL.

let
surface
%iosurface-lookupid
if
cffi:null-pointer-psurface
nilsurface
defunrelease-iosurface
surface
%cf-releasesurface
values
defmacrowith-locked-iosurface
surface&keyread-only
&bodybody

Run body with surface locked for CPU access to its base address.

let
s
gensym"SURFACE"
options
gensym"OPTIONS"
`
let
,s,surface
,options
%iosurface-lock,s,options
cffi:null-pointer
unwind-protect
progn,@body
%iosurface-unlock,s,options
cffi:null-pointer
defunread-iosurface-pixel
surfacexy

The four bytes of the pixel at X, Y as a list, in memory order (BGRA).

with-locked-iosurface
surface:read-onlyt
let
base
iosurface-base-addresssurface
offset
+
*y
iosurface-bytes-per-rowsurface
*x4
loopforibelow4collect
cffi:mem-refbase:uint8
+offseti

Metal wraps a surface as a shared-storage texture.

objc:define-objective-c-message%new-metal-texture-with-iosurface
"newTextureWithDescriptor:iosurface:plane:":object:ownership:owned:class"MTLTexture"
descriptor:object
surface:pointer
plane:uint64