twitchapon-anim

Basic Twitchapon Receiver/Visuals
git clone git://bsandro.tech/twitchapon-anim
Log | Files | Refs | README | LICENSE

ca.go (6018B)


      1 // Copyright 2018 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 // +build darwin
     16 
     17 // Package ca provides access to Apple's Core Animation API (https://developer.apple.com/documentation/quartzcore).
     18 //
     19 // This package is in very early stages of development.
     20 // It's a minimal implementation with scope limited to
     21 // supporting the movingtriangle example.
     22 package ca
     23 
     24 import (
     25 	"errors"
     26 	"unsafe"
     27 
     28 	"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
     29 )
     30 
     31 // Suppress the warnings about availability guard with -Wno-unguarded-availability-new.
     32 // It is because old Xcode (8 or older?) does not accept @available syntax.
     33 
     34 // #cgo CFLAGS: -Wno-unguarded-availability-new
     35 // #cgo !ios CFLAGS: -mmacosx-version-min=10.12
     36 // #cgo LDFLAGS: -framework QuartzCore -framework Foundation -framework CoreGraphics
     37 //
     38 // #include "ca.h"
     39 import "C"
     40 
     41 // Layer is an object that manages image-based content and
     42 // allows you to perform animations on that content.
     43 //
     44 // Reference: https://developer.apple.com/documentation/quartzcore/calayer.
     45 type Layer interface {
     46 	// Layer returns the underlying CALayer * pointer.
     47 	Layer() unsafe.Pointer
     48 }
     49 
     50 // MetalLayer is a Core Animation Metal layer, a layer that manages a pool of Metal drawables.
     51 //
     52 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer.
     53 type MetalLayer struct {
     54 	metalLayer unsafe.Pointer
     55 }
     56 
     57 // MakeMetalLayer creates a new Core Animation Metal layer.
     58 //
     59 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer.
     60 func MakeMetalLayer() MetalLayer {
     61 	return MetalLayer{C.MakeMetalLayer()}
     62 }
     63 
     64 // Layer implements the Layer interface.
     65 func (ml MetalLayer) Layer() unsafe.Pointer { return ml.metalLayer }
     66 
     67 // PixelFormat returns the pixel format of textures for rendering layer content.
     68 //
     69 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat.
     70 func (ml MetalLayer) PixelFormat() mtl.PixelFormat {
     71 	return mtl.PixelFormat(C.MetalLayer_PixelFormat(ml.metalLayer))
     72 }
     73 
     74 // SetDevice sets the Metal device responsible for the layer's drawable resources.
     75 //
     76 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478163-device.
     77 func (ml MetalLayer) SetDevice(device mtl.Device) {
     78 	C.MetalLayer_SetDevice(ml.metalLayer, device.Device())
     79 }
     80 
     81 // SetOpaque a Boolean value indicating whether the layer contains completely opaque content.
     82 func (ml MetalLayer) SetOpaque(opaque bool) {
     83 	if opaque {
     84 		C.MetalLayer_SetOpaque(ml.metalLayer, 1)
     85 	} else {
     86 		C.MetalLayer_SetOpaque(ml.metalLayer, 0)
     87 	}
     88 }
     89 
     90 // SetPixelFormat controls the pixel format of textures for rendering layer content.
     91 //
     92 // The pixel format for a Metal layer must be PixelFormatBGRA8UNorm, PixelFormatBGRA8UNormSRGB,
     93 // PixelFormatRGBA16Float, PixelFormatBGRA10XR, or PixelFormatBGRA10XRSRGB.
     94 // SetPixelFormat panics for other values.
     95 //
     96 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat.
     97 func (ml MetalLayer) SetPixelFormat(pf mtl.PixelFormat) {
     98 	e := C.MetalLayer_SetPixelFormat(ml.metalLayer, C.uint16_t(pf))
     99 	if e != nil {
    100 		panic(errors.New(C.GoString(e)))
    101 	}
    102 }
    103 
    104 // SetMaximumDrawableCount controls the number of Metal drawables in the resource pool
    105 // managed by Core Animation.
    106 //
    107 // It can set to 2 or 3 only. SetMaximumDrawableCount panics for other values.
    108 //
    109 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2938720-maximumdrawablecount.
    110 func (ml MetalLayer) SetMaximumDrawableCount(count int) {
    111 	e := C.MetalLayer_SetMaximumDrawableCount(ml.metalLayer, C.uint_t(count))
    112 	if e != nil {
    113 		panic(errors.New(C.GoString(e)))
    114 	}
    115 }
    116 
    117 // SetDisplaySyncEnabled controls whether the Metal layer and its drawables
    118 // are synchronized with the display's refresh rate.
    119 //
    120 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled.
    121 func (ml MetalLayer) SetDisplaySyncEnabled(enabled bool) {
    122 	switch enabled {
    123 	case true:
    124 		C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, 1)
    125 	case false:
    126 		C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, 0)
    127 	}
    128 }
    129 
    130 // SetDrawableSize sets the size, in pixels, of textures for rendering layer content.
    131 //
    132 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478174-drawablesize.
    133 func (ml MetalLayer) SetDrawableSize(width, height int) {
    134 	C.MetalLayer_SetDrawableSize(ml.metalLayer, C.double(width), C.double(height))
    135 }
    136 
    137 // NextDrawable returns a Metal drawable.
    138 //
    139 // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478172-nextdrawable.
    140 func (ml MetalLayer) NextDrawable() (MetalDrawable, error) {
    141 	md := C.MetalLayer_NextDrawable(ml.metalLayer)
    142 	if md == nil {
    143 		return MetalDrawable{}, errors.New("nextDrawable returned nil")
    144 	}
    145 
    146 	return MetalDrawable{md}, nil
    147 }
    148 
    149 // MetalDrawable is a displayable resource that can be rendered or written to by Metal.
    150 //
    151 // Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable.
    152 type MetalDrawable struct {
    153 	metalDrawable unsafe.Pointer
    154 }
    155 
    156 // Drawable implements the mtl.Drawable interface.
    157 func (md MetalDrawable) Drawable() unsafe.Pointer { return md.metalDrawable }
    158 
    159 // Texture returns a Metal texture object representing the drawable object's content.
    160 //
    161 // Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable/1478159-texture.
    162 func (md MetalDrawable) Texture() mtl.Texture {
    163 	return mtl.NewTexture(C.MetalDrawable_Texture(md.metalDrawable))
    164 }