| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #ifndef QGRAPHICSCONTEXT_OPENGL2_P_H
|
|---|
| 43 | #define QGRAPHICSCONTEXT_OPENGL2_P_H
|
|---|
| 44 |
|
|---|
| 45 | //
|
|---|
| 46 | // W A R N I N G
|
|---|
| 47 | // -------------
|
|---|
| 48 | //
|
|---|
| 49 | // This file is not part of the Qt API. It exists purely as an
|
|---|
| 50 | // implementation detail. This header file may change from version to
|
|---|
| 51 | // version without notice, or even be removed.
|
|---|
| 52 | //
|
|---|
| 53 | // We mean it.
|
|---|
| 54 | //
|
|---|
| 55 |
|
|---|
| 56 | #include <QDebug>
|
|---|
| 57 |
|
|---|
| 58 | #include <private/qpaintengineex_p.h>
|
|---|
| 59 | #include <private/qglengineshadermanager_p.h>
|
|---|
| 60 | #include <private/qgl2pexvertexarray_p.h>
|
|---|
| 61 | #include <private/qglpaintdevice_p.h>
|
|---|
| 62 | #include <private/qglpixmapfilter_p.h>
|
|---|
| 63 | #include <private/qfontengine_p.h>
|
|---|
| 64 | #include <private/qdatabuffer_p.h>
|
|---|
| 65 | #include <private/qtriangulatingstroker_p.h>
|
|---|
| 66 |
|
|---|
| 67 | enum EngineMode {
|
|---|
| 68 | ImageDrawingMode,
|
|---|
| 69 | TextDrawingMode,
|
|---|
| 70 | BrushDrawingMode,
|
|---|
| 71 | ImageArrayDrawingMode
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | QT_BEGIN_NAMESPACE
|
|---|
| 75 |
|
|---|
| 76 | #define GL_STENCIL_HIGH_BIT GLuint(0x80)
|
|---|
| 77 | #define QT_BRUSH_TEXTURE_UNIT GLuint(0)
|
|---|
| 78 | #define QT_IMAGE_TEXTURE_UNIT GLuint(0) //Can be the same as brush texture unit
|
|---|
| 79 | #define QT_MASK_TEXTURE_UNIT GLuint(1)
|
|---|
| 80 | #define QT_BACKGROUND_TEXTURE_UNIT GLuint(2)
|
|---|
| 81 |
|
|---|
| 82 | class QGL2PaintEngineExPrivate;
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | class QOpenGL2PaintEngineState : public QPainterState
|
|---|
| 86 | {
|
|---|
| 87 | public:
|
|---|
| 88 | QOpenGL2PaintEngineState(QOpenGL2PaintEngineState &other);
|
|---|
| 89 | QOpenGL2PaintEngineState();
|
|---|
| 90 | ~QOpenGL2PaintEngineState();
|
|---|
| 91 |
|
|---|
| 92 | uint isNew : 1;
|
|---|
| 93 | uint needsClipBufferClear : 1;
|
|---|
| 94 | uint clipTestEnabled : 1;
|
|---|
| 95 | uint canRestoreClip : 1;
|
|---|
| 96 | uint matrixChanged : 1;
|
|---|
| 97 | uint compositionModeChanged : 1;
|
|---|
| 98 | uint opacityChanged : 1;
|
|---|
| 99 | uint renderHintsChanged : 1;
|
|---|
| 100 | uint clipChanged : 1;
|
|---|
| 101 | uint currentClip : 8;
|
|---|
| 102 |
|
|---|
| 103 | QRect rectangleClip;
|
|---|
| 104 | };
|
|---|
| 105 |
|
|---|
| 106 | class Q_OPENGL_EXPORT QGL2PaintEngineEx : public QPaintEngineEx
|
|---|
| 107 | {
|
|---|
| 108 | Q_DECLARE_PRIVATE(QGL2PaintEngineEx)
|
|---|
| 109 | public:
|
|---|
| 110 | QGL2PaintEngineEx();
|
|---|
| 111 | ~QGL2PaintEngineEx();
|
|---|
| 112 |
|
|---|
| 113 | bool begin(QPaintDevice *device);
|
|---|
| 114 | void ensureActive();
|
|---|
| 115 | bool end();
|
|---|
| 116 |
|
|---|
| 117 | virtual void clipEnabledChanged();
|
|---|
| 118 | virtual void penChanged();
|
|---|
| 119 | virtual void brushChanged();
|
|---|
| 120 | virtual void brushOriginChanged();
|
|---|
| 121 | virtual void opacityChanged();
|
|---|
| 122 | virtual void compositionModeChanged();
|
|---|
| 123 | virtual void renderHintsChanged();
|
|---|
| 124 | virtual void transformChanged();
|
|---|
| 125 |
|
|---|
| 126 | virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
|
|---|
| 127 | virtual void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap,
|
|---|
| 128 | QPainter::PixmapFragmentHints hints);
|
|---|
| 129 | virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
|
|---|
| 130 | Qt::ImageConversionFlags flags = Qt::AutoColor);
|
|---|
| 131 | virtual void drawTextItem(const QPointF &p, const QTextItem &textItem);
|
|---|
| 132 | virtual void fill(const QVectorPath &path, const QBrush &brush);
|
|---|
| 133 | virtual void stroke(const QVectorPath &path, const QPen &pen);
|
|---|
| 134 | virtual void clip(const QVectorPath &path, Qt::ClipOperation op);
|
|---|
| 135 |
|
|---|
| 136 | virtual void drawStaticTextItem(QStaticTextItem *textItem);
|
|---|
| 137 |
|
|---|
| 138 | bool drawTexture(const QRectF &r, GLuint textureId, const QSize &size, const QRectF &sr);
|
|---|
| 139 |
|
|---|
| 140 | Type type() const { return OpenGL2; }
|
|---|
| 141 |
|
|---|
| 142 | virtual void setState(QPainterState *s);
|
|---|
| 143 | virtual QPainterState *createState(QPainterState *orig) const;
|
|---|
| 144 | inline QOpenGL2PaintEngineState *state() {
|
|---|
| 145 | return static_cast<QOpenGL2PaintEngineState *>(QPaintEngineEx::state());
|
|---|
| 146 | }
|
|---|
| 147 | inline const QOpenGL2PaintEngineState *state() const {
|
|---|
| 148 | return static_cast<const QOpenGL2PaintEngineState *>(QPaintEngineEx::state());
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void beginNativePainting();
|
|---|
| 152 | void endNativePainting();
|
|---|
| 153 |
|
|---|
| 154 | void invalidateState();
|
|---|
| 155 |
|
|---|
| 156 | QPixmapFilter *pixmapFilter(int type, const QPixmapFilter *prototype);
|
|---|
| 157 |
|
|---|
| 158 | void setRenderTextActive(bool);
|
|---|
| 159 |
|
|---|
| 160 | bool isNativePaintingActive() const;
|
|---|
| 161 | private:
|
|---|
| 162 | Q_DISABLE_COPY(QGL2PaintEngineEx)
|
|---|
| 163 | };
|
|---|
| 164 |
|
|---|
| 165 | class QGL2PaintEngineExPrivate : public QPaintEngineExPrivate
|
|---|
| 166 | {
|
|---|
| 167 | Q_DECLARE_PUBLIC(QGL2PaintEngineEx)
|
|---|
| 168 | public:
|
|---|
| 169 | enum StencilFillMode {
|
|---|
| 170 | OddEvenFillMode,
|
|---|
| 171 | WindingFillMode,
|
|---|
| 172 | TriStripStrokeFillMode
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | QGL2PaintEngineExPrivate(QGL2PaintEngineEx *q_ptr) :
|
|---|
| 176 | q(q_ptr),
|
|---|
| 177 | shaderManager(0),
|
|---|
| 178 | width(0), height(0),
|
|---|
| 179 | ctx(0),
|
|---|
| 180 | useSystemClip(true),
|
|---|
| 181 | elementIndicesVBOId(0),
|
|---|
| 182 | opacityArray(0),
|
|---|
| 183 | snapToPixelGrid(false),
|
|---|
| 184 | addOffset(false),
|
|---|
| 185 | nativePaintingActive(false),
|
|---|
| 186 | inverseScale(1),
|
|---|
| 187 | lastMaskTextureUsed(0)
|
|---|
| 188 | { }
|
|---|
| 189 |
|
|---|
| 190 | ~QGL2PaintEngineExPrivate();
|
|---|
| 191 |
|
|---|
| 192 | void updateBrushTexture();
|
|---|
| 193 | void updateBrushUniforms();
|
|---|
| 194 | void updateMatrix();
|
|---|
| 195 | void updateCompositionMode();
|
|---|
| 196 | void updateTextureFilter(GLenum target, GLenum wrapMode, bool smoothPixmapTransform, GLuint id = -1);
|
|---|
| 197 |
|
|---|
| 198 | void resetGLState();
|
|---|
| 199 |
|
|---|
| 200 | // fill, stroke, drawTexture, drawPixmaps & drawCachedGlyphs are the main rendering entry-points,
|
|---|
| 201 | // however writeClip can also be thought of as en entry point as it does similar things.
|
|---|
| 202 | void fill(const QVectorPath &path);
|
|---|
| 203 | void stroke(const QVectorPath &path, const QPen &pen);
|
|---|
| 204 | void drawTexture(const QGLRect& dest, const QGLRect& src, const QSize &textureSize, bool opaque, bool pattern = false);
|
|---|
| 205 | void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap,
|
|---|
| 206 | QPainter::PixmapFragmentHints hints);
|
|---|
| 207 | void drawCachedGlyphs(QFontEngineGlyphCache::Type glyphType, QStaticTextItem *staticTextItem);
|
|---|
| 208 |
|
|---|
| 209 | // Calls glVertexAttributePointer if the pointer has changed
|
|---|
| 210 | inline void setVertexAttributePointer(unsigned int arrayIndex, const GLfloat *pointer);
|
|---|
| 211 |
|
|---|
| 212 | // draws whatever is in the vertex array:
|
|---|
| 213 | void drawVertexArrays(const float *data, int *stops, int stopCount, GLenum primitive);
|
|---|
| 214 | void drawVertexArrays(QGL2PEXVertexArray &vertexArray, GLenum primitive) {
|
|---|
| 215 | drawVertexArrays((const float *) vertexArray.data(), vertexArray.stops(), vertexArray.stopCount(), primitive);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | // Composites the bounding rect onto dest buffer:
|
|---|
| 219 | void composite(const QGLRect& boundingRect);
|
|---|
| 220 |
|
|---|
| 221 | // Calls drawVertexArrays to render into stencil buffer:
|
|---|
| 222 | void fillStencilWithVertexArray(const float *data, int count, int *stops, int stopCount, const QGLRect &bounds, StencilFillMode mode);
|
|---|
| 223 | void fillStencilWithVertexArray(QGL2PEXVertexArray& vertexArray, bool useWindingFill) {
|
|---|
| 224 | fillStencilWithVertexArray((const float *) vertexArray.data(), 0, vertexArray.stops(), vertexArray.stopCount(),
|
|---|
| 225 | vertexArray.boundingRect(),
|
|---|
| 226 | useWindingFill ? WindingFillMode : OddEvenFillMode);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void setBrush(const QBrush& brush);
|
|---|
| 230 | void transferMode(EngineMode newMode);
|
|---|
| 231 | bool prepareForDraw(bool srcPixelsAreOpaque); // returns true if the program has changed
|
|---|
| 232 | inline void useSimpleShader();
|
|---|
| 233 | inline GLuint location(const QGLEngineShaderManager::Uniform uniform) {
|
|---|
| 234 | return shaderManager->getUniformLocation(uniform);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | void clearClip(uint value);
|
|---|
| 238 | void writeClip(const QVectorPath &path, uint value);
|
|---|
| 239 | void resetClipIfNeeded();
|
|---|
| 240 |
|
|---|
| 241 | void updateClipScissorTest();
|
|---|
| 242 | void setScissor(const QRect &rect);
|
|---|
| 243 | void regenerateClip();
|
|---|
| 244 | void systemStateChanged();
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | static QGLEngineShaderManager* shaderManagerForEngine(QGL2PaintEngineEx *engine) { return engine->d_func()->shaderManager; }
|
|---|
| 248 | static QGL2PaintEngineExPrivate *getData(QGL2PaintEngineEx *engine) { return engine->d_func(); }
|
|---|
| 249 | static void cleanupVectorPath(QPaintEngineEx *engine, void *data);
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | QGL2PaintEngineEx* q;
|
|---|
| 253 | QGLEngineShaderManager* shaderManager;
|
|---|
| 254 | QGLPaintDevice* device;
|
|---|
| 255 | int width, height;
|
|---|
| 256 | QGLContext *ctx;
|
|---|
| 257 | EngineMode mode;
|
|---|
| 258 | QFontEngineGlyphCache::Type glyphCacheType;
|
|---|
| 259 |
|
|---|
| 260 | // Dirty flags
|
|---|
| 261 | bool matrixDirty; // Implies matrix uniforms are also dirty
|
|---|
| 262 | bool compositionModeDirty;
|
|---|
| 263 | bool brushTextureDirty;
|
|---|
| 264 | bool brushUniformsDirty;
|
|---|
| 265 | bool opacityUniformDirty;
|
|---|
| 266 | bool matrixUniformDirty;
|
|---|
| 267 |
|
|---|
| 268 | bool stencilClean; // Has the stencil not been used for clipping so far?
|
|---|
| 269 | bool useSystemClip;
|
|---|
| 270 | QRegion dirtyStencilRegion;
|
|---|
| 271 | QRect currentScissorBounds;
|
|---|
| 272 | uint maxClip;
|
|---|
| 273 |
|
|---|
| 274 | QBrush currentBrush; // May not be the state's brush!
|
|---|
| 275 | const QBrush noBrush;
|
|---|
| 276 |
|
|---|
| 277 | QPixmap currentBrushPixmap;
|
|---|
| 278 |
|
|---|
| 279 | QGL2PEXVertexArray vertexCoordinateArray;
|
|---|
| 280 | QGL2PEXVertexArray textureCoordinateArray;
|
|---|
| 281 | QVector<GLushort> elementIndices;
|
|---|
| 282 | GLuint elementIndicesVBOId;
|
|---|
| 283 | QDataBuffer<GLfloat> opacityArray;
|
|---|
| 284 | GLfloat staticVertexCoordinateArray[8];
|
|---|
| 285 | GLfloat staticTextureCoordinateArray[8];
|
|---|
| 286 |
|
|---|
| 287 | bool snapToPixelGrid;
|
|---|
| 288 | bool addOffset; // When enabled, adds a 0.49,0.49 offset to matrix in updateMatrix
|
|---|
| 289 | bool nativePaintingActive;
|
|---|
| 290 | GLfloat pmvMatrix[3][3];
|
|---|
| 291 | GLfloat inverseScale;
|
|---|
| 292 |
|
|---|
| 293 | GLuint lastTextureUsed;
|
|---|
| 294 | GLuint lastMaskTextureUsed;
|
|---|
| 295 |
|
|---|
| 296 | bool needsSync;
|
|---|
| 297 | bool multisamplingAlwaysEnabled;
|
|---|
| 298 |
|
|---|
| 299 | GLfloat depthRange[2];
|
|---|
| 300 |
|
|---|
| 301 | float textureInvertedY;
|
|---|
| 302 |
|
|---|
| 303 | QTriangulatingStroker stroker;
|
|---|
| 304 | QDashedStrokeProcessor dasher;
|
|---|
| 305 |
|
|---|
| 306 | QScopedPointer<QPixmapFilter> convolutionFilter;
|
|---|
| 307 | QScopedPointer<QPixmapFilter> colorizeFilter;
|
|---|
| 308 | QScopedPointer<QPixmapFilter> blurFilter;
|
|---|
| 309 | QScopedPointer<QPixmapFilter> dropShadowFilter;
|
|---|
| 310 |
|
|---|
| 311 | QSet<QVectorPath::CacheEntry *> pathCaches;
|
|---|
| 312 | QVector<GLuint> unusedVBOSToClean;
|
|---|
| 313 | QVector<GLuint> unusedIBOSToClean;
|
|---|
| 314 |
|
|---|
| 315 | const GLfloat *vertexAttribPointers[3];
|
|---|
| 316 | };
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 | void QGL2PaintEngineExPrivate::setVertexAttributePointer(unsigned int arrayIndex, const GLfloat *pointer)
|
|---|
| 320 | {
|
|---|
| 321 | Q_ASSERT(arrayIndex < 3);
|
|---|
| 322 | if (pointer == vertexAttribPointers[arrayIndex])
|
|---|
| 323 | return;
|
|---|
| 324 |
|
|---|
| 325 | vertexAttribPointers[arrayIndex] = pointer;
|
|---|
| 326 | if (arrayIndex == QT_OPACITY_ATTR)
|
|---|
| 327 | glVertexAttribPointer(arrayIndex, 1, GL_FLOAT, GL_FALSE, 0, pointer);
|
|---|
| 328 | else
|
|---|
| 329 | glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, pointer);
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | QT_END_NAMESPACE
|
|---|
| 333 |
|
|---|
| 334 | #endif
|
|---|