Debian Codegraph @

shiny/driver/mtldriver/internal/coreanim/coreanim.go

raw symbols SCIP
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build darwin
6
7 // Package coreanim provides access to Apple's Core Animation API
8 // (https://developer.apple.com/documentation/quartzcore).
9 //
10 // This package is in very early stages of development.
11 // It's a minimal implementation with scope limited to
12 // supporting mtldriver.
13 //
14 // It was copied from dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/coreanim.
15 package coreanim
16
17 import (
18 "errors"
19 "unsafe"
20
21 "dmitri.shuralyov.com/gpu/mtl"
22 )
23
24 /*
25 #cgo LDFLAGS: -framework QuartzCore -framework Foundation
26 #include <stdbool.h>
27 #include "coreanim.h"
28 */
29 import "C"
30
31 // Layer is an object that manages image-based content and
32 // allows you to perform animations on that content.
33 //
34 // Reference: https://developer.apple.com/documentation/quartzcore/calayer.
35 type Layer interface {
36 // Layer returns the underlying CALayer * pointer.
37 Layer() unsafe.Pointer
38 }
39
40 // MetalLayer is a Core Animation Metal layer, a layer that manages a pool of Metal drawables.
41 //
42 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer.
43 type MetalLayer struct {
44 metalLayer unsafe.Pointer
45 }
46
47 // MakeMetalLayer creates a new Core Animation Metal layer.
48 //
49 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer.
50 func MakeMetalLayer() MetalLayer {
51 return MetalLayer{C.MakeMetalLayer()}
52 }
53
54 // Layer implements the Layer interface.
55 func (ml MetalLayer) Layer() unsafe.Pointer { return ml.metalLayer }
56
57 // PixelFormat returns the pixel format of textures for rendering layer content.
58 //
59 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat.
60 func (ml MetalLayer) PixelFormat() mtl.PixelFormat {
61 return mtl.PixelFormat(C.MetalLayer_PixelFormat(ml.metalLayer))
62 }
63
64 // SetDevice sets the Metal device responsible for the layer's drawable resources.
65 //
66 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478163-device.
67 func (ml MetalLayer) SetDevice(device mtl.Device) {
68 C.MetalLayer_SetDevice(ml.metalLayer, device.Device())
69 }
70
71 // SetPixelFormat controls the pixel format of textures for rendering layer content.
72 //
73 // The pixel format for a Metal layer must be PixelFormatBGRA8UNorm, PixelFormatBGRA8UNormSRGB,
74 // PixelFormatRGBA16Float, PixelFormatBGRA10XR, or PixelFormatBGRA10XRSRGB.
75 // SetPixelFormat panics for other values.
76 //
77 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat.
78 func (ml MetalLayer) SetPixelFormat(pf mtl.PixelFormat) {
79 e := C.MetalLayer_SetPixelFormat(ml.metalLayer, C.uint16_t(pf))
80 if e != nil {
81 panic(errors.New(C.GoString(e)))
82 }
83 }
84
85 // SetMaximumDrawableCount controls the number of Metal drawables in the resource pool
86 // managed by Core Animation.
87 //
88 // It can set to 2 or 3 only. SetMaximumDrawableCount panics for other values.
89 //
90 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2938720-maximumdrawablecount.
91 func (ml MetalLayer) SetMaximumDrawableCount(count int) {
92 e := C.MetalLayer_SetMaximumDrawableCount(ml.metalLayer, C.uint_t(count))
93 if e != nil {
94 panic(errors.New(C.GoString(e)))
95 }
96 }
97
98 // SetDisplaySyncEnabled controls whether the Metal layer and its drawables
99 // are synchronized with the display's refresh rate.
100 //
101 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled.
102 func (ml MetalLayer) SetDisplaySyncEnabled(enabled bool) {
103 C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, C.bool(enabled))
104 }
105
106 // SetDrawableSize sets the size, in pixels, of textures for rendering layer content.
107 //
108 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478174-drawablesize.
109 func (ml MetalLayer) SetDrawableSize(width, height int) {
110 C.MetalLayer_SetDrawableSize(ml.metalLayer, C.double(width), C.double(height))
111 }
112
113 // NextDrawable returns a Metal drawable.
114 //
115 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478172-nextdrawable.
116 func (ml MetalLayer) NextDrawable() (MetalDrawable, error) {
117 md := C.MetalLayer_NextDrawable(ml.metalLayer)
118 if md == nil {
119 return MetalDrawable{}, errors.New("nextDrawable returned nil")
120 }
121
122 return MetalDrawable{md}, nil
123 }
124
125 // MetalDrawable is a displayable resource that can be rendered or written to by Metal.
126 //
127 // Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable.
128 type MetalDrawable struct {
129 metalDrawable unsafe.Pointer
130 }
131
132 // Drawable implements the mtl.Drawable interface.
133 func (md MetalDrawable) Drawable() unsafe.Pointer { return md.metalDrawable }
134
135 // Texture returns a Metal texture object representing the drawable object's content.
136 //
137 // Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable/1478159-texture.
138 func (md MetalDrawable) Texture() mtl.Texture {
139 return mtl.NewTexture(C.MetalDrawable_Texture(md.metalDrawable))
140 }