|
| 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 | +// +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/ca. |
| 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 "coreanim.h" |
| 27 | +*/ |
| 28 | +import "C" |
| 29 | + |
| 30 | +// Layer is an object that manages image-based content and |
| 31 | +// allows you to perform animations on that content. |
| 32 | +// |
| 33 | +// Reference: https://developer.apple.com/documentation/quartzcore/calayer. |
| 34 | +type Layer interface { |
| 35 | + // Layer returns the underlying CALayer * pointer. |
| 36 | + Layer() unsafe.Pointer |
| 37 | +} |
| 38 | + |
| 39 | +// MetalLayer is a Core Animation Metal layer, a layer that manages a pool of Metal drawables. |
| 40 | +// |
| 41 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer. |
| 42 | +type MetalLayer struct { |
| 43 | + metalLayer unsafe.Pointer |
| 44 | +} |
| 45 | + |
| 46 | +// MakeMetalLayer creates a new Core Animation Metal layer. |
| 47 | +// |
| 48 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer. |
| 49 | +func MakeMetalLayer() MetalLayer { |
| 50 | + return MetalLayer{C.MakeMetalLayer()} |
| 51 | +} |
| 52 | + |
| 53 | +// Layer implements the Layer interface. |
| 54 | +func (ml MetalLayer) Layer() unsafe.Pointer { return ml.metalLayer } |
| 55 | + |
| 56 | +// PixelFormat returns the pixel format of textures for rendering layer content. |
| 57 | +// |
| 58 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat. |
| 59 | +func (ml MetalLayer) PixelFormat() mtl.PixelFormat { |
| 60 | + return mtl.PixelFormat(C.MetalLayer_PixelFormat(ml.metalLayer)) |
| 61 | +} |
| 62 | + |
| 63 | +// SetDevice sets the Metal device responsible for the layer's drawable resources. |
| 64 | +// |
| 65 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478163-device. |
| 66 | +func (ml MetalLayer) SetDevice(device mtl.Device) { |
| 67 | + C.MetalLayer_SetDevice(ml.metalLayer, device.Device()) |
| 68 | +} |
| 69 | + |
| 70 | +// SetPixelFormat controls the pixel format of textures for rendering layer content. |
| 71 | +// |
| 72 | +// The pixel format for a Metal layer must be PixelFormatBGRA8UNorm, PixelFormatBGRA8UNormSRGB, |
| 73 | +// PixelFormatRGBA16Float, PixelFormatBGRA10XR, or PixelFormatBGRA10XRSRGB. |
| 74 | +// SetPixelFormat panics for other values. |
| 75 | +// |
| 76 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat. |
| 77 | +func (ml MetalLayer) SetPixelFormat(pf mtl.PixelFormat) { |
| 78 | + e := C.MetalLayer_SetPixelFormat(ml.metalLayer, C.uint16_t(pf)) |
| 79 | + if e != nil { |
| 80 | + panic(errors.New(C.GoString(e))) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// SetMaximumDrawableCount controls the number of Metal drawables in the resource pool |
| 85 | +// managed by Core Animation. |
| 86 | +// |
| 87 | +// It can set to 2 or 3 only. SetMaximumDrawableCount panics for other values. |
| 88 | +// |
| 89 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2938720-maximumdrawablecount. |
| 90 | +func (ml MetalLayer) SetMaximumDrawableCount(count int) { |
| 91 | + e := C.MetalLayer_SetMaximumDrawableCount(ml.metalLayer, C.uint_t(count)) |
| 92 | + if e != nil { |
| 93 | + panic(errors.New(C.GoString(e))) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// SetDisplaySyncEnabled controls whether the Metal layer and its drawables |
| 98 | +// are synchronized with the display's refresh rate. |
| 99 | +// |
| 100 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled. |
| 101 | +func (ml MetalLayer) SetDisplaySyncEnabled(enabled bool) { |
| 102 | + C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, toCBool(enabled)) |
| 103 | +} |
| 104 | + |
| 105 | +// SetDrawableSize sets the size, in pixels, of textures for rendering layer content. |
| 106 | +// |
| 107 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478174-drawablesize. |
| 108 | +func (ml MetalLayer) SetDrawableSize(width, height int) { |
| 109 | + C.MetalLayer_SetDrawableSize(ml.metalLayer, C.double(width), C.double(height)) |
| 110 | +} |
| 111 | + |
| 112 | +// NextDrawable returns a Metal drawable. |
| 113 | +// |
| 114 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478172-nextdrawable. |
| 115 | +func (ml MetalLayer) NextDrawable() (MetalDrawable, error) { |
| 116 | + md := C.MetalLayer_NextDrawable(ml.metalLayer) |
| 117 | + if md == nil { |
| 118 | + return MetalDrawable{}, errors.New("nextDrawable returned nil") |
| 119 | + } |
| 120 | + |
| 121 | + return MetalDrawable{md}, nil |
| 122 | +} |
| 123 | + |
| 124 | +// MetalDrawable is a displayable resource that can be rendered or written to by Metal. |
| 125 | +// |
| 126 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable. |
| 127 | +type MetalDrawable struct { |
| 128 | + metalDrawable unsafe.Pointer |
| 129 | +} |
| 130 | + |
| 131 | +// Drawable implements the mtl.Drawable interface. |
| 132 | +func (md MetalDrawable) Drawable() unsafe.Pointer { return md.metalDrawable } |
| 133 | + |
| 134 | +// Texture returns a Metal texture object representing the drawable object's content. |
| 135 | +// |
| 136 | +// Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable/1478159-texture. |
| 137 | +func (md MetalDrawable) Texture() mtl.Texture { |
| 138 | + return mtl.NewTexture(C.MetalDrawable_Texture(md.metalDrawable)) |
| 139 | +} |
| 140 | + |
| 141 | +func toCBool(b bool) C.BOOL { |
| 142 | + if b { |
| 143 | + return 1 |
| 144 | + } |
| 145 | + return 0 |
| 146 | +} |
0 commit comments