194 lines
11 KiB
Swift
194 lines
11 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
import MetalKit
|
||
import Combine
|
||
import ImageIO
|
||
import UniformTypeIdentifiers
|
||
|
||
enum DisplayScaling: String, CaseIterable {
|
||
case sharp, smooth, integer
|
||
var label: String {
|
||
switch self {
|
||
case .sharp: return "Sharp — Fit Window"
|
||
case .smooth: return "Smooth — Fit Window"
|
||
case .integer: return "Integer Pixels"
|
||
}
|
||
}
|
||
}
|
||
|
||
struct DisplayStyle: Equatable {
|
||
var scaling: DisplayScaling = .smooth
|
||
var scanlines = false
|
||
}
|
||
|
||
// Host presentation preferences deliberately stay outside machine profiles.
|
||
// Separate observation prevents emulator polls from rebuilding open controls.
|
||
@MainActor
|
||
final class DisplayPreferences: ObservableObject {
|
||
@Published var style: DisplayStyle { didSet {
|
||
defaults.set(style.scaling.rawValue, forKey: "displayScaling")
|
||
defaults.set(style.scanlines, forKey: "displayScanlines")
|
||
} }
|
||
private let defaults: UserDefaults
|
||
init(defaults: UserDefaults) {
|
||
self.defaults = defaults
|
||
style = DisplayStyle(scaling: DisplayScaling(rawValue: defaults.string(forKey: "displayScaling") ?? "") ?? .smooth,
|
||
scanlines: defaults.bool(forKey: "displayScanlines"))
|
||
}
|
||
}
|
||
|
||
// Immutable pixels and their matching active bounds, captured together before
|
||
// the save panel opens. Encoding and file writing need no emulator commands.
|
||
struct ScreenCapture: Sendable {
|
||
let pixels: Data
|
||
let area: CGRect
|
||
enum Failure: LocalizedError {
|
||
case noFrame, encoding
|
||
var errorDescription: String? { self == .noFrame ? "No CoCo picture is available to save yet." : "The CoCo picture could not be encoded as a PNG." }
|
||
}
|
||
init(pixels: Data?, area: CGRect) throws {
|
||
guard let pixels, pixels.count == 640 * 480 * 4 else { throw Failure.noFrame }
|
||
self.pixels = pixels; self.area = VideoGeometry.validated(area).integral
|
||
}
|
||
func png() throws -> Data {
|
||
let width = Int(area.width), height = Int(area.height)
|
||
var rgba = Data(count: width * height * 4)
|
||
rgba.withUnsafeMutableBytes { output in
|
||
pixels.withUnsafeBytes { input in
|
||
let src = input.bindMemory(to: UInt8.self), dst = output.bindMemory(to: UInt8.self)
|
||
for y in 0..<height { for x in 0..<width {
|
||
let i = ((Int(area.minY) + y) * 640 + Int(area.minX) + x) * 4
|
||
let o = (y * width + x) * 4
|
||
dst[o] = src[i+2]; dst[o+1] = src[i+1]; dst[o+2] = src[i]; dst[o+3] = 255
|
||
} }
|
||
}
|
||
}
|
||
guard let provider = CGDataProvider(data: rgba as CFData),
|
||
let color = CGColorSpace(name: CGColorSpace.sRGB),
|
||
let image = CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 32,
|
||
bytesPerRow: width * 4, space: color, bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue),
|
||
provider: provider, decode: nil, shouldInterpolate: false, intent: .defaultIntent) else { throw Failure.encoding }
|
||
let data = NSMutableData()
|
||
guard let destination = CGImageDestinationCreateWithData(data, UTType.png.identifier as CFString, 1, nil) else { throw Failure.encoding }
|
||
CGImageDestinationAddImage(destination, image, nil)
|
||
guard CGImageDestinationFinalize(destination) else { throw Failure.encoding }
|
||
return data as Data
|
||
}
|
||
func save(to url: URL) throws { try png().write(to: url, options: .atomic) }
|
||
}
|
||
|
||
// One layout calculation for Metal and mouse joysticks. Preserve the source
|
||
// raster's proportions, with a small logical-point green inset on every edge.
|
||
struct VideoGeometry {
|
||
static let fullFrame = CGRect(x:0,y:0,width:640,height:480)
|
||
static let border: CGFloat = 4
|
||
static let surround = MTLClearColorMake(0.10, 0.14, 0.12, 1)
|
||
static func validated(_ rect: CGRect) -> CGRect {
|
||
guard [rect.origin.x,rect.origin.y,rect.width,rect.height].allSatisfy({ $0.isFinite }),
|
||
rect.width > 0, rect.height > 0 else { return fullFrame }
|
||
let clipped = rect.intersection(fullFrame)
|
||
return clipped.isNull || clipped.isEmpty ? fullFrame : clipped
|
||
}
|
||
static func picture(in size: CGSize, source: CGRect, inset: CGFloat = border, scaling: DisplayScaling = .sharp) -> CGRect {
|
||
let source = validated(source)
|
||
let available = CGSize(width:max(0,size.width-2*inset),height:max(0,size.height-2*inset))
|
||
let fit = min(available.width/source.width,available.height/source.height)
|
||
let scale = scaling == .integer && fit >= 1 ? floor(fit) : fit
|
||
let width = source.width*scale, height = source.height*scale
|
||
let x = (size.width-width)/2, y = (size.height-height)/2
|
||
return CGRect(x:scaling == .integer && fit >= 1 ? floor(x) : x,
|
||
y:scaling == .integer && fit >= 1 ? floor(y) : y,width:width,height:height)
|
||
}
|
||
static func mouse(_ point: CGPoint, in size: CGSize, source: CGRect,
|
||
scaling: DisplayScaling = .sharp, backing: CGFloat = 1,
|
||
drawableSize: CGSize? = nil) -> CGPoint? {
|
||
guard size.width > 0, size.height > 0 else { return nil }
|
||
let physical = drawableSize ?? CGSize(width:size.width*max(1,backing),height:size.height*max(1,backing))
|
||
guard physical.width > 0, physical.height > 0 else { return nil }
|
||
// Use the actual drawable dimensions: fractional SwiftUI layouts can
|
||
// round backing pixels differently from bounds * screen scale.
|
||
let r = picture(in:physical,source:source,inset:border*physical.width/size.width,scaling:scaling)
|
||
// Metal's viewport starts at the top; AppKit mouse coordinates at bottom.
|
||
let pixel = CGPoint(x:point.x*physical.width/size.width,y:(size.height-point.y)*physical.height/size.height)
|
||
guard r.width > 0, r.height > 0, r.contains(pixel) else { return nil }
|
||
return CGPoint(x:(pixel.x-r.minX)/r.width,y:(pixel.y-r.minY)/r.height)
|
||
}
|
||
}
|
||
|
||
// All CPU-side access is on the main thread. Each submission owns its upload
|
||
// texture until the GPU finishes; no texture is overwritten while in flight.
|
||
final class MetalRenderer {
|
||
let device: MTLDevice
|
||
private let queue: MTLCommandQueue
|
||
private let pipeline: MTLRenderPipelineState
|
||
private let inFlight = DispatchSemaphore(value: 3)
|
||
enum Failure: LocalizedError {
|
||
case unavailable, shaders, resources
|
||
var errorDescription: String? {
|
||
switch self {
|
||
case .unavailable: return "Metal is unavailable on this Mac."
|
||
case .shaders: return "The app’s display shaders could not be loaded. Rebuild the app in Xcode."
|
||
case .resources: return "The Mac could not allocate the display resources."
|
||
}
|
||
}
|
||
}
|
||
static func library(device: MTLDevice, bundle: Bundle) throws -> MTLLibrary {
|
||
guard let url = bundle.url(forResource: "DisplayShader", withExtension: "txt") else { throw Failure.shaders }
|
||
return try device.makeLibrary(source: String(contentsOf: url, encoding: .utf8), options: nil)
|
||
}
|
||
init(device: MTLDevice, library: MTLLibrary) throws {
|
||
self.device = device
|
||
guard let queue = device.makeCommandQueue() else { throw Failure.resources }
|
||
self.queue = queue
|
||
let descriptor = MTLRenderPipelineDescriptor()
|
||
descriptor.vertexFunction = library.makeFunction(name: "vccVertex")
|
||
descriptor.fragmentFunction = library.makeFunction(name: "vccFragment")
|
||
guard descriptor.vertexFunction != nil, descriptor.fragmentFunction != nil else { throw Failure.shaders }
|
||
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
|
||
pipeline = try device.makeRenderPipelineState(descriptor: descriptor)
|
||
}
|
||
func encode(pixels: Data?, target: MTLTexture, drawable: CAMetalDrawable? = nil, activeArea: CGRect = VideoGeometry.fullFrame, inset: CGFloat = VideoGeometry.border, style: DisplayStyle = DisplayStyle()) throws -> MTLCommandBuffer? {
|
||
guard inFlight.wait(timeout: .now()) == .success else { return nil }
|
||
var submitted = false
|
||
defer { if !submitted { inFlight.signal() } }
|
||
guard let command = queue.makeCommandBuffer() else { throw Failure.resources }
|
||
let pass = MTLRenderPassDescriptor()
|
||
pass.colorAttachments[0].texture = target
|
||
pass.colorAttachments[0].loadAction = .clear
|
||
pass.colorAttachments[0].storeAction = .store
|
||
pass.colorAttachments[0].clearColor = VideoGeometry.surround
|
||
var source: MTLTexture?
|
||
if let pixels, pixels.count == 640 * 480 * 4 {
|
||
let descriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm,
|
||
width: 640, height: 480, mipmapped: false)
|
||
descriptor.storageMode = device.hasUnifiedMemory ? .shared : .managed; descriptor.usage = .shaderRead
|
||
guard let texture = device.makeTexture(descriptor: descriptor) else { throw Failure.resources }
|
||
pixels.withUnsafeBytes { data in
|
||
texture.replace(region: MTLRegionMake2D(0, 0, 640, 480), mipmapLevel: 0,
|
||
withBytes: data.baseAddress!, bytesPerRow: 640 * 4)
|
||
}
|
||
source = texture
|
||
}
|
||
guard let encoder = command.makeRenderCommandEncoder(descriptor: pass) else { throw Failure.resources }
|
||
if let source, CGFloat(target.width) > 2*inset, CGFloat(target.height) > 2*inset {
|
||
let crop = VideoGeometry.validated(activeArea)
|
||
let r = VideoGeometry.picture(in:CGSize(width:target.width,height:target.height),source:crop,inset:inset,scaling:style.scaling)
|
||
encoder.setViewport(MTLViewport(originX:Double(r.minX),originY:Double(r.minY),width:Double(r.width),height:Double(r.height),znear:0,zfar:1))
|
||
var region = SIMD4<Float>(Float(crop.minX/640),Float(crop.minY/480),Float(crop.width/640),Float(crop.height/480))
|
||
encoder.setFragmentBytes(®ion,length:MemoryLayout<SIMD4<Float>>.stride,index:0)
|
||
// Fade out at small sizes, where scanlines would alias or hide rows.
|
||
let strength: Float = style.scanlines ? 0.25 * Float(min(1,max(0,r.height/crop.height-1))) : 0
|
||
var effects = SIMD2<Float>(style.scaling == .smooth ? 1 : 0, strength)
|
||
encoder.setFragmentBytes(&effects,length:MemoryLayout<SIMD2<Float>>.stride,index:1)
|
||
encoder.setRenderPipelineState(pipeline)
|
||
encoder.setFragmentTexture(source, index: 0)
|
||
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)
|
||
}
|
||
encoder.endEncoding()
|
||
if let drawable { command.present(drawable) }
|
||
let semaphore = inFlight
|
||
command.addCompletedHandler { _ in semaphore.signal() }
|
||
command.commit(); submitted = true
|
||
return command
|
||
}
|
||
}
|