summaryrefslogtreecommitdiffstats
path: root/customcontext/context.cpp
blob: 8b4ab6da768042d531abf97f7ea03a5a72a02b1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Scenegraph Playground module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "context.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QElapsedTimer>

#include <QtGui/QWindow>
#include <QtGui/QOpenGLContext>

#include <QtQuick/QSGFlatColorMaterial>
#include <QtQuick/QSGVertexColorMaterial>
#include <QtQuick/QSGOpaqueTextureMaterial>
#include <QtQuick/QSGTextureMaterial>
#include <private/qsgdefaultimagenode_p.h>
#include <private/qsgdefaultrectanglenode_p.h>
#include <private/qsgdistancefieldglyphnode_p_p.h>
#include <private/qsgdefaultglyphnode_p.h>

#ifdef CUSTOMCONTEXT_ANIMATIONDRIVER
#include "animation/animationdriver.h"
#endif

#ifdef CUSTOMCONTEXT_SWAPLISTENINGANIMATIONDRIVER
#include "animation/swaplisteninganimationdriver.h"
#endif

#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
#include "renderer/overlaprenderer.h"
#endif

#ifdef CUSTOMCONTEXT_MACTEXTURE
#include "mactexture.h"
#endif

#ifdef CUSTOMCONTEXT_NONPRESERVEDTEXTURE
#include "nonpreservedtexture.h"
#endif

#ifdef CUSTOMCONTEXT_EGLGRALLOCTEXTURE
#include "eglgralloctexture.h"
#endif

#ifdef CUSTOMCONTEXT_MSAA
#include <private/qsgdefaultimagenode_p.h>
#include <private/qsgdefaultrectanglenode_p.h>
#endif

#if QT_VERSION >= 0x050200
#define CONTEXT_CLASS RenderContext
#define CONTEXT_CLASS_BASE QSGRenderContext
#else
#define CONTEXT_CLASS Context
#define CONTEXT_CLASS_BASE QSGContext
#endif

#if QT_VERSION >= 0x050200
#ifndef QSG_NO_RENDERER_TIMING
static bool qsg_render_timing = !qgetenv("QSG_RENDER_TIMING").isEmpty();
#endif
#endif

