diff --git a/src/cpp/components/CMakeLists.txt b/src/cpp/components/CMakeLists.txt index 3e74c2d..a19e8d4 100644 --- a/src/cpp/components/CMakeLists.txt +++ b/src/cpp/components/CMakeLists.txt @@ -1,6 +1,8 @@ set(COMPONENTS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/deivicelistviewmodel.cpp ${CMAKE_CURRENT_SOURCE_DIR}/deivicelistviewmodel.h + ${CMAKE_CURRENT_SOURCE_DIR}/fpsitem.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/fpsitem.h ) target_sources(${EXE_NAME} PRIVATE ${COMPONENTS_SRC}) diff --git a/src/cpp/components/fpsitem.cpp b/src/cpp/components/fpsitem.cpp new file mode 100644 index 0000000..8c373b0 --- /dev/null +++ b/src/cpp/components/fpsitem.cpp @@ -0,0 +1,24 @@ +#include "fpsitem.h" + +#include +#include + +FpsItem::FpsItem() { + auto *timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, [this] { + setFps(_frameCount); + _frameCount = 0; + }); + connect(this, &QQuickItem::windowChanged, this, [this] { + if (window()) { + connect(window(), &QQuickWindow::afterRendering, this, [this] { _frameCount++; }, Qt::DirectConnection); + } + }); + timer->start(1000); +} + +void FpsItem::setFps(int fps) +{ + m_fps = fps; + Q_EMIT fpsChanged(fps); +} diff --git a/src/cpp/components/fpsitem.h b/src/cpp/components/fpsitem.h new file mode 100644 index 0000000..13e488d --- /dev/null +++ b/src/cpp/components/fpsitem.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +class FpsItem : public QQuickItem { + Q_OBJECT + Q_PROPERTY(int fps WRITE setFps READ getFps NOTIFY fpsChanged) +public: + FpsItem(); + void setFps(int); + [[nodiscard]] inline int getFps() const { return m_fps; }; + +signals: + void fpsChanged(int); + +private: + int _frameCount = 0; + int m_fps = 0; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ecb3b4f..1eaa434 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include "cpp/imagePageTool/imagePageTool.h" #include "cpp/settingPageTools/settingPageTools.h" #include "cpp/utils/notificationcontroller.h" +#include "cpp/components/fpsitem.h" bool checkADB() { QProcess process; @@ -96,8 +97,15 @@ int main(int argc, char *argv[]) qmlRegisterSingletonInstance("NotificationController", 1, 0, "NotificationController", NotificationController::instance()); qInfo() << "load8" << loaderTimer.elapsed(); + qmlRegisterType( "FpsItem", 1, 0, "FpsItem"); + QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL); + QSurfaceFormat format; + format.setSwapBehavior(QSurfaceFormat::DoubleBuffer); + format.setSwapInterval(0); + QSurfaceFormat::setDefaultFormat(format); + QQmlApplicationEngine engine; const QUrl url("qrc:/qml/Main.qml"); engine.load(url); diff --git a/src/qml/Main.qml b/src/qml/Main.qml index e329939..717c700 100644 --- a/src/qml/Main.qml +++ b/src/qml/Main.qml @@ -8,6 +8,7 @@ import "./components/" import "./pages/" import ADBControl 1.0 import WallpaperHelper 1.0 +import FpsItem 1.0 ApplicationWindow { id: root @@ -27,6 +28,20 @@ ApplicationWindow { blurTarget: rootRect } + FpsItem { + id: fpsItem + } + + Text { + z: 1000 + anchors.right: parent.right + anchors.bottom: parent.bottom + anchors.bottomMargin: 10 + anchors.rightMargin: 10 + text: "fps:" + fpsItem.fps + color: "#0495d5" + } + Rectangle { id: rootRect color: "transparent"