1
0

add nanovg based osd

This commit is contained in:
2023-10-19 15:03:27 +08:00
parent d03c80b281
commit 330af71593
20 changed files with 23556 additions and 6 deletions

View File

@@ -2,14 +2,17 @@
#include <GLFW/glfw3.h>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <mpv/client.h>
#include "nanovg/nanovg.h"
#ifdef USE_D3D11
#include <mpv/render_dxgi.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#include <d3d11.h>
#define NANOVG_D3D11_IMPLEMENTATION
#include "nanovg/nanovg_d3d11.h"
static ID3D11Device* d3dDevice = nullptr;
static ID3D11DeviceContext* d3dDeviceContext = nullptr;
@@ -23,9 +26,14 @@ bool ResizeD3D(GLFWwindow* window, int width, int height);
#else
#include <mpv/render_gl.h>
#include <glad/glad.h>
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg/nanovg_gl.h"
#endif
#ifdef __SWITCH__
#ifdef _WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#elif defined( __SWITCH__)
#include <switch.h>
extern "C" void userAppInit() {
@@ -60,6 +68,7 @@ int main(int argc, char* argv[]) {
if (!glfwInit()) die("init glfw failed");
NVGcontext* vg = nullptr;
mpv_render_context* mpv_context = nullptr;
mpv_handle* mpv = mpv_create();
mpv_set_option_string(mpv, "subs-fallback", "yes");
@@ -67,6 +76,7 @@ int main(int argc, char* argv[]) {
mpv_set_option_string(mpv, "vd-lavc-dr", "yes");
mpv_set_option_string(mpv, "terminal", "yes");
mpv_set_option_string(mpv, "hwdec", "auto");
mpv_set_option_string(mpv, "vo", "gpu-next");
#ifdef _DEBUG
mpv_set_option_string(this->mpv, "msg-level", "ffmpeg=trace");
mpv_set_option_string(this->mpv, "msg-level", "all=v");
@@ -97,6 +107,10 @@ int main(int argc, char* argv[]) {
printf("glfwKeyCallback key press %d\n", key);
}
});
#ifdef _WIN32
HWND wid = glfwGetWin32Window(window);
mpv_set_option(mpv, "wid", MPV_FORMAT_INT64, &wid);
#endif
#ifdef USE_D3D11
// Initialize Direct3D
HWND hwnd = glfwGetWin32Window(window);
@@ -104,6 +118,8 @@ int main(int argc, char* argv[]) {
if (!InitD3D(hwnd)) die("init dx11 failed");
glfwSetFramebufferSizeCallback(window, (GLFWframebuffersizefun)ResizeD3D);
vg = nvgCreateD3D11(d3dDevice, NVG_STENCIL_STROKES | NVG_ANTIALIAS);
mpv_dxgi_init_params init_params = {d3dDevice, d3dSwapChain};
mpv_render_param params[] = {
{MPV_RENDER_PARAM_API_TYPE, (void*)MPV_RENDER_API_TYPE_DXGI},
@@ -117,6 +133,11 @@ int main(int argc, char* argv[]) {
glfwSwapInterval(1);
glfwSetTime(0);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_ANTIALIAS);
mpv_opengl_init_params gl_init_params{
[](void* fn_ctx, const char* name) { return (void*)glfwGetProcAddress(name); }, nullptr};
mpv_render_param params[] = {
@@ -135,6 +156,17 @@ int main(int argc, char* argv[]) {
},
mpv_context);
#ifdef __SWITCH__
PlFontData fd;
if (R_SUCCEEDED(plGetSharedFontByType(&fd, PlSharedFontType_Standard))) {
int font = nvgCreateFontMem(vg, "sans", (unsigned char*)fd.address, fd.size, 0);
if (font < 0) die("load font failed");
}
#else
int font = nvgCreateFont(vg, "sans", "Roboto-Regular.ttf");
if (font < 0) die("load font failed");
#endif
const char* args[] = {"loadfile", argv[1], nullptr};
if (mpv_command(mpv, args) < 0) die("load file failed");
@@ -160,9 +192,37 @@ int main(int argc, char* argv[]) {
mpv_render_context_report_swap(mpv_context);
#ifdef USE_D3D11
d3dDeviceContext->OMSetRenderTargets(1, &d3dRenderTargetView, nullptr);
d3dSwapChain->Present(1, 0); // Present with vsync
#else
glViewport(0, 0, fbw, fbh);
#endif
if (osdShow) {
// Draw OSD
std::string profiles[4] = {
"Video Codec: " + std::string(mpv_get_property_string(mpv, "video-codec")),
"Pixel Format: " + std::string(mpv_get_property_string(mpv, "video-params/pixelformat")),
"Hardware Decode: " + std::string(mpv_get_property_string(mpv, "hwdec-current")),
"Video Bitrate: " + std::to_string(mpv_property_int(mpv, "video-bitrate") / 1024),
};
nvgBeginFrame(vg, fbw, fbh, 1.0f);
nvgBeginPath(vg);
nvgRect(vg, 10, 10, 800, 400);
nvgFillColor(vg, nvgRGBA(0, 0, 0, 128));
nvgFill(vg);
nvgFontSize(vg, 20.0f);
nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_TOP);
nvgFillColor(vg, nvgRGBA(240, 240, 240, 255));
nvgFontFace(vg, "sans");
for (size_t i = 0; i < 4; i++) {
nvgText(vg, 30, 30 * (i + 1), profiles[i].c_str(), nullptr);
}
nvgEndFrame(vg);
}
#ifdef USE_D3D11
d3dSwapChain->Present(1, 0); // Present with vsync
#else
glfwSwapBuffers(window);
#endif
glfwWaitEvents();
@@ -173,7 +233,10 @@ int main(int argc, char* argv[]) {
mpv_terminate_destroy(mpv);
#ifdef USE_D3D11
nvgDeleteD3D11(vg);
CleanupD3D();
#elif defined(NANOVG_GL3)
nvgDeleteGL3(vg);
#endif
glfwDestroyWindow(window);
glfwTerminate();