namespace CustomContext
{

#if QT_VERSION >= 0x050200
RenderContext::RenderContext(QSGContext *ctx)
    : QSGRenderContext(ctx)
{
#ifdef CUSTOMCONTEXT_DITHER
    m_dither = qgetenv("CUSTOMCONTEXT_NO_DITHER").isEmpty();
    m_ditherProgram = 0;
    qDebug(" - ordered 2x2 dither: %s", m_dither ? "yes" : "no");
#endif

#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
    m_overlapRenderer = qgetenv("CUSTOMCONTEXT_NO_OVERLAPRENDERER").isEmpty();
    m_clipProgram = 0;
    qDebug(" - overlaprenderer: %s", m_overlapRenderer ? "yes" : "no");
#ifdef CUSTOMCONTEXT_MATERIALPRELOAD
    m_materialPreloading = qgetenv("CUSTOMCONTEXT_NO_MATERIAL_PRELOADING").isEmpty();
    qDebug(" - material preload: %s", m_materialPreloading ? "yes" : "no");
#endif
#endif
}

#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
QSGMaterialShader *RenderContext::prepareMaterial(QSGMaterial *material)
{
    QSGMaterialType *type = material->type();
    QSGMaterialShader *shader = m_materials.value(type);
    if (shader)
        return shader;

#ifndef QSG_NO_RENDER_TIMING
    if (qsg_render_timing)
        qsg_renderer_timer.start();
#endif

    shader = material->createShader();
    compile(shader, material);
    QSGRenderContext::initialize(shader);
    m_materials[type] = shader;

#ifndef QSG_NO_RENDER_TIMING
    if (qsg_render_timing)
        printf("   - compiling material: %dms\n", (int) qsg_renderer_timer.elapsed());
#endif

    return shader;
}
#endif  //CUSTOMCONTEXT_OVERLAPRENDERER
#endif  //QT_VERSION >= 0x050200

Context::Context(QObject *parent)
    : QSGContext(parent)
    , m_sampleCount(0)
    , m_useMultisampling(false)
{
    m_useMultisampling = !qgetenv("CUSTOMCONTEXT_MULTISAMPLE").isEmpty();
    if (m_useMultisampling) {
        m_sampleCount= 16;
        QByteArray overrideSamples = qgetenv("CUSTOMCONTEXT_MULTISAMPLE_COUNT");
        bool ok;
        int override = overrideSamples.toInt(&ok);
        if (ok)
            m_sampleCount = override;
    }

    m_depthBuffer = qgetenv("CUSTOMCONTEXT_NO_DEPTH_BUFFER").isEmpty();

#ifdef CUSTOMCONTEXT_ANIMATIONDRIVER
    m_animationDriver = qgetenv("CUSTOMCONTEXT_NO_ANIMATIONDRIVER").isEmpty();
#endif

#ifdef CUSTOMCONTEXT_SWAPLISTENINGANIMATIONDRIVER
    m_swapListeningAnimationDriver = qgetenv("CUSTOMCONTEXT_NO_SWAPLISTENINGANIMATIONDRIVER").isEmpty();
#endif

#ifdef CUSTOMCONTEXT_ATLASTEXTURE
    m_atlasTexture = qgetenv("CUSTOMCONTEXT_NO_ATLASTEXTURE").isEmpty();
#endif

#ifdef CUSTOMCONTEXT_MACTEXTURE
    m_macTexture = qgetenv("CUSTOMCONTEXT_NO_MACTEXTURE").isEmpty();
#endif

#ifdef CUSTOMCONTEXT_EGLGRALLOCTEXTURE
    m_eglGrallocTexture = qEnvironmentVariableIsEmpty("CUSTOMCONTEXT_NO_EGLGRALLOCTEXTURE");
#endif

#ifdef CUSTOMCONTEXT_THREADUPLOADTEXTURE
    m_threadUploadTexture = qgetenv("CUSTOMCONTEXT_NO_THREADUPLOADTEXTURE").isEmpty();
    connect(this, SIGNAL(invalidated()), &m_threadUploadManager, SLOT(invalidated()), Qt::DirectConnection);
#endif

#ifdef CUSTOMCONTEXT_NONPRESERVEDTEXTURE
    m_nonPreservedTexture = qgetenv("CUSTOMCONTEXT_NO_NONPRESERVEDTEXTURE").isEmpty();
#endif

#ifdef CUSTOMCONTEXT_MSAA
    m_defaultImageNodes = qEnvironmentVariableIsSet("CUSTOMCONTEXT_DEFAULT_IMAGENODES");
    m_defaultRectangleNodes = qEnvironmentVariableIsSet("CUSTOMCONTEXT_DEFAULT_RECTANGLENODES");
#endif

#if QT_VERSION < 0x050200
#ifdef CUSTOMCONTEXT_DITHER
    m_dither = qgetenv("CUSTOMCONTEXT_NO_DITHER").isEmpty();
    m_ditherProgram = 0;
#endif

#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
    m_overlapRenderer = qgetenv("CUSTOMCONTEXT_NO_OVERLAPRENDERER").isEmpty();
    m_clipProgram = 0;
#endif

#ifdef CUSTOMCONTEXT_MATERIALPRELOAD
    m_materialPreloading = qgetenv("CUSTOMCONTEXT_NO_MATERIAL_PRELOADING").isEmpty();
#endif
#endif  //QT_VERSION < 0x050200

#ifdef CUSTOMCONTEXT_DEBUG
    qDebug("CustomContext created:");
    qDebug(" - multisampling: %s, samples=%d", m_useMultisampling ? "yes" : "no", m_sampleCount);
    qDebug(" - depth buffer: %s", m_depthBuffer ? "yes" : "no");

#ifdef PROGRAM_BINARY
    qDebug(" - program binary: yes");
#endif

#ifdef CUSTOMCONTEXT_SWAPLISTENINGANIMATIONDRIVER
    qDebug(" - swap listening animation driver: %s", m_swapListeningAnimationDriver ? "yes" : "no");
#endif
#ifdef CUSTOMCONTEXT_ANIMATIONDRIVER
    qDebug(" - custom animation driver: %s", m_animationDriver ? "yes" : "no");
#endif
#ifdef CUSTOMCONTEXT_ATLASTEXTURE
    qDebug(" - atlas textures: %s", m_atlasTexture ? "yes" : "no" );
#endif
#ifdef CUSTOMCONTEXT_THREADUPLOADTEXTURE
    qDebug(" - threaded texture upload: %s", m_threadUploadTexture ? "yes" : "no");
#endif
#ifdef CUSTOMCONTEXT_EGLGRALLOCTEXTURE
    qDebug(" - EGLImage/Gralloc based texture: %s", m_eglGrallocTexture ? "yes" : "no");
#endif
#ifdef CUSTOMCONTEXT_MACTEXTURE
    qDebug(" - mac textures: %s", m_macTexture ? "yes" : "no");
#endif
#ifdef CUSTOMCONTEXT_NONPRESERVEDTEXTURE
    qDebug(" - non preserved textures: %s", m_nonPreservedTexture ? "yes" : "no");
#endif

#if QT_VERSION < 0x050200
#ifdef CUSTOMCONTEXT_DITHER
    qDebug(" - ordered 2x2 dither: %s", m_dither ? "yes" : "no");
#endif
#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
    qDebug(" - overlaprenderer: %s", m_overlapRenderer ? "yes" : "no");
#endif
#ifdef CUSTOMCONTEXT_MATERIALPRELOAD
    qDebug(" - material preload: %s", m_materialPreloading ? "yes" : "no");
#endif
#endif  //QT_VERSION < 0x050200

#ifdef CUSTOMCONTEXT_NO_DFGLYPHS
    qDebug(" - distance fields disabled");
#endif

#ifdef CUSTOMCONTEXT_MSAA
    qDebug(" - msaa rectangles: %s", !m_defaultRectangleNodes ? "yes" : "no");
    qDebug(" - msaa images: %s", !m_defaultImageNodes ? "yes" : "no");
#endif

#endif

}



void CONTEXT_CLASS::initialize(QOpenGLContext *context)
{

#ifdef CUSTOMCONTEXT_DITHER
    if (m_dither)
        m_ditherProgram = new OrderedDither2x2(context);
#endif

#ifdef CUSTOMCONTEXT_THREADUPLOADTEXTURE
    if (m_threadUploadTexture)
        m_threadUploadManager.initialized(context);
#endif

#ifdef CUSTOMCONTEXT_MATERIALPRELOAD
#ifdef CUSTOMCONTEXT_DEBUG
    QElapsedTimer prepareTimer;
    prepareTimer.start();
#endif
    if (m_materialPreloading) {
        {
            QSGVertexColorMaterial m;
            prepareMaterial(&m);
        }
        {
            QSGFlatColorMaterial m;
            prepareMaterial(&m);
        }
        {
            QSGOpaqueTextureMaterial m;
            prepareMaterial(&m);
        }
        {
            QSGTextureMaterial m;
            prepareMaterial(&m);
        }
        {
            SmoothTextureMaterial m;
            prepareMaterial(&m);
        }
        {
            SmoothColorMaterial m;
            prepareMaterial(&m);
        }
        {
            QSGDistanceFieldTextMaterial m;
            prepareMaterial(&m);
        }
#ifdef CUSTOMCONTEXT_ATLASTEXTURE
        if (m_atlasTexture) {
            m_atlasManager.preload();
        }
#endif
    }
#endif

#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
    if (m_overlapRenderer) {
        m_clipProgram = new QOpenGLShaderProgram();
        m_clipProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,
                                            "attribute highp vec4 vCoord;       \n"
                                            "uniform highp mat4 matrix;         \n"
                                            "void main() {                      \n"
                                            "    gl_Position = matrix * vCoord; \n"
                                            "}");
        m_clipProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,
                                            "void main() {                                   \n"
                                            "    gl_FragColor = vec4(0.81, 0.83, 0.12, 1.0); \n" // Trolltech green ftw!
                                            "}");
        m_clipProgram->bindAttributeLocation("vCoord", 0);
        m_clipProgram->link();
        m_clipMatrixID = m_clipProgram->uniformLocation("matrix");
    }
