1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtOpenGL module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QOPENGLPAINTENGINE_P_H |
41 | #define QOPENGLPAINTENGINE_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QDebug> |
55 | |
56 | #include <qopenglpaintdevice.h> |
57 | |
58 | #include <private/qpaintengineex_p.h> |
59 | #include <private/qopenglengineshadermanager_p.h> |
60 | #include <private/qopengl2pexvertexarray_p.h> |
61 | #include <private/qfontengine_p.h> |
62 | #include <private/qdatabuffer_p.h> |
63 | #include <private/qtriangulatingstroker_p.h> |
64 | |
65 | #include <private/qopenglextensions_p.h> |
66 | |
67 | #include <QOpenGLVertexArrayObject> |
68 | #include <QOpenGLBuffer> |
69 | |
70 | enum EngineMode { |
71 | ImageDrawingMode, |
72 | TextDrawingMode, |
73 | BrushDrawingMode, |
74 | ImageArrayDrawingMode, |
75 | ImageOpacityArrayDrawingMode |
76 | }; |
77 | |
78 | QT_BEGIN_NAMESPACE |
79 | |
80 | #define GL_STENCIL_HIGH_BIT GLuint(0x80) |
81 | #define QT_UNKNOWN_TEXTURE_UNIT GLuint(-1) |
82 | #define QT_DEFAULT_TEXTURE_UNIT GLuint(0) |
83 | #define QT_BRUSH_TEXTURE_UNIT GLuint(0) |
84 | #define QT_IMAGE_TEXTURE_UNIT GLuint(0) //Can be the same as brush texture unit |
85 | #define QT_MASK_TEXTURE_UNIT GLuint(1) |
86 | #define QT_BACKGROUND_TEXTURE_UNIT GLuint(2) |
87 | |
88 | class QOpenGL2PaintEngineExPrivate; |
89 | |
90 | class QOpenGL2PaintEngineState : public QPainterState |
91 | { |
92 | public: |
93 | QOpenGL2PaintEngineState(QOpenGL2PaintEngineState &other); |
94 | QOpenGL2PaintEngineState(); |
95 | ~QOpenGL2PaintEngineState(); |
96 | |
97 | uint isNew : 1; |
98 | uint needsClipBufferClear : 1; |
99 | uint clipTestEnabled : 1; |
100 | uint canRestoreClip : 1; |
101 | uint matrixChanged : 1; |
102 | uint compositionModeChanged : 1; |
103 | uint opacityChanged : 1; |
104 | uint renderHintsChanged : 1; |
105 | uint clipChanged : 1; |
106 | uint currentClip : 8; |
107 | |
108 | QRect rectangleClip; |
109 | }; |
110 | |
111 | class Q_OPENGL_EXPORT QOpenGL2PaintEngineEx : public QPaintEngineEx |
112 | { |
113 | Q_DECLARE_PRIVATE(QOpenGL2PaintEngineEx) |
114 | public: |
115 | QOpenGL2PaintEngineEx(); |
116 | ~QOpenGL2PaintEngineEx(); |
117 | |
118 | bool begin(QPaintDevice *device) override; |
119 | void ensureActive(); |
120 | bool end() override; |
121 | |
122 | virtual void clipEnabledChanged() override; |
123 | virtual void penChanged() override; |
124 | virtual void brushChanged() override; |
125 | virtual void brushOriginChanged() override; |
126 | virtual void opacityChanged() override; |
127 | virtual void compositionModeChanged() override; |
128 | virtual void renderHintsChanged() override; |
129 | virtual void transformChanged() override; |
130 | |
131 | virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override; |
132 | virtual void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, |
133 | QPainter::PixmapFragmentHints hints) override; |
134 | virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, |
135 | Qt::ImageConversionFlags flags = Qt::AutoColor) override; |
136 | virtual void drawTextItem(const QPointF &p, const QTextItem &textItem) override; |
137 | virtual void fill(const QVectorPath &path, const QBrush &brush) override; |
138 | virtual void stroke(const QVectorPath &path, const QPen &pen) override; |
139 | virtual void clip(const QVectorPath &path, Qt::ClipOperation op) override; |
140 | |
141 | virtual void drawStaticTextItem(QStaticTextItem *textItem) override; |
142 | |
143 | bool drawTexture(const QRectF &r, GLuint textureId, const QSize &size, const QRectF &sr); |
144 | |
145 | Type type() const override { return OpenGL2; } |
146 | |
147 | virtual void setState(QPainterState *s) override; |
148 | virtual QPainterState *createState(QPainterState *orig) const override; |
149 | inline QOpenGL2PaintEngineState *state() { |
150 | return static_cast<QOpenGL2PaintEngineState *>(QPaintEngineEx::state()); |
151 | } |
152 | inline const QOpenGL2PaintEngineState *state() const { |
153 | return static_cast<const QOpenGL2PaintEngineState *>(QPaintEngineEx::state()); |
154 | } |
155 | |
156 | void beginNativePainting() override; |
157 | void endNativePainting() override; |
158 | |
159 | void invalidateState(); |
160 | |
161 | void setRenderTextActive(bool); |
162 | |
163 | bool isNativePaintingActive() const; |
164 | bool requiresPretransformedGlyphPositions(QFontEngine *, const QTransform &) const override { return false; } |
165 | bool shouldDrawCachedGlyphs(QFontEngine *, const QTransform &) const override; |
166 | |
167 | private: |
168 | Q_DISABLE_COPY_MOVE(QOpenGL2PaintEngineEx) |
169 | |
170 | friend class QOpenGLEngineShaderManager; |
171 | }; |
172 | |
173 | // This probably needs to grow to GL_MAX_VERTEX_ATTRIBS, but 3 is ok for now as that's |
174 | // all the GL2 engine uses: |
175 | #define QT_GL_VERTEX_ARRAY_TRACKED_COUNT 3 |
176 | |
177 | class QOpenGL2PaintEngineExPrivate : public QPaintEngineExPrivate |
178 | { |
179 | Q_DECLARE_PUBLIC(QOpenGL2PaintEngineEx) |
180 | public: |
181 | enum StencilFillMode { |
182 | OddEvenFillMode, |
183 | WindingFillMode, |
184 | TriStripStrokeFillMode |
185 | }; |
186 | |
187 | QOpenGL2PaintEngineExPrivate(QOpenGL2PaintEngineEx *q_ptr) : |
188 | q(q_ptr), |
189 | shaderManager(nullptr), |
190 | width(0), height(0), |
191 | ctx(nullptr), |
192 | useSystemClip(true), |
193 | elementIndicesVBOId(0), |
194 | opacityArray(0), |
195 | snapToPixelGrid(false), |
196 | nativePaintingActive(false), |
197 | inverseScale(1), |
198 | lastTextureUnitUsed(QT_UNKNOWN_TEXTURE_UNIT), |
199 | vertexBuffer(QOpenGLBuffer::VertexBuffer), |
200 | texCoordBuffer(QOpenGLBuffer::VertexBuffer), |
201 | opacityBuffer(QOpenGLBuffer::VertexBuffer), |
202 | indexBuffer(QOpenGLBuffer::IndexBuffer) |
203 | { } |
204 | |
205 | ~QOpenGL2PaintEngineExPrivate(); |
206 | |
207 | void updateBrushTexture(); |
208 | void updateBrushUniforms(); |
209 | void updateMatrix(); |
210 | void updateCompositionMode(); |
211 | |
212 | enum TextureUpdateMode { UpdateIfNeeded, ForceUpdate }; |
213 | template<typename T> |
214 | void updateTexture(GLenum textureUnit, const T &texture, GLenum wrapMode, GLenum filterMode, TextureUpdateMode updateMode = UpdateIfNeeded); |
215 | template<typename T> |
216 | GLuint bindTexture(const T &texture); |
217 | void activateTextureUnit(GLenum textureUnit); |
218 | |
219 | void resetGLState(); |
220 | |
221 | // fill, stroke, drawTexture, drawPixmaps & drawCachedGlyphs are the main rendering entry-points, |
222 | // however writeClip can also be thought of as en entry point as it does similar things. |
223 | void fill(const QVectorPath &path); |
224 | void stroke(const QVectorPath &path, const QPen &pen); |
225 | void drawTexture(const QOpenGLRect& dest, const QOpenGLRect& src, const QSize &textureSize, bool opaque, bool pattern = false); |
226 | void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, |
227 | QPainter::PixmapFragmentHints hints); |
228 | void drawCachedGlyphs(QFontEngine::GlyphFormat glyphFormat, QStaticTextItem *staticTextItem); |
229 | |
230 | // Calls glVertexAttributePointer if the pointer has changed |
231 | inline void uploadData(unsigned int arrayIndex, const GLfloat *data, GLuint count); |
232 | inline bool uploadIndexData(const void *data, GLenum indexValueType, GLuint count); |
233 | |
234 | // draws whatever is in the vertex array: |
235 | void drawVertexArrays(const float *data, int *stops, int stopCount, GLenum primitive); |
236 | void drawVertexArrays(QOpenGL2PEXVertexArray &vertexArray, GLenum primitive) { |
237 | drawVertexArrays((const float *) vertexArray.data(), vertexArray.stops(), vertexArray.stopCount(), primitive); |
238 | } |
239 | |
240 | // Composites the bounding rect onto dest buffer: |
241 | void composite(const QOpenGLRect& boundingRect); |
242 | |
243 | // Calls drawVertexArrays to render into stencil buffer: |
244 | void fillStencilWithVertexArray(const float *data, int count, int *stops, int stopCount, const QOpenGLRect &bounds, StencilFillMode mode); |
245 | void fillStencilWithVertexArray(QOpenGL2PEXVertexArray& vertexArray, bool useWindingFill) { |
246 | fillStencilWithVertexArray((const float *) vertexArray.data(), 0, vertexArray.stops(), vertexArray.stopCount(), |
247 | vertexArray.boundingRect(), |
248 | useWindingFill ? WindingFillMode : OddEvenFillMode); |
249 | } |
250 | |
251 | void setBrush(const QBrush& brush); |
252 | void transferMode(EngineMode newMode); |
253 | bool prepareForDraw(bool srcPixelsAreOpaque); // returns true if the program has changed |
254 | bool prepareForCachedGlyphDraw(const QFontEngineGlyphCache &cache); |
255 | inline void useSimpleShader(); |
256 | inline GLuint location(const QOpenGLEngineShaderManager::Uniform uniform) { |
257 | return shaderManager->getUniformLocation(uniform); |
258 | } |
259 | |
260 | void clearClip(uint value); |
261 | void writeClip(const QVectorPath &path, uint value); |
262 | void resetClipIfNeeded(); |
263 | |
264 | void updateClipScissorTest(); |
265 | void setScissor(const QRect &rect); |
266 | void regenerateClip(); |
267 | void systemStateChanged() override; |
268 | |
269 | void setVertexAttribArrayEnabled(int arrayIndex, bool enabled = true); |
270 | void syncGlState(); |
271 | |
272 | static QOpenGLEngineShaderManager* shaderManagerForEngine(QOpenGL2PaintEngineEx *engine) { return engine->d_func()->shaderManager; } |
273 | static QOpenGL2PaintEngineExPrivate *getData(QOpenGL2PaintEngineEx *engine) { return engine->d_func(); } |
274 | static void cleanupVectorPath(QPaintEngineEx *engine, void *data); |
275 | |
276 | QOpenGLExtensions funcs; |
277 | |
278 | QOpenGL2PaintEngineEx* q; |
279 | QOpenGLEngineShaderManager* shaderManager; |
280 | QOpenGLPaintDevice* device; |
281 | int width, height; |
282 | QOpenGLContext *ctx; |
283 | EngineMode mode; |
284 | QFontEngine::GlyphFormat glyphCacheFormat; |
285 | |
286 | bool vertexAttributeArraysEnabledState[QT_GL_VERTEX_ARRAY_TRACKED_COUNT]; |
287 | |
288 | // Dirty flags |
289 | bool matrixDirty; // Implies matrix uniforms are also dirty |
290 | bool compositionModeDirty; |
291 | bool brushTextureDirty; |
292 | bool brushUniformsDirty; |
293 | bool opacityUniformDirty; |
294 | bool matrixUniformDirty; |
295 | |
296 | bool stencilClean; // Has the stencil not been used for clipping so far? |
297 | bool useSystemClip; |
298 | QRegion dirtyStencilRegion; |
299 | QRect currentScissorBounds; |
300 | uint maxClip; |
301 | |
302 | QBrush currentBrush; // May not be the state's brush! |
303 | const QBrush noBrush; |
304 | |
305 | QImage currentBrushImage; |
306 | |
307 | QOpenGL2PEXVertexArray vertexCoordinateArray; |
308 | QOpenGL2PEXVertexArray textureCoordinateArray; |
309 | QList<GLushort> elementIndices; |
310 | GLuint elementIndicesVBOId; |
311 | QDataBuffer<GLfloat> opacityArray; |
312 | GLfloat staticVertexCoordinateArray[8]; |
313 | GLfloat staticTextureCoordinateArray[8]; |
314 | |
315 | bool snapToPixelGrid; |
316 | bool nativePaintingActive; |
317 | GLfloat pmvMatrix[3][3]; |
318 | GLfloat inverseScale; |
319 | |
320 | GLenum lastTextureUnitUsed; |
321 | GLuint lastTextureUsed; |
322 | |
323 | QOpenGLVertexArrayObject vao; |
324 | QOpenGLBuffer vertexBuffer; |
325 | QOpenGLBuffer texCoordBuffer; |
326 | QOpenGLBuffer opacityBuffer; |
327 | QOpenGLBuffer indexBuffer; |
328 | |
329 | bool needsSync; |
330 | bool multisamplingAlwaysEnabled; |
331 | |
332 | QTriangulatingStroker stroker; |
333 | QDashedStrokeProcessor dasher; |
334 | |
335 | QVarLengthArray<GLuint, 8> unusedVBOSToClean; |
336 | QVarLengthArray<GLuint, 8> unusedIBOSToClean; |
337 | |
338 | const GLfloat *vertexAttribPointers[3]; |
339 | }; |
340 | |
341 | |
342 | void QOpenGL2PaintEngineExPrivate::uploadData(unsigned int arrayIndex, const GLfloat *data, GLuint count) |
343 | { |
344 | Q_ASSERT(arrayIndex < 3); |
345 | |
346 | // If a vertex array object is created we have a profile that supports them |
347 | // and we will upload the data via a QOpenGLBuffer. Otherwise we will use |
348 | // the legacy way of uploading the data via glVertexAttribPointer. |
349 | if (vao.isCreated()) { |
350 | if (arrayIndex == QT_VERTEX_COORDS_ATTR) { |
351 | vertexBuffer.bind(); |
352 | vertexBuffer.allocate(data, count * sizeof(float)); |
353 | } |
354 | if (arrayIndex == QT_TEXTURE_COORDS_ATTR) { |
355 | texCoordBuffer.bind(); |
356 | texCoordBuffer.allocate(data, count * sizeof(float)); |
357 | } |
358 | if (arrayIndex == QT_OPACITY_ATTR) { |
359 | opacityBuffer.bind(); |
360 | opacityBuffer.allocate(data, count * sizeof(float)); |
361 | } |
362 | if (arrayIndex == QT_OPACITY_ATTR) |
363 | funcs.glVertexAttribPointer(arrayIndex, 1, GL_FLOAT, GL_FALSE, 0, nullptr); |
364 | else |
365 | funcs.glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, nullptr); |
366 | } else { |
367 | // If we already uploaded the data we don't have to do it again |
368 | if (data == vertexAttribPointers[arrayIndex]) |
369 | return; |
370 | |
371 | // Store the data in cache and upload it to the graphics card. |
372 | vertexAttribPointers[arrayIndex] = data; |
373 | if (arrayIndex == QT_OPACITY_ATTR) |
374 | funcs.glVertexAttribPointer(arrayIndex, 1, GL_FLOAT, GL_FALSE, 0, data); |
375 | else |
376 | funcs.glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, data); |
377 | } |
378 | } |
379 | |
380 | bool QOpenGL2PaintEngineExPrivate::uploadIndexData(const void *data, GLenum indexValueType, GLuint count) |
381 | { |
382 | // Follow the uploadData() logic: VBOs are used only when VAO support is available. |
383 | // Otherwise the legacy client-side pointer path is used. |
384 | if (vao.isCreated()) { |
385 | Q_ASSERT(indexValueType == GL_UNSIGNED_SHORT || indexValueType == GL_UNSIGNED_INT); |
386 | indexBuffer.bind(); |
387 | indexBuffer.allocate(data, count * (indexValueType == GL_UNSIGNED_SHORT ? sizeof(quint16) : sizeof(quint32))); |
388 | return true; |
389 | } |
390 | return false; |
391 | } |
392 | |
393 | QT_END_NAMESPACE |
394 | |
395 | #endif |
396 | |