Skip to content

Commit

Permalink
Optimize MTHKView.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Sep 23, 2024
1 parent bcf128d commit 236055e
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions Sources/IO/MTHKView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public class MTHKView: MTKView {
}

private var currentSampleBuffer: CMSampleBuffer?

private let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()

private lazy var commandQueue: (any MTLCommandQueue)? = {
return device?.makeCommandQueue()
}()
Expand Down Expand Up @@ -90,10 +87,12 @@ public class MTHKView: MTKView {
/// Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.
override open func awakeFromNib() {
super.awakeFromNib()

framebufferOnly = false
enableSetNeedsDisplay = true

if let device {
context = CIContext(mtlDevice: device)
context = CIContext(mtlDevice: device, options: [.cacheIntermediates: false, .name: "MTHKView"])
}
}

Expand Down Expand Up @@ -138,14 +137,22 @@ public class MTHKView: MTKView {
default:
break
}
let bounds = CGRect(origin: .zero, size: drawableSize)
var scaledImage: CIImage = displayImage

scaledImage = scaledImage
var scaledImage = displayImage
.transformed(by: CGAffineTransform(translationX: translationX, y: translationY))
.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))

context.render(scaledImage, to: currentDrawable.texture, commandBuffer: commandBuffer, bounds: bounds, colorSpace: colorSpace)
let destination = CIRenderDestination(
width: Int(drawableSize.width),
height: Int(drawableSize.height),
pixelFormat: colorPixelFormat,
commandBuffer: commandBuffer,
mtlTextureProvider: { () -> (any MTLTexture) in
return currentDrawable.texture
})

_ = try? context.startTask(toRender: scaledImage, to: destination)

commandBuffer.present(currentDrawable)
commandBuffer.commit()
}
Expand Down

0 comments on commit 236055e

Please sign in to comment.