src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp
branchRCL_3
changeset 4 3b1da2848fc7
parent 3 41300fa6a67c
child 5 d3bac044e0f0
--- a/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp	Tue Feb 02 00:43:10 2010 +0200
+++ b/src/opengl/gl2paintengineex/qpaintengineex_opengl2.cpp	Fri Feb 19 23:40:16 2010 +0200
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
@@ -75,15 +75,14 @@
 #include <QPaintEngine>
 #include <private/qpainter_p.h>
 #include <private/qfontengine_p.h>
-#include <private/qtextureglyphcache_p.h>
 #include <private/qpixmapdata_gl_p.h>
 #include <private/qdatabuffer_p.h>
 
 #include "qglgradientcache_p.h"
 #include "qglengineshadermanager_p.h"
 #include "qgl2pexvertexarray_p.h"
-
 #include "qtriangulatingstroker_p.h"
+#include "qtextureglyphcache_gl_p.h"
 
 #include <QDebug>
 
@@ -91,254 +90,6 @@
 
 //#define QT_GL_NO_SCISSOR_TEST
 
-static const GLuint GL_STENCIL_HIGH_BIT         = 0x80;
-static const GLuint QT_BRUSH_TEXTURE_UNIT       = 0;
-static const GLuint QT_IMAGE_TEXTURE_UNIT       = 0; //Can be the same as brush texture unit
-static const GLuint QT_MASK_TEXTURE_UNIT        = 1;
-static const GLuint QT_BACKGROUND_TEXTURE_UNIT  = 2;
-
-#ifdef Q_WS_WIN
-extern Q_GUI_EXPORT bool qt_cleartype_enabled;
-#endif
-
-class QGLTextureGlyphCache : public QObject, public QTextureGlyphCache
-{
-    Q_OBJECT
-public:
-    QGLTextureGlyphCache(QGLContext *context, QFontEngineGlyphCache::Type type, const QTransform &matrix);
-    ~QGLTextureGlyphCache();
-
-    virtual void createTextureData(int width, int height);
-    virtual void resizeTextureData(int width, int height);
-    virtual void fillTexture(const Coord &c, glyph_t glyph);
-    virtual int glyphMargin() const;
-
-    inline GLuint texture() const { return m_texture; }
-
-    inline int width() const { return m_width; }
-    inline int height() const { return m_height; }
-
-    inline void setPaintEnginePrivate(QGL2PaintEngineExPrivate *p) { pex = p; }
-
-
-public Q_SLOTS:
-    void contextDestroyed(const QGLContext *context) {
-        if (context == ctx) {
-            QList<const QGLContext *> shares = qgl_share_reg()->shares(ctx);
-            if (shares.isEmpty()) {
-                glDeleteFramebuffers(1, &m_fbo);
-                if (m_width || m_height)
-                    glDeleteTextures(1, &m_texture);
-                ctx = 0;
-            } else {
-                // since the context holding the texture is shared, and
-                // about to be destroyed, we have to transfer ownership
-                // of the texture to one of the share contexts
-                ctx = const_cast<QGLContext *>((ctx == shares.at(0)) ? shares.at(1) : shares.at(0));
-            }
-        }
-    }
-
-private:
-    QGLContext *ctx;
-
-    QGL2PaintEngineExPrivate *pex;
-
-    GLuint m_texture;
-    GLuint m_fbo;
-
-    int m_width;
-    int m_height;
-
-    QGLShaderProgram *m_program;
-};
-
-QGLTextureGlyphCache::QGLTextureGlyphCache(QGLContext *context, QFontEngineGlyphCache::Type type, const QTransform &matrix)
-    : QTextureGlyphCache(type, matrix)
-    , ctx(context)
-    , m_width(0)
-    , m_height(0)
-{
-    glGenFramebuffers(1, &m_fbo);
-    connect(QGLSignalProxy::instance(), SIGNAL(aboutToDestroyContext(const QGLContext*)),
-            SLOT(contextDestroyed(const QGLContext*)));
-}
-
-QGLTextureGlyphCache::~QGLTextureGlyphCache()
-{
-    if (ctx) {
-        QGLShareContextScope scope(ctx);
-        glDeleteFramebuffers(1, &m_fbo);
-
-        if (m_width || m_height)
-            glDeleteTextures(1, &m_texture);
-    }
-}
-
-void QGLTextureGlyphCache::createTextureData(int width, int height)
-{
-    glGenTextures(1, &m_texture);
-    glBindTexture(GL_TEXTURE_2D, m_texture);
-
-    m_width = width;
-    m_height = height;
-
-    QVarLengthArray<uchar> data(width * height);
-    for (int i = 0; i < data.size(); ++i)
-        data[i] = 0;
-
-    if (m_type == QFontEngineGlyphCache::Raster_RGBMask)
-        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &data[0]);
-    else
-        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &data[0]);
-
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-}
-
-void QGLTextureGlyphCache::resizeTextureData(int width, int height)
-{
-    // ### the QTextureGlyphCache API needs to be reworked to allow
-    // ### resizeTextureData to fail
-
-    int oldWidth = m_width;
-    int oldHeight = m_height;
-
-    GLuint oldTexture = m_texture;
-    createTextureData(width, height);
-
-    glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
-
-    GLuint tmp_texture;
-    glGenTextures(1, &tmp_texture);
-    glBindTexture(GL_TEXTURE_2D, tmp_texture);
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, oldWidth, oldHeight, 0,
-                 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-    glBindTexture(GL_TEXTURE_2D, 0);
-    glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
-                           GL_TEXTURE_2D, tmp_texture, 0);
-
-    glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT);
-    glBindTexture(GL_TEXTURE_2D, oldTexture);
-
-    pex->transferMode(BrushDrawingMode);
-
-#ifndef QT_OPENGL_ES_2
-    if (pex->inRenderText)
-        glPushAttrib(GL_ENABLE_BIT | GL_VIEWPORT_BIT | GL_SCISSOR_BIT);
-#endif
-
-    glDisable(GL_STENCIL_TEST);
-    glDisable(GL_DEPTH_TEST);
-    glDisable(GL_SCISSOR_TEST);
-    glDisable(GL_BLEND);
-
-    glViewport(0, 0, oldWidth, oldHeight);
-
-    float vertexCoordinateArray[] = { -1, -1, 1, -1, 1, 1, -1, 1 };
-    float textureCoordinateArray[] = { 0, 0, 1, 0, 1, 1, 0, 1 };
-
-    glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-    glEnableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
-
-    glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinateArray);
-    glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinateArray);
-
-    pex->shaderManager->blitProgram()->bind();
-    pex->shaderManager->blitProgram()->setUniformValue("imageTexture", QT_IMAGE_TEXTURE_UNIT);
-    pex->shaderManager->setDirty();
-
-    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-    glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-    glDisableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
-
-    glBindTexture(GL_TEXTURE_2D, m_texture);
-
-#ifdef QT_OPENGL_ES_2
-    QDataBuffer<uchar> buffer(4*oldWidth*oldHeight);
-    buffer.resize(4*oldWidth*oldHeight);
-    glReadPixels(0, 0, oldWidth, oldHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data());
-
-    // do an in-place conversion from GL_RGBA to GL_ALPHA
-    for (int i=0; i<oldWidth*oldHeight; ++i)
-        buffer.data()[i] = buffer.at(4*i + 3);
-
-    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, oldWidth, oldHeight,
-                    GL_ALPHA, GL_UNSIGNED_BYTE, buffer.data());
-#else
-    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, oldWidth, oldHeight);
-#endif
-
-    glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
-                              GL_RENDERBUFFER_EXT, 0);
-    glDeleteTextures(1, &tmp_texture);
-    glDeleteTextures(1, &oldTexture);
-
-    glBindFramebuffer(GL_FRAMEBUFFER_EXT, ctx->d_ptr->current_fbo);
-
-    glViewport(0, 0, pex->width, pex->height);
-    pex->updateClipScissorTest();
-
-#ifndef QT_OPENGL_ES_2
-    if (pex->inRenderText)
-        glPopAttrib();
-#endif
-}
-
-void QGLTextureGlyphCache::fillTexture(const Coord &c, glyph_t glyph)
-{
-    QImage mask = textureMapForGlyph(glyph);
-    const int maskWidth = mask.width();
-    const int maskHeight = mask.height();
-
-    if (mask.format() == QImage::Format_Mono) {
-        mask = mask.convertToFormat(QImage::Format_Indexed8);
-        for (int y = 0; y < maskHeight; ++y) {
-            uchar *src = (uchar *) mask.scanLine(y);
-            for (int x = 0; x < maskWidth; ++x)
-                src[x] = -src[x]; // convert 0 and 1 into 0 and 255
-        }
-     }
-
-
-    glBindTexture(GL_TEXTURE_2D, m_texture);
-    if (mask.format() == QImage::Format_RGB32) {
-        glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, maskWidth, maskHeight, GL_BGRA, GL_UNSIGNED_BYTE, mask.bits());
-    } else {
-#ifdef QT_OPENGL_ES2
-        glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, maskWidth, maskHeight, GL_ALPHA, GL_UNSIGNED_BYTE, mask.bits());
-#else
-        // glTexSubImage2D() might cause some garbage to appear in the texture if the mask width is
-        // not a multiple of four bytes. The bug appeared on a computer with 32-bit Windows Vista
-        // and nVidia GeForce 8500GT. GL_UNPACK_ALIGNMENT is set to four bytes, 'mask' has a
-        // multiple of four bytes per line, and most of the glyph shows up correctly in the
-        // texture, which makes me think that this is a driver bug.
-        // One workaround is to make sure the mask width is a multiple of four bytes, for instance
-        // by converting it to a format with four bytes per pixel. Another is to copy one line at a
-        // time.
-
-        for (int i = 0; i < maskHeight; ++i)
-            glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y + i, maskWidth, 1, GL_ALPHA, GL_UNSIGNED_BYTE, mask.scanLine(i));
-#endif
-    }
-}
-
-int QGLTextureGlyphCache::glyphMargin() const
-{
-#if defined(Q_WS_MAC)
-    return 2;
-#elif defined (Q_WS_X11)
-    return 0;
-#else
-    return m_type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0;
-#endif
-}
-
 extern QImage qt_imageForBrush(int brushStyle, bool invert);
 
 ////////////////////////////////// Private Methods //////////////////////////////////////////
