diff options
author | der richter <der.richter@gmx.de> | 2020-12-20 20:03:12 +0100 |
---|---|---|
committer | der richter <der.richter@gmx.de> | 2021-01-13 16:23:47 +0100 |
commit | bc58361d86e695d3bf875e1dd826f9fbbd586502 (patch) | |
tree | deba27b464142641a036251f4e9f28b0b6f2bcde /osdep | |
parent | d5ab5482a9b35650600ca68bcfee752995e8337b (diff) | |
download | mpv-bc58361d86e695d3bf875e1dd826f9fbbd586502.tar.bz2 mpv-bc58361d86e695d3bf875e1dd826f9fbbd586502.tar.xz |
mac: fix usage of temporary pointers
the pointer used to initialise the respective structs is only guaranteed
to be alive within this constructor. the struct itself is used later and
the data it points to, is not guaranteed to be the same.
to fix this we define a scope that pointer is definitely valid and use
it within this scope. a helper function was added to get the pointers
for several data at once. otherwise we would need to nest
withUnsafeMutableBytes several times, which would make it hard to read.
Diffstat (limited to 'osdep')
-rw-r--r-- | osdep/macos/libmpv_helper.swift | 68 | ||||
-rw-r--r-- | osdep/macos/mpv_helper.swift | 18 |
2 files changed, 57 insertions, 29 deletions
diff --git a/osdep/macos/libmpv_helper.swift b/osdep/macos/libmpv_helper.swift index d1b00cf304..b140f99587 100644 --- a/osdep/macos/libmpv_helper.swift +++ b/osdep/macos/libmpv_helper.swift @@ -47,23 +47,26 @@ class LibmpvHelper { } func initRender() { - var advanced: CInt = 1 + let advanced: CInt = 1 let api = UnsafeMutableRawPointer(mutating: (MPV_RENDER_API_TYPE_OPENGL as NSString).utf8String) - var pAddress = mpv_opengl_init_params(get_proc_address: getProcAddress, + let pAddress = mpv_opengl_init_params(get_proc_address: getProcAddress, get_proc_address_ctx: nil, extra_exts: nil) - var params: [mpv_render_param] = [ - mpv_render_param(type: MPV_RENDER_PARAM_API_TYPE, data: api), - mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, data: &pAddress), - mpv_render_param(type: MPV_RENDER_PARAM_ADVANCED_CONTROL, data: &advanced), - mpv_render_param() - ] - - if (mpv_render_context_create(&mpvRenderContext, mpvHandle, ¶ms) < 0) - { - log.sendError("Render context init has failed.") - exit(1) + + MPVHelper.withUnsafeMutableRawPointers([pAddress, advanced]) { (pointers: [UnsafeMutableRawPointer?]) in + var params: [mpv_render_param] = [ + mpv_render_param(type: MPV_RENDER_PARAM_API_TYPE, data: api), + mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, data: pointers[0]), + mpv_render_param(type: MPV_RENDER_PARAM_ADVANCED_CONTROL, data: pointers[1]), + mpv_render_param() + ] + + if (mpv_render_context_create(&mpvRenderContext, mpvHandle, ¶ms) < 0) { + log.sendError("Render context init has failed.") + exit(1) + } } + } let getProcAddress: (@convention(c) (UnsafeMutableRawPointer?, UnsafePointer<Int8>?) @@ -119,26 +122,29 @@ class LibmpvHelper { deinitLock.lock() if mpvRenderContext != nil { var i: GLint = 0 - var flip: CInt = 1 - var skip: CInt = skip ? 1 : 0 - var ditherDepth = depth + let flip: CInt = 1 + let skip: CInt = skip ? 1 : 0 + let ditherDepth = depth glGetIntegerv(GLenum(GL_DRAW_FRAMEBUFFER_BINDING), &i) // CAOpenGLLayer has ownership of FBO zero yet can return it to us, // so only utilize a newly received FBO ID if it is nonzero. fbo = i != 0 ? i : fbo - var data = mpv_opengl_fbo(fbo: Int32(fbo), + let data = mpv_opengl_fbo(fbo: Int32(fbo), w: Int32(surface.width), h: Int32(surface.height), internal_format: 0) - var params: [mpv_render_param] = [ - mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: &data), - mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: &flip), - mpv_render_param(type: MPV_RENDER_PARAM_DEPTH, data: &ditherDepth), - mpv_render_param(type: MPV_RENDER_PARAM_SKIP_RENDERING, data: &skip), - mpv_render_param() - ] - mpv_render_context_render(mpvRenderContext, ¶ms); + + MPVHelper.withUnsafeMutableRawPointers([data, flip, ditherDepth, skip]) { (pointers: [UnsafeMutableRawPointer?]) in + var params: [mpv_render_param] = [ + mpv_render_param(type: MPV_RENDER_PARAM_OPENGL_FBO, data: pointers[0]), + mpv_render_param(type: MPV_RENDER_PARAM_FLIP_Y, data: pointers[1]), + mpv_render_param(type: MPV_RENDER_PARAM_DEPTH, data: pointers[2]), + mpv_render_param(type: MPV_RENDER_PARAM_SKIP_RENDERING, data: pointers[3]), + mpv_render_param() + ] + mpv_render_context_render(mpvRenderContext, ¶ms); + } } else { glClearColor(0, 0, 0, 1) glClear(GLbitfield(GL_COLOR_BUFFER_BIT)) @@ -161,16 +167,20 @@ class LibmpvHelper { let u8Ptr = baseAddress.assumingMemoryBound(to: UInt8.self) let iccBstr = bstrdup(nil, bstr(start: u8Ptr, len: ptr.count)) var icc = mpv_byte_array(data: iccBstr.start, size: iccBstr.len) - let params = mpv_render_param(type: MPV_RENDER_PARAM_ICC_PROFILE, data: &icc) - mpv_render_context_set_parameter(mpvRenderContext, params) + withUnsafeMutableBytes(of: &icc) { (ptr: UnsafeMutableRawBufferPointer) in + let params = mpv_render_param(type: MPV_RENDER_PARAM_ICC_PROFILE, data: ptr.baseAddress) + mpv_render_context_set_parameter(mpvRenderContext, params) + } } } func setRenderLux(_ lux: Int) { if mpvRenderContext == nil { return } var light = lux - let params = mpv_render_param(type: MPV_RENDER_PARAM_AMBIENT_LIGHT, data: &light) - mpv_render_context_set_parameter(mpvRenderContext, params) + withUnsafeMutableBytes(of: &light) { (ptr: UnsafeMutableRawBufferPointer) in + let params = mpv_render_param(type: MPV_RENDER_PARAM_AMBIENT_LIGHT, data: ptr.baseAddress) + mpv_render_context_set_parameter(mpvRenderContext, params) + } } func commandAsync(_ cmd: [String?], id: UInt64 = 1) { diff --git a/osdep/macos/mpv_helper.swift b/osdep/macos/mpv_helper.swift index 0e207b994b..171687bd89 100644 --- a/osdep/macos/mpv_helper.swift +++ b/osdep/macos/mpv_helper.swift @@ -123,4 +123,22 @@ class MPVHelper { class func bridge<T: AnyObject>(ptr: UnsafeRawPointer) -> T { return Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue() } + + class func withUnsafeMutableRawPointers(_ arguments: [Any], + pointers: [UnsafeMutableRawPointer?] = [], + closure: (_ pointers: [UnsafeMutableRawPointer?]) -> Void) { + if arguments.count > 0 { + let args = Array(arguments.dropFirst(1)) + var newPtrs = pointers + var firstArg = arguments.first + withUnsafeMutableBytes(of: &firstArg) { (ptr: UnsafeMutableRawBufferPointer) in + newPtrs.append(ptr.baseAddress) + withUnsafeMutableRawPointers(args, pointers: newPtrs, closure: closure) + } + + return + } + + closure(pointers) + } } |