Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Camera9// File : App/Sources/Features/Camera/MonitorView.swift10// Description : The viewfinder. An MTKView that draws the latest frame of11// the pipeline through MonitorRenderer at display rate. The12// same pass draws peaking, zebras, false colour and the13// display LUT, so overlays can never drift from the picture.14// Module : Features15// Repository : https://git.spboucher.ai/orix-camera16// Project Website : https://www.orixlab.dev17// Created : 2026-11-2118// Last Modified : 2026-11-2119//20// =============================================================================21// AUTHOR & PROFESSIONAL AFFILIATION22// =============================================================================23//24// Author : Simon-Pierre Boucher25// Position : Professor of Finance26// Institution : Université du Québec en Outaouais (UQO)27// Department : Département des sciences administratives28// Organization : SP Boucher29//30// Website : https://spboucher.ai31// Git : https://git.spboucher.ai32//33// =============================================================================34// OFFICIAL CONTACT DIRECTORY35// =============================================================================36//37// General : info@spboucher.ai38// Professional : contact@spboucher.ai39// Direct : simon@spboucher.ai40// Research : research@spboucher.ai41// Development : dev@spboucher.ai42// Support : support@spboucher.ai43// Security/Admin : admin@spboucher.ai44// Notifications : notifications@spboucher.ai45//46// Preferred Contact: support@spboucher.ai47//48// =============================================================================49// COPYRIGHT & INTELLECTUAL PROPERTY50// =============================================================================51//52// Copyright (c) 202653// Simon-Pierre Boucher54//55// All rights reserved where applicable.56//57// This file is governed by the applicable project license,58// copyright notices, institutional agreements and relevant law.59//60// Unless expressly authorized by the applicable license or61// rights holder, copying, redistribution, sublicensing,62// publication and commercial exploitation are not permitted.63//64// Existing third-party rights and notices remain unaffected.65//66// =============================================================================67// CONFIDENTIALITY & SECURITY68// =============================================================================69//70// Classification : Public71// Distribution : Public repository; use is governed by the project license72// Security Contact : admin@spboucher.ai73//74// Access to this source does not independently grant permission75// to redistribute or disclose confidential materials.76//77// Do not embed credentials, private keys, access tokens,78// certificates or confidential personal information.79//80// =============================================================================81// LICENSE & MAINTENANCE82// =============================================================================83//84// License : Proprietary, all rights reserved (see license file)85// License File : LICENSE86// Maintainer : Simon-Pierre Boucher87// Technical Contact: dev@spboucher.ai88//89// =============================================================================90// END OF SP BOUCHER SOURCE HEADER91// =============================================================================9293import MetalKit94import ORIXImaging95import SwiftUI9697struct MonitorView: UIViewRepresentable {98 let model: CameraModel99100 func makeCoordinator() -> Coordinator {101 let c = Coordinator(pipeline: model.pipeline, monitor: model.monitor, renderer: model.renderer)102 c.onResize = { [weak model] layer in model?.attach(layer: layer) }103 return c104 }105106 func makeUIView(context: Context) -> MTKView {107 let view = MTKView(frame: .zero, device: model.renderer?.device)108 view.colorPixelFormat = .bgra8Unorm109 view.framebufferOnly = true110 view.clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 1)111 view.preferredFramesPerSecond = 60112 view.isPaused = false113 view.enableSetNeedsDisplay = false114 view.delegate = context.coordinator115 view.isAccessibilityElement = false116 view.backgroundColor = .black117 // The layer tells the system how the preview is oriented, so it can report horizon-level rotation.118 DispatchQueue.main.async { model.attach(layer: view.layer) }119 return view120 }121122 func updateUIView(_ view: MTKView, context: Context) {123 // Under thermal pressure the monitor refreshes at 30 Hz: less GPU work, same capture.124 view.preferredFramesPerSecond = (model.pressure == .serious || model.pressure == .critical) ? 30 : 60125 }126127 final class Coordinator: NSObject, MTKViewDelegate {128 let pipeline: FramePipeline129 let monitor: MonitorBox130 let renderer: MonitorRenderer?131 private let start = CACurrentMediaTime()132 var onResize: ((CALayer) -> Void)?133134 init(pipeline: FramePipeline, monitor: MonitorBox, renderer: MonitorRenderer?) {135 self.pipeline = pipeline; self.monitor = monitor; self.renderer = renderer136 }137138 func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {139 // A new size can mean a new interface orientation: let the backend re-evaluate the preview rotation.140 let layer = view.layer141 DispatchQueue.main.async { self.onResize?(layer) }142 }143144 func draw(in view: MTKView) {145 guard let renderer, let frame = monitor.overrideFrame ?? pipeline.latestFrame,146 let descriptor = view.currentRenderPassDescriptor, let drawable = view.currentDrawable else { return }147 let commandBuffer = renderer.encode(pixelBuffer: frame.pixelBuffer, settings: monitor.settings, time: CACurrentMediaTime() - start,148 descriptor: descriptor, drawableSize: view.drawableSize, pixelScale: Float(view.contentScaleFactor))149 commandBuffer?.present(drawable)150 commandBuffer?.commit()151 }152 }153}154