@@ -358,10 +109,10 @@
 void QGL2PaintEngineExPrivate::updateTextureFilter(GLenum target, GLenum wrapMode, bool smoothPixmapTransform, GLuint id)
 {
 //    glActiveTexture(GL_TEXTURE0 + QT_BRUSH_TEXTURE_UNIT); //### Is it always this texture unit?
-    if (id != GLuint(-1) && id == lastTexture)
+    if (id != GLuint(-1) && id == lastTextureUsed)
         return;
 
-    lastTexture = id;
+    lastTextureUsed = id;
 
     if (smoothPixmapTransform) {
         glTexParameterf(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@@ -388,36 +139,39 @@
 
 void QGL2PaintEngineExPrivate::setBrush(const QBrush& brush)
 {
-    Q_ASSERT(brush.style() != Qt::NoBrush);
-
     if (qbrush_fast_equals(currentBrush, brush))
         return;
 
-    currentBrush = brush;
+    const Qt::BrushStyle newStyle = qbrush_style(brush);
+    Q_ASSERT(newStyle != Qt::NoBrush);
 
-    brushTextureDirty = true;
-    brushUniformsDirty = true;
+    currentBrush = brush;
+    brushUniformsDirty = true; // All brushes have at least one uniform
+
+    if (newStyle > Qt::SolidPattern)
+        brushTextureDirty = true;
+
     if (currentBrush.style() == Qt::TexturePattern
         && qHasPixmapTexture(brush) && brush.texture().isQBitmap())
     {
         shaderManager->setSrcPixelType(QGLEngineShaderManager::TextureSrcWithPattern);
     } else {
-        shaderManager->setSrcPixelType(currentBrush.style());
+        shaderManager->setSrcPixelType(newStyle);
     }
-    shaderManager->optimiseForBrushTransform(currentBrush.transform());
+    shaderManager->optimiseForBrushTransform(currentBrush.transform().type());
 }
 
 
 void QGL2PaintEngineExPrivate::useSimpleShader()
 {
-    shaderManager->simpleProgram()->bind();
-    shaderManager->setDirty();
+    shaderManager->useSimpleProgram();
 
     if (matrixDirty)
         updateMatrix();
 
     if (simpleShaderMatrixUniformDirty) {
-        shaderManager->simpleProgram()->setUniformValue("pmvMatrix", pmvMatrix);
+        const GLuint location = shaderManager->simpleProgram()->uniformLocation("pmvMatrix");
+        glUniformMatrix3fv(location, 1, GL_FALSE, (GLfloat*)pmvMatrix);
         simpleShaderMatrixUniformDirty = false;
     }
 }
@@ -583,37 +337,52 @@
 {
 //     qDebug("QGL2PaintEngineExPrivate::updateMatrix()");
 
-    // We set up the 4x4 transformation matrix on the vertex shaders to
-    // be the equivalent of glOrtho(0, w, h, 0, -1, 1) * transform:
-    //
-    // | 2/width     0     0 -1 |   | m11  m21  0   dx |
-    // |   0    -2/height  0  1 |   | m12  m22  0   dy |
-    // |   0         0    -1  0 | * |  0    0   1   0  |
-    // |   0         0     0  1 |   | m13  m23  0  m33 |
-    //
-    // We expand out the multiplication to save the cost of a full 4x4
-    // matrix multiplication as most of the components are trivial.
     const QTransform& transform = q->state()->matrix;
 
-    qreal wfactor = 2.0 / width;
-    qreal hfactor = -2.0 / height;
+    // The projection matrix converts from Qt's coordinate system to GL's coordinate system
+    //    * GL's viewport is 2x2, Qt's is width x height
+    //    * GL has +y -> -y going from bottom -> top, Qt is the other way round
+    //    * GL has [0,0] in the center, Qt has it in the top-left
+    //
+    // This results in the Projection matrix below, which is multiplied by the painter's
+    // transformation matrix, as shown below:
+    //
+    //                Projection Matrix                      Painter Transform
+    // ------------------------------------------------   ------------------------
+    // | 2.0 / width  |      0.0      |     -1.0      |   |  m11  |  m21  |  dx  |
+    // |     0.0      | -2.0 / height |      1.0      | * |  m12  |  m22  |  dy  |
+    // |     0.0      |      0.0      |      1.0      |   |  m13  |  m23  |  m33 |
+    // ------------------------------------------------   ------------------------
+    //
+    // NOTE: The resultant matrix is also transposed, as GL expects column-major matracies
+
+    const GLfloat wfactor = 2.0f / width;
+    const GLfloat hfactor = -2.0f / height;
+    GLfloat dx = transform.dx();
+    GLfloat dy = transform.dy();
 
-    pmvMatrix[0][0] = wfactor * transform.m11() - transform.m13();
-    pmvMatrix[0][1] = hfactor * transform.m12() + transform.m13();
-    pmvMatrix[0][2] = 0.0;
-    pmvMatrix[0][3] = transform.m13();
-    pmvMatrix[1][0] = wfactor * transform.m21() - transform.m23();
-    pmvMatrix[1][1] = hfactor * transform.m22() + transform.m23();
-    pmvMatrix[1][2] = 0.0;
-    pmvMatrix[1][3] = transform.m23();
-    pmvMatrix[2][0] = 0.0;
-    pmvMatrix[2][1] = 0.0;
-    pmvMatrix[2][2] = -1.0;
-    pmvMatrix[2][3] = 0.0;
-    pmvMatrix[3][0] = wfactor * transform.dx() - transform.m33();
-    pmvMatrix[3][1] = hfactor * transform.dy() + transform.m33();
-    pmvMatrix[3][2] = 0.0;
-    pmvMatrix[3][3] = transform.m33();
+    // Non-integer translates can have strange effects for some rendering operations such as
+    // anti-aliased text rendering. In such cases, we snap the translate to the pixel grid.
+    if (snapToPixelGrid && transform.type() == QTransform::TxTranslate) {
+        // 0.50 needs to rounded down to 0.0 for consistency with raster engine:
+        dx = ceilf(dx - 0.5f);
+        dy = ceilf(dy - 0.5f);
+    }
+
+    if (addOffset) {
+        dx += 0.49f;
+        dy += 0.49f;
+    }
+
+    pmvMatrix[0][0] = (wfactor * transform.m11())  - transform.m13();
+    pmvMatrix[1][0] = (wfactor * transform.m21())  - transform.m23();
+    pmvMatrix[2][0] = (wfactor * dx) - transform.m33();
+    pmvMatrix[0][1] = (hfactor * transform.m12())  + transform.m13();
+    pmvMatrix[1][1] = (hfactor * transform.m22())  + transform.m23();
+    pmvMatrix[2][1] = (hfactor * dy) + transform.m33();
+    pmvMatrix[0][2] = transform.m13();
+    pmvMatrix[1][2] = transform.m23();
+    pmvMatrix[2][2] = transform.m33();
 
     // 1/10000 == 0.0001, so we have good enough res to cover curves
     // that span the entire widget...
@@ -700,7 +469,19 @@
 void QGL2PaintEngineExPrivate::drawTexture(const QGLRect& dest, const QGLRect& src, const QSize &textureSize, bool opaque, bool pattern)
 {
     // Setup for texture drawing
+    currentBrush = noBrush;
     shaderManager->setSrcPixelType(pattern ? QGLEngineShaderManager::PatternSrc : QGLEngineShaderManager::ImageSrc);
+
+    if (addOffset) {
+        addOffset = false;
+        matrixDirty = true;
+    }
+
+    if (snapToPixelGrid) {
+        snapToPixelGrid = false;
+        matrixDirty = true;
+    }
+
     if (prepareForDraw(opaque))
         shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT);
 
@@ -729,6 +510,10 @@
     QGLContext *ctx = d->ctx;
     glUseProgram(0);
 
+    // Disable all the vertex attribute arrays:
+    for (int i = 0; i < QT_GL_VERTEX_ARRAY_TRACKED_COUNT; ++i)
+        glDisableVertexAttribArray(i);
+
 #ifndef QT_OPENGL_ES_2
     // be nice to people who mix OpenGL 1.x code with QPainter commands
     // by setting modelview and projection matrices to mirror the GL 1
@@ -755,7 +540,7 @@
     Q_UNUSED(ctx);
 #endif
 
-    d->lastTexture = GLuint(-1);
+    d->lastTextureUsed = GLuint(-1);
     d->dirtyStencilRegion = QRect(0, 0, d->width, d->height);
     d->resetGLState();
 
@@ -774,6 +559,9 @@
     glDepthMask(true);
     glDepthFunc(GL_LESS);
     glClearDepth(1);
+    glStencilMask(0xff);
+    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
+    glStencilFunc(GL_ALWAYS, 0, 0xff);
 }
 
 void QGL2PaintEngineEx::endNativePainting()
@@ -782,49 +570,29 @@
     d->needsSync = true;
 }
 
-const QGLContext *QGL2PaintEngineEx::context()
-{
-    Q_D(QGL2PaintEngineEx);
-    return d->ctx;
-}
-
 void QGL2PaintEngineExPrivate::transferMode(EngineMode newMode)
 {
     if (newMode == mode)
         return;
 
     if (mode == TextDrawingMode || mode == ImageDrawingMode || mode == ImageArrayDrawingMode) {
-        glDisableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
-        glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-        glDisableVertexAttribArray(QT_OPACITY_ATTR);
-
-        lastTexture = GLuint(-1);
+        lastTextureUsed = GLuint(-1);
     }
 
     if (newMode == TextDrawingMode) {
-        glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-        glEnableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
-
-        glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinateArray.data());
-        glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinateArray.data());
+        setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinateArray.data());
+        setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinateArray.data());
     }
 
     if (newMode == ImageDrawingMode) {
-        glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-        glEnableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
-
-        glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, staticVertexCoordinateArray);
-        glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, staticTextureCoordinateArray);
+        setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray);
+        setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, staticTextureCoordinateArray);
     }
 
     if (newMode == ImageArrayDrawingMode) {
-        glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-        glEnableVertexAttribArray(QT_TEXTURE_COORDS_ATTR);
-        glEnableVertexAttribArray(QT_OPACITY_ATTR);
-
-        glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinateArray.data());
-        glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinateArray.data());
-        glVertexAttribPointer(QT_OPACITY_ATTR, 1, GL_FLOAT, GL_FALSE, 0, opacityArray.data());
+        setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinateArray.data());
+        setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinateArray.data());
+        setVertexAttributePointer(QT_OPACITY_ATTR, (GLfloat*)opacityArray.data());
     }
 
     // This needs to change when we implement high-quality anti-aliasing...
