Refactor ShapeFactory.

This commit is contained in:
shogo4405 2023-01-09 15:23:22 +09:00
parent be03719072
commit c1686fdccf
3 changed files with 70 additions and 46 deletions

View File

@ -331,6 +331,9 @@
BC110259292E661E00D48035 /* MultiCamCaptureSetting.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC110256292E661E00D48035 /* MultiCamCaptureSetting.swift */; };
BC11D94625A1B01000D710BA /* Screencast.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 2915EC521D85BDF100621092 /* Screencast.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
BC20DF38250377A3007BC608 /* IOUIScreenCaptureUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 299B131C1D35272D00A1E8F5 /* IOUIScreenCaptureUnit.swift */; };
BC3004CE296B0A1700119932 /* Shape.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC3004CD296B0A1700119932 /* Shape.swift */; };
BC3004CF296B0A1700119932 /* Shape.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC3004CD296B0A1700119932 /* Shape.swift */; };
BC3004D0296B0A1700119932 /* Shape.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC3004CD296B0A1700119932 /* Shape.swift */; };
BC34DFF025EBB53F005F975A /* Logboard.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC34DFD125EBB12C005F975A /* Logboard.xcframework */; };
BC34E00225EBB59C005F975A /* Logboard.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC34DFD125EBB12C005F975A /* Logboard.xcframework */; };
BC34E00825EBB716005F975A /* Logboard.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC34DFD125EBB12C005F975A /* Logboard.xcframework */; };
@ -857,6 +860,7 @@
BC1102492925147300D48035 /* IOCaptureUnit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IOCaptureUnit.swift; sourceTree = "<group>"; };
BC110252292DD6E900D48035 /* vImage_Buffer+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "vImage_Buffer+Extension.swift"; sourceTree = "<group>"; };
BC110256292E661E00D48035 /* MultiCamCaptureSetting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiCamCaptureSetting.swift; sourceTree = "<group>"; };
BC3004CD296B0A1700119932 /* Shape.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shape.swift; sourceTree = "<group>"; };
BC34DFD125EBB12C005F975A /* Logboard.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = Logboard.xcframework; path = Carthage/Build/Logboard.xcframework; sourceTree = "<group>"; };
BC34FA0A286CB90A00EFAF27 /* PiPHKView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PiPHKView.swift; sourceTree = "<group>"; };
BC44A1A823D31E92002D4297 /* AudioCodecBuffer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioCodecBuffer.swift; sourceTree = "<group>"; wrapsLines = 1; };
@ -1390,6 +1394,7 @@
BC110256292E661E00D48035 /* MultiCamCaptureSetting.swift */,
BC34FA0A286CB90A00EFAF27 /* PiPHKView.swift */,
295891001EEB7A8B00CE51E1 /* ScalingMode.swift */,
BC3004CD296B0A1700119932 /* Shape.swift */,
BC6FC91D29609A6800A746EE /* ShapeFactory.swift */,
29B8768D1CD70AFE00FC07DA /* SoundTransform.swift */,
29B8768F1CD70AFE00FC07DA /* VideoEffect.swift */,
@ -2125,6 +2130,7 @@
BC566F6E25D2ECC500573C4C /* HLSService.swift in Sources */,
BCC1A6EF2645B5F800661156 /* MP4PixelAspectRatioBox.swift in Sources */,
29EA87EA1E79A3B70043A5F8 /* CMBlockBuffer+Extension.swift in Sources */,
BC3004CE296B0A1700119932 /* Shape.swift in Sources */,
BC34FA0B286CB90A00EFAF27 /* PiPHKView.swift in Sources */,
BCA97C12263D8C850027213C /* MP4FullBox.swift in Sources */,
293B42E92340B4840086F973 /* RTMPObjectEncoding.swift in Sources */,
@ -2379,6 +2385,7 @@
29B8770F1CD70D5A00FC07DA /* AMF3Serializer.swift in Sources */,
29B877101CD70D5A00FC07DA /* ASClass.swift in Sources */,
29DF20672312A436004057C3 /* RTMPSocketCompatible.swift in Sources */,
BC3004CF296B0A1700119932 /* Shape.swift in Sources */,
BC562DCC29576D220048D89A /* AVCaptureSession.Preset+Extension.swift in Sources */,
BCC1A7082647E89300661156 /* BaseDescriptor.swift in Sources */,
BC34FA0E286CBD6D00EFAF27 /* PiPHKView.swift in Sources */,
@ -2581,6 +2588,7 @@
2958912C1EEB8F1D00CE51E1 /* FLVSoundSize.swift in Sources */,
295891241EEB8EC500CE51E1 /* FLVAVCPacketType.swift in Sources */,
2943ED55232FCA7C00ED6301 /* Setting.swift in Sources */,
BC3004D0296B0A1700119932 /* Shape.swift in Sources */,
29EB3DF31ED05773001CAE8B /* CMAudioFormatDescription+Extension.swift in Sources */,
29EB3DFC1ED057AC001CAE8B /* HTTPRequest.swift in Sources */,
BC1102402917C35B00D48035 /* CVPixelBufferPool+Extension.swift in Sources */,