#endif

#ifdef CUSTOMCONTEXT_DEBUG
    qDebug("CustomContext: initialized..");
#ifdef CUSTOMCONTEXT_MATERIALPRELOAD
    if (m_materialPreloading)
        qDebug(" - Standard materials compiled in: %d ms", (int) prepareTimer.elapsed());
#endif
#if QT_VERSION < 0x050200
    qDebug(" - OpenGL extensions: %s", glGetString(GL_EXTENSIONS));
    qDebug(" - OpenGL Vendor: %s", glGetString(GL_VENDOR));
    qDebug(" - OpenGL Version: %s", glGetString(GL_VERSION));
    qDebug(" - OpenGL Renderer: %s", glGetString(GL_RENDERER));
    qDebug(" - OpenGL Shading Language Version: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
    int textureSize;
    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &textureSize);
    qDebug(" - GL Max Texture Size: %d", textureSize);
#endif
#endif

    CONTEXT_CLASS_BASE::initialize(context);
}

void CONTEXT_CLASS::invalidate()
{
    CONTEXT_CLASS_BASE::invalidate();

#ifdef CUSTOMCONTEXT_DITHER
    delete m_ditherProgram;
    m_ditherProgram = 0;
#endif

#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
    delete m_clipProgram;
    m_clipProgram = 0;
#endif

#ifdef CUSTOMCONTEXT_ATLASTEXTURE
    m_atlasManager.invalidate();
#endif
}


