qt5base-lts/examples/opengl/threadedqopenglwidget/glwidget.cpp
Lucie Gérard 05fc3aef53 Use SPDX license identifiers
Replace the current license disclaimer in files by
a SPDX-License-Identifier.
Files that have to be modified by hand are modified.
License files are organized under LICENSES directory.

Task-number: QTBUG-67283
Change-Id: Id880c92784c40f3bbde861c0d93f58151c18b9f1
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Jörg Bornemann <joerg.bornemann@qt.io>
2022-05-16 16:37:38 +02:00

72 lines
1.8 KiB
C++

// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "glwidget.h"
#include "renderer.h"
#include <qmath.h>
#include <QGuiApplication>
GLWidget::GLWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
setMinimumSize(300, 250);
connect(this, &QOpenGLWidget::aboutToCompose, this, &GLWidget::onAboutToCompose);
connect(this, &QOpenGLWidget::frameSwapped, this, &GLWidget::onFrameSwapped);
connect(this, &QOpenGLWidget::aboutToResize, this, &GLWidget::onAboutToResize);
connect(this, &QOpenGLWidget::resized, this, &GLWidget::onResized);
m_thread = new QThread;
m_renderer = new Renderer(this);
m_renderer->moveToThread(m_thread);
connect(m_thread, &QThread::finished, m_renderer, &QObject::deleteLater);
connect(this, &GLWidget::renderRequested, m_renderer, &Renderer::render);
connect(m_renderer, &Renderer::contextWanted, this, &GLWidget::grabContext);
m_thread->start();
}
GLWidget::~GLWidget()
{
m_renderer->prepareExit();
m_thread->quit();
m_thread->wait();
delete m_thread;
}
void GLWidget::onAboutToCompose()
{
// We are on the gui thread here. Composition is about to
// begin. Wait until the render thread finishes.
m_renderer->lockRenderer();
}
void GLWidget::onFrameSwapped()
{
m_renderer->unlockRenderer();
// Assuming a blocking swap, our animation is driven purely by the
// vsync in this example.
emit renderRequested();
}
void GLWidget::onAboutToResize()
{
m_renderer->lockRenderer();
}
void GLWidget::onResized()
{
m_renderer->unlockRenderer();
}
void GLWidget::grabContext()
{
m_renderer->lockRenderer();
QMutexLocker lock(m_renderer->grabMutex());
context()->moveToThread(m_thread);
m_renderer->grabCond()->wakeAll();
m_renderer->unlockRenderer();
}