| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2010 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 drawTexture(const QRectF &r, GLuint textureId, const QSize &size, const QRectF &sr);
|
|---|
| 127 | virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
|
|---|
| 128 | virtual void drawPixmaps(const QDrawPixmaps::Data *drawingData, int dataCount, const QPixmap &pixmap, QDrawPixmaps::DrawingHints 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 |
|
|---|
| 137 | Type type() const { return OpenGL2; }
|
|---|
| 138 |
|
|---|
| 139 | virtual void setState(QPainterState *s);
|
|---|
| 140 | virtual QPainterState *createState(QPainterState *orig) const;
|
|---|
| 141 | inline QOpenGL2PaintEngineState *state() {
|
|---|
| 142 | return static_cast<QOpenGL2PaintEngineState *>(QPaintEngineEx::state());
|
|---|
| 143 | }
|
|---|
| 144 | inline const QOpenGL2PaintEngineState *state() const {
|
|---|
| 145 | return static_cast<const QOpenGL2PaintEngineState *>(QPaintEngineEx::state());
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | void beginNativePainting();
|
|---|
| 149 | void endNativePainting();
|
|---|
| 150 |
|
|---|
| 151 | QPixmapFilter *pixmapFilter(int type, const QPixmapFilter *prototype);
|
|---|
| 152 |
|
|---|
| 153 | void setRenderTextActive(bool);
|
|---|
| 154 |
|
|---|
| 155 | private:
|
|---|
| 156 | Q_DISABLE_COPY(QGL2PaintEngineEx)
|
|---|
| 157 | };
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | class QGL2PaintEngineExPrivate : public QPaintEngineExPrivate
|
|---|
| 161 | {
|
|---|
| 162 | Q_DECLARE_PUBLIC(QGL2PaintEngineEx)
|
|---|
| 163 | public:
|
|---|
| 164 | enum StencilFillMode {
|
|---|
| 165 | OddEvenFillMode,
|
|---|
| 166 | WindingFillMode,
|
|---|
| 167 | TriStripStrokeFillMode
|
|---|
| 168 | };
|
|---|
| 169 |
|
|---|
| 170 | QGL2PaintEngineExPrivate(QGL2PaintEngineEx *q_ptr) :
|
|---|
| 171 | q(q_ptr),
|
|---|
| 172 | shaderManager(0),
|
|---|
| 173 | width(0), height(0),
|
|---|
| 174 | ctx(0),
|
|---|
| 175 | useSystemClip(true),
|
|---|
| 176 | snapToPixelGrid(false),
|
|---|
| 177 | addOffset(false),
|
|---|
| 178 | inverseScale(1)
|
|---|
| 179 | { }
|
|---|
| 180 |
|
|---|
| 181 | ~QGL2PaintEngineExPrivate();
|
|---|
| 182 |
|
|---|
| 183 | void updateBrushTexture();
|
|---|
| 184 | void updateBrushUniforms();
|
|---|
| 185 | void updateMatrix();
|
|---|
| 186 | void updateCompositionMode();
|
|---|
| 187 | void updateTextureFilter(GLenum target, GLenum wrapMode, bool smoothPixmapTransform, GLuint id = -1);
|
|---|
| 188 |
|
|---|
| 189 | void resetGLState();
|
|---|
| 190 |
|
|---|
| 191 | // fill, stroke, drawTexture, drawPixmaps & drawCachedGlyphs are the main rendering entry-points,
|
|---|
| 192 | // however writeClip can also be thought of as en entry point as it does similar things.
|
|---|
| 193 | void fill(const QVectorPath &path);
|
|---|
| 194 | void stroke(const QVectorPath &path, const QPen &pen);
|
|---|
| 195 | void drawTexture(const QGLRect& dest, const QGLRect& src, const QSize &textureSize, bool opaque, bool pattern = false);
|
|---|
| 196 | void drawPixmaps(const QDrawPixmaps::Data *drawingData, int dataCount, const QPixmap &pixmap, QDrawPixmaps::DrawingHints hints);
|
|---|
| 197 | void drawCachedGlyphs(const QPointF &p, QFontEngineGlyphCache::Type glyphType, const QTextItemInt &ti);
|
|---|
| 198 |
|
|---|
| 199 | // Calls glVertexAttributePointer if the pointer has changed
|
|---|
| 200 | inline void setVertexAttributePointer(unsigned int arrayIndex, const GLfloat *pointer);
|
|---|
| 201 |
|
|---|
| 202 | // draws whatever is in the vertex array:
|
|---|
| 203 | void drawVertexArrays(const float *data, int *stops, int stopCount, GLenum primitive);
|
|---|
| 204 | void drawVertexArrays(QGL2PEXVertexArray &vertexArray, GLenum primitive) {
|
|---|
| 205 | drawVertexArrays((const float *) vertexArray.data(), vertexArray.stops(), vertexArray.stopCount(), primitive);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | // Composites the bounding rect onto dest buffer:
|
|---|
| 209 | void composite(const QGLRect& boundingRect);
|
|---|
| 210 |
|
|---|
| 211 | // Calls drawVertexArrays to render into stencil buffer:
|
|---|
| 212 | void fillStencilWithVertexArray(const float *data, int count, int *stops, int stopCount, const QGLRect &bounds, StencilFillMode mode);
|
|---|
| 213 | void fillStencilWithVertexArray(QGL2PEXVertexArray& vertexArray, bool useWindingFill) {
|
|---|
| 214 | fillStencilWithVertexArray((const float *) vertexArray.data(), 0, vertexArray.stops(), vertexArray.stopCount(),
|
|---|
| 215 | vertexArray.boundingRect(),
|
|---|
| 216 | useWindingFill ? WindingFillMode : OddEvenFillMode);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | void setBrush(const QBrush& brush);
|
|---|
| 220 | void transferMode(EngineMode newMode);
|
|---|
| 221 | bool prepareForDraw(bool srcPixelsAreOpaque); // returns true if the program has changed
|
|---|
| 222 | inline void useSimpleShader();
|
|---|
| 223 | inline GLuint location(const QGLEngineShaderManager::Uniform uniform) {
|
|---|
| 224 | return shaderManager->getUniformLocation(uniform);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | void clearClip(uint value);
|
|---|
| 228 | void writeClip(const QVectorPath &path, uint value);
|
|---|
| 229 | void resetClipIfNeeded();
|
|---|
| 230 |
|
|---|
| 231 | void updateClipScissorTest();
|
|---|
| 232 | void setScissor(const QRect &rect);
|
|---|
| 233 | void regenerateClip();
|
|---|
| 234 | void systemStateChanged();
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | static QGLEngineShaderManager* shaderManagerForEngine(QGL2PaintEngineEx *engine) { return engine->d_func()->shaderManager; }
|
|---|
| 238 | static QGL2PaintEngineExPrivate *getData(QGL2PaintEngineEx *engine) { return engine->d_func(); }
|
|---|
| 239 | static void cleanupVectorPath(QPaintEngineEx *engine, void *data);
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 | QGL2PaintEngineEx* q;
|
|---|
| 243 | QGLEngineShaderManager* shaderManager;
|
|---|
| 244 | QGLPaintDevice* device;
|
|---|
| 245 | int width, height;
|
|---|
| 246 | QGLContext *ctx;
|
|---|
| 247 | EngineMode mode;
|
|---|
| 248 | QFontEngineGlyphCache::Type glyphCacheType;
|
|---|
| 249 |
|
|---|
| 250 | // Dirty flags
|
|---|
| 251 | bool matrixDirty; // Implies matrix uniforms are also dirty
|
|---|
| 252 | bool compositionModeDirty;
|
|---|
| 253 | bool brushTextureDirty;
|
|---|
| 254 | bool brushUniformsDirty;
|
|---|
| 255 | bool opacityUniformDirty;
|
|---|
| 256 |
|
|---|
| 257 | bool stencilClean; // Has the stencil not been used for clipping so far?
|
|---|
| 258 | bool useSystemClip;
|
|---|
| 259 | QRegion dirtyStencilRegion;
|
|---|
| 260 | QRect currentScissorBounds;
|
|---|
| 261 | uint maxClip;
|
|---|
| 262 |
|
|---|
| 263 | QBrush currentBrush; // May not be the state's brush!
|
|---|
| 264 | const QBrush noBrush;
|
|---|
| 265 |
|
|---|
| 266 | QGL2PEXVertexArray vertexCoordinateArray;
|
|---|
| 267 | QGL2PEXVertexArray textureCoordinateArray;
|
|---|
| 268 | QDataBuffer<GLfloat> opacityArray;
|
|---|
| 269 | GLfloat staticVertexCoordinateArray[8];
|
|---|
| 270 | GLfloat staticTextureCoordinateArray[8];
|
|---|
| 271 |
|
|---|
| 272 | bool snapToPixelGrid;
|
|---|
| 273 | bool addOffset; // When enabled, adds a 0.49,0.49 offset to matrix in updateMatrix
|
|---|
| 274 | GLfloat pmvMatrix[3][3];
|
|---|
| 275 | GLfloat inverseScale;
|
|---|
| 276 |
|
|---|
| 277 | GLuint lastTextureUsed;
|
|---|
| 278 |
|
|---|
| 279 | bool needsSync;
|
|---|
| 280 | bool multisamplingAlwaysEnabled;
|
|---|
| 281 |
|
|---|
| 282 | GLfloat depthRange[2];
|
|---|
| 283 |
|
|---|
| 284 | float textureInvertedY;
|
|---|
| 285 |
|
|---|
| 286 | QTriangulatingStroker stroker;
|
|---|
| 287 | QDashedStrokeProcessor dasher;
|
|---|
| 288 |
|
|---|
| 289 | QScopedPointer<QPixmapFilter> convolutionFilter;
|
|---|
| 290 | QScopedPointer<QPixmapFilter> colorizeFilter;
|
|---|
| 291 | QScopedPointer<QPixmapFilter> blurFilter;
|
|---|
| 292 | QScopedPointer<QPixmapFilter> dropShadowFilter;
|
|---|
| 293 |
|
|---|
| 294 | QSet<QVectorPath::CacheEntry *> pathCaches;
|
|---|
| 295 | QVector<GLuint> unusedVBOSToClean;
|
|---|
| 296 |
|
|---|
| 297 | const GLfloat *vertexAttribPointers[3];
|
|---|
| 298 | };
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 | void QGL2PaintEngineExPrivate::setVertexAttributePointer(unsigned int arrayIndex, const GLfloat *pointer)
|
|---|
| 302 | {
|
|---|
| 303 | Q_ASSERT(arrayIndex < 3);
|
|---|
| 304 | if (pointer == vertexAttribPointers[arrayIndex])
|
|---|
| 305 | return;
|
|---|
| 306 |
|
|---|
| 307 | vertexAttribPointers[arrayIndex] = pointer;
|
|---|
| 308 | if (arrayIndex == QT_OPACITY_ATTR)
|
|---|
| 309 | glVertexAttribPointer(arrayIndex, 1, GL_FLOAT, GL_FALSE, 0, pointer);
|
|---|
| 310 | else
|
|---|
| 311 | glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, pointer);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | QT_END_NAMESPACE
|
|---|
| 315 |
|
|---|
| 316 | #endif
|
|---|