summaryrefslogtreecommitdiffstats
path: root/video/out/cocoa-cb/video_layer.swift
diff options
context:
space:
mode:
authorAkemi <der.richter@gmx.de>2018-11-14 14:57:36 +0100
committerJan Ekström <jeebjp@gmail.com>2019-04-02 02:05:11 +0300
commit0bea3841539f47150d99677f4a7974fe16398783 (patch)
treec3f7ba8118ec77ebfb23e1c5bb0881f0460c66cf /video/out/cocoa-cb/video_layer.swift
parent716b8719288321c3eb145c97f11ade24373edcdd (diff)
downloadmpv-0bea3841539f47150d99677f4a7974fe16398783.tar.bz2
mpv-0bea3841539f47150d99677f4a7974fe16398783.tar.xz
cocoa-cb: simplify CGL pixel format creation
i found the old pixel format creation a bit too messy. pixel format attribute arrays and look ups were all over the place, the actual logic what kind of format was created was inscrutable, the software pixel format was hardcoded and no probing was done. i split the attributes into mandatory and optional ones, one mandatory for a hardware and software pixel format each, and moved those to the top of the class. that way new attributes can be easily added to either the mandatory or optional attributes and they don't mess up the actual pixel creation logic any more. furthermore both hardware and software pixel formats are being probed the same way now. to minimise code duplications the probing was moved into its own function.
Diffstat (limited to 'video/out/cocoa-cb/video_layer.swift')
-rw-r--r--video/out/cocoa-cb/video_layer.swift132
1 files changed, 75 insertions, 57 deletions
diff --git a/video/out/cocoa-cb/video_layer.swift b/video/out/cocoa-cb/video_layer.swift
index 69a9620409..5de57b9e00 100644
--- a/video/out/cocoa-cb/video_layer.swift
+++ b/video/out/cocoa-cb/video_layer.swift
@@ -39,6 +39,43 @@ class VideoLayer: CAOpenGLLayer {
let queue: DispatchQueue = DispatchQueue(label: "io.mpv.queue.draw")
+ let glVersions: [CGLOpenGLProfile] = [
+ kCGLOGLPVersion_3_2_Core,
+ kCGLOGLPVersion_Legacy
+ ]
+
+ let glFormatBase: [CGLPixelFormatAttribute] = [
+ kCGLPFAOpenGLProfile,
+ kCGLPFAAccelerated,
+ kCGLPFADoubleBuffer
+ ]
+
+ let glFormatSoftwareBase: [CGLPixelFormatAttribute] = [
+ kCGLPFAOpenGLProfile,
+ kCGLPFARendererID,
+ CGLPixelFormatAttribute(UInt32(kCGLRendererGenericFloatID)),
+ kCGLPFADoubleBuffer
+ ]
+
+ let glFormatOptional: [CGLPixelFormatAttribute] = [
+ kCGLPFABackingStore,
+ kCGLPFAAllowOfflineRenderers,
+ kCGLPFASupportsAutomaticGraphicsSwitching
+ ]
+
+ let attributeLookUp: [UInt32:String] = [
+ kCGLOGLPVersion_3_2_Core.rawValue: "kCGLOGLPVersion_3_2_Core",
+ kCGLOGLPVersion_Legacy.rawValue: "kCGLOGLPVersion_Legacy",
+ kCGLPFAOpenGLProfile.rawValue: "kCGLPFAOpenGLProfile",
+ UInt32(kCGLRendererGenericFloatID): "kCGLRendererGenericFloatID",
+ kCGLPFARendererID.rawValue: "kCGLPFARendererID",
+ kCGLPFAAccelerated.rawValue: "kCGLPFAAccelerated",
+ kCGLPFADoubleBuffer.rawValue: "kCGLPFADoubleBuffer",
+ kCGLPFABackingStore.rawValue: "kCGLPFABackingStore",
+ kCGLPFAAllowOfflineRenderers.rawValue: "kCGLPFAAllowOfflineRenderers",
+ kCGLPFASupportsAutomaticGraphicsSwitching.rawValue: "kCGLPFASupportsAutomaticGraphicsSwitching",
+ ]
+
var needsICCUpdate: Bool = false {
didSet {
if needsICCUpdate == true {
@@ -147,82 +184,63 @@ class VideoLayer: CAOpenGLLayer {
override func copyCGLPixelFormat(forDisplayMask mask: UInt32) -> CGLPixelFormatObj {
if cglPixelFormat != nil { return cglPixelFormat! }
- let attributeLookUp: [UInt32:String] = [
- kCGLOGLPVersion_3_2_Core.rawValue: "kCGLOGLPVersion_3_2_Core",
- kCGLOGLPVersion_Legacy.rawValue: "kCGLOGLPVersion_Legacy",
- kCGLPFAOpenGLProfile.rawValue: "kCGLPFAOpenGLProfile",
- kCGLPFAAccelerated.rawValue: "kCGLPFAAccelerated",
- kCGLPFADoubleBuffer.rawValue: "kCGLPFADoubleBuffer",
- kCGLPFABackingStore.rawValue: "kCGLPFABackingStore",
- kCGLPFAAllowOfflineRenderers.rawValue: "kCGLPFAAllowOfflineRenderers",
- kCGLPFASupportsAutomaticGraphicsSwitching.rawValue: "kCGLPFASupportsAutomaticGraphicsSwitching",
- 0: ""
- ]
-
- let glVersions: [CGLOpenGLProfile] = [
- kCGLOGLPVersion_3_2_Core,
- kCGLOGLPVersion_Legacy
- ]
+ var pix: CGLPixelFormatObj?
+ var err: CGLError = CGLError(rawValue: 0)
+
+ if mpv.macOpts!.cocoa_cb_sw_renderer != 1 {
+ (pix, err) = createPixelFormat()
+ }
+
+ if (err != kCGLNoError || pix == nil) && mpv.macOpts!.cocoa_cb_sw_renderer != 0 {
+ (pix, err) = createPixelFormat(software: true)
+ }
+
+ if err != kCGLNoError || pix == nil {
+ mpv.sendError("Couldn't create any CGL pixel format")
+ exit(1)
+ }
+
+ return pix!
+ }
+ func createPixelFormat(software: Bool = false) -> (CGLPixelFormatObj?, CGLError) {
var pix: CGLPixelFormatObj?
var err: CGLError = CGLError(rawValue: 0)
var npix: GLint = 0
- verLoop : for ver in glVersions {
- if mpv.macOpts!.cocoa_cb_sw_renderer == 1 { break }
-
- var glAttributes: [CGLPixelFormatAttribute] = [
- kCGLPFAOpenGLProfile, CGLPixelFormatAttribute(ver.rawValue),
- kCGLPFAAccelerated,
- kCGLPFADoubleBuffer,
- kCGLPFABackingStore,
- kCGLPFAAllowOfflineRenderers,
- kCGLPFASupportsAutomaticGraphicsSwitching,
- _CGLPixelFormatAttribute(rawValue: 0)
- ]
-
- for index in stride(from: glAttributes.count-2, through: 4, by: -1) {
- err = CGLChoosePixelFormat(glAttributes, &pix, &npix)
+ for ver in glVersions {
+ var glBase = software ? glFormatSoftwareBase : glFormatBase
+ glBase.insert(CGLPixelFormatAttribute(ver.rawValue), at: 1)
+ var glFormat = glBase + glFormatOptional
+
+ for index in stride(from: glFormat.count-1, through: glBase.count-1, by: -1) {
+ let format = glFormat + [_CGLPixelFormatAttribute(rawValue: 0)]
+ err = CGLChoosePixelFormat(format, &pix, &npix)
+
if err == kCGLBadAttribute || err == kCGLBadPixelFormat || pix == nil {
- glAttributes.remove(at: index)
+ glFormat.remove(at: index)
} else {
- var attArray = glAttributes.map({ (value: _CGLPixelFormatAttribute) -> String in
+ let attArray = glFormat.map({ (value: _CGLPixelFormatAttribute) -> String in
return attributeLookUp[value.rawValue]!
})
- attArray.removeLast()
mpv.sendVerbose("Created CGL pixel format with attributes: " +
"\(attArray.joined(separator: ", "))")
- break verLoop
+ return (pix, err)
}
}
}
- if (err != kCGLNoError || pix == nil) && mpv.macOpts!.cocoa_cb_sw_renderer != 0 {
- if mpv.macOpts!.cocoa_cb_sw_renderer == -1 {
- let errS = String(cString: CGLErrorString(err))
- mpv.sendWarning("Couldn't create hardware accelerated CGL " +
- "pixel format, falling back to software " +
- "renderer: \(errS) (\(err.rawValue))")
- }
-
- let glAttributes: [CGLPixelFormatAttribute] = [
- kCGLPFAOpenGLProfile, CGLPixelFormatAttribute(kCGLOGLPVersion_3_2_Core.rawValue),
- kCGLPFARendererID, CGLPixelFormatAttribute(UInt32(kCGLRendererGenericFloatID)),
- kCGLPFADoubleBuffer,
- kCGLPFABackingStore,
- _CGLPixelFormatAttribute(rawValue: 0)
- ]
+ let errS = String(cString: CGLErrorString(err))
+ mpv.sendWarning("Couldn't create a " +
+ "\(software ? "software" : "hardware accelerated") " +
+ "CGL pixel format: \(errS) (\(err.rawValue))")
- err = CGLChoosePixelFormat(glAttributes, &pix, &npix)
+ if software == false && mpv.macOpts!.cocoa_cb_sw_renderer == -1 {
+ mpv.sendWarning("Falling back to software renderer")
}
- if err != kCGLNoError || pix == nil {
- let errS = String(cString: CGLErrorString(err))
- mpv.sendError("Couldn't create any CGL pixel format: \(errS) (\(err.rawValue))")
- exit(1)
- }
- return pix!
+ return (pix, err)
}
override func copyCGLContext(forPixelFormat pf: CGLPixelFormatObj) -> CGLContextObj {