view.go (2381B)
1 // Copyright 2019 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 metal 18 19 import ( 20 "sync" 21 22 "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca" 23 "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl" 24 ) 25 26 type view struct { 27 window uintptr 28 uiview uintptr 29 30 windowChanged bool 31 vsync bool 32 33 device mtl.Device 34 ml ca.MetalLayer 35 36 once sync.Once 37 } 38 39 func (v *view) setDrawableSize(width, height int) { 40 v.ml.SetDrawableSize(width, height) 41 } 42 43 func (v *view) getMTLDevice() mtl.Device { 44 return v.device 45 } 46 47 func (v *view) setDisplaySyncEnabled(enabled bool) { 48 // TODO: Now SetVsyncEnabled is called only from the main thread, and d.t.Run is not available since 49 // recursive function call via Run is forbidden. 50 // Fix this to use d.t.Run to avoid confusion. 51 v.ml.SetDisplaySyncEnabled(enabled) 52 v.vsync = enabled 53 } 54 55 func (v *view) colorPixelFormat() mtl.PixelFormat { 56 return v.ml.PixelFormat() 57 } 58 59 func (v *view) reset() error { 60 var err error 61 v.device, err = mtl.CreateSystemDefaultDevice() 62 if err != nil { 63 return err 64 } 65 66 v.ml = ca.MakeMetalLayer() 67 v.ml.SetDevice(v.device) 68 // https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat 69 // 70 // The pixel format for a Metal layer must be MTLPixelFormatBGRA8Unorm, 71 // MTLPixelFormatBGRA8Unorm_sRGB, MTLPixelFormatRGBA16Float, MTLPixelFormatBGRA10_XR, or 72 // MTLPixelFormatBGRA10_XR_sRGB. 73 v.ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm) 74 v.ml.SetMaximumDrawableCount(3) 75 76 // The vsync state might be reset. Set the state again (#1364). 77 v.ml.SetDisplaySyncEnabled(v.vsync) 78 return nil 79 } 80 81 func (v *view) drawable() ca.MetalDrawable { 82 d, err := v.ml.NextDrawable() 83 if err != nil { 84 // Drawable is nil. This can happen at the initial state. Let's wait and see. 85 return ca.MetalDrawable{} 86 } 87 return d 88 }