diff --git a/cmake/macros/SetupHifiTestCase.cmake b/cmake/macros/SetupHifiTestCase.cmake new file mode 100644 index 0000000000..facef8131e --- /dev/null +++ b/cmake/macros/SetupHifiTestCase.cmake @@ -0,0 +1,132 @@ +# +# SetupHifiTestCase.cmake +# +# Copyright 2015 High Fidelity, Inc. +# +# Distributed under the Apache License, Version 2.0. +# See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +# + +# Sets up a hifi testcase using QtTest. +# Can be called with arguments; like setup_hifi_project, the arguments correspond to qt modules, so call it +# via setup_hifi_testcase(Script Network Qml) to build the testcase with Qt5Script, Qt5Network, and Qt5Qml linked, +# for example. + +# One special quirk of this is that because we are creating multiple testcase targets (instead of just one), +# any dependencies and other setup that the testcase has must be declared in a macro, and setup_hifi_testcase() +# must be called *after* this declaration, not before it. + +# Here's a full example: +# tests/my-foo-test/CMakeLists.txt: +# +# # Declare testcase dependencies +# macro (setup_hifi_testcase) +# bunch +# of +# custom +# dependencies... +# +# link_hifi_libraries(shared networking etc) +# +# copy_dlls_beside_windows_executable() +# endmacro() +# +# setup_hifi_testcase(Network etc) +# +# Additionally, all .cpp files in the src dir (eg tests/my-foo-test/src) must: +# - Contain exactly one test class (any supporting code must be either external or inline in a .hpp file) +# - Be built against QtTestLib (test class should be a QObject, with test methods defined as private slots) +# - Contain a QTEST_MAIN declaration at the end of the file (or at least be a standalone executable) +# +# All other testing infrastructure is generated automatically. +# + +macro(SETUP_HIFI_TESTCASE) + if (NOT DEFINED TEST_PROJ_NAME) + message(SEND_ERROR "Missing TEST_PROJ_NAME (setup_hifi_testcase was called incorrectly?)") + elseif (NOT COMMAND SETUP_TESTCASE_DEPENDENCIES) + message(SEND_ERROR "Missing testcase dependencies declaration (SETUP_TESTCASE_DEPENDENCIES)") + elseif (DEFINED ${TEST_PROJ_NAME}_BUILT) + message(WARNING "testcase \"" ${TEST_PROJ_NAME} "\" was already built") + else () + set(${TEST_PROJ_NAME}_BUILT 1) + + file(GLOB TEST_PROJ_SRC_FILES src/*) + file(GLOB TEST_PROJ_SRC_SUBDIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/src/*) + + foreach (DIR ${TEST_PROJ_SRC_SUBDIRS}) + if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/src/${DIR}") + file(GLOB DIR_CONTENTS "src/${DIR}/*") + set(TEST_PROJ_SRC_FILES ${TEST_PROJ_SRC_FILES} "${DIR_CONTENTS}") + endif() + endforeach() + + # Find test classes to build into test executables. + # Warn about any .cpp files that are *not* test classes (*Test[s].cpp), since those files will not be used. + foreach (SRC_FILE ${TEST_PROJ_SRC_FILES}) + string(REGEX MATCH ".+Tests?\\.cpp$" TEST_CPP_FILE ${SRC_FILE}) + string(REGEX MATCH ".+\\.cpp$" NON_TEST_CPP_FILE ${SRC_FILE}) + if (TEST_CPP_FILE) + list(APPEND TEST_CASE_FILES ${TEST_CPP_FILE}) + elseif (NON_TEST_CPP_FILE) + message(WARNING "ignoring .cpp file (not a test class -- this will not be linked or compiled!): " ${NON_TEST_CPP_FILE}) + endif () + endforeach () + + if (TEST_CASE_FILES) + set(TEST_PROJ_TARGETS "") + + # Add each test class executable (duplicates functionality in SetupHifiProject.cmake) + # The resulting targets will be buried inside of hidden/test-executables so that they don't clutter up + # the project view in Xcode, Visual Studio, etc. + foreach (TEST_FILE ${TEST_CASE_FILES}) + get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE) + set(TARGET_NAME ${TEST_PROJ_NAME}-${TEST_NAME}) + + project(${TARGET_NAME}) + + # grab the implemenation and header files + set(TARGET_SRCS ${TEST_FILE}) # only one source / .cpp file (the test class) + + add_executable(${TARGET_NAME} ${TEST_FILE}) + add_test(${TARGET_NAME}-test ${TARGET_NAME}) + + list (APPEND ${TEST_PROJ_NAME}_TARGETS ${TARGET_NAME}) + #list (APPEND ALL_TEST_TARGETS ${TARGET_NAME}) + + set(${TARGET_NAME}_DEPENDENCY_QT_MODULES ${ARGN}) + + list(APPEND ${TARGET_NAME}_DEPENDENCY_QT_MODULES Core Test) + + # find these Qt modules and link them to our own target + find_package(Qt5 COMPONENTS ${${TARGET_NAME}_DEPENDENCY_QT_MODULES} REQUIRED) + + foreach(QT_MODULE ${${TARGET_NAME}_DEPENDENCY_QT_MODULES}) + target_link_libraries(${TARGET_NAME} Qt5::${QT_MODULE}) + endforeach() + + set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "hidden/test-executables") + + # handle testcase-specific dependencies (this a macro that should be defined in the cmakelists.txt file in each tests subdir) + + SETUP_TESTCASE_DEPENDENCIES () + endforeach () + + set(TEST_TARGET ${TEST_PROJ_NAME}-tests) + + # Add a dummy target so that the project files are visible. + # This target will also build + run the other test targets using ctest when built. + + add_custom_target(${TEST_TARGET} ALL + COMMAND ctest . + SOURCES ${TEST_PROJ_SRC_FILES} # display source files under the testcase target + DEPENDS ${${TEST_PROJ_NAME}_TARGETS}) + set_target_properties(${TEST_TARGET} PROPERTIES FOLDER "Tests") + + list (APPEND ALL_TEST_TARGETS ${TEST_TARGET}) + set(ALL_TEST_TARGETS "${ALL_TEST_TARGETS}" PARENT_SCOPE) + else () + message(WARNING "No testcases in " ${TEST_PROJ_NAME}) + endif () + endif () +endmacro(SETUP_HIFI_TESTCASE) \ No newline at end of file diff --git a/tests/jitter/src/JitterTests.cpp b/tests/jitter/src/JitterTests.cpp new file mode 100644 index 0000000000..b09cb40d3e --- /dev/null +++ b/tests/jitter/src/JitterTests.cpp @@ -0,0 +1,391 @@ +// +// main.cpp +// JitterTester +// +// Created by Philip on 8/1/14. +// Copyright (c) 2014 High Fidelity, Inc. All rights reserved. +// + +#include +#ifdef _WINDOWS +#include +#else +#include +#include +#endif +#include +#include + +#include +#include +#include +#include // for usecTimestampNow +#include +#include + +#include "JitterTests.h" + +// Uncomment this to run manually +//#define RUN_MANUALLY + +#ifndef RUN_MANUALLY + +QTEST_MAIN(JitterTests) + +#else // RUN_MANUALLY + +const quint64 MSEC_TO_USEC = 1000; +const quint64 LARGE_STATS_TIME = 500; // we don't expect stats calculation to take more than this many usecs + +void runSend(const char* addressOption, int port, int gap, int size, int report); +void runReceive(const char* addressOption, int port, int gap, int size, int report); + + +int main(int argc, const char * argv[]) { + if (argc != 7) { + printf("usage: jitter-tests <--send|--receive>
\n"); + exit(1); + } + const char* typeOption = argv[1]; + const char* addressOption = argv[2]; + const char* portOption = argv[3]; + const char* gapOption = argv[4]; + const char* sizeOption = argv[5]; + const char* reportOption = argv[6]; + int port = atoi(portOption); + int gap = atoi(gapOption); + int size = atoi(sizeOption); + int report = atoi(reportOption); + + std::cout << "type:" << typeOption << "\n"; + std::cout << "address:" << addressOption << "\n"; + std::cout << "port:" << port << "\n"; + std::cout << "gap:" << gap << "\n"; + std::cout << "size:" << size << "\n"; + + if (strcmp(typeOption, "--send") == 0) { + runSend(addressOption, port, gap, size, report); + } else if (strcmp(typeOption, "--receive") == 0) { + runReceive(addressOption, port, gap, size, report); + } + exit(1); +} + +void runSend(const char* addressOption, int port, int gap, int size, int report) { + std::cout << "runSend...\n"; + +#ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + printf("WSAStartup failed with error %d\n", WSAGetLastError()); + return; + } +#endif + + int sockfd; + struct sockaddr_in servaddr; + + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + inet_pton(AF_INET, addressOption, &servaddr.sin_addr); + servaddr.sin_port = htons(port); + + const int SAMPLES_FOR_SECOND = 1000000 / gap; + std::cout << "SAMPLES_FOR_SECOND:" << SAMPLES_FOR_SECOND << "\n"; + const int INTERVALS_PER_30_SECONDS = 30; + std::cout << "INTERVALS_PER_30_SECONDS:" << INTERVALS_PER_30_SECONDS << "\n"; + const int SAMPLES_FOR_30_SECONDS = 30 * SAMPLES_FOR_SECOND; + std::cout << "SAMPLES_FOR_30_SECONDS:" << SAMPLES_FOR_30_SECONDS << "\n"; + const int REPORTS_FOR_30_SECONDS = 30 * MSECS_PER_SECOND / report; + std::cout << "REPORTS_FOR_30_SECONDS:" << REPORTS_FOR_30_SECONDS << "\n"; + + int intervalsPerReport = report / MSEC_TO_USEC; + if (intervalsPerReport < 1) { + intervalsPerReport = 1; + } + std::cout << "intervalsPerReport:" << intervalsPerReport << "\n"; + MovingMinMaxAvg timeGaps(SAMPLES_FOR_SECOND, INTERVALS_PER_30_SECONDS); + MovingMinMaxAvg timeGapsPerReport(SAMPLES_FOR_SECOND, intervalsPerReport); + + char* outputBuffer = new char[size]; + memset(outputBuffer, 0, size); + + quint16 outgoingSequenceNumber = 0; + + + StDev stDevReportInterval; + StDev stDev30s; + StDev stDev; + + SimpleMovingAverage averageNetworkTime(SAMPLES_FOR_30_SECONDS); + SimpleMovingAverage averageStatsCalcultionTime(SAMPLES_FOR_30_SECONDS); + float lastStatsCalculationTime = 0.0f; // we add out stats calculation time in the next calculation window + bool hasStatsCalculationTime = false; + + quint64 last = usecTimestampNow(); + quint64 lastReport = 0; + + while (true) { + + quint64 now = usecTimestampNow(); + int actualGap = now - last; + + + if (actualGap >= gap) { + + // pack seq num + memcpy(outputBuffer, &outgoingSequenceNumber, sizeof(quint16)); + + quint64 networkStart = usecTimestampNow(); + int n = sendto(sockfd, outputBuffer, size, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)); + quint64 networkEnd = usecTimestampNow(); + float networkElapsed = (float)(networkEnd - networkStart); + + if (n < 0) { + std::cout << "Send error: " << strerror(errno) << "\n"; + } + outgoingSequenceNumber++; + + quint64 statsCalcultionStart = usecTimestampNow(); + + int gapDifferece = actualGap - gap; + + timeGaps.update(gapDifferece); + timeGapsPerReport.update(gapDifferece); + stDev.addValue(gapDifferece); + stDev30s.addValue(gapDifferece); + stDevReportInterval.addValue(gapDifferece); + last = now; + + // track out network time and stats calculation times + averageNetworkTime.updateAverage(networkElapsed); + + // for our stats calculation time, we actually delay the updating by one sample. + // we do this so that the calculation of the average timing for the stats calculation + // happen inside of the calculation processing. This ensures that tracking stats on + // stats calculation doesn't side effect the remaining running time. + if (hasStatsCalculationTime) { + averageStatsCalcultionTime.updateAverage(lastStatsCalculationTime); + } + + if (now - lastReport >= (report * MSEC_TO_USEC)) { + + std::cout << "\n" + << "SEND gap Difference From Expected\n" + << "Overall:\n" + << "min: " << timeGaps.getMin() << " usecs, " + << "max: " << timeGaps.getMax() << " usecs, " + << "avg: " << timeGaps.getAverage() << " usecs, " + << "stdev: " << stDev.getStDev() << " usecs\n" + << "Last 30s:\n" + << "min: " << timeGaps.getWindowMin() << " usecs, " + << "max: " << timeGaps.getWindowMax() << " usecs, " + << "avg: " << timeGaps.getWindowAverage() << " usecs, " + << "stdev: " << stDev30s.getStDev() << " usecs\n" + << "Last report interval:\n" + << "min: " << timeGapsPerReport.getWindowMin() << " usecs, " + << "max: " << timeGapsPerReport.getWindowMax() << " usecs, " + << "avg: " << timeGapsPerReport.getWindowAverage() << " usecs, " + << "stdev: " << stDevReportInterval.getStDev() << " usecs\n" + << "Average Execution Times Last 30s:\n" + << " network: " << averageNetworkTime.getAverage() << " usecs average\n" + << " stats: " << averageStatsCalcultionTime.getAverage() << " usecs average" + << "\n"; + + stDevReportInterval.reset(); + if (stDev30s.getSamples() > SAMPLES_FOR_30_SECONDS) { + stDev30s.reset(); + } + + lastReport = now; + } + + quint64 statsCalcultionEnd = usecTimestampNow(); + lastStatsCalculationTime = (float)(statsCalcultionEnd - statsCalcultionStart); + if (lastStatsCalculationTime > LARGE_STATS_TIME) { + qDebug() << "WARNING -- unexpectedly large lastStatsCalculationTime=" << lastStatsCalculationTime; + } + hasStatsCalculationTime = true; + + } + } + delete[] outputBuffer; + +#ifdef _WIN32 + WSACleanup(); +#endif +} + +void runReceive(const char* addressOption, int port, int gap, int size, int report) { + std::cout << "runReceive...\n"; + +#ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { + printf("WSAStartup failed with error %d\n", WSAGetLastError()); + return; + } +#endif + + int sockfd, n; + struct sockaddr_in myaddr; + + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + + memset(&myaddr, 0, sizeof(myaddr)); + myaddr.sin_family = AF_INET; + myaddr.sin_addr.s_addr = htonl(INADDR_ANY); + myaddr.sin_port = htons(port); + + + const int SAMPLES_FOR_SECOND = 1000000 / gap; + std::cout << "SAMPLES_FOR_SECOND:" << SAMPLES_FOR_SECOND << "\n"; + const int INTERVALS_PER_30_SECONDS = 30; + std::cout << "INTERVALS_PER_30_SECONDS:" << INTERVALS_PER_30_SECONDS << "\n"; + const int SAMPLES_FOR_30_SECONDS = 30 * SAMPLES_FOR_SECOND; + std::cout << "SAMPLES_FOR_30_SECONDS:" << SAMPLES_FOR_30_SECONDS << "\n"; + const int REPORTS_FOR_30_SECONDS = 30 * MSECS_PER_SECOND / report; + std::cout << "REPORTS_FOR_30_SECONDS:" << REPORTS_FOR_30_SECONDS << "\n"; + + int intervalsPerReport = report / MSEC_TO_USEC; + if (intervalsPerReport < 1) { + intervalsPerReport = 1; + } + std::cout << "intervalsPerReport:" << intervalsPerReport << "\n"; + MovingMinMaxAvg timeGaps(SAMPLES_FOR_SECOND, INTERVALS_PER_30_SECONDS); + MovingMinMaxAvg timeGapsPerReport(SAMPLES_FOR_SECOND, intervalsPerReport); + + char* inputBuffer = new char[size]; + memset(inputBuffer, 0, size); + + + SequenceNumberStats seqStats(REPORTS_FOR_30_SECONDS); + + StDev stDevReportInterval; + StDev stDev30s; + StDev stDev; + + SimpleMovingAverage averageNetworkTime(SAMPLES_FOR_30_SECONDS); + SimpleMovingAverage averageStatsCalcultionTime(SAMPLES_FOR_30_SECONDS); + float lastStatsCalculationTime = 0.0f; // we add out stats calculation time in the next calculation window + bool hasStatsCalculationTime = false; + + if (bind(sockfd, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { + std::cout << "bind failed\n"; + return; + } + + quint64 last = 0; // first case + quint64 lastReport = 0; + + while (true) { + + quint64 networkStart = usecTimestampNow(); + n = recvfrom(sockfd, inputBuffer, size, 0, NULL, NULL); // we don't care about where it came from + + quint64 networkEnd = usecTimestampNow(); + float networkElapsed = (float)(networkEnd - networkStart); + + if (n < 0) { + std::cout << "Receive error: " << strerror(errno) << "\n"; + } + + // parse seq num + quint16 incomingSequenceNumber = *(reinterpret_cast(inputBuffer)); + seqStats.sequenceNumberReceived(incomingSequenceNumber); + + if (last == 0) { + last = usecTimestampNow(); + std::cout << "first packet received\n"; + } else { + + quint64 statsCalcultionStart = usecTimestampNow(); + quint64 now = usecTimestampNow(); + int actualGap = now - last; + + int gapDifferece = actualGap - gap; + timeGaps.update(gapDifferece); + timeGapsPerReport.update(gapDifferece); + stDev.addValue(gapDifferece); + stDev30s.addValue(gapDifferece); + stDevReportInterval.addValue(gapDifferece); + last = now; + + // track out network time and stats calculation times + averageNetworkTime.updateAverage(networkElapsed); + + // for our stats calculation time, we actually delay the updating by one sample. + // we do this so that the calculation of the average timing for the stats calculation + // happen inside of the calculation processing. This ensures that tracking stats on + // stats calculation doesn't side effect the remaining running time. + if (hasStatsCalculationTime) { + averageStatsCalcultionTime.updateAverage(lastStatsCalculationTime); + } + + if (now - lastReport >= (report * MSEC_TO_USEC)) { + + seqStats.pushStatsToHistory(); + + std::cout << "RECEIVE gap Difference From Expected\n" + << "Overall:\n" + << "min: " << timeGaps.getMin() << " usecs, " + << "max: " << timeGaps.getMax() << " usecs, " + << "avg: " << timeGaps.getAverage() << " usecs, " + << "stdev: " << stDev.getStDev() << " usecs\n" + << "Last 30s:\n" + << "min: " << timeGaps.getWindowMin() << " usecs, " + << "max: " << timeGaps.getWindowMax() << " usecs, " + << "avg: " << timeGaps.getWindowAverage() << " usecs, " + << "stdev: " << stDev30s.getStDev() << " usecs\n" + << "Last report interval:\n" + << "min: " << timeGapsPerReport.getWindowMin() << " usecs, " + << "max: " << timeGapsPerReport.getWindowMax() << " usecs, " + << "avg: " << timeGapsPerReport.getWindowAverage() << " usecs, " + << "stdev: " << stDevReportInterval.getStDev() << " usecs\n" + << "Average Execution Times Last 30s:\n" + << " network: " << averageNetworkTime.getAverage() << " usecs average\n" + << " stats: " << averageStatsCalcultionTime.getAverage() << " usecs average" + << "\n"; + stDevReportInterval.reset(); + + if (stDev30s.getSamples() > SAMPLES_FOR_30_SECONDS) { + stDev30s.reset(); + } + + PacketStreamStats packetStatsLast30s = seqStats.getStatsForHistoryWindow(); + PacketStreamStats packetStatsLastReportInterval = seqStats.getStatsForLastHistoryInterval(); + + std::cout << "RECEIVE Packet Stats\n" + << "Overall:\n" + << "lost: " << seqStats.getLost() << ", " + << "lost %: " << seqStats.getStats().getLostRate() * 100.0f << "%\n" + << "Last 30s:\n" + << "lost: " << packetStatsLast30s._lost << ", " + << "lost %: " << packetStatsLast30s.getLostRate() * 100.0f << "%\n" + << "Last report interval:\n" + << "lost: " << packetStatsLastReportInterval._lost << ", " + << "lost %: " << packetStatsLastReportInterval.getLostRate() * 100.0f << "%\n" + << "\n\n"; + + lastReport = now; + } + + quint64 statsCalcultionEnd = usecTimestampNow(); + + lastStatsCalculationTime = (float)(statsCalcultionEnd - statsCalcultionStart); + if (lastStatsCalculationTime > LARGE_STATS_TIME) { + qDebug() << "WARNING -- unexpectedly large lastStatsCalculationTime=" << lastStatsCalculationTime; + } + hasStatsCalculationTime = true; + } + } + delete[] inputBuffer; + +#ifdef _WIN32 + WSACleanup(); +#endif +} + +#endif // #ifdef RUN_MANUALLY diff --git a/tests/render-utils/src/main.cpp b/tests/render-utils/src/main.cpp new file mode 100644 index 0000000000..87338e414b --- /dev/null +++ b/tests/render-utils/src/main.cpp @@ -0,0 +1,270 @@ +// +// main.cpp +// tests/render-utils/src +// +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "TextRenderer.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +class RateCounter { + std::vector times; + QElapsedTimer timer; +public: + RateCounter() { + timer.start(); + } + + void reset() { + times.clear(); + } + + unsigned int count() const { + return times.size() - 1; + } + + float elapsed() const { + if (times.size() < 1) { + return 0.0f; + } + float elapsed = *times.rbegin() - *times.begin(); + return elapsed; + } + + void increment() { + times.push_back(timer.elapsed() / 1000.0f); + } + + float rate() const { + if (elapsed() == 0.0f) { + return NAN; + } + return (float) count() / elapsed(); + } +}; + + +const QString& getQmlDir() { + static QString dir; + if (dir.isEmpty()) { + QDir path(__FILE__); + path.cdUp(); + dir = path.cleanPath(path.absoluteFilePath("../../../interface/resources/qml/")) + "/"; + qDebug() << "Qml Path: " << dir; + } + return dir; +} + +// Create a simple OpenGL window that renders text in various ways +class QTestWindow : public QWindow { + Q_OBJECT + + QOpenGLContext* _context{ nullptr }; + QSize _size; + TextRenderer* _textRenderer[4]; + RateCounter fps; + +protected: + void renderText(); + +private: + void resizeWindow(const QSize& size) { + _size = size; + } + +public: + QTestWindow() { + setSurfaceType(QSurface::OpenGLSurface); + + QSurfaceFormat format; + // Qt Quick may need a depth and stencil buffer. Always make sure these are available. + format.setDepthBufferSize(16); + format.setStencilBufferSize(8); + format.setVersion(4, 5); + format.setProfile(QSurfaceFormat::OpenGLContextProfile::CompatibilityProfile); + format.setOption(QSurfaceFormat::DebugContext); + + setFormat(format); + + _context = new QOpenGLContext; + _context->setFormat(format); + _context->create(); + + show(); + makeCurrent(); + + { + QOpenGLDebugLogger* logger = new QOpenGLDebugLogger(this); + logger->initialize(); // initializes in the current context, i.e. ctx + logger->enableMessages(); + connect(logger, &QOpenGLDebugLogger::messageLogged, this, [&](const QOpenGLDebugMessage & debugMessage) { + qDebug() << debugMessage; + }); + // logger->startLogging(QOpenGLDebugLogger::SynchronousLogging); + } + qDebug() << (const char*)glGetString(GL_VERSION); + +#ifdef WIN32 + glewExperimental = true; + GLenum err = glewInit(); + if (GLEW_OK != err) { + /* Problem: glewInit failed, something is seriously wrong. */ + const GLubyte * errStr = glewGetErrorString(err); + qDebug("Error: %s\n", errStr); + } + qDebug("Status: Using GLEW %s\n", glewGetString(GLEW_VERSION)); + + if (wglewGetExtension("WGL_EXT_swap_control")) { + int swapInterval = wglGetSwapIntervalEXT(); + qDebug("V-Sync is %s\n", (swapInterval > 0 ? "ON" : "OFF")); + } + glGetError(); +#endif + + _textRenderer[0] = TextRenderer::getInstance(SANS_FONT_FAMILY, 12, false); + _textRenderer[1] = TextRenderer::getInstance(SERIF_FONT_FAMILY, 12, false, + TextRenderer::SHADOW_EFFECT); + _textRenderer[2] = TextRenderer::getInstance(MONO_FONT_FAMILY, 48, -1, + false, TextRenderer::OUTLINE_EFFECT); + _textRenderer[3] = TextRenderer::getInstance(INCONSOLATA_FONT_FAMILY, 24); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glClearColor(0.2f, 0.2f, 0.2f, 1); + glDisable(GL_DEPTH_TEST); + + makeCurrent(); + + setFramePosition(QPoint(-1000, 0)); + resize(QSize(800, 600)); + } + + virtual ~QTestWindow() { + } + + void draw(); + void makeCurrent() { + _context->makeCurrent(this); + } + +protected: + + void resizeEvent(QResizeEvent* ev) override { + resizeWindow(ev->size()); + } +}; + +#ifndef SERIF_FONT_FAMILY +#define SERIF_FONT_FAMILY "Times New Roman" +#endif + +static const wchar_t* EXAMPLE_TEXT = L"Hello"; +//static const wchar_t* EXAMPLE_TEXT = L"\xC1y Hello 1.0\ny\xC1 line 2\n\xC1y"; +static const glm::uvec2 QUAD_OFFSET(10, 10); + +static const glm::vec3 COLORS[4] = { { 1.0, 1.0, 1.0 }, { 0.5, 1.0, 0.5 }, { + 1.0, 0.5, 0.5 }, { 0.5, 0.5, 1.0 } }; + +void QTestWindow::renderText() { + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, _size.width(), _size.height(), 0, 1, -1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + const glm::uvec2 size = glm::uvec2(_size.width() / 2, _size.height() / 2); + + const glm::uvec2 offsets[4] = { + { QUAD_OFFSET.x, QUAD_OFFSET.y }, + { size.x + QUAD_OFFSET.x, QUAD_OFFSET.y }, + { size.x + QUAD_OFFSET.x, size.y + QUAD_OFFSET.y }, + { QUAD_OFFSET.x, size.y + QUAD_OFFSET.y }, + }; + + QString str = QString::fromWCharArray(EXAMPLE_TEXT); + for (int i = 0; i < 4; ++i) { + glm::vec2 bounds = _textRenderer[i]->computeExtent(str); + glPushMatrix(); + { + glTranslatef(offsets[i].x, offsets[i].y, 0); + glColor3f(0, 0, 0); + glBegin(GL_QUADS); + { + glVertex2f(0, 0); + glVertex2f(0, bounds.y); + glVertex2f(bounds.x, bounds.y); + glVertex2f(bounds.x, 0); + } + glEnd(); + } + glPopMatrix(); + const int testCount = 100; + for (int j = 0; j < testCount; ++j) { + // Draw backgrounds around where the text will appear + // Draw the text itself + _textRenderer[i]->draw(offsets[i].x, offsets[i].y, str.toLocal8Bit().constData(), + glm::vec4(COLORS[i], 1.0f)); + } + } +} + +void QTestWindow::draw() { + if (!isVisible()) { + return; + } + + makeCurrent(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glViewport(0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio()); + + renderText(); + + _context->swapBuffers(this); + glFinish(); + + fps.increment(); + if (fps.elapsed() >= 2.0f) { + qDebug() << "FPS: " << fps.rate(); + fps.reset(); + } +} + +int main(int argc, char** argv) { + QGuiApplication app(argc, argv); + QTestWindow window; + QTimer timer; + timer.setInterval(1); + app.connect(&timer, &QTimer::timeout, &app, [&] { + window.draw(); + }); + timer.start(); + app.exec(); + return 0; +} + +#include "main.moc" diff --git a/tests/ui/src/main.cpp b/tests/ui/src/main.cpp new file mode 100644 index 0000000000..19070e9699 --- /dev/null +++ b/tests/ui/src/main.cpp @@ -0,0 +1,510 @@ +// +// main.cpp +// tests/render-utils/src +// +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "OffscreenUi.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "MessageDialog.h" +#include "VrMenu.h" +#include "InfoView.h" +#include + +class RateCounter { + std::vector times; + QElapsedTimer timer; +public: + RateCounter() { + timer.start(); + } + + void reset() { + times.clear(); + } + + unsigned int count() const { + return times.size() - 1; + } + + float elapsed() const { + if (times.size() < 1) { + return 0.0f; + } + float elapsed = *times.rbegin() - *times.begin(); + return elapsed; + } + + void increment() { + times.push_back(timer.elapsed() / 1000.0f); + } + + float rate() const { + if (elapsed() == 0.0f) { + return NAN; + } + return (float) count() / elapsed(); + } +}; + + +class MenuConstants : public QObject{ + Q_OBJECT + Q_ENUMS(Item) + +public: + enum Item { + AboutApp, + AddRemoveFriends, + AddressBar, + AlignForearmsWithWrists, + AlternateIK, + AmbientOcclusion, + Animations, + Atmosphere, + Attachments, + AudioNoiseReduction, + AudioScope, + AudioScopeFiftyFrames, + AudioScopeFiveFrames, + AudioScopeFrames, + AudioScopePause, + AudioScopeTwentyFrames, + AudioStats, + AudioStatsShowInjectedStreams, + BandwidthDetails, + BlueSpeechSphere, + BookmarkLocation, + Bookmarks, + CascadedShadows, + CachesSize, + Chat, + Collisions, + Console, + ControlWithSpeech, + CopyAddress, + CopyPath, + DecreaseAvatarSize, + DeleteBookmark, + DisableActivityLogger, + DisableLightEntities, + DisableNackPackets, + DiskCacheEditor, + DisplayHands, + DisplayHandTargets, + DisplayModelBounds, + DisplayModelTriangles, + DisplayModelElementChildProxies, + DisplayModelElementProxy, + DisplayDebugTimingDetails, + DontDoPrecisionPicking, + DontFadeOnOctreeServerChanges, + DontRenderEntitiesAsScene, + EchoLocalAudio, + EchoServerAudio, + EditEntitiesHelp, + Enable3DTVMode, + EnableCharacterController, + EnableGlowEffect, + EnableVRMode, + ExpandMyAvatarSimulateTiming, + ExpandMyAvatarTiming, + ExpandOtherAvatarTiming, + ExpandPaintGLTiming, + ExpandUpdateTiming, + Faceshift, + FilterSixense, + FirstPerson, + FrameTimer, + Fullscreen, + FullscreenMirror, + GlowWhenSpeaking, + NamesAboveHeads, + GoToUser, + HMDTools, + IncreaseAvatarSize, + KeyboardMotorControl, + LeapMotionOnHMD, + LoadScript, + LoadScriptURL, + LoadRSSDKFile, + LodTools, + Login, + Log, + LowVelocityFilter, + Mirror, + MuteAudio, + MuteEnvironment, + MuteFaceTracking, + NoFaceTracking, + NoShadows, + OctreeStats, + OffAxisProjection, + OnlyDisplayTopTen, + PackageModel, + Pair, + PipelineWarnings, + Preferences, + Quit, + ReloadAllScripts, + RenderBoundingCollisionShapes, + RenderFocusIndicator, + RenderHeadCollisionShapes, + RenderLookAtVectors, + RenderSkeletonCollisionShapes, + RenderTargetFramerate, + RenderTargetFramerateUnlimited, + RenderTargetFramerate60, + RenderTargetFramerate50, + RenderTargetFramerate40, + RenderTargetFramerate30, + RenderTargetFramerateVSyncOn, + RenderResolution, + RenderResolutionOne, + RenderResolutionTwoThird, + RenderResolutionHalf, + RenderResolutionThird, + RenderResolutionQuarter, + RenderAmbientLight, + RenderAmbientLightGlobal, + RenderAmbientLight0, + RenderAmbientLight1, + RenderAmbientLight2, + RenderAmbientLight3, + RenderAmbientLight4, + RenderAmbientLight5, + RenderAmbientLight6, + RenderAmbientLight7, + RenderAmbientLight8, + RenderAmbientLight9, + ResetAvatarSize, + ResetSensors, + RunningScripts, + RunTimingTests, + ScriptEditor, + ScriptedMotorControl, + ShowBordersEntityNodes, + ShowIKConstraints, + SimpleShadows, + SixenseEnabled, + SixenseMouseInput, + SixenseLasers, + ShiftHipsForIdleAnimations, + Stars, + Stats, + StereoAudio, + StopAllScripts, + SuppressShortTimings, + TestPing, + ToolWindow, + TransmitterDrive, + TurnWithHead, + UseAudioForMouth, + UseCamera, + VelocityFilter, + VisibleToEveryone, + VisibleToFriends, + VisibleToNoOne, + Wireframe, + }; + +public: + MenuConstants(QObject* parent = nullptr) : QObject(parent) { + + } +}; + +const QString& getResourcesDir() { + static QString dir; + if (dir.isEmpty()) { + QDir path(__FILE__); + path.cdUp(); + dir = path.cleanPath(path.absoluteFilePath("../../../interface/resources/")) + "/"; + qDebug() << "Resources Path: " << dir; + } + return dir; +} + +const QString& getQmlDir() { + static QString dir; + if (dir.isEmpty()) { + dir = getResourcesDir() + "qml/"; + qDebug() << "Qml Path: " << dir; + } + return dir; +} + +const QString& getTestQmlDir() { + static QString dir; + if (dir.isEmpty()) { + QDir path(__FILE__); + path.cdUp(); + dir = path.cleanPath(path.absoluteFilePath("../")) + "/"; + qDebug() << "Qml Test Path: " << dir; + } + return dir; +} + +// Create a simple OpenGL window that renders text in various ways +class QTestWindow : public QWindow, private QOpenGLFunctions { + Q_OBJECT + + QOpenGLContext* _context{ nullptr }; + QSize _size; + bool _altPressed{ false }; + RateCounter fps; + QTimer _timer; + int testQmlTexture{ 0 }; + +public: + QObject* rootMenu; + + QTestWindow() { + _timer.setInterval(1); + connect(&_timer, &QTimer::timeout, [=] { + draw(); + }); + + DependencyManager::set(); + setSurfaceType(QSurface::OpenGLSurface); + + QSurfaceFormat format; + format.setDepthBufferSize(16); + format.setStencilBufferSize(8); + format.setVersion(4, 1); + format.setProfile(QSurfaceFormat::OpenGLContextProfile::CompatibilityProfile); + format.setOption(QSurfaceFormat::DebugContext); + + setFormat(format); + + _context = new QOpenGLContext; + _context->setFormat(format); + if (!_context->create()) { + qFatal("Could not create OpenGL context"); + } + + show(); + makeCurrent(); + initializeOpenGLFunctions(); + + { + QOpenGLDebugLogger* logger = new QOpenGLDebugLogger(this); + logger->initialize(); // initializes in the current context, i.e. ctx + logger->enableMessages(); + connect(logger, &QOpenGLDebugLogger::messageLogged, this, [&](const QOpenGLDebugMessage & debugMessage) { + qDebug() << debugMessage; + }); + // logger->startLogging(QOpenGLDebugLogger::SynchronousLogging); + } + + qDebug() << (const char*)this->glGetString(GL_VERSION); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glClearColor(0.2f, 0.2f, 0.2f, 1); + glDisable(GL_DEPTH_TEST); + + MessageDialog::registerType(); + VrMenu::registerType(); + InfoView::registerType(); + qmlRegisterType("Hifi", 1, 0, "MenuConstants"); + + + auto offscreenUi = DependencyManager::get(); + offscreenUi->create(_context); + connect(offscreenUi.data(), &OffscreenUi::textureUpdated, this, [this, offscreenUi](int textureId) { + offscreenUi->lockTexture(textureId); + assert(!glGetError()); + GLuint oldTexture = testQmlTexture; + testQmlTexture = textureId; + if (oldTexture) { + offscreenUi->releaseTexture(oldTexture); + } + }); + + makeCurrent(); + + offscreenUi->setProxyWindow(this); + QDesktopWidget* desktop = QApplication::desktop(); + QRect rect = desktop->availableGeometry(desktop->screenCount() - 1); + int height = rect.height(); + //rect.setHeight(height / 2); + rect.setY(rect.y() + height / 2); + setGeometry(rect); +// setFramePosition(QPoint(-1000, 0)); +// resize(QSize(800, 600)); + +#ifdef QML_CONTROL_GALLERY + offscreenUi->setBaseUrl(QUrl::fromLocalFile(getTestQmlDir())); + offscreenUi->load(QUrl("main.qml")); +#else + offscreenUi->setBaseUrl(QUrl::fromLocalFile(getQmlDir())); + offscreenUi->load(QUrl("TestRoot.qml")); + offscreenUi->load(QUrl("TestMenu.qml")); + // Requires a root menu to have been loaded before it can load + VrMenu::load(); +#endif + installEventFilter(offscreenUi.data()); + offscreenUi->resume(); + _timer.start(); + } + + virtual ~QTestWindow() { + DependencyManager::destroy(); + } + +private: + void draw() { + if (!isVisible()) { + return; + } + + makeCurrent(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glViewport(0, 0, _size.width() * devicePixelRatio(), _size.height() * devicePixelRatio()); + + renderQml(); + + _context->swapBuffers(this); + glFinish(); + + fps.increment(); + if (fps.elapsed() >= 2.0f) { + qDebug() << "FPS: " << fps.rate(); + fps.reset(); + } + } + + void makeCurrent() { + _context->makeCurrent(this); + } + + void renderQml(); + + void resizeWindow(const QSize & size) { + _size = size; + DependencyManager::get()->resize(_size); + } + + +protected: + void resizeEvent(QResizeEvent* ev) override { + resizeWindow(ev->size()); + } + + + void keyPressEvent(QKeyEvent* event) { + _altPressed = Qt::Key_Alt == event->key(); + switch (event->key()) { + case Qt::Key_B: + if (event->modifiers() & Qt::CTRL) { + auto offscreenUi = DependencyManager::get(); + offscreenUi->load("Browser.qml"); + } + break; + case Qt::Key_L: + if (event->modifiers() & Qt::CTRL) { + InfoView::show(getResourcesDir() + "html/interface-welcome.html", true); + } + break; + case Qt::Key_K: + if (event->modifiers() & Qt::CTRL) { + OffscreenUi::question("Message title", "Message contents", [](QMessageBox::Button b){ + qDebug() << b; + }); + } + break; + case Qt::Key_J: + if (event->modifiers() & Qt::CTRL) { + auto offscreenUi = DependencyManager::get(); + rootMenu = offscreenUi->getRootItem()->findChild("rootMenu"); + QMetaObject::invokeMethod(rootMenu, "popup"); + } + break; + } + QWindow::keyPressEvent(event); + } + QQmlContext* menuContext{ nullptr }; + void keyReleaseEvent(QKeyEvent *event) { + if (_altPressed && Qt::Key_Alt == event->key()) { + VrMenu::toggle(); + } + } + + void moveEvent(QMoveEvent* event) { + static qreal oldPixelRatio = 0.0; + if (devicePixelRatio() != oldPixelRatio) { + oldPixelRatio = devicePixelRatio(); + resizeWindow(size()); + } + QWindow::moveEvent(event); + } +}; + +void QTestWindow::renderQml() { + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + if (testQmlTexture > 0) { + glEnable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, testQmlTexture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + } + glBegin(GL_QUADS); + { + glTexCoord2f(0, 0); + glVertex2f(-1, -1); + glTexCoord2f(0, 1); + glVertex2f(-1, 1); + glTexCoord2f(1, 1); + glVertex2f(1, 1); + glTexCoord2f(1, 0); + glVertex2f(1, -1); + } + glEnd(); +} + + +const char * LOG_FILTER_RULES = R"V0G0N( +hifi.offscreen.focus.debug=false +qt.quick.mouse.debug=false +)V0G0N"; + +int main(int argc, char** argv) { + QApplication app(argc, argv); + QLoggingCategory::setFilterRules(LOG_FILTER_RULES); + QTestWindow window; + app.exec(); + return 0; +} + +#include "main.moc"