@@ -846,13 +614,14 @@
     qreal iscale;
 };
 
-void qopengl2paintengine_cleanup_vectorpath(QPaintEngineEx *engine, void *data)
+void QGL2PaintEngineExPrivate::cleanupVectorPath(QPaintEngineEx *engine, void *data)
 {
     QGL2PEVectorPathCache *c = (QGL2PEVectorPathCache *) data;
 #ifdef QT_OPENGL_CACHE_AS_VBOS
-    QGL2PaintEngineExPrivate *d = QGL2PaintEngineExPrivate::getData((QGL2PaintEngineEx *) engine);
-    d->unusedVBOSToClean << c->vbo;
+    Q_ASSERT(engine->type() == QPaintEngine::OpenGL2);
+    static_cast<QGL2PaintEngineEx *>(engine)->d_func()->unusedVBOSToClean << c->vbo;
 #else
+    Q_UNUSED(engine);
     qFree(c->vertices);
 #endif
     delete c;
@@ -863,6 +632,21 @@
 {
     transferMode(BrushDrawingMode);
 
+    const QOpenGL2PaintEngineState *s = q->state();
+    const bool newAddOffset = !(s->renderHints & QPainter::Antialiasing) &&
+                              (qbrush_style(currentBrush) == Qt::SolidPattern) &&
+                              !multisamplingAlwaysEnabled;
+
+    if (addOffset != newAddOffset) {
+        addOffset = newAddOffset;
+        matrixDirty = true;
+    }
+
+    if (snapToPixelGrid) {
+        snapToPixelGrid = false;
+        matrixDirty = true;
+    }
+
     // Might need to call updateMatrix to re-calculate inverseScale
     if (matrixDirty)
         updateMatrix();
@@ -898,7 +682,7 @@
             } else {
                 cache = new QGL2PEVectorPathCache;
                 cache->vertexCount = 0;
-                data = const_cast<QVectorPath &>(path).addCacheData(q, cache, qopengl2paintengine_cleanup_vectorpath);
+                data = const_cast<QVectorPath &>(path).addCacheData(q, cache, cleanupVectorPath);
             }
 
             // Flatten the path at the current scale factor and fill it into the cache struct.
@@ -921,12 +705,11 @@
             }
 
             prepareForDraw(currentBrush.isOpaque());
