Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Feb 12, 2024
1 parent 927d577 commit fec3654
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B47D051216CC5350007B594"
BuildableName = "BrightroomUI.framework"
BlueprintIdentifier = "BrightroomEngine"
BuildableName = "BrightroomEngine"
BlueprintName = "BrightroomEngine"
ReferencedContainer = "container:..">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "BrightroomUI"
BuildableName = "BrightroomUI"
BlueprintName = "BrightroomUI"
ReferencedContainer = "container:Brightroom.xcodeproj">
ReferencedContainer = "container:..">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
Expand All @@ -40,15 +54,6 @@
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B47D051216CC5350007B594"
BuildableName = "BrightroomUI.framework"
BlueprintName = "BrightroomUI"
ReferencedContainer = "container:Brightroom.xcodeproj">
</BuildableReference>
</MacroExpansion>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand All @@ -59,10 +64,10 @@
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B47D051216CC5350007B594"
BuildableName = "BrightroomUI.framework"
BlueprintIdentifier = "BrightroomUI"
BuildableName = "BrightroomUI"
BlueprintName = "BrightroomUI"
ReferencedContainer = "container:Brightroom.xcodeproj">
ReferencedContainer = "container:..">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/VergeGroup/Verge.git",
"state" : {
"revision" : "e1656a1e46ce044d0f05bfba69dba0273a8a71ee",
"version" : "12.0.0-beta.2"
"revision" : "6e41f28d0e4ca2e8309a61ae186f465d04761e23",
"version" : "12.0.0"
}
}
],
Expand Down
12 changes: 7 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let package = Package(
products: [
.library(name: "BrightroomEngine", targets: ["BrightroomUI"]),
.library(name: "BrightroomUI", targets: ["BrightroomUI"]),
.library(name: "BrightroomUIPhotosCrop", targets: ["BrightroomUIPhotosCrop"])
],
dependencies: [
.package(url: "https://github.com/VergeGroup/Verge.git", from: "12.0.0-beta.2"),
Expand All @@ -17,14 +18,15 @@ let package = Package(
targets: [
.target(
name: "BrightroomEngine",
dependencies: ["Verge"],
exclude: ["Info.plist"]
dependencies: ["Verge"]
),
.target(
name: "BrightroomUI",
dependencies: ["BrightroomEngine", "Verge", "TransitionPatch"],
exclude: ["Info.plist"],
swiftSettings: [.define("SWIFT_PACKAGE_MANAGER")]
dependencies: ["BrightroomEngine", "Verge", "TransitionPatch"]
),
.target(
name: "BrightroomUIPhotosCrop",
dependencies: ["BrightroomUI"]
)
],
swiftLanguageVersions: [.v5]
Expand Down
9 changes: 0 additions & 9 deletions Sources/BrightroomEngine/BrightroomEngine.h

This file was deleted.

155 changes: 92 additions & 63 deletions Sources/BrightroomEngine/Core/EditingStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,40 @@ import SwiftUI
import UIKit
import Verge

@available(iOS 13, *)
extension EditingStack: ObservableObject {}

public enum EditingStackError: Error {
case unableToCreateRendererInLoading
}

fileprivate enum MTLImageCreationError: Error {
private enum MTLImageCreationError: Error {
case imageTooBig
}

fileprivate extension MTLDevice {
func supportsImage(size: CGSize) -> Bool {
#if DEBUG
extension MTLDevice {
fileprivate func supportsImage(size: CGSize) -> Bool {
#if DEBUG
switch MTLGPUFamily.apple1 {
case .apple1, .apple2, .apple3, .apple4, .apple5, .apple6, .apple7, .apple8, .common1, .common2, .common3, .mac1, .mac2, .macCatalyst1, .macCatalyst2, .metal3:
break;
@unknown default: //If a warning is triggered here, please check https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf for a possibly new value in the Maximum 2D texture width and height table.
case .apple1,
.apple2,
.apple3,
.apple4,
.apple5,
.apple6,
.apple7,
.apple8,
.apple9,
.common1,
.common2,
.common3,
.mac1,
.mac2,
.macCatalyst1,
.macCatalyst2,
.metal3:
break
@unknown default: //If a warning is triggered here, please check https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf for a possibly new value in the Maximum 2D texture width and height table.
break
}
#endif
#endif
let maxSideSize: CGFloat = self.supportsFamily(.apple3) ? 16384 : 8192
return size.width <= maxSideSize && size.height <= maxSideSize
}
Expand All @@ -58,9 +71,17 @@ fileprivate extension MTLDevice {
/// Please make sure of EditingStack is started state before editing in UI with calling `start()`.
open class EditingStack: Hashable, StoreComponentType {

private static let centralQueue = DispatchQueue.init(label: "app.muukii.Brightroom.EditingStack.central", qos: .default, attributes: .concurrent)
private static let centralQueue = DispatchQueue.init(
label: "app.muukii.Brightroom.EditingStack.central",
qos: .default,
attributes: .concurrent
)

private let backgroundQueue = DispatchQueue.init(label: "app.muukii.Brightroom.EditingStack", qos: .default, target: centralQueue)
private let backgroundQueue = DispatchQueue.init(
label: "app.muukii.Brightroom.EditingStack",
qos: .default,
target: centralQueue
)

public struct Options {

Expand All @@ -84,13 +105,13 @@ open class EditingStack: Hashable, StoreComponentType {
public struct Loading: Equatable {}

public struct Loaded: Equatable {

// MARK: - Properties

fileprivate let imageSource: ImageSource

public let metadata: ImageProvider.State.ImageMetadata

private let initialEditing: Edit

/**
Expand Down Expand Up @@ -154,9 +175,9 @@ open class EditingStack: Hashable, StoreComponentType {

return false
}

// MARK: - Initializers

init(
imageSource: ImageSource,
metadata: ImageProvider.State.ImageMetadata,
Expand Down Expand Up @@ -248,7 +269,10 @@ open class EditingStack: Hashable, StoreComponentType {

private let editingImageMaxPixelSize: CGFloat = 2560

private let debounceForCreatingCGImage = _BrightroomDebounce(interval: 0.1, queue: DispatchQueue.init(label: "Brightroom.cgImage"))
private let debounceForCreatingCGImage = _BrightroomDebounce(
interval: 0.1,
queue: DispatchQueue.init(label: "Brightroom.cgImage")
)

// MARK: - Initializers

Expand All @@ -272,14 +296,15 @@ open class EditingStack: Hashable, StoreComponentType {
initialState: .init()
)

filterPresets = colorCubeStorage.filters.map {
FilterPreset(
name: $0.name,
identifier: $0.identifier,
filters: [$0.asAny()],
userInfo: [:]
)
} + presetStorage.presets
filterPresets =
colorCubeStorage.filters.map {
FilterPreset(
name: $0.name,
identifier: $0.identifier,
filters: [$0.asAny()],
userInfo: [:]
)
} + presetStorage.presets

self.imageProvider = imageProvider
}
Expand All @@ -289,7 +314,7 @@ open class EditingStack: Hashable, StoreComponentType {

- Calling from background-thread supported.
*/
public func start(onPreparationCompleted: @escaping () -> Void = {}) {
public func start(onPreparationCompleted: @escaping @MainActor () -> Void = {}) {

let previousHasCompleted = commit { s -> Bool in
/**
Expand Down Expand Up @@ -335,7 +360,7 @@ open class EditingStack: Hashable, StoreComponentType {

guard let self = self else { return }

state.ifChanged(\.loadedImage) { image in
state.ifChanged(\.loadedImage).do { image in

guard let image = image else {
return
Expand Down Expand Up @@ -380,7 +405,9 @@ open class EditingStack: Hashable, StoreComponentType {
orientation: metadata.orientation
)
} catch {
EngineSanitizer.global.onDidFindRuntimeError(.failedToRenderCGImageForCrop(sourceImage: editingSourceCGImage))
EngineSanitizer.global.onDidFindRuntimeError(
.failedToRenderCGImageForCrop(sourceImage: editingSourceCGImage)
)
assertionFailure()
return editingSourceCGImage
}
Expand Down Expand Up @@ -440,14 +467,14 @@ open class EditingStack: Hashable, StoreComponentType {
if let loadedState = state.mapIfPresent(\.loadedState) {
modifyingState.map(keyPath: \.loadedState!) { (nextState) -> Void in

loadedState.ifChanged(\.thumbnailImage) { image in
loadedState.ifChanged(\.thumbnailImage).do { image in

nextState.previewFilterPresets = self.filterPresets.map {
PreviewFilterPreset(sourceImage: image, filter: $0)
}
}

loadedState.ifChanged(\.currentEdit.filters) { currentEdit in
loadedState.ifChanged(\.currentEdit.filters).do { currentEdit in

self.debounceForCreatingCGImage.on { [weak self] in

Expand Down Expand Up @@ -576,7 +603,10 @@ open class EditingStack: Hashable, StoreComponentType {

let imageSource = loaded.imageSource

let renderer = BrightRoomImageRenderer(source: imageSource, orientation: loaded.metadata.orientation)
let renderer = BrightRoomImageRenderer(
source: imageSource,
orientation: loaded.metadata.orientation
)

// TODO: Clean up ImageRenderer.Edit

Expand Down Expand Up @@ -646,7 +676,6 @@ open class EditingStack: Hashable, StoreComponentType {

}


/// TODO: As possible, creates CIImage from MTLTexture
/// 16bits image can't be MTLTexture with MTKTextureLoader.
/// https://stackoverflow.com/questions/54710592/cant-load-large-jpeg-into-a-mtltexture-with-mtktextureloader
Expand All @@ -656,43 +685,43 @@ private func makeMTLTexture(from cgImage: CGImage, device: MTLDevice) throws ->
}

#if true
let loader = MTKTextureLoader(device: device)
let texture = try loader.newTexture(cgImage: cgImage, options: [:])
return texture
let loader = MTKTextureLoader(device: device)
let texture = try loader.newTexture(cgImage: cgImage, options: [:])
return texture
#else

// Here does not work well.
// Here does not work well.

let textureDescriptor = MTLTextureDescriptor()
let textureDescriptor = MTLTextureDescriptor()

textureDescriptor.pixelFormat = .rgba16Uint
textureDescriptor.width = cgImage.width
textureDescriptor.height = cgImage.height
textureDescriptor.pixelFormat = .rgba16Uint
textureDescriptor.width = cgImage.width
textureDescriptor.height = cgImage.height

let texture = try device.makeTexture(descriptor: textureDescriptor).unwrap(
orThrow: "Failed to create MTLTexture"
)
let texture = try device.makeTexture(descriptor: textureDescriptor).unwrap(
orThrow: "Failed to create MTLTexture"
)

let context = try CGContext.makeContext(for: cgImage)
.perform { context in
let flip = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: CGFloat(cgImage.height))
context.concatenate(flip)
context.draw(
cgImage,
in: CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
)
}
let context = try CGContext.makeContext(for: cgImage)
.perform { context in
let flip = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: CGFloat(cgImage.height))
context.concatenate(flip)
context.draw(
cgImage,
in: CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
)
}

let data = try context.data.unwrap()
let data = try context.data.unwrap()

texture.replace(
region: MTLRegionMake2D(0, 0, cgImage.width, cgImage.height),
mipmapLevel: 0,
withBytes: data,
bytesPerRow: 8 * cgImage.width
)
texture.replace(
region: MTLRegionMake2D(0, 0, cgImage.width, cgImage.height),
mipmapLevel: 0,
withBytes: data,
bytesPerRow: 8 * cgImage.width
)

return texture
return texture
#endif

}
Expand Down
Loading

0 comments on commit fec3654

Please sign in to comment.