From ec26b1a43bf194a36e1965b009203f87b6e83cca Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 8 Aug 2021 15:13:44 +0900 Subject: [PATCH] internal/graphicsdriver/metal: Refactoring: Remove vsyncInited --- internal/graphicsdriver/metal/ca/ca.go | 11 +++++++++++ internal/graphicsdriver/metal/ca/ca.h | 2 ++ internal/graphicsdriver/metal/ca/ca.m | 14 ++++++-------- internal/graphicsdriver/metal/view.go | 20 ++++++++++++-------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/internal/graphicsdriver/metal/ca/ca.go b/internal/graphicsdriver/metal/ca/ca.go index c8582ede7..a8febd527 100644 --- a/internal/graphicsdriver/metal/ca/ca.go +++ b/internal/graphicsdriver/metal/ca/ca.go @@ -154,6 +154,17 @@ func (ml MetalLayer) PresentsWithTransaction() bool { return C.MetalLayer_PresentsWithTransaction(ml.metalLayer) != 0 } +// SetPresentsWithTransaction sets a Boolean value that determines whether the layer presents its content using a Core Animation transaction. +// +// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478157-presentswithtransaction +func (ml MetalLayer) SetPresentsWithTransaction(presentsWithTransaction bool) { + if presentsWithTransaction { + C.MetalLayer_SetPresentsWithTransaction(ml.metalLayer, 1) + } else { + C.MetalLayer_SetPresentsWithTransaction(ml.metalLayer, 0) + } +} + // SetFramebufferOnly sets a Boolean value that determines whether the layer’s textures are used only for rendering. // // https://developer.apple.com/documentation/quartzcore/cametallayer/1478168-framebufferonly diff --git a/internal/graphicsdriver/metal/ca/ca.h b/internal/graphicsdriver/metal/ca/ca.h index afa8f2add..2c0ed8ba8 100644 --- a/internal/graphicsdriver/metal/ca/ca.h +++ b/internal/graphicsdriver/metal/ca/ca.h @@ -29,6 +29,8 @@ const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer, void MetalLayer_SetDisplaySyncEnabled(void *metalLayer, uint8_t displaySyncEnabled); void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height); +void MetalLayer_SetPresentsWithTransaction(void *metalLayer, + uint8_t presentsWithTransaction); void *MetalLayer_NextDrawable(void *metalLayer); void MetalLayer_SetFramebufferOnly(void *metalLayer, uint8_t framebufferOnly); uint8_t MetalLayer_PresentsWithTransaction(void *metalLayer); diff --git a/internal/graphicsdriver/metal/ca/ca.m b/internal/graphicsdriver/metal/ca/ca.m index 11d13921e..ff1ae6190 100644 --- a/internal/graphicsdriver/metal/ca/ca.m +++ b/internal/graphicsdriver/metal/ca/ca.m @@ -85,20 +85,18 @@ void MetalLayer_SetDisplaySyncEnabled(void *metalLayer, [((CAMetalLayer *)metalLayer) setDisplaySyncEnabled:displaySyncEnabled]; } #endif - - // setting presentsWithTransaction YES makes the FPS stable (#1196). We're not - // sure why... - if (displaySyncEnabled) { - [((CAMetalLayer *)metalLayer) setPresentsWithTransaction:YES]; - } else { - [((CAMetalLayer *)metalLayer) setPresentsWithTransaction:NO]; - } } void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height) { ((CAMetalLayer *)metalLayer).drawableSize = (CGSize){width, height}; } +void MetalLayer_SetPresentsWithTransaction(void *metalLayer, + uint8_t presentsWithTransaction) { + [((CAMetalLayer *)metalLayer) + setPresentsWithTransaction:presentsWithTransaction]; +} + void *MetalLayer_NextDrawable(void *metalLayer) { return [(CAMetalLayer *)metalLayer nextDrawable]; } diff --git a/internal/graphicsdriver/metal/view.go b/internal/graphicsdriver/metal/view.go index 10a12a3c2..51df46dc0 100644 --- a/internal/graphicsdriver/metal/view.go +++ b/internal/graphicsdriver/metal/view.go @@ -29,8 +29,7 @@ type view struct { uiview uintptr windowChanged bool - vsync bool - vsyncInited bool + vsyncDisabled bool device mtl.Device ml ca.MetalLayer @@ -47,12 +46,19 @@ func (v *view) getMTLDevice() mtl.Device { } func (v *view) setDisplaySyncEnabled(enabled bool) { - if v.vsync == enabled && !v.vsyncInited { + if !v.vsyncDisabled == enabled { return } + v.forceSetDisplaySyncEnabled(enabled) +} + +func (v *view) forceSetDisplaySyncEnabled(enabled bool) { v.ml.SetDisplaySyncEnabled(enabled) - v.vsync = enabled - v.vsyncInited = true + + // setting presentsWithTransaction true makes the FPS stable (#1196). We're not sure why... + v.ml.SetPresentsWithTransaction(enabled) + + v.vsyncDisabled = !enabled } func (v *view) colorPixelFormat() mtl.PixelFormat { @@ -80,9 +86,7 @@ func (v *view) reset() error { v.ml.SetMaximumDrawableCount(2) // The vsync state might be reset. Set the state again (#1364). - if v.vsyncInited { - v.ml.SetDisplaySyncEnabled(v.vsync) - } + v.forceSetDisplaySyncEnabled(!v.vsyncDisabled) v.ml.SetFramebufferOnly(true) return nil