feat: optimize photo rendering

This commit is contained in:
dscyrescotti
2024-06-17 20:44:12 +07:00
parent f0b80f3171
commit 425d442741
5 changed files with 36 additions and 7 deletions

View File

@@ -20,8 +20,13 @@ class Textures {
if let penTexture = penTextures[textureName] {
return penTexture
}
let options: [MTKTextureLoader.Option: Any] = [
.SRGB: false,
.generateMipmaps: true,
.textureStorageMode: NSNumber(value: MTLStorageMode.private.rawValue)
]
let textureLoader = MTKTextureLoader(device: device)
let penTexture = try? textureLoader.newTexture(name: textureName, scaleFactor: 1.0, bundle: .main, options: [.SRGB: false])
let penTexture = try? textureLoader.newTexture(name: textureName, scaleFactor: 1.0, bundle: .main, options: options)
penTextures[textureName] = penTexture
return penTexture
}
@@ -30,7 +35,12 @@ class Textures {
static func createPhotoTexture(for url: URL, on device: MTLDevice) -> MTLTexture? {
let textureLoader = MTKTextureLoader(device: device)
do {
let photoTexture = try textureLoader.newTexture(URL: url, options: [.SRGB: false])
let options: [MTKTextureLoader.Option: Any] = [
.SRGB: false,
.generateMipmaps: true,
.textureStorageMode: NSNumber(value: MTLStorageMode.private.rawValue)
]
let photoTexture = try textureLoader.newTexture(URL: url, options: options)
return photoTexture
} catch {
NSLog("[Memola] - \(error.localizedDescription)")

View File

@@ -77,7 +77,6 @@ extension Photo: Drawable {
renderEncoder.setFragmentTexture(texture, index: 0)
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
renderEncoder.drawPrimitives(type: .triangleStrip, vertexStart: 0, vertexCount: vertices.count)
texture = nil
}
}

View File

@@ -114,12 +114,32 @@ public class Tool: NSObject, ObservableObject {
}
func selectPhoto(_ image: UIImage, for canvasID: NSManagedObjectID) {
let photoItem = bookmarkPhoto(of: image, with: canvasID)
guard let resizedImage = resizePhoto(of: image) else { return }
let photoItem = bookmarkPhoto(of: resizedImage, with: canvasID)
withAnimation {
selectedPhotoItem = photoItem
}
}
private func resizePhoto(of image: UIImage) -> UIImage? {
let targetSize = CGSize(width: 768, height: 768)
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
let newSize = CGSize(
width: size.width * min(widthRatio, heightRatio),
height: size.height * min(widthRatio, heightRatio)
)
let rect = CGRect(origin: .zero, size: newSize)
UIGraphicsBeginImageContextWithOptions(newSize, true, 1.0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
private func bookmarkPhoto(of image: UIImage, with canvasID: NSManagedObjectID) -> PhotoItem? {
guard let data = image.jpegData(compressionQuality: 1) else { return nil }
let fileManager = FileManager.default

View File

@@ -32,7 +32,7 @@ class DrawingView: UIView {
}
func updateDrawableSize(with size: CGSize) {
renderView.drawableSize = size.multiply(by: 3)
renderView.drawableSize = size.multiply(by: 2.5)
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {

View File

@@ -16,8 +16,8 @@ struct PhotoItem: Identifiable, Equatable {
var dimension: CGSize {
let size = image.size
let maxSize = max(size.width, size.height)
let width = size.width * 200 / maxSize
let height = size.height * 200 / maxSize
let width = size.width * 128 / maxSize
let height = size.height * 128 / maxSize
return CGSize(width: width, height: height)
}
}