#ifdef CUSTOMCONTEXT_SURFACEFORMAT
QSurfaceFormat Context::defaultSurfaceFormat() const
{
    QSurfaceFormat format;
    format.setStencilBufferSize(8);
    if (m_depthBuffer)
        format.setDepthBufferSize(24);
    if (m_useMultisampling)
        format.setSamples(m_sampleCount);
    return format;
}
#endif



QSGTexture *CONTEXT_CLASS::createTexture(const QImage &image) const
{
#ifdef CUSTOMCONTEXT_ATLASTEXTURE
    if (m_atlasTexture) {
        QSGTexture *t = const_cast<Context *>(this)->m_atlasManager.create(image);
        if (t)
            return t;
    }
#endif

#ifdef CUSTOMCONTEXT_MACTEXTURE
    if (m_macTexture)
        return new MacTexture(image);
#endif

    return CONTEXT_CLASS_BASE::createTexture(image);
}



QSGRenderer *CONTEXT_CLASS::createRenderer()
{
#ifdef CUSTOMCONTEXT_OVERLAPRENDERER
    if (m_overlapRenderer) {
        OverlapRenderer::Renderer *renderer =
                new OverlapRenderer::Renderer(this);
        renderer->setClipProgram(m_clipProgram, m_clipMatrixID);
        return renderer;
    }
#endif
    return CONTEXT_CLASS_BASE::createRenderer();
}



QAnimationDriver *Context::createAnimationDriver(QObject *parent)
{
#ifdef CUSTOMCONTEXT_ANIMATIONDRIVER
    if (m_animationDriver)
        return new AnimationDriver();
#endif
#ifdef CUSTOMCONTEXT_SWAPLISTENINGANIMATIONDRIVER
    if (m_swapListeningAnimationDriver)
        return new SwapListeningAnimationDriver();
#endif

   return QSGContext::createAnimationDriver(parent);
}



QQuickTextureFactory *Context::createTextureFactory(const QImage &image)
{
    Q_UNUSED(image);

#ifdef CUSTOMCONTEXT_EGLGRALLOCTEXTURE
    if (m_eglGrallocTexture) {
        EglGrallocTextureFactory *tf = EglGrallocTextureFactory::create(image);
        if (tf)
            return tf;
    }
#endif

#ifdef CUSTOMCONTEXT_THREADUPLOADTEXTURE
    if (m_threadUploadTexture)
        return m_threadUploadManager.create(image);
#endif

#ifdef CUSTOMCONTEXT_NONPRESERVEDTEXTURE
    if (m_nonPreservedTexture)
        return new NonPreservedTextureFactory(image, this);
#endif

    return 0;
}



void CONTEXT_CLASS::renderNextFrame(QSGRenderer *renderer, GLuint fbo)
{
    CONTEXT_CLASS_BASE::renderNextFrame(renderer, fbo);

#ifdef CUSTOMCONTEXT_DITHER
    if (m_dither) {
        QSurface *s = QOpenGLContext::currentContext()->surface();
        QSize size = static_cast<QWindow *>(s)->size();
        m_ditherProgram->prepare();
        m_ditherProgram->draw(size.width(), size.height());
    }
#endif
}



#ifdef CUSTOMCONTEXT_NO_DFGLYPHS
QSGGlyphNode *Context::createGlyphNode()
{
    return new QSGDefaultGlyphNode();
}
#endif

#ifdef CUSTOMCONTEXT_MSAA

class MSAAImageNode : public QSGDefaultImageNode {
public:
    void setAntialiasing(bool) { }
};

class MSAARectangleNode : public QSGDefaultRectangleNode {
public:
    void setAntialiasing(bool) { }
};

QSGImageNode *Context::createImageNode()
{
    return m_defaultImageNodes ? QSGContext::createImageNode() : new MSAAImageNode();
}

QSGRectangleNode *Context::createRectangleNode()
{
    return m_defaultRectangleNodes ? QSGContext::createRectangleNode() : new MSAARectangleNode();
}
#endif




} // namespace