Misc: fix bugs that were preventing starting a new stream after disconnecting. Known issue: this leaks a bit of memory. This also removes all OSX-specific code from the Apple example I used, as I have no intention of using this on Mac.
This commit is contained in:
@@ -158,6 +158,11 @@ CFStringRef __currentColorSpace;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
Log(LOG_I, @"MetalVideoRenderer dealloc");
|
||||
|
||||
if (_commandQueue) {
|
||||
_commandQueue = nil;
|
||||
}
|
||||
if (_CscParamsBuffer) {
|
||||
_CscParamsBuffer = nil;
|
||||
}
|
||||
@@ -172,6 +177,9 @@ CFStringRef __currentColorSpace;
|
||||
if (_renderPassDescriptor) {
|
||||
_renderPassDescriptor = nil;
|
||||
}
|
||||
if (_textureCache) {
|
||||
_textureCache = nil;
|
||||
}
|
||||
if (__currentColorSpace) {
|
||||
CFRelease(__currentColorSpace);
|
||||
}
|
||||
@@ -179,12 +187,7 @@ CFStringRef __currentColorSpace;
|
||||
|
||||
#if !TARGET_OS_TV
|
||||
- (void)reportMaxEDRHeadroom {
|
||||
CGFloat maxHeadroom = 1.0f;
|
||||
#if TARGET_OS_OSX
|
||||
maxHeadroom = [[NSScreen mainScreen] maximumPotentialExtendedDynamicRangeColorComponentValue];
|
||||
#else
|
||||
maxHeadroom = [[UIScreen mainScreen] potentialEDRHeadroom];
|
||||
#endif
|
||||
CGFloat maxHeadroom = [[UIScreen mainScreen] potentialEDRHeadroom];
|
||||
if (maxHeadroom > 1.0) {
|
||||
LogOnce(LOG_I, @"Display supports EDR with a max headroom of %.1f", maxHeadroom);
|
||||
} else {
|
||||
@@ -193,12 +196,7 @@ CFStringRef __currentColorSpace;
|
||||
}
|
||||
|
||||
- (void)pollCurrentEDRHeadroom {
|
||||
CGFloat headroom = 1.0f;
|
||||
#if TARGET_OS_OSX
|
||||
headroom = [[NSScreen mainScreen] maximumExtendedDynamicRangeColorComponentValue];
|
||||
#else
|
||||
headroom = [[UIScreen mainScreen] currentEDRHeadroom];
|
||||
#endif
|
||||
CGFloat headroom = [[UIScreen mainScreen] currentEDRHeadroom];
|
||||
if (headroom != _currentEDRHeadroom) {
|
||||
Log(LOG_I, @"EDR headroom changed to %.1f", headroom);
|
||||
_currentEDRHeadroom = (float)headroom;
|
||||
@@ -448,7 +446,7 @@ CFStringRef __currentColorSpace;
|
||||
- (void)renderFrame:(Frame *)frame toLayer:(CAMetalLayer *)layer {
|
||||
@autoreleasepool {
|
||||
if (self.isStopping) {
|
||||
Log(LOG_I, @"XXX Metal renderThread is stopping. returning from renderFrame");
|
||||
Log(LOG_I, @"[MetalVideoRenderer] isStopping");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -573,10 +571,12 @@ CFStringRef __currentColorSpace;
|
||||
|
||||
[renderEncoder setVertexBuffer:_VideoVertexBuffer offset:0 atIndex:0];
|
||||
[renderEncoder setFragmentBuffer:_CscParamsBuffer offset:0 atIndex:0];
|
||||
#if !TARGET_OS_TV
|
||||
if (layer.pixelFormat == MTLPixelFormatRGBA16Float) {
|
||||
[self pollCurrentEDRHeadroom];
|
||||
[renderEncoder setFragmentBytes:&_currentEDRHeadroom length:sizeof(float) atIndex:1];
|
||||
}
|
||||
#endif
|
||||
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
|
||||
[renderEncoder endEncoding];
|
||||
|
||||
@@ -617,8 +617,6 @@ CFStringRef __currentColorSpace;
|
||||
#endif
|
||||
|
||||
[commandBuffer commit];
|
||||
|
||||
// Wait for the command buffer to complete and free our CVMetalTextureCache references
|
||||
[commandBuffer waitUntilCompleted];
|
||||
}
|
||||
}
|
||||
@@ -633,12 +631,14 @@ CFStringRef __currentColorSpace;
|
||||
}
|
||||
|
||||
- (void)shutdown {
|
||||
Log(LOG_I, @"XXX MetalVideoRenderer shutodwn");
|
||||
self.isStopping = YES;
|
||||
if (!self.isStopping) {
|
||||
self.isStopping = YES;
|
||||
Log(LOG_I, @"[MetalVideoRenderer] shutdown");
|
||||
|
||||
// Ensure no rendering is in flight
|
||||
for (NSUInteger i = 0; i < MaxFramesInFlight; i++) {
|
||||
dispatch_semaphore_signal(_inFlightSemaphore);
|
||||
// Ensure no rendering is in flight
|
||||
for (NSUInteger i = 0; i < MaxFramesInFlight; i++) {
|
||||
dispatch_semaphore_signal(_inFlightSemaphore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
#import <Metal/Metal.h>
|
||||
#import <QuartzCore/CAMetalDisplayLink.h>
|
||||
#import <QuartzCore/CAMetalLayer.h>
|
||||
#import "MetalConfig.h"
|
||||
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
#import <UIKit/UIKit.h>
|
||||
#define PlatformView UIView
|
||||
#else
|
||||
#import <AppKit/AppKit.h>
|
||||
#define PlatformView NSView
|
||||
#endif
|
||||
#import "MetalConfig.h"
|
||||
|
||||
// The protocol to provide resize and redraw callbacks to a delegate.
|
||||
@protocol MetalViewDelegate <NSObject>
|
||||
@@ -17,15 +10,13 @@
|
||||
- (void)drawableResize:(CGSize)size;
|
||||
- (void)renderTo:(nonnull CAMetalLayer *)layer;
|
||||
- (void)waitToRenderTo:(nonnull CAMetalLayer *)layer;
|
||||
- (void)shutdown;
|
||||
|
||||
@end
|
||||
|
||||
// The Metal game view base class.
|
||||
@interface MetalView : PlatformView <CALayerDelegate>
|
||||
@interface MetalView : UIView <CALayerDelegate>
|
||||
|
||||
@property (nonatomic, nonnull, readonly) CAMetalLayer *metalLayer;
|
||||
@property (nonatomic, getter=isPaused) BOOL paused;
|
||||
@property (nonatomic, nullable) id<MetalViewDelegate> delegate;
|
||||
@property (nonatomic) float framerate;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// This is based on the following Apple example:
|
||||
// This is based on the following Apple example
|
||||
// https://developer.apple.com/documentation/metal/achieving-smooth-frame-rates-with-a-metal-display-link?language=objc
|
||||
// https://developer.apple.com/wwdc23/10123/
|
||||
|
||||
@@ -29,29 +29,23 @@
|
||||
}
|
||||
|
||||
- (void)initCommon {
|
||||
#if TARGET_OS_OSX
|
||||
self.wantsLayer = YES;
|
||||
|
||||
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
|
||||
#endif
|
||||
|
||||
_metalLayer = (CAMetalLayer *)self.layer;
|
||||
|
||||
self.layer.delegate = self;
|
||||
}
|
||||
|
||||
- (void)shutdown {
|
||||
if (_renderThread) {
|
||||
Log(LOG_I, @"[MetalView] sending renderThread a cancel message");
|
||||
[_renderThread cancel];
|
||||
// wait for thread to exist
|
||||
Log(LOG_I, @"[MetalView] waiting on renderThread to finish");
|
||||
while (!_renderThread.isFinished) {
|
||||
Log(LOG_I, @"XXX MetalView waiting on renderThread to finish");
|
||||
usleep(100);
|
||||
}
|
||||
Log(LOG_I, @"[MetalView] renderThread has finished");
|
||||
_renderThread = nil;
|
||||
}
|
||||
}
|
||||
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
+ (Class)layerClass {
|
||||
return [CAMetalLayer class];
|
||||
}
|
||||
@@ -59,29 +53,11 @@
|
||||
- (void)didMoveToWindow {
|
||||
[self movedToWindow];
|
||||
}
|
||||
#else
|
||||
- (CALayer *)makeBackingLayer {
|
||||
return [CAMetalLayer layer];
|
||||
}
|
||||
|
||||
- (void)viewDidMoveToWindow {
|
||||
[self movedToWindow];
|
||||
}
|
||||
#endif // END TARGET_OS_IOS || TARGET_OS_TV
|
||||
|
||||
- (void)movedToWindow {
|
||||
if (!self.window) {
|
||||
return;
|
||||
|
||||
// We have been removed
|
||||
if (_renderThread) {
|
||||
[_renderThread cancel];
|
||||
// wait for thread to exist
|
||||
while (!_renderThread.isFinished) {
|
||||
Log(LOG_I, @"XXX MetalView waiting on renderThread to finish");
|
||||
usleep(100);
|
||||
}
|
||||
}
|
||||
Log(LOG_I, @"[MetalView] movedToWindow(nil): shutting down...");
|
||||
[self shutdown];
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -93,21 +69,18 @@
|
||||
[self.delegate renderTo:self.metalLayer];
|
||||
}
|
||||
}
|
||||
Log(LOG_I, @"XXX Metal renderThread shutting down");
|
||||
Log(LOG_I, @"[MetalView] renderThread is exiting");
|
||||
}];
|
||||
_renderThread.name = @"MetalVideoRenderer";
|
||||
_renderThread.qualityOfService = NSQualityOfServiceUserInteractive;
|
||||
[_renderThread start];
|
||||
Log(LOG_I, @"[MetalView] started renderThread %@", _renderThread);
|
||||
|
||||
// Perform any actions that need to know the size and scale of the drawable. When UIKit calls
|
||||
// didMoveToWindow after the view initialization, this is the first opportunity to notify
|
||||
// components of the drawable's size.
|
||||
#if AUTOMATICALLY_RESIZE
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
[self resizeDrawable:self.window.screen.nativeScale];
|
||||
#else
|
||||
[self resizeDrawable:self.window.screen.backingScaleFactor];
|
||||
#endif
|
||||
#else
|
||||
// Notify the delegate of the default drawable size when the system can calculate it.
|
||||
CGSize defaultDrawableSize = self.bounds.size;
|
||||
@@ -123,7 +96,6 @@
|
||||
|
||||
// Override all methods that indicate the view's size has changed.
|
||||
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
- (void)setContentScaleFactor:(CGFloat)contentScaleFactor {
|
||||
[super setContentScaleFactor:contentScaleFactor];
|
||||
[self resizeDrawable:self.window.screen.nativeScale];
|
||||
@@ -143,22 +115,6 @@
|
||||
[super setBounds:bounds];
|
||||
[self resizeDrawable:self.window.screen.nativeScale];
|
||||
}
|
||||
#else
|
||||
- (void)viewDidChangeBackingProperties {
|
||||
[super viewDidChangeBackingProperties];
|
||||
[self resizeDrawable:self.window.screen.backingScaleFactor];
|
||||
}
|
||||
|
||||
- (void)setFrameSize:(NSSize)size {
|
||||
[super setFrameSize:size];
|
||||
[self resizeDrawable:self.window.screen.backingScaleFactor];
|
||||
}
|
||||
|
||||
- (void)setBoundsSize:(NSSize)size {
|
||||
[super setBoundsSize:size];
|
||||
[self resizeDrawable:self.window.screen.backingScaleFactor];
|
||||
}
|
||||
#endif
|
||||
|
||||
- (void)resizeDrawable:(CGFloat)scaleFactor {
|
||||
CGSize newSize = self.bounds.size;
|
||||
|
||||
@@ -1,18 +1,11 @@
|
||||
#import <Metal/Metal.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "FrameQueue.h"
|
||||
#import "ImGuiRenderer.h"
|
||||
#import "MetalVideoRenderer.h"
|
||||
#import "MetalView.h"
|
||||
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
#import <UIKit/UIKit.h>
|
||||
#define PlatformViewController UIViewController
|
||||
#else
|
||||
#import <AppKit/AppKit.h>
|
||||
#define PlatformViewController NSViewController
|
||||
#endif
|
||||
|
||||
@interface MetalViewController : PlatformViewController <MetalViewDelegate>
|
||||
@interface MetalViewController : UIViewController <MetalViewDelegate>
|
||||
|
||||
@property (nonatomic) CGRect bounds;
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ The implementation of the cross-platform game view controller.
|
||||
|
||||
- (void)loadView {
|
||||
self.view = [[MetalView alloc] initWithFrame:_bounds];
|
||||
Log(LOG_I, @"[MetalViewController] created MetalView %@", (MetalView *)self.view);
|
||||
}
|
||||
|
||||
- (void)viewDidLoad {
|
||||
@@ -51,7 +52,7 @@ The implementation of the cross-platform game view controller.
|
||||
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
|
||||
if (!device) {
|
||||
Log(LOG_E, @"Metal isn't supported on this device.");
|
||||
self.view = [[PlatformView alloc] initWithFrame:self.view.frame];
|
||||
self.view = [[UIView alloc] initWithFrame:self.view.frame];
|
||||
return;
|
||||
}
|
||||
view.metalLayer.device = device;
|
||||
@@ -64,12 +65,12 @@ The implementation of the cross-platform game view controller.
|
||||
Log(LOG_E, @"The renderer couldn't be initialized.");
|
||||
return;
|
||||
}
|
||||
self->_renderer = renderer;
|
||||
Log(LOG_I, @"[MetalViewController] viewDidLoad, created renderer: %@", renderer);
|
||||
|
||||
// Initialize the renderer-dependent view properties.
|
||||
view.metalLayer.pixelFormat = renderer.colorPixelFormat;
|
||||
view.metalLayer.maximumDrawableCount = 3;
|
||||
|
||||
self->_renderer = renderer;
|
||||
}
|
||||
|
||||
- (void)waitToRenderTo:(nonnull CAMetalLayer *)layer {
|
||||
@@ -95,46 +96,20 @@ The implementation of the cross-platform game view controller.
|
||||
[_renderer drawableResize:size];
|
||||
}
|
||||
|
||||
- (void)shutdown {
|
||||
[_renderer shutdown];
|
||||
}
|
||||
|
||||
- (void)viewDidDisappear:(BOOL)animated {
|
||||
[super viewDidDisappear:animated];
|
||||
|
||||
Log(LOG_I, @"XXX MetalViewController viewDidDisappear");
|
||||
Log(LOG_I, @"[MetalViewController] viewDidDisappear");
|
||||
|
||||
[_metalView shutdown];
|
||||
[_renderer shutdown];
|
||||
_renderer = nil;
|
||||
}
|
||||
|
||||
#if TARGET_OS_IOS
|
||||
/// Hides the Home indicator button automatically.
|
||||
// Hides the Home indicator button automatically.
|
||||
- (BOOL)prefersHomeIndicatorAutoHidden {
|
||||
return YES;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if TARGET_OS_OSX
|
||||
/// Makes the view controller the first responder to receive keyboard events.
|
||||
- (void)viewDidAppear {
|
||||
[_metalView.window makeFirstResponder:self];
|
||||
}
|
||||
|
||||
/// Receives the keydown events to avoid system beeps.
|
||||
///
|
||||
/// The `GameInputKeyboardMouse` class handles keyboard events.
|
||||
- (void)keyDown:(NSEvent *)event {
|
||||
// Reference the parameter to avoid an unused parameter warning.
|
||||
(void)(event);
|
||||
}
|
||||
|
||||
/// Receives the keyup events to avoid system beeps.
|
||||
///
|
||||
/// The `GameInputKeyboardMouse` class handles keyboard events.
|
||||
- (void)keyUp:(NSEvent *)event {
|
||||
// Reference the parameter to avoid an unused parameter warning.
|
||||
(void)(event);
|
||||
}
|
||||
#endif
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user