54
Sources/Media/Shape.swift Normal file
View File

@ -0,0 +1,54 @@
import Foundation
#if os(macOS)
import AppKit
class RoundedSquareShape: Shape {
var rect: CGRect = .zero
var cornerRadius: CGFloat = .zero
func makeCGImage() -> CGImage? {
guard let context = CGContext(
data: nil,
width: Int(rect.width),
height: Int(rect.height),
bitsPerComponent: 8,
bytesPerRow: Int(rect.width) * 4,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue).rawValue
) else {
return nil
}
let path = CGPath.init(roundedRect: rect, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil)
context.setFillColor(NSColor.white.cgColor)
context.addPath(path)
context.closePath()
context.fillPath()
return context.makeImage()
}
}
#else
import UIKit
class RoundedSquareShape: Shape {
var rect: CGRect = .zero
var cornerRadius: CGFloat = .zero
func makeCGImage() -> CGImage? {
UIGraphicsBeginImageContext(rect.size)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
let roundedPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
context.setFillColor(UIColor.white.cgColor)
context.addPath(roundedPath.cgPath)
context.closePath()
context.fillPath()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image?.cgImage
}
}
#endif

View File

@ -1,17 +1,16 @@
import Accelerate
import CoreMedia
import Foundation
#if canImport(UIKit)
import UIKit
#endif
#if canImport(AppKit)
import AppKit
#endif
protocol Shape {
func makeCGImage() -> CGImage?
}
class ShapeFactory {
static let shared = ShapeFactory()
private var imageBuffers: [String: vImage_Buffer] = [:]
private var roundedSquareShape = RoundedSquareShape()
func cornerRadius(_ size: CGSize, cornerRadius: CGFloat) -> vImage_Buffer {
let key = "\(size.width):\(size.height):\(cornerRadius)"
@ -19,8 +18,10 @@ class ShapeFactory {
return buffer
}
var imageBuffer = vImage_Buffer()
roundedSquareShape.rect = .init(origin: .zero, size: size)
roundedSquareShape.cornerRadius = cornerRadius
guard
let image = makeCGImage(.init(origin: .zero, size: size), cornerRadius: cornerRadius),
let image = roundedSquareShape.makeCGImage(),
var format = vImage_CGImageFormat(cgImage: image),
vImageBuffer_InitWithCGImage(&imageBuffer, &format, nil, image, vImage_Flags(kvImageNoFlags)) == kvImageNoError else {
return imageBuffer
@ -35,43 +36,4 @@ class ShapeFactory {
}
imageBuffers.removeAll()
}
#if canImport(UIKit)
private func makeCGImage(_ rect: CGRect, cornerRadius: CGFloat) -> CGImage? {
UIGraphicsBeginImageContext(rect.size)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
let roundedPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius)
context.setFillColor(UIColor.white.cgColor)
context.addPath(roundedPath.cgPath)
context.closePath()
context.fillPath()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image?.cgImage
}
#endif
#if canImport(AppKit)
private func makeCGImage(_ rect: CGRect, cornerRadius: CGFloat) -> CGImage? {
guard let context = CGContext(
data: nil,
width: Int(rect.width),
height: Int(rect.height),
bitsPerComponent: 8,
bytesPerRow: Int(rect.width) * 4,
space: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue).rawValue
) else {
return nil
}
let path = CGPath.init(roundedRect: rect, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil)
context.setFillColor(NSColor.white.cgColor)
context.addPath(path)
context.closePath()
context.fillPath()
return context.makeImage()
}
#endif
}