Skip to content

Commit

Permalink
GPU rendering with Metal
Browse files Browse the repository at this point in the history
  • Loading branch information
jslegendre committed Nov 22, 2021
1 parent ced59c9 commit 25f6404
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 88 deletions.
4 changes: 4 additions & 0 deletions TouchBar Simulator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1;
PRODUCT_BUNDLE_IDENTIFIER = "com.github.jslegendre.TouchBar-Simulator";
PRODUCT_NAME = "$(TARGET_NAME)";
SYSTEM_FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -445,6 +446,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1;
PRODUCT_BUNDLE_IDENTIFIER = "com.github.jslegendre.TouchBar-Simulator";
PRODUCT_NAME = "$(TARGET_NAME)";
SYSTEM_FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -464,6 +466,7 @@
HEADER_SEARCH_PATHS = "$(PROJECT_DIR)";
INFOPLIST_FILE = TouchBarSimulatorService/Info.plist;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1;
PRODUCT_BUNDLE_IDENTIFIER = com.github.jslegendre.TouchBarSimulatorService;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -484,6 +487,7 @@
HEADER_SEARCH_PATHS = "$(PROJECT_DIR)";
INFOPLIST_FILE = TouchBarSimulatorService/Info.plist;
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.1;
PRODUCT_BUNDLE_IDENTIFIER = com.github.jslegendre.TouchBarSimulatorService;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,4 @@
uuid = "5F44A03D-51A9-4BC0-969F-DDCD69FD825D"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "3624293E-E49A-4CCE-9D32-2273F05E2290"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "TouchBarSimulatorService/TouchBarSimulatorService.m"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "65"
endingLineNumber = "65"
landmarkName = "-awakeFromNib"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
8 changes: 6 additions & 2 deletions TouchBar Simulator/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ @implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.window.worksWhenModal = YES;
[self.window _setPreventsActivation:YES];
self.window.styleMask |= NSWindowStyleMaskUtilityWindow;
[self.window setIgnoresMouseEvents:NO];
// Touchbar pixel size @2x scaled is 2008 x 60
//self.window.aspectRatio = NSMakeSize(502, 15);

// Retina scaling (1004 x 30) + 10px padding is 1014 x 40
self.window.aspectRatio = NSMakeSize(507, 20); //NSMakeSize(169, 5);
self.window.movableByWindowBackground = NO;
self.window.contentView.wantsLayer = YES;
self.window.contentView.layer.backgroundColor = NSColor.blackColor.CGColor;
Expand All @@ -34,7 +39,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}];
}


- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
Expand Down
10 changes: 6 additions & 4 deletions TouchBar Simulator/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17147" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="19455" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17147"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19455"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -11,7 +12,7 @@
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate">
<connections>
<outlet property="window" destination="osO-BA-XhC" id="Lfe-qF-ako"/>
Expand Down Expand Up @@ -681,10 +682,11 @@
<point key="canvasLocation" x="-101" y="120"/>
</menu>
<window allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" frameAutosaveName="" animationBehavior="default" titlebarAppearsTransparent="YES" titleVisibility="hidden" id="osO-BA-XhC" customClass="NSPanel">
<windowStyleMask key="styleMask" titled="YES" closable="YES" utility="YES" nonactivatingPanel="YES" HUD="YES"/>
<windowStyleMask key="styleMask" titled="YES" closable="YES" resizable="YES" utility="YES" nonactivatingPanel="YES" HUD="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="75" y="103" width="1014" height="40"/>
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="775"/>
<value key="minSize" type="size" width="1014" height="40"/>
<view key="contentView" id="dzb-0s-QIR">
<rect key="frame" x="0.0" y="0.0" width="1014" height="40"/>
<autoresizingMask key="autoresizingMask"/>
Expand Down
6 changes: 3 additions & 3 deletions TouchBar Simulator/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSBackgroundOnly</key>
<true/>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
3 changes: 0 additions & 3 deletions TouchBar Simulator/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
}
return NSApplicationMain(argc, argv);
}
6 changes: 3 additions & 3 deletions TouchBarSimulatorService/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>NSPrincipalClass</key>
<string>NSViewServiceApplication</string>
<key>XPCService</key>
<dict>
<key>ServiceType</key>
<string>Application</string>
<key>JoinExistingSession</key>
<true/>
<key>RunLoopType</key>
<string>_NSApplicationMain</string>
<key>ServiceType</key>
<string>Application</string>
</dict>
</dict>
</plist>
58 changes: 42 additions & 16 deletions TouchBarSimulatorService/TouchBarSimulatorService.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//

#import <AppKit/AppKit.h>
#import <MetalKit/MetalKit.h>
#import <CoreImage/CoreImage.h>
#import "TouchBarSimulatorService.h"
#import "DFRFoundation.h"
@import QuartzCore;


@interface TouchBarSimulatorStreamView : NSView
Expand Down Expand Up @@ -42,11 +43,6 @@ - (void)mouseMoved:(NSEvent *)event {
[self mouseEvent:event];
}

//- (void)awakeFromNib {
// self.layer.backgroundColor = NSColor.blackColor.CGColor;
// self.wantsLayer = YES;
//}

- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
self.layer.backgroundColor = NSColor.blackColor.CGColor;
Expand All @@ -69,34 +65,64 @@ - (void)awakeFromNib {

@end

@interface TouchBarSimulatorService ()
@interface TouchBarSimulatorService () <MTKViewDelegate>
@property CGDisplayStreamRef touchBarStream;
@property DFRTouchBarSimulator *simulator;
@property (nonatomic,strong) MTKView *mtlView;
@property (nonatomic,strong) CIImage *frame;
@property (nonatomic,strong) id<MTLDevice> device;
@property (nonatomic,strong) id<MTLCommandQueue> commandQueue;
@property (nonatomic,strong) CIContext *context;
@end

@implementation TouchBarSimulatorService
- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size {}

- (void)drawInMTKView:(MTKView *)view {
if(!_frame) return;
id<MTLTexture> outputTexture = _mtlView.currentDrawable.texture;
if (!outputTexture) return;
if (_frame.extent.size.width == 0) return;

_mtlView.drawableSize = _frame.extent.size;
id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
[self.context render:_frame toMTLTexture:outputTexture commandBuffer:commandBuffer bounds:_frame.extent colorSpace:CGColorSpaceCreateDeviceRGB()];
[commandBuffer presentDrawable:self.mtlView.currentDrawable];
[commandBuffer commit];
}

- (void)viewDidLoad {
[super viewDidLoad];


self.frame = nil;
self.device = MTLCreateSystemDefaultDevice();
self.mtlView = [[MTKView alloc] initWithFrame:self.view.frame device:self.device];
self.mtlView.clearColor = MTLClearColorMake(0, 0, 0, 0);
self.mtlView.delegate = self;
self.mtlView.framebufferOnly = NO;
self.mtlView.autoResizeDrawable = YES;
self.mtlView.enableSetNeedsDisplay = YES;
self.context = [CIContext contextWithMTLDevice:self.device options:@{kCIContextWorkingColorSpace: (__bridge id)CGColorSpaceCreateDeviceRGB(), kCIContextUseSoftwareRenderer: [NSNumber numberWithBool:NO], kCIContextHighQualityDownsample: [NSNumber numberWithBool:YES]}];
self.commandQueue = [self.device newCommandQueue];
self.mtlView.frame = NSMakeRect(5, 5, 1004, 30);
[self.view addSubview:_mtlView];

TouchBarSimulatorStreamView *streamView = [[TouchBarSimulatorStreamView alloc] initWithFrame:NSMakeRect(5, 5, 1004, 30)];
[self.view addSubview:streamView];

self.simulator = DFRTouchBarSimulatorCreate(3, 0, 3);
DFRTouchBar *touchBar = DFRTouchBarSimulatorGetTouchBar(self.simulator);
[streamView setSimulator:self.simulator];

DFRTouchBar *touchBar = DFRTouchBarSimulatorGetTouchBar(self.simulator);
self.touchBarStream = DFRTouchBarCreateDisplayStream(touchBar, 0, dispatch_get_main_queue(), ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef _Nullable frameSurface, CGDisplayStreamUpdateRef _Nullable updateRef) {
[CATransaction begin];
[CATransaction setDisableActions:YES];
[streamView.layer setContents:(__bridge id _Nullable)(frameSurface)];
[CATransaction commit];
self->_frame = [CIImage imageWithIOSurface:frameSurface];
self->_mtlView.needsDisplay = YES;
// streamView.layer.contents = (__bridge id _Nullable)(frameSurface);
});

if (self.touchBarStream) {
CGDisplayStreamStart(self.touchBarStream);
}

[streamView setSimulator:self.simulator];
}

- (void)viewWillDisappear {
Expand Down
5 changes: 3 additions & 2 deletions TouchBarSimulatorService/TouchBarSimulatorService.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17147" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="19455" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17147"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19455"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down
38 changes: 1 addition & 37 deletions TouchBarSimulatorService/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,8 @@

#import <Foundation/Foundation.h>
#import <ViewBridge/ViewBridge.h>
#import "TouchBarSimulatorService.h"

//@interface ServiceDelegate : NSObject <NSXPCListenerDelegate>
//@end
//
//@implementation ServiceDelegate
//
//- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
// // This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
//
// // Configure the connection.
// // First, set the interface that the exported object implements.
// newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(TouchBarSimulatorServiceProtocol)];
//
// // Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object.
// TouchBarSimulatorService *exportedObject = [TouchBarSimulatorService new];
// newConnection.exportedObject = exportedObject;
//
// // Resuming the connection allows the system to deliver more incoming messages.
// [newConnection resume];
//
// // Returning YES from this method tells the system that you have accepted this connection. If you want to reject the connection for some reason, call -invalidate on the connection and return NO.
// return YES;
//}
//
//@end

int main(int argc, const char *argv[])
{
// // Create the delegate for the service.
// ServiceDelegate *delegate = [ServiceDelegate new];
//
// // Set up the one NSXPCListener for this service. It will handle all incoming connections.
// NSXPCListener *listener = [NSXPCListener serviceListener];
// listener.delegate = delegate;
//
// // Resuming the serviceListener starts this service. This method does not return.
// [listener resume];
int main(int argc, const char *argv[]) {
NSViewServiceApplicationMain();
return 0;
}

0 comments on commit 25f6404

Please sign in to comment.