1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/Shared/Player/MPV/MPVOGLView.swift

72 lines
1.9 KiB
Swift
Raw Normal View History

2022-02-17 01:53:11 +05:30
import GLKit
2022-03-27 17:12:20 +05:30
import Logging
2023-06-26 09:48:28 +05:30
import MPVKit
2023-09-23 20:12:46 +05:30
import OpenGLES
2022-02-17 01:53:11 +05:30
final class MPVOGLView: GLKView {
2022-03-27 17:12:20 +05:30
private var logger = Logger(label: "stream.yattee.mpv.oglview")
2022-02-17 01:53:11 +05:30
private var defaultFBO: GLint?
var mpvGL: UnsafeMutableRawPointer?
2022-12-27 00:11:37 +05:30
var queue = DispatchQueue(label: "stream.yattee.opengl")
2022-02-17 01:53:11 +05:30
var needsDrawing = true
override init(frame: CGRect) {
guard let context = EAGLContext(api: .openGLES2) else {
2022-02-17 01:53:11 +05:30
print("Failed to initialize OpenGLES 2.0 context")
exit(1)
}
2022-03-27 17:12:20 +05:30
logger.info("frame size: \(frame.width) x \(frame.height)")
2022-02-17 01:53:11 +05:30
super.init(frame: frame, context: context)
self.context = context
bindDrawable()
2022-02-17 01:53:11 +05:30
defaultFBO = -1
isOpaque = true
enableSetNeedsDisplay = false
2022-02-17 01:53:11 +05:30
fillBlack()
}
func fillBlack() {
glClearColor(0, 0, 0, 0)
glClear(UInt32(GL_COLOR_BUFFER_BIT))
}
2022-03-27 17:12:20 +05:30
override func draw(_: CGRect) {
2022-09-28 19:57:01 +05:30
guard needsDrawing, let mpvGL else {
2022-06-16 05:33:15 +05:30
return
}
2022-02-17 01:53:11 +05:30
glGetIntegerv(UInt32(GL_FRAMEBUFFER_BINDING), &defaultFBO!)
2022-03-27 17:12:20 +05:30
var dims: [GLint] = [0, 0, 0, 0]
glGetIntegerv(GLenum(GL_VIEWPORT), &dims)
2022-06-16 05:33:15 +05:30
var data = mpv_opengl_fbo(
fbo: Int32(defaultFBO!),
w: Int32(dims[2]),
h: Int32(dims[3]),
internal_format: 0
)
var flip: CInt = 1
withUnsafeMutablePointer(to: &flip) { flip in
withUnsafeMutablePointer(to: &data) { data in
var params = [
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()
]
mpv_render_context_render(OpaquePointer(mpvGL), &params)
2022-02-17 01:53:11 +05:30
}
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}