-            glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
 #ifdef QT_OPENGL_CACHE_AS_VBOS
             glBindBuffer(GL_ARRAY_BUFFER, cache->vbo);
-            glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, false, 0, 0);
+            setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, 0);
 #else
-            glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, false, 0, cache->vertices);
+            setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, cache->vertices);
 #endif
             glDrawArrays(cache->primitiveType, 0, cache->vertexCount);
 
@@ -963,20 +746,11 @@
             // Pass when high bit is set, replace stencil value with 0
             glStencilFunc(GL_NOTEQUAL, 0, GL_STENCIL_HIGH_BIT);
         }
-
         prepareForDraw(currentBrush.isOpaque());
 
-        if (inRenderText)
-            prepareDepthRangeForRenderText();
-
         // Stencil the brush onto the dest buffer
         composite(vertexCoordinateArray.boundingRect());
-
-        if (inRenderText)
-            restoreDepthRangeForRenderText();
-
         glStencilMask(0);
-
         updateClipScissorTest();
     }
 }
@@ -1015,13 +789,6 @@
     useSimpleShader();
     glEnable(GL_STENCIL_TEST); // For some reason, this has to happen _after_ the simple shader is use()'d
 
-#ifndef QT_OPENGL_ES_2
-    if (inRenderText) {
-        glPushAttrib(GL_ENABLE_BIT);
-        glDisable(GL_DEPTH_TEST);
-    }
-#endif
-
     if (mode == WindingFillMode) {
         Q_ASSERT(stops && !count);
         if (q->state()->clipTestEnabled) {
@@ -1062,10 +829,8 @@
         glStencilMask(GL_STENCIL_HIGH_BIT);
 #if 0
         glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); // Simply invert the stencil bit
-        glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-        glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, data);
+        setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, data);
         glDrawArrays(GL_TRIANGLE_STRIP, 0, count);
