Avoid direct GL calls in Qt Quick examples and tests

Change-Id: I204a5513708aeff5cae00d06d4f0c27c20a13ace
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@jollamobile.com>
This commit is contained in:
Laszlo Agocs 2014-06-26 15:08:47 +02:00
parent 8adc7bad22
commit 08f9b552c3
8 changed files with 30 additions and 16 deletions

View File

@ -91,6 +91,8 @@ void Squircle::handleWindowChanged(QQuickWindow *win)
void Squircle::paint()
{
if (!m_program) {
initializeOpenGLFunctions();
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute highp vec4 vertices;"

View File

@ -44,9 +44,10 @@
#include <QtQuick/QQuickItem>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QOpenGLFunctions>
//! [1]
class Squircle : public QQuickItem
class Squircle : public QQuickItem, protected QOpenGLFunctions
{
Q_OBJECT

View File

@ -66,6 +66,8 @@ void LogoRenderer::paintQtLogo()
void LogoRenderer::initialize()
{
initializeOpenGLFunctions();
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
QOpenGLShader *vshader1 = new QOpenGLShader(QOpenGLShader::Vertex, &program1);

View File

@ -44,12 +44,13 @@
#include <QtGui/qvector3d.h>
#include <QtGui/qmatrix4x4.h>
#include <QtGui/qopenglshaderprogram.h>
#include <QtGui/qopenglfunctions.h>
#include <QTime>
#include <QVector>
class LogoRenderer {
class LogoRenderer : protected QOpenGLFunctions
{
public:
LogoRenderer();
~LogoRenderer();

View File

@ -93,14 +93,14 @@ public slots:
}
m_renderFbo->bind();
glViewport(0, 0, m_size.width(), m_size.height());
context->functions()->glViewport(0, 0, m_size.width(), m_size.height());
m_logoRenderer->render();
// We need to flush the contents to the FBO before posting
// the texture to the other thread, otherwise, we might
// get unexpected results.
glFlush();
context->functions()->glFlush();
m_renderFbo->bindDefault();
qSwap(m_renderFbo, m_displayFbo);

View File

@ -45,6 +45,7 @@
#include <QtQuick/qquickview.h>
#include <QtGui/qopenglcontext.h>
#include <QtGui/qopenglframebufferobject.h>
#include <QtGui/qopenglfunctions.h>
#include <QtQuick/QQuickFramebufferObject>
@ -62,7 +63,7 @@ struct FrameInfo {
QSize fboSize;
} frameInfo;
class ColorRenderer : public QQuickFramebufferObject::Renderer
class ColorRenderer : public QQuickFramebufferObject::Renderer, protected QOpenGLFunctions
{
public:
void render();
@ -142,6 +143,7 @@ void ColorRenderer::synchronize(QQuickFramebufferObject *item)
QOpenGLFramebufferObject *ColorRenderer::createFramebufferObject(const QSize &size)
{
initializeOpenGLFunctions();
frameInfo.createFBOCount++;
QOpenGLFramebufferObjectFormat format;
if (msaa)

View File

@ -44,6 +44,7 @@
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include <QtGui/qopenglcontext.h>
#include <QtGui/qopenglfunctions.h>
#include "../../shared/util.h"
@ -107,13 +108,13 @@ tst_QQuickItemLayer::tst_QQuickItemLayer()
window.create();
context.create();
context.makeCurrent(&window);
const char *vendor = (const char *)glGetString(GL_VENDOR);
const char *renderer = (const char *)glGetString(GL_RENDERER);
const char *vendor = (const char *)context.functions()->glGetString(GL_VENDOR);
const char *renderer = (const char *)context.functions()->glGetString(GL_RENDERER);
m_isMesaSoftwareRasterizer = strcmp(vendor, "Mesa Project") == 0
&& strcmp(renderer, "Software Rasterizer") == 0;
if (m_isMesaSoftwareRasterizer) {
// Expects format: <OpenGL version> Mesa <Mesa version>[-devel] [...]
const char *version = (const char *)glGetString(GL_VERSION);
const char *version = (const char *)context.functions()->glGetString(GL_VERSION);
QList<QByteArray> list = QByteArray(version).split(' ');
if (list.size() >= 3) {
list = list.at(2).split('-').at(0).split('.');

View File

@ -44,6 +44,7 @@
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include <QtGui/qopenglcontext.h>
#include <QtGui/qopenglfunctions.h>
#include <QtGui/qscreen.h>
#include <private/qsgrendernode_p.h>
@ -86,8 +87,8 @@ public:
virtual void render(const RenderState &)
{
// If clip has been set, scissoring will make sure the right area is cleared.
glClearColor(color.redF(), color.greenF(), color.blueF(), 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
QOpenGLContext::currentContext()->functions()->glClearColor(color.redF(), color.greenF(), color.blueF(), 1.0f);
QOpenGLContext::currentContext()->functions()->glClear(GL_COLOR_BUFFER_BIT);
}
QColor color;
@ -129,9 +130,11 @@ private:
QColor m_color;
};
class MessUpNode : public QSGRenderNode
class MessUpNode : public QSGRenderNode, protected QOpenGLFunctions
{
public:
MessUpNode() : initialized(false) { }
virtual StateFlags changedStates()
{
return StateFlags(DepthState) | StencilState | ScissorState | ColorState | BlendState
@ -140,17 +143,17 @@ public:
virtual void render(const RenderState &)
{
if (!initialized) {
initializeOpenGLFunctions();
initialized = true;
}
// Don't draw anything, just mess up the state
glViewport(10, 10, 10, 10);
glDisable(GL_SCISSOR_TEST);
glDepthMask(true);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_EQUAL);
#if defined(QT_OPENGL_ES)
glClearDepthf(1);
#else
glClearDepth(1);
#endif
glClearStencil(42);
glClearColor(1.0f, 0.5f, 1.0f, 0.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
@ -163,6 +166,8 @@ public:
glFrontFace(frontFace == GL_CW ? GL_CCW : GL_CW);
glEnable(GL_CULL_FACE);
}
bool initialized;
};
class MessUpItem : public QQuickItem