#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #pragma comment(lib, "WinMM") #include #include #define g_FrameW 1440//3840//7680 #define g_FrameH 960//2160//4320 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); m_bThreadRunning_Update = false; m_ptrThread_Update = nullptr; const int pixel_w = g_FrameW; const int pixel_h = g_FrameH; // m_varOpenGLView.Init(eGL_ImageFormat_RGBA, pixel_w, pixel_h); { uint32_t hWnd = 0; if (m_varOpenGLView.GetWndHandle(hWnd)) { QWindow* pWindow = QWindow::fromWinId((WId)hWnd); QWidget* pWindowContainer = QWidget::createWindowContainer(pWindow); ui->m_GLViewLayout->addWidget(pWindowContainer); } } // m_pTestImageData = new uint8_t[pixel_w * pixel_h * 4 * 2 + 1]; for (int i = 0; i < pixel_w * pixel_h * 4 * 2; i++) { m_pTestImageData[i] = i % 255; } m_nTestImageDataIndex = 0; // StartThread_Update(); // connect(&m_varTimerGL, &QTimer::timeout, this, &MainWindow::OnTimer_UpdateGL); m_varTimerGL.setTimerType(Qt::PreciseTimer); m_varTimerGL.start(20); //glfwTerminate(); //exit(EXIT_SUCCESS); ///////////////////////////////////////////////// } MainWindow::~MainWindow() { disconnect(&m_varTimerGL, &QTimer::timeout, this, &MainWindow::OnTimer_UpdateGL); StopThread_Update(); ::timeEndPeriod(0); m_varOpenGLView.Uninit(); if (nullptr != m_pTestImageData) { delete [] m_pTestImageData; m_pTestImageData = nullptr; } delete ui; } void MainWindow::OnTimer_UpdateGL() { const int pixel_w = g_FrameW; const int pixel_h = g_FrameH; m_varOpenGLView.DrawImage(reinterpret_cast(&m_pTestImageData[m_nTestImageDataIndex]), pixel_w, pixel_h); } void MainWindow::ThreadEntry(pCallback_ThreadFun pThreadFun, void* pOwner) { MainWindow* pThis = reinterpret_cast(pOwner); if (nullptr == pThis) { return; } (pThis->*pThreadFun)(); } bool MainWindow::ThreadFun_Update() { const int pixel_w = g_FrameW; const int pixel_h = g_FrameH; uint64_t szCounter[100]; memset(szCounter, 0, sizeof(szCounter)); int nIndex = 0; int nCounter = 0; while (m_bThreadRunning_Update) { nIndex += pixel_w * 4; if (nIndex >= pixel_w * pixel_h * 4) { nIndex = 0; } m_nTestImageDataIndex = nIndex; memcpy(&szCounter[0], &szCounter[1], 99 * sizeof(uint64_t)); szCounter[99] = std::chrono::high_resolution_clock::now().time_since_epoch().count(); nCounter++; if (nCounter >= 120) { nCounter = 0; double dSpan = double(szCounter[99] - szCounter[0]); double dFPS = 100.0 / (dSpan / 1000000000.0); qDebug("dSpan : %f, fps: %f", dSpan, dFPS); int jj = 0; } std::this_thread::sleep_for(std::chrono::microseconds(10 * 1000)); // 100FPS } return true; } bool MainWindow::StartThread_Update() { StopThread_Update(); // m_bThreadRunning_Update = true; m_ptrThread_Update = new std::thread(std::bind(&MainWindow::ThreadEntry, &MainWindow::ThreadFun_Update, (void*)this)); return true; } void MainWindow::StopThread_Update() { // if (m_bThreadRunning_Update) { m_bThreadRunning_Update = false; if (nullptr != m_ptrThread_Update) { m_ptrThread_Update->join(); delete m_ptrThread_Update; m_ptrThread_Update = nullptr; } } }