-        glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
 #else
 
         glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
@@ -1075,21 +840,13 @@
         } else {
             glStencilFunc(GL_ALWAYS, GL_STENCIL_HIGH_BIT, 0xff);
         }
-        glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-        glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, data);
+        setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, data);
         glDrawArrays(GL_TRIANGLE_STRIP, 0, count);
-        glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
 #endif
     }
 
     // Enable color writes & disable stencil writes
     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-
-#ifndef QT_OPENGL_ES_2
-    if (inRenderText)
-        glPopAttrib();
-#endif
-
 }
 
 /*
@@ -1183,7 +940,7 @@
         updateBrushUniforms();
 
     if (shaderMatrixUniformDirty) {
-        shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::PmvMatrix), pmvMatrix);
+        glUniformMatrix3fv(location(QGLEngineShaderManager::PmvMatrix), 1, GL_FALSE, (GLfloat*)pmvMatrix);
         shaderMatrixUniformDirty = false;
     }
 
@@ -1197,20 +954,9 @@
 
 void QGL2PaintEngineExPrivate::composite(const QGLRect& boundingRect)
 {
-    // Setup a vertex array for the bounding rect:
-    GLfloat rectVerts[] = {
-        boundingRect.left, boundingRect.top,
-        boundingRect.left, boundingRect.bottom,
-        boundingRect.right, boundingRect.bottom,
-        boundingRect.right, boundingRect.top
-    };
-
-    glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-    glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, rectVerts);
-
+    setCoords(staticVertexCoordinateArray, boundingRect);
+    setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray);
     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
-
-    glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
 }
 
 // Draws the vertex array as a set of <vertexArrayStops.size()> triangle fans.
@@ -1218,8 +964,7 @@
                                                 GLenum primitive)
 {
     // Now setup the pointer to the vertex array:
-    glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-    glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, data);
+    setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)data);
 
     int previousStop = 0;
     for (int i=0; i<stopCount; ++i) {
@@ -1232,31 +977,6 @@
         glDrawArrays(primitive, previousStop, stop - previousStop);
         previousStop = stop;
     }
-    glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-}
-
-void QGL2PaintEngineExPrivate::prepareDepthRangeForRenderText()
-{
-#ifndef QT_OPENGL_ES_2
-    // Get the z translation value from the model view matrix and
-    // transform it using the ortogonal projection with z-near = 0,
-    // and z-far = 1, which is used in QGLWidget::renderText()
-    GLdouble model[4][4];
-    glGetDoublev(GL_MODELVIEW_MATRIX, &model[0][0]);
-    float deviceZ = -2 * model[3][2] - 1;
-
-    glGetFloatv(GL_DEPTH_RANGE, depthRange);
-    float windowZ = depthRange[0] + (deviceZ + 1) * 0.5 * (depthRange[1] - depthRange[0]);
-
-    glDepthRange(windowZ, windowZ);
-#endif
-}
-
-void QGL2PaintEngineExPrivate::restoreDepthRangeForRenderText()
-{
-#ifndef QT_OPENGL_ES_2
-    glDepthRange(depthRange[0], depthRange[1]);
-#endif
 }
 
 /////////////////////////////////// Public Methods //////////////////////////////////////////
@@ -1274,31 +994,11 @@
 {
     Q_D(QGL2PaintEngineEx);
 
-    Qt::BrushStyle style = qbrush_style(brush);
-    if (style == Qt::NoBrush)
+    if (qbrush_style(brush) == Qt::NoBrush)
         return;
-    if (!d->inRenderText)
-        ensureActive();
-
-    QOpenGL2PaintEngineState *s = state();
-    bool doOffset = !(s->renderHints & QPainter::Antialiasing) &&
-                    (style == Qt::SolidPattern) &&
-                    !d->multisamplingAlwaysEnabled;
-
-    if (doOffset) {
-        d->temporaryTransform = s->matrix;
-        QTransform tx = QTransform::fromTranslate(.49, .49);
-        s->matrix = s->matrix * tx;
-        d->matrixDirty = true;
-    }
-
+    ensureActive();
     d->setBrush(brush);
     d->fill(path);
-
-    if (doOffset) {
-        s->matrix = d->temporaryTransform;
-        d->matrixDirty = true;
-    }
 }
 
 extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp
@@ -1308,9 +1008,8 @@
 {
     Q_D(QGL2PaintEngineEx);
 
-    Qt::PenStyle penStyle = qpen_style(pen);
     const QBrush &penBrush = qpen_brush(pen);
-    if (penStyle == Qt::NoPen || qbrush_style(penBrush) == Qt::NoBrush)
+    if (qpen_style(pen) == Qt::NoPen || qbrush_style(penBrush) == Qt::NoBrush)
         return;
 
     QOpenGL2PaintEngineState *s = state();
@@ -1321,53 +1020,57 @@
     }
 
     ensureActive();
+    d->setBrush(penBrush);
+    d->stroke(path, pen);
+}
 
-    bool doOffset = !(s->renderHints & QPainter::Antialiasing) && !d->multisamplingAlwaysEnabled;
-    if (doOffset) {
-        d->temporaryTransform = s->matrix;
-        QTransform tx = QTransform::fromTranslate(0.49, .49);
-        s->matrix = s->matrix * tx;
-        d->matrixDirty = true;
+void QGL2PaintEngineExPrivate::stroke(const QVectorPath &path, const QPen &pen)
+{
+    const QOpenGL2PaintEngineState *s = q->state();
+    const bool newAddOffset = !(s->renderHints & QPainter::Antialiasing) && !multisamplingAlwaysEnabled;
+    if (addOffset != newAddOffset) {
+        addOffset = newAddOffset;
+        matrixDirty = true;
     }
 
-    bool opaque = penBrush.isOpaque() && s->opacity > 0.99;
-    d->setBrush(penBrush);
-    d->transferMode(BrushDrawingMode);
+    if (snapToPixelGrid) {
+        snapToPixelGrid = false;
+        matrixDirty = true;
+    }
+
+    const Qt::PenStyle penStyle = qpen_style(pen);
+    const QBrush &penBrush = qpen_brush(pen);
+    const bool opaque = penBrush.isOpaque() && s->opacity > 0.99;
+
+    transferMode(BrushDrawingMode);
 
     // updateMatrix() is responsible for setting the inverse scale on
     // the strokers, so we need to call it here and not wait for
     // prepareForDraw() down below.
-    d->updateMatrix();
+    updateMatrix();
 
     if (penStyle == Qt::SolidLine) {
-        d->stroker.process(path, pen);
+        stroker.process(path, pen);
 
     } else { // Some sort of dash
-        d->dasher.process(path, pen);
+        dasher.process(path, pen);
 
-        QVectorPath dashStroke(d->dasher.points(),
-                               d->dasher.elementCount(),
-                               d->dasher.elementTypes());
-        d->stroker.process(dashStroke, pen);
+        QVectorPath dashStroke(dasher.points(),
+                               dasher.elementCount(),
+                               dasher.elementTypes());
+        stroker.process(dashStroke, pen);
     }
 
-
-    QGLContext *ctx = d->ctx;
-    Q_UNUSED(ctx);
-
     if (opaque) {
-        d->prepareForDraw(opaque);
-        glEnableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-        glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, false, 0, d->stroker.vertices());
-        glDrawArrays(GL_TRIANGLE_STRIP, 0, d->stroker.vertexCount() / 2);
+        prepareForDraw(opaque);
+        setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, stroker.vertices());
+        glDrawArrays(GL_TRIANGLE_STRIP, 0, stroker.vertexCount() / 2);
 
 //         QBrush b(Qt::green);
 //         d->setBrush(&b);
 //         d->prepareForDraw(true);
 //         glDrawArrays(GL_LINE_STRIP, 0, d->stroker.vertexCount() / 2);
 
-        glDisableVertexAttribArray(QT_VERTEX_COORDS_ATTR);
-
     } else {
         qreal width = qpen_widthf(pen) / 2;
         if (width == 0)
@@ -1377,30 +1080,25 @@
                       : width;
 
         if (pen.isCosmetic())
-            extra = extra * d->inverseScale;
+            extra = extra * inverseScale;
 
         QRectF bounds = path.controlPointRect().adjusted(-extra, -extra, extra, extra);
 
-        d->fillStencilWithVertexArray(d->stroker.vertices(), d->stroker.vertexCount() / 2,
+        fillStencilWithVertexArray(stroker.vertices(), stroker.vertexCount() / 2,
                                       0, 0, bounds, QGL2PaintEngineExPrivate::TriStripStrokeFillMode);
 
         glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
 
         // Pass when any bit is set, replace stencil value with 0
         glStencilFunc(GL_NOTEQUAL, 0, GL_STENCIL_HIGH_BIT);
-        d->prepareForDraw(false);
+        prepareForDraw(false);
 
         // Stencil the brush onto the dest buffer
-        d->composite(bounds);
+        composite(bounds);
 
         glStencilMask(0);
 
-        d->updateClipScissorTest();
-    }
-
-    if (doOffset) {
-        s->matrix = d->temporaryTransform;
-        d->matrixDirty = true;
+        updateClipScissorTest();
     }
 }
 
@@ -1440,7 +1138,7 @@
 #endif
 
     Q_D(QGL2PaintEngineEx);
-    d->lastTexture = GLuint(-1);
+    d->lastTextureUsed = GLuint(-1);
     d->brushTextureDirty = true;
 //    qDebug("QGL2PaintEngineEx::renderHintsChanged() not implemented!");
 }
@@ -1518,8 +1216,7 @@
 {
     Q_D(QGL2PaintEngineEx);
 
-    if (!d->inRenderText)
-        ensureActive();
+    ensureActive();
     QOpenGL2PaintEngineState *s = state();
 
     const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
@@ -1538,7 +1235,7 @@
                                             ? QFontEngineGlyphCache::Type(ti.fontEngine->glyphFormat)
                                             : d->glyphCacheType;
 
-    if (d->inRenderText || txtype > QTransform::TxTranslate)
+    if (txtype > QTransform::TxTranslate)
         glyphType = QFontEngineGlyphCache::Raster_A8;
 
     if (glyphType == QFontEngineGlyphCache::Raster_RGBMask
@@ -1580,8 +1277,6 @@
     if (cache->width() == 0 || cache->height() == 0)
         return;
 
-    if (inRenderText)
-        transferMode(BrushDrawingMode);
     transferMode(TextDrawingMode);
 
     int margin = cache->glyphMargin();
@@ -1589,9 +1284,6 @@
     GLfloat dx = 1.0 / cache->width();
     GLfloat dy = 1.0 / cache->height();
 
-    QGLPoint *oldVertexCoordinateDataPtr = vertexCoordinateArray.data();
-    QGLPoint *oldTextureCoordinateDataPtr = textureCoordinateArray.data();
-
     vertexCoordinateArray.clear();
     textureCoordinateArray.clear();
 
@@ -1604,17 +1296,21 @@
         textureCoordinateArray.addRect(QRectF(c.x*dx, c.y*dy, c.w * dx, c.h * dy));
     }
 
-    if (vertexCoordinateArray.data() != oldVertexCoordinateDataPtr)
-        glVertexAttribPointer(QT_VERTEX_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, vertexCoordinateArray.data());
-    if (textureCoordinateArray.data() != oldTextureCoordinateDataPtr)
-        glVertexAttribPointer(QT_TEXTURE_COORDS_ATTR, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinateArray.data());
+    setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinateArray.data());
+    setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinateArray.data());
+
+    if (addOffset) {
+        addOffset = false;
+        matrixDirty = true;
+    }
+    if (!snapToPixelGrid) {
+        snapToPixelGrid = true;
+        matrixDirty = true;
+    }
 
     QBrush pensBrush = q->state()->pen.brush();
     setBrush(pensBrush);
 
-    if (inRenderText)
-        prepareDepthRangeForRenderText();
-
     if (glyphType == QFontEngineGlyphCache::Raster_RGBMask) {
 
         // Subpixel antialiasing without gamma correction
@@ -1699,27 +1395,40 @@
 
     shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::MaskTexture), QT_MASK_TEXTURE_UNIT);
     glDrawArrays(GL_TRIANGLES, 0, 6 * glyphs.size());
-
-    if (inRenderText)
-        restoreDepthRangeForRenderText();
 }
 
 void QGL2PaintEngineEx::drawPixmaps(const QDrawPixmaps::Data *drawingData, int dataCount, const QPixmap &pixmap, QDrawPixmaps::DrawingHints hints)
 {
+    Q_D(QGL2PaintEngineEx);
     // Use fallback for extended composition modes.
     if (state()->composition_mode > QPainter::CompositionMode_Plus) {
         QPaintEngineEx::drawPixmaps(drawingData, dataCount, pixmap, hints);
         return;
     }
 
-    Q_D(QGL2PaintEngineEx);
+    ensureActive();
+    d->drawPixmaps(drawingData, dataCount, pixmap, hints);
+}
 
+
+void QGL2PaintEngineExPrivate::drawPixmaps(const QDrawPixmaps::Data *drawingData, int dataCount, const QPixmap &pixmap, QDrawPixmaps::DrawingHints hints)
+{
     GLfloat dx = 1.0f / pixmap.size().width();
     GLfloat dy = 1.0f / pixmap.size().height();
 
-    d->vertexCoordinateArray.clear();
-    d->textureCoordinateArray.clear();
-    d->opacityArray.reset();
+    vertexCoordinateArray.clear();
+    textureCoordinateArray.clear();
+    opacityArray.reset();
+
+    if (addOffset) {
+        addOffset = false;
+        matrixDirty = true;
+    }
+
+    if (snapToPixelGrid) {
+        snapToPixelGrid = false;
+        matrixDirty = true;
+    }
 
     bool allOpaque = true;
 
@@ -1736,31 +1445,28 @@
         QGLPoint bottomRight(right * c - bottom * s, right * s + bottom * c);
         QGLPoint bottomLeft(-right * c - bottom * s, -right * s + bottom * c);
 
-        d->vertexCoordinateArray.lineToArray(bottomRight.x + drawingData[i].point.x(), bottomRight.y + drawingData[i].point.y());
-        d->vertexCoordinateArray.lineToArray(-bottomLeft.x + drawingData[i].point.x(), -bottomLeft.y + drawingData[i].point.y());
-        d->vertexCoordinateArray.lineToArray(-bottomRight.x + drawingData[i].point.x(), -bottomRight.y + drawingData[i].point.y());
-        d->vertexCoordinateArray.lineToArray(-bottomRight.x + drawingData[i].point.x(), -bottomRight.y + drawingData[i].point.y());
-        d->vertexCoordinateArray.lineToArray(bottomLeft.x + drawingData[i].point.x(), bottomLeft.y + drawingData[i].point.y());
-        d->vertexCoordinateArray.lineToArray(bottomRight.x + drawingData[i].point.x(), bottomRight.y + drawingData[i].point.y());
+        vertexCoordinateArray.lineToArray(bottomRight.x + drawingData[i].point.x(), bottomRight.y + drawingData[i].point.y());
+        vertexCoordinateArray.lineToArray(-bottomLeft.x + drawingData[i].point.x(), -bottomLeft.y + drawingData[i].point.y());
+        vertexCoordinateArray.lineToArray(-bottomRight.x + drawingData[i].point.x(), -bottomRight.y + drawingData[i].point.y());
+        vertexCoordinateArray.lineToArray(-bottomRight.x + drawingData[i].point.x(), -bottomRight.y + drawingData[i].point.y());
+        vertexCoordinateArray.lineToArray(bottomLeft.x + drawingData[i].point.x(), bottomLeft.y + drawingData[i].point.y());
+        vertexCoordinateArray.lineToArray(bottomRight.x + drawingData[i].point.x(), bottomRight.y + drawingData[i].point.y());
 
         QGLRect src(drawingData[i].source.left() * dx, drawingData[i].source.top() * dy,
                     drawingData[i].source.right() * dx, drawingData[i].source.bottom() * dy);
 
-        d->textureCoordinateArray.lineToArray(src.right, src.bottom);
-        d->textureCoordinateArray.lineToArray(src.right, src.top);
-        d->textureCoordinateArray.lineToArray(src.left, src.top);
-        d->textureCoordinateArray.lineToArray(src.left, src.top);
-        d->textureCoordinateArray.lineToArray(src.left, src.bottom);
-        d->textureCoordinateArray.lineToArray(src.right, src.bottom);
+        textureCoordinateArray.lineToArray(src.right, src.bottom);
+        textureCoordinateArray.lineToArray(src.right, src.top);
+        textureCoordinateArray.lineToArray(src.left, src.top);
+        textureCoordinateArray.lineToArray(src.left, src.top);
+        textureCoordinateArray.lineToArray(src.left, src.bottom);
+        textureCoordinateArray.lineToArray(src.right, src.bottom);
 
-        qreal opacity = drawingData[i].opacity * state()->opacity;
-        d->opacityArray << opacity << opacity << opacity << opacity << opacity << opacity;
+        qreal opacity = drawingData[i].opacity * q->state()->opacity;
+        opacityArray << opacity << opacity << opacity << opacity << opacity << opacity;
         allOpaque &= (opacity >= 0.99f);
     }
 
-    ensureActive();
-
-    QGLContext *ctx = d->ctx;
     glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT);
     QGLTexture *texture = ctx->d_func()->bindTexture(pixmap, GL_TEXTURE_2D, GL_RGBA,
                                                      QGLContext::InternalBindOption
@@ -1768,27 +1474,28 @@
 
     if (texture->options & QGLContext::InvertedYBindOption) {
         // Flip texture y-coordinate.
-        QGLPoint *data = d->textureCoordinateArray.data();
+        QGLPoint *data = textureCoordinateArray.data();
         for (int i = 0; i < 6 * dataCount; ++i)
             data[i].y = 1 - data[i].y;
     }
 
-    d->transferMode(ImageArrayDrawingMode);
+    transferMode(ImageArrayDrawingMode);
 
     bool isBitmap = pixmap.isQBitmap();
     bool isOpaque = !isBitmap && (!pixmap.hasAlphaChannel() || (hints & QDrawPixmaps::OpaqueHint)) && allOpaque;
 
-    d->updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE,
-                           state()->renderHints & QPainter::SmoothPixmapTransform, texture->id);
+    updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE,
+                           q->state()->renderHints & QPainter::SmoothPixmapTransform, texture->id);
 
     // Setup for texture drawing
-    d->shaderManager->setSrcPixelType(isBitmap ? QGLEngineShaderManager::PatternSrc : QGLEngineShaderManager::ImageSrc);
-    if (d->prepareForDraw(isOpaque))
-        d->shaderManager->currentProgram()->setUniformValue(d->location(QGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT);
+    currentBrush = noBrush;
+    shaderManager->setSrcPixelType(isBitmap ? QGLEngineShaderManager::PatternSrc : QGLEngineShaderManager::ImageSrc);
+    if (prepareForDraw(isOpaque))
+        shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT);
 
     if (isBitmap) {
-        QColor col = qt_premultiplyColor(state()->pen.color(), (GLfloat)state()->opacity);
-        d->shaderManager->currentProgram()->setUniformValue(d->location(QGLEngineShaderManager::PatternColor), col);
+        QColor col = qt_premultiplyColor(q->state()->pen.color(), (GLfloat)q->state()->opacity);
+        shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::PatternColor), col);
     }
 
     glDrawArrays(GL_TRIANGLES, 0, 6 * dataCount);
@@ -1820,7 +1527,7 @@
     d->compositionModeDirty = true;
     d->opacityUniformDirty = true;
     d->needsSync = true;
-    d->use_system_clip = !systemClip().isEmpty();
+    d->useSystemClip = !systemClip().isEmpty();
     d->currentBrush = QBrush();
 
     d->dirtyStencilRegion = QRect(0, 0, d->width, d->height);
@@ -1840,11 +1547,9 @@
 
     d->shaderManager = new QGLEngineShaderManager(d->ctx);
 
-    if (!d->inRenderText) {
-        glDisable(GL_STENCIL_TEST);
-        glDisable(GL_DEPTH_TEST);
-        glDisable(GL_SCISSOR_TEST);
-    }
+    glDisable(GL_STENCIL_TEST);
+    glDisable(GL_DEPTH_TEST);
+    glDisable(GL_SCISSOR_TEST);
 
 #if !defined(QT_OPENGL_ES_2)
     glDisable(GL_MULTISAMPLE);
@@ -1854,6 +1559,7 @@
 
 #if !defined(QT_OPENGL_ES_2)
 #if defined(Q_WS_WIN)
+    extern Q_GUI_EXPORT bool qt_cleartype_enabled;
     if (qt_cleartype_enabled)
 #endif
         d->glyphCacheType = QFontEngineGlyphCache::Raster_RGBMask;
@@ -1921,6 +1627,9 @@
         glViewport(0, 0, d->width, d->height);
         d->needsSync = false;
         d->shaderManager->setDirty();
+        d->ctx->d_func()->syncGlState();
+        for (int i = 0; i < 3; ++i)
+            d->vertexAttribPointers[i] = (GLfloat*)-1; // Assume the pointers are clobbered
         setState(state());
     }
 }
@@ -1941,12 +1650,12 @@
 #else
     QRect bounds = q->state()->rectangleClip;
     if (!q->state()->clipEnabled) {
-        if (use_system_clip)
+        if (useSystemClip)
             bounds = systemClip.boundingRect();
         else
             bounds = QRect(0, 0, width, height);
     } else {
-        if (use_system_clip)
+        if (useSystemClip)
             bounds = bounds.intersected(systemClip.boundingRect());
         else
             bounds = bounds.intersected(QRect(0, 0, width, height));
@@ -2001,6 +1710,15 @@
 {
     transferMode(BrushDrawingMode);
 
+    if (addOffset) {
+        addOffset = false;
+        matrixDirty = true;
+    }
+    if (snapToPixelGrid) {
+        snapToPixelGrid = false;
+        matrixDirty = true;
+    }
+
     if (matrixDirty)
         updateMatrix();
 
@@ -2100,7 +1818,7 @@
 
     switch (op) {
     case Qt::NoClip:
-        if (d->use_system_clip) {
+        if (d->useSystemClip) {
             state()->clipTestEnabled = true;
             state()->currentClip = 1;
         } else {
@@ -2172,13 +1890,13 @@
     q->state()->clipChanged = true;
 
     if (systemClip.isEmpty()) {
-        use_system_clip = false;
+        useSystemClip = false;
     } else {
         if (q->paintDevice()->devType() == QInternal::Widget && currentClipWidget) {
             QWidgetPrivate *widgetPrivate = qt_widget_private(currentClipWidget->window());
-            use_system_clip = widgetPrivate->extra && widgetPrivate->extra->inRenderWithPainter;
+            useSystemClip = widgetPrivate->extra && widgetPrivate->extra->inRenderWithPainter;
         } else {
-            use_system_clip = true;
+            useSystemClip = true;
         }
     }
 
@@ -2188,19 +1906,19 @@
     q->state()->currentClip = 1;
     maxClip = 1;
 
-    q->state()->rectangleClip = use_system_clip ? systemClip.boundingRect() : QRect(0, 0, width, height);
+    q->state()->rectangleClip = useSystemClip ? systemClip.boundingRect() : QRect(0, 0, width, height);
     updateClipScissorTest();
 
     if (systemClip.rectCount() == 1) {
         if (systemClip.boundingRect() == QRect(0, 0, width, height))
-            use_system_clip = false;
+            useSystemClip = false;
 #ifndef QT_GL_NO_SCISSOR_TEST
         // scissoring takes care of the system clip
         return;
 #endif
     }
 
-    if (use_system_clip) {
+    if (useSystemClip) {
         clearClip(0);
 
         QPainterPath path;
@@ -2279,12 +1997,6 @@
     return s;
 }
 
-void QGL2PaintEngineEx::setRenderTextActive(bool active)
-{
-    Q_D(QGL2PaintEngineEx);
-    d->inRenderText = active;
-}
-
 QOpenGL2PaintEngineState::QOpenGL2PaintEngineState(QOpenGL2PaintEngineState &other)
     : QPainterState(other)
 {
@@ -2309,5 +2021,3 @@
 }
 
 QT_END_NAMESPACE
-
-#include "qpaintengineex_opengl2.moc"