Sample CodeiOS, iPadOS, Mac Catalyst, macOS, tvOSReviewed 2026-07-21View on Apple Developer

Achieving smooth frame rates with a Metal display link

At a glance

Item Summary
Purpose Pace rendering with minimal input latency while providing essential information to the operating system for power-efficient rendering, thermal mitigation, and the scheduling of sustainable workloads.
App architecture A C/Objective-C header, Metal, Objective-C, Objective-C++ sample bundle with entry-bearing project variants iOS, macOS, tvOS, each leading to Metal APIs.
Main patterns View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks
Project style 23 scanned source file(s) across C/Objective-C header, Metal, Objective-C, Objective-C++, organized around ranked entry, type, and file boundaries.
Execution model No structured execution marker indexed; callback threading requires source review.
State/event model No structured observation or publisher-scheduling marker indexed.
Key frameworks/packages QuartzCore, simd, UIKit, Metal, AppKit; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── iOS/
│   └── main.m
├── macOS/
│   └── main.m
├── tvOS/
│   └── main.m
└── Shared/
    ├── AppDelegate.h
    ├── AppDelegate.m
    ├── GameView.h
    ├── ShaderTypes.h
    ├── AssetLoader.h
    ├── AssetLoader.mm
    ├── GameView.m
    ├── GameViewController.h
    └── GameViewController.mm

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: C/Objective-C header, Metal, Objective-C, Objective-C++.
  • The verified tree contains 9 project/configuration file(s) and 16 source declaration(s).

Overall architecture

Reference code

iOS/main.m:11 — architecture anchor

int main(int argc, char * argv[]) {
    NSString * appDelegateClassName;
    @autoreleasepool {
        // Setup code that might create autoreleased objects goes here.
        appDelegateClassName = NSStringFromClass([AppDelegate class]);
    }
    return UIApplicationMain(argc, argv, nil, appDelegateClassName);
}

Interpretation

The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.

Ownership and state

Ownership evidence

Shared/AppDelegate.h:20 — stored dependency or nearest verified ownership anchor

#if TARGET_IOS || TARGET_TVOS
// ...
#else
@property(strong, nonatomic) IBOutlet NSWindow *window;
#endif
Owner Object or state Relationship Mutation authority
AppDelegate NSWindow (window) retains or copies an assigned value Header-visible collaborators
GameView CAMetalLayer (metalLayer) stores or receives The declaring implementation writes; property clients read
GameView BOOL (paused) stores or receives Header-visible collaborators
GameView id (delegate) stores or receives Header-visible collaborators

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

No source-visible execution, scheduling, or synchronization boundary was found in the indexed source.

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
Source import QuartzCore The cited file imports this module; runtime use and architectural role are not inferred. Shared/GameInput.mm:8
Source import simd The cited file imports this module; runtime use and architectural role are not inferred. Shared/GameInput.h:13
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. Shared/AppDelegate.h:9
Source import Metal The cited file imports this module; runtime use and architectural role are not inferred. Shared/AssetLoader.h:8
Source import AppKit The cited file imports this module; runtime use and architectural role are not inferred. Shared/GameView.h:17
Source import Cocoa The cited file imports this module; runtime use and architectural role are not inferred. Shared/AppDelegate.h:11

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

Shared/GameView.h:22 — representative type boundary

@protocol GameViewDelegate <NSObject>
// ...
@end
Type Responsibility Depends on or conforms to
GameViewDelegate Defines a capability or collaboration contract NSObject
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
GameView User-interface presentation and input forwarding PlatformView, CALayerDelegate, CAMetalDisplayLinkDelegate
AssetLoader Loads and prepares feature data or resources NSObject
GameViewController View lifecycle, callbacks, and feature coordination PlatformViewController, GameViewDelegate
Renderer Owns drawing, GPU, or presentation processing NSObject
WindowSceneDelegate Receives callback-driven events UIResponder, UIWindowSceneDelegate
FrameData Represents feature data Concrete collaborators/imported frameworks
ModelConstantsData Represents feature data Concrete collaborators/imported frameworks
VertexPosition Represents a feature value or composable behavior Concrete collaborators/imported frameworks

The source explicitly defines local protocol relationships: GameViewControllerGameViewDelegate.

Access control

Symbol Access Verified effect Likely rationale
window (Shared/AppDelegate.h:20) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
keys (Shared/GameInput.h:104) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
mouseButtons (Shared/GameInput.h:107) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.
mouseDelta (Shared/GameInput.h:110) header-visible The declaration is exposed to translation units that import the header. Inference: declare a contract needed by other Objective-C/C translation units.

Reference code

Shared/AppDelegate.h:20 — representative boundary

#if TARGET_IOS || TARGET_TVOS
// ...
#else
@property(strong, nonatomic) IBOutlet NSWindow *window;
#endif

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
View lifecycle, callbacks, and feature coordination GameViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AppDelegate, GameViewDelegate, WindowSceneDelegate The source’s Delegate suffix makes this role explicit.
Loads and prepares feature data or resources AssetLoader The source’s Loader suffix makes this role explicit.
Owns drawing, GPU, or presentation processing Renderer The source’s Renderer suffix makes this role explicit.
User-interface presentation and input forwarding GameView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization Shared/GameViewController.h:20 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Protocol-oriented abstraction Shared/GameViewController.h:20 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks Shared/AppDelegate.h:15 Callback protocols invert event delivery back into the sample’s owner.

Main application flow

Reference code

Shared/GameView.m:142setupCAMetalLink()

- (void)setupCAMetalLink
{
    [self stopRenderLoop];
    [self makeMetalLink:self.metalLayer];

#if TARGET_MACOS
    // Register to receive a notification when the window closes so that you
    // can stop the display link.
    NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self
                           selector:@selector(windowWillClose:)
                               name:NSWindowWillCloseNotification
                             object:self.window];
#endif
}

Naming conventions

  • Types: Controller: GameViewController; Delegate: AppDelegate, GameViewDelegate, WindowSceneDelegate; Loader: AssetLoader; Renderer: Renderer; View: GameView.
  • Protocols: GameViewDelegate.
  • Methods: application, applicationShouldTerminateAfterLastWindowClosed, drawableResize, renderTo, initCommon, resizeDrawable, stopRenderLoop, renderUpdate.
  • Files: Shared/AppDelegate.h, Shared/AppDelegate.m, Shared/GameView.h, Shared/AssetLoader.h, Shared/AssetLoader.mm, Shared/GameView.m.

Architecture takeaways

  • main is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches QuartzCore, simd, UIKit, Metal through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
iOS/main.m Cited implementation, Feature implementation
Shared/AppDelegate.h Cited implementation, window, UIKit, Cocoa, AppDelegate
Shared/GameView.h GameViewDelegate, AppKit, GameView
Shared/GameInput.h keys, mouseButtons, mouseDelta, simd, GamepadState, GameInput
Shared/GameViewController.h GameViewController, Cited implementation
Shared/GameInput.mm QuartzCore, GameInput
Shared/AssetLoader.h Metal, AssetLoader
macOS/main.m Feature implementation
tvOS/main.m Feature implementation
Shared/AppDelegate.m AppDelegate
Shared/ShaderTypes.h FrameData, ModelConstantsData, VertexPosition, VertexGenerics, MeshConstantData
Shared/AssetLoader.mm AssetLoader
Shared/GameView.m GameView
Shared/GameViewController.mm GameViewController
Shared/Renderer.h Renderer
Shared/Renderer.mm Renderer