349 lines
23 KiB
Swift
349 lines
23 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import XCTest
|
|
import MetalKit
|
|
|
|
final class NativeTests: XCTestCase {
|
|
func testNaturalKeyboardChordsAndRelease() {
|
|
var keys = CocoKeyboard()
|
|
keys.modifiers(shift: true, control: false, alt: false)
|
|
XCTAssertEqual(keys.mask, CocoKeyboard.shift)
|
|
keys.press(code: 19, characters: "@")
|
|
XCTAssertEqual(keys.mask, 1, "Mac Shift-2 must become unshifted CoCo @")
|
|
keys.release(code: 19)
|
|
XCTAssertEqual(keys.mask, CocoKeyboard.shift)
|
|
keys.press(code: 39, characters: "\"")
|
|
XCTAssertEqual(keys.mask, (UInt64(1) << 34) | CocoKeyboard.shift)
|
|
keys.press(code: 39, characters: "'") // Host repeat must not change the held chord.
|
|
XCTAssertEqual(keys.mask, (UInt64(1) << 34) | CocoKeyboard.shift)
|
|
keys.releaseAll(); XCTAssertEqual(keys.mask, 0)
|
|
keys.press(code: 0, characters: "a"); keys.press(code: 1, characters: "s")
|
|
XCTAssertEqual(keys.mask, (UInt64(1) << 1) | (UInt64(1) << 19))
|
|
keys.release(code: 0); XCTAssertEqual(keys.mask, UInt64(1) << 19)
|
|
keys.releaseAll()
|
|
keys.press(code: 123, characters: "")
|
|
XCTAssertEqual(keys.mask, UInt64(1) << 29)
|
|
keys.modifiers(shift: false, control: true, alt: true)
|
|
XCTAssertEqual(keys.mask, (UInt64(1) << 29) | CocoKeyboard.control | CocoKeyboard.alt)
|
|
keys.releaseAll(); XCTAssertEqual(keys.mask, 0)
|
|
keys.press(code: 0, characters: "a")
|
|
keys.modifiers(shift: true, control: false, alt: false)
|
|
XCTAssertEqual(keys.mask, 2 | CocoKeyboard.shift)
|
|
keys.modifiers(shift: false, control: false, alt: false)
|
|
XCTAssertEqual(keys.mask, 2, "Shift releases while a letter remains held")
|
|
XCTAssertEqual(CocoKeyboard.chord(for: "+"), (UInt64(1) << 43) | CocoKeyboard.shift)
|
|
XCTAssertNil(CocoKeyboard.chord(for: "é"))
|
|
}
|
|
func testROMFileValidationAndBoundedRead() throws {
|
|
var valid = Data(repeating: 0x12, count: 32768)
|
|
valid[32766] = 0x80; valid[32767] = 0
|
|
XCTAssertNoThrow(try ROMImage.validate(valid))
|
|
XCTAssertThrowsError(try ROMImage.validate(Data()))
|
|
XCTAssertThrowsError(try ROMImage.validate(valid + Data([0])))
|
|
var bad = valid; bad[32766] = 0xff; bad[32767] = 0xff
|
|
XCTAssertThrowsError(try ROMImage.validate(bad))
|
|
let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
|
|
defer { try? FileManager.default.removeItem(at: folder) }
|
|
let file = folder.appendingPathComponent("authored.rom")
|
|
try valid.write(to: file)
|
|
XCTAssertEqual(try ROMImage.read(file), valid)
|
|
XCTAssertThrowsError(try ROMImage.read(folder.appendingPathComponent("missing.rom")))
|
|
XCTAssertThrowsError(try ROMImage.read(folder))
|
|
try (valid + Data(repeating: 0, count: 1024 * 1024)).write(to: file)
|
|
XCTAssertThrowsError(try ROMImage.read(file))
|
|
}
|
|
func testMetalPixelsOrientationAlphaAndLetterbox() throws {
|
|
guard let device = MTLCreateSystemDefaultDevice() else { throw XCTSkip("Metal device unavailable") }
|
|
let renderer = try MetalRenderer(device: device, library: MetalRenderer.library(device: device, bundle: Bundle(for: Self.self)))
|
|
// Different top/bottom and left/right colors catch flipped coordinates.
|
|
var words = [UInt32](repeating: 0, count: 640 * 480)
|
|
for y in 0..<480 { for x in 0..<640 {
|
|
words[y * 640 + x] = y < 240 ? (x < 320 ? 0x00ff0000 : 0x0000ff00)
|
|
: (x < 320 ? 0x000000ff : 0x00ffffff)
|
|
}}
|
|
let data = words.withUnsafeBytes { Data($0) }
|
|
let descriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm,
|
|
width: 800, height: 480, mipmapped: false)
|
|
descriptor.storageMode = device.hasUnifiedMemory ? .shared : .managed; descriptor.usage = .renderTarget
|
|
let target = try XCTUnwrap(device.makeTexture(descriptor: descriptor))
|
|
let command = try XCTUnwrap(renderer.encode(pixels: data, target: target))
|
|
command.waitUntilCompleted(); XCTAssertEqual(command.status, .completed)
|
|
if !device.hasUnifiedMemory {
|
|
let queue = try XCTUnwrap(device.makeCommandQueue())
|
|
let sync = try XCTUnwrap(queue.makeCommandBuffer())
|
|
let blit = try XCTUnwrap(sync.makeBlitCommandEncoder())
|
|
blit.synchronize(resource: target); blit.endEncoding(); sync.commit(); sync.waitUntilCompleted()
|
|
}
|
|
var output = [UInt32](repeating: 0, count: 800 * 480)
|
|
output.withUnsafeMutableBytes { bytes in
|
|
target.getBytes(bytes.baseAddress!, bytesPerRow: 800 * 4,
|
|
from: MTLRegionMake2D(0, 0, 800, 480), mipmapLevel: 0)
|
|
}
|
|
// The normalized clear color includes 0.10 * 255 = 25.5; GPU
|
|
// quantization may differ by one RGB unit, while alpha stays opaque.
|
|
for index in [0, 799] {
|
|
let pixel = output[index]
|
|
XCTAssertEqual(pixel >> 24, 255, "Opaque dark green surround")
|
|
XCTAssertEqual(Int((pixel >> 16) & 255), 26, accuracy: 1)
|
|
XCTAssertEqual(Int((pixel >> 8) & 255), 36, accuracy: 1)
|
|
XCTAssertEqual(Int(pixel & 255), 31, accuracy: 1)
|
|
}
|
|
XCTAssertEqual(output[10 * 800 + 100], 0xffff0000, "Top-left red, alpha forced opaque")
|
|
XCTAssertEqual(output[10 * 800 + 600], 0xff00ff00, "Top-right green")
|
|
XCTAssertEqual(output[470 * 800 + 100], 0xff0000ff, "Bottom-left blue")
|
|
XCTAssertEqual(output[470 * 800 + 600], 0xffffffff, "Bottom-right white")
|
|
}
|
|
func testTapeFiles() throws {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
let url = directory.appendingPathComponent("Tape é.CAS")
|
|
let bytes = Data([0x55, 0x3c, 0xa5])
|
|
try bytes.write(to: url)
|
|
XCTAssertEqual(try TapeImage.read(url), bytes)
|
|
XCTAssertThrowsError(try TapeImage.read(directory))
|
|
XCTAssertThrowsError(try TapeImage.read(directory.appendingPathComponent("missing.wav")))
|
|
let large = directory.appendingPathComponent("large.wav")
|
|
try Data().write(to: large)
|
|
let file = try FileHandle(forWritingTo: large)
|
|
try file.truncate(atOffset: UInt64(TapeImage.limit + 1)); try file.close()
|
|
XCTAssertThrowsError(try TapeImage.read(large))
|
|
XCTAssertEqual(try Data(contentsOf: url), bytes, "Loading never modifies the source file")
|
|
}
|
|
func testMetalResizePreservesPixelsAndAspect() throws {
|
|
guard let device = MTLCreateSystemDefaultDevice() else { throw XCTSkip("Metal device unavailable") }
|
|
let renderer = try MetalRenderer(device: device, library: MetalRenderer.library(device: device, bundle: Bundle(for: Self.self)))
|
|
for crop in [CGRect(x:0,y:0,width:640,height:480), CGRect(x:64,y:50,width:512,height:384),
|
|
CGRect(x:0,y:50,width:640,height:384), CGRect(x:0,y:16,width:640,height:450),
|
|
CGRect(x:64,y:38,width:512,height:398)] {
|
|
// Magenta marks the guest border. Black is real picture content and
|
|
// must remain present, never mistaken for padding or auto-cropped.
|
|
var words = [UInt32](repeating:0xff00ff,count:640*480)
|
|
for y in Int(crop.minY)..<Int(crop.maxY) { for x in Int(crop.minX)..<Int(crop.maxX) {
|
|
words[y*640+x] = CGFloat(y) < crop.midY ? (CGFloat(x) < crop.midX ? 0 : 0xff0000) : (CGFloat(x) < crop.midX ? 0x00ff00 : 0xffffff)
|
|
}}
|
|
let frame=words.withUnsafeBytes { Data($0) }
|
|
for (width,height) in [(640,480),(1280,960),(480,800),(1001,701),(320,240)] {
|
|
let descriptor=MTLTextureDescriptor.texture2DDescriptor(pixelFormat:.bgra8Unorm,width:width,height:height,mipmapped:false)
|
|
descriptor.storageMode=device.hasUnifiedMemory ? .shared : .managed;descriptor.usage = .renderTarget
|
|
let target=try XCTUnwrap(device.makeTexture(descriptor:descriptor))
|
|
let command=try XCTUnwrap(renderer.encode(pixels:frame,target:target,activeArea:crop))
|
|
command.waitUntilCompleted();XCTAssertEqual(command.status,.completed)
|
|
if !device.hasUnifiedMemory {
|
|
let queue=try XCTUnwrap(device.makeCommandQueue()), sync=try XCTUnwrap(queue.makeCommandBuffer())
|
|
let blit=try XCTUnwrap(sync.makeBlitCommandEncoder());blit.synchronize(resource:target)
|
|
blit.endEncoding();sync.commit();sync.waitUntilCompleted()
|
|
}
|
|
var output=[UInt32](repeating:0,count:width*height)
|
|
output.withUnsafeMutableBytes { target.getBytes($0.baseAddress!,bytesPerRow:width*4,from:MTLRegionMake2D(0,0,width,height),mipmapLevel:0) }
|
|
let scale=min((Double(width)-8)/crop.width,(Double(height)-8)/crop.height)
|
|
let left=(Double(width)-crop.width*scale)/2,top=(Double(height)-crop.height*scale)/2
|
|
for (x,y,color) in [(0.25,0.25,UInt32(0xff000000)),(0.75,0.25,0xffff0000),(0.25,0.75,0xff00ff00),(0.75,0.75,0xffffffff)] {
|
|
let index=Int(top+y*crop.height*scale)*width+Int(left+x*crop.width*scale)
|
|
XCTAssertEqual(output[index],color,"Active content, orientation and proportions at \(width)x\(height), \(crop)")
|
|
}
|
|
XCTAssertFalse(output.contains(0xffff00ff),"Guest border must not appear")
|
|
// Four sides retain the same dark green surround, never black.
|
|
for index in [width/2,(height-1)*width+width/2,(height/2)*width,(height/2)*width+width-1] {
|
|
let pixel=output[index]
|
|
XCTAssertEqual(Int((pixel>>16)&255),26,accuracy:1)
|
|
XCTAssertEqual(Int((pixel>>8)&255),36,accuracy:1)
|
|
XCTAssertEqual(Int(pixel&255),31,accuracy:1)
|
|
}
|
|
XCTAssertEqual(frame,words.withUnsafeBytes { Data($0) })
|
|
}
|
|
}
|
|
}
|
|
|
|
func testActiveVideoGeometryAndMouseMatchAcrossResize() {
|
|
for crop in [CGRect(x:64,y:50,width:512,height:384),CGRect(x:0,y:16,width:640,height:450)] {
|
|
for size in [CGSize(width:1200,height:700),CGSize(width:400,height:800),CGSize(width:200,height:150)] {
|
|
let r=VideoGeometry.picture(in:size,source:crop)
|
|
XCTAssertEqual(r.width/r.height,crop.width/crop.height,accuracy:0.000001)
|
|
XCTAssertGreaterThanOrEqual(r.minX,4);XCTAssertGreaterThanOrEqual(r.minY,4)
|
|
XCTAssertEqual(r.midX,size.width/2);XCTAssertEqual(r.midY,size.height/2)
|
|
XCTAssertTrue(abs(r.minX-4)<0.0001 || abs(r.minY-4)<0.0001,"Picture maximizes the available area")
|
|
XCTAssertEqual(VideoGeometry.mouse(CGPoint(x:r.midX,y:r.midY),in:size,source:crop),CGPoint(x:0.5,y:0.5))
|
|
XCTAssertNil(VideoGeometry.mouse(.zero,in:size,source:crop))
|
|
let topLeft=VideoGeometry.mouse(CGPoint(x:r.minX+0.001,y:r.maxY-0.001),in:size,source:crop)!
|
|
XCTAssertLessThan(topLeft.x,0.001);XCTAssertLessThan(topLeft.y,0.001)
|
|
let bottomRight=VideoGeometry.mouse(CGPoint(x:r.maxX-0.001,y:r.minY+0.001),in:size,source:crop)!
|
|
XCTAssertGreaterThan(bottomRight.x,0.999);XCTAssertGreaterThan(bottomRight.y,0.999)
|
|
}
|
|
}
|
|
XCTAssertEqual(VideoGeometry.validated(.zero),VideoGeometry.fullFrame)
|
|
XCTAssertEqual(VideoGeometry.validated(CGRect(x:-10,y:-10,width:650,height:490)),VideoGeometry.fullFrame)
|
|
XCTAssertNil(VideoGeometry.mouse(.zero,in:CGSize(width:4,height:4),source:VideoGeometry.fullFrame))
|
|
}
|
|
|
|
func testDiskFilesAndAtomicExport() throws {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
let file = directory.appendingPathComponent("Disk é.DSK")
|
|
let bytes = Data(repeating: 0xff, count: 161280)
|
|
try DiskFiles.export(bytes, to: file)
|
|
var image = try DiskFiles.readImage(file)
|
|
XCTAssertEqual(image.data, bytes); XCTAssertFalse(image.readOnly)
|
|
try FileManager.default.setAttributes([.posixPermissions: 0o444], ofItemAtPath: file.path)
|
|
image = try DiskFiles.readImage(file); XCTAssertTrue(image.readOnly)
|
|
XCTAssertEqual(try Data(contentsOf: file), bytes, "Loading a protected file does not change it")
|
|
try FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: file.path)
|
|
let modified = Data(repeating: 0x5a, count: 161280)
|
|
try DiskFiles.export(modified, to: file)
|
|
XCTAssertEqual(try DiskFiles.readImage(file).data, modified, "Atomic overwrite round trip")
|
|
XCTAssertThrowsError(try DiskFiles.export(bytes, to: directory.appendingPathComponent("missing/failed.dsk")))
|
|
XCTAssertEqual(try Data(contentsOf: file), modified)
|
|
XCTAssertThrowsError(try DiskFiles.readImage(directory))
|
|
XCTAssertThrowsError(try DiskFiles.readImage(directory.appendingPathComponent("missing.dsk")))
|
|
let unsupported = directory.appendingPathComponent("unsupported.img")
|
|
try bytes.write(to: unsupported)
|
|
XCTAssertThrowsError(try DiskFiles.readImage(unsupported))
|
|
for suffix in ["DMK", "vDk"] {
|
|
let url = directory.appendingPathComponent("Floppy." + suffix)
|
|
try bytes.write(to: url)
|
|
XCTAssertEqual(try DiskFiles.readImage(url).data, bytes)
|
|
XCTAssertEqual(MediaDestination.choices(for: url), (0..<4).map(MediaDestination.floppy))
|
|
XCTAssertThrowsError(try DiskFiles.readCartridgeROM(url))
|
|
}
|
|
let huge = directory.appendingPathComponent("huge.dsk")
|
|
try Data().write(to: huge)
|
|
let handle = try FileHandle(forWritingTo: huge)
|
|
try handle.truncate(atOffset: UInt64(DiskFiles.limit + 1)); try handle.close()
|
|
XCTAssertThrowsError(try DiskFiles.readImage(huge))
|
|
let rom = directory.appendingPathComponent("disk rom.rom")
|
|
for size in [8192, 16384] {
|
|
try Data(repeating: 0, count: size).write(to: rom)
|
|
XCTAssertEqual(try DiskFiles.readROM(rom).count, size)
|
|
}
|
|
for size in [0, 8191, 8193, 16385, 32768] {
|
|
try Data(repeating: 0, count: size).write(to: rom)
|
|
XCTAssertThrowsError(try DiskFiles.readROM(rom))
|
|
}
|
|
}
|
|
}
|
|
|
|
final class DisplayControlsTests: XCTestCase {
|
|
@MainActor func testAppearanceDefaultsPersistenceAndInvalidFallback() throws {
|
|
let suite = "VCCDisplay-" + UUID().uuidString
|
|
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
|
defer { defaults.removePersistentDomain(forName: suite) }
|
|
let p = DisplayPreferences(defaults: defaults)
|
|
XCTAssertEqual(p.style, DisplayStyle())
|
|
XCTAssertEqual(p.style.scaling, .smooth)
|
|
p.style = DisplayStyle(scaling: .integer, scanlines: true)
|
|
XCTAssertEqual(DisplayPreferences(defaults: defaults).style, p.style)
|
|
p.style = DisplayStyle(scaling: .sharp)
|
|
XCTAssertEqual(DisplayPreferences(defaults: defaults).style.scaling, .sharp)
|
|
p.style = DisplayStyle()
|
|
XCTAssertEqual(DisplayPreferences(defaults: defaults).style, DisplayStyle())
|
|
defaults.set("unsupported", forKey: "displayScaling")
|
|
XCTAssertEqual(DisplayPreferences(defaults: defaults).style.scaling, .smooth)
|
|
}
|
|
|
|
func testIntegerGeometryMatchesMouseOnRetinaAndSmallWindows() {
|
|
let source = CGRect(x: 64, y: 50, width: 512, height: 384)
|
|
for backing: CGFloat in [1, 2] {
|
|
for size in [CGSize(width: 1101, height: 801), CGSize(width: 250, height: 180)] {
|
|
let physical = CGSize(width: size.width*backing, height: size.height*backing)
|
|
let r = VideoGeometry.picture(in: physical, source: source, inset: 4*backing, scaling: .integer)
|
|
let fit = min((physical.width-8*backing)/512,(physical.height-8*backing)/384)
|
|
XCTAssertEqual(r.width/512, fit >= 1 ? floor(fit) : fit, accuracy: 0.00001)
|
|
if fit >= 1 { XCTAssertEqual(r.minX, floor(r.minX)); XCTAssertEqual(r.minY, floor(r.minY)) }
|
|
for normalized in [CGPoint(x: 0.01,y: 0.01), CGPoint(x: 0.5,y: 0.5), CGPoint(x: 0.99,y: 0.99)] {
|
|
let point = CGPoint(x: (r.minX+normalized.x*r.width)/backing,
|
|
y: size.height-(r.minY+normalized.y*r.height)/backing)
|
|
let actual = VideoGeometry.mouse(point, in: size, source: source, scaling: .integer, backing: backing)
|
|
XCTAssertEqual(actual!.x, normalized.x, accuracy: 0.00001)
|
|
XCTAssertEqual(actual!.y, normalized.y, accuracy: 0.00001)
|
|
}
|
|
XCTAssertNil(VideoGeometry.mouse(.zero, in: size, source: source, scaling: .integer, backing: backing))
|
|
}
|
|
}
|
|
let logical = CGSize(width: 601.3, height: 451.7), actual = CGSize(width: 1203, height: 903)
|
|
let r = VideoGeometry.picture(in: actual, source: source, inset: 4*actual.width/logical.width, scaling: .integer)
|
|
let point = CGPoint(x: r.midX/actual.width*logical.width, y: (1-r.midY/actual.height)*logical.height)
|
|
let normalized = VideoGeometry.mouse(point, in: logical, source: source, scaling: .integer, drawableSize: actual)
|
|
XCTAssertEqual(normalized!.x, 0.5, accuracy: 0.00001)
|
|
XCTAssertEqual(normalized!.y, 0.5, accuracy: 0.00001)
|
|
}
|
|
|
|
func testPNGHasNativeCropCorrectOrientationOpaqueColorsAndFrozenPixels() throws {
|
|
for rect in [CGRect(x: 64,y: 50,width: 512,height: 384), CGRect(x: 0,y: 16,width: 640,height: 450)] {
|
|
var words = [UInt32](repeating: 0x00ff00ff,count: 640*480)
|
|
for y in Int(rect.minY)..<Int(rect.maxY) { for x in Int(rect.minX)..<Int(rect.maxX) {
|
|
words[y*640+x] = CGFloat(y) < rect.midY ? 0x00ff0000 : 0x000000ff
|
|
} }
|
|
words[Int(rect.midY)*640+Int(rect.midX)] = 0 // Genuine black content stays black.
|
|
var frame = words.withUnsafeBytes { Data($0) }
|
|
let capture = try ScreenCapture(pixels: frame, area: rect)
|
|
frame.resetBytes(in: 0..<frame.count) // Copy-on-write: later frames cannot alter capture.
|
|
let png = try capture.png()
|
|
let bitmap = try XCTUnwrap(NSBitmapImageRep(data: png))
|
|
XCTAssertEqual(bitmap.pixelsWide, Int(rect.width)); XCTAssertEqual(bitmap.pixelsHigh, Int(rect.height))
|
|
// Read PNG samples directly: NSBitmapImageRep.colorAt converts via
|
|
// AppKit's calibrated color space and does not preserve raw primaries.
|
|
func sample(_ x: Int, _ y: Int) -> [Int] {
|
|
var values = [Int](repeating: 0, count: 4)
|
|
bitmap.getPixel(&values, atX: x, y: y)
|
|
return values
|
|
}
|
|
XCTAssertEqual(sample(0, 0), [255, 0, 0, 255])
|
|
XCTAssertEqual(sample(bitmap.pixelsWide-1, bitmap.pixelsHigh-1), [0, 0, 255, 255])
|
|
XCTAssertEqual(sample(bitmap.pixelsWide/2, bitmap.pixelsHigh/2), [0, 0, 0, 255])
|
|
XCTAssertEqual(try capture.png(), png)
|
|
}
|
|
}
|
|
|
|
func testPNGExportAndFailureKeepExistingFiles() throws {
|
|
XCTAssertThrowsError(try ScreenCapture(pixels: nil, area: .zero))
|
|
XCTAssertThrowsError(try ScreenCapture(pixels: Data([1]), area: .zero))
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
let capture = try ScreenCapture(pixels: Data(repeating: 0,count: 640*480*4),area: .zero)
|
|
let file = directory.appendingPathComponent("Screen é.png")
|
|
try capture.save(to: file)
|
|
let original = try Data(contentsOf: file)
|
|
XCTAssertEqual(original, try capture.png())
|
|
XCTAssertThrowsError(try capture.save(to: directory.appendingPathComponent("missing/Screen.png")))
|
|
XCTAssertEqual(try Data(contentsOf: file), original)
|
|
}
|
|
|
|
func testMetalSmoothScanlinesAndSmallSizeFallback() throws {
|
|
guard let device = MTLCreateSystemDefaultDevice() else { throw XCTSkip("Metal device unavailable") }
|
|
let renderer = try MetalRenderer(device: device, library: MetalRenderer.library(device: device, bundle: Bundle(for: Self.self)))
|
|
let crop = CGRect(x:64,y:50,width:512,height:384)
|
|
func render(_ words: [UInt32], _ width: Int, _ height: Int, _ style: DisplayStyle) throws -> [UInt32] {
|
|
let descriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: .bgra8Unorm,width: width,height: height,mipmapped: false)
|
|
descriptor.storageMode = device.hasUnifiedMemory ? .shared : .managed; descriptor.usage = .renderTarget
|
|
let texture = try XCTUnwrap(device.makeTexture(descriptor: descriptor))
|
|
let command = try XCTUnwrap(renderer.encode(pixels: words.withUnsafeBytes { Data($0) },target: texture,activeArea: crop,style: style))
|
|
command.waitUntilCompleted(); XCTAssertEqual(command.status, .completed)
|
|
if !device.hasUnifiedMemory {
|
|
let queue = try XCTUnwrap(device.makeCommandQueue()), sync = try XCTUnwrap(queue.makeCommandBuffer())
|
|
let blit = try XCTUnwrap(sync.makeBlitCommandEncoder()); blit.synchronize(resource: texture)
|
|
blit.endEncoding(); sync.commit(); sync.waitUntilCompleted()
|
|
}
|
|
var output = [UInt32](repeating:0,count:width*height)
|
|
output.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!,bytesPerRow:width*4,from:MTLRegionMake2D(0,0,width,height),mipmapLevel:0) }
|
|
return output
|
|
}
|
|
var words = [UInt32](repeating:0x00ff00ff,count:640*480)
|
|
for y in 50..<434 { for x in 64..<576 { words[y*640+x] = 0x00ffffff } }
|
|
let lines = try render(words,1032,776,DisplayStyle(scaling:.integer,scanlines:true))
|
|
XCTAssertEqual(lines[4*1032+4],0xffffffff)
|
|
let dark = lines[6*1032+4]
|
|
XCTAssertEqual(Int(dark&255),191,accuracy:1); XCTAssertEqual(dark>>24,255)
|
|
XCTAssertEqual(lines[0],0xff1a241f,"Effects must leave the surround unchanged")
|
|
XCTAssertFalse(lines.contains(0xffff00ff),"Crop must not leak the guest border")
|
|
XCTAssertEqual(try render(words,300,200,DisplayStyle(scanlines:true)),try render(words,300,200,DisplayStyle()),"Small displays suppress scanline aliasing")
|
|
for y in 50..<434 { for x in 64..<576 { words[y*640+x] = x%2 == 0 ? 0 : 0x00ffffff } }
|
|
let sharp = try render(words,1001,701,DisplayStyle(scaling: .sharp))
|
|
let smooth = try render(words,1001,701,DisplayStyle(scaling:.smooth))
|
|
let gray: (UInt32) -> Bool = { p in let c=p&255; return c>0 && c<255 && c==((p>>8)&255) && c==((p>>16)&255) }
|
|
XCTAssertFalse(sharp.contains(where:gray)); XCTAssertTrue(smooth.contains(where:gray),"Smooth mode interpolates neighboring pixels")
|
|
XCTAssertFalse(smooth.contains(0xffff00ff))
|
|
}
|
|
}
|